IGraphicBufferProducer.h 24.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * Copyright (C) 2010 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

17 18
#ifndef ANDROID_GUI_IGRAPHICBUFFERPRODUCER_H
#define ANDROID_GUI_IGRAPHICBUFFERPRODUCER_H
19 20 21 22 23 24 25 26 27

#include <stdint.h>
#include <sys/types.h>

#include <utils/Errors.h>
#include <utils/RefBase.h>

#include <binder/IInterface.h>

28
#include <ui/Fence.h>
29 30
#include <ui/GraphicBuffer.h>
#include <ui/Rect.h>
31
#include <ui/Region.h>
32 33 34 35

namespace android {
// ----------------------------------------------------------------------------

36
class IProducerListener;
37
class NativeHandle;
38
class Surface;
39

Andy McFadden's avatar
Andy McFadden committed
40
/*
Andy McFadden's avatar
Andy McFadden committed
41 42 43 44
 * This class defines the Binder IPC interface for the producer side of
 * a queue of graphics buffers.  It's used to send graphics data from one
 * component to another.  For example, a class that decodes video for
 * playback might use this to provide frames.  This is typically done
45
 * indirectly, through Surface.
Andy McFadden's avatar
Andy McFadden committed
46
 *
Andy McFadden's avatar
Andy McFadden committed
47 48 49 50
 * The underlying mechanism is a BufferQueue, which implements
 * BnGraphicBufferProducer.  In normal operation, the producer calls
 * dequeueBuffer() to get an empty buffer, fills it with data, then
 * calls queueBuffer() to make it available to the consumer.
Andy McFadden's avatar
Andy McFadden committed
51
 *
52
 * This class was previously called ISurfaceTexture.
Andy McFadden's avatar
Andy McFadden committed
53
 */
54
class IGraphicBufferProducer : public IInterface
55 56
{
public:
57
    DECLARE_META_INTERFACE(GraphicBufferProducer);
58

59
    enum {
60 61
        // A flag returned by dequeueBuffer when the client needs to call
        // requestBuffer immediately thereafter.
62
        BUFFER_NEEDS_REALLOCATION = 0x1,
63 64
        // A flag returned by dequeueBuffer when all mirrored slots should be
        // released by the client. This flag should always be processed first.
65 66
        RELEASE_ALL_BUFFERS       = 0x2,
    };
67

68
    // requestBuffer requests a new buffer for the given index. The server (i.e.
69 70
    // the IGraphicBufferProducer implementation) assigns the newly created
    // buffer to the given slot index, and the client is expected to mirror the
71 72
    // slot->buffer mapping so that it's not necessary to transfer a
    // GraphicBuffer for every dequeue operation.
73 74 75 76 77 78 79 80
    //
    // The slot must be in the range of [0, NUM_BUFFER_SLOTS).
    //
    // Return of a value other than NO_ERROR means an error has occurred:
    // * NO_INIT - the buffer queue has been abandoned.
    // * BAD_VALUE - one of the two conditions occurred:
    //              * slot was out of range (see above)
    //              * buffer specified by the slot is not dequeued
81
    virtual status_t requestBuffer(int slot, sp<GraphicBuffer>* buf) = 0;
82 83 84 85

    // setBufferCount sets the number of buffer slots available. Calling this
    // will also cause all buffer slots to be emptied. The caller should empty
    // its mirrored copy of the buffer slots when calling this method.
86 87 88 89
    //
    // This function should not be called when there are any dequeued buffer
    // slots, doing so will result in a BAD_VALUE error returned.
    //
90 91 92 93 94 95 96
    // The buffer count should be at most NUM_BUFFER_SLOTS (inclusive), but at least
    // the minimum undequeued buffer count (exclusive). The minimum value
    // can be obtained by calling query(NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS).
    // In particular the range is (minUndequeudBuffers, NUM_BUFFER_SLOTS].
    //
    // The buffer count may also be set to 0 (the default), to indicate that
    // the producer does not wish to set a value.
97 98 99 100 101 102
    //
    // Return of a value other than NO_ERROR means an error has occurred:
    // * NO_INIT - the buffer queue has been abandoned.
    // * BAD_VALUE - one of the below conditions occurred:
    //              * bufferCount was out of range (see above)
    //              * client has one or more buffers dequeued
103 104 105 106
    virtual status_t setBufferCount(int bufferCount) = 0;

    // dequeueBuffer requests a new buffer slot for the client to use. Ownership
    // of the slot is transfered to the client, meaning that the server will not
107 108 109 110 111 112 113 114 115 116 117 118
    // use the contents of the buffer associated with that slot.
    //
    // The slot index returned may or may not contain a buffer (client-side).
    // If the slot is empty the client should call requestBuffer to assign a new
    // buffer to that slot.
    //
    // Once the client is done filling this buffer, it is expected to transfer
    // buffer ownership back to the server with either cancelBuffer on
    // the dequeued slot or to fill in the contents of its associated buffer
    // contents and call queueBuffer.
    //
    // If dequeueBuffer returns the BUFFER_NEEDS_REALLOCATION flag, the client is
119
    // expected to call requestBuffer immediately.
120
    //
121 122 123
    // If dequeueBuffer returns the RELEASE_ALL_BUFFERS flag, the client is
    // expected to release all of the mirrored slot->buffer mappings.
    //
124 125
    // The fence parameter will be updated to hold the fence associated with
    // the buffer. The contents of the buffer must not be overwritten until the
126
    // fence signals. If the fence is Fence::NO_FENCE, the buffer may be written
127
    // immediately.
128
    //
129 130 131 132 133 134 135 136 137
    // The async parameter sets whether we're in asynchronous mode for this
    // dequeueBuffer() call.
    //
    // The width and height parameters must be no greater than the minimum of
    // GL_MAX_VIEWPORT_DIMS and GL_MAX_TEXTURE_SIZE (see: glGetIntegerv).
    // An error due to invalid dimensions might not be reported until
    // updateTexImage() is called.  If width and height are both zero, the
    // default values specified by setDefaultBufferSize() are used instead.
    //
138
    // If the format is 0, the default format will be used.
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
    //
    // The usage argument specifies gralloc buffer usage flags.  The values
    // are enumerated in <gralloc.h>, e.g. GRALLOC_USAGE_HW_RENDER.  These
    // will be merged with the usage flags specified by
    // IGraphicBufferConsumer::setConsumerUsageBits.
    //
    // This call will block until a buffer is available to be dequeued. If
    // both the producer and consumer are controlled by the app, then this call
    // can never block and will return WOULD_BLOCK if no buffer is available.
    //
    // A non-negative value with flags set (see above) will be returned upon
    // success.
    //
    // Return of a negative means an error has occurred:
    // * NO_INIT - the buffer queue has been abandoned.
154 155 156 157 158 159 160 161
    // * BAD_VALUE - both in async mode and buffer count was less than the
    //               max numbers of buffers that can be allocated at once.
    // * INVALID_OPERATION - cannot attach the buffer because it would cause
    //                       too many buffers to be dequeued, either because
    //                       the producer already has a single buffer dequeued
    //                       and did not set a buffer count, or because a
    //                       buffer count was set and this call would cause
    //                       it to be exceeded.
162 163 164 165 166 167 168
    // * WOULD_BLOCK - no buffer is currently available, and blocking is disabled
    //                 since both the producer/consumer are controlled by app
    // * NO_MEMORY - out of memory, cannot allocate the graphics buffer.
    //
    // All other negative values are an unknown error returned downstream
    // from the graphics allocator (typically errno).
    virtual status_t dequeueBuffer(int* slot, sp<Fence>* fence, bool async,
169
            uint32_t w, uint32_t h, PixelFormat format, uint32_t usage) = 0;
170

171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
    // detachBuffer attempts to remove all ownership of the buffer in the given
    // slot from the buffer queue. If this call succeeds, the slot will be
    // freed, and there will be no way to obtain the buffer from this interface.
    // The freed slot will remain unallocated until either it is selected to
    // hold a freshly allocated buffer in dequeueBuffer or a buffer is attached
    // to the slot. The buffer must have already been dequeued, and the caller
    // must already possesses the sp<GraphicBuffer> (i.e., must have called
    // requestBuffer).
    //
    // Return of a value other than NO_ERROR means an error has occurred:
    // * NO_INIT - the buffer queue has been abandoned.
    // * BAD_VALUE - the given slot number is invalid, either because it is
    //               out of the range [0, NUM_BUFFER_SLOTS), or because the slot
    //               it refers to is not currently dequeued and requested.
    virtual status_t detachBuffer(int slot) = 0;

187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
    // detachNextBuffer is equivalent to calling dequeueBuffer, requestBuffer,
    // and detachBuffer in sequence, except for two things:
    //
    // 1) It is unnecessary to know the dimensions, format, or usage of the
    //    next buffer.
    // 2) It will not block, since if it cannot find an appropriate buffer to
    //    return, it will return an error instead.
    //
    // Only slots that are free but still contain a GraphicBuffer will be
    // considered, and the oldest of those will be returned. outBuffer is
    // equivalent to outBuffer from the requestBuffer call, and outFence is
    // equivalent to fence from the dequeueBuffer call.
    //
    // Return of a value other than NO_ERROR means an error has occurred:
    // * NO_INIT - the buffer queue has been abandoned.
    // * BAD_VALUE - either outBuffer or outFence were NULL.
    // * NO_MEMORY - no slots were found that were both free and contained a
    //               GraphicBuffer.
    virtual status_t detachNextBuffer(sp<GraphicBuffer>* outBuffer,
            sp<Fence>* outFence) = 0;

208 209 210 211 212 213 214 215 216 217 218 219 220
    // attachBuffer attempts to transfer ownership of a buffer to the buffer
    // queue. If this call succeeds, it will be as if this buffer was dequeued
    // from the returned slot number. As such, this call will fail if attaching
    // this buffer would cause too many buffers to be simultaneously dequeued.
    //
    // If attachBuffer returns the RELEASE_ALL_BUFFERS flag, the caller is
    // expected to release all of the mirrored slot->buffer mappings.
    //
    // A non-negative value with flags set (see above) will be returned upon
    // success.
    //
    // Return of a negative value means an error has occurred:
    // * NO_INIT - the buffer queue has been abandoned.
221 222 223
    // * BAD_VALUE - outSlot or buffer were NULL, invalid combination of
    //               async mode and buffer count override, or the generation
    //               number of the buffer did not match the buffer queue.
224 225 226 227 228 229 230 231 232 233 234 235
    // * INVALID_OPERATION - cannot attach the buffer because it would cause
    //                       too many buffers to be dequeued, either because
    //                       the producer already has a single buffer dequeued
    //                       and did not set a buffer count, or because a
    //                       buffer count was set and this call would cause
    //                       it to be exceeded.
    // * WOULD_BLOCK - no buffer slot is currently available, and blocking is
    //                 disabled since both the producer/consumer are
    //                 controlled by the app.
    virtual status_t attachBuffer(int* outSlot,
            const sp<GraphicBuffer>& buffer) = 0;

236 237
    // queueBuffer indicates that the client has finished filling in the
    // contents of the buffer associated with slot and transfers ownership of
238 239 240 241 242 243 244 245
    // that slot back to the server.
    //
    // It is not valid to call queueBuffer on a slot that is not owned
    // by the client or one for which a buffer associated via requestBuffer
    // (an attempt to do so will fail with a return value of BAD_VALUE).
    //
    // In addition, the input must be described by the client (as documented
    // below). Any other properties (zero point, etc)
246
    // are client-dependent, and should be documented by the client.
247
    //
248 249 250 251
    // The slot must be in the range of [0, NUM_BUFFER_SLOTS).
    //
    // Upon success, the output will be filled with meaningful values
    // (refer to the documentation below).
252
    //
253 254 255 256 257 258 259 260 261 262 263
    // Return of a value other than NO_ERROR means an error has occurred:
    // * NO_INIT - the buffer queue has been abandoned.
    // * BAD_VALUE - one of the below conditions occurred:
    //              * fence was NULL
    //              * scaling mode was unknown
    //              * both in async mode and buffer count was less than the
    //                max numbers of buffers that can be allocated at once
    //              * slot index was out of range (see above).
    //              * the slot was not in the dequeued state
    //              * the slot was enqueued without requesting a buffer
    //              * crop rect is out of bounds of the buffer dimensions
264

Mathias Agopian's avatar
Mathias Agopian committed
265 266
    struct QueueBufferInput : public Flattenable<QueueBufferInput> {
        friend class Flattenable<QueueBufferInput>;
267
        inline QueueBufferInput(const Parcel& parcel);
268 269
        // timestamp - a monotonically increasing value in nanoseconds
        // isAutoTimestamp - if the timestamp was synthesized at queue time
270
        // dataSpace - description of the contents, interpretation depends on format
271 272 273 274 275 276
        // crop - a crop rectangle that's used as a hint to the consumer
        // scalingMode - a set of flags from NATIVE_WINDOW_SCALING_* in <window.h>
        // transform - a set of flags from NATIVE_WINDOW_TRANSFORM_* in <window.h>
        // async - if the buffer is queued in asynchronous mode
        // fence - a fence that the consumer must wait on before reading the buffer,
        //         set this to Fence::NO_FENCE if the buffer is ready immediately
277 278
        // sticky - the sticky transform set in Surface (only used by the LEGACY
        //          camera mode).
279
        inline QueueBufferInput(int64_t timestamp, bool isAutoTimestamp,
280 281 282 283 284 285
                android_dataspace dataSpace, const Rect& crop, int scalingMode,
                uint32_t transform, bool async, const sp<Fence>& fence,
                uint32_t sticky = 0)
                : timestamp(timestamp), isAutoTimestamp(isAutoTimestamp),
                  dataSpace(dataSpace), crop(crop), scalingMode(scalingMode),
                  transform(transform), stickyTransform(sticky),
286
                  async(async), fence(fence), surfaceDamage() { }
287
        inline void deflate(int64_t* outTimestamp, bool* outIsAutoTimestamp,
288 289 290
                android_dataspace* outDataSpace,
                Rect* outCrop, int* outScalingMode,
                uint32_t* outTransform, bool* outAsync, sp<Fence>* outFence,
291
                uint32_t* outStickyTransform = NULL) const {
292
            *outTimestamp = timestamp;
293
            *outIsAutoTimestamp = bool(isAutoTimestamp);
294
            *outDataSpace = dataSpace;
295 296 297
            *outCrop = crop;
            *outScalingMode = scalingMode;
            *outTransform = transform;
298
            *outAsync = bool(async);
299
            *outFence = fence;
300 301 302
            if (outStickyTransform != NULL) {
                *outStickyTransform = stickyTransform;
            }
303
        }
304

Mathias Agopian's avatar
Mathias Agopian committed
305 306 307 308 309
        // Flattenable protocol
        size_t getFlattenedSize() const;
        size_t getFdCount() const;
        status_t flatten(void*& buffer, size_t& size, int*& fds, size_t& count) const;
        status_t unflatten(void const*& buffer, size_t& size, int const*& fds, size_t& count);
310

311 312 313
        const Region& getSurfaceDamage() const { return surfaceDamage; }
        void setSurfaceDamage(const Region& damage) { surfaceDamage = damage; }

314 315
    private:
        int64_t timestamp;
316
        int isAutoTimestamp;
317
        android_dataspace dataSpace;
318 319 320
        Rect crop;
        int scalingMode;
        uint32_t transform;
321
        uint32_t stickyTransform;
322
        int async;
323
        sp<Fence> fence;
324
        Region surfaceDamage;
325 326 327
    };

    // QueueBufferOutput must be a POD structure
328
    struct __attribute__ ((__packed__)) QueueBufferOutput {
329
        inline QueueBufferOutput() { }
330 331 332 333 334
        // outWidth - filled with default width applied to the buffer
        // outHeight - filled with default height applied to the buffer
        // outTransformHint - filled with default transform applied to the buffer
        // outNumPendingBuffers - num buffers queued that haven't yet been acquired
        //                        (counting the currently queued buffer)
335
        inline void deflate(uint32_t* outWidth,
336 337 338
                uint32_t* outHeight,
                uint32_t* outTransformHint,
                uint32_t* outNumPendingBuffers) const {
339 340 341
            *outWidth = width;
            *outHeight = height;
            *outTransformHint = transformHint;
342
            *outNumPendingBuffers = numPendingBuffers;
343 344
        }
        inline void inflate(uint32_t inWidth, uint32_t inHeight,
345
                uint32_t inTransformHint, uint32_t inNumPendingBuffers) {
346 347 348
            width = inWidth;
            height = inHeight;
            transformHint = inTransformHint;
349
            numPendingBuffers = inNumPendingBuffers;
350 351 352 353 354
        }
    private:
        uint32_t width;
        uint32_t height;
        uint32_t transformHint;
355
        uint32_t numPendingBuffers;
356 357 358 359
    };

    virtual status_t queueBuffer(int slot,
            const QueueBufferInput& input, QueueBufferOutput* output) = 0;
360 361 362 363

    // cancelBuffer indicates that the client does not wish to fill in the
    // buffer associated with slot and transfers ownership of the slot back to
    // the server.
364 365 366 367 368
    //
    // The buffer is not queued for use by the consumer.
    //
    // The buffer will not be overwritten until the fence signals.  The fence
    // will usually be the one obtained from dequeueBuffer.
369
    virtual void cancelBuffer(int slot, const sp<Fence>& fence) = 0;
370

371
    // query retrieves some information for this surface
372 373 374 375 376
    // 'what' tokens allowed are that of NATIVE_WINDOW_* in <window.h>
    //
    // Return of a value other than NO_ERROR means an error has occurred:
    // * NO_INIT - the buffer queue has been abandoned.
    // * BAD_VALUE - what was out of range
377
    virtual int query(int what, int* value) = 0;
378

379 380
    // connect attempts to connect a client API to the IGraphicBufferProducer.
    // This must be called before any other IGraphicBufferProducer methods are
381
    // called except for getAllocator. A consumer must be already connected.
382 383
    //
    // This method will fail if the connect was previously called on the
384
    // IGraphicBufferProducer and no corresponding disconnect call was made.
385
    //
386 387 388 389 390
    // The listener is an optional binder callback object that can be used if
    // the producer wants to be notified when the consumer releases a buffer
    // back to the BufferQueue. It is also used to detect the death of the
    // producer. If only the latter functionality is desired, there is a
    // DummyProducerListener class in IProducerListener.h that can be used.
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
    //
    // The api should be one of the NATIVE_WINDOW_API_* values in <window.h>
    //
    // The producerControlledByApp should be set to true if the producer is hosted
    // by an untrusted process (typically app_process-forked processes). If both
    // the producer and the consumer are app-controlled then all buffer queues
    // will operate in async mode regardless of the async flag.
    //
    // Upon success, the output will be filled with meaningful data
    // (refer to QueueBufferOutput documentation above).
    //
    // Return of a value other than NO_ERROR means an error has occurred:
    // * NO_INIT - one of the following occurred:
    //             * the buffer queue was abandoned
    //             * no consumer has yet connected
    // * BAD_VALUE - one of the following has occurred:
    //             * the producer is already connected
    //             * api was out of range (see above).
409
    //             * output was NULL.
410 411 412 413
    // * DEAD_OBJECT - the token is hosted by an already-dead process
    //
    // Additional negative errors may be returned by the internals, they
    // should be treated as opaque fatal unrecoverable errors.
414
    virtual status_t connect(const sp<IProducerListener>& listener,
415
            int api, bool producerControlledByApp, QueueBufferOutput* output) = 0;
416

417 418 419 420 421
    // disconnect attempts to disconnect a client API from the
    // IGraphicBufferProducer.  Calling this method will cause any subsequent
    // calls to other IGraphicBufferProducer methods to fail except for
    // getAllocator and connect.  Successfully calling connect after this will
    // allow the other methods to succeed again.
422
    //
423
    // This method will fail if the the IGraphicBufferProducer is not currently
424
    // connected to the specified client API.
425 426 427 428 429 430 431 432 433 434 435
    //
    // The api should be one of the NATIVE_WINDOW_API_* values in <window.h>
    //
    // Disconnecting from an abandoned IGraphicBufferProducer is legal and
    // is considered a no-op.
    //
    // Return of a value other than NO_ERROR means an error has occurred:
    // * BAD_VALUE - one of the following has occurred:
    //             * the api specified does not match the one that was connected
    //             * api was out of range (see above).
    // * DEAD_OBJECT - the token is hosted by an already-dead process
436
    virtual status_t disconnect(int api) = 0;
437 438 439 440 441 442 443 444 445 446 447 448

    // Attaches a sideband buffer stream to the IGraphicBufferProducer.
    //
    // A sideband stream is a device-specific mechanism for passing buffers
    // from the producer to the consumer without using dequeueBuffer/
    // queueBuffer. If a sideband stream is present, the consumer can choose
    // whether to acquire buffers from the sideband stream or from the queued
    // buffers.
    //
    // Passing NULL or a different stream handle will detach the previous
    // handle if any.
    virtual status_t setSidebandStream(const sp<NativeHandle>& stream) = 0;
449 450 451 452 453 454 455 456 457 458 459 460

    // Allocates buffers based on the given dimensions/format.
    //
    // This function will allocate up to the maximum number of buffers
    // permitted by the current BufferQueue configuration. It will use the
    // given format, dimensions, and usage bits, which are interpreted in the
    // same way as for dequeueBuffer, and the async flag must be set the same
    // way as for dequeueBuffer to ensure that the correct number of buffers are
    // allocated. This is most useful to avoid an allocation delay during
    // dequeueBuffer. If there are already the maximum number of buffers
    // allocated, this function has no effect.
    virtual void allocateBuffers(bool async, uint32_t width, uint32_t height,
461
            PixelFormat format, uint32_t usage) = 0;
462 463 464 465 466 467 468 469 470 471 472 473

    // Sets whether dequeueBuffer is allowed to allocate new buffers.
    //
    // Normally dequeueBuffer does not discriminate between free slots which
    // already have an allocated buffer and those which do not, and will
    // allocate a new buffer if the slot doesn't have a buffer or if the slot's
    // buffer doesn't match the requested size, format, or usage. This method
    // allows the producer to restrict the eligible slots to those which already
    // have an allocated buffer of the correct size, format, and usage. If no
    // eligible slot is available, dequeueBuffer will block or return an error
    // as usual.
    virtual status_t allowAllocation(bool allow) = 0;
474 475 476 477 478 479 480 481 482

    // Sets the current generation number of the BufferQueue.
    //
    // This generation number will be inserted into any buffers allocated by the
    // BufferQueue, and any attempts to attach a buffer with a different
    // generation number will fail. Buffers already in the queue are not
    // affected and will retain their current generation number. The generation
    // number defaults to 0.
    virtual status_t setGenerationNumber(uint32_t generationNumber) = 0;
483 484 485 486
};

// ----------------------------------------------------------------------------

487
class BnGraphicBufferProducer : public BnInterface<IGraphicBufferProducer>
488 489 490 491 492 493 494 495 496 497 498
{
public:
    virtual status_t    onTransact( uint32_t code,
                                    const Parcel& data,
                                    Parcel* reply,
                                    uint32_t flags = 0);
};

// ----------------------------------------------------------------------------
}; // namespace android

499
#endif // ANDROID_GUI_IGRAPHICBUFFERPRODUCER_H