File: SkSurfaceGanesh.h

package info (click to toggle)
webkit2gtk 2.48.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 429,764 kB
  • sloc: cpp: 3,697,587; javascript: 194,444; ansic: 169,997; python: 46,499; asm: 19,295; ruby: 18,528; perl: 16,602; xml: 4,650; yacc: 2,360; sh: 2,098; java: 1,993; lex: 1,327; pascal: 366; makefile: 298
file content (218 lines) | stat: -rw-r--r-- 11,155 bytes parent folder | download | duplicates (18)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/*
 * Copyright 2023 Google LLC
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#ifndef SkSurfaceGanesh_DEFINED
#define SkSurfaceGanesh_DEFINED

#include "include/core/SkImageInfo.h"
#include "include/core/SkRefCnt.h"
#include "include/core/SkSurface.h"
#include "include/gpu/ganesh/GrTypes.h"
#include "include/private/base/SkAPI.h"

class GrBackendRenderTarget;
class GrBackendTexture;
class GrRecordingContext;
class SkColorSpace;
class GrSurfaceCharacterization;
class SkSurfaceProps;
enum SkColorType : int;
namespace skgpu {
enum class Budgeted : bool;
}

namespace SkSurfaces {

/** Caller data passed to RenderTarget/TextureReleaseProc; may be nullptr. */
using ReleaseContext = void*;

/** User function called when supplied render target may be deleted. */
using RenderTargetReleaseProc = void (*)(ReleaseContext);

/** User function called when supplied texture may be deleted. */
using TextureReleaseProc = void (*)(ReleaseContext);

/** Returns SkSurface on GPU indicated by context. Allocates memory for
    pixels, based on the width, height, and SkColorType in SkImageInfo.  budgeted
    selects whether allocation for pixels is tracked by context. imageInfo
    describes the pixel format in SkColorType, and transparency in
    SkAlphaType, and color matching in SkColorSpace.

    sampleCount requests the number of samples per pixel.
    Pass zero to disable multi-sample anti-aliasing.  The request is rounded
    up to the next supported count, or rounded down if it is larger than the
    maximum supported count.

    surfaceOrigin pins either the top-left or the bottom-left corner to the origin.

    shouldCreateWithMips hints that SkImage returned by makeImageSnapshot() is mip map.

    @param context               GPU context
    @param imageInfo             width, height, SkColorType, SkAlphaType, SkColorSpace;
                                 width, or height, or both, may be zero
    @param sampleCount           samples per pixel, or 0 to disable full scene anti-aliasing
    @param surfaceOrigin         How to align the pixel data.
    @param surfaceProps          LCD striping orientation and setting for device independent
                                 fonts; may be nullptr
    @param shouldCreateWithMips  hint that SkSurface will host mip map images
    @return                      SkSurface if all parameters are valid; otherwise, nullptr
*/
SK_API sk_sp<SkSurface> RenderTarget(GrRecordingContext* context,
                                     skgpu::Budgeted budgeted,
                                     const SkImageInfo& imageInfo,
                                     int sampleCount,
                                     GrSurfaceOrigin surfaceOrigin,
                                     const SkSurfaceProps* surfaceProps,
                                     bool shouldCreateWithMips = false,
                                     bool isProtected = false);
inline sk_sp<SkSurface> RenderTarget(GrRecordingContext* context,
                                     skgpu::Budgeted budgeted,
                                     const SkImageInfo& imageInfo,
                                     int sampleCount,
                                     const SkSurfaceProps* surfaceProps) {
    return RenderTarget(
            context, budgeted, imageInfo, sampleCount, kBottomLeft_GrSurfaceOrigin, surfaceProps);
}
inline sk_sp<SkSurface> RenderTarget(GrRecordingContext* context,
                                     skgpu::Budgeted budgeted,
                                     const SkImageInfo& imageInfo) {
    if (!imageInfo.width() || !imageInfo.height()) {
        return nullptr;
    }
    return RenderTarget(context, budgeted, imageInfo, 0, kBottomLeft_GrSurfaceOrigin, nullptr);
}

/** Returns SkSurface on GPU indicated by context that is compatible with the provided
    characterization. budgeted selects whether allocation for pixels is tracked by context.

    @param context           GPU context
    @param characterization  description of the desired SkSurface
    @return                  SkSurface if all parameters are valid; otherwise, nullptr
*/
SK_API sk_sp<SkSurface> RenderTarget(GrRecordingContext* context,
                                     const GrSurfaceCharacterization& characterization,
                                     skgpu::Budgeted budgeted);

/** Wraps a GPU-backed texture into SkSurface. Caller must ensure the texture is
    valid for the lifetime of returned SkSurface. If sampleCnt greater than zero,
    creates an intermediate MSAA SkSurface which is used for drawing backendTexture.

    SkSurface is returned if all parameters are valid. backendTexture is valid if
    its pixel configuration agrees with colorSpace and context; for instance, if
    backendTexture has an sRGB configuration, then context must support sRGB,
    and colorSpace must be present. Further, backendTexture width and height must
    not exceed context capabilities, and the context must be able to support
    back-end textures.

    Upon success textureReleaseProc is called when it is safe to delete the texture in the
    backend API (accounting only for use of the texture by this surface). If SkSurface creation
    fails textureReleaseProc is called before this function returns.

    @param context             GPU context
    @param backendTexture      texture residing on GPU
    @param sampleCnt           samples per pixel, or 0 to disable full scene anti-aliasing
    @param colorSpace          range of colors; may be nullptr
    @param surfaceProps        LCD striping orientation and setting for device independent
                               fonts; may be nullptr
    @param textureReleaseProc  function called when texture can be released
    @param releaseContext      state passed to textureReleaseProc
    @return                    SkSurface if all parameters are valid; otherwise, nullptr
*/
SK_API sk_sp<SkSurface> WrapBackendTexture(GrRecordingContext* context,
                                           const GrBackendTexture& backendTexture,
                                           GrSurfaceOrigin origin,
                                           int sampleCnt,
                                           SkColorType colorType,
                                           sk_sp<SkColorSpace> colorSpace,
                                           const SkSurfaceProps* surfaceProps,
                                           TextureReleaseProc textureReleaseProc = nullptr,
                                           ReleaseContext releaseContext = nullptr);

/** Wraps a GPU-backed buffer into SkSurface. Caller must ensure backendRenderTarget
    is valid for the lifetime of returned SkSurface.

    SkSurface is returned if all parameters are valid. backendRenderTarget is valid if
    its pixel configuration agrees with colorSpace and context; for instance, if
    backendRenderTarget has an sRGB configuration, then context must support sRGB,
    and colorSpace must be present. Further, backendRenderTarget width and height must
    not exceed context capabilities, and the context must be able to support
    back-end render targets.

    Upon success releaseProc is called when it is safe to delete the render target in the
    backend API (accounting only for use of the render target by this surface). If SkSurface
    creation fails releaseProc is called before this function returns.

    @param context                  GPU context
    @param backendRenderTarget      GPU intermediate memory buffer
    @param colorSpace               range of colors
    @param surfaceProps             LCD striping orientation and setting for device independent
                                    fonts; may be nullptr
    @param releaseProc              function called when backendRenderTarget can be released
    @param releaseContext           state passed to releaseProc
    @return                         SkSurface if all parameters are valid; otherwise, nullptr
*/
SK_API sk_sp<SkSurface> WrapBackendRenderTarget(GrRecordingContext* context,
                                                const GrBackendRenderTarget& backendRenderTarget,
                                                GrSurfaceOrigin origin,
                                                SkColorType colorType,
                                                sk_sp<SkColorSpace> colorSpace,
                                                const SkSurfaceProps* surfaceProps,
                                                RenderTargetReleaseProc releaseProc = nullptr,
                                                ReleaseContext releaseContext = nullptr);

using BackendHandleAccess = SkSurface::BackendHandleAccess;

/** Retrieves the back-end texture. If SkSurface has no back-end texture, an invalid
    object is returned. Call GrBackendTexture::isValid to determine if the result
    is valid.

    The returned GrBackendTexture should be discarded if the SkSurface is drawn to or deleted.

    @return                     GPU texture reference; invalid on failure
*/
SK_API GrBackendTexture GetBackendTexture(SkSurface*, BackendHandleAccess);

/** Retrieves the back-end render target. If SkSurface has no back-end render target, an invalid
    object is returned. Call GrBackendRenderTarget::isValid to determine if the result
    is valid.

    The returned GrBackendRenderTarget should be discarded if the SkSurface is drawn to
    or deleted.

    @return                     GPU render target reference; invalid on failure
*/
SK_API GrBackendRenderTarget GetBackendRenderTarget(SkSurface*, BackendHandleAccess);

/** If a surface is a Ganesh-backed surface, is being drawn with MSAA, and there is a resolve
    texture, this call will insert a resolve command into the stream of gpu commands. In order
    for the resolve to actually have an effect, the work still needs to be flushed and submitted
    to the GPU after recording the resolve command. If a resolve is not supported or the
    SkSurface has no dirty work to resolve, then this call is a no-op.

    This call is most useful when the SkSurface is created by wrapping a single sampled gpu
    texture, but asking Skia to render with MSAA. If the client wants to use the wrapped texture
    outside of Skia, the only way to trigger a resolve is either to call this command or use
    GrDirectContext::flush.
 */
SK_API void ResolveMSAA(SkSurface* surface);
inline void ResolveMSAA(const sk_sp<SkSurface>& surface) {
    return ResolveMSAA(surface.get());
}

}  // namespace SkSurfaces

namespace skgpu::ganesh {
// Clients should strive to call GrDirectContext::flush directly. However, there exist some
// places where the GrDirectContext is hard to find, these helpers allow for the flushing of the
// provided surface. This is a no-op if the surface is nullptr or not GPU backed.
SK_API GrSemaphoresSubmitted Flush(sk_sp<SkSurface>);
SK_API GrSemaphoresSubmitted Flush(SkSurface*);
SK_API void FlushAndSubmit(sk_sp<SkSurface>);
SK_API void FlushAndSubmit(SkSurface*);
}  // namespace skgpu::ganesh

#endif