Flutter Engine
The Flutter Engine
SkImage_Ganesh.h
Go to the documentation of this file.
1/*
2 * Copyright 2015 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#ifndef SkImage_Ganesh_DEFINED
9#define SkImage_Ganesh_DEFINED
10
15#include "src/base/SkSpinlock.h"
16#include "src/gpu/Swizzle.h"
20
21#include <cstddef>
22#include <cstdint>
23#include <memory>
24#include <tuple>
25
26class GrBackendFormat;
28class GrDirectContext;
30class GrImageContext;
32class GrRenderTask;
33class GrSurfaceProxy;
34class SkColorInfo;
35class SkColorSpace;
36class SkImage;
37class SkMatrix;
38enum class GrColorType;
39enum class GrImageTexGenPolicy : int;
40enum class GrSemaphoresSubmitted : bool;
41enum class SkTileMode;
42enum GrSurfaceOrigin : int;
43enum SkColorType : int;
44enum SkYUVColorSpace : int;
45struct GrFlushInfo;
46struct SkIRect;
47struct SkISize;
48struct SkImageInfo;
49struct SkRect;
50
51namespace skgpu {
52enum class Mipmapped : bool;
53enum class Protected : bool;
54}
55
56class SkImage_Ganesh final : public SkImage_GaneshBase {
57public:
59 uint32_t uniqueID,
62
64 GrSurfaceProxyView volatileSrc,
65 SkColorInfo colorInfo);
66
67 ~SkImage_Ganesh() override;
68
69 // From SkImage.h
70 size_t textureSize() const override;
71
72 // From SkImage_Base.h
74
75 bool onHasMipmaps() const override;
76 bool onIsProtected() const override;
77
81 GrDirectContext*) const final;
82
84
86 SkIRect srcRect,
90 ReadPixelsContext) const override;
91
93 bool readAlpha,
95 SkIRect srcRect,
96 SkISize dstSize,
100 ReadPixelsContext) const override;
101
102 void generatingSurfaceIsDeleted() override;
103
104 // From SkImage_GaneshBase.h
106
107 std::tuple<GrSurfaceProxyView, GrColorType> asView(GrRecordingContext*,
109 GrImageTexGenPolicy) const override;
110
111 std::unique_ptr<GrFragmentProcessor> asFragmentProcessor(GrRecordingContext*,
113 const SkTileMode[2],
114 const SkMatrix&,
115 const SkRect*,
116 const SkRect*) const override;
117
118 // If this is image is a cached SkSurface snapshot then this method is called by the SkSurface
119 // before a write to check if the surface must make a copy to avoid modifying the image's
120 // contents.
121 bool surfaceMustCopyOnWrite(GrSurfaceProxy* surfaceProxy) const;
122
124 bool flushPendingGrContextIO,
125 GrSurfaceOrigin* origin) const;
126
127 GrSurfaceOrigin origin() const override { return fOrigin; }
128
129private:
131 GrSurfaceProxyView volatileSrc,
132 sk_sp<GrSurfaceProxy> stableCopy,
133 sk_sp<GrRenderTask> copyTask,
134 int volatileSrcTargetCount,
136
137 GrSurfaceProxyView makeView(GrRecordingContext*) const;
138
139 // Thread-safe wrapper around the proxies backing this image. Handles dynamically switching
140 // from a "volatile" proxy that may be overwritten (by an SkSurface that this image was snapped
141 // from) to a "stable" proxy that is a copy of the volatile proxy. It allows the image to cancel
142 // the copy if the stable proxy is never required because the contents of the volatile proxy
143 // were never mutated by the SkSurface during the image lifetime.
144 class ProxyChooser {
145 public:
146 ProxyChooser(sk_sp<GrSurfaceProxy> stableProxy,
147 sk_sp<GrSurfaceProxy> volatileProxy,
148 sk_sp<GrRenderTask> copyTask,
149 int volatileProxyTargetCount);
150
151 ProxyChooser(sk_sp<GrSurfaceProxy> stableProxy);
152
153 ~ProxyChooser();
154
155 // Checks if there is a volatile proxy that is safe to use. If so returns it, otherwise
156 // returns the stable proxy (and drops the volatile one if it exists).
158 // Call when it is known copy is necessary.
159 sk_sp<GrSurfaceProxy> switchToStableProxy() SK_EXCLUDES(fLock);
160 // Call when it is known for sure copy won't be necessary.
161 sk_sp<GrSurfaceProxy> makeVolatileProxyStable() SK_EXCLUDES(fLock);
162
163 bool surfaceMustCopyOnWrite(GrSurfaceProxy* surfaceProxy) const SK_EXCLUDES(fLock);
164
165 // Queries that should be independent of which proxy is in use.
166 size_t gpuMemorySize() const SK_EXCLUDES(fLock);
167 skgpu::Mipmapped mipmapped() const SK_EXCLUDES(fLock);
168 skgpu::Protected isProtected() const SK_EXCLUDES(fLock);
169#ifdef SK_DEBUG
170 const GrBackendFormat& backendFormat() SK_EXCLUDES(fLock);
171#endif
172
173 private:
174 mutable SkSpinlock fLock;
175 sk_sp<GrSurfaceProxy> fStableProxy SK_GUARDED_BY(fLock);
176 sk_sp<GrSurfaceProxy> fVolatileProxy SK_GUARDED_BY(fLock);
177 sk_sp<GrRenderTask> fVolatileToStableCopyTask;
178 // The number of GrRenderTasks targeting the volatile proxy at creation time. If the
179 // proxy's target count increases it indicates additional writes and we must switch
180 // to using the stable copy.
181 const int fVolatileProxyTargetCount = 0;
182 };
183
184 mutable ProxyChooser fChooser;
185 skgpu::Swizzle fSwizzle;
186 GrSurfaceOrigin fOrigin;
187
188 using INHERITED = SkImage_GaneshBase;
189};
190
191#endif
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition: DM.cpp:213
GrColorType
Definition: GrTypesPriv.h:540
GrSurfaceOrigin
Definition: GrTypes.h:147
GrSemaphoresSubmitted
Definition: GrTypes.h:229
SkColorType
Definition: SkColorType.h:19
GrImageTexGenPolicy
Definition: SkGr.h:141
SkYUVColorSpace
Definition: SkImageInfo.h:68
#define SK_EXCLUDES(...)
#define SK_GUARDED_BY(x)
SkTileMode
Definition: SkTileMode.h:13
virtual sk_sp< SkImage > onMakeColorTypeAndColorSpace(SkColorType, sk_sp< SkColorSpace >, GrDirectContext *) const=0
SkImage_GaneshBase(sk_sp< GrImageContext >, SkImageInfo, uint32_t uniqueID)
GrImageContext * context() const final
void generatingSurfaceIsDeleted() override
std::unique_ptr< GrFragmentProcessor > asFragmentProcessor(GrRecordingContext *, SkSamplingOptions, const SkTileMode[2], const SkMatrix &, const SkRect *, const SkRect *) const override
SkImage_Ganesh(sk_sp< GrImageContext > context, uint32_t uniqueID, GrSurfaceProxyView view, SkColorInfo info)
sk_sp< SkImage > onMakeColorTypeAndColorSpace(SkColorType, sk_sp< SkColorSpace >, GrDirectContext *) const final
bool onIsProtected() const override
SkImage_Base::Type type() const override
sk_sp< SkImage > onReinterpretColorSpace(sk_sp< SkColorSpace >) const final
void onAsyncRescaleAndReadPixelsYUV420(SkYUVColorSpace, bool readAlpha, sk_sp< SkColorSpace >, SkIRect srcRect, SkISize dstSize, RescaleGamma, RescaleMode, ReadPixelsCallback, ReadPixelsContext) const override
bool getExistingBackendTexture(GrBackendTexture *outTexture, bool flushPendingGrContextIO, GrSurfaceOrigin *origin) const
GrSurfaceOrigin origin() const override
bool onHasMipmaps() const override
GrSemaphoresSubmitted flush(GrDirectContext *, const GrFlushInfo &) const override
bool surfaceMustCopyOnWrite(GrSurfaceProxy *surfaceProxy) const
void onAsyncRescaleAndReadPixels(const SkImageInfo &, SkIRect srcRect, RescaleGamma, RescaleMode, ReadPixelsCallback, ReadPixelsContext) const override
size_t textureSize() const override
~SkImage_Ganesh() override
std::tuple< GrSurfaceProxyView, GrColorType > asView(GrRecordingContext *, skgpu::Mipmapped, GrImageTexGenPolicy) const override
static sk_sp< SkImage > MakeWithVolatileSrc(sk_sp< GrRecordingContext > rContext, GrSurfaceProxyView volatileSrc, SkColorInfo colorInfo)
uint32_t uniqueID() const
Definition: SkImage.h:311
RescaleMode
Definition: SkImage.h:587
RescaleGamma
Definition: SkImage.h:585
bool isProtected() const
Definition: SkImage.cpp:294
SkImage::ReadPixelsContext ReadPixelsContext
Definition: Device.cpp:81
SkImage::ReadPixelsCallback ReadPixelsCallback
Definition: Device.cpp:80
Definition: GpuTools.h:21
Mipmapped
Definition: GpuTypes.h:53
Protected
Definition: GpuTypes.h:61
Definition: SkRect.h:32
Definition: SkSize.h:16