blob: 58b5abfcd02d249ddebfa78a1b56aab02df1fb4f [file] [log] [blame]
Stan Iliev7e910df2017-06-02 10:29:21 -04001/*
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
Greg Danielf1251112018-08-27 09:55:03 -040012#include "GrTypesPriv.h"
13
Derek Sollenberger1bd76c42017-10-16 11:57:59 -040014extern "C" {
15 typedef struct AHardwareBuffer AHardwareBuffer;
16}
Stan Iliev7e910df2017-06-02 10:29:21 -040017
18/**
19 * GrAHardwareBufferImageGenerator allows to create an SkImage attached to
20 * an existing android native hardware buffer. A hardware buffer has to be
21 * created with AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE usage, because it is
22 * bound to an external texture using an EGLImage. The image generator will
23 * keep a reference to the hardware buffer for its lifetime. A hardware buffer
24 * can be shared between processes and same buffer can be used in multiple GPU
25 * contexts.
26 * To implement certain features like tiling, Skia may copy the texture to
27 * avoid OpenGL API limitations.
28 */
29class GrAHardwareBufferImageGenerator : public SkImageGenerator {
30public:
31 static std::unique_ptr<SkImageGenerator> Make(AHardwareBuffer*, SkAlphaType,
32 sk_sp<SkColorSpace>);
33
34 ~GrAHardwareBufferImageGenerator() override;
35
Greg Daniel9af948d2018-08-27 09:53:51 -040036 typedef void* DeleteImageCtx;
37 typedef void (*DeleteImageProc)(DeleteImageCtx);
38
39 static void DeleteEGLImage(void* ctx);
40
Stan Iliev7e910df2017-06-02 10:29:21 -040041protected:
42
43 bool onIsValid(GrContext*) const override;
44
Stan Ilievba81af22017-06-08 15:16:53 -040045 TexGenType onCanGenerateTexture() const override { return TexGenType::kCheap; }
Christopher Cameron77e96662017-07-08 01:47:47 -070046 sk_sp<GrTextureProxy> onGenerateTexture(GrContext*, const SkImageInfo&, const SkIPoint&,
Greg Danielf88c12e2017-10-09 09:57:35 -040047 bool willNeedMipMaps) override;
Stan Iliev7e910df2017-06-02 10:29:21 -040048
49private:
50 GrAHardwareBufferImageGenerator(const SkImageInfo&, AHardwareBuffer*, SkAlphaType);
Greg Danielf1251112018-08-27 09:55:03 -040051 void makeProxy(GrContext* context);
Stan Iliev7e910df2017-06-02 10:29:21 -040052
Greg Danielf1251112018-08-27 09:55:03 -040053 void releaseTextureRef();
54
55 static void ReleaseRefHelper_TextureReleaseProc(void* ctx);
56
57 AHardwareBuffer* fHardwareBuffer;
58
59 // There is never a ref associated with this pointer. We rely on our atomic bookkeeping
60 // with the context ID to know when this pointer is valid and safe to use. This lets us
61 // avoid releasing a ref from another thread, or get into races during context shutdown.
62 GrTexture* fOwnedTexture = nullptr;
63 uint32_t fOwningContextID = SK_InvalidGenID;
64
65 sk_sp<GrTextureProxy> fCachedProxy;
Stan Iliev7e910df2017-06-02 10:29:21 -040066
67 typedef SkImageGenerator INHERITED;
68};
69#endif // GrAHardwareBufferImageGenerator_DEFINED