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