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