ion_buffer.h revision 8a71b13127f737a46c55ba94d31ba0262e7cbf8a
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 buffer_handle_t handle() const { if (buffer_.get()) return buffer_->handle; 64 else return nullptr; } 65 int width() const { if (buffer_.get()) return buffer_->getWidth(); 66 else return 0; } 67 int height() const { if (buffer_.get()) return buffer_->getHeight(); 68 else return 0; } 69 int layer_count() const { if (buffer_.get()) return buffer_->getLayerCount(); 70 else return 0; } 71 int stride() const { if (buffer_.get()) return buffer_->getStride(); 72 else return 0; } 73 int layer_stride() const { return 0; } 74 int format() const { if (buffer_.get()) return buffer_->getPixelFormat(); 75 else return 0; } 76 int usage() const { if (buffer_.get()) return buffer_->getUsage(); 77 else return 0; } 78 79 private: 80 sp<GraphicBuffer> buffer_; 81 82 IonBuffer(const IonBuffer&) = delete; 83 void operator=(const IonBuffer&) = delete; 84}; 85 86} // namespace dvr 87} // namespace android 88 89#endif // ANDROID_DVR_ION_BUFFER_H_ 90