blob: ebec96377640ce07fb4867c1420d5079fc67ded1 [file] [log] [blame]
Ari Hausman-Cohen3eece6f2016-07-21 11:08:24 -07001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef V4L2_GRALLOC_H
18#define V4L2_GRALLOC_H
19
20#include <linux/videodev2.h>
21
22#include <unordered_map>
23
24#include <hardware/camera3.h>
25#include <hardware/gralloc.h>
26#include <system/graphics.h>
27
28namespace v4l2_camera_hal {
29
30// Generously allow up to 3MB.
31static constexpr size_t V4L2_MAX_JPEG_SIZE = 3000000;
32
33// V4L2Gralloc is a wrapper around relevant parts of a gralloc module,
34// with some assistive transformations.
35class V4L2Gralloc {
36public:
Ari Hausman-Cohen681eaa22016-07-21 16:28:17 -070037 // Use this method to create V4L2Gralloc objects. Functionally equivalent
38 // to "new V4L2Gralloc", except that it may return nullptr in case of failure.
39 static V4L2Gralloc* NewV4L2Gralloc();
40 virtual ~V4L2Gralloc();
Ari Hausman-Cohen3eece6f2016-07-21 11:08:24 -070041
42 // Lock a camera buffer. Sets device buffer user pointer and length.
43 int lock(const camera3_stream_buffer_t* camera_buffer,
44 uint32_t bytes_per_line,
45 v4l2_buffer* device_buffer);
46 // Unlock a buffer that was locked by this helper (equality determined
47 // based on buffer user pointer, not the specific object).
48 int unlock(const v4l2_buffer* device_buffer);
Ari Hausman-Cohen4ab49622016-07-21 14:33:54 -070049 // Release all held locks.
50 int unlockAllBuffers();
Ari Hausman-Cohen3eece6f2016-07-21 11:08:24 -070051
Ari Hausman-Cohen3eece6f2016-07-21 11:08:24 -070052private:
Ari Hausman-Cohen681eaa22016-07-21 16:28:17 -070053 // Constructor is private to allow failing on bad input.
54 // Use NewV4L2Gralloc instead.
55 V4L2Gralloc(const gralloc_module_t* module);
56
Ari Hausman-Cohen3eece6f2016-07-21 11:08:24 -070057 const gralloc_module_t* mModule;
58
59 struct BufferData {
60 const camera3_stream_buffer_t* camera_buffer;
61 // Below fields only used when a ycbcr format transform is necessary.
62 std::unique_ptr<android_ycbcr> transform_dest; // nullptr if no transform.
63 uint32_t v4l2_bytes_per_line;
64 };
65 // Map data pointer : BufferData about that buffer.
66 std::unordered_map<void*, const BufferData*> mBufferMap;
67};
68} // namespace default_camera_hal
69
70#endif // CAMERA_GRALLOC_H_