18ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis/*
28ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis * Copyright (C) 2010 The Android Open Source Project
38ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis *
48ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis * Licensed under the Apache License, Version 2.0 (the "License");
58ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis * you may not use this file except in compliance with the License.
68ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis * You may obtain a copy of the License at
78ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis *
88ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis *      http://www.apache.org/licenses/LICENSE-2.0
98ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis *
108ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis * Unless required by applicable law or agreed to in writing, software
118ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis * distributed under the License is distributed on an "AS IS" BASIS,
128ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
138ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis * See the License for the specific language governing permissions and
148ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis * limitations under the License.
158ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis */
168ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis
172adaf04fab35cf47c824d74d901b54094e01ccd3Andy McFadden#ifndef ANDROID_GUI_CONSUMER_H
182adaf04fab35cf47c824d74d901b54094e01ccd3Andy McFadden#define ANDROID_GUI_CONSUMER_H
198ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis
208ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis#include <EGL/egl.h>
218ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis#include <EGL/eglext.h>
228ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis
232adaf04fab35cf47c824d74d901b54094e01ccd3Andy McFadden#include <gui/IGraphicBufferProducer.h>
246b091c53000c843211c218ce40287a7edca9bc63Daniel Lam#include <gui/BufferQueue.h>
259fea3421ffddf6480f57f55a25936a886043d909Jamie Gennis#include <gui/ConsumerBase.h>
268ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis
278ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis#include <ui/GraphicBuffer.h>
288ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis
29fa28c35c21d1bf8b38f541758c291bc17a2d7270Jamie Gennis#include <utils/String8.h>
309a78c90cd46b2a3bd637b056873149d3b94384b4Jamie Gennis#include <utils/Vector.h>
31fa28c35c21d1bf8b38f541758c291bc17a2d7270Jamie Gennis#include <utils/threads.h>
328ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis
338ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennisnamespace android {
348ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis// ----------------------------------------------------------------------------
358ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis
366b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
3768c7794183a7dbfe3b20d4ce832f8eb79c2c619aMathias Agopianclass String8;
389a78c90cd46b2a3bd637b056873149d3b94384b4Jamie Gennis
390273adbf0bc202eda2ca579ae0773464ea9c701fAndy McFadden/*
402adaf04fab35cf47c824d74d901b54094e01ccd3Andy McFadden * GLConsumer consumes buffers of graphics data from a BufferQueue,
410273adbf0bc202eda2ca579ae0773464ea9c701fAndy McFadden * and makes them available to OpenGL as a texture.
420273adbf0bc202eda2ca579ae0773464ea9c701fAndy McFadden *
432adaf04fab35cf47c824d74d901b54094e01ccd3Andy McFadden * A typical usage pattern is to set up the GLConsumer with the
440273adbf0bc202eda2ca579ae0773464ea9c701fAndy McFadden * desired options, and call updateTexImage() when a new frame is desired.
450273adbf0bc202eda2ca579ae0773464ea9c701fAndy McFadden * If a new frame is available, the texture will be updated.  If not,
460273adbf0bc202eda2ca579ae0773464ea9c701fAndy McFadden * the previous contents are retained.
470273adbf0bc202eda2ca579ae0773464ea9c701fAndy McFadden *
480273adbf0bc202eda2ca579ae0773464ea9c701fAndy McFadden * By default, the texture is attached to the GL_TEXTURE_EXTERNAL_OES
490273adbf0bc202eda2ca579ae0773464ea9c701fAndy McFadden * texture target, in the EGL context of the first thread that calls
500273adbf0bc202eda2ca579ae0773464ea9c701fAndy McFadden * updateTexImage().
510273adbf0bc202eda2ca579ae0773464ea9c701fAndy McFadden *
522adaf04fab35cf47c824d74d901b54094e01ccd3Andy McFadden * This class was previously called SurfaceTexture.
530273adbf0bc202eda2ca579ae0773464ea9c701fAndy McFadden */
542adaf04fab35cf47c824d74d901b54094e01ccd3Andy McFaddenclass GLConsumer : public ConsumerBase {
558ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennispublic:
56e49ba8e2ed8e17156eb00c8fc8e2285df62bc018Dan Stoza    enum { TEXTURE_EXTERNAL = 0x8D65 }; // GL_TEXTURE_EXTERNAL_OES
579fea3421ffddf6480f57f55a25936a886043d909Jamie Gennis    typedef ConsumerBase::FrameAvailableListener FrameAvailableListener;
58c4d4aeab52435c177ded6abdd578fec8191f0f5dJamie Gennis
59ab57491de3a89a2d454d3060d36adef71741a7aeDan Stoza    // GLConsumer constructs a new GLConsumer object. If the constructor with
60ab57491de3a89a2d454d3060d36adef71741a7aeDan Stoza    // the tex parameter is used, tex indicates the name of the OpenGL ES
61ab57491de3a89a2d454d3060d36adef71741a7aeDan Stoza    // texture to which images are to be streamed. texTarget specifies the
62ab57491de3a89a2d454d3060d36adef71741a7aeDan Stoza    // OpenGL ES texture target to which the texture will be bound in
63ab57491de3a89a2d454d3060d36adef71741a7aeDan Stoza    // updateTexImage. useFenceSync specifies whether fences should be used to
64ab57491de3a89a2d454d3060d36adef71741a7aeDan Stoza    // synchronize access to buffers if that behavior is enabled at
65ab57491de3a89a2d454d3060d36adef71741a7aeDan Stoza    // compile-time.
6674bed55fff0132be319bcd1703970516ae28b3a9Jamie Gennis    //
672adaf04fab35cf47c824d74d901b54094e01ccd3Andy McFadden    // A GLConsumer may be detached from one OpenGL ES context and then
6874bed55fff0132be319bcd1703970516ae28b3a9Jamie Gennis    // attached to a different context using the detachFromContext and
6974bed55fff0132be319bcd1703970516ae28b3a9Jamie Gennis    // attachToContext methods, respectively. The intention of these methods is
702adaf04fab35cf47c824d74d901b54094e01ccd3Andy McFadden    // purely to allow a GLConsumer to be transferred from one consumer
7174bed55fff0132be319bcd1703970516ae28b3a9Jamie Gennis    // context to another. If such a transfer is not needed there is no
7274bed55fff0132be319bcd1703970516ae28b3a9Jamie Gennis    // requirement that either of these methods be called.
73ab57491de3a89a2d454d3060d36adef71741a7aeDan Stoza    //
74ab57491de3a89a2d454d3060d36adef71741a7aeDan Stoza    // If the constructor with the tex parameter is used, the GLConsumer is
75ab57491de3a89a2d454d3060d36adef71741a7aeDan Stoza    // created in a state where it is considered attached to an OpenGL ES
76ab57491de3a89a2d454d3060d36adef71741a7aeDan Stoza    // context for the purposes of the attachToContext and detachFromContext
77ab57491de3a89a2d454d3060d36adef71741a7aeDan Stoza    // methods. However, despite being considered "attached" to a context, the
78ab57491de3a89a2d454d3060d36adef71741a7aeDan Stoza    // specific OpenGL ES context doesn't get latched until the first call to
79ab57491de3a89a2d454d3060d36adef71741a7aeDan Stoza    // updateTexImage. After that point, all calls to updateTexImage must be
80ab57491de3a89a2d454d3060d36adef71741a7aeDan Stoza    // made with the same OpenGL ES context current.
81ab57491de3a89a2d454d3060d36adef71741a7aeDan Stoza    //
82ab57491de3a89a2d454d3060d36adef71741a7aeDan Stoza    // If the constructor without the tex parameter is used, the GLConsumer is
83ab57491de3a89a2d454d3060d36adef71741a7aeDan Stoza    // created in a detached state, and attachToContext must be called before
84ab57491de3a89a2d454d3060d36adef71741a7aeDan Stoza    // calls to updateTexImage.
85db89edc94bd2a78226b407f9f7261e202e7fa325Mathias Agopian    GLConsumer(const sp<IGraphicBufferConsumer>& bq,
86e49ba8e2ed8e17156eb00c8fc8e2285df62bc018Dan Stoza            uint32_t tex, uint32_t texureTarget, bool useFenceSync,
87e49ba8e2ed8e17156eb00c8fc8e2285df62bc018Dan Stoza            bool isControlledByApp);
888ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis
89ab57491de3a89a2d454d3060d36adef71741a7aeDan Stoza    GLConsumer(const sp<IGraphicBufferConsumer>& bq, uint32_t texureTarget,
90ab57491de3a89a2d454d3060d36adef71741a7aeDan Stoza            bool useFenceSync, bool isControlledByApp);
91ab57491de3a89a2d454d3060d36adef71741a7aeDan Stoza
92bf974abe92f7495529916fe0f483f3b56e7c30e3Andy McFadden    // updateTexImage acquires the most recently queued buffer, and sets the
93bf974abe92f7495529916fe0f483f3b56e7c30e3Andy McFadden    // image contents of the target texture to it.
948ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis    //
958ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis    // This call may only be made while the OpenGL ES context to which the
968ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis    // target texture belongs is bound to the calling thread.
9761e04b92bdeafc6fca89052d14dab1bd0c384a71Jamie Gennis    //
98bf974abe92f7495529916fe0f483f3b56e7c30e3Andy McFadden    // This calls doGLFenceWait to ensure proper synchronization.
998ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis    status_t updateTexImage();
1008ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis
101ad678e18b66f495efa78dc3b9ab99b579945c9e2Mathias Agopian    // releaseTexImage releases the texture acquired in updateTexImage().
102ad678e18b66f495efa78dc3b9ab99b579945c9e2Mathias Agopian    // This is intended to be used in single buffer mode.
103ad678e18b66f495efa78dc3b9ab99b579945c9e2Mathias Agopian    //
104ad678e18b66f495efa78dc3b9ab99b579945c9e2Mathias Agopian    // This call may only be made while the OpenGL ES context to which the
105ad678e18b66f495efa78dc3b9ab99b579945c9e2Mathias Agopian    // target texture belongs is bound to the calling thread.
106ad678e18b66f495efa78dc3b9ab99b579945c9e2Mathias Agopian    status_t releaseTexImage();
107ad678e18b66f495efa78dc3b9ab99b579945c9e2Mathias Agopian
10813f01cbdbd34779a234bc674df79e23672fd5c0bJesse Hall    // setReleaseFence stores a fence that will signal when the current buffer
10913f01cbdbd34779a234bc674df79e23672fd5c0bJesse Hall    // is no longer being read. This fence will be returned to the producer
11013f01cbdbd34779a234bc674df79e23672fd5c0bJesse Hall    // when the current buffer is released by updateTexImage(). Multiple
11113f01cbdbd34779a234bc674df79e23672fd5c0bJesse Hall    // fences can be set for a given buffer; they will be merged into a single
11213f01cbdbd34779a234bc674df79e23672fd5c0bJesse Hall    // union fence.
1139e56aa0fdb5f7121b9b975c6c16db103ea4d2fe9Dan Stoza    virtual void setReleaseFence(const sp<Fence>& fence);
114ef19414bd8b77a26f5751f3845be79025a8263feJesse Hall
115f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennis    // getTransformMatrix retrieves the 4x4 texture coordinate transform matrix
116f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennis    // associated with the texture image set by the most recent call to
117f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennis    // updateTexImage.
118f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennis    //
119f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennis    // This transform matrix maps 2D homogeneous texture coordinates of the form
120f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennis    // (s, t, 0, 1) with s and t in the inclusive range [0, 1] to the texture
121f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennis    // coordinate that should be used to sample that location from the texture.
122f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennis    // Sampling the texture outside of the range of this transform is undefined.
123f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennis    //
124f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennis    // This transform is necessary to compensate for transforms that the stream
125f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennis    // content producer may implicitly apply to the content. By forcing users of
1262adaf04fab35cf47c824d74d901b54094e01ccd3Andy McFadden    // a GLConsumer to apply this transform we avoid performing an extra
127f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennis    // copy of the data that would be needed to hide the transform from the
128f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennis    // user.
129f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennis    //
130f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennis    // The matrix is stored in column-major order so that it may be passed
131f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennis    // directly to OpenGL ES via the glLoadMatrixf or glUniformMatrix4fv
132f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennis    // functions.
133f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennis    void getTransformMatrix(float mtx[16]);
134f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennis
1351a61da5e28fa16ad556a58193c8bbeb32a5f636dJohn Reck    // Computes the transform matrix documented by getTransformMatrix
1361a61da5e28fa16ad556a58193c8bbeb32a5f636dJohn Reck    // from the BufferItem sub parts.
1371a61da5e28fa16ad556a58193c8bbeb32a5f636dJohn Reck    static void computeTransformMatrix(float outTransform[16],
1381a61da5e28fa16ad556a58193c8bbeb32a5f636dJohn Reck            const sp<GraphicBuffer>& buf, const Rect& cropRect,
1391a61da5e28fa16ad556a58193c8bbeb32a5f636dJohn Reck            uint32_t transform, bool filtering);
1401a61da5e28fa16ad556a58193c8bbeb32a5f636dJohn Reck
1411d01a12e7150be569557b64da9b8663c62c13594Eino-Ville Talvala    // getTimestamp retrieves the timestamp associated with the texture image
1421d01a12e7150be569557b64da9b8663c62c13594Eino-Ville Talvala    // set by the most recent call to updateTexImage.
1431d01a12e7150be569557b64da9b8663c62c13594Eino-Ville Talvala    //
1441d01a12e7150be569557b64da9b8663c62c13594Eino-Ville Talvala    // The timestamp is in nanoseconds, and is monotonically increasing. Its
1451d01a12e7150be569557b64da9b8663c62c13594Eino-Ville Talvala    // other semantics (zero point, etc) are source-dependent and should be
1461d01a12e7150be569557b64da9b8663c62c13594Eino-Ville Talvala    // documented by the source.
1471d01a12e7150be569557b64da9b8663c62c13594Eino-Ville Talvala    int64_t getTimestamp();
1481d01a12e7150be569557b64da9b8663c62c13594Eino-Ville Talvala
149d171da973de3c6b30263011334fdcd916739144fEino-Ville Talvala    // getFrameNumber retrieves the frame number associated with the texture
150d171da973de3c6b30263011334fdcd916739144fEino-Ville Talvala    // image set by the most recent call to updateTexImage.
151d171da973de3c6b30263011334fdcd916739144fEino-Ville Talvala    //
152d171da973de3c6b30263011334fdcd916739144fEino-Ville Talvala    // The frame number is an incrementing counter set to 0 at the creation of
153d171da973de3c6b30263011334fdcd916739144fEino-Ville Talvala    // the BufferQueue associated with this consumer.
154d723bd7669b4fc88dc282d8bf8ba5ecb2849d22fDan Stoza    uint64_t getFrameNumber();
155d171da973de3c6b30263011334fdcd916739144fEino-Ville Talvala
156a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian    // setDefaultBufferSize is used to set the size of buffers returned by
157a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian    // requestBuffers when a with and height of zero is requested.
158a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian    // A call to setDefaultBufferSize() may trigger requestBuffers() to
159a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian    // be called from the client.
160194c76c0477189700fda068e19b953b1d9af201aMathias Agopian    // The width and height parameters must be no greater than the minimum of
161194c76c0477189700fda068e19b953b1d9af201aMathias Agopian    // GL_MAX_VIEWPORT_DIMS and GL_MAX_TEXTURE_SIZE (see: glGetIntegerv).
162194c76c0477189700fda068e19b953b1d9af201aMathias Agopian    // An error due to invalid dimensions might not be reported until
163194c76c0477189700fda068e19b953b1d9af201aMathias Agopian    // updateTexImage() is called.
164194c76c0477189700fda068e19b953b1d9af201aMathias Agopian    status_t setDefaultBufferSize(uint32_t width, uint32_t height);
165a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian
1665c1139fea3cc0fd9847a6594d853a458152b2fbcJamie Gennis    // setFilteringEnabled sets whether the transform matrix should be computed
1675c1139fea3cc0fd9847a6594d853a458152b2fbcJamie Gennis    // for use with bilinear filtering.
1685c1139fea3cc0fd9847a6594d853a458152b2fbcJamie Gennis    void setFilteringEnabled(bool enabled);
1695c1139fea3cc0fd9847a6594d853a458152b2fbcJamie Gennis
1707a042bf324fe3f3d5d4085fda21fe50dc0c362b4Mathias Agopian    // getCurrentBuffer returns the buffer associated with the current image.
1717a042bf324fe3f3d5d4085fda21fe50dc0c362b4Mathias Agopian    sp<GraphicBuffer> getCurrentBuffer() const;
1727a042bf324fe3f3d5d4085fda21fe50dc0c362b4Mathias Agopian
1737a042bf324fe3f3d5d4085fda21fe50dc0c362b4Mathias Agopian    // getCurrentTextureTarget returns the texture target of the current
1747a042bf324fe3f3d5d4085fda21fe50dc0c362b4Mathias Agopian    // texture as returned by updateTexImage().
1753f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian    uint32_t getCurrentTextureTarget() const;
1767a042bf324fe3f3d5d4085fda21fe50dc0c362b4Mathias Agopian
177efc7ab6dcea8c22ddd7c0259ef4ab4bbf1e93044Jamie Gennis    // getCurrentCrop returns the cropping rectangle of the current buffer.
1787a042bf324fe3f3d5d4085fda21fe50dc0c362b4Mathias Agopian    Rect getCurrentCrop() const;
1797a042bf324fe3f3d5d4085fda21fe50dc0c362b4Mathias Agopian
180efc7ab6dcea8c22ddd7c0259ef4ab4bbf1e93044Jamie Gennis    // getCurrentTransform returns the transform of the current buffer.
1817a042bf324fe3f3d5d4085fda21fe50dc0c362b4Mathias Agopian    uint32_t getCurrentTransform() const;
1827a042bf324fe3f3d5d4085fda21fe50dc0c362b4Mathias Agopian
183efc7ab6dcea8c22ddd7c0259ef4ab4bbf1e93044Jamie Gennis    // getCurrentScalingMode returns the scaling mode of the current buffer.
1847734ebfe47f42f980c1b44c1f284a91d8ad1d6c7Mathias Agopian    uint32_t getCurrentScalingMode() const;
1857734ebfe47f42f980c1b44c1f284a91d8ad1d6c7Mathias Agopian
186dc5b485f74edf2d2f31c62054eb6c180421a3adeJesse Hall    // getCurrentFence returns the fence indicating when the current buffer is
187dc5b485f74edf2d2f31c62054eb6c180421a3adeJesse Hall    // ready to be read from.
188dc5b485f74edf2d2f31c62054eb6c180421a3adeJesse Hall    sp<Fence> getCurrentFence() const;
189dc5b485f74edf2d2f31c62054eb6c180421a3adeJesse Hall
19061e04b92bdeafc6fca89052d14dab1bd0c384a71Jamie Gennis    // doGLFenceWait inserts a wait command into the OpenGL ES command stream
19161e04b92bdeafc6fca89052d14dab1bd0c384a71Jamie Gennis    // to ensure that it is safe for future OpenGL ES commands to access the
192bf974abe92f7495529916fe0f483f3b56e7c30e3Andy McFadden    // current texture buffer.
19361e04b92bdeafc6fca89052d14dab1bd0c384a71Jamie Gennis    status_t doGLFenceWait() const;
19461e04b92bdeafc6fca89052d14dab1bd0c384a71Jamie Gennis
1952adaf04fab35cf47c824d74d901b54094e01ccd3Andy McFadden    // set the name of the GLConsumer that will be used to identify it in
196fa28c35c21d1bf8b38f541758c291bc17a2d7270Jamie Gennis    // log messages.
197fa28c35c21d1bf8b38f541758c291bc17a2d7270Jamie Gennis    void setName(const String8& name);
198fa28c35c21d1bf8b38f541758c291bc17a2d7270Jamie Gennis
199b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam    // These functions call the corresponding BufferQueue implementation
200b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam    // so the refactoring can proceed smoothly
201d723bd7669b4fc88dc282d8bf8ba5ecb2849d22fDan Stoza    status_t setDefaultBufferFormat(PixelFormat defaultFormat);
2025b75a513e431c097ae704cba2f7affa6bfaecec9Eino-Ville Talvala    status_t setDefaultBufferDataSpace(android_dataspace defaultDataSpace);
203b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam    status_t setConsumerUsageBits(uint32_t usage);
204b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam    status_t setTransformHint(uint32_t hint);
20519e3e06e3c65a7c001a6fe0971744ba5ff536515Pablo Ceballos    status_t setMaxAcquiredBufferCount(int maxAcquiredBuffers);
206b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam
2072adaf04fab35cf47c824d74d901b54094e01ccd3Andy McFadden    // detachFromContext detaches the GLConsumer from the calling thread's
20874bed55fff0132be319bcd1703970516ae28b3a9Jamie Gennis    // current OpenGL ES context.  This context must be the same as the context
20974bed55fff0132be319bcd1703970516ae28b3a9Jamie Gennis    // that was current for previous calls to updateTexImage.
21074bed55fff0132be319bcd1703970516ae28b3a9Jamie Gennis    //
2112adaf04fab35cf47c824d74d901b54094e01ccd3Andy McFadden    // Detaching a GLConsumer from an OpenGL ES context will result in the
21274bed55fff0132be319bcd1703970516ae28b3a9Jamie Gennis    // deletion of the OpenGL ES texture object into which the images were being
2132adaf04fab35cf47c824d74d901b54094e01ccd3Andy McFadden    // streamed.  After a GLConsumer has been detached from the OpenGL ES
21474bed55fff0132be319bcd1703970516ae28b3a9Jamie Gennis    // context calls to updateTexImage will fail returning INVALID_OPERATION
2152adaf04fab35cf47c824d74d901b54094e01ccd3Andy McFadden    // until the GLConsumer is attached to a new OpenGL ES context using the
21674bed55fff0132be319bcd1703970516ae28b3a9Jamie Gennis    // attachToContext method.
21774bed55fff0132be319bcd1703970516ae28b3a9Jamie Gennis    status_t detachFromContext();
21874bed55fff0132be319bcd1703970516ae28b3a9Jamie Gennis
2192adaf04fab35cf47c824d74d901b54094e01ccd3Andy McFadden    // attachToContext attaches a GLConsumer that is currently in the
2202adaf04fab35cf47c824d74d901b54094e01ccd3Andy McFadden    // 'detached' state to the current OpenGL ES context.  A GLConsumer is
22174bed55fff0132be319bcd1703970516ae28b3a9Jamie Gennis    // in the 'detached' state iff detachFromContext has successfully been
22274bed55fff0132be319bcd1703970516ae28b3a9Jamie Gennis    // called and no calls to attachToContext have succeeded since the last
22374bed55fff0132be319bcd1703970516ae28b3a9Jamie Gennis    // detachFromContext call.  Calls to attachToContext made on a
2242adaf04fab35cf47c824d74d901b54094e01ccd3Andy McFadden    // GLConsumer that is not in the 'detached' state will result in an
22574bed55fff0132be319bcd1703970516ae28b3a9Jamie Gennis    // INVALID_OPERATION error.
22674bed55fff0132be319bcd1703970516ae28b3a9Jamie Gennis    //
22774bed55fff0132be319bcd1703970516ae28b3a9Jamie Gennis    // The tex argument specifies the OpenGL ES texture object name in the
22874bed55fff0132be319bcd1703970516ae28b3a9Jamie Gennis    // new context into which the image contents will be streamed.  A successful
22974bed55fff0132be319bcd1703970516ae28b3a9Jamie Gennis    // call to attachToContext will result in this texture object being bound to
23074bed55fff0132be319bcd1703970516ae28b3a9Jamie Gennis    // the texture target and populated with the image contents that were
23174bed55fff0132be319bcd1703970516ae28b3a9Jamie Gennis    // current at the time of the last call to detachFromContext.
2323f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian    status_t attachToContext(uint32_t tex);
23374bed55fff0132be319bcd1703970516ae28b3a9Jamie Gennis
234ce7a6c0fc9d75b80da030d1790321e84475f956aJamie Gennisprotected:
235ed059a8d754770c3cf28b78dba30f7a6ba475dbeJamie Gennis
2369fea3421ffddf6480f57f55a25936a886043d909Jamie Gennis    // abandonLocked overrides the ConsumerBase method to clear
2375c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner    // mCurrentTextureImage in addition to the ConsumerBase behavior.
2389fea3421ffddf6480f57f55a25936a886043d909Jamie Gennis    virtual void abandonLocked();
2399fea3421ffddf6480f57f55a25936a886043d909Jamie Gennis
2402adaf04fab35cf47c824d74d901b54094e01ccd3Andy McFadden    // dumpLocked overrides the ConsumerBase method to dump GLConsumer-
2419fea3421ffddf6480f57f55a25936a886043d909Jamie Gennis    // specific info in addition to the ConsumerBase behavior.
24274d211ae26a0257c6075a823812e40b55aa1e653Mathias Agopian    virtual void dumpLocked(String8& result, const char* prefix) const;
2439fea3421ffddf6480f57f55a25936a886043d909Jamie Gennis
2449fea3421ffddf6480f57f55a25936a886043d909Jamie Gennis    // acquireBufferLocked overrides the ConsumerBase method to update the
2459fea3421ffddf6480f57f55a25936a886043d909Jamie Gennis    // mEglSlots array in addition to the ConsumerBase behavior.
246a4650a50a0b35e9e4342d6600b6eb24fd94bb8e5Dan Stoza    virtual status_t acquireBufferLocked(BufferItem *item, nsecs_t presentWhen,
247a4650a50a0b35e9e4342d6600b6eb24fd94bb8e5Dan Stoza            uint64_t maxFrameNumber = 0) override;
2489fea3421ffddf6480f57f55a25936a886043d909Jamie Gennis
2499fea3421ffddf6480f57f55a25936a886043d909Jamie Gennis    // releaseBufferLocked overrides the ConsumerBase method to update the
2509fea3421ffddf6480f57f55a25936a886043d909Jamie Gennis    // mEglSlots array in addition to the ConsumerBase.
251c5d7b7d323bba8772a9005f7d300ad983a04733aLajos Molnar    virtual status_t releaseBufferLocked(int slot,
252c5d7b7d323bba8772a9005f7d300ad983a04733aLajos Molnar            const sp<GraphicBuffer> graphicBuffer,
253c5d7b7d323bba8772a9005f7d300ad983a04733aLajos Molnar            EGLDisplay display, EGLSyncKHR eglFence);
254fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis
255c5d7b7d323bba8772a9005f7d300ad983a04733aLajos Molnar    status_t releaseBufferLocked(int slot,
256c5d7b7d323bba8772a9005f7d300ad983a04733aLajos Molnar            const sp<GraphicBuffer> graphicBuffer, EGLSyncKHR eglFence) {
257c5d7b7d323bba8772a9005f7d300ad983a04733aLajos Molnar        return releaseBufferLocked(slot, graphicBuffer, mEglDisplay, eglFence);
258bf974abe92f7495529916fe0f483f3b56e7c30e3Andy McFadden    }
259bf974abe92f7495529916fe0f483f3b56e7c30e3Andy McFadden
260d723bd7669b4fc88dc282d8bf8ba5ecb2849d22fDan Stoza    static bool isExternalFormat(PixelFormat format);
2617a042bf324fe3f3d5d4085fda21fe50dc0c362b4Mathias Agopian
2623ce460440b0ab0b6d6407c9d8735b19f6e3b8408Dan Stoza    struct PendingRelease {
2633ce460440b0ab0b6d6407c9d8735b19f6e3b8408Dan Stoza        PendingRelease() : isPending(false), currentTexture(-1),
2643ce460440b0ab0b6d6407c9d8735b19f6e3b8408Dan Stoza                graphicBuffer(), display(nullptr), fence(nullptr) {}
2653ce460440b0ab0b6d6407c9d8735b19f6e3b8408Dan Stoza
2663ce460440b0ab0b6d6407c9d8735b19f6e3b8408Dan Stoza        bool isPending;
2673ce460440b0ab0b6d6407c9d8735b19f6e3b8408Dan Stoza        int currentTexture;
2683ce460440b0ab0b6d6407c9d8735b19f6e3b8408Dan Stoza        sp<GraphicBuffer> graphicBuffer;
2693ce460440b0ab0b6d6407c9d8735b19f6e3b8408Dan Stoza        EGLDisplay display;
2703ce460440b0ab0b6d6407c9d8735b19f6e3b8408Dan Stoza        EGLSyncKHR fence;
2713ce460440b0ab0b6d6407c9d8735b19f6e3b8408Dan Stoza    };
2723ce460440b0ab0b6d6407c9d8735b19f6e3b8408Dan Stoza
273bf974abe92f7495529916fe0f483f3b56e7c30e3Andy McFadden    // This releases the buffer in the slot referenced by mCurrentTexture,
274bf974abe92f7495529916fe0f483f3b56e7c30e3Andy McFadden    // then updates state to refer to the BufferItem, which must be a
2753ce460440b0ab0b6d6407c9d8735b19f6e3b8408Dan Stoza    // newly-acquired buffer. If pendingRelease is not null, the parameters
2763ce460440b0ab0b6d6407c9d8735b19f6e3b8408Dan Stoza    // which would have been passed to releaseBufferLocked upon the successful
2773ce460440b0ab0b6d6407c9d8735b19f6e3b8408Dan Stoza    // completion of the method will instead be returned to the caller, so that
2783ce460440b0ab0b6d6407c9d8735b19f6e3b8408Dan Stoza    // it may call releaseBufferLocked itself later.
2793ce460440b0ab0b6d6407c9d8735b19f6e3b8408Dan Stoza    status_t updateAndReleaseLocked(const BufferItem& item,
2803ce460440b0ab0b6d6407c9d8735b19f6e3b8408Dan Stoza            PendingRelease* pendingRelease = nullptr);
281bf974abe92f7495529916fe0f483f3b56e7c30e3Andy McFadden
282bf974abe92f7495529916fe0f483f3b56e7c30e3Andy McFadden    // Binds mTexName and the current buffer to mTexTarget.  Uses
2835c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner    // mCurrentTexture if it's set, mCurrentTextureImage if not.  If the
28497eba8904c2f221c42a9473407223a4c3a213f75Andy McFadden    // bind succeeds, this calls doGLFenceWait.
28597eba8904c2f221c42a9473407223a4c3a213f75Andy McFadden    status_t bindTextureImageLocked();
286bf974abe92f7495529916fe0f483f3b56e7c30e3Andy McFadden
287bf974abe92f7495529916fe0f483f3b56e7c30e3Andy McFadden    // Gets the current EGLDisplay and EGLContext values, and compares them
288bf974abe92f7495529916fe0f483f3b56e7c30e3Andy McFadden    // to mEglDisplay and mEglContext.  If the fields have been previously
289bf974abe92f7495529916fe0f483f3b56e7c30e3Andy McFadden    // set, the values must match; if not, the fields are set to the current
290bf974abe92f7495529916fe0f483f3b56e7c30e3Andy McFadden    // values.
29145155969dc747d09d267cd1f22baf0eaf886a801Mathias Agopian    // The contextCheck argument is used to ensure that a GL context is
29245155969dc747d09d267cd1f22baf0eaf886a801Mathias Agopian    // properly set; when set to false, the check is not performed.
29345155969dc747d09d267cd1f22baf0eaf886a801Mathias Agopian    status_t checkAndUpdateEglStateLocked(bool contextCheck = false);
2948ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis
295bf974abe92f7495529916fe0f483f3b56e7c30e3Andy McFaddenprivate:
2965c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner    // EglImage is a utility class for tracking and creating EGLImageKHRs. There
2975c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner    // is primarily just one image per slot, but there is also special cases:
2985c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner    //  - For releaseTexImage, we use a debug image (mReleasedTexImage)
2995c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner    //  - After freeBuffer, we must still keep the current image/buffer
3005c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner    // Reference counting EGLImages lets us handle all these cases easily while
3015c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner    // also only creating new EGLImages from buffers when required.
3025c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner    class EglImage : public LightRefBase<EglImage>  {
3035c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner    public:
3045c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner        EglImage(sp<GraphicBuffer> graphicBuffer);
3055c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner
3065c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner        // createIfNeeded creates an EGLImage if required (we haven't created
3075c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner        // one yet, or the EGLDisplay or crop-rect has changed).
3082d14a0ed4f5861bfa67e9db138ffdc70d2d5e6e4Eric Penner        status_t createIfNeeded(EGLDisplay display,
3092d14a0ed4f5861bfa67e9db138ffdc70d2d5e6e4Eric Penner                                const Rect& cropRect,
3102d14a0ed4f5861bfa67e9db138ffdc70d2d5e6e4Eric Penner                                bool forceCreate = false);
3115c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner
3125c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner        // This calls glEGLImageTargetTexture2DOES to bind the image to the
3135c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner        // texture in the specified texture target.
3145c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner        void bindToTextureTarget(uint32_t texTarget);
3155c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner
3165c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner        const sp<GraphicBuffer>& graphicBuffer() { return mGraphicBuffer; }
3175c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner        const native_handle* graphicBufferHandle() {
3185c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner            return mGraphicBuffer == NULL ? NULL : mGraphicBuffer->handle;
3195c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner        }
3205c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner
3215c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner    private:
3225c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner        // Only allow instantiation using ref counting.
3235c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner        friend class LightRefBase<EglImage>;
3245c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner        virtual ~EglImage();
3255c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner
3265c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner        // createImage creates a new EGLImage from a GraphicBuffer.
3275c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner        EGLImageKHR createImage(EGLDisplay dpy,
3285c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner                const sp<GraphicBuffer>& graphicBuffer, const Rect& crop);
3295c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner
3305c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner        // Disallow copying
3315c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner        EglImage(const EglImage& rhs);
3325c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner        void operator = (const EglImage& rhs);
3335c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner
3345c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner        // mGraphicBuffer is the buffer that was used to create this image.
3355c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner        sp<GraphicBuffer> mGraphicBuffer;
3365c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner
3375c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner        // mEglImage is the EGLImage created from mGraphicBuffer.
3385c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner        EGLImageKHR mEglImage;
3395c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner
3405c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner        // mEGLDisplay is the EGLDisplay that was used to create mEglImage.
3415c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner        EGLDisplay mEglDisplay;
3428ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis
3435c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner        // mCropRect is the crop rectangle passed to EGL when mEglImage
3445c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner        // was created.
3455c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner        Rect mCropRect;
3465c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner    };
3475c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner
3485c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner    // freeBufferLocked frees up the given buffer slot. If the slot has been
349fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    // initialized this will release the reference to the GraphicBuffer in that
350fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    // slot and destroy the EGLImage in that slot.  Otherwise it has no effect.
351fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    //
352fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    // This method must be called with mMutex locked.
3539fea3421ffddf6480f57f55a25936a886043d909Jamie Gennis    virtual void freeBufferLocked(int slotIndex);
354fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis
355e96e9e1093b5700e9f403a6e2479da7dc36d3b71Mathias Agopian    // computeCurrentTransformMatrixLocked computes the transform matrix for the
356736aa9573bb7b78f9c315f396c104491b3639426Jamie Gennis    // current texture.  It uses mCurrentTransform and the current GraphicBuffer
357736aa9573bb7b78f9c315f396c104491b3639426Jamie Gennis    // to compute this matrix and stores it in mCurrentTransformMatrix.
3585c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner    // mCurrentTextureImage must not be NULL.
359e96e9e1093b5700e9f403a6e2479da7dc36d3b71Mathias Agopian    void computeCurrentTransformMatrixLocked();
360736aa9573bb7b78f9c315f396c104491b3639426Jamie Gennis
36197eba8904c2f221c42a9473407223a4c3a213f75Andy McFadden    // doGLFenceWaitLocked inserts a wait command into the OpenGL ES command
36297eba8904c2f221c42a9473407223a4c3a213f75Andy McFadden    // stream to ensure that it is safe for future OpenGL ES commands to
36397eba8904c2f221c42a9473407223a4c3a213f75Andy McFadden    // access the current texture buffer.
36497eba8904c2f221c42a9473407223a4c3a213f75Andy McFadden    status_t doGLFenceWaitLocked() const;
36597eba8904c2f221c42a9473407223a4c3a213f75Andy McFadden
36674bed55fff0132be319bcd1703970516ae28b3a9Jamie Gennis    // syncForReleaseLocked performs the synchronization needed to release the
36774bed55fff0132be319bcd1703970516ae28b3a9Jamie Gennis    // current slot from an OpenGL ES context.  If needed it will set the
36874bed55fff0132be319bcd1703970516ae28b3a9Jamie Gennis    // current slot's fence to guard against a producer accessing the buffer
36974bed55fff0132be319bcd1703970516ae28b3a9Jamie Gennis    // before the outstanding accesses have completed.
37074bed55fff0132be319bcd1703970516ae28b3a9Jamie Gennis    status_t syncForReleaseLocked(EGLDisplay dpy);
37174bed55fff0132be319bcd1703970516ae28b3a9Jamie Gennis
3729870c9b66cc73ee31aabba23aa06deaf673ee5efMathias Agopian    // returns a graphic buffer used when the texture image has been released
3739870c9b66cc73ee31aabba23aa06deaf673ee5efMathias Agopian    static sp<GraphicBuffer> getDebugTexImageBuffer();
3749870c9b66cc73ee31aabba23aa06deaf673ee5efMathias Agopian
3752adaf04fab35cf47c824d74d901b54094e01ccd3Andy McFadden    // The default consumer usage flags that GLConsumer always sets on its
37685b217668d6840c8e6a109adfb99461313676f8dEino-Ville Talvala    // BufferQueue instance; these will be OR:d with any additional flags passed
3772adaf04fab35cf47c824d74d901b54094e01ccd3Andy McFadden    // from the GLConsumer user. In particular, GLConsumer will always
37885b217668d6840c8e6a109adfb99461313676f8dEino-Ville Talvala    // consume buffers as hardware textures.
37985b217668d6840c8e6a109adfb99461313676f8dEino-Ville Talvala    static const uint32_t DEFAULT_USAGE_FLAGS = GraphicBuffer::USAGE_HW_TEXTURE;
38085b217668d6840c8e6a109adfb99461313676f8dEino-Ville Talvala
3815c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner    // mCurrentTextureImage is the EglImage/buffer of the current texture. It's
3829a78c90cd46b2a3bd637b056873149d3b94384b4Jamie Gennis    // possible that this buffer is not associated with any buffer slot, so we
38329c870271e8d3f8c40c356283650ba54fe71a16bJamie Gennis    // must track it separately in order to support the getCurrentBuffer method.
3845c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner    sp<EglImage> mCurrentTextureImage;
3859a78c90cd46b2a3bd637b056873149d3b94384b4Jamie Gennis
386f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennis    // mCurrentCrop is the crop rectangle that applies to the current texture.
3877734ebfe47f42f980c1b44c1f284a91d8ad1d6c7Mathias Agopian    // It gets set each time updateTexImage is called.
388f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennis    Rect mCurrentCrop;
389f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennis
390f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennis    // mCurrentTransform is the transform identifier for the current texture. It
3917734ebfe47f42f980c1b44c1f284a91d8ad1d6c7Mathias Agopian    // gets set each time updateTexImage is called.
392f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennis    uint32_t mCurrentTransform;
393f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennis
3947734ebfe47f42f980c1b44c1f284a91d8ad1d6c7Mathias Agopian    // mCurrentScalingMode is the scaling mode for the current texture. It gets
395e692ab9a6be63193c5b52a6562d85d06c40463b8Mathias Agopian    // set each time updateTexImage is called.
3967734ebfe47f42f980c1b44c1f284a91d8ad1d6c7Mathias Agopian    uint32_t mCurrentScalingMode;
3977734ebfe47f42f980c1b44c1f284a91d8ad1d6c7Mathias Agopian
398dc5b485f74edf2d2f31c62054eb6c180421a3adeJesse Hall    // mCurrentFence is the fence received from BufferQueue in updateTexImage.
399dc5b485f74edf2d2f31c62054eb6c180421a3adeJesse Hall    sp<Fence> mCurrentFence;
400dc5b485f74edf2d2f31c62054eb6c180421a3adeJesse Hall
401736aa9573bb7b78f9c315f396c104491b3639426Jamie Gennis    // mCurrentTransformMatrix is the transform matrix for the current texture.
402736aa9573bb7b78f9c315f396c104491b3639426Jamie Gennis    // It gets computed by computeTransformMatrix each time updateTexImage is
403736aa9573bb7b78f9c315f396c104491b3639426Jamie Gennis    // called.
404736aa9573bb7b78f9c315f396c104491b3639426Jamie Gennis    float mCurrentTransformMatrix[16];
405736aa9573bb7b78f9c315f396c104491b3639426Jamie Gennis
4061d01a12e7150be569557b64da9b8663c62c13594Eino-Ville Talvala    // mCurrentTimestamp is the timestamp for the current texture. It
4077734ebfe47f42f980c1b44c1f284a91d8ad1d6c7Mathias Agopian    // gets set each time updateTexImage is called.
4081d01a12e7150be569557b64da9b8663c62c13594Eino-Ville Talvala    int64_t mCurrentTimestamp;
4091d01a12e7150be569557b64da9b8663c62c13594Eino-Ville Talvala
410d171da973de3c6b30263011334fdcd916739144fEino-Ville Talvala    // mCurrentFrameNumber is the frame counter for the current texture.
411d171da973de3c6b30263011334fdcd916739144fEino-Ville Talvala    // It gets set each time updateTexImage is called.
412d723bd7669b4fc88dc282d8bf8ba5ecb2849d22fDan Stoza    uint64_t mCurrentFrameNumber;
413d171da973de3c6b30263011334fdcd916739144fEino-Ville Talvala
414016c8cbce4dde21f2703b9865f52d16b8d5d5ae2Daniel Lam    uint32_t mDefaultWidth, mDefaultHeight;
415016c8cbce4dde21f2703b9865f52d16b8d5d5ae2Daniel Lam
4165c1139fea3cc0fd9847a6594d853a458152b2fbcJamie Gennis    // mFilteringEnabled indicates whether the transform matrix is computed for
4175c1139fea3cc0fd9847a6594d853a458152b2fbcJamie Gennis    // use with bilinear filtering. It defaults to true and is changed by
4185c1139fea3cc0fd9847a6594d853a458152b2fbcJamie Gennis    // setFilteringEnabled().
4195c1139fea3cc0fd9847a6594d853a458152b2fbcJamie Gennis    bool mFilteringEnabled;
4205c1139fea3cc0fd9847a6594d853a458152b2fbcJamie Gennis
4218ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis    // mTexName is the name of the OpenGL texture to which streamed images will
422292a31a4c2ae2f6faf134e8e4a726583017dad06Pannag Sanketi    // be bound when updateTexImage is called. It is set at construction time
42374bed55fff0132be319bcd1703970516ae28b3a9Jamie Gennis    // and can be changed with a call to attachToContext.
4243f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian    uint32_t mTexName;
4258ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis
42686edf4f6470ee0f108bf40d3c1d23bf0a78c9c38Jamie Gennis    // mUseFenceSync indicates whether creation of the EGL_KHR_fence_sync
42786edf4f6470ee0f108bf40d3c1d23bf0a78c9c38Jamie Gennis    // extension should be used to prevent buffers from being dequeued before
42886edf4f6470ee0f108bf40d3c1d23bf0a78c9c38Jamie Gennis    // it's safe for them to be written. It gets set at construction time and
42986edf4f6470ee0f108bf40d3c1d23bf0a78c9c38Jamie Gennis    // never changes.
43086edf4f6470ee0f108bf40d3c1d23bf0a78c9c38Jamie Gennis    const bool mUseFenceSync;
43186edf4f6470ee0f108bf40d3c1d23bf0a78c9c38Jamie Gennis
432fb1b5a2f333800574b0da435d1200cf9b13d723fJamie Gennis    // mTexTarget is the GL texture target with which the GL texture object is
433fb1b5a2f333800574b0da435d1200cf9b13d723fJamie Gennis    // associated.  It is set in the constructor and never changed.  It is
434fb1b5a2f333800574b0da435d1200cf9b13d723fJamie Gennis    // almost always GL_TEXTURE_EXTERNAL_OES except for one use case in Android
435fb1b5a2f333800574b0da435d1200cf9b13d723fJamie Gennis    // Browser.  In that case it is set to GL_TEXTURE_2D to allow
436fb1b5a2f333800574b0da435d1200cf9b13d723fJamie Gennis    // glCopyTexSubImage to read from the texture.  This is a hack to work
437fb1b5a2f333800574b0da435d1200cf9b13d723fJamie Gennis    // around a GL driver limitation on the number of FBO attachments, which the
438fb1b5a2f333800574b0da435d1200cf9b13d723fJamie Gennis    // browser's tile cache exceeds.
4393f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian    const uint32_t mTexTarget;
440a929748ddb67cbece3337c7fda7877fdeb973aa4Sunita Nadampalli
441fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    // EGLSlot contains the information and object references that
4422adaf04fab35cf47c824d74d901b54094e01ccd3Andy McFadden    // GLConsumer maintains about a BufferQueue buffer slot.
443bf974abe92f7495529916fe0f483f3b56e7c30e3Andy McFadden    struct EglSlot {
4445c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner        EglSlot() : mEglFence(EGL_NO_SYNC_KHR) {}
445eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam
446eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam        // mEglImage is the EGLImage created from mGraphicBuffer.
4475c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner        sp<EglImage> mEglImage;
448dbe9245e2e362b12e184cd33b9a27b0901f81244Jamie Gennis
449eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam        // mFence is the EGL sync object that must signal before the buffer
450eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam        // associated with this buffer slot may be dequeued. It is initialized
451eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam        // to EGL_NO_SYNC_KHR when the buffer is created and (optionally, based
452eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam        // on a compile-time option) set to a new sync object in updateTexImage.
4539fea3421ffddf6480f57f55a25936a886043d909Jamie Gennis        EGLSyncKHR mEglFence;
454eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    };
455eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam
4562adaf04fab35cf47c824d74d901b54094e01ccd3Andy McFadden    // mEglDisplay is the EGLDisplay with which this GLConsumer is currently
457ce561372186c7549a8a5fe996ac5965cda087007Jamie Gennis    // associated.  It is intialized to EGL_NO_DISPLAY and gets set to the
45874bed55fff0132be319bcd1703970516ae28b3a9Jamie Gennis    // current display when updateTexImage is called for the first time and when
45974bed55fff0132be319bcd1703970516ae28b3a9Jamie Gennis    // attachToContext is called.
460ce561372186c7549a8a5fe996ac5965cda087007Jamie Gennis    EGLDisplay mEglDisplay;
461ce561372186c7549a8a5fe996ac5965cda087007Jamie Gennis
4622adaf04fab35cf47c824d74d901b54094e01ccd3Andy McFadden    // mEglContext is the OpenGL ES context with which this GLConsumer is
463ce561372186c7549a8a5fe996ac5965cda087007Jamie Gennis    // currently associated.  It is initialized to EGL_NO_CONTEXT and gets set
464ce561372186c7549a8a5fe996ac5965cda087007Jamie Gennis    // to the current GL context when updateTexImage is called for the first
46574bed55fff0132be319bcd1703970516ae28b3a9Jamie Gennis    // time and when attachToContext is called.
466ce561372186c7549a8a5fe996ac5965cda087007Jamie Gennis    EGLContext mEglContext;
467ce561372186c7549a8a5fe996ac5965cda087007Jamie Gennis
468fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    // mEGLSlots stores the buffers that have been allocated by the BufferQueue
469fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    // for each buffer slot.  It is initialized to null pointers, and gets
470fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    // filled in with the result of BufferQueue::acquire when the
471fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    // client dequeues a buffer from a
472fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    // slot that has not yet been used. The buffer allocated to a slot will also
473fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    // be replaced if the requested buffer usage or geometry differs from that
474fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    // of the buffer allocated to a slot.
475bf974abe92f7495529916fe0f483f3b56e7c30e3Andy McFadden    EglSlot mEglSlots[BufferQueue::NUM_BUFFER_SLOTS];
476eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam
477eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    // mCurrentTexture is the buffer slot index of the buffer that is currently
478eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    // bound to the OpenGL texture. It is initialized to INVALID_BUFFER_SLOT,
479eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    // indicating that no buffer slot is currently bound to the texture. Note,
480eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    // however, that a value of INVALID_BUFFER_SLOT does not necessarily mean
481eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    // that no buffer is bound to the texture. A call to setBufferCount will
482eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    // reset mCurrentTexture to INVALID_BUFFER_SLOT.
483eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    int mCurrentTexture;
484eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam
4859fea3421ffddf6480f57f55a25936a886043d909Jamie Gennis    // mAttached indicates whether the ConsumerBase is currently attached to
48674bed55fff0132be319bcd1703970516ae28b3a9Jamie Gennis    // an OpenGL ES context.  For legacy reasons, this is initialized to true,
4879fea3421ffddf6480f57f55a25936a886043d909Jamie Gennis    // indicating that the ConsumerBase is considered to be attached to
48874bed55fff0132be319bcd1703970516ae28b3a9Jamie Gennis    // whatever context is current at the time of the first updateTexImage call.
48974bed55fff0132be319bcd1703970516ae28b3a9Jamie Gennis    // It is set to false by detachFromContext, and then set to true again by
49074bed55fff0132be319bcd1703970516ae28b3a9Jamie Gennis    // attachToContext.
49174bed55fff0132be319bcd1703970516ae28b3a9Jamie Gennis    bool mAttached;
492ad678e18b66f495efa78dc3b9ab99b579945c9e2Mathias Agopian
4939870c9b66cc73ee31aabba23aa06deaf673ee5efMathias Agopian    // protects static initialization
4949870c9b66cc73ee31aabba23aa06deaf673ee5efMathias Agopian    static Mutex sStaticInitLock;
4959870c9b66cc73ee31aabba23aa06deaf673ee5efMathias Agopian
496ad678e18b66f495efa78dc3b9ab99b579945c9e2Mathias Agopian    // mReleasedTexImageBuffer is a dummy buffer used when in single buffer
497ad678e18b66f495efa78dc3b9ab99b579945c9e2Mathias Agopian    // mode and releaseTexImage() has been called
4989870c9b66cc73ee31aabba23aa06deaf673ee5efMathias Agopian    static sp<GraphicBuffer> sReleasedTexImageBuffer;
4995c3d243fcca6d0678bcbb0461bbbe0b63f35212cEric Penner    sp<EglImage> mReleasedTexImage;
5008ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis};
5018ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis
5028ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis// ----------------------------------------------------------------------------
5038ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis}; // namespace android
5048ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis
5052adaf04fab35cf47c824d74d901b54094e01ccd3Andy McFadden#endif // ANDROID_GUI_CONSUMER_H
506