SurfaceTexture.h revision fb1b5a2f333800574b0da435d1200cf9b13d723f
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>
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 Gennis#define ANDROID_GRAPHICS_SURFACETEXTURE_JNI_ID "mSurfaceTexture"
348ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis
358ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennisnamespace android {
368ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis// ----------------------------------------------------------------------------
378ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis
389a78c90cd46b2a3bd637b056873149d3b94384b4Jamie Gennisclass IGraphicBufferAlloc;
3968c7794183a7dbfe3b20d4ce832f8eb79c2c619aMathias Agopianclass String8;
409a78c90cd46b2a3bd637b056873149d3b94384b4Jamie Gennis
418ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennisclass SurfaceTexture : public BnSurfaceTexture {
428ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennispublic:
439d4d6c101d90d4a1d1ca9413cf3eb89d1f1898d6Jamie Gennis    enum { MIN_UNDEQUEUED_BUFFERS = 2 };
448072711307aa98ee5ee6f7369860ae38c3e19656Mathias Agopian    enum {
458072711307aa98ee5ee6f7369860ae38c3e19656Mathias Agopian        MIN_ASYNC_BUFFER_SLOTS = MIN_UNDEQUEUED_BUFFERS + 1,
468072711307aa98ee5ee6f7369860ae38c3e19656Mathias Agopian        MIN_SYNC_BUFFER_SLOTS  = MIN_UNDEQUEUED_BUFFERS
478072711307aa98ee5ee6f7369860ae38c3e19656Mathias Agopian    };
488ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis    enum { NUM_BUFFER_SLOTS = 32 };
49fe0a87b54654a1392650e7f1862df473287d8332Jamie Gennis    enum { NO_CONNECTED_API = 0 };
508ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis
51c4d4aeab52435c177ded6abdd578fec8191f0f5dJamie Gennis    struct FrameAvailableListener : public virtual RefBase {
523d8063b02e06020c8062addcc9ec49048d3bdb9aJamie Gennis        // onFrameAvailable() is called from queueBuffer() each time an
533d8063b02e06020c8062addcc9ec49048d3bdb9aJamie Gennis        // additional frame becomes available for consumption. This means that
543d8063b02e06020c8062addcc9ec49048d3bdb9aJamie Gennis        // frames that are queued while in asynchronous mode only trigger the
553d8063b02e06020c8062addcc9ec49048d3bdb9aJamie Gennis        // callback if no previous frames are pending. Frames queued while in
563d8063b02e06020c8062addcc9ec49048d3bdb9aJamie Gennis        // synchronous mode always trigger the callback.
573d8063b02e06020c8062addcc9ec49048d3bdb9aJamie Gennis        //
583d8063b02e06020c8062addcc9ec49048d3bdb9aJamie Gennis        // This is called without any lock held and can be called concurrently
593d8063b02e06020c8062addcc9ec49048d3bdb9aJamie Gennis        // by multiple threads.
60c4d4aeab52435c177ded6abdd578fec8191f0f5dJamie Gennis        virtual void onFrameAvailable() = 0;
61c4d4aeab52435c177ded6abdd578fec8191f0f5dJamie Gennis    };
62c4d4aeab52435c177ded6abdd578fec8191f0f5dJamie Gennis
638ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis    // tex indicates the name OpenGL texture to which images are to be streamed.
648ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis    // This texture name cannot be changed once the SurfaceTexture is created.
65fb1b5a2f333800574b0da435d1200cf9b13d723fJamie Gennis    SurfaceTexture(GLuint tex, bool allowSynchronousMode = true,
66fb1b5a2f333800574b0da435d1200cf9b13d723fJamie Gennis            GLenum texTarget = GL_TEXTURE_EXTERNAL_OES);
678ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis
688ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis    virtual ~SurfaceTexture();
698ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis
708ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis    // setBufferCount updates the number of available buffer slots.  After
718ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis    // calling this all buffer slots are both unallocated and owned by the
728ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis    // SurfaceTexture object (i.e. they are not owned by the client).
738ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis    virtual status_t setBufferCount(int bufferCount);
748ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis
757b305fffc39d0fe0926e7fd2d7f6a524fbce62b7Jamie Gennis    virtual status_t requestBuffer(int slot, sp<GraphicBuffer>* buf);
768ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis
778ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis    // dequeueBuffer gets the next buffer slot index for the client to use. If a
788ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis    // buffer slot is available then that slot index is written to the location
798ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis    // pointed to by the buf argument and a status of OK is returned.  If no
808ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis    // slot is available then a status of -EBUSY is returned and buf is
818ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis    // unmodified.
82c04f153353cdb0d291297d10452239f791d3fd2bMathias Agopian    virtual status_t dequeueBuffer(int *buf, uint32_t w, uint32_t h,
83c04f153353cdb0d291297d10452239f791d3fd2bMathias Agopian            uint32_t format, uint32_t usage);
848ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis
851d01a12e7150be569557b64da9b8663c62c13594Eino-Ville Talvala    // queueBuffer returns a filled buffer to the SurfaceTexture. In addition, a
861d01a12e7150be569557b64da9b8663c62c13594Eino-Ville Talvala    // timestamp must be provided for the buffer. The timestamp is in
871d01a12e7150be569557b64da9b8663c62c13594Eino-Ville Talvala    // nanoseconds, and must be monotonically increasing. Its other semantics
881d01a12e7150be569557b64da9b8663c62c13594Eino-Ville Talvala    // (zero point, etc) are client-dependent and should be documented by the
891d01a12e7150be569557b64da9b8663c62c13594Eino-Ville Talvala    // client.
9097c602c5af5f3ffd69009bf496d86347b71a2b4cMathias Agopian    virtual status_t queueBuffer(int buf, int64_t timestamp,
9197c602c5af5f3ffd69009bf496d86347b71a2b4cMathias Agopian            uint32_t* outWidth, uint32_t* outHeight, uint32_t* outTransform);
928ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis    virtual void cancelBuffer(int buf);
938ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis    virtual status_t setCrop(const Rect& reg);
948ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis    virtual status_t setTransform(uint32_t transform);
957734ebfe47f42f980c1b44c1f284a91d8ad1d6c7Mathias Agopian    virtual status_t setScalingMode(int mode);
968ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis
97eafabcdc1639fb96062d9e3c39b0ae27b0238ae1Mathias Agopian    virtual int query(int what, int* value);
98eafabcdc1639fb96062d9e3c39b0ae27b0238ae1Mathias Agopian
998072711307aa98ee5ee6f7369860ae38c3e19656Mathias Agopian    // setSynchronousMode set whether dequeueBuffer is synchronous or
1008072711307aa98ee5ee6f7369860ae38c3e19656Mathias Agopian    // asynchronous. In synchronous mode, dequeueBuffer blocks until
1018072711307aa98ee5ee6f7369860ae38c3e19656Mathias Agopian    // a buffer is available, the currently bound buffer can be dequeued and
1028072711307aa98ee5ee6f7369860ae38c3e19656Mathias Agopian    // queued buffers will be retired in order.
1038072711307aa98ee5ee6f7369860ae38c3e19656Mathias Agopian    // The default mode is asynchronous.
1048072711307aa98ee5ee6f7369860ae38c3e19656Mathias Agopian    virtual status_t setSynchronousMode(bool enabled);
1058072711307aa98ee5ee6f7369860ae38c3e19656Mathias Agopian
106fe0a87b54654a1392650e7f1862df473287d8332Jamie Gennis    // connect attempts to connect a client API to the SurfaceTexture.  This
107fe0a87b54654a1392650e7f1862df473287d8332Jamie Gennis    // must be called before any other ISurfaceTexture methods are called except
108fe0a87b54654a1392650e7f1862df473287d8332Jamie Gennis    // for getAllocator.
109fe0a87b54654a1392650e7f1862df473287d8332Jamie Gennis    //
110fe0a87b54654a1392650e7f1862df473287d8332Jamie Gennis    // This method will fail if the connect was previously called on the
111fe0a87b54654a1392650e7f1862df473287d8332Jamie Gennis    // SurfaceTexture and no corresponding disconnect call was made.
1125bfc24515bb5c8ea7975f72d538df37753733a2fMathias Agopian    virtual status_t connect(int api,
1135bfc24515bb5c8ea7975f72d538df37753733a2fMathias Agopian            uint32_t* outWidth, uint32_t* outHeight, uint32_t* outTransform);
114fe0a87b54654a1392650e7f1862df473287d8332Jamie Gennis
115fe0a87b54654a1392650e7f1862df473287d8332Jamie Gennis    // disconnect attempts to disconnect a client API from the SurfaceTexture.
116fe0a87b54654a1392650e7f1862df473287d8332Jamie Gennis    // Calling this method will cause any subsequent calls to other
117fe0a87b54654a1392650e7f1862df473287d8332Jamie Gennis    // ISurfaceTexture methods to fail except for getAllocator and connect.
118fe0a87b54654a1392650e7f1862df473287d8332Jamie Gennis    // Successfully calling connect after this will allow the other methods to
119fe0a87b54654a1392650e7f1862df473287d8332Jamie Gennis    // succeed again.
120fe0a87b54654a1392650e7f1862df473287d8332Jamie Gennis    //
121fe0a87b54654a1392650e7f1862df473287d8332Jamie Gennis    // This method will fail if the the SurfaceTexture is not currently
122fe0a87b54654a1392650e7f1862df473287d8332Jamie Gennis    // connected to the specified client API.
123fe0a87b54654a1392650e7f1862df473287d8332Jamie Gennis    virtual status_t disconnect(int api);
124fe0a87b54654a1392650e7f1862df473287d8332Jamie Gennis
1258ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis    // updateTexImage sets the image contents of the target texture to that of
1268ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis    // the most recently queued buffer.
1278ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis    //
1288ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis    // This call may only be made while the OpenGL ES context to which the
1298ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis    // target texture belongs is bound to the calling thread.
1308ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis    status_t updateTexImage();
1318ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis
1328072711307aa98ee5ee6f7369860ae38c3e19656Mathias Agopian    // setBufferCountServer set the buffer count. If the client has requested
1338072711307aa98ee5ee6f7369860ae38c3e19656Mathias Agopian    // a buffer count using setBufferCount, the server-buffer count will
1348072711307aa98ee5ee6f7369860ae38c3e19656Mathias Agopian    // take effect once the client sets the count back to zero.
1358072711307aa98ee5ee6f7369860ae38c3e19656Mathias Agopian    status_t setBufferCountServer(int bufferCount);
1368072711307aa98ee5ee6f7369860ae38c3e19656Mathias Agopian
137f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennis    // getTransformMatrix retrieves the 4x4 texture coordinate transform matrix
138f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennis    // associated with the texture image set by the most recent call to
139f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennis    // updateTexImage.
140f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennis    //
141f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennis    // This transform matrix maps 2D homogeneous texture coordinates of the form
142f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennis    // (s, t, 0, 1) with s and t in the inclusive range [0, 1] to the texture
143f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennis    // coordinate that should be used to sample that location from the texture.
144f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennis    // Sampling the texture outside of the range of this transform is undefined.
145f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennis    //
146f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennis    // This transform is necessary to compensate for transforms that the stream
147f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennis    // content producer may implicitly apply to the content. By forcing users of
148f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennis    // a SurfaceTexture to apply this transform we avoid performing an extra
149f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennis    // copy of the data that would be needed to hide the transform from the
150f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennis    // user.
151f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennis    //
152f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennis    // The matrix is stored in column-major order so that it may be passed
153f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennis    // directly to OpenGL ES via the glLoadMatrixf or glUniformMatrix4fv
154f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennis    // functions.
155f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennis    void getTransformMatrix(float mtx[16]);
156f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennis
1571d01a12e7150be569557b64da9b8663c62c13594Eino-Ville Talvala    // getTimestamp retrieves the timestamp associated with the texture image
1581d01a12e7150be569557b64da9b8663c62c13594Eino-Ville Talvala    // set by the most recent call to updateTexImage.
1591d01a12e7150be569557b64da9b8663c62c13594Eino-Ville Talvala    //
1601d01a12e7150be569557b64da9b8663c62c13594Eino-Ville Talvala    // The timestamp is in nanoseconds, and is monotonically increasing. Its
1611d01a12e7150be569557b64da9b8663c62c13594Eino-Ville Talvala    // other semantics (zero point, etc) are source-dependent and should be
1621d01a12e7150be569557b64da9b8663c62c13594Eino-Ville Talvala    // documented by the source.
1631d01a12e7150be569557b64da9b8663c62c13594Eino-Ville Talvala    int64_t getTimestamp();
1641d01a12e7150be569557b64da9b8663c62c13594Eino-Ville Talvala
165c4d4aeab52435c177ded6abdd578fec8191f0f5dJamie Gennis    // setFrameAvailableListener sets the listener object that will be notified
166c4d4aeab52435c177ded6abdd578fec8191f0f5dJamie Gennis    // when a new frame becomes available.
167292a31a4c2ae2f6faf134e8e4a726583017dad06Pannag Sanketi    void setFrameAvailableListener(const sp<FrameAvailableListener>& listener);
168c4d4aeab52435c177ded6abdd578fec8191f0f5dJamie Gennis
1691b20cde313b5ef8acdace742328df867956d24cbJamie Gennis    // getAllocator retrieves the binder object that must be referenced as long
1701b20cde313b5ef8acdace742328df867956d24cbJamie Gennis    // as the GraphicBuffers dequeued from this SurfaceTexture are referenced.
1711b20cde313b5ef8acdace742328df867956d24cbJamie Gennis    // Holding this binder reference prevents SurfaceFlinger from freeing the
1721b20cde313b5ef8acdace742328df867956d24cbJamie Gennis    // buffers before the client is done with them.
1731b20cde313b5ef8acdace742328df867956d24cbJamie Gennis    sp<IBinder> getAllocator();
1741b20cde313b5ef8acdace742328df867956d24cbJamie Gennis
175a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian    // setDefaultBufferSize is used to set the size of buffers returned by
176a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian    // requestBuffers when a with and height of zero is requested.
177a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian    // A call to setDefaultBufferSize() may trigger requestBuffers() to
178a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian    // be called from the client.
179a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian    status_t setDefaultBufferSize(uint32_t w, uint32_t h);
180a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian
1817a042bf324fe3f3d5d4085fda21fe50dc0c362b4Mathias Agopian    // getCurrentBuffer returns the buffer associated with the current image.
1827a042bf324fe3f3d5d4085fda21fe50dc0c362b4Mathias Agopian    sp<GraphicBuffer> getCurrentBuffer() const;
1837a042bf324fe3f3d5d4085fda21fe50dc0c362b4Mathias Agopian
1847a042bf324fe3f3d5d4085fda21fe50dc0c362b4Mathias Agopian    // getCurrentTextureTarget returns the texture target of the current
1857a042bf324fe3f3d5d4085fda21fe50dc0c362b4Mathias Agopian    // texture as returned by updateTexImage().
1867a042bf324fe3f3d5d4085fda21fe50dc0c362b4Mathias Agopian    GLenum getCurrentTextureTarget() const;
1877a042bf324fe3f3d5d4085fda21fe50dc0c362b4Mathias Agopian
1887a042bf324fe3f3d5d4085fda21fe50dc0c362b4Mathias Agopian    // getCurrentCrop returns the cropping rectangle of the current buffer
1897a042bf324fe3f3d5d4085fda21fe50dc0c362b4Mathias Agopian    Rect getCurrentCrop() const;
1907a042bf324fe3f3d5d4085fda21fe50dc0c362b4Mathias Agopian
1917a042bf324fe3f3d5d4085fda21fe50dc0c362b4Mathias Agopian    // getCurrentTransform returns the transform of the current buffer
1927a042bf324fe3f3d5d4085fda21fe50dc0c362b4Mathias Agopian    uint32_t getCurrentTransform() const;
1937a042bf324fe3f3d5d4085fda21fe50dc0c362b4Mathias Agopian
1947734ebfe47f42f980c1b44c1f284a91d8ad1d6c7Mathias Agopian    // getCurrentScalingMode returns the scaling mode of the current buffer
1957734ebfe47f42f980c1b44c1f284a91d8ad1d6c7Mathias Agopian    uint32_t getCurrentScalingMode() const;
1967734ebfe47f42f980c1b44c1f284a91d8ad1d6c7Mathias Agopian
1977b305fffc39d0fe0926e7fd2d7f6a524fbce62b7Jamie Gennis    // abandon frees all the buffers and puts the SurfaceTexture into the
1987b305fffc39d0fe0926e7fd2d7f6a524fbce62b7Jamie Gennis    // 'abandoned' state.  Once put in this state the SurfaceTexture can never
1997b305fffc39d0fe0926e7fd2d7f6a524fbce62b7Jamie Gennis    // leave it.  When in the 'abandoned' state, all methods of the
2007b305fffc39d0fe0926e7fd2d7f6a524fbce62b7Jamie Gennis    // ISurfaceTexture interface will fail with the NO_INIT error.
2017b305fffc39d0fe0926e7fd2d7f6a524fbce62b7Jamie Gennis    //
2027b305fffc39d0fe0926e7fd2d7f6a524fbce62b7Jamie Gennis    // Note that while calling this method causes all the buffers to be freed
2037b305fffc39d0fe0926e7fd2d7f6a524fbce62b7Jamie Gennis    // from the perspective of the the SurfaceTexture, if there are additional
2047b305fffc39d0fe0926e7fd2d7f6a524fbce62b7Jamie Gennis    // references on the buffers (e.g. if a buffer is referenced by a client or
2057b305fffc39d0fe0926e7fd2d7f6a524fbce62b7Jamie Gennis    // by OpenGL ES as a texture) then those buffer will remain allocated.
2067b305fffc39d0fe0926e7fd2d7f6a524fbce62b7Jamie Gennis    void abandon();
2077b305fffc39d0fe0926e7fd2d7f6a524fbce62b7Jamie Gennis
208fa28c35c21d1bf8b38f541758c291bc17a2d7270Jamie Gennis    // set the name of the SurfaceTexture that will be used to identify it in
209fa28c35c21d1bf8b38f541758c291bc17a2d7270Jamie Gennis    // log messages.
210fa28c35c21d1bf8b38f541758c291bc17a2d7270Jamie Gennis    void setName(const String8& name);
211fa28c35c21d1bf8b38f541758c291bc17a2d7270Jamie Gennis
21268c7794183a7dbfe3b20d4ce832f8eb79c2c619aMathias Agopian    // dump our state in a String
21368c7794183a7dbfe3b20d4ce832f8eb79c2c619aMathias Agopian    void dump(String8& result) const;
21468c7794183a7dbfe3b20d4ce832f8eb79c2c619aMathias Agopian    void dump(String8& result, const char* prefix, char* buffer, size_t SIZE) const;
21568c7794183a7dbfe3b20d4ce832f8eb79c2c619aMathias Agopian
2167a042bf324fe3f3d5d4085fda21fe50dc0c362b4Mathias Agopianprotected:
2178ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis
2188e19c2e97e11505ee2ecf336275fd956f2ccfa22Mathias Agopian    // freeBufferLocked frees the resources (both GraphicBuffer and EGLImage)
2198e19c2e97e11505ee2ecf336275fd956f2ccfa22Mathias Agopian    // for the given slot.
2208e19c2e97e11505ee2ecf336275fd956f2ccfa22Mathias Agopian    void freeBufferLocked(int index);
2218e19c2e97e11505ee2ecf336275fd956f2ccfa22Mathias Agopian
2228e19c2e97e11505ee2ecf336275fd956f2ccfa22Mathias Agopian    // freeAllBuffersLocked frees the resources (both GraphicBuffer and
2238e19c2e97e11505ee2ecf336275fd956f2ccfa22Mathias Agopian    // EGLImage) for all slots.
224ef51b992192adf4fc432686ab346f5fc7a13bc95Mathias Agopian    void freeAllBuffersLocked();
2252560d14ce8e38984032d999e3fdf8da9a47baf3cMathias Agopian
2268e19c2e97e11505ee2ecf336275fd956f2ccfa22Mathias Agopian    // freeAllBuffersExceptHeadLocked frees the resources (both GraphicBuffer
2278e19c2e97e11505ee2ecf336275fd956f2ccfa22Mathias Agopian    // and EGLImage) for all slots except the head of mQueue
2288e19c2e97e11505ee2ecf336275fd956f2ccfa22Mathias Agopian    void freeAllBuffersExceptHeadLocked();
2298e19c2e97e11505ee2ecf336275fd956f2ccfa22Mathias Agopian
2302560d14ce8e38984032d999e3fdf8da9a47baf3cMathias Agopian    // drainQueueLocked drains the buffer queue if we're in synchronous mode
2318e19c2e97e11505ee2ecf336275fd956f2ccfa22Mathias Agopian    // returns immediately otherwise. return NO_INIT if SurfaceTexture
2328e19c2e97e11505ee2ecf336275fd956f2ccfa22Mathias Agopian    // became abandoned or disconnected during this call.
2338e19c2e97e11505ee2ecf336275fd956f2ccfa22Mathias Agopian    status_t drainQueueLocked();
2348e19c2e97e11505ee2ecf336275fd956f2ccfa22Mathias Agopian
2358e19c2e97e11505ee2ecf336275fd956f2ccfa22Mathias Agopian    // drainQueueAndFreeBuffersLocked drains the buffer queue if we're in
2368e19c2e97e11505ee2ecf336275fd956f2ccfa22Mathias Agopian    // synchronous mode and free all buffers. In asynchronous mode, all buffers
2378e19c2e97e11505ee2ecf336275fd956f2ccfa22Mathias Agopian    // are freed except the current buffer.
2388e19c2e97e11505ee2ecf336275fd956f2ccfa22Mathias Agopian    status_t drainQueueAndFreeBuffersLocked();
2392560d14ce8e38984032d999e3fdf8da9a47baf3cMathias Agopian
2407a042bf324fe3f3d5d4085fda21fe50dc0c362b4Mathias Agopian    static bool isExternalFormat(uint32_t format);
2417a042bf324fe3f3d5d4085fda21fe50dc0c362b4Mathias Agopian
2427a042bf324fe3f3d5d4085fda21fe50dc0c362b4Mathias Agopianprivate:
2438ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis
2448ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis    // createImage creates a new EGLImage from a GraphicBuffer.
2458ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis    EGLImageKHR createImage(EGLDisplay dpy,
2468ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis            const sp<GraphicBuffer>& graphicBuffer);
2478ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis
2488072711307aa98ee5ee6f7369860ae38c3e19656Mathias Agopian    status_t setBufferCountServerLocked(int bufferCount);
2498072711307aa98ee5ee6f7369860ae38c3e19656Mathias Agopian
250736aa9573bb7b78f9c315f396c104491b3639426Jamie Gennis    // computeCurrentTransformMatrix computes the transform matrix for the
251736aa9573bb7b78f9c315f396c104491b3639426Jamie Gennis    // current texture.  It uses mCurrentTransform and the current GraphicBuffer
252736aa9573bb7b78f9c315f396c104491b3639426Jamie Gennis    // to compute this matrix and stores it in mCurrentTransformMatrix.
253736aa9573bb7b78f9c315f396c104491b3639426Jamie Gennis    void computeCurrentTransformMatrix();
254736aa9573bb7b78f9c315f396c104491b3639426Jamie Gennis
2558ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis    enum { INVALID_BUFFER_SLOT = -1 };
2568ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis
2578ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis    struct BufferSlot {
258b3e518c820c7dbe35587bd45c510e4e5e7cfd9c9Mathias Agopian
259b3e518c820c7dbe35587bd45c510e4e5e7cfd9c9Mathias Agopian        BufferSlot()
260b3e518c820c7dbe35587bd45c510e4e5e7cfd9c9Mathias Agopian            : mEglImage(EGL_NO_IMAGE_KHR),
261b3e518c820c7dbe35587bd45c510e4e5e7cfd9c9Mathias Agopian              mEglDisplay(EGL_NO_DISPLAY),
262b3e518c820c7dbe35587bd45c510e4e5e7cfd9c9Mathias Agopian              mBufferState(BufferSlot::FREE),
263b3e518c820c7dbe35587bd45c510e4e5e7cfd9c9Mathias Agopian              mRequestBufferCalled(false),
2648cd5ba4b7f01d3a54a8f8bc6d1793aa5fc8e09efJamie Gennis              mTransform(0),
2657734ebfe47f42f980c1b44c1f284a91d8ad1d6c7Mathias Agopian              mScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE),
2668cd5ba4b7f01d3a54a8f8bc6d1793aa5fc8e09efJamie Gennis              mTimestamp(0) {
2678cd5ba4b7f01d3a54a8f8bc6d1793aa5fc8e09efJamie Gennis            mCrop.makeInvalid();
268b3e518c820c7dbe35587bd45c510e4e5e7cfd9c9Mathias Agopian        }
269b3e518c820c7dbe35587bd45c510e4e5e7cfd9c9Mathias Agopian
2708ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis        // mGraphicBuffer points to the buffer allocated for this slot or is NULL
2718ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis        // if no buffer has been allocated.
2728ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis        sp<GraphicBuffer> mGraphicBuffer;
2738ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis
2748ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis        // mEglImage is the EGLImage created from mGraphicBuffer.
2758ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis        EGLImageKHR mEglImage;
2768ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis
2778ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis        // mEglDisplay is the EGLDisplay used to create mEglImage.
2788ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis        EGLDisplay mEglDisplay;
2798ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis
2808cd5ba4b7f01d3a54a8f8bc6d1793aa5fc8e09efJamie Gennis        // BufferState represents the different states in which a buffer slot
2818cd5ba4b7f01d3a54a8f8bc6d1793aa5fc8e09efJamie Gennis        // can be.
2828cd5ba4b7f01d3a54a8f8bc6d1793aa5fc8e09efJamie Gennis        enum BufferState {
2838cd5ba4b7f01d3a54a8f8bc6d1793aa5fc8e09efJamie Gennis            // FREE indicates that the buffer is not currently being used and
2848cd5ba4b7f01d3a54a8f8bc6d1793aa5fc8e09efJamie Gennis            // will not be used in the future until it gets dequeued and
28529b5762efc359022168e5099c1d17925444d3147Mathias Agopian            // subsequently queued by the client.
2868cd5ba4b7f01d3a54a8f8bc6d1793aa5fc8e09efJamie Gennis            FREE = 0,
2878cd5ba4b7f01d3a54a8f8bc6d1793aa5fc8e09efJamie Gennis
2888cd5ba4b7f01d3a54a8f8bc6d1793aa5fc8e09efJamie Gennis            // DEQUEUED indicates that the buffer has been dequeued by the
2898cd5ba4b7f01d3a54a8f8bc6d1793aa5fc8e09efJamie Gennis            // client, but has not yet been queued or canceled. The buffer is
2908cd5ba4b7f01d3a54a8f8bc6d1793aa5fc8e09efJamie Gennis            // considered 'owned' by the client, and the server should not use
2918cd5ba4b7f01d3a54a8f8bc6d1793aa5fc8e09efJamie Gennis            // it for anything.
2928cd5ba4b7f01d3a54a8f8bc6d1793aa5fc8e09efJamie Gennis            //
2938cd5ba4b7f01d3a54a8f8bc6d1793aa5fc8e09efJamie Gennis            // Note that when in synchronous-mode (mSynchronousMode == true),
2948cd5ba4b7f01d3a54a8f8bc6d1793aa5fc8e09efJamie Gennis            // the buffer that's currently attached to the texture may be
2958cd5ba4b7f01d3a54a8f8bc6d1793aa5fc8e09efJamie Gennis            // dequeued by the client.  That means that the current buffer can
2968cd5ba4b7f01d3a54a8f8bc6d1793aa5fc8e09efJamie Gennis            // be in either the DEQUEUED or QUEUED state.  In asynchronous mode,
2978cd5ba4b7f01d3a54a8f8bc6d1793aa5fc8e09efJamie Gennis            // however, the current buffer is always in the QUEUED state.
2988cd5ba4b7f01d3a54a8f8bc6d1793aa5fc8e09efJamie Gennis            DEQUEUED = 1,
2998cd5ba4b7f01d3a54a8f8bc6d1793aa5fc8e09efJamie Gennis
3008cd5ba4b7f01d3a54a8f8bc6d1793aa5fc8e09efJamie Gennis            // QUEUED indicates that the buffer has been queued by the client,
3018cd5ba4b7f01d3a54a8f8bc6d1793aa5fc8e09efJamie Gennis            // and has not since been made available for the client to dequeue.
3028cd5ba4b7f01d3a54a8f8bc6d1793aa5fc8e09efJamie Gennis            // Attaching the buffer to the texture does NOT transition the
3038cd5ba4b7f01d3a54a8f8bc6d1793aa5fc8e09efJamie Gennis            // buffer away from the QUEUED state. However, in Synchronous mode
3048cd5ba4b7f01d3a54a8f8bc6d1793aa5fc8e09efJamie Gennis            // the current buffer may be dequeued by the client under some
3058cd5ba4b7f01d3a54a8f8bc6d1793aa5fc8e09efJamie Gennis            // circumstances. See the note about the current buffer in the
3068cd5ba4b7f01d3a54a8f8bc6d1793aa5fc8e09efJamie Gennis            // documentation for DEQUEUED.
3078cd5ba4b7f01d3a54a8f8bc6d1793aa5fc8e09efJamie Gennis            QUEUED = 2,
3088cd5ba4b7f01d3a54a8f8bc6d1793aa5fc8e09efJamie Gennis        };
3098cd5ba4b7f01d3a54a8f8bc6d1793aa5fc8e09efJamie Gennis
3108cd5ba4b7f01d3a54a8f8bc6d1793aa5fc8e09efJamie Gennis        // mBufferState is the current state of this buffer slot.
3118cd5ba4b7f01d3a54a8f8bc6d1793aa5fc8e09efJamie Gennis        BufferState mBufferState;
312b3e518c820c7dbe35587bd45c510e4e5e7cfd9c9Mathias Agopian
313b3e518c820c7dbe35587bd45c510e4e5e7cfd9c9Mathias Agopian        // mRequestBufferCalled is used for validating that the client did
314b3e518c820c7dbe35587bd45c510e4e5e7cfd9c9Mathias Agopian        // call requestBuffer() when told to do so. Technically this is not
315b3e518c820c7dbe35587bd45c510e4e5e7cfd9c9Mathias Agopian        // needed but useful for debugging and catching client bugs.
316b3e518c820c7dbe35587bd45c510e4e5e7cfd9c9Mathias Agopian        bool mRequestBufferCalled;
317b3e518c820c7dbe35587bd45c510e4e5e7cfd9c9Mathias Agopian
3188cd5ba4b7f01d3a54a8f8bc6d1793aa5fc8e09efJamie Gennis        // mCrop is the current crop rectangle for this buffer slot. This gets
3198cd5ba4b7f01d3a54a8f8bc6d1793aa5fc8e09efJamie Gennis        // set to mNextCrop each time queueBuffer gets called for this buffer.
3208cd5ba4b7f01d3a54a8f8bc6d1793aa5fc8e09efJamie Gennis        Rect mCrop;
321b3e518c820c7dbe35587bd45c510e4e5e7cfd9c9Mathias Agopian
3228cd5ba4b7f01d3a54a8f8bc6d1793aa5fc8e09efJamie Gennis        // mTransform is the current transform flags for this buffer slot. This
3238cd5ba4b7f01d3a54a8f8bc6d1793aa5fc8e09efJamie Gennis        // gets set to mNextTransform each time queueBuffer gets called for this
3248cd5ba4b7f01d3a54a8f8bc6d1793aa5fc8e09efJamie Gennis        // slot.
3258cd5ba4b7f01d3a54a8f8bc6d1793aa5fc8e09efJamie Gennis        uint32_t mTransform;
326b3e518c820c7dbe35587bd45c510e4e5e7cfd9c9Mathias Agopian
3277734ebfe47f42f980c1b44c1f284a91d8ad1d6c7Mathias Agopian        // mScalingMode is the current scaling mode for this buffer slot. This
3287734ebfe47f42f980c1b44c1f284a91d8ad1d6c7Mathias Agopian        // gets set to mNextScalingMode each time queueBuffer gets called for
3297734ebfe47f42f980c1b44c1f284a91d8ad1d6c7Mathias Agopian        // this slot.
3307734ebfe47f42f980c1b44c1f284a91d8ad1d6c7Mathias Agopian        uint32_t mScalingMode;
3317734ebfe47f42f980c1b44c1f284a91d8ad1d6c7Mathias Agopian
3328cd5ba4b7f01d3a54a8f8bc6d1793aa5fc8e09efJamie Gennis        // mTimestamp is the current timestamp for this buffer slot. This gets
3338cd5ba4b7f01d3a54a8f8bc6d1793aa5fc8e09efJamie Gennis        // to set by queueBuffer each time this slot is queued.
3348cd5ba4b7f01d3a54a8f8bc6d1793aa5fc8e09efJamie Gennis        int64_t mTimestamp;
3358ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis    };
3368ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis
3378ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis    // mSlots is the array of buffer slots that must be mirrored on the client
3388ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis    // side. This allows buffer ownership to be transferred between the client
3398ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis    // and server without sending a GraphicBuffer over binder. The entire array
3408ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis    // is initialized to NULL at construction time, and buffers are allocated
3418ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis    // for a slot when requestBuffer is called with that slot's index.
3428ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis    BufferSlot mSlots[NUM_BUFFER_SLOTS];
3438ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis
344a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian    // mDefaultWidth holds the default width of allocated buffers. It is used
345a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian    // in requestBuffers() if a width and height of zero is specified.
346a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian    uint32_t mDefaultWidth;
347a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian
348a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian    // mDefaultHeight holds the default height of allocated buffers. It is used
349a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian    // in requestBuffers() if a width and height of zero is specified.
350a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian    uint32_t mDefaultHeight;
351a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian
352a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian    // mPixelFormat holds the pixel format of allocated buffers. It is used
353a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian    // in requestBuffers() if a format of zero is specified.
354a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian    uint32_t mPixelFormat;
355a5c75c01620179ce00812354778a29a80d76e71fMathias Agopian
3568ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis    // mBufferCount is the number of buffer slots that the client and server
3578072711307aa98ee5ee6f7369860ae38c3e19656Mathias Agopian    // must maintain. It defaults to MIN_ASYNC_BUFFER_SLOTS and can be changed
3588072711307aa98ee5ee6f7369860ae38c3e19656Mathias Agopian    // by calling setBufferCount or setBufferCountServer
3598ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis    int mBufferCount;
3608ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis
361ae468f43418c3cbae74c7f546283f6bb4e4df690Jamie Gennis    // mClientBufferCount is the number of buffer slots requested by the client.
362ae468f43418c3cbae74c7f546283f6bb4e4df690Jamie Gennis    // The default is zero, which means the client doesn't care how many buffers
363ae468f43418c3cbae74c7f546283f6bb4e4df690Jamie Gennis    // there is.
3648072711307aa98ee5ee6f7369860ae38c3e19656Mathias Agopian    int mClientBufferCount;
3658072711307aa98ee5ee6f7369860ae38c3e19656Mathias Agopian
3668072711307aa98ee5ee6f7369860ae38c3e19656Mathias Agopian    // mServerBufferCount buffer count requested by the server-side
3678072711307aa98ee5ee6f7369860ae38c3e19656Mathias Agopian    int mServerBufferCount;
3688072711307aa98ee5ee6f7369860ae38c3e19656Mathias Agopian
3698ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis    // mCurrentTexture is the buffer slot index of the buffer that is currently
37067eedd74ab78c2bfed9fcdc74947b97289254ca4Jamie Gennis    // bound to the OpenGL texture. It is initialized to INVALID_BUFFER_SLOT,
37167eedd74ab78c2bfed9fcdc74947b97289254ca4Jamie Gennis    // indicating that no buffer slot is currently bound to the texture. Note,
37267eedd74ab78c2bfed9fcdc74947b97289254ca4Jamie Gennis    // however, that a value of INVALID_BUFFER_SLOT does not necessarily mean
37367eedd74ab78c2bfed9fcdc74947b97289254ca4Jamie Gennis    // that no buffer is bound to the texture. A call to setBufferCount will
37467eedd74ab78c2bfed9fcdc74947b97289254ca4Jamie Gennis    // reset mCurrentTexture to INVALID_BUFFER_SLOT.
3758ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis    int mCurrentTexture;
3768ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis
3779a78c90cd46b2a3bd637b056873149d3b94384b4Jamie Gennis    // mCurrentTextureBuf is the graphic buffer of the current texture. It's
3789a78c90cd46b2a3bd637b056873149d3b94384b4Jamie Gennis    // possible that this buffer is not associated with any buffer slot, so we
37929c870271e8d3f8c40c356283650ba54fe71a16bJamie Gennis    // must track it separately in order to support the getCurrentBuffer method.
3809a78c90cd46b2a3bd637b056873149d3b94384b4Jamie Gennis    sp<GraphicBuffer> mCurrentTextureBuf;
3819a78c90cd46b2a3bd637b056873149d3b94384b4Jamie Gennis
382f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennis    // mCurrentCrop is the crop rectangle that applies to the current texture.
3837734ebfe47f42f980c1b44c1f284a91d8ad1d6c7Mathias Agopian    // It gets set each time updateTexImage is called.
384f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennis    Rect mCurrentCrop;
385f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennis
386f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennis    // mCurrentTransform is the transform identifier for the current texture. It
3877734ebfe47f42f980c1b44c1f284a91d8ad1d6c7Mathias Agopian    // gets set each time updateTexImage is called.
388f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennis    uint32_t mCurrentTransform;
389f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennis
3907734ebfe47f42f980c1b44c1f284a91d8ad1d6c7Mathias Agopian    // mCurrentScalingMode is the scaling mode for the current texture. It gets
3917734ebfe47f42f980c1b44c1f284a91d8ad1d6c7Mathias Agopian    // set to each time updateTexImage is called.
3927734ebfe47f42f980c1b44c1f284a91d8ad1d6c7Mathias Agopian    uint32_t mCurrentScalingMode;
3937734ebfe47f42f980c1b44c1f284a91d8ad1d6c7Mathias Agopian
394736aa9573bb7b78f9c315f396c104491b3639426Jamie Gennis    // mCurrentTransformMatrix is the transform matrix for the current texture.
395736aa9573bb7b78f9c315f396c104491b3639426Jamie Gennis    // It gets computed by computeTransformMatrix each time updateTexImage is
396736aa9573bb7b78f9c315f396c104491b3639426Jamie Gennis    // called.
397736aa9573bb7b78f9c315f396c104491b3639426Jamie Gennis    float mCurrentTransformMatrix[16];
398736aa9573bb7b78f9c315f396c104491b3639426Jamie Gennis
3991d01a12e7150be569557b64da9b8663c62c13594Eino-Ville Talvala    // mCurrentTimestamp is the timestamp for the current texture. It
4007734ebfe47f42f980c1b44c1f284a91d8ad1d6c7Mathias Agopian    // gets set each time updateTexImage is called.
4011d01a12e7150be569557b64da9b8663c62c13594Eino-Ville Talvala    int64_t mCurrentTimestamp;
4021d01a12e7150be569557b64da9b8663c62c13594Eino-Ville Talvala
403f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennis    // mNextCrop is the crop rectangle that will be used for the next buffer
404f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennis    // that gets queued. It is set by calling setCrop.
405f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennis    Rect mNextCrop;
406f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennis
407f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennis    // mNextTransform is the transform identifier that will be used for the next
408f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennis    // buffer that gets queued. It is set by calling setTransform.
409f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennis    uint32_t mNextTransform;
410f238e28500ca756fbd9e323f728ce7c8dda59475Jamie Gennis
4117734ebfe47f42f980c1b44c1f284a91d8ad1d6c7Mathias Agopian    // mNextScalingMode is the scaling mode that will be used for the next
4127734ebfe47f42f980c1b44c1f284a91d8ad1d6c7Mathias Agopian    // buffers that get queued. It is set by calling setScalingMode.
4137734ebfe47f42f980c1b44c1f284a91d8ad1d6c7Mathias Agopian    int mNextScalingMode;
4147734ebfe47f42f980c1b44c1f284a91d8ad1d6c7Mathias Agopian
4158ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis    // mTexName is the name of the OpenGL texture to which streamed images will
416292a31a4c2ae2f6faf134e8e4a726583017dad06Pannag Sanketi    // be bound when updateTexImage is called. It is set at construction time
4178ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis    // changed with a call to setTexName.
4188ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis    const GLuint mTexName;
4198ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis
4209a78c90cd46b2a3bd637b056873149d3b94384b4Jamie Gennis    // mGraphicBufferAlloc is the connection to SurfaceFlinger that is used to
4219a78c90cd46b2a3bd637b056873149d3b94384b4Jamie Gennis    // allocate new GraphicBuffer objects.
4229a78c90cd46b2a3bd637b056873149d3b94384b4Jamie Gennis    sp<IGraphicBufferAlloc> mGraphicBufferAlloc;
4239a78c90cd46b2a3bd637b056873149d3b94384b4Jamie Gennis
424c4d4aeab52435c177ded6abdd578fec8191f0f5dJamie Gennis    // mFrameAvailableListener is the listener object that will be called when a
425c4d4aeab52435c177ded6abdd578fec8191f0f5dJamie Gennis    // new frame becomes available. If it is not NULL it will be called from
426c4d4aeab52435c177ded6abdd578fec8191f0f5dJamie Gennis    // queueBuffer.
427c4d4aeab52435c177ded6abdd578fec8191f0f5dJamie Gennis    sp<FrameAvailableListener> mFrameAvailableListener;
428c4d4aeab52435c177ded6abdd578fec8191f0f5dJamie Gennis
429b3e518c820c7dbe35587bd45c510e4e5e7cfd9c9Mathias Agopian    // mSynchronousMode whether we're in synchronous mode or not
430b3e518c820c7dbe35587bd45c510e4e5e7cfd9c9Mathias Agopian    bool mSynchronousMode;
431b3e518c820c7dbe35587bd45c510e4e5e7cfd9c9Mathias Agopian
43214a0e58074f2698829b6554f578e6762c377caa3Grace Kloba    // mAllowSynchronousMode whether we allow synchronous mode or not
43314a0e58074f2698829b6554f578e6762c377caa3Grace Kloba    const bool mAllowSynchronousMode;
43414a0e58074f2698829b6554f578e6762c377caa3Grace Kloba
435fe0a87b54654a1392650e7f1862df473287d8332Jamie Gennis    // mConnectedApi indicates the API that is currently connected to this
436fe0a87b54654a1392650e7f1862df473287d8332Jamie Gennis    // SurfaceTexture.  It defaults to NO_CONNECTED_API (= 0), and gets updated
437fe0a87b54654a1392650e7f1862df473287d8332Jamie Gennis    // by the connect and disconnect methods.
438fe0a87b54654a1392650e7f1862df473287d8332Jamie Gennis    int mConnectedApi;
439fe0a87b54654a1392650e7f1862df473287d8332Jamie Gennis
440b3e518c820c7dbe35587bd45c510e4e5e7cfd9c9Mathias Agopian    // mDequeueCondition condition used for dequeueBuffer in synchronous mode
441b3e518c820c7dbe35587bd45c510e4e5e7cfd9c9Mathias Agopian    mutable Condition mDequeueCondition;
442b3e518c820c7dbe35587bd45c510e4e5e7cfd9c9Mathias Agopian
443b3e518c820c7dbe35587bd45c510e4e5e7cfd9c9Mathias Agopian    // mQueue is a FIFO of queued buffers used in synchronous mode
444b3e518c820c7dbe35587bd45c510e4e5e7cfd9c9Mathias Agopian    typedef Vector<int> Fifo;
445b3e518c820c7dbe35587bd45c510e4e5e7cfd9c9Mathias Agopian    Fifo mQueue;
446b3e518c820c7dbe35587bd45c510e4e5e7cfd9c9Mathias Agopian
4477b305fffc39d0fe0926e7fd2d7f6a524fbce62b7Jamie Gennis    // mAbandoned indicates that the SurfaceTexture will no longer be used to
4487b305fffc39d0fe0926e7fd2d7f6a524fbce62b7Jamie Gennis    // consume images buffers pushed to it using the ISurfaceTexture interface.
4497b305fffc39d0fe0926e7fd2d7f6a524fbce62b7Jamie Gennis    // It is initialized to false, and set to true in the abandon method.  A
4507b305fffc39d0fe0926e7fd2d7f6a524fbce62b7Jamie Gennis    // SurfaceTexture that has been abandoned will return the NO_INIT error from
4517b305fffc39d0fe0926e7fd2d7f6a524fbce62b7Jamie Gennis    // all ISurfaceTexture methods capable of returning an error.
4527b305fffc39d0fe0926e7fd2d7f6a524fbce62b7Jamie Gennis    bool mAbandoned;
4537b305fffc39d0fe0926e7fd2d7f6a524fbce62b7Jamie Gennis
454fa28c35c21d1bf8b38f541758c291bc17a2d7270Jamie Gennis    // mName is a string used to identify the SurfaceTexture in log messages.
455fa28c35c21d1bf8b38f541758c291bc17a2d7270Jamie Gennis    // It is set by the setName method.
456fa28c35c21d1bf8b38f541758c291bc17a2d7270Jamie Gennis    String8 mName;
457fa28c35c21d1bf8b38f541758c291bc17a2d7270Jamie Gennis
4588ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis    // mMutex is the mutex used to prevent concurrent access to the member
4598ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis    // variables of SurfaceTexture objects. It must be locked whenever the
4608ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis    // member variables are accessed.
4617a042bf324fe3f3d5d4085fda21fe50dc0c362b4Mathias Agopian    mutable Mutex mMutex;
462736aa9573bb7b78f9c315f396c104491b3639426Jamie Gennis
463fb1b5a2f333800574b0da435d1200cf9b13d723fJamie Gennis    // mTexTarget is the GL texture target with which the GL texture object is
464fb1b5a2f333800574b0da435d1200cf9b13d723fJamie Gennis    // associated.  It is set in the constructor and never changed.  It is
465fb1b5a2f333800574b0da435d1200cf9b13d723fJamie Gennis    // almost always GL_TEXTURE_EXTERNAL_OES except for one use case in Android
466fb1b5a2f333800574b0da435d1200cf9b13d723fJamie Gennis    // Browser.  In that case it is set to GL_TEXTURE_2D to allow
467fb1b5a2f333800574b0da435d1200cf9b13d723fJamie Gennis    // glCopyTexSubImage to read from the texture.  This is a hack to work
468fb1b5a2f333800574b0da435d1200cf9b13d723fJamie Gennis    // around a GL driver limitation on the number of FBO attachments, which the
469fb1b5a2f333800574b0da435d1200cf9b13d723fJamie Gennis    // browser's tile cache exceeds.
470fb1b5a2f333800574b0da435d1200cf9b13d723fJamie Gennis    const GLenum mTexTarget;
4718ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis};
4728ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis
4738ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis// ----------------------------------------------------------------------------
4748ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis}; // namespace android
4758ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis
4768ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis#endif // ANDROID_GUI_SURFACETEXTURE_H
477