• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
8 #include "include/core/SkBitmap.h"
9 #include "include/core/SkColor.h"
10 #include "include/core/SkColorSpace.h"
11 #include "include/core/SkFont.h"
12 #include "include/core/SkImageInfo.h"
13 #include "include/core/SkMatrix.h"
14 #include "include/core/SkPaint.h"
15 #include "include/core/SkSize.h"
16 #include "include/core/SkSurfaceProps.h"
17 #include "include/core/SkTypes.h"
18 #include "include/gpu/GpuTypes.h"
19 #include "include/gpu/GrBackendSurface.h"
20 #include "include/gpu/GrDirectContext.h"
21 #include "include/gpu/GrTypes.h"
22 #include "include/private/gpu/ganesh/GrTypesPriv.h"
23 #include "src/gpu/AtlasTypes.h"
24 #include "src/gpu/SkBackingFit.h"
25 #include "src/gpu/ganesh/GrCaps.h"
26 #include "src/gpu/ganesh/GrDeferredUpload.h"
27 #include "src/gpu/ganesh/GrDirectContextPriv.h"
28 #include "src/gpu/ganesh/GrDrawOpAtlas.h"
29 #include "src/gpu/ganesh/GrDstProxyView.h"
30 #include "src/gpu/ganesh/GrOnFlushResourceProvider.h"
31 #include "src/gpu/ganesh/GrOpFlushState.h"
32 #include "src/gpu/ganesh/GrSurfaceProxy.h"
33 #include "src/gpu/ganesh/GrSurfaceProxyView.h"
34 #include "src/gpu/ganesh/GrXferProcessor.h"
35 #include "src/gpu/ganesh/SurfaceDrawContext.h"
36 #include "src/gpu/ganesh/ops/AtlasTextOp.h"
37 #include "src/gpu/ganesh/ops/GrOp.h"
38 #include "src/gpu/ganesh/text/GrAtlasManager.h"
39 #include "tests/CtsEnforcement.h"
40 #include "tests/Test.h"
41 #include "tools/fonts/FontToolUtils.h"
42 #include "tools/gpu/ganesh/AtlasTextOpTools.h"
43 #include "tools/gpu/ganesh/GrAtlasTools.h"
44 
45 #include <cstddef>
46 #include <cstdint>
47 #include <memory>
48 
49 class GrResourceProvider;
50 struct GrContextOptions;
51 
52 using namespace skgpu::ganesh;
53 using MaskFormat = skgpu::MaskFormat;
54 
55 static const int kNumPlots = 2;
56 static const int kPlotSize = 32;
57 static const int kAtlasSize = kNumPlots * kPlotSize;
58 
59 class AssertOnEvict : public skgpu::PlotEvictionCallback {
60 public:
evict(skgpu::PlotLocator)61     void evict(skgpu::PlotLocator) override {
62         SkASSERT(0); // The unit test shouldn't exercise this code path
63     }
64 };
65 
check(skiatest::Reporter * r,GrDrawOpAtlas * atlas,uint32_t expectedActive,uint32_t expectedMax,int expectedAlloced)66 static void check(skiatest::Reporter* r, GrDrawOpAtlas* atlas,
67                   uint32_t expectedActive, uint32_t expectedMax, int expectedAlloced) {
68     REPORTER_ASSERT(r, expectedActive == atlas->numActivePages());
69     REPORTER_ASSERT(r, expectedMax == atlas->maxPages());
70     REPORTER_ASSERT(r, expectedAlloced == GrDrawOpAtlasTools::NumAllocated(atlas));
71 }
72 
73 class TestingUploadTarget : public GrDeferredUploadTarget {
74 public:
TestingUploadTarget()75     TestingUploadTarget() { }
76 
tokenTracker()77     const skgpu::TokenTracker* tokenTracker() final { return &fTokenTracker; }
writeableTokenTracker()78     skgpu::TokenTracker* writeableTokenTracker() { return &fTokenTracker; }
79 
addInlineUpload(GrDeferredTextureUploadFn &&)80     skgpu::AtlasToken addInlineUpload(GrDeferredTextureUploadFn&&) final {
81         SkASSERT(0); // this test shouldn't invoke this code path
82         return fTokenTracker.nextDrawToken();
83     }
84 
addASAPUpload(GrDeferredTextureUploadFn && upload)85     skgpu::AtlasToken addASAPUpload(GrDeferredTextureUploadFn&& upload) final {
86         return fTokenTracker.nextFlushToken();
87     }
88 
issueDrawToken()89     void issueDrawToken() { fTokenTracker.issueDrawToken(); }
issueFlushToken()90     void issueFlushToken() { fTokenTracker.issueFlushToken(); }
91 
92 private:
93     skgpu::TokenTracker fTokenTracker;
94 
95     using INHERITED = GrDeferredUploadTarget;
96 };
97 
fill_plot(GrDrawOpAtlas * atlas,GrResourceProvider * resourceProvider,GrDeferredUploadTarget * target,skgpu::AtlasLocator * atlasLocator,int alpha)98 static bool fill_plot(GrDrawOpAtlas* atlas,
99                       GrResourceProvider* resourceProvider,
100                       GrDeferredUploadTarget* target,
101                       skgpu::AtlasLocator* atlasLocator,
102                       int alpha) {
103     SkImageInfo ii = SkImageInfo::MakeA8(kPlotSize, kPlotSize);
104 
105     SkBitmap data;
106     data.allocPixels(ii);
107     data.eraseARGB(alpha, 0, 0, 0);
108 
109     GrDrawOpAtlas::ErrorCode code;
110     code = atlas->addToAtlas(resourceProvider, target, kPlotSize, kPlotSize,
111                              data.getAddr(0, 0), atlasLocator);
112     return GrDrawOpAtlas::ErrorCode::kSucceeded == code;
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.
DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS(BasicDrawOpAtlas,reporter,ctxInfo,CtsEnforcement::kApiLevel_T)118 DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS(BasicDrawOpAtlas,
119                                        reporter,
120                                        ctxInfo,
121                                        CtsEnforcement::kApiLevel_T) {
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,
133                                                            GrRenderable::kNo);
134 
135     AssertOnEvict evictor;
136     skgpu::AtlasGenerationCounter counter;
137 
138     std::unique_ptr<GrDrawOpAtlas> atlas = GrDrawOpAtlas::Make(
139                                                 proxyProvider,
140                                                 format,
141                                                 GrColorTypeToSkColorType(atlasColorType),
142                                                 GrColorTypeBytesPerPixel(atlasColorType),
143                                                 kAtlasSize, kAtlasSize,
144                                                 kAtlasSize/kNumPlots, kAtlasSize/kNumPlots,
145                                                 &counter,
146                                                 GrDrawOpAtlas::AllowMultitexturing::kYes,
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);
156         REPORTER_ASSERT(reporter, result);
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);
166     REPORTER_ASSERT(reporter, result);
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.
DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS(GrAtlasTextOpPreparation,reporter,ctxInfo,CtsEnforcement::kApiLevel_T)182 DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS(GrAtlasTextOpPreparation,
183                                        reporter,
184                                        ctxInfo,
185                                        CtsEnforcement::kApiLevel_T) {
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,
192                                                        GrColorType::kRGBA_8888,
193                                                        nullptr,
194                                                        SkBackingFit::kApprox,
195                                                        {32, 32},
196                                                        SkSurfaceProps(),
197                                                        /*label=*/"AtlasTextOpPreparation");
198 
199     SkPaint paint;
200     paint.setColor(SK_ColorRED);
201 
202     SkFont font = ToolUtils::DefaultFont();
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,
225                                   GrDstProxyView(),
226                                   GrXferBarrierFlags::kNone,
227                                   GrLoadOp::kLoad);
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 
test_atlas_config(skiatest::Reporter * reporter,int maxTextureSize,size_t maxBytes,MaskFormat maskFormat,SkISize expectedDimensions,SkISize expectedPlotDimensions)241 void 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 
DEF_GANESH_TEST(GrDrawOpAtlasConfig_Basic,reporter,options,CtsEnforcement::kApiLevel_T)249 DEF_GANESH_TEST(GrDrawOpAtlasConfig_Basic, reporter, options, CtsEnforcement::kApiLevel_T) {
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 }
304