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/GrTypesPriv.h"
13 
14 class GrGpuResource;
15 
16 extern "C" {
17     typedef struct AHardwareBuffer AHardwareBuffer;
18 }
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,
34                                                   sk_sp<SkColorSpace>, GrSurfaceOrigin);
35 
36     ~GrAHardwareBufferImageGenerator() override;
37 
38     static void DeleteGLTexture(void* ctx);
39 
40 protected:
41 
42     bool onIsValid(GrContext*) const override;
43 
onCanGenerateTexture()44     TexGenType onCanGenerateTexture() const override { return TexGenType::kCheap; }
45     sk_sp<GrTextureProxy> onGenerateTexture(GrRecordingContext*, const SkImageInfo&,
46                                             const SkIPoint&, bool willNeedMipMaps) override;
47 
48 private:
49     GrAHardwareBufferImageGenerator(const SkImageInfo&, AHardwareBuffer*, SkAlphaType,
50                                     bool isProtectedContent, uint32_t bufferFormat,
51                                     GrSurfaceOrigin surfaceOrigin);
52     sk_sp<GrTextureProxy> makeProxy(GrRecordingContext* context);
53 
54     void releaseTextureRef();
55 
56     static void ReleaseRefHelper_TextureReleaseProc(void* ctx);
57 
58     AHardwareBuffer* fHardwareBuffer;
59     uint32_t         fBufferFormat;
60     const bool       fIsProtectedContent;
61     GrSurfaceOrigin  fSurfaceOrigin;
62 
63     typedef SkImageGenerator INHERITED;
64 };
65 #endif  // GrAHardwareBufferImageGenerator_DEFINED
66