1 /*
2 * Copyright 2021 Google LLC
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 #include "gm/gm.h"
9 #include "include/core/SkCanvas.h"
10 #include "include/core/SkPaint.h"
11 #include "include/core/SkRect.h"
12 #include "include/core/SkSize.h"
13 #include "include/core/SkString.h"
14 #include "include/gpu/GrDirectContext.h"
15
16 #include "src/core/SkCanvasPriv.h"
17 #include "src/core/SkConvertPixels.h"
18 #include "src/gpu/GrDirectContextPriv.h"
19 #include "src/gpu/GrPaint.h"
20 #include "src/gpu/GrProxyProvider.h"
21 #include "src/gpu/GrResourceProvider.h"
22 #include "src/gpu/SkGr.h"
23 #include "src/gpu/effects/GrTextureEffect.h"
24 #include "src/gpu/v1/SurfaceDrawContext_v1.h"
25
26 #include "tools/gpu/ProxyUtils.h"
27
create_view(GrDirectContext * dContext,const SkBitmap & src,GrSurfaceOrigin origin)28 static GrSurfaceProxyView create_view(GrDirectContext* dContext,
29 const SkBitmap& src,
30 GrSurfaceOrigin origin) {
31 SkASSERT(src.colorType() == kRGBA_8888_SkColorType);
32
33 #define USE_LAZY_PROXIES 1 // Toggle this to generate the reference images
34
35 if (USE_LAZY_PROXIES) {
36 auto format = dContext->priv().caps()->getDefaultBackendFormat(GrColorType::kRGBA_8888,
37 GrRenderable::kNo);
38 if (!format.isValid()) {
39 return {};
40 }
41
42 sk_sp<GrTextureProxy> proxy = GrProxyProvider::MakeFullyLazyProxy(
43 [src](GrResourceProvider* rp,
44 const GrSurfaceProxy::LazySurfaceDesc& desc)
45 -> GrSurfaceProxy::LazyCallbackResult {
46 SkASSERT(desc.fMipmapped == GrMipmapped::kNo);
47 GrMipLevel mipLevel = {src.getPixels(), src.rowBytes(), nullptr};
48 auto colorType = SkColorTypeToGrColorType(src.colorType());
49
50 return rp->createTexture(src.dimensions(),
51 desc.fFormat,
52 desc.fTextureType,
53 colorType,
54 desc.fRenderable,
55 desc.fSampleCnt,
56 desc.fBudgeted,
57 desc.fFit,
58 desc.fProtected,
59 mipLevel);
60 },
61 format, GrRenderable::kNo, 1, GrProtected::kNo, *dContext->priv().caps(),
62 GrSurfaceProxy::UseAllocator::kYes);
63
64 if (!proxy) {
65 return {};
66 }
67
68 auto swizzle = dContext->priv().caps()->getReadSwizzle(proxy->backendFormat(),
69 GrColorType::kRGBA_8888);
70 return GrSurfaceProxyView(std::move(proxy), origin, swizzle);
71 }
72
73 return sk_gpu_test::MakeTextureProxyViewFromData(dContext,
74 GrRenderable::kNo,
75 origin,
76 src.pixmap());
77 }
78
79 // Create an over large texture which is initialized to opaque black outside of the content
80 // rect. The inside of the content rect consists of a grey coordinate frame lacking the -Y axis.
81 // The -X and +X axes have a red and green dot at their ends (respectively). Finally, the content
82 // rect has a 1-pixel wide blue border.
create_bitmap(SkIRect contentRect,SkISize fullSize,GrSurfaceOrigin origin)83 static SkBitmap create_bitmap(SkIRect contentRect, SkISize fullSize, GrSurfaceOrigin origin) {
84
85 const int kContentSize = contentRect.width();
86 SkBitmap contentBM;
87
88 {
89 SkImageInfo contentInfo = SkImageInfo::Make(kContentSize, kContentSize,
90 kRGBA_8888_SkColorType, kOpaque_SkAlphaType);
91 contentBM.allocPixels(contentInfo);
92
93 contentBM.eraseColor(SK_ColorWHITE);
94
95 const int halfM1 = kContentSize/2 - 1;
96
97 // The coordinate frame
98 contentBM.eraseArea(SkIRect::MakeXYWH(halfM1, 2,2, halfM1), SK_ColorGRAY);
99 contentBM.eraseArea(SkIRect::MakeXYWH(2, halfM1, kContentSize-4, 2), SK_ColorGRAY);
100
101 contentBM.eraseArea(SkIRect::MakeXYWH(2, halfM1, 2, 2), SK_ColorRED);
102 contentBM.eraseArea(SkIRect::MakeXYWH(kContentSize-4, halfM1, 2, 2), SK_ColorGREEN);
103
104 // The 1-pixel wide rim around the content rect
105 contentBM.eraseArea(SkIRect::MakeXYWH(0, 0, kContentSize, 1), SK_ColorBLUE);
106 contentBM.eraseArea(SkIRect::MakeXYWH(0, 0, 1, kContentSize), SK_ColorBLUE);
107 contentBM.eraseArea(SkIRect::MakeXYWH(kContentSize-1, 0, 1, kContentSize), SK_ColorBLUE);
108 contentBM.eraseArea(SkIRect::MakeXYWH(0, kContentSize-1, kContentSize, 1), SK_ColorBLUE);
109 }
110
111 SkBitmap bigBM;
112
113 {
114 const int kLeft = contentRect.fLeft;
115 const int kTop = contentRect.fTop;
116
117 SkImageInfo bigInfo = SkImageInfo::Make(fullSize.fWidth, fullSize.fHeight,
118 kRGBA_8888_SkColorType, kOpaque_SkAlphaType);
119
120 bigBM.allocPixels(bigInfo);
121
122 bigBM.eraseColor(SK_ColorBLACK);
123
124 const char* src = static_cast<const char*>(contentBM.getPixels());
125 size_t srcRB = contentBM.rowBytes();
126 size_t dstRB = bigBM.rowBytes();
127
128 if (USE_LAZY_PROXIES && origin == kBottomLeft_GrSurfaceOrigin) {
129 char* dst = static_cast<char*>(bigBM.getAddr(kLeft, fullSize.height() - kTop - 1));
130 for (int y = 0; y < contentBM.height(); ++y) {
131 memcpy(dst, src, srcRB);
132 src = src + srcRB;
133 dst = dst - dstRB;
134 }
135 } else {
136 char* dst = static_cast<char*>(bigBM.getAddr(kLeft, kTop));
137 SkRectMemcpy(dst, dstRB, src, srcRB,
138 contentBM.rowBytes(), contentBM.height());
139 }
140
141 bigBM.setAlphaType(kOpaque_SkAlphaType);
142 bigBM.setImmutable();
143 }
144
145 return bigBM;
146 }
147
draw_texture(const GrCaps * caps,skgpu::v1::SurfaceDrawContext * sdc,const GrSurfaceProxyView & src,const SkIRect & srcRect,const SkIRect & drawRect,const SkMatrix & mat,GrSamplerState::WrapMode xTileMode,GrSamplerState::WrapMode yTileMode)148 static void draw_texture(const GrCaps* caps,
149 skgpu::v1::SurfaceDrawContext* sdc,
150 const GrSurfaceProxyView& src,
151 const SkIRect& srcRect,
152 const SkIRect& drawRect,
153 const SkMatrix& mat,
154 GrSamplerState::WrapMode xTileMode,
155 GrSamplerState::WrapMode yTileMode) {
156 GrSamplerState sampler(xTileMode, yTileMode, SkFilterMode::kNearest);
157
158 auto fp = GrTextureEffect::MakeSubset(src, kOpaque_SkAlphaType, mat,
159 sampler, SkRect::Make(srcRect), *caps);
160 GrPaint paint;
161 paint.setColorFragmentProcessor(std::move(fp));
162
163 sdc->drawRect(nullptr, std::move(paint), GrAA::kNo, SkMatrix::I(), SkRect::Make(drawRect));
164 }
165
166 namespace skiagm {
167
168 // This GM exercises all the different tile modes for a texture that cannot be normalized
169 // early (i.e., rectangle or fully-lazy).
170 // TODO: should we also test w/ mipmapping?
171 class LazyTilingGM : public GpuGM {
172 public:
LazyTilingGM(GrSurfaceOrigin origin)173 LazyTilingGM(GrSurfaceOrigin origin)
174 : fOrigin(origin)
175 , fContentRect(SkIRect::MakeXYWH(kLeftContentOffset, kTopContentOffset,
176 kContentSize, kContentSize)) {
177 this->setBGColor(0xFFCCCCCC);
178 }
179
180 protected:
181
onShortName()182 SkString onShortName() override {
183 return SkStringPrintf("lazytiling_%s", fOrigin == kTopLeft_GrSurfaceOrigin ? "tl" : "bl");
184 }
185
onISize()186 SkISize onISize() override {
187 return SkISize::Make(kTotalWidth, kTotalHeight);
188 }
189
onGpuSetup(GrDirectContext * dContext,SkString * errorMsg)190 DrawResult onGpuSetup(GrDirectContext* dContext, SkString* errorMsg) override {
191 if (!dContext || dContext->abandoned()) {
192 return DrawResult::kSkip;
193 }
194
195 auto bm = create_bitmap(fContentRect,
196 { kLeftContentOffset + kContentSize + kRightContentPadding,
197 kTopContentOffset + kContentSize + kBottomContentPadding },
198 fOrigin);
199
200 fView = create_view(dContext, bm, fOrigin);
201 if (!fView.proxy()) {
202 *errorMsg = "Failed to create proxy";
203 return DrawResult::kFail;
204 }
205
206 return DrawResult::kOk;
207 }
208
onDraw(GrRecordingContext * rContext,SkCanvas * canvas,SkString * errorMsg)209 DrawResult onDraw(GrRecordingContext* rContext, SkCanvas* canvas, SkString* errorMsg) override {
210 SkSamplingOptions sampling(SkFilterMode::kNearest, SkMipmapMode::kNone);
211 SkPaint p;
212
213 auto sdc = SkCanvasPriv::TopDeviceSurfaceDrawContext(canvas);
214 if (!sdc) {
215 *errorMsg = kErrorMsg_DrawSkippedGpuOnly;
216 return DrawResult::kSkip;
217 }
218
219 int y = kPad;
220 for (auto yMode : { SkTileMode::kClamp, SkTileMode::kRepeat,
221 SkTileMode::kMirror, SkTileMode::kDecal }) {
222 int x = kPad;
223 for (auto xMode : { SkTileMode::kClamp, SkTileMode::kRepeat,
224 SkTileMode::kMirror, SkTileMode::kDecal }) {
225 SkIRect cellRect = SkIRect::MakeXYWH(x, y, 2*kContentSize, 2*kContentSize);
226 SkRect contentRect = SkRect::MakeXYWH(x+kContentSize/2, y+kContentSize/2,
227 kContentSize, kContentSize);
228
229 SkMatrix texMatrix = SkMatrix::RectToRect(contentRect, SkRect::Make(fContentRect));
230
231 draw_texture(rContext->priv().caps(),
232 sdc,
233 fView,
234 fContentRect,
235 cellRect,
236 texMatrix,
237 SkTileModeToWrapMode(xMode),
238 SkTileModeToWrapMode(yMode));
239
240 x += 2*kContentSize+kPad;
241 }
242
243 y += 2*kContentSize+kPad;
244 }
245
246 return DrawResult::kOk;
247 }
248
249 private:
250 inline static constexpr int kLeftContentOffset = 8;
251 inline static constexpr int kTopContentOffset = 16;
252 inline static constexpr int kRightContentPadding = 24;
253 inline static constexpr int kBottomContentPadding = 80;
254
255 inline static constexpr int kPad = 4; // on-screen padding between cells
256
257 inline static constexpr int kContentSize = 32;
258
259 // Each cell in this GM's grid is a square - 2*kContentSize on a side
260 inline static constexpr int kTotalWidth = (2*kContentSize+kPad) * kSkTileModeCount + kPad;
261 inline static constexpr int kTotalHeight = (2*kContentSize+kPad) * kSkTileModeCount + kPad;
262
263 GrSurfaceOrigin fOrigin;
264 SkIRect fContentRect;
265 GrSurfaceProxyView fView;
266
267 using INHERITED = GM;
268 };
269
270 //////////////////////////////////////////////////////////////////////////////
271
272 DEF_GM(return new LazyTilingGM(kTopLeft_GrSurfaceOrigin);)
273 DEF_GM(return new LazyTilingGM(kBottomLeft_GrSurfaceOrigin);)
274
275 } // namespace skiagm
276