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 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 protected:
42 
43     bool onIsValid(GrRecordingContext*) const override;
44 
45     GrSurfaceProxyView onGenerateTexture(GrRecordingContext*, const SkImageInfo&, const SkIPoint&,
46                                          GrMipmapped, GrImageTexGenPolicy) override;
47 
48 private:
49     GrAHardwareBufferImageGenerator(const SkImageInfo&, AHardwareBuffer*, SkAlphaType,
50                                     bool isProtectedContent, uint32_t bufferFormat,
51                                     GrSurfaceOrigin surfaceOrigin);
52     GrSurfaceProxyView makeView(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     using INHERITED = SkImageGenerator;
64 };
65 #endif  // GrAHardwareBufferImageGenerator_DEFINED
66