GrAHardwareBufferImageGenerator.h 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /*
  2. * Copyright 2017 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 GrAHardwareBufferImageGenerator_DEFINED
  8. #define GrAHardwareBufferImageGenerator_DEFINED
  9. #include "include/core/SkImageGenerator.h"
  10. #include "include/private/GrTypesPriv.h"
  11. class GrGpuResource;
  12. extern "C" {
  13. typedef struct AHardwareBuffer AHardwareBuffer;
  14. }
  15. /**
  16. * GrAHardwareBufferImageGenerator allows to create an SkImage attached to
  17. * an existing android native hardware buffer. A hardware buffer has to be
  18. * created with AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE usage, because it is
  19. * bound to an external texture using an EGLImage. The image generator will
  20. * keep a reference to the hardware buffer for its lifetime. A hardware buffer
  21. * can be shared between processes and same buffer can be used in multiple GPU
  22. * contexts.
  23. * To implement certain features like tiling, Skia may copy the texture to
  24. * avoid OpenGL API limitations.
  25. */
  26. class GrAHardwareBufferImageGenerator : public SkImageGenerator {
  27. public:
  28. static std::unique_ptr<SkImageGenerator> Make(AHardwareBuffer*, SkAlphaType,
  29. sk_sp<SkColorSpace>, GrSurfaceOrigin);
  30. ~GrAHardwareBufferImageGenerator() override;
  31. static void DeleteGLTexture(void* ctx);
  32. protected:
  33. bool onIsValid(GrContext*) const override;
  34. TexGenType onCanGenerateTexture() const override { return TexGenType::kCheap; }
  35. sk_sp<GrTextureProxy> onGenerateTexture(GrRecordingContext*, const SkImageInfo&,
  36. const SkIPoint&, bool willNeedMipMaps) override;
  37. private:
  38. GrAHardwareBufferImageGenerator(const SkImageInfo&, AHardwareBuffer*, SkAlphaType,
  39. bool isProtectedContent, uint32_t bufferFormat,
  40. GrSurfaceOrigin surfaceOrigin);
  41. sk_sp<GrTextureProxy> makeProxy(GrRecordingContext* context);
  42. void releaseTextureRef();
  43. static void ReleaseRefHelper_TextureReleaseProc(void* ctx);
  44. AHardwareBuffer* fHardwareBuffer;
  45. uint32_t fBufferFormat;
  46. const bool fIsProtectedContent;
  47. GrSurfaceOrigin fSurfaceOrigin;
  48. typedef SkImageGenerator INHERITED;
  49. };
  50. #endif // GrAHardwareBufferImageGenerator_DEFINED