blob: aca99269ad2edd30dd99f7e5e399fe082f302c16 [file] [log] [blame]
Robert Phillips4bc70112018-03-01 10:24:02 -05001/*
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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkBitmap.h"
9#include "include/core/SkColor.h"
10#include "include/core/SkColorSpace.h"
Ben Wagner9707a7e2019-05-06 17:17:19 -040011#include "include/core/SkFont.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "include/core/SkImageInfo.h"
13#include "include/core/SkMatrix.h"
14#include "include/core/SkPaint.h"
15#include "include/core/SkPoint.h"
16#include "include/core/SkRefCnt.h"
Ben Wagner9707a7e2019-05-06 17:17:19 -040017#include "include/core/SkSize.h"
18#include "include/core/SkTypes.h"
19#include "include/gpu/GrBackendSurface.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050020#include "include/gpu/GrContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050021#include "include/private/GrTypesPriv.h"
22#include "src/core/SkIPoint16.h"
Ben Wagner9707a7e2019-05-06 17:17:19 -040023#include "src/gpu/GrCaps.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050024#include "src/gpu/GrContextPriv.h"
25#include "src/gpu/GrDeferredUpload.h"
26#include "src/gpu/GrDrawOpAtlas.h"
27#include "src/gpu/GrDrawingManager.h"
28#include "src/gpu/GrMemoryPool.h"
29#include "src/gpu/GrOnFlushResourceProvider.h"
30#include "src/gpu/GrOpFlushState.h"
31#include "src/gpu/GrRenderTargetContext.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040032#include "src/gpu/GrTextureProxy.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050033#include "src/gpu/GrXferProcessor.h"
34#include "src/gpu/ops/GrDrawOp.h"
Ben Wagner9707a7e2019-05-06 17:17:19 -040035#include "src/gpu/ops/GrOp.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050036#include "src/gpu/text/GrAtlasManager.h"
37#include "src/gpu/text/GrTextContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050038#include "tests/Test.h"
39#include "tools/gpu/GrContextFactory.h"
Ben Wagnerd90cd3b2018-05-22 10:48:08 -040040
41#include <memory>
Ben Wagner9707a7e2019-05-06 17:17:19 -040042#include <utility>
Ben Wagnerd90cd3b2018-05-22 10:48:08 -040043
44class GrResourceProvider;
Robert Phillips4bc70112018-03-01 10:24:02 -050045
46static const int kNumPlots = 2;
47static const int kPlotSize = 32;
48static const int kAtlasSize = kNumPlots * kPlotSize;
49
50int GrDrawOpAtlas::numAllocated_TestingOnly() const {
51 int count = 0;
52 for (uint32_t i = 0; i < this->maxPages(); ++i) {
Greg Daniel9715b6c2019-12-10 15:03:10 -050053 if (fViews[i].proxy()->isInstantiated()) {
Robert Phillips4bc70112018-03-01 10:24:02 -050054 ++count;
55 }
56 }
57
58 return count;
59}
60
Kevin Lubickb5502b22018-03-12 10:17:06 -040061void GrAtlasManager::setMaxPages_TestingOnly(uint32_t maxPages) {
Robert Phillipsd2e9f762018-03-07 11:54:37 -050062 for (int i = 0; i < kMaskFormatCount; i++) {
63 if (fAtlases[i]) {
Kevin Lubickb5502b22018-03-12 10:17:06 -040064 fAtlases[i]->setMaxPages_TestingOnly(maxPages);
Robert Phillipsd2e9f762018-03-07 11:54:37 -050065 }
66 }
67}
68
69void GrDrawOpAtlas::setMaxPages_TestingOnly(uint32_t maxPages) {
70 SkASSERT(!fNumActivePages);
71
72 fMaxPages = maxPages;
73}
74
Herb Derby1a496c52020-01-22 17:26:56 -050075class DummyEvict : public GrDrawOpAtlas::EvictionCallback {
76public:
Herb Derby4d721712020-01-24 14:31:16 -050077 void evict(GrDrawOpAtlas::PlotLocator plotLocator) override {
Herb Derby1a496c52020-01-22 17:26:56 -050078 SkASSERT(0); // The unit test shouldn't exercise this code path
79 }
80};
Robert Phillips4bc70112018-03-01 10:24:02 -050081
82static void check(skiatest::Reporter* r, GrDrawOpAtlas* atlas,
83 uint32_t expectedActive, uint32_t expectedMax, int expectedAlloced) {
84 REPORTER_ASSERT(r, expectedActive == atlas->numActivePages());
85 REPORTER_ASSERT(r, expectedMax == atlas->maxPages());
86 REPORTER_ASSERT(r, expectedAlloced == atlas->numAllocated_TestingOnly());
87}
88
89class TestingUploadTarget : public GrDeferredUploadTarget {
90public:
91 TestingUploadTarget() { }
92
Robert Phillipsd2e9f762018-03-07 11:54:37 -050093 const GrTokenTracker* tokenTracker() final { return &fTokenTracker; }
94 GrTokenTracker* writeableTokenTracker() { return &fTokenTracker; }
Robert Phillips4bc70112018-03-01 10:24:02 -050095
96 GrDeferredUploadToken addInlineUpload(GrDeferredTextureUploadFn&&) final {
97 SkASSERT(0); // this test shouldn't invoke this code path
98 return fTokenTracker.nextDrawToken();
99 }
100
101 virtual GrDeferredUploadToken addASAPUpload(GrDeferredTextureUploadFn&& upload) final {
102 return fTokenTracker.nextTokenToFlush();
103 }
104
105 void issueDrawToken() { fTokenTracker.issueDrawToken(); }
106 void flushToken() { fTokenTracker.flushToken(); }
107
108private:
109 GrTokenTracker fTokenTracker;
110
111 typedef GrDeferredUploadTarget INHERITED;
112};
113
114static bool fill_plot(GrDrawOpAtlas* atlas,
115 GrResourceProvider* resourceProvider,
116 GrDeferredUploadTarget* target,
Herb Derby4d721712020-01-24 14:31:16 -0500117 GrDrawOpAtlas::PlotLocator* plotLocator,
Robert Phillips4bc70112018-03-01 10:24:02 -0500118 int alpha) {
119 SkImageInfo ii = SkImageInfo::MakeA8(kPlotSize, kPlotSize);
120
121 SkBitmap data;
122 data.allocPixels(ii);
123 data.eraseARGB(alpha, 0, 0, 0);
124
125 SkIPoint16 loc;
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500126 GrDrawOpAtlas::ErrorCode code;
Herb Derby4d721712020-01-24 14:31:16 -0500127 code = atlas->addToAtlas(resourceProvider, plotLocator, target, kPlotSize, kPlotSize,
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500128 data.getAddr(0, 0), &loc);
129 return GrDrawOpAtlas::ErrorCode::kSucceeded == code;
Robert Phillips4bc70112018-03-01 10:24:02 -0500130}
131
132
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500133// This is a basic DrawOpAtlas test. It simply verifies that multitexture atlases correctly
134// add and remove pages. Note that this is simulating flush-time behavior.
135DEF_GPUTEST_FOR_RENDERING_CONTEXTS(BasicDrawOpAtlas, reporter, ctxInfo) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500136 auto context = ctxInfo.grContext();
Robert Phillips9da87e02019-02-04 13:26:26 -0500137 auto proxyProvider = context->priv().proxyProvider();
138 auto resourceProvider = context->priv().resourceProvider();
139 auto drawingManager = context->priv().drawingManager();
Robert Phillips0a15cc62019-07-30 12:49:10 -0400140 const GrCaps* caps = context->priv().caps();
Robert Phillips4bc70112018-03-01 10:24:02 -0500141
142 GrOnFlushResourceProvider onFlushResourceProvider(drawingManager);
143 TestingUploadTarget uploadTarget;
144
Robert Phillips0a15cc62019-07-30 12:49:10 -0400145 GrBackendFormat format = caps->getDefaultBackendFormat(GrColorType::kAlpha_8,
146 GrRenderable::kNo);
Greg Daniel4065d452018-11-16 15:43:41 -0500147
Herb Derby1a496c52020-01-22 17:26:56 -0500148 DummyEvict evictor;
Herb Derby0ef780b2020-01-24 15:57:11 -0500149 GrDrawOpAtlas::GenerationCounter counter;
Herb Derby1a496c52020-01-22 17:26:56 -0500150
Robert Phillips4bc70112018-03-01 10:24:02 -0500151 std::unique_ptr<GrDrawOpAtlas> atlas = GrDrawOpAtlas::Make(
152 proxyProvider,
Greg Daniel4065d452018-11-16 15:43:41 -0500153 format,
Robert Phillips42dda082019-05-14 13:29:45 -0400154 GrColorType::kAlpha_8,
Robert Phillips4bc70112018-03-01 10:24:02 -0500155 kAtlasSize, kAtlasSize,
Jim Van Verthf6206f92018-12-14 08:22:24 -0500156 kAtlasSize/kNumPlots, kAtlasSize/kNumPlots,
Herb Derby0ef780b2020-01-24 15:57:11 -0500157 &counter,
Robert Phillips4bc70112018-03-01 10:24:02 -0500158 GrDrawOpAtlas::AllowMultitexturing::kYes,
Herb Derby1a496c52020-01-22 17:26:56 -0500159 &evictor);
Robert Phillips4bc70112018-03-01 10:24:02 -0500160 check(reporter, atlas.get(), 0, 4, 0);
161
162 // Fill up the first level
Herb Derby4d721712020-01-24 14:31:16 -0500163 GrDrawOpAtlas::PlotLocator plotLocators[kNumPlots * kNumPlots];
Robert Phillips4bc70112018-03-01 10:24:02 -0500164 for (int i = 0; i < kNumPlots * kNumPlots; ++i) {
Herb Derby4d721712020-01-24 14:31:16 -0500165 bool result = fill_plot(
166 atlas.get(), resourceProvider, &uploadTarget, &plotLocators[i], i * 32);
Robert Phillips4bc70112018-03-01 10:24:02 -0500167 REPORTER_ASSERT(reporter, result);
168 check(reporter, atlas.get(), 1, 4, 1);
169 }
170
171 atlas->instantiate(&onFlushResourceProvider);
172 check(reporter, atlas.get(), 1, 4, 1);
173
174 // Force allocation of a second level
Herb Derby4d721712020-01-24 14:31:16 -0500175 GrDrawOpAtlas::PlotLocator plotLocator;
176 bool result = fill_plot(atlas.get(), resourceProvider, &uploadTarget, &plotLocator, 4 * 32);
Robert Phillips4bc70112018-03-01 10:24:02 -0500177 REPORTER_ASSERT(reporter, result);
178 check(reporter, atlas.get(), 2, 4, 2);
179
180 // Simulate a lot of draws using only the first plot. The last texture should be compacted.
181 for (int i = 0; i < 512; ++i) {
Herb Derby4d721712020-01-24 14:31:16 -0500182 atlas->setLastUseToken(plotLocators[0], uploadTarget.tokenTracker()->nextDrawToken());
Robert Phillips4bc70112018-03-01 10:24:02 -0500183 uploadTarget.issueDrawToken();
184 uploadTarget.flushToken();
185 atlas->compact(uploadTarget.tokenTracker()->nextTokenToFlush());
186 }
187
188 check(reporter, atlas.get(), 1, 4, 1);
189}
190
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500191// This test verifies that the GrAtlasTextOp::onPrepare method correctly handles a failure
192// when allocating an atlas page.
193DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrAtlasTextOpPreparation, reporter, ctxInfo) {
194
195 auto context = ctxInfo.grContext();
196
Robert Phillips9da87e02019-02-04 13:26:26 -0500197 auto gpu = context->priv().getGpu();
198 auto resourceProvider = context->priv().resourceProvider();
199 auto drawingManager = context->priv().drawingManager();
Herb Derby26cbe512018-05-24 14:39:01 -0400200 auto textContext = drawingManager->getTextContext();
Robert Phillips9da87e02019-02-04 13:26:26 -0500201 auto opMemoryPool = context->priv().opMemoryPool();
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500202
Greg Daniele20fcad2020-01-08 11:52:34 -0500203 auto rtc = GrRenderTargetContext::Make(
204 context, GrColorType::kRGBA_8888, nullptr, SkBackingFit::kApprox, {32, 32});
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500205
206 SkPaint paint;
207 paint.setColor(SK_ColorRED);
Mike Reed191e64b2019-01-02 15:35:29 -0500208
209 SkFont font;
210 font.setEdging(SkFont::Edging::kAlias);
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500211
212 const char* text = "a";
213
Herb Derbybc6f9c92018-08-08 13:58:45 -0400214 std::unique_ptr<GrDrawOp> op = textContext->createOp_TestingOnly(
Mike Reed191e64b2019-01-02 15:35:29 -0500215 context, textContext, rtc.get(), paint, font, SkMatrix::I(), text, 16, 16);
Chris Dalton6ce447a2019-06-23 18:07:38 -0600216 bool hasMixedSampledCoverage = false;
217 op->finalize(*context->priv().caps(), nullptr, hasMixedSampledCoverage, GrClampType::kAuto);
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500218
219 TestingUploadTarget uploadTarget;
220
Robert Phillipse5f73282019-06-18 17:15:04 -0400221 GrOpFlushState flushState(gpu, resourceProvider, uploadTarget.writeableTokenTracker());
Greg Daniel16f5c652019-10-29 11:26:01 -0400222
Brian Salomon8afde5f2020-04-01 16:22:00 -0400223 GrSurfaceProxyView surfaceView = rtc->writeSurfaceView();
Robert Phillips901aff02019-10-08 12:32:56 -0400224 GrOpFlushState::OpArgs opArgs(op.get(),
Greg Daniel16f5c652019-10-29 11:26:01 -0400225 &surfaceView,
Robert Phillips901aff02019-10-08 12:32:56 -0400226 nullptr,
Greg Daniel524e28b2019-11-01 11:48:53 -0400227 GrXferProcessor::DstProxyView(GrSurfaceProxyView(),
228 SkIPoint::Make(0, 0)));
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500229
230 // Cripple the atlas manager so it can't allocate any pages. This will force a failure
231 // in the preparation of the text op
Robert Phillips9da87e02019-02-04 13:26:26 -0500232 auto atlasManager = context->priv().getAtlasManager();
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500233 unsigned int numProxies;
Greg Daniel9715b6c2019-12-10 15:03:10 -0500234 atlasManager->getViews(kA8_GrMaskFormat, &numProxies);
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500235 atlasManager->setMaxPages_TestingOnly(0);
236
237 flushState.setOpArgs(&opArgs);
238 op->prepare(&flushState);
239 flushState.setOpArgs(nullptr);
Robert Phillipsc994a932018-06-19 13:09:54 -0400240 opMemoryPool->release(std::move(op));
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500241}
Herb Derby15d9ef22018-10-18 13:41:32 -0400242
Jim Van Verthf6206f92018-12-14 08:22:24 -0500243void test_atlas_config(skiatest::Reporter* reporter, int maxTextureSize, size_t maxBytes,
244 GrMaskFormat maskFormat, SkISize expectedDimensions,
245 SkISize expectedPlotDimensions) {
246 GrDrawOpAtlasConfig config(maxTextureSize, maxBytes);
247 REPORTER_ASSERT(reporter, config.atlasDimensions(maskFormat) == expectedDimensions);
248 REPORTER_ASSERT(reporter, config.plotDimensions(maskFormat) == expectedPlotDimensions);
249}
250
Herb Derby15d9ef22018-10-18 13:41:32 -0400251DEF_GPUTEST(GrDrawOpAtlasConfig_Basic, reporter, options) {
Jim Van Verthf6206f92018-12-14 08:22:24 -0500252 // 1/4 MB
253 test_atlas_config(reporter, 65536, 256 * 1024, kARGB_GrMaskFormat,
254 { 256, 256 }, { 256, 256 });
255 test_atlas_config(reporter, 65536, 256 * 1024, kA8_GrMaskFormat,
256 { 512, 512 }, { 256, 256 });
257 // 1/2 MB
258 test_atlas_config(reporter, 65536, 512 * 1024, kARGB_GrMaskFormat,
259 { 512, 256 }, { 256, 256 });
260 test_atlas_config(reporter, 65536, 512 * 1024, kA8_GrMaskFormat,
261 { 1024, 512 }, { 256, 256 });
262 // 1 MB
263 test_atlas_config(reporter, 65536, 1024 * 1024, kARGB_GrMaskFormat,
264 { 512, 512 }, { 256, 256 });
265 test_atlas_config(reporter, 65536, 1024 * 1024, kA8_GrMaskFormat,
266 { 1024, 1024 }, { 256, 256 });
267 // 2 MB
268 test_atlas_config(reporter, 65536, 2 * 1024 * 1024, kARGB_GrMaskFormat,
269 { 1024, 512 }, { 256, 256 });
270 test_atlas_config(reporter, 65536, 2 * 1024 * 1024, kA8_GrMaskFormat,
271 { 2048, 1024 }, { 512, 256 });
272 // 4 MB
273 test_atlas_config(reporter, 65536, 4 * 1024 * 1024, kARGB_GrMaskFormat,
274 { 1024, 1024 }, { 256, 256 });
275 test_atlas_config(reporter, 65536, 4 * 1024 * 1024, kA8_GrMaskFormat,
Jim Van Verth578b0892018-12-20 20:48:55 +0000276 { 2048, 2048 }, { 512, 512 });
Jim Van Verthf6206f92018-12-14 08:22:24 -0500277 // 8 MB
278 test_atlas_config(reporter, 65536, 8 * 1024 * 1024, kARGB_GrMaskFormat,
279 { 2048, 1024 }, { 256, 256 });
280 test_atlas_config(reporter, 65536, 8 * 1024 * 1024, kA8_GrMaskFormat,
Jim Van Verth578b0892018-12-20 20:48:55 +0000281 { 2048, 2048 }, { 512, 512 });
Jim Van Verthf6206f92018-12-14 08:22:24 -0500282 // 16 MB (should be same as 8 MB)
283 test_atlas_config(reporter, 65536, 16 * 1024 * 1024, kARGB_GrMaskFormat,
284 { 2048, 1024 }, { 256, 256 });
285 test_atlas_config(reporter, 65536, 16 * 1024 * 1024, kA8_GrMaskFormat,
Jim Van Verth578b0892018-12-20 20:48:55 +0000286 { 2048, 2048 }, { 512, 512 });
Jim Van Verthf6206f92018-12-14 08:22:24 -0500287
288 // 4MB, restricted texture size
289 test_atlas_config(reporter, 1024, 8 * 1024 * 1024, kARGB_GrMaskFormat,
290 { 1024, 1024 }, { 256, 256 });
291 test_atlas_config(reporter, 1024, 8 * 1024 * 1024, kA8_GrMaskFormat,
292 { 1024, 1024 }, { 256, 256 });
293
294 // 3 MB (should be same as 2 MB)
295 test_atlas_config(reporter, 65536, 3 * 1024 * 1024, kARGB_GrMaskFormat,
296 { 1024, 512 }, { 256, 256 });
297 test_atlas_config(reporter, 65536, 3 * 1024 * 1024, kA8_GrMaskFormat,
298 { 2048, 1024 }, { 512, 256 });
299
300 // minimum size
301 test_atlas_config(reporter, 65536, 0, kARGB_GrMaskFormat,
302 { 256, 256 }, { 256, 256 });
303 test_atlas_config(reporter, 65536, 0, kA8_GrMaskFormat,
304 { 512, 512 }, { 256, 256 });
Brian Salomon58f153c2018-10-18 21:51:15 -0400305}