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