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::kNextRelease)62 DEF_GRAPHITE_TEST_FOR_RENDERING_CONTEXTS(BasicDrawAtlas,
63 reporter,
64 context,
65 CtsEnforcement::kNextRelease) {
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(), 0, 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(), 1, 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(), 2, 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(), /*forceCompact=*/false);
106 }
107 check(reporter, atlas.get(), 1, 0);
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(), /*forceCompact=*/false);
113 }
114 check(reporter, atlas.get(), 0, 1);
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(), p, 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
135 ///////////////////////////////////////////////////////////////////////////////////////////////
136
137 namespace {
test_draw_atlas_config(skiatest::Reporter * reporter,int maxTextureSize,size_t maxBytes,MaskFormat maskFormat,SkISize expectedDimensions,SkISize expectedPlotDimensions)138 void test_draw_atlas_config(skiatest::Reporter* reporter, int maxTextureSize, size_t maxBytes,
139 MaskFormat maskFormat, SkISize expectedDimensions,
140 SkISize expectedPlotDimensions) {
141 DrawAtlasConfig config(maxTextureSize, maxBytes);
142 REPORTER_ASSERT(reporter, config.atlasDimensions(maskFormat) == expectedDimensions);
143 REPORTER_ASSERT(reporter, config.plotDimensions(maskFormat) == expectedPlotDimensions);
144 }
145 } // anonymous namespace
146
DEF_GRAPHITE_TEST(DrawAtlasConfig_Basic,reporter,CtsEnforcement::kNextRelease)147 DEF_GRAPHITE_TEST(DrawAtlasConfig_Basic, reporter, CtsEnforcement::kNextRelease) {
148 // 1/4 MB
149 test_draw_atlas_config(reporter, 65536, 256 * 1024, MaskFormat::kARGB,
150 { 256, 256 }, { 256, 256 });
151 test_draw_atlas_config(reporter, 65536, 256 * 1024, MaskFormat::kA8,
152 { 512, 512 }, { 256, 256 });
153 // 1/2 MB
154 test_draw_atlas_config(reporter, 65536, 512 * 1024, MaskFormat::kARGB,
155 { 512, 256 }, { 256, 256 });
156 test_draw_atlas_config(reporter, 65536, 512 * 1024, MaskFormat::kA8,
157 { 1024, 512 }, { 256, 256 });
158 // 1 MB
159 test_draw_atlas_config(reporter, 65536, 1024 * 1024, MaskFormat::kARGB,
160 { 512, 512 }, { 256, 256 });
161 test_draw_atlas_config(reporter, 65536, 1024 * 1024, MaskFormat::kA8,
162 { 1024, 1024 }, { 256, 256 });
163 // 2 MB
164 test_draw_atlas_config(reporter, 65536, 2 * 1024 * 1024, MaskFormat::kARGB,
165 { 1024, 512 }, { 256, 256 });
166 test_draw_atlas_config(reporter, 65536, 2 * 1024 * 1024, MaskFormat::kA8,
167 { 2048, 1024 }, { 512, 256 });
168 // 4 MB
169 test_draw_atlas_config(reporter, 65536, 4 * 1024 * 1024, MaskFormat::kARGB,
170 { 1024, 1024 }, { 256, 256 });
171 test_draw_atlas_config(reporter, 65536, 4 * 1024 * 1024, MaskFormat::kA8,
172 { 2048, 2048 }, { 512, 512 });
173 // 8 MB
174 test_draw_atlas_config(reporter, 65536, 8 * 1024 * 1024, MaskFormat::kARGB,
175 { 2048, 1024 }, { 256, 256 });
176 test_draw_atlas_config(reporter, 65536, 8 * 1024 * 1024, MaskFormat::kA8,
177 { 2048, 2048 }, { 512, 512 });
178 // 16 MB (should be same as 8 MB)
179 test_draw_atlas_config(reporter, 65536, 16 * 1024 * 1024, MaskFormat::kARGB,
180 { 2048, 1024 }, { 256, 256 });
181 test_draw_atlas_config(reporter, 65536, 16 * 1024 * 1024, MaskFormat::kA8,
182 { 2048, 2048 }, { 512, 512 });
183
184 // 4MB, restricted texture size
185 test_draw_atlas_config(reporter, 1024, 8 * 1024 * 1024, MaskFormat::kARGB,
186 { 1024, 1024 }, { 256, 256 });
187 test_draw_atlas_config(reporter, 1024, 8 * 1024 * 1024, MaskFormat::kA8,
188 { 1024, 1024 }, { 256, 256 });
189
190 // 3 MB (should be same as 2 MB)
191 test_draw_atlas_config(reporter, 65536, 3 * 1024 * 1024, MaskFormat::kARGB,
192 { 1024, 512 }, { 256, 256 });
193 test_draw_atlas_config(reporter, 65536, 3 * 1024 * 1024, MaskFormat::kA8,
194 { 2048, 1024 }, { 512, 256 });
195
196 // minimum size
197 test_draw_atlas_config(reporter, 65536, 0, MaskFormat::kARGB,
198 { 256, 256 }, { 256, 256 });
199 test_draw_atlas_config(reporter, 65536, 0, MaskFormat::kA8,
200 { 512, 512 }, { 256, 256 });
201 }
202