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