Brian Salomon | f033404 | 2020-01-31 16:16:39 -0500 | [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/gm.h" |
| 11 | #include "include/core/SkBitmap.h" |
| 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" |
| 17 | #include "include/core/SkSize.h" |
| 18 | #include "include/core/SkString.h" |
Brian Salomon | f033404 | 2020-01-31 16:16:39 -0500 | [diff] [blame] | 19 | #include "include/private/SkTArray.h" |
Robert Phillips | 7a0d3c3 | 2021-07-21 15:39:51 -0400 | [diff] [blame] | 20 | #include "src/core/SkCanvasPriv.h" |
Adlai Holler | a069304 | 2020-10-14 11:23:11 -0400 | [diff] [blame] | 21 | #include "src/gpu/GrDirectContextPriv.h" |
Brian Salomon | f033404 | 2020-01-31 16:16:39 -0500 | [diff] [blame] | 22 | #include "src/gpu/GrProxyProvider.h" |
Brian Salomon | f033404 | 2020-01-31 16:16:39 -0500 | [diff] [blame] | 23 | #include "src/gpu/GrSamplerState.h" |
Brian Salomon | 27c4202 | 2021-04-28 12:39:21 -0400 | [diff] [blame] | 24 | #include "src/gpu/SkGr.h" |
Robert Phillips | 550de7f | 2021-07-06 16:28:52 -0400 | [diff] [blame] | 25 | #include "src/gpu/effects/GrTextureEffect.h" |
Robert Phillips | 4dca831 | 2021-07-28 15:13:20 -0400 | [diff] [blame] | 26 | #include "src/gpu/v1/SurfaceDrawContext_v1.h" |
Brian Salomon | f033404 | 2020-01-31 16:16:39 -0500 | [diff] [blame] | 27 | #include "tools/Resources.h" |
| 28 | #include "tools/gpu/TestOps.h" |
| 29 | |
| 30 | #include <memory> |
| 31 | #include <utility> |
| 32 | |
Brian Salomon | e69b9ef | 2020-07-22 11:18:06 -0400 | [diff] [blame] | 33 | using MipmapMode = GrSamplerState::MipmapMode; |
| 34 | using Filter = GrSamplerState::Filter; |
| 35 | using Wrap = GrSamplerState::WrapMode; |
| 36 | |
Brian Salomon | f033404 | 2020-01-31 16:16:39 -0500 | [diff] [blame] | 37 | namespace skiagm { |
| 38 | /** |
| 39 | * This GM directly exercises GrTextureEffect::MakeTexelSubset. |
| 40 | */ |
| 41 | class TexelSubset : public GpuGM { |
| 42 | public: |
Brian Salomon | e69b9ef | 2020-07-22 11:18:06 -0400 | [diff] [blame] | 43 | TexelSubset(Filter filter, MipmapMode mm, bool upscale) |
| 44 | : fFilter(filter), fMipmapMode(mm), fUpscale(upscale) { |
Brian Salomon | f033404 | 2020-01-31 16:16:39 -0500 | [diff] [blame] | 45 | this->setBGColor(0xFFFFFFFF); |
| 46 | } |
| 47 | |
| 48 | protected: |
| 49 | SkString onShortName() override { |
| 50 | SkString name("texel_subset"); |
| 51 | switch (fFilter) { |
Brian Salomon | e69b9ef | 2020-07-22 11:18:06 -0400 | [diff] [blame] | 52 | case Filter::kNearest: |
Brian Salomon | f033404 | 2020-01-31 16:16:39 -0500 | [diff] [blame] | 53 | name.append("_nearest"); |
| 54 | break; |
Brian Salomon | e69b9ef | 2020-07-22 11:18:06 -0400 | [diff] [blame] | 55 | case Filter::kLinear: |
| 56 | name.append("_linear"); |
Brian Salomon | f033404 | 2020-01-31 16:16:39 -0500 | [diff] [blame] | 57 | break; |
| 58 | } |
Brian Salomon | f735351 | 2020-07-22 19:26:48 -0400 | [diff] [blame] | 59 | switch (fMipmapMode) { |
| 60 | case MipmapMode::kNone: |
| 61 | break; |
| 62 | case MipmapMode::kNearest: |
| 63 | name.append("_mipmap_nearest"); |
| 64 | break; |
| 65 | case MipmapMode::kLinear: |
| 66 | name.append("_mipmap_linear"); |
| 67 | break; |
| 68 | } |
Brian Salomon | f033404 | 2020-01-31 16:16:39 -0500 | [diff] [blame] | 69 | name.append(fUpscale ? "_up" : "_down"); |
| 70 | return name; |
| 71 | } |
| 72 | |
| 73 | SkISize onISize() override { |
Brian Salomon | 16033c9 | 2020-02-13 11:25:11 -0500 | [diff] [blame] | 74 | static constexpr int kN = GrSamplerState::kWrapModeCount; |
Brian Salomon | f46f19b | 2020-02-13 14:11:28 -0500 | [diff] [blame] | 75 | int w = kTestPad + 2*kN*(kImageSize.width() + 2*kDrawPad + kTestPad); |
| 76 | int h = kTestPad + 2*kN*(kImageSize.height() + 2*kDrawPad + kTestPad); |
Brian Salomon | f033404 | 2020-01-31 16:16:39 -0500 | [diff] [blame] | 77 | return {w, h}; |
| 78 | } |
| 79 | |
| 80 | void onOnceBeforeDraw() override { |
| 81 | GetResourceAsBitmap("images/mandrill_128.png", &fBitmap); |
| 82 | // Make the bitmap non-square to detect any width/height confusion. |
| 83 | fBitmap.extractSubset(&fBitmap, SkIRect::MakeSize(fBitmap.dimensions()).makeInset(0, 20)); |
| 84 | SkASSERT(fBitmap.dimensions() == kImageSize); |
| 85 | } |
| 86 | |
Robert Phillips | 7a0d3c3 | 2021-07-21 15:39:51 -0400 | [diff] [blame] | 87 | DrawResult onDraw(GrRecordingContext* rContext, SkCanvas* canvas, SkString* errorMsg) override { |
| 88 | auto sdc = SkCanvasPriv::TopDeviceSurfaceDrawContext(canvas); |
| 89 | if (!sdc) { |
| 90 | *errorMsg = kErrorMsg_DrawSkippedGpuOnly; |
Brian Salomon | e69b9ef | 2020-07-22 11:18:06 -0400 | [diff] [blame] | 91 | return DrawResult::kSkip; |
| 92 | } |
Robert Phillips | 7a0d3c3 | 2021-07-21 15:39:51 -0400 | [diff] [blame] | 93 | |
| 94 | GrMipmapped mipmapped = (fMipmapMode != MipmapMode::kNone) ? GrMipmapped::kYes |
| 95 | : GrMipmapped::kNo; |
| 96 | if (mipmapped == GrMipmapped::kYes && !rContext->priv().caps()->mipmapSupport()) { |
| 97 | return DrawResult::kSkip; |
| 98 | } |
| 99 | auto view = std::get<0>(GrMakeCachedBitmapProxyView(rContext, fBitmap, mipmapped)); |
Brian Salomon | ecbb0fb | 2020-02-28 18:07:32 -0500 | [diff] [blame] | 100 | if (!view) { |
Brian Salomon | f033404 | 2020-01-31 16:16:39 -0500 | [diff] [blame] | 101 | *errorMsg = "Failed to create proxy."; |
| 102 | return DrawResult::kFail; |
| 103 | } |
| 104 | |
| 105 | SkIRect texelSubset; |
| 106 | // Use a smaller subset when upscaling so that wrap is hit on all sides of the rect we |
| 107 | // will draw. |
| 108 | if (fUpscale) { |
| 109 | texelSubset = SkIRect::MakeXYWH(fBitmap.width()/3 - 1, 2*fBitmap.height()/5 - 1, |
| 110 | fBitmap.width()/4 + 2, fBitmap.height()/5 + 2); |
| 111 | } else { |
| 112 | texelSubset = SkIRect::MakeXYWH( fBitmap.width()/8 - 1, fBitmap.height()/7 - 1, |
| 113 | 3*fBitmap.width()/5 + 2, 4*fBitmap.height()/5 + 2); |
| 114 | } |
| 115 | |
| 116 | SkTArray<SkMatrix> textureMatrices; |
| 117 | |
| 118 | SkRect a = SkRect::Make(texelSubset); |
Brian Salomon | 63a04a8 | 2020-02-03 10:47:05 -0500 | [diff] [blame] | 119 | SkRect b = fUpscale ? a.makeInset (.31f * a.width(), .31f * a.height()) |
Brian Salomon | f033404 | 2020-01-31 16:16:39 -0500 | [diff] [blame] | 120 | : a.makeOutset(.25f * a.width(), .25f * a.height()); |
Mike Reed | 2ac6ce8 | 2021-01-15 12:26:22 -0500 | [diff] [blame] | 121 | textureMatrices.push_back() = SkMatrix::RectToRect(a, b); |
Brian Salomon | f033404 | 2020-01-31 16:16:39 -0500 | [diff] [blame] | 122 | |
| 123 | b = fUpscale ? a.makeInset (.25f * a.width(), .35f * a.height()) |
| 124 | : a.makeOutset(.20f * a.width(), .35f * a.height()); |
Mike Reed | 2ac6ce8 | 2021-01-15 12:26:22 -0500 | [diff] [blame] | 125 | textureMatrices.push_back() = SkMatrix::RectToRect(a, b); |
Brian Salomon | f033404 | 2020-01-31 16:16:39 -0500 | [diff] [blame] | 126 | textureMatrices.back().preRotate(45.f, a.centerX(), a.centerY()); |
| 127 | textureMatrices.back().postSkew(.05f, -.05f); |
| 128 | |
Brian Salomon | f033404 | 2020-01-31 16:16:39 -0500 | [diff] [blame] | 129 | SkBitmap subsetBmp; |
| 130 | fBitmap.extractSubset(&subsetBmp, texelSubset); |
| 131 | subsetBmp.setImmutable(); |
Robert Phillips | 7a0d3c3 | 2021-07-21 15:39:51 -0400 | [diff] [blame] | 132 | auto subsetView = std::get<0>(GrMakeCachedBitmapProxyView(rContext, subsetBmp, mipmapped)); |
Brian Salomon | f033404 | 2020-01-31 16:16:39 -0500 | [diff] [blame] | 133 | |
| 134 | SkRect localRect = SkRect::Make(fBitmap.bounds()).makeOutset(kDrawPad, kDrawPad); |
| 135 | |
| 136 | auto size = this->onISize(); |
| 137 | |
| 138 | SkScalar y = kDrawPad + kTestPad; |
| 139 | SkRect drawRect; |
| 140 | for (int tm = 0; tm < textureMatrices.count(); ++tm) { |
Brian Salomon | 16033c9 | 2020-02-13 11:25:11 -0500 | [diff] [blame] | 141 | for (int my = 0; my < GrSamplerState::kWrapModeCount; ++my) { |
Brian Salomon | f033404 | 2020-01-31 16:16:39 -0500 | [diff] [blame] | 142 | SkScalar x = kDrawPad + kTestPad; |
Brian Salomon | e69b9ef | 2020-07-22 11:18:06 -0400 | [diff] [blame] | 143 | auto wmy = static_cast<Wrap>(my); |
Brian Salomon | 16033c9 | 2020-02-13 11:25:11 -0500 | [diff] [blame] | 144 | for (int mx = 0; mx < GrSamplerState::kWrapModeCount; ++mx) { |
Brian Salomon | e69b9ef | 2020-07-22 11:18:06 -0400 | [diff] [blame] | 145 | auto wmx = static_cast<Wrap>(mx); |
Brian Salomon | 16033c9 | 2020-02-13 11:25:11 -0500 | [diff] [blame] | 146 | |
Robert Phillips | 7a0d3c3 | 2021-07-21 15:39:51 -0400 | [diff] [blame] | 147 | const auto& caps = *rContext->priv().caps(); |
Brian Salomon | 16033c9 | 2020-02-13 11:25:11 -0500 | [diff] [blame] | 148 | |
Brian Salomon | e69b9ef | 2020-07-22 11:18:06 -0400 | [diff] [blame] | 149 | GrSamplerState sampler(wmx, wmy, fFilter, fMipmapMode); |
Brian Salomon | 16033c9 | 2020-02-13 11:25:11 -0500 | [diff] [blame] | 150 | |
Brian Salomon | f033404 | 2020-01-31 16:16:39 -0500 | [diff] [blame] | 151 | drawRect = localRect.makeOffset(x, y); |
Brian Salomon | 16033c9 | 2020-02-13 11:25:11 -0500 | [diff] [blame] | 152 | |
| 153 | std::unique_ptr<GrFragmentProcessor> fp1; |
Brian Salomon | b3779f5 | 2020-02-14 11:22:52 -0500 | [diff] [blame] | 154 | fp1 = GrTextureEffect::MakeSubset(view, |
| 155 | fBitmap.alphaType(), |
| 156 | textureMatrices[tm], |
| 157 | sampler, |
| 158 | SkRect::Make(texelSubset), |
| 159 | caps); |
Brian Salomon | f46f19b | 2020-02-13 14:11:28 -0500 | [diff] [blame] | 160 | if (!fp1) { |
| 161 | continue; |
Brian Salomon | 16033c9 | 2020-02-13 11:25:11 -0500 | [diff] [blame] | 162 | } |
Brian Salomon | f46f19b | 2020-02-13 14:11:28 -0500 | [diff] [blame] | 163 | |
Brian Salomon | f033404 | 2020-01-31 16:16:39 -0500 | [diff] [blame] | 164 | // Throw a translate in the local matrix just to test having something other |
| 165 | // than identity. Compensate with an offset local rect. |
| 166 | static constexpr SkVector kT = {-100, 300}; |
Robert Phillips | 7a0d3c3 | 2021-07-21 15:39:51 -0400 | [diff] [blame] | 167 | if (auto op = sk_gpu_test::test_ops::MakeRect(rContext, |
Brian Salomon | f033404 | 2020-01-31 16:16:39 -0500 | [diff] [blame] | 168 | std::move(fp1), |
| 169 | drawRect, |
| 170 | localRect.makeOffset(kT), |
Mike Reed | 1f60733 | 2020-05-21 12:11:27 -0400 | [diff] [blame] | 171 | SkMatrix::Translate(-kT))) { |
Robert Phillips | 7a0d3c3 | 2021-07-21 15:39:51 -0400 | [diff] [blame] | 172 | sdc->addDrawOp(std::move(op)); |
Brian Salomon | f033404 | 2020-01-31 16:16:39 -0500 | [diff] [blame] | 173 | } |
Brian Salomon | 16033c9 | 2020-02-13 11:25:11 -0500 | [diff] [blame] | 174 | |
Brian Salomon | f033404 | 2020-01-31 16:16:39 -0500 | [diff] [blame] | 175 | x += localRect.width() + kTestPad; |
| 176 | |
Brian Salomon | 16033c9 | 2020-02-13 11:25:11 -0500 | [diff] [blame] | 177 | // Now draw with a subsetted proxy using fixed function texture sampling |
| 178 | // rather than a texture subset as a comparison. |
| 179 | drawRect = localRect.makeOffset(x, y); |
Brian Salomon | f033404 | 2020-01-31 16:16:39 -0500 | [diff] [blame] | 180 | SkMatrix subsetTextureMatrix = SkMatrix::Concat( |
Mike Reed | 1f60733 | 2020-05-21 12:11:27 -0400 | [diff] [blame] | 181 | SkMatrix::Translate(-texelSubset.topLeft()), textureMatrices[tm]); |
Brian Salomon | f033404 | 2020-01-31 16:16:39 -0500 | [diff] [blame] | 182 | |
Brian Salomon | e69b9ef | 2020-07-22 11:18:06 -0400 | [diff] [blame] | 183 | auto fp2 = GrTextureEffect::Make(subsetView, |
| 184 | fBitmap.alphaType(), |
Greg Daniel | d2ccbb5 | 2020-02-05 10:45:39 -0500 | [diff] [blame] | 185 | subsetTextureMatrix, |
Brian Salomon | e69b9ef | 2020-07-22 11:18:06 -0400 | [diff] [blame] | 186 | sampler, |
| 187 | caps); |
Robert Phillips | 7a0d3c3 | 2021-07-21 15:39:51 -0400 | [diff] [blame] | 188 | if (auto op = sk_gpu_test::test_ops::MakeRect(rContext, std::move(fp2), |
| 189 | drawRect, localRect)) { |
| 190 | sdc->addDrawOp(std::move(op)); |
Brian Salomon | f033404 | 2020-01-31 16:16:39 -0500 | [diff] [blame] | 191 | } |
Brian Salomon | 16033c9 | 2020-02-13 11:25:11 -0500 | [diff] [blame] | 192 | |
| 193 | if (mx < GrSamplerState::kWrapModeCount - 1) { |
Brian Salomon | f1e316f | 2020-02-02 07:57:38 -0500 | [diff] [blame] | 194 | SkScalar midX = |
| 195 | SkScalarFloorToScalar(drawRect.right() + kTestPad/2.f) + 0.5f; |
| 196 | canvas->drawLine({midX, -1}, {midX, (float)size.fHeight+1}, {}); |
Brian Salomon | f033404 | 2020-01-31 16:16:39 -0500 | [diff] [blame] | 197 | } |
| 198 | x += localRect.width() + kTestPad; |
| 199 | } |
Brian Salomon | 16033c9 | 2020-02-13 11:25:11 -0500 | [diff] [blame] | 200 | if (my < GrSamplerState::kWrapModeCount - 1) { |
Brian Salomon | f1e316f | 2020-02-02 07:57:38 -0500 | [diff] [blame] | 201 | SkScalar midY = SkScalarFloorToScalar(drawRect.bottom() + kTestPad/2.f) + 0.5f; |
| 202 | canvas->drawLine({-1, midY}, {(float)size.fWidth+1, midY}, {}); |
Brian Salomon | f033404 | 2020-01-31 16:16:39 -0500 | [diff] [blame] | 203 | } |
| 204 | y += localRect.height() + kTestPad; |
| 205 | } |
| 206 | if (tm < textureMatrices.count() - 1) { |
| 207 | SkPaint paint; |
| 208 | paint.setColor(SK_ColorRED); |
Brian Salomon | f1e316f | 2020-02-02 07:57:38 -0500 | [diff] [blame] | 209 | SkScalar midY = SkScalarFloorToScalar(drawRect.bottom() + kTestPad/2.f) + 0.5f; |
| 210 | canvas->drawLine({-1, midY}, {(float)size.fWidth + 1, midY}, paint); |
Brian Salomon | f033404 | 2020-01-31 16:16:39 -0500 | [diff] [blame] | 211 | } |
| 212 | } |
| 213 | return DrawResult::kOk; |
| 214 | } |
| 215 | |
| 216 | private: |
| 217 | static constexpr SkISize kImageSize = {128, 88}; |
| 218 | static constexpr SkScalar kDrawPad = 10.f; |
| 219 | static constexpr SkScalar kTestPad = 10.f; |
| 220 | SkBitmap fBitmap; |
Brian Salomon | e69b9ef | 2020-07-22 11:18:06 -0400 | [diff] [blame] | 221 | Filter fFilter; |
| 222 | MipmapMode fMipmapMode; |
Brian Salomon | f033404 | 2020-01-31 16:16:39 -0500 | [diff] [blame] | 223 | bool fUpscale; |
| 224 | |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 225 | using INHERITED = GM; |
Brian Salomon | f033404 | 2020-01-31 16:16:39 -0500 | [diff] [blame] | 226 | }; |
| 227 | |
Brian Salomon | f735351 | 2020-07-22 19:26:48 -0400 | [diff] [blame] | 228 | DEF_GM(return new TexelSubset(Filter::kNearest, MipmapMode::kNone , false);) |
| 229 | DEF_GM(return new TexelSubset(Filter::kNearest, MipmapMode::kNone , true );) |
| 230 | DEF_GM(return new TexelSubset(Filter::kLinear , MipmapMode::kNone , false);) |
| 231 | DEF_GM(return new TexelSubset(Filter::kLinear , MipmapMode::kNone , true );) |
Brian Salomon | f033404 | 2020-01-31 16:16:39 -0500 | [diff] [blame] | 232 | // It doesn't make sense to have upscaling MIP map. |
Brian Salomon | f735351 | 2020-07-22 19:26:48 -0400 | [diff] [blame] | 233 | DEF_GM(return new TexelSubset(Filter::kNearest, MipmapMode::kNearest, false);) |
| 234 | DEF_GM(return new TexelSubset(Filter::kLinear , MipmapMode::kNearest, false);) |
| 235 | DEF_GM(return new TexelSubset(Filter::kNearest, MipmapMode::kLinear , false);) |
| 236 | DEF_GM(return new TexelSubset(Filter::kLinear , MipmapMode::kLinear , false);) |
Brian Salomon | f033404 | 2020-01-31 16:16:39 -0500 | [diff] [blame] | 237 | |
John Stiles | a6841be | 2020-08-06 14:11:56 -0400 | [diff] [blame] | 238 | } // namespace skiagm |