• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/image/SkImage_Gpu.h"
9 
10 #include "include/core/SkCanvas.h"
11 #include "include/gpu/GrBackendSurface.h"
12 #include "include/gpu/GrDirectContext.h"
13 #include "include/gpu/GrRecordingContext.h"
14 #include "include/gpu/GrYUVABackendTextures.h"
15 #include "include/private/SkImageInfoPriv.h"
16 #include "src/core/SkAutoPixmapStorage.h"
17 #include "src/core/SkBitmapCache.h"
18 #include "src/core/SkMipmap.h"
19 #include "src/core/SkScopeExit.h"
20 #include "src/core/SkTraceEvent.h"
21 #include "src/gpu/GrAHardwareBufferImageGenerator.h"
22 #include "src/gpu/GrAHardwareBufferUtils.h"
23 #include "src/gpu/GrBackendTextureImageGenerator.h"
24 #include "src/gpu/GrBackendUtils.h"
25 #include "src/gpu/GrCaps.h"
26 #include "src/gpu/GrColorSpaceXform.h"
27 #include "src/gpu/GrContextThreadSafeProxyPriv.h"
28 #include "src/gpu/GrDirectContextPriv.h"
29 #include "src/gpu/GrDrawingManager.h"
30 #include "src/gpu/GrGpu.h"
31 #include "src/gpu/GrImageContextPriv.h"
32 #include "src/gpu/GrImageInfo.h"
33 #include "src/gpu/GrProxyProvider.h"
34 #include "src/gpu/GrRecordingContextPriv.h"
35 #include "src/gpu/GrSemaphore.h"
36 #include "src/gpu/GrTexture.h"
37 #include "src/gpu/GrTextureProxy.h"
38 #include "src/gpu/GrTextureProxyPriv.h"
39 #include "src/gpu/GrYUVATextureProxies.h"
40 #include "src/gpu/SurfaceFillContext.h"
41 #include "src/gpu/effects/GrTextureEffect.h"
42 #include "src/gpu/gl/GrGLTexture.h"
43 
44 #include <cstddef>
45 #include <cstring>
46 #include <type_traits>
47 
ProxyChooser(sk_sp<GrSurfaceProxy> stableProxy)48 inline SkImage_Gpu::ProxyChooser::ProxyChooser(sk_sp<GrSurfaceProxy> stableProxy)
49         : fStableProxy(std::move(stableProxy)) {
50     SkASSERT(fStableProxy);
51 }
52 
ProxyChooser(sk_sp<GrSurfaceProxy> stableProxy,sk_sp<GrSurfaceProxy> volatileProxy,sk_sp<GrRenderTask> copyTask,int volatileProxyTargetCount)53 inline SkImage_Gpu::ProxyChooser::ProxyChooser(sk_sp<GrSurfaceProxy> stableProxy,
54                                                sk_sp<GrSurfaceProxy> volatileProxy,
55                                                sk_sp<GrRenderTask> copyTask,
56                                                int volatileProxyTargetCount)
57         : fStableProxy(std::move(stableProxy))
58         , fVolatileProxy(std::move(volatileProxy))
59         , fVolatileToStableCopyTask(std::move(copyTask))
60         , fVolatileProxyTargetCount(volatileProxyTargetCount) {
61     SkASSERT(fStableProxy);
62     SkASSERT(fVolatileProxy);
63     SkASSERT(fVolatileToStableCopyTask);
64 }
65 
~ProxyChooser()66 inline SkImage_Gpu::ProxyChooser::~ProxyChooser() {
67     // The image is being destroyed. If there is a stable copy proxy but we've been able to use
68     // the volatile proxy for all requests then we can skip the copy.
69     if (fVolatileToStableCopyTask) {
70         fVolatileToStableCopyTask->makeSkippable();
71     }
72 }
73 
chooseProxy(GrRecordingContext * context)74 inline sk_sp<GrSurfaceProxy> SkImage_Gpu::ProxyChooser::chooseProxy(GrRecordingContext* context) {
75     SkAutoSpinlock hold(fLock);
76     if (fVolatileProxy) {
77         SkASSERT(fVolatileProxyTargetCount <= fVolatileProxy->getTaskTargetCount());
78         // If this image is used off the direct context it originated on, i.e. on a recording-only
79         // context, we don't know how the recording context's actions are ordered WRT direct context
80         // actions until the recording context's DAG is imported into the direct context.
81         if (context->asDirectContext() &&
82             fVolatileProxyTargetCount == fVolatileProxy->getTaskTargetCount()) {
83             return fVolatileProxy;
84         }
85         fVolatileProxy.reset();
86         fVolatileToStableCopyTask.reset();
87         return fStableProxy;
88     }
89     return fStableProxy;
90 }
91 
switchToStableProxy()92 inline sk_sp<GrSurfaceProxy> SkImage_Gpu::ProxyChooser::switchToStableProxy() {
93     SkAutoSpinlock hold(fLock);
94     fVolatileProxy.reset();
95     fVolatileToStableCopyTask.reset();
96     return fStableProxy;
97 }
98 
makeVolatileProxyStable()99 inline sk_sp<GrSurfaceProxy> SkImage_Gpu::ProxyChooser::makeVolatileProxyStable() {
100     SkAutoSpinlock hold(fLock);
101     if (fVolatileProxy) {
102         fStableProxy = std::move(fVolatileProxy);
103         fVolatileToStableCopyTask->makeSkippable();
104         fVolatileToStableCopyTask.reset();
105     }
106     return fStableProxy;
107 }
108 
surfaceMustCopyOnWrite(GrSurfaceProxy * surfaceProxy) const109 inline bool SkImage_Gpu::ProxyChooser::surfaceMustCopyOnWrite(GrSurfaceProxy* surfaceProxy) const {
110     SkAutoSpinlock hold(fLock);
111     return surfaceProxy->underlyingUniqueID() == fStableProxy->underlyingUniqueID();
112 }
113 
gpuMemorySize() const114 inline size_t SkImage_Gpu::ProxyChooser::gpuMemorySize() const {
115     SkAutoSpinlock hold(fLock);
116     size_t size = fStableProxy->gpuMemorySize();
117     if (fVolatileProxy) {
118         SkASSERT(fVolatileProxy->gpuMemorySize() == size);
119     }
120     return size;
121 }
122 
mipmapped() const123 inline GrMipmapped SkImage_Gpu::ProxyChooser::mipmapped() const {
124     SkAutoSpinlock hold(fLock);
125     GrMipmapped mipmapped = fStableProxy->asTextureProxy()->mipmapped();
126     if (fVolatileProxy) {
127         SkASSERT(fVolatileProxy->asTextureProxy()->mipmapped() == mipmapped);
128     }
129     return mipmapped;
130 }
131 
132 #ifdef SK_DEBUG
backendFormat()133 inline GrBackendFormat SkImage_Gpu::ProxyChooser::backendFormat() {
134     SkAutoSpinlock hold(fLock);
135     if (fVolatileProxy) {
136         SkASSERT(fVolatileProxy->backendFormat() == fStableProxy->backendFormat());
137     }
138     return fStableProxy->backendFormat();
139 }
140 #endif
141 
142 //////////////////////////////////////////////////////////////////////////////
143 
SkImage_Gpu(sk_sp<GrImageContext> context,uint32_t uniqueID,GrSurfaceProxyView view,SkColorInfo info)144 SkImage_Gpu::SkImage_Gpu(sk_sp<GrImageContext> context,
145                          uint32_t uniqueID,
146                          GrSurfaceProxyView view,
147                          SkColorInfo info)
148         : INHERITED(std::move(context),
149                     SkImageInfo::Make(view.proxy()->backingStoreDimensions(), std::move(info)),
150                     uniqueID)
151         , fChooser(view.detachProxy())
152         , fSwizzle(view.swizzle())
153         , fOrigin(view.origin()) {
154 #ifdef SK_DEBUG
155     const GrBackendFormat& format = fChooser.backendFormat();
156     const GrCaps* caps = this->context()->priv().caps();
157     GrColorType grCT = SkColorTypeToGrColorType(this->colorType());
158     SkASSERT(caps->isFormatCompressed(format) ||
159              caps->areColorTypeAndFormatCompatible(grCT, format));
160 #endif
161 }
162 
SkImage_Gpu(sk_sp<GrDirectContext> dContext,GrSurfaceProxyView volatileSrc,sk_sp<GrSurfaceProxy> stableCopy,sk_sp<GrRenderTask> copyTask,int volatileSrcTargetCount,SkColorInfo info)163 SkImage_Gpu::SkImage_Gpu(sk_sp<GrDirectContext> dContext,
164                          GrSurfaceProxyView volatileSrc,
165                          sk_sp<GrSurfaceProxy> stableCopy,
166                          sk_sp<GrRenderTask> copyTask,
167                          int volatileSrcTargetCount,
168                          SkColorInfo info)
169         : INHERITED(std::move(dContext),
170                     SkImageInfo::Make(volatileSrc.proxy()->backingStoreDimensions(),
171                                       std::move(info)),
172                     kNeedNewImageUniqueID)
173         , fChooser(std::move(stableCopy),
174                    volatileSrc.detachProxy(),
175                    std::move(copyTask),
176                    volatileSrcTargetCount)
177         , fSwizzle(volatileSrc.swizzle())
178         , fOrigin(volatileSrc.origin()) {
179 #ifdef SK_DEBUG
180     const GrBackendFormat& format = fChooser.backendFormat();
181     const GrCaps* caps = this->context()->priv().caps();
182     GrColorType grCT = SkColorTypeToGrColorType(this->colorType());
183     SkASSERT(caps->isFormatCompressed(format) ||
184              caps->areColorTypeAndFormatCompatible(grCT, format));
185 #endif
186 }
187 
MakeWithVolatileSrc(sk_sp<GrRecordingContext> rContext,GrSurfaceProxyView volatileSrc,SkColorInfo colorInfo)188 sk_sp<SkImage> SkImage_Gpu::MakeWithVolatileSrc(sk_sp<GrRecordingContext> rContext,
189                                                 GrSurfaceProxyView volatileSrc,
190                                                 SkColorInfo colorInfo) {
191     SkASSERT(rContext);
192     SkASSERT(volatileSrc);
193     SkASSERT(volatileSrc.proxy()->asTextureProxy());
194     GrMipmapped mm = volatileSrc.proxy()->asTextureProxy()->mipmapped();
195     sk_sp<GrRenderTask> copyTask;
196     auto copy = GrSurfaceProxy::Copy(rContext.get(),
197                                      volatileSrc.refProxy(),
198                                      volatileSrc.origin(),
199                                      mm,
200                                      SkBackingFit::kExact,
201                                      SkBudgeted::kYes,
202                                      &copyTask);
203     if (!copy) {
204         return nullptr;
205     }
206     // We only attempt to make a dual-proxy image on a direct context. This optimziation requires
207     // knowing how things are ordered and recording-only contexts are not well ordered WRT other
208     // recording contexts.
209     if (auto direct = sk_ref_sp(rContext->asDirectContext())) {
210         int targetCount = volatileSrc.proxy()->getTaskTargetCount();
211         return sk_sp<SkImage>(new SkImage_Gpu(std::move(direct),
212                                               std::move(volatileSrc),
213                                               std::move(copy),
214                                               std::move(copyTask),
215                                               targetCount,
216                                               std::move(colorInfo)));
217     }
218     GrSurfaceProxyView copyView(std::move(copy), volatileSrc.origin(), volatileSrc.swizzle());
219     return sk_make_sp<SkImage_Gpu>(std::move(rContext),
220                                    kNeedNewImageUniqueID,
221                                    std::move(copyView),
222                                    std::move(colorInfo));
223 }
224 
225 SkImage_Gpu::~SkImage_Gpu() = default;
226 
surfaceMustCopyOnWrite(GrSurfaceProxy * surfaceProxy) const227 bool SkImage_Gpu::surfaceMustCopyOnWrite(GrSurfaceProxy* surfaceProxy) const {
228     return fChooser.surfaceMustCopyOnWrite(surfaceProxy);
229 }
230 
onHasMipmaps() const231 bool SkImage_Gpu::onHasMipmaps() const { return fChooser.mipmapped() == GrMipmapped::kYes; }
232 
onFlush(GrDirectContext * dContext,const GrFlushInfo & info) const233 GrSemaphoresSubmitted SkImage_Gpu::onFlush(GrDirectContext* dContext,
234                                            const GrFlushInfo& info) const {
235     if (!fContext->priv().matches(dContext) || dContext->abandoned()) {
236         if (info.fSubmittedProc) {
237             info.fSubmittedProc(info.fSubmittedContext, false);
238         }
239         if (info.fFinishedProc) {
240             info.fFinishedProc(info.fFinishedContext);
241         }
242         return GrSemaphoresSubmitted::kNo;
243     }
244 
245     sk_sp<GrSurfaceProxy> proxy = fChooser.chooseProxy(dContext);
246     return dContext->priv().flushSurface(proxy.get(),
247                                          SkSurface::BackendSurfaceAccess::kNoAccess,
248                                          info);
249 }
250 
onGetBackendTexture(bool flushPendingGrContextIO,GrSurfaceOrigin * origin) const251 GrBackendTexture SkImage_Gpu::onGetBackendTexture(bool flushPendingGrContextIO,
252                                                   GrSurfaceOrigin* origin) const {
253     auto direct = fContext->asDirectContext();
254     if (!direct) {
255         // This image was created with a DDL context and cannot be instantiated.
256         return GrBackendTexture();  // invalid
257     }
258     if (direct->abandoned()) {
259         return GrBackendTexture();  // invalid;
260     }
261 
262     // We don't know how client's use of the texture will be ordered WRT Skia's. Ensure the
263     // texture seen by the client won't be mutated by a SkSurface.
264     sk_sp<GrSurfaceProxy> proxy = fChooser.switchToStableProxy();
265 
266     if (!proxy->isInstantiated()) {
267         auto resourceProvider = direct->priv().resourceProvider();
268 
269         if (!proxy->instantiate(resourceProvider)) {
270             return GrBackendTexture();  // invalid
271         }
272     }
273 
274     GrTexture* texture = proxy->peekTexture();
275     if (texture) {
276         if (flushPendingGrContextIO) {
277             direct->priv().flushSurface(proxy.get());
278         }
279         if (origin) {
280             *origin = fOrigin;
281         }
282         return texture->getBackendTexture();
283     }
284     return GrBackendTexture();  // invalid
285 }
286 
onTextureSize() const287 size_t SkImage_Gpu::onTextureSize() const { return fChooser.gpuMemorySize(); }
288 
onMakeColorTypeAndColorSpace(SkColorType targetCT,sk_sp<SkColorSpace> targetCS,GrDirectContext * dContext) const289 sk_sp<SkImage> SkImage_Gpu::onMakeColorTypeAndColorSpace(SkColorType targetCT,
290                                                          sk_sp<SkColorSpace> targetCS,
291                                                          GrDirectContext* dContext) const {
292     SkColorInfo info(targetCT, this->alphaType(), std::move(targetCS));
293     if (!fContext->priv().matches(dContext)) {
294         return nullptr;
295     }
296 
297     auto sfc = dContext->priv().makeSFCWithFallback(GrImageInfo(info, this->dimensions()),
298                                                     SkBackingFit::kExact);
299     if (!sfc) {
300         return nullptr;
301     }
302     // We respecify info's CT because we called MakeWithFallback.
303     auto ct = GrColorTypeToSkColorType(sfc->colorInfo().colorType());
304     info = info.makeColorType(ct);
305 
306     // Draw this image's texture into the SFC.
307     auto [view, _] = this->asView(dContext, GrMipmapped(this->hasMipmaps()));
308     auto texFP = GrTextureEffect::Make(std::move(view), this->alphaType());
309     auto colorFP = GrColorSpaceXformEffect::Make(std::move(texFP),
310                                                  this->imageInfo().colorInfo(),
311                                                  info);
312     sfc->fillWithFP(std::move(colorFP));
313 
314     return sk_make_sp<SkImage_Gpu>(sk_ref_sp(dContext),
315                                    kNeedNewImageUniqueID,
316                                    sfc->readSurfaceView(),
317                                    std::move(info));
318 }
319 
onReinterpretColorSpace(sk_sp<SkColorSpace> newCS) const320 sk_sp<SkImage> SkImage_Gpu::onReinterpretColorSpace(sk_sp<SkColorSpace> newCS) const {
321     // It doesn't seem worth the complexity of trying to share the ProxyChooser among multiple
322     // images. Just fall back to the stable copy.
323     GrSurfaceProxyView view(fChooser.switchToStableProxy(), fOrigin, fSwizzle);
324     return sk_make_sp<SkImage_Gpu>(fContext,
325                                    kNeedNewImageUniqueID,
326                                    std::move(view),
327                                    this->imageInfo().colorInfo().makeColorSpace(std::move(newCS)));
328 }
329 
onAsyncRescaleAndReadPixels(const SkImageInfo & info,const SkIRect & srcRect,RescaleGamma rescaleGamma,RescaleMode rescaleMode,ReadPixelsCallback callback,ReadPixelsContext context) const330 void SkImage_Gpu::onAsyncRescaleAndReadPixels(const SkImageInfo& info,
331                                               const SkIRect& srcRect,
332                                               RescaleGamma rescaleGamma,
333                                               RescaleMode rescaleMode,
334                                               ReadPixelsCallback callback,
335                                               ReadPixelsContext context) const {
336     auto dContext = fContext->asDirectContext();
337     if (!dContext) {
338         // DDL TODO: buffer up the readback so it occurs when the DDL is drawn?
339         callback(context, nullptr);
340         return;
341     }
342     auto ctx = dContext->priv().makeSC(this->makeView(dContext), this->imageInfo().colorInfo());
343     if (!ctx) {
344         callback(context, nullptr);
345         return;
346     }
347     ctx->asyncRescaleAndReadPixels(dContext, info, srcRect, rescaleGamma, rescaleMode,
348                                    callback, context);
349 }
350 
onAsyncRescaleAndReadPixelsYUV420(SkYUVColorSpace yuvColorSpace,sk_sp<SkColorSpace> dstColorSpace,const SkIRect & srcRect,const SkISize & dstSize,RescaleGamma rescaleGamma,RescaleMode rescaleMode,ReadPixelsCallback callback,ReadPixelsContext context) const351 void SkImage_Gpu::onAsyncRescaleAndReadPixelsYUV420(SkYUVColorSpace yuvColorSpace,
352                                                     sk_sp<SkColorSpace> dstColorSpace,
353                                                     const SkIRect& srcRect,
354                                                     const SkISize& dstSize,
355                                                     RescaleGamma rescaleGamma,
356                                                     RescaleMode rescaleMode,
357                                                     ReadPixelsCallback callback,
358                                                     ReadPixelsContext context) const {
359     auto dContext = fContext->asDirectContext();
360     if (!dContext) {
361         // DDL TODO: buffer up the readback so it occurs when the DDL is drawn?
362         callback(context, nullptr);
363         return;
364     }
365     auto ctx = dContext->priv().makeSC(this->makeView(dContext), this->imageInfo().colorInfo());
366     if (!ctx) {
367         callback(context, nullptr);
368         return;
369     }
370     ctx->asyncRescaleAndReadPixelsYUV420(dContext,
371                                          yuvColorSpace,
372                                          std::move(dstColorSpace),
373                                          srcRect,
374                                          dstSize,
375                                          rescaleGamma,
376                                          rescaleMode,
377                                          callback,
378                                          context);
379 }
380 
generatingSurfaceIsDeleted()381 void SkImage_Gpu::generatingSurfaceIsDeleted() { fChooser.makeVolatileProxyStable(); }
382 
383 ///////////////////////////////////////////////////////////////////////////////////////////////////
384 
new_wrapped_texture_common(GrRecordingContext * rContext,const GrBackendTexture & backendTex,GrColorType colorType,GrSurfaceOrigin origin,SkAlphaType at,sk_sp<SkColorSpace> colorSpace,GrWrapOwnership ownership,sk_sp<GrRefCntedCallback> releaseHelper)385 static sk_sp<SkImage> new_wrapped_texture_common(GrRecordingContext* rContext,
386                                                  const GrBackendTexture& backendTex,
387                                                  GrColorType colorType,
388                                                  GrSurfaceOrigin origin,
389                                                  SkAlphaType at,
390                                                  sk_sp<SkColorSpace> colorSpace,
391                                                  GrWrapOwnership ownership,
392                                                  sk_sp<GrRefCntedCallback> releaseHelper) {
393     if (!backendTex.isValid() || backendTex.width() <= 0 || backendTex.height() <= 0) {
394         return nullptr;
395     }
396 
397     GrProxyProvider* proxyProvider = rContext->priv().proxyProvider();
398     sk_sp<GrTextureProxy> proxy = proxyProvider->wrapBackendTexture(
399             backendTex, ownership, GrWrapCacheable::kNo, kRead_GrIOType, std::move(releaseHelper));
400     if (!proxy) {
401         return nullptr;
402     }
403 
404     GrSwizzle swizzle = rContext->priv().caps()->getReadSwizzle(proxy->backendFormat(), colorType);
405     GrSurfaceProxyView view(std::move(proxy), origin, swizzle);
406     SkColorInfo info(GrColorTypeToSkColorType(colorType), at, std::move(colorSpace));
407     return sk_make_sp<SkImage_Gpu>(sk_ref_sp(rContext),
408                                    kNeedNewImageUniqueID,
409                                    std::move(view),
410                                    std::move(info));
411 }
412 
MakeFromCompressedTexture(GrRecordingContext * rContext,const GrBackendTexture & tex,GrSurfaceOrigin origin,SkAlphaType at,sk_sp<SkColorSpace> cs,TextureReleaseProc releaseP,ReleaseContext releaseC)413 sk_sp<SkImage> SkImage::MakeFromCompressedTexture(GrRecordingContext* rContext,
414                                                   const GrBackendTexture& tex,
415                                                   GrSurfaceOrigin origin,
416                                                   SkAlphaType at,
417                                                   sk_sp<SkColorSpace> cs,
418                                                   TextureReleaseProc releaseP,
419                                                   ReleaseContext releaseC) {
420     auto releaseHelper = GrRefCntedCallback::Make(releaseP, releaseC);
421 
422     if (!rContext) {
423         return nullptr;
424     }
425 
426     const GrCaps* caps = rContext->priv().caps();
427 
428     if (!SkImage_GpuBase::ValidateCompressedBackendTexture(caps, tex, at)) {
429         return nullptr;
430     }
431 
432     GrProxyProvider* proxyProvider = rContext->priv().proxyProvider();
433     sk_sp<GrTextureProxy> proxy = proxyProvider->wrapCompressedBackendTexture(
434             tex, kBorrow_GrWrapOwnership, GrWrapCacheable::kNo, std::move(releaseHelper));
435     if (!proxy) {
436         return nullptr;
437     }
438 
439     CompressionType type = GrBackendFormatToCompressionType(tex.getBackendFormat());
440     SkColorType ct = GrCompressionTypeToSkColorType(type);
441 
442     GrSurfaceProxyView view(std::move(proxy), origin, GrSwizzle::RGBA());
443     return sk_make_sp<SkImage_Gpu>(sk_ref_sp(rContext),
444                                    kNeedNewImageUniqueID,
445                                    std::move(view),
446                                    SkColorInfo(ct, at, std::move(cs)));
447 }
448 
MakeFromTexture(GrRecordingContext * rContext,const GrBackendTexture & tex,GrSurfaceOrigin origin,SkColorType ct,SkAlphaType at,sk_sp<SkColorSpace> cs,TextureReleaseProc releaseP,ReleaseContext releaseC)449 sk_sp<SkImage> SkImage::MakeFromTexture(GrRecordingContext* rContext,
450                                         const GrBackendTexture& tex, GrSurfaceOrigin origin,
451                                         SkColorType ct, SkAlphaType at, sk_sp<SkColorSpace> cs,
452                                         TextureReleaseProc releaseP, ReleaseContext releaseC) {
453     auto releaseHelper = GrRefCntedCallback::Make(releaseP, releaseC);
454 
455     if (!rContext) {
456         return nullptr;
457     }
458 
459     const GrCaps* caps = rContext->priv().caps();
460 
461     GrColorType grColorType = SkColorTypeToGrColorType(ct);
462     if (GrColorType::kUnknown == grColorType) {
463         return nullptr;
464     }
465 
466     if (!SkImage_GpuBase::ValidateBackendTexture(caps, tex, grColorType, ct, at, cs)) {
467         return nullptr;
468     }
469 
470     return new_wrapped_texture_common(rContext, tex, grColorType, origin, at, std::move(cs),
471                                       kBorrow_GrWrapOwnership, std::move(releaseHelper));
472 }
473 
MakeFromAdoptedTexture(GrRecordingContext * rContext,const GrBackendTexture & tex,GrSurfaceOrigin origin,SkColorType ct,SkAlphaType at,sk_sp<SkColorSpace> cs)474 sk_sp<SkImage> SkImage::MakeFromAdoptedTexture(GrRecordingContext* rContext,
475                                                const GrBackendTexture& tex, GrSurfaceOrigin origin,
476                                                SkColorType ct, SkAlphaType at,
477                                                sk_sp<SkColorSpace> cs) {
478     auto dContext = GrAsDirectContext(rContext);
479     if (!dContext) {
480         // We have a DDL context and we don't support adopted textures for them.
481         return nullptr;
482     }
483 
484     const GrCaps* caps = dContext->priv().caps();
485 
486     GrColorType grColorType = SkColorTypeToGrColorType(ct);
487     if (GrColorType::kUnknown == grColorType) {
488         return nullptr;
489     }
490 
491     if (!SkImage_GpuBase::ValidateBackendTexture(caps, tex, grColorType, ct, at, cs)) {
492         return nullptr;
493     }
494 
495     return new_wrapped_texture_common(dContext, tex, grColorType, origin, at, std::move(cs),
496                                       kAdopt_GrWrapOwnership, nullptr);
497 }
498 
MakeTextureFromCompressed(GrDirectContext * direct,sk_sp<SkData> data,int width,int height,CompressionType type,GrMipmapped mipMapped,GrProtected isProtected)499 sk_sp<SkImage> SkImage::MakeTextureFromCompressed(GrDirectContext* direct, sk_sp<SkData> data,
500                                                   int width, int height, CompressionType type,
501                                                   GrMipmapped mipMapped,
502                                                   GrProtected isProtected) {
503     if (!direct || !data) {
504         return nullptr;
505     }
506 
507     GrBackendFormat beFormat = direct->compressedBackendFormat(type);
508     if (!beFormat.isValid()) {
509         sk_sp<SkImage> tmp = MakeRasterFromCompressed(std::move(data), width, height, type);
510         if (!tmp) {
511             return nullptr;
512         }
513         return tmp->makeTextureImage(direct, mipMapped);
514     }
515 
516     GrProxyProvider* proxyProvider = direct->priv().proxyProvider();
517     sk_sp<GrTextureProxy> proxy = proxyProvider->createCompressedTextureProxy(
518             {width, height}, SkBudgeted::kYes, mipMapped, isProtected, type, std::move(data));
519     if (!proxy) {
520         return nullptr;
521     }
522     GrSurfaceProxyView view(std::move(proxy));
523 
524     SkColorType colorType = GrCompressionTypeToSkColorType(type);
525 
526     return sk_make_sp<SkImage_Gpu>(sk_ref_sp(direct),
527                                    kNeedNewImageUniqueID,
528                                    std::move(view),
529                                    SkColorInfo(colorType, kOpaque_SkAlphaType, nullptr));
530 }
531 
makeTextureImage(GrDirectContext * dContext,GrMipmapped mipmapped,SkBudgeted budgeted) const532 sk_sp<SkImage> SkImage::makeTextureImage(GrDirectContext* dContext,
533                                          GrMipmapped mipmapped,
534                                          SkBudgeted budgeted) const {
535     if (!dContext) {
536         return nullptr;
537     }
538     if (!dContext->priv().caps()->mipmapSupport() || this->dimensions().area() <= 1) {
539         mipmapped = GrMipmapped::kNo;
540     }
541 
542     if (this->isTextureBacked()) {
543         if (!as_IB(this)->context()->priv().matches(dContext)) {
544             return nullptr;
545         }
546 
547         if (this->isTextureBacked() && (mipmapped == GrMipmapped::kNo || this->hasMipmaps())) {
548             return sk_ref_sp(const_cast<SkImage*>(this));
549         }
550     }
551     GrImageTexGenPolicy policy = budgeted == SkBudgeted::kYes
552                                          ? GrImageTexGenPolicy::kNew_Uncached_Budgeted
553                                          : GrImageTexGenPolicy::kNew_Uncached_Unbudgeted;
554     // TODO: Don't flatten YUVA images here. Add mips to the planes instead.
555     auto [view, ct] = as_IB(this)->asView(dContext, mipmapped, policy);
556     if (!view) {
557         return nullptr;
558     }
559     SkASSERT(view.asTextureProxy());
560     SkASSERT(mipmapped == GrMipmapped::kNo ||
561              view.asTextureProxy()->mipmapped() == GrMipmapped::kYes);
562     SkColorInfo colorInfo(GrColorTypeToSkColorType(ct), this->alphaType(), this->refColorSpace());
563     return sk_make_sp<SkImage_Gpu>(sk_ref_sp(dContext),
564                                    this->uniqueID(),
565                                    std::move(view),
566                                    std::move(colorInfo));
567 }
568 
569 ///////////////////////////////////////////////////////////////////////////////////////////////////
570 
MakePromiseTexture(sk_sp<GrContextThreadSafeProxy> threadSafeProxy,const GrBackendFormat & backendFormat,SkISize dimensions,GrMipmapped mipMapped,GrSurfaceOrigin origin,SkColorType colorType,SkAlphaType alphaType,sk_sp<SkColorSpace> colorSpace,PromiseImageTextureFulfillProc textureFulfillProc,PromiseImageTextureReleaseProc textureReleaseProc,PromiseImageTextureContext textureContext)571 sk_sp<SkImage> SkImage::MakePromiseTexture(sk_sp<GrContextThreadSafeProxy> threadSafeProxy,
572                                            const GrBackendFormat& backendFormat,
573                                            SkISize dimensions,
574                                            GrMipmapped mipMapped,
575                                            GrSurfaceOrigin origin,
576                                            SkColorType colorType,
577                                            SkAlphaType alphaType,
578                                            sk_sp<SkColorSpace> colorSpace,
579                                            PromiseImageTextureFulfillProc textureFulfillProc,
580                                            PromiseImageTextureReleaseProc textureReleaseProc,
581                                            PromiseImageTextureContext textureContext) {
582     // Our contract is that we will always call the release proc even on failure.
583     // We use the helper to convey the context, so we need to ensure make doesn't fail.
584     textureReleaseProc = textureReleaseProc ? textureReleaseProc : [](void*) {};
585     auto releaseHelper = GrRefCntedCallback::Make(textureReleaseProc, textureContext);
586     SkImageInfo info = SkImageInfo::Make(dimensions, colorType, alphaType, colorSpace);
587     if (!SkImageInfoIsValid(info)) {
588         return nullptr;
589     }
590 
591     if (!threadSafeProxy) {
592         return nullptr;
593     }
594 
595     if (dimensions.isEmpty()) {
596         return nullptr;
597     }
598 
599     GrColorType grColorType = SkColorTypeToGrColorType(colorType);
600     if (GrColorType::kUnknown == grColorType) {
601         return nullptr;
602     }
603 
604     if (!threadSafeProxy->priv().caps()->areColorTypeAndFormatCompatible(grColorType,
605                                                                          backendFormat)) {
606         return nullptr;
607     }
608 
609     auto proxy = SkImage_GpuBase::MakePromiseImageLazyProxy(threadSafeProxy.get(),
610                                                             dimensions,
611                                                             backendFormat,
612                                                             mipMapped,
613                                                             textureFulfillProc,
614                                                             std::move(releaseHelper));
615     if (!proxy) {
616         return nullptr;
617     }
618     GrSwizzle swizzle = threadSafeProxy->priv().caps()->getReadSwizzle(backendFormat, grColorType);
619     GrSurfaceProxyView view(std::move(proxy), origin, swizzle);
620     sk_sp<GrImageContext> ctx(GrImageContextPriv::MakeForPromiseImage(std::move(threadSafeProxy)));
621     return sk_make_sp<SkImage_Gpu>(std::move(ctx),
622                                    kNeedNewImageUniqueID,
623                                    std::move(view),
624                                    SkColorInfo(colorType, alphaType, std::move(colorSpace)));
625 }
626 
627 ///////////////////////////////////////////////////////////////////////////////////////////////////
628 
MakeCrossContextFromPixmap(GrDirectContext * dContext,const SkPixmap & originalPixmap,bool buildMips,bool limitToMaxTextureSize)629 sk_sp<SkImage> SkImage::MakeCrossContextFromPixmap(GrDirectContext* dContext,
630                                                    const SkPixmap& originalPixmap, bool buildMips,
631                                                    bool limitToMaxTextureSize) {
632     // Some backends or drivers don't support (safely) moving resources between contexts
633     if (!dContext || !dContext->priv().caps()->crossContextTextureSupport()) {
634         return SkImage::MakeRasterCopy(originalPixmap);
635     }
636 
637     // If non-power-of-two mipmapping isn't supported, ignore the client's request
638     if (!dContext->priv().caps()->mipmapSupport()) {
639         buildMips = false;
640     }
641 
642     const SkPixmap* pixmap = &originalPixmap;
643     SkAutoPixmapStorage resized;
644     int maxTextureSize = dContext->priv().caps()->maxTextureSize();
645     int maxDim = std::max(originalPixmap.width(), originalPixmap.height());
646     if (limitToMaxTextureSize && maxDim > maxTextureSize) {
647         float scale = static_cast<float>(maxTextureSize) / maxDim;
648         int newWidth = std::min(static_cast<int>(originalPixmap.width() * scale), maxTextureSize);
649         int newHeight = std::min(static_cast<int>(originalPixmap.height() * scale), maxTextureSize);
650         SkImageInfo info = originalPixmap.info().makeWH(newWidth, newHeight);
651         SkSamplingOptions sampling(SkFilterMode::kLinear);
652         if (!resized.tryAlloc(info) || !originalPixmap.scalePixels(resized, sampling)) {
653             return nullptr;
654         }
655         pixmap = &resized;
656     }
657     // Turn the pixmap into a GrTextureProxy
658     SkBitmap bmp;
659     bmp.installPixels(*pixmap);
660     GrMipmapped mipmapped = buildMips ? GrMipmapped::kYes : GrMipmapped::kNo;
661     auto [view, ct] = GrMakeUncachedBitmapProxyView(dContext, bmp, mipmapped);
662     if (!view) {
663         return SkImage::MakeRasterCopy(*pixmap);
664     }
665 
666     sk_sp<GrTexture> texture = sk_ref_sp(view.proxy()->peekTexture());
667 
668     // Flush any writes or uploads
669     dContext->priv().flushSurface(view.proxy());
670     GrGpu* gpu = dContext->priv().getGpu();
671 
672     std::unique_ptr<GrSemaphore> sema = gpu->prepareTextureForCrossContextUsage(texture.get());
673 
674     SkColorType skCT = GrColorTypeToSkColorType(ct);
675     auto gen = GrBackendTextureImageGenerator::Make(std::move(texture), view.origin(),
676                                                     std::move(sema), skCT,
677                                                     pixmap->alphaType(),
678                                                     pixmap->info().refColorSpace());
679     return SkImage::MakeFromGenerator(std::move(gen));
680 }
681 
682 #if defined(SK_BUILD_FOR_ANDROID) && __ANDROID_API__ >= 26
MakeFromAHardwareBuffer(AHardwareBuffer * graphicBuffer,SkAlphaType at,sk_sp<SkColorSpace> cs,GrSurfaceOrigin surfaceOrigin)683 sk_sp<SkImage> SkImage::MakeFromAHardwareBuffer(AHardwareBuffer* graphicBuffer, SkAlphaType at,
684                                                 sk_sp<SkColorSpace> cs,
685                                                 GrSurfaceOrigin surfaceOrigin) {
686     auto gen = GrAHardwareBufferImageGenerator::Make(graphicBuffer, at, cs, surfaceOrigin);
687     return SkImage::MakeFromGenerator(std::move(gen));
688 }
689 
MakeFromAHardwareBufferWithData(GrDirectContext * dContext,const SkPixmap & pixmap,AHardwareBuffer * hardwareBuffer,GrSurfaceOrigin surfaceOrigin)690 sk_sp<SkImage> SkImage::MakeFromAHardwareBufferWithData(GrDirectContext* dContext,
691                                                         const SkPixmap& pixmap,
692                                                         AHardwareBuffer* hardwareBuffer,
693                                                         GrSurfaceOrigin surfaceOrigin) {
694     AHardwareBuffer_Desc bufferDesc;
695     AHardwareBuffer_describe(hardwareBuffer, &bufferDesc);
696 
697     if (!SkToBool(bufferDesc.usage & AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE)) {
698         return nullptr;
699     }
700 
701 
702     GrBackendFormat backendFormat = GrAHardwareBufferUtils::GetBackendFormat(dContext,
703                                                                              hardwareBuffer,
704                                                                              bufferDesc.format,
705                                                                              true);
706 
707     if (!backendFormat.isValid()) {
708         return nullptr;
709     }
710 
711     GrAHardwareBufferUtils::DeleteImageProc deleteImageProc = nullptr;
712     GrAHardwareBufferUtils::UpdateImageProc updateImageProc = nullptr;
713     GrAHardwareBufferUtils::TexImageCtx deleteImageCtx = nullptr;
714 
715     const bool isRenderable = SkToBool(bufferDesc.usage & AHARDWAREBUFFER_USAGE_GPU_FRAMEBUFFER);
716 
717     GrBackendTexture backendTexture =
718             GrAHardwareBufferUtils::MakeBackendTexture(dContext, hardwareBuffer,
719                                                        bufferDesc.width, bufferDesc.height,
720                                                        &deleteImageProc, &updateImageProc,
721                                                        &deleteImageCtx, false, backendFormat,
722                                                        isRenderable);
723     if (!backendTexture.isValid()) {
724         return nullptr;
725     }
726     SkASSERT(deleteImageProc);
727 
728     auto releaseHelper = GrRefCntedCallback::Make(deleteImageProc, deleteImageCtx);
729 
730     SkColorType colorType =
731             GrAHardwareBufferUtils::GetSkColorTypeFromBufferFormat(bufferDesc.format);
732 
733     GrColorType grColorType = SkColorTypeToGrColorType(colorType);
734 
735     GrProxyProvider* proxyProvider = dContext->priv().proxyProvider();
736     if (!proxyProvider) {
737         return nullptr;
738     }
739 
740     sk_sp<GrTextureProxy> proxy = proxyProvider->wrapBackendTexture(
741             backendTexture, kBorrow_GrWrapOwnership, GrWrapCacheable::kNo, kRW_GrIOType,
742             std::move(releaseHelper));
743     if (!proxy) {
744         return nullptr;
745     }
746 
747     GrSwizzle swizzle = dContext->priv().caps()->getReadSwizzle(backendFormat, grColorType);
748     GrSurfaceProxyView framebufferView(std::move(proxy), surfaceOrigin, swizzle);
749     SkColorInfo colorInfo = pixmap.info().colorInfo().makeColorType(colorType);
750     sk_sp<SkImage> image = sk_make_sp<SkImage_Gpu>(sk_ref_sp(dContext),
751                                                    kNeedNewImageUniqueID,
752                                                    framebufferView,
753                                                    std::move(colorInfo));
754     if (!image) {
755         return nullptr;
756     }
757 
758     GrDrawingManager* drawingManager = dContext->priv().drawingManager();
759     if (!drawingManager) {
760         return nullptr;
761     }
762 
763     skgpu::SurfaceContext surfaceContext(
764             dContext, std::move(framebufferView),image->imageInfo().colorInfo());
765 
766     surfaceContext.writePixels(dContext, pixmap, {0, 0});
767 
768     GrSurfaceProxy* p[1] = {surfaceContext.asSurfaceProxy()};
769     drawingManager->flush(SkMakeSpan(p), SkSurface::BackendSurfaceAccess::kNoAccess, {}, nullptr);
770 
771     return image;
772 }
773 #endif
774 
775 ///////////////////////////////////////////////////////////////////////////////////////////////////
776 
MakeBackendTextureFromSkImage(GrDirectContext * direct,sk_sp<SkImage> image,GrBackendTexture * backendTexture,BackendTextureReleaseProc * releaseProc)777 bool SkImage::MakeBackendTextureFromSkImage(GrDirectContext* direct,
778                                             sk_sp<SkImage> image,
779                                             GrBackendTexture* backendTexture,
780                                             BackendTextureReleaseProc* releaseProc) {
781     if (!image || !backendTexture || !releaseProc) {
782         return false;
783     }
784 
785     auto [view, ct] = as_IB(image)->asView(direct, GrMipmapped::kNo);
786 
787     if (!view) {
788         return false;
789     }
790 
791     // Flush any pending IO on the texture.
792     direct->priv().flushSurface(view.proxy());
793 
794     GrTexture* texture = view.asTextureProxy()->peekTexture();
795     if (!texture) {
796         return false;
797     }
798     // We must make a copy of the image if the image is not unique, if the GrTexture owned by the
799     // image is not unique, or if the texture wraps an external object.
800     if (!image->unique() || !texture->unique() ||
801         texture->resourcePriv().refsWrappedObjects()) {
802         // onMakeSubset will always copy the image.
803         image = as_IB(image)->onMakeSubset(image->bounds(), direct);
804         if (!image) {
805             return false;
806         }
807         return MakeBackendTextureFromSkImage(direct, std::move(image), backendTexture, releaseProc);
808     }
809 
810     SkASSERT(!texture->resourcePriv().refsWrappedObjects());
811     SkASSERT(texture->unique());
812     SkASSERT(image->unique());
813 
814     // Take a reference to the GrTexture and release the image.
815     sk_sp<GrTexture> textureRef = sk_ref_sp(texture);
816     view.reset();
817     image = nullptr;
818     SkASSERT(textureRef->unique());
819 
820     // Steal the backend texture from the GrTexture, releasing the GrTexture in the process.
821     return GrTexture::StealBackendTexture(std::move(textureRef), backendTexture, releaseProc);
822 }
823 
onAsView(GrRecordingContext * recordingContext,GrMipmapped mipmapped,GrImageTexGenPolicy policy) const824 std::tuple<GrSurfaceProxyView, GrColorType> SkImage_Gpu::onAsView(
825         GrRecordingContext* recordingContext,
826         GrMipmapped mipmapped,
827         GrImageTexGenPolicy policy) const {
828     if (!fContext->priv().matches(recordingContext)) {
829         return {};
830     }
831     if (policy != GrImageTexGenPolicy::kDraw) {
832         return {CopyView(recordingContext, this->makeView(recordingContext), mipmapped, policy),
833                 SkColorTypeToGrColorType(this->colorType())};
834     }
835     GrSurfaceProxyView view = this->makeView(recordingContext);
836     GrColorType ct = SkColorTypeToGrColorType(this->colorType());
837     if (mipmapped == GrMipmapped::kYes) {
838         view = FindOrMakeCachedMipmappedView(recordingContext, std::move(view), this->uniqueID());
839     }
840     return {std::move(view), ct};
841 }
842 
onAsFragmentProcessor(GrRecordingContext * rContext,SkSamplingOptions sampling,const SkTileMode tileModes[2],const SkMatrix & m,const SkRect * subset,const SkRect * domain) const843 std::unique_ptr<GrFragmentProcessor> SkImage_Gpu::onAsFragmentProcessor(
844         GrRecordingContext* rContext,
845         SkSamplingOptions sampling,
846         const SkTileMode tileModes[2],
847         const SkMatrix& m,
848         const SkRect* subset,
849         const SkRect* domain) const {
850     if (!fContext->priv().matches(rContext)) {
851         return {};
852     }
853     auto mm = sampling.mipmap == SkMipmapMode::kNone ? GrMipmapped::kNo : GrMipmapped::kYes;
854     return MakeFragmentProcessorFromView(rContext,
855                                          std::get<0>(this->asView(rContext, mm)),
856                                          this->alphaType(),
857                                          sampling,
858                                          tileModes,
859                                          m,
860                                          subset,
861                                          domain);
862 }
863 
makeView(GrRecordingContext * rContext) const864 GrSurfaceProxyView SkImage_Gpu::makeView(GrRecordingContext* rContext) const {
865     return {fChooser.chooseProxy(rContext), fOrigin, fSwizzle};
866 }
867 
hintCacheGpuResource()868 void SkImage_Gpu::hintCacheGpuResource() {
869     auto dContext = fContext->asDirectContext();
870     if (!dContext) {
871         return;
872     }
873     GrTextureProxy* texProxy = this->makeView(dContext).asTextureProxy();
874     if (texProxy) {
875         texProxy->setUserCacheTarget(true);
876     }
877 }
878