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