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);
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);
82 return nullptr;
83 }
84
85 if (releaseFenceId != -1) {
86 sp<Fence> releaseFence(new Fence(releaseFenceId));
87 status_t err = st.addReleaseFenceLocked(st.mCurrentTexture,
88 st.mSlots[st.mCurrentTexture].mGraphicBuffer,
89 releaseFence);
90 if (err != OK) {
91 IMG_LOGE("dequeueImage: error adding release fence: %s (%d)", strerror(-err), err);
92 st.releaseBufferLocked(slot, st.mSlots[slot].mGraphicBuffer);
93 return nullptr;
94 }
95 }
96
97 // Finally release the old buffer.
98 status_t status =
99 st.releaseBufferLocked(st.mCurrentTexture,
100 st.mSlots[st.mCurrentTexture].mGraphicBuffer, display,
101 mImageSlots[st.mCurrentTexture].eglFence());
102 if (status < NO_ERROR) {
103 IMG_LOGE("dequeueImage: failed to release buffer: %s (%d)", strerror(-status), status);
104 err = status;
105 // Keep going, with error raised.
106 }
107 }
108
109 // Update the state.
110 st.mCurrentTexture = slot;
111 st.mCurrentCrop = item.mCrop;
112 st.mCurrentTransform = item.mTransform;
113 st.mCurrentScalingMode = item.mScalingMode;
114 st.mCurrentTimestamp = item.mTimestamp;
115 st.mCurrentDataSpace = item.mDataSpace;
116 st.mCurrentFence = item.mFence;
117 st.mCurrentFenceTime = item.mFenceTime;
118 st.mCurrentFrameNumber = item.mFrameNumber;
119 st.computeCurrentTransformMatrixLocked();
120
121 *outDataspace = item.mDataSpace;
122 *outHdrMetadata = item.mHdrMetadata;
123 *outSlotid = slot;
124 return st.mSlots[slot].mGraphicBuffer;
125 }
126
127 } /* namespace android */
128