GrImageTextureMaker.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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/GrImageTextureMaker.h"
  8. #include "src/gpu/GrColorSpaceXform.h"
  9. #include "src/gpu/SkGr.h"
  10. #include "src/gpu/effects/GrYUVtoRGBEffect.h"
  11. #include "src/image/SkImage_GpuYUVA.h"
  12. #include "src/image/SkImage_Lazy.h"
  13. static GrColorSpaceInfo make_info(const SkImage*& image) {
  14. return GrColorSpaceInfo(SkColorTypeToGrColorType(image->colorType()),
  15. image->alphaType(),
  16. image->refColorSpace());
  17. }
  18. GrImageTextureMaker::GrImageTextureMaker(GrRecordingContext* context, const SkImage* client,
  19. SkImage::CachingHint chint, bool useDecal)
  20. : INHERITED(context, client->width(), client->height(), make_info(client), useDecal)
  21. , fImage(static_cast<const SkImage_Lazy*>(client))
  22. , fCachingHint(chint) {
  23. SkASSERT(client->isLazyGenerated());
  24. GrMakeKeyFromImageID(&fOriginalKey, client->uniqueID(),
  25. SkIRect::MakeWH(this->width(), this->height()));
  26. }
  27. sk_sp<GrTextureProxy> GrImageTextureMaker::refOriginalTextureProxy(bool willBeMipped,
  28. AllowedTexGenType onlyIfFast) {
  29. return fImage->lockTextureProxy(this->context(), fOriginalKey, fCachingHint,
  30. willBeMipped, onlyIfFast);
  31. }
  32. void GrImageTextureMaker::makeCopyKey(const CopyParams& stretch, GrUniqueKey* paramsCopyKey) {
  33. if (fOriginalKey.isValid() && SkImage::kAllow_CachingHint == fCachingHint) {
  34. GrUniqueKey cacheKey;
  35. fImage->makeCacheKeyFromOrigKey(fOriginalKey, &cacheKey);
  36. MakeCopyKeyFromOrigKey(cacheKey, stretch, paramsCopyKey);
  37. }
  38. }
  39. /////////////////////////////////////////////////////////////////////////////////////////////////
  40. GrYUVAImageTextureMaker::GrYUVAImageTextureMaker(GrContext* context, const SkImage* client,
  41. bool useDecal)
  42. : INHERITED(context, client->width(), client->height(), make_info(client), useDecal)
  43. , fImage(static_cast<const SkImage_GpuYUVA*>(client)) {
  44. SkASSERT(as_IB(client)->isYUVA());
  45. GrMakeKeyFromImageID(&fOriginalKey, client->uniqueID(),
  46. SkIRect::MakeWH(this->width(), this->height()));
  47. }
  48. sk_sp<GrTextureProxy> GrYUVAImageTextureMaker::refOriginalTextureProxy(bool willBeMipped,
  49. AllowedTexGenType onlyIfFast) {
  50. if (AllowedTexGenType::kCheap == onlyIfFast) {
  51. return nullptr;
  52. }
  53. if (willBeMipped) {
  54. return fImage->asMippedTextureProxyRef(this->context());
  55. } else {
  56. return fImage->asTextureProxyRef(this->context());
  57. }
  58. }
  59. void GrYUVAImageTextureMaker::makeCopyKey(const CopyParams& stretch, GrUniqueKey* paramsCopyKey) {
  60. // TODO: Do we ever want to disable caching?
  61. if (fOriginalKey.isValid()) {
  62. GrUniqueKey cacheKey;
  63. static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
  64. GrUniqueKey::Builder builder(&cacheKey, fOriginalKey, kDomain, 0, "Image");
  65. MakeCopyKeyFromOrigKey(cacheKey, stretch, paramsCopyKey);
  66. }
  67. }
  68. std::unique_ptr<GrFragmentProcessor> GrYUVAImageTextureMaker::createFragmentProcessor(
  69. const SkMatrix& textureMatrix,
  70. const SkRect& constraintRect,
  71. FilterConstraint filterConstraint,
  72. bool coordsLimitedToConstraintRect,
  73. const GrSamplerState::Filter* filterOrNullForBicubic) {
  74. // Check simple cases to see if we need to fall back to flattening the image (or whether it's
  75. // already been flattened.)
  76. if (!filterOrNullForBicubic || this->domainNeedsDecal() || fImage->fRGBProxy) {
  77. return this->INHERITED::createFragmentProcessor(textureMatrix, constraintRect,
  78. filterConstraint,
  79. coordsLimitedToConstraintRect,
  80. filterOrNullForBicubic);
  81. }
  82. // Check to see if the client has given us pre-mipped textures or we can generate them
  83. // If not, fall back to bilerp. Also fall back to bilerp when a domain is requested
  84. GrSamplerState::Filter filter = *filterOrNullForBicubic;
  85. if (GrSamplerState::Filter::kMipMap == filter &&
  86. (filterConstraint == GrTextureProducer::kYes_FilterConstraint ||
  87. !fImage->setupMipmapsForPlanes(this->context()))) {
  88. filter = GrSamplerState::Filter::kBilerp;
  89. }
  90. // Cannot rely on GrTextureProducer's domain infrastructure since we need to calculate domain's
  91. // per plane, which may be different, so respect the filterConstraint without any additional
  92. // analysis.
  93. const SkRect* domain = nullptr;
  94. if (filterConstraint == GrTextureProducer::kYes_FilterConstraint) {
  95. domain = &constraintRect;
  96. }
  97. auto fp = GrYUVtoRGBEffect::Make(fImage->fProxies, fImage->fYUVAIndices,
  98. fImage->fYUVColorSpace, filter, textureMatrix, domain);
  99. if (fImage->fFromColorSpace) {
  100. fp = GrColorSpaceXformEffect::Make(std::move(fp), fImage->fFromColorSpace.get(),
  101. fImage->alphaType(), fImage->colorSpace());
  102. }
  103. return fp;
  104. }