blob: f4e2a676160fa1e4204f7c2980967694129536ac [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"
Jamie Gennise70d8b42011-01-09 13:24:09 -080018//#define LOG_NDEBUG 0
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080019
20#define GL_GLEXT_PROTOTYPES
21#define EGL_EGLEXT_PROTOTYPES
22
23#include <EGL/egl.h>
24#include <EGL/eglext.h>
25#include <GLES2/gl2.h>
26#include <GLES2/gl2ext.h>
27
28#include <gui/SurfaceTexture.h>
29
30#include <surfaceflinger/ISurfaceComposer.h>
31#include <surfaceflinger/SurfaceComposerClient.h>
Jamie Gennis9a78c902011-01-12 18:30:40 -080032#include <surfaceflinger/IGraphicBufferAlloc.h>
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080033
34#include <utils/Log.h>
35
36namespace android {
37
Jamie Gennisf238e282011-01-09 16:33:17 -080038// Transform matrices
39static float mtxIdentity[16] = {
40 1, 0, 0, 0,
41 0, 1, 0, 0,
42 0, 0, 1, 0,
43 0, 0, 0, 1,
44};
45static float mtxFlipH[16] = {
46 -1, 0, 0, 0,
47 0, 1, 0, 0,
48 0, 0, 1, 0,
49 1, 0, 0, 1,
50};
51static float mtxFlipV[16] = {
52 1, 0, 0, 0,
53 0, -1, 0, 0,
54 0, 0, 1, 0,
55 0, 1, 0, 1,
56};
57static float mtxRot90[16] = {
58 0, 1, 0, 0,
59 -1, 0, 0, 0,
60 0, 0, 1, 0,
61 1, 0, 0, 1,
62};
63static float mtxRot180[16] = {
64 -1, 0, 0, 0,
65 0, -1, 0, 0,
66 0, 0, 1, 0,
67 1, 1, 0, 1,
68};
69static float mtxRot270[16] = {
70 0, -1, 0, 0,
71 1, 0, 0, 0,
72 0, 0, 1, 0,
73 0, 1, 0, 1,
74};
75
76static void mtxMul(float out[16], const float a[16], const float b[16]);
77
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080078SurfaceTexture::SurfaceTexture(GLuint tex) :
Mathias Agopiana5c75c02011-03-31 19:10:24 -070079 mDefaultWidth(1),
80 mDefaultHeight(1),
81 mPixelFormat(PIXEL_FORMAT_RGBA_8888),
82 mUseDefaultSize(true),
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -080083 mBufferCount(MIN_BUFFER_SLOTS),
84 mCurrentTexture(INVALID_BUFFER_SLOT),
85 mCurrentTransform(0),
86 mCurrentTimestamp(0),
87 mLastQueued(INVALID_BUFFER_SLOT),
88 mLastQueuedTransform(0),
89 mLastQueuedTimestamp(0),
90 mNextTransform(0),
91 mTexName(tex) {
Jamie Gennise70d8b42011-01-09 13:24:09 -080092 LOGV("SurfaceTexture::SurfaceTexture");
Jamie Gennis3461e0f2011-01-07 16:05:47 -080093 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
94 mSlots[i].mEglImage = EGL_NO_IMAGE_KHR;
95 mSlots[i].mEglDisplay = EGL_NO_DISPLAY;
96 mSlots[i].mOwnedByClient = false;
97 }
Jamie Gennis9a78c902011-01-12 18:30:40 -080098 sp<ISurfaceComposer> composer(ComposerService::getComposerService());
99 mGraphicBufferAlloc = composer->createGraphicBufferAlloc();
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800100}
101
102SurfaceTexture::~SurfaceTexture() {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800103 LOGV("SurfaceTexture::~SurfaceTexture");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800104 freeAllBuffers();
105}
106
107status_t SurfaceTexture::setBufferCount(int bufferCount) {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800108 LOGV("SurfaceTexture::setBufferCount");
Jamie Gennis9d4d6c12011-02-27 14:10:20 -0800109
110 if (bufferCount < MIN_BUFFER_SLOTS) {
111 return BAD_VALUE;
112 }
113
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800114 Mutex::Autolock lock(mMutex);
115 freeAllBuffers();
116 mBufferCount = bufferCount;
Jamie Gennis67eedd72011-01-09 13:25:39 -0800117 mCurrentTexture = INVALID_BUFFER_SLOT;
118 mLastQueued = INVALID_BUFFER_SLOT;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800119 return OK;
120}
121
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700122status_t SurfaceTexture::setDefaultBufferSize(uint32_t w, uint32_t h)
123{
124 Mutex::Autolock lock(mMutex);
125 if ((w != mDefaultWidth) || (h != mDefaultHeight)) {
126 mDefaultWidth = w;
127 mDefaultHeight = h;
128 }
129 return OK;
130}
131
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800132sp<GraphicBuffer> SurfaceTexture::requestBuffer(int buf,
133 uint32_t w, uint32_t h, uint32_t format, uint32_t usage) {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800134 LOGV("SurfaceTexture::requestBuffer");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800135 Mutex::Autolock lock(mMutex);
136 if (buf < 0 || mBufferCount <= buf) {
137 LOGE("requestBuffer: slot index out of range [0, %d]: %d",
138 mBufferCount, buf);
139 return 0;
140 }
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700141 if ((w && !h) || (!w & h)) {
142 LOGE("requestBuffer: invalid size: w=%u, h=%u: %d", w, h, buf);
143 return 0;
144 }
145
146 const bool useDefaultSize = !w && !h;
147 if (useDefaultSize) {
148 // use the default size
149 w = mDefaultWidth;
150 h = mDefaultHeight;
151 }
152
153 const bool updateFormat = (format != 0);
154 if (!updateFormat) {
155 // keep the current (or default) format
156 format = mPixelFormat;
157 }
158
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800159 usage |= GraphicBuffer::USAGE_HW_TEXTURE;
Jamie Gennis9a78c902011-01-12 18:30:40 -0800160 sp<GraphicBuffer> graphicBuffer(
161 mGraphicBufferAlloc->createGraphicBuffer(w, h, format, usage));
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800162 if (graphicBuffer == 0) {
163 LOGE("requestBuffer: SurfaceComposer::createGraphicBuffer failed");
164 } else {
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700165 mUseDefaultSize = useDefaultSize;
166 if (updateFormat) {
167 mPixelFormat = format;
168 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800169 mSlots[buf].mGraphicBuffer = graphicBuffer;
170 if (mSlots[buf].mEglImage != EGL_NO_IMAGE_KHR) {
171 eglDestroyImageKHR(mSlots[buf].mEglDisplay, mSlots[buf].mEglImage);
172 mSlots[buf].mEglImage = EGL_NO_IMAGE_KHR;
173 mSlots[buf].mEglDisplay = EGL_NO_DISPLAY;
174 }
Jamie Gennis9a78c902011-01-12 18:30:40 -0800175 mAllocdBuffers.add(graphicBuffer);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800176 }
177 return graphicBuffer;
178}
179
180status_t SurfaceTexture::dequeueBuffer(int *buf) {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800181 LOGV("SurfaceTexture::dequeueBuffer");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800182 Mutex::Autolock lock(mMutex);
183 int found = INVALID_BUFFER_SLOT;
184 for (int i = 0; i < mBufferCount; i++) {
Jamie Gennis235c4242011-01-11 15:00:09 -0800185 if (!mSlots[i].mOwnedByClient && i != mCurrentTexture && i != mLastQueued) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800186 mSlots[i].mOwnedByClient = true;
187 found = i;
188 break;
189 }
190 }
191 if (found == INVALID_BUFFER_SLOT) {
192 return -EBUSY;
193 }
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700194
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800195 *buf = found;
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700196
197 const sp<GraphicBuffer>& buffer(mSlots[found].mGraphicBuffer);
198 if (buffer == NULL) {
199 return ISurfaceTexture::BUFFER_NEEDS_REALLOCATION;
200 }
201 if ((mUseDefaultSize) &&
202 ((uint32_t(buffer->width) != mDefaultWidth) ||
203 (uint32_t(buffer->height) != mDefaultHeight))) {
204 return ISurfaceTexture::BUFFER_NEEDS_REALLOCATION;
205 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800206 return OK;
207}
208
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800209status_t SurfaceTexture::queueBuffer(int buf, int64_t timestamp) {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800210 LOGV("SurfaceTexture::queueBuffer");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800211 Mutex::Autolock lock(mMutex);
212 if (buf < 0 || mBufferCount <= buf) {
213 LOGE("queueBuffer: slot index out of range [0, %d]: %d",
214 mBufferCount, buf);
215 return -EINVAL;
216 } else if (!mSlots[buf].mOwnedByClient) {
217 LOGE("queueBuffer: slot %d is not owned by the client", buf);
218 return -EINVAL;
219 } else if (mSlots[buf].mGraphicBuffer == 0) {
220 LOGE("queueBuffer: slot %d was enqueued without requesting a buffer",
221 buf);
222 return -EINVAL;
223 }
224 mSlots[buf].mOwnedByClient = false;
225 mLastQueued = buf;
Jamie Gennisf238e282011-01-09 16:33:17 -0800226 mLastQueuedCrop = mNextCrop;
227 mLastQueuedTransform = mNextTransform;
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800228 mLastQueuedTimestamp = timestamp;
Jamie Gennisc4d4aea2011-01-13 14:43:36 -0800229 if (mFrameAvailableListener != 0) {
230 mFrameAvailableListener->onFrameAvailable();
231 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800232 return OK;
233}
234
235void SurfaceTexture::cancelBuffer(int buf) {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800236 LOGV("SurfaceTexture::cancelBuffer");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800237 Mutex::Autolock lock(mMutex);
238 if (buf < 0 || mBufferCount <= buf) {
239 LOGE("cancelBuffer: slot index out of range [0, %d]: %d", mBufferCount,
240 buf);
241 return;
242 } else if (!mSlots[buf].mOwnedByClient) {
243 LOGE("cancelBuffer: slot %d is not owned by the client", buf);
244 return;
245 }
246 mSlots[buf].mOwnedByClient = false;
247}
248
Jamie Gennisf238e282011-01-09 16:33:17 -0800249status_t SurfaceTexture::setCrop(const Rect& crop) {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800250 LOGV("SurfaceTexture::setCrop");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800251 Mutex::Autolock lock(mMutex);
Jamie Gennisf238e282011-01-09 16:33:17 -0800252 mNextCrop = crop;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800253 return OK;
254}
255
256status_t SurfaceTexture::setTransform(uint32_t transform) {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800257 LOGV("SurfaceTexture::setTransform");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800258 Mutex::Autolock lock(mMutex);
Jamie Gennisf238e282011-01-09 16:33:17 -0800259 mNextTransform = transform;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800260 return OK;
261}
262
263status_t SurfaceTexture::updateTexImage() {
Jamie Gennise70d8b42011-01-09 13:24:09 -0800264 LOGV("SurfaceTexture::updateTexImage");
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800265 Mutex::Autolock lock(mMutex);
266
267 // We always bind the texture even if we don't update its contents.
268 glBindTexture(GL_TEXTURE_EXTERNAL_OES, mTexName);
269
270 // Initially both mCurrentTexture and mLastQueued are INVALID_BUFFER_SLOT,
271 // so this check will fail until a buffer gets queued.
272 if (mCurrentTexture != mLastQueued) {
Jamie Gennisf238e282011-01-09 16:33:17 -0800273 // Update the GL texture object.
Jamie Gennis9a78c902011-01-12 18:30:40 -0800274 EGLImageKHR image = mSlots[mLastQueued].mEglImage;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800275 if (image == EGL_NO_IMAGE_KHR) {
276 EGLDisplay dpy = eglGetCurrentDisplay();
Jamie Gennis9a78c902011-01-12 18:30:40 -0800277 sp<GraphicBuffer> graphicBuffer = mSlots[mLastQueued].mGraphicBuffer;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800278 image = createImage(dpy, graphicBuffer);
Jamie Gennis9a78c902011-01-12 18:30:40 -0800279 mSlots[mLastQueued].mEglImage = image;
280 mSlots[mLastQueued].mEglDisplay = dpy;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800281 }
Jamie Gennis0eb88512011-01-26 11:52:02 -0800282
283 GLint error;
284 while ((error = glGetError()) != GL_NO_ERROR) {
285 LOGE("GL error cleared before updating SurfaceTexture: %#04x", error);
286 }
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800287 glEGLImageTargetTexture2DOES(GL_TEXTURE_EXTERNAL_OES, (GLeglImageOES)image);
Jamie Gennis0eb88512011-01-26 11:52:02 -0800288 bool failed = false;
289 while ((error = glGetError()) != GL_NO_ERROR) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800290 LOGE("error binding external texture image %p (slot %d): %#04x",
Jamie Gennis9a78c902011-01-12 18:30:40 -0800291 image, mLastQueued, error);
Jamie Gennis0eb88512011-01-26 11:52:02 -0800292 failed = true;
293 }
294 if (failed) {
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800295 return -EINVAL;
296 }
Jamie Gennis9a78c902011-01-12 18:30:40 -0800297
298 // Update the SurfaceTexture state.
299 mCurrentTexture = mLastQueued;
300 mCurrentTextureBuf = mSlots[mCurrentTexture].mGraphicBuffer;
301 mCurrentCrop = mLastQueuedCrop;
302 mCurrentTransform = mLastQueuedTransform;
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800303 mCurrentTimestamp = mLastQueuedTimestamp;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800304 }
305 return OK;
306}
307
Jamie Gennisf238e282011-01-09 16:33:17 -0800308void SurfaceTexture::getTransformMatrix(float mtx[16]) {
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800309 LOGV("SurfaceTexture::getTransformMatrix");
Jamie Gennisf238e282011-01-09 16:33:17 -0800310 Mutex::Autolock lock(mMutex);
311
Jamie Gennisa214c642011-01-14 13:53:31 -0800312 float xform[16];
313 for (int i = 0; i < 16; i++) {
314 xform[i] = mtxIdentity[i];
315 }
316 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) {
317 float result[16];
318 mtxMul(result, xform, mtxFlipH);
319 for (int i = 0; i < 16; i++) {
320 xform[i] = result[i];
321 }
322 }
323 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) {
324 float result[16];
325 mtxMul(result, xform, mtxFlipV);
326 for (int i = 0; i < 16; i++) {
327 xform[i] = result[i];
328 }
329 }
330 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
331 float result[16];
332 mtxMul(result, xform, mtxRot90);
333 for (int i = 0; i < 16; i++) {
334 xform[i] = result[i];
335 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800336 }
337
338 sp<GraphicBuffer>& buf(mSlots[mCurrentTexture].mGraphicBuffer);
Jamie Gennisa214c642011-01-14 13:53:31 -0800339 float tx, ty, sx, sy;
340 if (!mCurrentCrop.isEmpty()) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800341 // In order to prevent bilinear sampling at the of the crop rectangle we
342 // may need to shrink it by 2 texels in each direction. Normally this
343 // would just need to take 1/2 a texel off each end, but because the
344 // chroma channels will likely be subsampled we need to chop off a whole
345 // texel. This will cause artifacts if someone does nearest sampling
346 // with 1:1 pixel:texel ratio, but it's impossible to simultaneously
347 // accomodate the bilinear and nearest sampling uses.
348 //
349 // If nearest sampling turns out to be a desirable usage of these
350 // textures then we could add the ability to switch a SurfaceTexture to
351 // nearest-mode. Preferably, however, the image producers (video
352 // decoder, camera, etc.) would simply not use a crop rectangle (or at
353 // least not tell the framework about it) so that the GPU can do the
354 // correct edge behavior.
355 int xshrink = 0, yshrink = 0;
356 if (mCurrentCrop.left > 0) {
357 tx = float(mCurrentCrop.left + 1) / float(buf->getWidth());
358 xshrink++;
359 } else {
360 tx = 0.0f;
361 }
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700362 if (mCurrentCrop.right < int32_t(buf->getWidth())) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800363 xshrink++;
364 }
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700365 if (mCurrentCrop.bottom < int32_t(buf->getHeight())) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800366 ty = (float(buf->getHeight() - mCurrentCrop.bottom) + 1.0f) /
367 float(buf->getHeight());
368 yshrink++;
369 } else {
370 ty = 0.0f;
371 }
372 if (mCurrentCrop.top > 0) {
373 yshrink++;
374 }
375 sx = float(mCurrentCrop.width() - xshrink) / float(buf->getWidth());
376 sy = float(mCurrentCrop.height() - yshrink) / float(buf->getHeight());
Jamie Gennisa214c642011-01-14 13:53:31 -0800377 } else {
378 tx = 0.0f;
379 ty = 0.0f;
380 sx = 1.0f;
381 sy = 1.0f;
382 }
Jamie Gennisf238e282011-01-09 16:33:17 -0800383 float crop[16] = {
Jamie Gennisa214c642011-01-14 13:53:31 -0800384 sx, 0, 0, 0,
385 0, sy, 0, 0,
Jamie Gennisf238e282011-01-09 16:33:17 -0800386 0, 0, 1, 0,
Jamie Gennisd99c0882011-03-10 16:24:46 -0800387 tx, ty, 0, 1,
Jamie Gennisf238e282011-01-09 16:33:17 -0800388 };
389
Jamie Gennisa214c642011-01-14 13:53:31 -0800390 float mtxBeforeFlipV[16];
391 mtxMul(mtxBeforeFlipV, crop, xform);
392
393 // SurfaceFlinger expects the top of its window textures to be at a Y
394 // coordinate of 0, so SurfaceTexture must behave the same way. We don't
395 // want to expose this to applications, however, so we must add an
396 // additional vertical flip to the transform after all the other transforms.
397 mtxMul(mtx, mtxFlipV, mtxBeforeFlipV);
Jamie Gennisf238e282011-01-09 16:33:17 -0800398}
399
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800400nsecs_t SurfaceTexture::getTimestamp() {
401 LOGV("SurfaceTexture::getTimestamp");
402 Mutex::Autolock lock(mMutex);
403 return mCurrentTimestamp;
404}
405
Jamie Gennisc4d4aea2011-01-13 14:43:36 -0800406void SurfaceTexture::setFrameAvailableListener(
407 const sp<FrameAvailableListener>& l) {
408 LOGV("SurfaceTexture::setFrameAvailableListener");
409 Mutex::Autolock lock(mMutex);
410 mFrameAvailableListener = l;
411}
412
Jamie Gennis1b20cde2011-02-02 15:31:47 -0800413sp<IBinder> SurfaceTexture::getAllocator() {
414 LOGV("SurfaceTexture::getAllocator");
415 return mGraphicBufferAlloc->asBinder();
416}
417
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800418void SurfaceTexture::freeAllBuffers() {
419 for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
420 mSlots[i].mGraphicBuffer = 0;
421 mSlots[i].mOwnedByClient = false;
422 if (mSlots[i].mEglImage != EGL_NO_IMAGE_KHR) {
423 eglDestroyImageKHR(mSlots[i].mEglDisplay, mSlots[i].mEglImage);
424 mSlots[i].mEglImage = EGL_NO_IMAGE_KHR;
425 mSlots[i].mEglDisplay = EGL_NO_DISPLAY;
426 }
427 }
Jamie Gennis9a78c902011-01-12 18:30:40 -0800428
429 int exceptBuf = -1;
430 for (size_t i = 0; i < mAllocdBuffers.size(); i++) {
431 if (mAllocdBuffers[i] == mCurrentTextureBuf) {
432 exceptBuf = i;
433 break;
434 }
435 }
436 mAllocdBuffers.clear();
437 if (exceptBuf >= 0) {
438 mAllocdBuffers.add(mCurrentTextureBuf);
439 }
440 mGraphicBufferAlloc->freeAllGraphicBuffersExcept(exceptBuf);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800441}
442
443EGLImageKHR SurfaceTexture::createImage(EGLDisplay dpy,
444 const sp<GraphicBuffer>& graphicBuffer) {
445 EGLClientBuffer cbuf = (EGLClientBuffer)graphicBuffer->getNativeBuffer();
446 EGLint attrs[] = {
447 EGL_IMAGE_PRESERVED_KHR, EGL_TRUE,
448 EGL_NONE,
449 };
450 EGLImageKHR image = eglCreateImageKHR(dpy, EGL_NO_CONTEXT,
451 EGL_NATIVE_BUFFER_ANDROID, cbuf, attrs);
452 EGLint error = eglGetError();
453 if (error != EGL_SUCCESS) {
454 LOGE("error creating EGLImage: %#x", error);
455 } else if (image == EGL_NO_IMAGE_KHR) {
456 LOGE("no error reported, but no image was returned by "
457 "eglCreateImageKHR");
458 }
459 return image;
460}
461
Jamie Gennisf238e282011-01-09 16:33:17 -0800462static void mtxMul(float out[16], const float a[16], const float b[16]) {
463 out[0] = a[0]*b[0] + a[4]*b[1] + a[8]*b[2] + a[12]*b[3];
464 out[1] = a[1]*b[0] + a[5]*b[1] + a[9]*b[2] + a[13]*b[3];
465 out[2] = a[2]*b[0] + a[6]*b[1] + a[10]*b[2] + a[14]*b[3];
466 out[3] = a[3]*b[0] + a[7]*b[1] + a[11]*b[2] + a[15]*b[3];
467
468 out[4] = a[0]*b[4] + a[4]*b[5] + a[8]*b[6] + a[12]*b[7];
469 out[5] = a[1]*b[4] + a[5]*b[5] + a[9]*b[6] + a[13]*b[7];
470 out[6] = a[2]*b[4] + a[6]*b[5] + a[10]*b[6] + a[14]*b[7];
471 out[7] = a[3]*b[4] + a[7]*b[5] + a[11]*b[6] + a[15]*b[7];
472
473 out[8] = a[0]*b[8] + a[4]*b[9] + a[8]*b[10] + a[12]*b[11];
474 out[9] = a[1]*b[8] + a[5]*b[9] + a[9]*b[10] + a[13]*b[11];
475 out[10] = a[2]*b[8] + a[6]*b[9] + a[10]*b[10] + a[14]*b[11];
476 out[11] = a[3]*b[8] + a[7]*b[9] + a[11]*b[10] + a[15]*b[11];
477
478 out[12] = a[0]*b[12] + a[4]*b[13] + a[8]*b[14] + a[12]*b[15];
479 out[13] = a[1]*b[12] + a[5]*b[13] + a[9]*b[14] + a[13]*b[15];
480 out[14] = a[2]*b[12] + a[6]*b[13] + a[10]*b[14] + a[14]*b[15];
481 out[15] = a[3]*b[12] + a[7]*b[13] + a[11]*b[14] + a[15]*b[15];
482}
483
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800484}; // namespace android