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