Home | History | Annotate | Download | only in device3
      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_CAMERA3_OUTPUT_STREAM_H
     18 #define ANDROID_SERVERS_CAMERA3_OUTPUT_STREAM_H
     19 
     20 #include <utils/RefBase.h>
     21 #include <gui/IProducerListener.h>
     22 #include <gui/Surface.h>
     23 
     24 #include "Camera3Stream.h"
     25 #include "Camera3IOStreamBase.h"
     26 #include "Camera3OutputStreamInterface.h"
     27 #include "Camera3BufferManager.h"
     28 
     29 namespace android {
     30 
     31 namespace camera3 {
     32 
     33 class Camera3BufferManager;
     34 
     35 /**
     36  * Stream info structure that holds the necessary stream info for buffer manager to use for
     37  * buffer allocation and management.
     38  */
     39 struct StreamInfo {
     40     int streamId;
     41     int streamSetId;
     42     uint32_t width;
     43     uint32_t height;
     44     uint32_t format;
     45     android_dataspace dataSpace;
     46     uint32_t combinedUsage;
     47     size_t totalBufferCount;
     48     bool isConfigured;
     49     StreamInfo(int id = CAMERA3_STREAM_ID_INVALID,
     50             int setId = CAMERA3_STREAM_SET_ID_INVALID,
     51             uint32_t w = 0,
     52             uint32_t h = 0,
     53             uint32_t fmt = 0,
     54             android_dataspace ds = HAL_DATASPACE_UNKNOWN,
     55             uint32_t usage = 0,
     56             size_t bufferCount = 0,
     57             bool configured = false) :
     58                 streamId(id),
     59                 streamSetId(setId),
     60                 width(w),
     61                 height(h),
     62                 format(fmt),
     63                 dataSpace(ds),
     64                 combinedUsage(usage),
     65                 totalBufferCount(bufferCount),
     66                 isConfigured(configured){}
     67 };
     68 
     69 /**
     70  * A class for managing a single stream of output data from the camera device.
     71  */
     72 class Camera3OutputStream :
     73         public Camera3IOStreamBase,
     74         public Camera3OutputStreamInterface {
     75   public:
     76     /**
     77      * Set up a stream for formats that have 2 dimensions, such as RAW and YUV.
     78      * A valid stream set id needs to be set to support buffer sharing between multiple
     79      * streams.
     80      */
     81     Camera3OutputStream(int id, sp<Surface> consumer,
     82             uint32_t width, uint32_t height, int format,
     83             android_dataspace dataSpace, camera3_stream_rotation_t rotation,
     84             nsecs_t timestampOffset, int setId = CAMERA3_STREAM_SET_ID_INVALID);
     85 
     86     /**
     87      * Set up a stream for formats that have a variable buffer size for the same
     88      * dimensions, such as compressed JPEG.
     89      * A valid stream set id needs to be set to support buffer sharing between multiple
     90      * streams.
     91      */
     92     Camera3OutputStream(int id, sp<Surface> consumer,
     93             uint32_t width, uint32_t height, size_t maxSize, int format,
     94             android_dataspace dataSpace, camera3_stream_rotation_t rotation,
     95             nsecs_t timestampOffset, int setId = CAMERA3_STREAM_SET_ID_INVALID);
     96 
     97     /**
     98      * Set up a stream with deferred consumer for formats that have 2 dimensions, such as
     99      * RAW and YUV. The consumer must be set before using this stream for output. A valid
    100      * stream set id needs to be set to support buffer sharing between multiple streams.
    101      */
    102     Camera3OutputStream(int id, uint32_t width, uint32_t height, int format,
    103             uint32_t consumerUsage, android_dataspace dataSpace,
    104             camera3_stream_rotation_t rotation, nsecs_t timestampOffset,
    105             int setId = CAMERA3_STREAM_SET_ID_INVALID);
    106 
    107     virtual ~Camera3OutputStream();
    108 
    109     /**
    110      * Camera3Stream interface
    111      */
    112 
    113     virtual void     dump(int fd, const Vector<String16> &args) const;
    114 
    115     /**
    116      * Set the transform on the output stream; one of the
    117      * HAL_TRANSFORM_* / NATIVE_WINDOW_TRANSFORM_* constants.
    118      */
    119     status_t         setTransform(int transform);
    120 
    121     /**
    122      * Return if this output stream is for video encoding.
    123      */
    124     bool isVideoStream() const;
    125     /**
    126      * Return if this output stream is consumed by hardware composer.
    127      */
    128     bool isConsumedByHWComposer() const;
    129 
    130     /**
    131      * Return if this output stream is consumed by hardware texture.
    132      */
    133     bool isConsumedByHWTexture() const;
    134 
    135     /**
    136      * Return if the consumer configuration of this stream is deferred.
    137      */
    138     virtual bool isConsumerConfigurationDeferred() const;
    139 
    140     /**
    141      * Set the consumer surface to the output stream.
    142      */
    143     virtual status_t setConsumer(sp<Surface> consumer);
    144 
    145     class BufferReleasedListener : public BnProducerListener {
    146         public:
    147           BufferReleasedListener(wp<Camera3OutputStream> parent) : mParent(parent) {}
    148 
    149           /**
    150           * Implementation of IProducerListener, used to notify this stream that the consumer
    151           * has returned a buffer and it is ready to return to Camera3BufferManager for reuse.
    152           */
    153           virtual void onBufferReleased();
    154 
    155         private:
    156           wp<Camera3OutputStream> mParent;
    157     };
    158 
    159     virtual status_t detachBuffer(sp<GraphicBuffer>* buffer, int* fenceFd);
    160 
    161     /**
    162      * Set the graphic buffer manager to get/return the stream buffers.
    163      *
    164      * It is only legal to call this method when stream is in STATE_CONSTRUCTED state.
    165      */
    166     status_t setBufferManager(sp<Camera3BufferManager> bufferManager);
    167 
    168   protected:
    169     Camera3OutputStream(int id, camera3_stream_type_t type,
    170             uint32_t width, uint32_t height, int format,
    171             android_dataspace dataSpace, camera3_stream_rotation_t rotation,
    172             int setId = CAMERA3_STREAM_SET_ID_INVALID);
    173 
    174     /**
    175      * Note that we release the lock briefly in this function
    176      */
    177     virtual status_t returnBufferCheckedLocked(
    178             const camera3_stream_buffer &buffer,
    179             nsecs_t timestamp,
    180             bool output,
    181             /*out*/
    182             sp<Fence> *releaseFenceOut);
    183 
    184     virtual status_t disconnectLocked();
    185 
    186     sp<Surface> mConsumer;
    187 
    188   private:
    189 
    190     static const nsecs_t       kDequeueBufferTimeout   = 1000000000; // 1 sec
    191 
    192     int               mTransform;
    193 
    194     virtual status_t  setTransformLocked(int transform);
    195 
    196     bool mTraceFirstBuffer;
    197 
    198     // Name of Surface consumer
    199     String8           mConsumerName;
    200 
    201     // Whether consumer assumes MONOTONIC timestamp
    202     bool mUseMonoTimestamp;
    203 
    204     /**
    205      * GraphicBuffer manager this stream is registered to. Used to replace the buffer
    206      * allocation/deallocation role of BufferQueue.
    207      */
    208     sp<Camera3BufferManager> mBufferManager;
    209 
    210     /**
    211      * Buffer released listener, used to notify the buffer manager that a buffer is released
    212      * from consumer side.
    213      */
    214     sp<BufferReleasedListener> mBufferReleasedListener;
    215 
    216     /**
    217      * Flag indicating if the buffer manager is used to allocate the stream buffers
    218      */
    219     bool mUseBufferManager;
    220 
    221     /**
    222      * Timestamp offset for video and hardware composer consumed streams
    223      */
    224     nsecs_t mTimestampOffset;
    225 
    226     /**
    227      * Consumer end point usage flag set by the constructor for the deferred
    228      * consumer case.
    229      */
    230     uint32_t    mConsumerUsage;
    231 
    232     /**
    233      * Internal Camera3Stream interface
    234      */
    235     virtual status_t getBufferLocked(camera3_stream_buffer *buffer);
    236     virtual status_t returnBufferLocked(
    237             const camera3_stream_buffer &buffer,
    238             nsecs_t timestamp);
    239 
    240     virtual status_t configureQueueLocked();
    241 
    242     virtual status_t getEndpointUsage(uint32_t *usage) const;
    243 
    244 }; // class Camera3OutputStream
    245 
    246 } // namespace camera3
    247 
    248 } // namespace android
    249 
    250 #endif
    251