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