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 8 namespace android { 9 namespace dvr { 10 11 // IonBuffer is an abstraction of Ion/Gralloc buffers. 12 class 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 format, 20 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 layer_count, 35 uint32_t format, uint64_t usage); 36 37 // Resets the underlying native handle and parameters, freeing the previous 38 // native handle if necessary. 39 void Reset(buffer_handle_t handle, uint32_t width, uint32_t height, 40 uint32_t layer_count, uint32_t stride, uint32_t format, 41 uint64_t usage); 42 43 // Like Reset but also registers the native handle, which is necessary for 44 // native handles received over IPC. Returns 0 on success or a negative errno 45 // code otherwise. If import fails the previous native handle is left intact. 46 int Import(buffer_handle_t handle, uint32_t width, uint32_t height, 47 uint32_t layer_count, uint32_t stride, uint32_t format, 48 uint64_t usage); 49 50 // Like Reset but imports a native handle from raw fd and int arrays. Returns 51 // 0 on success or a negative errno code otherwise. If import fails the 52 // previous native handle is left intact. 53 int Import(const int* fd_array, int fd_count, const int* int_array, 54 int int_count, uint32_t width, uint32_t height, 55 uint32_t layer_count, uint32_t stride, uint32_t format, 56 uint64_t usage); 57 58 // Duplicates the native handle underlying |other| and then imports it. This 59 // is useful for creating multiple, independent views of the same Ion/Gralloc 60 // buffer. Returns 0 on success or a negative errno code otherwise. If 61 // duplication or import fail the previous native handle is left intact. 62 int Duplicate(const IonBuffer* other); 63 64 int Lock(uint32_t usage, int x, int y, int width, int height, void** address); 65 int LockYUV(uint32_t usage, int x, int y, int width, int height, 66 struct android_ycbcr* yuv); 67 int Unlock(); 68 buffer()69 const sp<GraphicBuffer>& buffer() const { return buffer_; } handle()70 buffer_handle_t handle() const { 71 return buffer_.get() ? buffer_->handle : nullptr; 72 } width()73 uint32_t width() const { return buffer_.get() ? buffer_->getWidth() : 0; } height()74 uint32_t height() const { return buffer_.get() ? buffer_->getHeight() : 0; } layer_count()75 uint32_t layer_count() const { 76 return buffer_.get() ? buffer_->getLayerCount() : 0; 77 } stride()78 uint32_t stride() const { return buffer_.get() ? buffer_->getStride() : 0; } format()79 uint32_t format() const { 80 return buffer_.get() ? buffer_->getPixelFormat() : 0; 81 } usage()82 uint64_t usage() const { 83 return buffer_.get() ? static_cast<uint64_t>(buffer_->getUsage()) : 0; 84 } 85 86 private: 87 sp<GraphicBuffer> buffer_; 88 89 IonBuffer(const IonBuffer&) = delete; 90 void operator=(const IonBuffer&) = delete; 91 }; 92 93 } // namespace dvr 94 } // namespace android 95 96 #endif // ANDROID_DVR_ION_BUFFER_H_ 97