blob: 9579996342cc98595f753467332d4341a9641a8d [file] [log] [blame]
Jamie Gennis8ba32fa2010-12-20 11:27:26 -08001/*
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#define LOG_TAG "SurfaceTexture"
18
19#define GL_GLEXT_PROTOTYPES
20#define EGL_EGLEXT_PROTOTYPES
21
22#include <EGL/egl.h>
23#include <EGL/eglext.h>
24#include <GLES2/gl2.h>
25#include <GLES2/gl2ext.h>
26
27#include <gui/SurfaceTexture.h>
28
29#include <surfaceflinger/ISurfaceComposer.h>
30#include <surfaceflinger/SurfaceComposerClient.h>
31
32#include <utils/Log.h>
33
34namespace android {
35
36SurfaceTexture::SurfaceTexture(GLuint tex) :
37 mBufferCount(MIN_BUFFER_SLOTS), mCurrentTexture(INVALID_BUFFER_SLOT),
38 mLastQueued(INVALID_BUFFER_SLOT), mTexName(tex) {
39}
40
41SurfaceTexture::~SurfaceTexture() {
42 freeAllBuffers();
43}
44
45status_t SurfaceTexture::setBufferCount(int bufferCount) {
46 Mutex::Autolock lock(mMutex);
47 freeAllBuffers();
48 mBufferCount = bufferCount;
49 return OK;
50}
51
52sp<GraphicBuffer> SurfaceTexture::requestBuffer(int buf,
53 uint32_t w, uint32_t h, uint32_t format, uint32_t usage) {
54 Mutex::Autolock lock(mMutex);
55 if (buf < 0 || mBufferCount <= buf) {
56 LOGE("requestBuffer: slot index out of range [0, %d]: %d",
57 mBufferCount, buf);
58 return 0;
59 }
60 usage |= GraphicBuffer::USAGE_HW_TEXTURE;
61 sp<ISurfaceComposer> composer(ComposerService::getComposerService());
62 sp<GraphicBuffer> graphicBuffer(composer->createGraphicBuffer(w, h,
63 format, usage));
64 if (graphicBuffer == 0) {
65 LOGE("requestBuffer: SurfaceComposer::createGraphicBuffer failed");
66 } else {
67 mSlots[buf].mGraphicBuffer = graphicBuffer;
68 if (mSlots[buf].mEglImage != EGL_NO_IMAGE_KHR) {
69 eglDestroyImageKHR(mSlots[buf].mEglDisplay, mSlots[buf].mEglImage);
70 mSlots[buf].mEglImage = EGL_NO_IMAGE_KHR;
71 mSlots[buf].mEglDisplay = EGL_NO_DISPLAY;
72 }
73 }
74 return graphicBuffer;
75}
76
77status_t SurfaceTexture::dequeueBuffer(int *buf) {
78 Mutex::Autolock lock(mMutex);
79 int found = INVALID_BUFFER_SLOT;
80 for (int i = 0; i < mBufferCount; i++) {
81 if (!mSlots[i].mOwnedByClient && i != mCurrentTexture) {
82 mSlots[i].mOwnedByClient = true;
83 found = i;
84 break;
85 }
86 }
87 if (found == INVALID_BUFFER_SLOT) {
88 return -EBUSY;
89 }
90 *buf = found;
91 return OK;
92}
93
94status_t SurfaceTexture::queueBuffer(int buf) {
95 Mutex::Autolock lock(mMutex);
96 if (buf < 0 || mBufferCount <= buf) {
97 LOGE("queueBuffer: slot index out of range [0, %d]: %d",
98 mBufferCount, buf);
99 return -EINVAL;
100 } else if (!mSlots[buf].mOwnedByClient) {
101 LOGE("queueBuffer: slot %d is not owned by the client", buf);
102 return -EINVAL;
103 } else if (mSlots[buf].mGraphicBuffer == 0) {
104 LOGE("queueBuffer: slot %d was enqueued without requesting a buffer",
105 buf);
106 return -EINVAL;
107 }
108 mSlots[buf].mOwnedByClient = false;
109 mLastQueued = buf;
110 return OK;
111}
112
113void SurfaceTexture::cancelBuffer(int buf) {
114 Mutex::Autolock lock(mMutex);
115 if (buf < 0 || mBufferCount <= buf) {
116 LOGE("cancelBuffer: slot index out of range [0, %d]: %d", mBufferCount,
117 buf);
118 return;
119 } else if (!mSlots[buf].mOwnedByClient) {
120 LOGE("cancelBuffer: slot %d is not owned by the client", buf);
121 return;
122 }
123 mSlots[buf].mOwnedByClient = false;
124}
125
126status_t SurfaceTexture::setCrop(const Rect& reg) {
127 Mutex::Autolock lock(mMutex);
128 // XXX: How should we handle crops?
129 return OK;
130}
131
132status_t SurfaceTexture::setTransform(uint32_t transform) {
133 Mutex::Autolock lock(mMutex);
134 // XXX: How should we handle transforms?
135 return OK;
136}
137
138status_t SurfaceTexture::updateTexImage() {
139 Mutex::Autolock lock(mMutex);
140
141 // We always bind the texture even if we don't update its contents.
142 glBindTexture(GL_TEXTURE_EXTERNAL_OES, mTexName);
143
144 // Initially both mCurrentTexture and mLastQueued are INVALID_BUFFER_SLOT,
145 // so this check will fail until a buffer gets queued.
146 if (mCurrentTexture != mLastQueued) {
147 // XXX: Figure out the right target.
148 mCurrentTexture = mLastQueued;
149 EGLImageKHR image = mSlots[mCurrentTexture].mEglImage;
150 if (image == EGL_NO_IMAGE_KHR) {
151 EGLDisplay dpy = eglGetCurrentDisplay();
152 sp<GraphicBuffer> graphicBuffer = mSlots[mCurrentTexture].mGraphicBuffer;
153 image = createImage(dpy, graphicBuffer);
154 mSlots[mCurrentTexture].mEglImage = image;
155 mSlots[mCurrentTexture].mEglDisplay = dpy;
156 }
157 glEGLImageTargetTexture2DOES(GL_TEXTURE_EXTERNAL_OES, (GLeglImageOES)image);
158 GLint error = glGetError();
159 if (error != GL_NO_ERROR) {
160 LOGE("error binding external texture image %p (slot %d): %#04x",
161 image, mCurrentTexture, error);
162 return -EINVAL;
163 }
164 }
165 return OK;
166}
167
168void SurfaceTexture::freeAllBuffers() {
169 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
170 mSlots[i].mGraphicBuffer = 0;
171 mSlots[i].mOwnedByClient = false;
172 if (mSlots[i].mEglImage != EGL_NO_IMAGE_KHR) {
173 eglDestroyImageKHR(mSlots[i].mEglDisplay, mSlots[i].mEglImage);
174 mSlots[i].mEglImage = EGL_NO_IMAGE_KHR;
175 mSlots[i].mEglDisplay = EGL_NO_DISPLAY;
176 }
177 }
178}
179
180EGLImageKHR SurfaceTexture::createImage(EGLDisplay dpy,
181 const sp<GraphicBuffer>& graphicBuffer) {
182 EGLClientBuffer cbuf = (EGLClientBuffer)graphicBuffer->getNativeBuffer();
183 EGLint attrs[] = {
184 EGL_IMAGE_PRESERVED_KHR, EGL_TRUE,
185 EGL_NONE,
186 };
187 EGLImageKHR image = eglCreateImageKHR(dpy, EGL_NO_CONTEXT,
188 EGL_NATIVE_BUFFER_ANDROID, cbuf, attrs);
189 EGLint error = eglGetError();
190 if (error != EGL_SUCCESS) {
191 LOGE("error creating EGLImage: %#x", error);
192 } else if (image == EGL_NO_IMAGE_KHR) {
193 LOGE("no error reported, but no image was returned by "
194 "eglCreateImageKHR");
195 }
196 return image;
197}
198
199}; // namespace android