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 | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 8 | |
| 9 | // This test only works with the GPU backend. |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 10 | |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 11 | #include "gm.h" |
bsalomon@google.com | a68937c | 2012-08-03 15:00:52 +0000 | [diff] [blame] | 12 | |
| 13 | #if SK_SUPPORT_GPU |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 14 | #include "GrContext.h" |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 15 | #include "effects/GrSimpleTextureEffect.h" |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 16 | #include "SkColorPriv.h" |
| 17 | #include "SkDevice.h" |
| 18 | |
| 19 | namespace skiagm { |
| 20 | |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 21 | static const int S = 200; |
| 22 | |
| 23 | class TexDataGM : public GM { |
| 24 | public: |
bsalomon@google.com | 48dd1a2 | 2011-10-31 14:18:20 +0000 | [diff] [blame] | 25 | TexDataGM() { |
| 26 | this->setBGColor(0xff000000); |
| 27 | } |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 28 | |
| 29 | protected: |
| 30 | virtual SkString onShortName() { |
| 31 | return SkString("texdata"); |
| 32 | } |
| 33 | |
| 34 | virtual SkISize onISize() { |
| 35 | return make_isize(2*S, 2*S); |
| 36 | } |
| 37 | |
commit-bot@chromium.org | 1d5bbb2 | 2013-10-14 14:15:28 +0000 | [diff] [blame] | 38 | virtual uint32_t onGetFlags() const SK_OVERRIDE { return kGPUOnly_Flag; } |
| 39 | |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 40 | virtual void onDraw(SkCanvas* canvas) { |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 41 | SkBaseDevice* device = canvas->getTopDevice(); |
commit-bot@chromium.org | b8d00db | 2013-06-26 19:18:23 +0000 | [diff] [blame] | 42 | GrRenderTarget* target = device->accessRenderTarget(); |
mtklein@google.com | 62b50b7 | 2013-09-16 20:42:15 +0000 | [diff] [blame] | 43 | GrContext* ctx = GM::GetGr(canvas); |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 44 | if (ctx && target) { |
commit-bot@chromium.org | beede90 | 2013-10-16 11:50:50 +0000 | [diff] [blame] | 45 | SkAutoTArray<SkPMColor> gTextureData((2 * S) * (2 * S)); |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 46 | static const int stride = 2 * S; |
| 47 | static const SkPMColor gray = SkPackARGB32(0x40, 0x40, 0x40, 0x40); |
| 48 | static const SkPMColor white = SkPackARGB32(0xff, 0xff, 0xff, 0xff); |
| 49 | static const SkPMColor red = SkPackARGB32(0x80, 0x80, 0x00, 0x00); |
| 50 | static const SkPMColor blue = SkPackARGB32(0x80, 0x00, 0x00, 0x80); |
| 51 | static const SkPMColor green = SkPackARGB32(0x80, 0x00, 0x80, 0x00); |
| 52 | static const SkPMColor black = SkPackARGB32(0x00, 0x00, 0x00, 0x00); |
| 53 | for (int i = 0; i < 2; ++i) { |
| 54 | int offset = 0; |
| 55 | // fill upper-left |
| 56 | for (int y = 0; y < S; ++y) { |
| 57 | for (int x = 0; x < S; ++x) { |
| 58 | gTextureData[offset + y * stride + x] = gray; |
| 59 | } |
| 60 | } |
| 61 | // fill upper-right |
| 62 | offset = S; |
| 63 | for (int y = 0; y < S; ++y) { |
| 64 | for (int x = 0; x < S; ++x) { |
| 65 | gTextureData[offset + y * stride + x] = white; |
| 66 | } |
| 67 | } |
| 68 | // fill lower left |
| 69 | offset = S * stride; |
| 70 | for (int y = 0; y < S; ++y) { |
| 71 | for (int x = 0; x < S; ++x) { |
| 72 | gTextureData[offset + y * stride + x] = black; |
| 73 | } |
| 74 | } |
| 75 | // fill lower right |
| 76 | offset = S * stride + S; |
| 77 | for (int y = 0; y < S; ++y) { |
| 78 | for (int x = 0; x < S; ++x) { |
| 79 | gTextureData[offset + y * stride + x] = gray; |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | GrTextureDesc desc; |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 84 | // use RT flag bit because in GL it makes the texture be bottom-up |
| 85 | desc.fFlags = i ? kRenderTarget_GrTextureFlagBit : |
| 86 | kNone_GrTextureFlags; |
bsalomon@google.com | fec0bc3 | 2013-02-07 14:43:04 +0000 | [diff] [blame] | 87 | desc.fConfig = kSkia8888_GrPixelConfig; |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 88 | desc.fWidth = 2 * S; |
| 89 | desc.fHeight = 2 * S; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 90 | GrTexture* texture = |
commit-bot@chromium.org | beede90 | 2013-10-16 11:50:50 +0000 | [diff] [blame] | 91 | ctx->createUncachedTexture(desc, gTextureData.get(), 0); |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 92 | |
| 93 | if (!texture) { |
| 94 | return; |
| 95 | } |
commit-bot@chromium.org | a4de8c2 | 2013-09-09 13:38:37 +0000 | [diff] [blame] | 96 | SkAutoUnref au(texture); |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 97 | |
commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame] | 98 | GrContext::AutoClip acs(ctx, SkRect::MakeWH(2*S, 2*S)); |
robertphillips@google.com | 3e11c0b | 2012-07-11 18:20:35 +0000 | [diff] [blame] | 99 | |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 100 | ctx->setRenderTarget(target); |
| 101 | |
| 102 | GrPaint paint; |
bsalomon@google.com | c7448ce | 2012-10-05 19:04:13 +0000 | [diff] [blame] | 103 | paint.setBlendFunc(kOne_GrBlendCoeff, kISA_GrBlendCoeff); |
bsalomon@google.com | b9086a0 | 2012-11-01 18:02:54 +0000 | [diff] [blame] | 104 | SkMatrix vm; |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 105 | if (i) { |
bsalomon@google.com | 9d12f5c | 2011-09-29 18:08:18 +0000 | [diff] [blame] | 106 | vm.setRotate(90 * SK_Scalar1, |
| 107 | S * SK_Scalar1, |
| 108 | S * SK_Scalar1); |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 109 | } else { |
| 110 | vm.reset(); |
| 111 | } |
| 112 | ctx->setMatrix(vm); |
bsalomon@google.com | b9086a0 | 2012-11-01 18:02:54 +0000 | [diff] [blame] | 113 | SkMatrix tm; |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 114 | tm = vm; |
bsalomon@google.com | dfdb7e5 | 2012-10-16 15:19:45 +0000 | [diff] [blame] | 115 | tm.postIDiv(2*S, 2*S); |
commit-bot@chromium.org | 42dacab | 2013-07-13 17:24:24 +0000 | [diff] [blame] | 116 | paint.addColorTextureEffect(texture, tm); |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 117 | |
commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame] | 118 | ctx->drawRect(paint, SkRect::MakeWH(2*S, 2*S)); |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 119 | |
| 120 | // now update the lower right of the texture in first pass |
| 121 | // or upper right in second pass |
| 122 | offset = 0; |
| 123 | for (int y = 0; y < S; ++y) { |
| 124 | for (int x = 0; x < S; ++x) { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 125 | gTextureData[offset + y * stride + x] = |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 126 | ((x + y) % 2) ? (i ? green : red) : blue; |
| 127 | } |
| 128 | } |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 129 | texture->writePixels(S, (i ? 0 : S), S, S, |
commit-bot@chromium.org | beede90 | 2013-10-16 11:50:50 +0000 | [diff] [blame] | 130 | texture->config(), gTextureData.get(), |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 131 | 4 * stride); |
commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame] | 132 | ctx->drawRect(paint, SkRect::MakeWH(2*S, 2*S)); |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 133 | } |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | private: |
| 138 | typedef GM INHERITED; |
| 139 | }; |
| 140 | |
| 141 | ////////////////////////////////////////////////////////////////////////////// |
| 142 | |
| 143 | static GM* MyFactory(void*) { return new TexDataGM; } |
| 144 | static GMRegistry reg(MyFactory); |
| 145 | |
| 146 | } |
| 147 | |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 148 | #endif |