blob: 8ff8ac54aca11f39b24280a910991d0efe105dec [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,
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -080050 int api1CameraId,
Igor Murashkine7ee7632013-06-11 18:10:18 -070051 int cameraFacing,
52 int clientPid,
53 uid_t clientUid,
54 int servicePid) :
Eino-Ville Talvalae992e752014-11-07 16:17:48 -080055 BasicClient(cameraService,
Marco Nelissenf8880202014-11-14 07:58:25 -080056 IInterface::asBinder(remoteCallback),
Eino-Ville Talvalae992e752014-11-07 16:17:48 -080057 clientPackageName,
58 cameraId,
59 cameraFacing,
60 clientPid,
61 clientUid,
62 servicePid),
Igor Murashkine7ee7632013-06-11 18:10:18 -070063 mRemoteCallback(remoteCallback) {
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -080064 // We don't need it for API2 clients, but Camera2ClientBase requires it.
65 (void) api1CameraId;
Igor Murashkine7ee7632013-06-11 18:10:18 -070066}
Igor Murashkine7ee7632013-06-11 18:10:18 -070067
68// Interface used by CameraService
69
70CameraDeviceClient::CameraDeviceClient(const sp<CameraService>& cameraService,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080071 const sp<hardware::camera2::ICameraDeviceCallbacks>& remoteCallback,
72 const String16& clientPackageName,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080073 const String8& cameraId,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080074 int cameraFacing,
75 int clientPid,
76 uid_t clientUid,
77 int servicePid) :
Igor Murashkine7ee7632013-06-11 18:10:18 -070078 Camera2ClientBase(cameraService, remoteCallback, clientPackageName,
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -080079 cameraId, /*API1 camera ID*/ -1,
80 cameraFacing, clientPid, clientUid, servicePid),
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -070081 mInputStream(),
Chien-Yu Chene8c535e2016-04-14 12:18:26 -070082 mStreamingRequestId(REQUEST_ID_NONE),
Igor Murashkine7ee7632013-06-11 18:10:18 -070083 mRequestIdCounter(0) {
84
85 ATRACE_CALL();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080086 ALOGI("CameraDeviceClient %s: Opened", cameraId.string());
Igor Murashkine7ee7632013-06-11 18:10:18 -070087}
88
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080089status_t CameraDeviceClient::initialize(sp<CameraProviderManager> manager) {
90 return initializeImpl(manager);
91}
92
93template<typename TProviderPtr>
94status_t CameraDeviceClient::initializeImpl(TProviderPtr providerPtr) {
Igor Murashkine7ee7632013-06-11 18:10:18 -070095 ATRACE_CALL();
96 status_t res;
97
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080098 res = Camera2ClientBase::initialize(providerPtr);
Igor Murashkine7ee7632013-06-11 18:10:18 -070099 if (res != OK) {
100 return res;
101 }
102
103 String8 threadName;
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -0700104 mFrameProcessor = new FrameProcessorBase(mDevice);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800105 threadName = String8::format("CDU-%s-FrameProc", mCameraIdStr.string());
Igor Murashkine7ee7632013-06-11 18:10:18 -0700106 mFrameProcessor->run(threadName.string());
107
108 mFrameProcessor->registerListener(FRAME_PROCESSOR_LISTENER_MIN_ID,
109 FRAME_PROCESSOR_LISTENER_MAX_ID,
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -0800110 /*listener*/this,
Zhijun He25a0aef2014-06-25 11:40:02 -0700111 /*sendPartials*/true);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700112
113 return OK;
114}
115
116CameraDeviceClient::~CameraDeviceClient() {
117}
118
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800119binder::Status CameraDeviceClient::submitRequest(
120 const hardware::camera2::CaptureRequest& request,
121 bool streaming,
122 /*out*/
123 hardware::camera2::utils::SubmitInfo *submitInfo) {
124 std::vector<hardware::camera2::CaptureRequest> requestList = { request };
125 return submitRequestList(requestList, streaming, submitInfo);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700126}
127
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800128binder::Status CameraDeviceClient::insertGbpLocked(const sp<IGraphicBufferProducer>& gbp,
Emilian Peevf873aa52018-01-26 14:58:28 +0000129 SurfaceMap* outSurfaceMap, Vector<int32_t>* outputStreamIds, int32_t *currentStreamId) {
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800130 int idx = mStreamMap.indexOfKey(IInterface::asBinder(gbp));
131
132 // Trying to submit request with surface that wasn't created
133 if (idx == NAME_NOT_FOUND) {
134 ALOGE("%s: Camera %s: Tried to submit a request with a surface that"
135 " we have not called createStream on",
136 __FUNCTION__, mCameraIdStr.string());
137 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
138 "Request targets Surface that is not part of current capture session");
139 }
140
141 const StreamSurfaceId& streamSurfaceId = mStreamMap.valueAt(idx);
142 if (outSurfaceMap->find(streamSurfaceId.streamId()) == outSurfaceMap->end()) {
143 (*outSurfaceMap)[streamSurfaceId.streamId()] = std::vector<size_t>();
144 outputStreamIds->push_back(streamSurfaceId.streamId());
145 }
146 (*outSurfaceMap)[streamSurfaceId.streamId()].push_back(streamSurfaceId.surfaceId());
147
148 ALOGV("%s: Camera %s: Appending output stream %d surface %d to request",
149 __FUNCTION__, mCameraIdStr.string(), streamSurfaceId.streamId(),
150 streamSurfaceId.surfaceId());
151
Emilian Peevf873aa52018-01-26 14:58:28 +0000152 if (currentStreamId != nullptr) {
153 *currentStreamId = streamSurfaceId.streamId();
154 }
155
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800156 return binder::Status::ok();
157}
158
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800159binder::Status CameraDeviceClient::submitRequestList(
160 const std::vector<hardware::camera2::CaptureRequest>& requests,
161 bool streaming,
162 /*out*/
163 hardware::camera2::utils::SubmitInfo *submitInfo) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700164 ATRACE_CALL();
Mark Salyzyn50468412014-06-18 16:33:43 -0700165 ALOGV("%s-start of function. Request list size %zu", __FUNCTION__, requests.size());
Jianing Wei90e59c92014-03-12 18:29:36 -0700166
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800167 binder::Status res = binder::Status::ok();
168 status_t err;
169 if ( !(res = checkPidStatus(__FUNCTION__) ).isOk()) {
170 return res;
171 }
Jianing Wei90e59c92014-03-12 18:29:36 -0700172
173 Mutex::Autolock icl(mBinderSerializationLock);
174
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800175 if (!mDevice.get()) {
176 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
177 }
Jianing Wei90e59c92014-03-12 18:29:36 -0700178
179 if (requests.empty()) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800180 ALOGE("%s: Camera %s: Sent null request. Rejecting request.",
181 __FUNCTION__, mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800182 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, "Empty request list");
Jianing Wei90e59c92014-03-12 18:29:36 -0700183 }
184
Emilian Peevaebbe412018-01-15 13:53:24 +0000185 List<const CameraDeviceBase::PhysicalCameraSettingsList> metadataRequestList;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700186 std::list<const SurfaceMap> surfaceMapList;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800187 submitInfo->mRequestId = mRequestIdCounter;
Jianing Wei90e59c92014-03-12 18:29:36 -0700188 uint32_t loopCounter = 0;
189
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800190 for (auto&& request: requests) {
191 if (request.mIsReprocess) {
Chien-Yu Chened0412e2015-04-27 15:04:22 -0700192 if (!mInputStream.configured) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800193 ALOGE("%s: Camera %s: no input stream is configured.", __FUNCTION__,
194 mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800195 return STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800196 "No input configured for camera %s but request is for reprocessing",
197 mCameraIdStr.string());
Chien-Yu Chened0412e2015-04-27 15:04:22 -0700198 } else if (streaming) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800199 ALOGE("%s: Camera %s: streaming reprocess requests not supported.", __FUNCTION__,
200 mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800201 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
202 "Repeating reprocess requests not supported");
Emilian Peevaebbe412018-01-15 13:53:24 +0000203 } else if (request.mPhysicalCameraSettings.size() > 1) {
204 ALOGE("%s: Camera %s: reprocess requests not supported for "
205 "multiple physical cameras.", __FUNCTION__,
206 mCameraIdStr.string());
207 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
208 "Reprocess requests not supported for multiple cameras");
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700209 }
Jianing Wei90e59c92014-03-12 18:29:36 -0700210 }
211
Emilian Peevaebbe412018-01-15 13:53:24 +0000212 if (request.mPhysicalCameraSettings.empty()) {
213 ALOGE("%s: Camera %s: request doesn't contain any settings.", __FUNCTION__,
214 mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800215 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
Emilian Peevaebbe412018-01-15 13:53:24 +0000216 "Request doesn't contain any settings");
217 }
218
219 //The first capture settings should always match the logical camera id
220 String8 logicalId(request.mPhysicalCameraSettings.begin()->id.c_str());
221 if (mDevice->getId() != logicalId) {
222 ALOGE("%s: Camera %s: Invalid camera request settings.", __FUNCTION__,
223 mCameraIdStr.string());
224 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
225 "Invalid camera request settings");
226 }
227
Emilian Peevaebbe412018-01-15 13:53:24 +0000228 if (request.mSurfaceList.isEmpty() && request.mStreamIdxList.size() == 0) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800229 ALOGE("%s: Camera %s: Requests must have at least one surface target. "
230 "Rejecting request.", __FUNCTION__, mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800231 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
232 "Request has no output targets");
Jianing Wei90e59c92014-03-12 18:29:36 -0700233 }
234
Jianing Wei90e59c92014-03-12 18:29:36 -0700235 /**
Shuzhen Wang0129d522016-10-30 22:43:41 -0700236 * Write in the output stream IDs and map from stream ID to surface ID
237 * which we calculate from the capture request's list of surface target
Jianing Wei90e59c92014-03-12 18:29:36 -0700238 */
Shuzhen Wang0129d522016-10-30 22:43:41 -0700239 SurfaceMap surfaceMap;
Jianing Wei90e59c92014-03-12 18:29:36 -0700240 Vector<int32_t> outputStreamIds;
Emilian Peevf873aa52018-01-26 14:58:28 +0000241 std::vector<std::string> requestedPhysicalIds;
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800242 if (request.mSurfaceList.size() > 0) {
243 for (sp<Surface> surface : request.mSurfaceList) {
244 if (surface == 0) continue;
Jianing Wei90e59c92014-03-12 18:29:36 -0700245
Emilian Peevf873aa52018-01-26 14:58:28 +0000246 int32_t streamId;
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800247 sp<IGraphicBufferProducer> gbp = surface->getIGraphicBufferProducer();
Emilian Peevf873aa52018-01-26 14:58:28 +0000248 res = insertGbpLocked(gbp, &surfaceMap, &outputStreamIds, &streamId);
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800249 if (!res.isOk()) {
250 return res;
251 }
Emilian Peevf873aa52018-01-26 14:58:28 +0000252
253 ssize_t index = mConfiguredOutputs.indexOfKey(streamId);
254 if (index >= 0) {
255 String8 requestedPhysicalId(
256 mConfiguredOutputs.valueAt(index).getPhysicalCameraId());
257 requestedPhysicalIds.push_back(requestedPhysicalId.string());
258 } else {
259 ALOGW("%s: Output stream Id not found among configured outputs!", __FUNCTION__);
260 }
Jianing Wei90e59c92014-03-12 18:29:36 -0700261 }
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800262 } else {
263 for (size_t i = 0; i < request.mStreamIdxList.size(); i++) {
264 int streamId = request.mStreamIdxList.itemAt(i);
265 int surfaceIdx = request.mSurfaceIdxList.itemAt(i);
Jianing Wei90e59c92014-03-12 18:29:36 -0700266
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800267 ssize_t index = mConfiguredOutputs.indexOfKey(streamId);
268 if (index < 0) {
269 ALOGE("%s: Camera %s: Tried to submit a request with a surface that"
270 " we have not called createStream on: stream %d",
271 __FUNCTION__, mCameraIdStr.string(), streamId);
272 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
273 "Request targets Surface that is not part of current capture session");
274 }
275
276 const auto& gbps = mConfiguredOutputs.valueAt(index).getGraphicBufferProducers();
277 if ((size_t)surfaceIdx >= gbps.size()) {
278 ALOGE("%s: Camera %s: Tried to submit a request with a surface that"
279 " we have not called createStream on: stream %d, surfaceIdx %d",
280 __FUNCTION__, mCameraIdStr.string(), streamId, surfaceIdx);
281 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
282 "Request targets Surface has invalid surface index");
283 }
284
Emilian Peevf873aa52018-01-26 14:58:28 +0000285 res = insertGbpLocked(gbps[surfaceIdx], &surfaceMap, &outputStreamIds, nullptr);
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800286 if (!res.isOk()) {
287 return res;
288 }
Emilian Peevf873aa52018-01-26 14:58:28 +0000289
290 String8 requestedPhysicalId(
291 mConfiguredOutputs.valueAt(index).getPhysicalCameraId());
292 requestedPhysicalIds.push_back(requestedPhysicalId.string());
Shuzhen Wang0129d522016-10-30 22:43:41 -0700293 }
Jianing Wei90e59c92014-03-12 18:29:36 -0700294 }
295
Emilian Peevf873aa52018-01-26 14:58:28 +0000296 CameraDeviceBase::PhysicalCameraSettingsList physicalSettingsList;
297 for (const auto& it : request.mPhysicalCameraSettings) {
298 String8 physicalId(it.id.c_str());
299 if (physicalId != mDevice->getId()) {
300 auto found = std::find(requestedPhysicalIds.begin(), requestedPhysicalIds.end(),
301 it.id);
302 if (found == requestedPhysicalIds.end()) {
303 ALOGE("%s: Camera %s: Physical camera id: %s not part of attached outputs.",
304 __FUNCTION__, mCameraIdStr.string(), physicalId.string());
305 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
306 "Invalid physical camera id");
307 }
308 }
309
310 CameraMetadata metadata(it.settings);
311 if (metadata.isEmpty()) {
312 ALOGE("%s: Camera %s: Sent empty metadata packet. Rejecting request.",
313 __FUNCTION__, mCameraIdStr.string());
314 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
315 "Request settings are empty");
316 }
317
318 physicalSettingsList.push_back({it.id, metadata});
319 }
320
321 if (streaming && (physicalSettingsList.size() > 1)) {
322 ALOGE("%s: Camera %s: Individual physical camera settings are not supported in "
323 "streaming requests. Rejecting request.", __FUNCTION__, mCameraIdStr.string());
324 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
325 "Streaming request contains individual physical requests");
326 }
327
328 if (!enforceRequestPermissions(physicalSettingsList.begin()->metadata)) {
329 // Callee logs
330 return STATUS_ERROR(CameraService::ERROR_PERMISSION_DENIED,
331 "Caller does not have permission to change restricted controls");
332 }
333
Emilian Peevaebbe412018-01-15 13:53:24 +0000334 physicalSettingsList.begin()->metadata.update(ANDROID_REQUEST_OUTPUT_STREAMS,
335 &outputStreamIds[0], outputStreamIds.size());
Jianing Wei90e59c92014-03-12 18:29:36 -0700336
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800337 if (request.mIsReprocess) {
Emilian Peevaebbe412018-01-15 13:53:24 +0000338 physicalSettingsList.begin()->metadata.update(ANDROID_REQUEST_INPUT_STREAMS,
339 &mInputStream.id, 1);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700340 }
341
Emilian Peevaebbe412018-01-15 13:53:24 +0000342 physicalSettingsList.begin()->metadata.update(ANDROID_REQUEST_ID,
343 &(submitInfo->mRequestId), /*size*/1);
Jianing Wei90e59c92014-03-12 18:29:36 -0700344 loopCounter++; // loopCounter starts from 1
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800345 ALOGV("%s: Camera %s: Creating request with ID %d (%d of %zu)",
346 __FUNCTION__, mCameraIdStr.string(), submitInfo->mRequestId,
347 loopCounter, requests.size());
Jianing Wei90e59c92014-03-12 18:29:36 -0700348
Emilian Peevaebbe412018-01-15 13:53:24 +0000349 metadataRequestList.push_back(physicalSettingsList);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700350 surfaceMapList.push_back(surfaceMap);
Jianing Wei90e59c92014-03-12 18:29:36 -0700351 }
352 mRequestIdCounter++;
353
354 if (streaming) {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700355 err = mDevice->setStreamingRequestList(metadataRequestList, surfaceMapList,
356 &(submitInfo->mLastFrameNumber));
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800357 if (err != OK) {
358 String8 msg = String8::format(
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800359 "Camera %s: Got error %s (%d) after trying to set streaming request",
360 mCameraIdStr.string(), strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800361 ALOGE("%s: %s", __FUNCTION__, msg.string());
362 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION,
363 msg.string());
Jianing Wei90e59c92014-03-12 18:29:36 -0700364 } else {
Shuzhen Wangc9ca6782016-04-26 13:40:31 -0700365 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700366 mStreamingRequestId = submitInfo->mRequestId;
Jianing Wei90e59c92014-03-12 18:29:36 -0700367 }
368 } else {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700369 err = mDevice->captureList(metadataRequestList, surfaceMapList,
370 &(submitInfo->mLastFrameNumber));
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800371 if (err != OK) {
372 String8 msg = String8::format(
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800373 "Camera %s: Got error %s (%d) after trying to submit capture request",
374 mCameraIdStr.string(), strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800375 ALOGE("%s: %s", __FUNCTION__, msg.string());
376 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION,
377 msg.string());
Jianing Wei90e59c92014-03-12 18:29:36 -0700378 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800379 ALOGV("%s: requestId = %d ", __FUNCTION__, submitInfo->mRequestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700380 }
381
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800382 ALOGV("%s: Camera %s: End of function", __FUNCTION__, mCameraIdStr.string());
Jianing Wei90e59c92014-03-12 18:29:36 -0700383 return res;
384}
385
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800386binder::Status CameraDeviceClient::cancelRequest(
387 int requestId,
388 /*out*/
389 int64_t* lastFrameNumber) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700390 ATRACE_CALL();
391 ALOGV("%s, requestId = %d", __FUNCTION__, requestId);
392
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800393 status_t err;
394 binder::Status res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700395
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800396 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700397
398 Mutex::Autolock icl(mBinderSerializationLock);
399
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800400 if (!mDevice.get()) {
401 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
402 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700403
Shuzhen Wangc9ca6782016-04-26 13:40:31 -0700404 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700405 if (mStreamingRequestId != requestId) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800406 String8 msg = String8::format("Camera %s: Canceling request ID %d doesn't match "
407 "current request ID %d", mCameraIdStr.string(), requestId, mStreamingRequestId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800408 ALOGE("%s: %s", __FUNCTION__, msg.string());
409 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Igor Murashkine7ee7632013-06-11 18:10:18 -0700410 }
411
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800412 err = mDevice->clearStreamingRequest(lastFrameNumber);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700413
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800414 if (err == OK) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800415 ALOGV("%s: Camera %s: Successfully cleared streaming request",
416 __FUNCTION__, mCameraIdStr.string());
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700417 mStreamingRequestId = REQUEST_ID_NONE;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800418 } else {
419 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800420 "Camera %s: Error clearing streaming request: %s (%d)",
421 mCameraIdStr.string(), strerror(-err), err);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700422 }
423
424 return res;
425}
426
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800427binder::Status CameraDeviceClient::beginConfigure() {
Ruben Brunkb2119af2014-05-09 19:57:56 -0700428 // TODO: Implement this.
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -0700429 ATRACE_CALL();
Zhijun He1fa89992015-06-01 15:44:31 -0700430 ALOGV("%s: Not implemented yet.", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800431 return binder::Status::ok();
Ruben Brunkb2119af2014-05-09 19:57:56 -0700432}
433
Emilian Peev5fbe0ba2017-10-20 15:45:45 +0100434binder::Status CameraDeviceClient::endConfigure(int operatingMode,
435 const hardware::camera2::impl::CameraMetadataNative& sessionParams) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -0700436 ATRACE_CALL();
Shuzhen Wang0129d522016-10-30 22:43:41 -0700437 ALOGV("%s: ending configure (%d input stream, %zu output surfaces)",
438 __FUNCTION__, mInputStream.configured ? 1 : 0,
439 mStreamMap.size());
Igor Murashkine2d167e2014-08-19 16:19:59 -0700440
Zhijun He0effd522016-03-04 10:22:27 -0800441 binder::Status res;
442 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
443
444 Mutex::Autolock icl(mBinderSerializationLock);
445
446 if (!mDevice.get()) {
447 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
448 }
449
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800450 if (operatingMode < 0) {
451 String8 msg = String8::format(
452 "Camera %s: Invalid operating mode %d requested", mCameraIdStr.string(), operatingMode);
453 ALOGE("%s: %s", __FUNCTION__, msg.string());
454 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
455 msg.string());
456 }
457
Zhijun He1fa89992015-06-01 15:44:31 -0700458 // Sanitize the high speed session against necessary capability bit.
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800459 bool isConstrainedHighSpeed = (operatingMode == ICameraDeviceUser::CONSTRAINED_HIGH_SPEED_MODE);
Zhijun He1fa89992015-06-01 15:44:31 -0700460 if (isConstrainedHighSpeed) {
461 CameraMetadata staticInfo = mDevice->info();
462 camera_metadata_entry_t entry = staticInfo.find(ANDROID_REQUEST_AVAILABLE_CAPABILITIES);
463 bool isConstrainedHighSpeedSupported = false;
464 for(size_t i = 0; i < entry.count; ++i) {
465 uint8_t capability = entry.data.u8[i];
466 if (capability == ANDROID_REQUEST_AVAILABLE_CAPABILITIES_CONSTRAINED_HIGH_SPEED_VIDEO) {
467 isConstrainedHighSpeedSupported = true;
468 break;
469 }
470 }
471 if (!isConstrainedHighSpeedSupported) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800472 String8 msg = String8::format(
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800473 "Camera %s: Try to create a constrained high speed configuration on a device"
474 " that doesn't support it.", mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800475 ALOGE("%s: %s", __FUNCTION__, msg.string());
476 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
477 msg.string());
Zhijun He1fa89992015-06-01 15:44:31 -0700478 }
479 }
480
Emilian Peev5fbe0ba2017-10-20 15:45:45 +0100481 status_t err = mDevice->configureStreams(sessionParams, operatingMode);
Eino-Ville Talvalaace80582016-03-18 13:27:35 -0700482 if (err == BAD_VALUE) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800483 String8 msg = String8::format("Camera %s: Unsupported set of inputs/outputs provided",
484 mCameraIdStr.string());
Zhijun He5d677d12016-05-29 16:52:39 -0700485 ALOGE("%s: %s", __FUNCTION__, msg.string());
486 res = STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Eino-Ville Talvalaace80582016-03-18 13:27:35 -0700487 } else if (err != OK) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800488 String8 msg = String8::format("Camera %s: Error configuring streams: %s (%d)",
489 mCameraIdStr.string(), strerror(-err), err);
Zhijun He5d677d12016-05-29 16:52:39 -0700490 ALOGE("%s: %s", __FUNCTION__, msg.string());
491 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800492 }
493
494 return res;
Ruben Brunkb2119af2014-05-09 19:57:56 -0700495}
496
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800497binder::Status CameraDeviceClient::deleteStream(int streamId) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700498 ATRACE_CALL();
499 ALOGV("%s (streamId = 0x%x)", __FUNCTION__, streamId);
500
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800501 binder::Status res;
502 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700503
504 Mutex::Autolock icl(mBinderSerializationLock);
505
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800506 if (!mDevice.get()) {
507 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
508 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700509
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700510 bool isInput = false;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700511 std::vector<sp<IBinder>> surfaces;
Zhijun He5d677d12016-05-29 16:52:39 -0700512 ssize_t dIndex = NAME_NOT_FOUND;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700513
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700514 if (mInputStream.configured && mInputStream.id == streamId) {
515 isInput = true;
516 } else {
517 // Guard against trying to delete non-created streams
518 for (size_t i = 0; i < mStreamMap.size(); ++i) {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700519 if (streamId == mStreamMap.valueAt(i).streamId()) {
520 surfaces.push_back(mStreamMap.keyAt(i));
521 }
522 }
523
524 // See if this stream is one of the deferred streams.
525 for (size_t i = 0; i < mDeferredStreams.size(); ++i) {
526 if (streamId == mDeferredStreams[i]) {
527 dIndex = i;
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700528 break;
529 }
530 }
531
Shuzhen Wang0129d522016-10-30 22:43:41 -0700532 if (surfaces.empty() && dIndex == NAME_NOT_FOUND) {
533 String8 msg = String8::format("Camera %s: Invalid stream ID (%d) specified, no such"
534 " stream created yet", mCameraIdStr.string(), streamId);
535 ALOGW("%s: %s", __FUNCTION__, msg.string());
536 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700537 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700538 }
539
540 // Also returns BAD_VALUE if stream ID was not valid
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800541 status_t err = mDevice->deleteStream(streamId);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700542
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800543 if (err != OK) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800544 String8 msg = String8::format("Camera %s: Unexpected error %s (%d) when deleting stream %d",
545 mCameraIdStr.string(), strerror(-err), err, streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800546 ALOGE("%s: %s", __FUNCTION__, msg.string());
547 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
548 } else {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700549 if (isInput) {
550 mInputStream.configured = false;
Zhijun He5d677d12016-05-29 16:52:39 -0700551 } else {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700552 for (auto& surface : surfaces) {
553 mStreamMap.removeItem(surface);
554 }
555
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800556 mConfiguredOutputs.removeItem(streamId);
557
Shuzhen Wang0129d522016-10-30 22:43:41 -0700558 if (dIndex != NAME_NOT_FOUND) {
559 mDeferredStreams.removeItemsAt(dIndex);
560 }
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700561 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700562 }
563
564 return res;
565}
566
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800567binder::Status CameraDeviceClient::createStream(
568 const hardware::camera2::params::OutputConfiguration &outputConfiguration,
569 /*out*/
570 int32_t* newStreamId) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700571 ATRACE_CALL();
Igor Murashkine7ee7632013-06-11 18:10:18 -0700572
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800573 binder::Status res;
574 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700575
576 Mutex::Autolock icl(mBinderSerializationLock);
577
Shuzhen Wang0129d522016-10-30 22:43:41 -0700578 const std::vector<sp<IGraphicBufferProducer>>& bufferProducers =
579 outputConfiguration.getGraphicBufferProducers();
580 size_t numBufferProducers = bufferProducers.size();
Shuzhen Wang758c2152017-01-10 18:26:18 -0800581 bool deferredConsumer = outputConfiguration.isDeferred();
582 bool isShared = outputConfiguration.isShared();
Shuzhen Wangc28189a2017-11-27 23:05:10 -0800583 String8 physicalCameraId = String8(outputConfiguration.getPhysicalCameraId());
Shuzhen Wang0129d522016-10-30 22:43:41 -0700584
585 if (numBufferProducers > MAX_SURFACES_PER_STREAM) {
586 ALOGE("%s: GraphicBufferProducer count %zu for stream exceeds limit of %d",
587 __FUNCTION__, bufferProducers.size(), MAX_SURFACES_PER_STREAM);
588 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, "Surface count is too high");
589 }
Shuzhen Wang758c2152017-01-10 18:26:18 -0800590 bool deferredConsumerOnly = deferredConsumer && numBufferProducers == 0;
Zhijun He5d677d12016-05-29 16:52:39 -0700591 int surfaceType = outputConfiguration.getSurfaceType();
592 bool validSurfaceType = ((surfaceType == OutputConfiguration::SURFACE_TYPE_SURFACE_VIEW) ||
593 (surfaceType == OutputConfiguration::SURFACE_TYPE_SURFACE_TEXTURE));
Shuzhen Wang0129d522016-10-30 22:43:41 -0700594
Zhijun He5d677d12016-05-29 16:52:39 -0700595 if (deferredConsumer && !validSurfaceType) {
596 ALOGE("%s: Target surface is invalid: bufferProducer = %p, surfaceType = %d.",
Shuzhen Wang0129d522016-10-30 22:43:41 -0700597 __FUNCTION__, bufferProducers[0].get(), surfaceType);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800598 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, "Target Surface is invalid");
Yin-Chia Yeh89f14da2014-06-10 16:05:44 -0700599 }
Zhijun He5d677d12016-05-29 16:52:39 -0700600
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800601 if (!mDevice.get()) {
602 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
603 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700604
Shuzhen Wangc28189a2017-11-27 23:05:10 -0800605 if (!checkPhysicalCameraId(physicalCameraId)) {
606 String8 msg = String8::format("Camera %s: Camera doesn't support physicalCameraId %s.",
607 mCameraIdStr.string(), physicalCameraId.string());
608 ALOGE("%s: %s", __FUNCTION__, msg.string());
609 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
610 }
Shuzhen Wang0129d522016-10-30 22:43:41 -0700611 std::vector<sp<Surface>> surfaces;
612 std::vector<sp<IBinder>> binders;
Zhijun He5d677d12016-05-29 16:52:39 -0700613 status_t err;
614
615 // Create stream for deferred surface case.
Shuzhen Wang0129d522016-10-30 22:43:41 -0700616 if (deferredConsumerOnly) {
Shuzhen Wang758c2152017-01-10 18:26:18 -0800617 return createDeferredSurfaceStreamLocked(outputConfiguration, isShared, newStreamId);
Zhijun He5d677d12016-05-29 16:52:39 -0700618 }
619
Shuzhen Wang758c2152017-01-10 18:26:18 -0800620 OutputStreamInfo streamInfo;
621 bool isStreamInfoValid = false;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700622 for (auto& bufferProducer : bufferProducers) {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700623 // Don't create multiple streams for the same target surface
Shuzhen Wang0129d522016-10-30 22:43:41 -0700624 sp<IBinder> binder = IInterface::asBinder(bufferProducer);
Shuzhen Wang758c2152017-01-10 18:26:18 -0800625 ssize_t index = mStreamMap.indexOfKey(binder);
626 if (index != NAME_NOT_FOUND) {
627 String8 msg = String8::format("Camera %s: Surface already has a stream created for it "
628 "(ID %zd)", mCameraIdStr.string(), index);
629 ALOGW("%s: %s", __FUNCTION__, msg.string());
630 return STATUS_ERROR(CameraService::ERROR_ALREADY_EXISTS, msg.string());
Shuzhen Wang0129d522016-10-30 22:43:41 -0700631 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700632
Shuzhen Wang758c2152017-01-10 18:26:18 -0800633 sp<Surface> surface;
634 res = createSurfaceFromGbp(streamInfo, isStreamInfoValid, surface, bufferProducer);
635
636 if (!res.isOk())
637 return res;
638
639 if (!isStreamInfoValid) {
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800640 // Streaming sharing is only supported for IMPLEMENTATION_DEFINED
641 // formats.
642 if (isShared && streamInfo.format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
643 String8 msg = String8::format("Camera %s: Stream sharing is only supported for "
644 "IMPLEMENTATION_DEFINED format", mCameraIdStr.string());
645 ALOGW("%s: %s", __FUNCTION__, msg.string());
646 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
647 }
Shuzhen Wang758c2152017-01-10 18:26:18 -0800648 isStreamInfoValid = true;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700649 }
650
Shuzhen Wang758c2152017-01-10 18:26:18 -0800651 binders.push_back(IInterface::asBinder(bufferProducer));
Shuzhen Wang0129d522016-10-30 22:43:41 -0700652 surfaces.push_back(surface);
Ruben Brunkbba75572014-11-20 17:29:50 -0800653 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700654
Zhijun He125684a2015-12-26 15:07:30 -0800655 int streamId = camera3::CAMERA3_STREAM_ID_INVALID;
Emilian Peev40ead602017-09-26 15:46:36 +0100656 std::vector<int> surfaceIds;
Shuzhen Wang758c2152017-01-10 18:26:18 -0800657 err = mDevice->createStream(surfaces, deferredConsumer, streamInfo.width,
658 streamInfo.height, streamInfo.format, streamInfo.dataSpace,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800659 static_cast<camera3_stream_rotation_t>(outputConfiguration.getRotation()),
Shuzhen Wangc28189a2017-11-27 23:05:10 -0800660 &streamId, physicalCameraId, &surfaceIds, outputConfiguration.getSurfaceSetID(),
661 isShared);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700662
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800663 if (err != OK) {
664 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800665 "Camera %s: Error creating output stream (%d x %d, fmt %x, dataSpace %x): %s (%d)",
Shuzhen Wang758c2152017-01-10 18:26:18 -0800666 mCameraIdStr.string(), streamInfo.width, streamInfo.height, streamInfo.format,
667 streamInfo.dataSpace, strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800668 } else {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700669 int i = 0;
670 for (auto& binder : binders) {
671 ALOGV("%s: mStreamMap add binder %p streamId %d, surfaceId %d",
672 __FUNCTION__, binder.get(), streamId, i);
Emilian Peev40ead602017-09-26 15:46:36 +0100673 mStreamMap.add(binder, StreamSurfaceId(streamId, surfaceIds[i]));
674 i++;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700675 }
Shuzhen Wang758c2152017-01-10 18:26:18 -0800676
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800677 mConfiguredOutputs.add(streamId, outputConfiguration);
Shuzhen Wang758c2152017-01-10 18:26:18 -0800678 mStreamInfoMap[streamId] = streamInfo;
679
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800680 ALOGV("%s: Camera %s: Successfully created a new stream ID %d for output surface"
Shuzhen Wang0129d522016-10-30 22:43:41 -0700681 " (%d x %d) with format 0x%x.",
Shuzhen Wang758c2152017-01-10 18:26:18 -0800682 __FUNCTION__, mCameraIdStr.string(), streamId, streamInfo.width,
683 streamInfo.height, streamInfo.format);
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -0700684
Zhijun He5d677d12016-05-29 16:52:39 -0700685 // Set transform flags to ensure preview to be rotated correctly.
686 res = setStreamTransformLocked(streamId);
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -0700687
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800688 *newStreamId = streamId;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700689 }
690
691 return res;
692}
693
Zhijun He5d677d12016-05-29 16:52:39 -0700694binder::Status CameraDeviceClient::createDeferredSurfaceStreamLocked(
695 const hardware::camera2::params::OutputConfiguration &outputConfiguration,
Shuzhen Wang758c2152017-01-10 18:26:18 -0800696 bool isShared,
Zhijun He5d677d12016-05-29 16:52:39 -0700697 /*out*/
698 int* newStreamId) {
699 int width, height, format, surfaceType;
Emilian Peev050f5dc2017-05-18 14:43:56 +0100700 uint64_t consumerUsage;
Zhijun He5d677d12016-05-29 16:52:39 -0700701 android_dataspace dataSpace;
702 status_t err;
703 binder::Status res;
704
705 if (!mDevice.get()) {
706 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
707 }
708
709 // Infer the surface info for deferred surface stream creation.
710 width = outputConfiguration.getWidth();
711 height = outputConfiguration.getHeight();
712 surfaceType = outputConfiguration.getSurfaceType();
713 format = HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED;
714 dataSpace = android_dataspace_t::HAL_DATASPACE_UNKNOWN;
715 // Hardcode consumer usage flags: SurfaceView--0x900, SurfaceTexture--0x100.
716 consumerUsage = GraphicBuffer::USAGE_HW_TEXTURE;
717 if (surfaceType == OutputConfiguration::SURFACE_TYPE_SURFACE_VIEW) {
718 consumerUsage |= GraphicBuffer::USAGE_HW_COMPOSER;
719 }
720 int streamId = camera3::CAMERA3_STREAM_ID_INVALID;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700721 std::vector<sp<Surface>> noSurface;
Emilian Peev40ead602017-09-26 15:46:36 +0100722 std::vector<int> surfaceIds;
Shuzhen Wangc28189a2017-11-27 23:05:10 -0800723 String8 physicalCameraId(outputConfiguration.getPhysicalCameraId());
Shuzhen Wang0129d522016-10-30 22:43:41 -0700724 err = mDevice->createStream(noSurface, /*hasDeferredConsumer*/true, width,
725 height, format, dataSpace,
Zhijun He5d677d12016-05-29 16:52:39 -0700726 static_cast<camera3_stream_rotation_t>(outputConfiguration.getRotation()),
Shuzhen Wangc28189a2017-11-27 23:05:10 -0800727 &streamId, physicalCameraId, &surfaceIds,
728 outputConfiguration.getSurfaceSetID(), isShared,
Emilian Peev40ead602017-09-26 15:46:36 +0100729 consumerUsage);
Zhijun He5d677d12016-05-29 16:52:39 -0700730
731 if (err != OK) {
732 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800733 "Camera %s: Error creating output stream (%d x %d, fmt %x, dataSpace %x): %s (%d)",
734 mCameraIdStr.string(), width, height, format, dataSpace, strerror(-err), err);
Zhijun He5d677d12016-05-29 16:52:39 -0700735 } else {
736 // Can not add streamId to mStreamMap here, as the surface is deferred. Add it to
737 // a separate list to track. Once the deferred surface is set, this id will be
738 // relocated to mStreamMap.
739 mDeferredStreams.push_back(streamId);
740
Shuzhen Wang758c2152017-01-10 18:26:18 -0800741 mStreamInfoMap.emplace(std::piecewise_construct, std::forward_as_tuple(streamId),
742 std::forward_as_tuple(width, height, format, dataSpace, consumerUsage));
743
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800744 ALOGV("%s: Camera %s: Successfully created a new stream ID %d for a deferred surface"
Zhijun He5d677d12016-05-29 16:52:39 -0700745 " (%d x %d) stream with format 0x%x.",
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800746 __FUNCTION__, mCameraIdStr.string(), streamId, width, height, format);
Zhijun He5d677d12016-05-29 16:52:39 -0700747
748 // Set transform flags to ensure preview to be rotated correctly.
749 res = setStreamTransformLocked(streamId);
750
751 *newStreamId = streamId;
752 }
753 return res;
754}
755
756binder::Status CameraDeviceClient::setStreamTransformLocked(int streamId) {
757 int32_t transform = 0;
758 status_t err;
759 binder::Status res;
760
761 if (!mDevice.get()) {
762 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
763 }
764
765 err = getRotationTransformLocked(&transform);
766 if (err != OK) {
767 // Error logged by getRotationTransformLocked.
768 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION,
769 "Unable to calculate rotation transform for new stream");
770 }
771
772 err = mDevice->setStreamTransform(streamId, transform);
773 if (err != OK) {
774 String8 msg = String8::format("Failed to set stream transform (stream id %d)",
775 streamId);
776 ALOGE("%s: %s", __FUNCTION__, msg.string());
777 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
778 }
779
780 return res;
781}
Ruben Brunkbba75572014-11-20 17:29:50 -0800782
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800783binder::Status CameraDeviceClient::createInputStream(
784 int width, int height, int format,
785 /*out*/
786 int32_t* newStreamId) {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700787
788 ATRACE_CALL();
789 ALOGV("%s (w = %d, h = %d, f = 0x%x)", __FUNCTION__, width, height, format);
790
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800791 binder::Status res;
792 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700793
794 Mutex::Autolock icl(mBinderSerializationLock);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800795
796 if (!mDevice.get()) {
797 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
798 }
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700799
800 if (mInputStream.configured) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800801 String8 msg = String8::format("Camera %s: Already has an input stream "
Yi Kong0f414de2017-12-15 13:48:50 -0800802 "configured (ID %d)", mCameraIdStr.string(), mInputStream.id);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800803 ALOGE("%s: %s", __FUNCTION__, msg.string() );
804 return STATUS_ERROR(CameraService::ERROR_ALREADY_EXISTS, msg.string());
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700805 }
806
807 int streamId = -1;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800808 status_t err = mDevice->createInputStream(width, height, format, &streamId);
809 if (err == OK) {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700810 mInputStream.configured = true;
811 mInputStream.width = width;
812 mInputStream.height = height;
813 mInputStream.format = format;
814 mInputStream.id = streamId;
815
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800816 ALOGV("%s: Camera %s: Successfully created a new input stream ID %d",
817 __FUNCTION__, mCameraIdStr.string(), streamId);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700818
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800819 *newStreamId = streamId;
820 } else {
821 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800822 "Camera %s: Error creating new input stream: %s (%d)", mCameraIdStr.string(),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800823 strerror(-err), err);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700824 }
825
826 return res;
827}
828
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800829binder::Status CameraDeviceClient::getInputSurface(/*out*/ view::Surface *inputSurface) {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700830
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800831 binder::Status res;
832 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
833
834 if (inputSurface == NULL) {
835 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, "Null input surface");
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700836 }
837
838 Mutex::Autolock icl(mBinderSerializationLock);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800839 if (!mDevice.get()) {
840 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
841 }
842 sp<IGraphicBufferProducer> producer;
843 status_t err = mDevice->getInputBufferProducer(&producer);
844 if (err != OK) {
845 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800846 "Camera %s: Error getting input Surface: %s (%d)",
847 mCameraIdStr.string(), strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800848 } else {
849 inputSurface->name = String16("CameraInput");
850 inputSurface->graphicBufferProducer = producer;
851 }
852 return res;
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700853}
854
Emilian Peev40ead602017-09-26 15:46:36 +0100855binder::Status CameraDeviceClient::updateOutputConfiguration(int streamId,
856 const hardware::camera2::params::OutputConfiguration &outputConfiguration) {
857 ATRACE_CALL();
858
859 binder::Status res;
860 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
861
862 Mutex::Autolock icl(mBinderSerializationLock);
863
864 if (!mDevice.get()) {
865 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
866 }
867
868 const std::vector<sp<IGraphicBufferProducer> >& bufferProducers =
869 outputConfiguration.getGraphicBufferProducers();
870 auto producerCount = bufferProducers.size();
871 if (producerCount == 0) {
872 ALOGE("%s: bufferProducers must not be empty", __FUNCTION__);
873 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
874 "bufferProducers must not be empty");
875 }
876
877 // The first output is the one associated with the output configuration.
878 // It should always be present, valid and the corresponding stream id should match.
879 sp<IBinder> binder = IInterface::asBinder(bufferProducers[0]);
880 ssize_t index = mStreamMap.indexOfKey(binder);
881 if (index == NAME_NOT_FOUND) {
882 ALOGE("%s: Outputconfiguration is invalid", __FUNCTION__);
883 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
884 "OutputConfiguration is invalid");
885 }
886 if (mStreamMap.valueFor(binder).streamId() != streamId) {
887 ALOGE("%s: Stream Id: %d provided doesn't match the id: %d in the stream map",
888 __FUNCTION__, streamId, mStreamMap.valueFor(binder).streamId());
889 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
890 "Stream id is invalid");
891 }
892
893 std::vector<size_t> removedSurfaceIds;
894 std::vector<sp<IBinder>> removedOutputs;
895 std::vector<sp<Surface>> newOutputs;
896 std::vector<OutputStreamInfo> streamInfos;
897 KeyedVector<sp<IBinder>, sp<IGraphicBufferProducer>> newOutputsMap;
898 for (auto &it : bufferProducers) {
899 newOutputsMap.add(IInterface::asBinder(it), it);
900 }
901
902 for (size_t i = 0; i < mStreamMap.size(); i++) {
903 ssize_t idx = newOutputsMap.indexOfKey(mStreamMap.keyAt(i));
904 if (idx == NAME_NOT_FOUND) {
905 if (mStreamMap[i].streamId() == streamId) {
906 removedSurfaceIds.push_back(mStreamMap[i].surfaceId());
907 removedOutputs.push_back(mStreamMap.keyAt(i));
908 }
909 } else {
910 if (mStreamMap[i].streamId() != streamId) {
911 ALOGE("%s: Output surface already part of a different stream", __FUNCTION__);
912 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
913 "Target Surface is invalid");
914 }
915 newOutputsMap.removeItemsAt(idx);
916 }
917 }
918
919 for (size_t i = 0; i < newOutputsMap.size(); i++) {
920 OutputStreamInfo outInfo;
921 sp<Surface> surface;
922 res = createSurfaceFromGbp(outInfo, /*isStreamInfoValid*/ false, surface,
923 newOutputsMap.valueAt(i));
924 if (!res.isOk())
925 return res;
926
927 // Stream sharing is only supported for IMPLEMENTATION_DEFINED
928 // formats.
929 if (outInfo.format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
930 String8 msg = String8::format("Camera %s: Stream sharing is only supported for "
931 "IMPLEMENTATION_DEFINED format", mCameraIdStr.string());
932 ALOGW("%s: %s", __FUNCTION__, msg.string());
933 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
934 }
935 streamInfos.push_back(outInfo);
936 newOutputs.push_back(surface);
937 }
938
939 //Trivial case no changes required
940 if (removedSurfaceIds.empty() && newOutputs.empty()) {
941 return binder::Status::ok();
942 }
943
944 KeyedVector<sp<Surface>, size_t> outputMap;
945 auto ret = mDevice->updateStream(streamId, newOutputs, streamInfos, removedSurfaceIds,
946 &outputMap);
947 if (ret != OK) {
948 switch (ret) {
949 case NAME_NOT_FOUND:
950 case BAD_VALUE:
951 case -EBUSY:
952 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
953 "Camera %s: Error updating stream: %s (%d)",
954 mCameraIdStr.string(), strerror(ret), ret);
955 break;
956 default:
957 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
958 "Camera %s: Error updating stream: %s (%d)",
959 mCameraIdStr.string(), strerror(ret), ret);
960 break;
961 }
962 } else {
963 for (const auto &it : removedOutputs) {
964 mStreamMap.removeItem(it);
965 }
966
967 for (size_t i = 0; i < outputMap.size(); i++) {
968 mStreamMap.add(IInterface::asBinder(outputMap.keyAt(i)->getIGraphicBufferProducer()),
969 StreamSurfaceId(streamId, outputMap.valueAt(i)));
970 }
971
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800972 mConfiguredOutputs.replaceValueFor(streamId, outputConfiguration);
973
Emilian Peev40ead602017-09-26 15:46:36 +0100974 ALOGV("%s: Camera %s: Successful stream ID %d update",
975 __FUNCTION__, mCameraIdStr.string(), streamId);
976 }
977
978 return res;
979}
980
Eman Copty6d7af0e2016-06-17 20:46:40 -0700981bool CameraDeviceClient::isPublicFormat(int32_t format)
982{
983 switch(format) {
984 case HAL_PIXEL_FORMAT_RGBA_8888:
985 case HAL_PIXEL_FORMAT_RGBX_8888:
986 case HAL_PIXEL_FORMAT_RGB_888:
987 case HAL_PIXEL_FORMAT_RGB_565:
988 case HAL_PIXEL_FORMAT_BGRA_8888:
989 case HAL_PIXEL_FORMAT_YV12:
990 case HAL_PIXEL_FORMAT_Y8:
991 case HAL_PIXEL_FORMAT_Y16:
992 case HAL_PIXEL_FORMAT_RAW16:
993 case HAL_PIXEL_FORMAT_RAW10:
994 case HAL_PIXEL_FORMAT_RAW12:
995 case HAL_PIXEL_FORMAT_RAW_OPAQUE:
996 case HAL_PIXEL_FORMAT_BLOB:
997 case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED:
998 case HAL_PIXEL_FORMAT_YCbCr_420_888:
999 case HAL_PIXEL_FORMAT_YCbCr_422_888:
1000 case HAL_PIXEL_FORMAT_YCbCr_444_888:
1001 case HAL_PIXEL_FORMAT_FLEX_RGB_888:
1002 case HAL_PIXEL_FORMAT_FLEX_RGBA_8888:
1003 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
1004 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
1005 case HAL_PIXEL_FORMAT_YCbCr_422_I:
1006 return true;
1007 default:
1008 return false;
1009 }
1010}
1011
Shuzhen Wang758c2152017-01-10 18:26:18 -08001012binder::Status CameraDeviceClient::createSurfaceFromGbp(
1013 OutputStreamInfo& streamInfo, bool isStreamInfoValid,
1014 sp<Surface>& surface, const sp<IGraphicBufferProducer>& gbp) {
1015
1016 // bufferProducer must be non-null
1017 if (gbp == nullptr) {
1018 String8 msg = String8::format("Camera %s: Surface is NULL", mCameraIdStr.string());
1019 ALOGW("%s: %s", __FUNCTION__, msg.string());
1020 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1021 }
1022 // HACK b/10949105
1023 // Query consumer usage bits to set async operation mode for
1024 // GLConsumer using controlledByApp parameter.
1025 bool useAsync = false;
Emilian Peev050f5dc2017-05-18 14:43:56 +01001026 uint64_t consumerUsage = 0;
Shuzhen Wang758c2152017-01-10 18:26:18 -08001027 status_t err;
Emilian Peev050f5dc2017-05-18 14:43:56 +01001028 if ((err = gbp->getConsumerUsage(&consumerUsage)) != OK) {
Shuzhen Wang758c2152017-01-10 18:26:18 -08001029 String8 msg = String8::format("Camera %s: Failed to query Surface consumer usage: %s (%d)",
1030 mCameraIdStr.string(), strerror(-err), err);
1031 ALOGE("%s: %s", __FUNCTION__, msg.string());
1032 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
1033 }
1034 if (consumerUsage & GraphicBuffer::USAGE_HW_TEXTURE) {
Emilian Peev050f5dc2017-05-18 14:43:56 +01001035 ALOGW("%s: Camera %s with consumer usage flag: %" PRIu64 ": Forcing asynchronous mode for stream",
Shuzhen Wang758c2152017-01-10 18:26:18 -08001036 __FUNCTION__, mCameraIdStr.string(), consumerUsage);
1037 useAsync = true;
1038 }
1039
Emilian Peev050f5dc2017-05-18 14:43:56 +01001040 uint64_t disallowedFlags = GraphicBuffer::USAGE_HW_VIDEO_ENCODER |
Shuzhen Wang758c2152017-01-10 18:26:18 -08001041 GRALLOC_USAGE_RENDERSCRIPT;
Emilian Peev050f5dc2017-05-18 14:43:56 +01001042 uint64_t allowedFlags = GraphicBuffer::USAGE_SW_READ_MASK |
Shuzhen Wang758c2152017-01-10 18:26:18 -08001043 GraphicBuffer::USAGE_HW_TEXTURE |
1044 GraphicBuffer::USAGE_HW_COMPOSER;
1045 bool flexibleConsumer = (consumerUsage & disallowedFlags) == 0 &&
1046 (consumerUsage & allowedFlags) != 0;
1047
1048 surface = new Surface(gbp, useAsync);
1049 ANativeWindow *anw = surface.get();
1050
1051 int width, height, format;
1052 android_dataspace dataSpace;
1053 if ((err = anw->query(anw, NATIVE_WINDOW_WIDTH, &width)) != OK) {
1054 String8 msg = String8::format("Camera %s: Failed to query Surface width: %s (%d)",
1055 mCameraIdStr.string(), strerror(-err), err);
1056 ALOGE("%s: %s", __FUNCTION__, msg.string());
1057 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
1058 }
1059 if ((err = anw->query(anw, NATIVE_WINDOW_HEIGHT, &height)) != OK) {
1060 String8 msg = String8::format("Camera %s: Failed to query Surface height: %s (%d)",
1061 mCameraIdStr.string(), strerror(-err), err);
1062 ALOGE("%s: %s", __FUNCTION__, msg.string());
1063 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
1064 }
1065 if ((err = anw->query(anw, NATIVE_WINDOW_FORMAT, &format)) != OK) {
1066 String8 msg = String8::format("Camera %s: Failed to query Surface format: %s (%d)",
1067 mCameraIdStr.string(), strerror(-err), err);
1068 ALOGE("%s: %s", __FUNCTION__, msg.string());
1069 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
1070 }
1071 if ((err = anw->query(anw, NATIVE_WINDOW_DEFAULT_DATASPACE,
1072 reinterpret_cast<int*>(&dataSpace))) != OK) {
1073 String8 msg = String8::format("Camera %s: Failed to query Surface dataspace: %s (%d)",
1074 mCameraIdStr.string(), strerror(-err), err);
1075 ALOGE("%s: %s", __FUNCTION__, msg.string());
1076 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
1077 }
1078
1079 // FIXME: remove this override since the default format should be
1080 // IMPLEMENTATION_DEFINED. b/9487482
1081 if (format >= HAL_PIXEL_FORMAT_RGBA_8888 &&
1082 format <= HAL_PIXEL_FORMAT_BGRA_8888) {
1083 ALOGW("%s: Camera %s: Overriding format %#x to IMPLEMENTATION_DEFINED",
1084 __FUNCTION__, mCameraIdStr.string(), format);
1085 format = HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED;
1086 }
1087 // Round dimensions to the nearest dimensions available for this format
1088 if (flexibleConsumer && isPublicFormat(format) &&
1089 !CameraDeviceClient::roundBufferDimensionNearest(width, height,
1090 format, dataSpace, mDevice->info(), /*out*/&width, /*out*/&height)) {
1091 String8 msg = String8::format("Camera %s: No supported stream configurations with "
1092 "format %#x defined, failed to create output stream",
1093 mCameraIdStr.string(), format);
1094 ALOGE("%s: %s", __FUNCTION__, msg.string());
1095 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1096 }
1097
1098 if (!isStreamInfoValid) {
1099 streamInfo.width = width;
1100 streamInfo.height = height;
1101 streamInfo.format = format;
1102 streamInfo.dataSpace = dataSpace;
1103 streamInfo.consumerUsage = consumerUsage;
1104 return binder::Status::ok();
1105 }
1106 if (width != streamInfo.width) {
1107 String8 msg = String8::format("Camera %s:Surface width doesn't match: %d vs %d",
1108 mCameraIdStr.string(), width, streamInfo.width);
1109 ALOGE("%s: %s", __FUNCTION__, msg.string());
1110 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1111 }
1112 if (height != streamInfo.height) {
1113 String8 msg = String8::format("Camera %s:Surface height doesn't match: %d vs %d",
1114 mCameraIdStr.string(), height, streamInfo.height);
1115 ALOGE("%s: %s", __FUNCTION__, msg.string());
1116 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1117 }
1118 if (format != streamInfo.format) {
1119 String8 msg = String8::format("Camera %s:Surface format doesn't match: %d vs %d",
1120 mCameraIdStr.string(), format, streamInfo.format);
1121 ALOGE("%s: %s", __FUNCTION__, msg.string());
1122 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1123 }
Shuzhen Wange8ecda92017-02-20 17:10:28 -08001124 if (format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
1125 if (dataSpace != streamInfo.dataSpace) {
1126 String8 msg = String8::format("Camera %s:Surface dataSpace doesn't match: %d vs %d",
1127 mCameraIdStr.string(), dataSpace, streamInfo.dataSpace);
1128 ALOGE("%s: %s", __FUNCTION__, msg.string());
1129 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1130 }
1131 //At the native side, there isn't a way to check whether 2 surfaces come from the same
1132 //surface class type. Use usage flag to approximate the comparison.
1133 if (consumerUsage != streamInfo.consumerUsage) {
1134 String8 msg = String8::format(
Emilian Peev050f5dc2017-05-18 14:43:56 +01001135 "Camera %s:Surface usage flag doesn't match %" PRIu64 " vs %" PRIu64 "",
Shuzhen Wange8ecda92017-02-20 17:10:28 -08001136 mCameraIdStr.string(), consumerUsage, streamInfo.consumerUsage);
1137 ALOGE("%s: %s", __FUNCTION__, msg.string());
1138 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1139 }
Shuzhen Wang758c2152017-01-10 18:26:18 -08001140 }
1141 return binder::Status::ok();
1142}
1143
Shuzhen Wangc28189a2017-11-27 23:05:10 -08001144bool CameraDeviceClient::checkPhysicalCameraId(const String8& physicalCameraId) {
1145 if (0 == physicalCameraId.size()) {
1146 return true;
1147 }
1148
1149 CameraMetadata staticInfo = mDevice->info();
1150 camera_metadata_entry_t entryCap;
1151 bool isLogicalCam = false;
1152
1153 entryCap = staticInfo.find(ANDROID_REQUEST_AVAILABLE_CAPABILITIES);
1154 for (size_t i = 0; i < entryCap.count; ++i) {
1155 uint8_t capability = entryCap.data.u8[i];
1156 if (capability == ANDROID_REQUEST_AVAILABLE_CAPABILITIES_LOGICAL_MULTI_CAMERA) {
1157 isLogicalCam = true;
1158 }
1159 }
1160 if (!isLogicalCam) {
1161 return false;
1162 }
1163
1164 camera_metadata_entry_t entryIds = staticInfo.find(ANDROID_LOGICAL_MULTI_CAMERA_PHYSICAL_IDS);
1165 const uint8_t* ids = entryIds.data.u8;
1166 size_t start = 0;
1167 for (size_t i = 0; i < entryIds.count; ++i) {
1168 if (ids[i] == '\0') {
1169 if (start != i) {
1170 String8 currentId((const char*)ids+start);
1171 if (currentId == physicalCameraId) {
1172 return true;
1173 }
1174 }
1175 start = i+1;
1176 }
1177 }
1178 return false;
1179}
1180
Ruben Brunkbba75572014-11-20 17:29:50 -08001181bool CameraDeviceClient::roundBufferDimensionNearest(int32_t width, int32_t height,
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -08001182 int32_t format, android_dataspace dataSpace, const CameraMetadata& info,
Ruben Brunkbba75572014-11-20 17:29:50 -08001183 /*out*/int32_t* outWidth, /*out*/int32_t* outHeight) {
1184
1185 camera_metadata_ro_entry streamConfigs =
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -08001186 (dataSpace == HAL_DATASPACE_DEPTH) ?
1187 info.find(ANDROID_DEPTH_AVAILABLE_DEPTH_STREAM_CONFIGURATIONS) :
Ruben Brunkbba75572014-11-20 17:29:50 -08001188 info.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS);
1189
1190 int32_t bestWidth = -1;
1191 int32_t bestHeight = -1;
1192
1193 // Iterate through listed stream configurations and find the one with the smallest euclidean
1194 // distance from the given dimensions for the given format.
1195 for (size_t i = 0; i < streamConfigs.count; i += 4) {
1196 int32_t fmt = streamConfigs.data.i32[i];
1197 int32_t w = streamConfigs.data.i32[i + 1];
1198 int32_t h = streamConfigs.data.i32[i + 2];
1199
1200 // Ignore input/output type for now
1201 if (fmt == format) {
1202 if (w == width && h == height) {
1203 bestWidth = width;
1204 bestHeight = height;
1205 break;
1206 } else if (w <= ROUNDING_WIDTH_CAP && (bestWidth == -1 ||
1207 CameraDeviceClient::euclidDistSquare(w, h, width, height) <
1208 CameraDeviceClient::euclidDistSquare(bestWidth, bestHeight, width, height))) {
1209 bestWidth = w;
1210 bestHeight = h;
1211 }
1212 }
1213 }
1214
1215 if (bestWidth == -1) {
1216 // Return false if no configurations for this format were listed
1217 return false;
1218 }
1219
1220 // Set the outputs to the closet width/height
1221 if (outWidth != NULL) {
1222 *outWidth = bestWidth;
1223 }
1224 if (outHeight != NULL) {
1225 *outHeight = bestHeight;
1226 }
1227
1228 // Return true if at least one configuration for this format was listed
1229 return true;
1230}
1231
1232int64_t CameraDeviceClient::euclidDistSquare(int32_t x0, int32_t y0, int32_t x1, int32_t y1) {
1233 int64_t d0 = x0 - x1;
1234 int64_t d1 = y0 - y1;
1235 return d0 * d0 + d1 * d1;
1236}
1237
Igor Murashkine7ee7632013-06-11 18:10:18 -07001238// Create a request object from a template.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001239binder::Status CameraDeviceClient::createDefaultRequest(int templateId,
1240 /*out*/
1241 hardware::camera2::impl::CameraMetadataNative* request)
Igor Murashkine7ee7632013-06-11 18:10:18 -07001242{
1243 ATRACE_CALL();
1244 ALOGV("%s (templateId = 0x%x)", __FUNCTION__, templateId);
1245
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001246 binder::Status res;
1247 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -07001248
1249 Mutex::Autolock icl(mBinderSerializationLock);
1250
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001251 if (!mDevice.get()) {
1252 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1253 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001254
1255 CameraMetadata metadata;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001256 status_t err;
1257 if ( (err = mDevice->createDefaultRequest(templateId, &metadata) ) == OK &&
Igor Murashkine7ee7632013-06-11 18:10:18 -07001258 request != NULL) {
1259
1260 request->swap(metadata);
Chien-Yu Chen9cd14022016-03-09 12:21:01 -08001261 } else if (err == BAD_VALUE) {
1262 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001263 "Camera %s: Template ID %d is invalid or not supported: %s (%d)",
1264 mCameraIdStr.string(), templateId, strerror(-err), err);
Chien-Yu Chen9cd14022016-03-09 12:21:01 -08001265
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001266 } else {
1267 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001268 "Camera %s: Error creating default request for template %d: %s (%d)",
1269 mCameraIdStr.string(), templateId, strerror(-err), err);
Igor Murashkine7ee7632013-06-11 18:10:18 -07001270 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001271 return res;
1272}
1273
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001274binder::Status CameraDeviceClient::getCameraInfo(
1275 /*out*/
1276 hardware::camera2::impl::CameraMetadataNative* info)
Igor Murashkine7ee7632013-06-11 18:10:18 -07001277{
1278 ATRACE_CALL();
1279 ALOGV("%s", __FUNCTION__);
1280
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001281 binder::Status res;
Igor Murashkine7ee7632013-06-11 18:10:18 -07001282
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001283 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -07001284
1285 Mutex::Autolock icl(mBinderSerializationLock);
1286
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001287 if (!mDevice.get()) {
1288 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1289 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001290
Igor Murashkin099b4572013-07-12 17:52:16 -07001291 if (info != NULL) {
1292 *info = mDevice->info(); // static camera metadata
1293 // TODO: merge with device-specific camera metadata
1294 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001295
1296 return res;
1297}
1298
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001299binder::Status CameraDeviceClient::waitUntilIdle()
Zhijun He2ab500c2013-07-23 08:02:53 -07001300{
1301 ATRACE_CALL();
1302 ALOGV("%s", __FUNCTION__);
1303
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001304 binder::Status res;
1305 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Zhijun He2ab500c2013-07-23 08:02:53 -07001306
1307 Mutex::Autolock icl(mBinderSerializationLock);
1308
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001309 if (!mDevice.get()) {
1310 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1311 }
Zhijun He2ab500c2013-07-23 08:02:53 -07001312
1313 // FIXME: Also need check repeating burst.
Shuzhen Wangc9ca6782016-04-26 13:40:31 -07001314 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001315 if (mStreamingRequestId != REQUEST_ID_NONE) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001316 String8 msg = String8::format(
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001317 "Camera %s: Try to waitUntilIdle when there are active streaming requests",
1318 mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001319 ALOGE("%s: %s", __FUNCTION__, msg.string());
1320 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
Zhijun He2ab500c2013-07-23 08:02:53 -07001321 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001322 status_t err = mDevice->waitUntilDrained();
1323 if (err != OK) {
1324 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001325 "Camera %s: Error waiting to drain: %s (%d)",
1326 mCameraIdStr.string(), strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001327 }
Zhijun He2ab500c2013-07-23 08:02:53 -07001328 ALOGV("%s Done", __FUNCTION__);
Zhijun He2ab500c2013-07-23 08:02:53 -07001329 return res;
1330}
1331
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001332binder::Status CameraDeviceClient::flush(
1333 /*out*/
1334 int64_t* lastFrameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001335 ATRACE_CALL();
1336 ALOGV("%s", __FUNCTION__);
1337
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001338 binder::Status res;
1339 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001340
1341 Mutex::Autolock icl(mBinderSerializationLock);
1342
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001343 if (!mDevice.get()) {
1344 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1345 }
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001346
Shuzhen Wangc9ca6782016-04-26 13:40:31 -07001347 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001348 mStreamingRequestId = REQUEST_ID_NONE;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001349 status_t err = mDevice->flush(lastFrameNumber);
1350 if (err != OK) {
1351 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001352 "Camera %s: Error flushing device: %s (%d)", mCameraIdStr.string(), strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001353 }
1354 return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001355}
1356
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001357binder::Status CameraDeviceClient::prepare(int streamId) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001358 ATRACE_CALL();
1359 ALOGV("%s", __FUNCTION__);
1360
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001361 binder::Status res;
1362 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001363
1364 Mutex::Autolock icl(mBinderSerializationLock);
1365
1366 // Guard against trying to prepare non-created streams
1367 ssize_t index = NAME_NOT_FOUND;
1368 for (size_t i = 0; i < mStreamMap.size(); ++i) {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001369 if (streamId == mStreamMap.valueAt(i).streamId()) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001370 index = i;
1371 break;
1372 }
1373 }
1374
1375 if (index == NAME_NOT_FOUND) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001376 String8 msg = String8::format("Camera %s: Invalid stream ID (%d) specified, no stream "
1377 "with that ID exists", mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001378 ALOGW("%s: %s", __FUNCTION__, msg.string());
1379 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001380 }
1381
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001382 // Also returns BAD_VALUE if stream ID was not valid, or stream already
1383 // has been used
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001384 status_t err = mDevice->prepare(streamId);
1385 if (err == BAD_VALUE) {
1386 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001387 "Camera %s: Stream %d has already been used, and cannot be prepared",
1388 mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001389 } else if (err != OK) {
1390 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001391 "Camera %s: Error preparing stream %d: %s (%d)", mCameraIdStr.string(), streamId,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001392 strerror(-err), err);
1393 }
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001394 return res;
1395}
1396
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001397binder::Status CameraDeviceClient::prepare2(int maxCount, int streamId) {
Ruben Brunkc78ac262015-08-13 17:58:46 -07001398 ATRACE_CALL();
1399 ALOGV("%s", __FUNCTION__);
1400
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001401 binder::Status res;
1402 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Ruben Brunkc78ac262015-08-13 17:58:46 -07001403
1404 Mutex::Autolock icl(mBinderSerializationLock);
1405
1406 // Guard against trying to prepare non-created streams
1407 ssize_t index = NAME_NOT_FOUND;
1408 for (size_t i = 0; i < mStreamMap.size(); ++i) {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001409 if (streamId == mStreamMap.valueAt(i).streamId()) {
Ruben Brunkc78ac262015-08-13 17:58:46 -07001410 index = i;
1411 break;
1412 }
1413 }
1414
1415 if (index == NAME_NOT_FOUND) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001416 String8 msg = String8::format("Camera %s: Invalid stream ID (%d) specified, no stream "
1417 "with that ID exists", mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001418 ALOGW("%s: %s", __FUNCTION__, msg.string());
1419 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Ruben Brunkc78ac262015-08-13 17:58:46 -07001420 }
1421
1422 if (maxCount <= 0) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001423 String8 msg = String8::format("Camera %s: maxCount (%d) must be greater than 0",
1424 mCameraIdStr.string(), maxCount);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001425 ALOGE("%s: %s", __FUNCTION__, msg.string());
1426 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Ruben Brunkc78ac262015-08-13 17:58:46 -07001427 }
1428
1429 // Also returns BAD_VALUE if stream ID was not valid, or stream already
1430 // has been used
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001431 status_t err = mDevice->prepare(maxCount, streamId);
1432 if (err == BAD_VALUE) {
1433 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001434 "Camera %s: Stream %d has already been used, and cannot be prepared",
1435 mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001436 } else if (err != OK) {
1437 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001438 "Camera %s: Error preparing stream %d: %s (%d)", mCameraIdStr.string(), streamId,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001439 strerror(-err), err);
1440 }
Ruben Brunkc78ac262015-08-13 17:58:46 -07001441
1442 return res;
1443}
1444
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001445binder::Status CameraDeviceClient::tearDown(int streamId) {
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001446 ATRACE_CALL();
1447 ALOGV("%s", __FUNCTION__);
1448
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001449 binder::Status res;
1450 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001451
1452 Mutex::Autolock icl(mBinderSerializationLock);
1453
1454 // Guard against trying to prepare non-created streams
1455 ssize_t index = NAME_NOT_FOUND;
1456 for (size_t i = 0; i < mStreamMap.size(); ++i) {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001457 if (streamId == mStreamMap.valueAt(i).streamId()) {
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001458 index = i;
1459 break;
1460 }
1461 }
1462
1463 if (index == NAME_NOT_FOUND) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001464 String8 msg = String8::format("Camera %s: Invalid stream ID (%d) specified, no stream "
1465 "with that ID exists", mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001466 ALOGW("%s: %s", __FUNCTION__, msg.string());
1467 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001468 }
1469
1470 // Also returns BAD_VALUE if stream ID was not valid or if the stream is in
1471 // use
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001472 status_t err = mDevice->tearDown(streamId);
1473 if (err == BAD_VALUE) {
1474 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001475 "Camera %s: Stream %d is still in use, cannot be torn down",
1476 mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001477 } else if (err != OK) {
1478 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001479 "Camera %s: Error tearing down stream %d: %s (%d)", mCameraIdStr.string(), streamId,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001480 strerror(-err), err);
1481 }
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001482
1483 return res;
1484}
1485
Shuzhen Wang758c2152017-01-10 18:26:18 -08001486binder::Status CameraDeviceClient::finalizeOutputConfigurations(int32_t streamId,
Zhijun He5d677d12016-05-29 16:52:39 -07001487 const hardware::camera2::params::OutputConfiguration &outputConfiguration) {
1488 ATRACE_CALL();
1489
1490 binder::Status res;
1491 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1492
1493 Mutex::Autolock icl(mBinderSerializationLock);
1494
Shuzhen Wang0129d522016-10-30 22:43:41 -07001495 const std::vector<sp<IGraphicBufferProducer> >& bufferProducers =
1496 outputConfiguration.getGraphicBufferProducers();
Zhijun He5d677d12016-05-29 16:52:39 -07001497
Shuzhen Wang0129d522016-10-30 22:43:41 -07001498 if (bufferProducers.size() == 0) {
1499 ALOGE("%s: bufferProducers must not be empty", __FUNCTION__);
Zhijun He5d677d12016-05-29 16:52:39 -07001500 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, "Target Surface is invalid");
1501 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07001502
Shuzhen Wang758c2152017-01-10 18:26:18 -08001503 // streamId should be in mStreamMap if this stream already has a surface attached
1504 // to it. Otherwise, it should be in mDeferredStreams.
1505 bool streamIdConfigured = false;
1506 ssize_t deferredStreamIndex = NAME_NOT_FOUND;
1507 for (size_t i = 0; i < mStreamMap.size(); i++) {
1508 if (mStreamMap.valueAt(i).streamId() == streamId) {
1509 streamIdConfigured = true;
1510 break;
1511 }
Zhijun He5d677d12016-05-29 16:52:39 -07001512 }
Shuzhen Wang758c2152017-01-10 18:26:18 -08001513 for (size_t i = 0; i < mDeferredStreams.size(); i++) {
1514 if (streamId == mDeferredStreams[i]) {
1515 deferredStreamIndex = i;
1516 break;
Shuzhen Wang0129d522016-10-30 22:43:41 -07001517 }
1518
Shuzhen Wang758c2152017-01-10 18:26:18 -08001519 }
1520 if (deferredStreamIndex == NAME_NOT_FOUND && !streamIdConfigured) {
1521 String8 msg = String8::format("Camera %s: deferred surface is set to a unknown stream"
1522 "(ID %d)", mCameraIdStr.string(), streamId);
1523 ALOGW("%s: %s", __FUNCTION__, msg.string());
1524 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Zhijun He5d677d12016-05-29 16:52:39 -07001525 }
1526
Shuzhen Wang88fd0052017-02-09 16:40:07 -08001527 if (mStreamInfoMap[streamId].finalized) {
1528 String8 msg = String8::format("Camera %s: finalizeOutputConfigurations has been called"
1529 " on stream ID %d", mCameraIdStr.string(), streamId);
1530 ALOGW("%s: %s", __FUNCTION__, msg.string());
1531 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1532 }
1533
Zhijun He5d677d12016-05-29 16:52:39 -07001534 if (!mDevice.get()) {
1535 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1536 }
1537
Shuzhen Wang758c2152017-01-10 18:26:18 -08001538 std::vector<sp<Surface>> consumerSurfaces;
Shuzhen Wang758c2152017-01-10 18:26:18 -08001539 for (auto& bufferProducer : bufferProducers) {
1540 // Don't create multiple streams for the same target surface
Zhijun He5d677d12016-05-29 16:52:39 -07001541 ssize_t index = mStreamMap.indexOfKey(IInterface::asBinder(bufferProducer));
1542 if (index != NAME_NOT_FOUND) {
Shuzhen Wang758c2152017-01-10 18:26:18 -08001543 ALOGV("Camera %s: Surface already has a stream created "
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001544 " for it (ID %zd)", mCameraIdStr.string(), index);
Shuzhen Wang758c2152017-01-10 18:26:18 -08001545 continue;
Zhijun He5d677d12016-05-29 16:52:39 -07001546 }
Shuzhen Wang758c2152017-01-10 18:26:18 -08001547
1548 sp<Surface> surface;
1549 res = createSurfaceFromGbp(mStreamInfoMap[streamId], true /*isStreamInfoValid*/,
1550 surface, bufferProducer);
1551
1552 if (!res.isOk())
1553 return res;
1554
1555 consumerSurfaces.push_back(surface);
Zhijun He5d677d12016-05-29 16:52:39 -07001556 }
1557
Shuzhen Wanga81ce342017-04-13 15:18:36 -07001558 // Gracefully handle case where finalizeOutputConfigurations is called
1559 // without any new surface.
1560 if (consumerSurfaces.size() == 0) {
1561 mStreamInfoMap[streamId].finalized = true;
1562 return res;
1563 }
1564
Zhijun He5d677d12016-05-29 16:52:39 -07001565 // Finish the deferred stream configuration with the surface.
Shuzhen Wang758c2152017-01-10 18:26:18 -08001566 status_t err;
Emilian Peev40ead602017-09-26 15:46:36 +01001567 std::vector<int> consumerSurfaceIds;
1568 err = mDevice->setConsumerSurfaces(streamId, consumerSurfaces, &consumerSurfaceIds);
Zhijun He5d677d12016-05-29 16:52:39 -07001569 if (err == OK) {
Shuzhen Wang758c2152017-01-10 18:26:18 -08001570 for (size_t i = 0; i < consumerSurfaces.size(); i++) {
1571 sp<IBinder> binder = IInterface::asBinder(
1572 consumerSurfaces[i]->getIGraphicBufferProducer());
Emilian Peev40ead602017-09-26 15:46:36 +01001573 ALOGV("%s: mStreamMap add binder %p streamId %d, surfaceId %d", __FUNCTION__,
Shuzhen Wang758c2152017-01-10 18:26:18 -08001574 binder.get(), streamId, consumerSurfaceIds[i]);
1575 mStreamMap.add(binder, StreamSurfaceId(streamId, consumerSurfaceIds[i]));
1576 }
1577 if (deferredStreamIndex != NAME_NOT_FOUND) {
1578 mDeferredStreams.removeItemsAt(deferredStreamIndex);
Shuzhen Wang0129d522016-10-30 22:43:41 -07001579 }
Shuzhen Wang88fd0052017-02-09 16:40:07 -08001580 mStreamInfoMap[streamId].finalized = true;
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -08001581 mConfiguredOutputs.replaceValueFor(streamId, outputConfiguration);
Zhijun He5d677d12016-05-29 16:52:39 -07001582 } else if (err == NO_INIT) {
1583 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001584 "Camera %s: Deferred surface is invalid: %s (%d)",
1585 mCameraIdStr.string(), strerror(-err), err);
Zhijun He5d677d12016-05-29 16:52:39 -07001586 } else {
1587 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001588 "Camera %s: Error setting output stream deferred surface: %s (%d)",
1589 mCameraIdStr.string(), strerror(-err), err);
Zhijun He5d677d12016-05-29 16:52:39 -07001590 }
1591
1592 return res;
1593}
1594
Igor Murashkine7ee7632013-06-11 18:10:18 -07001595status_t CameraDeviceClient::dump(int fd, const Vector<String16>& args) {
Eino-Ville Talvalac4003962016-01-13 10:07:04 -08001596 return BasicClient::dump(fd, args);
1597}
1598
1599status_t CameraDeviceClient::dumpClient(int fd, const Vector<String16>& args) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001600 dprintf(fd, " CameraDeviceClient[%s] (%p) dump:\n",
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001601 mCameraIdStr.string(),
Eino-Ville Talvalae992e752014-11-07 16:17:48 -08001602 (getRemoteCallback() != NULL ?
Marco Nelissenf8880202014-11-14 07:58:25 -08001603 IInterface::asBinder(getRemoteCallback()).get() : NULL) );
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001604 dprintf(fd, " Current client UID %u\n", mClientUid);
Igor Murashkine7ee7632013-06-11 18:10:18 -07001605
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001606 dprintf(fd, " State:\n");
1607 dprintf(fd, " Request ID counter: %d\n", mRequestIdCounter);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001608 if (mInputStream.configured) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001609 dprintf(fd, " Current input stream ID: %d\n", mInputStream.id);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001610 } else {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001611 dprintf(fd, " No input stream configured.\n");
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001612 }
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001613 if (!mStreamMap.isEmpty()) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001614 dprintf(fd, " Current output stream/surface IDs:\n");
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001615 for (size_t i = 0; i < mStreamMap.size(); i++) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001616 dprintf(fd, " Stream %d Surface %d\n",
Shuzhen Wang0129d522016-10-30 22:43:41 -07001617 mStreamMap.valueAt(i).streamId(),
1618 mStreamMap.valueAt(i).surfaceId());
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001619 }
Zhijun He5d677d12016-05-29 16:52:39 -07001620 } else if (!mDeferredStreams.isEmpty()) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001621 dprintf(fd, " Current deferred surface output stream IDs:\n");
Zhijun He5d677d12016-05-29 16:52:39 -07001622 for (auto& streamId : mDeferredStreams) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001623 dprintf(fd, " Stream %d\n", streamId);
Zhijun He5d677d12016-05-29 16:52:39 -07001624 }
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001625 } else {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001626 dprintf(fd, " No output streams configured.\n");
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001627 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001628 // TODO: print dynamic/request section from most recent requests
1629 mFrameProcessor->dump(fd, args);
1630
1631 return dumpDevice(fd, args);
1632}
1633
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001634void CameraDeviceClient::notifyError(int32_t errorCode,
Jianing Weicb0652e2014-03-12 18:29:36 -07001635 const CaptureResultExtras& resultExtras) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001636 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001637 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001638
1639 if (remoteCb != 0) {
Jianing Weicb0652e2014-03-12 18:29:36 -07001640 remoteCb->onDeviceError(errorCode, resultExtras);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001641 }
1642}
1643
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001644void CameraDeviceClient::notifyRepeatingRequestError(long lastFrameNumber) {
1645 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
1646
1647 if (remoteCb != 0) {
Yin-Chia Yeh8ca23dc2017-09-05 18:15:56 -07001648 remoteCb->onRepeatingRequestError(lastFrameNumber, mStreamingRequestId);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001649 }
1650
Shuzhen Wangc9ca6782016-04-26 13:40:31 -07001651 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001652 mStreamingRequestId = REQUEST_ID_NONE;
1653}
1654
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001655void CameraDeviceClient::notifyIdle() {
1656 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001657 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001658
1659 if (remoteCb != 0) {
1660 remoteCb->onDeviceIdle();
1661 }
Eino-Ville Talvala412fe562015-08-20 17:08:32 -07001662 Camera2ClientBase::notifyIdle();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001663}
1664
Jianing Weicb0652e2014-03-12 18:29:36 -07001665void CameraDeviceClient::notifyShutter(const CaptureResultExtras& resultExtras,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001666 nsecs_t timestamp) {
1667 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001668 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001669 if (remoteCb != 0) {
Jianing Weicb0652e2014-03-12 18:29:36 -07001670 remoteCb->onCaptureStarted(resultExtras, timestamp);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001671 }
Eino-Ville Talvala412fe562015-08-20 17:08:32 -07001672 Camera2ClientBase::notifyShutter(resultExtras, timestamp);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001673}
1674
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001675void CameraDeviceClient::notifyPrepared(int streamId) {
1676 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001677 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001678 if (remoteCb != 0) {
1679 remoteCb->onPrepared(streamId);
1680 }
1681}
1682
Shuzhen Wang9d066012016-09-30 11:30:20 -07001683void CameraDeviceClient::notifyRequestQueueEmpty() {
1684 // Thread safe. Don't bother locking.
1685 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
1686 if (remoteCb != 0) {
1687 remoteCb->onRequestQueueEmpty();
1688 }
1689}
1690
Igor Murashkine7ee7632013-06-11 18:10:18 -07001691void CameraDeviceClient::detachDevice() {
1692 if (mDevice == 0) return;
1693
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001694 ALOGV("Camera %s: Stopping processors", mCameraIdStr.string());
Igor Murashkine7ee7632013-06-11 18:10:18 -07001695
1696 mFrameProcessor->removeListener(FRAME_PROCESSOR_LISTENER_MIN_ID,
1697 FRAME_PROCESSOR_LISTENER_MAX_ID,
1698 /*listener*/this);
1699 mFrameProcessor->requestExit();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001700 ALOGV("Camera %s: Waiting for threads", mCameraIdStr.string());
Igor Murashkine7ee7632013-06-11 18:10:18 -07001701 mFrameProcessor->join();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001702 ALOGV("Camera %s: Disconnecting device", mCameraIdStr.string());
Igor Murashkine7ee7632013-06-11 18:10:18 -07001703
1704 // WORKAROUND: HAL refuses to disconnect while there's streams in flight
1705 {
1706 mDevice->clearStreamingRequest();
1707
1708 status_t code;
1709 if ((code = mDevice->waitUntilDrained()) != OK) {
1710 ALOGE("%s: waitUntilDrained failed with code 0x%x", __FUNCTION__,
1711 code);
1712 }
1713 }
1714
1715 Camera2ClientBase::detachDevice();
1716}
1717
1718/** Device-related methods */
Jianing Weicb0652e2014-03-12 18:29:36 -07001719void CameraDeviceClient::onResultAvailable(const CaptureResult& result) {
Igor Murashkine7ee7632013-06-11 18:10:18 -07001720 ATRACE_CALL();
1721 ALOGV("%s", __FUNCTION__);
1722
Igor Murashkin4fb55c12013-08-29 17:43:01 -07001723 // Thread-safe. No lock necessary.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001724 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = mRemoteCallback;
Igor Murashkin4fb55c12013-08-29 17:43:01 -07001725 if (remoteCb != NULL) {
Shuzhen Wang5c22c152017-12-31 17:12:25 -08001726 remoteCb->onResultReceived(result.mMetadata, result.mResultExtras,
1727 result.mPhysicalMetadatas);
Igor Murashkine7ee7632013-06-11 18:10:18 -07001728 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001729}
1730
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001731binder::Status CameraDeviceClient::checkPidStatus(const char* checkLocation) {
Eino-Ville Talvala6192b892016-04-04 12:31:18 -07001732 if (mDisconnected) {
1733 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED,
1734 "The camera device has been disconnected");
1735 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001736 status_t res = checkPid(checkLocation);
1737 return (res == OK) ? binder::Status::ok() :
1738 STATUS_ERROR(CameraService::ERROR_PERMISSION_DENIED,
1739 "Attempt to use camera from a different process than original client");
1740}
1741
Igor Murashkine7ee7632013-06-11 18:10:18 -07001742// TODO: move to Camera2ClientBase
1743bool CameraDeviceClient::enforceRequestPermissions(CameraMetadata& metadata) {
1744
1745 const int pid = IPCThreadState::self()->getCallingPid();
1746 const int selfPid = getpid();
1747 camera_metadata_entry_t entry;
1748
1749 /**
1750 * Mixin default important security values
1751 * - android.led.transmit = defaulted ON
1752 */
1753 CameraMetadata staticInfo = mDevice->info();
1754 entry = staticInfo.find(ANDROID_LED_AVAILABLE_LEDS);
1755 for(size_t i = 0; i < entry.count; ++i) {
1756 uint8_t led = entry.data.u8[i];
1757
1758 switch(led) {
1759 case ANDROID_LED_AVAILABLE_LEDS_TRANSMIT: {
1760 uint8_t transmitDefault = ANDROID_LED_TRANSMIT_ON;
1761 if (!metadata.exists(ANDROID_LED_TRANSMIT)) {
1762 metadata.update(ANDROID_LED_TRANSMIT,
1763 &transmitDefault, 1);
1764 }
1765 break;
1766 }
1767 }
1768 }
1769
1770 // We can do anything!
1771 if (pid == selfPid) {
1772 return true;
1773 }
1774
1775 /**
1776 * Permission check special fields in the request
1777 * - android.led.transmit = android.permission.CAMERA_DISABLE_TRANSMIT
1778 */
1779 entry = metadata.find(ANDROID_LED_TRANSMIT);
1780 if (entry.count > 0 && entry.data.u8[0] != ANDROID_LED_TRANSMIT_ON) {
1781 String16 permissionString =
1782 String16("android.permission.CAMERA_DISABLE_TRANSMIT_LED");
1783 if (!checkCallingPermission(permissionString)) {
1784 const int uid = IPCThreadState::self()->getCallingUid();
1785 ALOGE("Permission Denial: "
1786 "can't disable transmit LED pid=%d, uid=%d", pid, uid);
1787 return false;
1788 }
1789 }
1790
1791 return true;
1792}
1793
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -07001794status_t CameraDeviceClient::getRotationTransformLocked(int32_t* transform) {
1795 ALOGV("%s: begin", __FUNCTION__);
1796
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -07001797 const CameraMetadata& staticInfo = mDevice->info();
Ruben Brunk5698d442014-06-18 10:39:40 -07001798 return CameraUtils::getRotationTransform(staticInfo, transform);
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -07001799}
1800
Igor Murashkine7ee7632013-06-11 18:10:18 -07001801} // namespace android