commit-bot@chromium.org | 2663263 | 2014-03-25 15:13:18 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 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 | */ |
| 7 | |
| 8 | // This test only works with the GPU backend. |
| 9 | |
| 10 | #include "gm.h" |
| 11 | |
commit-bot@chromium.org | 2663263 | 2014-03-25 15:13:18 +0000 | [diff] [blame] | 12 | #include "GrContext.h" |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 13 | #include "GrContextPriv.h" |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 14 | #include "GrProxyProvider.h" |
Brian Salomon | 6a63904 | 2016-12-14 11:08:17 -0500 | [diff] [blame] | 15 | #include "GrRenderTargetContextPriv.h" |
commit-bot@chromium.org | 2663263 | 2014-03-25 15:13:18 +0000 | [diff] [blame] | 16 | #include "SkBitmap.h" |
| 17 | #include "SkGr.h" |
| 18 | #include "SkGradientShader.h" |
joshualitt | 04194f3 | 2016-01-13 10:08:27 -0800 | [diff] [blame] | 19 | #include "effects/GrTextureDomain.h" |
Brian Salomon | 8952743 | 2016-12-16 09:52:16 -0500 | [diff] [blame] | 20 | #include "ops/GrDrawOp.h" |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 21 | #include "ops/GrRectOpFactory.h" |
commit-bot@chromium.org | 2663263 | 2014-03-25 15:13:18 +0000 | [diff] [blame] | 22 | |
| 23 | namespace skiagm { |
| 24 | /** |
| 25 | * This GM directly exercises GrTextureDomainEffect. |
| 26 | */ |
| 27 | class TextureDomainEffect : public GM { |
| 28 | public: |
| 29 | TextureDomainEffect() { |
| 30 | this->setBGColor(0xFFFFFFFF); |
| 31 | } |
| 32 | |
| 33 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 34 | SkString onShortName() override { |
commit-bot@chromium.org | 2663263 | 2014-03-25 15:13:18 +0000 | [diff] [blame] | 35 | return SkString("texture_domain_effect"); |
| 36 | } |
| 37 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 38 | SkISize onISize() override { |
joshualitt | 5ae5fc5 | 2014-07-29 12:59:27 -0700 | [diff] [blame] | 39 | const SkScalar canvasWidth = kDrawPad + |
| 40 | (kTargetWidth + 2 * kDrawPad) * GrTextureDomain::kModeCount + |
| 41 | kTestPad * GrTextureDomain::kModeCount; |
| 42 | return SkISize::Make(SkScalarCeilToInt(canvasWidth), 800); |
commit-bot@chromium.org | 2663263 | 2014-03-25 15:13:18 +0000 | [diff] [blame] | 43 | } |
| 44 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 45 | void onOnceBeforeDraw() override { |
Robert Phillips | 40fd7c9 | 2017-01-30 08:06:27 -0500 | [diff] [blame] | 46 | // TODO: do this with surfaces & images and gpu backend |
| 47 | SkImageInfo ii = SkImageInfo::Make(kTargetWidth, kTargetHeight, kN32_SkColorType, |
| 48 | kPremul_SkAlphaType); |
| 49 | fBmp.allocPixels(ii); |
commit-bot@chromium.org | 2663263 | 2014-03-25 15:13:18 +0000 | [diff] [blame] | 50 | SkCanvas canvas(fBmp); |
commit-bot@chromium.org | c5713e4 | 2014-04-07 21:18:46 +0000 | [diff] [blame] | 51 | canvas.clear(0x00000000); |
commit-bot@chromium.org | 2663263 | 2014-03-25 15:13:18 +0000 | [diff] [blame] | 52 | SkPaint paint; |
| 53 | |
| 54 | SkColor colors1[] = { SK_ColorCYAN, SK_ColorLTGRAY, SK_ColorGRAY }; |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 55 | paint.setShader(SkGradientShader::MakeSweep(65.f, 75.f, colors1, nullptr, |
| 56 | SK_ARRAY_COUNT(colors1))); |
| 57 | canvas.drawOval(SkRect::MakeXYWH(-5.f, -5.f, fBmp.width() + 10.f, fBmp.height() + 10.f), |
| 58 | paint); |
commit-bot@chromium.org | 2663263 | 2014-03-25 15:13:18 +0000 | [diff] [blame] | 59 | |
| 60 | SkColor colors2[] = { SK_ColorMAGENTA, SK_ColorLTGRAY, SK_ColorYELLOW }; |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 61 | paint.setShader(SkGradientShader::MakeSweep(45.f, 55.f, colors2, nullptr, |
| 62 | SK_ARRAY_COUNT(colors2))); |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 63 | paint.setBlendMode(SkBlendMode::kDarken); |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 64 | canvas.drawOval(SkRect::MakeXYWH(-5.f, -5.f, fBmp.width() + 10.f, fBmp.height() + 10.f), |
| 65 | paint); |
commit-bot@chromium.org | 2663263 | 2014-03-25 15:13:18 +0000 | [diff] [blame] | 66 | |
| 67 | SkColor colors3[] = { SK_ColorBLUE, SK_ColorLTGRAY, SK_ColorGREEN }; |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 68 | paint.setShader(SkGradientShader::MakeSweep(25.f, 35.f, colors3, nullptr, |
| 69 | SK_ARRAY_COUNT(colors3))); |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 70 | paint.setBlendMode(SkBlendMode::kLighten); |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 71 | canvas.drawOval(SkRect::MakeXYWH(-5.f, -5.f, fBmp.width() + 10.f, fBmp.height() + 10.f), |
| 72 | paint); |
commit-bot@chromium.org | 2663263 | 2014-03-25 15:13:18 +0000 | [diff] [blame] | 73 | } |
| 74 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 75 | void onDraw(SkCanvas* canvas) override { |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 76 | GrRenderTargetContext* renderTargetContext = |
| 77 | canvas->internal_private_accessTopLayerRenderTargetContext(); |
| 78 | if (!renderTargetContext) { |
halcanary | 2a24338 | 2015-09-09 08:16:41 -0700 | [diff] [blame] | 79 | skiagm::GM::DrawGpuOnlyMessage(canvas); |
commit-bot@chromium.org | 2663263 | 2014-03-25 15:13:18 +0000 | [diff] [blame] | 80 | return; |
| 81 | } |
| 82 | |
robertphillips | 175dd9b | 2016-04-28 14:32:04 -0700 | [diff] [blame] | 83 | GrContext* context = canvas->getGrContext(); |
| 84 | if (!context) { |
commit-bot@chromium.org | 2663263 | 2014-03-25 15:13:18 +0000 | [diff] [blame] | 85 | return; |
| 86 | } |
| 87 | |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 88 | GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider(); |
Robert Phillips | 40fd7c9 | 2017-01-30 08:06:27 -0500 | [diff] [blame] | 89 | GrSurfaceDesc desc; |
| 90 | desc.fWidth = fBmp.width(); |
| 91 | desc.fHeight = fBmp.height(); |
Brian Osman | 2b23c4b | 2018-06-01 12:25:08 -0400 | [diff] [blame] | 92 | desc.fConfig = SkColorType2GrPixelConfig(fBmp.colorType()); |
Greg Daniel | 0a7aa14 | 2018-02-21 13:02:32 -0500 | [diff] [blame] | 93 | SkASSERT(kUnknown_GrPixelConfig != desc.fConfig); |
Robert Phillips | 40fd7c9 | 2017-01-30 08:06:27 -0500 | [diff] [blame] | 94 | |
Brian Salomon | 58389b9 | 2018-03-07 13:01:25 -0500 | [diff] [blame] | 95 | sk_sp<GrTextureProxy> proxy = proxyProvider->createTextureProxy( |
| 96 | desc, SkBudgeted::kYes, fBmp.getPixels(), fBmp.rowBytes()); |
Robert Phillips | 2f49314 | 2017-03-02 18:18:38 -0500 | [diff] [blame] | 97 | if (!proxy) { |
commit-bot@chromium.org | 2663263 | 2014-03-25 15:13:18 +0000 | [diff] [blame] | 98 | return; |
| 99 | } |
| 100 | |
commit-bot@chromium.org | 2663263 | 2014-03-25 15:13:18 +0000 | [diff] [blame] | 101 | SkTArray<SkMatrix> textureMatrices; |
Robert Phillips | 67c18d6 | 2017-01-20 12:44:06 -0500 | [diff] [blame] | 102 | textureMatrices.push_back() = SkMatrix::I(); |
| 103 | textureMatrices.push_back() = SkMatrix::MakeScale(1.5f, 0.85f); |
| 104 | textureMatrices.push_back(); |
Robert Phillips | 40fd7c9 | 2017-01-30 08:06:27 -0500 | [diff] [blame] | 105 | textureMatrices.back().setRotate(45.f, proxy->width() / 2.f, proxy->height() / 2.f); |
commit-bot@chromium.org | 2663263 | 2014-03-25 15:13:18 +0000 | [diff] [blame] | 106 | |
| 107 | const SkIRect texelDomains[] = { |
halcanary | f622a6c | 2014-10-24 12:54:53 -0700 | [diff] [blame] | 108 | fBmp.bounds(), |
Robert Phillips | 40fd7c9 | 2017-01-30 08:06:27 -0500 | [diff] [blame] | 109 | SkIRect::MakeXYWH(fBmp.width() / 4, fBmp.height() / 4, |
| 110 | fBmp.width() / 2, fBmp.height() / 2), |
commit-bot@chromium.org | 2663263 | 2014-03-25 15:13:18 +0000 | [diff] [blame] | 111 | }; |
| 112 | |
halcanary | f622a6c | 2014-10-24 12:54:53 -0700 | [diff] [blame] | 113 | SkRect renderRect = SkRect::Make(fBmp.bounds()); |
commit-bot@chromium.org | 2663263 | 2014-03-25 15:13:18 +0000 | [diff] [blame] | 114 | renderRect.outset(kDrawPad, kDrawPad); |
| 115 | |
| 116 | SkScalar y = kDrawPad + kTestPad; |
| 117 | for (int tm = 0; tm < textureMatrices.count(); ++tm) { |
| 118 | for (size_t d = 0; d < SK_ARRAY_COUNT(texelDomains); ++d) { |
| 119 | SkScalar x = kDrawPad + kTestPad; |
| 120 | for (int m = 0; m < GrTextureDomain::kModeCount; ++m) { |
| 121 | GrTextureDomain::Mode mode = (GrTextureDomain::Mode) m; |
robertphillips | 28a838e | 2016-06-23 14:07:00 -0700 | [diff] [blame] | 122 | GrPaint grPaint; |
Brian Salomon | a163392 | 2017-01-09 11:46:10 -0500 | [diff] [blame] | 123 | grPaint.setXPFactory(GrPorterDuffXPFactory::Get(SkBlendMode::kSrc)); |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 124 | auto fp = GrTextureDomainEffect::Make( |
Brian Osman | 2240be9 | 2017-10-18 13:15:13 -0400 | [diff] [blame] | 125 | proxy, textureMatrices[tm], |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 126 | GrTextureDomain::MakeTexelDomainForMode(texelDomains[d], mode), mode, |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 127 | GrSamplerState::Filter::kNearest); |
commit-bot@chromium.org | 2663263 | 2014-03-25 15:13:18 +0000 | [diff] [blame] | 128 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 129 | if (!fp) { |
commit-bot@chromium.org | 2663263 | 2014-03-25 15:13:18 +0000 | [diff] [blame] | 130 | continue; |
| 131 | } |
robertphillips | 1d24b8d | 2015-03-26 19:57:08 -0700 | [diff] [blame] | 132 | const SkMatrix viewMatrix = SkMatrix::MakeTrans(x, y); |
robertphillips | 28a838e | 2016-06-23 14:07:00 -0700 | [diff] [blame] | 133 | grPaint.addColorFragmentProcessor(std::move(fp)); |
Brian Salomon | ac70f84 | 2017-05-08 10:43:33 -0400 | [diff] [blame] | 134 | renderTargetContext->priv().testingOnly_addDrawOp( |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 135 | GrRectOpFactory::MakeNonAAFill(context, std::move(grPaint), viewMatrix, |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 136 | renderRect, GrAAType::kNone)); |
commit-bot@chromium.org | 2663263 | 2014-03-25 15:13:18 +0000 | [diff] [blame] | 137 | x += renderRect.width() + kTestPad; |
| 138 | } |
| 139 | y += renderRect.height() + kTestPad; |
| 140 | } |
| 141 | } |
commit-bot@chromium.org | 2663263 | 2014-03-25 15:13:18 +0000 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | private: |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 145 | static constexpr SkScalar kDrawPad = 10.f; |
| 146 | static constexpr SkScalar kTestPad = 10.f;; |
| 147 | static constexpr int kTargetWidth = 100; |
| 148 | static constexpr int kTargetHeight = 100; |
commit-bot@chromium.org | 2663263 | 2014-03-25 15:13:18 +0000 | [diff] [blame] | 149 | SkBitmap fBmp; |
| 150 | |
| 151 | typedef GM INHERITED; |
| 152 | }; |
| 153 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 154 | DEF_GM(return new TextureDomainEffect;) |
commit-bot@chromium.org | 2663263 | 2014-03-25 15:13:18 +0000 | [diff] [blame] | 155 | } |