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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "gm/gm.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "include/core/SkBitmap.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 12 | #include "include/core/SkCanvas.h" |
| 13 | #include "include/core/SkColor.h" |
| 14 | #include "include/core/SkMatrix.h" |
| 15 | #include "include/core/SkPaint.h" |
| 16 | #include "include/core/SkRect.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 17 | #include "include/core/SkSize.h" |
| 18 | #include "include/core/SkString.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 19 | #include "include/effects/SkGradientShader.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 20 | #include "include/private/GrTypesPriv.h" |
| 21 | #include "include/private/SkTArray.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 22 | #include "src/gpu/GrContextPriv.h" |
| 23 | #include "src/gpu/GrProxyProvider.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 24 | #include "src/gpu/GrRenderTargetContext.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 25 | #include "src/gpu/GrRenderTargetContextPriv.h" |
Brian Salomon | 201cdbb | 2019-08-14 17:00:30 -0400 | [diff] [blame] | 26 | #include "src/gpu/GrSamplerState.h" |
Greg Daniel | f91aeb2 | 2019-06-18 09:58:02 -0400 | [diff] [blame] | 27 | #include "src/gpu/GrTextureProxy.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 28 | #include "src/gpu/effects/GrTextureDomain.h" |
Brian Salomon | e21af50 | 2019-11-22 16:56:36 -0500 | [diff] [blame] | 29 | #include "tools/gpu/TestOps.h" |
commit-bot@chromium.org | 2663263 | 2014-03-25 15:13:18 +0000 | [diff] [blame] | 30 | |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 31 | #include <memory> |
| 32 | #include <utility> |
| 33 | |
commit-bot@chromium.org | 2663263 | 2014-03-25 15:13:18 +0000 | [diff] [blame] | 34 | namespace skiagm { |
| 35 | /** |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 36 | * This GM directly exercises GrDomainEffect. |
commit-bot@chromium.org | 2663263 | 2014-03-25 15:13:18 +0000 | [diff] [blame] | 37 | */ |
Chris Dalton | 3a77837 | 2019-02-07 15:23:36 -0700 | [diff] [blame] | 38 | class TextureDomainEffect : public GpuGM { |
commit-bot@chromium.org | 2663263 | 2014-03-25 15:13:18 +0000 | [diff] [blame] | 39 | public: |
Brian Salomon | e21af50 | 2019-11-22 16:56:36 -0500 | [diff] [blame] | 40 | TextureDomainEffect(GrSamplerState::Filter filter) : fFilter(filter) { |
commit-bot@chromium.org | 2663263 | 2014-03-25 15:13:18 +0000 | [diff] [blame] | 41 | this->setBGColor(0xFFFFFFFF); |
| 42 | } |
| 43 | |
| 44 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 45 | SkString onShortName() override { |
Michael Ludwig | be315a2 | 2018-12-17 09:50:51 -0500 | [diff] [blame] | 46 | SkString name("texture_domain_effect"); |
| 47 | if (fFilter == GrSamplerState::Filter::kBilerp) { |
| 48 | name.append("_bilerp"); |
| 49 | } else if (fFilter == GrSamplerState::Filter::kMipMap) { |
| 50 | name.append("_mipmap"); |
| 51 | } |
| 52 | return name; |
commit-bot@chromium.org | 2663263 | 2014-03-25 15:13:18 +0000 | [diff] [blame] | 53 | } |
| 54 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 55 | SkISize onISize() override { |
Brian Salomon | e21af50 | 2019-11-22 16:56:36 -0500 | [diff] [blame] | 56 | const SkScalar canvasWidth = |
| 57 | kDrawPad + 2 * ((kTargetWidth + 2 * kDrawPad) * GrTextureDomain::kModeCount + |
| 58 | kTestPad * GrTextureDomain::kModeCount); |
joshualitt | 5ae5fc5 | 2014-07-29 12:59:27 -0700 | [diff] [blame] | 59 | return SkISize::Make(SkScalarCeilToInt(canvasWidth), 800); |
commit-bot@chromium.org | 2663263 | 2014-03-25 15:13:18 +0000 | [diff] [blame] | 60 | } |
| 61 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 62 | void onOnceBeforeDraw() override { |
Brian Osman | 65f3344 | 2019-03-25 13:42:35 -0400 | [diff] [blame] | 63 | fBitmap.allocN32Pixels(kTargetWidth, kTargetHeight); |
| 64 | SkCanvas canvas(fBitmap); |
| 65 | canvas.clear(0x00000000); |
commit-bot@chromium.org | 2663263 | 2014-03-25 15:13:18 +0000 | [diff] [blame] | 66 | SkPaint paint; |
| 67 | |
| 68 | SkColor colors1[] = { SK_ColorCYAN, SK_ColorLTGRAY, SK_ColorGRAY }; |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 69 | paint.setShader(SkGradientShader::MakeSweep(65.f, 75.f, colors1, nullptr, |
| 70 | SK_ARRAY_COUNT(colors1))); |
Brian Osman | 65f3344 | 2019-03-25 13:42:35 -0400 | [diff] [blame] | 71 | canvas.drawOval(SkRect::MakeXYWH(-5.f, -5.f, kTargetWidth + 10.f, kTargetHeight + 10.f), |
| 72 | paint); |
commit-bot@chromium.org | 2663263 | 2014-03-25 15:13:18 +0000 | [diff] [blame] | 73 | |
| 74 | SkColor colors2[] = { SK_ColorMAGENTA, SK_ColorLTGRAY, SK_ColorYELLOW }; |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 75 | paint.setShader(SkGradientShader::MakeSweep(45.f, 55.f, colors2, nullptr, |
| 76 | SK_ARRAY_COUNT(colors2))); |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 77 | paint.setBlendMode(SkBlendMode::kDarken); |
Brian Osman | 65f3344 | 2019-03-25 13:42:35 -0400 | [diff] [blame] | 78 | canvas.drawOval(SkRect::MakeXYWH(-5.f, -5.f, kTargetWidth + 10.f, kTargetHeight + 10.f), |
| 79 | paint); |
commit-bot@chromium.org | 2663263 | 2014-03-25 15:13:18 +0000 | [diff] [blame] | 80 | |
| 81 | SkColor colors3[] = { SK_ColorBLUE, SK_ColorLTGRAY, SK_ColorGREEN }; |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 82 | paint.setShader(SkGradientShader::MakeSweep(25.f, 35.f, colors3, nullptr, |
| 83 | SK_ARRAY_COUNT(colors3))); |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 84 | paint.setBlendMode(SkBlendMode::kLighten); |
Brian Osman | 65f3344 | 2019-03-25 13:42:35 -0400 | [diff] [blame] | 85 | canvas.drawOval(SkRect::MakeXYWH(-5.f, -5.f, kTargetWidth + 10.f, kTargetHeight + 10.f), |
| 86 | paint); |
commit-bot@chromium.org | 2663263 | 2014-03-25 15:13:18 +0000 | [diff] [blame] | 87 | } |
| 88 | |
Chris Dalton | 50e24d7 | 2019-02-07 16:20:09 -0700 | [diff] [blame] | 89 | DrawResult onDraw(GrContext* context, GrRenderTargetContext* renderTargetContext, |
| 90 | SkCanvas* canvas, SkString* errorMsg) override { |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 91 | GrProxyProvider* proxyProvider = context->priv().proxyProvider(); |
Michael Ludwig | be315a2 | 2018-12-17 09:50:51 -0500 | [diff] [blame] | 92 | sk_sp<GrTextureProxy> proxy; |
Brian Osman | 65f3344 | 2019-03-25 13:42:35 -0400 | [diff] [blame] | 93 | GrMipMapped mipMapped = fFilter == GrSamplerState::Filter::kMipMap && |
| 94 | context->priv().caps()->mipMapSupport() |
| 95 | ? GrMipMapped::kYes : GrMipMapped::kNo; |
| 96 | proxy = proxyProvider->createProxyFromBitmap(fBitmap, mipMapped); |
Robert Phillips | 2f49314 | 2017-03-02 18:18:38 -0500 | [diff] [blame] | 97 | if (!proxy) { |
Chris Dalton | 50e24d7 | 2019-02-07 16:20:09 -0700 | [diff] [blame] | 98 | *errorMsg = "Failed to create proxy."; |
| 99 | return DrawResult::kFail; |
commit-bot@chromium.org | 2663263 | 2014-03-25 15:13:18 +0000 | [diff] [blame] | 100 | } |
| 101 | |
commit-bot@chromium.org | 2663263 | 2014-03-25 15:13:18 +0000 | [diff] [blame] | 102 | SkTArray<SkMatrix> textureMatrices; |
Robert Phillips | 67c18d6 | 2017-01-20 12:44:06 -0500 | [diff] [blame] | 103 | textureMatrices.push_back() = SkMatrix::I(); |
| 104 | textureMatrices.push_back() = SkMatrix::MakeScale(1.5f, 0.85f); |
| 105 | textureMatrices.push_back(); |
Robert Phillips | 40fd7c9 | 2017-01-30 08:06:27 -0500 | [diff] [blame] | 106 | 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] | 107 | |
| 108 | const SkIRect texelDomains[] = { |
Brian Osman | 65f3344 | 2019-03-25 13:42:35 -0400 | [diff] [blame] | 109 | fBitmap.bounds(), |
| 110 | SkIRect::MakeXYWH(fBitmap.width() / 4 - 1, fBitmap.height() / 4 - 1, |
| 111 | fBitmap.width() / 2 + 2, fBitmap.height() / 2 + 2), |
commit-bot@chromium.org | 2663263 | 2014-03-25 15:13:18 +0000 | [diff] [blame] | 112 | }; |
| 113 | |
Brian Salomon | e21af50 | 2019-11-22 16:56:36 -0500 | [diff] [blame] | 114 | SkRect localRect = SkRect::Make(fBitmap.bounds()).makeOutset(kDrawPad, kDrawPad); |
commit-bot@chromium.org | 2663263 | 2014-03-25 15:13:18 +0000 | [diff] [blame] | 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; |
Michael Ludwig | be315a2 | 2018-12-17 09:50:51 -0500 | [diff] [blame] | 122 | if (fFilter != GrSamplerState::Filter::kNearest && |
| 123 | mode == GrTextureDomain::kRepeat_Mode) { |
| 124 | // Repeat mode doesn't produce correct results with bilerp filtering |
| 125 | continue; |
| 126 | } |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 127 | auto fp1 = GrSimpleTextureEffect::Make(proxy, fBitmap.alphaType(), |
| 128 | textureMatrices[tm], fFilter); |
| 129 | fp1 = GrDomainEffect::Make( |
| 130 | std::move(fp1), GrTextureDomain::MakeTexelDomain(texelDomains[d], mode), |
| 131 | mode, fFilter); |
Brian Salomon | e21af50 | 2019-11-22 16:56:36 -0500 | [diff] [blame] | 132 | if (!fp1) { |
commit-bot@chromium.org | 2663263 | 2014-03-25 15:13:18 +0000 | [diff] [blame] | 133 | continue; |
| 134 | } |
Brian Salomon | e21af50 | 2019-11-22 16:56:36 -0500 | [diff] [blame] | 135 | auto fp2 = fp1->clone(); |
| 136 | SkASSERT(fp2); |
| 137 | auto drawRect = localRect.makeOffset(x, y); |
| 138 | if (auto op = sk_gpu_test::test_ops::MakeRect( |
| 139 | context, std::move(fp1), drawRect, localRect)) { |
| 140 | renderTargetContext->priv().testingOnly_addDrawOp(std::move(op)); |
| 141 | } |
| 142 | x += localRect.width() + kTestPad; |
| 143 | // Draw again with a translated local rect and compensating translate matrix. |
| 144 | drawRect = localRect.makeOffset(x, y); |
| 145 | static constexpr SkVector kT = {-100, 300}; |
| 146 | if (auto op = sk_gpu_test::test_ops::MakeRect(context, |
| 147 | std::move(fp2), |
| 148 | drawRect, |
| 149 | localRect.makeOffset(kT), |
| 150 | SkMatrix::MakeTrans(-kT))) { |
| 151 | renderTargetContext->priv().testingOnly_addDrawOp(std::move(op)); |
| 152 | } |
| 153 | x += localRect.width() + kTestPad; |
commit-bot@chromium.org | 2663263 | 2014-03-25 15:13:18 +0000 | [diff] [blame] | 154 | } |
Brian Salomon | e21af50 | 2019-11-22 16:56:36 -0500 | [diff] [blame] | 155 | y += localRect.height() + kTestPad; |
commit-bot@chromium.org | 2663263 | 2014-03-25 15:13:18 +0000 | [diff] [blame] | 156 | } |
| 157 | } |
Chris Dalton | 50e24d7 | 2019-02-07 16:20:09 -0700 | [diff] [blame] | 158 | return DrawResult::kOk; |
commit-bot@chromium.org | 2663263 | 2014-03-25 15:13:18 +0000 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | private: |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 162 | static constexpr SkScalar kDrawPad = 10.f; |
Kevin Lubick | 48ce543 | 2019-01-04 08:54:32 -0500 | [diff] [blame] | 163 | static constexpr SkScalar kTestPad = 10.f; |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 164 | static constexpr int kTargetWidth = 100; |
| 165 | static constexpr int kTargetHeight = 100; |
Brian Osman | 65f3344 | 2019-03-25 13:42:35 -0400 | [diff] [blame] | 166 | SkBitmap fBitmap; |
Michael Ludwig | be315a2 | 2018-12-17 09:50:51 -0500 | [diff] [blame] | 167 | GrSamplerState::Filter fFilter; |
commit-bot@chromium.org | 2663263 | 2014-03-25 15:13:18 +0000 | [diff] [blame] | 168 | |
| 169 | typedef GM INHERITED; |
| 170 | }; |
| 171 | |
Michael Ludwig | be315a2 | 2018-12-17 09:50:51 -0500 | [diff] [blame] | 172 | DEF_GM(return new TextureDomainEffect(GrSamplerState::Filter::kNearest);) |
| 173 | DEF_GM(return new TextureDomainEffect(GrSamplerState::Filter::kBilerp);) |
| 174 | DEF_GM(return new TextureDomainEffect(GrSamplerState::Filter::kMipMap);) |
| 175 | |
commit-bot@chromium.org | 2663263 | 2014-03-25 15:13:18 +0000 | [diff] [blame] | 176 | } |