Coverage Report

Created: 2024-09-14 07:19

/src/skia/src/gpu/ganesh/GrSurface.h
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2012 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
#ifndef GrSurface_DEFINED
8
#define GrSurface_DEFINED
9
10
#include "include/core/SkRect.h"
11
#include "include/core/SkRefCnt.h"
12
#include "include/core/SkSize.h"
13
#include "include/gpu/GpuTypes.h"
14
#include "include/private/base/SkAssert.h"
15
#include "include/private/gpu/ganesh/GrTypesPriv.h"
16
#include "src/gpu/RefCntedCallback.h"
17
#include "src/gpu/ganesh/GrGpuResource.h"
18
#include "src/gpu/ganesh/GrGpuResourceCacheAccess.h"
19
#include "src/gpu/ganesh/GrGpuResourcePriv.h"
20
21
#include <cstddef>
22
#include <string_view>
23
24
class GrBackendFormat;
25
class GrDirectContext;
26
class GrGpu;
27
class GrRenderTarget;
28
class GrTexture;
29
30
class GrSurface : public GrGpuResource {
31
public:
32
    /**
33
     * Retrieves the dimensions of the surface.
34
     */
35
493k
    SkISize dimensions() const { return fDimensions; }
36
37
    /**
38
     * Retrieves the width of the surface.
39
     */
40
0
    int width() const { return fDimensions.width(); }
41
42
    /**
43
     * Retrieves the height of the surface.
44
     */
45
148k
    int height() const { return fDimensions.height(); }
46
47
    /**
48
     * Helper that gets the width and height of the surface as a bounding rectangle.
49
     */
50
0
    SkRect getBoundsRect() const { return SkRect::Make(this->dimensions()); }
51
52
    virtual GrBackendFormat backendFormat() const = 0;
53
54
    void setRelease(sk_sp<skgpu::RefCntedCallback> releaseHelper);
55
56
    // These match the definitions in SkImage, from whence they came.
57
    // TODO: Remove Chrome's need to call this on a GrTexture
58
    typedef void* ReleaseCtx;
59
    typedef void (*ReleaseProc)(ReleaseCtx);
60
0
    void setRelease(ReleaseProc proc, ReleaseCtx ctx) {
61
0
        this->setRelease(skgpu::RefCntedCallback::Make(proc, ctx));
62
0
    }
63
64
    /**
65
     * @return the texture associated with the surface, may be null.
66
     */
67
0
    virtual GrTexture* asTexture() { return nullptr; }
68
0
    virtual const GrTexture* asTexture() const { return nullptr; }
69
70
    /**
71
     * @return the render target underlying this surface, may be null.
72
     */
73
20.5k
    virtual GrRenderTarget* asRenderTarget() { return nullptr; }
74
67.5k
    virtual const GrRenderTarget* asRenderTarget() const { return nullptr; }
75
76
20.5k
    GrInternalSurfaceFlags flags() const { return fSurfaceFlags; }
77
78
    static size_t ComputeSize(const GrBackendFormat&,
79
                              SkISize dimensions,
80
                              int colorSamplesPerPixel,
81
                              skgpu::Mipmapped,
82
                              bool binSize = false);
83
84
    /**
85
     * The pixel values of this surface cannot be modified (e.g. doesn't support write pixels or
86
     * MIP map level regen).
87
     */
88
182k
    bool readOnly() const { return fSurfaceFlags & GrInternalSurfaceFlags::kReadOnly; }
89
90
0
    bool framebufferOnly() const {
91
0
        return fSurfaceFlags & GrInternalSurfaceFlags::kFramebufferOnly;
92
0
    }
93
94
    // Returns true if we are working with protected content.
95
116k
    bool isProtected() const { return fIsProtected == skgpu::Protected::kYes; }
96
97
0
    void setFramebufferOnly() {
98
0
        SkASSERT(this->asRenderTarget());
99
0
        fSurfaceFlags |= GrInternalSurfaceFlags::kFramebufferOnly;
100
0
    }
Unexecuted instantiation: GrSurface::setFramebufferOnly()
Unexecuted instantiation: GrSurface::setFramebufferOnly()
101
102
    class RefCntedReleaseProc : public SkNVRefCnt<RefCntedReleaseProc> {
103
    public:
104
        RefCntedReleaseProc(sk_sp<skgpu::RefCntedCallback> callback,
105
                            sk_sp<GrDirectContext> directContext);
106
107
        ~RefCntedReleaseProc();
108
109
    private:
110
        sk_sp<skgpu::RefCntedCallback> fCallback;
111
        sk_sp<GrDirectContext> fDirectContext;
112
    };
113
114
#if defined(GPU_TEST_UTILS)
115
0
    const GrSurface* asSurface() const override { return this; }
116
#endif
117
118
protected:
119
0
    void setGLRTFBOIDIs0() {
120
0
        SkASSERT(!this->requiresManualMSAAResolve());
121
0
        SkASSERT(!this->asTexture());
122
0
        SkASSERT(this->asRenderTarget());
123
0
        fSurfaceFlags |= GrInternalSurfaceFlags::kGLRTFBOIDIs0;
124
0
    }
125
0
    bool glRTFBOIDis0() const {
126
0
        return fSurfaceFlags & GrInternalSurfaceFlags::kGLRTFBOIDIs0;
127
0
    }
128
129
0
    void setRequiresManualMSAAResolve() {
130
0
        SkASSERT(!this->glRTFBOIDis0());
131
0
        SkASSERT(this->asRenderTarget());
132
0
        fSurfaceFlags |= GrInternalSurfaceFlags::kRequiresManualMSAAResolve;
133
0
    }
Unexecuted instantiation: GrSurface::setRequiresManualMSAAResolve()
Unexecuted instantiation: GrSurface::setRequiresManualMSAAResolve()
134
0
    bool requiresManualMSAAResolve() const {
135
0
        return fSurfaceFlags & GrInternalSurfaceFlags::kRequiresManualMSAAResolve;
136
0
    }
137
138
0
    void setReadOnly() {
139
0
        SkASSERT(!this->asRenderTarget());
140
0
        fSurfaceFlags |= GrInternalSurfaceFlags::kReadOnly;
141
0
    }
Unexecuted instantiation: GrSurface::setReadOnly()
Unexecuted instantiation: GrSurface::setReadOnly()
142
143
0
    void setVkRTSupportsInputAttachment() {
144
0
        SkASSERT(this->asRenderTarget());
145
0
        fSurfaceFlags |= GrInternalSurfaceFlags::kVkRTSupportsInputAttachment;
146
0
    }
Unexecuted instantiation: GrSurface::setVkRTSupportsInputAttachment()
Unexecuted instantiation: GrSurface::setVkRTSupportsInputAttachment()
147
148
    GrSurface(GrGpu* gpu,
149
              const SkISize& dimensions,
150
              skgpu::Protected isProtected,
151
              std::string_view label)
152
            : INHERITED(gpu, label)
153
            , fDimensions(dimensions)
154
            , fSurfaceFlags(GrInternalSurfaceFlags::kNone)
155
95.6k
            , fIsProtected(isProtected) {}
156
157
95.6k
    ~GrSurface() override {
158
        // check that invokeReleaseProc has been called (if needed)
159
95.6k
        SkASSERT(!fReleaseHelper);
160
95.6k
    }
161
162
    void onRelease() override;
163
    void onAbandon() override;
164
165
private:
166
0
    const char* getResourceType() const override { return "Surface"; }
167
168
    // Unmanaged backends (e.g. Vulkan) may want to specially handle the release proc in order to
169
    // ensure it isn't called until GPU work related to the resource is completed.
170
171
    // NOLINTNEXTLINE(performance-unnecessary-value-param)
172
0
    virtual void onSetRelease(sk_sp<RefCntedReleaseProc>) {}
173
174
123k
    void invokeReleaseProc() {
175
        // Depending on the ref count of fReleaseHelper this may or may not actually trigger the
176
        // ReleaseProc to be called.
177
123k
        fReleaseHelper.reset();
178
123k
    }
179
180
    SkISize                    fDimensions;
181
    GrInternalSurfaceFlags     fSurfaceFlags;
182
    skgpu::Protected           fIsProtected;
183
    sk_sp<RefCntedReleaseProc> fReleaseHelper;
184
185
    using INHERITED = GrGpuResource;
186
};
187
188
#endif