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