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" |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 15 | #include "GrDrawContext.h" |
egdaniel | 8d95ffa | 2014-12-08 13:26:43 -0800 | [diff] [blame] | 16 | #include "SkColorPriv.h" |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 17 | #include "effects/GrPorterDuffXferProcessor.h" |
| 18 | #include "effects/GrSimpleTextureEffect.h" |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 19 | |
| 20 | namespace skiagm { |
| 21 | |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 22 | static const int S = 200; |
| 23 | |
| 24 | class TexDataGM : public GM { |
| 25 | public: |
bsalomon@google.com | 48dd1a2 | 2011-10-31 14:18:20 +0000 | [diff] [blame] | 26 | TexDataGM() { |
| 27 | this->setBGColor(0xff000000); |
| 28 | } |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 29 | |
| 30 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 31 | SkString onShortName() override { |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 32 | return SkString("texdata"); |
| 33 | } |
| 34 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 35 | SkISize onISize() override { |
tfarina | f539318 | 2014-06-09 23:59:03 -0700 | [diff] [blame] | 36 | return SkISize::Make(2*S, 2*S); |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 37 | } |
| 38 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 39 | void onDraw(SkCanvas* canvas) override { |
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(); |
robertphillips | c9a3706 | 2015-09-01 08:34:28 -0700 | [diff] [blame] | 42 | SkAutoTUnref<GrDrawContext> drawContext(ctx ? ctx->drawContext() : nullptr); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 43 | if (drawContext && target) { |
commit-bot@chromium.org | beede90 | 2013-10-16 11:50:50 +0000 | [diff] [blame] | 44 | SkAutoTArray<SkPMColor> gTextureData((2 * S) * (2 * S)); |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 45 | static const int stride = 2 * S; |
| 46 | static const SkPMColor gray = SkPackARGB32(0x40, 0x40, 0x40, 0x40); |
| 47 | static const SkPMColor white = SkPackARGB32(0xff, 0xff, 0xff, 0xff); |
| 48 | static const SkPMColor red = SkPackARGB32(0x80, 0x80, 0x00, 0x00); |
| 49 | static const SkPMColor blue = SkPackARGB32(0x80, 0x00, 0x00, 0x80); |
| 50 | static const SkPMColor green = SkPackARGB32(0x80, 0x00, 0x80, 0x00); |
| 51 | static const SkPMColor black = SkPackARGB32(0x00, 0x00, 0x00, 0x00); |
| 52 | for (int i = 0; i < 2; ++i) { |
| 53 | int offset = 0; |
| 54 | // fill upper-left |
| 55 | for (int y = 0; y < S; ++y) { |
| 56 | for (int x = 0; x < S; ++x) { |
| 57 | gTextureData[offset + y * stride + x] = gray; |
| 58 | } |
| 59 | } |
| 60 | // fill upper-right |
| 61 | offset = S; |
| 62 | for (int y = 0; y < S; ++y) { |
| 63 | for (int x = 0; x < S; ++x) { |
| 64 | gTextureData[offset + y * stride + x] = white; |
| 65 | } |
| 66 | } |
| 67 | // fill lower left |
| 68 | offset = S * stride; |
| 69 | for (int y = 0; y < S; ++y) { |
| 70 | for (int x = 0; x < S; ++x) { |
| 71 | gTextureData[offset + y * stride + x] = black; |
| 72 | } |
| 73 | } |
| 74 | // fill lower right |
| 75 | offset = S * stride + S; |
| 76 | for (int y = 0; y < S; ++y) { |
| 77 | for (int x = 0; x < S; ++x) { |
| 78 | gTextureData[offset + y * stride + x] = gray; |
| 79 | } |
| 80 | } |
| 81 | |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 82 | GrSurfaceDesc desc; |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 83 | // 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] | 84 | desc.fFlags = i ? kRenderTarget_GrSurfaceFlag : |
| 85 | kNone_GrSurfaceFlags; |
bsalomon@google.com | fec0bc3 | 2013-02-07 14:43:04 +0000 | [diff] [blame] | 86 | desc.fConfig = kSkia8888_GrPixelConfig; |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 87 | desc.fWidth = 2 * S; |
| 88 | desc.fHeight = 2 * S; |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 89 | GrTexture* texture = ctx->textureProvider()->createTexture( |
| 90 | desc, false, gTextureData.get(), 0); |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 91 | |
| 92 | if (!texture) { |
| 93 | return; |
| 94 | } |
bsalomon | dcabb05 | 2014-07-21 14:24:01 -0700 | [diff] [blame] | 95 | SkAutoTUnref<GrTexture> au(texture); |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 96 | |
joshualitt | 570d2f8 | 2015-02-25 13:19:48 -0800 | [diff] [blame] | 97 | // setup new clip |
| 98 | GrClip clip(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 | GrPaint paint; |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 101 | paint.setPorterDuffXPFactory(SkXfermode::kSrcOver_Mode); |
| 102 | |
bsalomon@google.com | b9086a0 | 2012-11-01 18:02:54 +0000 | [diff] [blame] | 103 | SkMatrix vm; |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 104 | if (i) { |
bsalomon@google.com | 9d12f5c | 2011-09-29 18:08:18 +0000 | [diff] [blame] | 105 | vm.setRotate(90 * SK_Scalar1, |
| 106 | S * SK_Scalar1, |
| 107 | S * SK_Scalar1); |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 108 | } else { |
| 109 | vm.reset(); |
| 110 | } |
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); |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 114 | paint.addColorTextureProcessor(texture, tm); |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 115 | |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 116 | drawContext->drawRect(target, clip, paint, vm, 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); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 130 | drawContext->drawRect(target, clip, paint, vm, SkRect::MakeWH(2*S, 2*S)); |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 131 | } |
bsalomon | b62da80 | 2015-01-31 07:51:14 -0800 | [diff] [blame] | 132 | } else { |
| 133 | this->drawGpuOnlyMessage(canvas); |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 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 |