blob: bf1f488d708d166b1d40376dbe533b70f20330d9 [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"
Brian Salomonf0334042020-01-31 16:16:39 -050019#include "include/private/SkTArray.h"
Robert Phillips7a0d3c32021-07-21 15:39:51 -040020#include "src/core/SkCanvasPriv.h"
Adlai Hollera0693042020-10-14 11:23:11 -040021#include "src/gpu/GrDirectContextPriv.h"
Brian Salomonf0334042020-01-31 16:16:39 -050022#include "src/gpu/GrProxyProvider.h"
Brian Salomonf0334042020-01-31 16:16:39 -050023#include "src/gpu/GrSamplerState.h"
Brian Salomon27c42022021-04-28 12:39:21 -040024#include "src/gpu/SkGr.h"
Robert Phillips550de7f2021-07-06 16:28:52 -040025#include "src/gpu/effects/GrTextureEffect.h"
Robert Phillips4dca8312021-07-28 15:13:20 -040026#include "src/gpu/v1/SurfaceDrawContext_v1.h"
Brian Salomonf0334042020-01-31 16:16:39 -050027#include "tools/Resources.h"
28#include "tools/gpu/TestOps.h"
29
30#include <memory>
31#include <utility>
32
Brian Salomone69b9ef2020-07-22 11:18:06 -040033using MipmapMode = GrSamplerState::MipmapMode;
34using Filter = GrSamplerState::Filter;
35using Wrap = GrSamplerState::WrapMode;
36
Brian Salomonf0334042020-01-31 16:16:39 -050037namespace skiagm {
38/**
39 * This GM directly exercises GrTextureEffect::MakeTexelSubset.
40 */
41class TexelSubset : public GpuGM {
42public:
Brian Salomone69b9ef2020-07-22 11:18:06 -040043 TexelSubset(Filter filter, MipmapMode mm, bool upscale)
44 : fFilter(filter), fMipmapMode(mm), fUpscale(upscale) {
Brian Salomonf0334042020-01-31 16:16:39 -050045 this->setBGColor(0xFFFFFFFF);
46 }
47
48protected:
49 SkString onShortName() override {
50 SkString name("texel_subset");
51 switch (fFilter) {
Brian Salomone69b9ef2020-07-22 11:18:06 -040052 case Filter::kNearest:
Brian Salomonf0334042020-01-31 16:16:39 -050053 name.append("_nearest");
54 break;
Brian Salomone69b9ef2020-07-22 11:18:06 -040055 case Filter::kLinear:
56 name.append("_linear");
Brian Salomonf0334042020-01-31 16:16:39 -050057 break;
58 }
Brian Salomonf7353512020-07-22 19:26:48 -040059 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 Salomonf0334042020-01-31 16:16:39 -050069 name.append(fUpscale ? "_up" : "_down");
70 return name;
71 }
72
73 SkISize onISize() override {
Brian Salomon16033c92020-02-13 11:25:11 -050074 static constexpr int kN = GrSamplerState::kWrapModeCount;
Brian Salomonf46f19b2020-02-13 14:11:28 -050075 int w = kTestPad + 2*kN*(kImageSize.width() + 2*kDrawPad + kTestPad);
76 int h = kTestPad + 2*kN*(kImageSize.height() + 2*kDrawPad + kTestPad);
Brian Salomonf0334042020-01-31 16:16:39 -050077 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 Phillips7a0d3c32021-07-21 15:39:51 -040087 DrawResult onDraw(GrRecordingContext* rContext, SkCanvas* canvas, SkString* errorMsg) override {
88 auto sdc = SkCanvasPriv::TopDeviceSurfaceDrawContext(canvas);
89 if (!sdc) {
90 *errorMsg = kErrorMsg_DrawSkippedGpuOnly;
Brian Salomone69b9ef2020-07-22 11:18:06 -040091 return DrawResult::kSkip;
92 }
Robert Phillips7a0d3c32021-07-21 15:39:51 -040093
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 Salomonecbb0fb2020-02-28 18:07:32 -0500100 if (!view) {
Brian Salomonf0334042020-01-31 16:16:39 -0500101 *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 Salomon63a04a82020-02-03 10:47:05 -0500119 SkRect b = fUpscale ? a.makeInset (.31f * a.width(), .31f * a.height())
Brian Salomonf0334042020-01-31 16:16:39 -0500120 : a.makeOutset(.25f * a.width(), .25f * a.height());
Mike Reed2ac6ce82021-01-15 12:26:22 -0500121 textureMatrices.push_back() = SkMatrix::RectToRect(a, b);
Brian Salomonf0334042020-01-31 16:16:39 -0500122
123 b = fUpscale ? a.makeInset (.25f * a.width(), .35f * a.height())
124 : a.makeOutset(.20f * a.width(), .35f * a.height());
Mike Reed2ac6ce82021-01-15 12:26:22 -0500125 textureMatrices.push_back() = SkMatrix::RectToRect(a, b);
Brian Salomonf0334042020-01-31 16:16:39 -0500126 textureMatrices.back().preRotate(45.f, a.centerX(), a.centerY());
127 textureMatrices.back().postSkew(.05f, -.05f);
128
Brian Salomonf0334042020-01-31 16:16:39 -0500129 SkBitmap subsetBmp;
130 fBitmap.extractSubset(&subsetBmp, texelSubset);
131 subsetBmp.setImmutable();
Robert Phillips7a0d3c32021-07-21 15:39:51 -0400132 auto subsetView = std::get<0>(GrMakeCachedBitmapProxyView(rContext, subsetBmp, mipmapped));
Brian Salomonf0334042020-01-31 16:16:39 -0500133
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 Salomon16033c92020-02-13 11:25:11 -0500141 for (int my = 0; my < GrSamplerState::kWrapModeCount; ++my) {
Brian Salomonf0334042020-01-31 16:16:39 -0500142 SkScalar x = kDrawPad + kTestPad;
Brian Salomone69b9ef2020-07-22 11:18:06 -0400143 auto wmy = static_cast<Wrap>(my);
Brian Salomon16033c92020-02-13 11:25:11 -0500144 for (int mx = 0; mx < GrSamplerState::kWrapModeCount; ++mx) {
Brian Salomone69b9ef2020-07-22 11:18:06 -0400145 auto wmx = static_cast<Wrap>(mx);
Brian Salomon16033c92020-02-13 11:25:11 -0500146
Robert Phillips7a0d3c32021-07-21 15:39:51 -0400147 const auto& caps = *rContext->priv().caps();
Brian Salomon16033c92020-02-13 11:25:11 -0500148
Brian Salomone69b9ef2020-07-22 11:18:06 -0400149 GrSamplerState sampler(wmx, wmy, fFilter, fMipmapMode);
Brian Salomon16033c92020-02-13 11:25:11 -0500150
Brian Salomonf0334042020-01-31 16:16:39 -0500151 drawRect = localRect.makeOffset(x, y);
Brian Salomon16033c92020-02-13 11:25:11 -0500152
153 std::unique_ptr<GrFragmentProcessor> fp1;
Brian Salomonb3779f52020-02-14 11:22:52 -0500154 fp1 = GrTextureEffect::MakeSubset(view,
155 fBitmap.alphaType(),
156 textureMatrices[tm],
157 sampler,
158 SkRect::Make(texelSubset),
159 caps);
Brian Salomonf46f19b2020-02-13 14:11:28 -0500160 if (!fp1) {
161 continue;
Brian Salomon16033c92020-02-13 11:25:11 -0500162 }
Brian Salomonf46f19b2020-02-13 14:11:28 -0500163
Brian Salomonf0334042020-01-31 16:16:39 -0500164 // 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 Phillips7a0d3c32021-07-21 15:39:51 -0400167 if (auto op = sk_gpu_test::test_ops::MakeRect(rContext,
Brian Salomonf0334042020-01-31 16:16:39 -0500168 std::move(fp1),
169 drawRect,
170 localRect.makeOffset(kT),
Mike Reed1f607332020-05-21 12:11:27 -0400171 SkMatrix::Translate(-kT))) {
Robert Phillips7a0d3c32021-07-21 15:39:51 -0400172 sdc->addDrawOp(std::move(op));
Brian Salomonf0334042020-01-31 16:16:39 -0500173 }
Brian Salomon16033c92020-02-13 11:25:11 -0500174
Brian Salomonf0334042020-01-31 16:16:39 -0500175 x += localRect.width() + kTestPad;
176
Brian Salomon16033c92020-02-13 11:25:11 -0500177 // 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 Salomonf0334042020-01-31 16:16:39 -0500180 SkMatrix subsetTextureMatrix = SkMatrix::Concat(
Mike Reed1f607332020-05-21 12:11:27 -0400181 SkMatrix::Translate(-texelSubset.topLeft()), textureMatrices[tm]);
Brian Salomonf0334042020-01-31 16:16:39 -0500182
Brian Salomone69b9ef2020-07-22 11:18:06 -0400183 auto fp2 = GrTextureEffect::Make(subsetView,
184 fBitmap.alphaType(),
Greg Danield2ccbb52020-02-05 10:45:39 -0500185 subsetTextureMatrix,
Brian Salomone69b9ef2020-07-22 11:18:06 -0400186 sampler,
187 caps);
Robert Phillips7a0d3c32021-07-21 15:39:51 -0400188 if (auto op = sk_gpu_test::test_ops::MakeRect(rContext, std::move(fp2),
189 drawRect, localRect)) {
190 sdc->addDrawOp(std::move(op));
Brian Salomonf0334042020-01-31 16:16:39 -0500191 }
Brian Salomon16033c92020-02-13 11:25:11 -0500192
193 if (mx < GrSamplerState::kWrapModeCount - 1) {
Brian Salomonf1e316f2020-02-02 07:57:38 -0500194 SkScalar midX =
195 SkScalarFloorToScalar(drawRect.right() + kTestPad/2.f) + 0.5f;
196 canvas->drawLine({midX, -1}, {midX, (float)size.fHeight+1}, {});
Brian Salomonf0334042020-01-31 16:16:39 -0500197 }
198 x += localRect.width() + kTestPad;
199 }
Brian Salomon16033c92020-02-13 11:25:11 -0500200 if (my < GrSamplerState::kWrapModeCount - 1) {
Brian Salomonf1e316f2020-02-02 07:57:38 -0500201 SkScalar midY = SkScalarFloorToScalar(drawRect.bottom() + kTestPad/2.f) + 0.5f;
202 canvas->drawLine({-1, midY}, {(float)size.fWidth+1, midY}, {});
Brian Salomonf0334042020-01-31 16:16:39 -0500203 }
204 y += localRect.height() + kTestPad;
205 }
206 if (tm < textureMatrices.count() - 1) {
207 SkPaint paint;
208 paint.setColor(SK_ColorRED);
Brian Salomonf1e316f2020-02-02 07:57:38 -0500209 SkScalar midY = SkScalarFloorToScalar(drawRect.bottom() + kTestPad/2.f) + 0.5f;
210 canvas->drawLine({-1, midY}, {(float)size.fWidth + 1, midY}, paint);
Brian Salomonf0334042020-01-31 16:16:39 -0500211 }
212 }
213 return DrawResult::kOk;
214 }
215
216private:
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 Salomone69b9ef2020-07-22 11:18:06 -0400221 Filter fFilter;
222 MipmapMode fMipmapMode;
Brian Salomonf0334042020-01-31 16:16:39 -0500223 bool fUpscale;
224
John Stiles7571f9e2020-09-02 22:42:33 -0400225 using INHERITED = GM;
Brian Salomonf0334042020-01-31 16:16:39 -0500226};
227
Brian Salomonf7353512020-07-22 19:26:48 -0400228DEF_GM(return new TexelSubset(Filter::kNearest, MipmapMode::kNone , false);)
229DEF_GM(return new TexelSubset(Filter::kNearest, MipmapMode::kNone , true );)
230DEF_GM(return new TexelSubset(Filter::kLinear , MipmapMode::kNone , false);)
231DEF_GM(return new TexelSubset(Filter::kLinear , MipmapMode::kNone , true );)
Brian Salomonf0334042020-01-31 16:16:39 -0500232// It doesn't make sense to have upscaling MIP map.
Brian Salomonf7353512020-07-22 19:26:48 -0400233DEF_GM(return new TexelSubset(Filter::kNearest, MipmapMode::kNearest, false);)
234DEF_GM(return new TexelSubset(Filter::kLinear , MipmapMode::kNearest, false);)
235DEF_GM(return new TexelSubset(Filter::kNearest, MipmapMode::kLinear , false);)
236DEF_GM(return new TexelSubset(Filter::kLinear , MipmapMode::kLinear , false);)
Brian Salomonf0334042020-01-31 16:16:39 -0500237
John Stilesa6841be2020-08-06 14:11:56 -0400238} // namespace skiagm