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 "include/core/SkImageGenerator.h" 11 12 #include "include/private/gpu/ganesh/GrTypesPriv.h" 13 14 class GrGpuResource; 15 class GrSurfaceProxyView; 16 17 extern "C" { 18 typedef struct AHardwareBuffer AHardwareBuffer; 19 } 20 21 /** 22 * GrAHardwareBufferImageGenerator allows to create an SkImage attached to 23 * an existing android native hardware buffer. A hardware buffer has to be 24 * created with AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE usage, because it is 25 * bound to an external texture using an EGLImage. The image generator will 26 * keep a reference to the hardware buffer for its lifetime. A hardware buffer 27 * can be shared between processes and same buffer can be used in multiple GPU 28 * contexts. 29 * To implement certain features like tiling, Skia may copy the texture to 30 * avoid OpenGL API limitations. 31 */ 32 class GrAHardwareBufferImageGenerator : public SkImageGenerator { 33 public: 34 static std::unique_ptr<SkImageGenerator> Make(AHardwareBuffer*, SkAlphaType, 35 sk_sp<SkColorSpace>, GrSurfaceOrigin); 36 37 ~GrAHardwareBufferImageGenerator() override; 38 39 static void DeleteGLTexture(void* ctx); 40 41 private: 42 GrAHardwareBufferImageGenerator(const SkImageInfo&, AHardwareBuffer*, SkAlphaType, 43 bool isProtectedContent, uint32_t bufferFormat, 44 GrSurfaceOrigin surfaceOrigin); 45 46 bool onIsValid(GrRecordingContext*) const override; 47 48 GrSurfaceProxyView onGenerateTexture(GrRecordingContext*, const SkImageInfo&, 49 GrMipmapped, GrImageTexGenPolicy) override; 50 origin()51 GrSurfaceOrigin origin() const override { return fSurfaceOrigin; } 52 53 GrSurfaceProxyView makeView(GrRecordingContext* context); 54 55 void releaseTextureRef(); 56 57 static void ReleaseRefHelper_TextureReleaseProc(void* ctx); 58 59 AHardwareBuffer* fHardwareBuffer; 60 uint32_t fBufferFormat; 61 const bool fIsProtectedContent; 62 GrSurfaceOrigin fSurfaceOrigin; 63 64 using INHERITED = SkImageGenerator; 65 }; 66 #endif // GrAHardwareBufferImageGenerator_DEFINED 67