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" |
Robert Phillips | 901f29a | 2017-01-24 16:24:41 -0500 | [diff] [blame] | 14 | #include "GrContextPriv.h" |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 15 | #include "GrProxyProvider.h" |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 16 | #include "GrRenderTargetContext.h" |
Robert Phillips | 901f29a | 2017-01-24 16:24:41 -0500 | [diff] [blame] | 17 | #include "GrTextureContext.h" |
csmartdalton | 02fa32c | 2016-08-19 13:29:27 -0700 | [diff] [blame] | 18 | #include "GrFixedClip.h" |
egdaniel | 8d95ffa | 2014-12-08 13:26:43 -0800 | [diff] [blame] | 19 | #include "SkColorPriv.h" |
Robert Phillips | 40fd7c9 | 2017-01-30 08:06:27 -0500 | [diff] [blame] | 20 | #include "SkGr.h" |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 21 | #include "effects/GrPorterDuffXferProcessor.h" |
| 22 | #include "effects/GrSimpleTextureEffect.h" |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 23 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 24 | constexpr int S = 200; |
Robert Phillips | 901f29a | 2017-01-24 16:24:41 -0500 | [diff] [blame] | 25 | constexpr int kStride = 2 * S; |
| 26 | |
| 27 | // Fill in the pixels: |
| 28 | // gray | white |
| 29 | // ------------- |
| 30 | // black | gray |
| 31 | static void fill_in_pixels(SkPMColor* pixels) { |
| 32 | const SkPMColor gray = SkPackARGB32(0x40, 0x40, 0x40, 0x40); |
| 33 | const SkPMColor white = SkPackARGB32(0xff, 0xff, 0xff, 0xff); |
| 34 | const SkPMColor black = SkPackARGB32(0x00, 0x00, 0x00, 0x00); |
| 35 | |
| 36 | int offset = 0; |
| 37 | |
| 38 | // fill upper-left |
| 39 | for (int y = 0; y < S; ++y) { |
| 40 | for (int x = 0; x < S; ++x) { |
| 41 | pixels[offset + y * kStride + x] = gray; |
| 42 | } |
| 43 | } |
| 44 | // fill upper-right |
| 45 | offset = S; |
| 46 | for (int y = 0; y < S; ++y) { |
| 47 | for (int x = 0; x < S; ++x) { |
| 48 | pixels[offset + y * kStride + x] = white; |
| 49 | } |
| 50 | } |
| 51 | // fill lower left |
| 52 | offset = S * kStride; |
| 53 | for (int y = 0; y < S; ++y) { |
| 54 | for (int x = 0; x < S; ++x) { |
| 55 | pixels[offset + y * kStride + x] = black; |
| 56 | } |
| 57 | } |
| 58 | // fill lower right |
| 59 | offset = S * kStride + S; |
| 60 | for (int y = 0; y < S; ++y) { |
| 61 | for (int x = 0; x < S; ++x) { |
| 62 | pixels[offset + y * kStride + x] = gray; |
| 63 | } |
| 64 | } |
| 65 | } |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 66 | |
halcanary | 2a24338 | 2015-09-09 08:16:41 -0700 | [diff] [blame] | 67 | DEF_SIMPLE_GM_BG(texdata, canvas, 2 * S, 2 * S, SK_ColorBLACK) { |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 68 | GrRenderTargetContext* renderTargetContext = |
| 69 | canvas->internal_private_accessTopLayerRenderTargetContext(); |
| 70 | if (!renderTargetContext) { |
robertphillips | 175dd9b | 2016-04-28 14:32:04 -0700 | [diff] [blame] | 71 | skiagm::GM::DrawGpuOnlyMessage(canvas); |
| 72 | return; |
| 73 | } |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 74 | |
robertphillips | 175dd9b | 2016-04-28 14:32:04 -0700 | [diff] [blame] | 75 | GrContext* context = canvas->getGrContext(); |
| 76 | if (!context) { |
| 77 | return; |
| 78 | } |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 79 | |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 80 | GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider(); |
Robert Phillips | 901f29a | 2017-01-24 16:24:41 -0500 | [diff] [blame] | 81 | const SkImageInfo ii = SkImageInfo::Make(S, S, kBGRA_8888_SkColorType, kPremul_SkAlphaType); |
| 82 | |
robertphillips | 175dd9b | 2016-04-28 14:32:04 -0700 | [diff] [blame] | 83 | SkAutoTArray<SkPMColor> gTextureData((2 * S) * (2 * S)); |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 84 | const SkPMColor red = SkPackARGB32(0x80, 0x80, 0x00, 0x00); |
| 85 | const SkPMColor blue = SkPackARGB32(0x80, 0x00, 0x00, 0x80); |
| 86 | const SkPMColor green = SkPackARGB32(0x80, 0x00, 0x80, 0x00); |
robertphillips | 175dd9b | 2016-04-28 14:32:04 -0700 | [diff] [blame] | 87 | for (int i = 0; i < 2; ++i) { |
Robert Phillips | 901f29a | 2017-01-24 16:24:41 -0500 | [diff] [blame] | 88 | fill_in_pixels(gTextureData.get()); |
robertphillips | 175dd9b | 2016-04-28 14:32:04 -0700 | [diff] [blame] | 89 | |
| 90 | GrSurfaceDesc desc; |
robertphillips | 677da9d | 2016-05-11 05:15:55 -0700 | [diff] [blame] | 91 | desc.fOrigin = i ? kBottomLeft_GrSurfaceOrigin : kTopLeft_GrSurfaceOrigin; |
robertphillips | 175dd9b | 2016-04-28 14:32:04 -0700 | [diff] [blame] | 92 | desc.fWidth = 2 * S; |
| 93 | desc.fHeight = 2 * S; |
Robert Phillips | 40fd7c9 | 2017-01-30 08:06:27 -0500 | [diff] [blame] | 94 | desc.fConfig = SkImageInfo2GrPixelConfig(ii, *context->caps()); |
robertphillips | 175dd9b | 2016-04-28 14:32:04 -0700 | [diff] [blame] | 95 | |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 96 | sk_sp<GrTextureProxy> proxy = proxyProvider->createTextureProxy(desc, SkBudgeted::kNo, |
| 97 | gTextureData.get(), 0); |
Robert Phillips | 901f29a | 2017-01-24 16:24:41 -0500 | [diff] [blame] | 98 | if (!proxy) { |
robertphillips | 175dd9b | 2016-04-28 14:32:04 -0700 | [diff] [blame] | 99 | return; |
| 100 | } |
Robert Phillips | 901f29a | 2017-01-24 16:24:41 -0500 | [diff] [blame] | 101 | |
| 102 | sk_sp<GrSurfaceContext> tContext = context->contextPriv().makeWrappedSurfaceContext( |
Robert Phillips | d5f9cdd | 2018-01-31 09:29:48 -0500 | [diff] [blame] | 103 | std::move(proxy)); |
Robert Phillips | 901f29a | 2017-01-24 16:24:41 -0500 | [diff] [blame] | 104 | |
| 105 | if (!tContext) { |
| 106 | return; |
| 107 | } |
robertphillips | 175dd9b | 2016-04-28 14:32:04 -0700 | [diff] [blame] | 108 | |
| 109 | // setup new clip |
cdalton | 846c051 | 2016-05-13 10:25:00 -0700 | [diff] [blame] | 110 | GrFixedClip clip(SkIRect::MakeWH(2*S, 2*S)); |
robertphillips | 175dd9b | 2016-04-28 14:32:04 -0700 | [diff] [blame] | 111 | |
| 112 | GrPaint paint; |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 113 | paint.setPorterDuffXPFactory(SkBlendMode::kSrcOver); |
robertphillips | 175dd9b | 2016-04-28 14:32:04 -0700 | [diff] [blame] | 114 | |
| 115 | SkMatrix vm; |
| 116 | if (i) { |
Robert Phillips | 901f29a | 2017-01-24 16:24:41 -0500 | [diff] [blame] | 117 | vm.setRotate(90 * SK_Scalar1, S * SK_Scalar1, S * SK_Scalar1); |
robertphillips | 175dd9b | 2016-04-28 14:32:04 -0700 | [diff] [blame] | 118 | } else { |
| 119 | vm.reset(); |
| 120 | } |
Brian Osman | 2240be9 | 2017-10-18 13:15:13 -0400 | [diff] [blame] | 121 | paint.addColorTextureProcessor(tContext->asTextureProxyRef(), vm); |
robertphillips | 175dd9b | 2016-04-28 14:32:04 -0700 | [diff] [blame] | 122 | |
Brian Salomon | b74ef03 | 2017-08-10 12:46:01 -0400 | [diff] [blame] | 123 | renderTargetContext->drawRect(clip, GrPaint::Clone(paint), GrAA::kNo, vm, |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 124 | SkRect::MakeWH(2 * S, 2 * S)); |
robertphillips | 175dd9b | 2016-04-28 14:32:04 -0700 | [diff] [blame] | 125 | |
| 126 | // now update the lower right of the texture in first pass |
| 127 | // or upper right in second pass |
robertphillips | 175dd9b | 2016-04-28 14:32:04 -0700 | [diff] [blame] | 128 | for (int y = 0; y < S; ++y) { |
| 129 | for (int x = 0; x < S; ++x) { |
Robert Phillips | 901f29a | 2017-01-24 16:24:41 -0500 | [diff] [blame] | 130 | gTextureData[y * kStride + x] = ((x + y) % 2) ? (i ? green : red) : blue; |
robertphillips | 175dd9b | 2016-04-28 14:32:04 -0700 | [diff] [blame] | 131 | } |
| 132 | } |
Robert Phillips | 901f29a | 2017-01-24 16:24:41 -0500 | [diff] [blame] | 133 | |
| 134 | if (!tContext->writePixels(ii, gTextureData.get(), 4 * kStride, S, i ? 0 : S)) { |
| 135 | continue; |
| 136 | } |
| 137 | |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 138 | renderTargetContext->drawRect(clip, std::move(paint), GrAA::kNo, vm, |
| 139 | SkRect::MakeWH(2 * S, 2 * S)); |
robertphillips | 175dd9b | 2016-04-28 14:32:04 -0700 | [diff] [blame] | 140 | } |
bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 141 | } |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 142 | #endif |
robertphillips | 175dd9b | 2016-04-28 14:32:04 -0700 | [diff] [blame] | 143 | |