Coverage Report

Created: 2024-05-20 07:14

/src/skia/fuzz/FuzzCreateDDL.cpp
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2020 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/SkCanvas.h"
9
#include "include/core/SkPaint.h"
10
#include "include/core/SkSurface.h"
11
#include "include/gpu/GrDirectContext.h"
12
#include "include/gpu/ganesh/SkSurfaceGanesh.h"
13
#include "include/private/chromium/GrDeferredDisplayList.h"
14
#include "include/private/chromium/GrDeferredDisplayListRecorder.h"
15
#include "include/private/chromium/GrSurfaceCharacterization.h"
16
#include "include/private/gpu/ganesh/GrTypesPriv.h"
17
#include "src/gpu/ganesh/GrShaderCaps.h"
18
#include "tools/gpu/GrContextFactory.h"
19
20
#include "fuzz/Fuzz.h"
21
22
#include <tuple>
23
24
/**
25
 * The fuzzer aims to fuzz the use of GrDeferredDisplayList. It mainly consists of
26
 * three parts.
27
 * 1. In create_surface_characterization, (make_characterization) Create GrSurfaceCharacterization
28
 * by using GrDirectContext of ContextType::kGL as it can be applied on all platform, and
29
 * (make_surface) create a GPU backend surface of the same GrDirectContext
30
 * 2. (make_ddl) Create GrDeferredDisplayListRecorder from the GrSurfaceCharacterization, and test
31
 * the recoder's corresponding canvas.
32
 * 3. (make_ddl, draw_ddl) Create GrDeferredDisplayList from the SkDeferredDisplayRecorder and draw
33
 * the ddl on a GPU backend surface.
34
 */
35
36
static constexpr int kMaxWidth = 64;
37
static constexpr int kMaxHeight = 64;
38
static constexpr int kSampleCount = 1;
39
40
0
static SkSurfaceProps gen_fuzzed_surface_props(Fuzz* fuzz) {
41
0
    SkPixelGeometry pixel;
42
0
    fuzz->nextEnum(&pixel, kBGR_V_SkPixelGeometry);
43
0
    return SkSurfaceProps(0x0, pixel);
44
0
}
45
46
0
static SkPaint gen_fuzzed_skpaint(Fuzz* fuzz) {
47
0
    float R, G, B, Alpha;
48
0
    fuzz->nextRange(&R, -1, 2);
49
0
    fuzz->nextRange(&G, -1, 2);
50
0
    fuzz->nextRange(&B, -1, 2);
51
0
    fuzz->nextRange(&Alpha, 0, 1);
52
0
    SkColor4f color = {R, G, B, Alpha};
53
0
    return SkPaint(color);
54
0
}
55
56
0
static SkImageInfo gen_fuzzed_imageinfo(Fuzz* fuzz, SkColorType surfaceType) {
57
0
    int width, height;
58
0
    fuzz->nextRange(&width, 1, kMaxWidth);
59
0
    fuzz->nextRange(&height, 1, kMaxHeight);
60
0
    SkAlphaType alphaType;
61
0
    fuzz->nextEnum(&alphaType, SkAlphaType::kLastEnum_SkAlphaType);
62
0
    skcms_TransferFunction skcmsFn;
63
0
    uint8_t skcms;
64
0
    fuzz->nextRange(&skcms, 0, 5);
65
0
    switch (skcms) {
66
0
        case 0: {
67
0
            skcmsFn = SkNamedTransferFn::kSRGB;
68
0
            break;
69
0
        }
70
0
        case 1: {
71
0
            skcmsFn = SkNamedTransferFn::k2Dot2;
72
0
            break;
73
0
        }
74
0
        case 2: {
75
0
            skcmsFn = SkNamedTransferFn::kHLG;
76
0
            break;
77
0
        }
78
0
        case 3: {
79
0
            skcmsFn = SkNamedTransferFn::kLinear;
80
0
            break;
81
0
        }
82
0
        case 4: {
83
0
            skcmsFn = SkNamedTransferFn::kPQ;
84
0
            break;
85
0
        }
86
0
        case 5: {
87
0
            skcmsFn = SkNamedTransferFn::kRec2020;
88
0
            break;
89
0
        }
90
0
        default:
91
0
            SkASSERT(false);
92
0
            break;
93
0
    }
94
0
    skcms_Matrix3x3 skcmsMat;
95
0
    fuzz->nextRange(&skcms, 0, 4);
96
0
    switch (skcms) {
97
0
        case 0: {
98
0
            skcmsMat = SkNamedGamut::kAdobeRGB;
99
0
            break;
100
0
        }
101
0
        case 1: {
102
0
            skcmsMat = SkNamedGamut::kDisplayP3;
103
0
            break;
104
0
        }
105
0
        case 2: {
106
0
            skcmsMat = SkNamedGamut::kRec2020;
107
0
            break;
108
0
        }
109
0
        case 3: {
110
0
            skcmsMat = SkNamedGamut::kSRGB;
111
0
            break;
112
0
        }
113
0
        case 4: {
114
0
            skcmsMat = SkNamedGamut::kXYZ;
115
0
            break;
116
0
        }
117
0
        default:
118
0
            SkASSERT(false);
119
0
            break;
120
0
    }
121
0
    return SkImageInfo::Make(width, height, surfaceType, alphaType,
122
0
                             SkColorSpace::MakeRGB(skcmsFn, skcmsMat));
123
0
}
124
125
static GrSurfaceCharacterization make_characterization(Fuzz* fuzz, GrDirectContext* dContext,
126
                                                       SkImageInfo& ii, SkColorType surfaceType,
127
0
                                                       GrSurfaceOrigin origin) {
128
0
    if (!dContext->colorTypeSupportedAsSurface(surfaceType)) {
129
0
        SkDebugf("Couldn't create backend texture in the backend %s",
130
0
                 GrBackendApiToStr(dContext->backend()));
131
0
        return {};
132
0
    }
133
134
0
    GrBackendFormat backendFormat = dContext->defaultBackendFormat(surfaceType,
135
0
                                                                   GrRenderable::kYes);
136
0
    if (!backendFormat.isValid()) {
137
0
        SkDebugf("Color Type is not supported in the backend %s",
138
0
                 GrBackendApiToStr(dContext->backend()));
139
0
        return {};
140
0
    }
141
0
    GrProtected protect = GrProtected::kNo;
142
0
#ifdef SK_VULKAN
143
0
    fuzz->nextEnum(&protect, GrProtected::kYes);
144
0
#endif
145
0
    GrSurfaceCharacterization c;
146
0
    size_t maxResourceBytes = dContext->getResourceCacheLimit();
147
0
    c = dContext->threadSafeProxy()->createCharacterization(maxResourceBytes,
148
0
                                                            ii,
149
0
                                                            backendFormat,
150
0
                                                            kSampleCount,
151
0
                                                            origin,
152
0
                                                            gen_fuzzed_surface_props(fuzz),
153
0
                                                            skgpu::Mipmapped::kYes,
154
0
                                                            false,
155
0
                                                            true,
156
0
                                                            protect);
157
0
    if (!c.isValid()) {
158
0
        SkDebugf("Could not create Characterization in the backend %s",
159
0
                 GrBackendApiToStr(dContext->backend()));
160
0
        return {};
161
0
    }
162
0
    return c;
163
0
}
164
165
static sk_sp<GrDeferredDisplayList> make_ddl(Fuzz* fuzz, GrDirectContext* dContext,
166
0
                                             const GrSurfaceCharacterization& c) {
167
0
    GrDeferredDisplayListRecorder r(c);
168
0
    SkCanvas* canvas = r.getCanvas();
169
0
    if (!canvas) {
170
0
        SkDebugf("Could not create canvas for backend %s", GrBackendApiToStr(dContext->backend()));
171
0
        return nullptr;
172
0
    }
173
    // For now we only draw a rect into the DDL. This will be scaled up to draw more varied content.
174
0
    SkRect tile;
175
0
    fuzz->next(&tile);
176
0
    canvas->drawRect(tile, gen_fuzzed_skpaint(fuzz));
177
0
    return r.detach();
178
0
}
179
180
static sk_sp<SkSurface> make_surface(Fuzz* fuzz, GrDirectContext* dContext, const SkImageInfo& ii,
181
0
                                     GrSurfaceOrigin origin) {
182
0
    skgpu::Budgeted budgeted;
183
0
    fuzz->nextEnum(&budgeted, skgpu::Budgeted::kYes);
184
0
    SkSurfaceProps surfaceProps = gen_fuzzed_surface_props(fuzz);
185
0
    auto surface =
186
0
            SkSurfaces::RenderTarget(dContext, budgeted, ii, kSampleCount, origin, &surfaceProps);
187
0
    return surface;
188
0
}
189
190
0
static bool draw_ddl(sk_sp<SkSurface> surface, sk_sp<const GrDeferredDisplayList> ddl) {
191
0
    return skgpu::ganesh::DrawDDL(std::move(surface), std::move(ddl));
192
0
}
193
194
using SurfaceAndChar = std::tuple<sk_sp<SkSurface>, GrSurfaceCharacterization>;
195
static SurfaceAndChar create_surface_and_characterization(Fuzz* fuzz, GrDirectContext* dContext,
196
                                                          SkColorType surfaceType,
197
0
                                                          GrSurfaceOrigin origin) {
198
0
    SkImageInfo ii = gen_fuzzed_imageinfo(fuzz, surfaceType);
199
0
    GrSurfaceCharacterization c = make_characterization(fuzz, dContext, ii, surfaceType, origin);
200
0
    if (!c.isValid()) {
201
0
       return {};
202
0
    }
203
204
0
    auto surface = make_surface(fuzz, dContext, ii, origin);
205
0
    if (!surface) {
206
0
        return {};
207
0
    }
208
0
    return {surface, c};
209
0
}
210
211
156
DEF_FUZZ(CreateDDL, fuzz) {
212
156
    SkColorType surfaceType;
213
156
    GrSurfaceOrigin origin;
214
156
    fuzz->nextEnum(&surfaceType, SkColorType::kLastEnum_SkColorType);
215
156
    fuzz->nextEnum(&origin, GrSurfaceOrigin::kTopLeft_GrSurfaceOrigin);
216
217
156
    sk_gpu_test::GrContextFactory factory;
218
156
    auto ctxInfo = factory.getContextInfo(skgpu::ContextType::kGL);
219
220
156
    GrDirectContext* dContext = ctxInfo.directContext();
221
156
    if (!dContext) {
222
156
        SkDebugf("Context creation failed");
223
156
        return;
224
156
    }
225
226
0
    auto[surface, c] = create_surface_and_characterization(fuzz, dContext, surfaceType, origin);
227
0
    if (!surface || !c.isValid()) {
228
0
        return;
229
0
    }
230
231
0
    sk_sp<GrDeferredDisplayList> ddl = make_ddl(fuzz, dContext, c);
232
0
    if (!ddl) {
233
0
        SkDebugf("Could not create ddl %s", GrBackendApiToStr(dContext->backend()));
234
0
        return;
235
0
    }
236
0
    if (!draw_ddl(std::move(surface), std::move(ddl))) {
237
0
        SkDebugf("Could not draw ddl in the backend");
238
0
    }
239
0
    return;
240
0
}