Debian Codegraph @

src/gpu/graphite/Surface_Graphite.cpp

raw
1 /*
2 * Copyright 2021 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/Surface_Graphite.h"
9
10 #include "include/core/SkCapabilities.h"
11 #include "include/core/SkColorSpace.h"
12 #include "include/core/SkRecorder.h"
13 #include "include/gpu/graphite/BackendTexture.h"
14 #include "include/gpu/graphite/Recorder.h"
15 #include "include/gpu/graphite/Surface.h"
16 #include "src/core/SkSurfacePriv.h"
17 #include "src/gpu/RefCntedCallback.h"
18 #include "src/gpu/SkBackingFit.h"
19 #include "src/gpu/graphite/Caps.h"
20 #include "src/gpu/graphite/Device.h"
21 #include "src/gpu/graphite/Image_Graphite.h"
22 #include "src/gpu/graphite/Log.h"
23 #include "src/gpu/graphite/RecorderPriv.h"
24 #include "src/gpu/graphite/ResourceProvider.h"
25 #include "src/gpu/graphite/Texture.h"
26 #include "src/gpu/graphite/TextureFormat.h"
27
28 namespace skgpu::graphite {
29
30 Surface::Surface(sk_sp<Device> device)
31 : SkSurface_Base(device->width(), device->height(), &device->surfaceProps())
32 , fDevice(std::move(device))
33 , fImageView(Image::WrapDevice(fDevice)) {}
34
35 Surface::~Surface() {
36 // Mark the device immutable when the Surface is destroyed to flush any pending work to the
37 // recorder and to flag the device so that any linked image views can detach from the Device
38 // when they are next drawn.
39 fDevice->setImmutable();
40 }
41
42 SkImageInfo Surface::imageInfo() const {
43 return fDevice->imageInfo();
44 }
45
46 Recorder* Surface::onGetRecorder() const { return fDevice->recorder(); }
47
48 SkRecorder* Surface::onGetBaseRecorder() const { return fDevice->recorder(); }
49
50 TextureProxyView Surface::readSurfaceView() const {
51 return fDevice->readSurfaceView();
52 }
53
54 SkCanvas* Surface::onNewCanvas() { return new SkCanvas(fDevice); }
55
56 sk_sp<SkSurface> Surface::onNewSurface(const SkImageInfo& ii) {
57 return fDevice->makeSurface(ii, this->props());
58 }
59
60 sk_sp<SkImage> Surface::onNewImageSnapshot(const SkIRect* subset) {
61 return this->makeImageCopy(subset, fDevice->target()->mipmapped());
62 }
63
64 sk_sp<Image> Surface::asImage() const {
65 if (this->hasCachedImage()) {
66 SKGPU_LOG_W("Intermingling makeImageSnapshot and asImage calls may produce "
67 "unexpected results. Please use either the old _or_ new API.");
68 }
69 return fImageView;
70 }
71
72 sk_sp<SkImage> Surface::onMakeTemporaryImage() {
73 if (this->hasCachedImage()) {
74 SKGPU_LOG_W("Intermingling makeImageSnapshot and makeTemporaryImage calls may produce "
75 "unexpected results. Please use either the old _or_ new API.");
76 }
77 return this->asImage();
78 }
79
80 sk_sp<Image> Surface::makeImageCopy(const SkIRect* subset, Mipmapped mipmapped) const {
81 if (this->hasCachedImage()) {
82 SKGPU_LOG_W("Intermingling makeImageSnapshot and asImage calls may produce "
83 "unexpected results. Please use either the old _or_ new API.");
84 }
85
86 SkIRect srcRect = subset ? *subset : SkIRect::MakeSize(this->imageInfo().dimensions());
87 // NOTE: Must copy through fDevice and not fImageView if the surface's texture is not sampleable
88 return fDevice->makeImageCopy(srcRect, Budgeted::kNo, mipmapped, SkBackingFit::kExact);
89 }
90
91 void Surface::onWritePixels(const SkPixmap& pixmap, int x, int y) {
92 fDevice->writePixels(pixmap, x, y);
93 }
94
95 bool Surface::onCopyOnWrite(ContentChangeMode) { return true; }
96
97 void Surface::onAsyncRescaleAndReadPixels(const SkImageInfo& info,
98 SkIRect srcRect,
99 RescaleGamma rescaleGamma,
100 RescaleMode rescaleMode,
101 ReadPixelsCallback callback,
102 ReadPixelsContext context) {
103 // Not supported for Graphite. Use Context::asyncRescaleAndReadPixels instead.
104 callback(context, nullptr);
105 }
106
107 void Surface::onAsyncRescaleAndReadPixelsYUV420(SkYUVColorSpace yuvColorSpace,
108 bool readAlpha,
109 sk_sp<SkColorSpace> dstColorSpace,
110 SkIRect srcRect,
111 SkISize dstSize,
112 RescaleGamma rescaleGamma,
113 RescaleMode rescaleMode,
114 ReadPixelsCallback callback,
115 ReadPixelsContext context) {
116 // Not supported for Graphite. Use Context::asyncRescaleAndReadPixelsYUV420 instead.
117 callback(context, nullptr);
118 }
119
120 sk_sp<const SkCapabilities> Surface::onCapabilities() {
121 return fDevice->recorder()->priv().caps()->capabilities();
122 }
123
124 TextureProxy* Surface::backingTextureProxy() const { return fDevice->target(); }
125
126 // Note, devices flushed with this method add their tasks to the provided drawContext's task list,
127 // but no last task is tracked. If no drawContext is provided, the task is added to the root task
128 // list and if the device is a scratch device, the last task is recorded.
129 void Surface::flushToDrawContext(DrawContext* drawContext) {
130 this->fDevice->flushPendingWork(drawContext);
131 }
132
133 sk_sp<Surface> Surface::Make(Recorder* recorder,
134 const SkImageInfo& info,
135 std::string_view label,
136 Budgeted budgeted,
137 Mipmapped mipmapped,
138 SkBackingFit backingFit,
139 const SkSurfaceProps* props,
140 LoadOp initialLoadOp,
141 bool registerWithRecorder) {
142 sk_sp<Device> device = Device::Make(recorder,
143 info,
144 budgeted,
145 mipmapped,
146 backingFit,
147 SkSurfacePropsCopyOrDefault(props),
148 initialLoadOp,
149 label,
150 registerWithRecorder);
151 if (!device) {
152 return nullptr;
153 }
154 // A non-budgeted surface should be fully instantiated before we return it
155 // to the client.
156 SkASSERT(budgeted == Budgeted::kYes || device->target()->isInstantiated());
157 return sk_make_sp<Surface>(std::move(device));
158 }
159
160 void Flush(sk_sp<SkSurface> surface) {
161 return Flush(surface.get());
162 }
163
164 void Flush(SkSurface* surface) {
165 if (!surface) {
166 return;
167 }
168 auto sb = asSB(surface);
169 if (!sb->isGraphiteBacked()) {
170 return;
171 }
172 auto gs = static_cast<Surface*>(surface);
173 gs->fDevice->flushPendingWork(/*drawContext=*/nullptr);
174 }
175
176 } // namespace skgpu::graphite
177
178 using namespace skgpu::graphite;
179
180 namespace {
181
182 bool validate_backend_texture(const Caps* caps,
183 const BackendTexture& texture,
184 const SkColorInfo& info) {
185 if (!texture.isValid() ||
186 texture.dimensions().width() <= 0 ||
187 texture.dimensions().height() <= 0) {
188 return false;
189 }
190
191 if (!SkColorInfoIsValid(info)) {
192 return false;
193 }
194
195 if (!caps->isRenderable(texture.info())) {
196 return false;
197 }
198
199 return caps->areColorTypeAndTextureInfoCompatible(info.colorType(), texture.info());
200 }
201
202 } // anonymous namespace
203
204 namespace SkSurfaces {
205 sk_sp<SkImage> AsImage(sk_sp<const SkSurface> surface) {
206 if (!surface) {
207 return nullptr;
208 }
209 auto sb = asConstSB(surface.get());
210 if (!sb->isGraphiteBacked()) {
211 return nullptr;
212 }
213 auto gs = static_cast<const Surface*>(surface.get());
214 return gs->asImage();
215 }
216
217 sk_sp<SkImage> AsImageCopy(sk_sp<const SkSurface> surface,
218 const SkIRect* subset,
219 skgpu::Mipmapped mipmapped) {
220 if (!surface) {
221 return nullptr;
222 }
223 auto sb = asConstSB(surface.get());
224 if (!sb->isGraphiteBacked()) {
225 return nullptr;
226 }
227 auto gs = static_cast<const Surface*>(surface.get());
228 return gs->makeImageCopy(subset, mipmapped);
229 }
230
231 sk_sp<SkSurface> RenderTarget(Recorder* recorder,
232 const SkImageInfo& info,
233 skgpu::Mipmapped mipmapped,
234 const SkSurfaceProps* props,
235 std::string_view label) {
236 if (label.empty()) {
237 label = "SkSurfaceRenderTarget";
238 }
239 // The client is getting the ref on this surface so it must be unbudgeted.
240 return skgpu::graphite::Surface::Make(recorder, info, label, skgpu::Budgeted::kNo,
241 mipmapped, SkBackingFit::kExact, props);
242 }
243
244 sk_sp<SkSurface> WrapBackendTexture(Recorder* recorder,
245 const BackendTexture& backendTex,
246 sk_sp<SkColorSpace> cs,
247 const SkSurfaceProps* props,
248 TextureReleaseProc releaseP,
249 ReleaseContext releaseC,
250 std::string_view label) {
251 // TODO(476410476): When the SkColorType-taking WrapBackendTexture goes away, we can move its
252 // function body here and construct the SkColorInfo from this getDefaultColorType call.
253 SkColorType colorType = recorder->priv().caps()->getDefaultColorType(backendTex.info());
254 return WrapBackendTexture(recorder, backendTex, colorType, std::move(cs), props,
255 releaseP, releaseC, label);
256 }
257
258 sk_sp<SkSurface> WrapBackendTexture(Recorder* recorder,
259 const BackendTexture& backendTex,
260 SkColorType ct,
261 sk_sp<SkColorSpace> cs,
262 const SkSurfaceProps* props,
263 TextureReleaseProc releaseP,
264 ReleaseContext releaseC,
265 std::string_view label) {
266 auto releaseHelper = skgpu::RefCntedCallback::Make(releaseP, releaseC);
267
268 if (!recorder) {
269 return nullptr;
270 }
271
272 const Caps* caps = recorder->priv().caps();
273
274 SkColorInfo info(ct, kPremul_SkAlphaType, std::move(cs));
275
276 if (!validate_backend_texture(caps, backendTex, info)) {
277 SKGPU_LOG_E("validate_backend_texture failed: backendTex.info = %s; colorType = %d",
278 backendTex.info().toString().c_str(),
279 info.colorType());
280 return nullptr;
281 }
282
283 if (label.empty()) {
284 label = "SkSurfaceWrappedTexture";
285 }
286
287 sk_sp<Texture> texture =
288 recorder->priv().resourceProvider()->createWrappedTexture(backendTex, label);
289 if (!texture) {
290 return nullptr;
291 }
292 texture->setReleaseCallback(std::move(releaseHelper));
293
294 sk_sp<TextureProxy> proxy = TextureProxy::Wrap(std::move(texture));
295 SkISize deviceSize = proxy->dimensions();
296 // Use kLoad for this device to preserve the existing contents of the wrapped backend texture.
297 sk_sp<Device> device = Device::Make(recorder,
298 std::move(proxy),
299 deviceSize,
300 info,
301 SkSurfacePropsCopyOrDefault(props),
302 LoadOp::kLoad);
303 return device ? sk_make_sp<Surface>(std::move(device)) : nullptr;
304 }
305
306 } // namespace SkSurfaces