Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 1 | /* |
| 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 Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 20 | #include <android/hardware/camera2/BnCameraDeviceUser.h> |
| 21 | #include <android/hardware/camera2/ICameraDeviceCallbacks.h> |
Yin-Chia Yeh | b97babb | 2015-03-12 13:42:44 -0700 | [diff] [blame] | 22 | #include <camera/camera2/OutputConfiguration.h> |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 23 | #include <camera/camera2/SubmitInfo.h> |
Eino-Ville Talvala | 7b82efe | 2013-07-25 17:12:35 -0700 | [diff] [blame] | 24 | |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 25 | #include "CameraService.h" |
Eino-Ville Talvala | 7b82efe | 2013-07-25 17:12:35 -0700 | [diff] [blame] | 26 | #include "common/FrameProcessorBase.h" |
| 27 | #include "common/Camera2ClientBase.h" |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 28 | |
| 29 | namespace android { |
| 30 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 31 | struct CameraDeviceClientBase : |
| 32 | public CameraService::BasicClient, |
| 33 | public hardware::camera2::BnCameraDeviceUser |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 34 | { |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 35 | typedef hardware::camera2::ICameraDeviceCallbacks TCamCallbacks; |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 36 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 37 | const sp<hardware::camera2::ICameraDeviceCallbacks>& getRemoteCallback() { |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 38 | return mRemoteCallback; |
| 39 | } |
| 40 | |
| 41 | protected: |
| 42 | CameraDeviceClientBase(const sp<CameraService>& cameraService, |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 43 | const sp<hardware::camera2::ICameraDeviceCallbacks>& remoteCallback, |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 44 | const String16& clientPackageName, |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 45 | const String8& cameraId, |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 46 | int cameraFacing, |
| 47 | int clientPid, |
| 48 | uid_t clientUid, |
| 49 | int servicePid); |
| 50 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 51 | sp<hardware::camera2::ICameraDeviceCallbacks> mRemoteCallback; |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 52 | }; |
| 53 | |
| 54 | /** |
| 55 | * Implements the binder ICameraDeviceUser API, |
| 56 | * meant for HAL3-public implementation of |
| 57 | * android.hardware.photography.CameraDevice |
| 58 | */ |
| 59 | class CameraDeviceClient : |
| 60 | public Camera2ClientBase<CameraDeviceClientBase>, |
Eino-Ville Talvala | 7b82efe | 2013-07-25 17:12:35 -0700 | [diff] [blame] | 61 | public camera2::FrameProcessorBase::FilteredListener |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 62 | { |
| 63 | public: |
| 64 | /** |
| 65 | * ICameraDeviceUser interface (see ICameraDeviceUser for details) |
| 66 | */ |
| 67 | |
| 68 | // Note that the callee gets a copy of the metadata. |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 69 | virtual binder::Status submitRequest( |
| 70 | const hardware::camera2::CaptureRequest& request, |
| 71 | bool streaming = false, |
| 72 | /*out*/ |
| 73 | hardware::camera2::utils::SubmitInfo *submitInfo = nullptr); |
Jianing Wei | 90e59c9 | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 74 | // List of requests are copied. |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 75 | 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 Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 83 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 84 | virtual binder::Status beginConfigure(); |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 85 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 86 | virtual binder::Status endConfigure(bool isConstrainedHighSpeed = false); |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 87 | |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 88 | // Returns -EBUSY if device is not idle |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 89 | virtual binder::Status deleteStream(int streamId); |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 90 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 91 | virtual binder::Status createStream( |
| 92 | const hardware::camera2::params::OutputConfiguration &outputConfiguration, |
| 93 | /*out*/ |
| 94 | int32_t* newStreamId = NULL); |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 95 | |
Chien-Yu Chen | 618ff8a | 2015-03-13 11:27:17 -0700 | [diff] [blame] | 96 | // Create an input stream of width, height, and format. |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 97 | virtual binder::Status createInputStream(int width, int height, int format, |
| 98 | /*out*/ |
| 99 | int32_t* newStreamId = NULL); |
Chien-Yu Chen | 618ff8a | 2015-03-13 11:27:17 -0700 | [diff] [blame] | 100 | |
| 101 | // Get the buffer producer of the input stream |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 102 | virtual binder::Status getInputSurface( |
| 103 | /*out*/ |
| 104 | view::Surface *inputSurface); |
Chien-Yu Chen | 618ff8a | 2015-03-13 11:27:17 -0700 | [diff] [blame] | 105 | |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 106 | // Create a request object from a template. |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 107 | virtual binder::Status createDefaultRequest(int templateId, |
| 108 | /*out*/ |
| 109 | hardware::camera2::impl::CameraMetadataNative* request); |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 110 | |
| 111 | // Get the static metadata for the camera |
| 112 | // -- Caller owns the newly allocated metadata |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 113 | virtual binder::Status getCameraInfo( |
| 114 | /*out*/ |
| 115 | hardware::camera2::impl::CameraMetadataNative* cameraCharacteristics); |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 116 | |
Zhijun He | 2ab500c | 2013-07-23 08:02:53 -0700 | [diff] [blame] | 117 | // Wait until all the submitted requests have finished processing |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 118 | virtual binder::Status waitUntilIdle(); |
Eino-Ville Talvala | abaa51d | 2013-08-14 11:37:00 -0700 | [diff] [blame] | 119 | |
| 120 | // Flush all active and pending requests as fast as possible |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 121 | virtual binder::Status flush( |
| 122 | /*out*/ |
| 123 | int64_t* lastFrameNumber = NULL); |
Eino-Ville Talvala | abaa51d | 2013-08-14 11:37:00 -0700 | [diff] [blame] | 124 | |
Eino-Ville Talvala | 4d44cad | 2015-04-11 13:15:45 -0700 | [diff] [blame] | 125 | // Prepare stream by preallocating its buffers |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 126 | virtual binder::Status prepare(int32_t streamId); |
Eino-Ville Talvala | 4d44cad | 2015-04-11 13:15:45 -0700 | [diff] [blame] | 127 | |
Eino-Ville Talvala | b25e3c8 | 2015-07-15 16:04:27 -0700 | [diff] [blame] | 128 | // Tear down stream resources by freeing its unused buffers |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 129 | virtual binder::Status tearDown(int32_t streamId); |
Eino-Ville Talvala | b25e3c8 | 2015-07-15 16:04:27 -0700 | [diff] [blame] | 130 | |
Ruben Brunk | c78ac26 | 2015-08-13 17:58:46 -0700 | [diff] [blame] | 131 | // Prepare stream by preallocating up to maxCount of its buffers |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 132 | virtual binder::Status prepare2(int32_t maxCount, int32_t streamId); |
Ruben Brunk | c78ac26 | 2015-08-13 17:58:46 -0700 | [diff] [blame] | 133 | |
Zhijun He | 5d677d1 | 2016-05-29 16:52:39 -0700 | [diff] [blame] | 134 | // Set the deferred surface for a stream. |
| 135 | virtual binder::Status setDeferredConfiguration(int32_t streamId, |
| 136 | const hardware::camera2::params::OutputConfiguration &outputConfiguration); |
| 137 | |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 138 | /** |
| 139 | * Interface used by CameraService |
| 140 | */ |
| 141 | |
| 142 | CameraDeviceClient(const sp<CameraService>& cameraService, |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 143 | const sp<hardware::camera2::ICameraDeviceCallbacks>& remoteCallback, |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 144 | const String16& clientPackageName, |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 145 | const String8& cameraId, |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 146 | int cameraFacing, |
| 147 | int clientPid, |
| 148 | uid_t clientUid, |
| 149 | int servicePid); |
| 150 | virtual ~CameraDeviceClient(); |
| 151 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 152 | virtual status_t initialize(CameraModule *module) override; |
| 153 | virtual status_t initialize(sp<CameraProviderManager> manager) override; |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 154 | |
| 155 | virtual status_t dump(int fd, const Vector<String16>& args); |
| 156 | |
Eino-Ville Talvala | c400396 | 2016-01-13 10:07:04 -0800 | [diff] [blame] | 157 | virtual status_t dumpClient(int fd, const Vector<String16>& args); |
| 158 | |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 159 | /** |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 160 | * Device listener interface |
| 161 | */ |
| 162 | |
| 163 | virtual void notifyIdle(); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 164 | virtual void notifyError(int32_t errorCode, |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 165 | const CaptureResultExtras& resultExtras); |
| 166 | virtual void notifyShutter(const CaptureResultExtras& resultExtras, nsecs_t timestamp); |
Eino-Ville Talvala | 4d44cad | 2015-04-11 13:15:45 -0700 | [diff] [blame] | 167 | virtual void notifyPrepared(int streamId); |
Shuzhen Wang | 9d06601 | 2016-09-30 11:30:20 -0700 | [diff] [blame] | 168 | virtual void notifyRequestQueueEmpty(); |
Chien-Yu Chen | e8c535e | 2016-04-14 12:18:26 -0700 | [diff] [blame] | 169 | virtual void notifyRepeatingRequestError(long lastFrameNumber); |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 170 | |
| 171 | /** |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 172 | * Interface used by independent components of CameraDeviceClient. |
| 173 | */ |
| 174 | protected: |
| 175 | /** FilteredListener implementation **/ |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 176 | virtual void onResultAvailable(const CaptureResult& result); |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 177 | virtual void detachDevice(); |
| 178 | |
Igor Murashkin | f8b2a6f | 2013-09-17 17:03:28 -0700 | [diff] [blame] | 179 | // Calculate the ANativeWindow transform from android.sensor.orientation |
| 180 | status_t getRotationTransformLocked(/*out*/int32_t* transform); |
| 181 | |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 182 | private: |
| 183 | /** ICameraDeviceUser interface-related private members */ |
| 184 | |
| 185 | /** Preview callback related members */ |
Eino-Ville Talvala | 7b82efe | 2013-07-25 17:12:35 -0700 | [diff] [blame] | 186 | sp<camera2::FrameProcessorBase> mFrameProcessor; |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 187 | static const int32_t FRAME_PROCESSOR_LISTENER_MIN_ID = 0; |
| 188 | static const int32_t FRAME_PROCESSOR_LISTENER_MAX_ID = 0x7fffffffL; |
| 189 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 190 | template<typename TProviderPtr> |
| 191 | status_t initializeImpl(TProviderPtr providerPtr); |
| 192 | |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 193 | /** Utility members */ |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 194 | binder::Status checkPidStatus(const char* checkLocation); |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 195 | bool enforceRequestPermissions(CameraMetadata& metadata); |
| 196 | |
Ruben Brunk | bba7557 | 2014-11-20 17:29:50 -0800 | [diff] [blame] | 197 | // Find the square of the euclidean distance between two points |
| 198 | static int64_t euclidDistSquare(int32_t x0, int32_t y0, int32_t x1, int32_t y1); |
| 199 | |
Zhijun He | 5d677d1 | 2016-05-29 16:52:39 -0700 | [diff] [blame] | 200 | // Create an output stream with surface deferred for future. |
| 201 | binder::Status createDeferredSurfaceStreamLocked( |
| 202 | const hardware::camera2::params::OutputConfiguration &outputConfiguration, |
| 203 | int* newStreamId = NULL); |
| 204 | |
| 205 | // Set the stream transform flags to automatically rotate the camera stream for preview use |
| 206 | // cases. |
| 207 | binder::Status setStreamTransformLocked(int streamId); |
| 208 | |
Ruben Brunk | bba7557 | 2014-11-20 17:29:50 -0800 | [diff] [blame] | 209 | // Find the closest dimensions for a given format in available stream configurations with |
| 210 | // a width <= ROUNDING_WIDTH_CAP |
Ruben Brunk | 77a77f6 | 2015-06-12 16:15:35 -0700 | [diff] [blame] | 211 | static const int32_t ROUNDING_WIDTH_CAP = 1920; |
Ruben Brunk | bba7557 | 2014-11-20 17:29:50 -0800 | [diff] [blame] | 212 | static bool roundBufferDimensionNearest(int32_t width, int32_t height, int32_t format, |
Eino-Ville Talvala | 3d82c0d | 2015-02-23 15:19:19 -0800 | [diff] [blame] | 213 | android_dataspace dataSpace, const CameraMetadata& info, |
| 214 | /*out*/int32_t* outWidth, /*out*/int32_t* outHeight); |
Ruben Brunk | bba7557 | 2014-11-20 17:29:50 -0800 | [diff] [blame] | 215 | |
Eman Copty | 6d7af0e | 2016-06-17 20:46:40 -0700 | [diff] [blame] | 216 | //check if format is not custom format |
| 217 | static bool isPublicFormat(int32_t format); |
| 218 | |
Chien-Yu Chen | 618ff8a | 2015-03-13 11:27:17 -0700 | [diff] [blame] | 219 | // IGraphicsBufferProducer binder -> Stream ID for output streams |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 220 | KeyedVector<sp<IBinder>, int> mStreamMap; |
| 221 | |
Chien-Yu Chen | 618ff8a | 2015-03-13 11:27:17 -0700 | [diff] [blame] | 222 | struct InputStreamConfiguration { |
| 223 | bool configured; |
| 224 | int32_t width; |
| 225 | int32_t height; |
| 226 | int32_t format; |
| 227 | int32_t id; |
| 228 | } mInputStream; |
| 229 | |
Chien-Yu Chen | e8c535e | 2016-04-14 12:18:26 -0700 | [diff] [blame] | 230 | // Streaming request ID |
| 231 | int32_t mStreamingRequestId; |
Shuzhen Wang | c9ca678 | 2016-04-26 13:40:31 -0700 | [diff] [blame] | 232 | Mutex mStreamingRequestIdLock; |
Chien-Yu Chen | e8c535e | 2016-04-14 12:18:26 -0700 | [diff] [blame] | 233 | static const int32_t REQUEST_ID_NONE = -1; |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 234 | |
| 235 | int32_t mRequestIdCounter; |
Eino-Ville Talvala | 412fe56 | 2015-08-20 17:08:32 -0700 | [diff] [blame] | 236 | |
Zhijun He | 5d677d1 | 2016-05-29 16:52:39 -0700 | [diff] [blame] | 237 | // The list of output streams whose surfaces are deferred. We have to track them separately |
| 238 | // as there are no surfaces available and can not be put into mStreamMap. Once the deferred |
| 239 | // Surface is configured, the stream id will be moved to mStreamMap. |
| 240 | Vector<int32_t> mDeferredStreams; |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 241 | }; |
| 242 | |
| 243 | }; // namespace android |
| 244 | |
| 245 | #endif |