blob: 8e112a15854a18eb4a7abae634c495c502bdc675 [file] [log] [blame]
Igor Murashkine7ee7632013-06-11 18:10:18 -07001/*
Shuzhen Wangc28189a2017-11-27 23:05:10 -08002 * Copyright (C) 2013-2018 The Android Open Source Project
Igor Murashkine7ee7632013-06-11 18:10:18 -07003 *
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#define LOG_TAG "CameraDeviceClient"
18#define ATRACE_TAG ATRACE_TAG_CAMERA
Jianing Weicb0652e2014-03-12 18:29:36 -070019//#define LOG_NDEBUG 0
Igor Murashkine7ee7632013-06-11 18:10:18 -070020
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070021#include <cutils/properties.h>
Igor Murashkine7ee7632013-06-11 18:10:18 -070022#include <utils/Log.h>
23#include <utils/Trace.h>
Igor Murashkine7ee7632013-06-11 18:10:18 -070024#include <gui/Surface.h>
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070025#include <camera/camera2/CaptureRequest.h>
Ruben Brunk5698d442014-06-18 10:39:40 -070026#include <camera/CameraUtils.h>
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070027
28#include "common/CameraDeviceBase.h"
29#include "api2/CameraDeviceClient.h"
30
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080031// Convenience methods for constructing binder::Status objects for error returns
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070032
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080033#define STATUS_ERROR(errorCode, errorString) \
34 binder::Status::fromServiceSpecificError(errorCode, \
Eino-Ville Talvala02bf0322016-02-18 12:41:10 -080035 String8::format("%s:%d: %s", __FUNCTION__, __LINE__, errorString))
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080036
37#define STATUS_ERROR_FMT(errorCode, errorString, ...) \
38 binder::Status::fromServiceSpecificError(errorCode, \
Eino-Ville Talvala02bf0322016-02-18 12:41:10 -080039 String8::format("%s:%d: " errorString, __FUNCTION__, __LINE__, \
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080040 __VA_ARGS__))
Igor Murashkine7ee7632013-06-11 18:10:18 -070041
42namespace android {
43using namespace camera2;
44
45CameraDeviceClientBase::CameraDeviceClientBase(
46 const sp<CameraService>& cameraService,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080047 const sp<hardware::camera2::ICameraDeviceCallbacks>& remoteCallback,
Igor Murashkine7ee7632013-06-11 18:10:18 -070048 const String16& clientPackageName,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080049 const String8& cameraId,
Igor Murashkine7ee7632013-06-11 18:10:18 -070050 int cameraFacing,
51 int clientPid,
52 uid_t clientUid,
53 int servicePid) :
Eino-Ville Talvalae992e752014-11-07 16:17:48 -080054 BasicClient(cameraService,
Marco Nelissenf8880202014-11-14 07:58:25 -080055 IInterface::asBinder(remoteCallback),
Eino-Ville Talvalae992e752014-11-07 16:17:48 -080056 clientPackageName,
57 cameraId,
58 cameraFacing,
59 clientPid,
60 clientUid,
61 servicePid),
Igor Murashkine7ee7632013-06-11 18:10:18 -070062 mRemoteCallback(remoteCallback) {
63}
Igor Murashkine7ee7632013-06-11 18:10:18 -070064
65// Interface used by CameraService
66
67CameraDeviceClient::CameraDeviceClient(const sp<CameraService>& cameraService,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080068 const sp<hardware::camera2::ICameraDeviceCallbacks>& remoteCallback,
69 const String16& clientPackageName,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080070 const String8& cameraId,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080071 int cameraFacing,
72 int clientPid,
73 uid_t clientUid,
74 int servicePid) :
Igor Murashkine7ee7632013-06-11 18:10:18 -070075 Camera2ClientBase(cameraService, remoteCallback, clientPackageName,
76 cameraId, cameraFacing, clientPid, clientUid, servicePid),
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -070077 mInputStream(),
Chien-Yu Chene8c535e2016-04-14 12:18:26 -070078 mStreamingRequestId(REQUEST_ID_NONE),
Igor Murashkine7ee7632013-06-11 18:10:18 -070079 mRequestIdCounter(0) {
80
81 ATRACE_CALL();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080082 ALOGI("CameraDeviceClient %s: Opened", cameraId.string());
Igor Murashkine7ee7632013-06-11 18:10:18 -070083}
84
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080085status_t CameraDeviceClient::initialize(sp<CameraProviderManager> manager) {
86 return initializeImpl(manager);
87}
88
89template<typename TProviderPtr>
90status_t CameraDeviceClient::initializeImpl(TProviderPtr providerPtr) {
Igor Murashkine7ee7632013-06-11 18:10:18 -070091 ATRACE_CALL();
92 status_t res;
93
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080094 res = Camera2ClientBase::initialize(providerPtr);
Igor Murashkine7ee7632013-06-11 18:10:18 -070095 if (res != OK) {
96 return res;
97 }
98
99 String8 threadName;
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -0700100 mFrameProcessor = new FrameProcessorBase(mDevice);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800101 threadName = String8::format("CDU-%s-FrameProc", mCameraIdStr.string());
Igor Murashkine7ee7632013-06-11 18:10:18 -0700102 mFrameProcessor->run(threadName.string());
103
104 mFrameProcessor->registerListener(FRAME_PROCESSOR_LISTENER_MIN_ID,
105 FRAME_PROCESSOR_LISTENER_MAX_ID,
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -0800106 /*listener*/this,
Zhijun He25a0aef2014-06-25 11:40:02 -0700107 /*sendPartials*/true);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700108
109 return OK;
110}
111
112CameraDeviceClient::~CameraDeviceClient() {
113}
114
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800115binder::Status CameraDeviceClient::submitRequest(
116 const hardware::camera2::CaptureRequest& request,
117 bool streaming,
118 /*out*/
119 hardware::camera2::utils::SubmitInfo *submitInfo) {
120 std::vector<hardware::camera2::CaptureRequest> requestList = { request };
121 return submitRequestList(requestList, streaming, submitInfo);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700122}
123
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800124binder::Status CameraDeviceClient::insertGbpLocked(const sp<IGraphicBufferProducer>& gbp,
Emilian Peevf873aa52018-01-26 14:58:28 +0000125 SurfaceMap* outSurfaceMap, Vector<int32_t>* outputStreamIds, int32_t *currentStreamId) {
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800126 int idx = mStreamMap.indexOfKey(IInterface::asBinder(gbp));
127
128 // Trying to submit request with surface that wasn't created
129 if (idx == NAME_NOT_FOUND) {
130 ALOGE("%s: Camera %s: Tried to submit a request with a surface that"
131 " we have not called createStream on",
132 __FUNCTION__, mCameraIdStr.string());
133 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
134 "Request targets Surface that is not part of current capture session");
135 }
136
137 const StreamSurfaceId& streamSurfaceId = mStreamMap.valueAt(idx);
138 if (outSurfaceMap->find(streamSurfaceId.streamId()) == outSurfaceMap->end()) {
139 (*outSurfaceMap)[streamSurfaceId.streamId()] = std::vector<size_t>();
140 outputStreamIds->push_back(streamSurfaceId.streamId());
141 }
142 (*outSurfaceMap)[streamSurfaceId.streamId()].push_back(streamSurfaceId.surfaceId());
143
144 ALOGV("%s: Camera %s: Appending output stream %d surface %d to request",
145 __FUNCTION__, mCameraIdStr.string(), streamSurfaceId.streamId(),
146 streamSurfaceId.surfaceId());
147
Emilian Peevf873aa52018-01-26 14:58:28 +0000148 if (currentStreamId != nullptr) {
149 *currentStreamId = streamSurfaceId.streamId();
150 }
151
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800152 return binder::Status::ok();
153}
154
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800155binder::Status CameraDeviceClient::submitRequestList(
156 const std::vector<hardware::camera2::CaptureRequest>& requests,
157 bool streaming,
158 /*out*/
159 hardware::camera2::utils::SubmitInfo *submitInfo) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700160 ATRACE_CALL();
Mark Salyzyn50468412014-06-18 16:33:43 -0700161 ALOGV("%s-start of function. Request list size %zu", __FUNCTION__, requests.size());
Jianing Wei90e59c92014-03-12 18:29:36 -0700162
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800163 binder::Status res = binder::Status::ok();
164 status_t err;
165 if ( !(res = checkPidStatus(__FUNCTION__) ).isOk()) {
166 return res;
167 }
Jianing Wei90e59c92014-03-12 18:29:36 -0700168
169 Mutex::Autolock icl(mBinderSerializationLock);
170
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800171 if (!mDevice.get()) {
172 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
173 }
Jianing Wei90e59c92014-03-12 18:29:36 -0700174
175 if (requests.empty()) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800176 ALOGE("%s: Camera %s: Sent null request. Rejecting request.",
177 __FUNCTION__, mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800178 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, "Empty request list");
Jianing Wei90e59c92014-03-12 18:29:36 -0700179 }
180
Emilian Peevaebbe412018-01-15 13:53:24 +0000181 List<const CameraDeviceBase::PhysicalCameraSettingsList> metadataRequestList;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700182 std::list<const SurfaceMap> surfaceMapList;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800183 submitInfo->mRequestId = mRequestIdCounter;
Jianing Wei90e59c92014-03-12 18:29:36 -0700184 uint32_t loopCounter = 0;
185
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800186 for (auto&& request: requests) {
187 if (request.mIsReprocess) {
Chien-Yu Chened0412e2015-04-27 15:04:22 -0700188 if (!mInputStream.configured) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800189 ALOGE("%s: Camera %s: no input stream is configured.", __FUNCTION__,
190 mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800191 return STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800192 "No input configured for camera %s but request is for reprocessing",
193 mCameraIdStr.string());
Chien-Yu Chened0412e2015-04-27 15:04:22 -0700194 } else if (streaming) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800195 ALOGE("%s: Camera %s: streaming reprocess requests not supported.", __FUNCTION__,
196 mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800197 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
198 "Repeating reprocess requests not supported");
Emilian Peevaebbe412018-01-15 13:53:24 +0000199 } else if (request.mPhysicalCameraSettings.size() > 1) {
200 ALOGE("%s: Camera %s: reprocess requests not supported for "
201 "multiple physical cameras.", __FUNCTION__,
202 mCameraIdStr.string());
203 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
204 "Reprocess requests not supported for multiple cameras");
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700205 }
Jianing Wei90e59c92014-03-12 18:29:36 -0700206 }
207
Emilian Peevaebbe412018-01-15 13:53:24 +0000208 if (request.mPhysicalCameraSettings.empty()) {
209 ALOGE("%s: Camera %s: request doesn't contain any settings.", __FUNCTION__,
210 mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800211 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
Emilian Peevaebbe412018-01-15 13:53:24 +0000212 "Request doesn't contain any settings");
213 }
214
215 //The first capture settings should always match the logical camera id
216 String8 logicalId(request.mPhysicalCameraSettings.begin()->id.c_str());
217 if (mDevice->getId() != logicalId) {
218 ALOGE("%s: Camera %s: Invalid camera request settings.", __FUNCTION__,
219 mCameraIdStr.string());
220 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
221 "Invalid camera request settings");
222 }
223
Emilian Peevaebbe412018-01-15 13:53:24 +0000224 if (request.mSurfaceList.isEmpty() && request.mStreamIdxList.size() == 0) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800225 ALOGE("%s: Camera %s: Requests must have at least one surface target. "
226 "Rejecting request.", __FUNCTION__, mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800227 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
228 "Request has no output targets");
Jianing Wei90e59c92014-03-12 18:29:36 -0700229 }
230
Jianing Wei90e59c92014-03-12 18:29:36 -0700231 /**
Shuzhen Wang0129d522016-10-30 22:43:41 -0700232 * Write in the output stream IDs and map from stream ID to surface ID
233 * which we calculate from the capture request's list of surface target
Jianing Wei90e59c92014-03-12 18:29:36 -0700234 */
Shuzhen Wang0129d522016-10-30 22:43:41 -0700235 SurfaceMap surfaceMap;
Jianing Wei90e59c92014-03-12 18:29:36 -0700236 Vector<int32_t> outputStreamIds;
Emilian Peevf873aa52018-01-26 14:58:28 +0000237 std::vector<std::string> requestedPhysicalIds;
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800238 if (request.mSurfaceList.size() > 0) {
239 for (sp<Surface> surface : request.mSurfaceList) {
240 if (surface == 0) continue;
Jianing Wei90e59c92014-03-12 18:29:36 -0700241
Emilian Peevf873aa52018-01-26 14:58:28 +0000242 int32_t streamId;
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800243 sp<IGraphicBufferProducer> gbp = surface->getIGraphicBufferProducer();
Emilian Peevf873aa52018-01-26 14:58:28 +0000244 res = insertGbpLocked(gbp, &surfaceMap, &outputStreamIds, &streamId);
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800245 if (!res.isOk()) {
246 return res;
247 }
Emilian Peevf873aa52018-01-26 14:58:28 +0000248
249 ssize_t index = mConfiguredOutputs.indexOfKey(streamId);
250 if (index >= 0) {
251 String8 requestedPhysicalId(
252 mConfiguredOutputs.valueAt(index).getPhysicalCameraId());
253 requestedPhysicalIds.push_back(requestedPhysicalId.string());
254 } else {
255 ALOGW("%s: Output stream Id not found among configured outputs!", __FUNCTION__);
256 }
Jianing Wei90e59c92014-03-12 18:29:36 -0700257 }
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800258 } else {
259 for (size_t i = 0; i < request.mStreamIdxList.size(); i++) {
260 int streamId = request.mStreamIdxList.itemAt(i);
261 int surfaceIdx = request.mSurfaceIdxList.itemAt(i);
Jianing Wei90e59c92014-03-12 18:29:36 -0700262
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800263 ssize_t index = mConfiguredOutputs.indexOfKey(streamId);
264 if (index < 0) {
265 ALOGE("%s: Camera %s: Tried to submit a request with a surface that"
266 " we have not called createStream on: stream %d",
267 __FUNCTION__, mCameraIdStr.string(), streamId);
268 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
269 "Request targets Surface that is not part of current capture session");
270 }
271
272 const auto& gbps = mConfiguredOutputs.valueAt(index).getGraphicBufferProducers();
273 if ((size_t)surfaceIdx >= gbps.size()) {
274 ALOGE("%s: Camera %s: Tried to submit a request with a surface that"
275 " we have not called createStream on: stream %d, surfaceIdx %d",
276 __FUNCTION__, mCameraIdStr.string(), streamId, surfaceIdx);
277 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
278 "Request targets Surface has invalid surface index");
279 }
280
Emilian Peevf873aa52018-01-26 14:58:28 +0000281 res = insertGbpLocked(gbps[surfaceIdx], &surfaceMap, &outputStreamIds, nullptr);
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800282 if (!res.isOk()) {
283 return res;
284 }
Emilian Peevf873aa52018-01-26 14:58:28 +0000285
286 String8 requestedPhysicalId(
287 mConfiguredOutputs.valueAt(index).getPhysicalCameraId());
288 requestedPhysicalIds.push_back(requestedPhysicalId.string());
Shuzhen Wang0129d522016-10-30 22:43:41 -0700289 }
Jianing Wei90e59c92014-03-12 18:29:36 -0700290 }
291
Emilian Peevf873aa52018-01-26 14:58:28 +0000292 CameraDeviceBase::PhysicalCameraSettingsList physicalSettingsList;
293 for (const auto& it : request.mPhysicalCameraSettings) {
294 String8 physicalId(it.id.c_str());
295 if (physicalId != mDevice->getId()) {
296 auto found = std::find(requestedPhysicalIds.begin(), requestedPhysicalIds.end(),
297 it.id);
298 if (found == requestedPhysicalIds.end()) {
299 ALOGE("%s: Camera %s: Physical camera id: %s not part of attached outputs.",
300 __FUNCTION__, mCameraIdStr.string(), physicalId.string());
301 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
302 "Invalid physical camera id");
303 }
304 }
305
306 CameraMetadata metadata(it.settings);
307 if (metadata.isEmpty()) {
308 ALOGE("%s: Camera %s: Sent empty metadata packet. Rejecting request.",
309 __FUNCTION__, mCameraIdStr.string());
310 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
311 "Request settings are empty");
312 }
313
314 physicalSettingsList.push_back({it.id, metadata});
315 }
316
317 if (streaming && (physicalSettingsList.size() > 1)) {
318 ALOGE("%s: Camera %s: Individual physical camera settings are not supported in "
319 "streaming requests. Rejecting request.", __FUNCTION__, mCameraIdStr.string());
320 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
321 "Streaming request contains individual physical requests");
322 }
323
324 if (!enforceRequestPermissions(physicalSettingsList.begin()->metadata)) {
325 // Callee logs
326 return STATUS_ERROR(CameraService::ERROR_PERMISSION_DENIED,
327 "Caller does not have permission to change restricted controls");
328 }
329
Emilian Peevaebbe412018-01-15 13:53:24 +0000330 physicalSettingsList.begin()->metadata.update(ANDROID_REQUEST_OUTPUT_STREAMS,
331 &outputStreamIds[0], outputStreamIds.size());
Jianing Wei90e59c92014-03-12 18:29:36 -0700332
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800333 if (request.mIsReprocess) {
Emilian Peevaebbe412018-01-15 13:53:24 +0000334 physicalSettingsList.begin()->metadata.update(ANDROID_REQUEST_INPUT_STREAMS,
335 &mInputStream.id, 1);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700336 }
337
Emilian Peevaebbe412018-01-15 13:53:24 +0000338 physicalSettingsList.begin()->metadata.update(ANDROID_REQUEST_ID,
339 &(submitInfo->mRequestId), /*size*/1);
Jianing Wei90e59c92014-03-12 18:29:36 -0700340 loopCounter++; // loopCounter starts from 1
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800341 ALOGV("%s: Camera %s: Creating request with ID %d (%d of %zu)",
342 __FUNCTION__, mCameraIdStr.string(), submitInfo->mRequestId,
343 loopCounter, requests.size());
Jianing Wei90e59c92014-03-12 18:29:36 -0700344
Emilian Peevaebbe412018-01-15 13:53:24 +0000345 metadataRequestList.push_back(physicalSettingsList);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700346 surfaceMapList.push_back(surfaceMap);
Jianing Wei90e59c92014-03-12 18:29:36 -0700347 }
348 mRequestIdCounter++;
349
350 if (streaming) {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700351 err = mDevice->setStreamingRequestList(metadataRequestList, surfaceMapList,
352 &(submitInfo->mLastFrameNumber));
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800353 if (err != OK) {
354 String8 msg = String8::format(
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800355 "Camera %s: Got error %s (%d) after trying to set streaming request",
356 mCameraIdStr.string(), strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800357 ALOGE("%s: %s", __FUNCTION__, msg.string());
358 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION,
359 msg.string());
Jianing Wei90e59c92014-03-12 18:29:36 -0700360 } else {
Shuzhen Wangc9ca6782016-04-26 13:40:31 -0700361 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700362 mStreamingRequestId = submitInfo->mRequestId;
Jianing Wei90e59c92014-03-12 18:29:36 -0700363 }
364 } else {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700365 err = mDevice->captureList(metadataRequestList, surfaceMapList,
366 &(submitInfo->mLastFrameNumber));
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800367 if (err != OK) {
368 String8 msg = String8::format(
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800369 "Camera %s: Got error %s (%d) after trying to submit capture request",
370 mCameraIdStr.string(), strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800371 ALOGE("%s: %s", __FUNCTION__, msg.string());
372 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION,
373 msg.string());
Jianing Wei90e59c92014-03-12 18:29:36 -0700374 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800375 ALOGV("%s: requestId = %d ", __FUNCTION__, submitInfo->mRequestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700376 }
377
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800378 ALOGV("%s: Camera %s: End of function", __FUNCTION__, mCameraIdStr.string());
Jianing Wei90e59c92014-03-12 18:29:36 -0700379 return res;
380}
381
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800382binder::Status CameraDeviceClient::cancelRequest(
383 int requestId,
384 /*out*/
385 int64_t* lastFrameNumber) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700386 ATRACE_CALL();
387 ALOGV("%s, requestId = %d", __FUNCTION__, requestId);
388
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800389 status_t err;
390 binder::Status res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700391
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800392 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700393
394 Mutex::Autolock icl(mBinderSerializationLock);
395
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800396 if (!mDevice.get()) {
397 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
398 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700399
Shuzhen Wangc9ca6782016-04-26 13:40:31 -0700400 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700401 if (mStreamingRequestId != requestId) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800402 String8 msg = String8::format("Camera %s: Canceling request ID %d doesn't match "
403 "current request ID %d", mCameraIdStr.string(), requestId, mStreamingRequestId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800404 ALOGE("%s: %s", __FUNCTION__, msg.string());
405 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Igor Murashkine7ee7632013-06-11 18:10:18 -0700406 }
407
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800408 err = mDevice->clearStreamingRequest(lastFrameNumber);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700409
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800410 if (err == OK) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800411 ALOGV("%s: Camera %s: Successfully cleared streaming request",
412 __FUNCTION__, mCameraIdStr.string());
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700413 mStreamingRequestId = REQUEST_ID_NONE;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800414 } else {
415 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800416 "Camera %s: Error clearing streaming request: %s (%d)",
417 mCameraIdStr.string(), strerror(-err), err);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700418 }
419
420 return res;
421}
422
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800423binder::Status CameraDeviceClient::beginConfigure() {
Ruben Brunkb2119af2014-05-09 19:57:56 -0700424 // TODO: Implement this.
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -0700425 ATRACE_CALL();
Zhijun He1fa89992015-06-01 15:44:31 -0700426 ALOGV("%s: Not implemented yet.", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800427 return binder::Status::ok();
Ruben Brunkb2119af2014-05-09 19:57:56 -0700428}
429
Emilian Peev5fbe0ba2017-10-20 15:45:45 +0100430binder::Status CameraDeviceClient::endConfigure(int operatingMode,
431 const hardware::camera2::impl::CameraMetadataNative& sessionParams) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -0700432 ATRACE_CALL();
Shuzhen Wang0129d522016-10-30 22:43:41 -0700433 ALOGV("%s: ending configure (%d input stream, %zu output surfaces)",
434 __FUNCTION__, mInputStream.configured ? 1 : 0,
435 mStreamMap.size());
Igor Murashkine2d167e2014-08-19 16:19:59 -0700436
Zhijun He0effd522016-03-04 10:22:27 -0800437 binder::Status res;
438 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
439
440 Mutex::Autolock icl(mBinderSerializationLock);
441
442 if (!mDevice.get()) {
443 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
444 }
445
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800446 if (operatingMode < 0) {
447 String8 msg = String8::format(
448 "Camera %s: Invalid operating mode %d requested", mCameraIdStr.string(), operatingMode);
449 ALOGE("%s: %s", __FUNCTION__, msg.string());
450 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
451 msg.string());
452 }
453
Zhijun He1fa89992015-06-01 15:44:31 -0700454 // Sanitize the high speed session against necessary capability bit.
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800455 bool isConstrainedHighSpeed = (operatingMode == ICameraDeviceUser::CONSTRAINED_HIGH_SPEED_MODE);
Zhijun He1fa89992015-06-01 15:44:31 -0700456 if (isConstrainedHighSpeed) {
457 CameraMetadata staticInfo = mDevice->info();
458 camera_metadata_entry_t entry = staticInfo.find(ANDROID_REQUEST_AVAILABLE_CAPABILITIES);
459 bool isConstrainedHighSpeedSupported = false;
460 for(size_t i = 0; i < entry.count; ++i) {
461 uint8_t capability = entry.data.u8[i];
462 if (capability == ANDROID_REQUEST_AVAILABLE_CAPABILITIES_CONSTRAINED_HIGH_SPEED_VIDEO) {
463 isConstrainedHighSpeedSupported = true;
464 break;
465 }
466 }
467 if (!isConstrainedHighSpeedSupported) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800468 String8 msg = String8::format(
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800469 "Camera %s: Try to create a constrained high speed configuration on a device"
470 " that doesn't support it.", mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800471 ALOGE("%s: %s", __FUNCTION__, msg.string());
472 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
473 msg.string());
Zhijun He1fa89992015-06-01 15:44:31 -0700474 }
475 }
476
Emilian Peev5fbe0ba2017-10-20 15:45:45 +0100477 status_t err = mDevice->configureStreams(sessionParams, operatingMode);
Eino-Ville Talvalaace80582016-03-18 13:27:35 -0700478 if (err == BAD_VALUE) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800479 String8 msg = String8::format("Camera %s: Unsupported set of inputs/outputs provided",
480 mCameraIdStr.string());
Zhijun He5d677d12016-05-29 16:52:39 -0700481 ALOGE("%s: %s", __FUNCTION__, msg.string());
482 res = STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Eino-Ville Talvalaace80582016-03-18 13:27:35 -0700483 } else if (err != OK) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800484 String8 msg = String8::format("Camera %s: Error configuring streams: %s (%d)",
485 mCameraIdStr.string(), strerror(-err), err);
Zhijun He5d677d12016-05-29 16:52:39 -0700486 ALOGE("%s: %s", __FUNCTION__, msg.string());
487 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800488 }
489
490 return res;
Ruben Brunkb2119af2014-05-09 19:57:56 -0700491}
492
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800493binder::Status CameraDeviceClient::deleteStream(int streamId) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700494 ATRACE_CALL();
495 ALOGV("%s (streamId = 0x%x)", __FUNCTION__, streamId);
496
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800497 binder::Status res;
498 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700499
500 Mutex::Autolock icl(mBinderSerializationLock);
501
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800502 if (!mDevice.get()) {
503 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
504 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700505
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700506 bool isInput = false;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700507 std::vector<sp<IBinder>> surfaces;
Zhijun He5d677d12016-05-29 16:52:39 -0700508 ssize_t dIndex = NAME_NOT_FOUND;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700509
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700510 if (mInputStream.configured && mInputStream.id == streamId) {
511 isInput = true;
512 } else {
513 // Guard against trying to delete non-created streams
514 for (size_t i = 0; i < mStreamMap.size(); ++i) {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700515 if (streamId == mStreamMap.valueAt(i).streamId()) {
516 surfaces.push_back(mStreamMap.keyAt(i));
517 }
518 }
519
520 // See if this stream is one of the deferred streams.
521 for (size_t i = 0; i < mDeferredStreams.size(); ++i) {
522 if (streamId == mDeferredStreams[i]) {
523 dIndex = i;
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700524 break;
525 }
526 }
527
Shuzhen Wang0129d522016-10-30 22:43:41 -0700528 if (surfaces.empty() && dIndex == NAME_NOT_FOUND) {
529 String8 msg = String8::format("Camera %s: Invalid stream ID (%d) specified, no such"
530 " stream created yet", mCameraIdStr.string(), streamId);
531 ALOGW("%s: %s", __FUNCTION__, msg.string());
532 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700533 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700534 }
535
536 // Also returns BAD_VALUE if stream ID was not valid
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800537 status_t err = mDevice->deleteStream(streamId);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700538
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800539 if (err != OK) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800540 String8 msg = String8::format("Camera %s: Unexpected error %s (%d) when deleting stream %d",
541 mCameraIdStr.string(), strerror(-err), err, streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800542 ALOGE("%s: %s", __FUNCTION__, msg.string());
543 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
544 } else {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700545 if (isInput) {
546 mInputStream.configured = false;
Zhijun He5d677d12016-05-29 16:52:39 -0700547 } else {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700548 for (auto& surface : surfaces) {
549 mStreamMap.removeItem(surface);
550 }
551
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800552 mConfiguredOutputs.removeItem(streamId);
553
Shuzhen Wang0129d522016-10-30 22:43:41 -0700554 if (dIndex != NAME_NOT_FOUND) {
555 mDeferredStreams.removeItemsAt(dIndex);
556 }
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700557 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700558 }
559
560 return res;
561}
562
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800563binder::Status CameraDeviceClient::createStream(
564 const hardware::camera2::params::OutputConfiguration &outputConfiguration,
565 /*out*/
566 int32_t* newStreamId) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700567 ATRACE_CALL();
Igor Murashkine7ee7632013-06-11 18:10:18 -0700568
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800569 binder::Status res;
570 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700571
572 Mutex::Autolock icl(mBinderSerializationLock);
573
Shuzhen Wang0129d522016-10-30 22:43:41 -0700574 const std::vector<sp<IGraphicBufferProducer>>& bufferProducers =
575 outputConfiguration.getGraphicBufferProducers();
576 size_t numBufferProducers = bufferProducers.size();
Shuzhen Wang758c2152017-01-10 18:26:18 -0800577 bool deferredConsumer = outputConfiguration.isDeferred();
578 bool isShared = outputConfiguration.isShared();
Shuzhen Wangc28189a2017-11-27 23:05:10 -0800579 String8 physicalCameraId = String8(outputConfiguration.getPhysicalCameraId());
Shuzhen Wang0129d522016-10-30 22:43:41 -0700580
581 if (numBufferProducers > MAX_SURFACES_PER_STREAM) {
582 ALOGE("%s: GraphicBufferProducer count %zu for stream exceeds limit of %d",
583 __FUNCTION__, bufferProducers.size(), MAX_SURFACES_PER_STREAM);
584 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, "Surface count is too high");
585 }
Shuzhen Wang758c2152017-01-10 18:26:18 -0800586 bool deferredConsumerOnly = deferredConsumer && numBufferProducers == 0;
Zhijun He5d677d12016-05-29 16:52:39 -0700587 int surfaceType = outputConfiguration.getSurfaceType();
588 bool validSurfaceType = ((surfaceType == OutputConfiguration::SURFACE_TYPE_SURFACE_VIEW) ||
589 (surfaceType == OutputConfiguration::SURFACE_TYPE_SURFACE_TEXTURE));
Shuzhen Wang0129d522016-10-30 22:43:41 -0700590
Zhijun He5d677d12016-05-29 16:52:39 -0700591 if (deferredConsumer && !validSurfaceType) {
592 ALOGE("%s: Target surface is invalid: bufferProducer = %p, surfaceType = %d.",
Shuzhen Wang0129d522016-10-30 22:43:41 -0700593 __FUNCTION__, bufferProducers[0].get(), surfaceType);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800594 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, "Target Surface is invalid");
Yin-Chia Yeh89f14da2014-06-10 16:05:44 -0700595 }
Zhijun He5d677d12016-05-29 16:52:39 -0700596
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800597 if (!mDevice.get()) {
598 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
599 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700600
Shuzhen Wangc28189a2017-11-27 23:05:10 -0800601 if (!checkPhysicalCameraId(physicalCameraId)) {
602 String8 msg = String8::format("Camera %s: Camera doesn't support physicalCameraId %s.",
603 mCameraIdStr.string(), physicalCameraId.string());
604 ALOGE("%s: %s", __FUNCTION__, msg.string());
605 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
606 }
Shuzhen Wang0129d522016-10-30 22:43:41 -0700607 std::vector<sp<Surface>> surfaces;
608 std::vector<sp<IBinder>> binders;
Zhijun He5d677d12016-05-29 16:52:39 -0700609 status_t err;
610
611 // Create stream for deferred surface case.
Shuzhen Wang0129d522016-10-30 22:43:41 -0700612 if (deferredConsumerOnly) {
Shuzhen Wang758c2152017-01-10 18:26:18 -0800613 return createDeferredSurfaceStreamLocked(outputConfiguration, isShared, newStreamId);
Zhijun He5d677d12016-05-29 16:52:39 -0700614 }
615
Shuzhen Wang758c2152017-01-10 18:26:18 -0800616 OutputStreamInfo streamInfo;
617 bool isStreamInfoValid = false;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700618 for (auto& bufferProducer : bufferProducers) {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700619 // Don't create multiple streams for the same target surface
Shuzhen Wang0129d522016-10-30 22:43:41 -0700620 sp<IBinder> binder = IInterface::asBinder(bufferProducer);
Shuzhen Wang758c2152017-01-10 18:26:18 -0800621 ssize_t index = mStreamMap.indexOfKey(binder);
622 if (index != NAME_NOT_FOUND) {
623 String8 msg = String8::format("Camera %s: Surface already has a stream created for it "
624 "(ID %zd)", mCameraIdStr.string(), index);
625 ALOGW("%s: %s", __FUNCTION__, msg.string());
626 return STATUS_ERROR(CameraService::ERROR_ALREADY_EXISTS, msg.string());
Shuzhen Wang0129d522016-10-30 22:43:41 -0700627 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700628
Shuzhen Wang758c2152017-01-10 18:26:18 -0800629 sp<Surface> surface;
630 res = createSurfaceFromGbp(streamInfo, isStreamInfoValid, surface, bufferProducer);
631
632 if (!res.isOk())
633 return res;
634
635 if (!isStreamInfoValid) {
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800636 // Streaming sharing is only supported for IMPLEMENTATION_DEFINED
637 // formats.
638 if (isShared && streamInfo.format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
639 String8 msg = String8::format("Camera %s: Stream sharing is only supported for "
640 "IMPLEMENTATION_DEFINED format", mCameraIdStr.string());
641 ALOGW("%s: %s", __FUNCTION__, msg.string());
642 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
643 }
Shuzhen Wang758c2152017-01-10 18:26:18 -0800644 isStreamInfoValid = true;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700645 }
646
Shuzhen Wang758c2152017-01-10 18:26:18 -0800647 binders.push_back(IInterface::asBinder(bufferProducer));
Shuzhen Wang0129d522016-10-30 22:43:41 -0700648 surfaces.push_back(surface);
Ruben Brunkbba75572014-11-20 17:29:50 -0800649 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700650
Zhijun He125684a2015-12-26 15:07:30 -0800651 int streamId = camera3::CAMERA3_STREAM_ID_INVALID;
Emilian Peev40ead602017-09-26 15:46:36 +0100652 std::vector<int> surfaceIds;
Shuzhen Wang758c2152017-01-10 18:26:18 -0800653 err = mDevice->createStream(surfaces, deferredConsumer, streamInfo.width,
654 streamInfo.height, streamInfo.format, streamInfo.dataSpace,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800655 static_cast<camera3_stream_rotation_t>(outputConfiguration.getRotation()),
Shuzhen Wangc28189a2017-11-27 23:05:10 -0800656 &streamId, physicalCameraId, &surfaceIds, outputConfiguration.getSurfaceSetID(),
657 isShared);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700658
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800659 if (err != OK) {
660 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800661 "Camera %s: Error creating output stream (%d x %d, fmt %x, dataSpace %x): %s (%d)",
Shuzhen Wang758c2152017-01-10 18:26:18 -0800662 mCameraIdStr.string(), streamInfo.width, streamInfo.height, streamInfo.format,
663 streamInfo.dataSpace, strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800664 } else {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700665 int i = 0;
666 for (auto& binder : binders) {
667 ALOGV("%s: mStreamMap add binder %p streamId %d, surfaceId %d",
668 __FUNCTION__, binder.get(), streamId, i);
Emilian Peev40ead602017-09-26 15:46:36 +0100669 mStreamMap.add(binder, StreamSurfaceId(streamId, surfaceIds[i]));
670 i++;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700671 }
Shuzhen Wang758c2152017-01-10 18:26:18 -0800672
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800673 mConfiguredOutputs.add(streamId, outputConfiguration);
Shuzhen Wang758c2152017-01-10 18:26:18 -0800674 mStreamInfoMap[streamId] = streamInfo;
675
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800676 ALOGV("%s: Camera %s: Successfully created a new stream ID %d for output surface"
Shuzhen Wang0129d522016-10-30 22:43:41 -0700677 " (%d x %d) with format 0x%x.",
Shuzhen Wang758c2152017-01-10 18:26:18 -0800678 __FUNCTION__, mCameraIdStr.string(), streamId, streamInfo.width,
679 streamInfo.height, streamInfo.format);
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -0700680
Zhijun He5d677d12016-05-29 16:52:39 -0700681 // Set transform flags to ensure preview to be rotated correctly.
682 res = setStreamTransformLocked(streamId);
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -0700683
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800684 *newStreamId = streamId;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700685 }
686
687 return res;
688}
689
Zhijun He5d677d12016-05-29 16:52:39 -0700690binder::Status CameraDeviceClient::createDeferredSurfaceStreamLocked(
691 const hardware::camera2::params::OutputConfiguration &outputConfiguration,
Shuzhen Wang758c2152017-01-10 18:26:18 -0800692 bool isShared,
Zhijun He5d677d12016-05-29 16:52:39 -0700693 /*out*/
694 int* newStreamId) {
695 int width, height, format, surfaceType;
Emilian Peev050f5dc2017-05-18 14:43:56 +0100696 uint64_t consumerUsage;
Zhijun He5d677d12016-05-29 16:52:39 -0700697 android_dataspace dataSpace;
698 status_t err;
699 binder::Status res;
700
701 if (!mDevice.get()) {
702 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
703 }
704
705 // Infer the surface info for deferred surface stream creation.
706 width = outputConfiguration.getWidth();
707 height = outputConfiguration.getHeight();
708 surfaceType = outputConfiguration.getSurfaceType();
709 format = HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED;
710 dataSpace = android_dataspace_t::HAL_DATASPACE_UNKNOWN;
711 // Hardcode consumer usage flags: SurfaceView--0x900, SurfaceTexture--0x100.
712 consumerUsage = GraphicBuffer::USAGE_HW_TEXTURE;
713 if (surfaceType == OutputConfiguration::SURFACE_TYPE_SURFACE_VIEW) {
714 consumerUsage |= GraphicBuffer::USAGE_HW_COMPOSER;
715 }
716 int streamId = camera3::CAMERA3_STREAM_ID_INVALID;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700717 std::vector<sp<Surface>> noSurface;
Emilian Peev40ead602017-09-26 15:46:36 +0100718 std::vector<int> surfaceIds;
Shuzhen Wangc28189a2017-11-27 23:05:10 -0800719 String8 physicalCameraId(outputConfiguration.getPhysicalCameraId());
Shuzhen Wang0129d522016-10-30 22:43:41 -0700720 err = mDevice->createStream(noSurface, /*hasDeferredConsumer*/true, width,
721 height, format, dataSpace,
Zhijun He5d677d12016-05-29 16:52:39 -0700722 static_cast<camera3_stream_rotation_t>(outputConfiguration.getRotation()),
Shuzhen Wangc28189a2017-11-27 23:05:10 -0800723 &streamId, physicalCameraId, &surfaceIds,
724 outputConfiguration.getSurfaceSetID(), isShared,
Emilian Peev40ead602017-09-26 15:46:36 +0100725 consumerUsage);
Zhijun He5d677d12016-05-29 16:52:39 -0700726
727 if (err != OK) {
728 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800729 "Camera %s: Error creating output stream (%d x %d, fmt %x, dataSpace %x): %s (%d)",
730 mCameraIdStr.string(), width, height, format, dataSpace, strerror(-err), err);
Zhijun He5d677d12016-05-29 16:52:39 -0700731 } else {
732 // Can not add streamId to mStreamMap here, as the surface is deferred. Add it to
733 // a separate list to track. Once the deferred surface is set, this id will be
734 // relocated to mStreamMap.
735 mDeferredStreams.push_back(streamId);
736
Shuzhen Wang758c2152017-01-10 18:26:18 -0800737 mStreamInfoMap.emplace(std::piecewise_construct, std::forward_as_tuple(streamId),
738 std::forward_as_tuple(width, height, format, dataSpace, consumerUsage));
739
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800740 ALOGV("%s: Camera %s: Successfully created a new stream ID %d for a deferred surface"
Zhijun He5d677d12016-05-29 16:52:39 -0700741 " (%d x %d) stream with format 0x%x.",
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800742 __FUNCTION__, mCameraIdStr.string(), streamId, width, height, format);
Zhijun He5d677d12016-05-29 16:52:39 -0700743
744 // Set transform flags to ensure preview to be rotated correctly.
745 res = setStreamTransformLocked(streamId);
746
747 *newStreamId = streamId;
748 }
749 return res;
750}
751
752binder::Status CameraDeviceClient::setStreamTransformLocked(int streamId) {
753 int32_t transform = 0;
754 status_t err;
755 binder::Status res;
756
757 if (!mDevice.get()) {
758 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
759 }
760
761 err = getRotationTransformLocked(&transform);
762 if (err != OK) {
763 // Error logged by getRotationTransformLocked.
764 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION,
765 "Unable to calculate rotation transform for new stream");
766 }
767
768 err = mDevice->setStreamTransform(streamId, transform);
769 if (err != OK) {
770 String8 msg = String8::format("Failed to set stream transform (stream id %d)",
771 streamId);
772 ALOGE("%s: %s", __FUNCTION__, msg.string());
773 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
774 }
775
776 return res;
777}
Ruben Brunkbba75572014-11-20 17:29:50 -0800778
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800779binder::Status CameraDeviceClient::createInputStream(
780 int width, int height, int format,
781 /*out*/
782 int32_t* newStreamId) {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700783
784 ATRACE_CALL();
785 ALOGV("%s (w = %d, h = %d, f = 0x%x)", __FUNCTION__, width, height, format);
786
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800787 binder::Status res;
788 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700789
790 Mutex::Autolock icl(mBinderSerializationLock);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800791
792 if (!mDevice.get()) {
793 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
794 }
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700795
796 if (mInputStream.configured) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800797 String8 msg = String8::format("Camera %s: Already has an input stream "
Yi Kong0f414de2017-12-15 13:48:50 -0800798 "configured (ID %d)", mCameraIdStr.string(), mInputStream.id);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800799 ALOGE("%s: %s", __FUNCTION__, msg.string() );
800 return STATUS_ERROR(CameraService::ERROR_ALREADY_EXISTS, msg.string());
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700801 }
802
803 int streamId = -1;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800804 status_t err = mDevice->createInputStream(width, height, format, &streamId);
805 if (err == OK) {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700806 mInputStream.configured = true;
807 mInputStream.width = width;
808 mInputStream.height = height;
809 mInputStream.format = format;
810 mInputStream.id = streamId;
811
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800812 ALOGV("%s: Camera %s: Successfully created a new input stream ID %d",
813 __FUNCTION__, mCameraIdStr.string(), streamId);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700814
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800815 *newStreamId = streamId;
816 } else {
817 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800818 "Camera %s: Error creating new input stream: %s (%d)", mCameraIdStr.string(),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800819 strerror(-err), err);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700820 }
821
822 return res;
823}
824
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800825binder::Status CameraDeviceClient::getInputSurface(/*out*/ view::Surface *inputSurface) {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700826
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800827 binder::Status res;
828 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
829
830 if (inputSurface == NULL) {
831 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, "Null input surface");
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700832 }
833
834 Mutex::Autolock icl(mBinderSerializationLock);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800835 if (!mDevice.get()) {
836 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
837 }
838 sp<IGraphicBufferProducer> producer;
839 status_t err = mDevice->getInputBufferProducer(&producer);
840 if (err != OK) {
841 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800842 "Camera %s: Error getting input Surface: %s (%d)",
843 mCameraIdStr.string(), strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800844 } else {
845 inputSurface->name = String16("CameraInput");
846 inputSurface->graphicBufferProducer = producer;
847 }
848 return res;
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700849}
850
Emilian Peev40ead602017-09-26 15:46:36 +0100851binder::Status CameraDeviceClient::updateOutputConfiguration(int streamId,
852 const hardware::camera2::params::OutputConfiguration &outputConfiguration) {
853 ATRACE_CALL();
854
855 binder::Status res;
856 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
857
858 Mutex::Autolock icl(mBinderSerializationLock);
859
860 if (!mDevice.get()) {
861 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
862 }
863
864 const std::vector<sp<IGraphicBufferProducer> >& bufferProducers =
865 outputConfiguration.getGraphicBufferProducers();
866 auto producerCount = bufferProducers.size();
867 if (producerCount == 0) {
868 ALOGE("%s: bufferProducers must not be empty", __FUNCTION__);
869 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
870 "bufferProducers must not be empty");
871 }
872
873 // The first output is the one associated with the output configuration.
874 // It should always be present, valid and the corresponding stream id should match.
875 sp<IBinder> binder = IInterface::asBinder(bufferProducers[0]);
876 ssize_t index = mStreamMap.indexOfKey(binder);
877 if (index == NAME_NOT_FOUND) {
878 ALOGE("%s: Outputconfiguration is invalid", __FUNCTION__);
879 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
880 "OutputConfiguration is invalid");
881 }
882 if (mStreamMap.valueFor(binder).streamId() != streamId) {
883 ALOGE("%s: Stream Id: %d provided doesn't match the id: %d in the stream map",
884 __FUNCTION__, streamId, mStreamMap.valueFor(binder).streamId());
885 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
886 "Stream id is invalid");
887 }
888
889 std::vector<size_t> removedSurfaceIds;
890 std::vector<sp<IBinder>> removedOutputs;
891 std::vector<sp<Surface>> newOutputs;
892 std::vector<OutputStreamInfo> streamInfos;
893 KeyedVector<sp<IBinder>, sp<IGraphicBufferProducer>> newOutputsMap;
894 for (auto &it : bufferProducers) {
895 newOutputsMap.add(IInterface::asBinder(it), it);
896 }
897
898 for (size_t i = 0; i < mStreamMap.size(); i++) {
899 ssize_t idx = newOutputsMap.indexOfKey(mStreamMap.keyAt(i));
900 if (idx == NAME_NOT_FOUND) {
901 if (mStreamMap[i].streamId() == streamId) {
902 removedSurfaceIds.push_back(mStreamMap[i].surfaceId());
903 removedOutputs.push_back(mStreamMap.keyAt(i));
904 }
905 } else {
906 if (mStreamMap[i].streamId() != streamId) {
907 ALOGE("%s: Output surface already part of a different stream", __FUNCTION__);
908 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
909 "Target Surface is invalid");
910 }
911 newOutputsMap.removeItemsAt(idx);
912 }
913 }
914
915 for (size_t i = 0; i < newOutputsMap.size(); i++) {
916 OutputStreamInfo outInfo;
917 sp<Surface> surface;
918 res = createSurfaceFromGbp(outInfo, /*isStreamInfoValid*/ false, surface,
919 newOutputsMap.valueAt(i));
920 if (!res.isOk())
921 return res;
922
923 // Stream sharing is only supported for IMPLEMENTATION_DEFINED
924 // formats.
925 if (outInfo.format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
926 String8 msg = String8::format("Camera %s: Stream sharing is only supported for "
927 "IMPLEMENTATION_DEFINED format", mCameraIdStr.string());
928 ALOGW("%s: %s", __FUNCTION__, msg.string());
929 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
930 }
931 streamInfos.push_back(outInfo);
932 newOutputs.push_back(surface);
933 }
934
935 //Trivial case no changes required
936 if (removedSurfaceIds.empty() && newOutputs.empty()) {
937 return binder::Status::ok();
938 }
939
940 KeyedVector<sp<Surface>, size_t> outputMap;
941 auto ret = mDevice->updateStream(streamId, newOutputs, streamInfos, removedSurfaceIds,
942 &outputMap);
943 if (ret != OK) {
944 switch (ret) {
945 case NAME_NOT_FOUND:
946 case BAD_VALUE:
947 case -EBUSY:
948 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
949 "Camera %s: Error updating stream: %s (%d)",
950 mCameraIdStr.string(), strerror(ret), ret);
951 break;
952 default:
953 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
954 "Camera %s: Error updating stream: %s (%d)",
955 mCameraIdStr.string(), strerror(ret), ret);
956 break;
957 }
958 } else {
959 for (const auto &it : removedOutputs) {
960 mStreamMap.removeItem(it);
961 }
962
963 for (size_t i = 0; i < outputMap.size(); i++) {
964 mStreamMap.add(IInterface::asBinder(outputMap.keyAt(i)->getIGraphicBufferProducer()),
965 StreamSurfaceId(streamId, outputMap.valueAt(i)));
966 }
967
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800968 mConfiguredOutputs.replaceValueFor(streamId, outputConfiguration);
969
Emilian Peev40ead602017-09-26 15:46:36 +0100970 ALOGV("%s: Camera %s: Successful stream ID %d update",
971 __FUNCTION__, mCameraIdStr.string(), streamId);
972 }
973
974 return res;
975}
976
Eman Copty6d7af0e2016-06-17 20:46:40 -0700977bool CameraDeviceClient::isPublicFormat(int32_t format)
978{
979 switch(format) {
980 case HAL_PIXEL_FORMAT_RGBA_8888:
981 case HAL_PIXEL_FORMAT_RGBX_8888:
982 case HAL_PIXEL_FORMAT_RGB_888:
983 case HAL_PIXEL_FORMAT_RGB_565:
984 case HAL_PIXEL_FORMAT_BGRA_8888:
985 case HAL_PIXEL_FORMAT_YV12:
986 case HAL_PIXEL_FORMAT_Y8:
987 case HAL_PIXEL_FORMAT_Y16:
988 case HAL_PIXEL_FORMAT_RAW16:
989 case HAL_PIXEL_FORMAT_RAW10:
990 case HAL_PIXEL_FORMAT_RAW12:
991 case HAL_PIXEL_FORMAT_RAW_OPAQUE:
992 case HAL_PIXEL_FORMAT_BLOB:
993 case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED:
994 case HAL_PIXEL_FORMAT_YCbCr_420_888:
995 case HAL_PIXEL_FORMAT_YCbCr_422_888:
996 case HAL_PIXEL_FORMAT_YCbCr_444_888:
997 case HAL_PIXEL_FORMAT_FLEX_RGB_888:
998 case HAL_PIXEL_FORMAT_FLEX_RGBA_8888:
999 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
1000 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
1001 case HAL_PIXEL_FORMAT_YCbCr_422_I:
1002 return true;
1003 default:
1004 return false;
1005 }
1006}
1007
Shuzhen Wang758c2152017-01-10 18:26:18 -08001008binder::Status CameraDeviceClient::createSurfaceFromGbp(
1009 OutputStreamInfo& streamInfo, bool isStreamInfoValid,
1010 sp<Surface>& surface, const sp<IGraphicBufferProducer>& gbp) {
1011
1012 // bufferProducer must be non-null
1013 if (gbp == nullptr) {
1014 String8 msg = String8::format("Camera %s: Surface is NULL", mCameraIdStr.string());
1015 ALOGW("%s: %s", __FUNCTION__, msg.string());
1016 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1017 }
1018 // HACK b/10949105
1019 // Query consumer usage bits to set async operation mode for
1020 // GLConsumer using controlledByApp parameter.
1021 bool useAsync = false;
Emilian Peev050f5dc2017-05-18 14:43:56 +01001022 uint64_t consumerUsage = 0;
Shuzhen Wang758c2152017-01-10 18:26:18 -08001023 status_t err;
Emilian Peev050f5dc2017-05-18 14:43:56 +01001024 if ((err = gbp->getConsumerUsage(&consumerUsage)) != OK) {
Shuzhen Wang758c2152017-01-10 18:26:18 -08001025 String8 msg = String8::format("Camera %s: Failed to query Surface consumer usage: %s (%d)",
1026 mCameraIdStr.string(), strerror(-err), err);
1027 ALOGE("%s: %s", __FUNCTION__, msg.string());
1028 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
1029 }
1030 if (consumerUsage & GraphicBuffer::USAGE_HW_TEXTURE) {
Emilian Peev050f5dc2017-05-18 14:43:56 +01001031 ALOGW("%s: Camera %s with consumer usage flag: %" PRIu64 ": Forcing asynchronous mode for stream",
Shuzhen Wang758c2152017-01-10 18:26:18 -08001032 __FUNCTION__, mCameraIdStr.string(), consumerUsage);
1033 useAsync = true;
1034 }
1035
Emilian Peev050f5dc2017-05-18 14:43:56 +01001036 uint64_t disallowedFlags = GraphicBuffer::USAGE_HW_VIDEO_ENCODER |
Shuzhen Wang758c2152017-01-10 18:26:18 -08001037 GRALLOC_USAGE_RENDERSCRIPT;
Emilian Peev050f5dc2017-05-18 14:43:56 +01001038 uint64_t allowedFlags = GraphicBuffer::USAGE_SW_READ_MASK |
Shuzhen Wang758c2152017-01-10 18:26:18 -08001039 GraphicBuffer::USAGE_HW_TEXTURE |
1040 GraphicBuffer::USAGE_HW_COMPOSER;
1041 bool flexibleConsumer = (consumerUsage & disallowedFlags) == 0 &&
1042 (consumerUsage & allowedFlags) != 0;
1043
1044 surface = new Surface(gbp, useAsync);
1045 ANativeWindow *anw = surface.get();
1046
1047 int width, height, format;
1048 android_dataspace dataSpace;
1049 if ((err = anw->query(anw, NATIVE_WINDOW_WIDTH, &width)) != OK) {
1050 String8 msg = String8::format("Camera %s: Failed to query Surface width: %s (%d)",
1051 mCameraIdStr.string(), strerror(-err), err);
1052 ALOGE("%s: %s", __FUNCTION__, msg.string());
1053 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
1054 }
1055 if ((err = anw->query(anw, NATIVE_WINDOW_HEIGHT, &height)) != OK) {
1056 String8 msg = String8::format("Camera %s: Failed to query Surface height: %s (%d)",
1057 mCameraIdStr.string(), strerror(-err), err);
1058 ALOGE("%s: %s", __FUNCTION__, msg.string());
1059 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
1060 }
1061 if ((err = anw->query(anw, NATIVE_WINDOW_FORMAT, &format)) != OK) {
1062 String8 msg = String8::format("Camera %s: Failed to query Surface format: %s (%d)",
1063 mCameraIdStr.string(), strerror(-err), err);
1064 ALOGE("%s: %s", __FUNCTION__, msg.string());
1065 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
1066 }
1067 if ((err = anw->query(anw, NATIVE_WINDOW_DEFAULT_DATASPACE,
1068 reinterpret_cast<int*>(&dataSpace))) != OK) {
1069 String8 msg = String8::format("Camera %s: Failed to query Surface dataspace: %s (%d)",
1070 mCameraIdStr.string(), strerror(-err), err);
1071 ALOGE("%s: %s", __FUNCTION__, msg.string());
1072 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
1073 }
1074
1075 // FIXME: remove this override since the default format should be
1076 // IMPLEMENTATION_DEFINED. b/9487482
1077 if (format >= HAL_PIXEL_FORMAT_RGBA_8888 &&
1078 format <= HAL_PIXEL_FORMAT_BGRA_8888) {
1079 ALOGW("%s: Camera %s: Overriding format %#x to IMPLEMENTATION_DEFINED",
1080 __FUNCTION__, mCameraIdStr.string(), format);
1081 format = HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED;
1082 }
1083 // Round dimensions to the nearest dimensions available for this format
1084 if (flexibleConsumer && isPublicFormat(format) &&
1085 !CameraDeviceClient::roundBufferDimensionNearest(width, height,
1086 format, dataSpace, mDevice->info(), /*out*/&width, /*out*/&height)) {
1087 String8 msg = String8::format("Camera %s: No supported stream configurations with "
1088 "format %#x defined, failed to create output stream",
1089 mCameraIdStr.string(), format);
1090 ALOGE("%s: %s", __FUNCTION__, msg.string());
1091 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1092 }
1093
1094 if (!isStreamInfoValid) {
1095 streamInfo.width = width;
1096 streamInfo.height = height;
1097 streamInfo.format = format;
1098 streamInfo.dataSpace = dataSpace;
1099 streamInfo.consumerUsage = consumerUsage;
1100 return binder::Status::ok();
1101 }
1102 if (width != streamInfo.width) {
1103 String8 msg = String8::format("Camera %s:Surface width doesn't match: %d vs %d",
1104 mCameraIdStr.string(), width, streamInfo.width);
1105 ALOGE("%s: %s", __FUNCTION__, msg.string());
1106 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1107 }
1108 if (height != streamInfo.height) {
1109 String8 msg = String8::format("Camera %s:Surface height doesn't match: %d vs %d",
1110 mCameraIdStr.string(), height, streamInfo.height);
1111 ALOGE("%s: %s", __FUNCTION__, msg.string());
1112 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1113 }
1114 if (format != streamInfo.format) {
1115 String8 msg = String8::format("Camera %s:Surface format doesn't match: %d vs %d",
1116 mCameraIdStr.string(), format, streamInfo.format);
1117 ALOGE("%s: %s", __FUNCTION__, msg.string());
1118 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1119 }
Shuzhen Wange8ecda92017-02-20 17:10:28 -08001120 if (format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
1121 if (dataSpace != streamInfo.dataSpace) {
1122 String8 msg = String8::format("Camera %s:Surface dataSpace doesn't match: %d vs %d",
1123 mCameraIdStr.string(), dataSpace, streamInfo.dataSpace);
1124 ALOGE("%s: %s", __FUNCTION__, msg.string());
1125 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1126 }
1127 //At the native side, there isn't a way to check whether 2 surfaces come from the same
1128 //surface class type. Use usage flag to approximate the comparison.
1129 if (consumerUsage != streamInfo.consumerUsage) {
1130 String8 msg = String8::format(
Emilian Peev050f5dc2017-05-18 14:43:56 +01001131 "Camera %s:Surface usage flag doesn't match %" PRIu64 " vs %" PRIu64 "",
Shuzhen Wange8ecda92017-02-20 17:10:28 -08001132 mCameraIdStr.string(), consumerUsage, streamInfo.consumerUsage);
1133 ALOGE("%s: %s", __FUNCTION__, msg.string());
1134 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1135 }
Shuzhen Wang758c2152017-01-10 18:26:18 -08001136 }
1137 return binder::Status::ok();
1138}
1139
Shuzhen Wangc28189a2017-11-27 23:05:10 -08001140bool CameraDeviceClient::checkPhysicalCameraId(const String8& physicalCameraId) {
1141 if (0 == physicalCameraId.size()) {
1142 return true;
1143 }
1144
1145 CameraMetadata staticInfo = mDevice->info();
1146 camera_metadata_entry_t entryCap;
1147 bool isLogicalCam = false;
1148
1149 entryCap = staticInfo.find(ANDROID_REQUEST_AVAILABLE_CAPABILITIES);
1150 for (size_t i = 0; i < entryCap.count; ++i) {
1151 uint8_t capability = entryCap.data.u8[i];
1152 if (capability == ANDROID_REQUEST_AVAILABLE_CAPABILITIES_LOGICAL_MULTI_CAMERA) {
1153 isLogicalCam = true;
1154 }
1155 }
1156 if (!isLogicalCam) {
1157 return false;
1158 }
1159
1160 camera_metadata_entry_t entryIds = staticInfo.find(ANDROID_LOGICAL_MULTI_CAMERA_PHYSICAL_IDS);
1161 const uint8_t* ids = entryIds.data.u8;
1162 size_t start = 0;
1163 for (size_t i = 0; i < entryIds.count; ++i) {
1164 if (ids[i] == '\0') {
1165 if (start != i) {
1166 String8 currentId((const char*)ids+start);
1167 if (currentId == physicalCameraId) {
1168 return true;
1169 }
1170 }
1171 start = i+1;
1172 }
1173 }
1174 return false;
1175}
1176
Ruben Brunkbba75572014-11-20 17:29:50 -08001177bool CameraDeviceClient::roundBufferDimensionNearest(int32_t width, int32_t height,
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -08001178 int32_t format, android_dataspace dataSpace, const CameraMetadata& info,
Ruben Brunkbba75572014-11-20 17:29:50 -08001179 /*out*/int32_t* outWidth, /*out*/int32_t* outHeight) {
1180
1181 camera_metadata_ro_entry streamConfigs =
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -08001182 (dataSpace == HAL_DATASPACE_DEPTH) ?
1183 info.find(ANDROID_DEPTH_AVAILABLE_DEPTH_STREAM_CONFIGURATIONS) :
Ruben Brunkbba75572014-11-20 17:29:50 -08001184 info.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS);
1185
1186 int32_t bestWidth = -1;
1187 int32_t bestHeight = -1;
1188
1189 // Iterate through listed stream configurations and find the one with the smallest euclidean
1190 // distance from the given dimensions for the given format.
1191 for (size_t i = 0; i < streamConfigs.count; i += 4) {
1192 int32_t fmt = streamConfigs.data.i32[i];
1193 int32_t w = streamConfigs.data.i32[i + 1];
1194 int32_t h = streamConfigs.data.i32[i + 2];
1195
1196 // Ignore input/output type for now
1197 if (fmt == format) {
1198 if (w == width && h == height) {
1199 bestWidth = width;
1200 bestHeight = height;
1201 break;
1202 } else if (w <= ROUNDING_WIDTH_CAP && (bestWidth == -1 ||
1203 CameraDeviceClient::euclidDistSquare(w, h, width, height) <
1204 CameraDeviceClient::euclidDistSquare(bestWidth, bestHeight, width, height))) {
1205 bestWidth = w;
1206 bestHeight = h;
1207 }
1208 }
1209 }
1210
1211 if (bestWidth == -1) {
1212 // Return false if no configurations for this format were listed
1213 return false;
1214 }
1215
1216 // Set the outputs to the closet width/height
1217 if (outWidth != NULL) {
1218 *outWidth = bestWidth;
1219 }
1220 if (outHeight != NULL) {
1221 *outHeight = bestHeight;
1222 }
1223
1224 // Return true if at least one configuration for this format was listed
1225 return true;
1226}
1227
1228int64_t CameraDeviceClient::euclidDistSquare(int32_t x0, int32_t y0, int32_t x1, int32_t y1) {
1229 int64_t d0 = x0 - x1;
1230 int64_t d1 = y0 - y1;
1231 return d0 * d0 + d1 * d1;
1232}
1233
Igor Murashkine7ee7632013-06-11 18:10:18 -07001234// Create a request object from a template.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001235binder::Status CameraDeviceClient::createDefaultRequest(int templateId,
1236 /*out*/
1237 hardware::camera2::impl::CameraMetadataNative* request)
Igor Murashkine7ee7632013-06-11 18:10:18 -07001238{
1239 ATRACE_CALL();
1240 ALOGV("%s (templateId = 0x%x)", __FUNCTION__, templateId);
1241
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001242 binder::Status res;
1243 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -07001244
1245 Mutex::Autolock icl(mBinderSerializationLock);
1246
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001247 if (!mDevice.get()) {
1248 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1249 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001250
1251 CameraMetadata metadata;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001252 status_t err;
1253 if ( (err = mDevice->createDefaultRequest(templateId, &metadata) ) == OK &&
Igor Murashkine7ee7632013-06-11 18:10:18 -07001254 request != NULL) {
1255
1256 request->swap(metadata);
Chien-Yu Chen9cd14022016-03-09 12:21:01 -08001257 } else if (err == BAD_VALUE) {
1258 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001259 "Camera %s: Template ID %d is invalid or not supported: %s (%d)",
1260 mCameraIdStr.string(), templateId, strerror(-err), err);
Chien-Yu Chen9cd14022016-03-09 12:21:01 -08001261
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001262 } else {
1263 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001264 "Camera %s: Error creating default request for template %d: %s (%d)",
1265 mCameraIdStr.string(), templateId, strerror(-err), err);
Igor Murashkine7ee7632013-06-11 18:10:18 -07001266 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001267 return res;
1268}
1269
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001270binder::Status CameraDeviceClient::getCameraInfo(
1271 /*out*/
1272 hardware::camera2::impl::CameraMetadataNative* info)
Igor Murashkine7ee7632013-06-11 18:10:18 -07001273{
1274 ATRACE_CALL();
1275 ALOGV("%s", __FUNCTION__);
1276
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001277 binder::Status res;
Igor Murashkine7ee7632013-06-11 18:10:18 -07001278
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001279 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -07001280
1281 Mutex::Autolock icl(mBinderSerializationLock);
1282
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001283 if (!mDevice.get()) {
1284 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1285 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001286
Igor Murashkin099b4572013-07-12 17:52:16 -07001287 if (info != NULL) {
1288 *info = mDevice->info(); // static camera metadata
1289 // TODO: merge with device-specific camera metadata
1290 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001291
1292 return res;
1293}
1294
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001295binder::Status CameraDeviceClient::waitUntilIdle()
Zhijun He2ab500c2013-07-23 08:02:53 -07001296{
1297 ATRACE_CALL();
1298 ALOGV("%s", __FUNCTION__);
1299
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001300 binder::Status res;
1301 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Zhijun He2ab500c2013-07-23 08:02:53 -07001302
1303 Mutex::Autolock icl(mBinderSerializationLock);
1304
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001305 if (!mDevice.get()) {
1306 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1307 }
Zhijun He2ab500c2013-07-23 08:02:53 -07001308
1309 // FIXME: Also need check repeating burst.
Shuzhen Wangc9ca6782016-04-26 13:40:31 -07001310 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001311 if (mStreamingRequestId != REQUEST_ID_NONE) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001312 String8 msg = String8::format(
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001313 "Camera %s: Try to waitUntilIdle when there are active streaming requests",
1314 mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001315 ALOGE("%s: %s", __FUNCTION__, msg.string());
1316 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
Zhijun He2ab500c2013-07-23 08:02:53 -07001317 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001318 status_t err = mDevice->waitUntilDrained();
1319 if (err != OK) {
1320 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001321 "Camera %s: Error waiting to drain: %s (%d)",
1322 mCameraIdStr.string(), strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001323 }
Zhijun He2ab500c2013-07-23 08:02:53 -07001324 ALOGV("%s Done", __FUNCTION__);
Zhijun He2ab500c2013-07-23 08:02:53 -07001325 return res;
1326}
1327
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001328binder::Status CameraDeviceClient::flush(
1329 /*out*/
1330 int64_t* lastFrameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001331 ATRACE_CALL();
1332 ALOGV("%s", __FUNCTION__);
1333
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001334 binder::Status res;
1335 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001336
1337 Mutex::Autolock icl(mBinderSerializationLock);
1338
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001339 if (!mDevice.get()) {
1340 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1341 }
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001342
Shuzhen Wangc9ca6782016-04-26 13:40:31 -07001343 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001344 mStreamingRequestId = REQUEST_ID_NONE;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001345 status_t err = mDevice->flush(lastFrameNumber);
1346 if (err != OK) {
1347 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001348 "Camera %s: Error flushing device: %s (%d)", mCameraIdStr.string(), strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001349 }
1350 return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001351}
1352
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001353binder::Status CameraDeviceClient::prepare(int streamId) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001354 ATRACE_CALL();
1355 ALOGV("%s", __FUNCTION__);
1356
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001357 binder::Status res;
1358 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001359
1360 Mutex::Autolock icl(mBinderSerializationLock);
1361
1362 // Guard against trying to prepare non-created streams
1363 ssize_t index = NAME_NOT_FOUND;
1364 for (size_t i = 0; i < mStreamMap.size(); ++i) {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001365 if (streamId == mStreamMap.valueAt(i).streamId()) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001366 index = i;
1367 break;
1368 }
1369 }
1370
1371 if (index == NAME_NOT_FOUND) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001372 String8 msg = String8::format("Camera %s: Invalid stream ID (%d) specified, no stream "
1373 "with that ID exists", mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001374 ALOGW("%s: %s", __FUNCTION__, msg.string());
1375 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001376 }
1377
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001378 // Also returns BAD_VALUE if stream ID was not valid, or stream already
1379 // has been used
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001380 status_t err = mDevice->prepare(streamId);
1381 if (err == BAD_VALUE) {
1382 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001383 "Camera %s: Stream %d has already been used, and cannot be prepared",
1384 mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001385 } else if (err != OK) {
1386 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001387 "Camera %s: Error preparing stream %d: %s (%d)", mCameraIdStr.string(), streamId,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001388 strerror(-err), err);
1389 }
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001390 return res;
1391}
1392
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001393binder::Status CameraDeviceClient::prepare2(int maxCount, int streamId) {
Ruben Brunkc78ac262015-08-13 17:58:46 -07001394 ATRACE_CALL();
1395 ALOGV("%s", __FUNCTION__);
1396
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001397 binder::Status res;
1398 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Ruben Brunkc78ac262015-08-13 17:58:46 -07001399
1400 Mutex::Autolock icl(mBinderSerializationLock);
1401
1402 // Guard against trying to prepare non-created streams
1403 ssize_t index = NAME_NOT_FOUND;
1404 for (size_t i = 0; i < mStreamMap.size(); ++i) {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001405 if (streamId == mStreamMap.valueAt(i).streamId()) {
Ruben Brunkc78ac262015-08-13 17:58:46 -07001406 index = i;
1407 break;
1408 }
1409 }
1410
1411 if (index == NAME_NOT_FOUND) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001412 String8 msg = String8::format("Camera %s: Invalid stream ID (%d) specified, no stream "
1413 "with that ID exists", mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001414 ALOGW("%s: %s", __FUNCTION__, msg.string());
1415 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Ruben Brunkc78ac262015-08-13 17:58:46 -07001416 }
1417
1418 if (maxCount <= 0) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001419 String8 msg = String8::format("Camera %s: maxCount (%d) must be greater than 0",
1420 mCameraIdStr.string(), maxCount);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001421 ALOGE("%s: %s", __FUNCTION__, msg.string());
1422 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Ruben Brunkc78ac262015-08-13 17:58:46 -07001423 }
1424
1425 // Also returns BAD_VALUE if stream ID was not valid, or stream already
1426 // has been used
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001427 status_t err = mDevice->prepare(maxCount, streamId);
1428 if (err == BAD_VALUE) {
1429 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001430 "Camera %s: Stream %d has already been used, and cannot be prepared",
1431 mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001432 } else if (err != OK) {
1433 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001434 "Camera %s: Error preparing stream %d: %s (%d)", mCameraIdStr.string(), streamId,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001435 strerror(-err), err);
1436 }
Ruben Brunkc78ac262015-08-13 17:58:46 -07001437
1438 return res;
1439}
1440
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001441binder::Status CameraDeviceClient::tearDown(int streamId) {
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001442 ATRACE_CALL();
1443 ALOGV("%s", __FUNCTION__);
1444
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001445 binder::Status res;
1446 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001447
1448 Mutex::Autolock icl(mBinderSerializationLock);
1449
1450 // Guard against trying to prepare non-created streams
1451 ssize_t index = NAME_NOT_FOUND;
1452 for (size_t i = 0; i < mStreamMap.size(); ++i) {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001453 if (streamId == mStreamMap.valueAt(i).streamId()) {
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001454 index = i;
1455 break;
1456 }
1457 }
1458
1459 if (index == NAME_NOT_FOUND) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001460 String8 msg = String8::format("Camera %s: Invalid stream ID (%d) specified, no stream "
1461 "with that ID exists", mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001462 ALOGW("%s: %s", __FUNCTION__, msg.string());
1463 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001464 }
1465
1466 // Also returns BAD_VALUE if stream ID was not valid or if the stream is in
1467 // use
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001468 status_t err = mDevice->tearDown(streamId);
1469 if (err == BAD_VALUE) {
1470 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001471 "Camera %s: Stream %d is still in use, cannot be torn down",
1472 mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001473 } else if (err != OK) {
1474 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001475 "Camera %s: Error tearing down stream %d: %s (%d)", mCameraIdStr.string(), streamId,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001476 strerror(-err), err);
1477 }
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001478
1479 return res;
1480}
1481
Shuzhen Wang758c2152017-01-10 18:26:18 -08001482binder::Status CameraDeviceClient::finalizeOutputConfigurations(int32_t streamId,
Zhijun He5d677d12016-05-29 16:52:39 -07001483 const hardware::camera2::params::OutputConfiguration &outputConfiguration) {
1484 ATRACE_CALL();
1485
1486 binder::Status res;
1487 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1488
1489 Mutex::Autolock icl(mBinderSerializationLock);
1490
Shuzhen Wang0129d522016-10-30 22:43:41 -07001491 const std::vector<sp<IGraphicBufferProducer> >& bufferProducers =
1492 outputConfiguration.getGraphicBufferProducers();
Zhijun He5d677d12016-05-29 16:52:39 -07001493
Shuzhen Wang0129d522016-10-30 22:43:41 -07001494 if (bufferProducers.size() == 0) {
1495 ALOGE("%s: bufferProducers must not be empty", __FUNCTION__);
Zhijun He5d677d12016-05-29 16:52:39 -07001496 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, "Target Surface is invalid");
1497 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07001498
Shuzhen Wang758c2152017-01-10 18:26:18 -08001499 // streamId should be in mStreamMap if this stream already has a surface attached
1500 // to it. Otherwise, it should be in mDeferredStreams.
1501 bool streamIdConfigured = false;
1502 ssize_t deferredStreamIndex = NAME_NOT_FOUND;
1503 for (size_t i = 0; i < mStreamMap.size(); i++) {
1504 if (mStreamMap.valueAt(i).streamId() == streamId) {
1505 streamIdConfigured = true;
1506 break;
1507 }
Zhijun He5d677d12016-05-29 16:52:39 -07001508 }
Shuzhen Wang758c2152017-01-10 18:26:18 -08001509 for (size_t i = 0; i < mDeferredStreams.size(); i++) {
1510 if (streamId == mDeferredStreams[i]) {
1511 deferredStreamIndex = i;
1512 break;
Shuzhen Wang0129d522016-10-30 22:43:41 -07001513 }
1514
Shuzhen Wang758c2152017-01-10 18:26:18 -08001515 }
1516 if (deferredStreamIndex == NAME_NOT_FOUND && !streamIdConfigured) {
1517 String8 msg = String8::format("Camera %s: deferred surface is set to a unknown stream"
1518 "(ID %d)", mCameraIdStr.string(), streamId);
1519 ALOGW("%s: %s", __FUNCTION__, msg.string());
1520 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Zhijun He5d677d12016-05-29 16:52:39 -07001521 }
1522
Shuzhen Wang88fd0052017-02-09 16:40:07 -08001523 if (mStreamInfoMap[streamId].finalized) {
1524 String8 msg = String8::format("Camera %s: finalizeOutputConfigurations has been called"
1525 " on stream ID %d", mCameraIdStr.string(), streamId);
1526 ALOGW("%s: %s", __FUNCTION__, msg.string());
1527 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1528 }
1529
Zhijun He5d677d12016-05-29 16:52:39 -07001530 if (!mDevice.get()) {
1531 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1532 }
1533
Shuzhen Wang758c2152017-01-10 18:26:18 -08001534 std::vector<sp<Surface>> consumerSurfaces;
Shuzhen Wang758c2152017-01-10 18:26:18 -08001535 for (auto& bufferProducer : bufferProducers) {
1536 // Don't create multiple streams for the same target surface
Zhijun He5d677d12016-05-29 16:52:39 -07001537 ssize_t index = mStreamMap.indexOfKey(IInterface::asBinder(bufferProducer));
1538 if (index != NAME_NOT_FOUND) {
Shuzhen Wang758c2152017-01-10 18:26:18 -08001539 ALOGV("Camera %s: Surface already has a stream created "
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001540 " for it (ID %zd)", mCameraIdStr.string(), index);
Shuzhen Wang758c2152017-01-10 18:26:18 -08001541 continue;
Zhijun He5d677d12016-05-29 16:52:39 -07001542 }
Shuzhen Wang758c2152017-01-10 18:26:18 -08001543
1544 sp<Surface> surface;
1545 res = createSurfaceFromGbp(mStreamInfoMap[streamId], true /*isStreamInfoValid*/,
1546 surface, bufferProducer);
1547
1548 if (!res.isOk())
1549 return res;
1550
1551 consumerSurfaces.push_back(surface);
Zhijun He5d677d12016-05-29 16:52:39 -07001552 }
1553
Shuzhen Wanga81ce342017-04-13 15:18:36 -07001554 // Gracefully handle case where finalizeOutputConfigurations is called
1555 // without any new surface.
1556 if (consumerSurfaces.size() == 0) {
1557 mStreamInfoMap[streamId].finalized = true;
1558 return res;
1559 }
1560
Zhijun He5d677d12016-05-29 16:52:39 -07001561 // Finish the deferred stream configuration with the surface.
Shuzhen Wang758c2152017-01-10 18:26:18 -08001562 status_t err;
Emilian Peev40ead602017-09-26 15:46:36 +01001563 std::vector<int> consumerSurfaceIds;
1564 err = mDevice->setConsumerSurfaces(streamId, consumerSurfaces, &consumerSurfaceIds);
Zhijun He5d677d12016-05-29 16:52:39 -07001565 if (err == OK) {
Shuzhen Wang758c2152017-01-10 18:26:18 -08001566 for (size_t i = 0; i < consumerSurfaces.size(); i++) {
1567 sp<IBinder> binder = IInterface::asBinder(
1568 consumerSurfaces[i]->getIGraphicBufferProducer());
Emilian Peev40ead602017-09-26 15:46:36 +01001569 ALOGV("%s: mStreamMap add binder %p streamId %d, surfaceId %d", __FUNCTION__,
Shuzhen Wang758c2152017-01-10 18:26:18 -08001570 binder.get(), streamId, consumerSurfaceIds[i]);
1571 mStreamMap.add(binder, StreamSurfaceId(streamId, consumerSurfaceIds[i]));
1572 }
1573 if (deferredStreamIndex != NAME_NOT_FOUND) {
1574 mDeferredStreams.removeItemsAt(deferredStreamIndex);
Shuzhen Wang0129d522016-10-30 22:43:41 -07001575 }
Shuzhen Wang88fd0052017-02-09 16:40:07 -08001576 mStreamInfoMap[streamId].finalized = true;
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -08001577 mConfiguredOutputs.replaceValueFor(streamId, outputConfiguration);
Zhijun He5d677d12016-05-29 16:52:39 -07001578 } else if (err == NO_INIT) {
1579 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001580 "Camera %s: Deferred surface is invalid: %s (%d)",
1581 mCameraIdStr.string(), strerror(-err), err);
Zhijun He5d677d12016-05-29 16:52:39 -07001582 } else {
1583 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001584 "Camera %s: Error setting output stream deferred surface: %s (%d)",
1585 mCameraIdStr.string(), strerror(-err), err);
Zhijun He5d677d12016-05-29 16:52:39 -07001586 }
1587
1588 return res;
1589}
1590
Igor Murashkine7ee7632013-06-11 18:10:18 -07001591status_t CameraDeviceClient::dump(int fd, const Vector<String16>& args) {
Eino-Ville Talvalac4003962016-01-13 10:07:04 -08001592 return BasicClient::dump(fd, args);
1593}
1594
1595status_t CameraDeviceClient::dumpClient(int fd, const Vector<String16>& args) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001596 dprintf(fd, " CameraDeviceClient[%s] (%p) dump:\n",
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001597 mCameraIdStr.string(),
Eino-Ville Talvalae992e752014-11-07 16:17:48 -08001598 (getRemoteCallback() != NULL ?
Marco Nelissenf8880202014-11-14 07:58:25 -08001599 IInterface::asBinder(getRemoteCallback()).get() : NULL) );
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001600 dprintf(fd, " Current client UID %u\n", mClientUid);
Igor Murashkine7ee7632013-06-11 18:10:18 -07001601
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001602 dprintf(fd, " State:\n");
1603 dprintf(fd, " Request ID counter: %d\n", mRequestIdCounter);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001604 if (mInputStream.configured) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001605 dprintf(fd, " Current input stream ID: %d\n", mInputStream.id);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001606 } else {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001607 dprintf(fd, " No input stream configured.\n");
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001608 }
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001609 if (!mStreamMap.isEmpty()) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001610 dprintf(fd, " Current output stream/surface IDs:\n");
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001611 for (size_t i = 0; i < mStreamMap.size(); i++) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001612 dprintf(fd, " Stream %d Surface %d\n",
Shuzhen Wang0129d522016-10-30 22:43:41 -07001613 mStreamMap.valueAt(i).streamId(),
1614 mStreamMap.valueAt(i).surfaceId());
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001615 }
Zhijun He5d677d12016-05-29 16:52:39 -07001616 } else if (!mDeferredStreams.isEmpty()) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001617 dprintf(fd, " Current deferred surface output stream IDs:\n");
Zhijun He5d677d12016-05-29 16:52:39 -07001618 for (auto& streamId : mDeferredStreams) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001619 dprintf(fd, " Stream %d\n", streamId);
Zhijun He5d677d12016-05-29 16:52:39 -07001620 }
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001621 } else {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001622 dprintf(fd, " No output streams configured.\n");
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001623 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001624 // TODO: print dynamic/request section from most recent requests
1625 mFrameProcessor->dump(fd, args);
1626
1627 return dumpDevice(fd, args);
1628}
1629
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001630void CameraDeviceClient::notifyError(int32_t errorCode,
Jianing Weicb0652e2014-03-12 18:29:36 -07001631 const CaptureResultExtras& resultExtras) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001632 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001633 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001634
1635 if (remoteCb != 0) {
Jianing Weicb0652e2014-03-12 18:29:36 -07001636 remoteCb->onDeviceError(errorCode, resultExtras);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001637 }
1638}
1639
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001640void CameraDeviceClient::notifyRepeatingRequestError(long lastFrameNumber) {
1641 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
1642
1643 if (remoteCb != 0) {
Yin-Chia Yeh8ca23dc2017-09-05 18:15:56 -07001644 remoteCb->onRepeatingRequestError(lastFrameNumber, mStreamingRequestId);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001645 }
1646
Shuzhen Wangc9ca6782016-04-26 13:40:31 -07001647 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001648 mStreamingRequestId = REQUEST_ID_NONE;
1649}
1650
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001651void CameraDeviceClient::notifyIdle() {
1652 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001653 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001654
1655 if (remoteCb != 0) {
1656 remoteCb->onDeviceIdle();
1657 }
Eino-Ville Talvala412fe562015-08-20 17:08:32 -07001658 Camera2ClientBase::notifyIdle();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001659}
1660
Jianing Weicb0652e2014-03-12 18:29:36 -07001661void CameraDeviceClient::notifyShutter(const CaptureResultExtras& resultExtras,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001662 nsecs_t timestamp) {
1663 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001664 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001665 if (remoteCb != 0) {
Jianing Weicb0652e2014-03-12 18:29:36 -07001666 remoteCb->onCaptureStarted(resultExtras, timestamp);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001667 }
Eino-Ville Talvala412fe562015-08-20 17:08:32 -07001668 Camera2ClientBase::notifyShutter(resultExtras, timestamp);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001669}
1670
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001671void CameraDeviceClient::notifyPrepared(int streamId) {
1672 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001673 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001674 if (remoteCb != 0) {
1675 remoteCb->onPrepared(streamId);
1676 }
1677}
1678
Shuzhen Wang9d066012016-09-30 11:30:20 -07001679void CameraDeviceClient::notifyRequestQueueEmpty() {
1680 // Thread safe. Don't bother locking.
1681 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
1682 if (remoteCb != 0) {
1683 remoteCb->onRequestQueueEmpty();
1684 }
1685}
1686
Igor Murashkine7ee7632013-06-11 18:10:18 -07001687void CameraDeviceClient::detachDevice() {
1688 if (mDevice == 0) return;
1689
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001690 ALOGV("Camera %s: Stopping processors", mCameraIdStr.string());
Igor Murashkine7ee7632013-06-11 18:10:18 -07001691
1692 mFrameProcessor->removeListener(FRAME_PROCESSOR_LISTENER_MIN_ID,
1693 FRAME_PROCESSOR_LISTENER_MAX_ID,
1694 /*listener*/this);
1695 mFrameProcessor->requestExit();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001696 ALOGV("Camera %s: Waiting for threads", mCameraIdStr.string());
Igor Murashkine7ee7632013-06-11 18:10:18 -07001697 mFrameProcessor->join();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001698 ALOGV("Camera %s: Disconnecting device", mCameraIdStr.string());
Igor Murashkine7ee7632013-06-11 18:10:18 -07001699
1700 // WORKAROUND: HAL refuses to disconnect while there's streams in flight
1701 {
1702 mDevice->clearStreamingRequest();
1703
1704 status_t code;
1705 if ((code = mDevice->waitUntilDrained()) != OK) {
1706 ALOGE("%s: waitUntilDrained failed with code 0x%x", __FUNCTION__,
1707 code);
1708 }
1709 }
1710
1711 Camera2ClientBase::detachDevice();
1712}
1713
1714/** Device-related methods */
Jianing Weicb0652e2014-03-12 18:29:36 -07001715void CameraDeviceClient::onResultAvailable(const CaptureResult& result) {
Igor Murashkine7ee7632013-06-11 18:10:18 -07001716 ATRACE_CALL();
1717 ALOGV("%s", __FUNCTION__);
1718
Igor Murashkin4fb55c12013-08-29 17:43:01 -07001719 // Thread-safe. No lock necessary.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001720 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = mRemoteCallback;
Igor Murashkin4fb55c12013-08-29 17:43:01 -07001721 if (remoteCb != NULL) {
Shuzhen Wang5c22c152017-12-31 17:12:25 -08001722 remoteCb->onResultReceived(result.mMetadata, result.mResultExtras,
1723 result.mPhysicalMetadatas);
Igor Murashkine7ee7632013-06-11 18:10:18 -07001724 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001725}
1726
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001727binder::Status CameraDeviceClient::checkPidStatus(const char* checkLocation) {
Eino-Ville Talvala6192b892016-04-04 12:31:18 -07001728 if (mDisconnected) {
1729 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED,
1730 "The camera device has been disconnected");
1731 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001732 status_t res = checkPid(checkLocation);
1733 return (res == OK) ? binder::Status::ok() :
1734 STATUS_ERROR(CameraService::ERROR_PERMISSION_DENIED,
1735 "Attempt to use camera from a different process than original client");
1736}
1737
Igor Murashkine7ee7632013-06-11 18:10:18 -07001738// TODO: move to Camera2ClientBase
1739bool CameraDeviceClient::enforceRequestPermissions(CameraMetadata& metadata) {
1740
1741 const int pid = IPCThreadState::self()->getCallingPid();
1742 const int selfPid = getpid();
1743 camera_metadata_entry_t entry;
1744
1745 /**
1746 * Mixin default important security values
1747 * - android.led.transmit = defaulted ON
1748 */
1749 CameraMetadata staticInfo = mDevice->info();
1750 entry = staticInfo.find(ANDROID_LED_AVAILABLE_LEDS);
1751 for(size_t i = 0; i < entry.count; ++i) {
1752 uint8_t led = entry.data.u8[i];
1753
1754 switch(led) {
1755 case ANDROID_LED_AVAILABLE_LEDS_TRANSMIT: {
1756 uint8_t transmitDefault = ANDROID_LED_TRANSMIT_ON;
1757 if (!metadata.exists(ANDROID_LED_TRANSMIT)) {
1758 metadata.update(ANDROID_LED_TRANSMIT,
1759 &transmitDefault, 1);
1760 }
1761 break;
1762 }
1763 }
1764 }
1765
1766 // We can do anything!
1767 if (pid == selfPid) {
1768 return true;
1769 }
1770
1771 /**
1772 * Permission check special fields in the request
1773 * - android.led.transmit = android.permission.CAMERA_DISABLE_TRANSMIT
1774 */
1775 entry = metadata.find(ANDROID_LED_TRANSMIT);
1776 if (entry.count > 0 && entry.data.u8[0] != ANDROID_LED_TRANSMIT_ON) {
1777 String16 permissionString =
1778 String16("android.permission.CAMERA_DISABLE_TRANSMIT_LED");
1779 if (!checkCallingPermission(permissionString)) {
1780 const int uid = IPCThreadState::self()->getCallingUid();
1781 ALOGE("Permission Denial: "
1782 "can't disable transmit LED pid=%d, uid=%d", pid, uid);
1783 return false;
1784 }
1785 }
1786
1787 return true;
1788}
1789
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -07001790status_t CameraDeviceClient::getRotationTransformLocked(int32_t* transform) {
1791 ALOGV("%s: begin", __FUNCTION__);
1792
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -07001793 const CameraMetadata& staticInfo = mDevice->info();
Ruben Brunk5698d442014-06-18 10:39:40 -07001794 return CameraUtils::getRotationTransform(staticInfo, transform);
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -07001795}
1796
Igor Murashkine7ee7632013-06-11 18:10:18 -07001797} // namespace android