1 /*
2  * Copyright 2019 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 #include <gui/BufferQueue.h>
18 #include <surfacetexture/ImageConsumer.h>
19 #include <surfacetexture/SurfaceTexture.h>
20 
21 // Macro for including the SurfaceTexture name in log messages
22 #define IMG_LOGE(x, ...) ALOGE("[%s] " x, st.mName.string(), ##__VA_ARGS__)
23 
24 namespace android {
25 
onReleaseBufferLocked(int buf)26 void ImageConsumer::onReleaseBufferLocked(int buf) {
27     mImageSlots[buf].eglFence() = EGL_NO_SYNC_KHR;
28 }
29 
dequeueBuffer(int * outSlotid,android_dataspace * outDataspace,bool * outQueueEmpty,SurfaceTexture & st,SurfaceTexture_createReleaseFence createFence,SurfaceTexture_fenceWait fenceWait,void * fencePassThroughHandle)30 sp<GraphicBuffer> ImageConsumer::dequeueBuffer(int* outSlotid, android_dataspace* outDataspace,
31                                                bool* outQueueEmpty, SurfaceTexture& st,
32                                                SurfaceTexture_createReleaseFence createFence,
33                                                SurfaceTexture_fenceWait fenceWait,
34                                                void* fencePassThroughHandle) {
35     BufferItem item;
36     status_t err;
37     err = st.acquireBufferLocked(&item, 0);
38     if (err != OK) {
39         if (err != BufferQueue::NO_BUFFER_AVAILABLE) {
40             IMG_LOGE("Error acquiring buffer: %s (%d)", strerror(err), err);
41         } else {
42             int slot = st.mCurrentTexture;
43             if (slot != BufferItem::INVALID_BUFFER_SLOT) {
44                 *outQueueEmpty = true;
45                 *outDataspace = st.mCurrentDataSpace;
46                 *outSlotid = slot;
47                 return st.mSlots[slot].mGraphicBuffer;
48             }
49         }
50         return nullptr;
51     }
52 
53     int slot = item.mSlot;
54     *outQueueEmpty = false;
55     if (item.mFence->isValid()) {
56         // If fence is not signaled, that means frame is not ready and
57         // outQueueEmpty is set to true. By the time the fence is signaled,
58         // there may be a new buffer queued. This is a proper detection for an
59         // empty queue and it is needed to avoid infinite loop in
60         // ASurfaceTexture_dequeueBuffer (see b/159921224).
61         *outQueueEmpty = item.mFence->getStatus() == Fence::Status::Unsignaled;
62 
63         // Wait on the producer fence for the buffer to be ready.
64         err = fenceWait(item.mFence->get(), fencePassThroughHandle);
65         if (err != OK) {
66             st.releaseBufferLocked(slot, st.mSlots[slot].mGraphicBuffer, EGL_NO_DISPLAY,
67                                    EGL_NO_SYNC_KHR);
68             return nullptr;
69         }
70     }
71 
72     // Release old buffer.
73     if (st.mCurrentTexture != BufferItem::INVALID_BUFFER_SLOT) {
74         // If needed, set the released slot's fence to guard against a producer
75         // accessing the buffer before the outstanding accesses have completed.
76         int releaseFenceId = -1;
77         EGLDisplay display = EGL_NO_DISPLAY;
78         err = createFence(st.mUseFenceSync, &mImageSlots[slot].eglFence(), &display,
79                           &releaseFenceId, fencePassThroughHandle);
80         if (OK != err) {
81             st.releaseBufferLocked(slot, st.mSlots[slot].mGraphicBuffer, EGL_NO_DISPLAY,
82                                    EGL_NO_SYNC_KHR);
83             return nullptr;
84         }
85 
86         if (releaseFenceId != -1) {
87             sp<Fence> releaseFence(new Fence(releaseFenceId));
88             status_t err = st.addReleaseFenceLocked(st.mCurrentTexture,
89                                                     st.mSlots[st.mCurrentTexture].mGraphicBuffer,
90                                                     releaseFence);
91             if (err != OK) {
92                 IMG_LOGE("dequeueImage: error adding release fence: %s (%d)", strerror(-err), err);
93                 st.releaseBufferLocked(slot, st.mSlots[slot].mGraphicBuffer, EGL_NO_DISPLAY,
94                                        EGL_NO_SYNC_KHR);
95                 return nullptr;
96             }
97         }
98 
99         // Finally release the old buffer.
100         status_t status =
101                 st.releaseBufferLocked(st.mCurrentTexture,
102                                        st.mSlots[st.mCurrentTexture].mGraphicBuffer, display,
103                                        mImageSlots[st.mCurrentTexture].eglFence());
104         if (status < NO_ERROR) {
105             IMG_LOGE("dequeueImage: failed to release buffer: %s (%d)", strerror(-status), status);
106             err = status;
107             // Keep going, with error raised.
108         }
109     }
110 
111     // Update the state.
112     st.mCurrentTexture = slot;
113     st.mCurrentCrop = item.mCrop;
114     st.mCurrentTransform = item.mTransform;
115     st.mCurrentScalingMode = item.mScalingMode;
116     st.mCurrentTimestamp = item.mTimestamp;
117     st.mCurrentDataSpace = item.mDataSpace;
118     st.mCurrentFence = item.mFence;
119     st.mCurrentFenceTime = item.mFenceTime;
120     st.mCurrentFrameNumber = item.mFrameNumber;
121     st.computeCurrentTransformMatrixLocked();
122 
123     *outDataspace = item.mDataSpace;
124     *outSlotid = slot;
125     return st.mSlots[slot].mGraphicBuffer;
126 }
127 
128 } /* namespace android */
129