robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 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 GrSurfaceProxy_DEFINED |
| 9 | #define GrSurfaceProxy_DEFINED |
| 10 | |
| 11 | #include "GrGpuResource.h" |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 12 | #include "GrSurface.h" |
Robert Phillips | 93f1633 | 2016-11-23 19:37:13 -0500 | [diff] [blame] | 13 | |
csmartdalton | bf4a8f9 | 2016-09-06 10:01:06 -0700 | [diff] [blame] | 14 | #include "SkRect.h" |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 15 | |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 16 | class GrBackendTexture; |
Robert Phillips | 3743013 | 2016-11-09 06:50:43 -0500 | [diff] [blame] | 17 | class GrCaps; |
Robert Phillips | c589b0b | 2017-04-17 07:53:07 -0400 | [diff] [blame] | 18 | class GrOpList; |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame^] | 19 | class GrProxyProvider; |
Brian Osman | 45580d3 | 2016-11-23 09:37:01 -0500 | [diff] [blame] | 20 | class GrRenderTargetOpList; |
| 21 | class GrRenderTargetProxy; |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 22 | class GrResourceProvider; |
Robert Phillips | d46697a | 2017-01-25 12:10:37 -0500 | [diff] [blame] | 23 | class GrSurfaceContext; |
Robert Phillips | 757914d | 2017-01-25 15:48:30 -0500 | [diff] [blame] | 24 | class GrSurfaceProxyPriv; |
Brian Osman | 45580d3 | 2016-11-23 09:37:01 -0500 | [diff] [blame] | 25 | class GrTextureOpList; |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 26 | class GrTextureProxy; |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 27 | |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 28 | // This class replicates the functionality GrIORef<GrSurface> but tracks the |
| 29 | // utilitization for later resource allocation (for the deferred case) and |
| 30 | // forwards on the utilization in the wrapped case |
| 31 | class GrIORefProxy : public SkNoncopyable { |
| 32 | public: |
| 33 | void ref() const { |
| 34 | this->validate(); |
| 35 | |
| 36 | ++fRefCnt; |
| 37 | if (fTarget) { |
| 38 | fTarget->ref(); |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | void unref() const { |
| 43 | this->validate(); |
| 44 | |
| 45 | if (fTarget) { |
| 46 | fTarget->unref(); |
| 47 | } |
| 48 | |
Robert Phillips | b6deea8 | 2017-05-11 14:14:30 -0400 | [diff] [blame] | 49 | --fRefCnt; |
| 50 | this->didRemoveRefOrPendingIO(); |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 51 | } |
| 52 | |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame] | 53 | #ifdef SK_DEBUG |
| 54 | bool isUnique_debugOnly() const { // For asserts. |
| 55 | SkASSERT(fRefCnt >= 0 && fPendingWrites >= 0 && fPendingReads >= 0); |
| 56 | return 1 == fRefCnt + fPendingWrites + fPendingReads; |
| 57 | } |
| 58 | #endif |
| 59 | |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 60 | void validate() const { |
Robert Phillips | b6deea8 | 2017-05-11 14:14:30 -0400 | [diff] [blame] | 61 | #ifdef SK_DEBUG |
| 62 | SkASSERT(fRefCnt >= 0); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 63 | SkASSERT(fPendingReads >= 0); |
| 64 | SkASSERT(fPendingWrites >= 0); |
| 65 | SkASSERT(fRefCnt + fPendingReads + fPendingWrites >= 1); |
| 66 | |
| 67 | if (fTarget) { |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 68 | // The backing GrSurface can have more refs than the proxy if the proxy |
| 69 | // started off wrapping an external resource (that came in with refs). |
| 70 | // The GrSurface should never have fewer refs than the proxy however. |
| 71 | SkASSERT(fTarget->fRefCnt >= fRefCnt); |
Robert Phillips | b6deea8 | 2017-05-11 14:14:30 -0400 | [diff] [blame] | 72 | SkASSERT(fTarget->fPendingReads >= fPendingReads); |
| 73 | SkASSERT(fTarget->fPendingWrites >= fPendingWrites); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 74 | } |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 75 | #endif |
| 76 | } |
| 77 | |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 78 | int32_t getProxyRefCnt_TestOnly() const; |
| 79 | int32_t getBackingRefCnt_TestOnly() const; |
| 80 | int32_t getPendingReadCnt_TestOnly() const; |
| 81 | int32_t getPendingWriteCnt_TestOnly() const; |
| 82 | |
Brian Salomon | cf75b00 | 2017-08-16 10:42:09 -0400 | [diff] [blame] | 83 | void addPendingRead() const { |
| 84 | this->validate(); |
| 85 | |
| 86 | ++fPendingReads; |
| 87 | if (fTarget) { |
| 88 | fTarget->addPendingRead(); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | void completedRead() const { |
| 93 | this->validate(); |
| 94 | |
| 95 | if (fTarget) { |
| 96 | fTarget->completedRead(); |
| 97 | } |
| 98 | |
| 99 | --fPendingReads; |
| 100 | this->didRemoveRefOrPendingIO(); |
| 101 | } |
| 102 | |
| 103 | void addPendingWrite() const { |
| 104 | this->validate(); |
| 105 | |
| 106 | ++fPendingWrites; |
| 107 | if (fTarget) { |
| 108 | fTarget->addPendingWrite(); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | void completedWrite() const { |
| 113 | this->validate(); |
| 114 | |
| 115 | if (fTarget) { |
| 116 | fTarget->completedWrite(); |
| 117 | } |
| 118 | |
| 119 | --fPendingWrites; |
| 120 | this->didRemoveRefOrPendingIO(); |
| 121 | } |
| 122 | |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 123 | protected: |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 124 | GrIORefProxy() : fTarget(nullptr), fRefCnt(1), fPendingReads(0), fPendingWrites(0) {} |
| 125 | GrIORefProxy(sk_sp<GrSurface> surface) : fRefCnt(1), fPendingReads(0), fPendingWrites(0) { |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 126 | // Since we're manually forwarding on refs & unrefs we don't want sk_sp doing |
| 127 | // anything extra. |
| 128 | fTarget = surface.release(); |
| 129 | } |
| 130 | virtual ~GrIORefProxy() { |
| 131 | // We don't unref 'fTarget' here since the 'unref' method will already |
| 132 | // have forwarded on the unref call that got use here. |
| 133 | } |
| 134 | |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 135 | // This GrIORefProxy was deferred before but has just been instantiated. To |
| 136 | // make all the reffing & unreffing work out we now need to transfer any deferred |
| 137 | // refs & unrefs to the new GrSurface |
| 138 | void transferRefs() { |
| 139 | SkASSERT(fTarget); |
| 140 | |
Robert Phillips | f8e2502 | 2017-11-08 15:24:31 -0500 | [diff] [blame] | 141 | SkASSERT(fTarget->fRefCnt > 0); |
Robert Phillips | 2d9cb57 | 2017-11-13 13:38:05 +0000 | [diff] [blame] | 142 | fTarget->fRefCnt += (fRefCnt-1); // don't xfer the proxy's creation ref |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 143 | fTarget->fPendingReads += fPendingReads; |
| 144 | fTarget->fPendingWrites += fPendingWrites; |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 145 | } |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 146 | |
Robert Phillips | 757914d | 2017-01-25 15:48:30 -0500 | [diff] [blame] | 147 | bool internalHasPendingIO() const { |
| 148 | if (fTarget) { |
| 149 | return fTarget->internalHasPendingIO(); |
| 150 | } |
| 151 | |
| 152 | return SkToBool(fPendingWrites | fPendingReads); |
| 153 | } |
| 154 | |
Robert Phillips | 7ee385e | 2017-03-30 08:02:11 -0400 | [diff] [blame] | 155 | bool internalHasPendingWrite() const { |
| 156 | if (fTarget) { |
| 157 | return fTarget->internalHasPendingWrite(); |
| 158 | } |
| 159 | |
| 160 | return SkToBool(fPendingWrites); |
| 161 | } |
| 162 | |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 163 | // For deferred proxies this will be null. For wrapped proxies it will point to the |
| 164 | // wrapped resource. |
| 165 | GrSurface* fTarget; |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 166 | |
| 167 | private: |
| 168 | // This class is used to manage conversion of refs to pending reads/writes. |
Robert Phillips | b6deea8 | 2017-05-11 14:14:30 -0400 | [diff] [blame] | 169 | friend class GrSurfaceProxyRef; |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 170 | template <typename, GrIOType> friend class GrPendingIOResource; |
| 171 | |
Robert Phillips | b6deea8 | 2017-05-11 14:14:30 -0400 | [diff] [blame] | 172 | void didRemoveRefOrPendingIO() const { |
| 173 | if (0 == fPendingReads && 0 == fPendingWrites && 0 == fRefCnt) { |
| 174 | delete this; |
| 175 | } |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | mutable int32_t fRefCnt; |
| 179 | mutable int32_t fPendingReads; |
| 180 | mutable int32_t fPendingWrites; |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 181 | }; |
| 182 | |
| 183 | class GrSurfaceProxy : public GrIORefProxy { |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 184 | public: |
Robert Phillips | 066f020 | 2017-07-25 10:16:35 -0400 | [diff] [blame] | 185 | static sk_sp<GrSurfaceProxy> MakeWrapped(sk_sp<GrSurface>, GrSurfaceOrigin); |
| 186 | static sk_sp<GrTextureProxy> MakeWrapped(sk_sp<GrTexture>, GrSurfaceOrigin); |
Robert Phillips | 3743013 | 2016-11-09 06:50:43 -0500 | [diff] [blame] | 187 | |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame^] | 188 | static sk_sp<GrTextureProxy> MakeDeferred(GrProxyProvider*, |
Robert Phillips | 7928e76 | 2017-02-28 16:30:28 -0500 | [diff] [blame] | 189 | const GrSurfaceDesc&, SkBackingFit, |
| 190 | SkBudgeted, uint32_t flags = 0); |
Robert Phillips | 3743013 | 2016-11-09 06:50:43 -0500 | [diff] [blame] | 191 | |
Robert Phillips | 8e8c755 | 2017-07-10 12:06:05 -0400 | [diff] [blame] | 192 | /** |
| 193 | * Creates a proxy that will be mipmapped. |
| 194 | * |
| 195 | * @param desc Description of the texture properties. |
| 196 | * @param budgeted Does the texture count against the resource cache budget? |
| 197 | * @param texels A contiguous array of mipmap levels |
| 198 | * @param mipLevelCount The amount of elements in the texels array |
| 199 | */ |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame^] | 200 | static sk_sp<GrTextureProxy> MakeDeferredMipMap(GrProxyProvider*, |
Robert Phillips | 8e8c755 | 2017-07-10 12:06:05 -0400 | [diff] [blame] | 201 | const GrSurfaceDesc& desc, SkBudgeted budgeted, |
Robert Phillips | 590533f | 2017-07-11 14:22:35 -0400 | [diff] [blame] | 202 | const GrMipLevel texels[], int mipLevelCount, |
Robert Phillips | 8e8c755 | 2017-07-10 12:06:05 -0400 | [diff] [blame] | 203 | SkDestinationSurfaceColorMode mipColorMode = |
| 204 | SkDestinationSurfaceColorMode::kLegacy); |
| 205 | |
Greg Daniel | e1da1d9 | 2017-10-06 15:59:27 -0400 | [diff] [blame] | 206 | /** |
| 207 | * Like the call above but there are no texels to upload. A texture proxy is returned that |
| 208 | * simply has space allocated for the mips. We will allocated the full amount of mip levels |
| 209 | * based on the width and height in the GrSurfaceDesc. |
| 210 | */ |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame^] | 211 | static sk_sp<GrTextureProxy> MakeDeferredMipMap(GrProxyProvider*, |
Greg Daniel | e1da1d9 | 2017-10-06 15:59:27 -0400 | [diff] [blame] | 212 | const GrSurfaceDesc& desc, SkBudgeted budgeted); |
| 213 | |
| 214 | |
Robert Phillips | 3743013 | 2016-11-09 06:50:43 -0500 | [diff] [blame] | 215 | // TODO: need to refine ownership semantics of 'srcData' if we're in completely |
| 216 | // deferred mode |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame^] | 217 | static sk_sp<GrTextureProxy> MakeDeferred(GrProxyProvider*, |
Robert Phillips | 3743013 | 2016-11-09 06:50:43 -0500 | [diff] [blame] | 218 | const GrSurfaceDesc&, SkBudgeted, |
| 219 | const void* srcData, size_t rowBytes); |
| 220 | |
Robert Phillips | c25db63 | 2017-12-13 09:22:45 -0500 | [diff] [blame] | 221 | static sk_sp<GrTextureProxy> MakeWrappedBackend(GrContext*, const GrBackendTexture&, |
| 222 | GrSurfaceOrigin); |
Robert Phillips | 26caf89 | 2017-01-27 10:58:31 -0500 | [diff] [blame] | 223 | |
Chris Dalton | 706a6ff | 2017-11-29 22:01:06 -0700 | [diff] [blame] | 224 | using LazyInstantiateCallback = std::function<sk_sp<GrTexture>(GrResourceProvider*, |
| 225 | GrSurfaceOrigin* outOrigin)>; |
| 226 | |
| 227 | enum class Renderable : bool { |
| 228 | kNo = false, |
| 229 | kYes = true |
| 230 | }; |
| 231 | |
| 232 | /** |
| 233 | * Creates a texture proxy that will be instantiated by a user-supplied callback during flush. |
| 234 | * (Mipmapping, MSAA, and stencil are not supported by this method.) |
| 235 | */ |
| 236 | static sk_sp<GrTextureProxy> MakeLazy(LazyInstantiateCallback&&, Renderable, GrPixelConfig); |
| 237 | |
| 238 | GrPixelConfig config() const { return fConfig; } |
| 239 | int width() const { SkASSERT(!this->isPendingLazyInstantiation()); return fWidth; } |
| 240 | int height() const { SkASSERT(!this->isPendingLazyInstantiation()); return fHeight; } |
| 241 | int worstCaseWidth() const; |
| 242 | int worstCaseHeight() const; |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 243 | GrSurfaceOrigin origin() const { |
Chris Dalton | 706a6ff | 2017-11-29 22:01:06 -0700 | [diff] [blame] | 244 | SkASSERT(!this->isPendingLazyInstantiation()); |
Brian Salomon | bb5711a | 2017-05-17 13:49:59 -0400 | [diff] [blame] | 245 | SkASSERT(kTopLeft_GrSurfaceOrigin == fOrigin || kBottomLeft_GrSurfaceOrigin == fOrigin); |
| 246 | return fOrigin; |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 247 | } |
Chris Dalton | 706a6ff | 2017-11-29 22:01:06 -0700 | [diff] [blame] | 248 | |
| 249 | // If the client gave us a LazyInstantiateCallback (via MakeLazy), then we will invoke that |
| 250 | // callback during flush. fWidth, fHeight, and fOrigin will be undefined until that time. |
| 251 | bool isPendingLazyInstantiation() const { return SkToBool(fLazyInstantiateCallback); } |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 252 | |
Robert Phillips | 294870f | 2016-11-11 12:38:40 -0500 | [diff] [blame] | 253 | class UniqueID { |
| 254 | public: |
Robert Phillips | 7ee385e | 2017-03-30 08:02:11 -0400 | [diff] [blame] | 255 | static UniqueID InvalidID() { |
| 256 | return UniqueID(uint32_t(SK_InvalidUniqueID)); |
| 257 | } |
| 258 | |
Robert Phillips | 294870f | 2016-11-11 12:38:40 -0500 | [diff] [blame] | 259 | // wrapped |
| 260 | explicit UniqueID(const GrGpuResource::UniqueID& id) : fID(id.asUInt()) { } |
Chris Dalton | 706a6ff | 2017-11-29 22:01:06 -0700 | [diff] [blame] | 261 | // deferred and lazy-callback |
Robert Phillips | 294870f | 2016-11-11 12:38:40 -0500 | [diff] [blame] | 262 | UniqueID() : fID(GrGpuResource::CreateUniqueID()) { } |
| 263 | |
| 264 | uint32_t asUInt() const { return fID; } |
| 265 | |
| 266 | bool operator==(const UniqueID& other) const { |
| 267 | return fID == other.fID; |
| 268 | } |
| 269 | bool operator!=(const UniqueID& other) const { |
| 270 | return !(*this == other); |
| 271 | } |
| 272 | |
Robert Phillips | 7ee385e | 2017-03-30 08:02:11 -0400 | [diff] [blame] | 273 | void makeInvalid() { fID = SK_InvalidUniqueID; } |
Robert Phillips | 294870f | 2016-11-11 12:38:40 -0500 | [diff] [blame] | 274 | bool isInvalid() const { return SK_InvalidUniqueID == fID; } |
| 275 | |
| 276 | private: |
Robert Phillips | 7ee385e | 2017-03-30 08:02:11 -0400 | [diff] [blame] | 277 | explicit UniqueID(uint32_t id) : fID(id) {} |
| 278 | |
| 279 | uint32_t fID; |
Robert Phillips | 294870f | 2016-11-11 12:38:40 -0500 | [diff] [blame] | 280 | }; |
| 281 | |
| 282 | /* |
| 283 | * The contract for the uniqueID is: |
| 284 | * for wrapped resources: |
| 285 | * the uniqueID will match that of the wrapped resource |
| 286 | * |
| 287 | * for deferred resources: |
| 288 | * the uniqueID will be different from the real resource, when it is allocated |
| 289 | * the proxy's uniqueID will not change across the instantiate call |
| 290 | * |
| 291 | * the uniqueIDs of the proxies and the resources draw from the same pool |
| 292 | * |
| 293 | * What this boils down to is that the uniqueID of a proxy can be used to consistently |
| 294 | * track/identify a proxy but should never be used to distinguish between |
| 295 | * resources and proxies - beware! |
| 296 | */ |
| 297 | UniqueID uniqueID() const { return fUniqueID; } |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 298 | |
Robert Phillips | 159e3c6 | 2017-06-19 12:02:00 -0400 | [diff] [blame] | 299 | UniqueID underlyingUniqueID() const { |
| 300 | if (fTarget) { |
| 301 | return UniqueID(fTarget->uniqueID()); |
| 302 | } |
| 303 | |
| 304 | return fUniqueID; |
| 305 | } |
| 306 | |
Robert Phillips | eee4d6e | 2017-06-05 09:26:07 -0400 | [diff] [blame] | 307 | virtual bool instantiate(GrResourceProvider* resourceProvider) = 0; |
Robert Phillips | 3743013 | 2016-11-09 06:50:43 -0500 | [diff] [blame] | 308 | |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 309 | /** |
robertphillips | 13a7eee | 2016-08-31 15:06:24 -0700 | [diff] [blame] | 310 | * Helper that gets the width and height of the surface as a bounding rectangle. |
| 311 | */ |
Chris Dalton | 706a6ff | 2017-11-29 22:01:06 -0700 | [diff] [blame] | 312 | SkRect getBoundsRect() const { |
| 313 | SkASSERT(!this->isPendingLazyInstantiation()); |
| 314 | return SkRect::MakeIWH(this->width(), this->height()); |
| 315 | } |
Robert Phillips | 784b7bf | 2016-12-09 13:35:02 -0500 | [diff] [blame] | 316 | |
robertphillips | 13a7eee | 2016-08-31 15:06:24 -0700 | [diff] [blame] | 317 | /** |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 318 | * @return the texture proxy associated with the surface proxy, may be NULL. |
| 319 | */ |
| 320 | virtual GrTextureProxy* asTextureProxy() { return nullptr; } |
| 321 | virtual const GrTextureProxy* asTextureProxy() const { return nullptr; } |
| 322 | |
| 323 | /** |
| 324 | * @return the render target proxy associated with the surface proxy, may be NULL. |
| 325 | */ |
| 326 | virtual GrRenderTargetProxy* asRenderTargetProxy() { return nullptr; } |
| 327 | virtual const GrRenderTargetProxy* asRenderTargetProxy() const { return nullptr; } |
| 328 | |
robertphillips | 13a7eee | 2016-08-31 15:06:24 -0700 | [diff] [blame] | 329 | /** |
| 330 | * Does the resource count against the resource budget? |
| 331 | */ |
| 332 | SkBudgeted isBudgeted() const { return fBudgeted; } |
| 333 | |
Robert Phillips | f2361d2 | 2016-10-25 14:20:06 -0400 | [diff] [blame] | 334 | void setLastOpList(GrOpList* opList); |
| 335 | GrOpList* getLastOpList() { return fLastOpList; } |
| 336 | |
Brian Osman | 45580d3 | 2016-11-23 09:37:01 -0500 | [diff] [blame] | 337 | GrRenderTargetOpList* getLastRenderTargetOpList(); |
| 338 | GrTextureOpList* getLastTextureOpList(); |
| 339 | |
Robert Phillips | 8bc06d0 | 2016-11-01 17:28:40 -0400 | [diff] [blame] | 340 | /** |
Robert Phillips | f5442bb | 2017-04-17 14:18:34 -0400 | [diff] [blame] | 341 | * Retrieves the amount of GPU memory that will be or currently is used by this resource |
Robert Phillips | 8bc06d0 | 2016-11-01 17:28:40 -0400 | [diff] [blame] | 342 | * in bytes. It is approximate since we aren't aware of additional padding or copies made |
| 343 | * by the driver. |
| 344 | * |
| 345 | * @return the amount of GPU memory used in bytes |
| 346 | */ |
| 347 | size_t gpuMemorySize() const { |
Chris Dalton | 706a6ff | 2017-11-29 22:01:06 -0700 | [diff] [blame] | 348 | SkASSERT(!this->isPendingLazyInstantiation()); |
Brian Salomon | bb5711a | 2017-05-17 13:49:59 -0400 | [diff] [blame] | 349 | if (fTarget) { |
| 350 | return fTarget->gpuMemorySize(); |
| 351 | } |
Robert Phillips | 8bc06d0 | 2016-11-01 17:28:40 -0400 | [diff] [blame] | 352 | if (kInvalidGpuMemorySize == fGpuMemorySize) { |
Brian Salomon | bb5711a | 2017-05-17 13:49:59 -0400 | [diff] [blame] | 353 | fGpuMemorySize = this->onUninstantiatedGpuMemorySize(); |
Robert Phillips | 8bc06d0 | 2016-11-01 17:28:40 -0400 | [diff] [blame] | 354 | SkASSERT(kInvalidGpuMemorySize != fGpuMemorySize); |
| 355 | } |
| 356 | return fGpuMemorySize; |
| 357 | } |
| 358 | |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 359 | // Helper function that creates a temporary SurfaceContext to perform the copy |
Robert Phillips | 0ae6faa | 2017-03-21 16:22:00 -0400 | [diff] [blame] | 360 | // It always returns a kExact-backed proxy bc it is used when converting an SkSpecialImage |
Brian Salomon | 63e7973 | 2017-05-15 21:23:13 -0400 | [diff] [blame] | 361 | // to an SkImage. The copy is is not a render target and not multisampled. |
Greg Daniel | 65c7f66 | 2017-10-30 13:39:09 -0400 | [diff] [blame] | 362 | static sk_sp<GrTextureProxy> Copy(GrContext*, GrSurfaceProxy* src, GrMipMapped, |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 363 | SkIRect srcRect, SkBudgeted); |
| 364 | |
| 365 | // Copy the entire 'src' |
Robert Phillips | 0ae6faa | 2017-03-21 16:22:00 -0400 | [diff] [blame] | 366 | // It always returns a kExact-backed proxy bc it is used in SkGpuDevice::snapSpecial |
Greg Daniel | 65c7f66 | 2017-10-30 13:39:09 -0400 | [diff] [blame] | 367 | static sk_sp<GrTextureProxy> Copy(GrContext* context, GrSurfaceProxy* src, GrMipMapped, |
Robert Phillips | 63c6746 | 2017-02-15 14:19:01 -0500 | [diff] [blame] | 368 | SkBudgeted budgeted); |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 369 | |
| 370 | // Test-only entry point - should decrease in use as proxies propagate |
Robert Phillips | d46697a | 2017-01-25 12:10:37 -0500 | [diff] [blame] | 371 | static sk_sp<GrSurfaceContext> TestCopy(GrContext* context, const GrSurfaceDesc& dstDesc, |
| 372 | GrSurfaceProxy* srcProxy); |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 373 | |
Robert Phillips | eaa8625 | 2016-11-08 13:49:39 +0000 | [diff] [blame] | 374 | bool isWrapped_ForTesting() const; |
| 375 | |
Brian Osman | 45580d3 | 2016-11-23 09:37:01 -0500 | [diff] [blame] | 376 | SkDEBUGCODE(void validate(GrContext*) const;) |
| 377 | |
Robert Phillips | 757914d | 2017-01-25 15:48:30 -0500 | [diff] [blame] | 378 | // Provides access to functions that aren't part of the public API. |
Robert Phillips | 420c4cf | 2017-09-28 09:00:45 -0400 | [diff] [blame] | 379 | inline GrSurfaceProxyPriv priv(); |
| 380 | inline const GrSurfaceProxyPriv priv() const; |
Robert Phillips | 757914d | 2017-01-25 15:48:30 -0500 | [diff] [blame] | 381 | |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 382 | protected: |
robertphillips | 8abb370 | 2016-08-31 14:04:06 -0700 | [diff] [blame] | 383 | // Deferred version |
Robert Phillips | c787e49 | 2017-02-28 11:26:32 -0500 | [diff] [blame] | 384 | GrSurfaceProxy(const GrSurfaceDesc& desc, SkBackingFit fit, SkBudgeted budgeted, uint32_t flags) |
Brian Salomon | bb5711a | 2017-05-17 13:49:59 -0400 | [diff] [blame] | 385 | : fConfig(desc.fConfig) |
| 386 | , fWidth(desc.fWidth) |
| 387 | , fHeight(desc.fHeight) |
| 388 | , fOrigin(desc.fOrigin) |
| 389 | , fFit(fit) |
| 390 | , fBudgeted(budgeted) |
| 391 | , fFlags(flags) |
Brian Salomon | d17b4a6 | 2017-05-23 16:53:47 -0400 | [diff] [blame] | 392 | , fNeedsClear(SkToBool(desc.fFlags & kPerformInitialClear_GrSurfaceFlag)) |
Brian Salomon | bb5711a | 2017-05-17 13:49:59 -0400 | [diff] [blame] | 393 | , fGpuMemorySize(kInvalidGpuMemorySize) |
| 394 | , fLastOpList(nullptr) { |
Robert Phillips | 294870f | 2016-11-11 12:38:40 -0500 | [diff] [blame] | 395 | // Note: this ctor pulls a new uniqueID from the same pool at the GrGpuResources |
robertphillips | 8abb370 | 2016-08-31 14:04:06 -0700 | [diff] [blame] | 396 | } |
| 397 | |
Chris Dalton | 706a6ff | 2017-11-29 22:01:06 -0700 | [diff] [blame] | 398 | // Lazy-callback version |
| 399 | GrSurfaceProxy(LazyInstantiateCallback&& callback, GrPixelConfig config); |
| 400 | |
robertphillips | 8abb370 | 2016-08-31 14:04:06 -0700 | [diff] [blame] | 401 | // Wrapped version |
Robert Phillips | 066f020 | 2017-07-25 10:16:35 -0400 | [diff] [blame] | 402 | GrSurfaceProxy(sk_sp<GrSurface> surface, GrSurfaceOrigin origin, SkBackingFit fit); |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 403 | |
Robert Phillips | f2361d2 | 2016-10-25 14:20:06 -0400 | [diff] [blame] | 404 | virtual ~GrSurfaceProxy(); |
| 405 | |
Robert Phillips | 757914d | 2017-01-25 15:48:30 -0500 | [diff] [blame] | 406 | friend class GrSurfaceProxyPriv; |
| 407 | |
| 408 | // Methods made available via GrSurfaceProxyPriv |
| 409 | bool hasPendingIO() const { |
| 410 | return this->internalHasPendingIO(); |
| 411 | } |
| 412 | |
Robert Phillips | 7ee385e | 2017-03-30 08:02:11 -0400 | [diff] [blame] | 413 | bool hasPendingWrite() const { |
| 414 | return this->internalHasPendingWrite(); |
| 415 | } |
| 416 | |
Robert Phillips | 57aa367 | 2017-07-21 11:38:13 -0400 | [diff] [blame] | 417 | void computeScratchKey(GrScratchKey*) const; |
| 418 | |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 419 | virtual sk_sp<GrSurface> createSurface(GrResourceProvider*) const = 0; |
| 420 | void assign(sk_sp<GrSurface> surface); |
| 421 | |
Robert Phillips | 6504813 | 2017-08-10 08:44:49 -0400 | [diff] [blame] | 422 | sk_sp<GrSurface> createSurfaceImpl(GrResourceProvider*, int sampleCnt, bool needsStencil, |
Greg Daniel | e252f08 | 2017-10-23 16:05:23 -0400 | [diff] [blame] | 423 | GrSurfaceFlags flags, GrMipMapped mipMapped, |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 424 | SkDestinationSurfaceColorMode mipColorMode) const; |
| 425 | |
Robert Phillips | 6504813 | 2017-08-10 08:44:49 -0400 | [diff] [blame] | 426 | bool instantiateImpl(GrResourceProvider* resourceProvider, int sampleCnt, bool needsStencil, |
Greg Daniel | e252f08 | 2017-10-23 16:05:23 -0400 | [diff] [blame] | 427 | GrSurfaceFlags flags, GrMipMapped mipMapped, |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 428 | SkDestinationSurfaceColorMode mipColorMode, const GrUniqueKey*); |
Brian Salomon | bb5711a | 2017-05-17 13:49:59 -0400 | [diff] [blame] | 429 | |
Chris Dalton | 706a6ff | 2017-11-29 22:01:06 -0700 | [diff] [blame] | 430 | private: |
Brian Salomon | bb5711a | 2017-05-17 13:49:59 -0400 | [diff] [blame] | 431 | // For wrapped resources, 'fConfig', 'fWidth', 'fHeight', and 'fOrigin; will always be filled in |
| 432 | // from the wrapped resource. |
| 433 | GrPixelConfig fConfig; |
| 434 | int fWidth; |
| 435 | int fHeight; |
| 436 | GrSurfaceOrigin fOrigin; |
Chris Dalton | 706a6ff | 2017-11-29 22:01:06 -0700 | [diff] [blame] | 437 | SkBackingFit fFit; // always kApprox for lazy-callback resources |
| 438 | // always kExact for wrapped resources |
| 439 | mutable SkBudgeted fBudgeted; // always kYes for lazy-callback resources |
| 440 | // set from the backing resource for wrapped resources |
Robert Phillips | b726d58 | 2017-03-09 16:36:32 -0500 | [diff] [blame] | 441 | // mutable bc of SkSurface/SkImage wishy-washiness |
Robert Phillips | c787e49 | 2017-02-28 11:26:32 -0500 | [diff] [blame] | 442 | const uint32_t fFlags; |
Robert Phillips | a4c41b3 | 2017-03-15 13:02:45 -0400 | [diff] [blame] | 443 | |
Robert Phillips | 294870f | 2016-11-11 12:38:40 -0500 | [diff] [blame] | 444 | const UniqueID fUniqueID; // set from the backing resource for wrapped resources |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 445 | |
Chris Dalton | 706a6ff | 2017-11-29 22:01:06 -0700 | [diff] [blame] | 446 | LazyInstantiateCallback fLazyInstantiateCallback; |
| 447 | SkDEBUGCODE(virtual void validateLazyTexture(const GrTexture*) = 0;) |
| 448 | |
Robert Phillips | 8bc06d0 | 2016-11-01 17:28:40 -0400 | [diff] [blame] | 449 | static const size_t kInvalidGpuMemorySize = ~static_cast<size_t>(0); |
Robert Phillips | 29e52f1 | 2016-11-03 10:19:14 -0400 | [diff] [blame] | 450 | SkDEBUGCODE(size_t getRawGpuMemorySize_debugOnly() const { return fGpuMemorySize; }) |
| 451 | |
Brian Salomon | bb5711a | 2017-05-17 13:49:59 -0400 | [diff] [blame] | 452 | virtual size_t onUninstantiatedGpuMemorySize() const = 0; |
Robert Phillips | 29e52f1 | 2016-11-03 10:19:14 -0400 | [diff] [blame] | 453 | |
Brian Salomon | d17b4a6 | 2017-05-23 16:53:47 -0400 | [diff] [blame] | 454 | bool fNeedsClear; |
| 455 | |
Robert Phillips | 8bc06d0 | 2016-11-01 17:28:40 -0400 | [diff] [blame] | 456 | // This entry is lazily evaluated so, when the proxy wraps a resource, the resource |
| 457 | // will be called but, when the proxy is deferred, it will compute the answer itself. |
| 458 | // If the proxy computes its own answer that answer is checked (in debug mode) in |
| 459 | // the instantiation method. |
| 460 | mutable size_t fGpuMemorySize; |
| 461 | |
Robert Phillips | f2361d2 | 2016-10-25 14:20:06 -0400 | [diff] [blame] | 462 | // The last opList that wrote to or is currently going to write to this surface |
Robert Phillips | b6deea8 | 2017-05-11 14:14:30 -0400 | [diff] [blame] | 463 | // The opList can be closed (e.g., no surface context is currently bound |
| 464 | // to this proxy). |
Robert Phillips | f2361d2 | 2016-10-25 14:20:06 -0400 | [diff] [blame] | 465 | // This back-pointer is required so that we can add a dependancy between |
| 466 | // the opList used to create the current contents of this surface |
| 467 | // and the opList of a destination surface to which this one is being drawn or copied. |
Robert Phillips | 6cdc22c | 2017-05-11 16:29:14 -0400 | [diff] [blame] | 468 | // This pointer is unreffed. OpLists own a ref on their surface proxies. |
Robert Phillips | f2361d2 | 2016-10-25 14:20:06 -0400 | [diff] [blame] | 469 | GrOpList* fLastOpList; |
| 470 | |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 471 | typedef GrIORefProxy INHERITED; |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 472 | }; |
| 473 | |
| 474 | #endif |