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