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 | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame] | 30 | SkString onShortName() SK_OVERRIDE { |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 31 | return SkString("texdata"); |
| 32 | } |
| 33 | |
mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame] | 34 | SkISize onISize() SK_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 | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame] | 38 | uint32_t onGetFlags() const SK_OVERRIDE { return kGPUOnly_Flag; } |
commit-bot@chromium.org | 1d5bbb2 | 2013-10-14 14:15:28 +0000 | [diff] [blame] | 39 | |
mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame] | 40 | void onDraw(SkCanvas* canvas) SK_OVERRIDE { |
reed@google.com | 9c135db | 2014-03-12 18:28:35 +0000 | [diff] [blame] | 41 | GrRenderTarget* target = canvas->internal_private_accessTopLayerRenderTarget(); |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 42 | GrContext* ctx = canvas->getGrContext(); |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 43 | if (ctx && 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; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 89 | GrTexture* texture = |
commit-bot@chromium.org | beede90 | 2013-10-16 11:50:50 +0000 | [diff] [blame] | 90 | ctx->createUncachedTexture(desc, 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 | |
commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame] | 97 | GrContext::AutoClip acs(ctx, SkRect::MakeWH(2*S, 2*S)); |
robertphillips@google.com | 3e11c0b | 2012-07-11 18:20:35 +0000 | [diff] [blame] | 98 | |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 99 | ctx->setRenderTarget(target); |
| 100 | |
| 101 | GrPaint paint; |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 102 | paint.setPorterDuffXPFactory(SkXfermode::kSrcOver_Mode); |
| 103 | |
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 | } |
bsalomon@google.com | b9086a0 | 2012-11-01 18:02:54 +0000 | [diff] [blame] | 112 | SkMatrix tm; |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 113 | tm = vm; |
bsalomon@google.com | dfdb7e5 | 2012-10-16 15:19:45 +0000 | [diff] [blame] | 114 | tm.postIDiv(2*S, 2*S); |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 115 | paint.addColorTextureProcessor(texture, tm); |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 116 | |
joshualitt | 5531d51 | 2014-12-17 15:50:11 -0800 | [diff] [blame] | 117 | ctx->drawRect(paint, vm, SkRect::MakeWH(2*S, 2*S)); |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 118 | |
| 119 | // now update the lower right of the texture in first pass |
| 120 | // or upper right in second pass |
| 121 | offset = 0; |
| 122 | for (int y = 0; y < S; ++y) { |
| 123 | for (int x = 0; x < S; ++x) { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 124 | gTextureData[offset + y * stride + x] = |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 125 | ((x + y) % 2) ? (i ? green : red) : blue; |
| 126 | } |
| 127 | } |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 128 | texture->writePixels(S, (i ? 0 : S), S, S, |
commit-bot@chromium.org | beede90 | 2013-10-16 11:50:50 +0000 | [diff] [blame] | 129 | texture->config(), gTextureData.get(), |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 130 | 4 * stride); |
joshualitt | 5531d51 | 2014-12-17 15:50:11 -0800 | [diff] [blame] | 131 | ctx->drawRect(paint, vm, SkRect::MakeWH(2*S, 2*S)); |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 132 | } |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | private: |
| 137 | typedef GM INHERITED; |
| 138 | }; |
| 139 | |
| 140 | ////////////////////////////////////////////////////////////////////////////// |
| 141 | |
| 142 | static GM* MyFactory(void*) { return new TexDataGM; } |
| 143 | static GMRegistry reg(MyFactory); |
| 144 | |
| 145 | } |
| 146 | |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 147 | #endif |