liyujie
2025-08-28 d9927380ed7c8366f762049be9f3fee225860833
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*
 * Copyright (c) 2021 by Allwinnertech Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
#ifndef V4L2_CAMERA_HAL_V4L2_GRALLOC_H_
#define V4L2_CAMERA_HAL_V4L2_GRALLOC_H_
 
#include "common.h"
#ifdef LOG_NDEBUG
#undef LOG_NDEBUG
#endif
 
#if DBG_V4L2_GRALLOC
#undef NDEBUG
#define LOG_NDEBUG 0
#else
#define LOG_NDEBUG 1
#endif
 
#include <errno.h>
#include <hardware/camera3.h>
#include <hardware/gralloc.h>
#include <linux/videodev2.h>
#include <system/graphics.h>
 
#include <unordered_map>
 
#include "v4l2_log.h"
 
namespace v4l2_camera_hal {
 
// Generously allow up to 6MB (the largest JPEG on the RPi camera is about 5MB).
static constexpr size_t V4L2_MAX_JPEG_SIZE = 20000000;
 
// V4L2Gralloc is a wrapper around relevant parts of a gralloc module,
// with some assistive transformations.
class V4L2Gralloc {
 public:
  // Use this method to create V4L2Gralloc objects. Functionally equivalent
  // to "new V4L2Gralloc", except that it may return nullptr in case of failure.
  static V4L2Gralloc* NewV4L2Gralloc();
  virtual ~V4L2Gralloc();
  // Lock buffer for picture.
  int lock_pic(const camera3_stream_buffer_t* camera_buffer,
               unsigned long* device_buffer);
  int lock_buffer(const camera3_stream_buffer_t* camera_buffer,
                  unsigned long* device_buffer);
  int unlock_buffer(const camera3_stream_buffer_t* camera_buffer);
 
  // Lock a camera buffer. Uses device buffer length, sets user pointer.
  int lock(const camera3_stream_buffer_t* camera_buffer,
           uint32_t bytes_per_line,
           v4l2_buffer* device_buffer);
  // Unlock a buffer that was locked by this helper (equality determined
  // based on buffer user pointer, not the specific object).
  int unlock(const v4l2_buffer* device_buffer);
  // Lock a camera buffer. Get vir addr, record index addr.
  int lock_index(const camera3_stream_buffer_t* camera_buffer,
                 unsigned long* device_buffer, uint32_t result_index);
  // Unlock buffer by index addr.
  int unlock_index(uint32_t result_index);
  int lock_handle(const buffer_handle_t* buffer,
                  void ** dst_addr);
  int lock_handle(const buffer_handle_t* buffer,
                  void ** dst_addr, unsigned long * mJpegBufferSizes);
  int lock_handle_ycbcr(const buffer_handle_t* buffer,
                        android_ycbcr * ycbcr_dst_addr);
 
  int get_share_fd(const buffer_handle_t* buffer,
                   int * sharefd, unsigned long * mJpegBufferSizes);
 
  int unlock_handle(const buffer_handle_t* buffer);
 
  // Release all held locks.
  int unlockAllBuffers();
 
 private:
  // Constructor is private to allow failing on bad input.
  // Use NewV4L2Gralloc instead.
  explicit V4L2Gralloc(const gralloc_module_t* module);
 
  const gralloc_module_t* mModule;
 
  struct BufferData {
    const camera3_stream_buffer_t* camera_buffer;
    // Below fields only used when a ycbcr format transform is necessary.
    uint32_t v4l2_bytes_per_line;
  };
  BufferData* aBufferData[MAX_BUFFER_NUM];
  buffer_handle_t aBufferDataVideo[MAX_BUFFER_NUM];
 
  // Map buffer index : BufferData about that buffer.
  std::unordered_map<int, const BufferData*> mBufferMap;
};
 
}  // namespace default_camera_hal
 
#endif  // V4L2_CAMERA_HAL_V4L2_GRALLOC_H_