Stan Iliev | 7e910df | 2017-06-02 10:29:21 -0400 | [diff] [blame] | 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 | |
| 10 | #include "SkImageGenerator.h" |
| 11 | |
Greg Daniel | f125111 | 2018-08-27 09:55:03 -0400 | [diff] [blame] | 12 | #include "GrTypesPriv.h" |
| 13 | |
Greg Daniel | 469dd65 | 2018-09-05 13:27:27 -0400 | [diff] [blame] | 14 | class GrGpuResource; |
| 15 | |
Derek Sollenberger | 1bd76c4 | 2017-10-16 11:57:59 -0400 | [diff] [blame] | 16 | extern "C" { |
| 17 | typedef struct AHardwareBuffer AHardwareBuffer; |
| 18 | } |
Stan Iliev | 7e910df | 2017-06-02 10:29:21 -0400 | [diff] [blame] | 19 | |
| 20 | /** |
| 21 | * GrAHardwareBufferImageGenerator allows to create an SkImage attached to |
| 22 | * an existing android native hardware buffer. A hardware buffer has to be |
| 23 | * created with AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE usage, because it is |
| 24 | * bound to an external texture using an EGLImage. The image generator will |
| 25 | * keep a reference to the hardware buffer for its lifetime. A hardware buffer |
| 26 | * can be shared between processes and same buffer can be used in multiple GPU |
| 27 | * contexts. |
| 28 | * To implement certain features like tiling, Skia may copy the texture to |
| 29 | * avoid OpenGL API limitations. |
| 30 | */ |
| 31 | class GrAHardwareBufferImageGenerator : public SkImageGenerator { |
| 32 | public: |
| 33 | static std::unique_ptr<SkImageGenerator> Make(AHardwareBuffer*, SkAlphaType, |
Stan Iliev | 505dd57 | 2018-09-13 14:20:03 -0400 | [diff] [blame] | 34 | sk_sp<SkColorSpace>, GrSurfaceOrigin); |
Stan Iliev | 7e910df | 2017-06-02 10:29:21 -0400 | [diff] [blame] | 35 | |
| 36 | ~GrAHardwareBufferImageGenerator() override; |
| 37 | |
Greg Daniel | 3860cfd1 | 2018-09-07 09:13:54 -0400 | [diff] [blame] | 38 | static void DeleteGLTexture(void* ctx); |
Greg Daniel | 9af948d | 2018-08-27 09:53:51 -0400 | [diff] [blame] | 39 | |
Stan Iliev | 7e910df | 2017-06-02 10:29:21 -0400 | [diff] [blame] | 40 | protected: |
| 41 | |
| 42 | bool onIsValid(GrContext*) const override; |
| 43 | |
Stan Iliev | ba81af2 | 2017-06-08 15:16:53 -0400 | [diff] [blame] | 44 | TexGenType onCanGenerateTexture() const override { return TexGenType::kCheap; } |
Christopher Cameron | 77e9666 | 2017-07-08 01:47:47 -0700 | [diff] [blame] | 45 | sk_sp<GrTextureProxy> onGenerateTexture(GrContext*, const SkImageInfo&, const SkIPoint&, |
Greg Daniel | f88c12e | 2017-10-09 09:57:35 -0400 | [diff] [blame] | 46 | bool willNeedMipMaps) override; |
Stan Iliev | 7e910df | 2017-06-02 10:29:21 -0400 | [diff] [blame] | 47 | |
| 48 | private: |
Stan Iliev | c01b5c7 | 2018-08-28 10:18:19 -0400 | [diff] [blame] | 49 | GrAHardwareBufferImageGenerator(const SkImageInfo&, AHardwareBuffer*, SkAlphaType, |
Stan Iliev | 505dd57 | 2018-09-13 14:20:03 -0400 | [diff] [blame] | 50 | bool isProtectedContent, uint32_t bufferFormat, |
| 51 | GrSurfaceOrigin surfaceOrigin); |
Greg Daniel | 3860cfd1 | 2018-09-07 09:13:54 -0400 | [diff] [blame] | 52 | sk_sp<GrTextureProxy> makeProxy(GrContext* context); |
Stan Iliev | 7e910df | 2017-06-02 10:29:21 -0400 | [diff] [blame] | 53 | |
Greg Daniel | f125111 | 2018-08-27 09:55:03 -0400 | [diff] [blame] | 54 | void releaseTextureRef(); |
| 55 | |
| 56 | static void ReleaseRefHelper_TextureReleaseProc(void* ctx); |
| 57 | |
| 58 | AHardwareBuffer* fHardwareBuffer; |
Greg Daniel | 3860cfd1 | 2018-09-07 09:13:54 -0400 | [diff] [blame] | 59 | uint32_t fBufferFormat; |
| 60 | const bool fIsProtectedContent; |
Stan Iliev | 505dd57 | 2018-09-13 14:20:03 -0400 | [diff] [blame] | 61 | GrSurfaceOrigin fSurfaceOrigin; |
Stan Iliev | 7e910df | 2017-06-02 10:29:21 -0400 | [diff] [blame] | 62 | |
| 63 | typedef SkImageGenerator INHERITED; |
| 64 | }; |
| 65 | #endif // GrAHardwareBufferImageGenerator_DEFINED |