texdata.cpp revision 96fcdcc219d2a0d3579719b84b28bede76efba64
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 20namespace skiagm { 21 22static const int S = 200; 23 24class TexDataGM : public GM { 25public: 26 TexDataGM() { 27 this->setBGColor(0xff000000); 28 } 29 30protected: 31 SkString onShortName() override { 32 return SkString("texdata"); 33 } 34 35 SkISize onISize() override { 36 return SkISize::Make(2*S, 2*S); 37 } 38 39 void onDraw(SkCanvas* canvas) override { 40 GrRenderTarget* target = canvas->internal_private_accessTopLayerRenderTarget(); 41 GrContext* ctx = canvas->getGrContext(); 42 GrDrawContext* drawContext = ctx ? ctx->drawContext() : nullptr; 43 if (drawContext && target) { 44 SkAutoTArray<SkPMColor> gTextureData((2 * S) * (2 * S)); 45 static const int stride = 2 * S; 46 static const SkPMColor gray = SkPackARGB32(0x40, 0x40, 0x40, 0x40); 47 static const SkPMColor white = SkPackARGB32(0xff, 0xff, 0xff, 0xff); 48 static const SkPMColor red = SkPackARGB32(0x80, 0x80, 0x00, 0x00); 49 static const SkPMColor blue = SkPackARGB32(0x80, 0x00, 0x00, 0x80); 50 static const SkPMColor green = SkPackARGB32(0x80, 0x00, 0x80, 0x00); 51 static const SkPMColor black = SkPackARGB32(0x00, 0x00, 0x00, 0x00); 52 for (int i = 0; i < 2; ++i) { 53 int offset = 0; 54 // fill upper-left 55 for (int y = 0; y < S; ++y) { 56 for (int x = 0; x < S; ++x) { 57 gTextureData[offset + y * stride + x] = gray; 58 } 59 } 60 // fill upper-right 61 offset = S; 62 for (int y = 0; y < S; ++y) { 63 for (int x = 0; x < S; ++x) { 64 gTextureData[offset + y * stride + x] = white; 65 } 66 } 67 // fill lower left 68 offset = S * stride; 69 for (int y = 0; y < S; ++y) { 70 for (int x = 0; x < S; ++x) { 71 gTextureData[offset + y * stride + x] = black; 72 } 73 } 74 // fill lower right 75 offset = S * stride + S; 76 for (int y = 0; y < S; ++y) { 77 for (int x = 0; x < S; ++x) { 78 gTextureData[offset + y * stride + x] = gray; 79 } 80 } 81 82 GrSurfaceDesc desc; 83 // use RT flag bit because in GL it makes the texture be bottom-up 84 desc.fFlags = i ? kRenderTarget_GrSurfaceFlag : 85 kNone_GrSurfaceFlags; 86 desc.fConfig = kSkia8888_GrPixelConfig; 87 desc.fWidth = 2 * S; 88 desc.fHeight = 2 * S; 89 GrTexture* texture = ctx->textureProvider()->createTexture( 90 desc, false, gTextureData.get(), 0); 91 92 if (!texture) { 93 return; 94 } 95 SkAutoTUnref<GrTexture> au(texture); 96 97 // setup new clip 98 GrClip clip(SkRect::MakeWH(2*S, 2*S)); 99 100 GrPaint paint; 101 paint.setPorterDuffXPFactory(SkXfermode::kSrcOver_Mode); 102 103 SkMatrix vm; 104 if (i) { 105 vm.setRotate(90 * SK_Scalar1, 106 S * SK_Scalar1, 107 S * SK_Scalar1); 108 } else { 109 vm.reset(); 110 } 111 SkMatrix tm; 112 tm = vm; 113 tm.postIDiv(2*S, 2*S); 114 paint.addColorTextureProcessor(texture, tm); 115 116 drawContext->drawRect(target, clip, paint, vm, SkRect::MakeWH(2*S, 2*S)); 117 118 // now update the lower right of the texture in first pass 119 // or upper right in second pass 120 offset = 0; 121 for (int y = 0; y < S; ++y) { 122 for (int x = 0; x < S; ++x) { 123 gTextureData[offset + y * stride + x] = 124 ((x + y) % 2) ? (i ? green : red) : blue; 125 } 126 } 127 texture->writePixels(S, (i ? 0 : S), S, S, 128 texture->config(), gTextureData.get(), 129 4 * stride); 130 drawContext->drawRect(target, clip, paint, vm, SkRect::MakeWH(2*S, 2*S)); 131 } 132 } else { 133 this->drawGpuOnlyMessage(canvas); 134 } 135 } 136 137private: 138 typedef GM INHERITED; 139}; 140 141////////////////////////////////////////////////////////////////////////////// 142 143static GM* MyFactory(void*) { return new TexDataGM; } 144static GMRegistry reg(MyFactory); 145 146} 147 148#endif 149