Home | History | Annotate | Download | only in arc
      1 /* Copyright 2017 The Chromium OS Authors. All rights reserved.
      2  * Use of this source code is governed by a BSD-style license that can be
      3  * found in the LICENSE file.
      4  */
      5 
      6 #ifndef HAL_USB_FRAME_BUFFER_H_
      7 #define HAL_USB_FRAME_BUFFER_H_
      8 
      9 #include <cstdint>
     10 #include <memory>
     11 
     12 #include <base/files/scoped_file.h>
     13 #include <base/synchronization/lock.h>
     14 #include <hardware/gralloc.h>
     15 
     16 namespace arc {
     17 
     18 class FrameBuffer {
     19  public:
     20   FrameBuffer();
     21   virtual ~FrameBuffer();
     22 
     23   // If mapped successfully, the address will be assigned to |data_| and return
     24   // 0. Otherwise, returns -EINVAL.
     25   virtual int Map() = 0;
     26 
     27   // Unmaps the mapped address. Returns 0 for success.
     28   virtual int Unmap() = 0;
     29 
     30   uint8_t* GetData() const { return data_; }
     31   size_t GetDataSize() const { return data_size_; }
     32   size_t GetBufferSize() const { return buffer_size_; }
     33   uint32_t GetWidth() const { return width_; }
     34   uint32_t GetHeight() const { return height_; }
     35   uint32_t GetFourcc() const { return fourcc_; }
     36 
     37   void SetFourcc(uint32_t fourcc) { fourcc_ = fourcc; }
     38   virtual int SetDataSize(size_t data_size);
     39 
     40  protected:
     41   uint8_t* data_;
     42 
     43   // The number of bytes used in the buffer.
     44   size_t data_size_;
     45 
     46   // The number of bytes allocated in the buffer.
     47   size_t buffer_size_;
     48 
     49   // Frame resolution.
     50   uint32_t width_;
     51   uint32_t height_;
     52 
     53   // This is V4L2_PIX_FMT_* in linux/videodev2.h.
     54   uint32_t fourcc_;
     55 };
     56 
     57 // AllocatedFrameBuffer is used for the buffer from hal malloc-ed. User should
     58 // be aware to manage the memory.
     59 class AllocatedFrameBuffer : public FrameBuffer {
     60  public:
     61   explicit AllocatedFrameBuffer(int buffer_size);
     62   explicit AllocatedFrameBuffer(uint8_t* buffer, int buffer_size);
     63   ~AllocatedFrameBuffer() override;
     64 
     65   // No-op for the two functions.
     66   int Map() override { return 0; }
     67   int Unmap() override { return 0; }
     68 
     69   void SetWidth(uint32_t width) { width_ = width; }
     70   void SetHeight(uint32_t height) { height_ = height; }
     71   int SetDataSize(size_t data_size) override;
     72   void Reset();
     73 
     74  private:
     75   std::unique_ptr<uint8_t[]> buffer_;
     76 };
     77 
     78 // V4L2FrameBuffer is used for the buffer from V4L2CameraDevice. Maps the fd
     79 // in constructor. Unmaps and closes the fd in destructor.
     80 class V4L2FrameBuffer : public FrameBuffer {
     81  public:
     82   V4L2FrameBuffer(base::ScopedFD fd, int buffer_size, uint32_t width,
     83                   uint32_t height, uint32_t fourcc);
     84   // Unmaps |data_| and closes |fd_|.
     85   ~V4L2FrameBuffer();
     86 
     87   int Map() override;
     88   int Unmap() override;
     89   int GetFd() const { return fd_.get(); }
     90 
     91  private:
     92   // File descriptor of V4L2 frame buffer.
     93   base::ScopedFD fd_;
     94 
     95   bool is_mapped_;
     96 
     97   // Lock to guard |is_mapped_|.
     98   base::Lock lock_;
     99 };
    100 
    101 // GrallocFrameBuffer is used for the buffer from Android framework. Uses
    102 // CameraBufferMapper to lock and unlock the buffer.
    103 class GrallocFrameBuffer : public FrameBuffer {
    104  public:
    105   GrallocFrameBuffer(buffer_handle_t buffer, uint32_t width, uint32_t height,
    106                      uint32_t fourcc, uint32_t device_buffer_length,
    107                      uint32_t stream_usage);
    108   ~GrallocFrameBuffer();
    109 
    110   int Map() override;
    111   int Unmap() override;
    112 
    113  private:
    114   // The currently used buffer for |buffer_mapper_| operations.
    115   buffer_handle_t buffer_;
    116 
    117   // Used to import gralloc buffer.
    118   const gralloc_module_t* gralloc_module_;
    119 
    120   bool is_mapped_;
    121 
    122   // Lock to guard |is_mapped_|.
    123   base::Lock lock_;
    124 
    125   // Camera stream and device buffer context.
    126   uint32_t device_buffer_length_;
    127   uint32_t stream_usage_;
    128 };
    129 
    130 }  // namespace arc
    131 
    132 #endif  // HAL_USB_FRAME_BUFFER_H_
    133