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