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