bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 1 | #include "gm.h" |
| 2 | #include "GrContext.h" |
| 3 | #include "SkColorPriv.h" |
| 4 | #include "SkDevice.h" |
| 5 | |
| 6 | namespace skiagm { |
| 7 | |
| 8 | extern GrContext* GetGr(); |
| 9 | |
| 10 | static const int S = 200; |
| 11 | |
| 12 | class TexDataGM : public GM { |
| 13 | public: |
| 14 | TexDataGM() {} |
| 15 | |
| 16 | protected: |
| 17 | virtual SkString onShortName() { |
| 18 | return SkString("texdata"); |
| 19 | } |
| 20 | |
| 21 | virtual SkISize onISize() { |
| 22 | return make_isize(2*S, 2*S); |
| 23 | } |
| 24 | |
| 25 | void drawBG(SkCanvas* canvas) { |
| 26 | canvas->drawColor(0xff000000); |
| 27 | } |
| 28 | |
| 29 | virtual void onDraw(SkCanvas* canvas) { |
| 30 | drawBG(canvas); |
| 31 | SkDevice* device = canvas->getDevice(); |
| 32 | GrRenderTarget* target = (GrRenderTarget*) device->accessRenderTarget(); |
| 33 | GrContext* ctx = GetGr(); |
| 34 | if (ctx && target) { |
| 35 | SkPMColor gTextureData[(2 * S) * (2 * S)]; |
| 36 | static const int stride = 2 * S; |
| 37 | static const SkPMColor gray = SkPackARGB32(0x40, 0x40, 0x40, 0x40); |
| 38 | static const SkPMColor white = SkPackARGB32(0xff, 0xff, 0xff, 0xff); |
| 39 | static const SkPMColor red = SkPackARGB32(0x80, 0x80, 0x00, 0x00); |
| 40 | static const SkPMColor blue = SkPackARGB32(0x80, 0x00, 0x00, 0x80); |
| 41 | static const SkPMColor green = SkPackARGB32(0x80, 0x00, 0x80, 0x00); |
| 42 | static 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 | GrTextureDesc desc; |
| 74 | desc.fAALevel = kNone_GrAALevel; |
| 75 | // use RT flag bit because in GL it makes the texture be bottom-up |
| 76 | desc.fFlags = i ? kRenderTarget_GrTextureFlagBit : |
| 77 | kNone_GrTextureFlags; |
| 78 | desc.fFormat = kRGBA_8888_GrPixelConfig; |
| 79 | desc.fWidth = 2 * S; |
| 80 | desc.fHeight = 2 * S; |
| 81 | GrTexture* texture = |
| 82 | ctx->createUncachedTexture(desc, gTextureData, 0); |
| 83 | |
| 84 | if (!texture) { |
| 85 | return; |
| 86 | } |
| 87 | GrAutoUnref au(texture); |
| 88 | |
| 89 | ctx->setClip(GrRect::MakeWH(2*S, 2*S)); |
| 90 | ctx->setRenderTarget(target); |
| 91 | |
| 92 | GrPaint paint; |
| 93 | paint.reset(); |
| 94 | paint.fColor = 0xffffffff; |
| 95 | paint.fSrcBlendCoeff = kOne_BlendCoeff; |
| 96 | paint.fDstBlendCoeff = kISA_BlendCoeff; |
| 97 | GrMatrix vm; |
| 98 | if (i) { |
| 99 | vm.setRotate(90, S , S); |
| 100 | } else { |
| 101 | vm.reset(); |
| 102 | } |
| 103 | ctx->setMatrix(vm); |
| 104 | GrMatrix tm; |
| 105 | tm = vm; |
| 106 | tm.postIDiv(2*S, 2*S); |
| 107 | paint.getTextureSampler(0)->setMatrix(tm); |
| 108 | paint.setTexture(0, texture); |
| 109 | |
| 110 | ctx->drawRect(paint, GrRect::MakeWH(2*S, 2*S)); |
| 111 | |
| 112 | // now update the lower right of the texture in first pass |
| 113 | // or upper right in second pass |
| 114 | offset = 0; |
| 115 | for (int y = 0; y < S; ++y) { |
| 116 | for (int x = 0; x < S; ++x) { |
| 117 | gTextureData[offset + y * stride + x] = |
| 118 | ((x + y) % 2) ? (i ? green : red) : blue; |
| 119 | } |
| 120 | } |
| 121 | // BUG: uploadTextureData doesn't force a flush |
| 122 | ctx->flush(); |
| 123 | texture->uploadTextureData(S, i ? 0 : S, S, S, gTextureData, 4 * stride); |
| 124 | ctx->drawRect(paint, GrRect::MakeWH(2*S, 2*S)); |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | private: |
| 130 | typedef GM INHERITED; |
| 131 | }; |
| 132 | |
| 133 | ////////////////////////////////////////////////////////////////////////////// |
| 134 | |
| 135 | static GM* MyFactory(void*) { return new TexDataGM; } |
| 136 | static GMRegistry reg(MyFactory); |
| 137 | |
| 138 | } |
| 139 | |