GLConsumer.h revision fa5b40ebb8923133df12dc70591bfe35b3f1c9b3
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
178ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis#ifndef ANDROID_GUI_SURFACETEXTURE_H
188ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis#define ANDROID_GUI_SURFACETEXTURE_H
198ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis
208ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis#include <EGL/egl.h>
218ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis#include <EGL/eglext.h>
228ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis#include <GLES2/gl2.h>
23fb1b5a2f333800574b0da435d1200cf9b13d723fJamie Gennis#include <GLES2/gl2ext.h>
248ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis
258ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis#include <gui/ISurfaceTexture.h>
266b091c53000c843211c218ce40287a7edca9bc63Daniel Lam#include <gui/BufferQueue.h>
278ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis
288ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis#include <ui/GraphicBuffer.h>
298ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis
30fa28c35c21d1bf8b38f541758c291bc17a2d7270Jamie Gennis#include <utils/String8.h>
319a78c90cd46b2a3bd637b056873149d3b94384b4Jamie Gennis#include <utils/Vector.h>
32fa28c35c21d1bf8b38f541758c291bc17a2d7270Jamie Gennis#include <utils/threads.h>
338ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis
348ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis#define ANDROID_GRAPHICS_SURFACETEXTURE_JNI_ID "mSurfaceTexture"
358ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis
368ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennisnamespace android {
378ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis// ----------------------------------------------------------------------------
388ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis
396b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
4068c7794183a7dbfe3b20d4ce832f8eb79c2c619aMathias Agopianclass String8;
419a78c90cd46b2a3bd637b056873149d3b94384b4Jamie Gennis
42fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennisclass SurfaceTexture : public virtual RefBase,
43fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis        protected BufferQueue::ConsumerListener {
448ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennispublic:
45fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    struct FrameAvailableListener : public virtual RefBase {
46fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis        // onFrameAvailable() is called each time an additional frame becomes
47fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis        // available for consumption. This means that frames that are queued
48fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis        // while in asynchronous mode only trigger the callback if no previous
49fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis        // frames are pending. Frames queued while in synchronous mode always
50fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis        // trigger the callback.
51fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis        //
52fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis        // This is called without any lock held and can be called concurrently
53fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis        // by multiple threads.
54fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis        virtual void onFrameAvailable() = 0;
55fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    };
56c4d4aeab52435c177ded6abdd578fec8191f0f5dJamie Gennis
5786edf4f6470ee0f108bf40d3c1d23bf0a78c9c38Jamie Gennis    // SurfaceTexture constructs a new SurfaceTexture object. tex indicates the
5886edf4f6470ee0f108bf40d3c1d23bf0a78c9c38Jamie Gennis    // name of the OpenGL ES texture to which images are to be streamed. This
5986edf4f6470ee0f108bf40d3c1d23bf0a78c9c38Jamie Gennis    // texture name cannot be changed once the SurfaceTexture is created.
6086edf4f6470ee0f108bf40d3c1d23bf0a78c9c38Jamie Gennis    // allowSynchronousMode specifies whether or not synchronous mode can be
6186edf4f6470ee0f108bf40d3c1d23bf0a78c9c38Jamie Gennis    // enabled. texTarget specifies the OpenGL ES texture target to which the
6286edf4f6470ee0f108bf40d3c1d23bf0a78c9c38Jamie Gennis    // texture will be bound in updateTexImage. useFenceSync specifies whether
6386edf4f6470ee0f108bf40d3c1d23bf0a78c9c38Jamie Gennis    // fences should be used to synchronize access to buffers if that behavior
64b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam    // is enabled at compile-time. A custom bufferQueue can be specified
65b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam    // if behavior for queue/dequeue/connect etc needs to be customized.
66b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam    // Otherwise a default BufferQueue will be created and used.
67fb1b5a2f333800574b0da435d1200cf9b13d723fJamie Gennis    SurfaceTexture(GLuint tex, bool allowSynchronousMode = true,
68b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam            GLenum texTarget = GL_TEXTURE_EXTERNAL_OES, bool useFenceSync = true,
69b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam            const sp<BufferQueue> &bufferQueue = 0);
708ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis
718ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis    virtual ~SurfaceTexture();
728ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis
738ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis    // updateTexImage sets the image contents of the target texture to that of
748ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis    // the most recently queued buffer.
758ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis    //
768ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis    // This call may only be made while the OpenGL ES context to which the
778ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis    // target texture belongs is bound to the calling thread.
788ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis    status_t updateTexImage();
798ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis
808072711307aa98ee5ee6f7369860ae38c3e19656Mathias Agopian    // setBufferCountServer set the buffer count. If the client has requested
818072711307aa98ee5ee6f7369860ae38c3e19656Mathias Agopian    // a buffer count using setBufferCount, the server-buffer count will
828072711307aa98ee5ee6f7369860ae38c3e19656Mathias Agopian    // take effect once the client sets the count back to zero.
838072711307aa98ee5ee6f7369860ae38c3e19656Mathias Agopian    status_t setBufferCountServer(int bufferCount);
848072711307aa98ee5ee6f7369860ae38c3e19656Mathias Agopian
85f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennis    // getTransformMatrix retrieves the 4x4 texture coordinate transform matrix
86f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennis    // associated with the texture image set by the most recent call to
87f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennis    // updateTexImage.
88f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennis    //
89f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennis    // This transform matrix maps 2D homogeneous texture coordinates of the form
90f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennis    // (s, t, 0, 1) with s and t in the inclusive range [0, 1] to the texture
91f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennis    // coordinate that should be used to sample that location from the texture.
92f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennis    // Sampling the texture outside of the range of this transform is undefined.
93f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennis    //
94f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennis    // This transform is necessary to compensate for transforms that the stream
95f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennis    // content producer may implicitly apply to the content. By forcing users of
96f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennis    // a SurfaceTexture to apply this transform we avoid performing an extra
97f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennis    // copy of the data that would be needed to hide the transform from the
98f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennis    // user.
99f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennis    //
100f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennis    // The matrix is stored in column-major order so that it may be passed
101f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennis    // directly to OpenGL ES via the glLoadMatrixf or glUniformMatrix4fv
102f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennis    // functions.
103f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennis    void getTransformMatrix(float mtx[16]);
104f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennis
1051d01a12e7150be569557b64da9b8663c62c13594Eino-Ville Talvala    // getTimestamp retrieves the timestamp associated with the texture image
1061d01a12e7150be569557b64da9b8663c62c13594Eino-Ville Talvala    // set by the most recent call to updateTexImage.
1071d01a12e7150be569557b64da9b8663c62c13594Eino-Ville Talvala    //
1081d01a12e7150be569557b64da9b8663c62c13594Eino-Ville Talvala    // The timestamp is in nanoseconds, and is monotonically increasing. Its
1091d01a12e7150be569557b64da9b8663c62c13594Eino-Ville Talvala    // other semantics (zero point, etc) are source-dependent and should be
1101d01a12e7150be569557b64da9b8663c62c13594Eino-Ville Talvala    // documented by the source.
1111d01a12e7150be569557b64da9b8663c62c13594Eino-Ville Talvala    int64_t getTimestamp();
1121d01a12e7150be569557b64da9b8663c62c13594Eino-Ville Talvala
113c4d4aeab52435c177ded6abdd578fec8191f0f5dJamie Gennis    // setFrameAvailableListener sets the listener object that will be notified
114c4d4aeab52435c177ded6abdd578fec8191f0f5dJamie Gennis    // when a new frame becomes available.
115292a31a4c2ae2f6faf134e8e4a726583017dad06Pannag Sanketi    void setFrameAvailableListener(const sp<FrameAvailableListener>& listener);
116c4d4aeab52435c177ded6abdd578fec8191f0f5dJamie Gennis
1171b20cde313b5ef8acdace742328df867956d24cbJamie Gennis    // getAllocator retrieves the binder object that must be referenced as long
1181b20cde313b5ef8acdace742328df867956d24cbJamie Gennis    // as the GraphicBuffers dequeued from this SurfaceTexture are referenced.
1191b20cde313b5ef8acdace742328df867956d24cbJamie Gennis    // Holding this binder reference prevents SurfaceFlinger from freeing the
1201b20cde313b5ef8acdace742328df867956d24cbJamie Gennis    // buffers before the client is done with them.
1211b20cde313b5ef8acdace742328df867956d24cbJamie Gennis    sp<IBinder> getAllocator();
1221b20cde313b5ef8acdace742328df867956d24cbJamie Gennis
123a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian    // setDefaultBufferSize is used to set the size of buffers returned by
124a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian    // requestBuffers when a with and height of zero is requested.
125a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian    // A call to setDefaultBufferSize() may trigger requestBuffers() to
126a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian    // be called from the client.
127194c76c0477189700fda068e19b953b1d9af201aMathias Agopian    // The width and height parameters must be no greater than the minimum of
128194c76c0477189700fda068e19b953b1d9af201aMathias Agopian    // GL_MAX_VIEWPORT_DIMS and GL_MAX_TEXTURE_SIZE (see: glGetIntegerv).
129194c76c0477189700fda068e19b953b1d9af201aMathias Agopian    // An error due to invalid dimensions might not be reported until
130194c76c0477189700fda068e19b953b1d9af201aMathias Agopian    // updateTexImage() is called.
131194c76c0477189700fda068e19b953b1d9af201aMathias Agopian    status_t setDefaultBufferSize(uint32_t width, uint32_t height);
132a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian
1337a042bf324fe3f3d5d4085fda21fe50dc0c362b4Mathias Agopian    // getCurrentBuffer returns the buffer associated with the current image.
1347a042bf324fe3f3d5d4085fda21fe50dc0c362b4Mathias Agopian    sp<GraphicBuffer> getCurrentBuffer() const;
1357a042bf324fe3f3d5d4085fda21fe50dc0c362b4Mathias Agopian
1367a042bf324fe3f3d5d4085fda21fe50dc0c362b4Mathias Agopian    // getCurrentTextureTarget returns the texture target of the current
1377a042bf324fe3f3d5d4085fda21fe50dc0c362b4Mathias Agopian    // texture as returned by updateTexImage().
1387a042bf324fe3f3d5d4085fda21fe50dc0c362b4Mathias Agopian    GLenum getCurrentTextureTarget() const;
1397a042bf324fe3f3d5d4085fda21fe50dc0c362b4Mathias Agopian
1407a042bf324fe3f3d5d4085fda21fe50dc0c362b4Mathias Agopian    // getCurrentCrop returns the cropping rectangle of the current buffer
1417a042bf324fe3f3d5d4085fda21fe50dc0c362b4Mathias Agopian    Rect getCurrentCrop() const;
1427a042bf324fe3f3d5d4085fda21fe50dc0c362b4Mathias Agopian
1437a042bf324fe3f3d5d4085fda21fe50dc0c362b4Mathias Agopian    // getCurrentTransform returns the transform of the current buffer
1447a042bf324fe3f3d5d4085fda21fe50dc0c362b4Mathias Agopian    uint32_t getCurrentTransform() const;
1457a042bf324fe3f3d5d4085fda21fe50dc0c362b4Mathias Agopian
1467734ebfe47f42f980c1b44c1f284a91d8ad1d6c7Mathias Agopian    // getCurrentScalingMode returns the scaling mode of the current buffer
1477734ebfe47f42f980c1b44c1f284a91d8ad1d6c7Mathias Agopian    uint32_t getCurrentScalingMode() const;
1487734ebfe47f42f980c1b44c1f284a91d8ad1d6c7Mathias Agopian
14959769469e4b9b2d8b12c020eb44b030b3927a50bJamie Gennis    // isSynchronousMode returns whether the SurfaceTexture is currently in
15059769469e4b9b2d8b12c020eb44b030b3927a50bJamie Gennis    // synchronous mode.
15159769469e4b9b2d8b12c020eb44b030b3927a50bJamie Gennis    bool isSynchronousMode() const;
15259769469e4b9b2d8b12c020eb44b030b3927a50bJamie Gennis
1537b305fffc39d0fe0926e7fd2d7f6a524fbce62b7Jamie Gennis    // abandon frees all the buffers and puts the SurfaceTexture into the
1547b305fffc39d0fe0926e7fd2d7f6a524fbce62b7Jamie Gennis    // 'abandoned' state.  Once put in this state the SurfaceTexture can never
1557b305fffc39d0fe0926e7fd2d7f6a524fbce62b7Jamie Gennis    // leave it.  When in the 'abandoned' state, all methods of the
1567b305fffc39d0fe0926e7fd2d7f6a524fbce62b7Jamie Gennis    // ISurfaceTexture interface will fail with the NO_INIT error.
1577b305fffc39d0fe0926e7fd2d7f6a524fbce62b7Jamie Gennis    //
1587b305fffc39d0fe0926e7fd2d7f6a524fbce62b7Jamie Gennis    // Note that while calling this method causes all the buffers to be freed
1597b305fffc39d0fe0926e7fd2d7f6a524fbce62b7Jamie Gennis    // from the perspective of the the SurfaceTexture, if there are additional
1607b305fffc39d0fe0926e7fd2d7f6a524fbce62b7Jamie Gennis    // references on the buffers (e.g. if a buffer is referenced by a client or
1617b305fffc39d0fe0926e7fd2d7f6a524fbce62b7Jamie Gennis    // by OpenGL ES as a texture) then those buffer will remain allocated.
1627b305fffc39d0fe0926e7fd2d7f6a524fbce62b7Jamie Gennis    void abandon();
1637b305fffc39d0fe0926e7fd2d7f6a524fbce62b7Jamie Gennis
164fa28c35c21d1bf8b38f541758c291bc17a2d7270Jamie Gennis    // set the name of the SurfaceTexture that will be used to identify it in
165fa28c35c21d1bf8b38f541758c291bc17a2d7270Jamie Gennis    // log messages.
166fa28c35c21d1bf8b38f541758c291bc17a2d7270Jamie Gennis    void setName(const String8& name);
167fa28c35c21d1bf8b38f541758c291bc17a2d7270Jamie Gennis
168b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam    // These functions call the corresponding BufferQueue implementation
169b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam    // so the refactoring can proceed smoothly
170b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam    status_t setDefaultBufferFormat(uint32_t defaultFormat);
171b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam    status_t setConsumerUsageBits(uint32_t usage);
172b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam    status_t setTransformHint(uint32_t hint);
173b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam    virtual status_t setSynchronousMode(bool enabled);
174b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam    virtual status_t setBufferCount(int bufferCount);
175b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam    virtual status_t connect(int api,
176b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam                uint32_t* outWidth, uint32_t* outHeight, uint32_t* outTransform);
177b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam
178b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam    sp<BufferQueue> getBufferQueue() const;
179b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam
18068c7794183a7dbfe3b20d4ce832f8eb79c2c619aMathias Agopian    // dump our state in a String
181eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    virtual void dump(String8& result) const;
182eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    virtual void dump(String8& result, const char* prefix, char* buffer, size_t SIZE) const;
18368c7794183a7dbfe3b20d4ce832f8eb79c2c619aMathias Agopian
1847a042bf324fe3f3d5d4085fda21fe50dc0c362b4Mathias Agopianprotected:
1858ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis
186fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    // Implementation of the BufferQueue::ConsumerListener interface.  These
187fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    // calls are used to notify the SurfaceTexture of asynchronous events in the
188fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    // BufferQueue.
189fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    virtual void onFrameAvailable();
190fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    virtual void onBuffersReleased();
191fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis
1927a042bf324fe3f3d5d4085fda21fe50dc0c362b4Mathias Agopian    static bool isExternalFormat(uint32_t format);
1937a042bf324fe3f3d5d4085fda21fe50dc0c362b4Mathias Agopian
1947a042bf324fe3f3d5d4085fda21fe50dc0c362b4Mathias Agopianprivate:
1958ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis
1968ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis    // createImage creates a new EGLImage from a GraphicBuffer.
1978ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis    EGLImageKHR createImage(EGLDisplay dpy,
1988ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis            const sp<GraphicBuffer>& graphicBuffer);
1998ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis
200fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    // freeBufferLocked frees up the given buffer slot.  If the slot has been
201fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    // initialized this will release the reference to the GraphicBuffer in that
202fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    // slot and destroy the EGLImage in that slot.  Otherwise it has no effect.
203fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    //
204fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    // This method must be called with mMutex locked.
205fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    void freeBufferLocked(int slotIndex);
206fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis
207736aa9573bb7b78f9c315f396c104491b3639426Jamie Gennis    // computeCurrentTransformMatrix computes the transform matrix for the
208736aa9573bb7b78f9c315f396c104491b3639426Jamie Gennis    // current texture.  It uses mCurrentTransform and the current GraphicBuffer
209736aa9573bb7b78f9c315f396c104491b3639426Jamie Gennis    // to compute this matrix and stores it in mCurrentTransformMatrix.
210736aa9573bb7b78f9c315f396c104491b3639426Jamie Gennis    void computeCurrentTransformMatrix();
211736aa9573bb7b78f9c315f396c104491b3639426Jamie Gennis
2129a78c90cd46b2a3bd637b056873149d3b94384b4Jamie Gennis    // mCurrentTextureBuf is the graphic buffer of the current texture. It's
2139a78c90cd46b2a3bd637b056873149d3b94384b4Jamie Gennis    // possible that this buffer is not associated with any buffer slot, so we
21429c870271e8d3f8c40c356283650ba54fe71a16bJamie Gennis    // must track it separately in order to support the getCurrentBuffer method.
2159a78c90cd46b2a3bd637b056873149d3b94384b4Jamie Gennis    sp<GraphicBuffer> mCurrentTextureBuf;
2169a78c90cd46b2a3bd637b056873149d3b94384b4Jamie Gennis
217f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennis    // mCurrentCrop is the crop rectangle that applies to the current texture.
2187734ebfe47f42f980c1b44c1f284a91d8ad1d6c7Mathias Agopian    // It gets set each time updateTexImage is called.
219f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennis    Rect mCurrentCrop;
220f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennis
221f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennis    // mCurrentTransform is the transform identifier for the current texture. It
2227734ebfe47f42f980c1b44c1f284a91d8ad1d6c7Mathias Agopian    // gets set each time updateTexImage is called.
223f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennis    uint32_t mCurrentTransform;
224f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennis
2257734ebfe47f42f980c1b44c1f284a91d8ad1d6c7Mathias Agopian    // mCurrentScalingMode is the scaling mode for the current texture. It gets
2267734ebfe47f42f980c1b44c1f284a91d8ad1d6c7Mathias Agopian    // set to each time updateTexImage is called.
2277734ebfe47f42f980c1b44c1f284a91d8ad1d6c7Mathias Agopian    uint32_t mCurrentScalingMode;
2287734ebfe47f42f980c1b44c1f284a91d8ad1d6c7Mathias Agopian
229736aa9573bb7b78f9c315f396c104491b3639426Jamie Gennis    // mCurrentTransformMatrix is the transform matrix for the current texture.
230736aa9573bb7b78f9c315f396c104491b3639426Jamie Gennis    // It gets computed by computeTransformMatrix each time updateTexImage is
231736aa9573bb7b78f9c315f396c104491b3639426Jamie Gennis    // called.
232736aa9573bb7b78f9c315f396c104491b3639426Jamie Gennis    float mCurrentTransformMatrix[16];
233736aa9573bb7b78f9c315f396c104491b3639426Jamie Gennis
2341d01a12e7150be569557b64da9b8663c62c13594Eino-Ville Talvala    // mCurrentTimestamp is the timestamp for the current texture. It
2357734ebfe47f42f980c1b44c1f284a91d8ad1d6c7Mathias Agopian    // gets set each time updateTexImage is called.
2361d01a12e7150be569557b64da9b8663c62c13594Eino-Ville Talvala    int64_t mCurrentTimestamp;
2371d01a12e7150be569557b64da9b8663c62c13594Eino-Ville Talvala
2388ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis    // mTexName is the name of the OpenGL texture to which streamed images will
239292a31a4c2ae2f6faf134e8e4a726583017dad06Pannag Sanketi    // be bound when updateTexImage is called. It is set at construction time
2408ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis    // changed with a call to setTexName.
2418ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis    const GLuint mTexName;
2428ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis
24386edf4f6470ee0f108bf40d3c1d23bf0a78c9c38Jamie Gennis    // mUseFenceSync indicates whether creation of the EGL_KHR_fence_sync
24486edf4f6470ee0f108bf40d3c1d23bf0a78c9c38Jamie Gennis    // extension should be used to prevent buffers from being dequeued before
24586edf4f6470ee0f108bf40d3c1d23bf0a78c9c38Jamie Gennis    // it's safe for them to be written. It gets set at construction time and
24686edf4f6470ee0f108bf40d3c1d23bf0a78c9c38Jamie Gennis    // never changes.
24786edf4f6470ee0f108bf40d3c1d23bf0a78c9c38Jamie Gennis    const bool mUseFenceSync;
24886edf4f6470ee0f108bf40d3c1d23bf0a78c9c38Jamie Gennis
249fb1b5a2f333800574b0da435d1200cf9b13d723fJamie Gennis    // mTexTarget is the GL texture target with which the GL texture object is
250fb1b5a2f333800574b0da435d1200cf9b13d723fJamie Gennis    // associated.  It is set in the constructor and never changed.  It is
251fb1b5a2f333800574b0da435d1200cf9b13d723fJamie Gennis    // almost always GL_TEXTURE_EXTERNAL_OES except for one use case in Android
252fb1b5a2f333800574b0da435d1200cf9b13d723fJamie Gennis    // Browser.  In that case it is set to GL_TEXTURE_2D to allow
253fb1b5a2f333800574b0da435d1200cf9b13d723fJamie Gennis    // glCopyTexSubImage to read from the texture.  This is a hack to work
254fb1b5a2f333800574b0da435d1200cf9b13d723fJamie Gennis    // around a GL driver limitation on the number of FBO attachments, which the
255fb1b5a2f333800574b0da435d1200cf9b13d723fJamie Gennis    // browser's tile cache exceeds.
256fb1b5a2f333800574b0da435d1200cf9b13d723fJamie Gennis    const GLenum mTexTarget;
257a929748ddb67cbece3337c7fda7877fdeb973aa4Sunita Nadampalli
258fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    // EGLSlot contains the information and object references that
259fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    // SurfaceTexture maintains about a BufferQueue buffer slot.
260eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    struct EGLSlot {
261eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam        EGLSlot()
262eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam        : mEglImage(EGL_NO_IMAGE_KHR),
263eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam          mEglDisplay(EGL_NO_DISPLAY),
264eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam          mFence(EGL_NO_SYNC_KHR) {
265eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam        }
266eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam
267eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam        sp<GraphicBuffer> mGraphicBuffer;
268eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam
269eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam        // mEglImage is the EGLImage created from mGraphicBuffer.
270eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam        EGLImageKHR mEglImage;
271eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam
272eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam        // mEglDisplay is the EGLDisplay used to create mEglImage.
273eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam        EGLDisplay mEglDisplay;
274eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam
275eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam        // mFence is the EGL sync object that must signal before the buffer
276eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam        // associated with this buffer slot may be dequeued. It is initialized
277eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam        // to EGL_NO_SYNC_KHR when the buffer is created and (optionally, based
278eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam        // on a compile-time option) set to a new sync object in updateTexImage.
279eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam        EGLSyncKHR mFence;
280eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    };
281eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam
282fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    // mEGLSlots stores the buffers that have been allocated by the BufferQueue
283fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    // for each buffer slot.  It is initialized to null pointers, and gets
284fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    // filled in with the result of BufferQueue::acquire when the
285fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    // client dequeues a buffer from a
286fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    // slot that has not yet been used. The buffer allocated to a slot will also
287fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    // be replaced if the requested buffer usage or geometry differs from that
288fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    // of the buffer allocated to a slot.
289b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam    EGLSlot mEGLSlots[BufferQueue::NUM_BUFFER_SLOTS];
290eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam
291eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    // mAbandoned indicates that the BufferQueue will no longer be used to
292eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    // consume images buffers pushed to it using the ISurfaceTexture interface.
293eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    // It is initialized to false, and set to true in the abandon method.  A
294eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    // BufferQueue that has been abandoned will return the NO_INIT error from
295eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    // all ISurfaceTexture methods capable of returning an error.
296eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    bool mAbandoned;
297eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam
298eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    // mName is a string used to identify the SurfaceTexture in log messages.
299eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    // It can be set by the setName method.
300eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    String8 mName;
301eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam
302fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    // mFrameAvailableListener is the listener object that will be called when a
303fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    // new frame becomes available. If it is not NULL it will be called from
304fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    // queueBuffer.
305fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    sp<FrameAvailableListener> mFrameAvailableListener;
306eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam
307eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    // mCurrentTexture is the buffer slot index of the buffer that is currently
308eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    // bound to the OpenGL texture. It is initialized to INVALID_BUFFER_SLOT,
309eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    // indicating that no buffer slot is currently bound to the texture. Note,
310eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    // however, that a value of INVALID_BUFFER_SLOT does not necessarily mean
311eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    // that no buffer is bound to the texture. A call to setBufferCount will
312eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    // reset mCurrentTexture to INVALID_BUFFER_SLOT.
313eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    int mCurrentTexture;
314eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam
315b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam    // The SurfaceTexture has-a BufferQueue and is responsible for creating this object
316b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam    // if none is supplied
317b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam    sp<BufferQueue> mBufferQueue;
318b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam
319fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    // mMutex is the mutex used to prevent concurrent access to the member
320fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    // variables of SurfaceTexture objects. It must be locked whenever the
321fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    // member variables are accessed.
322fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    mutable Mutex mMutex;
3238ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis};
3248ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis
3258ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis// ----------------------------------------------------------------------------
3268ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis}; // namespace android
3278ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis
3288ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis#endif // ANDROID_GUI_SURFACETEXTURE_H
329