Debian Codegraph @

src/gpu/graphite/Image_Base_Graphite.h

raw
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 skgpu_graphite_Image_Base_Graphite_DEFINED
9 #define skgpu_graphite_Image_Base_Graphite_DEFINED
10
11 #include "include/core/SkRecorder.h"
12 #include "include/gpu/GpuTypes.h"
13 #include "include/private/base/SkTArray.h"
14 #include "src/base/SkSpinlock.h"
15 #include "src/image/SkImage_Base.h"
16
17 #include <string_view>
18
19 enum class SkBackingFit;
20
21 namespace skgpu::graphite {
22
23 class Context;
24 class Device;
25 class DrawContext;
26 class Image;
27 class Recorder;
28 class TextureProxy;
29
30 class Image_Base : public SkImage_Base {
31 public:
32 ~Image_Base() override;
33
34 // Must be called at the time of recording an action that reads from the image, be it a draw
35 // or a copy operation. `drawContext` can be null if the "use" is scoped by a draw.
36 void notifyInUse(Recorder*, DrawContext* drawContext) const;
37
38 // Returns true if this image is linked to a device that may render their shared texture(s).
39 bool isDynamic() const;
40
41 // Always copy this image, even if 'subset' and mipmapping match this image exactly.
42 // The base implementation performs all copies as draws.
43 virtual sk_sp<Image> copyImage(Recorder*,
44 const SkIRect& subset,
45 Budgeted,
46 Mipmapped,
47 SkBackingFit,
48 std::string_view label) const;
49
50 // From SkImage.h
51 bool isValid(SkRecorder* recorder) const final {
52 return recorder && recorder->type() == SkRecorder::Type::kGraphite;
53 }
54
55 // From SkImage_Base.h
56 sk_sp<SkImage> onMakeSubset(SkRecorder*, const SkIRect&, RequiredProperties) const final;
57 sk_sp<SkImage> makeColorTypeAndColorSpace(SkRecorder*,
58 SkColorType targetCT,
59 sk_sp<SkColorSpace> targetCS,
60 RequiredProperties) const final;
61
62 // No-ops for Ganesh APIs
63 bool onReadPixels(GrDirectContext*,
64 const SkImageInfo& dstInfo,
65 void* dstPixels,
66 size_t dstRowBytes,
67 int srcX,
68 int srcY,
69 CachingHint) const final {
70 return false;
71 }
72
73 bool getROPixels(GrDirectContext*, SkBitmap*, CachingHint = kAllow_CachingHint) const final {
74 return false;
75 }
76
77 sk_sp<SkSurface> onMakeSurface(SkRecorder*, const SkImageInfo&) const final;
78
79 void onAsyncRescaleAndReadPixels(const SkImageInfo&,
80 SkIRect srcRect,
81 RescaleGamma,
82 RescaleMode,
83 ReadPixelsCallback,
84 ReadPixelsContext) const override;
85
86 void onAsyncRescaleAndReadPixelsYUV420(SkYUVColorSpace,
87 bool readAlpha,
88 sk_sp<SkColorSpace>,
89 SkIRect srcRect,
90 SkISize dstSize,
91 RescaleGamma,
92 RescaleMode,
93 ReadPixelsCallback,
94 ReadPixelsContext) const override;
95
96 protected:
97 Image_Base(const SkImageInfo& info, uint32_t uniqueID);
98
99 // If the passed-in image is linked with Devices that modify its texture, copy the links to
100 // this Image. This is used when a new Image is created that shares the same texture proxy as
101 // a dynamic image. This can only be called before the Image has been returned from a factory.
102 void linkDevices(const Image_Base*);
103 // Link this image to the Device that can write to their shared texture proxy, so that when the
104 // image is sampled in a draw, any pending work from the Device is automatically flushed. This
105 // can only be called before the Image has been returned from a factory function.
106 void linkDevice(sk_sp<Device>);
107
108 private:
109 // Devices are flushed in notifyImageInUse(). If a linked device is uniquely held by the image
110 // or if it's marked as immutable, it will be unlinked (allowing it to be destroyed eventually).
111 // If all linked devices are removed, this array will become empty. Other than initialization,
112 // this array cannot transition from an empty state to having linked devices, so while it's
113 // empty no locking is required.
114 mutable skia_private::STArray<1, sk_sp<Device>> fLinkedDevices SK_GUARDED_BY(fDeviceLinkLock);
115 mutable SkSpinlock fDeviceLinkLock;
116 };
117
118 } // namespace skgpu::graphite
119
120 #endif // skgpu_graphite_Image_Base_Graphite_DEFINED