blob: 959cbbedb9b54b8438feb481fcccbffc3c86f6c9 [file] [log] [blame]
Brian Salomonf0334042020-01-31 16:16:39 -05001/*
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"
Kevin Lubickdc6cc022023-01-13 11:24:27 -050019#include "include/private/base/SkTArray.h"
Robert Phillips7a0d3c32021-07-21 15:39:51 -040020#include "src/core/SkCanvasPriv.h"
Kevin Lubickacdc1082023-06-09 11:05:24 -040021#include "src/gpu/ganesh/GrCanvas.h"
Kevin Lubick502553f2022-11-28 12:42:38 -050022#include "src/gpu/ganesh/GrCaps.h"
Greg Daniel719239c2022-04-07 11:20:24 -040023#include "src/gpu/ganesh/GrDirectContextPriv.h"
24#include "src/gpu/ganesh/GrProxyProvider.h"
25#include "src/gpu/ganesh/GrSamplerState.h"
26#include "src/gpu/ganesh/SkGr.h"
Robert Phillipsc1b94082022-08-09 17:16:19 -040027#include "src/gpu/ganesh/SurfaceDrawContext.h"
Greg Daniel719239c2022-04-07 11:20:24 -040028#include "src/gpu/ganesh/effects/GrTextureEffect.h"
Kevin Lubick8b741882023-10-06 11:41:38 -040029#include "tools/DecodeUtils.h"
Brian Salomonf0334042020-01-31 16:16:39 -050030#include "tools/Resources.h"
31#include "tools/gpu/TestOps.h"
32
33#include <memory>
34#include <utility>
35
Herb Derbyec96c212023-03-06 10:31:22 -050036using namespace skia_private;
37
Brian Salomone69b9ef2020-07-22 11:18:06 -040038using MipmapMode = GrSamplerState::MipmapMode;
39using Filter = GrSamplerState::Filter;
40using Wrap = GrSamplerState::WrapMode;
41
Brian Salomonf0334042020-01-31 16:16:39 -050042namespace skiagm {
43/**
44 * This GM directly exercises GrTextureEffect::MakeTexelSubset.
45 */
46class TexelSubset : public GpuGM {
47public:
Brian Salomone69b9ef2020-07-22 11:18:06 -040048 TexelSubset(Filter filter, MipmapMode mm, bool upscale)
49 : fFilter(filter), fMipmapMode(mm), fUpscale(upscale) {
Brian Salomonf0334042020-01-31 16:16:39 -050050 this->setBGColor(0xFFFFFFFF);
51 }
52
53protected:
Leandro Lovisolo24fa2112023-08-15 19:05:17 +000054 SkString getName() const override {
Brian Salomonf0334042020-01-31 16:16:39 -050055 SkString name("texel_subset");
56 switch (fFilter) {
Brian Salomone69b9ef2020-07-22 11:18:06 -040057 case Filter::kNearest:
Brian Salomonf0334042020-01-31 16:16:39 -050058 name.append("_nearest");
59 break;
Brian Salomone69b9ef2020-07-22 11:18:06 -040060 case Filter::kLinear:
61 name.append("_linear");
Brian Salomonf0334042020-01-31 16:16:39 -050062 break;
63 }
Brian Salomonf7353512020-07-22 19:26:48 -040064 switch (fMipmapMode) {
65 case MipmapMode::kNone:
66 break;
67 case MipmapMode::kNearest:
68 name.append("_mipmap_nearest");
69 break;
70 case MipmapMode::kLinear:
71 name.append("_mipmap_linear");
72 break;
73 }
Brian Salomonf0334042020-01-31 16:16:39 -050074 name.append(fUpscale ? "_up" : "_down");
75 return name;
76 }
77
Leandro Lovisolo8f023882023-08-15 21:13:52 +000078 SkISize getISize() override {
Brian Salomon16033c92020-02-13 11:25:11 -050079 static constexpr int kN = GrSamplerState::kWrapModeCount;
Brian Salomonf46f19b2020-02-13 14:11:28 -050080 int w = kTestPad + 2*kN*(kImageSize.width() + 2*kDrawPad + kTestPad);
81 int h = kTestPad + 2*kN*(kImageSize.height() + 2*kDrawPad + kTestPad);
Brian Salomonf0334042020-01-31 16:16:39 -050082 return {w, h};
83 }
84
85 void onOnceBeforeDraw() override {
Kevin Lubick8b741882023-10-06 11:41:38 -040086 SkAssertResult(ToolUtils::GetResourceAsBitmap("images/mandrill_128.png", &fBitmap));
Brian Salomonf0334042020-01-31 16:16:39 -050087 // Make the bitmap non-square to detect any width/height confusion.
88 fBitmap.extractSubset(&fBitmap, SkIRect::MakeSize(fBitmap.dimensions()).makeInset(0, 20));
89 SkASSERT(fBitmap.dimensions() == kImageSize);
90 }
91
Robert Phillips7a0d3c32021-07-21 15:39:51 -040092 DrawResult onDraw(GrRecordingContext* rContext, SkCanvas* canvas, SkString* errorMsg) override {
Kevin Lubickacdc1082023-06-09 11:05:24 -040093 auto sdc = skgpu::ganesh::TopDeviceSurfaceDrawContext(canvas);
Robert Phillips7a0d3c32021-07-21 15:39:51 -040094 if (!sdc) {
95 *errorMsg = kErrorMsg_DrawSkippedGpuOnly;
Brian Salomone69b9ef2020-07-22 11:18:06 -040096 return DrawResult::kSkip;
97 }
Robert Phillips7a0d3c32021-07-21 15:39:51 -040098
Kevin Lubickdf73d162023-09-11 11:56:53 -040099 skgpu::Mipmapped mipmapped =
100 (fMipmapMode != MipmapMode::kNone) ? skgpu::Mipmapped::kYes : skgpu::Mipmapped::kNo;
101 if (mipmapped == skgpu::Mipmapped::kYes && !rContext->priv().caps()->mipmapSupport()) {
Robert Phillips7a0d3c32021-07-21 15:39:51 -0400102 return DrawResult::kSkip;
103 }
Aditya Kushwahfe1bc0a2022-06-22 18:13:00 -0700104 auto view = std::get<0>(GrMakeCachedBitmapProxyView(
105 rContext, fBitmap, /*label=*/"DrawResult_Draw_BitMap", mipmapped));
Brian Salomonecbb0fb2020-02-28 18:07:32 -0500106 if (!view) {
Brian Salomonf0334042020-01-31 16:16:39 -0500107 *errorMsg = "Failed to create proxy.";
108 return DrawResult::kFail;
109 }
110
111 SkIRect texelSubset;
112 // Use a smaller subset when upscaling so that wrap is hit on all sides of the rect we
113 // will draw.
114 if (fUpscale) {
115 texelSubset = SkIRect::MakeXYWH(fBitmap.width()/3 - 1, 2*fBitmap.height()/5 - 1,
116 fBitmap.width()/4 + 2, fBitmap.height()/5 + 2);
117 } else {
118 texelSubset = SkIRect::MakeXYWH( fBitmap.width()/8 - 1, fBitmap.height()/7 - 1,
119 3*fBitmap.width()/5 + 2, 4*fBitmap.height()/5 + 2);
120 }
121
Herb Derbyec96c212023-03-06 10:31:22 -0500122 TArray<SkMatrix> textureMatrices;
Brian Salomonf0334042020-01-31 16:16:39 -0500123
124 SkRect a = SkRect::Make(texelSubset);
Brian Salomon63a04a82020-02-03 10:47:05 -0500125 SkRect b = fUpscale ? a.makeInset (.31f * a.width(), .31f * a.height())
Brian Salomonf0334042020-01-31 16:16:39 -0500126 : a.makeOutset(.25f * a.width(), .25f * a.height());
Mike Reed2ac6ce82021-01-15 12:26:22 -0500127 textureMatrices.push_back() = SkMatrix::RectToRect(a, b);
Brian Salomonf0334042020-01-31 16:16:39 -0500128
129 b = fUpscale ? a.makeInset (.25f * a.width(), .35f * a.height())
130 : a.makeOutset(.20f * a.width(), .35f * a.height());
Mike Reed2ac6ce82021-01-15 12:26:22 -0500131 textureMatrices.push_back() = SkMatrix::RectToRect(a, b);
Brian Salomonf0334042020-01-31 16:16:39 -0500132 textureMatrices.back().preRotate(45.f, a.centerX(), a.centerY());
133 textureMatrices.back().postSkew(.05f, -.05f);
134
Brian Salomonf0334042020-01-31 16:16:39 -0500135 SkBitmap subsetBmp;
136 fBitmap.extractSubset(&subsetBmp, texelSubset);
137 subsetBmp.setImmutable();
Aditya Kushwahfe1bc0a2022-06-22 18:13:00 -0700138 auto subsetView = std::get<0>(GrMakeCachedBitmapProxyView(
139 rContext, subsetBmp, /*label=*/"DrawResult_Draw_SubsetBitMap", mipmapped));
Brian Salomonf0334042020-01-31 16:16:39 -0500140
141 SkRect localRect = SkRect::Make(fBitmap.bounds()).makeOutset(kDrawPad, kDrawPad);
142
Leandro Lovisolo8f023882023-08-15 21:13:52 +0000143 auto size = this->getISize();
Brian Salomonf0334042020-01-31 16:16:39 -0500144
145 SkScalar y = kDrawPad + kTestPad;
146 SkRect drawRect;
Herb Derbyffacce52022-11-09 10:51:34 -0500147 for (int tm = 0; tm < textureMatrices.size(); ++tm) {
Brian Salomon16033c92020-02-13 11:25:11 -0500148 for (int my = 0; my < GrSamplerState::kWrapModeCount; ++my) {
Brian Salomonf0334042020-01-31 16:16:39 -0500149 SkScalar x = kDrawPad + kTestPad;
Brian Salomone69b9ef2020-07-22 11:18:06 -0400150 auto wmy = static_cast<Wrap>(my);
Brian Salomon16033c92020-02-13 11:25:11 -0500151 for (int mx = 0; mx < GrSamplerState::kWrapModeCount; ++mx) {
Brian Salomone69b9ef2020-07-22 11:18:06 -0400152 auto wmx = static_cast<Wrap>(mx);
Brian Salomon16033c92020-02-13 11:25:11 -0500153
Robert Phillips7a0d3c32021-07-21 15:39:51 -0400154 const auto& caps = *rContext->priv().caps();
Brian Salomon16033c92020-02-13 11:25:11 -0500155
Brian Salomone69b9ef2020-07-22 11:18:06 -0400156 GrSamplerState sampler(wmx, wmy, fFilter, fMipmapMode);
Brian Salomon16033c92020-02-13 11:25:11 -0500157
Brian Salomonf0334042020-01-31 16:16:39 -0500158 drawRect = localRect.makeOffset(x, y);
Brian Salomon16033c92020-02-13 11:25:11 -0500159
160 std::unique_ptr<GrFragmentProcessor> fp1;
Brian Salomonb3779f52020-02-14 11:22:52 -0500161 fp1 = GrTextureEffect::MakeSubset(view,
162 fBitmap.alphaType(),
163 textureMatrices[tm],
164 sampler,
165 SkRect::Make(texelSubset),
166 caps);
Brian Salomonf46f19b2020-02-13 14:11:28 -0500167 if (!fp1) {
168 continue;
Brian Salomon16033c92020-02-13 11:25:11 -0500169 }
Brian Salomonf46f19b2020-02-13 14:11:28 -0500170
Brian Salomonf0334042020-01-31 16:16:39 -0500171 // Throw a translate in the local matrix just to test having something other
172 // than identity. Compensate with an offset local rect.
173 static constexpr SkVector kT = {-100, 300};
Robert Phillips7a0d3c32021-07-21 15:39:51 -0400174 if (auto op = sk_gpu_test::test_ops::MakeRect(rContext,
Brian Salomonf0334042020-01-31 16:16:39 -0500175 std::move(fp1),
176 drawRect,
177 localRect.makeOffset(kT),
Mike Reed1f607332020-05-21 12:11:27 -0400178 SkMatrix::Translate(-kT))) {
Robert Phillips7a0d3c32021-07-21 15:39:51 -0400179 sdc->addDrawOp(std::move(op));
Brian Salomonf0334042020-01-31 16:16:39 -0500180 }
Brian Salomon16033c92020-02-13 11:25:11 -0500181
Brian Salomonf0334042020-01-31 16:16:39 -0500182 x += localRect.width() + kTestPad;
183
Brian Salomon16033c92020-02-13 11:25:11 -0500184 // Now draw with a subsetted proxy using fixed function texture sampling
185 // rather than a texture subset as a comparison.
186 drawRect = localRect.makeOffset(x, y);
Brian Salomonf0334042020-01-31 16:16:39 -0500187 SkMatrix subsetTextureMatrix = SkMatrix::Concat(
Mike Reed1f607332020-05-21 12:11:27 -0400188 SkMatrix::Translate(-texelSubset.topLeft()), textureMatrices[tm]);
Brian Salomonf0334042020-01-31 16:16:39 -0500189
Brian Salomone69b9ef2020-07-22 11:18:06 -0400190 auto fp2 = GrTextureEffect::Make(subsetView,
191 fBitmap.alphaType(),
Greg Danield2ccbb52020-02-05 10:45:39 -0500192 subsetTextureMatrix,
Brian Salomone69b9ef2020-07-22 11:18:06 -0400193 sampler,
194 caps);
Robert Phillips7a0d3c32021-07-21 15:39:51 -0400195 if (auto op = sk_gpu_test::test_ops::MakeRect(rContext, std::move(fp2),
196 drawRect, localRect)) {
197 sdc->addDrawOp(std::move(op));
Brian Salomonf0334042020-01-31 16:16:39 -0500198 }
Brian Salomon16033c92020-02-13 11:25:11 -0500199
200 if (mx < GrSamplerState::kWrapModeCount - 1) {
Brian Salomonf1e316f2020-02-02 07:57:38 -0500201 SkScalar midX =
202 SkScalarFloorToScalar(drawRect.right() + kTestPad/2.f) + 0.5f;
203 canvas->drawLine({midX, -1}, {midX, (float)size.fHeight+1}, {});
Brian Salomonf0334042020-01-31 16:16:39 -0500204 }
205 x += localRect.width() + kTestPad;
206 }
Brian Salomon16033c92020-02-13 11:25:11 -0500207 if (my < GrSamplerState::kWrapModeCount - 1) {
Brian Salomonf1e316f2020-02-02 07:57:38 -0500208 SkScalar midY = SkScalarFloorToScalar(drawRect.bottom() + kTestPad/2.f) + 0.5f;
209 canvas->drawLine({-1, midY}, {(float)size.fWidth+1, midY}, {});
Brian Salomonf0334042020-01-31 16:16:39 -0500210 }
211 y += localRect.height() + kTestPad;
212 }
Herb Derbyffacce52022-11-09 10:51:34 -0500213 if (tm < textureMatrices.size() - 1) {
Brian Salomonf0334042020-01-31 16:16:39 -0500214 SkPaint paint;
215 paint.setColor(SK_ColorRED);
Brian Salomonf1e316f2020-02-02 07:57:38 -0500216 SkScalar midY = SkScalarFloorToScalar(drawRect.bottom() + kTestPad/2.f) + 0.5f;
217 canvas->drawLine({-1, midY}, {(float)size.fWidth + 1, midY}, paint);
Brian Salomonf0334042020-01-31 16:16:39 -0500218 }
219 }
220 return DrawResult::kOk;
221 }
222
223private:
Brian Salomon9fa47cc2021-10-08 18:48:26 -0400224 inline static constexpr SkISize kImageSize = {128, 88};
225 inline static constexpr SkScalar kDrawPad = 10.f;
226 inline static constexpr SkScalar kTestPad = 10.f;
Brian Salomonf0334042020-01-31 16:16:39 -0500227 SkBitmap fBitmap;
Brian Salomone69b9ef2020-07-22 11:18:06 -0400228 Filter fFilter;
229 MipmapMode fMipmapMode;
Brian Salomonf0334042020-01-31 16:16:39 -0500230 bool fUpscale;
231
John Stiles7571f9e2020-09-02 22:42:33 -0400232 using INHERITED = GM;
Brian Salomonf0334042020-01-31 16:16:39 -0500233};
234
Brian Salomonf7353512020-07-22 19:26:48 -0400235DEF_GM(return new TexelSubset(Filter::kNearest, MipmapMode::kNone , false);)
236DEF_GM(return new TexelSubset(Filter::kNearest, MipmapMode::kNone , true );)
237DEF_GM(return new TexelSubset(Filter::kLinear , MipmapMode::kNone , false);)
238DEF_GM(return new TexelSubset(Filter::kLinear , MipmapMode::kNone , true );)
Brian Salomonf0334042020-01-31 16:16:39 -0500239// It doesn't make sense to have upscaling MIP map.
Brian Salomonf7353512020-07-22 19:26:48 -0400240DEF_GM(return new TexelSubset(Filter::kNearest, MipmapMode::kNearest, false);)
241DEF_GM(return new TexelSubset(Filter::kLinear , MipmapMode::kNearest, false);)
242DEF_GM(return new TexelSubset(Filter::kNearest, MipmapMode::kLinear , false);)
243DEF_GM(return new TexelSubset(Filter::kLinear , MipmapMode::kLinear , false);)
Brian Salomonf0334042020-01-31 16:16:39 -0500244
John Stilesa6841be2020-08-06 14:11:56 -0400245} // namespace skiagm