GrYUVtoRGBEffect.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. #ifndef GrYUVtoRGBEffect_DEFINED
  8. #define GrYUVtoRGBEffect_DEFINED
  9. #include "include/core/SkTypes.h"
  10. #include "src/gpu/GrCoordTransform.h"
  11. #include "src/gpu/GrFragmentProcessor.h"
  12. #include "src/gpu/effects/GrTextureDomain.h"
  13. #include "include/core/SkYUVAIndex.h"
  14. class GrYUVtoRGBEffect : public GrFragmentProcessor {
  15. public:
  16. // The domain supported by this effect is more limited than the general GrTextureDomain due
  17. // to the multi-planar, varying resolution images that it has to sample. If 'domain' is provided
  18. // it is the Y plane's domain. This will automatically inset for bilinear filtering, and only
  19. // the clamp wrap mode is supported.
  20. static std::unique_ptr<GrFragmentProcessor> Make(const sk_sp<GrTextureProxy> proxies[],
  21. const SkYUVAIndex indices[4],
  22. SkYUVColorSpace yuvColorSpace,
  23. GrSamplerState::Filter filterMode,
  24. const SkMatrix& localMatrix = SkMatrix::I(),
  25. const SkRect* domain = nullptr);
  26. #ifdef SK_DEBUG
  27. SkString dumpInfo() const override;
  28. #endif
  29. SkYUVColorSpace yuvColorSpace() const { return fYUVColorSpace; }
  30. const SkYUVAIndex& yuvaIndex(int i) const { return fYUVAIndices[i]; }
  31. GrYUVtoRGBEffect(const GrYUVtoRGBEffect& src);
  32. std::unique_ptr<GrFragmentProcessor> clone() const override;
  33. const char* name() const override { return "YUVtoRGBEffect"; }
  34. private:
  35. GrYUVtoRGBEffect(const sk_sp<GrTextureProxy> proxies[], const SkSize scales[],
  36. const GrSamplerState::Filter filterModes[], int numPlanes,
  37. const SkYUVAIndex yuvaIndices[4], SkYUVColorSpace yuvColorSpace,
  38. const SkMatrix& localMatrix, const SkRect* domain)
  39. : INHERITED(kGrYUVtoRGBEffect_ClassID, kNone_OptimizationFlags)
  40. , fDomains{GrTextureDomain::IgnoredDomain(), GrTextureDomain::IgnoredDomain(),
  41. GrTextureDomain::IgnoredDomain(), GrTextureDomain::IgnoredDomain()}
  42. , fYUVColorSpace(yuvColorSpace) {
  43. for (int i = 0; i < numPlanes; ++i) {
  44. SkMatrix planeMatrix = SkMatrix::MakeScale(scales[i].width(), scales[i].height());
  45. if (domain) {
  46. SkASSERT(filterModes[i] != GrSamplerState::Filter::kMipMap);
  47. SkRect scaledDomain = planeMatrix.mapRect(*domain);
  48. if (filterModes[i] != GrSamplerState::Filter::kNearest) {
  49. // Inset by half a pixel for bilerp, after scaling to the size of the plane
  50. scaledDomain.inset(0.5f, 0.5f);
  51. }
  52. fDomains[i] = GrTextureDomain(proxies[i].get(), scaledDomain,
  53. GrTextureDomain::kClamp_Mode, GrTextureDomain::kClamp_Mode, i);
  54. }
  55. planeMatrix.preConcat(localMatrix);
  56. fSamplers[i].reset(std::move(proxies[i]),
  57. GrSamplerState(GrSamplerState::WrapMode::kClamp, filterModes[i]));
  58. fSamplerTransforms[i] = planeMatrix;
  59. fSamplerCoordTransforms[i] =
  60. GrCoordTransform(fSamplerTransforms[i], fSamplers[i].proxy());
  61. }
  62. this->setTextureSamplerCnt(numPlanes);
  63. for (int i = 0; i < numPlanes; ++i) {
  64. this->addCoordTransform(&fSamplerCoordTransforms[i]);
  65. }
  66. memcpy(fYUVAIndices, yuvaIndices, sizeof(fYUVAIndices));
  67. }
  68. GrGLSLFragmentProcessor* onCreateGLSLInstance() const override;
  69. void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override;
  70. bool onIsEqual(const GrFragmentProcessor&) const override;
  71. const TextureSampler& onTextureSampler(int) const override;
  72. GR_DECLARE_FRAGMENT_PROCESSOR_TEST
  73. TextureSampler fSamplers[4];
  74. SkMatrix44 fSamplerTransforms[4];
  75. GrCoordTransform fSamplerCoordTransforms[4];
  76. GrTextureDomain fDomains[4];
  77. SkYUVAIndex fYUVAIndices[4];
  78. SkYUVColorSpace fYUVColorSpace;
  79. typedef GrFragmentProcessor INHERITED;
  80. };
  81. #endif