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 | |
Greg Daniel | f91aeb2 | 2019-06-18 09:58:02 -0400 | [diff] [blame] | 8 | #include "src/gpu/GrSurfaceProxy.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 9 | #include "src/gpu/GrSurfaceProxyPriv.h" |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 10 | |
Robert Phillips | b7bfbc2 | 2020-07-01 12:55:01 -0400 | [diff] [blame] | 11 | #include "include/gpu/GrRecordingContext.h" |
Brian Salomon | 947efe2 | 2019-07-16 15:36:11 -0400 | [diff] [blame] | 12 | #include "src/core/SkMathPriv.h" |
Mike Reed | 13711eb | 2020-07-14 17:16:32 -0400 | [diff] [blame] | 13 | #include "src/core/SkMipmap.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 14 | #include "src/gpu/GrCaps.h" |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 15 | #include "src/gpu/GrClip.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 16 | #include "src/gpu/GrGpuResourcePriv.h" |
Greg Daniel | f41b2bd | 2019-08-22 16:19:24 -0400 | [diff] [blame] | 17 | #include "src/gpu/GrOpsTask.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 18 | #include "src/gpu/GrProxyProvider.h" |
| 19 | #include "src/gpu/GrRecordingContextPriv.h" |
Greg Daniel | e20fcad | 2020-01-08 11:52:34 -0500 | [diff] [blame] | 20 | #include "src/gpu/GrRenderTargetContext.h" |
Chris Dalton | effee20 | 2019-07-01 22:28:03 -0600 | [diff] [blame] | 21 | #include "src/gpu/GrStencilAttachment.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 22 | #include "src/gpu/GrSurfacePriv.h" |
| 23 | #include "src/gpu/GrTexturePriv.h" |
| 24 | #include "src/gpu/GrTextureRenderTargetProxy.h" |
Robert Phillips | f2361d2 | 2016-10-25 14:20:06 -0400 | [diff] [blame] | 25 | |
Greg Daniel | 65fa8ca | 2018-01-10 17:06:31 -0500 | [diff] [blame] | 26 | #ifdef SK_DEBUG |
Robert Phillips | 4e105e2 | 2020-07-16 09:18:50 -0400 | [diff] [blame] | 27 | #include "include/gpu/GrDirectContext.h" |
| 28 | #include "src/gpu/GrContextPriv.h" |
Brian Salomon | 201cdbb | 2019-08-14 17:00:30 -0400 | [diff] [blame] | 29 | #include "src/gpu/GrRenderTarget.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 30 | #include "src/gpu/GrRenderTargetPriv.h" |
Robert Phillips | c7c2baf | 2018-03-08 09:51:04 -0500 | [diff] [blame] | 31 | |
Brian Salomon | a56a746 | 2020-02-07 14:17:25 -0500 | [diff] [blame] | 32 | static bool is_valid_lazy(const SkISize& dimensions, SkBackingFit fit) { |
Brian Salomon | 1d19da0 | 2019-09-11 17:39:30 -0400 | [diff] [blame] | 33 | // A "fully" lazy proxy's width and height are not known until instantiation time. |
| 34 | // So fully lazy proxies are created with width and height < 0. Regular lazy proxies must be |
| 35 | // created with positive widths and heights. The width and height are set to 0 only after a |
| 36 | // failed instantiation. The former must be "approximate" fit while the latter can be either. |
Brian Salomon | a56a746 | 2020-02-07 14:17:25 -0500 | [diff] [blame] | 37 | return ((dimensions.fWidth < 0 && dimensions.fHeight < 0 && SkBackingFit::kApprox == fit) || |
| 38 | (dimensions.fWidth > 0 && dimensions.fHeight > 0)); |
Greg Daniel | 65fa8ca | 2018-01-10 17:06:31 -0500 | [diff] [blame] | 39 | } |
| 40 | |
Brian Salomon | a56a746 | 2020-02-07 14:17:25 -0500 | [diff] [blame] | 41 | static bool is_valid_non_lazy(SkISize dimensions) { |
| 42 | return dimensions.fWidth > 0 && dimensions.fHeight > 0; |
Greg Daniel | 65fa8ca | 2018-01-10 17:06:31 -0500 | [diff] [blame] | 43 | } |
| 44 | #endif |
| 45 | |
Brian Salomon | beb7f52 | 2019-08-30 16:19:42 -0400 | [diff] [blame] | 46 | // Deferred version |
| 47 | GrSurfaceProxy::GrSurfaceProxy(const GrBackendFormat& format, |
Brian Salomon | a56a746 | 2020-02-07 14:17:25 -0500 | [diff] [blame] | 48 | SkISize dimensions, |
Brian Salomon | beb7f52 | 2019-08-30 16:19:42 -0400 | [diff] [blame] | 49 | SkBackingFit fit, |
| 50 | SkBudgeted budgeted, |
| 51 | GrProtected isProtected, |
| 52 | GrInternalSurfaceFlags surfaceFlags, |
| 53 | UseAllocator useAllocator) |
Greg Daniel | e320486 | 2018-04-16 11:24:10 -0400 | [diff] [blame] | 54 | : fSurfaceFlags(surfaceFlags) |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 55 | , fFormat(format) |
Brian Salomon | a56a746 | 2020-02-07 14:17:25 -0500 | [diff] [blame] | 56 | , fDimensions(dimensions) |
Greg Daniel | 65fa8ca | 2018-01-10 17:06:31 -0500 | [diff] [blame] | 57 | , fFit(fit) |
| 58 | , fBudgeted(budgeted) |
Brian Salomon | beb7f52 | 2019-08-30 16:19:42 -0400 | [diff] [blame] | 59 | , fUseAllocator(useAllocator) |
Brian Salomon | e8a766b | 2019-07-19 14:24:36 -0400 | [diff] [blame] | 60 | , fIsProtected(isProtected) |
Brian Salomon | beb7f52 | 2019-08-30 16:19:42 -0400 | [diff] [blame] | 61 | , fGpuMemorySize(kInvalidGpuMemorySize) { |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 62 | SkASSERT(fFormat.isValid()); |
Brian Salomon | a56a746 | 2020-02-07 14:17:25 -0500 | [diff] [blame] | 63 | SkASSERT(is_valid_non_lazy(dimensions)); |
Brian Salomon | beb7f52 | 2019-08-30 16:19:42 -0400 | [diff] [blame] | 64 | } |
Jim Van Verth | 1676cb9 | 2019-01-15 13:24:45 -0500 | [diff] [blame] | 65 | |
Brian Salomon | beb7f52 | 2019-08-30 16:19:42 -0400 | [diff] [blame] | 66 | // Lazy-callback version |
| 67 | GrSurfaceProxy::GrSurfaceProxy(LazyInstantiateCallback&& callback, |
| 68 | const GrBackendFormat& format, |
Brian Salomon | a56a746 | 2020-02-07 14:17:25 -0500 | [diff] [blame] | 69 | SkISize dimensions, |
Brian Salomon | beb7f52 | 2019-08-30 16:19:42 -0400 | [diff] [blame] | 70 | SkBackingFit fit, |
| 71 | SkBudgeted budgeted, |
| 72 | GrProtected isProtected, |
| 73 | GrInternalSurfaceFlags surfaceFlags, |
| 74 | UseAllocator useAllocator) |
| 75 | : fSurfaceFlags(surfaceFlags) |
| 76 | , fFormat(format) |
Brian Salomon | a56a746 | 2020-02-07 14:17:25 -0500 | [diff] [blame] | 77 | , fDimensions(dimensions) |
Brian Salomon | beb7f52 | 2019-08-30 16:19:42 -0400 | [diff] [blame] | 78 | , fFit(fit) |
| 79 | , fBudgeted(budgeted) |
| 80 | , fUseAllocator(useAllocator) |
| 81 | , fLazyInstantiateCallback(std::move(callback)) |
| 82 | , fIsProtected(isProtected) |
| 83 | , fGpuMemorySize(kInvalidGpuMemorySize) { |
| 84 | SkASSERT(fFormat.isValid()); |
| 85 | SkASSERT(fLazyInstantiateCallback); |
Brian Salomon | a56a746 | 2020-02-07 14:17:25 -0500 | [diff] [blame] | 86 | SkASSERT(is_valid_lazy(dimensions, fit)); |
Chris Dalton | 706a6ff | 2017-11-29 22:01:06 -0700 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | // Wrapped version |
Brian Salomon | beb7f52 | 2019-08-30 16:19:42 -0400 | [diff] [blame] | 90 | GrSurfaceProxy::GrSurfaceProxy(sk_sp<GrSurface> surface, |
Brian Salomon | beb7f52 | 2019-08-30 16:19:42 -0400 | [diff] [blame] | 91 | SkBackingFit fit, |
| 92 | UseAllocator useAllocator) |
Robert Phillips | b520476 | 2019-06-19 14:12:13 -0400 | [diff] [blame] | 93 | : fTarget(std::move(surface)) |
Greg Daniel | e320486 | 2018-04-16 11:24:10 -0400 | [diff] [blame] | 94 | , fSurfaceFlags(fTarget->surfacePriv().flags()) |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 95 | , fFormat(fTarget->backendFormat()) |
Brian Salomon | 9f2b86c | 2019-10-22 10:37:46 -0400 | [diff] [blame] | 96 | , fDimensions(fTarget->dimensions()) |
Brian Salomon | bb5711a | 2017-05-17 13:49:59 -0400 | [diff] [blame] | 97 | , fFit(fit) |
Brian Salomon | fa2ebea | 2019-01-24 15:58:58 -0500 | [diff] [blame] | 98 | , fBudgeted(fTarget->resourcePriv().budgetedType() == GrBudgetedType::kBudgeted |
| 99 | ? SkBudgeted::kYes |
| 100 | : SkBudgeted::kNo) |
Brian Salomon | beb7f52 | 2019-08-30 16:19:42 -0400 | [diff] [blame] | 101 | , fUseAllocator(useAllocator) |
Brian Salomon | bb5711a | 2017-05-17 13:49:59 -0400 | [diff] [blame] | 102 | , fUniqueID(fTarget->uniqueID()) // Note: converting from unique resource ID to a proxy ID! |
Emircan Uysaler | 23ca4e7 | 2019-06-24 10:53:09 -0400 | [diff] [blame] | 103 | , fIsProtected(fTarget->isProtected() ? GrProtected::kYes : GrProtected::kNo) |
Brian Salomon | beb7f52 | 2019-08-30 16:19:42 -0400 | [diff] [blame] | 104 | , fGpuMemorySize(kInvalidGpuMemorySize) { |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 105 | SkASSERT(fFormat.isValid()); |
Robert Phillips | 019ff27 | 2017-07-24 14:47:57 -0400 | [diff] [blame] | 106 | } |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 107 | |
Robert Phillips | f2361d2 | 2016-10-25 14:20:06 -0400 | [diff] [blame] | 108 | GrSurfaceProxy::~GrSurfaceProxy() { |
Robert Phillips | f2361d2 | 2016-10-25 14:20:06 -0400 | [diff] [blame] | 109 | } |
| 110 | |
Robert Phillips | ba5c439 | 2018-07-25 12:37:14 -0400 | [diff] [blame] | 111 | sk_sp<GrSurface> GrSurfaceProxy::createSurfaceImpl(GrResourceProvider* resourceProvider, |
Brian Salomon | 4eb38b7 | 2019-08-05 12:58:39 -0400 | [diff] [blame] | 112 | int sampleCnt, |
Brian Salomon | f2c2ba9 | 2019-07-17 09:59:59 -0400 | [diff] [blame] | 113 | GrRenderable renderable, |
Brian Salomon | 7e67dca | 2020-07-21 09:27:25 -0400 | [diff] [blame] | 114 | GrMipmapped mipMapped) const { |
| 115 | SkASSERT(mipMapped == GrMipmapped::kNo || fFit == SkBackingFit::kExact); |
Brian Salomon | beb7f52 | 2019-08-30 16:19:42 -0400 | [diff] [blame] | 116 | SkASSERT(!this->isLazy()); |
Greg Daniel | d2d8e92 | 2018-02-12 12:07:39 -0500 | [diff] [blame] | 117 | SkASSERT(!fTarget); |
Robert Phillips | eaa8625 | 2016-11-08 13:49:39 +0000 | [diff] [blame] | 118 | |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 119 | sk_sp<GrSurface> surface; |
Brian Salomon | a90382f | 2019-09-17 09:01:56 -0400 | [diff] [blame] | 120 | if (SkBackingFit::kApprox == fFit) { |
Brian Salomon | a56a746 | 2020-02-07 14:17:25 -0500 | [diff] [blame] | 121 | surface = resourceProvider->createApproxTexture(fDimensions, fFormat, renderable, sampleCnt, |
Brian Salomon | a90382f | 2019-09-17 09:01:56 -0400 | [diff] [blame] | 122 | fIsProtected); |
Robert Phillips | eaa8625 | 2016-11-08 13:49:39 +0000 | [diff] [blame] | 123 | } else { |
Brian Salomon | a56a746 | 2020-02-07 14:17:25 -0500 | [diff] [blame] | 124 | surface = resourceProvider->createTexture(fDimensions, fFormat, renderable, sampleCnt, |
| 125 | mipMapped, fBudgeted, fIsProtected); |
Robert Phillips | eaa8625 | 2016-11-08 13:49:39 +0000 | [diff] [blame] | 126 | } |
Robert Phillips | 6504813 | 2017-08-10 08:44:49 -0400 | [diff] [blame] | 127 | if (!surface) { |
| 128 | return nullptr; |
| 129 | } |
| 130 | |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 131 | return surface; |
| 132 | } |
| 133 | |
Brian Salomon | 7d94bb5 | 2018-10-12 14:37:19 -0400 | [diff] [blame] | 134 | bool GrSurfaceProxy::canSkipResourceAllocator() const { |
Brian Salomon | beb7f52 | 2019-08-30 16:19:42 -0400 | [diff] [blame] | 135 | if (fUseAllocator == UseAllocator::kNo) { |
Robert Phillips | 5f78adf | 2019-04-22 12:41:39 -0400 | [diff] [blame] | 136 | // Usually an atlas or onFlush proxy |
| 137 | return true; |
| 138 | } |
| 139 | |
Brian Salomon | 7d94bb5 | 2018-10-12 14:37:19 -0400 | [diff] [blame] | 140 | auto peek = this->peekSurface(); |
| 141 | if (!peek) { |
| 142 | return false; |
| 143 | } |
| 144 | // If this resource is already allocated and not recyclable then the resource allocator does |
| 145 | // not need to do anything with it. |
| 146 | return !peek->resourcePriv().getScratchKey().isValid(); |
| 147 | } |
| 148 | |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 149 | void GrSurfaceProxy::assign(sk_sp<GrSurface> surface) { |
| 150 | SkASSERT(!fTarget && surface); |
Robert Phillips | abf7b76 | 2018-03-21 12:13:37 -0400 | [diff] [blame] | 151 | |
Greg Daniel | 849dce1 | 2018-04-24 14:32:53 -0400 | [diff] [blame] | 152 | SkDEBUGCODE(this->validateSurface(surface.get());) |
Robert Phillips | abf7b76 | 2018-03-21 12:13:37 -0400 | [diff] [blame] | 153 | |
Robert Phillips | e5f7328 | 2019-06-18 17:15:04 -0400 | [diff] [blame] | 154 | fTarget = std::move(surface); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 155 | |
Robert Phillips | eaa8625 | 2016-11-08 13:49:39 +0000 | [diff] [blame] | 156 | #ifdef SK_DEBUG |
Robert Phillips | c7c2baf | 2018-03-08 09:51:04 -0500 | [diff] [blame] | 157 | if (this->asRenderTargetProxy()) { |
| 158 | SkASSERT(fTarget->asRenderTarget()); |
Robert Phillips | c7c2baf | 2018-03-08 09:51:04 -0500 | [diff] [blame] | 159 | } |
| 160 | |
Robert Phillips | eaa8625 | 2016-11-08 13:49:39 +0000 | [diff] [blame] | 161 | if (kInvalidGpuMemorySize != this->getRawGpuMemorySize_debugOnly()) { |
Robert Phillips | 784b7bf | 2016-12-09 13:35:02 -0500 | [diff] [blame] | 162 | SkASSERT(fTarget->gpuMemorySize() <= this->getRawGpuMemorySize_debugOnly()); |
Robert Phillips | eaa8625 | 2016-11-08 13:49:39 +0000 | [diff] [blame] | 163 | } |
| 164 | #endif |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 165 | } |
Robert Phillips | eaa8625 | 2016-11-08 13:49:39 +0000 | [diff] [blame] | 166 | |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 167 | bool GrSurfaceProxy::instantiateImpl(GrResourceProvider* resourceProvider, int sampleCnt, |
Brian Salomon | 7e67dca | 2020-07-21 09:27:25 -0400 | [diff] [blame] | 168 | GrRenderable renderable, GrMipmapped mipMapped, |
Chris Dalton | 0b68dda | 2019-11-07 21:08:03 -0700 | [diff] [blame] | 169 | const GrUniqueKey* uniqueKey) { |
Brian Salomon | beb7f52 | 2019-08-30 16:19:42 -0400 | [diff] [blame] | 170 | SkASSERT(!this->isLazy()); |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 171 | if (fTarget) { |
Brian Salomon | 5790420 | 2018-12-17 14:45:00 -0500 | [diff] [blame] | 172 | if (uniqueKey && uniqueKey->isValid()) { |
| 173 | SkASSERT(fTarget->getUniqueKey().isValid() && fTarget->getUniqueKey() == *uniqueKey); |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 174 | } |
Chris Dalton | 0b68dda | 2019-11-07 21:08:03 -0700 | [diff] [blame] | 175 | return true; |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 176 | } |
| 177 | |
Chris Dalton | 0b68dda | 2019-11-07 21:08:03 -0700 | [diff] [blame] | 178 | sk_sp<GrSurface> surface = this->createSurfaceImpl(resourceProvider, sampleCnt, renderable, |
| 179 | mipMapped); |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 180 | if (!surface) { |
| 181 | return false; |
| 182 | } |
| 183 | |
Brian Osman | 28c434b | 2017-09-27 13:11:16 -0400 | [diff] [blame] | 184 | // If there was an invalidation message pending for this key, we might have just processed it, |
| 185 | // causing the key (stored on this proxy) to become invalid. |
| 186 | if (uniqueKey && uniqueKey->isValid()) { |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 187 | resourceProvider->assignUniqueKeyToResource(*uniqueKey, surface.get()); |
| 188 | } |
| 189 | |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 190 | this->assign(std::move(surface)); |
Robert Phillips | fe0253f | 2018-03-16 16:47:25 -0400 | [diff] [blame] | 191 | |
Robert Phillips | eee4d6e | 2017-06-05 09:26:07 -0400 | [diff] [blame] | 192 | return true; |
Robert Phillips | eaa8625 | 2016-11-08 13:49:39 +0000 | [diff] [blame] | 193 | } |
| 194 | |
Brian Salomon | 967df20 | 2018-12-07 11:15:53 -0500 | [diff] [blame] | 195 | void GrSurfaceProxy::deinstantiate() { |
Brian Salomon | fd98c2c | 2018-07-31 17:25:29 -0400 | [diff] [blame] | 196 | SkASSERT(this->isInstantiated()); |
Robert Phillips | e5f7328 | 2019-06-18 17:15:04 -0400 | [diff] [blame] | 197 | fTarget = nullptr; |
Robert Phillips | 4bc7011 | 2018-03-01 10:24:02 -0500 | [diff] [blame] | 198 | } |
| 199 | |
Greg Daniel | d51fa2f | 2020-01-22 16:53:38 -0500 | [diff] [blame] | 200 | void GrSurfaceProxy::computeScratchKey(const GrCaps& caps, GrScratchKey* key) const { |
Brian Salomon | beb7f52 | 2019-08-30 16:19:42 -0400 | [diff] [blame] | 201 | SkASSERT(!this->isFullyLazy()); |
Brian Salomon | f2c2ba9 | 2019-07-17 09:59:59 -0400 | [diff] [blame] | 202 | GrRenderable renderable = GrRenderable::kNo; |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 203 | int sampleCount = 1; |
Brian Salomon | f2c2ba9 | 2019-07-17 09:59:59 -0400 | [diff] [blame] | 204 | if (const auto* rtp = this->asRenderTargetProxy()) { |
| 205 | renderable = GrRenderable::kYes; |
Chris Dalton | 6ce447a | 2019-06-23 18:07:38 -0600 | [diff] [blame] | 206 | sampleCount = rtp->numSamples(); |
Robert Phillips | 57aa367 | 2017-07-21 11:38:13 -0400 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | const GrTextureProxy* tp = this->asTextureProxy(); |
Brian Salomon | 7e67dca | 2020-07-21 09:27:25 -0400 | [diff] [blame] | 210 | GrMipmapped mipMapped = GrMipmapped::kNo; |
Robert Phillips | 57aa367 | 2017-07-21 11:38:13 -0400 | [diff] [blame] | 211 | if (tp) { |
Brian Salomon | 8c82a87 | 2020-07-21 12:09:58 -0400 | [diff] [blame^] | 212 | mipMapped = tp->mipmapped(); |
Robert Phillips | 57aa367 | 2017-07-21 11:38:13 -0400 | [diff] [blame] | 213 | } |
| 214 | |
Greg Daniel | d51fa2f | 2020-01-22 16:53:38 -0500 | [diff] [blame] | 215 | GrTexturePriv::ComputeScratchKey(caps, this->backendFormat(), this->backingStoreDimensions(), |
| 216 | renderable, sampleCount, mipMapped, fIsProtected, key); |
Robert Phillips | 57aa367 | 2017-07-21 11:38:13 -0400 | [diff] [blame] | 217 | } |
| 218 | |
Robert Phillips | e9462dd | 2019-10-23 12:41:29 -0400 | [diff] [blame] | 219 | SkISize GrSurfaceProxy::backingStoreDimensions() const { |
Brian Salomon | beb7f52 | 2019-08-30 16:19:42 -0400 | [diff] [blame] | 220 | SkASSERT(!this->isFullyLazy()); |
Robert Phillips | a108c92 | 2017-10-10 10:42:19 -0400 | [diff] [blame] | 221 | if (fTarget) { |
Brian Salomon | 9f2b86c | 2019-10-22 10:37:46 -0400 | [diff] [blame] | 222 | return fTarget->dimensions(); |
Robert Phillips | a108c92 | 2017-10-10 10:42:19 -0400 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | if (SkBackingFit::kExact == fFit) { |
Brian Salomon | 9f2b86c | 2019-10-22 10:37:46 -0400 | [diff] [blame] | 226 | return fDimensions; |
Robert Phillips | a108c92 | 2017-10-10 10:42:19 -0400 | [diff] [blame] | 227 | } |
Brian Salomon | 9f2b86c | 2019-10-22 10:37:46 -0400 | [diff] [blame] | 228 | return GrResourceProvider::MakeApprox(fDimensions); |
Robert Phillips | a108c92 | 2017-10-10 10:42:19 -0400 | [diff] [blame] | 229 | } |
| 230 | |
Brian Salomon | 5c60b75 | 2019-12-13 15:03:43 -0500 | [diff] [blame] | 231 | bool GrSurfaceProxy::isFunctionallyExact() const { |
| 232 | SkASSERT(!this->isFullyLazy()); |
| 233 | return fFit == SkBackingFit::kExact || |
| 234 | fDimensions == GrResourceProvider::MakeApprox(fDimensions); |
| 235 | } |
| 236 | |
Greg Daniel | 82c6b10 | 2020-01-21 10:33:22 -0500 | [diff] [blame] | 237 | bool GrSurfaceProxy::isFormatCompressed(const GrCaps* caps) const { |
| 238 | return caps->isFormatCompressed(this->backendFormat()); |
| 239 | } |
| 240 | |
Brian Osman | 45580d3 | 2016-11-23 09:37:01 -0500 | [diff] [blame] | 241 | #ifdef SK_DEBUG |
Robert Phillips | 292a6b2 | 2019-02-14 14:49:02 -0500 | [diff] [blame] | 242 | void GrSurfaceProxy::validate(GrContext_Base* context) const { |
Brian Osman | 45580d3 | 2016-11-23 09:37:01 -0500 | [diff] [blame] | 243 | if (fTarget) { |
Adlai Holler | b81eeba | 2020-06-09 15:20:25 -0400 | [diff] [blame] | 244 | SkASSERT(fTarget->getContext()->priv().matches(context)); |
Brian Osman | 45580d3 | 2016-11-23 09:37:01 -0500 | [diff] [blame] | 245 | } |
Brian Osman | 45580d3 | 2016-11-23 09:37:01 -0500 | [diff] [blame] | 246 | } |
| 247 | #endif |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 248 | |
Brian Salomon | c524378 | 2020-04-02 12:50:34 -0400 | [diff] [blame] | 249 | sk_sp<GrSurfaceProxy> GrSurfaceProxy::Copy(GrRecordingContext* context, |
| 250 | GrSurfaceProxy* src, |
| 251 | GrSurfaceOrigin origin, |
Brian Salomon | 7e67dca | 2020-07-21 09:27:25 -0400 | [diff] [blame] | 252 | GrMipmapped mipMapped, |
Brian Salomon | c524378 | 2020-04-02 12:50:34 -0400 | [diff] [blame] | 253 | SkIRect srcRect, |
| 254 | SkBackingFit fit, |
| 255 | SkBudgeted budgeted, |
| 256 | RectsMustMatch rectsMustMatch) { |
Brian Salomon | beb7f52 | 2019-08-30 16:19:42 -0400 | [diff] [blame] | 257 | SkASSERT(!src->isFullyLazy()); |
Brian Salomon | 947efe2 | 2019-07-16 15:36:11 -0400 | [diff] [blame] | 258 | int width; |
| 259 | int height; |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 260 | |
| 261 | SkIPoint dstPoint; |
| 262 | if (rectsMustMatch == RectsMustMatch::kYes) { |
Brian Salomon | 947efe2 | 2019-07-16 15:36:11 -0400 | [diff] [blame] | 263 | width = src->width(); |
| 264 | height = src->height(); |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 265 | dstPoint = {srcRect.fLeft, srcRect.fTop}; |
| 266 | } else { |
Brian Salomon | 947efe2 | 2019-07-16 15:36:11 -0400 | [diff] [blame] | 267 | width = srcRect.width(); |
| 268 | height = srcRect.height(); |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 269 | dstPoint = {0, 0}; |
| 270 | } |
| 271 | |
Brian Salomon | 9f2b86c | 2019-10-22 10:37:46 -0400 | [diff] [blame] | 272 | if (!srcRect.intersect(SkIRect::MakeSize(src->dimensions()))) { |
Greg Daniel | 40903af | 2020-01-30 14:55:05 -0500 | [diff] [blame] | 273 | return {}; |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 274 | } |
Greg Daniel | bfa19c4 | 2019-12-19 16:41:40 -0500 | [diff] [blame] | 275 | auto format = src->backendFormat().makeTexture2D(); |
| 276 | SkASSERT(format.isValid()); |
Greg Daniel | bfa19c4 | 2019-12-19 16:41:40 -0500 | [diff] [blame] | 277 | |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 278 | if (src->backendFormat().textureType() != GrTextureType::kExternal) { |
Brian Salomon | c524378 | 2020-04-02 12:50:34 -0400 | [diff] [blame] | 279 | auto dstContext = |
| 280 | GrSurfaceContext::Make(context, {width, height}, format, GrRenderable::kNo, 1, |
| 281 | mipMapped, src->isProtected(), origin, GrColorType::kUnknown, |
| 282 | kUnknown_SkAlphaType, nullptr, fit, budgeted); |
| 283 | if (dstContext && dstContext->copy(src, srcRect, dstPoint)) { |
| 284 | return dstContext->asSurfaceProxyRef(); |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 285 | } |
Greg Daniel | c5167c0 | 2019-06-05 17:36:53 +0000 | [diff] [blame] | 286 | } |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 287 | if (src->asTextureProxy()) { |
Brian Salomon | c524378 | 2020-04-02 12:50:34 -0400 | [diff] [blame] | 288 | auto dstContext = GrRenderTargetContext::Make( |
| 289 | context, GrColorType::kUnknown, nullptr, fit, {width, height}, format, 1, mipMapped, |
| 290 | src->isProtected(), origin, budgeted, nullptr); |
| 291 | GrSurfaceProxyView view(sk_ref_sp(src), origin, GrSwizzle("rgba")); |
Greg Daniel | 573312e | 2020-02-07 17:22:35 -0500 | [diff] [blame] | 292 | if (dstContext && dstContext->blitTexture(std::move(view), srcRect, dstPoint)) { |
Brian Salomon | c524378 | 2020-04-02 12:50:34 -0400 | [diff] [blame] | 293 | return dstContext->asSurfaceProxyRef(); |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 294 | } |
Greg Daniel | 4c6f9b7 | 2019-06-06 13:40:59 -0400 | [diff] [blame] | 295 | } |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 296 | // Can't use backend copies or draws. |
Brian Salomon | c524378 | 2020-04-02 12:50:34 -0400 | [diff] [blame] | 297 | return nullptr; |
Robert Phillips | 63c6746 | 2017-02-15 14:19:01 -0500 | [diff] [blame] | 298 | } |
| 299 | |
Brian Salomon | c524378 | 2020-04-02 12:50:34 -0400 | [diff] [blame] | 300 | sk_sp<GrSurfaceProxy> GrSurfaceProxy::Copy(GrRecordingContext* context, |
| 301 | GrSurfaceProxy* src, |
| 302 | GrSurfaceOrigin origin, |
Brian Salomon | 7e67dca | 2020-07-21 09:27:25 -0400 | [diff] [blame] | 303 | GrMipmapped mipMapped, |
Brian Salomon | c524378 | 2020-04-02 12:50:34 -0400 | [diff] [blame] | 304 | SkBackingFit fit, |
| 305 | SkBudgeted budgeted) { |
Brian Salomon | beb7f52 | 2019-08-30 16:19:42 -0400 | [diff] [blame] | 306 | SkASSERT(!src->isFullyLazy()); |
Brian Salomon | c524378 | 2020-04-02 12:50:34 -0400 | [diff] [blame] | 307 | return Copy(context, src, origin, mipMapped, SkIRect::MakeSize(src->dimensions()), fit, |
| 308 | budgeted); |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 309 | } |
| 310 | |
Robert Phillips | b520476 | 2019-06-19 14:12:13 -0400 | [diff] [blame] | 311 | #if GR_TEST_UTILS |
| 312 | int32_t GrSurfaceProxy::testingOnly_getBackingRefCnt() const { |
| 313 | if (fTarget) { |
| 314 | return fTarget->testingOnly_getRefCnt(); |
| 315 | } |
| 316 | |
| 317 | return -1; // no backing GrSurface |
| 318 | } |
| 319 | |
| 320 | GrInternalSurfaceFlags GrSurfaceProxy::testingOnly_getFlags() const { |
| 321 | return fSurfaceFlags; |
| 322 | } |
| 323 | #endif |
| 324 | |
Robert Phillips | e897684 | 2019-08-09 08:27:26 -0400 | [diff] [blame] | 325 | void GrSurfaceProxyPriv::exactify(bool allocatedCaseOnly) { |
Brian Salomon | beb7f52 | 2019-08-30 16:19:42 -0400 | [diff] [blame] | 326 | SkASSERT(!fProxy->isFullyLazy()); |
Robert Phillips | 0ae6faa | 2017-03-21 16:22:00 -0400 | [diff] [blame] | 327 | if (this->isExact()) { |
| 328 | return; |
| 329 | } |
| 330 | |
| 331 | SkASSERT(SkBackingFit::kApprox == fProxy->fFit); |
| 332 | |
| 333 | if (fProxy->fTarget) { |
| 334 | // The kApprox but already instantiated case. Setting the proxy's width & height to |
| 335 | // the instantiated width & height could have side-effects going forward, since we're |
Ben Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 336 | // obliterating the area of interest information. This call (exactify) only used |
Robert Phillips | 0ae6faa | 2017-03-21 16:22:00 -0400 | [diff] [blame] | 337 | // when converting an SkSpecialImage to an SkImage so the proxy shouldn't be |
| 338 | // used for additional draws. |
Brian Salomon | 9f2b86c | 2019-10-22 10:37:46 -0400 | [diff] [blame] | 339 | fProxy->fDimensions = fProxy->fTarget->dimensions(); |
Robert Phillips | 0ae6faa | 2017-03-21 16:22:00 -0400 | [diff] [blame] | 340 | return; |
| 341 | } |
| 342 | |
Robert Phillips | e897684 | 2019-08-09 08:27:26 -0400 | [diff] [blame] | 343 | #ifndef SK_CRIPPLE_TEXTURE_REUSE |
| 344 | // In the post-implicit-allocation world we can't convert this proxy to be exact fit |
| 345 | // at this point. With explicit allocation switching this to exact will result in a |
| 346 | // different allocation at flush time. With implicit allocation, allocation would occur |
| 347 | // at draw time (rather than flush time) so this pathway was encountered less often (if |
| 348 | // at all). |
| 349 | if (allocatedCaseOnly) { |
| 350 | return; |
| 351 | } |
| 352 | #endif |
| 353 | |
Robert Phillips | 0ae6faa | 2017-03-21 16:22:00 -0400 | [diff] [blame] | 354 | // The kApprox uninstantiated case. Making this proxy be exact should be okay. |
| 355 | // It could mess things up if prior decisions were based on the approximate size. |
| 356 | fProxy->fFit = SkBackingFit::kExact; |
| 357 | // If fGpuMemorySize is used when caching specialImages for the image filter DAG. If it has |
Robert Phillips | 6cdc22c | 2017-05-11 16:29:14 -0400 | [diff] [blame] | 358 | // already been computed we want to leave it alone so that amount will be removed when |
Robert Phillips | 0ae6faa | 2017-03-21 16:22:00 -0400 | [diff] [blame] | 359 | // the special image goes away. If it hasn't been computed yet it might as well compute the |
| 360 | // exact amount. |
| 361 | } |
| 362 | |
Greg Daniel | bddcc95 | 2018-01-24 13:22:24 -0500 | [diff] [blame] | 363 | bool GrSurfaceProxyPriv::doLazyInstantiation(GrResourceProvider* resourceProvider) { |
Brian Salomon | beb7f52 | 2019-08-30 16:19:42 -0400 | [diff] [blame] | 364 | SkASSERT(fProxy->isLazy()); |
Chris Dalton | 706a6ff | 2017-11-29 22:01:06 -0700 | [diff] [blame] | 365 | |
Robert Phillips | 0790f8a | 2018-09-18 13:11:03 -0400 | [diff] [blame] | 366 | sk_sp<GrSurface> surface; |
| 367 | if (fProxy->asTextureProxy() && fProxy->asTextureProxy()->getUniqueKey().isValid()) { |
| 368 | // First try to reattach to a cached version if the proxy is uniquely keyed |
| 369 | surface = resourceProvider->findByUniqueKey<GrSurface>( |
| 370 | fProxy->asTextureProxy()->getUniqueKey()); |
| 371 | } |
| 372 | |
Brian Salomon | b6a3a3b | 2019-04-01 12:29:34 -0400 | [diff] [blame] | 373 | bool syncKey = true; |
Brian Salomon | beb7f52 | 2019-08-30 16:19:42 -0400 | [diff] [blame] | 374 | bool releaseCallback = false; |
Robert Phillips | 0790f8a | 2018-09-18 13:11:03 -0400 | [diff] [blame] | 375 | if (!surface) { |
Brian Salomon | 63410e9 | 2020-03-23 18:32:50 -0400 | [diff] [blame] | 376 | auto result = fProxy->fLazyInstantiateCallback(resourceProvider, fProxy->callbackDesc()); |
Brian Salomon | b6a3a3b | 2019-04-01 12:29:34 -0400 | [diff] [blame] | 377 | surface = std::move(result.fSurface); |
| 378 | syncKey = result.fKeyMode == GrSurfaceProxy::LazyInstantiationKeyMode::kSynced; |
Brian Salomon | beb7f52 | 2019-08-30 16:19:42 -0400 | [diff] [blame] | 379 | releaseCallback = surface && result.fReleaseCallback; |
Greg Daniel | 457469c | 2018-02-08 15:05:44 -0500 | [diff] [blame] | 380 | } |
Robert Phillips | e8fabb2 | 2018-02-04 14:33:21 -0500 | [diff] [blame] | 381 | if (!surface) { |
Brian Salomon | 9f2b86c | 2019-10-22 10:37:46 -0400 | [diff] [blame] | 382 | fProxy->fDimensions.setEmpty(); |
Greg Daniel | bddcc95 | 2018-01-24 13:22:24 -0500 | [diff] [blame] | 383 | return false; |
Chris Dalton | 706a6ff | 2017-11-29 22:01:06 -0700 | [diff] [blame] | 384 | } |
| 385 | |
Brian Salomon | 1d19da0 | 2019-09-11 17:39:30 -0400 | [diff] [blame] | 386 | if (fProxy->isFullyLazy()) { |
Robert Phillips | c1b6066 | 2018-06-26 10:20:08 -0400 | [diff] [blame] | 387 | // This was a fully lazy proxy. We need to fill in the width & height. For partially |
| 388 | // lazy proxies we must preserve the original width & height since that indicates |
| 389 | // the content area. |
Brian Salomon | 9f2b86c | 2019-10-22 10:37:46 -0400 | [diff] [blame] | 390 | fProxy->fDimensions = surface->dimensions(); |
Robert Phillips | c1b6066 | 2018-06-26 10:20:08 -0400 | [diff] [blame] | 391 | } |
Chris Dalton | 706a6ff | 2017-11-29 22:01:06 -0700 | [diff] [blame] | 392 | |
Brian Salomon | 9f2b86c | 2019-10-22 10:37:46 -0400 | [diff] [blame] | 393 | SkASSERT(fProxy->width() <= surface->width()); |
| 394 | SkASSERT(fProxy->height() <= surface->height()); |
Chris Dalton | f91b755 | 2019-04-29 16:21:18 -0600 | [diff] [blame] | 395 | |
Greg Daniel | 303e83e | 2018-09-10 14:10:19 -0400 | [diff] [blame] | 396 | if (GrTextureProxy* texProxy = fProxy->asTextureProxy()) { |
Brian Salomon | b6a3a3b | 2019-04-01 12:29:34 -0400 | [diff] [blame] | 397 | texProxy->setTargetKeySync(syncKey); |
| 398 | if (syncKey) { |
| 399 | const GrUniqueKey& key = texProxy->getUniqueKey(); |
| 400 | if (key.isValid()) { |
| 401 | if (!surface->asTexture()->getUniqueKey().isValid()) { |
| 402 | // If 'surface' is newly created, attach the unique key |
| 403 | resourceProvider->assignUniqueKeyToResource(key, surface.get()); |
| 404 | } else { |
| 405 | // otherwise we had better have reattached to a cached version |
| 406 | SkASSERT(surface->asTexture()->getUniqueKey() == key); |
| 407 | } |
Robert Phillips | 0790f8a | 2018-09-18 13:11:03 -0400 | [diff] [blame] | 408 | } else { |
Brian Salomon | b6a3a3b | 2019-04-01 12:29:34 -0400 | [diff] [blame] | 409 | SkASSERT(!surface->getUniqueKey().isValid()); |
Robert Phillips | 0790f8a | 2018-09-18 13:11:03 -0400 | [diff] [blame] | 410 | } |
Greg Daniel | 303e83e | 2018-09-10 14:10:19 -0400 | [diff] [blame] | 411 | } |
| 412 | } |
| 413 | |
Robert Phillips | e8fabb2 | 2018-02-04 14:33:21 -0500 | [diff] [blame] | 414 | this->assign(std::move(surface)); |
Brian Salomon | beb7f52 | 2019-08-30 16:19:42 -0400 | [diff] [blame] | 415 | if (releaseCallback) { |
| 416 | fProxy->fLazyInstantiateCallback = nullptr; |
| 417 | } |
| 418 | |
Greg Daniel | bddcc95 | 2018-01-24 13:22:24 -0500 | [diff] [blame] | 419 | return true; |
Chris Dalton | 706a6ff | 2017-11-29 22:01:06 -0700 | [diff] [blame] | 420 | } |
| 421 | |
Greg Daniel | 849dce1 | 2018-04-24 14:32:53 -0400 | [diff] [blame] | 422 | #ifdef SK_DEBUG |
| 423 | void GrSurfaceProxy::validateSurface(const GrSurface* surface) { |
Greg Daniel | d51fa2f | 2020-01-22 16:53:38 -0500 | [diff] [blame] | 424 | SkASSERT(surface->backendFormat() == fFormat); |
Greg Daniel | 849dce1 | 2018-04-24 14:32:53 -0400 | [diff] [blame] | 425 | |
Greg Daniel | 849dce1 | 2018-04-24 14:32:53 -0400 | [diff] [blame] | 426 | this->onValidateSurface(surface); |
| 427 | } |
| 428 | #endif |