1e4eec20f6263f4a42ae462456f60ea6c4518bb0aAlex Vakulenko#ifndef ANDROID_DVR_ION_BUFFER_H_
2e4eec20f6263f4a42ae462456f60ea6c4518bb0aAlex Vakulenko#define ANDROID_DVR_ION_BUFFER_H_
3e4eec20f6263f4a42ae462456f60ea6c4518bb0aAlex Vakulenko
4e4eec20f6263f4a42ae462456f60ea6c4518bb0aAlex Vakulenko#include <hardware/gralloc.h>
58a71b13127f737a46c55ba94d31ba0262e7cbf8aMark Urbanus#include <log/log.h>
68a71b13127f737a46c55ba94d31ba0262e7cbf8aMark Urbanus#include <ui/GraphicBuffer.h>
7e4eec20f6263f4a42ae462456f60ea6c4518bb0aAlex Vakulenko
8e4eec20f6263f4a42ae462456f60ea6c4518bb0aAlex Vakulenkonamespace android {
9e4eec20f6263f4a42ae462456f60ea6c4518bb0aAlex Vakulenkonamespace dvr {
10e4eec20f6263f4a42ae462456f60ea6c4518bb0aAlex Vakulenko
11e4eec20f6263f4a42ae462456f60ea6c4518bb0aAlex Vakulenko// IonBuffer is an abstraction of Ion/Gralloc buffers.
12e4eec20f6263f4a42ae462456f60ea6c4518bb0aAlex Vakulenkoclass IonBuffer {
13e4eec20f6263f4a42ae462456f60ea6c4518bb0aAlex Vakulenko public:
14e4eec20f6263f4a42ae462456f60ea6c4518bb0aAlex Vakulenko  IonBuffer();
15044963e1dd0479035b6e5f2c6bd618bde7143710Jiwen 'Steve' Cai  IonBuffer(uint32_t width, uint32_t height, uint32_t format, uint64_t usage);
16cd52dd9f1b301854b4e1734e3741d9cef8f784b1Corey Tabaka  IonBuffer(buffer_handle_t handle, uint32_t width, uint32_t height,
17044963e1dd0479035b6e5f2c6bd618bde7143710Jiwen 'Steve' Cai            uint32_t stride, uint32_t format, uint64_t usage);
18cd52dd9f1b301854b4e1734e3741d9cef8f784b1Corey Tabaka  IonBuffer(buffer_handle_t handle, uint32_t width, uint32_t height,
19108e84f7e6a9117b66dba000dcf16c6d8c862e16Hendrik Wagenaar            uint32_t layer_count, uint32_t stride, uint32_t format,
20108e84f7e6a9117b66dba000dcf16c6d8c862e16Hendrik Wagenaar            uint64_t usage);
21e4eec20f6263f4a42ae462456f60ea6c4518bb0aAlex Vakulenko  ~IonBuffer();
22e4eec20f6263f4a42ae462456f60ea6c4518bb0aAlex Vakulenko
23e4eec20f6263f4a42ae462456f60ea6c4518bb0aAlex Vakulenko  IonBuffer(IonBuffer&& other);
24e4eec20f6263f4a42ae462456f60ea6c4518bb0aAlex Vakulenko  IonBuffer& operator=(IonBuffer&& other);
25e4eec20f6263f4a42ae462456f60ea6c4518bb0aAlex Vakulenko
26e4eec20f6263f4a42ae462456f60ea6c4518bb0aAlex Vakulenko  // Frees the underlying native handle and leaves the instance initialized to
27e4eec20f6263f4a42ae462456f60ea6c4518bb0aAlex Vakulenko  // empty.
28e4eec20f6263f4a42ae462456f60ea6c4518bb0aAlex Vakulenko  void FreeHandle();
29e4eec20f6263f4a42ae462456f60ea6c4518bb0aAlex Vakulenko
30e4eec20f6263f4a42ae462456f60ea6c4518bb0aAlex Vakulenko  // Allocates a new native handle with the given parameters, freeing the
31e4eec20f6263f4a42ae462456f60ea6c4518bb0aAlex Vakulenko  // previous native handle if necessary. Returns 0 on success or a negative
32e4eec20f6263f4a42ae462456f60ea6c4518bb0aAlex Vakulenko  // errno code otherwise. If allocation fails the previous native handle is
33e4eec20f6263f4a42ae462456f60ea6c4518bb0aAlex Vakulenko  // left intact.
34108e84f7e6a9117b66dba000dcf16c6d8c862e16Hendrik Wagenaar  int Alloc(uint32_t width, uint32_t height, uint32_t layer_count,
35108e84f7e6a9117b66dba000dcf16c6d8c862e16Hendrik Wagenaar            uint32_t format, uint64_t usage);
36e4eec20f6263f4a42ae462456f60ea6c4518bb0aAlex Vakulenko
37e4eec20f6263f4a42ae462456f60ea6c4518bb0aAlex Vakulenko  // Resets the underlying native handle and parameters, freeing the previous
38e4eec20f6263f4a42ae462456f60ea6c4518bb0aAlex Vakulenko  // native handle if necessary.
39cd52dd9f1b301854b4e1734e3741d9cef8f784b1Corey Tabaka  void Reset(buffer_handle_t handle, uint32_t width, uint32_t height,
40108e84f7e6a9117b66dba000dcf16c6d8c862e16Hendrik Wagenaar             uint32_t layer_count, uint32_t stride, uint32_t format,
41108e84f7e6a9117b66dba000dcf16c6d8c862e16Hendrik Wagenaar             uint64_t usage);
42e4eec20f6263f4a42ae462456f60ea6c4518bb0aAlex Vakulenko
43e4eec20f6263f4a42ae462456f60ea6c4518bb0aAlex Vakulenko  // Like Reset but also registers the native handle, which is necessary for
44e4eec20f6263f4a42ae462456f60ea6c4518bb0aAlex Vakulenko  // native handles received over IPC. Returns 0 on success or a negative errno
45e4eec20f6263f4a42ae462456f60ea6c4518bb0aAlex Vakulenko  // code otherwise. If import fails the previous native handle is left intact.
46cd52dd9f1b301854b4e1734e3741d9cef8f784b1Corey Tabaka  int Import(buffer_handle_t handle, uint32_t width, uint32_t height,
47108e84f7e6a9117b66dba000dcf16c6d8c862e16Hendrik Wagenaar             uint32_t layer_count, uint32_t stride, uint32_t format,
48108e84f7e6a9117b66dba000dcf16c6d8c862e16Hendrik Wagenaar             uint64_t usage);
49e4eec20f6263f4a42ae462456f60ea6c4518bb0aAlex Vakulenko
50e4eec20f6263f4a42ae462456f60ea6c4518bb0aAlex Vakulenko  // Like Reset but imports a native handle from raw fd and int arrays. Returns
51e4eec20f6263f4a42ae462456f60ea6c4518bb0aAlex Vakulenko  // 0 on success or a negative errno code otherwise. If import fails the
52e4eec20f6263f4a42ae462456f60ea6c4518bb0aAlex Vakulenko  // previous native handle is left intact.
53e4eec20f6263f4a42ae462456f60ea6c4518bb0aAlex Vakulenko  int Import(const int* fd_array, int fd_count, const int* int_array,
54108e84f7e6a9117b66dba000dcf16c6d8c862e16Hendrik Wagenaar             int int_count, uint32_t width, uint32_t height,
55108e84f7e6a9117b66dba000dcf16c6d8c862e16Hendrik Wagenaar             uint32_t layer_count, uint32_t stride, uint32_t format,
56108e84f7e6a9117b66dba000dcf16c6d8c862e16Hendrik Wagenaar             uint64_t usage);
57e4eec20f6263f4a42ae462456f60ea6c4518bb0aAlex Vakulenko
58e4eec20f6263f4a42ae462456f60ea6c4518bb0aAlex Vakulenko  // Duplicates the native handle underlying |other| and then imports it. This
59e4eec20f6263f4a42ae462456f60ea6c4518bb0aAlex Vakulenko  // is useful for creating multiple, independent views of the same Ion/Gralloc
60e4eec20f6263f4a42ae462456f60ea6c4518bb0aAlex Vakulenko  // buffer. Returns 0 on success or a negative errno code otherwise. If
61e4eec20f6263f4a42ae462456f60ea6c4518bb0aAlex Vakulenko  // duplication or import fail the previous native handle is left intact.
62e4eec20f6263f4a42ae462456f60ea6c4518bb0aAlex Vakulenko  int Duplicate(const IonBuffer* other);
63e4eec20f6263f4a42ae462456f60ea6c4518bb0aAlex Vakulenko
64cd52dd9f1b301854b4e1734e3741d9cef8f784b1Corey Tabaka  int Lock(uint32_t usage, int x, int y, int width, int height, void** address);
65cd52dd9f1b301854b4e1734e3741d9cef8f784b1Corey Tabaka  int LockYUV(uint32_t usage, int x, int y, int width, int height,
66e4eec20f6263f4a42ae462456f60ea6c4518bb0aAlex Vakulenko              struct android_ycbcr* yuv);
67e4eec20f6263f4a42ae462456f60ea6c4518bb0aAlex Vakulenko  int Unlock();
682655e1cd5498749381701aa1fa7f29c66364a7efCorey Tabaka
692655e1cd5498749381701aa1fa7f29c66364a7efCorey Tabaka  const sp<GraphicBuffer>& buffer() const { return buffer_; }
702655e1cd5498749381701aa1fa7f29c66364a7efCorey Tabaka  buffer_handle_t handle() const {
712655e1cd5498749381701aa1fa7f29c66364a7efCorey Tabaka    return buffer_.get() ? buffer_->handle : nullptr;
722655e1cd5498749381701aa1fa7f29c66364a7efCorey Tabaka  }
73cd52dd9f1b301854b4e1734e3741d9cef8f784b1Corey Tabaka  uint32_t width() const { return buffer_.get() ? buffer_->getWidth() : 0; }
74cd52dd9f1b301854b4e1734e3741d9cef8f784b1Corey Tabaka  uint32_t height() const { return buffer_.get() ? buffer_->getHeight() : 0; }
75cd52dd9f1b301854b4e1734e3741d9cef8f784b1Corey Tabaka  uint32_t layer_count() const {
762655e1cd5498749381701aa1fa7f29c66364a7efCorey Tabaka    return buffer_.get() ? buffer_->getLayerCount() : 0;
772655e1cd5498749381701aa1fa7f29c66364a7efCorey Tabaka  }
78cd52dd9f1b301854b4e1734e3741d9cef8f784b1Corey Tabaka  uint32_t stride() const { return buffer_.get() ? buffer_->getStride() : 0; }
79cd52dd9f1b301854b4e1734e3741d9cef8f784b1Corey Tabaka  uint32_t format() const {
80cd52dd9f1b301854b4e1734e3741d9cef8f784b1Corey Tabaka    return buffer_.get() ? buffer_->getPixelFormat() : 0;
81cd52dd9f1b301854b4e1734e3741d9cef8f784b1Corey Tabaka  }
82044963e1dd0479035b6e5f2c6bd618bde7143710Jiwen 'Steve' Cai  uint64_t usage() const {
83044963e1dd0479035b6e5f2c6bd618bde7143710Jiwen 'Steve' Cai    return buffer_.get() ? static_cast<uint64_t>(buffer_->getUsage()) : 0;
84044963e1dd0479035b6e5f2c6bd618bde7143710Jiwen 'Steve' Cai  }
85e4eec20f6263f4a42ae462456f60ea6c4518bb0aAlex Vakulenko
86e4eec20f6263f4a42ae462456f60ea6c4518bb0aAlex Vakulenko private:
878a71b13127f737a46c55ba94d31ba0262e7cbf8aMark Urbanus  sp<GraphicBuffer> buffer_;
88e4eec20f6263f4a42ae462456f60ea6c4518bb0aAlex Vakulenko
89e4eec20f6263f4a42ae462456f60ea6c4518bb0aAlex Vakulenko  IonBuffer(const IonBuffer&) = delete;
90e4eec20f6263f4a42ae462456f60ea6c4518bb0aAlex Vakulenko  void operator=(const IonBuffer&) = delete;
91e4eec20f6263f4a42ae462456f60ea6c4518bb0aAlex Vakulenko};
92e4eec20f6263f4a42ae462456f60ea6c4518bb0aAlex Vakulenko
93e4eec20f6263f4a42ae462456f60ea6c4518bb0aAlex Vakulenko}  // namespace dvr
94e4eec20f6263f4a42ae462456f60ea6c4518bb0aAlex Vakulenko}  // namespace android
95e4eec20f6263f4a42ae462456f60ea6c4518bb0aAlex Vakulenko
96e4eec20f6263f4a42ae462456f60ea6c4518bb0aAlex Vakulenko#endif  // ANDROID_DVR_ION_BUFFER_H_
97