SkImage_GpuYUVA.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 SkImage_GpuYUVA_DEFINED
  8. #define SkImage_GpuYUVA_DEFINED
  9. #include "include/gpu/GrBackendSurface.h"
  10. #include "include/gpu/GrContext.h"
  11. #include "src/core/SkCachedData.h"
  12. #include "src/image/SkImage_GpuBase.h"
  13. class GrTexture;
  14. struct SkYUVASizeInfo;
  15. // Wraps the 3 or 4 planes of a YUVA image for consumption by the GPU.
  16. // Initially any direct rendering will be done by passing the individual planes to a shader.
  17. // Once any method requests a flattened image (e.g., onReadPixels), the flattened RGB
  18. // proxy will be stored and used for any future rendering.
  19. class SkImage_GpuYUVA : public SkImage_GpuBase {
  20. public:
  21. friend class GrYUVAImageTextureMaker;
  22. SkImage_GpuYUVA(sk_sp<GrContext>, int width, int height, uint32_t uniqueID, SkYUVColorSpace,
  23. sk_sp<GrTextureProxy> proxies[], int numProxies, const SkYUVAIndex[4],
  24. GrSurfaceOrigin, sk_sp<SkColorSpace>);
  25. ~SkImage_GpuYUVA() override;
  26. GrSemaphoresSubmitted onFlush(GrContext*, const GrFlushInfo&) override;
  27. // This returns the single backing proxy if the YUV channels have already been flattened but
  28. // nullptr if they have not.
  29. GrTextureProxy* peekProxy() const override;
  30. sk_sp<GrTextureProxy> asTextureProxyRef(GrRecordingContext*) const override;
  31. virtual bool onIsTextureBacked() const override { return fProxies[0] || fRGBProxy; }
  32. sk_sp<SkImage> onMakeColorTypeAndColorSpace(GrRecordingContext*,
  33. SkColorType, sk_sp<SkColorSpace>) const final;
  34. virtual bool isYUVA() const override { return true; }
  35. bool setupMipmapsForPlanes(GrRecordingContext*) const;
  36. // Returns a ref-ed texture proxy with miplevels
  37. sk_sp<GrTextureProxy> asMippedTextureProxyRef(GrRecordingContext*) const;
  38. #if GR_TEST_UTILS
  39. bool testingOnly_IsFlattened() const {
  40. // We should only have the flattened proxy or the planar proxies at one point in time.
  41. SkASSERT(SkToBool(fRGBProxy) != SkToBool(fProxies[0]));
  42. return SkToBool(fRGBProxy);
  43. }
  44. #endif
  45. /**
  46. * This is the implementation of SkDeferredDisplayListRecorder::makeYUVAPromiseTexture.
  47. */
  48. static sk_sp<SkImage> MakePromiseYUVATexture(GrContext* context,
  49. SkYUVColorSpace yuvColorSpace,
  50. const GrBackendFormat yuvaFormats[],
  51. const SkISize yuvaSizes[],
  52. const SkYUVAIndex yuvaIndices[4],
  53. int width,
  54. int height,
  55. GrSurfaceOrigin imageOrigin,
  56. sk_sp<SkColorSpace> imageColorSpace,
  57. PromiseImageTextureFulfillProc textureFulfillProc,
  58. PromiseImageTextureReleaseProc textureReleaseProc,
  59. PromiseImageTextureDoneProc textureDoneProc,
  60. PromiseImageTextureContext textureContexts[],
  61. PromiseImageApiVersion);
  62. private:
  63. SkImage_GpuYUVA(const SkImage_GpuYUVA* image, sk_sp<SkColorSpace>);
  64. // This array will usually only be sparsely populated.
  65. // The actual non-null fields are dictated by the 'fYUVAIndices' indices
  66. mutable sk_sp<GrTextureProxy> fProxies[4];
  67. int fNumProxies;
  68. SkYUVAIndex fYUVAIndices[4];
  69. const SkYUVColorSpace fYUVColorSpace;
  70. GrSurfaceOrigin fOrigin;
  71. // If this is non-null then the planar data should be converted from fFromColorSpace to
  72. // this->colorSpace(). Otherwise we assume the planar data (post YUV->RGB conversion) is already
  73. // in this->colorSpace().
  74. const sk_sp<SkColorSpace> fFromColorSpace;
  75. // Repeated calls to onMakeColorSpace will result in a proliferation of unique IDs and
  76. // SkImage_GpuYUVA instances. Cache the result of the last successful onMakeColorSpace call.
  77. mutable sk_sp<SkColorSpace> fOnMakeColorSpaceTarget;
  78. mutable sk_sp<SkImage> fOnMakeColorSpaceResult;
  79. // This is only allocated when the image needs to be flattened rather than
  80. // using the separate YUVA planes. From thence forth we will only use the
  81. // the RGBProxy.
  82. mutable sk_sp<GrTextureProxy> fRGBProxy;
  83. typedef SkImage_GpuBase INHERITED;
  84. };
  85. #endif