• 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 struct AHardwareBuffer;
13 
14 /**
15  *  GrAHardwareBufferImageGenerator allows to create an SkImage attached to
16  *  an existing android native hardware buffer. A hardware buffer has to be
17  *  created with AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE usage, because it is
18  *  bound to an external texture using an EGLImage. The image generator will
19  *  keep a reference to the hardware buffer for its lifetime. A hardware buffer
20  *  can be shared between processes and same buffer can be used in multiple GPU
21  *  contexts.
22  *  To implement certain features like tiling, Skia may copy the texture to
23  *  avoid OpenGL API limitations.
24  */
25 class GrAHardwareBufferImageGenerator : public SkImageGenerator {
26 public:
27     static std::unique_ptr<SkImageGenerator> Make(AHardwareBuffer*, SkAlphaType,
28                                                   sk_sp<SkColorSpace>);
29 
30     ~GrAHardwareBufferImageGenerator() override;
31 
32 protected:
33 
34     bool onIsValid(GrContext*) const override;
35 
36 #if SK_SUPPORT_GPU
onCanGenerateTexture()37     TexGenType onCanGenerateTexture() const override { return TexGenType::kCheap; }
38     sk_sp<GrTextureProxy> onGenerateTexture(GrContext*, const SkImageInfo&, const SkIPoint&,
39                                             SkTransferFunctionBehavior) override;
40 #endif
41 
42 private:
43     GrAHardwareBufferImageGenerator(const SkImageInfo&, AHardwareBuffer*, SkAlphaType);
44     sk_sp<GrTextureProxy> makeProxy(GrContext* context);
45     void clear();
46 
47     static void deleteImageTexture(void* ctx);
48 
49     AHardwareBuffer* fGraphicBuffer;
50     GrTexture* fOriginalTexture = nullptr;
51     uint32_t fOwningContextID;
52 
53     typedef SkImageGenerator INHERITED;
54 };
55 #endif  // GrAHardwareBufferImageGenerator_DEFINED
56