summaryrefslogtreecommitdiff
path: root/common/hal/utils/hdrplus_request_processor.cc
blob: 9ca39fb78702a0bb90fdb5b7bddce146d9d45465 (plain)
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/*
 * Copyright (C) 2019 The Android Open Source Project
 *
 * 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.
 */

//#define LOG_NDEBUG 0
#define LOG_TAG "GCH_HdrplusRequestProcessor"
#define ATRACE_TAG ATRACE_TAG_CAMERA
#include <log/log.h>
#include <utils/Trace.h>

#include "hdrplus_request_processor.h"
#include "vendor_tag_defs.h"

namespace android {
namespace google_camera_hal {

std::unique_ptr<HdrplusRequestProcessor> HdrplusRequestProcessor::Create(
    CameraDeviceSessionHwl* device_session_hwl, int32_t raw_stream_id,
    uint32_t physical_camera_id) {
  ATRACE_CALL();
  if (device_session_hwl == nullptr) {
    ALOGE("%s: device_session_hwl (%p) is nullptr", __FUNCTION__,
          device_session_hwl);
    return nullptr;
  }

  auto request_processor = std::unique_ptr<HdrplusRequestProcessor>(
      new HdrplusRequestProcessor(physical_camera_id));
  if (request_processor == nullptr) {
    ALOGE("%s: Creating HdrplusRequestProcessor failed.", __FUNCTION__);
    return nullptr;
  }

  status_t res =
      request_processor->Initialize(device_session_hwl, raw_stream_id);
  if (res != OK) {
    ALOGE("%s: Initializing HdrplusRequestProcessor failed: %s (%d).",
          __FUNCTION__, strerror(-res), res);
    return nullptr;
  }

  return request_processor;
}

status_t HdrplusRequestProcessor::Initialize(
    CameraDeviceSessionHwl* device_session_hwl, int32_t raw_stream_id) {
  ATRACE_CALL();
  std::unique_ptr<HalCameraMetadata> characteristics;
  status_t res = NO_INIT;
  uint32_t num_physical_cameras =
      device_session_hwl->GetPhysicalCameraIds().size();
  if (num_physical_cameras > 0) {
    res = device_session_hwl->GetPhysicalCameraCharacteristics(
        kCameraId, &characteristics);
    if (res != OK) {
      ALOGE("%s: GetPhysicalCameraCharacteristics failed.", __FUNCTION__);
      return BAD_VALUE;
    }
  } else {
    res = device_session_hwl->GetCameraCharacteristics(&characteristics);
    if (res != OK) {
      ALOGE("%s: GetCameraCharacteristics failed.", __FUNCTION__);
      return BAD_VALUE;
    }
  }

  camera_metadata_ro_entry entry;
  res = characteristics->Get(
      ANDROID_SENSOR_INFO_PRE_CORRECTION_ACTIVE_ARRAY_SIZE, &entry);
  if (res == OK) {
    active_array_width_ = entry.data.i32[2];
    active_array_height_ = entry.data.i32[3];
    ALOGI("%s Active size (%d x %d).", __FUNCTION__, active_array_width_,
          active_array_height_);
  } else {
    ALOGE("%s Get active size failed: %s (%d).", __FUNCTION__, strerror(-res),
          res);
    return res;
  }

  res = characteristics->Get(VendorTagIds::kHdrplusPayloadFrames, &entry);
  if (res != OK || entry.data.i32[0] <= 0) {
    ALOGE("%s: Getting kHdrplusPayloadFrames failed or number <= 0",
          __FUNCTION__);
    return BAD_VALUE;
  }
  payload_frames_ = entry.data.i32[0];
  ALOGI("%s: HDR+ payload_frames_: %d", __FUNCTION__, payload_frames_);
  raw_stream_id_ = raw_stream_id;

  return OK;
}

status_t HdrplusRequestProcessor::ConfigureStreams(
    InternalStreamManager* internal_stream_manager,
    const StreamConfiguration& stream_config,
    StreamConfiguration* process_block_stream_config) {
  ATRACE_CALL();
  if (process_block_stream_config == nullptr ||
      internal_stream_manager == nullptr) {
    ALOGE(
        "%s: process_block_stream_config (%p) is nullptr or "
        "internal_stream_manager (%p) is nullptr",
        __FUNCTION__, process_block_stream_config, internal_stream_manager);
    return BAD_VALUE;
  }

  internal_stream_manager_ = internal_stream_manager;

  Stream raw_stream;
  raw_stream.stream_type = StreamType::kInput;
  raw_stream.width = active_array_width_;
  raw_stream.height = active_array_height_;
  raw_stream.format = HAL_PIXEL_FORMAT_RAW10;
  raw_stream.usage = 0;
  raw_stream.rotation = StreamRotation::kRotation0;
  raw_stream.data_space = HAL_DATASPACE_ARBITRARY;
  // Set id back to raw_stream and then HWL can get correct HAL stream ID
  raw_stream.id = raw_stream_id_;

  process_block_stream_config->streams = stream_config.streams;
  // Add internal RAW stream
  process_block_stream_config->streams.push_back(raw_stream);
  process_block_stream_config->operation_mode = stream_config.operation_mode;
  process_block_stream_config->session_params =
      HalCameraMetadata::Clone(stream_config.session_params.get());
  process_block_stream_config->stream_config_counter =
      stream_config.stream_config_counter;
  process_block_stream_config->log_id = stream_config.log_id;

  return OK;
}

status_t HdrplusRequestProcessor::SetProcessBlock(
    std::unique_ptr<ProcessBlock> process_block) {
  ATRACE_CALL();
  if (process_block == nullptr) {
    ALOGE("%s: process_block is nullptr", __FUNCTION__);
    return BAD_VALUE;
  }

  std::lock_guard<std::mutex> lock(process_block_lock_);
  if (process_block_ != nullptr) {
    ALOGE("%s: Already configured.", __FUNCTION__);
    return ALREADY_EXISTS;
  }

  process_block_ = std::move(process_block);
  return OK;
}

bool HdrplusRequestProcessor::IsReadyForNextRequest() {
  ATRACE_CALL();
  if (internal_stream_manager_ == nullptr) {
    ALOGW("%s: internal_stream_manager_ nullptr", __FUNCTION__);
    return false;
  }
  if (internal_stream_manager_->IsPendingBufferEmpty(raw_stream_id_) == false) {
    return false;
  }
  return true;
}

void HdrplusRequestProcessor::RemoveJpegMetadata(
    std::vector<std::unique_ptr<HalCameraMetadata>>* metadata) {
  const uint32_t tags[] = {
      ANDROID_JPEG_THUMBNAIL_SIZE,  ANDROID_JPEG_ORIENTATION,
      ANDROID_JPEG_QUALITY,         ANDROID_JPEG_THUMBNAIL_QUALITY,
      ANDROID_JPEG_GPS_COORDINATES, ANDROID_JPEG_GPS_PROCESSING_METHOD,
      ANDROID_JPEG_GPS_TIMESTAMP};
  if (metadata == nullptr) {
    ALOGW("%s: metadata is nullptr", __FUNCTION__);
    return;
  }

  for (uint32_t i = 0; i < metadata->size(); i++) {
    for (uint32_t tag_index = 0; tag_index < sizeof(tags) / sizeof(uint32_t);
         tag_index++) {
      if (metadata->at(i) == nullptr) {
        continue;
      }
      status_t res = metadata->at(i)->Erase(tags[tag_index]);
      if (res != OK) {
        ALOGW("%s: (%d)erase index(%d) failed: %s(%d)", __FUNCTION__, i,
              tag_index, strerror(-res), res);
      }
    }
  }
}

status_t HdrplusRequestProcessor::ProcessRequest(const CaptureRequest& request) {
  ATRACE_CALL();
  std::lock_guard<std::mutex> lock(process_block_lock_);
  if (process_block_ == nullptr) {
    ALOGE("%s: Not configured yet.", __FUNCTION__);
    return NO_INIT;
  }

  if (IsReadyForNextRequest() == false) {
    return BAD_VALUE;
  }

  CaptureRequest block_request;
  block_request.frame_number = request.frame_number;
  block_request.settings = HalCameraMetadata::Clone(request.settings.get());
  block_request.output_buffers = request.output_buffers;
  for (auto& [camera_id, physical_metadata] : request.physical_camera_settings) {
    block_request.physical_camera_settings[camera_id] =
        HalCameraMetadata::Clone(physical_metadata.get());
  }

  // Get multiple raw buffer and metadata from internal stream as input
  status_t result = internal_stream_manager_->GetMostRecentStreamBuffer(
      raw_stream_id_, &(block_request.input_buffers),
      &(block_request.input_buffer_metadata), payload_frames_);
  if (result != OK) {
    ALOGE("%s: frame:%d GetStreamBuffer failed.", __FUNCTION__,
          request.frame_number);
    return UNKNOWN_ERROR;
  }

  RemoveJpegMetadata(&(block_request.input_buffer_metadata));
  std::vector<ProcessBlockRequest> block_requests(1);
  block_requests[0].request = std::move(block_request);
  ALOGD("%s: frame number %u is an HDR+ request.", __FUNCTION__,
        request.frame_number);

  return process_block_->ProcessRequests(block_requests, request);
}

status_t HdrplusRequestProcessor::Flush() {
  ATRACE_CALL();
  std::lock_guard<std::mutex> lock(process_block_lock_);
  if (process_block_ == nullptr) {
    return OK;
  }

  return process_block_->Flush();
}

}  // namespace google_camera_hal
}  // namespace android