xref: /aosp_15_r20/external/skia/include/gpu/ganesh/SkImageGanesh.h (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1*c8dee2aaSAndroid Build Coastguard Worker /*
2*c8dee2aaSAndroid Build Coastguard Worker  * Copyright 2023 Google LLC
3*c8dee2aaSAndroid Build Coastguard Worker  *
4*c8dee2aaSAndroid Build Coastguard Worker  * Use of this source code is governed by a BSD-style license that can be
5*c8dee2aaSAndroid Build Coastguard Worker  * found in the LICENSE file.
6*c8dee2aaSAndroid Build Coastguard Worker  */
7*c8dee2aaSAndroid Build Coastguard Worker 
8*c8dee2aaSAndroid Build Coastguard Worker #ifndef SkImageGanesh_DEFINED
9*c8dee2aaSAndroid Build Coastguard Worker #define SkImageGanesh_DEFINED
10*c8dee2aaSAndroid Build Coastguard Worker 
11*c8dee2aaSAndroid Build Coastguard Worker #include "include/core/SkImage.h"
12*c8dee2aaSAndroid Build Coastguard Worker #include "include/core/SkRefCnt.h"
13*c8dee2aaSAndroid Build Coastguard Worker #include "include/gpu/GpuTypes.h"
14*c8dee2aaSAndroid Build Coastguard Worker #include "include/gpu/ganesh/GrTypes.h"
15*c8dee2aaSAndroid Build Coastguard Worker #include "include/private/base/SkAPI.h"
16*c8dee2aaSAndroid Build Coastguard Worker 
17*c8dee2aaSAndroid Build Coastguard Worker #include <functional>
18*c8dee2aaSAndroid Build Coastguard Worker #include <utility>
19*c8dee2aaSAndroid Build Coastguard Worker 
20*c8dee2aaSAndroid Build Coastguard Worker class GrBackendTexture;
21*c8dee2aaSAndroid Build Coastguard Worker class GrDirectContext;
22*c8dee2aaSAndroid Build Coastguard Worker class GrRecordingContext;
23*c8dee2aaSAndroid Build Coastguard Worker class GrYUVABackendTextures;
24*c8dee2aaSAndroid Build Coastguard Worker class SkColorSpace;
25*c8dee2aaSAndroid Build Coastguard Worker class SkData;
26*c8dee2aaSAndroid Build Coastguard Worker class SkImageFilter;
27*c8dee2aaSAndroid Build Coastguard Worker struct SkIPoint;
28*c8dee2aaSAndroid Build Coastguard Worker class SkPixmap;
29*c8dee2aaSAndroid Build Coastguard Worker class SkYUVAPixmaps;
30*c8dee2aaSAndroid Build Coastguard Worker enum SkAlphaType : int;
31*c8dee2aaSAndroid Build Coastguard Worker enum SkColorType : int;
32*c8dee2aaSAndroid Build Coastguard Worker enum class SkTextureCompressionType;
33*c8dee2aaSAndroid Build Coastguard Worker struct SkIRect;
34*c8dee2aaSAndroid Build Coastguard Worker 
35*c8dee2aaSAndroid Build Coastguard Worker /**
36*c8dee2aaSAndroid Build Coastguard Worker  * All factories in this file refer to the Ganesh GPU backend when they say GPU.
37*c8dee2aaSAndroid Build Coastguard Worker  */
38*c8dee2aaSAndroid Build Coastguard Worker 
39*c8dee2aaSAndroid Build Coastguard Worker namespace SkImages {
40*c8dee2aaSAndroid Build Coastguard Worker /** Defines a callback function, taking one parameter of type GrBackendTexture with
41*c8dee2aaSAndroid Build Coastguard Worker     no return value. Function is called when backend texture is to be released.
42*c8dee2aaSAndroid Build Coastguard Worker */
43*c8dee2aaSAndroid Build Coastguard Worker using BackendTextureReleaseProc = std::function<void(GrBackendTexture)>;
44*c8dee2aaSAndroid Build Coastguard Worker /** User function called when supplied texture may be deleted. */
45*c8dee2aaSAndroid Build Coastguard Worker using TextureReleaseProc = void (*)(ReleaseContext);
46*c8dee2aaSAndroid Build Coastguard Worker 
47*c8dee2aaSAndroid Build Coastguard Worker /** Creates GPU-backed SkImage from backendTexture associated with context.
48*c8dee2aaSAndroid Build Coastguard Worker     Skia will assume ownership of the resource and will release it when no longer needed.
49*c8dee2aaSAndroid Build Coastguard Worker     A non-null SkImage is returned if format of backendTexture is recognized and supported.
50*c8dee2aaSAndroid Build Coastguard Worker     Recognized formats vary by GPU backend.
51*c8dee2aaSAndroid Build Coastguard Worker     @param context         GPU context
52*c8dee2aaSAndroid Build Coastguard Worker     @param backendTexture  texture residing on GPU
53*c8dee2aaSAndroid Build Coastguard Worker     @param textureOrigin   origin of backendTexture
54*c8dee2aaSAndroid Build Coastguard Worker     @param colorType       color type of the resulting image
55*c8dee2aaSAndroid Build Coastguard Worker     @param alphaType       alpha type of the resulting image
56*c8dee2aaSAndroid Build Coastguard Worker     @param colorSpace      range of colors; may be nullptr
57*c8dee2aaSAndroid Build Coastguard Worker     @return                created SkImage, or nullptr
58*c8dee2aaSAndroid Build Coastguard Worker */
59*c8dee2aaSAndroid Build Coastguard Worker SK_API sk_sp<SkImage> AdoptTextureFrom(GrRecordingContext* context,
60*c8dee2aaSAndroid Build Coastguard Worker                                        const GrBackendTexture& backendTexture,
61*c8dee2aaSAndroid Build Coastguard Worker                                        GrSurfaceOrigin textureOrigin,
62*c8dee2aaSAndroid Build Coastguard Worker                                        SkColorType colorType);
63*c8dee2aaSAndroid Build Coastguard Worker SK_API sk_sp<SkImage> AdoptTextureFrom(GrRecordingContext* context,
64*c8dee2aaSAndroid Build Coastguard Worker                                        const GrBackendTexture& backendTexture,
65*c8dee2aaSAndroid Build Coastguard Worker                                        GrSurfaceOrigin textureOrigin,
66*c8dee2aaSAndroid Build Coastguard Worker                                        SkColorType colorType,
67*c8dee2aaSAndroid Build Coastguard Worker                                        SkAlphaType alphaType);
68*c8dee2aaSAndroid Build Coastguard Worker SK_API sk_sp<SkImage> AdoptTextureFrom(GrRecordingContext* context,
69*c8dee2aaSAndroid Build Coastguard Worker                                        const GrBackendTexture& backendTexture,
70*c8dee2aaSAndroid Build Coastguard Worker                                        GrSurfaceOrigin textureOrigin,
71*c8dee2aaSAndroid Build Coastguard Worker                                        SkColorType colorType,
72*c8dee2aaSAndroid Build Coastguard Worker                                        SkAlphaType alphaType,
73*c8dee2aaSAndroid Build Coastguard Worker                                        sk_sp<SkColorSpace> colorSpace);
74*c8dee2aaSAndroid Build Coastguard Worker 
75*c8dee2aaSAndroid Build Coastguard Worker /** Creates GPU-backed SkImage from the provided GPU texture associated with context.
76*c8dee2aaSAndroid Build Coastguard Worker     GPU texture must stay valid and unchanged until textureReleaseProc is called by Skia.
77*c8dee2aaSAndroid Build Coastguard Worker     Skia will call textureReleaseProc with the passed-in releaseContext when SkImage
78*c8dee2aaSAndroid Build Coastguard Worker     is deleted or no longer refers to the texture.
79*c8dee2aaSAndroid Build Coastguard Worker     A non-null SkImage is returned if format of backendTexture is recognized and supported.
80*c8dee2aaSAndroid Build Coastguard Worker     Recognized formats vary by GPU backend.
81*c8dee2aaSAndroid Build Coastguard Worker     @note When using a DDL recording context, textureReleaseProc will be called on the
82*c8dee2aaSAndroid Build Coastguard Worker     GPU thread after the DDL is played back on the direct context.
83*c8dee2aaSAndroid Build Coastguard Worker     @param context             GPU context
84*c8dee2aaSAndroid Build Coastguard Worker     @param backendTexture      texture residing on GPU
85*c8dee2aaSAndroid Build Coastguard Worker     @param colorSpace          This describes the color space of this image's contents, as
86*c8dee2aaSAndroid Build Coastguard Worker                                seen after sampling. In general, if the format of the backend
87*c8dee2aaSAndroid Build Coastguard Worker                                texture is SRGB, some linear colorSpace should be supplied
88*c8dee2aaSAndroid Build Coastguard Worker                                (e.g., SkColorSpace::MakeSRGBLinear()). If the format of the
89*c8dee2aaSAndroid Build Coastguard Worker                                backend texture is linear, then the colorSpace should include
90*c8dee2aaSAndroid Build Coastguard Worker                                a description of the transfer function as
91*c8dee2aaSAndroid Build Coastguard Worker                                well (e.g., SkColorSpace::MakeSRGB()).
92*c8dee2aaSAndroid Build Coastguard Worker     @param textureReleaseProc  function called when texture can be released
93*c8dee2aaSAndroid Build Coastguard Worker     @param releaseContext      state passed to textureReleaseProc
94*c8dee2aaSAndroid Build Coastguard Worker     @return                    created SkImage, or nullptr
95*c8dee2aaSAndroid Build Coastguard Worker */
96*c8dee2aaSAndroid Build Coastguard Worker SK_API sk_sp<SkImage> BorrowTextureFrom(GrRecordingContext* context,
97*c8dee2aaSAndroid Build Coastguard Worker                                         const GrBackendTexture& backendTexture,
98*c8dee2aaSAndroid Build Coastguard Worker                                         GrSurfaceOrigin origin,
99*c8dee2aaSAndroid Build Coastguard Worker                                         SkColorType colorType,
100*c8dee2aaSAndroid Build Coastguard Worker                                         SkAlphaType alphaType,
101*c8dee2aaSAndroid Build Coastguard Worker                                         sk_sp<SkColorSpace> colorSpace,
102*c8dee2aaSAndroid Build Coastguard Worker                                         TextureReleaseProc textureReleaseProc = nullptr,
103*c8dee2aaSAndroid Build Coastguard Worker                                         ReleaseContext releaseContext = nullptr);
104*c8dee2aaSAndroid Build Coastguard Worker 
105*c8dee2aaSAndroid Build Coastguard Worker /** Creates a GPU-backed SkImage from pixmap. It is uploaded to GPU backend using context.
106*c8dee2aaSAndroid Build Coastguard Worker     Created SkImage is available to other GPU contexts, and is available across thread
107*c8dee2aaSAndroid Build Coastguard Worker     boundaries. All contexts must be in the same GPU share group, or otherwise
108*c8dee2aaSAndroid Build Coastguard Worker     share resources.
109*c8dee2aaSAndroid Build Coastguard Worker     When SkImage is no longer referenced, context releases texture memory
110*c8dee2aaSAndroid Build Coastguard Worker     asynchronously.
111*c8dee2aaSAndroid Build Coastguard Worker     SkColorSpace of SkImage is determined by pixmap.colorSpace().
112*c8dee2aaSAndroid Build Coastguard Worker     SkImage is returned referring to GPU backend if context is not nullptr,
113*c8dee2aaSAndroid Build Coastguard Worker     format of data is recognized and supported, and if context supports moving
114*c8dee2aaSAndroid Build Coastguard Worker     resources between contexts. Otherwise, pixmap pixel data is copied and SkImage
115*c8dee2aaSAndroid Build Coastguard Worker     as returned in raster format if possible; nullptr may be returned.
116*c8dee2aaSAndroid Build Coastguard Worker     Recognized GPU formats vary by platform and GPU backend.
117*c8dee2aaSAndroid Build Coastguard Worker     @param context                GPU context
118*c8dee2aaSAndroid Build Coastguard Worker     @param pixmap                 SkImageInfo, pixel address, and row bytes
119*c8dee2aaSAndroid Build Coastguard Worker     @param buildMips              create SkImage as mip map if true
120*c8dee2aaSAndroid Build Coastguard Worker     @param limitToMaxTextureSize  downscale image to GPU maximum texture size, if necessary
121*c8dee2aaSAndroid Build Coastguard Worker     @return                       created SkImage, or nullptr
122*c8dee2aaSAndroid Build Coastguard Worker */
123*c8dee2aaSAndroid Build Coastguard Worker SK_API sk_sp<SkImage> CrossContextTextureFromPixmap(GrDirectContext* context,
124*c8dee2aaSAndroid Build Coastguard Worker                                                     const SkPixmap& pixmap,
125*c8dee2aaSAndroid Build Coastguard Worker                                                     bool buildMips,
126*c8dee2aaSAndroid Build Coastguard Worker                                                     bool limitToMaxTextureSize = false);
127*c8dee2aaSAndroid Build Coastguard Worker 
128*c8dee2aaSAndroid Build Coastguard Worker /** Creates a GPU-backed SkImage from a GPU backend texture. The backend texture must stay
129*c8dee2aaSAndroid Build Coastguard Worker     valid and unchanged until textureReleaseProc is called. The textureReleaseProc is
130*c8dee2aaSAndroid Build Coastguard Worker     called when the SkImage is deleted or no longer refers to the texture and will be
131*c8dee2aaSAndroid Build Coastguard Worker     passed the releaseContext.
132*c8dee2aaSAndroid Build Coastguard Worker     An SkImage is returned if the format of backendTexture is recognized and supported.
133*c8dee2aaSAndroid Build Coastguard Worker     Recognized formats vary by GPU backend.
134*c8dee2aaSAndroid Build Coastguard Worker     @note When using a DDL recording context, textureReleaseProc will be called on the
135*c8dee2aaSAndroid Build Coastguard Worker     GPU thread after the DDL is played back on the direct context.
136*c8dee2aaSAndroid Build Coastguard Worker     @param context             the GPU context
137*c8dee2aaSAndroid Build Coastguard Worker     @param backendTexture      a texture already allocated by the GPU
138*c8dee2aaSAndroid Build Coastguard Worker     @param alphaType           This characterizes the nature of the alpha values in the
139*c8dee2aaSAndroid Build Coastguard Worker                                backend texture. For opaque compressed formats (e.g., ETC1)
140*c8dee2aaSAndroid Build Coastguard Worker                                this should usually be set to kOpaq
141*c8dee2aaSAndroid Build Coastguard Worker                                ue_SkAlphaType.
142*c8dee2aaSAndroid Build Coastguard Worker     @param colorSpace          This describes the color space of this image's contents, as
143*c8dee2aaSAndroid Build Coastguard Worker                                seen after sampling. In general, if the format of the backend
144*c8dee2aaSAndroid Build Coastguard Worker                                texture is SRGB, some linear colorSpace should be supplied
145*c8dee2aaSAndroid Build Coastguard Worker                                (e.g., SkColorSpace::MakeSRGBLinear()). If the format of the
146*c8dee2aaSAndroid Build Coastguard Worker                                backend texture is linear, then the colorSpace should include
147*c8dee2aaSAndroid Build Coastguard Worker                                a description of the transfer function as
148*c8dee2aaSAndroid Build Coastguard Worker                                well (e.g., SkColorSpace::MakeSRGB()).
149*c8dee2aaSAndroid Build Coastguard Worker     @param textureReleaseProc  function called when the backend texture can be released
150*c8dee2aaSAndroid Build Coastguard Worker     @param releaseContext      state passed to textureReleaseProc
151*c8dee2aaSAndroid Build Coastguard Worker     @return                    created SkImage, or nullptr
152*c8dee2aaSAndroid Build Coastguard Worker */
153*c8dee2aaSAndroid Build Coastguard Worker SK_API sk_sp<SkImage> TextureFromCompressedTexture(GrRecordingContext* context,
154*c8dee2aaSAndroid Build Coastguard Worker                                                    const GrBackendTexture& backendTexture,
155*c8dee2aaSAndroid Build Coastguard Worker                                                    GrSurfaceOrigin origin,
156*c8dee2aaSAndroid Build Coastguard Worker                                                    SkAlphaType alphaType,
157*c8dee2aaSAndroid Build Coastguard Worker                                                    sk_sp<SkColorSpace> colorSpace,
158*c8dee2aaSAndroid Build Coastguard Worker                                                    TextureReleaseProc textureReleaseProc = nullptr,
159*c8dee2aaSAndroid Build Coastguard Worker                                                    ReleaseContext releaseContext = nullptr);
160*c8dee2aaSAndroid Build Coastguard Worker 
161*c8dee2aaSAndroid Build Coastguard Worker /** Creates a GPU-backed SkImage from compressed data.
162*c8dee2aaSAndroid Build Coastguard Worker     This method will return an SkImage representing the compressed data.
163*c8dee2aaSAndroid Build Coastguard Worker     If the GPU doesn't support the specified compression method, the data
164*c8dee2aaSAndroid Build Coastguard Worker     will be decompressed and then wrapped in a GPU-backed image.
165*c8dee2aaSAndroid Build Coastguard Worker     Note: one can query the supported compression formats via
166*c8dee2aaSAndroid Build Coastguard Worker     GrRecordingContext::compressedBackendFormat.
167*c8dee2aaSAndroid Build Coastguard Worker     @param context     GPU context
168*c8dee2aaSAndroid Build Coastguard Worker     @param data        compressed data to store in SkImage
169*c8dee2aaSAndroid Build Coastguard Worker     @param width       width of full SkImage
170*c8dee2aaSAndroid Build Coastguard Worker     @param height      height of full SkImage
171*c8dee2aaSAndroid Build Coastguard Worker     @param type        type of compression used
172*c8dee2aaSAndroid Build Coastguard Worker     @param mipmapped   does 'data' contain data for all the mipmap levels?
173*c8dee2aaSAndroid Build Coastguard Worker     @param isProtected do the contents of 'data' require DRM protection (on Vulkan)?
174*c8dee2aaSAndroid Build Coastguard Worker     @return            created SkImage, or nullptr
175*c8dee2aaSAndroid Build Coastguard Worker */
176*c8dee2aaSAndroid Build Coastguard Worker SK_API sk_sp<SkImage> TextureFromCompressedTextureData(
177*c8dee2aaSAndroid Build Coastguard Worker         GrDirectContext* direct,
178*c8dee2aaSAndroid Build Coastguard Worker         sk_sp<SkData> data,
179*c8dee2aaSAndroid Build Coastguard Worker         int width,
180*c8dee2aaSAndroid Build Coastguard Worker         int height,
181*c8dee2aaSAndroid Build Coastguard Worker         SkTextureCompressionType type,
182*c8dee2aaSAndroid Build Coastguard Worker         skgpu::Mipmapped mipmapped = skgpu::Mipmapped::kNo,
183*c8dee2aaSAndroid Build Coastguard Worker         GrProtected isProtected = GrProtected::kNo);
184*c8dee2aaSAndroid Build Coastguard Worker 
185*c8dee2aaSAndroid Build Coastguard Worker /** Returns SkImage backed by GPU texture associated with context. Returned SkImage is
186*c8dee2aaSAndroid Build Coastguard Worker     compatible with SkSurface created with dstColorSpace. The returned SkImage respects
187*c8dee2aaSAndroid Build Coastguard Worker     mipmapped setting; if mipmapped equals skgpu::Mipmapped::kYes, the backing texture
188*c8dee2aaSAndroid Build Coastguard Worker     allocates mip map levels.
189*c8dee2aaSAndroid Build Coastguard Worker     The mipmapped parameter is effectively treated as kNo if MIP maps are not supported by the
190*c8dee2aaSAndroid Build Coastguard Worker     GPU.
191*c8dee2aaSAndroid Build Coastguard Worker     Returns original SkImage if the image is already texture-backed, the context matches, and
192*c8dee2aaSAndroid Build Coastguard Worker     mipmapped is compatible with the backing GPU texture. skgpu::Budgeted is ignored in this
193*c8dee2aaSAndroid Build Coastguard Worker    case.
194*c8dee2aaSAndroid Build Coastguard Worker     Returns nullptr if context is nullptr, or if SkImage was created with another
195*c8dee2aaSAndroid Build Coastguard Worker     GrDirectContext.
196*c8dee2aaSAndroid Build Coastguard Worker     @param GrDirectContext  the GrDirectContext in play, if it exists
197*c8dee2aaSAndroid Build Coastguard Worker     @param SkImage          a non-null pointer to an SkImage.
198*c8dee2aaSAndroid Build Coastguard Worker     @param skgpu::Mipmapped Whether created SkImage texture must allocate mip map levels.
199*c8dee2aaSAndroid Build Coastguard Worker                             Defaults to no.
200*c8dee2aaSAndroid Build Coastguard Worker     @param skgpu::Budgeted  Whether to count a newly created texture for the returned image
201*c8dee2aaSAndroid Build Coastguard Worker                             counts against the context's budget. Defaults to yes.
202*c8dee2aaSAndroid Build Coastguard Worker     @return                 created SkImage, or nullptr
203*c8dee2aaSAndroid Build Coastguard Worker */
204*c8dee2aaSAndroid Build Coastguard Worker SK_API sk_sp<SkImage> TextureFromImage(GrDirectContext*,
205*c8dee2aaSAndroid Build Coastguard Worker                                        const SkImage*,
206*c8dee2aaSAndroid Build Coastguard Worker                                        skgpu::Mipmapped = skgpu::Mipmapped::kNo,
207*c8dee2aaSAndroid Build Coastguard Worker                                        skgpu::Budgeted = skgpu::Budgeted::kYes);
208*c8dee2aaSAndroid Build Coastguard Worker inline sk_sp<SkImage> TextureFromImage(GrDirectContext* ctx,
209*c8dee2aaSAndroid Build Coastguard Worker                                        const sk_sp<const SkImage>& img,
210*c8dee2aaSAndroid Build Coastguard Worker                                        skgpu::Mipmapped m = skgpu::Mipmapped::kNo,
211*c8dee2aaSAndroid Build Coastguard Worker                                        skgpu::Budgeted b = skgpu::Budgeted::kYes) {
212*c8dee2aaSAndroid Build Coastguard Worker     return TextureFromImage(ctx, img.get(), m, b);
213*c8dee2aaSAndroid Build Coastguard Worker }
214*c8dee2aaSAndroid Build Coastguard Worker 
215*c8dee2aaSAndroid Build Coastguard Worker /** Creates a GPU-backed SkImage from SkYUVAPixmaps.
216*c8dee2aaSAndroid Build Coastguard Worker     The image will remain planar with each plane converted to a texture using the passed
217*c8dee2aaSAndroid Build Coastguard Worker     GrRecordingContext.
218*c8dee2aaSAndroid Build Coastguard Worker     SkYUVAPixmaps has a SkYUVAInfo which specifies the transformation from YUV to RGB.
219*c8dee2aaSAndroid Build Coastguard Worker     The SkColorSpace of the resulting RGB values is specified by imageColorSpace. This will
220*c8dee2aaSAndroid Build Coastguard Worker     be the SkColorSpace reported by the image and when drawn the RGB values will be converted
221*c8dee2aaSAndroid Build Coastguard Worker     from this space into the destination space (if the destination is tagged).
222*c8dee2aaSAndroid Build Coastguard Worker     Currently, this is only supported using the GPU backend and will fail if context is nullptr.
223*c8dee2aaSAndroid Build Coastguard Worker     SkYUVAPixmaps does not need to remain valid after this returns.
224*c8dee2aaSAndroid Build Coastguard Worker     @param context                GPU context
225*c8dee2aaSAndroid Build Coastguard Worker     @param pixmaps                The planes as pixmaps with supported SkYUVAInfo that
226*c8dee2aaSAndroid Build Coastguard Worker                                   specifies conversion to RGB.
227*c8dee2aaSAndroid Build Coastguard Worker     @param buildMips              create internal YUVA textures as mip map if kYes. This is
228*c8dee2aaSAndroid Build Coastguard Worker                                   silently ignored if the context does not support mip maps.
229*c8dee2aaSAndroid Build Coastguard Worker     @param limitToMaxTextureSize  downscale image to GPU maximum texture size, if necessary
230*c8dee2aaSAndroid Build Coastguard Worker     @param imageColorSpace        range of colors of the resulting image; may be nullptr
231*c8dee2aaSAndroid Build Coastguard Worker     @return                       created SkImage, or nullptr
232*c8dee2aaSAndroid Build Coastguard Worker */
233*c8dee2aaSAndroid Build Coastguard Worker SK_API sk_sp<SkImage> TextureFromYUVAPixmaps(GrRecordingContext* context,
234*c8dee2aaSAndroid Build Coastguard Worker                                              const SkYUVAPixmaps& pixmaps,
235*c8dee2aaSAndroid Build Coastguard Worker                                              skgpu::Mipmapped buildMips,
236*c8dee2aaSAndroid Build Coastguard Worker                                              bool limitToMaxTextureSize,
237*c8dee2aaSAndroid Build Coastguard Worker                                              sk_sp<SkColorSpace> imageColorSpace);
238*c8dee2aaSAndroid Build Coastguard Worker SK_API sk_sp<SkImage> TextureFromYUVAPixmaps(GrRecordingContext* context,
239*c8dee2aaSAndroid Build Coastguard Worker                                              const SkYUVAPixmaps& pixmaps,
240*c8dee2aaSAndroid Build Coastguard Worker                                              skgpu::Mipmapped buildMips = skgpu::Mipmapped::kNo,
241*c8dee2aaSAndroid Build Coastguard Worker                                              bool limitToMaxTextureSize = false);
242*c8dee2aaSAndroid Build Coastguard Worker 
243*c8dee2aaSAndroid Build Coastguard Worker /** Creates a GPU-backed SkImage from YUV[A] planar textures. This requires that the textures
244*c8dee2aaSAndroid Build Coastguard Worker  *  stay valid for the lifetime of the image. The ReleaseContext can be used to know when it is
245*c8dee2aaSAndroid Build Coastguard Worker  *  safe to either delete or overwrite the textures. If ReleaseProc is provided it is also called
246*c8dee2aaSAndroid Build Coastguard Worker  *  before return on failure.
247*c8dee2aaSAndroid Build Coastguard Worker     @param context            GPU context
248*c8dee2aaSAndroid Build Coastguard Worker     @param yuvaTextures       A set of textures containing YUVA data and a description of the
249*c8dee2aaSAndroid Build Coastguard Worker                               data and transformation to RGBA.
250*c8dee2aaSAndroid Build Coastguard Worker     @param imageColorSpace    range of colors of the resulting image after conversion to RGB;
251*c8dee2aaSAndroid Build Coastguard Worker                               may be nullptr
252*c8dee2aaSAndroid Build Coastguard Worker     @param textureReleaseProc called when the backend textures can be released
253*c8dee2aaSAndroid Build Coastguard Worker     @param releaseContext     state passed to textureReleaseProc
254*c8dee2aaSAndroid Build Coastguard Worker     @return                   created SkImage, or nullptr
255*c8dee2aaSAndroid Build Coastguard Worker */
256*c8dee2aaSAndroid Build Coastguard Worker SK_API sk_sp<SkImage> TextureFromYUVATextures(GrRecordingContext* context,
257*c8dee2aaSAndroid Build Coastguard Worker                                               const GrYUVABackendTextures& yuvaTextures,
258*c8dee2aaSAndroid Build Coastguard Worker                                               sk_sp<SkColorSpace> imageColorSpace,
259*c8dee2aaSAndroid Build Coastguard Worker                                               TextureReleaseProc textureReleaseProc = nullptr,
260*c8dee2aaSAndroid Build Coastguard Worker                                               ReleaseContext releaseContext = nullptr);
261*c8dee2aaSAndroid Build Coastguard Worker SK_API sk_sp<SkImage> TextureFromYUVATextures(GrRecordingContext* context,
262*c8dee2aaSAndroid Build Coastguard Worker                                               const GrYUVABackendTextures& yuvaTextures);
263*c8dee2aaSAndroid Build Coastguard Worker 
264*c8dee2aaSAndroid Build Coastguard Worker /** Retrieves the existing backend texture. If SkImage is not a Ganesh-backend texture image
265*c8dee2aaSAndroid Build Coastguard Worker     or otherwise does not have such a texture, false is returned. Otherwise, outTexture will
266*c8dee2aaSAndroid Build Coastguard Worker     be set to the image's texture.
267*c8dee2aaSAndroid Build Coastguard Worker 
268*c8dee2aaSAndroid Build Coastguard Worker     If flushPendingGrContextIO is true, completes deferred I/O operations.
269*c8dee2aaSAndroid Build Coastguard Worker     If origin in not nullptr, copies location of content drawn into SkImage.
270*c8dee2aaSAndroid Build Coastguard Worker     @param outTexture               Will be set to the underlying texture of the image if non-null.
271*c8dee2aaSAndroid Build Coastguard Worker     @param flushPendingGrContextIO  flag to flush outstanding requests
272*c8dee2aaSAndroid Build Coastguard Worker     @param origin                   Will be set to the origin orientation of the image if non-null.
273*c8dee2aaSAndroid Build Coastguard Worker     @return                         false if a Ganesh backend texture cannot be retrieved.
274*c8dee2aaSAndroid Build Coastguard Worker */
275*c8dee2aaSAndroid Build Coastguard Worker SK_API bool GetBackendTextureFromImage(const SkImage* img,
276*c8dee2aaSAndroid Build Coastguard Worker                                        GrBackendTexture* outTexture,
277*c8dee2aaSAndroid Build Coastguard Worker                                        bool flushPendingGrContextIO,
278*c8dee2aaSAndroid Build Coastguard Worker                                        GrSurfaceOrigin* origin = nullptr);
279*c8dee2aaSAndroid Build Coastguard Worker inline bool GetBackendTextureFromImage(const sk_sp<const SkImage>& img,
280*c8dee2aaSAndroid Build Coastguard Worker                                        GrBackendTexture* outTexture,
281*c8dee2aaSAndroid Build Coastguard Worker                                        bool flushPendingGrContextIO,
282*c8dee2aaSAndroid Build Coastguard Worker                                        GrSurfaceOrigin* origin = nullptr) {
283*c8dee2aaSAndroid Build Coastguard Worker     return GetBackendTextureFromImage(img.get(), outTexture, flushPendingGrContextIO, origin);
284*c8dee2aaSAndroid Build Coastguard Worker }
285*c8dee2aaSAndroid Build Coastguard Worker 
286*c8dee2aaSAndroid Build Coastguard Worker /** Extracts the backendTexture from an existing SkImage.
287*c8dee2aaSAndroid Build Coastguard Worker     If the image is not already GPU-backed, the raster data will be uploaded as a texture
288*c8dee2aaSAndroid Build Coastguard Worker     and returned.
289*c8dee2aaSAndroid Build Coastguard Worker     If this is the only reference to the image, the old image's texture will be
290*c8dee2aaSAndroid Build Coastguard Worker     moved out of the passed in image.
291*c8dee2aaSAndroid Build Coastguard Worker     If the image is shared (has a refcount > 1), the texture will be copied and then returned.
292*c8dee2aaSAndroid Build Coastguard Worker     @param context                    GPU context
293*c8dee2aaSAndroid Build Coastguard Worker     @param image                      image, either CPU-backed or GPU-backed
294*c8dee2aaSAndroid Build Coastguard Worker     @param backendTexture             Will be set to the underlying texture of the image.
295*c8dee2aaSAndroid Build Coastguard Worker     @param backendTextureReleaseProc  Called when the texture is released
296*c8dee2aaSAndroid Build Coastguard Worker     @return                           false if image cannot be uploaded.
297*c8dee2aaSAndroid Build Coastguard Worker */
298*c8dee2aaSAndroid Build Coastguard Worker SK_API bool MakeBackendTextureFromImage(GrDirectContext* context,
299*c8dee2aaSAndroid Build Coastguard Worker                                         sk_sp<SkImage> image,
300*c8dee2aaSAndroid Build Coastguard Worker                                         GrBackendTexture* backendTexture,
301*c8dee2aaSAndroid Build Coastguard Worker                                         BackendTextureReleaseProc* backendTextureReleaseProc);
302*c8dee2aaSAndroid Build Coastguard Worker // Legacy name
GetBackendTextureFromImage(GrDirectContext * context,sk_sp<SkImage> image,GrBackendTexture * backendTexture,BackendTextureReleaseProc * backendTextureReleaseProc)303*c8dee2aaSAndroid Build Coastguard Worker inline bool GetBackendTextureFromImage(GrDirectContext* context,
304*c8dee2aaSAndroid Build Coastguard Worker                                        sk_sp<SkImage> image,
305*c8dee2aaSAndroid Build Coastguard Worker                                        GrBackendTexture* backendTexture,
306*c8dee2aaSAndroid Build Coastguard Worker                                        BackendTextureReleaseProc* backendTextureReleaseProc) {
307*c8dee2aaSAndroid Build Coastguard Worker     return MakeBackendTextureFromImage(context, std::move(image), backendTexture,
308*c8dee2aaSAndroid Build Coastguard Worker                                        backendTextureReleaseProc);
309*c8dee2aaSAndroid Build Coastguard Worker }
310*c8dee2aaSAndroid Build Coastguard Worker 
311*c8dee2aaSAndroid Build Coastguard Worker /** Returns subset of this image as a texture-backed image.
312*c8dee2aaSAndroid Build Coastguard Worker 
313*c8dee2aaSAndroid Build Coastguard Worker     Returns nullptr if any of the following are true:
314*c8dee2aaSAndroid Build Coastguard Worker       - Subset is empty
315*c8dee2aaSAndroid Build Coastguard Worker       - Subset is not contained inside the image's bounds
316*c8dee2aaSAndroid Build Coastguard Worker       - Pixels in the source image could not be read or copied
317*c8dee2aaSAndroid Build Coastguard Worker       - The source image is texture-backed and context does not match the source image's context.
318*c8dee2aaSAndroid Build Coastguard Worker 
319*c8dee2aaSAndroid Build Coastguard Worker     @param context the non-null GrDirectContext to which the subset should be uploaded.
320*c8dee2aaSAndroid Build Coastguard Worker     @param subset  bounds of returned SkImage
321*c8dee2aaSAndroid Build Coastguard Worker     @return        the subsetted image, uploaded as a texture, or nullptr
322*c8dee2aaSAndroid Build Coastguard Worker */
323*c8dee2aaSAndroid Build Coastguard Worker SK_API sk_sp<SkImage> SubsetTextureFrom(GrDirectContext* context,
324*c8dee2aaSAndroid Build Coastguard Worker                                         const SkImage* img,
325*c8dee2aaSAndroid Build Coastguard Worker                                         const SkIRect& subset);
326*c8dee2aaSAndroid Build Coastguard Worker 
327*c8dee2aaSAndroid Build Coastguard Worker /** Creates a filtered SkImage on the GPU. filter processes the src image, potentially changing
328*c8dee2aaSAndroid Build Coastguard Worker     color, position, and size. subset is the bounds of src that are processed
329*c8dee2aaSAndroid Build Coastguard Worker     by filter. clipBounds is the expected bounds of the filtered SkImage. outSubset
330*c8dee2aaSAndroid Build Coastguard Worker     is required storage for the actual bounds of the filtered SkImage. offset is
331*c8dee2aaSAndroid Build Coastguard Worker     required storage for translation of returned SkImage.
332*c8dee2aaSAndroid Build Coastguard Worker 
333*c8dee2aaSAndroid Build Coastguard Worker     Returns nullptr if SkImage could not be created or if the recording context provided doesn't
334*c8dee2aaSAndroid Build Coastguard Worker     match the GPU context in which the image was created. If nullptr is returned, outSubset
335*c8dee2aaSAndroid Build Coastguard Worker     and offset are undefined.
336*c8dee2aaSAndroid Build Coastguard Worker 
337*c8dee2aaSAndroid Build Coastguard Worker     Useful for animation of SkImageFilter that varies size from frame to frame.
338*c8dee2aaSAndroid Build Coastguard Worker     Returned SkImage is created larger than required by filter so that GPU texture
339*c8dee2aaSAndroid Build Coastguard Worker     can be reused with different sized effects. outSubset describes the valid bounds
340*c8dee2aaSAndroid Build Coastguard Worker     of GPU texture returned. offset translates the returned SkImage to keep subsequent
341*c8dee2aaSAndroid Build Coastguard Worker     animation frames aligned with respect to each other.
342*c8dee2aaSAndroid Build Coastguard Worker 
343*c8dee2aaSAndroid Build Coastguard Worker     @param context     the GrRecordingContext in play - if it exists
344*c8dee2aaSAndroid Build Coastguard Worker     @param filter      how SkImage is sampled when transformed
345*c8dee2aaSAndroid Build Coastguard Worker     @param subset      bounds of SkImage processed by filter
346*c8dee2aaSAndroid Build Coastguard Worker     @param clipBounds  expected bounds of filtered SkImage
347*c8dee2aaSAndroid Build Coastguard Worker     @param outSubset   storage for returned SkImage bounds
348*c8dee2aaSAndroid Build Coastguard Worker     @param offset      storage for returned SkImage translation
349*c8dee2aaSAndroid Build Coastguard Worker     @return            filtered SkImage, or nullptr
350*c8dee2aaSAndroid Build Coastguard Worker */
351*c8dee2aaSAndroid Build Coastguard Worker SK_API sk_sp<SkImage> MakeWithFilter(GrRecordingContext* context,
352*c8dee2aaSAndroid Build Coastguard Worker                                      sk_sp<SkImage> src,
353*c8dee2aaSAndroid Build Coastguard Worker                                      const SkImageFilter* filter,
354*c8dee2aaSAndroid Build Coastguard Worker                                      const SkIRect& subset,
355*c8dee2aaSAndroid Build Coastguard Worker                                      const SkIRect& clipBounds,
356*c8dee2aaSAndroid Build Coastguard Worker                                      SkIRect* outSubset,
357*c8dee2aaSAndroid Build Coastguard Worker                                      SkIPoint* offset);
358*c8dee2aaSAndroid Build Coastguard Worker 
359*c8dee2aaSAndroid Build Coastguard Worker }  // namespace SkImages
360*c8dee2aaSAndroid Build Coastguard Worker 
361*c8dee2aaSAndroid Build Coastguard Worker #endif
362