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_ISURFACETEXTURE_H
18#define ANDROID_GUI_ISURFACETEXTURE_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
32namespace android {
33// ----------------------------------------------------------------------------
34
35class SurfaceTextureClient;
36
37class ISurfaceTexture : public IInterface
38{
39public:
40    DECLARE_META_INTERFACE(SurfaceTexture);
41
42    enum {
43        BUFFER_NEEDS_REALLOCATION = 0x1,
44        RELEASE_ALL_BUFFERS       = 0x2,
45    };
46
47    // requestBuffer requests a new buffer for the given index. The server (i.e.
48    // the ISurfaceTexture implementation) assigns the newly created buffer to
49    // the given slot index, and the client is expected to mirror the
50    // slot->buffer mapping so that it's not necessary to transfer a
51    // GraphicBuffer for every dequeue operation.
52    virtual status_t requestBuffer(int slot, sp<GraphicBuffer>* buf) = 0;
53
54    // setBufferCount sets the number of buffer slots available. Calling this
55    // will also cause all buffer slots to be emptied. The caller should empty
56    // its mirrored copy of the buffer slots when calling this method.
57    virtual status_t setBufferCount(int bufferCount) = 0;
58
59    // dequeueBuffer requests a new buffer slot for the client to use. Ownership
60    // of the slot is transfered to the client, meaning that the server will not
61    // use the contents of the buffer associated with that slot. The slot index
62    // returned may or may not contain a buffer. If the slot is empty the client
63    // should call requestBuffer to assign a new buffer to that slot. The client
64    // is expected to either call cancelBuffer on the dequeued slot or to fill
65    // in the contents of its associated buffer contents and call queueBuffer.
66    // If dequeueBuffer return BUFFER_NEEDS_REALLOCATION, the client is
67    // expected to call requestBuffer immediately.
68    //
69    // The fence parameter will be updated to hold the fence associated with
70    // the buffer. The contents of the buffer must not be overwritten until the
71    // fence signals. If the fence is NULL, the buffer may be written
72    // immediately.
73    virtual status_t dequeueBuffer(int *slot, sp<Fence>& fence,
74            uint32_t w, uint32_t h, uint32_t format, uint32_t usage) = 0;
75
76    // queueBuffer indicates that the client has finished filling in the
77    // contents of the buffer associated with slot and transfers ownership of
78    // that slot back to the server. It is not valid to call queueBuffer on a
79    // slot that is not owned by the client or one for which a buffer associated
80    // via requestBuffer. In addition, a timestamp must be provided by the
81    // client for this buffer. The timestamp is measured in nanoseconds, and
82    // must be monotonically increasing. Its other properties (zero point, etc)
83    // are client-dependent, and should be documented by the client.
84    //
85    // outWidth, outHeight and outTransform are filled with the default width
86    // and height of the window and current transform applied to buffers,
87    // respectively.
88
89    struct QueueBufferInput : public Flattenable {
90        inline QueueBufferInput(const Parcel& parcel);
91        inline QueueBufferInput(int64_t timestamp,
92                const Rect& crop, int scalingMode, uint32_t transform,
93                sp<Fence> fence)
94        : timestamp(timestamp), crop(crop), scalingMode(scalingMode),
95          transform(transform), fence(fence) { }
96        inline void deflate(int64_t* outTimestamp, Rect* outCrop,
97                int* outScalingMode, uint32_t* outTransform,
98                sp<Fence>* outFence) const {
99            *outTimestamp = timestamp;
100            *outCrop = crop;
101            *outScalingMode = scalingMode;
102            *outTransform = transform;
103            *outFence = fence;
104        }
105
106        // Flattenable interface
107        virtual size_t getFlattenedSize() const;
108        virtual size_t getFdCount() const;
109        virtual status_t flatten(void* buffer, size_t size,
110                int fds[], size_t count) const;
111        virtual status_t unflatten(void const* buffer, size_t size,
112                int fds[], size_t count);
113
114    private:
115        int64_t timestamp;
116        Rect crop;
117        int scalingMode;
118        uint32_t transform;
119        sp<Fence> fence;
120    };
121
122    // QueueBufferOutput must be a POD structure
123    struct QueueBufferOutput {
124        inline QueueBufferOutput() { }
125        inline void deflate(uint32_t* outWidth,
126                uint32_t* outHeight,
127                uint32_t* outTransformHint,
128                uint32_t* outNumPendingBuffers) const {
129            *outWidth = width;
130            *outHeight = height;
131            *outTransformHint = transformHint;
132            *outNumPendingBuffers = numPendingBuffers;
133        }
134        inline void inflate(uint32_t inWidth, uint32_t inHeight,
135                uint32_t inTransformHint, uint32_t inNumPendingBuffers) {
136            width = inWidth;
137            height = inHeight;
138            transformHint = inTransformHint;
139            numPendingBuffers = inNumPendingBuffers;
140        }
141    private:
142        uint32_t width;
143        uint32_t height;
144        uint32_t transformHint;
145        uint32_t numPendingBuffers;
146    };
147
148    virtual status_t queueBuffer(int slot,
149            const QueueBufferInput& input, QueueBufferOutput* output) = 0;
150
151    // cancelBuffer indicates that the client does not wish to fill in the
152    // buffer associated with slot and transfers ownership of the slot back to
153    // the server.
154    virtual void cancelBuffer(int slot, sp<Fence> fence) = 0;
155
156    // query retrieves some information for this surface
157    // 'what' tokens allowed are that of android_natives.h
158    virtual int query(int what, int* value) = 0;
159
160    // setSynchronousMode set whether dequeueBuffer is synchronous or
161    // asynchronous. In synchronous mode, dequeueBuffer blocks until
162    // a buffer is available, the currently bound buffer can be dequeued and
163    // queued buffers will be retired in order.
164    // The default mode is asynchronous.
165    virtual status_t setSynchronousMode(bool enabled) = 0;
166
167    // connect attempts to connect a client API to the SurfaceTexture.  This
168    // must be called before any other ISurfaceTexture methods are called except
169    // for getAllocator.
170    //
171    // This method will fail if the connect was previously called on the
172    // SurfaceTexture and no corresponding disconnect call was made.
173    //
174    // outWidth, outHeight and outTransform are filled with the default width
175    // and height of the window and current transform applied to buffers,
176    // respectively.
177    virtual status_t connect(int api, QueueBufferOutput* output) = 0;
178
179    // disconnect attempts to disconnect a client API from the SurfaceTexture.
180    // Calling this method will cause any subsequent calls to other
181    // ISurfaceTexture methods to fail except for getAllocator and connect.
182    // Successfully calling connect after this will allow the other methods to
183    // succeed again.
184    //
185    // This method will fail if the the SurfaceTexture is not currently
186    // connected to the specified client API.
187    virtual status_t disconnect(int api) = 0;
188};
189
190// ----------------------------------------------------------------------------
191
192class BnSurfaceTexture : public BnInterface<ISurfaceTexture>
193{
194public:
195    virtual status_t    onTransact( uint32_t code,
196                                    const Parcel& data,
197                                    Parcel* reply,
198                                    uint32_t flags = 0);
199};
200
201// ----------------------------------------------------------------------------
202}; // namespace android
203
204#endif // ANDROID_GUI_ISURFACETEXTURE_H
205