Home | History | Annotate | Download | only in api2
      1 /*
      2  * Copyright (C) 2013-2018 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 
     20 #include <android/hardware/camera2/BnCameraDeviceUser.h>
     21 #include <android/hardware/camera2/ICameraDeviceCallbacks.h>
     22 #include <camera/camera2/OutputConfiguration.h>
     23 #include <camera/camera2/SessionConfiguration.h>
     24 #include <camera/camera2/SubmitInfo.h>
     25 
     26 #include "CameraService.h"
     27 #include "common/FrameProcessorBase.h"
     28 #include "common/Camera2ClientBase.h"
     29 #include "CompositeStream.h"
     30 
     31 using android::camera3::OutputStreamInfo;
     32 using android::camera3::CompositeStream;
     33 
     34 namespace android {
     35 
     36 struct CameraDeviceClientBase :
     37          public CameraService::BasicClient,
     38          public hardware::camera2::BnCameraDeviceUser
     39 {
     40     typedef hardware::camera2::ICameraDeviceCallbacks TCamCallbacks;
     41 
     42     const sp<hardware::camera2::ICameraDeviceCallbacks>& getRemoteCallback() {
     43         return mRemoteCallback;
     44     }
     45 
     46 protected:
     47     CameraDeviceClientBase(const sp<CameraService>& cameraService,
     48             const sp<hardware::camera2::ICameraDeviceCallbacks>& remoteCallback,
     49             const String16& clientPackageName,
     50             const String8& cameraId,
     51             int api1CameraId,
     52             int cameraFacing,
     53             int clientPid,
     54             uid_t clientUid,
     55             int servicePid);
     56 
     57     sp<hardware::camera2::ICameraDeviceCallbacks> mRemoteCallback;
     58 };
     59 
     60 /**
     61  * Implements the binder ICameraDeviceUser API,
     62  * meant for HAL3-public implementation of
     63  * android.hardware.photography.CameraDevice
     64  */
     65 class CameraDeviceClient :
     66         public Camera2ClientBase<CameraDeviceClientBase>,
     67         public camera2::FrameProcessorBase::FilteredListener
     68 {
     69 public:
     70     /**
     71      * ICameraDeviceUser interface (see ICameraDeviceUser for details)
     72      */
     73 
     74     // Note that the callee gets a copy of the metadata.
     75     virtual binder::Status submitRequest(
     76             const hardware::camera2::CaptureRequest& request,
     77             bool streaming = false,
     78             /*out*/
     79             hardware::camera2::utils::SubmitInfo *submitInfo = nullptr) override;
     80     // List of requests are copied.
     81     virtual binder::Status submitRequestList(
     82             const std::vector<hardware::camera2::CaptureRequest>& requests,
     83             bool streaming = false,
     84             /*out*/
     85             hardware::camera2::utils::SubmitInfo *submitInfo = nullptr) override;
     86     virtual binder::Status cancelRequest(int requestId,
     87             /*out*/
     88             int64_t* lastFrameNumber = NULL) override;
     89 
     90     virtual binder::Status beginConfigure() override;
     91 
     92     virtual binder::Status endConfigure(int operatingMode,
     93             const hardware::camera2::impl::CameraMetadataNative& sessionParams) override;
     94 
     95     // Verify specific session configuration.
     96     virtual binder::Status isSessionConfigurationSupported(
     97             const SessionConfiguration& sessionConfiguration,
     98             /*out*/
     99             bool* streamStatus) override;
    100 
    101     // Returns -EBUSY if device is not idle or in error state
    102     virtual binder::Status deleteStream(int streamId) override;
    103 
    104     virtual binder::Status createStream(
    105             const hardware::camera2::params::OutputConfiguration &outputConfiguration,
    106             /*out*/
    107             int32_t* newStreamId = NULL) override;
    108 
    109     // Create an input stream of width, height, and format.
    110     virtual binder::Status createInputStream(int width, int height, int format,
    111             /*out*/
    112             int32_t* newStreamId = NULL) override;
    113 
    114     // Get the buffer producer of the input stream
    115     virtual binder::Status getInputSurface(
    116             /*out*/
    117             view::Surface *inputSurface) override;
    118 
    119     // Create a request object from a template.
    120     virtual binder::Status createDefaultRequest(int templateId,
    121             /*out*/
    122             hardware::camera2::impl::CameraMetadataNative* request) override;
    123 
    124     // Get the static metadata for the camera
    125     // -- Caller owns the newly allocated metadata
    126     virtual binder::Status getCameraInfo(
    127             /*out*/
    128             hardware::camera2::impl::CameraMetadataNative* cameraCharacteristics) override;
    129 
    130     // Wait until all the submitted requests have finished processing
    131     virtual binder::Status waitUntilIdle() override;
    132 
    133     // Flush all active and pending requests as fast as possible
    134     virtual binder::Status flush(
    135             /*out*/
    136             int64_t* lastFrameNumber = NULL) override;
    137 
    138     // Prepare stream by preallocating its buffers
    139     virtual binder::Status prepare(int32_t streamId) override;
    140 
    141     // Tear down stream resources by freeing its unused buffers
    142     virtual binder::Status tearDown(int32_t streamId) override;
    143 
    144     // Prepare stream by preallocating up to maxCount of its buffers
    145     virtual binder::Status prepare2(int32_t maxCount, int32_t streamId) override;
    146 
    147     // Update an output configuration
    148     virtual binder::Status updateOutputConfiguration(int streamId,
    149             const hardware::camera2::params::OutputConfiguration &outputConfiguration) override;
    150 
    151     // Finalize the output configurations with surfaces not added before.
    152     virtual binder::Status finalizeOutputConfigurations(int32_t streamId,
    153             const hardware::camera2::params::OutputConfiguration &outputConfiguration) override;
    154 
    155     /**
    156      * Interface used by CameraService
    157      */
    158 
    159     CameraDeviceClient(const sp<CameraService>& cameraService,
    160             const sp<hardware::camera2::ICameraDeviceCallbacks>& remoteCallback,
    161             const String16& clientPackageName,
    162             const String8& cameraId,
    163             int cameraFacing,
    164             int clientPid,
    165             uid_t clientUid,
    166             int servicePid);
    167     virtual ~CameraDeviceClient();
    168 
    169     virtual status_t      initialize(sp<CameraProviderManager> manager,
    170             const String8& monitorTags) override;
    171 
    172     virtual status_t      dump(int fd, const Vector<String16>& args);
    173 
    174     virtual status_t      dumpClient(int fd, const Vector<String16>& args);
    175 
    176     /**
    177      * Device listener interface
    178      */
    179 
    180     virtual void notifyIdle();
    181     virtual void notifyError(int32_t errorCode,
    182                              const CaptureResultExtras& resultExtras);
    183     virtual void notifyShutter(const CaptureResultExtras& resultExtras, nsecs_t timestamp);
    184     virtual void notifyPrepared(int streamId);
    185     virtual void notifyRequestQueueEmpty();
    186     virtual void notifyRepeatingRequestError(long lastFrameNumber);
    187 
    188     /**
    189      * Interface used by independent components of CameraDeviceClient.
    190      */
    191 protected:
    192     /** FilteredListener implementation **/
    193     virtual void          onResultAvailable(const CaptureResult& result);
    194     virtual void          detachDevice();
    195 
    196     // Calculate the ANativeWindow transform from android.sensor.orientation
    197     status_t              getRotationTransformLocked(/*out*/int32_t* transform);
    198 
    199 private:
    200     // StreamSurfaceId encapsulates streamId + surfaceId for a particular surface.
    201     // streamId specifies the index of the stream the surface belongs to, and the
    202     // surfaceId specifies the index of the surface within the stream. (one stream
    203     // could contain multiple surfaces.)
    204     class StreamSurfaceId final {
    205     public:
    206         StreamSurfaceId() {
    207             mStreamId = -1;
    208             mSurfaceId = -1;
    209         }
    210         StreamSurfaceId(int32_t streamId, int32_t surfaceId) {
    211             mStreamId = streamId;
    212             mSurfaceId = surfaceId;
    213         }
    214         int32_t streamId() const {
    215             return mStreamId;
    216         }
    217         int32_t surfaceId() const {
    218             return mSurfaceId;
    219         }
    220 
    221     private:
    222         int32_t mStreamId;
    223         int32_t mSurfaceId;
    224 
    225     }; // class StreamSurfaceId
    226 
    227 private:
    228     /** ICameraDeviceUser interface-related private members */
    229 
    230     /** Preview callback related members */
    231     sp<camera2::FrameProcessorBase> mFrameProcessor;
    232     static const int32_t FRAME_PROCESSOR_LISTENER_MIN_ID = 0;
    233     static const int32_t FRAME_PROCESSOR_LISTENER_MAX_ID = 0x7fffffffL;
    234 
    235     std::vector<int32_t> mSupportedPhysicalRequestKeys;
    236 
    237     template<typename TProviderPtr>
    238     status_t      initializeImpl(TProviderPtr providerPtr, const String8& monitorTags);
    239 
    240     /** Utility members */
    241     binder::Status checkPidStatus(const char* checkLocation);
    242     binder::Status checkOperatingModeLocked(int operatingMode) const;
    243     binder::Status checkPhysicalCameraIdLocked(String8 physicalCameraId);
    244     binder::Status checkSurfaceTypeLocked(size_t numBufferProducers, bool deferredConsumer,
    245             int surfaceType) const;
    246     static void mapStreamInfo(const OutputStreamInfo &streamInfo,
    247             camera3_stream_rotation_t rotation, String8 physicalId,
    248             hardware::camera::device::V3_4::Stream *stream /*out*/);
    249     bool enforceRequestPermissions(CameraMetadata& metadata);
    250 
    251     // Find the square of the euclidean distance between two points
    252     static int64_t euclidDistSquare(int32_t x0, int32_t y0, int32_t x1, int32_t y1);
    253 
    254     // Create an output stream with surface deferred for future.
    255     binder::Status createDeferredSurfaceStreamLocked(
    256             const hardware::camera2::params::OutputConfiguration &outputConfiguration,
    257             bool isShared,
    258             int* newStreamId = NULL);
    259 
    260     // Set the stream transform flags to automatically rotate the camera stream for preview use
    261     // cases.
    262     binder::Status setStreamTransformLocked(int streamId);
    263 
    264     // Find the closest dimensions for a given format in available stream configurations with
    265     // a width <= ROUNDING_WIDTH_CAP
    266     static const int32_t ROUNDING_WIDTH_CAP = 1920;
    267     static bool roundBufferDimensionNearest(int32_t width, int32_t height, int32_t format,
    268             android_dataspace dataSpace, const CameraMetadata& info,
    269             /*out*/int32_t* outWidth, /*out*/int32_t* outHeight);
    270 
    271     //check if format is not custom format
    272     static bool isPublicFormat(int32_t format);
    273 
    274     // Create a Surface from an IGraphicBufferProducer. Returns error if
    275     // IGraphicBufferProducer's property doesn't match with streamInfo
    276     binder::Status createSurfaceFromGbp(OutputStreamInfo& streamInfo, bool isStreamInfoValid,
    277             sp<Surface>& surface, const sp<IGraphicBufferProducer>& gbp,
    278             const String8& physicalCameraId);
    279 
    280 
    281     // Utility method to insert the surface into SurfaceMap
    282     binder::Status insertGbpLocked(const sp<IGraphicBufferProducer>& gbp,
    283             /*out*/SurfaceMap* surfaceMap, /*out*/Vector<int32_t>* streamIds,
    284             /*out*/int32_t*  currentStreamId);
    285 
    286     // Check that the physicalCameraId passed in is spported by the camera
    287     // device.
    288     bool checkPhysicalCameraId(const String8& physicalCameraId);
    289 
    290     // IGraphicsBufferProducer binder -> Stream ID + Surface ID for output streams
    291     KeyedVector<sp<IBinder>, StreamSurfaceId> mStreamMap;
    292 
    293     // Stream ID -> OutputConfiguration. Used for looking up Surface by stream/surface index
    294     KeyedVector<int32_t, hardware::camera2::params::OutputConfiguration> mConfiguredOutputs;
    295 
    296     struct InputStreamConfiguration {
    297         bool configured;
    298         int32_t width;
    299         int32_t height;
    300         int32_t format;
    301         int32_t id;
    302     } mInputStream;
    303 
    304     // Streaming request ID
    305     int32_t mStreamingRequestId;
    306     Mutex mStreamingRequestIdLock;
    307     static const int32_t REQUEST_ID_NONE = -1;
    308 
    309     int32_t mRequestIdCounter;
    310 
    311     // The list of output streams whose surfaces are deferred. We have to track them separately
    312     // as there are no surfaces available and can not be put into mStreamMap. Once the deferred
    313     // Surface is configured, the stream id will be moved to mStreamMap.
    314     Vector<int32_t> mDeferredStreams;
    315 
    316     // stream ID -> outputStreamInfo mapping
    317     std::unordered_map<int32_t, OutputStreamInfo> mStreamInfoMap;
    318 
    319     KeyedVector<sp<IBinder>, sp<CompositeStream>> mCompositeStreamMap;
    320 
    321     static const int32_t MAX_SURFACES_PER_STREAM = 4;
    322     sp<CameraProviderManager> mProviderManager;
    323 };
    324 
    325 }; // namespace android
    326 
    327 #endif
    328