Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 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 | |
Andy McFadden | 2adaf04 | 2012-12-18 09:49:45 -0800 | [diff] [blame^] | 17 | #ifndef ANDROID_GUI_IGRAPHICBUFFERPRODUCER_H |
| 18 | #define ANDROID_GUI_IGRAPHICBUFFERPRODUCER_H |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 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 | |
Jesse Hall | f785754 | 2012-06-14 15:26:33 -0700 | [diff] [blame] | 28 | #include <ui/Fence.h> |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 29 | #include <ui/GraphicBuffer.h> |
| 30 | #include <ui/Rect.h> |
| 31 | |
| 32 | namespace android { |
| 33 | // ---------------------------------------------------------------------------- |
| 34 | |
Mathias Agopian | eafabcd | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 35 | class SurfaceTextureClient; |
| 36 | |
Andy McFadden | 0273adb | 2012-12-12 17:10:08 -0800 | [diff] [blame] | 37 | /* |
| 38 | * This class defines an interface that is implemented by classes that |
| 39 | * produce buffers of graphics data. For example, a class that decodes |
| 40 | * video for playback might use this to provide frames. This is |
| 41 | * typically done indirectly, through SurfaceTextureClient. |
| 42 | * |
| 43 | * The underlying mechanism is a BufferQueue. In normal operation, the |
| 44 | * producer calls dequeueBuffer() to get an empty buffer, fills it with |
| 45 | * data, then calls queueBuffer() to make it available to the consumer. |
| 46 | * |
Andy McFadden | 2adaf04 | 2012-12-18 09:49:45 -0800 | [diff] [blame^] | 47 | * The BnGraphicBufferProducer and BpGraphicBufferProducer classes provide |
| 48 | * the Binder IPC implementation. |
Andy McFadden | 0273adb | 2012-12-12 17:10:08 -0800 | [diff] [blame] | 49 | * |
Andy McFadden | 2adaf04 | 2012-12-18 09:49:45 -0800 | [diff] [blame^] | 50 | * This class was previously called ISurfaceTexture. |
Andy McFadden | 0273adb | 2012-12-12 17:10:08 -0800 | [diff] [blame] | 51 | */ |
Andy McFadden | 2adaf04 | 2012-12-18 09:49:45 -0800 | [diff] [blame^] | 52 | class IGraphicBufferProducer : public IInterface |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 53 | { |
| 54 | public: |
Andy McFadden | 2adaf04 | 2012-12-18 09:49:45 -0800 | [diff] [blame^] | 55 | DECLARE_META_INTERFACE(GraphicBufferProducer); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 56 | |
Mathias Agopian | 8072711 | 2011-05-02 19:51:12 -0700 | [diff] [blame] | 57 | enum { |
| 58 | BUFFER_NEEDS_REALLOCATION = 0x1, |
| 59 | RELEASE_ALL_BUFFERS = 0x2, |
| 60 | }; |
Mathias Agopian | a5c75c0 | 2011-03-31 19:10:24 -0700 | [diff] [blame] | 61 | |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 62 | // requestBuffer requests a new buffer for the given index. The server (i.e. |
Andy McFadden | 2adaf04 | 2012-12-18 09:49:45 -0800 | [diff] [blame^] | 63 | // the IGraphicBufferProducer implementation) assigns the newly created |
| 64 | // buffer to the given slot index, and the client is expected to mirror the |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 65 | // slot->buffer mapping so that it's not necessary to transfer a |
| 66 | // GraphicBuffer for every dequeue operation. |
Jamie Gennis | 7b305ff | 2011-07-19 12:08:33 -0700 | [diff] [blame] | 67 | virtual status_t requestBuffer(int slot, sp<GraphicBuffer>* buf) = 0; |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 68 | |
| 69 | // setBufferCount sets the number of buffer slots available. Calling this |
| 70 | // will also cause all buffer slots to be emptied. The caller should empty |
| 71 | // its mirrored copy of the buffer slots when calling this method. |
| 72 | virtual status_t setBufferCount(int bufferCount) = 0; |
| 73 | |
| 74 | // dequeueBuffer requests a new buffer slot for the client to use. Ownership |
| 75 | // of the slot is transfered to the client, meaning that the server will not |
| 76 | // use the contents of the buffer associated with that slot. The slot index |
| 77 | // returned may or may not contain a buffer. If the slot is empty the client |
| 78 | // should call requestBuffer to assign a new buffer to that slot. The client |
| 79 | // is expected to either call cancelBuffer on the dequeued slot or to fill |
| 80 | // in the contents of its associated buffer contents and call queueBuffer. |
Mathias Agopian | a5c75c0 | 2011-03-31 19:10:24 -0700 | [diff] [blame] | 81 | // If dequeueBuffer return BUFFER_NEEDS_REALLOCATION, the client is |
| 82 | // expected to call requestBuffer immediately. |
Jesse Hall | f785754 | 2012-06-14 15:26:33 -0700 | [diff] [blame] | 83 | // |
| 84 | // The fence parameter will be updated to hold the fence associated with |
| 85 | // the buffer. The contents of the buffer must not be overwritten until the |
| 86 | // fence signals. If the fence is NULL, the buffer may be written |
| 87 | // immediately. |
| 88 | virtual status_t dequeueBuffer(int *slot, sp<Fence>& fence, |
| 89 | uint32_t w, uint32_t h, uint32_t format, uint32_t usage) = 0; |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 90 | |
| 91 | // queueBuffer indicates that the client has finished filling in the |
| 92 | // contents of the buffer associated with slot and transfers ownership of |
| 93 | // that slot back to the server. It is not valid to call queueBuffer on a |
| 94 | // slot that is not owned by the client or one for which a buffer associated |
Eino-Ville Talvala | 1d01a12 | 2011-02-18 11:02:42 -0800 | [diff] [blame] | 95 | // via requestBuffer. In addition, a timestamp must be provided by the |
| 96 | // client for this buffer. The timestamp is measured in nanoseconds, and |
| 97 | // must be monotonically increasing. Its other properties (zero point, etc) |
| 98 | // are client-dependent, and should be documented by the client. |
Mathias Agopian | 97c602c | 2011-07-19 15:24:46 -0700 | [diff] [blame] | 99 | // |
Mathias Agopian | c10d9d9 | 2011-07-20 16:46:11 -0700 | [diff] [blame] | 100 | // outWidth, outHeight and outTransform are filled with the default width |
| 101 | // and height of the window and current transform applied to buffers, |
Mathias Agopian | 97c602c | 2011-07-19 15:24:46 -0700 | [diff] [blame] | 102 | // respectively. |
Mathias Agopian | f0bc2f1 | 2012-04-09 16:14:01 -0700 | [diff] [blame] | 103 | |
Jesse Hall | c777b0b | 2012-06-28 12:52:05 -0700 | [diff] [blame] | 104 | struct QueueBufferInput : public Flattenable { |
| 105 | inline QueueBufferInput(const Parcel& parcel); |
Mathias Agopian | f0bc2f1 | 2012-04-09 16:14:01 -0700 | [diff] [blame] | 106 | inline QueueBufferInput(int64_t timestamp, |
Jesse Hall | c777b0b | 2012-06-28 12:52:05 -0700 | [diff] [blame] | 107 | const Rect& crop, int scalingMode, uint32_t transform, |
| 108 | sp<Fence> fence) |
Mathias Agopian | f0bc2f1 | 2012-04-09 16:14:01 -0700 | [diff] [blame] | 109 | : timestamp(timestamp), crop(crop), scalingMode(scalingMode), |
Jesse Hall | c777b0b | 2012-06-28 12:52:05 -0700 | [diff] [blame] | 110 | transform(transform), fence(fence) { } |
Mathias Agopian | f0bc2f1 | 2012-04-09 16:14:01 -0700 | [diff] [blame] | 111 | inline void deflate(int64_t* outTimestamp, Rect* outCrop, |
Jesse Hall | c777b0b | 2012-06-28 12:52:05 -0700 | [diff] [blame] | 112 | int* outScalingMode, uint32_t* outTransform, |
| 113 | sp<Fence>* outFence) const { |
Mathias Agopian | f0bc2f1 | 2012-04-09 16:14:01 -0700 | [diff] [blame] | 114 | *outTimestamp = timestamp; |
| 115 | *outCrop = crop; |
| 116 | *outScalingMode = scalingMode; |
| 117 | *outTransform = transform; |
Jesse Hall | c777b0b | 2012-06-28 12:52:05 -0700 | [diff] [blame] | 118 | *outFence = fence; |
Mathias Agopian | f0bc2f1 | 2012-04-09 16:14:01 -0700 | [diff] [blame] | 119 | } |
Jesse Hall | c777b0b | 2012-06-28 12:52:05 -0700 | [diff] [blame] | 120 | |
| 121 | // Flattenable interface |
| 122 | virtual size_t getFlattenedSize() const; |
| 123 | virtual size_t getFdCount() const; |
| 124 | virtual status_t flatten(void* buffer, size_t size, |
| 125 | int fds[], size_t count) const; |
| 126 | virtual status_t unflatten(void const* buffer, size_t size, |
| 127 | int fds[], size_t count); |
| 128 | |
Mathias Agopian | f0bc2f1 | 2012-04-09 16:14:01 -0700 | [diff] [blame] | 129 | private: |
| 130 | int64_t timestamp; |
| 131 | Rect crop; |
| 132 | int scalingMode; |
| 133 | uint32_t transform; |
Jesse Hall | c777b0b | 2012-06-28 12:52:05 -0700 | [diff] [blame] | 134 | sp<Fence> fence; |
Mathias Agopian | f0bc2f1 | 2012-04-09 16:14:01 -0700 | [diff] [blame] | 135 | }; |
| 136 | |
| 137 | // QueueBufferOutput must be a POD structure |
| 138 | struct QueueBufferOutput { |
| 139 | inline QueueBufferOutput() { } |
| 140 | inline void deflate(uint32_t* outWidth, |
Mathias Agopian | 2488b20 | 2012-04-20 17:19:28 -0700 | [diff] [blame] | 141 | uint32_t* outHeight, |
| 142 | uint32_t* outTransformHint, |
| 143 | uint32_t* outNumPendingBuffers) const { |
Mathias Agopian | f0bc2f1 | 2012-04-09 16:14:01 -0700 | [diff] [blame] | 144 | *outWidth = width; |
| 145 | *outHeight = height; |
| 146 | *outTransformHint = transformHint; |
Mathias Agopian | 2488b20 | 2012-04-20 17:19:28 -0700 | [diff] [blame] | 147 | *outNumPendingBuffers = numPendingBuffers; |
Mathias Agopian | f0bc2f1 | 2012-04-09 16:14:01 -0700 | [diff] [blame] | 148 | } |
| 149 | inline void inflate(uint32_t inWidth, uint32_t inHeight, |
Mathias Agopian | 2488b20 | 2012-04-20 17:19:28 -0700 | [diff] [blame] | 150 | uint32_t inTransformHint, uint32_t inNumPendingBuffers) { |
Mathias Agopian | f0bc2f1 | 2012-04-09 16:14:01 -0700 | [diff] [blame] | 151 | width = inWidth; |
| 152 | height = inHeight; |
| 153 | transformHint = inTransformHint; |
Mathias Agopian | 2488b20 | 2012-04-20 17:19:28 -0700 | [diff] [blame] | 154 | numPendingBuffers = inNumPendingBuffers; |
Mathias Agopian | f0bc2f1 | 2012-04-09 16:14:01 -0700 | [diff] [blame] | 155 | } |
| 156 | private: |
| 157 | uint32_t width; |
| 158 | uint32_t height; |
| 159 | uint32_t transformHint; |
Mathias Agopian | 2488b20 | 2012-04-20 17:19:28 -0700 | [diff] [blame] | 160 | uint32_t numPendingBuffers; |
Mathias Agopian | f0bc2f1 | 2012-04-09 16:14:01 -0700 | [diff] [blame] | 161 | }; |
| 162 | |
| 163 | virtual status_t queueBuffer(int slot, |
| 164 | const QueueBufferInput& input, QueueBufferOutput* output) = 0; |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 165 | |
| 166 | // cancelBuffer indicates that the client does not wish to fill in the |
| 167 | // buffer associated with slot and transfers ownership of the slot back to |
| 168 | // the server. |
Jesse Hall | c777b0b | 2012-06-28 12:52:05 -0700 | [diff] [blame] | 169 | virtual void cancelBuffer(int slot, sp<Fence> fence) = 0; |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 170 | |
Mathias Agopian | eafabcd | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 171 | // query retrieves some information for this surface |
| 172 | // 'what' tokens allowed are that of android_natives.h |
| 173 | virtual int query(int what, int* value) = 0; |
Mathias Agopian | 8072711 | 2011-05-02 19:51:12 -0700 | [diff] [blame] | 174 | |
| 175 | // setSynchronousMode set whether dequeueBuffer is synchronous or |
| 176 | // asynchronous. In synchronous mode, dequeueBuffer blocks until |
| 177 | // a buffer is available, the currently bound buffer can be dequeued and |
| 178 | // queued buffers will be retired in order. |
| 179 | // The default mode is asynchronous. |
| 180 | virtual status_t setSynchronousMode(bool enabled) = 0; |
Jamie Gennis | fe0a87b | 2011-07-13 19:12:20 -0700 | [diff] [blame] | 181 | |
Andy McFadden | 2adaf04 | 2012-12-18 09:49:45 -0800 | [diff] [blame^] | 182 | // connect attempts to connect a client API to the IGraphicBufferProducer. |
| 183 | // This must be called before any other IGraphicBufferProducer methods are |
| 184 | // called except for getAllocator. |
Jamie Gennis | fe0a87b | 2011-07-13 19:12:20 -0700 | [diff] [blame] | 185 | // |
| 186 | // This method will fail if the connect was previously called on the |
Andy McFadden | 2adaf04 | 2012-12-18 09:49:45 -0800 | [diff] [blame^] | 187 | // IGraphicBufferProducer and no corresponding disconnect call was made. |
Mathias Agopian | 5bfc245 | 2011-08-08 19:14:03 -0700 | [diff] [blame] | 188 | // |
| 189 | // outWidth, outHeight and outTransform are filled with the default width |
| 190 | // and height of the window and current transform applied to buffers, |
| 191 | // respectively. |
Mathias Agopian | 24202f5 | 2012-04-23 14:28:58 -0700 | [diff] [blame] | 192 | virtual status_t connect(int api, QueueBufferOutput* output) = 0; |
Jamie Gennis | fe0a87b | 2011-07-13 19:12:20 -0700 | [diff] [blame] | 193 | |
Andy McFadden | 2adaf04 | 2012-12-18 09:49:45 -0800 | [diff] [blame^] | 194 | // disconnect attempts to disconnect a client API from the |
| 195 | // IGraphicBufferProducer. Calling this method will cause any subsequent |
| 196 | // calls to other IGraphicBufferProducer methods to fail except for |
| 197 | // getAllocator and connect. Successfully calling connect after this will |
| 198 | // allow the other methods to succeed again. |
Jamie Gennis | fe0a87b | 2011-07-13 19:12:20 -0700 | [diff] [blame] | 199 | // |
Andy McFadden | 2adaf04 | 2012-12-18 09:49:45 -0800 | [diff] [blame^] | 200 | // This method will fail if the the IGraphicBufferProducer is not currently |
Jamie Gennis | fe0a87b | 2011-07-13 19:12:20 -0700 | [diff] [blame] | 201 | // connected to the specified client API. |
| 202 | virtual status_t disconnect(int api) = 0; |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 203 | }; |
| 204 | |
| 205 | // ---------------------------------------------------------------------------- |
| 206 | |
Andy McFadden | 2adaf04 | 2012-12-18 09:49:45 -0800 | [diff] [blame^] | 207 | class BnGraphicBufferProducer : public BnInterface<IGraphicBufferProducer> |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 208 | { |
| 209 | public: |
| 210 | virtual status_t onTransact( uint32_t code, |
| 211 | const Parcel& data, |
| 212 | Parcel* reply, |
| 213 | uint32_t flags = 0); |
| 214 | }; |
| 215 | |
| 216 | // ---------------------------------------------------------------------------- |
| 217 | }; // namespace android |
| 218 | |
Andy McFadden | 2adaf04 | 2012-12-18 09:49:45 -0800 | [diff] [blame^] | 219 | #endif // ANDROID_GUI_IGRAPHICBUFFERPRODUCER_H |