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 | a4c41b3 | 2017-03-15 13:02:45 -0400 | [diff] [blame] | 18 | #include "GrTexturePriv.h" |
Robert Phillips | 3743013 | 2016-11-09 06:50:43 -0500 | [diff] [blame] | 19 | #include "GrTextureRenderTargetProxy.h" |
Robert Phillips | f2361d2 | 2016-10-25 14:20:06 -0400 | [diff] [blame] | 20 | |
Robert Phillips | 93f1633 | 2016-11-23 19:37:13 -0500 | [diff] [blame] | 21 | #include "SkMathPriv.h" |
Greg Daniel | e1da1d9 | 2017-10-06 15:59:27 -0400 | [diff] [blame] | 22 | #include "SkMipMap.h" |
Robert Phillips | 93f1633 | 2016-11-23 19:37:13 -0500 | [diff] [blame] | 23 | |
Greg Daniel | 65fa8ca | 2018-01-10 17:06:31 -0500 | [diff] [blame] | 24 | #ifdef SK_DEBUG |
| 25 | static bool is_valid_fully_lazy(const GrSurfaceDesc& desc, SkBackingFit fit) { |
| 26 | return desc.fWidth <= 0 && |
| 27 | desc.fHeight <= 0 && |
| 28 | desc.fConfig != kUnknown_GrPixelConfig && |
Brian Salomon | d0d7270 | 2018-02-01 14:16:02 -0500 | [diff] [blame^] | 29 | desc.fSampleCnt == 1 && |
Greg Daniel | 65fa8ca | 2018-01-10 17:06:31 -0500 | [diff] [blame] | 30 | SkBackingFit::kApprox == fit; |
| 31 | } |
| 32 | |
| 33 | static bool is_valid_partially_lazy(const GrSurfaceDesc& desc) { |
| 34 | return ((desc.fWidth > 0 && desc.fHeight > 0) || |
| 35 | (desc.fWidth <= 0 && desc.fHeight <= 0)) && |
| 36 | desc.fConfig != kUnknown_GrPixelConfig; |
| 37 | } |
| 38 | |
| 39 | static bool is_valid_non_lazy(const GrSurfaceDesc& desc) { |
| 40 | return desc.fWidth > 0 && |
| 41 | desc.fHeight > 0 && |
| 42 | desc.fConfig != kUnknown_GrPixelConfig; |
| 43 | } |
| 44 | #endif |
| 45 | |
Chris Dalton | 706a6ff | 2017-11-29 22:01:06 -0700 | [diff] [blame] | 46 | // Lazy-callback version |
Greg Daniel | 65fa8ca | 2018-01-10 17:06:31 -0500 | [diff] [blame] | 47 | GrSurfaceProxy::GrSurfaceProxy(LazyInstantiateCallback&& callback, const GrSurfaceDesc& desc, |
| 48 | SkBackingFit fit, SkBudgeted budgeted, uint32_t flags) |
| 49 | : fConfig(desc.fConfig) |
| 50 | , fWidth(desc.fWidth) |
| 51 | , fHeight(desc.fHeight) |
| 52 | , fOrigin(desc.fOrigin) |
| 53 | , fFit(fit) |
| 54 | , fBudgeted(budgeted) |
| 55 | , fFlags(flags) |
Chris Dalton | 706a6ff | 2017-11-29 22:01:06 -0700 | [diff] [blame] | 56 | , fLazyInstantiateCallback(std::move(callback)) |
Greg Daniel | 65fa8ca | 2018-01-10 17:06:31 -0500 | [diff] [blame] | 57 | , fNeedsClear(SkToBool(desc.fFlags & kPerformInitialClear_GrSurfaceFlag)) |
Chris Dalton | 706a6ff | 2017-11-29 22:01:06 -0700 | [diff] [blame] | 58 | , fGpuMemorySize(kInvalidGpuMemorySize) |
| 59 | , fLastOpList(nullptr) { |
| 60 | // 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] | 61 | if (fLazyInstantiateCallback) { |
| 62 | SkASSERT(is_valid_fully_lazy(desc, fit) || is_valid_partially_lazy(desc)); |
| 63 | } else { |
| 64 | SkASSERT(is_valid_non_lazy(desc)); |
| 65 | } |
| 66 | |
Chris Dalton | 706a6ff | 2017-11-29 22:01:06 -0700 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | // Wrapped version |
Robert Phillips | 066f020 | 2017-07-25 10:16:35 -0400 | [diff] [blame] | 70 | GrSurfaceProxy::GrSurfaceProxy(sk_sp<GrSurface> surface, GrSurfaceOrigin origin, SkBackingFit fit) |
Brian Salomon | bb5711a | 2017-05-17 13:49:59 -0400 | [diff] [blame] | 71 | : INHERITED(std::move(surface)) |
| 72 | , fConfig(fTarget->config()) |
| 73 | , fWidth(fTarget->width()) |
| 74 | , fHeight(fTarget->height()) |
Robert Phillips | 066f020 | 2017-07-25 10:16:35 -0400 | [diff] [blame] | 75 | , fOrigin(origin) |
Brian Salomon | bb5711a | 2017-05-17 13:49:59 -0400 | [diff] [blame] | 76 | , fFit(fit) |
| 77 | , fBudgeted(fTarget->resourcePriv().isBudgeted()) |
| 78 | , fFlags(0) |
| 79 | , 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] | 80 | , fNeedsClear(false) |
Brian Salomon | bb5711a | 2017-05-17 13:49:59 -0400 | [diff] [blame] | 81 | , fGpuMemorySize(kInvalidGpuMemorySize) |
Robert Phillips | 019ff27 | 2017-07-24 14:47:57 -0400 | [diff] [blame] | 82 | , fLastOpList(nullptr) { |
Robert Phillips | 019ff27 | 2017-07-24 14:47:57 -0400 | [diff] [blame] | 83 | } |
Robert Phillips | c7635fa | 2016-10-28 13:25:24 -0400 | [diff] [blame] | 84 | |
Robert Phillips | f2361d2 | 2016-10-25 14:20:06 -0400 | [diff] [blame] | 85 | GrSurfaceProxy::~GrSurfaceProxy() { |
Greg Daniel | 94a6ce8 | 2018-01-16 16:14:41 -0500 | [diff] [blame] | 86 | if (fLazyInstantiateCallback) { |
Greg Daniel | 0a375db | 2018-02-01 12:21:39 -0500 | [diff] [blame] | 87 | // We call the callback with a null GrResourceProvider to signal that the lambda should |
| 88 | // clean itself up if it is holding onto any captured objects. |
Greg Daniel | 94a6ce8 | 2018-01-16 16:14:41 -0500 | [diff] [blame] | 89 | this->fLazyInstantiateCallback(nullptr, nullptr); |
| 90 | } |
Robert Phillips | 6cdc22c | 2017-05-11 16:29:14 -0400 | [diff] [blame] | 91 | // For this to be deleted the opList that held a ref on it (if there was one) must have been |
| 92 | // deleted. Which would have cleared out this back pointer. |
| 93 | SkASSERT(!fLastOpList); |
Robert Phillips | f2361d2 | 2016-10-25 14:20:06 -0400 | [diff] [blame] | 94 | } |
| 95 | |
Robert Phillips | eafd48a | 2017-11-16 07:52:08 -0500 | [diff] [blame] | 96 | bool GrSurfaceProxyPriv::AttachStencilIfNeeded(GrResourceProvider* resourceProvider, |
| 97 | GrSurface* surface, bool needsStencil) { |
Robert Phillips | 6504813 | 2017-08-10 08:44:49 -0400 | [diff] [blame] | 98 | if (needsStencil) { |
| 99 | GrRenderTarget* rt = surface->asRenderTarget(); |
| 100 | if (!rt) { |
| 101 | SkASSERT(0); |
| 102 | return false; |
| 103 | } |
| 104 | |
| 105 | if (!resourceProvider->attachStencilAttachment(rt)) { |
| 106 | return false; |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | return true; |
| 111 | } |
| 112 | |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 113 | sk_sp<GrSurface> GrSurfaceProxy::createSurfaceImpl( |
Robert Phillips | 6504813 | 2017-08-10 08:44:49 -0400 | [diff] [blame] | 114 | GrResourceProvider* resourceProvider, |
| 115 | int sampleCnt, bool needsStencil, |
Greg Daniel | e252f08 | 2017-10-23 16:05:23 -0400 | [diff] [blame] | 116 | GrSurfaceFlags flags, GrMipMapped mipMapped, |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 117 | SkDestinationSurfaceColorMode mipColorMode) const { |
Greg Daniel | 65fa8ca | 2018-01-10 17:06:31 -0500 | [diff] [blame] | 118 | SkASSERT(GrSurfaceProxy::LazyState::kNot == this->lazyInstantiationState()); |
Greg Daniel | e252f08 | 2017-10-23 16:05:23 -0400 | [diff] [blame] | 119 | SkASSERT(GrMipMapped::kNo == mipMapped); |
Brian Salomon | bb5711a | 2017-05-17 13:49:59 -0400 | [diff] [blame] | 120 | GrSurfaceDesc desc; |
Brian Salomon | bb5711a | 2017-05-17 13:49:59 -0400 | [diff] [blame] | 121 | desc.fFlags = flags; |
Brian Salomon | d17b4a6 | 2017-05-23 16:53:47 -0400 | [diff] [blame] | 122 | if (fNeedsClear) { |
| 123 | desc.fFlags |= kPerformInitialClear_GrSurfaceFlag; |
| 124 | } |
Robert Phillips | 16d8ec6 | 2017-07-27 16:16:25 -0400 | [diff] [blame] | 125 | desc.fOrigin = fOrigin; |
| 126 | desc.fWidth = fWidth; |
| 127 | desc.fHeight = fHeight; |
| 128 | desc.fConfig = fConfig; |
| 129 | desc.fSampleCnt = sampleCnt; |
Robert Phillips | eaa8625 | 2016-11-08 13:49:39 +0000 | [diff] [blame] | 130 | |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 131 | sk_sp<GrSurface> surface; |
Robert Phillips | eaa8625 | 2016-11-08 13:49:39 +0000 | [diff] [blame] | 132 | if (SkBackingFit::kApprox == fFit) { |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 133 | surface.reset(resourceProvider->createApproxTexture(desc, fFlags).release()); |
Robert Phillips | eaa8625 | 2016-11-08 13:49:39 +0000 | [diff] [blame] | 134 | } else { |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 135 | surface.reset(resourceProvider->createTexture(desc, fBudgeted, fFlags).release()); |
Robert Phillips | eaa8625 | 2016-11-08 13:49:39 +0000 | [diff] [blame] | 136 | } |
Robert Phillips | 6504813 | 2017-08-10 08:44:49 -0400 | [diff] [blame] | 137 | if (!surface) { |
| 138 | return nullptr; |
| 139 | } |
| 140 | |
| 141 | surface->asTexture()->texturePriv().setMipColorMode(mipColorMode); |
| 142 | |
Robert Phillips | eafd48a | 2017-11-16 07:52:08 -0500 | [diff] [blame] | 143 | if (!GrSurfaceProxyPriv::AttachStencilIfNeeded(resourceProvider, surface.get(), needsStencil)) { |
Robert Phillips | 6504813 | 2017-08-10 08:44:49 -0400 | [diff] [blame] | 144 | return nullptr; |
Robert Phillips | eaa8625 | 2016-11-08 13:49:39 +0000 | [diff] [blame] | 145 | } |
| 146 | |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 147 | return surface; |
| 148 | } |
| 149 | |
| 150 | void GrSurfaceProxy::assign(sk_sp<GrSurface> surface) { |
| 151 | SkASSERT(!fTarget && surface); |
| 152 | fTarget = surface.release(); |
robertphillips | 1125a03 | 2016-11-16 11:17:17 -0800 | [diff] [blame] | 153 | this->INHERITED::transferRefs(); |
| 154 | |
Robert Phillips | eaa8625 | 2016-11-08 13:49:39 +0000 | [diff] [blame] | 155 | #ifdef SK_DEBUG |
| 156 | if (kInvalidGpuMemorySize != this->getRawGpuMemorySize_debugOnly()) { |
Robert Phillips | 784b7bf | 2016-12-09 13:35:02 -0500 | [diff] [blame] | 157 | SkASSERT(fTarget->gpuMemorySize() <= this->getRawGpuMemorySize_debugOnly()); |
Robert Phillips | eaa8625 | 2016-11-08 13:49:39 +0000 | [diff] [blame] | 158 | } |
| 159 | #endif |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 160 | } |
Robert Phillips | eaa8625 | 2016-11-08 13:49:39 +0000 | [diff] [blame] | 161 | |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 162 | bool GrSurfaceProxy::instantiateImpl(GrResourceProvider* resourceProvider, int sampleCnt, |
Greg Daniel | e252f08 | 2017-10-23 16:05:23 -0400 | [diff] [blame] | 163 | bool needsStencil, GrSurfaceFlags flags, GrMipMapped mipMapped, |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 164 | SkDestinationSurfaceColorMode mipColorMode, |
| 165 | const GrUniqueKey* uniqueKey) { |
Greg Daniel | 65fa8ca | 2018-01-10 17:06:31 -0500 | [diff] [blame] | 166 | SkASSERT(LazyState::kNot == this->lazyInstantiationState()); |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 167 | if (fTarget) { |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 168 | if (uniqueKey) { |
| 169 | SkASSERT(fTarget->getUniqueKey() == *uniqueKey); |
| 170 | } |
Robert Phillips | eafd48a | 2017-11-16 07:52:08 -0500 | [diff] [blame] | 171 | return GrSurfaceProxyPriv::AttachStencilIfNeeded(resourceProvider, fTarget, needsStencil); |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 172 | } |
| 173 | |
Robert Phillips | 6504813 | 2017-08-10 08:44:49 -0400 | [diff] [blame] | 174 | sk_sp<GrSurface> surface = this->createSurfaceImpl(resourceProvider, sampleCnt, needsStencil, |
Greg Daniel | e252f08 | 2017-10-23 16:05:23 -0400 | [diff] [blame] | 175 | flags, mipMapped, mipColorMode); |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 176 | if (!surface) { |
| 177 | return false; |
| 178 | } |
| 179 | |
Brian Osman | 28c434b | 2017-09-27 13:11:16 -0400 | [diff] [blame] | 180 | // If there was an invalidation message pending for this key, we might have just processed it, |
| 181 | // causing the key (stored on this proxy) to become invalid. |
| 182 | if (uniqueKey && uniqueKey->isValid()) { |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 183 | resourceProvider->assignUniqueKeyToResource(*uniqueKey, surface.get()); |
| 184 | } |
| 185 | |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 186 | this->assign(std::move(surface)); |
Robert Phillips | eee4d6e | 2017-06-05 09:26:07 -0400 | [diff] [blame] | 187 | return true; |
Robert Phillips | eaa8625 | 2016-11-08 13:49:39 +0000 | [diff] [blame] | 188 | } |
| 189 | |
Robert Phillips | 57aa367 | 2017-07-21 11:38:13 -0400 | [diff] [blame] | 190 | void GrSurfaceProxy::computeScratchKey(GrScratchKey* key) const { |
Greg Daniel | 65fa8ca | 2018-01-10 17:06:31 -0500 | [diff] [blame] | 191 | SkASSERT(LazyState::kFully != this->lazyInstantiationState()); |
Robert Phillips | 57aa367 | 2017-07-21 11:38:13 -0400 | [diff] [blame] | 192 | const GrRenderTargetProxy* rtp = this->asRenderTargetProxy(); |
Brian Salomon | d0d7270 | 2018-02-01 14:16:02 -0500 | [diff] [blame^] | 193 | int sampleCount = 1; |
Robert Phillips | 57aa367 | 2017-07-21 11:38:13 -0400 | [diff] [blame] | 194 | if (rtp) { |
| 195 | sampleCount = rtp->numStencilSamples(); |
| 196 | } |
| 197 | |
| 198 | const GrTextureProxy* tp = this->asTextureProxy(); |
Greg Daniel | e252f08 | 2017-10-23 16:05:23 -0400 | [diff] [blame] | 199 | GrMipMapped mipMapped = GrMipMapped::kNo; |
Robert Phillips | 57aa367 | 2017-07-21 11:38:13 -0400 | [diff] [blame] | 200 | if (tp) { |
Greg Daniel | e252f08 | 2017-10-23 16:05:23 -0400 | [diff] [blame] | 201 | mipMapped = tp->mipMapped(); |
Robert Phillips | 57aa367 | 2017-07-21 11:38:13 -0400 | [diff] [blame] | 202 | } |
| 203 | |
Robert Phillips | a108c92 | 2017-10-10 10:42:19 -0400 | [diff] [blame] | 204 | int width = this->worstCaseWidth(); |
| 205 | int height = this->worstCaseHeight(); |
Robert Phillips | 57aa367 | 2017-07-21 11:38:13 -0400 | [diff] [blame] | 206 | |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 207 | GrTexturePriv::ComputeScratchKey(this->config(), width, height, SkToBool(rtp), sampleCount, |
Greg Daniel | e252f08 | 2017-10-23 16:05:23 -0400 | [diff] [blame] | 208 | mipMapped, key); |
Robert Phillips | 57aa367 | 2017-07-21 11:38:13 -0400 | [diff] [blame] | 209 | } |
| 210 | |
Robert Phillips | f2361d2 | 2016-10-25 14:20:06 -0400 | [diff] [blame] | 211 | void GrSurfaceProxy::setLastOpList(GrOpList* opList) { |
Robert Phillips | 6cdc22c | 2017-05-11 16:29:14 -0400 | [diff] [blame] | 212 | #ifdef SK_DEBUG |
Robert Phillips | f2361d2 | 2016-10-25 14:20:06 -0400 | [diff] [blame] | 213 | if (fLastOpList) { |
Robert Phillips | f2361d2 | 2016-10-25 14:20:06 -0400 | [diff] [blame] | 214 | SkASSERT(fLastOpList->isClosed()); |
Robert Phillips | 4a39504 | 2017-04-24 16:27:17 +0000 | [diff] [blame] | 215 | } |
Robert Phillips | 6cdc22c | 2017-05-11 16:29:14 -0400 | [diff] [blame] | 216 | #endif |
Robert Phillips | f2361d2 | 2016-10-25 14:20:06 -0400 | [diff] [blame] | 217 | |
Robert Phillips | 6cdc22c | 2017-05-11 16:29:14 -0400 | [diff] [blame] | 218 | // Un-reffed |
| 219 | fLastOpList = opList; |
Robert Phillips | f2361d2 | 2016-10-25 14:20:06 -0400 | [diff] [blame] | 220 | } |
Robert Phillips | 3743013 | 2016-11-09 06:50:43 -0500 | [diff] [blame] | 221 | |
Brian Osman | 45580d3 | 2016-11-23 09:37:01 -0500 | [diff] [blame] | 222 | GrRenderTargetOpList* GrSurfaceProxy::getLastRenderTargetOpList() { |
| 223 | return fLastOpList ? fLastOpList->asRenderTargetOpList() : nullptr; |
| 224 | } |
| 225 | |
| 226 | GrTextureOpList* GrSurfaceProxy::getLastTextureOpList() { |
| 227 | return fLastOpList ? fLastOpList->asTextureOpList() : nullptr; |
| 228 | } |
| 229 | |
Robert Phillips | a108c92 | 2017-10-10 10:42:19 -0400 | [diff] [blame] | 230 | int GrSurfaceProxy::worstCaseWidth() const { |
Greg Daniel | 65fa8ca | 2018-01-10 17:06:31 -0500 | [diff] [blame] | 231 | SkASSERT(LazyState::kFully != this->lazyInstantiationState()); |
Robert Phillips | a108c92 | 2017-10-10 10:42:19 -0400 | [diff] [blame] | 232 | if (fTarget) { |
| 233 | return fTarget->width(); |
| 234 | } |
| 235 | |
| 236 | if (SkBackingFit::kExact == fFit) { |
| 237 | return fWidth; |
| 238 | } |
| 239 | return SkTMax(GrResourceProvider::kMinScratchTextureSize, GrNextPow2(fWidth)); |
| 240 | } |
| 241 | |
| 242 | int GrSurfaceProxy::worstCaseHeight() const { |
Greg Daniel | 65fa8ca | 2018-01-10 17:06:31 -0500 | [diff] [blame] | 243 | SkASSERT(LazyState::kFully != this->lazyInstantiationState()); |
Robert Phillips | a108c92 | 2017-10-10 10:42:19 -0400 | [diff] [blame] | 244 | if (fTarget) { |
| 245 | return fTarget->height(); |
| 246 | } |
| 247 | |
| 248 | if (SkBackingFit::kExact == fFit) { |
| 249 | return fHeight; |
| 250 | } |
| 251 | return SkTMax(GrResourceProvider::kMinScratchTextureSize, GrNextPow2(fHeight)); |
| 252 | } |
| 253 | |
Brian Osman | 45580d3 | 2016-11-23 09:37:01 -0500 | [diff] [blame] | 254 | #ifdef SK_DEBUG |
| 255 | void GrSurfaceProxy::validate(GrContext* context) const { |
| 256 | if (fTarget) { |
| 257 | SkASSERT(fTarget->getContext() == context); |
| 258 | } |
| 259 | |
| 260 | INHERITED::validate(); |
| 261 | } |
| 262 | #endif |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 263 | |
Robert Phillips | 63c6746 | 2017-02-15 14:19:01 -0500 | [diff] [blame] | 264 | sk_sp<GrTextureProxy> GrSurfaceProxy::Copy(GrContext* context, |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 265 | GrSurfaceProxy* src, |
Greg Daniel | 65c7f66 | 2017-10-30 13:39:09 -0400 | [diff] [blame] | 266 | GrMipMapped mipMapped, |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 267 | SkIRect srcRect, |
| 268 | SkBudgeted budgeted) { |
Greg Daniel | 65fa8ca | 2018-01-10 17:06:31 -0500 | [diff] [blame] | 269 | SkASSERT(LazyState::kFully != src->lazyInstantiationState()); |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 270 | if (!srcRect.intersect(SkIRect::MakeWH(src->width(), src->height()))) { |
| 271 | return nullptr; |
| 272 | } |
| 273 | |
Brian Salomon | 63e7973 | 2017-05-15 21:23:13 -0400 | [diff] [blame] | 274 | GrSurfaceDesc dstDesc; |
Robert Phillips | 16d8ec6 | 2017-07-27 16:16:25 -0400 | [diff] [blame] | 275 | dstDesc.fOrigin = src->origin(); |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 276 | dstDesc.fWidth = srcRect.width(); |
| 277 | dstDesc.fHeight = srcRect.height(); |
Robert Phillips | 16d8ec6 | 2017-07-27 16:16:25 -0400 | [diff] [blame] | 278 | dstDesc.fConfig = src->config(); |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 279 | |
| 280 | sk_sp<GrSurfaceContext> dstContext(context->contextPriv().makeDeferredSurfaceContext( |
| 281 | dstDesc, |
Greg Daniel | 65c7f66 | 2017-10-30 13:39:09 -0400 | [diff] [blame] | 282 | mipMapped, |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 283 | SkBackingFit::kExact, |
| 284 | budgeted)); |
| 285 | if (!dstContext) { |
| 286 | return nullptr; |
| 287 | } |
| 288 | |
| 289 | if (!dstContext->copy(src, srcRect, SkIPoint::Make(0, 0))) { |
| 290 | return nullptr; |
| 291 | } |
| 292 | |
Robert Phillips | 63c6746 | 2017-02-15 14:19:01 -0500 | [diff] [blame] | 293 | return dstContext->asTextureProxyRef(); |
| 294 | } |
| 295 | |
| 296 | sk_sp<GrTextureProxy> GrSurfaceProxy::Copy(GrContext* context, GrSurfaceProxy* src, |
Greg Daniel | 65c7f66 | 2017-10-30 13:39:09 -0400 | [diff] [blame] | 297 | GrMipMapped mipMapped, SkBudgeted budgeted) { |
Greg Daniel | 65fa8ca | 2018-01-10 17:06:31 -0500 | [diff] [blame] | 298 | SkASSERT(LazyState::kFully != src->lazyInstantiationState()); |
Greg Daniel | 65c7f66 | 2017-10-30 13:39:09 -0400 | [diff] [blame] | 299 | return Copy(context, src, mipMapped, SkIRect::MakeWH(src->width(), src->height()), budgeted); |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 300 | } |
| 301 | |
Robert Phillips | d46697a | 2017-01-25 12:10:37 -0500 | [diff] [blame] | 302 | sk_sp<GrSurfaceContext> GrSurfaceProxy::TestCopy(GrContext* context, const GrSurfaceDesc& dstDesc, |
| 303 | GrSurfaceProxy* srcProxy) { |
Greg Daniel | 65fa8ca | 2018-01-10 17:06:31 -0500 | [diff] [blame] | 304 | SkASSERT(LazyState::kFully != srcProxy->lazyInstantiationState()); |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 305 | sk_sp<GrSurfaceContext> dstContext(context->contextPriv().makeDeferredSurfaceContext( |
| 306 | dstDesc, |
Greg Daniel | 65c7f66 | 2017-10-30 13:39:09 -0400 | [diff] [blame] | 307 | GrMipMapped::kNo, |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 308 | SkBackingFit::kExact, |
Robert Phillips | d46697a | 2017-01-25 12:10:37 -0500 | [diff] [blame] | 309 | SkBudgeted::kYes)); |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 310 | if (!dstContext) { |
| 311 | return nullptr; |
| 312 | } |
| 313 | |
Robert Phillips | d46697a | 2017-01-25 12:10:37 -0500 | [diff] [blame] | 314 | if (!dstContext->copy(srcProxy)) { |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 315 | return nullptr; |
| 316 | } |
| 317 | |
Robert Phillips | d46697a | 2017-01-25 12:10:37 -0500 | [diff] [blame] | 318 | return dstContext; |
Robert Phillips | e2f7d18 | 2016-12-15 09:23:05 -0500 | [diff] [blame] | 319 | } |
Robert Phillips | b726d58 | 2017-03-09 16:36:32 -0500 | [diff] [blame] | 320 | |
Robert Phillips | 0ae6faa | 2017-03-21 16:22:00 -0400 | [diff] [blame] | 321 | void GrSurfaceProxyPriv::exactify() { |
Greg Daniel | 65fa8ca | 2018-01-10 17:06:31 -0500 | [diff] [blame] | 322 | SkASSERT(GrSurfaceProxy::LazyState::kFully != fProxy->lazyInstantiationState()); |
Robert Phillips | 0ae6faa | 2017-03-21 16:22:00 -0400 | [diff] [blame] | 323 | if (this->isExact()) { |
| 324 | return; |
| 325 | } |
| 326 | |
| 327 | SkASSERT(SkBackingFit::kApprox == fProxy->fFit); |
| 328 | |
| 329 | if (fProxy->fTarget) { |
| 330 | // The kApprox but already instantiated case. Setting the proxy's width & height to |
| 331 | // 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] | 332 | // obliterating the area of interest information. This call (exactify) only used |
Robert Phillips | 0ae6faa | 2017-03-21 16:22:00 -0400 | [diff] [blame] | 333 | // when converting an SkSpecialImage to an SkImage so the proxy shouldn't be |
| 334 | // used for additional draws. |
Brian Salomon | bb5711a | 2017-05-17 13:49:59 -0400 | [diff] [blame] | 335 | fProxy->fWidth = fProxy->fTarget->width(); |
| 336 | fProxy->fHeight = fProxy->fTarget->height(); |
Robert Phillips | 0ae6faa | 2017-03-21 16:22:00 -0400 | [diff] [blame] | 337 | return; |
| 338 | } |
| 339 | |
| 340 | // The kApprox uninstantiated case. Making this proxy be exact should be okay. |
| 341 | // It could mess things up if prior decisions were based on the approximate size. |
| 342 | fProxy->fFit = SkBackingFit::kExact; |
| 343 | // 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] | 344 | // 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] | 345 | // the special image goes away. If it hasn't been computed yet it might as well compute the |
| 346 | // exact amount. |
| 347 | } |
| 348 | |
Greg Daniel | bddcc95 | 2018-01-24 13:22:24 -0500 | [diff] [blame] | 349 | bool GrSurfaceProxyPriv::doLazyInstantiation(GrResourceProvider* resourceProvider) { |
Greg Daniel | 0a375db | 2018-02-01 12:21:39 -0500 | [diff] [blame] | 350 | SkASSERT(GrSurfaceProxy::LazyState::kNot != fProxy->lazyInstantiationState()); |
Chris Dalton | 706a6ff | 2017-11-29 22:01:06 -0700 | [diff] [blame] | 351 | |
Greg Daniel | f2336e4 | 2018-01-23 16:38:14 -0500 | [diff] [blame] | 352 | GrSurfaceOrigin* outOrigin; |
| 353 | if (GrSurfaceProxy::LazyState::kPartially == fProxy->lazyInstantiationState()) { |
| 354 | // In the partially instantiated case, we set the origin on the SurfaceProxy at creation |
| 355 | // time (via a GrSurfaceDesc). In the lambda, the creation of the texture never needs to |
| 356 | // know the origin, and it also can't change or have any effect on it. Thus we just pass in |
| 357 | // nullptr in this case since it should never be set. |
| 358 | outOrigin = nullptr; |
| 359 | } else { |
| 360 | outOrigin = &fProxy->fOrigin; |
| 361 | } |
| 362 | |
| 363 | sk_sp<GrTexture> texture = fProxy->fLazyInstantiateCallback(resourceProvider, outOrigin); |
| 364 | |
Chris Dalton | 706a6ff | 2017-11-29 22:01:06 -0700 | [diff] [blame] | 365 | if (!texture) { |
| 366 | fProxy->fWidth = 0; |
| 367 | fProxy->fHeight = 0; |
| 368 | fProxy->fOrigin = kTopLeft_GrSurfaceOrigin; |
Greg Daniel | bddcc95 | 2018-01-24 13:22:24 -0500 | [diff] [blame] | 369 | return false; |
Chris Dalton | 706a6ff | 2017-11-29 22:01:06 -0700 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | fProxy->fWidth = texture->width(); |
| 373 | fProxy->fHeight = texture->height(); |
| 374 | |
| 375 | SkASSERT(texture->config() == fProxy->fConfig); |
Chris Dalton | 706a6ff | 2017-11-29 22:01:06 -0700 | [diff] [blame] | 376 | SkDEBUGCODE(fProxy->validateLazyTexture(texture.get());) |
| 377 | this->assign(std::move(texture)); |
Greg Daniel | bddcc95 | 2018-01-24 13:22:24 -0500 | [diff] [blame] | 378 | return true; |
Chris Dalton | 706a6ff | 2017-11-29 22:01:06 -0700 | [diff] [blame] | 379 | } |
| 380 | |