blob: 7ece59de3496a240ca52ad7ac7f23de126c09ac1 [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
Ari Hausman-Cohen3841a7f2016-07-19 17:27:52 -070017#ifndef V4L2_CAMERA_HAL_V4L2_GRALLOC_H_
18#define V4L2_CAMERA_HAL_V4L2_GRALLOC_H_
Ari Hausman-Cohen3eece6f2016-07-21 11:08:24 -070019
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
Ari Hausman-Cohen9e6fd982016-08-02 16:29:53 -070030// Generously allow up to 6MB (the largest JPEG on the RPi camera is about 5MB).
31static constexpr size_t V4L2_MAX_JPEG_SIZE = 6000000;
Ari Hausman-Cohen3eece6f2016-07-21 11:08:24 -070032
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
Ari Hausman-Cohen9e6fd982016-08-02 16:29:53 -070042 // Lock a camera buffer. Uses device buffer length, sets user pointer.
Ari Hausman-Cohen3eece6f2016-07-21 11:08:24 -070043 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};
Ari Hausman-Cohen3eece6f2016-07-21 11:08:24 -070068
Ari Hausman-Cohen3841a7f2016-07-19 17:27:52 -070069} // namespace default_camera_hal
70
71#endif // V4L2_CAMERA_HAL_V4L2_GRALLOC_H_