texdata.cpp revision cefc43112c8f6fe3702facb89447bdfcc2715345
1/* 2 * Copyright 2011 Google Inc. 3 * 4 * Use of this source code is governed by a BSD-style license that can be 5 * found in the LICENSE file. 6 */ 7 8// This test only works with the GPU backend. 9 10#include "gm.h" 11 12#if SK_SUPPORT_GPU 13#include "GrContext.h" 14#include "GrRenderTargetContext.h" 15#include "GrFixedClip.h" 16#include "SkColorPriv.h" 17#include "effects/GrPorterDuffXferProcessor.h" 18#include "effects/GrSimpleTextureEffect.h" 19 20constexpr int S = 200; 21 22DEF_SIMPLE_GM_BG(texdata, canvas, 2 * S, 2 * S, SK_ColorBLACK) { 23 GrRenderTargetContext* renderTargetContext = 24 canvas->internal_private_accessTopLayerRenderTargetContext(); 25 if (!renderTargetContext) { 26 skiagm::GM::DrawGpuOnlyMessage(canvas); 27 return; 28 } 29 30 GrContext* context = canvas->getGrContext(); 31 if (!context) { 32 return; 33 } 34 35 SkAutoTArray<SkPMColor> gTextureData((2 * S) * (2 * S)); 36 constexpr int stride = 2 * S; 37 const SkPMColor gray = SkPackARGB32(0x40, 0x40, 0x40, 0x40); 38 const SkPMColor white = SkPackARGB32(0xff, 0xff, 0xff, 0xff); 39 const SkPMColor red = SkPackARGB32(0x80, 0x80, 0x00, 0x00); 40 const SkPMColor blue = SkPackARGB32(0x80, 0x00, 0x00, 0x80); 41 const SkPMColor green = SkPackARGB32(0x80, 0x00, 0x80, 0x00); 42 const SkPMColor black = SkPackARGB32(0x00, 0x00, 0x00, 0x00); 43 for (int i = 0; i < 2; ++i) { 44 int offset = 0; 45 // fill upper-left 46 for (int y = 0; y < S; ++y) { 47 for (int x = 0; x < S; ++x) { 48 gTextureData[offset + y * stride + x] = gray; 49 } 50 } 51 // fill upper-right 52 offset = S; 53 for (int y = 0; y < S; ++y) { 54 for (int x = 0; x < S; ++x) { 55 gTextureData[offset + y * stride + x] = white; 56 } 57 } 58 // fill lower left 59 offset = S * stride; 60 for (int y = 0; y < S; ++y) { 61 for (int x = 0; x < S; ++x) { 62 gTextureData[offset + y * stride + x] = black; 63 } 64 } 65 // fill lower right 66 offset = S * stride + S; 67 for (int y = 0; y < S; ++y) { 68 for (int x = 0; x < S; ++x) { 69 gTextureData[offset + y * stride + x] = gray; 70 } 71 } 72 73 GrSurfaceDesc desc; 74 desc.fOrigin = i ? kBottomLeft_GrSurfaceOrigin : kTopLeft_GrSurfaceOrigin; 75 desc.fConfig = kSkia8888_GrPixelConfig; 76 desc.fWidth = 2 * S; 77 desc.fHeight = 2 * S; 78 GrTexture* texture = context->textureProvider()->createTexture( 79 desc, SkBudgeted::kNo, gTextureData.get(), 0); 80 81 if (!texture) { 82 return; 83 } 84 sk_sp<GrTexture> au(texture); 85 86 // setup new clip 87 GrFixedClip clip(SkIRect::MakeWH(2*S, 2*S)); 88 89 GrPaint paint; 90 paint.setPorterDuffXPFactory(SkBlendMode::kSrcOver); 91 92 SkMatrix vm; 93 if (i) { 94 vm.setRotate(90 * SK_Scalar1, 95 S * SK_Scalar1, 96 S * SK_Scalar1); 97 } else { 98 vm.reset(); 99 } 100 SkMatrix tm; 101 tm = vm; 102 tm.postIDiv(2*S, 2*S); 103 paint.addColorTextureProcessor(texture, nullptr, tm); 104 105 renderTargetContext->drawRect(clip, paint, vm, SkRect::MakeWH(2*S, 2*S)); 106 107 // now update the lower right of the texture in first pass 108 // or upper right in second pass 109 offset = 0; 110 for (int y = 0; y < S; ++y) { 111 for (int x = 0; x < S; ++x) { 112 gTextureData[offset + y * stride + x] = 113 ((x + y) % 2) ? (i ? green : red) : blue; 114 } 115 } 116 texture->writePixels(S, (i ? 0 : S), S, S, 117 texture->config(), gTextureData.get(), 118 4 * stride); 119 renderTargetContext->drawRect(clip, paint, vm, SkRect::MakeWH(2*S, 2*S)); 120 } 121} 122#endif 123 124