GrBitmapTextureMaker.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. #include "src/gpu/GrBitmapTextureMaker.h"
  8. #include "include/core/SkBitmap.h"
  9. #include "include/core/SkPixelRef.h"
  10. #include "include/private/GrRecordingContext.h"
  11. #include "src/core/SkMipMap.h"
  12. #include "src/gpu/GrGpuResourcePriv.h"
  13. #include "src/gpu/GrProxyProvider.h"
  14. #include "src/gpu/GrRecordingContextPriv.h"
  15. #include "src/gpu/GrSurfaceContext.h"
  16. #include "src/gpu/SkGr.h"
  17. static GrColorSpaceInfo make_info(const SkBitmap& bm) {
  18. return GrColorSpaceInfo(SkColorTypeToGrColorType(bm.colorType()), bm.alphaType(),
  19. bm.refColorSpace());
  20. }
  21. GrBitmapTextureMaker::GrBitmapTextureMaker(GrRecordingContext* context, const SkBitmap& bitmap,
  22. bool useDecal)
  23. : INHERITED(context, bitmap.width(), bitmap.height(), make_info(bitmap), useDecal)
  24. , fBitmap(bitmap) {
  25. if (!bitmap.isVolatile()) {
  26. SkIPoint origin = bitmap.pixelRefOrigin();
  27. SkIRect subset = SkIRect::MakeXYWH(origin.fX, origin.fY, bitmap.width(),
  28. bitmap.height());
  29. GrMakeKeyFromImageID(&fOriginalKey, bitmap.pixelRef()->getGenerationID(), subset);
  30. }
  31. }
  32. sk_sp<GrTextureProxy> GrBitmapTextureMaker::refOriginalTextureProxy(bool willBeMipped,
  33. AllowedTexGenType onlyIfFast) {
  34. if (AllowedTexGenType::kCheap == onlyIfFast) {
  35. return nullptr;
  36. }
  37. GrProxyProvider* proxyProvider = this->context()->priv().proxyProvider();
  38. sk_sp<GrTextureProxy> proxy;
  39. if (fOriginalKey.isValid()) {
  40. proxy = proxyProvider->findOrCreateProxyByUniqueKey(fOriginalKey, kTopLeft_GrSurfaceOrigin);
  41. if (proxy && (!willBeMipped || GrMipMapped::kYes == proxy->mipMapped())) {
  42. return proxy;
  43. }
  44. }
  45. if (!proxy) {
  46. proxy = proxyProvider->createProxyFromBitmap(fBitmap, willBeMipped ? GrMipMapped::kYes
  47. : GrMipMapped::kNo);
  48. if (proxy) {
  49. if (fOriginalKey.isValid()) {
  50. proxyProvider->assignUniqueKeyToProxy(fOriginalKey, proxy.get());
  51. }
  52. if (!willBeMipped || GrMipMapped::kYes == proxy->mipMapped()) {
  53. SkASSERT(proxy->origin() == kTopLeft_GrSurfaceOrigin);
  54. if (fOriginalKey.isValid()) {
  55. GrInstallBitmapUniqueKeyInvalidator(
  56. fOriginalKey, proxyProvider->contextID(), fBitmap.pixelRef());
  57. }
  58. return proxy;
  59. }
  60. }
  61. }
  62. if (proxy) {
  63. SkASSERT(willBeMipped);
  64. SkASSERT(GrMipMapped::kNo == proxy->mipMapped());
  65. // We need a mipped proxy, but we either found a proxy earlier that wasn't mipped or
  66. // generated a non mipped proxy. Thus we generate a new mipped surface and copy the original
  67. // proxy into the base layer. We will then let the gpu generate the rest of the mips.
  68. if (auto mippedProxy = GrCopyBaseMipMapToTextureProxy(this->context(), proxy.get())) {
  69. SkASSERT(mippedProxy->origin() == kTopLeft_GrSurfaceOrigin);
  70. if (fOriginalKey.isValid()) {
  71. // In this case we are stealing the key from the original proxy which should only
  72. // happen when we have just generated mipmaps for an originally unmipped
  73. // proxy/texture. This means that all future uses of the key will access the
  74. // mipmapped version. The texture backing the unmipped version will remain in the
  75. // resource cache until the last texture proxy referencing it is deleted at which
  76. // time it too will be deleted or recycled.
  77. SkASSERT(proxy->getUniqueKey() == fOriginalKey);
  78. proxyProvider->removeUniqueKeyFromProxy(proxy.get());
  79. proxyProvider->assignUniqueKeyToProxy(fOriginalKey, mippedProxy.get());
  80. GrInstallBitmapUniqueKeyInvalidator(fOriginalKey, proxyProvider->contextID(),
  81. fBitmap.pixelRef());
  82. }
  83. return mippedProxy;
  84. }
  85. // We failed to make a mipped proxy with the base copied into it. This could have
  86. // been from failure to make the proxy or failure to do the copy. Thus we will fall
  87. // back to just using the non mipped proxy; See skbug.com/7094.
  88. return proxy;
  89. }
  90. return nullptr;
  91. }
  92. void GrBitmapTextureMaker::makeCopyKey(const CopyParams& copyParams, GrUniqueKey* copyKey) {
  93. // Destination color space is irrelevant - we always upload the bitmap's contents as-is
  94. if (fOriginalKey.isValid()) {
  95. MakeCopyKeyFromOrigKey(fOriginalKey, copyParams, copyKey);
  96. }
  97. }
  98. void GrBitmapTextureMaker::didCacheCopy(const GrUniqueKey& copyKey, uint32_t contextUniqueID) {
  99. GrInstallBitmapUniqueKeyInvalidator(copyKey, contextUniqueID, fBitmap.pixelRef());
  100. }