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; |
| 78 | desc.fAALevel = kNone_GrAALevel; |
| 79 | // use RT flag bit because in GL it makes the texture be bottom-up |
| 80 | desc.fFlags = i ? kRenderTarget_GrTextureFlagBit : |
| 81 | kNone_GrTextureFlags; |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 82 | desc.fFormat = kSkia8888_PM_GrPixelConfig; |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 83 | desc.fWidth = 2 * S; |
| 84 | desc.fHeight = 2 * S; |
| 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; |
| 112 | tm.postIDiv(2*S, 2*S); |
| 113 | paint.getTextureSampler(0)->setMatrix(tm); |
| 114 | paint.setTexture(0, texture); |
| 115 | |
| 116 | ctx->drawRect(paint, GrRect::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 | // BUG: uploadTextureData doesn't force a flush |
| 128 | ctx->flush(); |
| 129 | texture->uploadTextureData(S, i ? 0 : S, S, S, gTextureData, 4 * stride); |
| 130 | ctx->drawRect(paint, GrRect::MakeWH(2*S, 2*S)); |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | private: |
| 136 | typedef GM INHERITED; |
| 137 | }; |
| 138 | |
| 139 | ////////////////////////////////////////////////////////////////////////////// |
| 140 | |
| 141 | static GM* MyFactory(void*) { return new TexDataGM; } |
| 142 | static GMRegistry reg(MyFactory); |
| 143 | |
| 144 | } |
| 145 | |