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 | |
Derek Sollenberger | 1bd76c4 | 2017-10-16 11:57:59 -0400 | [diff] [blame] | 14 | extern "C" { |
| 15 | typedef struct AHardwareBuffer AHardwareBuffer; |
| 16 | } |
Stan Iliev | 7e910df | 2017-06-02 10:29:21 -0400 | [diff] [blame] | 17 | |
| 18 | /** |
| 19 | * GrAHardwareBufferImageGenerator allows to create an SkImage attached to |
| 20 | * an existing android native hardware buffer. A hardware buffer has to be |
| 21 | * created with AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE usage, because it is |
| 22 | * bound to an external texture using an EGLImage. The image generator will |
| 23 | * keep a reference to the hardware buffer for its lifetime. A hardware buffer |
| 24 | * can be shared between processes and same buffer can be used in multiple GPU |
| 25 | * contexts. |
| 26 | * To implement certain features like tiling, Skia may copy the texture to |
| 27 | * avoid OpenGL API limitations. |
| 28 | */ |
| 29 | class GrAHardwareBufferImageGenerator : public SkImageGenerator { |
| 30 | public: |
| 31 | static std::unique_ptr<SkImageGenerator> Make(AHardwareBuffer*, SkAlphaType, |
| 32 | sk_sp<SkColorSpace>); |
| 33 | |
| 34 | ~GrAHardwareBufferImageGenerator() override; |
| 35 | |
Greg Daniel | 9af948d | 2018-08-27 09:53:51 -0400 | [diff] [blame] | 36 | typedef void* DeleteImageCtx; |
| 37 | typedef void (*DeleteImageProc)(DeleteImageCtx); |
| 38 | |
| 39 | static void DeleteEGLImage(void* ctx); |
| 40 | |
Stan Iliev | 7e910df | 2017-06-02 10:29:21 -0400 | [diff] [blame] | 41 | protected: |
| 42 | |
| 43 | bool onIsValid(GrContext*) const override; |
| 44 | |
Stan Iliev | ba81af2 | 2017-06-08 15:16:53 -0400 | [diff] [blame] | 45 | TexGenType onCanGenerateTexture() const override { return TexGenType::kCheap; } |
Christopher Cameron | 77e9666 | 2017-07-08 01:47:47 -0700 | [diff] [blame] | 46 | sk_sp<GrTextureProxy> onGenerateTexture(GrContext*, const SkImageInfo&, const SkIPoint&, |
Greg Daniel | f88c12e | 2017-10-09 09:57:35 -0400 | [diff] [blame] | 47 | bool willNeedMipMaps) override; |
Stan Iliev | 7e910df | 2017-06-02 10:29:21 -0400 | [diff] [blame] | 48 | |
| 49 | private: |
| 50 | GrAHardwareBufferImageGenerator(const SkImageInfo&, AHardwareBuffer*, SkAlphaType); |
Greg Daniel | f125111 | 2018-08-27 09:55:03 -0400 | [diff] [blame] | 51 | void makeProxy(GrContext* context); |
Stan Iliev | 7e910df | 2017-06-02 10:29:21 -0400 | [diff] [blame] | 52 | |
Greg Daniel | f125111 | 2018-08-27 09:55:03 -0400 | [diff] [blame] | 53 | void releaseTextureRef(); |
| 54 | |
| 55 | static void ReleaseRefHelper_TextureReleaseProc(void* ctx); |
| 56 | |
| 57 | AHardwareBuffer* fHardwareBuffer; |
| 58 | |
| 59 | // There is never a ref associated with this pointer. We rely on our atomic bookkeeping |
| 60 | // with the context ID to know when this pointer is valid and safe to use. This lets us |
| 61 | // avoid releasing a ref from another thread, or get into races during context shutdown. |
| 62 | GrTexture* fOwnedTexture = nullptr; |
| 63 | uint32_t fOwningContextID = SK_InvalidGenID; |
| 64 | |
| 65 | sk_sp<GrTextureProxy> fCachedProxy; |
Stan Iliev | 7e910df | 2017-06-02 10:29:21 -0400 | [diff] [blame] | 66 | |
| 67 | typedef SkImageGenerator INHERITED; |
| 68 | }; |
| 69 | #endif // GrAHardwareBufferImageGenerator_DEFINED |