Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #ifndef ANDROID_GUI_SURFACETEXTURE_H |
| 18 | #define ANDROID_GUI_SURFACETEXTURE_H |
| 19 | |
| 20 | #include <EGL/egl.h> |
| 21 | #include <EGL/eglext.h> |
| 22 | #include <GLES2/gl2.h> |
| 23 | |
| 24 | #include <gui/ISurfaceTexture.h> |
| 25 | |
| 26 | #include <ui/GraphicBuffer.h> |
| 27 | |
| 28 | #include <utils/threads.h> |
| 29 | |
| 30 | #define ANDROID_GRAPHICS_SURFACETEXTURE_JNI_ID "mSurfaceTexture" |
| 31 | |
| 32 | namespace android { |
| 33 | // ---------------------------------------------------------------------------- |
| 34 | |
| 35 | class SurfaceTexture : public BnSurfaceTexture { |
| 36 | public: |
| 37 | enum { MIN_BUFFER_SLOTS = 3 }; |
| 38 | enum { NUM_BUFFER_SLOTS = 32 }; |
| 39 | |
| 40 | // tex indicates the name OpenGL texture to which images are to be streamed. |
| 41 | // This texture name cannot be changed once the SurfaceTexture is created. |
| 42 | SurfaceTexture(GLuint tex); |
| 43 | |
| 44 | virtual ~SurfaceTexture(); |
| 45 | |
| 46 | // setBufferCount updates the number of available buffer slots. After |
| 47 | // calling this all buffer slots are both unallocated and owned by the |
| 48 | // SurfaceTexture object (i.e. they are not owned by the client). |
| 49 | virtual status_t setBufferCount(int bufferCount); |
| 50 | |
| 51 | virtual sp<GraphicBuffer> requestBuffer(int buf, uint32_t w, uint32_t h, |
| 52 | uint32_t format, uint32_t usage); |
| 53 | |
| 54 | // dequeueBuffer gets the next buffer slot index for the client to use. If a |
| 55 | // buffer slot is available then that slot index is written to the location |
| 56 | // pointed to by the buf argument and a status of OK is returned. If no |
| 57 | // slot is available then a status of -EBUSY is returned and buf is |
| 58 | // unmodified. |
| 59 | virtual status_t dequeueBuffer(int *buf); |
| 60 | |
| 61 | virtual status_t queueBuffer(int buf); |
| 62 | virtual void cancelBuffer(int buf); |
| 63 | virtual status_t setCrop(const Rect& reg); |
| 64 | virtual status_t setTransform(uint32_t transform); |
| 65 | |
| 66 | // updateTexImage sets the image contents of the target texture to that of |
| 67 | // the most recently queued buffer. |
| 68 | // |
| 69 | // This call may only be made while the OpenGL ES context to which the |
| 70 | // target texture belongs is bound to the calling thread. |
| 71 | status_t updateTexImage(); |
| 72 | |
| 73 | private: |
| 74 | |
| 75 | // freeAllBuffers frees the resources (both GraphicBuffer and EGLImage) for |
| 76 | // all slots. |
| 77 | void freeAllBuffers(); |
| 78 | |
| 79 | // createImage creates a new EGLImage from a GraphicBuffer. |
| 80 | EGLImageKHR createImage(EGLDisplay dpy, |
| 81 | const sp<GraphicBuffer>& graphicBuffer); |
| 82 | |
| 83 | enum { INVALID_BUFFER_SLOT = -1 }; |
| 84 | |
| 85 | struct BufferSlot { |
| 86 | // mGraphicBuffer points to the buffer allocated for this slot or is NULL |
| 87 | // if no buffer has been allocated. |
| 88 | sp<GraphicBuffer> mGraphicBuffer; |
| 89 | |
| 90 | // mEglImage is the EGLImage created from mGraphicBuffer. |
| 91 | EGLImageKHR mEglImage; |
| 92 | |
| 93 | // mEglDisplay is the EGLDisplay used to create mEglImage. |
| 94 | EGLDisplay mEglDisplay; |
| 95 | |
| 96 | // mOwnedByClient indicates whether the slot is currently accessible to a |
| 97 | // client and should not be used by the SurfaceTexture object. It gets |
| 98 | // set to true when dequeueBuffer returns the slot and is reset to false |
| 99 | // when the client calls either queueBuffer or cancelBuffer on the slot. |
| 100 | bool mOwnedByClient; |
| 101 | }; |
| 102 | |
| 103 | // mSlots is the array of buffer slots that must be mirrored on the client |
| 104 | // side. This allows buffer ownership to be transferred between the client |
| 105 | // and server without sending a GraphicBuffer over binder. The entire array |
| 106 | // is initialized to NULL at construction time, and buffers are allocated |
| 107 | // for a slot when requestBuffer is called with that slot's index. |
| 108 | BufferSlot mSlots[NUM_BUFFER_SLOTS]; |
| 109 | |
| 110 | // mBufferCount is the number of buffer slots that the client and server |
| 111 | // must maintain. It defaults to MIN_BUFFER_SLOTS and can be changed by |
| 112 | // calling setBufferCount. |
| 113 | int mBufferCount; |
| 114 | |
| 115 | // mCurrentTexture is the buffer slot index of the buffer that is currently |
Jamie Gennis | 67eedd7 | 2011-01-09 13:25:39 -0800 | [diff] [blame] | 116 | // bound to the OpenGL texture. It is initialized to INVALID_BUFFER_SLOT, |
| 117 | // indicating that no buffer slot is currently bound to the texture. Note, |
| 118 | // however, that a value of INVALID_BUFFER_SLOT does not necessarily mean |
| 119 | // that no buffer is bound to the texture. A call to setBufferCount will |
| 120 | // reset mCurrentTexture to INVALID_BUFFER_SLOT. |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 121 | int mCurrentTexture; |
| 122 | |
| 123 | // mLastQueued is the buffer slot index of the most recently enqueued buffer. |
| 124 | // At construction time it is initialized to INVALID_BUFFER_SLOT, and is |
| 125 | // updated each time queueBuffer is called. |
| 126 | int mLastQueued; |
| 127 | |
| 128 | // mTexName is the name of the OpenGL texture to which streamed images will |
| 129 | // be bound when updateTexImage is called. It is set at construction time |
| 130 | // changed with a call to setTexName. |
| 131 | const GLuint mTexName; |
| 132 | |
| 133 | // mMutex is the mutex used to prevent concurrent access to the member |
| 134 | // variables of SurfaceTexture objects. It must be locked whenever the |
| 135 | // member variables are accessed. |
| 136 | Mutex mMutex; |
| 137 | }; |
| 138 | |
| 139 | // ---------------------------------------------------------------------------- |
| 140 | }; // namespace android |
| 141 | |
| 142 | #endif // ANDROID_GUI_SURFACETEXTURE_H |