ion_buffer.h revision 044963e1dd0479035b6e5f2c6bd618bde7143710
1#ifndef ANDROID_DVR_ION_BUFFER_H_
2#define ANDROID_DVR_ION_BUFFER_H_
3
4#include <hardware/gralloc.h>
5#include <log/log.h>
6#include <ui/GraphicBuffer.h>
7
8namespace android {
9namespace dvr {
10
11// IonBuffer is an abstraction of Ion/Gralloc buffers.
12class IonBuffer {
13 public:
14  IonBuffer();
15  IonBuffer(uint32_t width, uint32_t height, uint32_t format, uint64_t usage);
16  IonBuffer(buffer_handle_t handle, uint32_t width, uint32_t height,
17            uint32_t stride, uint32_t format, uint64_t usage);
18  IonBuffer(buffer_handle_t handle, uint32_t width, uint32_t height,
19            uint32_t layer_count, uint32_t stride, uint32_t layer_stride,
20            uint32_t format, uint64_t usage);
21  ~IonBuffer();
22
23  IonBuffer(IonBuffer&& other);
24  IonBuffer& operator=(IonBuffer&& other);
25
26  // Frees the underlying native handle and leaves the instance initialized to
27  // empty.
28  void FreeHandle();
29
30  // Allocates a new native handle with the given parameters, freeing the
31  // previous native handle if necessary. Returns 0 on success or a negative
32  // errno code otherwise. If allocation fails the previous native handle is
33  // left intact.
34  int Alloc(uint32_t width, uint32_t height, uint32_t format, uint64_t usage);
35
36  // Resets the underlying native handle and parameters, freeing the previous
37  // native handle if necessary.
38  void Reset(buffer_handle_t handle, uint32_t width, uint32_t height,
39             uint32_t stride, uint32_t format, uint64_t usage);
40
41  // Like Reset but also registers the native handle, which is necessary for
42  // native handles received over IPC. Returns 0 on success or a negative errno
43  // code otherwise. If import fails the previous native handle is left intact.
44  int Import(buffer_handle_t handle, uint32_t width, uint32_t height,
45             uint32_t stride, uint32_t format, uint64_t usage);
46
47  // Like Reset but imports a native handle from raw fd and int arrays. Returns
48  // 0 on success or a negative errno code otherwise. If import fails the
49  // previous native handle is left intact.
50  int Import(const int* fd_array, int fd_count, const int* int_array,
51             int int_count, uint32_t width, uint32_t height, uint32_t stride,
52             uint32_t format, uint64_t usage);
53
54  // Duplicates the native handle underlying |other| and then imports it. This
55  // is useful for creating multiple, independent views of the same Ion/Gralloc
56  // buffer. Returns 0 on success or a negative errno code otherwise. If
57  // duplication or import fail the previous native handle is left intact.
58  int Duplicate(const IonBuffer* other);
59
60  int Lock(uint32_t usage, int x, int y, int width, int height, void** address);
61  int LockYUV(uint32_t usage, int x, int y, int width, int height,
62              struct android_ycbcr* yuv);
63  int Unlock();
64
65  const sp<GraphicBuffer>& buffer() const { return buffer_; }
66  buffer_handle_t handle() const {
67    return buffer_.get() ? buffer_->handle : nullptr;
68  }
69  uint32_t width() const { return buffer_.get() ? buffer_->getWidth() : 0; }
70  uint32_t height() const { return buffer_.get() ? buffer_->getHeight() : 0; }
71  uint32_t layer_count() const {
72    return buffer_.get() ? buffer_->getLayerCount() : 0;
73  }
74  uint32_t stride() const { return buffer_.get() ? buffer_->getStride() : 0; }
75  uint32_t layer_stride() const { return 0; }
76  uint32_t format() const {
77    return buffer_.get() ? buffer_->getPixelFormat() : 0;
78  }
79  uint64_t usage() const {
80    return buffer_.get() ? static_cast<uint64_t>(buffer_->getUsage()) : 0;
81  }
82
83 private:
84  sp<GraphicBuffer> buffer_;
85
86  IonBuffer(const IonBuffer&) = delete;
87  void operator=(const IonBuffer&) = delete;
88};
89
90}  // namespace dvr
91}  // namespace android
92
93#endif  // ANDROID_DVR_ION_BUFFER_H_
94