• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
12 extern "C" {
13     typedef struct AHardwareBuffer AHardwareBuffer;
14 }
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
onCanGenerateTexture()39     TexGenType onCanGenerateTexture() const override { return TexGenType::kCheap; }
40     sk_sp<GrTextureProxy> onGenerateTexture(GrContext*, const SkImageInfo&, const SkIPoint&,
41                                             SkTransferFunctionBehavior,
42                                             bool willNeedMipMaps) override;
43 #endif
44 
45 private:
46     GrAHardwareBufferImageGenerator(const SkImageInfo&, AHardwareBuffer*, SkAlphaType);
47     sk_sp<GrTextureProxy> makeProxy(GrContext* context);
48     void clear();
49 
50     static void deleteImageTexture(void* ctx);
51 
52     AHardwareBuffer* fGraphicBuffer;
53     GrTexture* fOriginalTexture = nullptr;
54     uint32_t fOwningContextID;
55 
56     typedef SkImageGenerator INHERITED;
57 };
58 #endif  // GrAHardwareBufferImageGenerator_DEFINED
59