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