SurfaceFlingerConsumer.cpp revision 2adaf04fab35cf47c824d74d901b54094e01ccd3
1/*
2 * Copyright (C) 2012 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 ATRACE_TAG ATRACE_TAG_GRAPHICS
18
19#include "SurfaceFlingerConsumer.h"
20
21#include <utils/Trace.h>
22#include <utils/Errors.h>
23
24
25namespace android {
26
27// ---------------------------------------------------------------------------
28
29status_t SurfaceFlingerConsumer::updateTexImage(BufferRejecter* rejecter)
30{
31    ATRACE_CALL();
32    ALOGV("updateTexImage");
33    Mutex::Autolock lock(mMutex);
34
35    if (mAbandoned) {
36        ALOGE("updateTexImage: GLConsumer is abandoned!");
37        return NO_INIT;
38    }
39
40    // Make sure the EGL state is the same as in previous calls.
41    status_t err = checkAndUpdateEglStateLocked();
42    if (err != NO_ERROR) {
43        return err;
44    }
45
46    BufferQueue::BufferItem item;
47
48    // Acquire the next buffer.
49    // In asynchronous mode the list is guaranteed to be one buffer
50    // deep, while in synchronous mode we use the oldest buffer.
51    err = acquireBufferLocked(&item);
52    if (err != NO_ERROR) {
53        if (err == BufferQueue::NO_BUFFER_AVAILABLE) {
54            // This variant of updateTexImage does not guarantee that the
55            // texture is bound, so no need to call glBindTexture.
56            err = NO_ERROR;
57        } else {
58            ALOGE("updateTexImage: acquire failed: %s (%d)",
59                strerror(-err), err);
60        }
61        return err;
62    }
63
64
65    // We call the rejecter here, in case the caller has a reason to
66    // not accept this buffer.  This is used by SurfaceFlinger to
67    // reject buffers which have the wrong size
68    int buf = item.mBuf;
69    if (rejecter && rejecter->reject(mSlots[buf].mGraphicBuffer, item)) {
70        releaseBufferLocked(buf, EGL_NO_SYNC_KHR);
71        return NO_ERROR;
72    }
73
74    // Release the previous buffer.
75    err = releaseAndUpdateLocked(item);
76    if (err != NO_ERROR) {
77        return err;
78    }
79
80    if (!sUseNativeFenceSync) {
81        // Bind the new buffer to the GL texture.
82        //
83        // Older devices require the "implicit" synchronization provided
84        // by glEGLImageTargetTexture2DOES, which this method calls.  Newer
85        // devices will either call this in Layer::onDraw, or (if it's not
86        // a GL-composited layer) not at all.
87        err = bindTextureImageLocked();
88    }
89
90    return err;
91}
92
93status_t SurfaceFlingerConsumer::bindTextureImage()
94{
95    Mutex::Autolock lock(mMutex);
96
97    return bindTextureImageLocked();
98}
99
100// ---------------------------------------------------------------------------
101}; // namespace android
102
103