• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2024 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 "include/core/SkBitmap.h"
9 #include "include/core/SkColorType.h"
10 #include "include/core/SkImageInfo.h"
11 #include "include/gpu/graphite/Context.h"
12 #include "include/gpu/graphite/Recorder.h"
13 #include "src/gpu/graphite/DrawAtlas.h"
14 #include "src/gpu/graphite/RecorderPriv.h"
15 #include "src/gpu/graphite/TextureProxy.h"
16 
17 #include "tests/CtsEnforcement.h"
18 #include "tests/Test.h"
19 
20 using namespace skgpu::graphite;
21 using MaskFormat = skgpu::MaskFormat;
22 
23 namespace {
24 const int kNumPlots = 2;
25 const int kPlotSize = 32;
26 const int kAtlasSize = kNumPlots * kPlotSize;
27 uint32_t gEvictCount = 0;
28 
29 class PlotEvictionCounter : public skgpu::PlotEvictionCallback {
30 public:
evict(skgpu::PlotLocator)31     void evict(skgpu::PlotLocator) override {
32         ++gEvictCount;
33     }
34 };
35 
check(skiatest::Reporter * r,DrawAtlas * atlas,uint32_t expectedActive,uint32_t expectedEvictCount)36 void check(skiatest::Reporter* r, DrawAtlas* atlas,
37            uint32_t expectedActive, uint32_t expectedEvictCount) {
38     REPORTER_ASSERT(r, atlas->numActivePages() == expectedActive);
39     REPORTER_ASSERT(r, gEvictCount == expectedEvictCount);
40     REPORTER_ASSERT(r, atlas->maxPages() == skgpu::PlotLocator::kMaxMultitexturePages);
41 }
42 
fill_plot(DrawAtlas * atlas,Recorder * recorder,skgpu::AtlasLocator * atlasLocator,int alpha)43 bool fill_plot(DrawAtlas* atlas,
44                Recorder* recorder,
45                skgpu::AtlasLocator* atlasLocator,
46                int alpha) {
47     SkImageInfo ii = SkImageInfo::MakeA8(kPlotSize, kPlotSize);
48 
49     SkBitmap data;
50     data.allocPixels(ii);
51     data.eraseARGB(alpha, 0, 0, 0);
52 
53     DrawAtlas::ErrorCode code;
54     code = atlas->addToAtlas(recorder, kPlotSize, kPlotSize,
55                              data.getAddr(0, 0), atlasLocator);
56     return DrawAtlas::ErrorCode::kSucceeded == code;
57 }
58 }  // anonymous namespace
59 
60 // This is a basic DrawOpAtlas test. It simply verifies that multitexture atlases correctly
61 // add and remove pages. Note that this is simulating flush-time behavior.
DEF_GRAPHITE_TEST_FOR_RENDERING_CONTEXTS(BasicDrawAtlas,reporter,context,CtsEnforcement::kNever)62 DEF_GRAPHITE_TEST_FOR_RENDERING_CONTEXTS(BasicDrawAtlas,
63                                          reporter,
64                                          context,
65                                          CtsEnforcement::kNever) {
66     auto recorder = context->makeRecorder();
67 
68     SkColorType atlasColorType = kAlpha_8_SkColorType;
69     PlotEvictionCounter evictor;
70     skgpu::AtlasGenerationCounter counter;
71 
72     std::unique_ptr<DrawAtlas> atlas = DrawAtlas::Make(atlasColorType,
73                                                        SkColorTypeBytesPerPixel(atlasColorType),
74                                                        kAtlasSize, kAtlasSize,
75                                                        kAtlasSize/kNumPlots, kAtlasSize/kNumPlots,
76                                                        &counter,
77                                                        DrawAtlas::AllowMultitexturing::kYes,
78                                                        DrawAtlas::UseStorageTextures::kNo,
79                                                        &evictor,
80                                                        /*label=*/"BasicDrawAtlasTest");
81     check(reporter, atlas.get(), /*expectedActive=*/0, /*expectedEvictCount=*/0);
82 
83     // Fill up the first page
84     skgpu::AtlasLocator atlasLocator;
85     skgpu::AtlasLocator testAtlasLocator;
86     for (int i = 0; i < kNumPlots * kNumPlots; ++i) {
87         bool result = fill_plot(atlas.get(), recorder.get(), &atlasLocator, i * 32);
88         REPORTER_ASSERT(reporter, result);
89         // We will check drawing with the first plot.
90         if (i == 0) {
91             testAtlasLocator = atlasLocator;
92         }
93         check(reporter, atlas.get(), /*expectedActive=*/1, /*expectedEvictCount=*/0);
94     }
95 
96     // Force creation of a second page.
97     bool result = fill_plot(atlas.get(), recorder.get(), &atlasLocator, 4 * 32);
98     REPORTER_ASSERT(reporter, result);
99     check(reporter, atlas.get(), /*expectedActive=*/2, /*expectedEvictCount=*/0);
100 
101     // Simulate a lot of draws using only the first plot. The last texture should be compacted.
102     for (int i = 0; i < 512; ++i) {
103         atlas->setLastUseToken(testAtlasLocator, recorder->priv().tokenTracker()->nextFlushToken());
104         recorder->priv().issueFlushToken();
105         atlas->compact(recorder->priv().tokenTracker()->nextFlushToken());
106     }
107     check(reporter, atlas.get(), /*expectedActive=*/1, /*expectedEvictCount=*/1);
108 
109     // Simulate a lot of non-atlas draws. We should end up with no textures.
110     for (int i = 0; i < 512; ++i) {
111         recorder->priv().issueFlushToken();
112         atlas->compact(recorder->priv().tokenTracker()->nextFlushToken());
113     }
114     check(reporter, atlas.get(), /*expectedActive=*/0, /*expectedEvictCount=*/5);
115 
116     // Fill the atlas all the way up.
117     gEvictCount = 0;
118     for (int p = 1; p <= 4; ++p) {
119         for (int i = 0; i < kNumPlots * kNumPlots; ++i) {
120             result = fill_plot(atlas.get(), recorder.get(), &atlasLocator, p * i * 16);
121             atlas->setLastUseToken(atlasLocator, recorder->priv().tokenTracker()->nextFlushToken());
122             // We will check drawing with plot index 2 in the 3rd page
123             if (p == 3 && i == 2) {
124                 testAtlasLocator = atlasLocator;
125             }
126             REPORTER_ASSERT(reporter, result);
127         }
128         check(reporter, atlas.get(), /*expectedActive=*/p, /*expectedEvictCount=*/0);
129     }
130     // Try one more, it should fail.
131     result = fill_plot(atlas.get(), recorder.get(), &atlasLocator, 0xff);
132     REPORTER_ASSERT(reporter, !result);
133 
134     // Try to clear everything out. Should fail because there are pending "draws."
135     atlas->freeGpuResources(recorder->priv().tokenTracker()->nextFlushToken());
136     check(reporter, atlas.get(), /*expectedActive=*/4, /*expectedEvictCount=*/0);
137 
138     // Flush those draws
139     recorder->priv().issueFlushToken();
140     atlas->compact(recorder->priv().tokenTracker()->nextFlushToken());
141 
142     // Simulate a draw using only a plot in the 3rd page
143     atlas->setLastUseToken(testAtlasLocator, recorder->priv().tokenTracker()->nextFlushToken());
144 
145     // FreeGpuResources should only remove the 4th page
146     atlas->freeGpuResources(recorder->priv().tokenTracker()->nextFlushToken());
147     check(reporter, atlas.get(), /*expectedActive=*/3, /*expectedEvictCount=*/4);
148 
149     // Now flush
150     recorder->priv().issueFlushToken();
151     atlas->compact(recorder->priv().tokenTracker()->nextFlushToken());
152 
153     // FreeGpuResources should clear everything out
154     atlas->freeGpuResources(recorder->priv().tokenTracker()->nextFlushToken());
155     check(reporter, atlas.get(), /*expectedActive=*/0, /*expectedEvictCount=*/16);
156 }
157 
158 ///////////////////////////////////////////////////////////////////////////////////////////////
159 
160 namespace {
test_draw_atlas_config(skiatest::Reporter * reporter,int maxTextureSize,size_t maxBytes,MaskFormat maskFormat,SkISize expectedDimensions,SkISize expectedPlotDimensions)161 void test_draw_atlas_config(skiatest::Reporter* reporter, int maxTextureSize, size_t maxBytes,
162                             MaskFormat maskFormat, SkISize expectedDimensions,
163                             SkISize expectedPlotDimensions) {
164     DrawAtlasConfig config(maxTextureSize, maxBytes);
165     REPORTER_ASSERT(reporter, config.atlasDimensions(maskFormat) == expectedDimensions);
166     REPORTER_ASSERT(reporter, config.plotDimensions(maskFormat) == expectedPlotDimensions);
167 }
168 }  // anonymous namespace
169 
DEF_GRAPHITE_TEST(DrawAtlasConfig_Basic,reporter,CtsEnforcement::kNever)170 DEF_GRAPHITE_TEST(DrawAtlasConfig_Basic, reporter, CtsEnforcement::kNever) {
171     // 1/4 MB
172     test_draw_atlas_config(reporter, 65536, 256 * 1024, MaskFormat::kARGB,
173                            { 256, 256 }, { 256, 256 });
174     test_draw_atlas_config(reporter, 65536, 256 * 1024, MaskFormat::kA8,
175                            { 512, 512 }, { 256, 256 });
176     // 1/2 MB
177     test_draw_atlas_config(reporter, 65536, 512 * 1024, MaskFormat::kARGB,
178                            { 512, 256 }, { 256, 256 });
179     test_draw_atlas_config(reporter, 65536, 512 * 1024, MaskFormat::kA8,
180                            { 1024, 512 }, { 256, 256 });
181     // 1 MB
182     test_draw_atlas_config(reporter, 65536, 1024 * 1024, MaskFormat::kARGB,
183                            { 512, 512 }, { 256, 256 });
184     test_draw_atlas_config(reporter, 65536, 1024 * 1024, MaskFormat::kA8,
185                            { 1024, 1024 }, { 256, 256 });
186     // 2 MB
187     test_draw_atlas_config(reporter, 65536, 2 * 1024 * 1024, MaskFormat::kARGB,
188                            { 1024, 512 }, { 256, 256 });
189     test_draw_atlas_config(reporter, 65536, 2 * 1024 * 1024, MaskFormat::kA8,
190                            { 2048, 1024 }, { 512, 256 });
191     // 4 MB
192     test_draw_atlas_config(reporter, 65536, 4 * 1024 * 1024, MaskFormat::kARGB,
193                            { 1024, 1024 }, { 256, 256 });
194     test_draw_atlas_config(reporter, 65536, 4 * 1024 * 1024, MaskFormat::kA8,
195                            { 2048, 2048 }, { 512, 512 });
196     // 8 MB
197     test_draw_atlas_config(reporter, 65536, 8 * 1024 * 1024, MaskFormat::kARGB,
198                            { 2048, 1024 }, { 256, 256 });
199     test_draw_atlas_config(reporter, 65536, 8 * 1024 * 1024, MaskFormat::kA8,
200                            { 2048, 2048 }, { 512, 512 });
201     // 16 MB (should be same as 8 MB)
202     test_draw_atlas_config(reporter, 65536, 16 * 1024 * 1024, MaskFormat::kARGB,
203                            { 2048, 1024 }, { 256, 256 });
204     test_draw_atlas_config(reporter, 65536, 16 * 1024 * 1024, MaskFormat::kA8,
205                            { 2048, 2048 }, { 512, 512 });
206 
207     // 4MB, restricted texture size
208     test_draw_atlas_config(reporter, 1024, 8 * 1024 * 1024, MaskFormat::kARGB,
209                            { 1024, 1024 }, { 256, 256 });
210     test_draw_atlas_config(reporter, 1024, 8 * 1024 * 1024, MaskFormat::kA8,
211                            { 1024, 1024 }, { 256, 256 });
212 
213     // 3 MB (should be same as 2 MB)
214     test_draw_atlas_config(reporter, 65536, 3 * 1024 * 1024, MaskFormat::kARGB,
215                            { 1024, 512 }, { 256, 256 });
216     test_draw_atlas_config(reporter, 65536, 3 * 1024 * 1024, MaskFormat::kA8,
217                            { 2048, 1024 }, { 512, 256 });
218 
219     // minimum size
220     test_draw_atlas_config(reporter, 65536, 0, MaskFormat::kARGB,
221                            { 256, 256 }, { 256, 256 });
222     test_draw_atlas_config(reporter, 65536, 0, MaskFormat::kA8,
223                            { 512, 512 }, { 256, 256 });
224 }
225