1 /*
2  * Copyright (C) 2019 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 HW_EMULATOR_CAMERA_BASE_H
18 #define HW_EMULATOR_CAMERA_BASE_H
19 
20 #include <log/log.h>
21 
22 #include <memory>
23 
24 #include "HandleImporter.h"
25 #include "hwl_types.h"
26 
27 namespace android {
28 
29 using android::hardware::camera::common::V1_0::helper::HandleImporter;
30 using google_camera_hal::HwlPipelineCallback;
31 using google_camera_hal::StreamBuffer;
32 
33 struct YCbCrPlanes {
34   uint8_t* img_y = nullptr;
35   uint8_t* img_cb = nullptr;
36   uint8_t* img_cr = nullptr;
37   uint32_t y_stride = 0;
38   uint32_t cbcr_stride = 0;
39   uint32_t cbcr_step = 0;
40 };
41 
42 struct SinglePlane {
43   uint8_t* img = nullptr;
44   uint32_t stride = 0;
45   uint32_t buffer_size = 0;
46 };
47 
48 struct SensorBuffer {
49   uint32_t width, height;
50   uint32_t frame_number;
51   uint32_t pipeline_id;
52   uint32_t camera_id;
53   android_pixel_format_t format;
54   android_dataspace_t dataSpace;
55   StreamBuffer stream_buffer;
56   HandleImporter importer;
57   HwlPipelineCallback callback;
58   int acquire_fence_fd;
59   bool is_input;
60   bool is_failed_request;
61 
62   union Plane {
63     SinglePlane img;
64     YCbCrPlanes img_y_crcb;
65   } plane;
66 
SensorBufferSensorBuffer67   SensorBuffer()
68       : width(0),
69         height(0),
70         frame_number(0),
71         pipeline_id(0),
72         camera_id(0),
73         format(HAL_PIXEL_FORMAT_RGBA_8888),
74         dataSpace(HAL_DATASPACE_UNKNOWN),
75         acquire_fence_fd(-1),
76         is_input(false),
77         is_failed_request(false),
78         plane{} {
79   }
80 
81   SensorBuffer(const SensorBuffer&) = delete;
82   SensorBuffer& operator=(const SensorBuffer&) = delete;
83 };
84 
85 typedef std::vector<std::unique_ptr<SensorBuffer>> Buffers;
86 
87 }  // namespace android
88 
89 using android::google_camera_hal::BufferStatus;
90 using android::google_camera_hal::ErrorCode;
91 using android::google_camera_hal::HwlPipelineResult;
92 using android::google_camera_hal::MessageType;
93 using android::google_camera_hal::NotifyMessage;
94 
95 template <>
96 struct std::default_delete<android::SensorBuffer> {
97   inline void operator()(android::SensorBuffer* buffer) const {
98     if (buffer != nullptr) {
99       if (buffer->stream_buffer.buffer != nullptr) {
100         buffer->importer.unlock(buffer->stream_buffer.buffer);
101         buffer->importer.freeBuffer(buffer->stream_buffer.buffer);
102       }
103 
104       if (buffer->acquire_fence_fd >= 0) {
105         buffer->importer.closeFence(buffer->acquire_fence_fd);
106       }
107 
108       if ((buffer->stream_buffer.status != BufferStatus::kOk) &&
109           (buffer->callback.notify != nullptr) && (!buffer->is_failed_request)) {
110         NotifyMessage msg = {
111             .type = MessageType::kError,
112             .message.error = {.frame_number = buffer->frame_number,
113                               .error_stream_id = buffer->stream_buffer.stream_id,
114                               .error_code = ErrorCode::kErrorBuffer}};
115         buffer->callback.notify(buffer->pipeline_id, msg);
116       }
117 
118       if (buffer->callback.process_pipeline_result != nullptr) {
119         auto result = std::make_unique<HwlPipelineResult>();
120         result->camera_id = buffer->camera_id;
121         result->pipeline_id = buffer->pipeline_id;
122         result->frame_number = buffer->frame_number;
123         result->partial_result = 0;
124 
125         buffer->stream_buffer.acquire_fence =
126             buffer->stream_buffer.release_fence = nullptr;
127         if (buffer->is_input) {
128           result->input_buffers.push_back(buffer->stream_buffer);
129         } else {
130           result->output_buffers.push_back(buffer->stream_buffer);
131         }
132         buffer->callback.process_pipeline_result(std::move(result));
133       }
134       delete buffer;
135     }
136   }
137 };
138 
139 #endif
140