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