Surface.h revision 832b6b94684cbd8bf0fcf00bd431ee5e8de03ba7
1f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan/*
2f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan * Copyright (C) 2010 The Android Open Source Project
3f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan *
4f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan * Licensed under the Apache License, Version 2.0 (the "License");
5f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan * you may not use this file except in compliance with the License.
6f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan * You may obtain a copy of the License at
7f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan *
8f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan *      http://www.apache.org/licenses/LICENSE-2.0
9f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan *
10f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan * Unless required by applicable law or agreed to in writing, software
11f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan * distributed under the License is distributed on an "AS IS" BASIS,
12f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan * See the License for the specific language governing permissions and
14f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan * limitations under the License.
15f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan */
16f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan
17f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan#ifndef ANDROID_GUI_SURFACE_H
18f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan#define ANDROID_GUI_SURFACE_H
19f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan
20f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan#include <gui/IGraphicBufferProducer.h>
21f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan#include <gui/BufferQueueDefs.h>
22f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan
23f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan#include <ui/ANativeObjectBase.h>
24f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan#include <ui/Region.h>
25f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan
26f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan#include <utils/Condition.h>
27f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan#include <utils/Mutex.h>
28f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan#include <utils/RefBase.h>
29f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan
30f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Panstruct ANativeWindow_Buffer;
31f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan
32f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pannamespace android {
33f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan
34f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Panclass ISurfaceComposer;
35f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan
365213be94c3b27fff928f5f221e5951c7ede1058bRandy Pan/*
37f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan * An implementation of ANativeWindow that feeds graphics buffers into a
38f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan * BufferQueue.
39f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan *
40c22ad7f19888f7967edcf30080543bb632ddd68dRandy Pan * This is typically used by programs that want to render frames through
41f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan * some means (maybe OpenGL, a software renderer, or a hardware decoder)
42f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan * and have the frames they create forwarded to SurfaceFlinger for
43f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan * compositing.  For example, a video decoder could render a frame and call
44f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan * eglSwapBuffers(), which invokes ANativeWindow callbacks defined by
45f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan * Surface.  Surface then forwards the buffers through Binder IPC
46f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan * to the BufferQueue's producer interface, providing the new frame to a
47f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan * consumer such as GLConsumer.
48f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan */
4913fb8e99c1a793c42e102e4f16f63de02f6eeed3Randy Panclass Surface
50f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan    : public ANativeObjectBase<ANativeWindow, Surface, RefBase>
51e4b4b229331da3964671606f18557b2e7f681b45Amin Shaikh{
522cf3661d146e2bdd0d761028e91df775c8eabca3Amin Shaikhpublic:
53f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan
54f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan    /*
55f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan     * creates a Surface from the given IGraphicBufferProducer (which concrete
56c22ad7f19888f7967edcf30080543bb632ddd68dRandy Pan     * implementation is a BufferQueue).
57f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan     *
58f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan     * Surface is mainly state-less while it's disconnected, it can be
59f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan     * viewed as a glorified IGraphicBufferProducer holder. It's therefore
60f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan     * safe to create other Surfaces from the same IGraphicBufferProducer.
61f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan     *
62f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan     * However, once a Surface is connected, it'll prevent other Surfaces
63f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan     * referring to the same IGraphicBufferProducer to become connected and
64f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan     * therefore prevent them to be used as actual producers of buffers.
65f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan     *
66f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan     * the controlledByApp flag indicates that this Surface (producer) is
67f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan     * controlled by the application. This flag is used at connect time.
68f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan     */
69f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan    explicit Surface(const sp<IGraphicBufferProducer>& bufferProducer,
70f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan            bool controlledByApp = false);
71f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan
72f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan    /* getIGraphicBufferProducer() returns the IGraphicBufferProducer this
73f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan     * Surface was created with. Usually it's an error to use the
7413fb8e99c1a793c42e102e4f16f63de02f6eeed3Randy Pan     * IGraphicBufferProducer while the Surface is connected.
7513fb8e99c1a793c42e102e4f16f63de02f6eeed3Randy Pan     */
76f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan    sp<IGraphicBufferProducer> getIGraphicBufferProducer() const;
77f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan
78f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan    /* convenience function to check that the given surface is non NULL as
7917c2a7b30e5680b11fc0073ce322ee7bc14ef2c5Randy Pan     * well as its IGraphicBufferProducer */
80f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan    static bool isValid(const sp<Surface>& surface) {
81f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan        return surface != NULL && surface->getIGraphicBufferProducer() != NULL;
82f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan    }
83f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan
84f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan    /* Attaches a sideband buffer stream to the Surface's IGraphicBufferProducer.
85f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan     *
86f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan     * A sideband stream is a device-specific mechanism for passing buffers
87f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan     * from the producer to the consumer without using dequeueBuffer/
88f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan     * queueBuffer. If a sideband stream is present, the consumer can choose
89f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan     * whether to acquire buffers from the sideband stream or from the queued
90f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan     * buffers.
91f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan     *
92f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan     * Passing NULL or a different stream handle will detach the previous
93f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan     * handle if any.
94f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan     */
95f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan    void setSidebandStream(const sp<NativeHandle>& stream);
96f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan
97f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan    /* Allocates buffers based on the current dimensions/format.
98f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan     *
995213be94c3b27fff928f5f221e5951c7ede1058bRandy Pan     * This function will allocate up to the maximum number of buffers
100f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan     * permitted by the current BufferQueue configuration. It will use the
10167a4541372684c5d0a3f834f8be76bd8d29ca7acPeter Qiu     * default format and dimensions. This is most useful to avoid an allocation
10267a4541372684c5d0a3f834f8be76bd8d29ca7acPeter Qiu     * delay during dequeueBuffer. If there are already the maximum number of
10367a4541372684c5d0a3f834f8be76bd8d29ca7acPeter Qiu     * buffers allocated, this function has no effect.
10467a4541372684c5d0a3f834f8be76bd8d29ca7acPeter Qiu     */
10567a4541372684c5d0a3f834f8be76bd8d29ca7acPeter Qiu    void allocateBuffers();
10667a4541372684c5d0a3f834f8be76bd8d29ca7acPeter Qiu
10767a4541372684c5d0a3f834f8be76bd8d29ca7acPeter Qiu    /* Sets the generation number on the IGraphicBufferProducer and updates the
10867a4541372684c5d0a3f834f8be76bd8d29ca7acPeter Qiu     * generation number on any buffers attached to the Surface after this call.
10967a4541372684c5d0a3f834f8be76bd8d29ca7acPeter Qiu     * See IGBP::setGenerationNumber for more information. */
110f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan    status_t setGenerationNumber(uint32_t generationNumber);
111f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan
112f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan    // See IGraphicBufferProducer::getConsumerName
113f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan    String8 getConsumerName() const;
114f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan
115f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan    // See IGraphicBufferProducer::getNextFrameNumber
116f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan    uint64_t getNextFrameNumber() const;
117f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan
118f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan    /* Set the scaling mode to be used with a Surface.
119f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan     * See NATIVE_WINDOW_SET_SCALING_MODE and its parameters
12039d6bdc92f98fd7100382d8fb5f7f7044dbcb083Randy Pan     * in <system/window.h>. */
12139d6bdc92f98fd7100382d8fb5f7f7044dbcb083Randy Pan    int setScalingMode(int mode);
12239d6bdc92f98fd7100382d8fb5f7f7044dbcb083Randy Pan
12339d6bdc92f98fd7100382d8fb5f7f7044dbcb083Randy Pan    // See IGraphicBufferProducer::setDequeueTimeout
12439d6bdc92f98fd7100382d8fb5f7f7044dbcb083Randy Pan    status_t setDequeueTimeout(nsecs_t timeout);
12539d6bdc92f98fd7100382d8fb5f7f7044dbcb083Randy Pan
12639d6bdc92f98fd7100382d8fb5f7f7044dbcb083Randy Pan    /*
12739d6bdc92f98fd7100382d8fb5f7f7044dbcb083Randy Pan     * Wait for frame number to increase past lastFrame for at most
12839d6bdc92f98fd7100382d8fb5f7f7044dbcb083Randy Pan     * timeoutNs. Useful for one thread to wait for another unknown
12939d6bdc92f98fd7100382d8fb5f7f7044dbcb083Randy Pan     * thread to queue a buffer.
13039d6bdc92f98fd7100382d8fb5f7f7044dbcb083Randy Pan     */
13139d6bdc92f98fd7100382d8fb5f7f7044dbcb083Randy Pan    bool waitForNextFrame(uint64_t lastFrame, nsecs_t timeout);
13239d6bdc92f98fd7100382d8fb5f7f7044dbcb083Randy Pan
13339d6bdc92f98fd7100382d8fb5f7f7044dbcb083Randy Pan    // See IGraphicBufferProducer::getLastQueuedBuffer
13439d6bdc92f98fd7100382d8fb5f7f7044dbcb083Randy Pan    // See GLConsumer::getTransformMatrix for outTransformMatrix format
13539d6bdc92f98fd7100382d8fb5f7f7044dbcb083Randy Pan    status_t getLastQueuedBuffer(sp<GraphicBuffer>* outBuffer,
13639d6bdc92f98fd7100382d8fb5f7f7044dbcb083Randy Pan            sp<Fence>* outFence, float outTransformMatrix[16]);
13739d6bdc92f98fd7100382d8fb5f7f7044dbcb083Randy Pan
1385213be94c3b27fff928f5f221e5951c7ede1058bRandy Pan    /* Enables or disables frame timestamp tracking. It is disabled by default
1395213be94c3b27fff928f5f221e5951c7ede1058bRandy Pan     * to avoid overhead during queue and dequeue for applications that don't
1405213be94c3b27fff928f5f221e5951c7ede1058bRandy Pan     * need the feature. If disabled, calls to getFrameTimestamps will fail.
1415213be94c3b27fff928f5f221e5951c7ede1058bRandy Pan     */
1425213be94c3b27fff928f5f221e5951c7ede1058bRandy Pan    void enableFrameTimestamps(bool enable);
1435213be94c3b27fff928f5f221e5951c7ede1058bRandy Pan
1445213be94c3b27fff928f5f221e5951c7ede1058bRandy Pan    // See IGraphicBufferProducer::getFrameTimestamps
1455213be94c3b27fff928f5f221e5951c7ede1058bRandy Pan    status_t getFrameTimestamps(uint64_t frameNumber,
1465213be94c3b27fff928f5f221e5951c7ede1058bRandy Pan            nsecs_t* outRequestedPresentTime, nsecs_t* outAcquireTime,
147f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan            nsecs_t* outLatchTime, nsecs_t* outFirstRefreshStartTime,
148f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan            nsecs_t* outLastRefreshStartTime, nsecs_t* outGlCompositionDoneTime,
149f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan            nsecs_t* outDisplayPresentTime, nsecs_t* outDisplayRetireTime,
150f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan            nsecs_t* outDequeueReadyTime, nsecs_t* outReleaseTime);
151f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan    status_t getDisplayRefreshCycleDuration(nsecs_t* outRefreshDuration);
152f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan
153f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan    status_t getUniqueId(uint64_t* outId) const;
154f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan
155f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Panprotected:
156f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan    virtual ~Surface();
157f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan
158f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan    // Virtual for testing.
159f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan    virtual sp<ISurfaceComposer> composerService() const;
160f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan
16113fb8e99c1a793c42e102e4f16f63de02f6eeed3Randy Panprivate:
162f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan    // can't be copied
1634de361cb58da0b0c410c8c41dc97fb87f687d270Randy Pan    Surface& operator = (const Surface& rhs);
1644de361cb58da0b0c410c8c41dc97fb87f687d270Randy Pan    Surface(const Surface& rhs);
165f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan
16613fb8e99c1a793c42e102e4f16f63de02f6eeed3Randy Pan    // ANativeWindow hooks
16713fb8e99c1a793c42e102e4f16f63de02f6eeed3Randy Pan    static int hook_cancelBuffer(ANativeWindow* window,
16813fb8e99c1a793c42e102e4f16f63de02f6eeed3Randy Pan            ANativeWindowBuffer* buffer, int fenceFd);
169f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan    static int hook_dequeueBuffer(ANativeWindow* window,
170f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan            ANativeWindowBuffer** buffer, int* fenceFd);
171f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan    static int hook_perform(ANativeWindow* window, int operation, ...);
172f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan    static int hook_query(const ANativeWindow* window, int what, int* value);
17313fb8e99c1a793c42e102e4f16f63de02f6eeed3Randy Pan    static int hook_queueBuffer(ANativeWindow* window,
174f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan            ANativeWindowBuffer* buffer, int fenceFd);
17513fb8e99c1a793c42e102e4f16f63de02f6eeed3Randy Pan    static int hook_setSwapInterval(ANativeWindow* window, int interval);
176f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan
177f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan    static int hook_cancelBuffer_DEPRECATED(ANativeWindow* window,
178f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan            ANativeWindowBuffer* buffer);
179f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan    static int hook_dequeueBuffer_DEPRECATED(ANativeWindow* window,
180c301f93e031e6a2c470524f98045fad771545a7eRoshan Pius            ANativeWindowBuffer** buffer);
181c301f93e031e6a2c470524f98045fad771545a7eRoshan Pius    static int hook_lockBuffer_DEPRECATED(ANativeWindow* window,
182f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan            ANativeWindowBuffer* buffer);
183f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan    static int hook_queueBuffer_DEPRECATED(ANativeWindow* window,
184f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan            ANativeWindowBuffer* buffer);
185f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan
186f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan    int dispatchConnect(va_list args);
1874de361cb58da0b0c410c8c41dc97fb87f687d270Randy Pan    int dispatchDisconnect(va_list args);
188f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan    int dispatchSetBufferCount(va_list args);
189f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan    int dispatchSetBuffersGeometry(va_list args);
190f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan    int dispatchSetBuffersDimensions(va_list args);
191f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan    int dispatchSetBuffersUserDimensions(va_list args);
192f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan    int dispatchSetBuffersFormat(va_list args);
193f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan    int dispatchSetScalingMode(va_list args);
194d335557ef8c724395a39c8fdda26653c90dcc1c5Randy Pan    int dispatchSetBuffersTransform(va_list args);
19596c4e4d724bd9e666adf177722eb7136ab340185Randy Pan    int dispatchSetBuffersStickyTransform(va_list args);
19696c4e4d724bd9e666adf177722eb7136ab340185Randy Pan    int dispatchSetBuffersTimestamp(va_list args);
197f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan    int dispatchSetCrop(va_list args);
1984de361cb58da0b0c410c8c41dc97fb87f687d270Randy Pan    int dispatchSetPostTransformCrop(va_list args);
199c22ad7f19888f7967edcf30080543bb632ddd68dRandy Pan    int dispatchSetUsage(va_list args);
200c22ad7f19888f7967edcf30080543bb632ddd68dRandy Pan    int dispatchLock(va_list args);
201c22ad7f19888f7967edcf30080543bb632ddd68dRandy Pan    int dispatchUnlockAndPost(va_list args);
202c22ad7f19888f7967edcf30080543bb632ddd68dRandy Pan    int dispatchSetSidebandStream(va_list args);
203c22ad7f19888f7967edcf30080543bb632ddd68dRandy Pan    int dispatchSetBuffersDataSpace(va_list args);
204c22ad7f19888f7967edcf30080543bb632ddd68dRandy Pan    int dispatchSetSurfaceDamage(va_list args);
20532f4de6e3cd333c657ac3aff078028a1d52600e3Randy Pan    int dispatchSetSharedBufferMode(va_list args);
206c22ad7f19888f7967edcf30080543bb632ddd68dRandy Pan    int dispatchSetAutoRefresh(va_list args);
207f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan    int dispatchGetNextFrameId(va_list args);
208f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan    int dispatchEnableFrameTimestamps(va_list args);
209f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan    int dispatchGetFrameTimestamps(va_list args);
210f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan    int dispatchGetDisplayRefreshCycleDuration(va_list args);
211f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan
2124de361cb58da0b0c410c8c41dc97fb87f687d270Randy Panprotected:
213f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan    virtual int dequeueBuffer(ANativeWindowBuffer** buffer, int* fenceFd);
214f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan    virtual int cancelBuffer(ANativeWindowBuffer* buffer, int fenceFd);
215f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan    virtual int queueBuffer(ANativeWindowBuffer* buffer, int fenceFd);
21667a4541372684c5d0a3f834f8be76bd8d29ca7acPeter Qiu    virtual int perform(int operation, va_list args);
217f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan    virtual int setSwapInterval(int interval);
218f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan
219f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan    virtual int lockBuffer_DEPRECATED(ANativeWindowBuffer* buffer);
220f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan
221f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan    virtual int connect(int api);
222f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan    virtual int setBufferCount(int bufferCount);
223f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan    virtual int setBuffersUserDimensions(uint32_t width, uint32_t height);
224f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan    virtual int setBuffersFormat(PixelFormat format);
225f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan    virtual int setBuffersTransform(uint32_t transform);
226f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan    virtual int setBuffersStickyTransform(uint32_t transform);
227f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan    virtual int setBuffersTimestamp(int64_t timestamp);
228f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan    virtual int setBuffersDataSpace(android_dataspace dataSpace);
229f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan    virtual int setCrop(Rect const* rect);
230f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan    virtual int setUsage(uint32_t reqUsage);
231f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan    virtual void setSurfaceDamage(android_native_rect_t* rects, size_t numRects);
232f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan
233f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Panpublic:
234f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan    virtual int disconnect(int api,
235f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan            IGraphicBufferProducer::DisconnectMode mode =
236f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan                    IGraphicBufferProducer::DisconnectMode::Api);
237f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan
238f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan    virtual int setMaxDequeuedBufferCount(int maxDequeuedBuffers);
239f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan    virtual int setAsyncMode(bool async);
240f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan    virtual int setSharedBufferMode(bool sharedBufferMode);
241f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan    virtual int setAutoRefresh(bool autoRefresh);
242f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan    virtual int setBuffersDimensions(uint32_t width, uint32_t height);
243f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan    virtual int lock(ANativeWindow_Buffer* outBuffer, ARect* inOutDirtyBounds);
244f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan    virtual int unlockAndPost();
245f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan    virtual int query(int what, int* value) const;
246f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan
247f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan    virtual int connect(int api, const sp<IProducerListener>& listener);
248f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan    virtual int detachNextBuffer(sp<GraphicBuffer>* outBuffer,
249f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan            sp<Fence>* outFence);
250f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan    virtual int attachBuffer(ANativeWindowBuffer*);
251f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan
252f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Panprotected:
253f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan    enum { NUM_BUFFER_SLOTS = BufferQueueDefs::NUM_BUFFER_SLOTS };
254f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan    enum { DEFAULT_FORMAT = PIXEL_FORMAT_RGBA_8888 };
255f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan
256f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan    void querySupportedTimestampsLocked() const;
257f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan
258f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan    void freeAllBuffers();
259f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan    int getSlotFromBufferLocked(android_native_buffer_t* buffer) const;
260f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan
26167a4541372684c5d0a3f834f8be76bd8d29ca7acPeter Qiu    struct BufferSlot {
26232a2c670100b84fb049f55ad5a8a300c60a48aa5Randy Pan        sp<GraphicBuffer> buffer;
26332a2c670100b84fb049f55ad5a8a300c60a48aa5Randy Pan        Region dirtyRegion;
26432a2c670100b84fb049f55ad5a8a300c60a48aa5Randy Pan    };
26532a2c670100b84fb049f55ad5a8a300c60a48aa5Randy Pan
26667a4541372684c5d0a3f834f8be76bd8d29ca7acPeter Qiu    // mSurfaceTexture is the interface to the surface texture server. All
26732a2c670100b84fb049f55ad5a8a300c60a48aa5Randy Pan    // operations on the surface texture client ultimately translate into
26867a4541372684c5d0a3f834f8be76bd8d29ca7acPeter Qiu    // interactions with the server using this interface.
26967a4541372684c5d0a3f834f8be76bd8d29ca7acPeter Qiu    // TODO: rename to mBufferProducer
27067a4541372684c5d0a3f834f8be76bd8d29ca7acPeter Qiu    sp<IGraphicBufferProducer> mGraphicBufferProducer;
271f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan
272f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan    // mSlots stores the buffers that have been allocated for each buffer slot.
273f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan    // It is initialized to null pointers, and gets filled in with the result of
274f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan    // IGraphicBufferProducer::requestBuffer when the client dequeues a buffer from a
275f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan    // slot that has not yet been used. The buffer allocated to a slot will also
276f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan    // be replaced if the requested buffer usage or geometry differs from that
277f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan    // of the buffer allocated to a slot.
278f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan    BufferSlot mSlots[NUM_BUFFER_SLOTS];
279f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan
280f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan    // mReqWidth is the buffer width that will be requested at the next dequeue
281f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan    // operation. It is initialized to 1.
282f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan    uint32_t mReqWidth;
283f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan
284f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan    // mReqHeight is the buffer height that will be requested at the next
285f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan    // dequeue operation. It is initialized to 1.
286f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan    uint32_t mReqHeight;
287235642dba4359c1e68618f27c949e744765cbbccAmin Shaikh
288235642dba4359c1e68618f27c949e744765cbbccAmin Shaikh    // mReqFormat is the buffer pixel format that will be requested at the next
289235642dba4359c1e68618f27c949e744765cbbccAmin Shaikh    // deuque operation. It is initialized to PIXEL_FORMAT_RGBA_8888.
290235642dba4359c1e68618f27c949e744765cbbccAmin Shaikh    PixelFormat mReqFormat;
291235642dba4359c1e68618f27c949e744765cbbccAmin Shaikh
292235642dba4359c1e68618f27c949e744765cbbccAmin Shaikh    // mReqUsage is the set of buffer usage flags that will be requested
293235642dba4359c1e68618f27c949e744765cbbccAmin Shaikh    // at the next deuque operation. It is initialized to 0.
294235642dba4359c1e68618f27c949e744765cbbccAmin Shaikh    uint32_t mReqUsage;
295235642dba4359c1e68618f27c949e744765cbbccAmin Shaikh
296235642dba4359c1e68618f27c949e744765cbbccAmin Shaikh    // mTimestamp is the timestamp that will be used for the next buffer queue
297235642dba4359c1e68618f27c949e744765cbbccAmin Shaikh    // operation. It defaults to NATIVE_WINDOW_TIMESTAMP_AUTO, which means that
298235642dba4359c1e68618f27c949e744765cbbccAmin Shaikh    // a timestamp is auto-generated when queueBuffer is called.
299235642dba4359c1e68618f27c949e744765cbbccAmin Shaikh    int64_t mTimestamp;
300235642dba4359c1e68618f27c949e744765cbbccAmin Shaikh
301235642dba4359c1e68618f27c949e744765cbbccAmin Shaikh    // mDataSpace is the buffer dataSpace that will be used for the next buffer
302235642dba4359c1e68618f27c949e744765cbbccAmin Shaikh    // queue operation. It defaults to HAL_DATASPACE_UNKNOWN, which
303f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan    // means that the buffer contains some type of color data.
304f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan    android_dataspace mDataSpace;
305f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan
306f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan    // mCrop is the crop rectangle that will be used for the next buffer
307e4b4b229331da3964671606f18557b2e7f681b45Amin Shaikh    // that gets queued. It is set by calling setCrop.
308e4b4b229331da3964671606f18557b2e7f681b45Amin Shaikh    Rect mCrop;
309f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan
310f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan    // mScalingMode is the scaling mode that will be used for the next
311f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan    // buffers that get queued. It is set by calling setScalingMode.
312f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan    int mScalingMode;
313f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan
314f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan    // mTransform is the transform identifier that will be used for the next
315f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan    // buffer that gets queued. It is set by calling setTransform.
316f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan    uint32_t mTransform;
317f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan
318f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan    // mStickyTransform is a transform that is applied on top of mTransform
319f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan    // in each buffer that is queued.  This is typically used to force the
320f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan    // compositor to apply a transform, and will prevent the transform hint
321f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan    // from being set by the compositor.
322f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan    uint32_t mStickyTransform;
323f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan
324f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan    // mDefaultWidth is default width of the buffers, regardless of the
325f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan    // native_window_set_buffers_dimensions call.
326f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan    uint32_t mDefaultWidth;
327f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan
328f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan    // mDefaultHeight is default height of the buffers, regardless of the
329f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan    // native_window_set_buffers_dimensions call.
330f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan    uint32_t mDefaultHeight;
331f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan
332f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan    // mUserWidth, if non-zero, is an application-specified override
333f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan    // of mDefaultWidth.  This is lower priority than the width set by
334f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan    // native_window_set_buffers_dimensions.
335f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan    uint32_t mUserWidth;
336f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan
337235642dba4359c1e68618f27c949e744765cbbccAmin Shaikh    // mUserHeight, if non-zero, is an application-specified override
338f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan    // of mDefaultHeight.  This is lower priority than the height set
339f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan    // by native_window_set_buffers_dimensions.
340235642dba4359c1e68618f27c949e744765cbbccAmin Shaikh    uint32_t mUserHeight;
341f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan
342f2a46f11dd92f2820e96b1b8b69b433012d6bcefRandy Pan    // mTransformHint is the transform probably applied to buffers of this
343    // window. this is only a hint, actual transform may differ.
344    uint32_t mTransformHint;
345
346    // mProducerControlledByApp whether this buffer producer is controlled
347    // by the application
348    bool mProducerControlledByApp;
349
350    // mSwapIntervalZero set if we should drop buffers at queue() time to
351    // achieve an asynchronous swap interval
352    bool mSwapIntervalZero;
353
354    // mConsumerRunningBehind whether the consumer is running more than
355    // one buffer behind the producer.
356    mutable bool mConsumerRunningBehind;
357
358    // mMutex is the mutex used to prevent concurrent access to the member
359    // variables of Surface objects. It must be locked whenever the
360    // member variables are accessed.
361    mutable Mutex mMutex;
362
363    // must be used from the lock/unlock thread
364    sp<GraphicBuffer>           mLockedBuffer;
365    sp<GraphicBuffer>           mPostedBuffer;
366    bool                        mConnectedToCpu;
367
368    // When a CPU producer is attached, this reflects the region that the
369    // producer wished to update as well as whether the Surface was able to copy
370    // the previous buffer back to allow a partial update.
371    //
372    // When a non-CPU producer is attached, this reflects the surface damage
373    // (the change since the previous frame) passed in by the producer.
374    Region mDirtyRegion;
375
376    // Stores the current generation number. See setGenerationNumber and
377    // IGraphicBufferProducer::setGenerationNumber for more information.
378    uint32_t mGenerationNumber;
379
380    // Caches the values that have been passed to the producer.
381    bool mSharedBufferMode;
382    bool mAutoRefresh;
383
384    // If in shared buffer mode and auto refresh is enabled, store the shared
385    // buffer slot and return it for all calls to queue/dequeue without going
386    // over Binder.
387    int mSharedBufferSlot;
388
389    // This is true if the shared buffer has already been queued/canceled. It's
390    // used to prevent a mismatch between the number of queue/dequeue calls.
391    bool mSharedBufferHasBeenQueued;
392
393    // These are used to satisfy the NATIVE_WINDOW_LAST_*_DURATION queries
394    nsecs_t mLastDequeueDuration = 0;
395    nsecs_t mLastQueueDuration = 0;
396
397    Condition mQueueBufferCondition;
398
399    uint64_t mNextFrameNumber = 1;
400    uint64_t mLastFrameNumber = 0;
401
402    // Mutable because ANativeWindow::query needs this class const.
403    mutable bool mQueriedSupportedTimestamps;
404    mutable bool mFrameTimestampsSupportsPresent;
405    mutable bool mFrameTimestampsSupportsRetire;
406
407    // A cached copy of the FrameEventHistory maintained by the consumer.
408    bool mEnableFrameTimestamps = false;
409    std::unique_ptr<ProducerFrameEventHistory> mFrameEventHistory;
410};
411
412} // namespace android
413
414#endif  // ANDROID_GUI_SURFACE_H
415