Debian Codegraph @

fuzz/FuzzPrecompile.cpp

raw
1 /*
2 * Copyright 2023 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 "fuzz/Fuzz.h"
9
10 #include "include/core/SkCanvas.h"
11 #include "include/core/SkColorFilter.h"
12 #include "include/core/SkColorSpace.h"
13 #include "include/core/SkFont.h"
14 #include "include/core/SkImageInfo.h"
15 #include "include/core/SkPaint.h"
16 #include "include/core/SkPathBuilder.h"
17 #include "include/core/SkRefCnt.h"
18 #include "include/effects/SkColorMatrix.h"
19 #include "include/gpu/graphite/Context.h"
20 #include "include/gpu/graphite/PrecompileContext.h"
21 #include "include/gpu/graphite/Surface.h"
22 #include "include/gpu/graphite/precompile/Precompile.h"
23 #include "include/gpu/graphite/precompile/PrecompileColorFilter.h"
24 #include "modules/skcms/skcms.h"
25 #include "src/core/SkBlenderBase.h"
26 #include "src/gpu/graphite/ContextPriv.h"
27 #include "src/gpu/graphite/ContextUtils.h"
28 #include "src/gpu/graphite/DrawContext.h"
29 #include "src/gpu/graphite/KeyContext.h"
30 #include "src/gpu/graphite/PaintParams.h"
31 #include "src/gpu/graphite/PaintParamsKey.h"
32 #include "src/gpu/graphite/PipelineData.h"
33 #include "src/gpu/graphite/RecorderPriv.h"
34 #include "src/gpu/graphite/RenderPassDesc.h"
35 #include "src/gpu/graphite/Renderer.h"
36 #include "src/gpu/graphite/RuntimeEffectDictionary.h"
37 #include "src/gpu/graphite/geom/Geometry.h"
38 #include "src/gpu/graphite/precompile/PaintOptionsPriv.h"
39 #include "tools/ganesh/GrContextFactory.h"
40 #include "tools/graphite/ContextFactory.h"
41
42 using namespace skgpu::graphite;
43
44 namespace {
45
46 SkBlendMode random_blend_mode(Fuzz* fuzz) {
47 uint32_t temp;
48 fuzz->next(&temp);
49 return (SkBlendMode) (temp % kSkBlendModeCount);
50 }
51
52 SkColor random_opaque_skcolor(Fuzz* fuzz) {
53 SkColor color;
54 fuzz->next(&color);
55 return 0xff000000 | color;
56 }
57
58 SkColor4f random_color4f(Fuzz* fuzz) {
59 bool makeOpaque;
60 fuzz->next(&makeOpaque);
61
62 SkColor4f color;
63 fuzz->nextRange(&color.fR, 0, 1);
64 fuzz->nextRange(&color.fG, 0, 1);
65 fuzz->nextRange(&color.fB, 0, 1);
66 if (makeOpaque) {
67 color.fA = 1.0;
68 } else {
69 fuzz->nextRange(&color.fA, 0, 1);
70 }
71
72 return color;
73 }
74
75 SkPath make_path() {
76 SkPathBuilder path;
77 path.moveTo(0, 0);
78 path.lineTo(8, 2);
79 path.lineTo(16, 0);
80 path.lineTo(14, 8);
81 path.lineTo(16, 16);
82 path.lineTo(8, 14);
83 path.lineTo(0, 16);
84 path.lineTo(2, 8);
85 path.close();
86 return path.detach();
87 }
88
89 //--------------------------------------------------------------------------------------------------
90 // color spaces
91
92 const skcms_TransferFunction& random_transfer_function(Fuzz* fuzz) {
93 static constexpr skcms_TransferFunction gTransferFunctions[] = {
94 SkNamedTransferFn::kSRGB,
95 SkNamedTransferFn::k2Dot2,
96 SkNamedTransferFn::kLinear,
97 SkNamedTransferFn::kRec2020,
98 SkNamedTransferFn::kPQ,
99 SkNamedTransferFn::kHLG,
100 };
101
102 uint32_t xferFunction;
103 fuzz->next(&xferFunction);
104 xferFunction %= std::size(gTransferFunctions);
105 return gTransferFunctions[xferFunction];
106 }
107
108 const skcms_Matrix3x3& random_gamut(Fuzz* fuzz) {
109 static constexpr skcms_Matrix3x3 gGamuts[] = {
110 SkNamedGamut::kSRGB,
111 SkNamedGamut::kAdobeRGB,
112 SkNamedGamut::kDisplayP3,
113 SkNamedGamut::kRec2020,
114 SkNamedGamut::kXYZ,
115 };
116
117 uint32_t gamut;
118 fuzz->next(&gamut);
119 gamut %= std::size(gGamuts);
120 return gGamuts[gamut];
121 }
122
123 enum class ColorSpaceType {
124 kNone,
125 kSRGB,
126 kSRGBLinear,
127 kRGB,
128
129 kLast = kRGB
130 };
131
132 static constexpr int kColorSpaceTypeCount = static_cast<int>(ColorSpaceType::kLast) + 1;
133
134 sk_sp<SkColorSpace> create_colorspace(Fuzz* fuzz, ColorSpaceType csType) {
135 switch (csType) {
136 case ColorSpaceType::kNone:
137 return nullptr;
138 case ColorSpaceType::kSRGB:
139 return SkColorSpace::MakeSRGB();
140 case ColorSpaceType::kSRGBLinear:
141 return SkColorSpace::MakeSRGBLinear();
142 case ColorSpaceType::kRGB:
143 return SkColorSpace::MakeRGB(random_transfer_function(fuzz), random_gamut(fuzz));
144 }
145
146 SkUNREACHABLE;
147 }
148
149 sk_sp<SkColorSpace> create_random_colorspace(Fuzz* fuzz) {
150 uint32_t temp;
151 fuzz->next(&temp);
152 ColorSpaceType csType = (ColorSpaceType) (temp % kColorSpaceTypeCount);
153
154 return create_colorspace(fuzz, csType);
155 }
156
157 //--------------------------------------------------------------------------------------------------
158 // color filters
159
160 enum class ColorFilterType {
161 kNone,
162 kBlend,
163 kMatrix,
164 kHSLAMatrix,
165 // TODO: add more color filters
166
167 kLast = kHSLAMatrix
168 };
169
170 static constexpr int kColorFilterTypeCount = static_cast<int>(ColorFilterType::kLast) + 1;
171
172 std::pair<sk_sp<SkColorFilter>, sk_sp<PrecompileColorFilter>> create_blend_colorfilter(
173 Fuzz* fuzz) {
174
175 sk_sp<SkColorFilter> cf;
176
177 // SkColorFilters::Blend is clever and can weed out noop color filters. Loop until we get
178 // a valid color filter.
179 while (!cf && !fuzz->exhausted()) {
180 cf = SkColorFilters::Blend(random_color4f(fuzz),
181 create_random_colorspace(fuzz),
182 random_blend_mode(fuzz));
183 }
184
185 sk_sp<PrecompileColorFilter> o = cf ? PrecompileColorFilters::Blend() : nullptr;
186
187 return { cf, o };
188 }
189
190 std::pair<sk_sp<SkColorFilter>, sk_sp<PrecompileColorFilter>> create_matrix_colorfilter() {
191 sk_sp<SkColorFilter> cf = SkColorFilters::Matrix(
192 SkColorMatrix::RGBtoYUV(SkYUVColorSpace::kJPEG_Full_SkYUVColorSpace));
193 sk_sp<PrecompileColorFilter> o = PrecompileColorFilters::Matrix();
194
195 return { cf, o };
196 }
197
198 std::pair<sk_sp<SkColorFilter>, sk_sp<PrecompileColorFilter>> create_hsla_matrix_colorfilter() {
199 sk_sp<SkColorFilter> cf = SkColorFilters::HSLAMatrix(
200 SkColorMatrix::RGBtoYUV(SkYUVColorSpace::kJPEG_Full_SkYUVColorSpace));
201 sk_sp<PrecompileColorFilter> o = PrecompileColorFilters::HSLAMatrix();
202
203 return { cf, o };
204 }
205
206 std::pair<sk_sp<SkColorFilter>, sk_sp<PrecompileColorFilter>> create_colorfilter(
207 Fuzz* fuzz,
208 ColorFilterType type,
209 int depth) {
210 if (depth <= 0) {
211 return {};
212 }
213
214 switch (type) {
215 case ColorFilterType::kNone:
216 return { nullptr, nullptr };
217 case ColorFilterType::kBlend:
218 return create_blend_colorfilter(fuzz);
219 case ColorFilterType::kMatrix:
220 return create_matrix_colorfilter();
221 case ColorFilterType::kHSLAMatrix:
222 return create_hsla_matrix_colorfilter();
223 }
224
225 SkUNREACHABLE;
226 }
227
228 std::pair<sk_sp<SkColorFilter>, sk_sp<PrecompileColorFilter>> create_random_colorfilter(
229 Fuzz* fuzz,
230 int depth) {
231
232 uint32_t temp;
233 fuzz->next(&temp);
234 ColorFilterType cf = (ColorFilterType) (temp % kColorFilterTypeCount);
235
236 return create_colorfilter(fuzz, cf, depth);
237 }
238
239 //--------------------------------------------------------------------------------------------------
240 std::pair<SkPaint, PaintOptions> create_random_paint(Fuzz* fuzz, int depth) {
241 if (depth <= 0) {
242 return {};
243 }
244
245 SkPaint paint;
246 paint.setColor(random_opaque_skcolor(fuzz));
247
248 PaintOptions paintOptions;
249
250 {
251 auto [cf, o] = create_random_colorfilter(fuzz, depth - 1);
252 SkASSERT_RELEASE(!cf == !o);
253
254 if (cf) {
255 paint.setColorFilter(std::move(cf));
256 paintOptions.setColorFilters({{o}});
257 }
258 }
259
260 return { paint, paintOptions };
261 }
262
263 //--------------------------------------------------------------------------------------------------
264 void check_draw(Context* context,
265 Recorder* recorder,
266 const SkPaint& paint,
267 DrawTypeFlags dt,
268 const SkPath& path) {
269 int before = context->priv().globalCache()->numGraphicsPipelines();
270
271 {
272 // TODO: vary the colorType of the target surface too
273 SkImageInfo ii = SkImageInfo::Make(16, 16,
274 kRGBA_8888_SkColorType,
275 kPremul_SkAlphaType);
276
277 sk_sp<SkSurface> surf = SkSurfaces::RenderTarget(recorder, ii);
278 SkCanvas* canvas = surf->getCanvas();
279
280 switch (dt) {
281 case DrawTypeFlags::kSimpleShape:
282 canvas->drawRect(SkRect::MakeWH(16, 16), paint);
283 break;
284 case DrawTypeFlags::kNonSimpleShape:
285 canvas->drawPath(path, paint);
286 break;
287 default:
288 SkASSERT_RELEASE(false);
289 break;
290 }
291
292 std::unique_ptr<skgpu::graphite::Recording> recording = recorder->snap();
293 context->insertRecording({ recording.get() });
294 context->submit(SyncToCpu::kYes);
295 }
296
297 int after = context->priv().globalCache()->numGraphicsPipelines();
298
299 // Actually using the SkPaint with the specified type of draw shouldn't have caused
300 // any additional compilation
301 SkASSERT_RELEASE(before == after);
302 }
303
304 sk_sp<DrawContext> get_precompile_draw_context(
305 const skgpu::graphite::Caps* caps, Context* context) {
306 std::unique_ptr<Recorder> drawRecorder = context->makeRecorder();
307 ResourceProvider* resourceProvider = drawRecorder->priv().resourceProvider();
308 constexpr SkISize drawSize = {128, 128};
309 const SkColorInfo colorInfo = SkColorInfo(kRGBA_8888_SkColorType,
310 kPremul_SkAlphaType,
311 SkColorSpace::MakeSRGB());
312 TextureInfo texInfo = caps->getDefaultSampledTextureInfo(colorInfo.colorType(),
313 skgpu::Mipmapped::kNo,
314 skgpu::Protected::kNo,
315 skgpu::Renderable::kYes);
316 sk_sp<TextureProxy> target = TextureProxy::Make(caps,
317 resourceProvider,
318 drawSize,
319 texInfo,
320 "PrecompileTarget",
321 skgpu::Budgeted::kYes);
322 sk_sp<DrawContext> precompileDrawContext = DrawContext::Make(caps,
323 std::move(target),
324 drawSize,
325 colorInfo,
326 {});
327 return precompileDrawContext;
328 }
329
330 void fuzz_graphite(Fuzz* fuzz, Context* context, int depth = 9) {
331 const skgpu::graphite::Caps* caps = context->priv().caps();
332 std::unique_ptr<PrecompileContext> precompileContext = context->makePrecompileContext();
333 std::unique_ptr<Recorder> recorder = context->makeRecorder();
334 ShaderCodeDictionary* dict = context->priv().shaderCodeDictionary();
335
336 SkColorInfo ci = SkColorInfo(kRGBA_8888_SkColorType, kPremul_SkAlphaType,
337 SkColorSpace::MakeSRGB());
338 Layout layout = context->backend() == skgpu::BackendApi::kMetal ? Layout::kMetal
339 : Layout::kStd140;
340
341 // Currently, we just use this as a valid parameter for keyContext (will hit asserts otherwise)
342 sk_sp<DrawContext> drawContext = get_precompile_draw_context(caps, context);
343
344 FloatStorageManager floatStorageManager;
345 PaintParamsKeyBuilder builder(dict);
346 PipelineDataGatherer gatherer(layout);
347 sk_sp<RuntimeEffectDictionary> rtDict = sk_make_sp<RuntimeEffectDictionary>();
348 KeyContext precompileKeyContext(caps, &floatStorageManager, &builder, &gatherer, dict, rtDict,
349 ci);
350
351 DrawTypeFlags kDrawType = DrawTypeFlags::kSimpleShape;
352 SkPath path = make_path();
353
354 auto [paint, paintOptions] = create_random_paint(fuzz, depth);
355
356 constexpr Coverage coverageOptions[3] = {
357 Coverage::kNone, Coverage::kSingleChannel, Coverage::kLCD};
358 uint32_t temp;
359 fuzz->next(&temp);
360 Coverage coverage = coverageOptions[temp % 3];
361
362 PaintParams paintParams(paint);
363 ShadingParams shadingParams(recorder->priv().caps(),
364 paintParams,
365 /* nonMSAAClip= */ {},
366 /* clipShader= */ nullptr,
367 coverage,
368 TextureFormat::kRGBA8);
369 SkDEBUGCODE(builder.checkReset());
370 SkDEBUGCODE(gatherer.checkReset());
371 KeyContext keyContext(recorder.get(),
372 drawContext.get(),
373 &floatStorageManager,
374 &builder,
375 &gatherer,
376 {},
377 ci,
378 KeyGenFlags::kDisableSamplingOptimization,
379 paintParams.color());
380 shadingParams.toKey(keyContext);
381 UniquePaintParamsID paintID = recorder->priv().shaderCodeDictionary()->findOrCreate(&builder);
382
383 RenderPassDesc unusedRenderPassDesc;
384 std::vector<UniquePaintParamsID> precompileIDs;
385 paintOptions.priv().buildCombinations(precompileKeyContext,
386 DrawTypeFlags::kNone,
387 /* withPrimitiveBlender= */ false,
388 coverage,
389 unusedRenderPassDesc,
390 [&](UniquePaintParamsID id,
391 DrawTypeFlags,
392 bool /* withPrimitiveBlender */,
393 Coverage,
394 const RenderPassDesc&) {
395 precompileIDs.push_back(id);
396 });
397
398 // The specific key generated by paintParams.toKey() should be one of the
399 // combinations generated by the combination system.
400 auto result = std::find(precompileIDs.begin(), precompileIDs.end(), paintID);
401
402 #ifdef SK_DEBUG
403 if (result == precompileIDs.end()) {
404 SkDebugf("From paint: ");
405 dict->dump(caps, paintID);
406
407 SkDebugf("From combination builder:");
408 for (auto iter : precompileIDs) {
409 dict->dump(caps, iter);
410 }
411 }
412 #endif
413
414 SkASSERT_RELEASE(result != precompileIDs.end());
415
416 {
417 static const RenderPassProperties kDefaultRenderPassProperties;
418
419 context->priv().globalCache()->resetGraphicsPipelines();
420
421 int before = context->priv().globalCache()->numGraphicsPipelines();
422 Precompile(precompileContext.get(), paintOptions, kDrawType,
423 {{ kDefaultRenderPassProperties }});
424 int after = context->priv().globalCache()->numGraphicsPipelines();
425
426 SkASSERT_RELEASE(before == 0);
427 SkASSERT_RELEASE(after > before);
428
429 check_draw(context, recorder.get(), paint, kDrawType, path);
430 }
431 }
432
433 } // anonymous namespace
434
435 DEF_FUZZ(Precompile, fuzz) {
436 skiatest::graphite::ContextFactory factory;
437
438 skgpu::ContextType contextType;
439 #if defined(SK_METAL)
440 contextType = skgpu::ContextType::kMetal;
441 #elif defined(SK_VULKAN)
442 contextType = skgpu::ContextType::kVulkan;
443 #else
444 contextType = skgpu::ContextType::kMock;
445 #endif
446
447 skiatest::graphite::ContextInfo ctxInfo = factory.getContextInfo(contextType);
448 skgpu::graphite::Context* context = ctxInfo.fContext;
449 if (!context) {
450 return;
451 }
452
453 fuzz_graphite(fuzz, context);
454 }