blob: d792b7d77e941289deadba5d5751568d9e411a69 [file] [log] [blame]
Igor Murashkine7ee7632013-06-11 18:10:18 -07001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ANDROID_SERVERS_CAMERA_PHOTOGRAPHY_CAMERADEVICECLIENT_H
18#define ANDROID_SERVERS_CAMERA_PHOTOGRAPHY_CAMERADEVICECLIENT_H
19
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080020#include <android/hardware/camera2/BnCameraDeviceUser.h>
21#include <android/hardware/camera2/ICameraDeviceCallbacks.h>
Yin-Chia Yehb97babb2015-03-12 13:42:44 -070022#include <camera/camera2/OutputConfiguration.h>
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080023#include <camera/camera2/SubmitInfo.h>
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070024
Igor Murashkine7ee7632013-06-11 18:10:18 -070025#include "CameraService.h"
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070026#include "common/FrameProcessorBase.h"
27#include "common/Camera2ClientBase.h"
Igor Murashkine7ee7632013-06-11 18:10:18 -070028
29namespace android {
30
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080031struct CameraDeviceClientBase :
32 public CameraService::BasicClient,
33 public hardware::camera2::BnCameraDeviceUser
Igor Murashkine7ee7632013-06-11 18:10:18 -070034{
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080035 typedef hardware::camera2::ICameraDeviceCallbacks TCamCallbacks;
Igor Murashkine7ee7632013-06-11 18:10:18 -070036
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080037 const sp<hardware::camera2::ICameraDeviceCallbacks>& getRemoteCallback() {
Igor Murashkine7ee7632013-06-11 18:10:18 -070038 return mRemoteCallback;
39 }
40
41protected:
42 CameraDeviceClientBase(const sp<CameraService>& cameraService,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080043 const sp<hardware::camera2::ICameraDeviceCallbacks>& remoteCallback,
Igor Murashkine7ee7632013-06-11 18:10:18 -070044 const String16& clientPackageName,
45 int cameraId,
46 int cameraFacing,
47 int clientPid,
48 uid_t clientUid,
49 int servicePid);
50
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080051 sp<hardware::camera2::ICameraDeviceCallbacks> mRemoteCallback;
Igor Murashkine7ee7632013-06-11 18:10:18 -070052};
53
54/**
55 * Implements the binder ICameraDeviceUser API,
56 * meant for HAL3-public implementation of
57 * android.hardware.photography.CameraDevice
58 */
59class CameraDeviceClient :
60 public Camera2ClientBase<CameraDeviceClientBase>,
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070061 public camera2::FrameProcessorBase::FilteredListener
Igor Murashkine7ee7632013-06-11 18:10:18 -070062{
63public:
64 /**
65 * ICameraDeviceUser interface (see ICameraDeviceUser for details)
66 */
67
68 // Note that the callee gets a copy of the metadata.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080069 virtual binder::Status submitRequest(
70 const hardware::camera2::CaptureRequest& request,
71 bool streaming = false,
72 /*out*/
73 hardware::camera2::utils::SubmitInfo *submitInfo = nullptr);
Jianing Wei90e59c92014-03-12 18:29:36 -070074 // List of requests are copied.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080075 virtual binder::Status submitRequestList(
76 const std::vector<hardware::camera2::CaptureRequest>& requests,
77 bool streaming = false,
78 /*out*/
79 hardware::camera2::utils::SubmitInfo *submitInfo = nullptr);
80 virtual binder::Status cancelRequest(int requestId,
81 /*out*/
82 int64_t* lastFrameNumber = NULL);
Igor Murashkine7ee7632013-06-11 18:10:18 -070083
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080084 virtual binder::Status beginConfigure();
Ruben Brunkb2119af2014-05-09 19:57:56 -070085
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080086 virtual binder::Status endConfigure(bool isConstrainedHighSpeed = false);
Ruben Brunkb2119af2014-05-09 19:57:56 -070087
Igor Murashkine7ee7632013-06-11 18:10:18 -070088 // Returns -EBUSY if device is not idle
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080089 virtual binder::Status deleteStream(int streamId);
Igor Murashkine7ee7632013-06-11 18:10:18 -070090
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080091 virtual binder::Status createStream(
92 const hardware::camera2::params::OutputConfiguration &outputConfiguration,
93 /*out*/
94 int32_t* newStreamId = NULL);
Igor Murashkine7ee7632013-06-11 18:10:18 -070095
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -070096 // Create an input stream of width, height, and format.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080097 virtual binder::Status createInputStream(int width, int height, int format,
98 /*out*/
99 int32_t* newStreamId = NULL);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700100
101 // Get the buffer producer of the input stream
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800102 virtual binder::Status getInputSurface(
103 /*out*/
104 view::Surface *inputSurface);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700105
Igor Murashkine7ee7632013-06-11 18:10:18 -0700106 // Create a request object from a template.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800107 virtual binder::Status createDefaultRequest(int templateId,
108 /*out*/
109 hardware::camera2::impl::CameraMetadataNative* request);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700110
111 // Get the static metadata for the camera
112 // -- Caller owns the newly allocated metadata
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800113 virtual binder::Status getCameraInfo(
114 /*out*/
115 hardware::camera2::impl::CameraMetadataNative* cameraCharacteristics);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700116
Zhijun He2ab500c2013-07-23 08:02:53 -0700117 // Wait until all the submitted requests have finished processing
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800118 virtual binder::Status waitUntilIdle();
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -0700119
120 // Flush all active and pending requests as fast as possible
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800121 virtual binder::Status flush(
122 /*out*/
123 int64_t* lastFrameNumber = NULL);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -0700124
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700125 // Prepare stream by preallocating its buffers
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800126 virtual binder::Status prepare(int32_t streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700127
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -0700128 // Tear down stream resources by freeing its unused buffers
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800129 virtual binder::Status tearDown(int32_t streamId);
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -0700130
Ruben Brunkc78ac262015-08-13 17:58:46 -0700131 // Prepare stream by preallocating up to maxCount of its buffers
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800132 virtual binder::Status prepare2(int32_t maxCount, int32_t streamId);
Ruben Brunkc78ac262015-08-13 17:58:46 -0700133
Igor Murashkine7ee7632013-06-11 18:10:18 -0700134 /**
135 * Interface used by CameraService
136 */
137
138 CameraDeviceClient(const sp<CameraService>& cameraService,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800139 const sp<hardware::camera2::ICameraDeviceCallbacks>& remoteCallback,
Igor Murashkine7ee7632013-06-11 18:10:18 -0700140 const String16& clientPackageName,
141 int cameraId,
142 int cameraFacing,
143 int clientPid,
144 uid_t clientUid,
145 int servicePid);
146 virtual ~CameraDeviceClient();
147
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800148 virtual status_t initialize(CameraModule *module);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700149
150 virtual status_t dump(int fd, const Vector<String16>& args);
151
Eino-Ville Talvalac4003962016-01-13 10:07:04 -0800152 virtual status_t dumpClient(int fd, const Vector<String16>& args);
153
Igor Murashkine7ee7632013-06-11 18:10:18 -0700154 /**
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700155 * Device listener interface
156 */
157
158 virtual void notifyIdle();
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800159 virtual void notifyError(int32_t errorCode,
Jianing Weicb0652e2014-03-12 18:29:36 -0700160 const CaptureResultExtras& resultExtras);
161 virtual void notifyShutter(const CaptureResultExtras& resultExtras, nsecs_t timestamp);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700162 virtual void notifyPrepared(int streamId);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700163 virtual void notifyRepeatingRequestError(long lastFrameNumber);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700164
165 /**
Igor Murashkine7ee7632013-06-11 18:10:18 -0700166 * Interface used by independent components of CameraDeviceClient.
167 */
168protected:
169 /** FilteredListener implementation **/
Jianing Weicb0652e2014-03-12 18:29:36 -0700170 virtual void onResultAvailable(const CaptureResult& result);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700171 virtual void detachDevice();
172
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -0700173 // Calculate the ANativeWindow transform from android.sensor.orientation
174 status_t getRotationTransformLocked(/*out*/int32_t* transform);
175
Igor Murashkine7ee7632013-06-11 18:10:18 -0700176private:
177 /** ICameraDeviceUser interface-related private members */
178
179 /** Preview callback related members */
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -0700180 sp<camera2::FrameProcessorBase> mFrameProcessor;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700181 static const int32_t FRAME_PROCESSOR_LISTENER_MIN_ID = 0;
182 static const int32_t FRAME_PROCESSOR_LISTENER_MAX_ID = 0x7fffffffL;
183
184 /** Utility members */
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800185 binder::Status checkPidStatus(const char* checkLocation);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700186 bool enforceRequestPermissions(CameraMetadata& metadata);
187
Ruben Brunkbba75572014-11-20 17:29:50 -0800188 // Find the square of the euclidean distance between two points
189 static int64_t euclidDistSquare(int32_t x0, int32_t y0, int32_t x1, int32_t y1);
190
191 // Find the closest dimensions for a given format in available stream configurations with
192 // a width <= ROUNDING_WIDTH_CAP
Ruben Brunk77a77f62015-06-12 16:15:35 -0700193 static const int32_t ROUNDING_WIDTH_CAP = 1920;
Ruben Brunkbba75572014-11-20 17:29:50 -0800194 static bool roundBufferDimensionNearest(int32_t width, int32_t height, int32_t format,
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -0800195 android_dataspace dataSpace, const CameraMetadata& info,
196 /*out*/int32_t* outWidth, /*out*/int32_t* outHeight);
Ruben Brunkbba75572014-11-20 17:29:50 -0800197
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700198 // IGraphicsBufferProducer binder -> Stream ID for output streams
Igor Murashkine7ee7632013-06-11 18:10:18 -0700199 KeyedVector<sp<IBinder>, int> mStreamMap;
200
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700201 struct InputStreamConfiguration {
202 bool configured;
203 int32_t width;
204 int32_t height;
205 int32_t format;
206 int32_t id;
207 } mInputStream;
208
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700209 // Streaming request ID
210 int32_t mStreamingRequestId;
Shuzhen Wangc9ca6782016-04-26 13:40:31 -0700211 Mutex mStreamingRequestIdLock;
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700212 static const int32_t REQUEST_ID_NONE = -1;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700213
214 int32_t mRequestIdCounter;
Eino-Ville Talvala412fe562015-08-20 17:08:32 -0700215
Igor Murashkine7ee7632013-06-11 18:10:18 -0700216};
217
218}; // namespace android
219
220#endif