xref: /aosp_15_r20/external/skia/include/gpu/graphite/ImageProvider.h (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1*c8dee2aaSAndroid Build Coastguard Worker /*
2*c8dee2aaSAndroid Build Coastguard Worker  * Copyright 2022 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 skgpu_graphite_ImageProvider_DEFINED
9*c8dee2aaSAndroid Build Coastguard Worker #define skgpu_graphite_ImageProvider_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 
14*c8dee2aaSAndroid Build Coastguard Worker namespace skgpu::graphite {
15*c8dee2aaSAndroid Build Coastguard Worker 
16*c8dee2aaSAndroid Build Coastguard Worker class Recorder;
17*c8dee2aaSAndroid Build Coastguard Worker 
18*c8dee2aaSAndroid Build Coastguard Worker /*
19*c8dee2aaSAndroid Build Coastguard Worker  * This class provides a centralized location for clients to perform any caching of images
20*c8dee2aaSAndroid Build Coastguard Worker  * they desire. Whenever Graphite encounters an SkImage which is not Graphite-backed
21*c8dee2aaSAndroid Build Coastguard Worker  * it will call ImageProvider::findOrCreate. The client's derived version of this class should
22*c8dee2aaSAndroid Build Coastguard Worker  * return a Graphite-backed version of the provided SkImage that meets the specified
23*c8dee2aaSAndroid Build Coastguard Worker  * requirements.
24*c8dee2aaSAndroid Build Coastguard Worker  *
25*c8dee2aaSAndroid Build Coastguard Worker  * Skia requires that 'findOrCreate' return a Graphite-backed image that preserves the
26*c8dee2aaSAndroid Build Coastguard Worker  * dimensions and alpha type of the original image. The bit depth of the
27*c8dee2aaSAndroid Build Coastguard Worker  * individual channels can change (e.g., 4444 -> 8888 is allowed) as well as the channels - as
28*c8dee2aaSAndroid Build Coastguard Worker  * long as the returned image has a superset of the original image's channels
29*c8dee2aaSAndroid Build Coastguard Worker  * (e.g., 565 -> 8888 opaque is allowed).
30*c8dee2aaSAndroid Build Coastguard Worker  *
31*c8dee2aaSAndroid Build Coastguard Worker  * Wrt mipmapping, the returned image can have different mipmap settings than requested. If
32*c8dee2aaSAndroid Build Coastguard Worker  * mipmapping was requested but not returned, the sampling level will be reduced to linear.
33*c8dee2aaSAndroid Build Coastguard Worker  * If the requirements are not met by the returned image (modulo the flexibility wrt mipmapping)
34*c8dee2aaSAndroid Build Coastguard Worker  * Graphite will drop the draw.
35*c8dee2aaSAndroid Build Coastguard Worker  *
36*c8dee2aaSAndroid Build Coastguard Worker  * All returned images must be backed by textures that have a TopLeft origin. If Skia is used to
37*c8dee2aaSAndroid Build Coastguard Worker  * create the texture (e.g. using makeTextureImage) then this is always guaranteed. If the client
38*c8dee2aaSAndroid Build Coastguard Worker  * returns a texture they created themselves and wrapped in Skia, they must ensure that texture has
39*c8dee2aaSAndroid Build Coastguard Worker  * a TopLeft origin.
40*c8dee2aaSAndroid Build Coastguard Worker  *
41*c8dee2aaSAndroid Build Coastguard Worker  * Note: by default, Graphite will not perform any caching of images
42*c8dee2aaSAndroid Build Coastguard Worker  *
43*c8dee2aaSAndroid Build Coastguard Worker  * Threading concerns:
44*c8dee2aaSAndroid Build Coastguard Worker  *   If the same ImageProvider is given to multiple Recorders it is up to the
45*c8dee2aaSAndroid Build Coastguard Worker  *   client to handle any required thread synchronization. This is not limited to just
46*c8dee2aaSAndroid Build Coastguard Worker  *   restricting access to whatever map a derived class may have but extends to ensuring
47*c8dee2aaSAndroid Build Coastguard Worker  *   that an image created on one Recorder has had its creation work submitted before it
48*c8dee2aaSAndroid Build Coastguard Worker  *   is used by any work submitted by another Recording. Please note, this requirement
49*c8dee2aaSAndroid Build Coastguard Worker  *   (re the submission of creation work and image usage on different threads) is common to all
50*c8dee2aaSAndroid Build Coastguard Worker  *   graphite SkImages and isn't unique to SkImages returned by the ImageProvider.
51*c8dee2aaSAndroid Build Coastguard Worker  *
52*c8dee2aaSAndroid Build Coastguard Worker  * TODO(b/240996632): add documentation re shutdown order.
53*c8dee2aaSAndroid Build Coastguard Worker  * TODO(b/240997067): add unit tests
54*c8dee2aaSAndroid Build Coastguard Worker  */
55*c8dee2aaSAndroid Build Coastguard Worker class SK_API ImageProvider : public SkRefCnt {
56*c8dee2aaSAndroid Build Coastguard Worker public:
57*c8dee2aaSAndroid Build Coastguard Worker     // If the client's derived class already has a Graphite-backed image that has the same
58*c8dee2aaSAndroid Build Coastguard Worker     // contents as 'image' and meets the requirements, then it can be returned.
59*c8dee2aaSAndroid Build Coastguard Worker     // makeTextureImage can always be called to create an acceptable Graphite-backed image
60*c8dee2aaSAndroid Build Coastguard Worker     // which could then be cached.
61*c8dee2aaSAndroid Build Coastguard Worker     virtual sk_sp<SkImage> findOrCreate(Recorder* recorder,
62*c8dee2aaSAndroid Build Coastguard Worker                                         const SkImage* image,
63*c8dee2aaSAndroid Build Coastguard Worker                                         SkImage::RequiredProperties) = 0;
64*c8dee2aaSAndroid Build Coastguard Worker };
65*c8dee2aaSAndroid Build Coastguard Worker 
66*c8dee2aaSAndroid Build Coastguard Worker } // namespace skgpu::graphite
67*c8dee2aaSAndroid Build Coastguard Worker 
68*c8dee2aaSAndroid Build Coastguard Worker #endif // skgpu_graphite_ImageProvider_DEFINED
69