Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
lazytiling.cpp
Go to the documentation of this file.
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"
12#include "include/core/SkRect.h"
13#include "include/core/SkSize.h"
26#include "src/gpu/ganesh/SkGr.h"
30
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) {
40 GrRenderable::kNo);
41 if (!format.isValid()) {
42 return {};
43 }
44
48 SkASSERT(desc.fMipmapped == skgpu::Mipmapped::kNo);
49 GrMipLevel mipLevel = {src.getPixels(), src.rowBytes(), nullptr};
50 auto colorType = SkColorTypeToGrColorType(src.colorType());
51
52 return rp->createTexture(src.dimensions(),
53 desc.fFormat,
54 desc.fTextureType,
56 desc.fRenderable,
57 desc.fSampleCnt,
58 desc.fBudgeted,
59 desc.fFit,
60 desc.fProtected,
61 mipLevel,
62 desc.fLabel);
63 },
64 format,
65 GrRenderable::kNo,
66 1,
67 GrProtected::kNo,
68 *dContext->priv().caps(),
70
71 if (!proxy) {
72 return {};
73 }
74
75 auto swizzle = dContext->priv().caps()->getReadSwizzle(proxy->backendFormat(),
77 return GrSurfaceProxyView(std::move(proxy), origin, swizzle);
78 }
79
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,
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,
126
127 bigBM.allocPixels(bigInfo);
128
130
131 const char* src = static_cast<const char*>(contentBM.getPixels());
132 size_t srcRB = contentBM.rowBytes();
133 size_t dstRB = bigBM.rowBytes();
134
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
149 bigBM.setImmutable();
150 }
151
152 return bigBM;
153}
154
155static void draw_texture(const GrCaps* caps,
157 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
166 sampler, SkRect::Make(srcRect), *caps);
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:
181 : fOrigin(origin)
182 , fContentRect(SkIRect::MakeXYWH(kLeftContentOffset, kTopContentOffset,
183 kContentSize, kContentSize)) {
184 this->setBGColor(0xFFCCCCCC);
185 }
186
187protected:
188 SkString getName() const override {
189 return SkStringPrintf("lazytiling_%s", fOrigin == kTopLeft_GrSurfaceOrigin ? "tl" : "bl");
190 }
191
192 SkISize getISize() override { return SkISize::Make(kTotalWidth, kTotalHeight); }
193
194 DrawResult onGpuSetup(SkCanvas* canvas, SkString* errorMsg, GraphiteTestContext*) override {
195 auto dContext = GrAsDirectContext(canvas->recordingContext());
196 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
214 DrawResult onDraw(GrRecordingContext* rContext, SkCanvas* canvas, SkString* errorMsg) override {
216 if (!sdc) {
217 *errorMsg = kErrorMsg_DrawSkippedGpuOnly;
218 return DrawResult::kSkip;
219 }
220
221 int y = kPad;
222 for (auto yMode : { SkTileMode::kClamp, SkTileMode::kRepeat,
224 int x = kPad;
225 for (auto xMode : { SkTileMode::kClamp, SkTileMode::kRepeat,
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,
240 SkTileModeToWrapMode(yMode));
241
242 x += 2*kContentSize+kPad;
243 }
244
245 y += 2*kContentSize+kPad;
246 }
247
248 return DrawResult::kOk;
249 }
250
251private:
252 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;
256
257 inline static constexpr int kPad = 4; // on-screen padding between cells
258
259 inline static constexpr int kContentSize = 32;
260
261 // Each cell in this GM's grid is a square - 2*kContentSize on a side
262 inline static constexpr int kTotalWidth = (2*kContentSize+kPad) * kSkTileModeCount + kPad;
263 inline static constexpr int kTotalHeight = (2*kContentSize+kPad) * kSkTileModeCount + kPad;
264
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
static GrDirectContext * GrAsDirectContext(GrContext_Base *base)
static constexpr GrColorType SkColorTypeToGrColorType(SkColorType ct)
GrSurfaceOrigin
Definition GrTypes.h:147
@ kBottomLeft_GrSurfaceOrigin
Definition GrTypes.h:149
@ kTopLeft_GrSurfaceOrigin
Definition GrTypes.h:148
@ kOpaque_SkAlphaType
pixel is opaque
Definition SkAlphaType.h:28
#define SkASSERT(cond)
Definition SkAssert.h:116
@ kRGBA_8888_SkColorType
pixel with 8 bits for red, green, blue, alpha; in 32-bit word
Definition SkColorType.h:24
constexpr SkColor SK_ColorGRAY
Definition SkColor.h:113
constexpr SkColor SK_ColorBLUE
Definition SkColor.h:135
constexpr SkColor SK_ColorRED
Definition SkColor.h:126
constexpr SkColor SK_ColorBLACK
Definition SkColor.h:103
constexpr SkColor SK_ColorGREEN
Definition SkColor.h:131
constexpr SkColor SK_ColorWHITE
Definition SkColor.h:122
static constexpr GrSamplerState::WrapMode SkTileModeToWrapMode(SkTileMode tileMode)
Definition SkGr.h:77
static SkColorType colorType(AImageDecoder *decoder, const AImageDecoderHeaderInfo *headerInfo)
static void SkRectMemcpy(void *dst, size_t dstRB, const void *src, size_t srcRB, size_t trimRowBytes, int rowCount)
SK_API SkString static SkString SkStringPrintf()
Definition SkString.h:287
static constexpr int kSkTileModeCount
Definition SkTileMode.h:39
constexpr int kPad
const GrCaps * caps() const
GrBackendFormat getDefaultBackendFormat(GrColorType, GrRenderable) const
Definition GrCaps.cpp:400
skgpu::Swizzle getReadSwizzle(const GrBackendFormat &format, GrColorType colorType) const
Definition GrCaps.cpp:443
GrDirectContextPriv priv()
static sk_sp< GrTextureProxy > MakeFullyLazyProxy(LazyInstantiateCallback &&, const GrBackendFormat &, GrRenderable, int renderTargetSampleCnt, GrProtected, const GrCaps &, UseAllocator)
GrRecordingContextPriv priv()
sk_sp< GrTexture > createTexture(SkISize dimensions, const GrBackendFormat &format, GrTextureType textureType, skgpu::Renderable renderable, int renderTargetSampleCnt, skgpu::Mipmapped mipmapped, skgpu::Budgeted budgeted, skgpu::Protected isProtected, std::string_view label)
GrSurfaceProxy * proxy() const
static std::unique_ptr< GrFragmentProcessor > MakeSubset(GrSurfaceProxyView, SkAlphaType, const SkMatrix &, GrSamplerState, const SkRect &subset, const GrCaps &caps, const float border[4]=kDefaultBorder, bool alwaysUseShaderTileMode=false)
void allocPixels(const SkImageInfo &info, size_t rowBytes)
Definition SkBitmap.cpp:258
void setImmutable()
Definition SkBitmap.cpp:400
void eraseArea(const SkIRect &area, SkColor c) const
Definition SkBitmap.h:854
void * getAddr(int x, int y) const
Definition SkBitmap.cpp:406
size_t rowBytes() const
Definition SkBitmap.h:238
void * getPixels() const
Definition SkBitmap.h:283
bool setAlphaType(SkAlphaType alphaType)
Definition SkBitmap.cpp:148
int height() const
Definition SkBitmap.h:158
void eraseColor(SkColor4f) const
Definition SkBitmap.cpp:442
virtual GrRecordingContext * recordingContext() const
static SkMatrix RectToRect(const SkRect &src, const SkRect &dst, ScaleToFit mode=kFill_ScaleToFit)
Definition SkMatrix.h:157
static const SkMatrix & I()
void drawRect(const GrClip *, GrPaint &&paint, GrAA, const SkMatrix &viewMatrix, const SkRect &, const GrStyle *style=nullptr)
SkString getName() const override
SkISize getISize() override
LazyTilingGM(GrSurfaceOrigin origin)
DrawResult onGpuSetup(SkCanvas *canvas, SkString *errorMsg, GraphiteTestContext *) override
DrawResult onDraw(GrRecordingContext *rContext, SkCanvas *canvas, SkString *errorMsg) override
const Paint & paint
uint32_t uint32_t * format
#define DEF_GM(CODE)
Definition gm.h:40
static GrSurfaceProxyView create_view(GrDirectContext *dContext, const SkBitmap &src, GrSurfaceOrigin origin)
static void draw_texture(const GrCaps *caps, skgpu::ganesh::SurfaceDrawContext *sdc, const GrSurfaceProxyView &src, const SkIRect &srcRect, const SkIRect &drawRect, const SkMatrix &mat, GrSamplerState::WrapMode xTileMode, GrSamplerState::WrapMode yTileMode)
static SkBitmap create_bitmap(SkIRect contentRect, SkISize fullSize, GrSurfaceOrigin origin)
#define USE_LAZY_PROXIES
double y
double x
GrSurfaceProxyView MakeTextureProxyViewFromData(GrDirectContext *dContext, GrRenderable renderable, GrSurfaceOrigin origin, GrCPixmap pixmap)
SurfaceDrawContext * TopDeviceSurfaceDrawContext(const SkCanvas *canvas)
Definition GrCanvas.cpp:20
DrawResult
Definition gm.h:104
int32_t fTop
smaller y-axis bounds
Definition SkRect.h:34
constexpr int32_t width() const
Definition SkRect.h:158
static constexpr SkIRect MakeXYWH(int32_t x, int32_t y, int32_t w, int32_t h)
Definition SkRect.h:104
int32_t fLeft
smaller x-axis bounds
Definition SkRect.h:33
static constexpr SkISize Make(int32_t w, int32_t h)
Definition SkSize.h:20
int32_t fHeight
Definition SkSize.h:18
int32_t fWidth
Definition SkSize.h:17
constexpr int32_t height() const
Definition SkSize.h:37
static SkImageInfo Make(int width, int height, SkColorType ct, SkAlphaType at)
static SkRect Make(const SkISize &size)
Definition SkRect.h:669
static constexpr SkRect MakeXYWH(float x, float y, float w, float h)
Definition SkRect.h:659