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 | |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 20 | static const int S = 200; |
| 21 | |
halcanary | 2a24338 | 2015-09-09 08:16:41 -0700 | [diff] [blame] | 22 | DEF_SIMPLE_GM_BG(texdata, canvas, 2 * S, 2 * S, SK_ColorBLACK) { |
reed@google.com | 9c135db | 2014-03-12 18:28:35 +0000 | [diff] [blame] | 23 | GrRenderTarget* target = canvas->internal_private_accessTopLayerRenderTarget(); |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 24 | GrContext* ctx = canvas->getGrContext(); |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 25 | SkAutoTUnref<GrDrawContext> drawContext(ctx ? ctx->drawContext(target) : nullptr); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 26 | if (drawContext && target) { |
commit-bot@chromium.org | beede90 | 2013-10-16 11:50:50 +0000 | [diff] [blame] | 27 | SkAutoTArray<SkPMColor> gTextureData((2 * S) * (2 * S)); |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 28 | static const int stride = 2 * S; |
| 29 | static const SkPMColor gray = SkPackARGB32(0x40, 0x40, 0x40, 0x40); |
| 30 | static const SkPMColor white = SkPackARGB32(0xff, 0xff, 0xff, 0xff); |
| 31 | static const SkPMColor red = SkPackARGB32(0x80, 0x80, 0x00, 0x00); |
| 32 | static const SkPMColor blue = SkPackARGB32(0x80, 0x00, 0x00, 0x80); |
| 33 | static const SkPMColor green = SkPackARGB32(0x80, 0x00, 0x80, 0x00); |
| 34 | static const SkPMColor black = SkPackARGB32(0x00, 0x00, 0x00, 0x00); |
| 35 | for (int i = 0; i < 2; ++i) { |
| 36 | int offset = 0; |
| 37 | // fill upper-left |
| 38 | for (int y = 0; y < S; ++y) { |
| 39 | for (int x = 0; x < S; ++x) { |
| 40 | gTextureData[offset + y * stride + x] = gray; |
| 41 | } |
| 42 | } |
| 43 | // fill upper-right |
| 44 | offset = S; |
| 45 | for (int y = 0; y < S; ++y) { |
| 46 | for (int x = 0; x < S; ++x) { |
| 47 | gTextureData[offset + y * stride + x] = white; |
| 48 | } |
| 49 | } |
| 50 | // fill lower left |
| 51 | offset = S * stride; |
| 52 | for (int y = 0; y < S; ++y) { |
| 53 | for (int x = 0; x < S; ++x) { |
| 54 | gTextureData[offset + y * stride + x] = black; |
| 55 | } |
| 56 | } |
| 57 | // fill lower right |
| 58 | offset = S * stride + S; |
| 59 | for (int y = 0; y < S; ++y) { |
| 60 | for (int x = 0; x < S; ++x) { |
| 61 | gTextureData[offset + y * stride + x] = gray; |
| 62 | } |
| 63 | } |
| 64 | |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 65 | GrSurfaceDesc desc; |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 66 | // 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] | 67 | desc.fFlags = i ? kRenderTarget_GrSurfaceFlag : |
| 68 | kNone_GrSurfaceFlags; |
bsalomon@google.com | fec0bc3 | 2013-02-07 14:43:04 +0000 | [diff] [blame] | 69 | desc.fConfig = kSkia8888_GrPixelConfig; |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 70 | desc.fWidth = 2 * S; |
| 71 | desc.fHeight = 2 * S; |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 72 | GrTexture* texture = ctx->textureProvider()->createTexture( |
bsalomon | 5ec26ae | 2016-02-25 08:33:02 -0800 | [diff] [blame] | 73 | desc, SkBudgeted::kNo, gTextureData.get(), 0); |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 74 | |
| 75 | if (!texture) { |
| 76 | return; |
| 77 | } |
bsalomon | dcabb05 | 2014-07-21 14:24:01 -0700 | [diff] [blame] | 78 | SkAutoTUnref<GrTexture> au(texture); |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 79 | |
joshualitt | 570d2f8 | 2015-02-25 13:19:48 -0800 | [diff] [blame] | 80 | // setup new clip |
| 81 | GrClip clip(SkRect::MakeWH(2*S, 2*S)); |
robertphillips@google.com | 3e11c0b | 2012-07-11 18:20:35 +0000 | [diff] [blame] | 82 | |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 83 | GrPaint paint; |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 84 | paint.setPorterDuffXPFactory(SkXfermode::kSrcOver_Mode); |
| 85 | |
bsalomon@google.com | b9086a0 | 2012-11-01 18:02:54 +0000 | [diff] [blame] | 86 | SkMatrix vm; |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 87 | if (i) { |
bsalomon@google.com | 9d12f5c | 2011-09-29 18:08:18 +0000 | [diff] [blame] | 88 | vm.setRotate(90 * SK_Scalar1, |
| 89 | S * SK_Scalar1, |
| 90 | S * SK_Scalar1); |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 91 | } else { |
| 92 | vm.reset(); |
| 93 | } |
bsalomon@google.com | b9086a0 | 2012-11-01 18:02:54 +0000 | [diff] [blame] | 94 | SkMatrix tm; |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 95 | tm = vm; |
bsalomon@google.com | dfdb7e5 | 2012-10-16 15:19:45 +0000 | [diff] [blame] | 96 | tm.postIDiv(2*S, 2*S); |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 97 | paint.addColorTextureProcessor(texture, tm); |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 98 | |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 99 | drawContext->drawRect(clip, paint, vm, SkRect::MakeWH(2*S, 2*S)); |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 100 | |
| 101 | // now update the lower right of the texture in first pass |
| 102 | // or upper right in second pass |
| 103 | offset = 0; |
| 104 | for (int y = 0; y < S; ++y) { |
| 105 | for (int x = 0; x < S; ++x) { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 106 | gTextureData[offset + y * stride + x] = |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 107 | ((x + y) % 2) ? (i ? green : red) : blue; |
| 108 | } |
| 109 | } |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 110 | texture->writePixels(S, (i ? 0 : S), S, S, |
commit-bot@chromium.org | beede90 | 2013-10-16 11:50:50 +0000 | [diff] [blame] | 111 | texture->config(), gTextureData.get(), |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 112 | 4 * stride); |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 113 | drawContext->drawRect(clip, paint, vm, SkRect::MakeWH(2*S, 2*S)); |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 114 | } |
bsalomon | b62da80 | 2015-01-31 07:51:14 -0800 | [diff] [blame] | 115 | } else { |
halcanary | 2a24338 | 2015-09-09 08:16:41 -0700 | [diff] [blame] | 116 | skiagm::GM::DrawGpuOnlyMessage(canvas); |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 117 | } |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 118 | } |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 119 | #endif |