SurfaceTexture.cpp revision 95dfd05c2c13735e5dbc1ae9e06fdba1053ffd8e
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#define LOG_TAG "SurfaceTexture"
18//#define LOG_NDEBUG 0
19
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 <hardware/hardware.h>
31
32#include <surfaceflinger/ISurfaceComposer.h>
33#include <surfaceflinger/SurfaceComposerClient.h>
34#include <surfaceflinger/IGraphicBufferAlloc.h>
35
36#include <utils/Log.h>
37#include <utils/String8.h>
38
39namespace android {
40
41// Transform matrices
42static float mtxIdentity[16] = {
43    1, 0, 0, 0,
44    0, 1, 0, 0,
45    0, 0, 1, 0,
46    0, 0, 0, 1,
47};
48static float mtxFlipH[16] = {
49    -1, 0, 0, 0,
50    0, 1, 0, 0,
51    0, 0, 1, 0,
52    1, 0, 0, 1,
53};
54static float mtxFlipV[16] = {
55    1, 0, 0, 0,
56    0, -1, 0, 0,
57    0, 0, 1, 0,
58    0, 1, 0, 1,
59};
60static float mtxRot90[16] = {
61    0, 1, 0, 0,
62    -1, 0, 0, 0,
63    0, 0, 1, 0,
64    1, 0, 0, 1,
65};
66static float mtxRot180[16] = {
67    -1, 0, 0, 0,
68    0, -1, 0, 0,
69    0, 0, 1, 0,
70    1, 1, 0, 1,
71};
72static float mtxRot270[16] = {
73    0, -1, 0, 0,
74    1, 0, 0, 0,
75    0, 0, 1, 0,
76    0, 1, 0, 1,
77};
78
79static void mtxMul(float out[16], const float a[16], const float b[16]);
80
81SurfaceTexture::SurfaceTexture(GLuint tex, bool allowSynchronousMode) :
82    mDefaultWidth(1),
83    mDefaultHeight(1),
84    mPixelFormat(PIXEL_FORMAT_RGBA_8888),
85    mBufferCount(MIN_ASYNC_BUFFER_SLOTS),
86    mClientBufferCount(0),
87    mServerBufferCount(MIN_ASYNC_BUFFER_SLOTS),
88    mCurrentTexture(INVALID_BUFFER_SLOT),
89    mCurrentTransform(0),
90    mCurrentTimestamp(0),
91    mNextTransform(0),
92    mNextScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE),
93    mTexName(tex),
94    mSynchronousMode(false),
95    mAllowSynchronousMode(allowSynchronousMode),
96    mConnectedApi(NO_CONNECTED_API),
97    mAbandoned(false) {
98    LOGV("SurfaceTexture::SurfaceTexture");
99    sp<ISurfaceComposer> composer(ComposerService::getComposerService());
100    mGraphicBufferAlloc = composer->createGraphicBufferAlloc();
101    mNextCrop.makeInvalid();
102    memcpy(mCurrentTransformMatrix, mtxIdentity, sizeof(mCurrentTransformMatrix));
103}
104
105SurfaceTexture::~SurfaceTexture() {
106    LOGV("SurfaceTexture::~SurfaceTexture");
107    freeAllBuffers();
108}
109
110status_t SurfaceTexture::setBufferCountServerLocked(int bufferCount) {
111    if (bufferCount > NUM_BUFFER_SLOTS)
112        return BAD_VALUE;
113
114    // special-case, nothing to do
115    if (bufferCount == mBufferCount)
116        return OK;
117
118    if (!mClientBufferCount &&
119        bufferCount >= mBufferCount) {
120        // easy, we just have more buffers
121        mBufferCount = bufferCount;
122        mServerBufferCount = bufferCount;
123        mDequeueCondition.signal();
124    } else {
125        // we're here because we're either
126        // - reducing the number of available buffers
127        // - or there is a client-buffer-count in effect
128
129        // less than 2 buffers is never allowed
130        if (bufferCount < 2)
131            return BAD_VALUE;
132
133        // when there is non client-buffer-count in effect, the client is not
134        // allowed to dequeue more than one buffer at a time,
135        // so the next time they dequeue a buffer, we know that they don't
136        // own one. the actual resizing will happen during the next
137        // dequeueBuffer.
138
139        mServerBufferCount = bufferCount;
140    }
141    return OK;
142}
143
144status_t SurfaceTexture::setBufferCountServer(int bufferCount) {
145    Mutex::Autolock lock(mMutex);
146    return setBufferCountServerLocked(bufferCount);
147}
148
149status_t SurfaceTexture::setBufferCount(int bufferCount) {
150    LOGV("SurfaceTexture::setBufferCount");
151    Mutex::Autolock lock(mMutex);
152
153    if (mAbandoned) {
154        LOGE("setBufferCount: SurfaceTexture has been abandoned!");
155        return NO_INIT;
156    }
157
158    if (bufferCount > NUM_BUFFER_SLOTS) {
159        LOGE("setBufferCount: bufferCount larger than slots available");
160        return BAD_VALUE;
161    }
162
163    // Error out if the user has dequeued buffers
164    for (int i=0 ; i<mBufferCount ; i++) {
165        if (mSlots[i].mBufferState == BufferSlot::DEQUEUED) {
166            LOGE("setBufferCount: client owns some buffers");
167            return -EINVAL;
168        }
169    }
170
171    const int minBufferSlots = mSynchronousMode ?
172            MIN_SYNC_BUFFER_SLOTS : MIN_ASYNC_BUFFER_SLOTS;
173    if (bufferCount == 0) {
174        mClientBufferCount = 0;
175        bufferCount = (mServerBufferCount >= minBufferSlots) ?
176                mServerBufferCount : minBufferSlots;
177        return setBufferCountServerLocked(bufferCount);
178    }
179
180    if (bufferCount < minBufferSlots) {
181        LOGE("setBufferCount: requested buffer count (%d) is less than "
182                "minimum (%d)", bufferCount, minBufferSlots);
183        return BAD_VALUE;
184    }
185
186    // here we're guaranteed that the client doesn't have dequeued buffers
187    // and will release all of its buffer references.
188    freeAllBuffers();
189    mBufferCount = bufferCount;
190    mClientBufferCount = bufferCount;
191    mCurrentTexture = INVALID_BUFFER_SLOT;
192    mQueue.clear();
193    mDequeueCondition.signal();
194    return OK;
195}
196
197status_t SurfaceTexture::setDefaultBufferSize(uint32_t w, uint32_t h)
198{
199    if (!w || !h) {
200        LOGE("setDefaultBufferSize: dimensions cannot be 0 (w=%d, h=%d)", w, h);
201        return BAD_VALUE;
202    }
203
204    Mutex::Autolock lock(mMutex);
205    mDefaultWidth = w;
206    mDefaultHeight = h;
207    return OK;
208}
209
210status_t SurfaceTexture::requestBuffer(int slot, sp<GraphicBuffer>* buf) {
211    LOGV("SurfaceTexture::requestBuffer");
212    Mutex::Autolock lock(mMutex);
213    if (mAbandoned) {
214        LOGE("requestBuffer: SurfaceTexture has been abandoned!");
215        return NO_INIT;
216    }
217    if (slot < 0 || mBufferCount <= slot) {
218        LOGE("requestBuffer: slot index out of range [0, %d]: %d",
219                mBufferCount, slot);
220        return BAD_VALUE;
221    }
222    mSlots[slot].mRequestBufferCalled = true;
223    *buf = mSlots[slot].mGraphicBuffer;
224    return NO_ERROR;
225}
226
227status_t SurfaceTexture::dequeueBuffer(int *outBuf, uint32_t w, uint32_t h,
228        uint32_t format, uint32_t usage) {
229    LOGV("SurfaceTexture::dequeueBuffer");
230
231    if (mAbandoned) {
232        LOGE("dequeueBuffer: SurfaceTexture has been abandoned!");
233        return NO_INIT;
234    }
235
236    if ((w && !h) || (!w && h)) {
237        LOGE("dequeueBuffer: invalid size: w=%u, h=%u", w, h);
238        return BAD_VALUE;
239    }
240
241    Mutex::Autolock lock(mMutex);
242
243    status_t returnFlags(OK);
244
245    int found, foundSync;
246    int dequeuedCount = 0;
247    bool tryAgain = true;
248    while (tryAgain) {
249        // We need to wait for the FIFO to drain if the number of buffer
250        // needs to change.
251        //
252        // The condition "number of buffer needs to change" is true if
253        // - the client doesn't care about how many buffers there are
254        // - AND the actual number of buffer is different from what was
255        //   set in the last setBufferCountServer()
256        //                         - OR -
257        //   setBufferCountServer() was set to a value incompatible with
258        //   the synchronization mode (for instance because the sync mode
259        //   changed since)
260        //
261        // As long as this condition is true AND the FIFO is not empty, we
262        // wait on mDequeueCondition.
263
264        int minBufferCountNeeded = mSynchronousMode ?
265                MIN_SYNC_BUFFER_SLOTS : MIN_ASYNC_BUFFER_SLOTS;
266
267        if (!mClientBufferCount &&
268                ((mServerBufferCount != mBufferCount) ||
269                        (mServerBufferCount < minBufferCountNeeded))) {
270            // wait for the FIFO to drain
271            while (!mQueue.isEmpty()) {
272                mDequeueCondition.wait(mMutex);
273                if (mAbandoned) {
274                    LOGE("dequeueBuffer: SurfaceTexture was abandoned while "
275                            "blocked!");
276                    return NO_INIT;
277                }
278            }
279            minBufferCountNeeded = mSynchronousMode ?
280                    MIN_SYNC_BUFFER_SLOTS : MIN_ASYNC_BUFFER_SLOTS;
281        }
282
283
284        if (!mClientBufferCount &&
285                ((mServerBufferCount != mBufferCount) ||
286                        (mServerBufferCount < minBufferCountNeeded))) {
287            // here we're guaranteed that mQueue is empty
288            freeAllBuffers();
289            mBufferCount = mServerBufferCount;
290            if (mBufferCount < minBufferCountNeeded)
291                mBufferCount = minBufferCountNeeded;
292            mCurrentTexture = INVALID_BUFFER_SLOT;
293            returnFlags |= ISurfaceTexture::RELEASE_ALL_BUFFERS;
294        }
295
296        // look for a free buffer to give to the client
297        found = INVALID_BUFFER_SLOT;
298        foundSync = INVALID_BUFFER_SLOT;
299        dequeuedCount = 0;
300        for (int i = 0; i < mBufferCount; i++) {
301            const int state = mSlots[i].mBufferState;
302            if (state == BufferSlot::DEQUEUED) {
303                dequeuedCount++;
304            }
305            if (state == BufferSlot::FREE /*|| i == mCurrentTexture*/) {
306                foundSync = i;
307                if (i != mCurrentTexture) {
308                    found = i;
309                    break;
310                }
311            }
312        }
313
314        // clients are not allowed to dequeue more than one buffer
315        // if they didn't set a buffer count.
316        if (!mClientBufferCount && dequeuedCount) {
317            return -EINVAL;
318        }
319
320        // See whether a buffer has been queued since the last setBufferCount so
321        // we know whether to perform the MIN_UNDEQUEUED_BUFFERS check below.
322        bool bufferHasBeenQueued = mCurrentTexture != INVALID_BUFFER_SLOT;
323        if (bufferHasBeenQueued) {
324            // make sure the client is not trying to dequeue more buffers
325            // than allowed.
326            const int avail = mBufferCount - (dequeuedCount+1);
327            if (avail < (MIN_UNDEQUEUED_BUFFERS-int(mSynchronousMode))) {
328                LOGE("dequeueBuffer: MIN_UNDEQUEUED_BUFFERS=%d exceeded (dequeued=%d)",
329                        MIN_UNDEQUEUED_BUFFERS-int(mSynchronousMode),
330                        dequeuedCount);
331                return -EBUSY;
332            }
333        }
334
335        // we're in synchronous mode and didn't find a buffer, we need to wait
336        // for for some buffers to be consumed
337        tryAgain = mSynchronousMode && (foundSync == INVALID_BUFFER_SLOT);
338        if (tryAgain) {
339            mDequeueCondition.wait(mMutex);
340        }
341    }
342
343    if (mSynchronousMode && found == INVALID_BUFFER_SLOT) {
344        // foundSync guaranteed to be != INVALID_BUFFER_SLOT
345        found = foundSync;
346    }
347
348    if (found == INVALID_BUFFER_SLOT) {
349        return -EBUSY;
350    }
351
352    const int buf = found;
353    *outBuf = found;
354
355    const bool useDefaultSize = !w && !h;
356    if (useDefaultSize) {
357        // use the default size
358        w = mDefaultWidth;
359        h = mDefaultHeight;
360    }
361
362    const bool updateFormat = (format != 0);
363    if (!updateFormat) {
364        // keep the current (or default) format
365        format = mPixelFormat;
366    }
367
368    // buffer is now in DEQUEUED (but can also be current at the same time,
369    // if we're in synchronous mode)
370    mSlots[buf].mBufferState = BufferSlot::DEQUEUED;
371
372    const sp<GraphicBuffer>& buffer(mSlots[buf].mGraphicBuffer);
373    if ((buffer == NULL) ||
374        (uint32_t(buffer->width)  != w) ||
375        (uint32_t(buffer->height) != h) ||
376        (uint32_t(buffer->format) != format) ||
377        ((uint32_t(buffer->usage) & usage) != usage))
378    {
379        usage |= GraphicBuffer::USAGE_HW_TEXTURE;
380        status_t error;
381        sp<GraphicBuffer> graphicBuffer(
382                mGraphicBufferAlloc->createGraphicBuffer(
383                        w, h, format, usage, &error));
384        if (graphicBuffer == 0) {
385            LOGE("dequeueBuffer: SurfaceComposer::createGraphicBuffer failed");
386            return error;
387        }
388        if (updateFormat) {
389            mPixelFormat = format;
390        }
391        mSlots[buf].mGraphicBuffer = graphicBuffer;
392        mSlots[buf].mRequestBufferCalled = false;
393        if (mSlots[buf].mEglImage != EGL_NO_IMAGE_KHR) {
394            eglDestroyImageKHR(mSlots[buf].mEglDisplay, mSlots[buf].mEglImage);
395            mSlots[buf].mEglImage = EGL_NO_IMAGE_KHR;
396            mSlots[buf].mEglDisplay = EGL_NO_DISPLAY;
397        }
398        returnFlags |= ISurfaceTexture::BUFFER_NEEDS_REALLOCATION;
399    }
400    return returnFlags;
401}
402
403status_t SurfaceTexture::setSynchronousMode(bool enabled) {
404    Mutex::Autolock lock(mMutex);
405
406    if (mAbandoned) {
407        LOGE("setSynchronousMode: SurfaceTexture has been abandoned!");
408        return NO_INIT;
409    }
410
411    status_t err = OK;
412    if (!mAllowSynchronousMode && enabled)
413        return err;
414
415    if (!enabled) {
416        // going to asynchronous mode, drain the queue
417        while (mSynchronousMode != enabled && !mQueue.isEmpty()) {
418            mDequeueCondition.wait(mMutex);
419        }
420    }
421
422    if (mSynchronousMode != enabled) {
423        // - if we're going to asynchronous mode, the queue is guaranteed to be
424        // empty here
425        // - if the client set the number of buffers, we're guaranteed that
426        // we have at least 3 (because we don't allow less)
427        mSynchronousMode = enabled;
428        mDequeueCondition.signal();
429    }
430    return err;
431}
432
433status_t SurfaceTexture::queueBuffer(int buf, int64_t timestamp,
434        uint32_t* outWidth, uint32_t* outHeight, uint32_t* outTransform) {
435    LOGV("SurfaceTexture::queueBuffer");
436
437    sp<FrameAvailableListener> listener;
438
439    { // scope for the lock
440        Mutex::Autolock lock(mMutex);
441        if (mAbandoned) {
442            LOGE("queueBuffer: SurfaceTexture has been abandoned!");
443            return NO_INIT;
444        }
445        if (buf < 0 || buf >= mBufferCount) {
446            LOGE("queueBuffer: slot index out of range [0, %d]: %d",
447                    mBufferCount, buf);
448            return -EINVAL;
449        } else if (mSlots[buf].mBufferState != BufferSlot::DEQUEUED) {
450            LOGE("queueBuffer: slot %d is not owned by the client (state=%d)",
451                    buf, mSlots[buf].mBufferState);
452            return -EINVAL;
453        } else if (buf == mCurrentTexture) {
454            LOGE("queueBuffer: slot %d is current!", buf);
455            return -EINVAL;
456        } else if (!mSlots[buf].mRequestBufferCalled) {
457            LOGE("queueBuffer: slot %d was enqueued without requesting a "
458                    "buffer", buf);
459            return -EINVAL;
460        }
461
462        if (mSynchronousMode) {
463            // In synchronous mode we queue all buffers in a FIFO.
464            mQueue.push_back(buf);
465
466            // Synchronous mode always signals that an additional frame should
467            // be consumed.
468            listener = mFrameAvailableListener;
469        } else {
470            // In asynchronous mode we only keep the most recent buffer.
471            if (mQueue.empty()) {
472                mQueue.push_back(buf);
473
474                // Asynchronous mode only signals that a frame should be
475                // consumed if no previous frame was pending. If a frame were
476                // pending then the consumer would have already been notified.
477                listener = mFrameAvailableListener;
478            } else {
479                Fifo::iterator front(mQueue.begin());
480                // buffer currently queued is freed
481                mSlots[*front].mBufferState = BufferSlot::FREE;
482                // and we record the new buffer index in the queued list
483                *front = buf;
484            }
485        }
486
487        mSlots[buf].mBufferState = BufferSlot::QUEUED;
488        mSlots[buf].mCrop = mNextCrop;
489        mSlots[buf].mTransform = mNextTransform;
490        mSlots[buf].mScalingMode = mNextScalingMode;
491        mSlots[buf].mTimestamp = timestamp;
492        mDequeueCondition.signal();
493    } // scope for the lock
494
495    // call back without lock held
496    if (listener != 0) {
497        listener->onFrameAvailable();
498    }
499
500    *outWidth = mDefaultWidth;
501    *outHeight = mDefaultHeight;
502    *outTransform = 0;
503
504    return OK;
505}
506
507void SurfaceTexture::cancelBuffer(int buf) {
508    LOGV("SurfaceTexture::cancelBuffer");
509    Mutex::Autolock lock(mMutex);
510
511    if (mAbandoned) {
512        LOGW("cancelBuffer: SurfaceTexture has been abandoned!");
513        return;
514    }
515
516    if (buf < 0 || buf >= mBufferCount) {
517        LOGE("cancelBuffer: slot index out of range [0, %d]: %d",
518                mBufferCount, buf);
519        return;
520    } else if (mSlots[buf].mBufferState != BufferSlot::DEQUEUED) {
521        LOGE("cancelBuffer: slot %d is not owned by the client (state=%d)",
522                buf, mSlots[buf].mBufferState);
523        return;
524    }
525    mSlots[buf].mBufferState = BufferSlot::FREE;
526    mDequeueCondition.signal();
527}
528
529status_t SurfaceTexture::setCrop(const Rect& crop) {
530    LOGV("SurfaceTexture::setCrop");
531    Mutex::Autolock lock(mMutex);
532    if (mAbandoned) {
533        LOGE("setCrop: SurfaceTexture has been abandoned!");
534        return NO_INIT;
535    }
536    mNextCrop = crop;
537    return OK;
538}
539
540status_t SurfaceTexture::setTransform(uint32_t transform) {
541    LOGV("SurfaceTexture::setTransform");
542    Mutex::Autolock lock(mMutex);
543    if (mAbandoned) {
544        LOGE("setTransform: SurfaceTexture has been abandoned!");
545        return NO_INIT;
546    }
547    mNextTransform = transform;
548    return OK;
549}
550
551status_t SurfaceTexture::connect(int api) {
552    LOGV("SurfaceTexture::connect(this=%p, %d)", this, api);
553    Mutex::Autolock lock(mMutex);
554
555    if (mAbandoned) {
556        LOGE("connect: SurfaceTexture has been abandoned!");
557        return NO_INIT;
558    }
559
560    int err = NO_ERROR;
561    switch (api) {
562        case NATIVE_WINDOW_API_EGL:
563        case NATIVE_WINDOW_API_CPU:
564        case NATIVE_WINDOW_API_MEDIA:
565        case NATIVE_WINDOW_API_CAMERA:
566            if (mConnectedApi != NO_CONNECTED_API) {
567                LOGE("connect: already connected (cur=%d, req=%d)",
568                        mConnectedApi, api);
569                err = -EINVAL;
570            } else {
571                mConnectedApi = api;
572            }
573            break;
574        default:
575            err = -EINVAL;
576            break;
577    }
578    return err;
579}
580
581status_t SurfaceTexture::disconnect(int api) {
582    LOGV("SurfaceTexture::disconnect(this=%p, %d)", this, api);
583    Mutex::Autolock lock(mMutex);
584
585    if (mAbandoned) {
586        LOGE("connect: SurfaceTexture has been abandoned!");
587        return NO_INIT;
588    }
589
590    int err = NO_ERROR;
591    switch (api) {
592        case NATIVE_WINDOW_API_EGL:
593        case NATIVE_WINDOW_API_CPU:
594        case NATIVE_WINDOW_API_MEDIA:
595        case NATIVE_WINDOW_API_CAMERA:
596            if (mConnectedApi == api) {
597                mConnectedApi = NO_CONNECTED_API;
598            } else {
599                LOGE("disconnect: connected to another api (cur=%d, req=%d)",
600                        mConnectedApi, api);
601                err = -EINVAL;
602            }
603            break;
604        default:
605            err = -EINVAL;
606            break;
607    }
608    return err;
609}
610
611status_t SurfaceTexture::setScalingMode(int mode) {
612    LOGV("SurfaceTexture::setScalingMode(%d)", mode);
613
614    switch (mode) {
615        case NATIVE_WINDOW_SCALING_MODE_FREEZE:
616        case NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW:
617            break;
618        default:
619            return BAD_VALUE;
620    }
621
622    Mutex::Autolock lock(mMutex);
623    mNextScalingMode = mode;
624    return OK;
625}
626
627status_t SurfaceTexture::updateTexImage() {
628    LOGV("SurfaceTexture::updateTexImage");
629    Mutex::Autolock lock(mMutex);
630
631    if (mAbandoned) {
632        LOGE("calling updateTexImage() on an abandoned SurfaceTexture");
633        //return NO_INIT;
634    }
635
636    // In asynchronous mode the list is guaranteed to be one buffer
637    // deep, while in synchronous mode we use the oldest buffer.
638    if (!mQueue.empty()) {
639        Fifo::iterator front(mQueue.begin());
640        int buf = *front;
641
642        if (uint32_t(buf) >= NUM_BUFFER_SLOTS) {
643            LOGE("buffer index out of range (index=%d)", buf);
644            //return BAD_VALUE;
645        }
646
647        // Update the GL texture object.
648        EGLImageKHR image = mSlots[buf].mEglImage;
649        if (image == EGL_NO_IMAGE_KHR) {
650            EGLDisplay dpy = eglGetCurrentDisplay();
651
652            if (mSlots[buf].mGraphicBuffer == 0) {
653                LOGE("buffer at slot %d is null", buf);
654                //return BAD_VALUE;
655            }
656
657            image = createImage(dpy, mSlots[buf].mGraphicBuffer);
658            mSlots[buf].mEglImage = image;
659            mSlots[buf].mEglDisplay = dpy;
660            if (image == EGL_NO_IMAGE_KHR) {
661                // NOTE: if dpy was invalid, createImage() is guaranteed to
662                // fail. so we'd end up here.
663                return -EINVAL;
664            }
665        }
666
667        GLint error;
668        while ((error = glGetError()) != GL_NO_ERROR) {
669            LOGW("updateTexImage: clearing GL error: %#04x", error);
670        }
671
672        glBindTexture(GL_TEXTURE_EXTERNAL_OES, mTexName);
673        glEGLImageTargetTexture2DOES(GL_TEXTURE_EXTERNAL_OES, (GLeglImageOES)image);
674
675        bool failed = false;
676        while ((error = glGetError()) != GL_NO_ERROR) {
677            LOGE("error binding external texture image %p (slot %d): %#04x",
678                    image, buf, error);
679            failed = true;
680        }
681        if (failed) {
682            return -EINVAL;
683        }
684
685        if (mCurrentTexture != INVALID_BUFFER_SLOT) {
686            // The current buffer becomes FREE if it was still in the queued
687            // state. If it has already been given to the client
688            // (synchronous mode), then it stays in DEQUEUED state.
689            if (mSlots[mCurrentTexture].mBufferState == BufferSlot::QUEUED)
690                mSlots[mCurrentTexture].mBufferState = BufferSlot::FREE;
691        }
692
693        // Update the SurfaceTexture state.
694        mCurrentTexture = buf;
695        mCurrentTextureBuf = mSlots[buf].mGraphicBuffer;
696        mCurrentCrop = mSlots[buf].mCrop;
697        mCurrentTransform = mSlots[buf].mTransform;
698        mCurrentScalingMode = mSlots[buf].mScalingMode;
699        mCurrentTimestamp = mSlots[buf].mTimestamp;
700        computeCurrentTransformMatrix();
701
702        // Now that we've passed the point at which failures can happen,
703        // it's safe to remove the buffer from the front of the queue.
704        mQueue.erase(front);
705        mDequeueCondition.signal();
706    } else {
707        // We always bind the texture even if we don't update its contents.
708        glBindTexture(GL_TEXTURE_EXTERNAL_OES, mTexName);
709    }
710
711    return OK;
712}
713
714bool SurfaceTexture::isExternalFormat(uint32_t format)
715{
716    switch (format) {
717    // supported YUV formats
718    case HAL_PIXEL_FORMAT_YV12:
719    // Legacy/deprecated YUV formats
720    case HAL_PIXEL_FORMAT_YCbCr_422_SP:
721    case HAL_PIXEL_FORMAT_YCrCb_420_SP:
722    case HAL_PIXEL_FORMAT_YCbCr_422_I:
723        return true;
724    }
725
726    // Any OEM format needs to be considered
727    if (format>=0x100 && format<=0x1FF)
728        return true;
729
730    return false;
731}
732
733GLenum SurfaceTexture::getCurrentTextureTarget() const {
734    return GL_TEXTURE_EXTERNAL_OES;
735}
736
737void SurfaceTexture::getTransformMatrix(float mtx[16]) {
738    Mutex::Autolock lock(mMutex);
739    memcpy(mtx, mCurrentTransformMatrix, sizeof(mCurrentTransformMatrix));
740}
741
742void SurfaceTexture::computeCurrentTransformMatrix() {
743    LOGV("SurfaceTexture::computeCurrentTransformMatrix");
744
745    float xform[16];
746    for (int i = 0; i < 16; i++) {
747        xform[i] = mtxIdentity[i];
748    }
749    if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) {
750        float result[16];
751        mtxMul(result, xform, mtxFlipH);
752        for (int i = 0; i < 16; i++) {
753            xform[i] = result[i];
754        }
755    }
756    if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) {
757        float result[16];
758        mtxMul(result, xform, mtxFlipV);
759        for (int i = 0; i < 16; i++) {
760            xform[i] = result[i];
761        }
762    }
763    if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
764        float result[16];
765        mtxMul(result, xform, mtxRot90);
766        for (int i = 0; i < 16; i++) {
767            xform[i] = result[i];
768        }
769    }
770
771    sp<GraphicBuffer>& buf(mSlots[mCurrentTexture].mGraphicBuffer);
772    float tx, ty, sx, sy;
773    if (!mCurrentCrop.isEmpty()) {
774        // In order to prevent bilinear sampling at the of the crop rectangle we
775        // may need to shrink it by 2 texels in each direction.  Normally this
776        // would just need to take 1/2 a texel off each end, but because the
777        // chroma channels will likely be subsampled we need to chop off a whole
778        // texel.  This will cause artifacts if someone does nearest sampling
779        // with 1:1 pixel:texel ratio, but it's impossible to simultaneously
780        // accomodate the bilinear and nearest sampling uses.
781        //
782        // If nearest sampling turns out to be a desirable usage of these
783        // textures then we could add the ability to switch a SurfaceTexture to
784        // nearest-mode.  Preferably, however, the image producers (video
785        // decoder, camera, etc.) would simply not use a crop rectangle (or at
786        // least not tell the framework about it) so that the GPU can do the
787        // correct edge behavior.
788        int xshrink = 0, yshrink = 0;
789        if (mCurrentCrop.left > 0) {
790            tx = float(mCurrentCrop.left + 1) / float(buf->getWidth());
791            xshrink++;
792        } else {
793            tx = 0.0f;
794        }
795        if (mCurrentCrop.right < int32_t(buf->getWidth())) {
796            xshrink++;
797        }
798        if (mCurrentCrop.bottom < int32_t(buf->getHeight())) {
799            ty = (float(buf->getHeight() - mCurrentCrop.bottom) + 1.0f) /
800                    float(buf->getHeight());
801            yshrink++;
802        } else {
803            ty = 0.0f;
804        }
805        if (mCurrentCrop.top > 0) {
806            yshrink++;
807        }
808        sx = float(mCurrentCrop.width() - xshrink) / float(buf->getWidth());
809        sy = float(mCurrentCrop.height() - yshrink) / float(buf->getHeight());
810    } else {
811        tx = 0.0f;
812        ty = 0.0f;
813        sx = 1.0f;
814        sy = 1.0f;
815    }
816    float crop[16] = {
817        sx, 0, 0, 0,
818        0, sy, 0, 0,
819        0, 0, 1, 0,
820        tx, ty, 0, 1,
821    };
822
823    float mtxBeforeFlipV[16];
824    mtxMul(mtxBeforeFlipV, crop, xform);
825
826    // SurfaceFlinger expects the top of its window textures to be at a Y
827    // coordinate of 0, so SurfaceTexture must behave the same way.  We don't
828    // want to expose this to applications, however, so we must add an
829    // additional vertical flip to the transform after all the other transforms.
830    mtxMul(mCurrentTransformMatrix, mtxFlipV, mtxBeforeFlipV);
831}
832
833nsecs_t SurfaceTexture::getTimestamp() {
834    LOGV("SurfaceTexture::getTimestamp");
835    Mutex::Autolock lock(mMutex);
836    return mCurrentTimestamp;
837}
838
839void SurfaceTexture::setFrameAvailableListener(
840        const sp<FrameAvailableListener>& listener) {
841    LOGV("SurfaceTexture::setFrameAvailableListener");
842    Mutex::Autolock lock(mMutex);
843    mFrameAvailableListener = listener;
844}
845
846void SurfaceTexture::freeAllBuffers() {
847    for (int i = 0; i < NUM_BUFFER_SLOTS; i++) {
848        mSlots[i].mGraphicBuffer = 0;
849        mSlots[i].mBufferState = BufferSlot::FREE;
850        if (mSlots[i].mEglImage != EGL_NO_IMAGE_KHR) {
851            eglDestroyImageKHR(mSlots[i].mEglDisplay, mSlots[i].mEglImage);
852            mSlots[i].mEglImage = EGL_NO_IMAGE_KHR;
853            mSlots[i].mEglDisplay = EGL_NO_DISPLAY;
854        }
855    }
856}
857
858EGLImageKHR SurfaceTexture::createImage(EGLDisplay dpy,
859        const sp<GraphicBuffer>& graphicBuffer) {
860    EGLClientBuffer cbuf = (EGLClientBuffer)graphicBuffer->getNativeBuffer();
861    EGLint attrs[] = {
862        EGL_IMAGE_PRESERVED_KHR,    EGL_TRUE,
863        EGL_NONE,
864    };
865    EGLImageKHR image = eglCreateImageKHR(dpy, EGL_NO_CONTEXT,
866            EGL_NATIVE_BUFFER_ANDROID, cbuf, attrs);
867    if (image == EGL_NO_IMAGE_KHR) {
868        EGLint error = eglGetError();
869        LOGE("error creating EGLImage: %#x", error);
870    }
871    return image;
872}
873
874sp<GraphicBuffer> SurfaceTexture::getCurrentBuffer() const {
875    Mutex::Autolock lock(mMutex);
876    return mCurrentTextureBuf;
877}
878
879Rect SurfaceTexture::getCurrentCrop() const {
880    Mutex::Autolock lock(mMutex);
881    return mCurrentCrop;
882}
883
884uint32_t SurfaceTexture::getCurrentTransform() const {
885    Mutex::Autolock lock(mMutex);
886    return mCurrentTransform;
887}
888
889uint32_t SurfaceTexture::getCurrentScalingMode() const {
890    Mutex::Autolock lock(mMutex);
891    return mCurrentScalingMode;
892}
893
894int SurfaceTexture::query(int what, int* outValue)
895{
896    Mutex::Autolock lock(mMutex);
897
898    if (mAbandoned) {
899        LOGE("query: SurfaceTexture has been abandoned!");
900        return NO_INIT;
901    }
902
903    int value;
904    switch (what) {
905    case NATIVE_WINDOW_WIDTH:
906        value = mDefaultWidth;
907        break;
908    case NATIVE_WINDOW_HEIGHT:
909        value = mDefaultHeight;
910        break;
911    case NATIVE_WINDOW_FORMAT:
912        value = mPixelFormat;
913        break;
914    case NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS:
915        value = mSynchronousMode ?
916                (MIN_UNDEQUEUED_BUFFERS-1) : MIN_UNDEQUEUED_BUFFERS;
917        break;
918    default:
919        return BAD_VALUE;
920    }
921    outValue[0] = value;
922    return NO_ERROR;
923}
924
925void SurfaceTexture::abandon() {
926    Mutex::Autolock lock(mMutex);
927    freeAllBuffers();
928    mAbandoned = true;
929    mCurrentTextureBuf.clear();
930    mDequeueCondition.signal();
931}
932
933void SurfaceTexture::dump(String8& result) const
934{
935    char buffer[1024];
936    dump(result, "", buffer, 1024);
937}
938
939void SurfaceTexture::dump(String8& result, const char* prefix,
940        char* buffer, size_t SIZE) const
941{
942    Mutex::Autolock _l(mMutex);
943    snprintf(buffer, SIZE,
944            "%smBufferCount=%d, mSynchronousMode=%d, default-size=[%dx%d], "
945            "mPixelFormat=%d, mTexName=%d\n",
946            prefix, mBufferCount, mSynchronousMode, mDefaultWidth, mDefaultHeight,
947            mPixelFormat, mTexName);
948    result.append(buffer);
949
950    String8 fifo;
951    int fifoSize = 0;
952    Fifo::const_iterator i(mQueue.begin());
953    while (i != mQueue.end()) {
954        snprintf(buffer, SIZE, "%02d ", *i++);
955        fifoSize++;
956        fifo.append(buffer);
957    }
958
959    snprintf(buffer, SIZE,
960            "%scurrent: {crop=[%d,%d,%d,%d], transform=0x%02x, current=%d}\n"
961            "%snext   : {crop=[%d,%d,%d,%d], transform=0x%02x, FIFO(%d)={%s}}\n"
962            ,
963            prefix, mCurrentCrop.left,
964            mCurrentCrop.top, mCurrentCrop.right, mCurrentCrop.bottom,
965            mCurrentTransform, mCurrentTexture,
966            prefix, mNextCrop.left, mNextCrop.top, mNextCrop.right, mNextCrop.bottom,
967            mCurrentTransform, fifoSize, fifo.string()
968    );
969    result.append(buffer);
970
971    struct {
972        const char * operator()(int state) const {
973            switch (state) {
974                case BufferSlot::DEQUEUED: return "DEQUEUED";
975                case BufferSlot::QUEUED: return "QUEUED";
976                case BufferSlot::FREE: return "FREE";
977                default: return "Unknown";
978            }
979        }
980    } stateName;
981
982    for (int i=0 ; i<mBufferCount ; i++) {
983        const BufferSlot& slot(mSlots[i]);
984        snprintf(buffer, SIZE,
985                "%s%s[%02d] state=%-8s, crop=[%d,%d,%d,%d], transform=0x%02x, "
986                "timestamp=%lld\n",
987                prefix, (i==mCurrentTexture)?">":" ", i, stateName(slot.mBufferState),
988                slot.mCrop.left, slot.mCrop.top, slot.mCrop.right, slot.mCrop.bottom,
989                slot.mTransform, slot.mTimestamp
990        );
991        result.append(buffer);
992    }
993}
994
995static void mtxMul(float out[16], const float a[16], const float b[16]) {
996    out[0] = a[0]*b[0] + a[4]*b[1] + a[8]*b[2] + a[12]*b[3];
997    out[1] = a[1]*b[0] + a[5]*b[1] + a[9]*b[2] + a[13]*b[3];
998    out[2] = a[2]*b[0] + a[6]*b[1] + a[10]*b[2] + a[14]*b[3];
999    out[3] = a[3]*b[0] + a[7]*b[1] + a[11]*b[2] + a[15]*b[3];
1000
1001    out[4] = a[0]*b[4] + a[4]*b[5] + a[8]*b[6] + a[12]*b[7];
1002    out[5] = a[1]*b[4] + a[5]*b[5] + a[9]*b[6] + a[13]*b[7];
1003    out[6] = a[2]*b[4] + a[6]*b[5] + a[10]*b[6] + a[14]*b[7];
1004    out[7] = a[3]*b[4] + a[7]*b[5] + a[11]*b[6] + a[15]*b[7];
1005
1006    out[8] = a[0]*b[8] + a[4]*b[9] + a[8]*b[10] + a[12]*b[11];
1007    out[9] = a[1]*b[8] + a[5]*b[9] + a[9]*b[10] + a[13]*b[11];
1008    out[10] = a[2]*b[8] + a[6]*b[9] + a[10]*b[10] + a[14]*b[11];
1009    out[11] = a[3]*b[8] + a[7]*b[9] + a[11]*b[10] + a[15]*b[11];
1010
1011    out[12] = a[0]*b[12] + a[4]*b[13] + a[8]*b[14] + a[12]*b[15];
1012    out[13] = a[1]*b[12] + a[5]*b[13] + a[9]*b[14] + a[13]*b[15];
1013    out[14] = a[2]*b[12] + a[6]*b[13] + a[10]*b[14] + a[14]*b[15];
1014    out[15] = a[3]*b[12] + a[7]*b[13] + a[11]*b[14] + a[15]*b[15];
1015}
1016
1017}; // namespace android
1018