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