Debian Codegraph @

src/gpu/ganesh/image/SkImage_Ganesh.cpp

raw
1 /*
2 * Copyright 2012 Google Inc.
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/ganesh/image/SkImage_Ganesh.h"
9
10 #include "include/core/SkColorSpace.h"
11 #include "include/core/SkImage.h"
12 #include "include/core/SkImageInfo.h"
13 #include "include/core/SkRect.h"
14 #include "include/core/SkSize.h"
15 #include "include/core/SkSurface.h"
16 #include "include/gpu/GpuTypes.h"
17 #include "include/gpu/ganesh/GrBackendSurface.h"
18 #include "include/gpu/ganesh/GrDirectContext.h"
19 #include "include/gpu/ganesh/GrRecordingContext.h"
20 #include "include/gpu/ganesh/GrTypes.h"
21 #include "include/private/base/SkAssert.h"
22 #include "include/private/gpu/ganesh/GrImageContext.h"
23 #include "include/private/gpu/ganesh/GrTypesPriv.h"
24 #include "src/gpu/SkBackingFit.h"
25 #include "src/gpu/ganesh/GrCaps.h"
26 #include "src/gpu/ganesh/GrColorInfo.h"
27 #include "src/gpu/ganesh/GrColorSpaceXform.h"
28 #include "src/gpu/ganesh/GrDirectContextPriv.h"
29 #include "src/gpu/ganesh/GrFragmentProcessor.h"
30 #include "src/gpu/ganesh/GrImageContextPriv.h"
31 #include "src/gpu/ganesh/GrImageInfo.h"
32 #include "src/gpu/ganesh/GrRenderTargetProxy.h"
33 #include "src/gpu/ganesh/GrRenderTask.h"
34 #include "src/gpu/ganesh/GrSurfaceProxy.h"
35 #include "src/gpu/ganesh/GrTexture.h"
36 #include "src/gpu/ganesh/GrTextureProxy.h"
37 #include "src/gpu/ganesh/SkGr.h"
38 #include "src/gpu/ganesh/SurfaceContext.h"
39 #include "src/gpu/ganesh/SurfaceDrawContext.h"
40 #include "src/gpu/ganesh/SurfaceFillContext.h"
41 #include "src/gpu/ganesh/effects/GrTextureEffect.h"
42 #include "src/gpu/ganesh/image/GrImageUtils.h"
43 #include "src/image/SkImage_Base.h"
44
45 #include <cstddef>
46 #include <utility>
47
48 class SkMatrix;
49 enum SkColorType : int;
50 enum class SkTileMode;
51
52 inline SkImage_Ganesh::ProxyChooser::ProxyChooser(sk_sp<GrSurfaceProxy> stableProxy)
53 : fStableProxy(std::move(stableProxy)) {
54 SkASSERT(fStableProxy);
55 }
56
57 inline SkImage_Ganesh::ProxyChooser::ProxyChooser(sk_sp<GrSurfaceProxy> stableProxy,
58 sk_sp<GrSurfaceProxy> volatileProxy,
59 sk_sp<GrRenderTask> copyTask,
60 int volatileProxyTargetCount)
61 : fStableProxy(std::move(stableProxy))
62 , fVolatileProxy(std::move(volatileProxy))
63 , fVolatileToStableCopyTask(std::move(copyTask))
64 , fVolatileProxyTargetCount(volatileProxyTargetCount) {
65 SkASSERT(fStableProxy);
66 SkASSERT(fVolatileProxy);
67 SkASSERT(fVolatileToStableCopyTask);
68 }
69
70 inline SkImage_Ganesh::ProxyChooser::~ProxyChooser() {
71 // The image is being destroyed. If there is a stable copy proxy but we've been able to use
72 // the volatile proxy for all requests then we can skip the copy.
73 if (fVolatileToStableCopyTask) {
74 fVolatileToStableCopyTask->makeSkippable();
75 }
76 }
77
78 inline sk_sp<GrSurfaceProxy> SkImage_Ganesh::ProxyChooser::chooseProxy(
79 GrRecordingContext* context,
80 GrRenderTargetProxy* targetProxy) {
81 SkAutoSpinlock hold(fLock);
82 if (fVolatileProxy) {
83 SkASSERT(fVolatileProxyTargetCount <= fVolatileProxy->getTaskTargetCount());
84 // If this image is used off the direct context it originated on, i.e. on a recording-only
85 // context, we don't know how the recording context's actions are ordered WRT direct context
86 // actions until the recording context's DAG is imported into the direct context.
87 if (context->asDirectContext() &&
88 fVolatileProxyTargetCount == fVolatileProxy->getTaskTargetCount()) {
89 // We cannot draw the volatile proxy into itself.
90 if (!targetProxy ||
91 targetProxy->underlyingUniqueID() != fVolatileProxy->underlyingUniqueID()) {
92 return fVolatileProxy;
93 }
94 }
95 fVolatileProxy.reset();
96 fVolatileToStableCopyTask.reset();
97 return fStableProxy;
98 }
99 return fStableProxy;
100 }
101
102 inline sk_sp<GrSurfaceProxy> SkImage_Ganesh::ProxyChooser::switchToStableProxy() {
103 SkAutoSpinlock hold(fLock);
104 fVolatileProxy.reset();
105 fVolatileToStableCopyTask.reset();
106 return fStableProxy;
107 }
108
109 inline sk_sp<GrSurfaceProxy> SkImage_Ganesh::ProxyChooser::makeVolatileProxyStable() {
110 SkAutoSpinlock hold(fLock);
111 if (fVolatileProxy) {
112 fStableProxy = std::move(fVolatileProxy);
113 fVolatileToStableCopyTask->makeSkippable();
114 fVolatileToStableCopyTask.reset();
115 }
116 return fStableProxy;
117 }
118
119 inline bool SkImage_Ganesh::ProxyChooser::surfaceMustCopyOnWrite(
120 GrSurfaceProxy* surfaceProxy) const {
121 SkAutoSpinlock hold(fLock);
122 return surfaceProxy->underlyingUniqueID() == fStableProxy->underlyingUniqueID();
123 }
124
125 inline size_t SkImage_Ganesh::ProxyChooser::gpuMemorySize() const {
126 SkAutoSpinlock hold(fLock);
127 size_t size = fStableProxy->gpuMemorySize();
128 if (fVolatileProxy) {
129 SkASSERT(fVolatileProxy->gpuMemorySize() == size);
130 }
131 return size;
132 }
133
134 inline skgpu::Mipmapped SkImage_Ganesh::ProxyChooser::mipmapped() const {
135 SkAutoSpinlock hold(fLock);
136 skgpu::Mipmapped mipmapped = fStableProxy->asTextureProxy()->mipmapped();
137 if (fVolatileProxy) {
138 SkASSERT(fVolatileProxy->asTextureProxy()->mipmapped() == mipmapped);
139 }
140 return mipmapped;
141 }
142
143 inline skgpu::Protected SkImage_Ganesh::ProxyChooser::isProtected() const {
144 SkAutoSpinlock hold(fLock);
145 skgpu::Protected isProtected = fStableProxy->asTextureProxy()->isProtected();
146 if (fVolatileProxy) {
147 SkASSERT(fVolatileProxy->asTextureProxy()->isProtected() == isProtected);
148 }
149 return isProtected;
150 }
151
152 #ifdef SK_DEBUG
153 inline const GrBackendFormat& SkImage_Ganesh::ProxyChooser::backendFormat() {
154 SkAutoSpinlock hold(fLock);
155 if (fVolatileProxy) {
156 SkASSERT(fVolatileProxy->backendFormat() == fStableProxy->backendFormat());
157 }
158 return fStableProxy->backendFormat();
159 }
160 #endif
161
162 //////////////////////////////////////////////////////////////////////////////
163
164 SkImage_Ganesh::SkImage_Ganesh(sk_sp<GrImageContext> context,
165 uint32_t uniqueID,
166 GrSurfaceProxyView view,
167 SkColorInfo info)
168 : INHERITED(std::move(context),
169 SkImageInfo::Make(view.proxy()->backingStoreDimensions(), std::move(info)),
170 uniqueID)
171 , fChooser(view.detachProxy())
172 , fSwizzle(view.swizzle())
173 , fOrigin(view.origin()) {
174 #ifdef SK_DEBUG
175 const GrBackendFormat& format = fChooser.backendFormat();
176 const GrCaps* caps = this->context()->priv().caps();
177 GrColorType grCT = SkColorTypeToGrColorType(this->colorType());
178 SkASSERT(caps->isFormatCompressed(format) ||
179 caps->areColorTypeAndFormatCompatible(grCT, format));
180 #endif
181 }
182
183 SkImage_Ganesh::SkImage_Ganesh(sk_sp<GrDirectContext> dContext,
184 GrSurfaceProxyView volatileSrc,
185 sk_sp<GrSurfaceProxy> stableCopy,
186 sk_sp<GrRenderTask> copyTask,
187 int volatileSrcTargetCount,
188 SkColorInfo info)
189 : INHERITED(
190 std::move(dContext),
191 SkImageInfo::Make(volatileSrc.proxy()->backingStoreDimensions(), std::move(info)),
192 kNeedNewImageUniqueID)
193 , fChooser(std::move(stableCopy),
194 volatileSrc.detachProxy(),
195 std::move(copyTask),
196 volatileSrcTargetCount)
197 , fSwizzle(volatileSrc.swizzle())
198 , fOrigin(volatileSrc.origin()) {
199 #ifdef SK_DEBUG
200 const GrBackendFormat& format = fChooser.backendFormat();
201 const GrCaps* caps = this->context()->priv().caps();
202 GrColorType grCT = SkColorTypeToGrColorType(this->colorType());
203 SkASSERT(caps->isFormatCompressed(format) ||
204 caps->areColorTypeAndFormatCompatible(grCT, format));
205 #endif
206 }
207
208 sk_sp<SkImage> SkImage_Ganesh::MakeWithVolatileSrc(sk_sp<GrRecordingContext> rContext,
209 GrSurfaceProxyView volatileSrc,
210 SkColorInfo colorInfo) {
211 SkASSERT(rContext);
212 SkASSERT(volatileSrc);
213 SkASSERT(volatileSrc.proxy()->asTextureProxy());
214 skgpu::Mipmapped mm = volatileSrc.proxy()->asTextureProxy()->mipmapped();
215 sk_sp<GrRenderTask> copyTask;
216 auto copy = GrSurfaceProxy::Copy(rContext.get(),
217 volatileSrc.refProxy(),
218 volatileSrc.origin(),
219 mm,
220 SkBackingFit::kExact,
221 skgpu::Budgeted::kYes,
222 /*label=*/"ImageGpu_MakeWithVolatileSrc",
223 &copyTask);
224 if (!copy) {
225 return nullptr;
226 }
227 // We only attempt to make a dual-proxy image on a direct context. This optimziation requires
228 // knowing how things are ordered and recording-only contexts are not well ordered WRT other
229 // recording contexts.
230 if (auto direct = sk_ref_sp(rContext->asDirectContext())) {
231 int targetCount = volatileSrc.proxy()->getTaskTargetCount();
232 return sk_sp<SkImage>(new SkImage_Ganesh(std::move(direct),
233 std::move(volatileSrc),
234 std::move(copy),
235 std::move(copyTask),
236 targetCount,
237 std::move(colorInfo)));
238 }
239 GrSurfaceProxyView copyView(std::move(copy), volatileSrc.origin(), volatileSrc.swizzle());
240 return sk_make_sp<SkImage_Ganesh>(
241 std::move(rContext), kNeedNewImageUniqueID, std::move(copyView), std::move(colorInfo));
242 }
243
244 SkImage_Ganesh::~SkImage_Ganesh() = default;
245
246 bool SkImage_Ganesh::surfaceMustCopyOnWrite(GrSurfaceProxy* surfaceProxy) const {
247 return fChooser.surfaceMustCopyOnWrite(surfaceProxy);
248 }
249
250 bool SkImage_Ganesh::onHasMipmaps() const { return fChooser.mipmapped() == skgpu::Mipmapped::kYes; }
251
252 bool SkImage_Ganesh::onIsProtected() const {
253 return fChooser.isProtected() == skgpu::Protected::kYes;
254 }
255
256 GrSemaphoresSubmitted SkImage_Ganesh::flush(GrDirectContext* dContext,
257 const GrFlushInfo& info) const {
258 if (!fContext->priv().matches(dContext) || dContext->abandoned()) {
259 if (info.fSubmittedProc) {
260 info.fSubmittedProc(info.fSubmittedContext, false);
261 }
262 if (info.fFinishedProc) {
263 info.fFinishedProc(info.fFinishedContext);
264 }
265 return GrSemaphoresSubmitted::kNo;
266 }
267
268 sk_sp<GrSurfaceProxy> proxy = fChooser.chooseProxy(dContext, nullptr);
269 return dContext->priv().flushSurface(
270 proxy.get(), SkSurfaces::BackendSurfaceAccess::kNoAccess, info);
271 }
272
273 bool SkImage_Ganesh::getExistingBackendTexture(GrBackendTexture* outTexture,
274 bool flushPendingGrContextIO,
275 GrSurfaceOrigin* origin) const {
276 auto direct = fContext->asDirectContext();
277 if (!direct) {
278 // This image was created with a DDL context and cannot be instantiated.
279 return false;
280 }
281 if (direct->abandoned()) {
282 return false;
283 }
284
285 // We don't know how client's use of the texture will be ordered WRT Skia's. Ensure the
286 // texture seen by the client won't be mutated by a SkSurface.
287 sk_sp<GrSurfaceProxy> proxy = fChooser.switchToStableProxy();
288
289 if (!proxy->isInstantiated()) {
290 auto resourceProvider = direct->priv().resourceProvider();
291
292 if (!proxy->instantiate(resourceProvider)) {
293 return false;
294 }
295 }
296
297 GrTexture* texture = proxy->peekTexture();
298 if (!texture) {
299 return false;
300 }
301 if (flushPendingGrContextIO) {
302 direct->priv().flushSurface(proxy.get());
303 }
304 if (origin) {
305 *origin = fOrigin;
306 }
307 if (outTexture) {
308 *outTexture = texture->getBackendTexture();
309 }
310 return true;
311 }
312
313 size_t SkImage_Ganesh::textureSize() const { return fChooser.gpuMemorySize(); }
314
315 sk_sp<SkImage> SkImage_Ganesh::onMakeColorTypeAndColorSpace(GrDirectContext* dContext,
316 SkColorType targetCT,
317 sk_sp<SkColorSpace> targetCS) const {
318 SkColorInfo info(targetCT, this->alphaType(), std::move(targetCS));
319 if (!fContext->priv().matches(dContext)) {
320 return nullptr;
321 }
322
323 sk_sp<GrSurfaceProxy> proxy = fChooser.chooseProxy(dContext, nullptr);
324
325 auto sfc = dContext->priv().makeSFCWithFallback(GrImageInfo(info, this->dimensions()),
326 SkBackingFit::kExact,
327 /* sampleCount= */ 1,
328 skgpu::Mipmapped::kNo,
329 proxy->isProtected());
330 if (!sfc) {
331 return nullptr;
332 }
333 // We respecify info's CT because we called MakeWithFallback.
334 auto ct = GrColorTypeToSkColorType(sfc->colorInfo().colorType());
335 info = info.makeColorType(ct);
336
337 // Draw this image's texture into the SFC.
338 auto [view, _] = skgpu::ganesh::AsView(dContext, this, skgpu::Mipmapped(this->hasMipmaps()),
339 sfc->asRenderTargetProxy());
340 auto texFP = GrTextureEffect::Make(std::move(view), this->alphaType());
341 auto colorFP =
342 GrColorSpaceXformEffect::Make(std::move(texFP), this->imageInfo().colorInfo(), info);
343 sfc->fillWithFP(std::move(colorFP));
344
345 return sk_make_sp<SkImage_Ganesh>(
346 sk_ref_sp(dContext), kNeedNewImageUniqueID, sfc->readSurfaceView(), std::move(info));
347 }
348
349 sk_sp<SkImage> SkImage_Ganesh::onReinterpretColorSpace(sk_sp<SkColorSpace> newCS) const {
350 // It doesn't seem worth the complexity of trying to share the ProxyChooser among multiple
351 // images. Just fall back to the stable copy.
352 GrSurfaceProxyView view(fChooser.switchToStableProxy(), fOrigin, fSwizzle);
353 return sk_make_sp<SkImage_Ganesh>(
354 fContext,
355 kNeedNewImageUniqueID,
356 std::move(view),
357 this->imageInfo().colorInfo().makeColorSpace(std::move(newCS)));
358 }
359
360 void SkImage_Ganesh::onAsyncRescaleAndReadPixels(const SkImageInfo& info,
361 SkIRect srcRect,
362 RescaleGamma rescaleGamma,
363 RescaleMode rescaleMode,
364 ReadPixelsCallback callback,
365 ReadPixelsContext context) const {
366 auto dContext = fContext->asDirectContext();
367 if (!dContext) {
368 // DDL TODO: buffer up the readback so it occurs when the DDL is drawn?
369 callback(context, nullptr);
370 return;
371 }
372 auto ctx = dContext->priv().makeSC(this->makeView(dContext, nullptr),
373 this->imageInfo().colorInfo());
374 if (!ctx) {
375 callback(context, nullptr);
376 return;
377 }
378 ctx->asyncRescaleAndReadPixels(
379 dContext, info, srcRect, rescaleGamma, rescaleMode, callback, context);
380 }
381
382 void SkImage_Ganesh::onAsyncRescaleAndReadPixelsYUV420(SkYUVColorSpace yuvColorSpace,
383 bool readAlpha,
384 sk_sp<SkColorSpace> dstColorSpace,
385 SkIRect srcRect,
386 SkISize dstSize,
387 RescaleGamma rescaleGamma,
388 RescaleMode rescaleMode,
389 ReadPixelsCallback callback,
390 ReadPixelsContext context) const {
391 auto dContext = fContext->asDirectContext();
392 if (!dContext) {
393 // DDL TODO: buffer up the readback so it occurs when the DDL is drawn?
394 callback(context, nullptr);
395 return;
396 }
397 auto ctx = dContext->priv().makeSC(this->makeView(dContext, nullptr),
398 this->imageInfo().colorInfo());
399 if (!ctx) {
400 callback(context, nullptr);
401 return;
402 }
403 ctx->asyncRescaleAndReadPixelsYUV420(dContext,
404 yuvColorSpace,
405 readAlpha,
406 std::move(dstColorSpace),
407 srcRect,
408 dstSize,
409 rescaleGamma,
410 rescaleMode,
411 callback,
412 context);
413 }
414
415 void SkImage_Ganesh::generatingSurfaceIsDeleted() { fChooser.makeVolatileProxyStable(); }
416
417 std::tuple<GrSurfaceProxyView, GrColorType> SkImage_Ganesh::asView(
418 GrRecordingContext* recordingContext,
419 skgpu::Mipmapped mipmapped,
420 GrImageTexGenPolicy policy,
421 GrRenderTargetProxy* targetProxy) const {
422 if (!fContext->priv().matches(recordingContext)) {
423 return {};
424 }
425 if (policy != GrImageTexGenPolicy::kDraw) {
426 return {skgpu::ganesh::CopyView(recordingContext,
427 this->makeView(recordingContext, targetProxy),
428 mipmapped,
429 policy,
430 /*label=*/"SkImageGpu_AsView"),
431 SkColorTypeToGrColorType(this->colorType())};
432 }
433 GrSurfaceProxyView view = this->makeView(recordingContext, targetProxy);
434 GrColorType ct = SkColorTypeToGrColorType(this->colorType());
435 if (mipmapped == skgpu::Mipmapped::kYes) {
436 view = skgpu::ganesh::FindOrMakeCachedMipmappedView(recordingContext, std::move(view),
437 this->uniqueID());
438 }
439 return {std::move(view), ct};
440 }
441
442 std::unique_ptr<GrFragmentProcessor> SkImage_Ganesh::asFragmentProcessor(
443 skgpu::ganesh::SurfaceDrawContext* sdc,
444 SkSamplingOptions sampling,
445 const SkTileMode tileModes[2],
446 const SkMatrix& m,
447 const SkRect* subset,
448 const SkRect* domain) const {
449 GrRecordingContext* rContext = sdc->recordingContext();
450 if (!fContext->priv().matches(rContext)) {
451 return {};
452 }
453 auto mm =
454 sampling.mipmap == SkMipmapMode::kNone ? skgpu::Mipmapped::kNo : skgpu::Mipmapped::kYes;
455 return skgpu::ganesh::MakeFragmentProcessorFromView(
456 rContext,
457 std::get<0>(skgpu::ganesh::AsView(rContext, this, mm, sdc->asRenderTargetProxy())),
458 this->alphaType(),
459 sampling,
460 tileModes,
461 m,
462 subset,
463 domain);
464 }
465
466 GrSurfaceProxyView SkImage_Ganesh::makeView(GrRecordingContext* rContext,
467 GrRenderTargetProxy* targetProxy) const {
468 return {fChooser.chooseProxy(rContext, targetProxy), fOrigin, fSwizzle};
469 }