blob: 1eefe1137ca681f0117505647db9542b22c24d70 [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:
Stan Ilievc01b5c72018-08-28 10:18:19 -040050 GrAHardwareBufferImageGenerator(const SkImageInfo&, AHardwareBuffer*, SkAlphaType,
Stan Iliev114b0912018-08-31 14:02:55 -040051 bool isProtectedContent, uint32_t bufferFormat);
Greg Danielf1251112018-08-27 09:55:03 -040052 void makeProxy(GrContext* context);
Stan Iliev7e910df2017-06-02 10:29:21 -040053
Greg Danielf1251112018-08-27 09:55:03 -040054 void releaseTextureRef();
55
56 static void ReleaseRefHelper_TextureReleaseProc(void* ctx);
57
58 AHardwareBuffer* fHardwareBuffer;
59
60 // There is never a ref associated with this pointer. We rely on our atomic bookkeeping
61 // with the context ID to know when this pointer is valid and safe to use. This lets us
62 // avoid releasing a ref from another thread, or get into races during context shutdown.
63 GrTexture* fOwnedTexture = nullptr;
64 uint32_t fOwningContextID = SK_InvalidGenID;
Stan Iliev114b0912018-08-31 14:02:55 -040065 uint32_t fBufferFormat;
Greg Danielf1251112018-08-27 09:55:03 -040066
67 sk_sp<GrTextureProxy> fCachedProxy;
Stan Ilievc01b5c72018-08-28 10:18:19 -040068 const bool fIsProtectedContent;
Stan Iliev7e910df2017-06-02 10:29:21 -040069
70 typedef SkImageGenerator INHERITED;
71};
72#endif // GrAHardwareBufferImageGenerator_DEFINED