blob: 15f5ec821ec79d28339aa7e60de2c853cf6c26aa [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"
21#include "include/private/GrTextureProxy.h"
22#include "include/private/GrTypesPriv.h"
23#include "src/core/SkIPoint16.h"
Ben Wagner9707a7e2019-05-06 17:17:19 -040024#include "src/gpu/GrCaps.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050025#include "src/gpu/GrContextPriv.h"
26#include "src/gpu/GrDeferredUpload.h"
27#include "src/gpu/GrDrawOpAtlas.h"
28#include "src/gpu/GrDrawingManager.h"
29#include "src/gpu/GrMemoryPool.h"
30#include "src/gpu/GrOnFlushResourceProvider.h"
31#include "src/gpu/GrOpFlushState.h"
32#include "src/gpu/GrRenderTargetContext.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) {
Brian Salomonfd98c2c2018-07-31 17:25:29 -040053 if (fProxies[i]->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
Robert Phillips4bc70112018-03-01 10:24:02 -050075void EvictionFunc(GrDrawOpAtlas::AtlasID atlasID, void*) {
76 SkASSERT(0); // The unit test shouldn't exercise this code path
77}
78
79static void check(skiatest::Reporter* r, GrDrawOpAtlas* atlas,
80 uint32_t expectedActive, uint32_t expectedMax, int expectedAlloced) {
81 REPORTER_ASSERT(r, expectedActive == atlas->numActivePages());
82 REPORTER_ASSERT(r, expectedMax == atlas->maxPages());
83 REPORTER_ASSERT(r, expectedAlloced == atlas->numAllocated_TestingOnly());
84}
85
86class TestingUploadTarget : public GrDeferredUploadTarget {
87public:
88 TestingUploadTarget() { }
89
Robert Phillipsd2e9f762018-03-07 11:54:37 -050090 const GrTokenTracker* tokenTracker() final { return &fTokenTracker; }
91 GrTokenTracker* writeableTokenTracker() { return &fTokenTracker; }
Robert Phillips4bc70112018-03-01 10:24:02 -050092
93 GrDeferredUploadToken addInlineUpload(GrDeferredTextureUploadFn&&) final {
94 SkASSERT(0); // this test shouldn't invoke this code path
95 return fTokenTracker.nextDrawToken();
96 }
97
98 virtual GrDeferredUploadToken addASAPUpload(GrDeferredTextureUploadFn&& upload) final {
99 return fTokenTracker.nextTokenToFlush();
100 }
101
102 void issueDrawToken() { fTokenTracker.issueDrawToken(); }
103 void flushToken() { fTokenTracker.flushToken(); }
104
105private:
106 GrTokenTracker fTokenTracker;
107
108 typedef GrDeferredUploadTarget INHERITED;
109};
110
111static bool fill_plot(GrDrawOpAtlas* atlas,
112 GrResourceProvider* resourceProvider,
113 GrDeferredUploadTarget* target,
114 GrDrawOpAtlas::AtlasID* atlasID,
115 int alpha) {
116 SkImageInfo ii = SkImageInfo::MakeA8(kPlotSize, kPlotSize);
117
118 SkBitmap data;
119 data.allocPixels(ii);
120 data.eraseARGB(alpha, 0, 0, 0);
121
122 SkIPoint16 loc;
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500123 GrDrawOpAtlas::ErrorCode code;
124 code = atlas->addToAtlas(resourceProvider, atlasID, target, kPlotSize, kPlotSize,
125 data.getAddr(0, 0), &loc);
126 return GrDrawOpAtlas::ErrorCode::kSucceeded == code;
Robert Phillips4bc70112018-03-01 10:24:02 -0500127}
128
129
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500130// This is a basic DrawOpAtlas test. It simply verifies that multitexture atlases correctly
131// add and remove pages. Note that this is simulating flush-time behavior.
132DEF_GPUTEST_FOR_RENDERING_CONTEXTS(BasicDrawOpAtlas, reporter, ctxInfo) {
Robert Phillips4bc70112018-03-01 10:24:02 -0500133 auto context = ctxInfo.grContext();
Robert Phillips9da87e02019-02-04 13:26:26 -0500134 auto proxyProvider = context->priv().proxyProvider();
135 auto resourceProvider = context->priv().resourceProvider();
136 auto drawingManager = context->priv().drawingManager();
Robert Phillips4bc70112018-03-01 10:24:02 -0500137
138 GrOnFlushResourceProvider onFlushResourceProvider(drawingManager);
139 TestingUploadTarget uploadTarget;
140
Greg Daniel4065d452018-11-16 15:43:41 -0500141 GrBackendFormat format =
Robert Phillips9da87e02019-02-04 13:26:26 -0500142 context->priv().caps()->getBackendFormatFromColorType(kAlpha_8_SkColorType);
Greg Daniel4065d452018-11-16 15:43:41 -0500143
Robert Phillips4bc70112018-03-01 10:24:02 -0500144 std::unique_ptr<GrDrawOpAtlas> atlas = GrDrawOpAtlas::Make(
145 proxyProvider,
Greg Daniel4065d452018-11-16 15:43:41 -0500146 format,
Robert Phillips4bc70112018-03-01 10:24:02 -0500147 kAlpha_8_GrPixelConfig,
148 kAtlasSize, kAtlasSize,
Jim Van Verthf6206f92018-12-14 08:22:24 -0500149 kAtlasSize/kNumPlots, kAtlasSize/kNumPlots,
Robert Phillips4bc70112018-03-01 10:24:02 -0500150 GrDrawOpAtlas::AllowMultitexturing::kYes,
151 EvictionFunc, nullptr);
152 check(reporter, atlas.get(), 0, 4, 0);
153
154 // Fill up the first level
155 GrDrawOpAtlas::AtlasID atlasIDs[kNumPlots * kNumPlots];
156 for (int i = 0; i < kNumPlots * kNumPlots; ++i) {
157 bool result = fill_plot(atlas.get(), resourceProvider, &uploadTarget, &atlasIDs[i], i*32);
158 REPORTER_ASSERT(reporter, result);
159 check(reporter, atlas.get(), 1, 4, 1);
160 }
161
162 atlas->instantiate(&onFlushResourceProvider);
163 check(reporter, atlas.get(), 1, 4, 1);
164
165 // Force allocation of a second level
166 GrDrawOpAtlas::AtlasID atlasID;
167 bool result = fill_plot(atlas.get(), resourceProvider, &uploadTarget, &atlasID, 4*32);
168 REPORTER_ASSERT(reporter, result);
169 check(reporter, atlas.get(), 2, 4, 2);
170
171 // Simulate a lot of draws using only the first plot. The last texture should be compacted.
172 for (int i = 0; i < 512; ++i) {
173 atlas->setLastUseToken(atlasIDs[0], uploadTarget.tokenTracker()->nextDrawToken());
174 uploadTarget.issueDrawToken();
175 uploadTarget.flushToken();
176 atlas->compact(uploadTarget.tokenTracker()->nextTokenToFlush());
177 }
178
179 check(reporter, atlas.get(), 1, 4, 1);
180}
181
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500182// This test verifies that the GrAtlasTextOp::onPrepare method correctly handles a failure
183// when allocating an atlas page.
184DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrAtlasTextOpPreparation, reporter, ctxInfo) {
185
186 auto context = ctxInfo.grContext();
187
Robert Phillips9da87e02019-02-04 13:26:26 -0500188 auto gpu = context->priv().getGpu();
189 auto resourceProvider = context->priv().resourceProvider();
Brian Salomon2c791fc2019-04-02 11:52:03 -0400190 auto resourceCache = context->priv().getResourceCache();
Robert Phillips9da87e02019-02-04 13:26:26 -0500191 auto drawingManager = context->priv().drawingManager();
Herb Derby26cbe512018-05-24 14:39:01 -0400192 auto textContext = drawingManager->getTextContext();
Robert Phillips9da87e02019-02-04 13:26:26 -0500193 auto opMemoryPool = context->priv().opMemoryPool();
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500194
Greg Daniel4065d452018-11-16 15:43:41 -0500195 GrBackendFormat format =
Robert Phillips9da87e02019-02-04 13:26:26 -0500196 context->priv().caps()->getBackendFormatFromColorType(kRGBA_8888_SkColorType);
Greg Daniel4065d452018-11-16 15:43:41 -0500197
Robert Phillips9da87e02019-02-04 13:26:26 -0500198 auto rtc = context->priv().makeDeferredRenderTargetContext(format,
199 SkBackingFit::kApprox,
200 32, 32,
201 kRGBA_8888_GrPixelConfig,
202 nullptr);
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500203
204 SkPaint paint;
205 paint.setColor(SK_ColorRED);
Mike Reed191e64b2019-01-02 15:35:29 -0500206
207 SkFont font;
208 font.setEdging(SkFont::Edging::kAlias);
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500209
210 const char* text = "a";
211
Herb Derbybc6f9c92018-08-08 13:58:45 -0400212 std::unique_ptr<GrDrawOp> op = textContext->createOp_TestingOnly(
Mike Reed191e64b2019-01-02 15:35:29 -0500213 context, textContext, rtc.get(), paint, font, SkMatrix::I(), text, 16, 16);
Brian Osman5ced0bf2019-03-15 10:15:29 -0400214 op->finalize(*context->priv().caps(), nullptr, GrFSAAType::kNone, GrClampType::kAuto);
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500215
216 TestingUploadTarget uploadTarget;
217
Brian Salomon2c791fc2019-04-02 11:52:03 -0400218 GrOpFlushState flushState(gpu, resourceProvider, resourceCache,
219 uploadTarget.writeableTokenTracker());
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500220 GrOpFlushState::OpArgs opArgs = {
221 op.get(),
222 rtc->asRenderTargetProxy(),
223 nullptr,
224 GrXferProcessor::DstProxy(nullptr, SkIPoint::Make(0, 0))
225 };
226
227 // Cripple the atlas manager so it can't allocate any pages. This will force a failure
228 // in the preparation of the text op
Robert Phillips9da87e02019-02-04 13:26:26 -0500229 auto atlasManager = context->priv().getAtlasManager();
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500230 unsigned int numProxies;
231 atlasManager->getProxies(kA8_GrMaskFormat, &numProxies);
232 atlasManager->setMaxPages_TestingOnly(0);
233
234 flushState.setOpArgs(&opArgs);
235 op->prepare(&flushState);
236 flushState.setOpArgs(nullptr);
Robert Phillipsc994a932018-06-19 13:09:54 -0400237 opMemoryPool->release(std::move(op));
Robert Phillipsd2e9f762018-03-07 11:54:37 -0500238}
Herb Derby15d9ef22018-10-18 13:41:32 -0400239
Jim Van Verthf6206f92018-12-14 08:22:24 -0500240void test_atlas_config(skiatest::Reporter* reporter, int maxTextureSize, size_t maxBytes,
241 GrMaskFormat maskFormat, SkISize expectedDimensions,
242 SkISize expectedPlotDimensions) {
243 GrDrawOpAtlasConfig config(maxTextureSize, maxBytes);
244 REPORTER_ASSERT(reporter, config.atlasDimensions(maskFormat) == expectedDimensions);
245 REPORTER_ASSERT(reporter, config.plotDimensions(maskFormat) == expectedPlotDimensions);
246}
247
Herb Derby15d9ef22018-10-18 13:41:32 -0400248DEF_GPUTEST(GrDrawOpAtlasConfig_Basic, reporter, options) {
Jim Van Verthf6206f92018-12-14 08:22:24 -0500249 // 1/4 MB
250 test_atlas_config(reporter, 65536, 256 * 1024, kARGB_GrMaskFormat,
251 { 256, 256 }, { 256, 256 });
252 test_atlas_config(reporter, 65536, 256 * 1024, kA8_GrMaskFormat,
253 { 512, 512 }, { 256, 256 });
254 // 1/2 MB
255 test_atlas_config(reporter, 65536, 512 * 1024, kARGB_GrMaskFormat,
256 { 512, 256 }, { 256, 256 });
257 test_atlas_config(reporter, 65536, 512 * 1024, kA8_GrMaskFormat,
258 { 1024, 512 }, { 256, 256 });
259 // 1 MB
260 test_atlas_config(reporter, 65536, 1024 * 1024, kARGB_GrMaskFormat,
261 { 512, 512 }, { 256, 256 });
262 test_atlas_config(reporter, 65536, 1024 * 1024, kA8_GrMaskFormat,
263 { 1024, 1024 }, { 256, 256 });
264 // 2 MB
265 test_atlas_config(reporter, 65536, 2 * 1024 * 1024, kARGB_GrMaskFormat,
266 { 1024, 512 }, { 256, 256 });
267 test_atlas_config(reporter, 65536, 2 * 1024 * 1024, kA8_GrMaskFormat,
268 { 2048, 1024 }, { 512, 256 });
269 // 4 MB
270 test_atlas_config(reporter, 65536, 4 * 1024 * 1024, kARGB_GrMaskFormat,
271 { 1024, 1024 }, { 256, 256 });
272 test_atlas_config(reporter, 65536, 4 * 1024 * 1024, kA8_GrMaskFormat,
Jim Van Verth578b0892018-12-20 20:48:55 +0000273 { 2048, 2048 }, { 512, 512 });
Jim Van Verthf6206f92018-12-14 08:22:24 -0500274 // 8 MB
275 test_atlas_config(reporter, 65536, 8 * 1024 * 1024, kARGB_GrMaskFormat,
276 { 2048, 1024 }, { 256, 256 });
277 test_atlas_config(reporter, 65536, 8 * 1024 * 1024, kA8_GrMaskFormat,
Jim Van Verth578b0892018-12-20 20:48:55 +0000278 { 2048, 2048 }, { 512, 512 });
Jim Van Verthf6206f92018-12-14 08:22:24 -0500279 // 16 MB (should be same as 8 MB)
280 test_atlas_config(reporter, 65536, 16 * 1024 * 1024, kARGB_GrMaskFormat,
281 { 2048, 1024 }, { 256, 256 });
282 test_atlas_config(reporter, 65536, 16 * 1024 * 1024, kA8_GrMaskFormat,
Jim Van Verth578b0892018-12-20 20:48:55 +0000283 { 2048, 2048 }, { 512, 512 });
Jim Van Verthf6206f92018-12-14 08:22:24 -0500284
285 // 4MB, restricted texture size
286 test_atlas_config(reporter, 1024, 8 * 1024 * 1024, kARGB_GrMaskFormat,
287 { 1024, 1024 }, { 256, 256 });
288 test_atlas_config(reporter, 1024, 8 * 1024 * 1024, kA8_GrMaskFormat,
289 { 1024, 1024 }, { 256, 256 });
290
291 // 3 MB (should be same as 2 MB)
292 test_atlas_config(reporter, 65536, 3 * 1024 * 1024, kARGB_GrMaskFormat,
293 { 1024, 512 }, { 256, 256 });
294 test_atlas_config(reporter, 65536, 3 * 1024 * 1024, kA8_GrMaskFormat,
295 { 2048, 1024 }, { 512, 256 });
296
297 // minimum size
298 test_atlas_config(reporter, 65536, 0, kARGB_GrMaskFormat,
299 { 256, 256 }, { 256, 256 });
300 test_atlas_config(reporter, 65536, 0, kA8_GrMaskFormat,
301 { 512, 512 }, { 256, 256 });
Brian Salomon58f153c2018-10-18 21:51:15 -0400302}