Debian Codegraph @

gm/texelsubset.cpp

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