robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #include "GrSurfaceProxy.h" |
Robert Phillips | b726d58 | 2017-03-09 16:36:32 -0500 | [diff] [blame] | 9 | #include "GrSurfaceProxyPriv.h" |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 10 | |
Robert Phillips | 784b7bf | 2016-12-09 13:35:02 -0500 | [diff] [blame] | 11 | #include "GrCaps.h" |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 12 | #include "GrContext.h" |
| 13 | #include "GrContextPriv.h" |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 14 | #include "GrGpuResourcePriv.h" |
Robert Phillips | f2361d2 | 2016-10-25 14:20:06 -0400 | [diff] [blame] | 15 | #include "GrOpList.h" |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 16 | #include "GrProxyProvider.h" |
Robert Phillips | d44146b | 2019-02-15 14:44:28 -0500 | [diff] [blame] | 17 | #include "GrRecordingContext.h" |
| 18 | #include "GrRecordingContextPriv.h" |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 19 | #include "GrSurfaceContext.h" |
Robert Phillips | fe0253f | 2018-03-16 16:47:25 -0400 | [diff] [blame] | 20 | #include "GrSurfacePriv.h" |
Robert Phillips | a4c41b3 | 2017-03-15 13:02:45 -0400 | [diff] [blame] | 21 | #include "GrTexturePriv.h" |
Robert Phillips | 3743013 | 2016-11-09 06:50:43 -0500 | [diff] [blame] | 22 | #include "GrTextureRenderTargetProxy.h" |
Robert Phillips | f2361d2 | 2016-10-25 14:20:06 -0400 | [diff] [blame] | 23 | |
Robert Phillips | 93f1633 | 2016-11-23 19:37:13 -0500 | [diff] [blame] | 24 | #include "SkMathPriv.h" |
Greg Daniel | e1da1d9 | 2017-10-06 15:59:27 -0400 | [diff] [blame] | 25 | #include "SkMipMap.h" |
Robert Phillips | 93f1633 | 2016-11-23 19:37:13 -0500 | [diff] [blame] | 26 | |
Greg Daniel | 65fa8ca | 2018-01-10 17:06:31 -0500 | [diff] [blame] | 27 | #ifdef SK_DEBUG |
Robert Phillips | c7c2baf | 2018-03-08 09:51:04 -0500 | [diff] [blame] | 28 | #include "GrRenderTarget.h" |
| 29 | #include "GrRenderTargetPriv.h" |
| 30 | |
Greg Daniel | 65fa8ca | 2018-01-10 17:06:31 -0500 | [diff] [blame] | 31 | static bool is_valid_fully_lazy(const GrSurfaceDesc& desc, SkBackingFit fit) { |
| 32 | return desc.fWidth <= 0 && |
| 33 | desc.fHeight <= 0 && |
| 34 | desc.fConfig != kUnknown_GrPixelConfig && |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 35 | desc.fSampleCnt == 1 && |
Greg Daniel | 65fa8ca | 2018-01-10 17:06:31 -0500 | [diff] [blame] | 36 | SkBackingFit::kApprox == fit; |
| 37 | } |
| 38 | |
| 39 | static bool is_valid_partially_lazy(const GrSurfaceDesc& desc) { |
| 40 | return ((desc.fWidth > 0 && desc.fHeight > 0) || |
| 41 | (desc.fWidth <= 0 && desc.fHeight <= 0)) && |
| 42 | desc.fConfig != kUnknown_GrPixelConfig; |
| 43 | } |
| 44 | |
| 45 | static bool is_valid_non_lazy(const GrSurfaceDesc& desc) { |
| 46 | return desc.fWidth > 0 && |
| 47 | desc.fHeight > 0 && |
| 48 | desc.fConfig != kUnknown_GrPixelConfig; |
| 49 | } |
| 50 | #endif |
| 51 | |
Chris Dalton | 706a6ff | 2017-11-29 22:01:06 -0700 | [diff] [blame] | 52 | // Lazy-callback version |
Greg Daniel | 457469c | 2018-02-08 15:05:44 -0500 | [diff] [blame] | 53 | GrSurfaceProxy::GrSurfaceProxy(LazyInstantiateCallback&& callback, LazyInstantiationType lazyType, |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 54 | const GrBackendFormat& format, const GrSurfaceDesc& desc, |
| 55 | GrSurfaceOrigin origin, SkBackingFit fit, |
Robert Phillips | fe0253f | 2018-03-16 16:47:25 -0400 | [diff] [blame] | 56 | SkBudgeted budgeted, GrInternalSurfaceFlags surfaceFlags) |
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) |
Greg Daniel | e320486 | 2018-04-16 11:24:10 -0400 | [diff] [blame] | 59 | , fConfig(desc.fConfig) |
Greg Daniel | 65fa8ca | 2018-01-10 17:06:31 -0500 | [diff] [blame] | 60 | , fWidth(desc.fWidth) |
| 61 | , fHeight(desc.fHeight) |
Brian Salomon | 2a4f983 | 2018-03-03 22:43:43 -0500 | [diff] [blame] | 62 | , fOrigin(origin) |
Greg Daniel | 65fa8ca | 2018-01-10 17:06:31 -0500 | [diff] [blame] | 63 | , fFit(fit) |
| 64 | , fBudgeted(budgeted) |
Chris Dalton | 706a6ff | 2017-11-29 22:01:06 -0700 | [diff] [blame] | 65 | , fLazyInstantiateCallback(std::move(callback)) |
Greg Daniel | 457469c | 2018-02-08 15:05:44 -0500 | [diff] [blame] | 66 | , fLazyInstantiationType(lazyType) |
Greg Daniel | 65fa8ca | 2018-01-10 17:06:31 -0500 | [diff] [blame] | 67 | , fNeedsClear(SkToBool(desc.fFlags & kPerformInitialClear_GrSurfaceFlag)) |
Chris Dalton | 706a6ff | 2017-11-29 22:01:06 -0700 | [diff] [blame] | 68 | , fGpuMemorySize(kInvalidGpuMemorySize) |
| 69 | , fLastOpList(nullptr) { |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 70 | SkASSERT(fFormat.isValid()); |
Chris Dalton | 706a6ff | 2017-11-29 22:01:06 -0700 | [diff] [blame] | 71 | // NOTE: the default fUniqueID ctor pulls a value from the same pool as the GrGpuResources. |
Greg Daniel | 65fa8ca | 2018-01-10 17:06:31 -0500 | [diff] [blame] | 72 | if (fLazyInstantiateCallback) { |
| 73 | SkASSERT(is_valid_fully_lazy(desc, fit) || is_valid_partially_lazy(desc)); |
| 74 | } else { |
| 75 | SkASSERT(is_valid_non_lazy(desc)); |
| 76 | } |
Jim Van Verth | 1676cb9 | 2019-01-15 13:24:45 -0500 | [diff] [blame] | 77 | |
| 78 | if (GrPixelConfigIsCompressed(desc.fConfig)) { |
| 79 | SkASSERT(!SkToBool(desc.fFlags & kRenderTarget_GrSurfaceFlag)); |
| 80 | fSurfaceFlags |= GrInternalSurfaceFlags::kReadOnly; |
| 81 | } |
Chris Dalton | 706a6ff | 2017-11-29 22:01:06 -0700 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | // Wrapped version |
Robert Phillips | 066f020 | 2017-07-25 10:16:35 -0400 | [diff] [blame] | 85 | GrSurfaceProxy::GrSurfaceProxy(sk_sp<GrSurface> surface, GrSurfaceOrigin origin, SkBackingFit fit) |
Brian Salomon | bb5711a | 2017-05-17 13:49:59 -0400 | [diff] [blame] | 86 | : INHERITED(std::move(surface)) |
Greg Daniel | e320486 | 2018-04-16 11:24:10 -0400 | [diff] [blame] | 87 | , fSurfaceFlags(fTarget->surfacePriv().flags()) |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 88 | , fFormat(fTarget->backendFormat()) |
Brian Salomon | bb5711a | 2017-05-17 13:49:59 -0400 | [diff] [blame] | 89 | , fConfig(fTarget->config()) |
| 90 | , fWidth(fTarget->width()) |
| 91 | , fHeight(fTarget->height()) |
Robert Phillips | 066f020 | 2017-07-25 10:16:35 -0400 | [diff] [blame] | 92 | , fOrigin(origin) |
Brian Salomon | bb5711a | 2017-05-17 13:49:59 -0400 | [diff] [blame] | 93 | , fFit(fit) |
Brian Salomon | fa2ebea | 2019-01-24 15:58:58 -0500 | [diff] [blame] | 94 | , fBudgeted(fTarget->resourcePriv().budgetedType() == GrBudgetedType::kBudgeted |
| 95 | ? SkBudgeted::kYes |
| 96 | : SkBudgeted::kNo) |
Brian Salomon | bb5711a | 2017-05-17 13:49:59 -0400 | [diff] [blame] | 97 | , fUniqueID(fTarget->uniqueID()) // Note: converting from unique resource ID to a proxy ID! |
Brian Salomon | d17b4a6 | 2017-05-23 16:53:47 -0400 | [diff] [blame] | 98 | , fNeedsClear(false) |
Brian Salomon | bb5711a | 2017-05-17 13:49:59 -0400 | [diff] [blame] | 99 | , fGpuMemorySize(kInvalidGpuMemorySize) |
Robert Phillips | 019ff27 | 2017-07-24 14:47:57 -0400 | [diff] [blame] | 100 | , fLastOpList(nullptr) { |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 101 | SkASSERT(fFormat.isValid()); |
Robert Phillips | 019ff27 | 2017-07-24 14:47:57 -0400 | [diff] [blame] | 102 | } |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 103 | |
Robert Phillips | f2361d2 | 2016-10-25 14:20:06 -0400 | [diff] [blame] | 104 | GrSurfaceProxy::~GrSurfaceProxy() { |
Robert Phillips | 6cdc22c | 2017-05-11 16:29:14 -0400 | [diff] [blame] | 105 | // For this to be deleted the opList that held a ref on it (if there was one) must have been |
| 106 | // deleted. Which would have cleared out this back pointer. |
| 107 | SkASSERT(!fLastOpList); |
Robert Phillips | f2361d2 | 2016-10-25 14:20:06 -0400 | [diff] [blame] | 108 | } |
| 109 | |
Robert Phillips | eafd48a | 2017-11-16 07:52:08 -0500 | [diff] [blame] | 110 | bool GrSurfaceProxyPriv::AttachStencilIfNeeded(GrResourceProvider* resourceProvider, |
| 111 | GrSurface* surface, bool needsStencil) { |
Robert Phillips | 6504813 | 2017-08-10 08:44:49 -0400 | [diff] [blame] | 112 | if (needsStencil) { |
| 113 | GrRenderTarget* rt = surface->asRenderTarget(); |
| 114 | if (!rt) { |
| 115 | SkASSERT(0); |
| 116 | return false; |
| 117 | } |
| 118 | |
| 119 | if (!resourceProvider->attachStencilAttachment(rt)) { |
| 120 | return false; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | return true; |
| 125 | } |
| 126 | |
Robert Phillips | ba5c439 | 2018-07-25 12:37:14 -0400 | [diff] [blame] | 127 | sk_sp<GrSurface> GrSurfaceProxy::createSurfaceImpl(GrResourceProvider* resourceProvider, |
| 128 | int sampleCnt, bool needsStencil, |
| 129 | GrSurfaceDescFlags descFlags, |
| 130 | GrMipMapped mipMapped) const { |
Greg Daniel | 65fa8ca | 2018-01-10 17:06:31 -0500 | [diff] [blame] | 131 | SkASSERT(GrSurfaceProxy::LazyState::kNot == this->lazyInstantiationState()); |
Greg Daniel | d2d8e92 | 2018-02-12 12:07:39 -0500 | [diff] [blame] | 132 | SkASSERT(!fTarget); |
Brian Salomon | bb5711a | 2017-05-17 13:49:59 -0400 | [diff] [blame] | 133 | GrSurfaceDesc desc; |
Robert Phillips | fe0253f | 2018-03-16 16:47:25 -0400 | [diff] [blame] | 134 | desc.fFlags = descFlags; |
Brian Salomon | d17b4a6 | 2017-05-23 16:53:47 -0400 | [diff] [blame] | 135 | if (fNeedsClear) { |
| 136 | desc.fFlags |= kPerformInitialClear_GrSurfaceFlag; |
| 137 | } |
Robert Phillips | 16d8ec6 | 2017-07-27 16:16:25 -0400 | [diff] [blame] | 138 | desc.fWidth = fWidth; |
| 139 | desc.fHeight = fHeight; |
| 140 | desc.fConfig = fConfig; |
| 141 | desc.fSampleCnt = sampleCnt; |
Robert Phillips | eaa8625 | 2016-11-08 13:49:39 +0000 | [diff] [blame] | 142 | |
Chris Dalton | d004e0b | 2018-09-27 09:28:03 -0600 | [diff] [blame] | 143 | GrResourceProvider::Flags resourceProviderFlags = GrResourceProvider::Flags::kNone; |
| 144 | if ((fSurfaceFlags & GrInternalSurfaceFlags::kNoPendingIO) || |
Robert Phillips | ba5c439 | 2018-07-25 12:37:14 -0400 | [diff] [blame] | 145 | resourceProvider->explicitlyAllocateGPUResources()) { |
| 146 | // The explicit resource allocator requires that any resources it pulls out of the |
| 147 | // cache have no pending IO. |
Chris Dalton | d004e0b | 2018-09-27 09:28:03 -0600 | [diff] [blame] | 148 | resourceProviderFlags = GrResourceProvider::Flags::kNoPendingIO; |
Robert Phillips | fe0253f | 2018-03-16 16:47:25 -0400 | [diff] [blame] | 149 | } |
| 150 | |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 151 | sk_sp<GrSurface> surface; |
Greg Daniel | f6f7b67 | 2018-02-15 13:06:26 -0500 | [diff] [blame] | 152 | if (GrMipMapped::kYes == mipMapped) { |
| 153 | SkASSERT(SkBackingFit::kExact == fFit); |
| 154 | |
| 155 | // SkMipMap doesn't include the base level in the level count so we have to add 1 |
| 156 | int mipCount = SkMipMap::ComputeLevelCount(desc.fWidth, desc.fHeight) + 1; |
| 157 | // We should have caught the case where mipCount == 1 when making the proxy and instead |
| 158 | // created a non-mipmapped proxy. |
| 159 | SkASSERT(mipCount > 1); |
| 160 | std::unique_ptr<GrMipLevel[]> texels(new GrMipLevel[mipCount]); |
| 161 | |
| 162 | // We don't want to upload any texel data |
| 163 | for (int i = 0; i < mipCount; i++) { |
| 164 | texels[i].fPixels = nullptr; |
| 165 | texels[i].fRowBytes = 0; |
| 166 | } |
| 167 | |
Brian Osman | 2b23c4b | 2018-06-01 12:25:08 -0400 | [diff] [blame] | 168 | surface = resourceProvider->createTexture(desc, fBudgeted, texels.get(), mipCount); |
Greg Daniel | f6f7b67 | 2018-02-15 13:06:26 -0500 | [diff] [blame] | 169 | if (surface) { |
| 170 | SkASSERT(surface->asTexture()); |
| 171 | SkASSERT(GrMipMapped::kYes == surface->asTexture()->texturePriv().mipMapped()); |
| 172 | } |
Robert Phillips | eaa8625 | 2016-11-08 13:49:39 +0000 | [diff] [blame] | 173 | } else { |
Greg Daniel | f6f7b67 | 2018-02-15 13:06:26 -0500 | [diff] [blame] | 174 | if (SkBackingFit::kApprox == fFit) { |
Robert Phillips | fe0253f | 2018-03-16 16:47:25 -0400 | [diff] [blame] | 175 | surface = resourceProvider->createApproxTexture(desc, resourceProviderFlags); |
Greg Daniel | f6f7b67 | 2018-02-15 13:06:26 -0500 | [diff] [blame] | 176 | } else { |
Robert Phillips | fe0253f | 2018-03-16 16:47:25 -0400 | [diff] [blame] | 177 | surface = resourceProvider->createTexture(desc, fBudgeted, resourceProviderFlags); |
Greg Daniel | f6f7b67 | 2018-02-15 13:06:26 -0500 | [diff] [blame] | 178 | } |
Robert Phillips | eaa8625 | 2016-11-08 13:49:39 +0000 | [diff] [blame] | 179 | } |
Robert Phillips | 6504813 | 2017-08-10 08:44:49 -0400 | [diff] [blame] | 180 | if (!surface) { |
| 181 | return nullptr; |
| 182 | } |
| 183 | |
Robert Phillips | eafd48a | 2017-11-16 07:52:08 -0500 | [diff] [blame] | 184 | if (!GrSurfaceProxyPriv::AttachStencilIfNeeded(resourceProvider, surface.get(), needsStencil)) { |
Robert Phillips | 6504813 | 2017-08-10 08:44:49 -0400 | [diff] [blame] | 185 | return nullptr; |
Robert Phillips | eaa8625 | 2016-11-08 13:49:39 +0000 | [diff] [blame] | 186 | } |
| 187 | |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 188 | return surface; |
| 189 | } |
| 190 | |
Brian Salomon | 7d94bb5 | 2018-10-12 14:37:19 -0400 | [diff] [blame] | 191 | bool GrSurfaceProxy::canSkipResourceAllocator() const { |
| 192 | auto peek = this->peekSurface(); |
| 193 | if (!peek) { |
| 194 | return false; |
| 195 | } |
| 196 | // If this resource is already allocated and not recyclable then the resource allocator does |
| 197 | // not need to do anything with it. |
| 198 | return !peek->resourcePriv().getScratchKey().isValid(); |
| 199 | } |
| 200 | |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 201 | void GrSurfaceProxy::assign(sk_sp<GrSurface> surface) { |
| 202 | SkASSERT(!fTarget && surface); |
Robert Phillips | abf7b76 | 2018-03-21 12:13:37 -0400 | [diff] [blame] | 203 | |
Greg Daniel | 849dce1 | 2018-04-24 14:32:53 -0400 | [diff] [blame] | 204 | SkDEBUGCODE(this->validateSurface(surface.get());) |
Robert Phillips | abf7b76 | 2018-03-21 12:13:37 -0400 | [diff] [blame] | 205 | |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 206 | fTarget = surface.release(); |
Robert Phillips | c7c2baf | 2018-03-08 09:51:04 -0500 | [diff] [blame] | 207 | |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 208 | this->INHERITED::transferRefs(); |
| 209 | |
Robert Phillips | eaa8625 | 2016-11-08 13:49:39 +0000 | [diff] [blame] | 210 | #ifdef SK_DEBUG |
Robert Phillips | c7c2baf | 2018-03-08 09:51:04 -0500 | [diff] [blame] | 211 | if (this->asRenderTargetProxy()) { |
| 212 | SkASSERT(fTarget->asRenderTarget()); |
| 213 | if (this->asRenderTargetProxy()->needsStencil()) { |
| 214 | SkASSERT(fTarget->asRenderTarget()->renderTargetPriv().getStencilAttachment()); |
| 215 | } |
| 216 | } |
| 217 | |
Robert Phillips | eaa8625 | 2016-11-08 13:49:39 +0000 | [diff] [blame] | 218 | if (kInvalidGpuMemorySize != this->getRawGpuMemorySize_debugOnly()) { |
Robert Phillips | 784b7bf | 2016-12-09 13:35:02 -0500 | [diff] [blame] | 219 | SkASSERT(fTarget->gpuMemorySize() <= this->getRawGpuMemorySize_debugOnly()); |
Robert Phillips | eaa8625 | 2016-11-08 13:49:39 +0000 | [diff] [blame] | 220 | } |
| 221 | #endif |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 222 | } |
Robert Phillips | eaa8625 | 2016-11-08 13:49:39 +0000 | [diff] [blame] | 223 | |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 224 | bool GrSurfaceProxy::instantiateImpl(GrResourceProvider* resourceProvider, int sampleCnt, |
Robert Phillips | fe0253f | 2018-03-16 16:47:25 -0400 | [diff] [blame] | 225 | bool needsStencil, GrSurfaceDescFlags descFlags, |
| 226 | GrMipMapped mipMapped, const GrUniqueKey* uniqueKey) { |
Greg Daniel | 65fa8ca | 2018-01-10 17:06:31 -0500 | [diff] [blame] | 227 | SkASSERT(LazyState::kNot == this->lazyInstantiationState()); |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 228 | if (fTarget) { |
Brian Salomon | 5790420 | 2018-12-17 14:45:00 -0500 | [diff] [blame] | 229 | if (uniqueKey && uniqueKey->isValid()) { |
| 230 | SkASSERT(fTarget->getUniqueKey().isValid() && fTarget->getUniqueKey() == *uniqueKey); |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 231 | } |
Robert Phillips | eafd48a | 2017-11-16 07:52:08 -0500 | [diff] [blame] | 232 | return GrSurfaceProxyPriv::AttachStencilIfNeeded(resourceProvider, fTarget, needsStencil); |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 233 | } |
| 234 | |
Robert Phillips | 6504813 | 2017-08-10 08:44:49 -0400 | [diff] [blame] | 235 | sk_sp<GrSurface> surface = this->createSurfaceImpl(resourceProvider, sampleCnt, needsStencil, |
Robert Phillips | fe0253f | 2018-03-16 16:47:25 -0400 | [diff] [blame] | 236 | descFlags, mipMapped); |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 237 | if (!surface) { |
| 238 | return false; |
| 239 | } |
| 240 | |
Brian Osman | 28c434b | 2017-09-27 13:11:16 -0400 | [diff] [blame] | 241 | // If there was an invalidation message pending for this key, we might have just processed it, |
| 242 | // causing the key (stored on this proxy) to become invalid. |
| 243 | if (uniqueKey && uniqueKey->isValid()) { |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 244 | resourceProvider->assignUniqueKeyToResource(*uniqueKey, surface.get()); |
| 245 | } |
| 246 | |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 247 | this->assign(std::move(surface)); |
Robert Phillips | fe0253f | 2018-03-16 16:47:25 -0400 | [diff] [blame] | 248 | |
Robert Phillips | eee4d6e | 2017-06-05 09:26:07 -0400 | [diff] [blame] | 249 | return true; |
Robert Phillips | eaa8625 | 2016-11-08 13:49:39 +0000 | [diff] [blame] | 250 | } |
| 251 | |
Brian Salomon | 967df20 | 2018-12-07 11:15:53 -0500 | [diff] [blame] | 252 | void GrSurfaceProxy::deinstantiate() { |
Brian Salomon | fd98c2c | 2018-07-31 17:25:29 -0400 | [diff] [blame] | 253 | SkASSERT(this->isInstantiated()); |
Robert Phillips | 4bc7011 | 2018-03-01 10:24:02 -0500 | [diff] [blame] | 254 | |
| 255 | this->release(); |
| 256 | } |
| 257 | |
Robert Phillips | 57aa367 | 2017-07-21 11:38:13 -0400 | [diff] [blame] | 258 | void GrSurfaceProxy::computeScratchKey(GrScratchKey* key) const { |
Greg Daniel | 65fa8ca | 2018-01-10 17:06:31 -0500 | [diff] [blame] | 259 | SkASSERT(LazyState::kFully != this->lazyInstantiationState()); |
Robert Phillips | 57aa367 | 2017-07-21 11:38:13 -0400 | [diff] [blame] | 260 | const GrRenderTargetProxy* rtp = this->asRenderTargetProxy(); |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 261 | int sampleCount = 1; |
Robert Phillips | 57aa367 | 2017-07-21 11:38:13 -0400 | [diff] [blame] | 262 | if (rtp) { |
| 263 | sampleCount = rtp->numStencilSamples(); |
| 264 | } |
| 265 | |
| 266 | const GrTextureProxy* tp = this->asTextureProxy(); |
Greg Daniel | e252f08 | 2017-10-23 16:05:23 -0400 | [diff] [blame] | 267 | GrMipMapped mipMapped = GrMipMapped::kNo; |
Robert Phillips | 57aa367 | 2017-07-21 11:38:13 -0400 | [diff] [blame] | 268 | if (tp) { |
Greg Daniel | e252f08 | 2017-10-23 16:05:23 -0400 | [diff] [blame] | 269 | mipMapped = tp->mipMapped(); |
Robert Phillips | 57aa367 | 2017-07-21 11:38:13 -0400 | [diff] [blame] | 270 | } |
| 271 | |
Robert Phillips | a108c92 | 2017-10-10 10:42:19 -0400 | [diff] [blame] | 272 | int width = this->worstCaseWidth(); |
| 273 | int height = this->worstCaseHeight(); |
Robert Phillips | 57aa367 | 2017-07-21 11:38:13 -0400 | [diff] [blame] | 274 | |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 275 | GrTexturePriv::ComputeScratchKey(this->config(), width, height, SkToBool(rtp), sampleCount, |
Greg Daniel | e252f08 | 2017-10-23 16:05:23 -0400 | [diff] [blame] | 276 | mipMapped, key); |
Robert Phillips | 57aa367 | 2017-07-21 11:38:13 -0400 | [diff] [blame] | 277 | } |
| 278 | |
Robert Phillips | f2361d2 | 2016-10-25 14:20:06 -0400 | [diff] [blame] | 279 | void GrSurfaceProxy::setLastOpList(GrOpList* opList) { |
Robert Phillips | 6cdc22c | 2017-05-11 16:29:14 -0400 | [diff] [blame] | 280 | #ifdef SK_DEBUG |
Robert Phillips | f2361d2 | 2016-10-25 14:20:06 -0400 | [diff] [blame] | 281 | if (fLastOpList) { |
Robert Phillips | f2361d2 | 2016-10-25 14:20:06 -0400 | [diff] [blame] | 282 | SkASSERT(fLastOpList->isClosed()); |
Robert Phillips | 4a39504 | 2017-04-24 16:27:17 +0000 | [diff] [blame] | 283 | } |
Robert Phillips | 6cdc22c | 2017-05-11 16:29:14 -0400 | [diff] [blame] | 284 | #endif |
Robert Phillips | f2361d2 | 2016-10-25 14:20:06 -0400 | [diff] [blame] | 285 | |
Robert Phillips | 6cdc22c | 2017-05-11 16:29:14 -0400 | [diff] [blame] | 286 | // Un-reffed |
| 287 | fLastOpList = opList; |
Robert Phillips | f2361d2 | 2016-10-25 14:20:06 -0400 | [diff] [blame] | 288 | } |
Robert Phillips | 3743013 | 2016-11-09 06:50:43 -0500 | [diff] [blame] | 289 | |
Brian Osman | 45580d3 | 2016-11-23 09:37:01 -0500 | [diff] [blame] | 290 | GrRenderTargetOpList* GrSurfaceProxy::getLastRenderTargetOpList() { |
| 291 | return fLastOpList ? fLastOpList->asRenderTargetOpList() : nullptr; |
| 292 | } |
| 293 | |
| 294 | GrTextureOpList* GrSurfaceProxy::getLastTextureOpList() { |
| 295 | return fLastOpList ? fLastOpList->asTextureOpList() : nullptr; |
| 296 | } |
| 297 | |
Robert Phillips | a108c92 | 2017-10-10 10:42:19 -0400 | [diff] [blame] | 298 | int GrSurfaceProxy::worstCaseWidth() const { |
Greg Daniel | 65fa8ca | 2018-01-10 17:06:31 -0500 | [diff] [blame] | 299 | SkASSERT(LazyState::kFully != this->lazyInstantiationState()); |
Robert Phillips | a108c92 | 2017-10-10 10:42:19 -0400 | [diff] [blame] | 300 | if (fTarget) { |
| 301 | return fTarget->width(); |
| 302 | } |
| 303 | |
| 304 | if (SkBackingFit::kExact == fFit) { |
| 305 | return fWidth; |
| 306 | } |
| 307 | return SkTMax(GrResourceProvider::kMinScratchTextureSize, GrNextPow2(fWidth)); |
| 308 | } |
| 309 | |
| 310 | int GrSurfaceProxy::worstCaseHeight() const { |
Greg Daniel | 65fa8ca | 2018-01-10 17:06:31 -0500 | [diff] [blame] | 311 | SkASSERT(LazyState::kFully != this->lazyInstantiationState()); |
Robert Phillips | a108c92 | 2017-10-10 10:42:19 -0400 | [diff] [blame] | 312 | if (fTarget) { |
| 313 | return fTarget->height(); |
| 314 | } |
| 315 | |
| 316 | if (SkBackingFit::kExact == fFit) { |
| 317 | return fHeight; |
| 318 | } |
| 319 | return SkTMax(GrResourceProvider::kMinScratchTextureSize, GrNextPow2(fHeight)); |
| 320 | } |
| 321 | |
Brian Osman | 45580d3 | 2016-11-23 09:37:01 -0500 | [diff] [blame] | 322 | #ifdef SK_DEBUG |
Robert Phillips | 292a6b2 | 2019-02-14 14:49:02 -0500 | [diff] [blame] | 323 | void GrSurfaceProxy::validate(GrContext_Base* context) const { |
Brian Osman | 45580d3 | 2016-11-23 09:37:01 -0500 | [diff] [blame] | 324 | if (fTarget) { |
| 325 | SkASSERT(fTarget->getContext() == context); |
| 326 | } |
| 327 | |
| 328 | INHERITED::validate(); |
| 329 | } |
| 330 | #endif |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 331 | |
Robert Phillips | d44146b | 2019-02-15 14:44:28 -0500 | [diff] [blame] | 332 | sk_sp<GrTextureProxy> GrSurfaceProxy::Copy(GrRecordingContext* context, |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 333 | GrSurfaceProxy* src, |
Greg Daniel | 65c7f66 | 2017-10-30 13:39:09 -0400 | [diff] [blame] | 334 | GrMipMapped mipMapped, |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 335 | SkIRect srcRect, |
Brian Salomon | fee3f9b | 2018-12-19 12:34:12 -0500 | [diff] [blame] | 336 | SkBackingFit fit, |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 337 | SkBudgeted budgeted) { |
Greg Daniel | 65fa8ca | 2018-01-10 17:06:31 -0500 | [diff] [blame] | 338 | SkASSERT(LazyState::kFully != src->lazyInstantiationState()); |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 339 | if (!srcRect.intersect(SkIRect::MakeWH(src->width(), src->height()))) { |
| 340 | return nullptr; |
| 341 | } |
| 342 | |
Brian Salomon | 63e7973 | 2017-05-15 21:23:13 -0400 | [diff] [blame] | 343 | GrSurfaceDesc dstDesc; |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 344 | dstDesc.fWidth = srcRect.width(); |
| 345 | dstDesc.fHeight = srcRect.height(); |
Robert Phillips | 16d8ec6 | 2017-07-27 16:16:25 -0400 | [diff] [blame] | 346 | dstDesc.fConfig = src->config(); |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 347 | |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 348 | GrBackendFormat format = src->backendFormat().makeTexture2D(); |
| 349 | if (!format.isValid()) { |
| 350 | return nullptr; |
| 351 | } |
| 352 | |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 353 | sk_sp<GrSurfaceContext> dstContext(context->priv().makeDeferredSurfaceContext( |
Brian Salomon | fee3f9b | 2018-12-19 12:34:12 -0500 | [diff] [blame] | 354 | format, dstDesc, src->origin(), mipMapped, fit, budgeted)); |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 355 | if (!dstContext) { |
| 356 | return nullptr; |
| 357 | } |
| 358 | |
| 359 | if (!dstContext->copy(src, srcRect, SkIPoint::Make(0, 0))) { |
| 360 | return nullptr; |
| 361 | } |
| 362 | |
Robert Phillips | 63c6746 | 2017-02-15 14:19:01 -0500 | [diff] [blame] | 363 | return dstContext->asTextureProxyRef(); |
| 364 | } |
| 365 | |
Robert Phillips | d44146b | 2019-02-15 14:44:28 -0500 | [diff] [blame] | 366 | sk_sp<GrTextureProxy> GrSurfaceProxy::Copy(GrRecordingContext* context, GrSurfaceProxy* src, |
Brian Salomon | fee3f9b | 2018-12-19 12:34:12 -0500 | [diff] [blame] | 367 | GrMipMapped mipMapped, SkBackingFit fit, |
| 368 | SkBudgeted budgeted) { |
Greg Daniel | 65fa8ca | 2018-01-10 17:06:31 -0500 | [diff] [blame] | 369 | SkASSERT(LazyState::kFully != src->lazyInstantiationState()); |
Brian Salomon | fee3f9b | 2018-12-19 12:34:12 -0500 | [diff] [blame] | 370 | return Copy(context, src, mipMapped, SkIRect::MakeWH(src->width(), src->height()), fit, |
| 371 | budgeted); |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 372 | } |
| 373 | |
Robert Phillips | d44146b | 2019-02-15 14:44:28 -0500 | [diff] [blame] | 374 | sk_sp<GrSurfaceContext> GrSurfaceProxy::TestCopy(GrRecordingContext* context, |
| 375 | const GrSurfaceDesc& dstDesc, |
Brian Salomon | 2a4f983 | 2018-03-03 22:43:43 -0500 | [diff] [blame] | 376 | GrSurfaceOrigin origin, GrSurfaceProxy* srcProxy) { |
Greg Daniel | 65fa8ca | 2018-01-10 17:06:31 -0500 | [diff] [blame] | 377 | SkASSERT(LazyState::kFully != srcProxy->lazyInstantiationState()); |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 378 | |
| 379 | GrBackendFormat format = srcProxy->backendFormat().makeTexture2D(); |
| 380 | if (!format.isValid()) { |
| 381 | return nullptr; |
| 382 | } |
| 383 | |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 384 | sk_sp<GrSurfaceContext> dstContext(context->priv().makeDeferredSurfaceContext( |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 385 | format, dstDesc, origin, GrMipMapped::kNo, SkBackingFit::kExact, SkBudgeted::kYes)); |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 386 | if (!dstContext) { |
| 387 | return nullptr; |
| 388 | } |
| 389 | |
Robert Phillips | d46697a | 2017-01-25 12:10:37 -0500 | [diff] [blame] | 390 | if (!dstContext->copy(srcProxy)) { |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 391 | return nullptr; |
| 392 | } |
| 393 | |
Robert Phillips | d46697a | 2017-01-25 12:10:37 -0500 | [diff] [blame] | 394 | return dstContext; |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 395 | } |
Robert Phillips | b726d58 | 2017-03-09 16:36:32 -0500 | [diff] [blame] | 396 | |
Robert Phillips | 0ae6faa | 2017-03-21 16:22:00 -0400 | [diff] [blame] | 397 | void GrSurfaceProxyPriv::exactify() { |
Greg Daniel | 65fa8ca | 2018-01-10 17:06:31 -0500 | [diff] [blame] | 398 | SkASSERT(GrSurfaceProxy::LazyState::kFully != fProxy->lazyInstantiationState()); |
Robert Phillips | 0ae6faa | 2017-03-21 16:22:00 -0400 | [diff] [blame] | 399 | if (this->isExact()) { |
| 400 | return; |
| 401 | } |
| 402 | |
| 403 | SkASSERT(SkBackingFit::kApprox == fProxy->fFit); |
| 404 | |
| 405 | if (fProxy->fTarget) { |
| 406 | // The kApprox but already instantiated case. Setting the proxy's width & height to |
| 407 | // 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] | 408 | // obliterating the area of interest information. This call (exactify) only used |
Robert Phillips | 0ae6faa | 2017-03-21 16:22:00 -0400 | [diff] [blame] | 409 | // when converting an SkSpecialImage to an SkImage so the proxy shouldn't be |
| 410 | // used for additional draws. |
Brian Salomon | bb5711a | 2017-05-17 13:49:59 -0400 | [diff] [blame] | 411 | fProxy->fWidth = fProxy->fTarget->width(); |
| 412 | fProxy->fHeight = fProxy->fTarget->height(); |
Robert Phillips | 0ae6faa | 2017-03-21 16:22:00 -0400 | [diff] [blame] | 413 | return; |
| 414 | } |
| 415 | |
| 416 | // The kApprox uninstantiated case. Making this proxy be exact should be okay. |
| 417 | // It could mess things up if prior decisions were based on the approximate size. |
| 418 | fProxy->fFit = SkBackingFit::kExact; |
| 419 | // 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] | 420 | // 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] | 421 | // the special image goes away. If it hasn't been computed yet it might as well compute the |
| 422 | // exact amount. |
| 423 | } |
| 424 | |
Greg Daniel | bddcc95 | 2018-01-24 13:22:24 -0500 | [diff] [blame] | 425 | bool GrSurfaceProxyPriv::doLazyInstantiation(GrResourceProvider* resourceProvider) { |
Greg Daniel | 0a375db | 2018-02-01 12:21:39 -0500 | [diff] [blame] | 426 | SkASSERT(GrSurfaceProxy::LazyState::kNot != fProxy->lazyInstantiationState()); |
Chris Dalton | 706a6ff | 2017-11-29 22:01:06 -0700 | [diff] [blame] | 427 | |
Robert Phillips | 0790f8a | 2018-09-18 13:11:03 -0400 | [diff] [blame] | 428 | sk_sp<GrSurface> surface; |
| 429 | if (fProxy->asTextureProxy() && fProxy->asTextureProxy()->getUniqueKey().isValid()) { |
| 430 | // First try to reattach to a cached version if the proxy is uniquely keyed |
| 431 | surface = resourceProvider->findByUniqueKey<GrSurface>( |
| 432 | fProxy->asTextureProxy()->getUniqueKey()); |
| 433 | } |
| 434 | |
| 435 | if (!surface) { |
| 436 | surface = fProxy->fLazyInstantiateCallback(resourceProvider); |
| 437 | } |
Greg Daniel | 457469c | 2018-02-08 15:05:44 -0500 | [diff] [blame] | 438 | if (GrSurfaceProxy::LazyInstantiationType::kSingleUse == fProxy->fLazyInstantiationType) { |
Greg Daniel | 457469c | 2018-02-08 15:05:44 -0500 | [diff] [blame] | 439 | fProxy->fLazyInstantiateCallback = nullptr; |
| 440 | } |
Robert Phillips | e8fabb2 | 2018-02-04 14:33:21 -0500 | [diff] [blame] | 441 | if (!surface) { |
Chris Dalton | 706a6ff | 2017-11-29 22:01:06 -0700 | [diff] [blame] | 442 | fProxy->fWidth = 0; |
| 443 | fProxy->fHeight = 0; |
Greg Daniel | bddcc95 | 2018-01-24 13:22:24 -0500 | [diff] [blame] | 444 | return false; |
Chris Dalton | 706a6ff | 2017-11-29 22:01:06 -0700 | [diff] [blame] | 445 | } |
| 446 | |
Robert Phillips | c1b6066 | 2018-06-26 10:20:08 -0400 | [diff] [blame] | 447 | if (fProxy->fWidth <= 0 || fProxy->fHeight <= 0) { |
| 448 | // This was a fully lazy proxy. We need to fill in the width & height. For partially |
| 449 | // lazy proxies we must preserve the original width & height since that indicates |
| 450 | // the content area. |
| 451 | SkASSERT(fProxy->fWidth <= 0 && fProxy->fHeight <= 0); |
| 452 | fProxy->fWidth = surface->width(); |
| 453 | fProxy->fHeight = surface->height(); |
| 454 | } |
Chris Dalton | 706a6ff | 2017-11-29 22:01:06 -0700 | [diff] [blame] | 455 | |
Robert Phillips | c7c2baf | 2018-03-08 09:51:04 -0500 | [diff] [blame] | 456 | bool needsStencil = fProxy->asRenderTargetProxy() |
| 457 | ? fProxy->asRenderTargetProxy()->needsStencil() |
| 458 | : false; |
| 459 | |
Robert Phillips | 01a9128 | 2018-07-26 08:03:04 -0400 | [diff] [blame] | 460 | if (!GrSurfaceProxyPriv::AttachStencilIfNeeded(resourceProvider, surface.get(), needsStencil)) { |
| 461 | return false; |
| 462 | } |
Robert Phillips | c7c2baf | 2018-03-08 09:51:04 -0500 | [diff] [blame] | 463 | |
Greg Daniel | 303e83e | 2018-09-10 14:10:19 -0400 | [diff] [blame] | 464 | if (GrTextureProxy* texProxy = fProxy->asTextureProxy()) { |
| 465 | const GrUniqueKey& key = texProxy->getUniqueKey(); |
| 466 | if (key.isValid()) { |
Robert Phillips | 0790f8a | 2018-09-18 13:11:03 -0400 | [diff] [blame] | 467 | if (!surface->asTexture()->getUniqueKey().isValid()) { |
| 468 | // If 'surface' is newly created, attach the unique key |
| 469 | resourceProvider->assignUniqueKeyToResource(key, surface.get()); |
| 470 | } else { |
| 471 | // otherwise we had better have reattached to a cached version |
| 472 | SkASSERT(surface->asTexture()->getUniqueKey() == key); |
| 473 | } |
Greg Daniel | 303e83e | 2018-09-10 14:10:19 -0400 | [diff] [blame] | 474 | } |
| 475 | } |
| 476 | |
Robert Phillips | e8fabb2 | 2018-02-04 14:33:21 -0500 | [diff] [blame] | 477 | this->assign(std::move(surface)); |
Greg Daniel | bddcc95 | 2018-01-24 13:22:24 -0500 | [diff] [blame] | 478 | return true; |
Chris Dalton | 706a6ff | 2017-11-29 22:01:06 -0700 | [diff] [blame] | 479 | } |
| 480 | |
Greg Daniel | 849dce1 | 2018-04-24 14:32:53 -0400 | [diff] [blame] | 481 | #ifdef SK_DEBUG |
| 482 | void GrSurfaceProxy::validateSurface(const GrSurface* surface) { |
| 483 | SkASSERT(surface->config() == fConfig); |
| 484 | |
| 485 | // Assert the flags are the same except for kNoPendingIO which is not passed onto the GrSurface. |
| 486 | GrInternalSurfaceFlags proxyFlags = fSurfaceFlags & ~GrInternalSurfaceFlags::kNoPendingIO; |
| 487 | GrInternalSurfaceFlags surfaceFlags = surface->surfacePriv().flags(); |
| 488 | SkASSERT((proxyFlags & GrInternalSurfaceFlags::kSurfaceMask) == |
| 489 | (surfaceFlags & GrInternalSurfaceFlags::kSurfaceMask)); |
| 490 | this->onValidateSurface(surface); |
| 491 | } |
| 492 | #endif |