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