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 "SkGradientShader.h" |
Brian Osman | 2700abc | 2018-09-12 10:19:41 -0400 | [diff] [blame] | 17 | #include "SkImage.h" |
Michael Ludwig | be315a2 | 2018-12-17 09:50:51 -0500 | [diff] [blame] | 18 | #include "SkImage_Base.h" |
Brian Osman | 2700abc | 2018-09-12 10:19:41 -0400 | [diff] [blame] | 19 | #include "SkSurface.h" |
joshualitt | 04194f3 | 2016-01-13 10:08:27 -0800 | [diff] [blame] | 20 | #include "effects/GrTextureDomain.h" |
Brian Salomon | 8952743 | 2016-12-16 09:52:16 -0500 | [diff] [blame] | 21 | #include "ops/GrDrawOp.h" |
Michael Ludwig | 72ab346 | 2018-12-10 12:43:36 -0500 | [diff] [blame] | 22 | #include "ops/GrFillRectOp.h" |
commit-bot@chromium.org | 2663263 | 2014-03-25 15:13:18 +0000 | [diff] [blame] | 23 | |
| 24 | namespace skiagm { |
| 25 | /** |
| 26 | * This GM directly exercises GrTextureDomainEffect. |
| 27 | */ |
Chris Dalton | 3a77837 | 2019-02-07 15:23:36 -0700 | [diff] [blame] | 28 | class TextureDomainEffect : public GpuGM { |
commit-bot@chromium.org | 2663263 | 2014-03-25 15:13:18 +0000 | [diff] [blame] | 29 | public: |
Michael Ludwig | be315a2 | 2018-12-17 09:50:51 -0500 | [diff] [blame] | 30 | TextureDomainEffect(GrSamplerState::Filter filter) |
| 31 | : fFilter(filter) { |
commit-bot@chromium.org | 2663263 | 2014-03-25 15:13:18 +0000 | [diff] [blame] | 32 | this->setBGColor(0xFFFFFFFF); |
| 33 | } |
| 34 | |
| 35 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 36 | SkString onShortName() override { |
Michael Ludwig | be315a2 | 2018-12-17 09:50:51 -0500 | [diff] [blame] | 37 | SkString name("texture_domain_effect"); |
| 38 | if (fFilter == GrSamplerState::Filter::kBilerp) { |
| 39 | name.append("_bilerp"); |
| 40 | } else if (fFilter == GrSamplerState::Filter::kMipMap) { |
| 41 | name.append("_mipmap"); |
| 42 | } |
| 43 | return name; |
commit-bot@chromium.org | 2663263 | 2014-03-25 15:13:18 +0000 | [diff] [blame] | 44 | } |
| 45 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 46 | SkISize onISize() override { |
joshualitt | 5ae5fc5 | 2014-07-29 12:59:27 -0700 | [diff] [blame] | 47 | const SkScalar canvasWidth = kDrawPad + |
| 48 | (kTargetWidth + 2 * kDrawPad) * GrTextureDomain::kModeCount + |
| 49 | kTestPad * GrTextureDomain::kModeCount; |
| 50 | return SkISize::Make(SkScalarCeilToInt(canvasWidth), 800); |
commit-bot@chromium.org | 2663263 | 2014-03-25 15:13:18 +0000 | [diff] [blame] | 51 | } |
| 52 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 53 | void onOnceBeforeDraw() override { |
Brian Osman | 2700abc | 2018-09-12 10:19:41 -0400 | [diff] [blame] | 54 | // TODO: do this with gpu backend |
Robert Phillips | 40fd7c9 | 2017-01-30 08:06:27 -0500 | [diff] [blame] | 55 | SkImageInfo ii = SkImageInfo::Make(kTargetWidth, kTargetHeight, kN32_SkColorType, |
| 56 | kPremul_SkAlphaType); |
Brian Osman | 2700abc | 2018-09-12 10:19:41 -0400 | [diff] [blame] | 57 | auto surface = SkSurface::MakeRaster(ii); |
| 58 | SkCanvas* canvas = surface->getCanvas(); |
| 59 | canvas->clear(0x00000000); |
commit-bot@chromium.org | 2663263 | 2014-03-25 15:13:18 +0000 | [diff] [blame] | 60 | SkPaint paint; |
| 61 | |
| 62 | SkColor colors1[] = { SK_ColorCYAN, SK_ColorLTGRAY, SK_ColorGRAY }; |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 63 | paint.setShader(SkGradientShader::MakeSweep(65.f, 75.f, colors1, nullptr, |
| 64 | SK_ARRAY_COUNT(colors1))); |
Brian Osman | 2700abc | 2018-09-12 10:19:41 -0400 | [diff] [blame] | 65 | canvas->drawOval(SkRect::MakeXYWH(-5.f, -5.f, kTargetWidth + 10.f, kTargetHeight + 10.f), |
| 66 | paint); |
commit-bot@chromium.org | 2663263 | 2014-03-25 15:13:18 +0000 | [diff] [blame] | 67 | |
| 68 | SkColor colors2[] = { SK_ColorMAGENTA, SK_ColorLTGRAY, SK_ColorYELLOW }; |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 69 | paint.setShader(SkGradientShader::MakeSweep(45.f, 55.f, colors2, nullptr, |
| 70 | SK_ARRAY_COUNT(colors2))); |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 71 | paint.setBlendMode(SkBlendMode::kDarken); |
Brian Osman | 2700abc | 2018-09-12 10:19:41 -0400 | [diff] [blame] | 72 | canvas->drawOval(SkRect::MakeXYWH(-5.f, -5.f, kTargetWidth + 10.f, kTargetHeight + 10.f), |
| 73 | paint); |
commit-bot@chromium.org | 2663263 | 2014-03-25 15:13:18 +0000 | [diff] [blame] | 74 | |
| 75 | SkColor colors3[] = { SK_ColorBLUE, SK_ColorLTGRAY, SK_ColorGREEN }; |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 76 | paint.setShader(SkGradientShader::MakeSweep(25.f, 35.f, colors3, nullptr, |
| 77 | SK_ARRAY_COUNT(colors3))); |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 78 | paint.setBlendMode(SkBlendMode::kLighten); |
Brian Osman | 2700abc | 2018-09-12 10:19:41 -0400 | [diff] [blame] | 79 | canvas->drawOval(SkRect::MakeXYWH(-5.f, -5.f, kTargetWidth + 10.f, kTargetHeight + 10.f), |
| 80 | paint); |
| 81 | |
| 82 | fImage = surface->makeImageSnapshot(); |
commit-bot@chromium.org | 2663263 | 2014-03-25 15:13:18 +0000 | [diff] [blame] | 83 | } |
| 84 | |
Chris Dalton | 50e24d7 | 2019-02-07 16:20:09 -0700 | [diff] [blame] | 85 | DrawResult onDraw(GrContext* context, GrRenderTargetContext* renderTargetContext, |
| 86 | SkCanvas* canvas, SkString* errorMsg) override { |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 87 | GrProxyProvider* proxyProvider = context->priv().proxyProvider(); |
Michael Ludwig | be315a2 | 2018-12-17 09:50:51 -0500 | [diff] [blame] | 88 | sk_sp<GrTextureProxy> proxy; |
| 89 | if (fFilter == GrSamplerState::Filter::kMipMap) { |
| 90 | SkBitmap copy; |
| 91 | SkImageInfo info = as_IB(fImage)->onImageInfo().makeColorType(kN32_SkColorType); |
| 92 | if (!copy.tryAllocPixels(info) || !fImage->readPixels(copy.pixmap(), 0, 0)) { |
Chris Dalton | 50e24d7 | 2019-02-07 16:20:09 -0700 | [diff] [blame] | 93 | *errorMsg = "Failed to read pixels."; |
| 94 | return DrawResult::kFail; |
Michael Ludwig | be315a2 | 2018-12-17 09:50:51 -0500 | [diff] [blame] | 95 | } |
| 96 | proxy = proxyProvider->createMipMapProxyFromBitmap(copy); |
| 97 | } else { |
| 98 | proxy = proxyProvider->createTextureProxy( |
Brian Osman | 2700abc | 2018-09-12 10:19:41 -0400 | [diff] [blame] | 99 | fImage, kNone_GrSurfaceFlags, 1, SkBudgeted::kYes, SkBackingFit::kExact); |
Michael Ludwig | be315a2 | 2018-12-17 09:50:51 -0500 | [diff] [blame] | 100 | } |
Robert Phillips | 2f49314 | 2017-03-02 18:18:38 -0500 | [diff] [blame] | 101 | if (!proxy) { |
Chris Dalton | 50e24d7 | 2019-02-07 16:20:09 -0700 | [diff] [blame] | 102 | *errorMsg = "Failed to create proxy."; |
| 103 | return DrawResult::kFail; |
commit-bot@chromium.org | 2663263 | 2014-03-25 15:13:18 +0000 | [diff] [blame] | 104 | } |
| 105 | |
commit-bot@chromium.org | 2663263 | 2014-03-25 15:13:18 +0000 | [diff] [blame] | 106 | SkTArray<SkMatrix> textureMatrices; |
Robert Phillips | 67c18d6 | 2017-01-20 12:44:06 -0500 | [diff] [blame] | 107 | textureMatrices.push_back() = SkMatrix::I(); |
| 108 | textureMatrices.push_back() = SkMatrix::MakeScale(1.5f, 0.85f); |
| 109 | textureMatrices.push_back(); |
Robert Phillips | 40fd7c9 | 2017-01-30 08:06:27 -0500 | [diff] [blame] | 110 | 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] | 111 | |
| 112 | const SkIRect texelDomains[] = { |
Brian Osman | 2700abc | 2018-09-12 10:19:41 -0400 | [diff] [blame] | 113 | fImage->bounds(), |
Michael Ludwig | be315a2 | 2018-12-17 09:50:51 -0500 | [diff] [blame] | 114 | SkIRect::MakeXYWH(fImage->width() / 4 - 1, fImage->height() / 4 - 1, |
| 115 | fImage->width() / 2 + 2, fImage->height() / 2 + 2), |
commit-bot@chromium.org | 2663263 | 2014-03-25 15:13:18 +0000 | [diff] [blame] | 116 | }; |
| 117 | |
Brian Osman | 2700abc | 2018-09-12 10:19:41 -0400 | [diff] [blame] | 118 | SkRect renderRect = SkRect::Make(fImage->bounds()); |
commit-bot@chromium.org | 2663263 | 2014-03-25 15:13:18 +0000 | [diff] [blame] | 119 | renderRect.outset(kDrawPad, kDrawPad); |
| 120 | |
| 121 | SkScalar y = kDrawPad + kTestPad; |
| 122 | for (int tm = 0; tm < textureMatrices.count(); ++tm) { |
| 123 | for (size_t d = 0; d < SK_ARRAY_COUNT(texelDomains); ++d) { |
| 124 | SkScalar x = kDrawPad + kTestPad; |
| 125 | for (int m = 0; m < GrTextureDomain::kModeCount; ++m) { |
| 126 | GrTextureDomain::Mode mode = (GrTextureDomain::Mode) m; |
Michael Ludwig | be315a2 | 2018-12-17 09:50:51 -0500 | [diff] [blame] | 127 | if (fFilter != GrSamplerState::Filter::kNearest && |
| 128 | mode == GrTextureDomain::kRepeat_Mode) { |
| 129 | // Repeat mode doesn't produce correct results with bilerp filtering |
| 130 | continue; |
| 131 | } |
| 132 | |
robertphillips | 28a838e | 2016-06-23 14:07:00 -0700 | [diff] [blame] | 133 | GrPaint grPaint; |
Brian Salomon | a163392 | 2017-01-09 11:46:10 -0500 | [diff] [blame] | 134 | grPaint.setXPFactory(GrPorterDuffXPFactory::Get(SkBlendMode::kSrc)); |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 135 | auto fp = GrTextureDomainEffect::Make( |
Brian Osman | 2240be9 | 2017-10-18 13:15:13 -0400 | [diff] [blame] | 136 | proxy, textureMatrices[tm], |
Michael Ludwig | be315a2 | 2018-12-17 09:50:51 -0500 | [diff] [blame] | 137 | GrTextureDomain::MakeTexelDomain(texelDomains[d], mode), |
| 138 | mode, fFilter); |
commit-bot@chromium.org | 2663263 | 2014-03-25 15:13:18 +0000 | [diff] [blame] | 139 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 140 | if (!fp) { |
commit-bot@chromium.org | 2663263 | 2014-03-25 15:13:18 +0000 | [diff] [blame] | 141 | continue; |
| 142 | } |
robertphillips | 1d24b8d | 2015-03-26 19:57:08 -0700 | [diff] [blame] | 143 | const SkMatrix viewMatrix = SkMatrix::MakeTrans(x, y); |
robertphillips | 28a838e | 2016-06-23 14:07:00 -0700 | [diff] [blame] | 144 | grPaint.addColorFragmentProcessor(std::move(fp)); |
Brian Salomon | ac70f84 | 2017-05-08 10:43:33 -0400 | [diff] [blame] | 145 | renderTargetContext->priv().testingOnly_addDrawOp( |
Michael Ludwig | 72ab346 | 2018-12-10 12:43:36 -0500 | [diff] [blame] | 146 | GrFillRectOp::Make(context, std::move(grPaint), GrAAType::kNone, |
| 147 | viewMatrix, renderRect)); |
commit-bot@chromium.org | 2663263 | 2014-03-25 15:13:18 +0000 | [diff] [blame] | 148 | x += renderRect.width() + kTestPad; |
| 149 | } |
| 150 | y += renderRect.height() + kTestPad; |
| 151 | } |
| 152 | } |
Chris Dalton | 50e24d7 | 2019-02-07 16:20:09 -0700 | [diff] [blame] | 153 | return DrawResult::kOk; |
commit-bot@chromium.org | 2663263 | 2014-03-25 15:13:18 +0000 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | private: |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 157 | static constexpr SkScalar kDrawPad = 10.f; |
Kevin Lubick | 48ce543 | 2019-01-04 08:54:32 -0500 | [diff] [blame] | 158 | static constexpr SkScalar kTestPad = 10.f; |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 159 | static constexpr int kTargetWidth = 100; |
| 160 | static constexpr int kTargetHeight = 100; |
Brian Osman | 2700abc | 2018-09-12 10:19:41 -0400 | [diff] [blame] | 161 | sk_sp<SkImage> fImage; |
Michael Ludwig | be315a2 | 2018-12-17 09:50:51 -0500 | [diff] [blame] | 162 | GrSamplerState::Filter fFilter; |
commit-bot@chromium.org | 2663263 | 2014-03-25 15:13:18 +0000 | [diff] [blame] | 163 | |
| 164 | typedef GM INHERITED; |
| 165 | }; |
| 166 | |
Michael Ludwig | be315a2 | 2018-12-17 09:50:51 -0500 | [diff] [blame] | 167 | DEF_GM(return new TextureDomainEffect(GrSamplerState::Filter::kNearest);) |
| 168 | DEF_GM(return new TextureDomainEffect(GrSamplerState::Filter::kBilerp);) |
| 169 | DEF_GM(return new TextureDomainEffect(GrSamplerState::Filter::kMipMap);) |
| 170 | |
commit-bot@chromium.org | 2663263 | 2014-03-25 15:13:18 +0000 | [diff] [blame] | 171 | } |