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" |
egdaniel | 8d95ffa | 2014-12-08 13:26:43 -0800 | [diff] [blame] | 15 | #include "SkColorPriv.h" |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 16 | #include "effects/GrPorterDuffXferProcessor.h" |
| 17 | #include "effects/GrSimpleTextureEffect.h" |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 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: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 30 | SkString onShortName() override { |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 31 | return SkString("texdata"); |
| 32 | } |
| 33 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 34 | SkISize onISize() override { |
tfarina | f539318 | 2014-06-09 23:59:03 -0700 | [diff] [blame] | 35 | return SkISize::Make(2*S, 2*S); |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 36 | } |
| 37 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 38 | void onDraw(SkCanvas* canvas) override { |
reed@google.com | 9c135db | 2014-03-12 18:28:35 +0000 | [diff] [blame] | 39 | GrRenderTarget* target = canvas->internal_private_accessTopLayerRenderTarget(); |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 40 | GrContext* ctx = canvas->getGrContext(); |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 41 | if (ctx && target) { |
commit-bot@chromium.org | beede90 | 2013-10-16 11:50:50 +0000 | [diff] [blame] | 42 | SkAutoTArray<SkPMColor> gTextureData((2 * S) * (2 * S)); |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 43 | static const int stride = 2 * S; |
| 44 | static const SkPMColor gray = SkPackARGB32(0x40, 0x40, 0x40, 0x40); |
| 45 | static const SkPMColor white = SkPackARGB32(0xff, 0xff, 0xff, 0xff); |
| 46 | static const SkPMColor red = SkPackARGB32(0x80, 0x80, 0x00, 0x00); |
| 47 | static const SkPMColor blue = SkPackARGB32(0x80, 0x00, 0x00, 0x80); |
| 48 | static const SkPMColor green = SkPackARGB32(0x80, 0x00, 0x80, 0x00); |
| 49 | static const SkPMColor black = SkPackARGB32(0x00, 0x00, 0x00, 0x00); |
| 50 | for (int i = 0; i < 2; ++i) { |
| 51 | int offset = 0; |
| 52 | // fill upper-left |
| 53 | for (int y = 0; y < S; ++y) { |
| 54 | for (int x = 0; x < S; ++x) { |
| 55 | gTextureData[offset + y * stride + x] = gray; |
| 56 | } |
| 57 | } |
| 58 | // fill upper-right |
| 59 | offset = S; |
| 60 | for (int y = 0; y < S; ++y) { |
| 61 | for (int x = 0; x < S; ++x) { |
| 62 | gTextureData[offset + y * stride + x] = white; |
| 63 | } |
| 64 | } |
| 65 | // fill lower left |
| 66 | offset = S * stride; |
| 67 | for (int y = 0; y < S; ++y) { |
| 68 | for (int x = 0; x < S; ++x) { |
| 69 | gTextureData[offset + y * stride + x] = black; |
| 70 | } |
| 71 | } |
| 72 | // fill lower right |
| 73 | offset = S * stride + S; |
| 74 | for (int y = 0; y < S; ++y) { |
| 75 | for (int x = 0; x < S; ++x) { |
| 76 | gTextureData[offset + y * stride + x] = gray; |
| 77 | } |
| 78 | } |
| 79 | |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 80 | GrSurfaceDesc desc; |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 81 | // use RT flag bit because in GL it makes the texture be bottom-up |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 82 | desc.fFlags = i ? kRenderTarget_GrSurfaceFlag : |
| 83 | kNone_GrSurfaceFlags; |
bsalomon@google.com | fec0bc3 | 2013-02-07 14:43:04 +0000 | [diff] [blame] | 84 | desc.fConfig = kSkia8888_GrPixelConfig; |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 85 | desc.fWidth = 2 * S; |
| 86 | desc.fHeight = 2 * S; |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 87 | GrTexture* texture = ctx->textureProvider()->createTexture( |
| 88 | desc, false, gTextureData.get(), 0); |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 89 | |
| 90 | if (!texture) { |
| 91 | return; |
| 92 | } |
bsalomon | dcabb05 | 2014-07-21 14:24:01 -0700 | [diff] [blame] | 93 | SkAutoTUnref<GrTexture> au(texture); |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 94 | |
joshualitt | 570d2f8 | 2015-02-25 13:19:48 -0800 | [diff] [blame] | 95 | // setup new clip |
| 96 | GrClip clip(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 | GrPaint paint; |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 99 | paint.setPorterDuffXPFactory(SkXfermode::kSrcOver_Mode); |
| 100 | |
bsalomon@google.com | b9086a0 | 2012-11-01 18:02:54 +0000 | [diff] [blame] | 101 | SkMatrix vm; |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 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 | } |
bsalomon@google.com | b9086a0 | 2012-11-01 18:02:54 +0000 | [diff] [blame] | 109 | SkMatrix tm; |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 110 | tm = vm; |
bsalomon@google.com | dfdb7e5 | 2012-10-16 15:19:45 +0000 | [diff] [blame] | 111 | tm.postIDiv(2*S, 2*S); |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 112 | paint.addColorTextureProcessor(texture, tm); |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 113 | |
joshualitt | 570d2f8 | 2015-02-25 13:19:48 -0800 | [diff] [blame] | 114 | ctx->drawRect(target, clip, paint, vm, SkRect::MakeWH(2*S, 2*S)); |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 115 | |
| 116 | // now update the lower right of the texture in first pass |
| 117 | // or upper right in second pass |
| 118 | offset = 0; |
| 119 | for (int y = 0; y < S; ++y) { |
| 120 | for (int x = 0; x < S; ++x) { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 121 | gTextureData[offset + y * stride + x] = |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 122 | ((x + y) % 2) ? (i ? green : red) : blue; |
| 123 | } |
| 124 | } |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 125 | texture->writePixels(S, (i ? 0 : S), S, S, |
commit-bot@chromium.org | beede90 | 2013-10-16 11:50:50 +0000 | [diff] [blame] | 126 | texture->config(), gTextureData.get(), |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 127 | 4 * stride); |
joshualitt | 570d2f8 | 2015-02-25 13:19:48 -0800 | [diff] [blame] | 128 | ctx->drawRect(target, clip, paint, vm, SkRect::MakeWH(2*S, 2*S)); |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 129 | } |
bsalomon | b62da80 | 2015-01-31 07:51:14 -0800 | [diff] [blame] | 130 | } else { |
| 131 | this->drawGpuOnlyMessage(canvas); |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 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 |