Home | History | Annotate | Download | only in gui
      1 /*
      2  * Copyright (C) 2010 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_GUI_IGRAPHICBUFFERPRODUCER_H
     18 #define ANDROID_GUI_IGRAPHICBUFFERPRODUCER_H
     19 
     20 #include <stdint.h>
     21 #include <sys/types.h>
     22 
     23 #include <utils/Errors.h>
     24 #include <utils/RefBase.h>
     25 
     26 #include <binder/IInterface.h>
     27 
     28 #include <ui/Fence.h>
     29 #include <ui/GraphicBuffer.h>
     30 #include <ui/Rect.h>
     31 
     32 namespace android {
     33 // ----------------------------------------------------------------------------
     34 
     35 class Surface;
     36 
     37 /*
     38  * This class defines the Binder IPC interface for the producer side of
     39  * a queue of graphics buffers.  It's used to send graphics data from one
     40  * component to another.  For example, a class that decodes video for
     41  * playback might use this to provide frames.  This is typically done
     42  * indirectly, through Surface.
     43  *
     44  * The underlying mechanism is a BufferQueue, which implements
     45  * BnGraphicBufferProducer.  In normal operation, the producer calls
     46  * dequeueBuffer() to get an empty buffer, fills it with data, then
     47  * calls queueBuffer() to make it available to the consumer.
     48  *
     49  * This class was previously called ISurfaceTexture.
     50  */
     51 class IGraphicBufferProducer : public IInterface
     52 {
     53 public:
     54     DECLARE_META_INTERFACE(GraphicBufferProducer);
     55 
     56     enum {
     57         BUFFER_NEEDS_REALLOCATION = 0x1,
     58         RELEASE_ALL_BUFFERS       = 0x2,
     59     };
     60 
     61     // requestBuffer requests a new buffer for the given index. The server (i.e.
     62     // the IGraphicBufferProducer implementation) assigns the newly created
     63     // buffer to the given slot index, and the client is expected to mirror the
     64     // slot->buffer mapping so that it's not necessary to transfer a
     65     // GraphicBuffer for every dequeue operation.
     66     virtual status_t requestBuffer(int slot, sp<GraphicBuffer>* buf) = 0;
     67 
     68     // setBufferCount sets the number of buffer slots available. Calling this
     69     // will also cause all buffer slots to be emptied. The caller should empty
     70     // its mirrored copy of the buffer slots when calling this method.
     71     virtual status_t setBufferCount(int bufferCount) = 0;
     72 
     73     // dequeueBuffer requests a new buffer slot for the client to use. Ownership
     74     // of the slot is transfered to the client, meaning that the server will not
     75     // use the contents of the buffer associated with that slot. The slot index
     76     // returned may or may not contain a buffer. If the slot is empty the client
     77     // should call requestBuffer to assign a new buffer to that slot. The client
     78     // is expected to either call cancelBuffer on the dequeued slot or to fill
     79     // in the contents of its associated buffer contents and call queueBuffer.
     80     // If dequeueBuffer return BUFFER_NEEDS_REALLOCATION, the client is
     81     // expected to call requestBuffer immediately.
     82     //
     83     // The fence parameter will be updated to hold the fence associated with
     84     // the buffer. The contents of the buffer must not be overwritten until the
     85     // fence signals. If the fence is NULL, the buffer may be written
     86     // immediately.
     87     //
     88     // The async parameter sets whether we're in asynchrnous mode for this
     89     // deququeBuffer() call.
     90     virtual status_t dequeueBuffer(int *slot, sp<Fence>* fence, bool async,
     91             uint32_t w, uint32_t h, uint32_t format, uint32_t usage) = 0;
     92 
     93     // queueBuffer indicates that the client has finished filling in the
     94     // contents of the buffer associated with slot and transfers ownership of
     95     // that slot back to the server. It is not valid to call queueBuffer on a
     96     // slot that is not owned by the client or one for which a buffer associated
     97     // via requestBuffer. In addition, a timestamp must be provided by the
     98     // client for this buffer. The timestamp is measured in nanoseconds, and
     99     // must be monotonically increasing. Its other properties (zero point, etc)
    100     // are client-dependent, and should be documented by the client.
    101     //
    102     // The async parameter sets whether we're queuing a buffer in asynchronous mode.
    103     //
    104     // outWidth, outHeight and outTransform are filled with the default width
    105     // and height of the window and current transform applied to buffers,
    106     // respectively.
    107 
    108     struct QueueBufferInput : public Flattenable<QueueBufferInput> {
    109         friend class Flattenable<QueueBufferInput>;
    110         inline QueueBufferInput(const Parcel& parcel);
    111         inline QueueBufferInput(int64_t timestamp, bool isAutoTimestamp,
    112                 const Rect& crop, int scalingMode, uint32_t transform, bool async,
    113                 const sp<Fence>& fence)
    114         : timestamp(timestamp), isAutoTimestamp(isAutoTimestamp), crop(crop),
    115           scalingMode(scalingMode), transform(transform), async(async),
    116           fence(fence) { }
    117         inline void deflate(int64_t* outTimestamp, bool* outIsAutoTimestamp,
    118                 Rect* outCrop, int* outScalingMode, uint32_t* outTransform,
    119                 bool* outAsync, sp<Fence>* outFence) const {
    120             *outTimestamp = timestamp;
    121             *outIsAutoTimestamp = bool(isAutoTimestamp);
    122             *outCrop = crop;
    123             *outScalingMode = scalingMode;
    124             *outTransform = transform;
    125             *outAsync = bool(async);
    126             *outFence = fence;
    127         }
    128 
    129         // Flattenable protocol
    130         size_t getFlattenedSize() const;
    131         size_t getFdCount() const;
    132         status_t flatten(void*& buffer, size_t& size, int*& fds, size_t& count) const;
    133         status_t unflatten(void const*& buffer, size_t& size, int const*& fds, size_t& count);
    134 
    135     private:
    136         int64_t timestamp;
    137         int isAutoTimestamp;
    138         Rect crop;
    139         int scalingMode;
    140         uint32_t transform;
    141         int async;
    142         sp<Fence> fence;
    143     };
    144 
    145     // QueueBufferOutput must be a POD structure
    146     struct QueueBufferOutput {
    147         inline QueueBufferOutput() { }
    148         inline void deflate(uint32_t* outWidth,
    149                 uint32_t* outHeight,
    150                 uint32_t* outTransformHint,
    151                 uint32_t* outNumPendingBuffers) const {
    152             *outWidth = width;
    153             *outHeight = height;
    154             *outTransformHint = transformHint;
    155             *outNumPendingBuffers = numPendingBuffers;
    156         }
    157         inline void inflate(uint32_t inWidth, uint32_t inHeight,
    158                 uint32_t inTransformHint, uint32_t inNumPendingBuffers) {
    159             width = inWidth;
    160             height = inHeight;
    161             transformHint = inTransformHint;
    162             numPendingBuffers = inNumPendingBuffers;
    163         }
    164     private:
    165         uint32_t width;
    166         uint32_t height;
    167         uint32_t transformHint;
    168         uint32_t numPendingBuffers;
    169     };
    170 
    171     virtual status_t queueBuffer(int slot,
    172             const QueueBufferInput& input, QueueBufferOutput* output) = 0;
    173 
    174     // cancelBuffer indicates that the client does not wish to fill in the
    175     // buffer associated with slot and transfers ownership of the slot back to
    176     // the server.
    177     virtual void cancelBuffer(int slot, const sp<Fence>& fence) = 0;
    178 
    179     // query retrieves some information for this surface
    180     // 'what' tokens allowed are that of android_natives.h
    181     virtual int query(int what, int* value) = 0;
    182 
    183     // connect attempts to connect a client API to the IGraphicBufferProducer.
    184     // This must be called before any other IGraphicBufferProducer methods are
    185     // called except for getAllocator.
    186     //
    187     // This method will fail if the connect was previously called on the
    188     // IGraphicBufferProducer and no corresponding disconnect call was made.
    189     //
    190     // outWidth, outHeight and outTransform are filled with the default width
    191     // and height of the window and current transform applied to buffers,
    192     // respectively. The token needs to be any binder object that lives in the
    193     // producer process -- it is solely used for obtaining a death notification
    194     // when the producer is killed.
    195     virtual status_t connect(const sp<IBinder>& token,
    196             int api, bool producerControlledByApp, QueueBufferOutput* output) = 0;
    197 
    198     // disconnect attempts to disconnect a client API from the
    199     // IGraphicBufferProducer.  Calling this method will cause any subsequent
    200     // calls to other IGraphicBufferProducer methods to fail except for
    201     // getAllocator and connect.  Successfully calling connect after this will
    202     // allow the other methods to succeed again.
    203     //
    204     // This method will fail if the the IGraphicBufferProducer is not currently
    205     // connected to the specified client API.
    206     virtual status_t disconnect(int api) = 0;
    207 };
    208 
    209 // ----------------------------------------------------------------------------
    210 
    211 class BnGraphicBufferProducer : public BnInterface<IGraphicBufferProducer>
    212 {
    213 public:
    214     virtual status_t    onTransact( uint32_t code,
    215                                     const Parcel& data,
    216                                     Parcel* reply,
    217                                     uint32_t flags = 0);
    218 };
    219 
    220 // ----------------------------------------------------------------------------
    221 }; // namespace android
    222 
    223 #endif // ANDROID_GUI_IGRAPHICBUFFERPRODUCER_H
    224