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