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