Home | History | Annotate | Download | only in camera
      1 /*
      2  * Copyright (C) 2011 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 HW_EMULATOR_CAMERA_PREVIEW_WINDOW_H
     18 #define HW_EMULATOR_CAMERA_PREVIEW_WINDOW_H
     19 
     20 #include <hardware/camera.h>
     21 #include <utils/Errors.h>
     22 #include <utils/Mutex.h>
     23 #include <utils/Timers.h>
     24 
     25 /*
     26  * Contains declaration of a class PreviewWindow that encapsulates functionality
     27  * of a preview window set via set_preview_window camera HAL API.
     28  */
     29 
     30 namespace android {
     31 
     32 class EmulatedCameraDevice;
     33 
     34 /* Encapsulates functionality of a preview window set via set_preview_window
     35  * camera HAL API.
     36  *
     37  * Objects of this class are contained in EmulatedCamera objects, and handle
     38  * relevant camera API callbacks.
     39  */
     40 class PreviewWindow {
     41  public:
     42   /* Constructs PreviewWindow instance. */
     43   PreviewWindow();
     44 
     45   /* Destructs PreviewWindow instance. */
     46   ~PreviewWindow();
     47 
     48   /***************************************************************************
     49    * Camera API
     50    **************************************************************************/
     51 
     52  public:
     53   /* Actual handler for camera_device_ops_t::set_preview_window callback.
     54    * This method is called by the containing emulated camera object when it is
     55    * handing the camera_device_ops_t::set_preview_window callback.
     56    * Param:
     57    *  window - Preview window to set. This parameter might be NULL, which
     58    *      indicates preview window reset.
     59    *  preview_fps - Preview's frame frequency. This parameter determins when
     60    *      a frame received via onNextFrameAvailable call will be pushed to
     61    *      the preview window. If 'window' parameter passed to this method is
     62    *      NULL, this parameter is ignored.
     63    * Return:
     64    *  NO_ERROR on success, or an appropriate error status.
     65    */
     66   status_t setPreviewWindow(struct preview_stream_ops* window, int preview_fps);
     67 
     68   /* Starts the preview.
     69    * This method is called by the containing emulated camera object when it is
     70    * handing the camera_device_ops_t::start_preview callback.
     71    */
     72   status_t startPreview();
     73 
     74   /* Stops the preview.
     75    * This method is called by the containing emulated camera object when it is
     76    * handing the camera_device_ops_t::start_preview callback.
     77    */
     78   void stopPreview();
     79 
     80   /* Checks if preview is enabled. */
     81   inline bool isPreviewEnabled() { return mPreviewEnabled; }
     82 
     83   /****************************************************************************
     84    * Public API
     85    ***************************************************************************/
     86 
     87  public:
     88   /* Next frame is available in the camera device.
     89    * This is a notification callback that is invoked by the camera device when
     90    * a new frame is available.
     91    * Note that most likely this method is called in context of a worker thread
     92    * that camera device has created for frame capturing.
     93    * Param:
     94    *  frame - Captured frame, or NULL if camera device didn't pull the frame
     95    *      yet. If NULL is passed in this parameter use GetCurrentFrame method
     96    *      of the camera device class to obtain the next frame. Also note that
     97    *      the size of the frame that is passed here (as well as the frame
     98    *      returned from the GetCurrentFrame method) is defined by the current
     99    *      frame settings (width + height + pixel format) for the camera device.
    100    * timestamp - Frame's timestamp.
    101    * camera_dev - Camera device instance that delivered the frame.
    102    */
    103   void onNextFrameAvailable(const void* frame, nsecs_t timestamp,
    104                             EmulatedCameraDevice* camera_dev);
    105 
    106   /***************************************************************************
    107    * Private API
    108    **************************************************************************/
    109 
    110  protected:
    111   /* Adjusts cached dimensions of the preview window frame according to the
    112    * frame dimensions used by the camera device.
    113    *
    114    * When preview is started, it's not known (hard to define) what are going
    115    * to be the dimensions of the frames that are going to be displayed. Plus,
    116    * it might be possible, that such dimensions can be changed on the fly. So,
    117    * in order to be always in sync with frame dimensions, this method is
    118    * called for each frame passed to onNextFrameAvailable method, in order to
    119    * properly adjust frame dimensions, used by the preview window.
    120    * Note that this method must be called while object is locked.
    121    * Param:
    122    *  camera_dev - Camera device, prpviding frames displayed in the preview
    123    *      window.
    124    * Return:
    125    *  true if cached dimensions have been adjusted, or false if cached
    126    *  dimensions match device's frame dimensions.
    127    */
    128   bool adjustPreviewDimensions(EmulatedCameraDevice* camera_dev);
    129 
    130   /* Checks if it's the time to push new frame to the preview window.
    131    * Note that this method must be called while object is locked. */
    132   bool isPreviewTime();
    133 
    134   /***************************************************************************
    135    * Data members
    136    **************************************************************************/
    137 
    138  protected:
    139   /* Locks this instance for data changes. */
    140   Mutex mObjectLock;
    141 
    142   /* Preview window instance. */
    143   preview_stream_ops* mPreviewWindow;
    144 
    145   /* Timestamp (abs. microseconds) when last frame has been pushed to the
    146    * preview window. */
    147   uint64_t mLastPreviewed;
    148 
    149   /* Preview frequency in microseconds. */
    150   uint32_t mPreviewAfter;
    151 
    152   /*
    153    * Cached preview window frame dimensions.
    154    */
    155 
    156   int mPreviewFrameWidth;
    157   int mPreviewFrameHeight;
    158 
    159   /* Preview status. */
    160   bool mPreviewEnabled;
    161 };
    162 
    163 }; /* namespace android */
    164 
    165 #endif /* HW_EMULATOR_CAMERA_PREVIEW_WINDOW_H */
    166