blob: a9bbeed5a84dc4dca714a0abc84addbeee0644dc [file] [log] [blame]
Robert Phillips94e67912021-01-21 13:39:08 -05001/*
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"
Kevin Lubick677a12f2022-03-09 08:23:14 -05009#include "include/core/SkBitmap.h"
Robert Phillips94e67912021-01-21 13:39:08 -050010#include "include/core/SkCanvas.h"
11#include "include/core/SkPaint.h"
12#include "include/core/SkRect.h"
13#include "include/core/SkSize.h"
14#include "include/core/SkString.h"
15#include "include/gpu/GrDirectContext.h"
Kevin Lubick406dacd2023-03-23 10:15:46 -040016#include "src/base/SkRectMemcpy.h"
Robert Phillips7a0d3c32021-07-21 15:39:51 -040017#include "src/core/SkCanvasPriv.h"
Robert Phillips94e67912021-01-21 13:39:08 -050018#include "src/core/SkConvertPixels.h"
Kevin Lubickacdc1082023-06-09 11:05:24 -040019#include "src/gpu/ganesh/GrCanvas.h"
Kevin Lubick502553f2022-11-28 12:42:38 -050020#include "src/gpu/ganesh/GrCaps.h"
Greg Daniel719239c2022-04-07 11:20:24 -040021#include "src/gpu/ganesh/GrDirectContextPriv.h"
22#include "src/gpu/ganesh/GrPaint.h"
23#include "src/gpu/ganesh/GrProxyProvider.h"
24#include "src/gpu/ganesh/GrResourceProvider.h"
Kevin Lubickc96cb632022-11-17 14:51:41 -050025#include "src/gpu/ganesh/GrTexture.h"
Greg Daniel719239c2022-04-07 11:20:24 -040026#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"
Robert Phillips94e67912021-01-21 13:39:08 -050029#include "tools/gpu/ProxyUtils.h"
30
31static GrSurfaceProxyView create_view(GrDirectContext* dContext,
32 const SkBitmap& src,
33 GrSurfaceOrigin origin) {
34 SkASSERT(src.colorType() == kRGBA_8888_SkColorType);
35
36#define USE_LAZY_PROXIES 1 // Toggle this to generate the reference images
37
38 if (USE_LAZY_PROXIES) {
39 auto format = dContext->priv().caps()->getDefaultBackendFormat(GrColorType::kRGBA_8888,
40 GrRenderable::kNo);
41 if (!format.isValid()) {
42 return {};
43 }
44
45 sk_sp<GrTextureProxy> proxy = GrProxyProvider::MakeFullyLazyProxy(
Kevin Lubickdf73d162023-09-11 11:56:53 -040046 [src](GrResourceProvider* rp, const GrSurfaceProxy::LazySurfaceDesc& desc)
47 -> GrSurfaceProxy::LazyCallbackResult {
48 SkASSERT(desc.fMipmapped == skgpu::Mipmapped::kNo);
Brian Salomon2c673402021-04-02 14:36:58 -040049 GrMipLevel mipLevel = {src.getPixels(), src.rowBytes(), nullptr};
Robert Phillips94e67912021-01-21 13:39:08 -050050 auto colorType = SkColorTypeToGrColorType(src.colorType());
51
Greg Daniel0e9d34d2021-08-13 16:20:18 -040052 return rp->createTexture(src.dimensions(),
53 desc.fFormat,
Greg Daniele56d31f2021-08-16 09:29:56 -040054 desc.fTextureType,
Greg Daniel0e9d34d2021-08-13 16:20:18 -040055 colorType,
56 desc.fRenderable,
57 desc.fSampleCnt,
58 desc.fBudgeted,
59 desc.fFit,
60 desc.fProtected,
Aditya Kushwaha696f8a2022-05-03 13:00:24 -070061 mipLevel,
62 desc.fLabel);
Robert Phillips94e67912021-01-21 13:39:08 -050063 },
Kevin Lubickdf73d162023-09-11 11:56:53 -040064 format,
65 GrRenderable::kNo,
66 1,
67 GrProtected::kNo,
68 *dContext->priv().caps(),
Robert Phillips94e67912021-01-21 13:39:08 -050069 GrSurfaceProxy::UseAllocator::kYes);
70
71 if (!proxy) {
72 return {};
73 }
74
75 auto swizzle = dContext->priv().caps()->getReadSwizzle(proxy->backendFormat(),
76 GrColorType::kRGBA_8888);
77 return GrSurfaceProxyView(std::move(proxy), origin, swizzle);
78 }
79
80 return sk_gpu_test::MakeTextureProxyViewFromData(dContext,
81 GrRenderable::kNo,
82 origin,
83 src.pixmap());
84}
85
86// Create an over large texture which is initialized to opaque black outside of the content
87// rect. The inside of the content rect consists of a grey coordinate frame lacking the -Y axis.
88// The -X and +X axes have a red and green dot at their ends (respectively). Finally, the content
89// rect has a 1-pixel wide blue border.
90static SkBitmap create_bitmap(SkIRect contentRect, SkISize fullSize, GrSurfaceOrigin origin) {
91
92 const int kContentSize = contentRect.width();
93 SkBitmap contentBM;
94
95 {
96 SkImageInfo contentInfo = SkImageInfo::Make(kContentSize, kContentSize,
97 kRGBA_8888_SkColorType, kOpaque_SkAlphaType);
98 contentBM.allocPixels(contentInfo);
99
100 contentBM.eraseColor(SK_ColorWHITE);
101
102 const int halfM1 = kContentSize/2 - 1;
103
104 // The coordinate frame
105 contentBM.eraseArea(SkIRect::MakeXYWH(halfM1, 2,2, halfM1), SK_ColorGRAY);
106 contentBM.eraseArea(SkIRect::MakeXYWH(2, halfM1, kContentSize-4, 2), SK_ColorGRAY);
107
108 contentBM.eraseArea(SkIRect::MakeXYWH(2, halfM1, 2, 2), SK_ColorRED);
109 contentBM.eraseArea(SkIRect::MakeXYWH(kContentSize-4, halfM1, 2, 2), SK_ColorGREEN);
110
111 // The 1-pixel wide rim around the content rect
112 contentBM.eraseArea(SkIRect::MakeXYWH(0, 0, kContentSize, 1), SK_ColorBLUE);
113 contentBM.eraseArea(SkIRect::MakeXYWH(0, 0, 1, kContentSize), SK_ColorBLUE);
114 contentBM.eraseArea(SkIRect::MakeXYWH(kContentSize-1, 0, 1, kContentSize), SK_ColorBLUE);
115 contentBM.eraseArea(SkIRect::MakeXYWH(0, kContentSize-1, kContentSize, 1), SK_ColorBLUE);
116 }
117
118 SkBitmap bigBM;
119
120 {
121 const int kLeft = contentRect.fLeft;
122 const int kTop = contentRect.fTop;
123
124 SkImageInfo bigInfo = SkImageInfo::Make(fullSize.fWidth, fullSize.fHeight,
125 kRGBA_8888_SkColorType, kOpaque_SkAlphaType);
126
127 bigBM.allocPixels(bigInfo);
128
129 bigBM.eraseColor(SK_ColorBLACK);
130
131 const char* src = static_cast<const char*>(contentBM.getPixels());
132 size_t srcRB = contentBM.rowBytes();
133 size_t dstRB = bigBM.rowBytes();
134
135 if (USE_LAZY_PROXIES && origin == kBottomLeft_GrSurfaceOrigin) {
136 char* dst = static_cast<char*>(bigBM.getAddr(kLeft, fullSize.height() - kTop - 1));
137 for (int y = 0; y < contentBM.height(); ++y) {
138 memcpy(dst, src, srcRB);
139 src = src + srcRB;
140 dst = dst - dstRB;
141 }
142 } else {
143 char* dst = static_cast<char*>(bigBM.getAddr(kLeft, kTop));
144 SkRectMemcpy(dst, dstRB, src, srcRB,
145 contentBM.rowBytes(), contentBM.height());
146 }
147
148 bigBM.setAlphaType(kOpaque_SkAlphaType);
149 bigBM.setImmutable();
150 }
151
152 return bigBM;
153}
154
155static void draw_texture(const GrCaps* caps,
Kevin Lubickf24283f2023-03-17 16:10:23 -0400156 skgpu::ganesh::SurfaceDrawContext* sdc,
Robert Phillips94e67912021-01-21 13:39:08 -0500157 const GrSurfaceProxyView& src,
158 const SkIRect& srcRect,
159 const SkIRect& drawRect,
160 const SkMatrix& mat,
161 GrSamplerState::WrapMode xTileMode,
162 GrSamplerState::WrapMode yTileMode) {
163 GrSamplerState sampler(xTileMode, yTileMode, SkFilterMode::kNearest);
164
165 auto fp = GrTextureEffect::MakeSubset(src, kOpaque_SkAlphaType, mat,
166 sampler, SkRect::Make(srcRect), *caps);
167 GrPaint paint;
168 paint.setColorFragmentProcessor(std::move(fp));
169
170 sdc->drawRect(nullptr, std::move(paint), GrAA::kNo, SkMatrix::I(), SkRect::Make(drawRect));
171}
172
173namespace skiagm {
174
175// This GM exercises all the different tile modes for a texture that cannot be normalized
176// early (i.e., rectangle or fully-lazy).
177// TODO: should we also test w/ mipmapping?
178class LazyTilingGM : public GpuGM {
179public:
180 LazyTilingGM(GrSurfaceOrigin origin)
181 : fOrigin(origin)
182 , fContentRect(SkIRect::MakeXYWH(kLeftContentOffset, kTopContentOffset,
183 kContentSize, kContentSize)) {
184 this->setBGColor(0xFFCCCCCC);
185 }
186
187protected:
Leandro Lovisolo24fa2112023-08-15 19:05:17 +0000188 SkString getName() const override {
Robert Phillips94e67912021-01-21 13:39:08 -0500189 return SkStringPrintf("lazytiling_%s", fOrigin == kTopLeft_GrSurfaceOrigin ? "tl" : "bl");
190 }
191
Leandro Lovisolo8f023882023-08-15 21:13:52 +0000192 SkISize getISize() override { return SkISize::Make(kTotalWidth, kTotalHeight); }
Robert Phillips94e67912021-01-21 13:39:08 -0500193
Brian Salomonc759bbf2023-12-05 11:11:27 -0500194 DrawResult onGpuSetup(SkCanvas* canvas, SkString* errorMsg, GraphiteTestContext*) override {
Jim Van Vertha8624432023-02-13 16:48:09 -0500195 auto dContext = GrAsDirectContext(canvas->recordingContext());
Robert Phillips94e67912021-01-21 13:39:08 -0500196 if (!dContext || dContext->abandoned()) {
197 return DrawResult::kSkip;
198 }
199
200 auto bm = create_bitmap(fContentRect,
201 { kLeftContentOffset + kContentSize + kRightContentPadding,
202 kTopContentOffset + kContentSize + kBottomContentPadding },
203 fOrigin);
204
205 fView = create_view(dContext, bm, fOrigin);
206 if (!fView.proxy()) {
207 *errorMsg = "Failed to create proxy";
208 return DrawResult::kFail;
209 }
210
211 return DrawResult::kOk;
212 }
213
Robert Phillips7a0d3c32021-07-21 15:39:51 -0400214 DrawResult onDraw(GrRecordingContext* rContext, SkCanvas* canvas, SkString* errorMsg) override {
Kevin Lubickacdc1082023-06-09 11:05:24 -0400215 auto sdc = skgpu::ganesh::TopDeviceSurfaceDrawContext(canvas);
Robert Phillips7a0d3c32021-07-21 15:39:51 -0400216 if (!sdc) {
217 *errorMsg = kErrorMsg_DrawSkippedGpuOnly;
218 return DrawResult::kSkip;
219 }
220
Robert Phillips94e67912021-01-21 13:39:08 -0500221 int y = kPad;
222 for (auto yMode : { SkTileMode::kClamp, SkTileMode::kRepeat,
223 SkTileMode::kMirror, SkTileMode::kDecal }) {
224 int x = kPad;
225 for (auto xMode : { SkTileMode::kClamp, SkTileMode::kRepeat,
226 SkTileMode::kMirror, SkTileMode::kDecal }) {
227 SkIRect cellRect = SkIRect::MakeXYWH(x, y, 2*kContentSize, 2*kContentSize);
228 SkRect contentRect = SkRect::MakeXYWH(x+kContentSize/2, y+kContentSize/2,
229 kContentSize, kContentSize);
230
231 SkMatrix texMatrix = SkMatrix::RectToRect(contentRect, SkRect::Make(fContentRect));
232
233 draw_texture(rContext->priv().caps(),
234 sdc,
235 fView,
236 fContentRect,
237 cellRect,
238 texMatrix,
239 SkTileModeToWrapMode(xMode),
240 SkTileModeToWrapMode(yMode));
241
242 x += 2*kContentSize+kPad;
243 }
244
245 y += 2*kContentSize+kPad;
246 }
247
Robert Phillips7a0d3c32021-07-21 15:39:51 -0400248 return DrawResult::kOk;
Robert Phillips94e67912021-01-21 13:39:08 -0500249 }
250
251private:
Brian Salomon9fa47cc2021-10-08 18:48:26 -0400252 inline static constexpr int kLeftContentOffset = 8;
253 inline static constexpr int kTopContentOffset = 16;
254 inline static constexpr int kRightContentPadding = 24;
255 inline static constexpr int kBottomContentPadding = 80;
Robert Phillips94e67912021-01-21 13:39:08 -0500256
Brian Salomon9fa47cc2021-10-08 18:48:26 -0400257 inline static constexpr int kPad = 4; // on-screen padding between cells
Robert Phillips94e67912021-01-21 13:39:08 -0500258
Brian Salomon9fa47cc2021-10-08 18:48:26 -0400259 inline static constexpr int kContentSize = 32;
Robert Phillips94e67912021-01-21 13:39:08 -0500260
261 // Each cell in this GM's grid is a square - 2*kContentSize on a side
Brian Salomon9fa47cc2021-10-08 18:48:26 -0400262 inline static constexpr int kTotalWidth = (2*kContentSize+kPad) * kSkTileModeCount + kPad;
263 inline static constexpr int kTotalHeight = (2*kContentSize+kPad) * kSkTileModeCount + kPad;
Robert Phillips94e67912021-01-21 13:39:08 -0500264
265 GrSurfaceOrigin fOrigin;
266 SkIRect fContentRect;
267 GrSurfaceProxyView fView;
268
269 using INHERITED = GM;
270};
271
272//////////////////////////////////////////////////////////////////////////////
273
274DEF_GM(return new LazyTilingGM(kTopLeft_GrSurfaceOrigin);)
275DEF_GM(return new LazyTilingGM(kBottomLeft_GrSurfaceOrigin);)
276
277} // namespace skiagm