2 * Copyright 2015 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #ifndef SkImage_Gpu_DEFINED
9 #define SkImage_Gpu_DEFINED
11 #include "include/private/SkSpinlock.h"
12 #include "src/core/SkImagePriv.h"
13 #include "src/gpu/ganesh/GrGpuResourcePriv.h"
14 #include "src/gpu/ganesh/GrSurfaceProxyPriv.h"
15 #include "src/gpu/ganesh/GrSurfaceProxyView.h"
16 #include "src/image/SkImage_GpuBase.h"
18 class GrDirectContext;
19 class GrRecordingContext;
24 class SkImage_Gpu final : public SkImage_GpuBase {
26 SkImage_Gpu(sk_sp<GrImageContext> context,
28 GrSurfaceProxyView view,
31 static sk_sp<SkImage> MakeWithVolatileSrc(sk_sp<GrRecordingContext> rContext,
32 GrSurfaceProxyView volatileSrc,
33 SkColorInfo colorInfo);
35 ~SkImage_Gpu() override;
37 // If this is image is a cached SkSurface snapshot then this method is called by the SkSurface
38 // before a write to check if the surface must make a copy to avoid modifying the image's
40 bool surfaceMustCopyOnWrite(GrSurfaceProxy* surfaceProxy) const;
42 bool onHasMipmaps() const override;
44 GrSemaphoresSubmitted onFlush(GrDirectContext*, const GrFlushInfo&) const override;
46 GrBackendTexture onGetBackendTexture(bool flushPendingGrContextIO,
47 GrSurfaceOrigin* origin) const final;
49 bool isGaneshBacked() const override { return true; }
51 size_t onTextureSize() const override;
53 sk_sp<SkImage> onMakeColorTypeAndColorSpace(SkColorType, sk_sp<SkColorSpace>,
54 GrDirectContext*) const final;
56 sk_sp<SkImage> onReinterpretColorSpace(sk_sp<SkColorSpace>) const final;
58 void onAsyncRescaleAndReadPixels(const SkImageInfo&,
59 const SkIRect& srcRect,
63 ReadPixelsContext) const override;
65 void onAsyncRescaleAndReadPixelsYUV420(SkYUVColorSpace,
67 const SkIRect& srcRect,
68 const SkISize& dstSize,
72 ReadPixelsContext) const override;
74 void generatingSurfaceIsDeleted() override;
77 SkImage_Gpu(sk_sp<GrDirectContext>,
78 GrSurfaceProxyView volatileSrc,
79 sk_sp<GrSurfaceProxy> stableCopy,
80 sk_sp<GrRenderTask> copyTask,
81 int volatileSrcTargetCount,
84 std::tuple<GrSurfaceProxyView, GrColorType> onAsView(GrRecordingContext*,
86 GrImageTexGenPolicy) const override;
88 std::unique_ptr<GrFragmentProcessor> onAsFragmentProcessor(GrRecordingContext*,
93 const SkRect*) const override;
95 GrSurfaceProxyView makeView(GrRecordingContext*) const;
97 // Thread-safe wrapper around the proxies backing this image. Handles dynamically switching
98 // from a "volatile" proxy that may be overwritten (by an SkSurface that this image was snapped
99 // from) to a "stable" proxy that is a copy of the volatile proxy. It allows the image to cancel
100 // the copy if the stable proxy is never required because the contents of the volatile proxy
101 // were never mutated by the SkSurface during the image lifetime.
104 ProxyChooser(sk_sp<GrSurfaceProxy> stableProxy,
105 sk_sp<GrSurfaceProxy> volatileProxy,
106 sk_sp<GrRenderTask> copyTask,
107 int volatileProxyTargetCount);
109 ProxyChooser(sk_sp<GrSurfaceProxy> stableProxy);
113 // Checks if there is a volatile proxy that is safe to use. If so returns it, otherwise
114 // returns the stable proxy (and drops the volatile one if it exists).
115 sk_sp<GrSurfaceProxy> chooseProxy(GrRecordingContext* context) SK_EXCLUDES(fLock);
116 // Call when it is known copy is necessary.
117 sk_sp<GrSurfaceProxy> switchToStableProxy() SK_EXCLUDES(fLock);
118 // Call when it is known for sure copy won't be necessary.
119 sk_sp<GrSurfaceProxy> makeVolatileProxyStable() SK_EXCLUDES(fLock);
121 bool surfaceMustCopyOnWrite(GrSurfaceProxy* surfaceProxy) const SK_EXCLUDES(fLock);
123 // Queries that should be independent of which proxy is in use.
124 size_t gpuMemorySize() const SK_EXCLUDES(fLock);
125 GrMipmapped mipmapped() const SK_EXCLUDES(fLock);
127 GrBackendFormat backendFormat() SK_EXCLUDES(fLock);
131 mutable SkSpinlock fLock;
132 sk_sp<GrSurfaceProxy> fStableProxy SK_GUARDED_BY(fLock);
133 sk_sp<GrSurfaceProxy> fVolatileProxy SK_GUARDED_BY(fLock);
134 sk_sp<GrRenderTask> fVolatileToStableCopyTask;
135 // The number of GrRenderTasks targeting the volatile proxy at creation time. If the
136 // proxy's target count increases it indicates additional writes and we must switch
137 // to using the stable copy.
138 const int fVolatileProxyTargetCount = 0;
141 mutable ProxyChooser fChooser;
142 skgpu::Swizzle fSwizzle;
143 GrSurfaceOrigin fOrigin;
145 using INHERITED = SkImage_GpuBase;