Flutter Engine
The Flutter Engine
DrawOpAtlasTest.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2018 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
11#include "include/core/SkFont.h"
15#include "include/core/SkSize.h"
21#include "include/gpu/GrTypes.h"
23#include "src/gpu/AtlasTypes.h"
40#include "tests/Test.h"
44
45#include <cstddef>
46#include <cstdint>
47#include <memory>
48
50struct GrContextOptions;
51
52using namespace skgpu::ganesh;
54
55static const int kNumPlots = 2;
56static const int kPlotSize = 32;
57static const int kAtlasSize = kNumPlots * kPlotSize;
58
60public:
61 void evict(skgpu::PlotLocator) override {
62 SkASSERT(0); // The unit test shouldn't exercise this code path
63 }
64};
65
67 uint32_t expectedActive, uint32_t expectedMax, int expectedAlloced) {
68 REPORTER_ASSERT(r, expectedActive == atlas->numActivePages());
69 REPORTER_ASSERT(r, expectedMax == atlas->maxPages());
71}
72
74public:
76
77 const skgpu::TokenTracker* tokenTracker() final { return &fTokenTracker; }
78 skgpu::TokenTracker* writeableTokenTracker() { return &fTokenTracker; }
79
81 SkASSERT(0); // this test shouldn't invoke this code path
82 return fTokenTracker.nextDrawToken();
83 }
84
86 return fTokenTracker.nextFlushToken();
87 }
88
89 void issueDrawToken() { fTokenTracker.issueDrawToken(); }
90 void issueFlushToken() { fTokenTracker.issueFlushToken(); }
91
92private:
93 skgpu::TokenTracker fTokenTracker;
94
95 using INHERITED = GrDeferredUploadTarget;
96};
97
99 GrResourceProvider* resourceProvider,
101 skgpu::AtlasLocator* atlasLocator,
102 int alpha) {
104
106 data.allocPixels(ii);
107 data.eraseARGB(alpha, 0, 0, 0);
108
110 code = atlas->addToAtlas(resourceProvider, target, kPlotSize, kPlotSize,
111 data.getAddr(0, 0), atlasLocator);
113}
114
115
116// This is a basic DrawOpAtlas test. It simply verifies that multitexture atlases correctly
117// add and remove pages. Note that this is simulating flush-time behavior.
119 reporter,
120 ctxInfo,
122 auto context = ctxInfo.directContext();
123 auto proxyProvider = context->priv().proxyProvider();
124 auto resourceProvider = context->priv().resourceProvider();
125 auto drawingManager = context->priv().drawingManager();
126 const GrCaps* caps = context->priv().caps();
127
128 GrOnFlushResourceProvider onFlushResourceProvider(drawingManager);
129 TestingUploadTarget uploadTarget;
130
131 GrColorType atlasColorType = GrColorType::kAlpha_8;
132 GrBackendFormat format = caps->getDefaultBackendFormat(atlasColorType,
134
135 AssertOnEvict evictor;
137
138 std::unique_ptr<GrDrawOpAtlas> atlas = GrDrawOpAtlas::Make(
139 proxyProvider,
140 format,
141 GrColorTypeToSkColorType(atlasColorType),
142 GrColorTypeBytesPerPixel(atlasColorType),
145 &counter,
147 &evictor,
148 /*label=*/"BasicDrawOpAtlasTest");
149 check(reporter, atlas.get(), 0, 4, 0);
150
151 // Fill up the first level
152 skgpu::AtlasLocator atlasLocators[kNumPlots * kNumPlots];
153 for (int i = 0; i < kNumPlots * kNumPlots; ++i) {
154 bool result = fill_plot(
155 atlas.get(), resourceProvider, &uploadTarget, &atlasLocators[i], i * 32);
157 check(reporter, atlas.get(), 1, 4, 1);
158 }
159
160 atlas->instantiate(&onFlushResourceProvider);
161 check(reporter, atlas.get(), 1, 4, 1);
162
163 // Force allocation of a second level
164 skgpu::AtlasLocator atlasLocator;
165 bool result = fill_plot(atlas.get(), resourceProvider, &uploadTarget, &atlasLocator, 4 * 32);
167 check(reporter, atlas.get(), 2, 4, 2);
168
169 // Simulate a lot of draws using only the first plot. The last texture should be compacted.
170 for (int i = 0; i < 512; ++i) {
171 atlas->setLastUseToken(atlasLocators[0], uploadTarget.tokenTracker()->nextDrawToken());
172 uploadTarget.issueDrawToken();
173 uploadTarget.issueFlushToken();
174 atlas->compact(uploadTarget.tokenTracker()->nextFlushToken());
175 }
176
177 check(reporter, atlas.get(), 1, 4, 1);
178}
179
180// This test verifies that the AtlasTextOp::onPrepare method correctly handles a failure
181// when allocating an atlas page.
183 reporter,
184 ctxInfo,
186 auto dContext = ctxInfo.directContext();
187
188 auto gpu = dContext->priv().getGpu();
189 auto resourceProvider = dContext->priv().resourceProvider();
190
191 auto sdc = skgpu::ganesh::SurfaceDrawContext::Make(dContext,
193 nullptr,
195 {32, 32},
197 /*label=*/"AtlasTextOpPreparation");
198
200 paint.setColor(SK_ColorRED);
201
203 font.setEdging(SkFont::Edging::kAlias);
204
205 const char* text = "a";
206
207 GrOp::Owner op =
208 AtlasTextOpTools::CreateOp(sdc.get(), paint, font, SkMatrix::I(), text, 16, 16);
209 if (!op) {
210 return;
211 }
212
213 auto atlasTextOp = (AtlasTextOp*)op.get();
214 atlasTextOp->finalize(*dContext->priv().caps(), nullptr, GrClampType::kAuto);
215
216 TestingUploadTarget uploadTarget;
217
218 GrOpFlushState flushState(gpu, resourceProvider, uploadTarget.writeableTokenTracker());
219
220 GrSurfaceProxyView surfaceView = sdc->writeSurfaceView();
221 GrOpFlushState::OpArgs opArgs(op.get(),
222 surfaceView,
223 false /*usesMSAASurface*/,
224 nullptr,
228
229 // Modify the atlas manager so it can't allocate any pages. This will force a failure
230 // in the preparation of the text op
231 auto atlasManager = dContext->priv().getAtlasManager();
232 unsigned int numProxies;
233 atlasManager->getViews(MaskFormat::kA8, &numProxies);
234 GrAtlasManagerTools::SetMaxPages(atlasManager, 0);
235
236 flushState.setOpArgs(&opArgs);
237 op->prepare(&flushState);
238 flushState.setOpArgs(nullptr);
239}
240
241void test_atlas_config(skiatest::Reporter* reporter, int maxTextureSize, size_t maxBytes,
242 MaskFormat maskFormat, SkISize expectedDimensions,
243 SkISize expectedPlotDimensions) {
244 GrDrawOpAtlasConfig config(maxTextureSize, maxBytes);
245 REPORTER_ASSERT(reporter, config.atlasDimensions(maskFormat) == expectedDimensions);
246 REPORTER_ASSERT(reporter, config.plotDimensions(maskFormat) == expectedPlotDimensions);
247}
248
250 // 1/4 MB
251 test_atlas_config(reporter, 65536, 256 * 1024, MaskFormat::kARGB,
252 { 256, 256 }, { 256, 256 });
253 test_atlas_config(reporter, 65536, 256 * 1024, MaskFormat::kA8,
254 { 512, 512 }, { 256, 256 });
255 // 1/2 MB
256 test_atlas_config(reporter, 65536, 512 * 1024, MaskFormat::kARGB,
257 { 512, 256 }, { 256, 256 });
258 test_atlas_config(reporter, 65536, 512 * 1024, MaskFormat::kA8,
259 { 1024, 512 }, { 256, 256 });
260 // 1 MB
261 test_atlas_config(reporter, 65536, 1024 * 1024, MaskFormat::kARGB,
262 { 512, 512 }, { 256, 256 });
263 test_atlas_config(reporter, 65536, 1024 * 1024, MaskFormat::kA8,
264 { 1024, 1024 }, { 256, 256 });
265 // 2 MB
266 test_atlas_config(reporter, 65536, 2 * 1024 * 1024, MaskFormat::kARGB,
267 { 1024, 512 }, { 256, 256 });
268 test_atlas_config(reporter, 65536, 2 * 1024 * 1024, MaskFormat::kA8,
269 { 2048, 1024 }, { 512, 256 });
270 // 4 MB
271 test_atlas_config(reporter, 65536, 4 * 1024 * 1024, MaskFormat::kARGB,
272 { 1024, 1024 }, { 256, 256 });
273 test_atlas_config(reporter, 65536, 4 * 1024 * 1024, MaskFormat::kA8,
274 { 2048, 2048 }, { 512, 512 });
275 // 8 MB
276 test_atlas_config(reporter, 65536, 8 * 1024 * 1024, MaskFormat::kARGB,
277 { 2048, 1024 }, { 256, 256 });
278 test_atlas_config(reporter, 65536, 8 * 1024 * 1024, MaskFormat::kA8,
279 { 2048, 2048 }, { 512, 512 });
280 // 16 MB (should be same as 8 MB)
281 test_atlas_config(reporter, 65536, 16 * 1024 * 1024, MaskFormat::kARGB,
282 { 2048, 1024 }, { 256, 256 });
283 test_atlas_config(reporter, 65536, 16 * 1024 * 1024, MaskFormat::kA8,
284 { 2048, 2048 }, { 512, 512 });
285
286 // 4MB, restricted texture size
287 test_atlas_config(reporter, 1024, 8 * 1024 * 1024, MaskFormat::kARGB,
288 { 1024, 1024 }, { 256, 256 });
289 test_atlas_config(reporter, 1024, 8 * 1024 * 1024, MaskFormat::kA8,
290 { 1024, 1024 }, { 256, 256 });
291
292 // 3 MB (should be same as 2 MB)
293 test_atlas_config(reporter, 65536, 3 * 1024 * 1024, MaskFormat::kARGB,
294 { 1024, 512 }, { 256, 256 });
295 test_atlas_config(reporter, 65536, 3 * 1024 * 1024, MaskFormat::kA8,
296 { 2048, 1024 }, { 512, 256 });
297
298 // minimum size
299 test_atlas_config(reporter, 65536, 0, MaskFormat::kARGB,
300 { 256, 256 }, { 256, 256 });
301 test_atlas_config(reporter, 65536, 0, MaskFormat::kA8,
302 { 512, 512 }, { 256, 256 });
303}
const char * options
static void check(skiatest::Reporter *r, GrDrawOpAtlas *atlas, uint32_t expectedActive, uint32_t expectedMax, int expectedAlloced)
DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS(BasicDrawOpAtlas, reporter, ctxInfo, CtsEnforcement::kApiLevel_T)
static const int kAtlasSize
static const int kNumPlots
void test_atlas_config(skiatest::Reporter *reporter, int maxTextureSize, size_t maxBytes, MaskFormat maskFormat, SkISize expectedDimensions, SkISize expectedPlotDimensions)
static const int kPlotSize
static bool fill_plot(GrDrawOpAtlas *atlas, GrResourceProvider *resourceProvider, GrDeferredUploadTarget *target, skgpu::AtlasLocator *atlasLocator, int alpha)
DEF_GANESH_TEST(GrDrawOpAtlasConfig_Basic, reporter, options, CtsEnforcement::kApiLevel_T)
reporter
Definition: FontMgrTest.cpp:39
std::function< void(GrDeferredTextureUploadWritePixelsFn &)> GrDeferredTextureUploadFn
static constexpr size_t GrColorTypeBytesPerPixel(GrColorType ct)
Definition: GrTypesPriv.h:896
static constexpr SkColorType GrColorTypeToSkColorType(GrColorType ct)
Definition: GrTypesPriv.h:589
GrColorType
Definition: GrTypesPriv.h:540
#define SkASSERT(cond)
Definition: SkAssert.h:116
constexpr SkColor SK_ColorRED
Definition: SkColor.h:126
#define REPORTER_ASSERT(r, cond,...)
Definition: Test.h:286
void evict(skgpu::PlotLocator) override
static void SetMaxPages(GrAtlasManager *, uint32_t maxPages)
Definition: GrCaps.h:57
GrBackendFormat getDefaultBackendFormat(GrColorType, GrRenderable) const
Definition: GrCaps.cpp:400
SkISize atlasDimensions(skgpu::MaskFormat type) const
SkISize plotDimensions(skgpu::MaskFormat type) const
static int NumAllocated(const GrDrawOpAtlas *)
static std::unique_ptr< GrDrawOpAtlas > Make(GrProxyProvider *proxyProvider, const GrBackendFormat &format, SkColorType ct, size_t bpp, int width, int height, int plotWidth, int plotHeight, skgpu::AtlasGenerationCounter *generationCounter, AllowMultitexturing allowMultitexturing, skgpu::PlotEvictionCallback *evictor, std::string_view label)
void setOpArgs(OpArgs *opArgs)
std::unique_ptr< GrOp > Owner
Definition: GrOp.h:72
Definition: SkFont.h:35
@ kAlias
no transparent pixels on glyph edges
static const SkMatrix & I()
Definition: SkMatrix.cpp:1544
skgpu::AtlasToken addASAPUpload(GrDeferredTextureUploadFn &&upload) final
const skgpu::TokenTracker * tokenTracker() final
skgpu::AtlasToken addInlineUpload(GrDeferredTextureUploadFn &&) final
skgpu::TokenTracker * writeableTokenTracker()
T * get() const
Definition: SkRefCnt.h:303
AtlasToken nextFlushToken() const
Definition: AtlasTypes.h:207
AtlasToken nextDrawToken() const
Definition: AtlasTypes.h:214
static std::unique_ptr< SurfaceDrawContext > Make(GrRecordingContext *, GrColorType, sk_sp< GrSurfaceProxy >, sk_sp< SkColorSpace >, GrSurfaceOrigin, const SkSurfaceProps &)
const Paint & paint
Definition: color_source.cc:38
GAsyncResult * result
uint32_t uint32_t * format
uint32_t * target
std::u16string text
sk_sp< const SkImage > atlas
Definition: SkRecords.h:331
SkFont DefaultFont()
font
Font Metadata and Metrics.
MaskFormat
Definition: AtlasTypes.h:98
Definition: upload.py:1
Definition: SkSize.h:16
static SkImageInfo MakeA8(int width, int height)
std::shared_ptr< const fml::Mapping > data
Definition: texture_gles.cc:63