epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 7 | |
| 8 | // This test only works with the GPU backend. |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 9 | |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 10 | #include "gm.h" |
bsalomon@google.com | a68937c | 2012-08-03 15:00:52 +0000 | [diff] [blame] | 11 | |
| 12 | #if SK_SUPPORT_GPU |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 13 | #include "GrContext.h" |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 14 | #include "GrDrawContext.h" |
csmartdalton | 02fa32c | 2016-08-19 13:29:27 -0700 | [diff] [blame] | 15 | #include "GrFixedClip.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 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 20 | constexpr int S = 200; |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 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) { |
robertphillips | 175dd9b | 2016-04-28 14:32:04 -0700 | [diff] [blame] | 23 | GrDrawContext* drawContext = canvas->internal_private_accessTopLayerDrawContext(); |
| 24 | if (!drawContext) { |
| 25 | skiagm::GM::DrawGpuOnlyMessage(canvas); |
| 26 | return; |
| 27 | } |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 28 | |
robertphillips | 175dd9b | 2016-04-28 14:32:04 -0700 | [diff] [blame] | 29 | GrContext* context = canvas->getGrContext(); |
| 30 | if (!context) { |
| 31 | return; |
| 32 | } |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 33 | |
robertphillips | 175dd9b | 2016-04-28 14:32:04 -0700 | [diff] [blame] | 34 | SkAutoTArray<SkPMColor> gTextureData((2 * S) * (2 * S)); |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 35 | constexpr int stride = 2 * S; |
| 36 | const SkPMColor gray = SkPackARGB32(0x40, 0x40, 0x40, 0x40); |
| 37 | const SkPMColor white = SkPackARGB32(0xff, 0xff, 0xff, 0xff); |
| 38 | const SkPMColor red = SkPackARGB32(0x80, 0x80, 0x00, 0x00); |
| 39 | const SkPMColor blue = SkPackARGB32(0x80, 0x00, 0x00, 0x80); |
| 40 | const SkPMColor green = SkPackARGB32(0x80, 0x00, 0x80, 0x00); |
| 41 | const SkPMColor black = SkPackARGB32(0x00, 0x00, 0x00, 0x00); |
robertphillips | 175dd9b | 2016-04-28 14:32:04 -0700 | [diff] [blame] | 42 | for (int i = 0; i < 2; ++i) { |
| 43 | int offset = 0; |
| 44 | // fill upper-left |
| 45 | for (int y = 0; y < S; ++y) { |
| 46 | for (int x = 0; x < S; ++x) { |
| 47 | gTextureData[offset + y * stride + x] = gray; |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 48 | } |
| 49 | } |
robertphillips | 175dd9b | 2016-04-28 14:32:04 -0700 | [diff] [blame] | 50 | // fill upper-right |
| 51 | offset = S; |
| 52 | for (int y = 0; y < S; ++y) { |
| 53 | for (int x = 0; x < S; ++x) { |
| 54 | gTextureData[offset + y * stride + x] = white; |
| 55 | } |
| 56 | } |
| 57 | // fill lower left |
| 58 | offset = S * stride; |
| 59 | for (int y = 0; y < S; ++y) { |
| 60 | for (int x = 0; x < S; ++x) { |
| 61 | gTextureData[offset + y * stride + x] = black; |
| 62 | } |
| 63 | } |
| 64 | // fill lower right |
| 65 | offset = S * stride + S; |
| 66 | for (int y = 0; y < S; ++y) { |
| 67 | for (int x = 0; x < S; ++x) { |
| 68 | gTextureData[offset + y * stride + x] = gray; |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | GrSurfaceDesc desc; |
robertphillips | 677da9d | 2016-05-11 05:15:55 -0700 | [diff] [blame] | 73 | desc.fOrigin = i ? kBottomLeft_GrSurfaceOrigin : kTopLeft_GrSurfaceOrigin; |
robertphillips | 175dd9b | 2016-04-28 14:32:04 -0700 | [diff] [blame] | 74 | desc.fConfig = kSkia8888_GrPixelConfig; |
| 75 | desc.fWidth = 2 * S; |
| 76 | desc.fHeight = 2 * S; |
| 77 | GrTexture* texture = context->textureProvider()->createTexture( |
| 78 | desc, SkBudgeted::kNo, gTextureData.get(), 0); |
| 79 | |
| 80 | if (!texture) { |
| 81 | return; |
| 82 | } |
| 83 | SkAutoTUnref<GrTexture> au(texture); |
| 84 | |
| 85 | // setup new clip |
cdalton | 846c051 | 2016-05-13 10:25:00 -0700 | [diff] [blame] | 86 | GrFixedClip clip(SkIRect::MakeWH(2*S, 2*S)); |
robertphillips | 175dd9b | 2016-04-28 14:32:04 -0700 | [diff] [blame] | 87 | |
| 88 | GrPaint paint; |
| 89 | paint.setPorterDuffXPFactory(SkXfermode::kSrcOver_Mode); |
| 90 | |
| 91 | SkMatrix vm; |
| 92 | if (i) { |
| 93 | vm.setRotate(90 * SK_Scalar1, |
| 94 | S * SK_Scalar1, |
| 95 | S * SK_Scalar1); |
| 96 | } else { |
| 97 | vm.reset(); |
| 98 | } |
| 99 | SkMatrix tm; |
| 100 | tm = vm; |
| 101 | tm.postIDiv(2*S, 2*S); |
brianosman | 54f30c1 | 2016-07-18 10:53:52 -0700 | [diff] [blame] | 102 | paint.addColorTextureProcessor(texture, nullptr, tm); |
robertphillips | 175dd9b | 2016-04-28 14:32:04 -0700 | [diff] [blame] | 103 | |
| 104 | drawContext->drawRect(clip, paint, vm, SkRect::MakeWH(2*S, 2*S)); |
| 105 | |
| 106 | // now update the lower right of the texture in first pass |
| 107 | // or upper right in second pass |
| 108 | offset = 0; |
| 109 | for (int y = 0; y < S; ++y) { |
| 110 | for (int x = 0; x < S; ++x) { |
| 111 | gTextureData[offset + y * stride + x] = |
| 112 | ((x + y) % 2) ? (i ? green : red) : blue; |
| 113 | } |
| 114 | } |
| 115 | texture->writePixels(S, (i ? 0 : S), S, S, |
| 116 | texture->config(), gTextureData.get(), |
| 117 | 4 * stride); |
| 118 | drawContext->drawRect(clip, paint, vm, SkRect::MakeWH(2*S, 2*S)); |
| 119 | } |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 120 | } |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 121 | #endif |
robertphillips | 175dd9b | 2016-04-28 14:32:04 -0700 | [diff] [blame] | 122 | |