GrProxyProvider.cpp 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974
  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. #include "src/gpu/GrProxyProvider.h"
  8. #include "include/core/SkBitmap.h"
  9. #include "include/core/SkImage.h"
  10. #include "include/gpu/GrContext.h"
  11. #include "include/gpu/GrRenderTarget.h"
  12. #include "include/gpu/GrTexture.h"
  13. #include "include/private/GrImageContext.h"
  14. #include "include/private/GrResourceKey.h"
  15. #include "include/private/GrSingleOwner.h"
  16. #include "include/private/SkImageInfoPriv.h"
  17. #include "src/core/SkAutoPixmapStorage.h"
  18. #include "src/core/SkImagePriv.h"
  19. #include "src/core/SkMipMap.h"
  20. #include "src/core/SkTraceEvent.h"
  21. #include "src/gpu/GrCaps.h"
  22. #include "src/gpu/GrContextPriv.h"
  23. #include "src/gpu/GrImageContextPriv.h"
  24. #include "src/gpu/GrResourceProvider.h"
  25. #include "src/gpu/GrSurfaceProxy.h"
  26. #include "src/gpu/GrSurfaceProxyPriv.h"
  27. #include "src/gpu/GrTextureProxyCacheAccess.h"
  28. #include "src/gpu/GrTextureRenderTargetProxy.h"
  29. #include "src/gpu/SkGr.h"
  30. #include "src/image/SkImage_Base.h"
  31. #define ASSERT_SINGLE_OWNER \
  32. SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(fImageContext->priv().singleOwner());)
  33. GrProxyProvider::GrProxyProvider(GrImageContext* imageContext) : fImageContext(imageContext) {}
  34. GrProxyProvider::~GrProxyProvider() {
  35. if (this->renderingDirectly()) {
  36. // In DDL-mode a proxy provider can still have extant uniquely keyed proxies (since
  37. // they need their unique keys to, potentially, find a cached resource when the
  38. // DDL is played) but, in non-DDL-mode they should all have been cleaned up by this point.
  39. SkASSERT(!fUniquelyKeyedProxies.count());
  40. }
  41. }
  42. bool GrProxyProvider::assignUniqueKeyToProxy(const GrUniqueKey& key, GrTextureProxy* proxy) {
  43. ASSERT_SINGLE_OWNER
  44. SkASSERT(key.isValid());
  45. if (this->isAbandoned() || !proxy) {
  46. return false;
  47. }
  48. #ifdef SK_DEBUG
  49. {
  50. GrContext* direct = fImageContext->priv().asDirectContext();
  51. if (direct) {
  52. GrResourceCache* resourceCache = direct->priv().getResourceCache();
  53. // If there is already a GrResource with this key then the caller has violated the
  54. // normal usage pattern of uniquely keyed resources (e.g., they have created one w/o
  55. // first seeing if it already existed in the cache).
  56. SkASSERT(!resourceCache->findAndRefUniqueResource(key));
  57. }
  58. }
  59. #endif
  60. SkASSERT(!fUniquelyKeyedProxies.find(key)); // multiple proxies can't get the same key
  61. proxy->cacheAccess().setUniqueKey(this, key);
  62. SkASSERT(proxy->getUniqueKey() == key);
  63. fUniquelyKeyedProxies.add(proxy);
  64. return true;
  65. }
  66. void GrProxyProvider::adoptUniqueKeyFromSurface(GrTextureProxy* proxy, const GrSurface* surf) {
  67. SkASSERT(surf->getUniqueKey().isValid());
  68. proxy->cacheAccess().setUniqueKey(this, surf->getUniqueKey());
  69. SkASSERT(proxy->getUniqueKey() == surf->getUniqueKey());
  70. // multiple proxies can't get the same key
  71. SkASSERT(!fUniquelyKeyedProxies.find(surf->getUniqueKey()));
  72. fUniquelyKeyedProxies.add(proxy);
  73. }
  74. void GrProxyProvider::removeUniqueKeyFromProxy(GrTextureProxy* proxy) {
  75. ASSERT_SINGLE_OWNER
  76. SkASSERT(proxy);
  77. SkASSERT(proxy->getUniqueKey().isValid());
  78. if (this->isAbandoned()) {
  79. return;
  80. }
  81. this->processInvalidUniqueKey(proxy->getUniqueKey(), proxy, InvalidateGPUResource::kYes);
  82. }
  83. sk_sp<GrTextureProxy> GrProxyProvider::findProxyByUniqueKey(const GrUniqueKey& key,
  84. GrSurfaceOrigin origin) {
  85. ASSERT_SINGLE_OWNER
  86. if (this->isAbandoned()) {
  87. return nullptr;
  88. }
  89. GrTextureProxy* proxy = fUniquelyKeyedProxies.find(key);
  90. if (proxy) {
  91. SkASSERT(proxy->getProxyRefCnt() >= 1);
  92. SkASSERT(proxy->origin() == origin);
  93. return sk_ref_sp(proxy);
  94. }
  95. return nullptr;
  96. }
  97. ///////////////////////////////////////////////////////////////////////////////
  98. #if GR_TEST_UTILS
  99. sk_sp<GrTextureProxy> GrProxyProvider::testingOnly_createInstantiatedProxy(
  100. const GrSurfaceDesc& desc, GrRenderable renderable, int renderTargetSampleCnt,
  101. GrSurfaceOrigin origin, SkBackingFit fit, SkBudgeted budgeted, GrProtected isProtected) {
  102. GrContext* direct = fImageContext->priv().asDirectContext();
  103. if (!direct) {
  104. return nullptr;
  105. }
  106. GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
  107. sk_sp<GrTexture> tex;
  108. if (SkBackingFit::kApprox == fit) {
  109. tex = resourceProvider->createApproxTexture(desc, renderable, renderTargetSampleCnt,
  110. isProtected,
  111. GrResourceProvider::Flags::kNoPendingIO);
  112. } else {
  113. tex = resourceProvider->createTexture(desc, renderable, renderTargetSampleCnt, budgeted,
  114. isProtected, GrResourceProvider::Flags::kNoPendingIO);
  115. }
  116. if (!tex) {
  117. return nullptr;
  118. }
  119. return this->createWrapped(std::move(tex), origin);
  120. }
  121. sk_sp<GrTextureProxy> GrProxyProvider::testingOnly_createWrapped(sk_sp<GrTexture> tex,
  122. GrSurfaceOrigin origin) {
  123. return this->createWrapped(std::move(tex), origin);
  124. }
  125. #endif
  126. sk_sp<GrTextureProxy> GrProxyProvider::createWrapped(sk_sp<GrTexture> tex, GrSurfaceOrigin origin) {
  127. #ifdef SK_DEBUG
  128. if (tex->getUniqueKey().isValid()) {
  129. SkASSERT(!this->findProxyByUniqueKey(tex->getUniqueKey(), origin));
  130. }
  131. #endif
  132. GrColorType colorType = GrPixelConfigToColorType(tex->config());
  133. GrSwizzle texSwizzle = this->caps()->getTextureSwizzle(tex->backendFormat(), colorType);
  134. if (tex->asRenderTarget()) {
  135. GrSwizzle outSwizzle = this->caps()->getOutputSwizzle(tex->backendFormat(), colorType);
  136. return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(std::move(tex), origin,
  137. texSwizzle, outSwizzle));
  138. } else {
  139. return sk_sp<GrTextureProxy>(new GrTextureProxy(std::move(tex), origin, texSwizzle));
  140. }
  141. }
  142. sk_sp<GrTextureProxy> GrProxyProvider::findOrCreateProxyByUniqueKey(const GrUniqueKey& key,
  143. GrSurfaceOrigin origin) {
  144. ASSERT_SINGLE_OWNER
  145. if (this->isAbandoned()) {
  146. return nullptr;
  147. }
  148. sk_sp<GrTextureProxy> result = this->findProxyByUniqueKey(key, origin);
  149. if (result) {
  150. return result;
  151. }
  152. GrContext* direct = fImageContext->priv().asDirectContext();
  153. if (!direct) {
  154. return nullptr;
  155. }
  156. GrResourceCache* resourceCache = direct->priv().getResourceCache();
  157. GrGpuResource* resource = resourceCache->findAndRefUniqueResource(key);
  158. if (!resource) {
  159. return nullptr;
  160. }
  161. sk_sp<GrTexture> texture(static_cast<GrSurface*>(resource)->asTexture());
  162. SkASSERT(texture);
  163. result = this->createWrapped(std::move(texture), origin);
  164. SkASSERT(result->getUniqueKey() == key);
  165. // createWrapped should've added this for us
  166. SkASSERT(fUniquelyKeyedProxies.find(key));
  167. return result;
  168. }
  169. sk_sp<GrTextureProxy> GrProxyProvider::createTextureProxy(sk_sp<SkImage> srcImage,
  170. GrRenderable renderable,
  171. int sampleCnt,
  172. SkBudgeted budgeted,
  173. SkBackingFit fit,
  174. GrInternalSurfaceFlags surfaceFlags) {
  175. ASSERT_SINGLE_OWNER
  176. SkASSERT(srcImage);
  177. if (this->isAbandoned()) {
  178. return nullptr;
  179. }
  180. const SkImageInfo& info = srcImage->imageInfo();
  181. SkColorType ct = info.colorType();
  182. GrColorType grCT = SkColorTypeToGrColorType(ct);
  183. GrBackendFormat format = this->caps()->getBackendFormatFromColorType(grCT);
  184. if (!format.isValid() || !this->caps()->isFormatTexturable(grCT, format)) {
  185. SkBitmap copy8888;
  186. if (!copy8888.tryAllocPixels(info.makeColorType(kRGBA_8888_SkColorType)) ||
  187. !srcImage->readPixels(copy8888.pixmap(), 0, 0)) {
  188. return nullptr;
  189. }
  190. copy8888.setImmutable();
  191. srcImage = SkMakeImageFromRasterBitmap(copy8888, kNever_SkCopyPixelsMode);
  192. ct = kRGBA_8888_SkColorType;
  193. grCT = GrColorType::kRGBA_8888;
  194. format = this->caps()->getBackendFormatFromColorType(grCT);
  195. if (!format.isValid()) {
  196. return nullptr;
  197. }
  198. }
  199. if (renderable == GrRenderable::kYes) {
  200. sampleCnt = this->caps()->getRenderTargetSampleCount(sampleCnt, grCT, format);
  201. if (!sampleCnt) {
  202. return nullptr;
  203. }
  204. }
  205. GrPixelConfig config = SkColorType2GrPixelConfig(ct);
  206. if (kUnknown_GrPixelConfig == config) {
  207. return nullptr;
  208. }
  209. GrSurfaceDesc desc;
  210. desc.fWidth = srcImage->width();
  211. desc.fHeight = srcImage->height();
  212. desc.fConfig = config;
  213. sk_sp<GrTextureProxy> proxy = this->createLazyProxy(
  214. [desc, renderable, sampleCnt, budgeted, srcImage,
  215. fit](GrResourceProvider* resourceProvider) {
  216. SkPixmap pixMap;
  217. SkAssertResult(srcImage->peekPixels(&pixMap));
  218. GrMipLevel mipLevel = { pixMap.addr(), pixMap.rowBytes() };
  219. return LazyInstantiationResult(resourceProvider->createTexture(
  220. desc, renderable, sampleCnt, budgeted, fit, GrProtected::kNo, mipLevel,
  221. GrResourceProvider::Flags::kNoPendingIO));
  222. },
  223. format, desc, renderable, sampleCnt, kTopLeft_GrSurfaceOrigin, GrMipMapped::kNo,
  224. surfaceFlags, fit, budgeted, GrProtected::kNo);
  225. if (!proxy) {
  226. return nullptr;
  227. }
  228. GrContext* direct = fImageContext->priv().asDirectContext();
  229. if (direct) {
  230. GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
  231. // In order to reuse code we always create a lazy proxy. When we aren't in DDL mode however
  232. // we're better off instantiating the proxy immediately here.
  233. if (!proxy->priv().doLazyInstantiation(resourceProvider)) {
  234. return nullptr;
  235. }
  236. }
  237. SkASSERT(proxy->width() == desc.fWidth);
  238. SkASSERT(proxy->height() == desc.fHeight);
  239. return proxy;
  240. }
  241. sk_sp<GrTextureProxy> GrProxyProvider::createMipMapProxy(const GrBackendFormat& format,
  242. const GrSurfaceDesc& desc,
  243. GrRenderable renderable,
  244. int renderTargetSampleCnt,
  245. GrSurfaceOrigin origin,
  246. SkBudgeted budgeted,
  247. GrProtected isProtected) {
  248. ASSERT_SINGLE_OWNER
  249. if (this->isAbandoned()) {
  250. return nullptr;
  251. }
  252. return this->createProxy(format, desc, renderable, renderTargetSampleCnt, origin,
  253. GrMipMapped::kYes, SkBackingFit::kExact, budgeted, isProtected,
  254. GrInternalSurfaceFlags::kNone);
  255. }
  256. sk_sp<GrTextureProxy> GrProxyProvider::createProxyFromBitmap(const SkBitmap& bitmap,
  257. GrMipMapped mipMapped) {
  258. ASSERT_SINGLE_OWNER
  259. SkASSERT(GrMipMapped::kNo == mipMapped || this->caps()->mipMapSupport());
  260. if (this->isAbandoned()) {
  261. return nullptr;
  262. }
  263. if (!SkImageInfoIsValid(bitmap.info())) {
  264. return nullptr;
  265. }
  266. ATRACE_ANDROID_FRAMEWORK("Upload %sTexture [%ux%u]",
  267. GrMipMapped::kYes == mipMapped ? "MipMap " : "",
  268. bitmap.width(), bitmap.height());
  269. // In non-ddl we will always instantiate right away. Thus we never want to copy the SkBitmap
  270. // even if its mutable. In ddl, if the bitmap is mutable then we must make a copy since the
  271. // upload of the data to the gpu can happen at anytime and the bitmap may change by then.
  272. SkCopyPixelsMode copyMode = this->renderingDirectly() ? kNever_SkCopyPixelsMode
  273. : kIfMutable_SkCopyPixelsMode;
  274. sk_sp<SkImage> baseLevel = SkMakeImageFromRasterBitmap(bitmap, copyMode);
  275. if (!baseLevel) {
  276. return nullptr;
  277. }
  278. // If mips weren't requested (or this was too small to have any), then take the fast path
  279. if (GrMipMapped::kNo == mipMapped ||
  280. 0 == SkMipMap::ComputeLevelCount(baseLevel->width(), baseLevel->height())) {
  281. return this->createTextureProxy(std::move(baseLevel), GrRenderable::kNo, 1,
  282. SkBudgeted::kYes, SkBackingFit::kExact);
  283. }
  284. GrColorType grColorType = SkColorTypeToGrColorType(bitmap.info().colorType());
  285. GrBackendFormat format = this->caps()->getBackendFormatFromColorType(grColorType);
  286. if (!format.isValid()) {
  287. return nullptr;
  288. }
  289. GrSurfaceDesc desc = GrImageInfoToSurfaceDesc(bitmap.info());
  290. if (!this->caps()->isFormatTexturable(grColorType, format)) {
  291. SkBitmap copy8888;
  292. if (!copy8888.tryAllocPixels(bitmap.info().makeColorType(kRGBA_8888_SkColorType)) ||
  293. !bitmap.readPixels(copy8888.pixmap())) {
  294. return nullptr;
  295. }
  296. copy8888.setImmutable();
  297. baseLevel = SkMakeImageFromRasterBitmap(copy8888, kNever_SkCopyPixelsMode);
  298. desc.fConfig = kRGBA_8888_GrPixelConfig;
  299. grColorType = GrColorType::kRGBA_8888;
  300. format = this->caps()->getBackendFormatFromColorType(grColorType);
  301. if (!format.isValid()) {
  302. return nullptr;
  303. }
  304. }
  305. SkPixmap pixmap;
  306. SkAssertResult(baseLevel->peekPixels(&pixmap));
  307. sk_sp<SkMipMap> mipmaps(SkMipMap::Build(pixmap, nullptr));
  308. if (!mipmaps) {
  309. return nullptr;
  310. }
  311. sk_sp<GrTextureProxy> proxy = this->createLazyProxy(
  312. [desc, baseLevel, mipmaps](GrResourceProvider* resourceProvider) {
  313. const int mipLevelCount = mipmaps->countLevels() + 1;
  314. std::unique_ptr<GrMipLevel[]> texels(new GrMipLevel[mipLevelCount]);
  315. SkPixmap pixmap;
  316. SkAssertResult(baseLevel->peekPixels(&pixmap));
  317. // DDL TODO: Instead of copying all this info into GrMipLevels we should just plumb
  318. // the use of SkMipMap down through Ganesh.
  319. texels[0].fPixels = pixmap.addr();
  320. texels[0].fRowBytes = pixmap.rowBytes();
  321. for (int i = 1; i < mipLevelCount; ++i) {
  322. SkMipMap::Level generatedMipLevel;
  323. mipmaps->getLevel(i - 1, &generatedMipLevel);
  324. texels[i].fPixels = generatedMipLevel.fPixmap.addr();
  325. texels[i].fRowBytes = generatedMipLevel.fPixmap.rowBytes();
  326. SkASSERT(texels[i].fPixels);
  327. }
  328. return LazyInstantiationResult(resourceProvider->createTexture(
  329. desc, GrRenderable::kNo, 1, SkBudgeted::kYes, GrProtected::kNo,
  330. texels.get(), mipLevelCount));
  331. },
  332. format, desc, GrRenderable::kNo, 1, kTopLeft_GrSurfaceOrigin, GrMipMapped::kYes,
  333. SkBackingFit::kExact, SkBudgeted::kYes, GrProtected::kNo);
  334. if (!proxy) {
  335. return nullptr;
  336. }
  337. GrContext* direct = fImageContext->priv().asDirectContext();
  338. if (direct) {
  339. GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
  340. // In order to reuse code we always create a lazy proxy. When we aren't in DDL mode however
  341. // we're better off instantiating the proxy immediately here.
  342. if (!proxy->priv().doLazyInstantiation(resourceProvider)) {
  343. return nullptr;
  344. }
  345. }
  346. return proxy;
  347. }
  348. #ifdef SK_DEBUG
  349. static bool validate_backend_format_and_config(const GrCaps* caps,
  350. const GrBackendFormat& format,
  351. GrPixelConfig config) {
  352. if (kUnknown_GrPixelConfig == config) {
  353. return false;
  354. }
  355. if (GrPixelConfigIsCompressed(config)) {
  356. // We have no way to verify these at the moment.
  357. return true;
  358. }
  359. GrColorType grCT = GrPixelConfigToColorType(config);
  360. return caps->areColorTypeAndFormatCompatible(grCT, format);
  361. }
  362. static bool validate_backend_format_and_colortype(const GrCaps* caps,
  363. GrColorType colorType,
  364. const GrBackendFormat& format) {
  365. return caps->areColorTypeAndFormatCompatible(colorType, format);
  366. }
  367. #endif
  368. sk_sp<GrTextureProxy> GrProxyProvider::createProxy(const GrBackendFormat& format,
  369. const GrSurfaceDesc& desc,
  370. GrRenderable renderable,
  371. int renderTargetSampleCnt,
  372. GrSurfaceOrigin origin,
  373. GrMipMapped mipMapped,
  374. SkBackingFit fit,
  375. SkBudgeted budgeted,
  376. GrProtected isProtected,
  377. GrInternalSurfaceFlags surfaceFlags) {
  378. if (GrPixelConfigIsCompressed(desc.fConfig)) {
  379. // Deferred proxies for compressed textures are not supported.
  380. return nullptr;
  381. }
  382. const GrCaps* caps = this->caps();
  383. GrColorType colorType = GrPixelConfigToColorType(desc.fConfig);
  384. SkASSERT(GrCaps::AreConfigsCompatible(desc.fConfig,
  385. caps->getConfigFromBackendFormat(format, colorType)));
  386. SkASSERT(validate_backend_format_and_colortype(caps, colorType, format));
  387. if (GrMipMapped::kYes == mipMapped) {
  388. // SkMipMap doesn't include the base level in the level count so we have to add 1
  389. int mipCount = SkMipMap::ComputeLevelCount(desc.fWidth, desc.fHeight) + 1;
  390. if (1 == mipCount) {
  391. mipMapped = GrMipMapped::kNo;
  392. }
  393. }
  394. if (!caps->validateSurfaceDesc(desc, renderable, renderTargetSampleCnt, mipMapped)) {
  395. return nullptr;
  396. }
  397. GrSurfaceDesc copyDesc = desc;
  398. GrSwizzle texSwizzle = caps->getTextureSwizzle(format, colorType);
  399. if (renderable == GrRenderable::kYes) {
  400. renderTargetSampleCnt =
  401. caps->getRenderTargetSampleCount(renderTargetSampleCnt, colorType, format);
  402. // We know anything we instantiate later from this deferred path will be
  403. // both texturable and renderable
  404. GrSwizzle outSwizzle = caps->getOutputSwizzle(format, colorType);
  405. return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(
  406. *caps, format, copyDesc, renderTargetSampleCnt, origin, mipMapped, texSwizzle,
  407. outSwizzle, fit, budgeted, isProtected, surfaceFlags));
  408. }
  409. return sk_sp<GrTextureProxy>(new GrTextureProxy(format, copyDesc, origin, mipMapped, texSwizzle,
  410. fit, budgeted, isProtected, surfaceFlags));
  411. }
  412. sk_sp<GrTextureProxy> GrProxyProvider::createCompressedTextureProxy(
  413. int width, int height, SkBudgeted budgeted, SkImage::CompressionType compressionType,
  414. sk_sp<SkData> data) {
  415. GrBackendFormat format = this->caps()->getBackendFormatFromCompressionType(compressionType);
  416. GrSurfaceDesc desc;
  417. desc.fConfig = GrCompressionTypePixelConfig(compressionType);
  418. desc.fWidth = width;
  419. desc.fHeight = height;
  420. if (!this->caps()->isConfigTexturable(desc.fConfig)) {
  421. return nullptr;
  422. }
  423. sk_sp<GrTextureProxy> proxy = this->createLazyProxy(
  424. [width, height, compressionType, budgeted, data](GrResourceProvider* resourceProvider) {
  425. return LazyInstantiationResult(resourceProvider->createCompressedTexture(
  426. width, height, compressionType, budgeted, data.get()));
  427. },
  428. format, desc, GrRenderable::kNo, 1, kTopLeft_GrSurfaceOrigin, GrMipMapped::kNo,
  429. SkBackingFit::kExact, SkBudgeted::kYes, GrProtected::kNo);
  430. if (!proxy) {
  431. return nullptr;
  432. }
  433. GrContext* direct = fImageContext->priv().asDirectContext();
  434. if (direct) {
  435. GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
  436. // In order to reuse code we always create a lazy proxy. When we aren't in DDL mode however
  437. // we're better off instantiating the proxy immediately here.
  438. if (!proxy->priv().doLazyInstantiation(resourceProvider)) {
  439. return nullptr;
  440. }
  441. }
  442. return proxy;
  443. }
  444. sk_sp<GrTextureProxy> GrProxyProvider::wrapBackendTexture(const GrBackendTexture& backendTex,
  445. GrColorType grColorType,
  446. GrSurfaceOrigin origin,
  447. GrWrapOwnership ownership,
  448. GrWrapCacheable cacheable,
  449. GrIOType ioType,
  450. ReleaseProc releaseProc,
  451. ReleaseContext releaseCtx) {
  452. SkASSERT(ioType != kWrite_GrIOType);
  453. if (this->isAbandoned()) {
  454. return nullptr;
  455. }
  456. // This is only supported on a direct GrContext.
  457. GrContext* direct = fImageContext->priv().asDirectContext();
  458. if (!direct) {
  459. return nullptr;
  460. }
  461. const GrCaps* caps = this->caps();
  462. SkASSERT(GrCaps::AreConfigsCompatible(backendTex.config(),
  463. caps->getConfigFromBackendFormat(
  464. backendTex.getBackendFormat(),
  465. grColorType)));
  466. SkASSERT(validate_backend_format_and_colortype(caps, grColorType,
  467. backendTex.getBackendFormat()));
  468. GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
  469. sk_sp<GrTexture> tex =
  470. resourceProvider->wrapBackendTexture(backendTex, grColorType,
  471. ownership, cacheable, ioType);
  472. if (!tex) {
  473. return nullptr;
  474. }
  475. if (releaseProc) {
  476. tex->setRelease(releaseProc, releaseCtx);
  477. }
  478. SkASSERT(!tex->asRenderTarget()); // Strictly a GrTexture
  479. // Make sure we match how we created the proxy with SkBudgeted::kNo
  480. SkASSERT(GrBudgetedType::kBudgeted != tex->resourcePriv().budgetedType());
  481. GrSwizzle texSwizzle = caps->getTextureSwizzle(tex->backendFormat(), grColorType);
  482. return sk_sp<GrTextureProxy>(new GrTextureProxy(std::move(tex), origin, texSwizzle));
  483. }
  484. sk_sp<GrTextureProxy> GrProxyProvider::wrapRenderableBackendTexture(
  485. const GrBackendTexture& backendTex, GrSurfaceOrigin origin, int sampleCnt,
  486. GrColorType colorType, GrWrapOwnership ownership, GrWrapCacheable cacheable,
  487. ReleaseProc releaseProc, ReleaseContext releaseCtx) {
  488. if (this->isAbandoned()) {
  489. return nullptr;
  490. }
  491. // This is only supported on a direct GrContext.
  492. GrContext* direct = fImageContext->priv().asDirectContext();
  493. if (!direct) {
  494. return nullptr;
  495. }
  496. const GrCaps* caps = this->caps();
  497. SkASSERT(GrCaps::AreConfigsCompatible(backendTex.config(),
  498. caps->getConfigFromBackendFormat(
  499. backendTex.getBackendFormat(),
  500. colorType)));
  501. SkASSERT(validate_backend_format_and_colortype(caps, colorType, backendTex.getBackendFormat()));
  502. GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
  503. sampleCnt = caps->getRenderTargetSampleCount(sampleCnt, colorType,
  504. backendTex.getBackendFormat());
  505. if (!sampleCnt) {
  506. return nullptr;
  507. }
  508. sk_sp<GrTexture> tex = resourceProvider->wrapRenderableBackendTexture(backendTex, sampleCnt,
  509. colorType, ownership,
  510. cacheable);
  511. if (!tex) {
  512. return nullptr;
  513. }
  514. if (releaseProc) {
  515. tex->setRelease(releaseProc, releaseCtx);
  516. }
  517. SkASSERT(tex->asRenderTarget()); // A GrTextureRenderTarget
  518. // Make sure we match how we created the proxy with SkBudgeted::kNo
  519. SkASSERT(GrBudgetedType::kBudgeted != tex->resourcePriv().budgetedType());
  520. GrSwizzle texSwizzle = caps->getTextureSwizzle(tex->backendFormat(), colorType);
  521. GrSwizzle outSwizzle = caps->getOutputSwizzle(tex->backendFormat(), colorType);
  522. return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(std::move(tex), origin, texSwizzle,
  523. outSwizzle));
  524. }
  525. sk_sp<GrSurfaceProxy> GrProxyProvider::wrapBackendRenderTarget(
  526. const GrBackendRenderTarget& backendRT, GrColorType grColorType,
  527. GrSurfaceOrigin origin, ReleaseProc releaseProc, ReleaseContext releaseCtx) {
  528. if (this->isAbandoned()) {
  529. return nullptr;
  530. }
  531. // This is only supported on a direct GrContext.
  532. GrContext* direct = fImageContext->priv().asDirectContext();
  533. if (!direct) {
  534. return nullptr;
  535. }
  536. #ifdef SK_DEBUG
  537. GrPixelConfig testConfig =
  538. this->caps()->validateBackendRenderTarget(backendRT, grColorType);
  539. SkASSERT(testConfig != kUnknown_GrPixelConfig);
  540. #endif
  541. GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
  542. sk_sp<GrRenderTarget> rt = resourceProvider->wrapBackendRenderTarget(backendRT, grColorType);
  543. if (!rt) {
  544. return nullptr;
  545. }
  546. if (releaseProc) {
  547. rt->setRelease(releaseProc, releaseCtx);
  548. }
  549. SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable
  550. SkASSERT(!rt->getUniqueKey().isValid());
  551. // Make sure we match how we created the proxy with SkBudgeted::kNo
  552. SkASSERT(GrBudgetedType::kBudgeted != rt->resourcePriv().budgetedType());
  553. GrSwizzle texSwizzle = this->caps()->getTextureSwizzle(rt->backendFormat(), grColorType);
  554. GrSwizzle outSwizzle = this->caps()->getOutputSwizzle(rt->backendFormat(), grColorType);
  555. return sk_sp<GrRenderTargetProxy>(new GrRenderTargetProxy(std::move(rt), origin, texSwizzle,
  556. outSwizzle));
  557. }
  558. sk_sp<GrSurfaceProxy> GrProxyProvider::wrapBackendTextureAsRenderTarget(
  559. const GrBackendTexture& backendTex, GrColorType grColorType,
  560. GrSurfaceOrigin origin, int sampleCnt) {
  561. if (this->isAbandoned()) {
  562. return nullptr;
  563. }
  564. // This is only supported on a direct GrContext.
  565. GrContext* direct = fImageContext->priv().asDirectContext();
  566. if (!direct) {
  567. return nullptr;
  568. }
  569. const GrCaps* caps = this->caps();
  570. SkASSERT(GrCaps::AreConfigsCompatible(backendTex.config(),
  571. caps->getConfigFromBackendFormat(
  572. backendTex.getBackendFormat(),
  573. grColorType)));
  574. SkASSERT(validate_backend_format_and_colortype(caps, grColorType,
  575. backendTex.getBackendFormat()));
  576. GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
  577. sk_sp<GrRenderTarget> rt =
  578. resourceProvider->wrapBackendTextureAsRenderTarget(backendTex, sampleCnt, grColorType);
  579. if (!rt) {
  580. return nullptr;
  581. }
  582. SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable
  583. SkASSERT(!rt->getUniqueKey().isValid());
  584. // This proxy should be unbudgeted because we're just wrapping an external resource
  585. SkASSERT(GrBudgetedType::kBudgeted != rt->resourcePriv().budgetedType());
  586. GrSwizzle texSwizzle = caps->getTextureSwizzle(rt->backendFormat(), grColorType);
  587. GrSwizzle outSwizzle = caps->getOutputSwizzle(rt->backendFormat(), grColorType);
  588. return sk_sp<GrSurfaceProxy>(new GrRenderTargetProxy(std::move(rt), origin, texSwizzle,
  589. outSwizzle));
  590. }
  591. sk_sp<GrRenderTargetProxy> GrProxyProvider::wrapVulkanSecondaryCBAsRenderTarget(
  592. const SkImageInfo& imageInfo, const GrVkDrawableInfo& vkInfo) {
  593. if (this->isAbandoned()) {
  594. return nullptr;
  595. }
  596. // This is only supported on a direct GrContext.
  597. GrContext* direct = fImageContext->priv().asDirectContext();
  598. if (!direct) {
  599. return nullptr;
  600. }
  601. GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
  602. sk_sp<GrRenderTarget> rt = resourceProvider->wrapVulkanSecondaryCBAsRenderTarget(imageInfo,
  603. vkInfo);
  604. if (!rt) {
  605. return nullptr;
  606. }
  607. SkASSERT(!rt->asTexture()); // A GrRenderTarget that's not textureable
  608. SkASSERT(!rt->getUniqueKey().isValid());
  609. // This proxy should be unbudgeted because we're just wrapping an external resource
  610. SkASSERT(GrBudgetedType::kBudgeted != rt->resourcePriv().budgetedType());
  611. GrColorType colorType = GrPixelConfigToColorType(rt->config());
  612. GrSwizzle texSwizzle = this->caps()->getTextureSwizzle(rt->backendFormat(), colorType);
  613. GrSwizzle outSwizzle = this->caps()->getOutputSwizzle(rt->backendFormat(), colorType);
  614. // All Vulkan surfaces uses top left origins.
  615. return sk_sp<GrRenderTargetProxy>(
  616. new GrRenderTargetProxy(std::move(rt),
  617. kTopLeft_GrSurfaceOrigin, texSwizzle, outSwizzle,
  618. GrRenderTargetProxy::WrapsVkSecondaryCB::kYes));
  619. }
  620. sk_sp<GrTextureProxy> GrProxyProvider::createLazyProxy(LazyInstantiateCallback&& callback,
  621. const GrBackendFormat& format,
  622. const GrSurfaceDesc& desc,
  623. GrRenderable renderable,
  624. int renderTargetSampleCnt,
  625. GrSurfaceOrigin origin,
  626. GrMipMapped mipMapped,
  627. SkBackingFit fit,
  628. SkBudgeted budgeted,
  629. GrProtected isProtected) {
  630. return this->createLazyProxy(std::move(callback), format, desc, renderable,
  631. renderTargetSampleCnt, origin, mipMapped,
  632. GrInternalSurfaceFlags::kNone, fit, budgeted, isProtected);
  633. }
  634. sk_sp<GrTextureProxy> GrProxyProvider::createLazyProxy(LazyInstantiateCallback&& callback,
  635. const GrBackendFormat& format,
  636. const GrSurfaceDesc& desc,
  637. GrRenderable renderable,
  638. int renderTargetSampleCnt,
  639. GrSurfaceOrigin origin,
  640. GrMipMapped mipMapped,
  641. GrInternalSurfaceFlags surfaceFlags,
  642. SkBackingFit fit,
  643. SkBudgeted budgeted,
  644. GrProtected isProtected) {
  645. // For non-ddl draws always make lazy proxy's single use.
  646. LazyInstantiationType lazyType = this->renderingDirectly() ? LazyInstantiationType::kSingleUse
  647. : LazyInstantiationType::kMultipleUse;
  648. return this->createLazyProxy(std::move(callback), format, desc, renderable,
  649. renderTargetSampleCnt, origin, mipMapped, surfaceFlags, fit,
  650. budgeted, isProtected, lazyType);
  651. }
  652. sk_sp<GrTextureProxy> GrProxyProvider::createLazyProxy(LazyInstantiateCallback&& callback,
  653. const GrBackendFormat& format,
  654. const GrSurfaceDesc& desc,
  655. GrRenderable renderable,
  656. int renderTargetSampleCnt,
  657. GrSurfaceOrigin origin,
  658. GrMipMapped mipMapped,
  659. GrInternalSurfaceFlags surfaceFlags,
  660. SkBackingFit fit,
  661. SkBudgeted budgeted,
  662. GrProtected isProtected,
  663. LazyInstantiationType lazyType) {
  664. SkASSERT((desc.fWidth <= 0 && desc.fHeight <= 0) ||
  665. (desc.fWidth > 0 && desc.fHeight > 0));
  666. if (desc.fWidth > this->caps()->maxTextureSize() ||
  667. desc.fHeight > this->caps()->maxTextureSize()) {
  668. return nullptr;
  669. }
  670. SkASSERT(validate_backend_format_and_config(this->caps(), format, desc.fConfig));
  671. GrColorType colorType = GrPixelConfigToColorType(desc.fConfig);
  672. GrSwizzle texSwizzle = this->caps()->getTextureSwizzle(format, colorType);
  673. GrSwizzle outSwizzle = this->caps()->getOutputSwizzle(format, colorType);
  674. return sk_sp<GrTextureProxy>(
  675. renderable == GrRenderable::kYes
  676. ? new GrTextureRenderTargetProxy(std::move(callback), lazyType, format, desc,
  677. renderTargetSampleCnt, origin, mipMapped,
  678. texSwizzle, outSwizzle, fit, budgeted,
  679. isProtected, surfaceFlags)
  680. : new GrTextureProxy(std::move(callback), lazyType, format, desc, origin,
  681. mipMapped, texSwizzle, fit, budgeted, isProtected,
  682. surfaceFlags));
  683. }
  684. sk_sp<GrRenderTargetProxy> GrProxyProvider::createLazyRenderTargetProxy(
  685. LazyInstantiateCallback&& callback, const GrBackendFormat& format,
  686. const GrSurfaceDesc& desc, int sampleCnt, GrSurfaceOrigin origin,
  687. GrInternalSurfaceFlags surfaceFlags, const TextureInfo* textureInfo, SkBackingFit fit,
  688. SkBudgeted budgeted, GrProtected isProtected, bool wrapsVkSecondaryCB) {
  689. SkASSERT((desc.fWidth <= 0 && desc.fHeight <= 0) ||
  690. (desc.fWidth > 0 && desc.fHeight > 0));
  691. if (desc.fWidth > this->caps()->maxRenderTargetSize() ||
  692. desc.fHeight > this->caps()->maxRenderTargetSize()) {
  693. return nullptr;
  694. }
  695. SkASSERT(validate_backend_format_and_config(this->caps(), format, desc.fConfig));
  696. using LazyInstantiationType = GrSurfaceProxy::LazyInstantiationType;
  697. // For non-ddl draws always make lazy proxy's single use.
  698. LazyInstantiationType lazyType = this->renderingDirectly() ? LazyInstantiationType::kSingleUse
  699. : LazyInstantiationType::kMultipleUse;
  700. GrColorType colorType = GrPixelConfigToColorType(desc.fConfig);
  701. GrSwizzle texSwizzle = this->caps()->getTextureSwizzle(format, colorType);
  702. GrSwizzle outSwizzle = this->caps()->getOutputSwizzle(format, colorType);
  703. if (textureInfo) {
  704. // Wrapped vulkan secondary command buffers don't support texturing since we won't have an
  705. // actual VkImage to texture from.
  706. SkASSERT(!wrapsVkSecondaryCB);
  707. return sk_sp<GrRenderTargetProxy>(new GrTextureRenderTargetProxy(
  708. std::move(callback), lazyType, format, desc, sampleCnt, origin,
  709. textureInfo->fMipMapped, texSwizzle, outSwizzle, fit, budgeted, isProtected,
  710. surfaceFlags));
  711. }
  712. GrRenderTargetProxy::WrapsVkSecondaryCB vkSCB =
  713. wrapsVkSecondaryCB ? GrRenderTargetProxy::WrapsVkSecondaryCB::kYes
  714. : GrRenderTargetProxy::WrapsVkSecondaryCB::kNo;
  715. return sk_sp<GrRenderTargetProxy>(new GrRenderTargetProxy(
  716. std::move(callback), lazyType, format, desc, sampleCnt, origin, texSwizzle, outSwizzle,
  717. fit, budgeted, isProtected, surfaceFlags, vkSCB));
  718. }
  719. sk_sp<GrTextureProxy> GrProxyProvider::MakeFullyLazyProxy(
  720. LazyInstantiateCallback&& callback, const GrBackendFormat& format, GrRenderable renderable,
  721. int renderTargetSampleCnt, GrProtected isProtected, GrSurfaceOrigin origin,
  722. GrPixelConfig config, const GrCaps& caps) {
  723. SkASSERT(renderTargetSampleCnt == 1 || renderable == GrRenderable::kYes);
  724. SkASSERT(validate_backend_format_and_config(&caps, format, config));
  725. GrSurfaceDesc desc;
  726. GrInternalSurfaceFlags surfaceFlags = GrInternalSurfaceFlags::kNone;
  727. desc.fWidth = -1;
  728. desc.fHeight = -1;
  729. desc.fConfig = config;
  730. GrColorType colorType = GrPixelConfigToColorType(desc.fConfig);
  731. GrSwizzle texSwizzle = caps.getTextureSwizzle(format, colorType);
  732. GrSwizzle outSwizzle = caps.getOutputSwizzle(format, colorType);
  733. return sk_sp<GrTextureProxy>(
  734. (GrRenderable::kYes == renderable)
  735. ? new GrTextureRenderTargetProxy(
  736. std::move(callback), LazyInstantiationType::kSingleUse, format, desc,
  737. renderTargetSampleCnt, origin, GrMipMapped::kNo, texSwizzle,
  738. outSwizzle, SkBackingFit::kApprox, SkBudgeted::kYes, isProtected,
  739. surfaceFlags)
  740. : new GrTextureProxy(std::move(callback), LazyInstantiationType::kSingleUse,
  741. format, desc, origin, GrMipMapped::kNo, texSwizzle,
  742. SkBackingFit::kApprox, SkBudgeted::kYes, isProtected,
  743. surfaceFlags));
  744. }
  745. bool GrProxyProvider::IsFunctionallyExact(GrSurfaceProxy* proxy) {
  746. const bool isInstantiated = proxy->isInstantiated();
  747. // A proxy is functionally exact if:
  748. // it is exact (obvs)
  749. // when it is instantiated it will be exact (i.e., power of two dimensions)
  750. // it is already instantiated and the proxy covers the entire backing surface
  751. return proxy->priv().isExact() ||
  752. (!isInstantiated && SkIsPow2(proxy->width()) && SkIsPow2(proxy->height())) ||
  753. (isInstantiated && proxy->worstCaseWidth() == proxy->width() &&
  754. proxy->worstCaseHeight() == proxy->height());
  755. }
  756. void GrProxyProvider::processInvalidUniqueKey(const GrUniqueKey& key, GrTextureProxy* proxy,
  757. InvalidateGPUResource invalidateGPUResource) {
  758. SkASSERT(key.isValid());
  759. if (!proxy) {
  760. proxy = fUniquelyKeyedProxies.find(key);
  761. }
  762. SkASSERT(!proxy || proxy->getUniqueKey() == key);
  763. // Locate the corresponding GrGpuResource (if it needs to be invalidated) before clearing the
  764. // proxy's unique key. We must do it in this order because 'key' may alias the proxy's key.
  765. sk_sp<GrGpuResource> invalidGpuResource;
  766. if (InvalidateGPUResource::kYes == invalidateGPUResource) {
  767. GrContext* direct = fImageContext->priv().asDirectContext();
  768. if (direct) {
  769. GrResourceProvider* resourceProvider = direct->priv().resourceProvider();
  770. invalidGpuResource = resourceProvider->findByUniqueKey<GrGpuResource>(key);
  771. }
  772. SkASSERT(!invalidGpuResource || invalidGpuResource->getUniqueKey() == key);
  773. }
  774. // Note: this method is called for the whole variety of GrGpuResources so often 'key'
  775. // will not be in 'fUniquelyKeyedProxies'.
  776. if (proxy) {
  777. fUniquelyKeyedProxies.remove(key);
  778. proxy->cacheAccess().clearUniqueKey();
  779. }
  780. if (invalidGpuResource) {
  781. invalidGpuResource->resourcePriv().removeUniqueKey();
  782. }
  783. }
  784. uint32_t GrProxyProvider::contextID() const {
  785. return fImageContext->priv().contextID();
  786. }
  787. const GrCaps* GrProxyProvider::caps() const {
  788. return fImageContext->priv().caps();
  789. }
  790. sk_sp<const GrCaps> GrProxyProvider::refCaps() const {
  791. return fImageContext->priv().refCaps();
  792. }
  793. bool GrProxyProvider::isAbandoned() const {
  794. return fImageContext->priv().abandoned();
  795. }
  796. void GrProxyProvider::orphanAllUniqueKeys() {
  797. UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies);
  798. for (UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies); !iter.done(); ++iter) {
  799. GrTextureProxy& tmp = *iter;
  800. tmp.fProxyProvider = nullptr;
  801. }
  802. }
  803. void GrProxyProvider::removeAllUniqueKeys() {
  804. UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies);
  805. for (UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies); !iter.done(); ++iter) {
  806. GrTextureProxy& tmp = *iter;
  807. this->processInvalidUniqueKey(tmp.getUniqueKey(), &tmp, InvalidateGPUResource::kNo);
  808. }
  809. SkASSERT(!fUniquelyKeyedProxies.count());
  810. }
  811. bool GrProxyProvider::renderingDirectly() const {
  812. return fImageContext->priv().asDirectContext();
  813. }