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