epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 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 | */ |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 8 | #include "gm.h" |
| 9 | #include "GrContext.h" |
| 10 | #include "SkColorPriv.h" |
| 11 | #include "SkDevice.h" |
| 12 | |
| 13 | namespace skiagm { |
| 14 | |
| 15 | extern GrContext* GetGr(); |
| 16 | |
| 17 | static const int S = 200; |
| 18 | |
| 19 | class TexDataGM : public GM { |
| 20 | public: |
bsalomon@google.com | 48dd1a2 | 2011-10-31 14:18:20 +0000 | [diff] [blame] | 21 | TexDataGM() { |
| 22 | this->setBGColor(0xff000000); |
| 23 | } |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 24 | |
| 25 | protected: |
| 26 | virtual SkString onShortName() { |
| 27 | return SkString("texdata"); |
| 28 | } |
| 29 | |
| 30 | virtual SkISize onISize() { |
| 31 | return make_isize(2*S, 2*S); |
| 32 | } |
| 33 | |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 34 | virtual void onDraw(SkCanvas* canvas) { |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 35 | SkDevice* device = canvas->getDevice(); |
| 36 | GrRenderTarget* target = (GrRenderTarget*) device->accessRenderTarget(); |
| 37 | GrContext* ctx = GetGr(); |
| 38 | if (ctx && target) { |
| 39 | SkPMColor gTextureData[(2 * S) * (2 * S)]; |
| 40 | static const int stride = 2 * S; |
| 41 | static const SkPMColor gray = SkPackARGB32(0x40, 0x40, 0x40, 0x40); |
| 42 | static const SkPMColor white = SkPackARGB32(0xff, 0xff, 0xff, 0xff); |
| 43 | static const SkPMColor red = SkPackARGB32(0x80, 0x80, 0x00, 0x00); |
| 44 | static const SkPMColor blue = SkPackARGB32(0x80, 0x00, 0x00, 0x80); |
| 45 | static const SkPMColor green = SkPackARGB32(0x80, 0x00, 0x80, 0x00); |
| 46 | static const SkPMColor black = SkPackARGB32(0x00, 0x00, 0x00, 0x00); |
| 47 | for (int i = 0; i < 2; ++i) { |
| 48 | int offset = 0; |
| 49 | // fill upper-left |
| 50 | for (int y = 0; y < S; ++y) { |
| 51 | for (int x = 0; x < S; ++x) { |
| 52 | gTextureData[offset + y * stride + x] = gray; |
| 53 | } |
| 54 | } |
| 55 | // fill upper-right |
| 56 | offset = S; |
| 57 | for (int y = 0; y < S; ++y) { |
| 58 | for (int x = 0; x < S; ++x) { |
| 59 | gTextureData[offset + y * stride + x] = white; |
| 60 | } |
| 61 | } |
| 62 | // fill lower left |
| 63 | offset = S * stride; |
| 64 | for (int y = 0; y < S; ++y) { |
| 65 | for (int x = 0; x < S; ++x) { |
| 66 | gTextureData[offset + y * stride + x] = black; |
| 67 | } |
| 68 | } |
| 69 | // fill lower right |
| 70 | offset = S * stride + S; |
| 71 | for (int y = 0; y < S; ++y) { |
| 72 | for (int x = 0; x < S; ++x) { |
| 73 | gTextureData[offset + y * stride + x] = gray; |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | GrTextureDesc desc; |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 78 | // use RT flag bit because in GL it makes the texture be bottom-up |
| 79 | desc.fFlags = i ? kRenderTarget_GrTextureFlagBit : |
| 80 | kNone_GrTextureFlags; |
bsalomon@google.com | 5bc34f0 | 2011-12-06 14:46:34 +0000 | [diff] [blame] | 81 | desc.fConfig = kSkia8888_PM_GrPixelConfig; |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 82 | desc.fWidth = 2 * S; |
| 83 | desc.fHeight = 2 * S; |
bsalomon@google.com | 78d6cf9 | 2012-01-30 18:09:31 +0000 | [diff] [blame] | 84 | desc.fSampleCnt = 0; |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 85 | GrTexture* texture = |
| 86 | ctx->createUncachedTexture(desc, gTextureData, 0); |
| 87 | |
| 88 | if (!texture) { |
| 89 | return; |
| 90 | } |
| 91 | GrAutoUnref au(texture); |
| 92 | |
| 93 | ctx->setClip(GrRect::MakeWH(2*S, 2*S)); |
| 94 | ctx->setRenderTarget(target); |
| 95 | |
| 96 | GrPaint paint; |
| 97 | paint.reset(); |
| 98 | paint.fColor = 0xffffffff; |
| 99 | paint.fSrcBlendCoeff = kOne_BlendCoeff; |
| 100 | paint.fDstBlendCoeff = kISA_BlendCoeff; |
| 101 | GrMatrix vm; |
| 102 | if (i) { |
bsalomon@google.com | 9d12f5c | 2011-09-29 18:08:18 +0000 | [diff] [blame] | 103 | vm.setRotate(90 * SK_Scalar1, |
| 104 | S * SK_Scalar1, |
| 105 | S * SK_Scalar1); |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 106 | } else { |
| 107 | vm.reset(); |
| 108 | } |
| 109 | ctx->setMatrix(vm); |
| 110 | GrMatrix tm; |
| 111 | tm = vm; |
bsalomon@google.com | aa814fe | 2011-12-12 18:45:07 +0000 | [diff] [blame] | 112 | GrMatrix* sampleMat = paint.textureSampler(0)->matrix(); |
| 113 | *sampleMat = vm; |
| 114 | sampleMat->postIDiv(2*S, 2*S); |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 115 | paint.setTexture(0, texture); |
| 116 | |
| 117 | ctx->drawRect(paint, GrRect::MakeWH(2*S, 2*S)); |
| 118 | |
| 119 | // now update the lower right of the texture in first pass |
| 120 | // or upper right in second pass |
| 121 | offset = 0; |
| 122 | for (int y = 0; y < S; ++y) { |
| 123 | for (int x = 0; x < S; ++x) { |
| 124 | gTextureData[offset + y * stride + x] = |
| 125 | ((x + y) % 2) ? (i ? green : red) : blue; |
| 126 | } |
| 127 | } |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 128 | texture->writePixels(S, (i ? 0 : S), S, S, |
| 129 | texture->config(), gTextureData, |
| 130 | 4 * stride); |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 131 | ctx->drawRect(paint, GrRect::MakeWH(2*S, 2*S)); |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | private: |
| 137 | typedef GM INHERITED; |
| 138 | }; |
| 139 | |
| 140 | ////////////////////////////////////////////////////////////////////////////// |
| 141 | |
| 142 | static GM* MyFactory(void*) { return new TexDataGM; } |
| 143 | static GMRegistry reg(MyFactory); |
| 144 | |
| 145 | } |
| 146 | |