Debian Codegraph @

src/gpu/graphite/Image_Base_Graphite.cpp

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 #include "src/gpu/graphite/Image_Base_Graphite.h"
9
10 #include "include/core/SkColorSpace.h"
11 #include "include/gpu/graphite/Image.h"
12 #include "include/gpu/graphite/Recorder.h"
13 #include "include/gpu/graphite/Surface.h"
14 #include "src/gpu/graphite/Device.h"
15 #include "src/gpu/graphite/DrawContext.h"
16 #include "src/gpu/graphite/Image_Graphite.h"
17 #include "src/gpu/graphite/Image_YUVA_Graphite.h"
18 #include "src/gpu/graphite/Log.h"
19 #include "src/gpu/graphite/RecorderPriv.h"
20 #include "src/gpu/graphite/Surface_Graphite.h"
21 #include "src/gpu/graphite/TextureUtils.h"
22 #include "src/gpu/graphite/task/Task.h"
23
24 namespace skgpu::graphite {
25
26 Image_Base::Image_Base(const SkImageInfo& info, uint32_t uniqueID)
27 : SkImage_Base(info, uniqueID) {}
28
29 Image_Base::~Image_Base() = default;
30
31 void Image_Base::linkDevices(const Image_Base* other) {
32 SkASSERT(other);
33
34 SkAutoSpinlock lock{other->fDeviceLinkLock};
35 for (const auto& device : other->fLinkedDevices) {
36 this->linkDevice(device);
37 }
38 }
39
40 void Image_Base::linkDevice(sk_sp<Device> device) {
41 // Technically this lock isn't needed since this is only called before the Image is returned to
42 // user code that could expose it to multiple threads. But this quiets threading warnings and
43 // should be uncontested.
44 SkAutoSpinlock lock{fDeviceLinkLock};
45 fLinkedDevices.push_back(std::move(device));
46 }
47
48 void Image_Base::notifyInUse(Recorder* recorder, DrawContext* drawContext) const {
49 SkASSERT(recorder);
50
51 // The ref counts stored on each linked device are thread safe, but the Image's sk_sp's that
52 // track the refs its responsible for are *not* thread safe. Use a spin lock since the majority
53 // of device-linked images will be used only on the Recorder's thread. Since it should be
54 // uncontended, the empty check is also done inside the lock vs. a double-checked locking
55 // pattern that is non-trivial to ensure correctness in C++.
56 SkAutoSpinlock lock{fDeviceLinkLock};
57
58 if (!fLinkedDevices.empty()) {
59 int emptyCount = 0;
60 for (sk_sp<Device>& device : fLinkedDevices) {
61 if (!device || device->notifyInUse(recorder, drawContext)) {
62 // Already unlinked or notifyInUse() signals the device doesn't need to be linked
63 // anymore. reset() is a no-op if device is already holding null.
64 device.reset();
65 emptyCount++;
66 }
67 }
68
69 if (emptyCount == fLinkedDevices.size()) {
70 fLinkedDevices.clear();
71 }
72 }
73 }
74
75 bool Image_Base::isDynamic() const {
76 SkAutoSpinlock lock{fDeviceLinkLock};
77 int emptyCount = 0;
78 if (!fLinkedDevices.empty()) {
79 for (sk_sp<Device>& device : fLinkedDevices) {
80 if (!device || !device->recorder() || device->unique()) {
81 device.reset();
82 emptyCount++;
83 }
84 }
85 if (emptyCount == fLinkedDevices.size()) {
86 fLinkedDevices.clear();
87 emptyCount = 0;
88 }
89 }
90
91 return emptyCount > 0;
92 }
93
94 // For now, CopyAsDraw is called with a nullptr for the drawContext, which causes the draw task
95 // to be pushed onto the root task list. However, this could be given a drawContext in the future.
96 sk_sp<Image> Image_Base::copyImage(Recorder* recorder,
97 const SkIRect& subset,
98 Budgeted budgeted,
99 Mipmapped mipmapped,
100 SkBackingFit backingFit,
101 std::string_view label) const {
102 return CopyAsDraw(recorder,
103 /*drawContext=*/nullptr, this, subset, this->imageInfo().colorInfo(),
104 budgeted, mipmapped, backingFit, label);
105 }
106
107 namespace {
108
109 TextureProxy* get_base_proxy_for_label(const Image_Base* baseImage) {
110 if (baseImage->type() == SkImage_Base::Type::kGraphite) {
111 const Image* img = static_cast<const Image*>(baseImage);
112 return img->textureProxyView().proxy();
113 }
114 SkASSERT(baseImage->type() == SkImage_Base::Type::kGraphiteYUVA);
115 // We will end up flattening to RGBA for a YUVA image when we get a subset. We just grab
116 // the label off of the first channel's proxy and use that to be the stand in label.
117 const Image_YUVA* img = static_cast<const Image_YUVA*>(baseImage);
118 return img->proxyView(0).proxy();
119 }
120
121 } // anonymous namespace
122
123 sk_sp<SkImage> Image_Base::onMakeSubset(SkRecorder* recorder,
124 const SkIRect& subset,
125 RequiredProperties requiredProps) const {
126 auto gRecorder = AsGraphiteRecorder(recorder);
127 if (!gRecorder) {
128 return nullptr;
129 }
130 // optimization : return self if the subset == our bounds and requirements met and the image's
131 // texture is immutable
132 if (this->bounds() == subset &&
133 (!requiredProps.fMipmapped || this->hasMipmaps()) &&
134 !this->isDynamic()) {
135 return sk_ref_sp(this);
136 }
137
138 TextureProxy* proxy = get_base_proxy_for_label(this);
139 SkASSERT(proxy);
140 std::string label = proxy->label();
141 if (label.empty()) {
142 label = "ImageSubsetTexture";
143 } else {
144 label += "_Subset";
145 }
146
147 // The copied image is not considered budgeted because this is a client-invoked API and they
148 // will own the image.
149 return this->copyImage(gRecorder,
150 subset,
151 Budgeted::kNo,
152 requiredProps.fMipmapped ? Mipmapped::kYes : Mipmapped::kNo,
153 SkBackingFit::kExact,
154 label);
155 }
156
157 sk_sp<SkSurface> Image_Base::onMakeSurface(SkRecorder* recorder, const SkImageInfo& info) const {
158 auto gRecorder = AsGraphiteRecorder(recorder);
159 if (!gRecorder) {
160 return nullptr;
161 }
162 return SkSurfaces::RenderTarget(gRecorder, info);
163 }
164
165 sk_sp<SkImage> Image_Base::makeColorTypeAndColorSpace(SkRecorder* recorder,
166 SkColorType targetCT,
167 sk_sp<SkColorSpace> targetCS,
168 RequiredProperties requiredProps) const {
169 auto gRecorder = AsGraphiteRecorder(recorder);
170 if (!gRecorder) {
171 return nullptr;
172 }
173
174 SkColorInfo dstColorInfo{targetCT, this->alphaType(), std::move(targetCS)};
175 // optimization : return self if there's no color type/space change and the image's texture
176 // is immutable
177 if (this->imageInfo().colorInfo() == dstColorInfo && !this->isDynamic()) {
178 return sk_ref_sp(this);
179 }
180
181 TextureProxy* proxy = get_base_proxy_for_label(this);
182 SkASSERT(proxy);
183 std::string label = proxy->label();
184 if (label.empty()) {
185 label = "ImageMakeCTandCSTexture";
186 } else {
187 label += "_CTandCSConversion";
188 }
189
190 // Use CopyAsDraw directly to perform the color space changes. The copied image is not
191 // considered budgeted because this is a client-invoked API and they will own the image.
192 return CopyAsDraw(gRecorder,
193 /*drawContext=*/nullptr,
194 this,
195 this->bounds(),
196 dstColorInfo,
197 Budgeted::kNo,
198 requiredProps.fMipmapped ? Mipmapped::kYes : Mipmapped::kNo,
199 SkBackingFit::kExact,
200 label);
201 }
202
203 // Ganesh APIs are no-ops
204 void Image_Base::onAsyncRescaleAndReadPixels(const SkImageInfo& info,
205 SkIRect srcRect,
206 RescaleGamma rescaleGamma,
207 RescaleMode rescaleMode,
208 ReadPixelsCallback callback,
209 ReadPixelsContext context) const {
210 SKGPU_LOG_W("Cannot use Ganesh async API with Graphite-backed image, use API on Context");
211 callback(context, nullptr);
212 }
213
214 void Image_Base::onAsyncRescaleAndReadPixelsYUV420(SkYUVColorSpace yuvColorSpace,
215 bool readAlpha,
216 sk_sp<SkColorSpace> dstColorSpace,
217 const SkIRect srcRect,
218 const SkISize dstSize,
219 RescaleGamma rescaleGamma,
220 RescaleMode rescaleMode,
221 ReadPixelsCallback callback,
222 ReadPixelsContext context) const {
223 SKGPU_LOG_W("Cannot use Ganesh async API with Graphite-backed image, use API on Context");
224 callback(context, nullptr);
225 }
226
227 } // namespace skgpu::graphite