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