xref: /aosp_15_r20/external/skia/include/private/chromium/SkImageChromium.h (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
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 #ifndef SkImageChromium_DEFINED
9 #define SkImageChromium_DEFINED
10 
11 #include "include/core/SkRefCnt.h"
12 #include "include/private/base/SkAPI.h"
13 
14 class GrBackendFormat;
15 class GrContextThreadSafeProxy;
16 class GrPromiseImageTexture;
17 class GrDirectContext;
18 class GrYUVABackendTextureInfo;
19 class SkColorSpace;
20 class SkImage;
21 enum SkAlphaType : int;
22 enum SkColorType : int;
23 enum GrSurfaceOrigin : int;
24 namespace skgpu {
25 enum class Mipmapped : bool;
26 }
27 struct SkISize;
28 
29 /**
30  * These functions expose features that are only for external use in Chromium.
31  */
32 
33 namespace SkImages {
34 
35 using PromiseImageTextureContext = void*;
36 using PromiseImageTextureFulfillProc = sk_sp<GrPromiseImageTexture> (*)(PromiseImageTextureContext);
37 using PromiseImageTextureReleaseProc = void (*)(PromiseImageTextureContext);
38 
39 /** Create a new GPU-backed SkImage that is very similar to an SkImage created by BorrowTextureFrom.
40     The difference is that the caller need not have created the texture nor populated it with the
41     image pixel data. Moreover, the SkImage may be created on a thread as the creation of the
42     image does not require access to the backend API or GrDirectContext. Instead of passing a
43     GrBackendTexture the client supplies a description of the texture consisting of
44     GrBackendFormat, width, height, and skgpu::Mipmapped state. The resulting SkImage can be drawn
45     to a GrDeferredDisplayListRecorder or directly to a GPU-backed SkSurface.
46     When the actual texture is required to perform a backend API draw, textureFulfillProc will
47     be called to receive a GrBackendTexture. The properties of the GrBackendTexture must match
48     those set during the SkImage creation, and it must refer to a valid existing texture in the
49     backend API context/device, and be populated with the image pixel data. The texture cannot
50     be deleted until textureReleaseProc is called.
51     There is at most one call to each of textureFulfillProc and textureReleaseProc.
52     textureReleaseProc is always called even if image creation fails or if the
53     image is never fulfilled (e.g. it is never drawn or all draws are clipped out)
54     @param gpuContextProxy     the thread-safe proxy of the gpu context. required.
55     @param backendFormat       format of promised gpu texture
56     @param dimensions          width & height of promised gpu texture
57     @param mipmapped           mip mapped state of promised gpu texture
58     @param origin              surface origin of promised gpu texture
59     @param colorType           color type of promised gpu texture
60     @param alphaType           alpha type of promised gpu texture
61     @param colorSpace          range of colors; may be nullptr
62     @param textureFulfillProc  function called to get actual gpu texture
63     @param textureReleaseProc  function called when texture can be deleted
64     @param textureContext      state passed to textureFulfillProc and textureReleaseProc
65     @return                    created SkImage, or nullptr
66 */
67 SK_API sk_sp<SkImage> PromiseTextureFrom(sk_sp<GrContextThreadSafeProxy> gpuContextProxy,
68                                          const GrBackendFormat& backendFormat,
69                                          SkISize dimensions,
70                                          skgpu::Mipmapped mipmapped,
71                                          GrSurfaceOrigin origin,
72                                          SkColorType colorType,
73                                          SkAlphaType alphaType,
74                                          sk_sp<SkColorSpace> colorSpace,
75                                          PromiseImageTextureFulfillProc textureFulfillProc,
76                                          PromiseImageTextureReleaseProc textureReleaseProc,
77                                          PromiseImageTextureContext textureContext);
78 
79 /** This is similar to 'PromiseTextureFrom' but it creates a GPU-backed SkImage from YUV[A] data.
80     The source data may be planar (i.e. spread across multiple textures). In
81     the extreme Y, U, V, and A are all in different planes and thus the image is specified by
82     four textures. 'backendTextureInfo' describes the planar arrangement, texture formats,
83     conversion to RGB, and origin of the textures. Separate 'textureFulfillProc' and
84     'textureReleaseProc' calls are made for each texture. Each texture has its own
85     PromiseImageTextureContext. If 'backendTextureInfo' is not valid then no release proc
86     calls are made. Otherwise, the calls will be made even on failure. 'textureContexts' has one
87     entry for each of the up to four textures, as indicated by 'backendTextureInfo'.
88     Currently the mip mapped property of 'backendTextureInfo' is ignored. However, in the
89     near future it will be required that if it is kYes then textureFulfillProc must return
90     a mip mapped texture for each plane in order to successfully draw the image.
91     @param gpuContextProxy     the thread-safe proxy of the gpu context. required.
92     @param backendTextureInfo  info about the promised yuva gpu texture
93     @param imageColorSpace     range of colors; may be nullptr
94     @param textureFulfillProc  function called to get actual gpu texture
95     @param textureReleaseProc  function called when texture can be deleted
96     @param textureContexts     state passed to textureFulfillProc and textureReleaseProc
97     @return                    created SkImage, or nullptr
98 */
99 SK_API sk_sp<SkImage> PromiseTextureFromYUVA(sk_sp<GrContextThreadSafeProxy> gpuContextProxy,
100                                              const GrYUVABackendTextureInfo& backendTextureInfo,
101                                              sk_sp<SkColorSpace> imageColorSpace,
102                                              PromiseImageTextureFulfillProc textureFulfillProc,
103                                              PromiseImageTextureReleaseProc textureReleaseProc,
104                                              PromiseImageTextureContext textureContexts[]);
105 
106 /** Returns the GPU context associated with this image or nullptr if the image is not Ganesh-backed.
107     We expose this only to help transition certain API calls and do not intend for this to stick
108     around forever.
109 */
110 SK_API GrDirectContext* GetContext(const SkImage* src);
GetContext(const sk_sp<const SkImage> & src)111 inline GrDirectContext* GetContext(const sk_sp<const SkImage>& src) {
112     return GetContext(src.get());
113 }
114 
115 }  // namespace SkImages
116 
117 #endif
118