Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018 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 "GrProxyProvider.h" |
| 9 | |
| 10 | #include "GrCaps.h" |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 11 | #include "GrRenderTarget.h" |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 12 | #include "GrResourceKey.h" |
| 13 | #include "GrResourceProvider.h" |
| 14 | #include "GrSurfaceProxy.h" |
| 15 | #include "GrSurfaceProxyPriv.h" |
| 16 | #include "GrTexture.h" |
| 17 | #include "GrTextureProxyCacheAccess.h" |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 18 | #include "GrTextureRenderTargetProxy.h" |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 19 | #include "../private/GrSingleOwner.h" |
Greg Daniel | a4ead65 | 2018-02-07 10:21:48 -0500 | [diff] [blame] | 20 | #include "SkBitmap.h" |
Greg Daniel | 9d86f1d | 2018-01-29 09:33:59 -0500 | [diff] [blame] | 21 | #include "SkGr.h" |
| 22 | #include "SkImage.h" |
| 23 | #include "SkImage_Base.h" |
Greg Daniel | a4ead65 | 2018-02-07 10:21:48 -0500 | [diff] [blame] | 24 | #include "SkImageInfoPriv.h" |
| 25 | #include "SkImagePriv.h" |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 26 | #include "SkMipMap.h" |
Greg Daniel | a4ead65 | 2018-02-07 10:21:48 -0500 | [diff] [blame] | 27 | #include "SkTraceEvent.h" |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 28 | |
| 29 | #define ASSERT_SINGLE_OWNER \ |
| 30 | SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(fSingleOwner);) |
| 31 | |
| 32 | GrProxyProvider::GrProxyProvider(GrResourceProvider* resourceProvider, |
| 33 | GrResourceCache* resourceCache, |
| 34 | sk_sp<const GrCaps> caps, |
| 35 | GrSingleOwner* owner) |
| 36 | : fResourceProvider(resourceProvider) |
| 37 | , fResourceCache(resourceCache) |
Robert Phillips | 4d12051 | 2018-01-19 13:22:07 -0500 | [diff] [blame] | 38 | , fAbandoned(false) |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 39 | , fCaps(caps) |
Brian Salomon | 238069b | 2018-07-11 15:58:57 -0400 | [diff] [blame^] | 40 | , fContextUniqueID(resourceCache->contextUniqueID()) |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 41 | #ifdef SK_DEBUG |
| 42 | , fSingleOwner(owner) |
| 43 | #endif |
| 44 | { |
Brian Salomon | 238069b | 2018-07-11 15:58:57 -0400 | [diff] [blame^] | 45 | SkASSERT(fResourceProvider); |
| 46 | SkASSERT(fResourceCache); |
| 47 | SkASSERT(fCaps); |
| 48 | SkASSERT(fSingleOwner); |
| 49 | } |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 50 | |
Brian Salomon | 238069b | 2018-07-11 15:58:57 -0400 | [diff] [blame^] | 51 | GrProxyProvider::GrProxyProvider(uint32_t contextUniqueID, |
| 52 | sk_sp<const GrCaps> caps, |
| 53 | GrSingleOwner* owner) |
| 54 | : fResourceProvider(nullptr) |
| 55 | , fResourceCache(nullptr) |
| 56 | , fAbandoned(false) |
| 57 | , fCaps(caps) |
| 58 | , fContextUniqueID(contextUniqueID) |
| 59 | #ifdef SK_DEBUG |
| 60 | , fSingleOwner(owner) |
| 61 | #endif |
| 62 | { |
| 63 | SkASSERT(fContextUniqueID != SK_InvalidUniqueID); |
| 64 | SkASSERT(fCaps); |
| 65 | SkASSERT(fSingleOwner); |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | GrProxyProvider::~GrProxyProvider() { |
| 69 | SkASSERT(!fUniquelyKeyedProxies.count()); |
| 70 | } |
| 71 | |
Robert Phillips | adbe132 | 2018-01-17 13:35:46 -0500 | [diff] [blame] | 72 | bool GrProxyProvider::assignUniqueKeyToProxy(const GrUniqueKey& key, GrTextureProxy* proxy) { |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 73 | ASSERT_SINGLE_OWNER |
| 74 | SkASSERT(key.isValid()); |
| 75 | if (this->isAbandoned() || !proxy) { |
Robert Phillips | adbe132 | 2018-01-17 13:35:46 -0500 | [diff] [blame] | 76 | return false; |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | // If there is already a GrResource with this key then the caller has violated the normal |
| 80 | // usage pattern of uniquely keyed resources (e.g., they have created one w/o first seeing |
| 81 | // if it already existed in the cache). |
Robert Phillips | d5f9cdd | 2018-01-31 09:29:48 -0500 | [diff] [blame] | 82 | SkASSERT(!fResourceCache || !fResourceCache->findAndRefUniqueResource(key)); |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 83 | |
| 84 | // Uncached resources can never have a unique key, unless they're wrapped resources. Wrapped |
| 85 | // resources are a special case: the unique keys give us a weak ref so that we can reuse the |
| 86 | // same resource (rather than re-wrapping). When a wrapped resource is no longer referenced, |
| 87 | // it will always be released - it is never converted to a scratch resource. |
| 88 | if (SkBudgeted::kNo == proxy->isBudgeted() && |
| 89 | (!proxy->priv().isInstantiated() || |
| 90 | !proxy->priv().peekSurface()->resourcePriv().refsWrappedObjects())) { |
Robert Phillips | adbe132 | 2018-01-17 13:35:46 -0500 | [diff] [blame] | 91 | return false; |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | SkASSERT(!fUniquelyKeyedProxies.find(key)); // multiple proxies can't get the same key |
| 95 | |
| 96 | proxy->cacheAccess().setUniqueKey(this, key); |
| 97 | SkASSERT(proxy->getUniqueKey() == key); |
| 98 | fUniquelyKeyedProxies.add(proxy); |
Robert Phillips | adbe132 | 2018-01-17 13:35:46 -0500 | [diff] [blame] | 99 | return true; |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | void GrProxyProvider::adoptUniqueKeyFromSurface(GrTextureProxy* proxy, const GrSurface* surf) { |
| 103 | SkASSERT(surf->getUniqueKey().isValid()); |
| 104 | proxy->cacheAccess().setUniqueKey(this, surf->getUniqueKey()); |
| 105 | SkASSERT(proxy->getUniqueKey() == surf->getUniqueKey()); |
| 106 | // multiple proxies can't get the same key |
| 107 | SkASSERT(!fUniquelyKeyedProxies.find(surf->getUniqueKey())); |
| 108 | fUniquelyKeyedProxies.add(proxy); |
| 109 | } |
| 110 | |
| 111 | void GrProxyProvider::removeUniqueKeyFromProxy(const GrUniqueKey& key, GrTextureProxy* proxy) { |
| 112 | ASSERT_SINGLE_OWNER |
| 113 | if (this->isAbandoned() || !proxy) { |
| 114 | return; |
| 115 | } |
| 116 | this->processInvalidProxyUniqueKey(key, proxy, true); |
| 117 | } |
| 118 | |
| 119 | sk_sp<GrTextureProxy> GrProxyProvider::findProxyByUniqueKey(const GrUniqueKey& key, |
| 120 | GrSurfaceOrigin origin) { |
| 121 | ASSERT_SINGLE_OWNER |
| 122 | |
| 123 | if (this->isAbandoned()) { |
| 124 | return nullptr; |
| 125 | } |
| 126 | |
| 127 | sk_sp<GrTextureProxy> result = sk_ref_sp(fUniquelyKeyedProxies.find(key)); |
| 128 | if (result) { |
| 129 | SkASSERT(result->origin() == origin); |
| 130 | } |
| 131 | return result; |
| 132 | } |
| 133 | |
Robert Phillips | adbe132 | 2018-01-17 13:35:46 -0500 | [diff] [blame] | 134 | sk_sp<GrTextureProxy> GrProxyProvider::createWrapped(sk_sp<GrTexture> tex, GrSurfaceOrigin origin) { |
| 135 | #ifdef SK_DEBUG |
| 136 | if (tex->getUniqueKey().isValid()) { |
| 137 | SkASSERT(!this->findProxyByUniqueKey(tex->getUniqueKey(), origin)); |
| 138 | } |
| 139 | #endif |
| 140 | |
| 141 | if (tex->asRenderTarget()) { |
| 142 | return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(std::move(tex), origin)); |
| 143 | } else { |
| 144 | return sk_sp<GrTextureProxy>(new GrTextureProxy(std::move(tex), origin)); |
| 145 | } |
| 146 | } |
| 147 | |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 148 | sk_sp<GrTextureProxy> GrProxyProvider::findOrCreateProxyByUniqueKey(const GrUniqueKey& key, |
| 149 | GrSurfaceOrigin origin) { |
| 150 | ASSERT_SINGLE_OWNER |
| 151 | |
| 152 | if (this->isAbandoned()) { |
| 153 | return nullptr; |
| 154 | } |
| 155 | |
| 156 | sk_sp<GrTextureProxy> result = this->findProxyByUniqueKey(key, origin); |
| 157 | if (result) { |
| 158 | return result; |
| 159 | } |
| 160 | |
Robert Phillips | d5f9cdd | 2018-01-31 09:29:48 -0500 | [diff] [blame] | 161 | if (!fResourceCache) { |
| 162 | return nullptr; |
| 163 | } |
| 164 | |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 165 | GrGpuResource* resource = fResourceCache->findAndRefUniqueResource(key); |
| 166 | if (!resource) { |
| 167 | return nullptr; |
| 168 | } |
| 169 | |
| 170 | sk_sp<GrTexture> texture(static_cast<GrSurface*>(resource)->asTexture()); |
| 171 | SkASSERT(texture); |
| 172 | |
Robert Phillips | adbe132 | 2018-01-17 13:35:46 -0500 | [diff] [blame] | 173 | result = this->createWrapped(std::move(texture), origin); |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 174 | SkASSERT(result->getUniqueKey() == key); |
Robert Phillips | adbe132 | 2018-01-17 13:35:46 -0500 | [diff] [blame] | 175 | // createWrapped should've added this for us |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 176 | SkASSERT(fUniquelyKeyedProxies.find(key)); |
| 177 | return result; |
| 178 | } |
| 179 | |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 180 | sk_sp<GrTextureProxy> GrProxyProvider::createInstantiatedProxy(const GrSurfaceDesc& desc, |
Brian Salomon | 2a4f983 | 2018-03-03 22:43:43 -0500 | [diff] [blame] | 181 | GrSurfaceOrigin origin, |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 182 | SkBackingFit fit, |
| 183 | SkBudgeted budgeted, |
Robert Phillips | fe0253f | 2018-03-16 16:47:25 -0400 | [diff] [blame] | 184 | GrSurfaceDescFlags descFlags) { |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 185 | sk_sp<GrTexture> tex; |
| 186 | |
| 187 | if (SkBackingFit::kApprox == fit) { |
Robert Phillips | fe0253f | 2018-03-16 16:47:25 -0400 | [diff] [blame] | 188 | tex = fResourceProvider->createApproxTexture(desc, descFlags); |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 189 | } else { |
Robert Phillips | fe0253f | 2018-03-16 16:47:25 -0400 | [diff] [blame] | 190 | tex = fResourceProvider->createTexture(desc, budgeted, descFlags); |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 191 | } |
| 192 | if (!tex) { |
| 193 | return nullptr; |
| 194 | } |
| 195 | |
Brian Salomon | 2a4f983 | 2018-03-03 22:43:43 -0500 | [diff] [blame] | 196 | return this->createWrapped(std::move(tex), origin); |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 197 | } |
| 198 | |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 199 | sk_sp<GrTextureProxy> GrProxyProvider::createTextureProxy(const GrSurfaceDesc& desc, |
Brian Salomon | 2a4f983 | 2018-03-03 22:43:43 -0500 | [diff] [blame] | 200 | SkBudgeted budgeted, const void* srcData, |
| 201 | size_t rowBytes) { |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 202 | ASSERT_SINGLE_OWNER |
| 203 | |
Robert Phillips | 579f094 | 2018-01-08 14:53:35 -0500 | [diff] [blame] | 204 | if (this->isAbandoned()) { |
| 205 | return nullptr; |
| 206 | } |
| 207 | |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 208 | if (srcData) { |
| 209 | GrMipLevel mipLevel = { srcData, rowBytes }; |
| 210 | |
Brian Salomon | 58389b9 | 2018-03-07 13:01:25 -0500 | [diff] [blame] | 211 | sk_sp<GrTexture> tex = |
| 212 | fResourceProvider->createTexture(desc, budgeted, SkBackingFit::kExact, mipLevel); |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 213 | if (!tex) { |
| 214 | return nullptr; |
| 215 | } |
| 216 | |
Brian Salomon | 58389b9 | 2018-03-07 13:01:25 -0500 | [diff] [blame] | 217 | return this->createWrapped(std::move(tex), kTopLeft_GrSurfaceOrigin); |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 218 | } |
| 219 | |
Brian Salomon | 58389b9 | 2018-03-07 13:01:25 -0500 | [diff] [blame] | 220 | return this->createProxy(desc, kTopLeft_GrSurfaceOrigin, SkBackingFit::kExact, budgeted); |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 221 | } |
| 222 | |
Greg Daniel | 9d86f1d | 2018-01-29 09:33:59 -0500 | [diff] [blame] | 223 | sk_sp<GrTextureProxy> GrProxyProvider::createTextureProxy(sk_sp<SkImage> srcImage, |
Robert Phillips | fe0253f | 2018-03-16 16:47:25 -0400 | [diff] [blame] | 224 | GrSurfaceDescFlags descFlags, |
Greg Daniel | 9d86f1d | 2018-01-29 09:33:59 -0500 | [diff] [blame] | 225 | int sampleCnt, |
Greg Daniel | fb3abcd | 2018-02-02 15:48:33 -0500 | [diff] [blame] | 226 | SkBudgeted budgeted, |
| 227 | SkBackingFit fit) { |
Greg Daniel | 9d86f1d | 2018-01-29 09:33:59 -0500 | [diff] [blame] | 228 | ASSERT_SINGLE_OWNER |
| 229 | SkASSERT(srcImage); |
| 230 | |
| 231 | if (this->isAbandoned()) { |
| 232 | return nullptr; |
| 233 | } |
| 234 | |
Brian Osman | 2b23c4b | 2018-06-01 12:25:08 -0400 | [diff] [blame] | 235 | GrPixelConfig config = SkImageInfo2GrPixelConfig(as_IB(srcImage)->onImageInfo()); |
Greg Daniel | f87651e | 2018-02-21 11:36:53 -0500 | [diff] [blame] | 236 | |
Greg Daniel | 0a7aa14 | 2018-02-21 13:02:32 -0500 | [diff] [blame] | 237 | if (kUnknown_GrPixelConfig == config) { |
| 238 | return nullptr; |
| 239 | } |
| 240 | |
Robert Phillips | fe0253f | 2018-03-16 16:47:25 -0400 | [diff] [blame] | 241 | if (SkToBool(descFlags & kRenderTarget_GrSurfaceFlag)) { |
Greg Daniel | f87651e | 2018-02-21 11:36:53 -0500 | [diff] [blame] | 242 | sampleCnt = this->caps()->getRenderTargetSampleCount(sampleCnt, config); |
| 243 | if (!sampleCnt) { |
| 244 | return nullptr; |
| 245 | } |
| 246 | } |
| 247 | |
Robert Phillips | fe0253f | 2018-03-16 16:47:25 -0400 | [diff] [blame] | 248 | GrInternalSurfaceFlags surfaceFlags = GrInternalSurfaceFlags::kNone; |
| 249 | if (SkToBool(descFlags & kRenderTarget_GrSurfaceFlag)) { |
Greg Daniel | 2a30390 | 2018-02-20 10:25:54 -0500 | [diff] [blame] | 250 | if (fCaps->usesMixedSamples() && sampleCnt > 1) { |
Robert Phillips | fe0253f | 2018-03-16 16:47:25 -0400 | [diff] [blame] | 251 | surfaceFlags |= GrInternalSurfaceFlags::kMixedSampled; |
Greg Daniel | 2a30390 | 2018-02-20 10:25:54 -0500 | [diff] [blame] | 252 | } |
| 253 | if (fCaps->maxWindowRectangles() > 0) { |
Robert Phillips | fe0253f | 2018-03-16 16:47:25 -0400 | [diff] [blame] | 254 | surfaceFlags |= GrInternalSurfaceFlags::kWindowRectsSupport; |
Greg Daniel | 2a30390 | 2018-02-20 10:25:54 -0500 | [diff] [blame] | 255 | } |
| 256 | } |
| 257 | |
Greg Daniel | 9d86f1d | 2018-01-29 09:33:59 -0500 | [diff] [blame] | 258 | GrSurfaceDesc desc; |
| 259 | desc.fWidth = srcImage->width(); |
| 260 | desc.fHeight = srcImage->height(); |
Robert Phillips | fe0253f | 2018-03-16 16:47:25 -0400 | [diff] [blame] | 261 | desc.fFlags = descFlags; |
Greg Daniel | 9d86f1d | 2018-01-29 09:33:59 -0500 | [diff] [blame] | 262 | desc.fSampleCnt = sampleCnt; |
Greg Daniel | f87651e | 2018-02-21 11:36:53 -0500 | [diff] [blame] | 263 | desc.fConfig = config; |
Greg Daniel | 9d86f1d | 2018-01-29 09:33:59 -0500 | [diff] [blame] | 264 | |
| 265 | sk_sp<GrTextureProxy> proxy = this->createLazyProxy( |
Brian Salomon | 58389b9 | 2018-03-07 13:01:25 -0500 | [diff] [blame] | 266 | [desc, budgeted, srcImage, fit](GrResourceProvider* resourceProvider) { |
Greg Daniel | 9d86f1d | 2018-01-29 09:33:59 -0500 | [diff] [blame] | 267 | if (!resourceProvider) { |
Greg Daniel | 0a375db | 2018-02-01 12:21:39 -0500 | [diff] [blame] | 268 | // Nothing to clean up here. Once the proxy (and thus lambda) is deleted the ref |
| 269 | // on srcImage will be released. |
Greg Daniel | 9d86f1d | 2018-01-29 09:33:59 -0500 | [diff] [blame] | 270 | return sk_sp<GrTexture>(); |
| 271 | } |
| 272 | SkPixmap pixMap; |
| 273 | SkAssertResult(srcImage->peekPixels(&pixMap)); |
| 274 | GrMipLevel mipLevel = { pixMap.addr(), pixMap.rowBytes() }; |
| 275 | |
Brian Salomon | 58389b9 | 2018-03-07 13:01:25 -0500 | [diff] [blame] | 276 | return resourceProvider->createTexture(desc, budgeted, fit, mipLevel); |
Brian Salomon | 2a4f983 | 2018-03-03 22:43:43 -0500 | [diff] [blame] | 277 | }, |
Robert Phillips | fe0253f | 2018-03-16 16:47:25 -0400 | [diff] [blame] | 278 | desc, kTopLeft_GrSurfaceOrigin, GrMipMapped::kNo, surfaceFlags, fit, budgeted); |
Greg Daniel | 9d86f1d | 2018-01-29 09:33:59 -0500 | [diff] [blame] | 279 | |
Greg Daniel | 92cbf3f | 2018-04-12 16:50:17 -0400 | [diff] [blame] | 280 | if (!proxy) { |
| 281 | return nullptr; |
| 282 | } |
| 283 | |
Greg Daniel | 9d86f1d | 2018-01-29 09:33:59 -0500 | [diff] [blame] | 284 | if (fResourceProvider) { |
| 285 | // In order to reuse code we always create a lazy proxy. When we aren't in DDL mode however |
| 286 | // we're better off instantiating the proxy immediately here. |
| 287 | if (!proxy->priv().doLazyInstantiation(fResourceProvider)) { |
| 288 | return nullptr; |
| 289 | } |
| 290 | } |
Robert Phillips | c1b6066 | 2018-06-26 10:20:08 -0400 | [diff] [blame] | 291 | |
| 292 | SkASSERT(proxy->width() == desc.fWidth); |
| 293 | SkASSERT(proxy->height() == desc.fHeight); |
Greg Daniel | 9d86f1d | 2018-01-29 09:33:59 -0500 | [diff] [blame] | 294 | return proxy; |
| 295 | } |
| 296 | |
Greg Daniel | 3081508 | 2018-02-09 16:08:30 -0500 | [diff] [blame] | 297 | sk_sp<GrTextureProxy> GrProxyProvider::createMipMapProxy(const GrSurfaceDesc& desc, |
Brian Salomon | 2a4f983 | 2018-03-03 22:43:43 -0500 | [diff] [blame] | 298 | GrSurfaceOrigin origin, |
Greg Daniel | 3081508 | 2018-02-09 16:08:30 -0500 | [diff] [blame] | 299 | SkBudgeted budgeted) { |
Robert Phillips | 579f094 | 2018-01-08 14:53:35 -0500 | [diff] [blame] | 300 | ASSERT_SINGLE_OWNER |
| 301 | |
| 302 | if (this->isAbandoned()) { |
| 303 | return nullptr; |
| 304 | } |
| 305 | |
Robert Phillips | fe0253f | 2018-03-16 16:47:25 -0400 | [diff] [blame] | 306 | return this->createProxy(desc, origin, GrMipMapped::kYes, SkBackingFit::kExact, budgeted, |
| 307 | GrInternalSurfaceFlags::kNone); |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 308 | } |
| 309 | |
Brian Osman | 2b23c4b | 2018-06-01 12:25:08 -0400 | [diff] [blame] | 310 | sk_sp<GrTextureProxy> GrProxyProvider::createMipMapProxyFromBitmap(const SkBitmap& bitmap) { |
| 311 | if (!SkImageInfoIsValid(bitmap.info())) { |
Greg Daniel | a4ead65 | 2018-02-07 10:21:48 -0500 | [diff] [blame] | 312 | return nullptr; |
| 313 | } |
| 314 | |
| 315 | SkPixmap pixmap; |
| 316 | if (!bitmap.peekPixels(&pixmap)) { |
| 317 | return nullptr; |
| 318 | } |
| 319 | |
| 320 | ATRACE_ANDROID_FRAMEWORK("Upload MipMap Texture [%ux%u]", pixmap.width(), pixmap.height()); |
Brian Osman | 2b23c4b | 2018-06-01 12:25:08 -0400 | [diff] [blame] | 321 | sk_sp<SkMipMap> mipmaps(SkMipMap::Build(pixmap, nullptr)); |
Greg Daniel | a4ead65 | 2018-02-07 10:21:48 -0500 | [diff] [blame] | 322 | if (!mipmaps) { |
| 323 | return nullptr; |
| 324 | } |
| 325 | |
| 326 | if (mipmaps->countLevels() < 0) { |
| 327 | return nullptr; |
| 328 | } |
| 329 | |
| 330 | // In non-ddl we will always instantiate right away. Thus we never want to copy the SkBitmap |
| 331 | // even if its mutable. In ddl, if the bitmap is mutable then we must make a copy since the |
| 332 | // upload of the data to the gpu can happen at anytime and the bitmap may change by then. |
Robert Phillips | 5c4b33b | 2018-03-20 16:23:08 -0400 | [diff] [blame] | 333 | SkCopyPixelsMode copyMode = this->recordingDDL() ? kIfMutable_SkCopyPixelsMode |
| 334 | : kNever_SkCopyPixelsMode; |
Greg Daniel | a4ead65 | 2018-02-07 10:21:48 -0500 | [diff] [blame] | 335 | sk_sp<SkImage> baseLevel = SkMakeImageFromRasterBitmap(bitmap, copyMode); |
| 336 | |
| 337 | if (!baseLevel) { |
| 338 | return nullptr; |
| 339 | } |
| 340 | |
Brian Osman | 2b23c4b | 2018-06-01 12:25:08 -0400 | [diff] [blame] | 341 | GrSurfaceDesc desc = GrImageInfoToSurfaceDesc(pixmap.info()); |
Greg Daniel | a4ead65 | 2018-02-07 10:21:48 -0500 | [diff] [blame] | 342 | |
| 343 | if (0 == mipmaps->countLevels()) { |
Brian Salomon | 58389b9 | 2018-03-07 13:01:25 -0500 | [diff] [blame] | 344 | return this->createTextureProxy(baseLevel, kNone_GrSurfaceFlags, 1, SkBudgeted::kYes, |
| 345 | SkBackingFit::kExact); |
Greg Daniel | a4ead65 | 2018-02-07 10:21:48 -0500 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | sk_sp<GrTextureProxy> proxy = this->createLazyProxy( |
Brian Osman | 2b23c4b | 2018-06-01 12:25:08 -0400 | [diff] [blame] | 349 | [desc, baseLevel, mipmaps](GrResourceProvider* resourceProvider) { |
Greg Daniel | a4ead65 | 2018-02-07 10:21:48 -0500 | [diff] [blame] | 350 | if (!resourceProvider) { |
| 351 | return sk_sp<GrTexture>(); |
| 352 | } |
| 353 | |
| 354 | const int mipLevelCount = mipmaps->countLevels() + 1; |
| 355 | std::unique_ptr<GrMipLevel[]> texels(new GrMipLevel[mipLevelCount]); |
| 356 | |
| 357 | SkPixmap pixmap; |
| 358 | SkAssertResult(baseLevel->peekPixels(&pixmap)); |
| 359 | |
| 360 | // DDL TODO: Instead of copying all this info into GrMipLevels we should just plumb |
| 361 | // the use of SkMipMap down through Ganesh. |
| 362 | texels[0].fPixels = pixmap.addr(); |
| 363 | texels[0].fRowBytes = pixmap.rowBytes(); |
| 364 | |
| 365 | for (int i = 1; i < mipLevelCount; ++i) { |
| 366 | SkMipMap::Level generatedMipLevel; |
| 367 | mipmaps->getLevel(i - 1, &generatedMipLevel); |
| 368 | texels[i].fPixels = generatedMipLevel.fPixmap.addr(); |
| 369 | texels[i].fRowBytes = generatedMipLevel.fPixmap.rowBytes(); |
| 370 | SkASSERT(texels[i].fPixels); |
| 371 | } |
| 372 | |
Brian Salomon | 58389b9 | 2018-03-07 13:01:25 -0500 | [diff] [blame] | 373 | return resourceProvider->createTexture(desc, SkBudgeted::kYes, texels.get(), |
Brian Osman | 2b23c4b | 2018-06-01 12:25:08 -0400 | [diff] [blame] | 374 | mipLevelCount); |
Brian Salomon | 2a4f983 | 2018-03-03 22:43:43 -0500 | [diff] [blame] | 375 | }, |
| 376 | desc, kTopLeft_GrSurfaceOrigin, GrMipMapped::kYes, SkBackingFit::kExact, |
| 377 | SkBudgeted::kYes); |
Greg Daniel | a4ead65 | 2018-02-07 10:21:48 -0500 | [diff] [blame] | 378 | |
Greg Daniel | 92cbf3f | 2018-04-12 16:50:17 -0400 | [diff] [blame] | 379 | if (!proxy) { |
| 380 | return nullptr; |
| 381 | } |
| 382 | |
Greg Daniel | a4ead65 | 2018-02-07 10:21:48 -0500 | [diff] [blame] | 383 | if (fResourceProvider) { |
| 384 | // In order to reuse code we always create a lazy proxy. When we aren't in DDL mode however |
| 385 | // we're better off instantiating the proxy immediately here. |
| 386 | if (!proxy->priv().doLazyInstantiation(fResourceProvider)) { |
| 387 | return nullptr; |
| 388 | } |
| 389 | } |
| 390 | return proxy; |
| 391 | } |
| 392 | |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 393 | sk_sp<GrTextureProxy> GrProxyProvider::createProxy(const GrSurfaceDesc& desc, |
Brian Salomon | 2a4f983 | 2018-03-03 22:43:43 -0500 | [diff] [blame] | 394 | GrSurfaceOrigin origin, |
Greg Daniel | f6f7b67 | 2018-02-15 13:06:26 -0500 | [diff] [blame] | 395 | GrMipMapped mipMapped, |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 396 | SkBackingFit fit, |
| 397 | SkBudgeted budgeted, |
Robert Phillips | fe0253f | 2018-03-16 16:47:25 -0400 | [diff] [blame] | 398 | GrInternalSurfaceFlags surfaceFlags) { |
Greg Daniel | f6f7b67 | 2018-02-15 13:06:26 -0500 | [diff] [blame] | 399 | if (GrMipMapped::kYes == mipMapped) { |
| 400 | // SkMipMap doesn't include the base level in the level count so we have to add 1 |
| 401 | int mipCount = SkMipMap::ComputeLevelCount(desc.fWidth, desc.fHeight) + 1; |
| 402 | if (1 == mipCount) { |
| 403 | mipMapped = GrMipMapped::kNo; |
| 404 | } |
| 405 | } |
| 406 | |
| 407 | if (!this->caps()->validateSurfaceDesc(desc, mipMapped)) { |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 408 | return nullptr; |
| 409 | } |
Brian Salomon | 3a2cc2c | 2018-02-03 00:25:12 +0000 | [diff] [blame] | 410 | GrSurfaceDesc copyDesc = desc; |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 411 | if (desc.fFlags & kRenderTarget_GrSurfaceFlag) { |
| 412 | copyDesc.fSampleCnt = |
| 413 | this->caps()->getRenderTargetSampleCount(desc.fSampleCnt, desc.fConfig); |
| 414 | } |
Brian Salomon | 3a2cc2c | 2018-02-03 00:25:12 +0000 | [diff] [blame] | 415 | |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 416 | if (copyDesc.fFlags & kRenderTarget_GrSurfaceFlag) { |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 417 | // We know anything we instantiate later from this deferred path will be |
| 418 | // both texturable and renderable |
Brian Salomon | 2a4f983 | 2018-03-03 22:43:43 -0500 | [diff] [blame] | 419 | return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy( |
Robert Phillips | fe0253f | 2018-03-16 16:47:25 -0400 | [diff] [blame] | 420 | *this->caps(), copyDesc, origin, mipMapped, fit, budgeted, surfaceFlags)); |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 421 | } |
| 422 | |
Brian Salomon | 2a4f983 | 2018-03-03 22:43:43 -0500 | [diff] [blame] | 423 | return sk_sp<GrTextureProxy>( |
Robert Phillips | fe0253f | 2018-03-16 16:47:25 -0400 | [diff] [blame] | 424 | new GrTextureProxy(copyDesc, origin, mipMapped, fit, budgeted, surfaceFlags)); |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 425 | } |
| 426 | |
Brian Salomon | 7578f3e | 2018-03-07 14:39:54 -0500 | [diff] [blame] | 427 | sk_sp<GrTextureProxy> GrProxyProvider::wrapBackendTexture(const GrBackendTexture& backendTex, |
| 428 | GrSurfaceOrigin origin, |
| 429 | GrWrapOwnership ownership, |
| 430 | ReleaseProc releaseProc, |
| 431 | ReleaseContext releaseCtx) { |
Robert Phillips | f9bec20 | 2018-01-16 09:21:01 -0500 | [diff] [blame] | 432 | if (this->isAbandoned()) { |
| 433 | return nullptr; |
| 434 | } |
| 435 | |
Brian Salomon | f777897 | 2018-03-08 10:13:17 -0500 | [diff] [blame] | 436 | // This is only supported on a direct GrContext. |
| 437 | if (!fResourceProvider) { |
| 438 | return nullptr; |
| 439 | } |
| 440 | |
| 441 | sk_sp<GrTexture> tex = fResourceProvider->wrapBackendTexture(backendTex, ownership); |
| 442 | if (!tex) { |
| 443 | return nullptr; |
| 444 | } |
Robert Phillips | adbe132 | 2018-01-17 13:35:46 -0500 | [diff] [blame] | 445 | |
Greg Daniel | 6a0176b | 2018-01-30 09:28:44 -0500 | [diff] [blame] | 446 | sk_sp<GrReleaseProcHelper> releaseHelper; |
| 447 | if (releaseProc) { |
| 448 | releaseHelper.reset(new GrReleaseProcHelper(releaseProc, releaseCtx)); |
Brian Salomon | f777897 | 2018-03-08 10:13:17 -0500 | [diff] [blame] | 449 | // This gives the texture a ref on the releaseHelper |
| 450 | tex->setRelease(releaseHelper); |
Greg Daniel | 6a0176b | 2018-01-30 09:28:44 -0500 | [diff] [blame] | 451 | } |
| 452 | |
Brian Salomon | f777897 | 2018-03-08 10:13:17 -0500 | [diff] [blame] | 453 | SkASSERT(!tex->asRenderTarget()); // Strictly a GrTexture |
| 454 | // Make sure we match how we created the proxy with SkBudgeted::kNo |
| 455 | SkASSERT(SkBudgeted::kNo == tex->resourcePriv().isBudgeted()); |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 456 | |
Brian Salomon | f777897 | 2018-03-08 10:13:17 -0500 | [diff] [blame] | 457 | return sk_sp<GrTextureProxy>(new GrTextureProxy(std::move(tex), origin)); |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 458 | } |
| 459 | |
Brian Salomon | 7578f3e | 2018-03-07 14:39:54 -0500 | [diff] [blame] | 460 | sk_sp<GrTextureProxy> GrProxyProvider::wrapRenderableBackendTexture( |
Brian Salomon | 02bd295 | 2018-03-07 15:20:21 -0500 | [diff] [blame] | 461 | const GrBackendTexture& backendTex, GrSurfaceOrigin origin, int sampleCnt, |
| 462 | GrWrapOwnership ownership) { |
Robert Phillips | f9bec20 | 2018-01-16 09:21:01 -0500 | [diff] [blame] | 463 | if (this->isAbandoned()) { |
| 464 | return nullptr; |
| 465 | } |
| 466 | |
Brian Salomon | f777897 | 2018-03-08 10:13:17 -0500 | [diff] [blame] | 467 | // This is only supported on a direct GrContext. |
| 468 | if (!fResourceProvider) { |
| 469 | return nullptr; |
| 470 | } |
| 471 | |
Greg Daniel | 6abda43 | 2018-02-15 14:55:00 -0500 | [diff] [blame] | 472 | sampleCnt = this->caps()->getRenderTargetSampleCount(sampleCnt, backendTex.config()); |
Greg Daniel | f87651e | 2018-02-21 11:36:53 -0500 | [diff] [blame] | 473 | if (!sampleCnt) { |
| 474 | return nullptr; |
| 475 | } |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 476 | |
Brian Salomon | f777897 | 2018-03-08 10:13:17 -0500 | [diff] [blame] | 477 | sk_sp<GrTexture> tex = |
| 478 | fResourceProvider->wrapRenderableBackendTexture(backendTex, sampleCnt, ownership); |
| 479 | if (!tex) { |
| 480 | return nullptr; |
Greg Daniel | 2a30390 | 2018-02-20 10:25:54 -0500 | [diff] [blame] | 481 | } |
| 482 | |
Brian Salomon | f777897 | 2018-03-08 10:13:17 -0500 | [diff] [blame] | 483 | SkASSERT(tex->asRenderTarget()); // A GrTextureRenderTarget |
| 484 | // Make sure we match how we created the proxy with SkBudgeted::kNo |
| 485 | SkASSERT(SkBudgeted::kNo == tex->resourcePriv().isBudgeted()); |
Greg Daniel | 6abda43 | 2018-02-15 14:55:00 -0500 | [diff] [blame] | 486 | |
Brian Salomon | f777897 | 2018-03-08 10:13:17 -0500 | [diff] [blame] | 487 | return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(std::move(tex), origin)); |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 488 | } |
| 489 | |
Brian Salomon | 7578f3e | 2018-03-07 14:39:54 -0500 | [diff] [blame] | 490 | sk_sp<GrSurfaceProxy> GrProxyProvider::wrapBackendRenderTarget( |
| 491 | const GrBackendRenderTarget& backendRT, GrSurfaceOrigin origin) { |
Robert Phillips | f9bec20 | 2018-01-16 09:21:01 -0500 | [diff] [blame] | 492 | if (this->isAbandoned()) { |
| 493 | return nullptr; |
| 494 | } |
| 495 | |
Brian Salomon | f777897 | 2018-03-08 10:13:17 -0500 | [diff] [blame] | 496 | // This is only supported on a direct GrContext. |
| 497 | if (!fResourceProvider) { |
| 498 | return nullptr; |
Greg Daniel | 2a30390 | 2018-02-20 10:25:54 -0500 | [diff] [blame] | 499 | } |
| 500 | |
Brian Salomon | f777897 | 2018-03-08 10:13:17 -0500 | [diff] [blame] | 501 | sk_sp<GrRenderTarget> rt = fResourceProvider->wrapBackendRenderTarget(backendRT); |
| 502 | if (!rt) { |
| 503 | return nullptr; |
Greg Daniel | 2a30390 | 2018-02-20 10:25:54 -0500 | [diff] [blame] | 504 | } |
Brian Salomon | f777897 | 2018-03-08 10:13:17 -0500 | [diff] [blame] | 505 | SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable |
| 506 | SkASSERT(!rt->getUniqueKey().isValid()); |
| 507 | // Make sure we match how we created the proxy with SkBudgeted::kNo |
| 508 | SkASSERT(SkBudgeted::kNo == rt->resourcePriv().isBudgeted()); |
| 509 | |
| 510 | return sk_sp<GrRenderTargetProxy>(new GrRenderTargetProxy(std::move(rt), origin)); |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 511 | } |
| 512 | |
Brian Salomon | 7578f3e | 2018-03-07 14:39:54 -0500 | [diff] [blame] | 513 | sk_sp<GrSurfaceProxy> GrProxyProvider::wrapBackendTextureAsRenderTarget( |
| 514 | const GrBackendTexture& backendTex, GrSurfaceOrigin origin, int sampleCnt) { |
Robert Phillips | f9bec20 | 2018-01-16 09:21:01 -0500 | [diff] [blame] | 515 | if (this->isAbandoned()) { |
| 516 | return nullptr; |
| 517 | } |
| 518 | |
Brian Salomon | f777897 | 2018-03-08 10:13:17 -0500 | [diff] [blame] | 519 | // This is only supported on a direct GrContext. |
| 520 | if (!fResourceProvider) { |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 521 | return nullptr; |
| 522 | } |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 523 | |
Brian Salomon | f777897 | 2018-03-08 10:13:17 -0500 | [diff] [blame] | 524 | sk_sp<GrRenderTarget> rt = |
| 525 | fResourceProvider->wrapBackendTextureAsRenderTarget(backendTex, sampleCnt); |
| 526 | if (!rt) { |
| 527 | return nullptr; |
Greg Daniel | f87651e | 2018-02-21 11:36:53 -0500 | [diff] [blame] | 528 | } |
Brian Salomon | f777897 | 2018-03-08 10:13:17 -0500 | [diff] [blame] | 529 | SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable |
| 530 | SkASSERT(!rt->getUniqueKey().isValid()); |
| 531 | // Make sure we match how we created the proxy with SkBudgeted::kNo |
| 532 | SkASSERT(SkBudgeted::kNo == rt->resourcePriv().isBudgeted()); |
Greg Daniel | f87651e | 2018-02-21 11:36:53 -0500 | [diff] [blame] | 533 | |
Brian Salomon | f777897 | 2018-03-08 10:13:17 -0500 | [diff] [blame] | 534 | return sk_sp<GrSurfaceProxy>(new GrRenderTargetProxy(std::move(rt), origin)); |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 535 | } |
| 536 | |
Robert Phillips | 777707b | 2018-01-17 11:40:14 -0500 | [diff] [blame] | 537 | sk_sp<GrTextureProxy> GrProxyProvider::createLazyProxy(LazyInstantiateCallback&& callback, |
| 538 | const GrSurfaceDesc& desc, |
Brian Salomon | 2a4f983 | 2018-03-03 22:43:43 -0500 | [diff] [blame] | 539 | GrSurfaceOrigin origin, |
| 540 | GrMipMapped mipMapped, SkBackingFit fit, |
| 541 | SkBudgeted budgeted) { |
| 542 | return this->createLazyProxy(std::move(callback), desc, origin, mipMapped, |
Robert Phillips | fe0253f | 2018-03-16 16:47:25 -0400 | [diff] [blame] | 543 | GrInternalSurfaceFlags::kNone, fit, budgeted); |
Greg Daniel | 2a30390 | 2018-02-20 10:25:54 -0500 | [diff] [blame] | 544 | } |
| 545 | |
| 546 | sk_sp<GrTextureProxy> GrProxyProvider::createLazyProxy(LazyInstantiateCallback&& callback, |
| 547 | const GrSurfaceDesc& desc, |
Brian Salomon | 2a4f983 | 2018-03-03 22:43:43 -0500 | [diff] [blame] | 548 | GrSurfaceOrigin origin, |
Greg Daniel | 2a30390 | 2018-02-20 10:25:54 -0500 | [diff] [blame] | 549 | GrMipMapped mipMapped, |
Robert Phillips | fe0253f | 2018-03-16 16:47:25 -0400 | [diff] [blame] | 550 | GrInternalSurfaceFlags surfaceFlags, |
Greg Daniel | 2a30390 | 2018-02-20 10:25:54 -0500 | [diff] [blame] | 551 | SkBackingFit fit, SkBudgeted budgeted) { |
Greg Daniel | a8d9211 | 2018-03-09 12:05:04 -0500 | [diff] [blame] | 552 | // For non-ddl draws always make lazy proxy's single use. |
| 553 | LazyInstantiationType lazyType = fResourceProvider ? LazyInstantiationType::kSingleUse |
| 554 | : LazyInstantiationType::kMultipleUse; |
Robert Phillips | fe0253f | 2018-03-16 16:47:25 -0400 | [diff] [blame] | 555 | return this->createLazyProxy(std::move(callback), desc, origin, mipMapped, surfaceFlags, |
Greg Daniel | a8d9211 | 2018-03-09 12:05:04 -0500 | [diff] [blame] | 556 | fit, budgeted, lazyType); |
| 557 | } |
| 558 | |
| 559 | sk_sp<GrTextureProxy> GrProxyProvider::createLazyProxy(LazyInstantiateCallback&& callback, |
| 560 | const GrSurfaceDesc& desc, |
| 561 | GrSurfaceOrigin origin, |
| 562 | GrMipMapped mipMapped, |
Robert Phillips | fe0253f | 2018-03-16 16:47:25 -0400 | [diff] [blame] | 563 | GrInternalSurfaceFlags surfaceFlags, |
Greg Daniel | a8d9211 | 2018-03-09 12:05:04 -0500 | [diff] [blame] | 564 | SkBackingFit fit, SkBudgeted budgeted, |
| 565 | LazyInstantiationType lazyType) { |
Robert Phillips | 777707b | 2018-01-17 11:40:14 -0500 | [diff] [blame] | 566 | SkASSERT((desc.fWidth <= 0 && desc.fHeight <= 0) || |
| 567 | (desc.fWidth > 0 && desc.fHeight > 0)); |
Greg Daniel | 92cbf3f | 2018-04-12 16:50:17 -0400 | [diff] [blame] | 568 | |
| 569 | if (desc.fWidth > fCaps->maxTextureSize() || desc.fHeight > fCaps->maxTextureSize()) { |
| 570 | return nullptr; |
| 571 | } |
| 572 | |
Greg Daniel | 457469c | 2018-02-08 15:05:44 -0500 | [diff] [blame] | 573 | |
Greg Daniel | 2a30390 | 2018-02-20 10:25:54 -0500 | [diff] [blame] | 574 | #ifdef SK_DEBUG |
| 575 | if (SkToBool(kRenderTarget_GrSurfaceFlag & desc.fFlags)) { |
Robert Phillips | fe0253f | 2018-03-16 16:47:25 -0400 | [diff] [blame] | 576 | if (SkToBool(surfaceFlags & GrInternalSurfaceFlags::kMixedSampled)) { |
Greg Daniel | 2a30390 | 2018-02-20 10:25:54 -0500 | [diff] [blame] | 577 | SkASSERT(fCaps->usesMixedSamples() && desc.fSampleCnt > 1); |
| 578 | } |
Robert Phillips | fe0253f | 2018-03-16 16:47:25 -0400 | [diff] [blame] | 579 | if (SkToBool(surfaceFlags & GrInternalSurfaceFlags::kWindowRectsSupport)) { |
Greg Daniel | 2a30390 | 2018-02-20 10:25:54 -0500 | [diff] [blame] | 580 | SkASSERT(fCaps->maxWindowRectangles() > 0); |
| 581 | } |
| 582 | } |
| 583 | #endif |
| 584 | |
Brian Salomon | 2a4f983 | 2018-03-03 22:43:43 -0500 | [diff] [blame] | 585 | return sk_sp<GrTextureProxy>( |
| 586 | SkToBool(kRenderTarget_GrSurfaceFlag & desc.fFlags) |
| 587 | ? new GrTextureRenderTargetProxy(std::move(callback), lazyType, desc, origin, |
Robert Phillips | fe0253f | 2018-03-16 16:47:25 -0400 | [diff] [blame] | 588 | mipMapped, fit, budgeted, surfaceFlags) |
Brian Salomon | 2a4f983 | 2018-03-03 22:43:43 -0500 | [diff] [blame] | 589 | : new GrTextureProxy(std::move(callback), lazyType, desc, origin, mipMapped, |
Robert Phillips | fe0253f | 2018-03-16 16:47:25 -0400 | [diff] [blame] | 590 | fit, budgeted, surfaceFlags)); |
Robert Phillips | 777707b | 2018-01-17 11:40:14 -0500 | [diff] [blame] | 591 | } |
| 592 | |
Robert Phillips | e8fabb2 | 2018-02-04 14:33:21 -0500 | [diff] [blame] | 593 | sk_sp<GrRenderTargetProxy> GrProxyProvider::createLazyRenderTargetProxy( |
Brian Salomon | 2a4f983 | 2018-03-03 22:43:43 -0500 | [diff] [blame] | 594 | LazyInstantiateCallback&& callback, const GrSurfaceDesc& desc, GrSurfaceOrigin origin, |
Robert Phillips | fe0253f | 2018-03-16 16:47:25 -0400 | [diff] [blame] | 595 | GrInternalSurfaceFlags surfaceFlags, Textureable textureable, GrMipMapped mipMapped, |
Brian Salomon | 2a4f983 | 2018-03-03 22:43:43 -0500 | [diff] [blame] | 596 | SkBackingFit fit, SkBudgeted budgeted) { |
Robert Phillips | e8fabb2 | 2018-02-04 14:33:21 -0500 | [diff] [blame] | 597 | SkASSERT((desc.fWidth <= 0 && desc.fHeight <= 0) || |
| 598 | (desc.fWidth > 0 && desc.fHeight > 0)); |
Greg Daniel | 92cbf3f | 2018-04-12 16:50:17 -0400 | [diff] [blame] | 599 | |
| 600 | if (desc.fWidth > fCaps->maxRenderTargetSize() || desc.fHeight > fCaps->maxRenderTargetSize()) { |
| 601 | return nullptr; |
| 602 | } |
| 603 | |
Greg Daniel | 2a30390 | 2018-02-20 10:25:54 -0500 | [diff] [blame] | 604 | SkASSERT(SkToBool(kRenderTarget_GrSurfaceFlag & desc.fFlags)); |
Greg Daniel | 457469c | 2018-02-08 15:05:44 -0500 | [diff] [blame] | 605 | |
Greg Daniel | 2a30390 | 2018-02-20 10:25:54 -0500 | [diff] [blame] | 606 | #ifdef SK_DEBUG |
Robert Phillips | fe0253f | 2018-03-16 16:47:25 -0400 | [diff] [blame] | 607 | if (SkToBool(surfaceFlags & GrInternalSurfaceFlags::kMixedSampled)) { |
Greg Daniel | 2a30390 | 2018-02-20 10:25:54 -0500 | [diff] [blame] | 608 | SkASSERT(fCaps->usesMixedSamples() && desc.fSampleCnt > 1); |
| 609 | } |
Robert Phillips | fe0253f | 2018-03-16 16:47:25 -0400 | [diff] [blame] | 610 | if (SkToBool(surfaceFlags & GrInternalSurfaceFlags::kWindowRectsSupport)) { |
Greg Daniel | 2a30390 | 2018-02-20 10:25:54 -0500 | [diff] [blame] | 611 | SkASSERT(fCaps->maxWindowRectangles() > 0); |
| 612 | } |
| 613 | #endif |
| 614 | |
Greg Daniel | 457469c | 2018-02-08 15:05:44 -0500 | [diff] [blame] | 615 | using LazyInstantiationType = GrSurfaceProxy::LazyInstantiationType; |
| 616 | // For non-ddl draws always make lazy proxy's single use. |
| 617 | LazyInstantiationType lazyType = fResourceProvider ? LazyInstantiationType::kSingleUse |
| 618 | : LazyInstantiationType::kMultipleUse; |
| 619 | |
Robert Phillips | e8fabb2 | 2018-02-04 14:33:21 -0500 | [diff] [blame] | 620 | if (Textureable::kYes == textureable) { |
Brian Salomon | 2a4f983 | 2018-03-03 22:43:43 -0500 | [diff] [blame] | 621 | return sk_sp<GrRenderTargetProxy>( |
| 622 | new GrTextureRenderTargetProxy(std::move(callback), lazyType, desc, origin, |
Robert Phillips | fe0253f | 2018-03-16 16:47:25 -0400 | [diff] [blame] | 623 | mipMapped, fit, budgeted, surfaceFlags)); |
Robert Phillips | e8fabb2 | 2018-02-04 14:33:21 -0500 | [diff] [blame] | 624 | } |
| 625 | |
Brian Salomon | 2a4f983 | 2018-03-03 22:43:43 -0500 | [diff] [blame] | 626 | return sk_sp<GrRenderTargetProxy>(new GrRenderTargetProxy( |
Robert Phillips | fe0253f | 2018-03-16 16:47:25 -0400 | [diff] [blame] | 627 | std::move(callback), lazyType, desc, origin, fit, budgeted, surfaceFlags)); |
Robert Phillips | e8fabb2 | 2018-02-04 14:33:21 -0500 | [diff] [blame] | 628 | } |
| 629 | |
Chris Dalton | 4c458b1 | 2018-06-16 17:22:59 -0600 | [diff] [blame] | 630 | sk_sp<GrTextureProxy> GrProxyProvider::MakeFullyLazyProxy(LazyInstantiateCallback&& callback, |
| 631 | Renderable renderable, |
| 632 | GrSurfaceOrigin origin, |
| 633 | GrPixelConfig config, |
| 634 | const GrCaps& caps) { |
Robert Phillips | 777707b | 2018-01-17 11:40:14 -0500 | [diff] [blame] | 635 | GrSurfaceDesc desc; |
Greg Daniel | e320486 | 2018-04-16 11:24:10 -0400 | [diff] [blame] | 636 | GrInternalSurfaceFlags surfaceFlags = GrInternalSurfaceFlags::kNoPendingIO; |
Robert Phillips | 777707b | 2018-01-17 11:40:14 -0500 | [diff] [blame] | 637 | if (Renderable::kYes == renderable) { |
| 638 | desc.fFlags = kRenderTarget_GrSurfaceFlag; |
Chris Dalton | 4c458b1 | 2018-06-16 17:22:59 -0600 | [diff] [blame] | 639 | if (caps.maxWindowRectangles() > 0) { |
Robert Phillips | fe0253f | 2018-03-16 16:47:25 -0400 | [diff] [blame] | 640 | surfaceFlags |= GrInternalSurfaceFlags::kWindowRectsSupport; |
Greg Daniel | 2a30390 | 2018-02-20 10:25:54 -0500 | [diff] [blame] | 641 | } |
Robert Phillips | 777707b | 2018-01-17 11:40:14 -0500 | [diff] [blame] | 642 | } |
Robert Phillips | 777707b | 2018-01-17 11:40:14 -0500 | [diff] [blame] | 643 | desc.fWidth = -1; |
| 644 | desc.fHeight = -1; |
| 645 | desc.fConfig = config; |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 646 | desc.fSampleCnt = 1; |
Robert Phillips | 777707b | 2018-01-17 11:40:14 -0500 | [diff] [blame] | 647 | |
Chris Dalton | 4c458b1 | 2018-06-16 17:22:59 -0600 | [diff] [blame] | 648 | return sk_sp<GrTextureProxy>( |
| 649 | (Renderable::kYes == renderable) |
| 650 | ? new GrTextureRenderTargetProxy(std::move(callback), |
| 651 | LazyInstantiationType::kSingleUse, desc, |
| 652 | origin, GrMipMapped::kNo, |
| 653 | SkBackingFit::kApprox, SkBudgeted::kYes, |
| 654 | surfaceFlags) |
| 655 | : new GrTextureProxy(std::move(callback), LazyInstantiationType::kSingleUse, |
| 656 | desc, origin, GrMipMapped::kNo, SkBackingFit::kApprox, |
| 657 | SkBudgeted::kYes, surfaceFlags)); |
Robert Phillips | 777707b | 2018-01-17 11:40:14 -0500 | [diff] [blame] | 658 | } |
| 659 | |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 660 | bool GrProxyProvider::IsFunctionallyExact(GrSurfaceProxy* proxy) { |
Robert Phillips | db3b979 | 2018-06-12 15:18:00 -0400 | [diff] [blame] | 661 | const bool isInstantiated = proxy->priv().isInstantiated(); |
| 662 | // A proxy is functionally exact if: |
| 663 | // it is exact (obvs) |
| 664 | // when it is instantiated it will be exact (i.e., power of two dimensions) |
| 665 | // it is already instantiated and the proxy covers the entire backing surface |
| 666 | return proxy->priv().isExact() || |
| 667 | (!isInstantiated && SkIsPow2(proxy->width()) && SkIsPow2(proxy->height())) || |
| 668 | (isInstantiated && proxy->worstCaseWidth() == proxy->width() && |
| 669 | proxy->worstCaseHeight() == proxy->height()); |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 670 | } |
| 671 | |
| 672 | void GrProxyProvider::processInvalidProxyUniqueKey(const GrUniqueKey& key) { |
| 673 | // Note: this method is called for the whole variety of GrGpuResources so often 'key' |
| 674 | // will not be in 'fUniquelyKeyedProxies'. |
| 675 | GrTextureProxy* proxy = fUniquelyKeyedProxies.find(key); |
| 676 | if (proxy) { |
| 677 | this->processInvalidProxyUniqueKey(key, proxy, false); |
| 678 | } |
| 679 | } |
| 680 | |
| 681 | void GrProxyProvider::processInvalidProxyUniqueKey(const GrUniqueKey& key, GrTextureProxy* proxy, |
| 682 | bool invalidateSurface) { |
| 683 | SkASSERT(proxy); |
| 684 | SkASSERT(proxy->getUniqueKey().isValid()); |
| 685 | SkASSERT(proxy->getUniqueKey() == key); |
| 686 | |
| 687 | fUniquelyKeyedProxies.remove(key); |
| 688 | proxy->cacheAccess().clearUniqueKey(); |
| 689 | |
| 690 | if (invalidateSurface && proxy->priv().isInstantiated()) { |
| 691 | GrSurface* surface = proxy->priv().peekSurface(); |
| 692 | if (surface) { |
| 693 | surface->resourcePriv().removeUniqueKey(); |
| 694 | } |
| 695 | } |
| 696 | } |
| 697 | |
| 698 | void GrProxyProvider::removeAllUniqueKeys() { |
| 699 | UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies); |
| 700 | for (UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies); !iter.done(); ++iter) { |
| 701 | GrTextureProxy& tmp = *iter; |
| 702 | |
| 703 | this->processInvalidProxyUniqueKey(tmp.getUniqueKey(), &tmp, false); |
| 704 | } |
| 705 | SkASSERT(!fUniquelyKeyedProxies.count()); |
| 706 | } |