texdata.cpp revision dbfd7ab10883f173f5c1b653a233e18dc6142002
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 "GrDrawContext.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 GrDrawContext* drawContext = canvas->internal_private_accessTopLayerDrawContext(); 24 if (!drawContext) { 25 skiagm::GM::DrawGpuOnlyMessage(canvas); 26 return; 27 } 28 29 GrContext* context = canvas->getGrContext(); 30 if (!context) { 31 return; 32 } 33 34 SkAutoTArray<SkPMColor> gTextureData((2 * S) * (2 * S)); 35 constexpr int stride = 2 * S; 36 const SkPMColor gray = SkPackARGB32(0x40, 0x40, 0x40, 0x40); 37 const SkPMColor white = SkPackARGB32(0xff, 0xff, 0xff, 0xff); 38 const SkPMColor red = SkPackARGB32(0x80, 0x80, 0x00, 0x00); 39 const SkPMColor blue = SkPackARGB32(0x80, 0x00, 0x00, 0x80); 40 const SkPMColor green = SkPackARGB32(0x80, 0x00, 0x80, 0x00); 41 const SkPMColor black = SkPackARGB32(0x00, 0x00, 0x00, 0x00); 42 for (int i = 0; i < 2; ++i) { 43 int offset = 0; 44 // fill upper-left 45 for (int y = 0; y < S; ++y) { 46 for (int x = 0; x < S; ++x) { 47 gTextureData[offset + y * stride + x] = gray; 48 } 49 } 50 // fill upper-right 51 offset = S; 52 for (int y = 0; y < S; ++y) { 53 for (int x = 0; x < S; ++x) { 54 gTextureData[offset + y * stride + x] = white; 55 } 56 } 57 // fill lower left 58 offset = S * stride; 59 for (int y = 0; y < S; ++y) { 60 for (int x = 0; x < S; ++x) { 61 gTextureData[offset + y * stride + x] = black; 62 } 63 } 64 // fill lower right 65 offset = S * stride + S; 66 for (int y = 0; y < S; ++y) { 67 for (int x = 0; x < S; ++x) { 68 gTextureData[offset + y * stride + x] = gray; 69 } 70 } 71 72 GrSurfaceDesc desc; 73 desc.fOrigin = i ? kBottomLeft_GrSurfaceOrigin : kTopLeft_GrSurfaceOrigin; 74 desc.fConfig = kSkia8888_GrPixelConfig; 75 desc.fWidth = 2 * S; 76 desc.fHeight = 2 * S; 77 GrTexture* texture = context->textureProvider()->createTexture( 78 desc, SkBudgeted::kNo, gTextureData.get(), 0); 79 80 if (!texture) { 81 return; 82 } 83 SkAutoTUnref<GrTexture> au(texture); 84 85 // setup new clip 86 GrFixedClip clip(SkIRect::MakeWH(2*S, 2*S)); 87 88 GrPaint paint; 89 paint.setPorterDuffXPFactory(SkXfermode::kSrcOver_Mode); 90 91 SkMatrix vm; 92 if (i) { 93 vm.setRotate(90 * SK_Scalar1, 94 S * SK_Scalar1, 95 S * SK_Scalar1); 96 } else { 97 vm.reset(); 98 } 99 SkMatrix tm; 100 tm = vm; 101 tm.postIDiv(2*S, 2*S); 102 paint.addColorTextureProcessor(texture, nullptr, tm); 103 104 drawContext->drawRect(clip, paint, vm, SkRect::MakeWH(2*S, 2*S)); 105 106 // now update the lower right of the texture in first pass 107 // or upper right in second pass 108 offset = 0; 109 for (int y = 0; y < S; ++y) { 110 for (int x = 0; x < S; ++x) { 111 gTextureData[offset + y * stride + x] = 112 ((x + y) % 2) ? (i ? green : red) : blue; 113 } 114 } 115 texture->writePixels(S, (i ? 0 : S), S, S, 116 texture->config(), gTextureData.get(), 117 4 * stride); 118 drawContext->drawRect(clip, paint, vm, SkRect::MakeWH(2*S, 2*S)); 119 } 120} 121#endif 122 123