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 if (item.mFence->isValid()) {
55 // Wait on the producer fence for the buffer to be ready.
56 err = fenceWait(item.mFence->get(), fencePassThroughHandle);
57 if (err != OK) {
58 st.releaseBufferLocked(slot, st.mSlots[slot].mGraphicBuffer, EGL_NO_DISPLAY,
59 EGL_NO_SYNC_KHR);
60 return nullptr;
61 }
62 }
63
64 // Release old buffer.
65 if (st.mCurrentTexture != BufferItem::INVALID_BUFFER_SLOT) {
66 // If needed, set the released slot's fence to guard against a producer
67 // accessing the buffer before the outstanding accesses have completed.
68 int releaseFenceId = -1;
69 EGLDisplay display = EGL_NO_DISPLAY;
70 err = createFence(st.mUseFenceSync, &mImageSlots[slot].eglFence(), &display,
71 &releaseFenceId, fencePassThroughHandle);
72 if (OK != err) {
73 st.releaseBufferLocked(slot, st.mSlots[slot].mGraphicBuffer, EGL_NO_DISPLAY,
74 EGL_NO_SYNC_KHR);
75 return nullptr;
76 }
77
78 if (releaseFenceId != -1) {
79 sp<Fence> releaseFence(new Fence(releaseFenceId));
80 status_t err = st.addReleaseFenceLocked(st.mCurrentTexture,
81 st.mSlots[st.mCurrentTexture].mGraphicBuffer,
82 releaseFence);
83 if (err != OK) {
84 IMG_LOGE("dequeueImage: error adding release fence: %s (%d)", strerror(-err), err);
85 st.releaseBufferLocked(slot, st.mSlots[slot].mGraphicBuffer, EGL_NO_DISPLAY,
86 EGL_NO_SYNC_KHR);
87 return nullptr;
88 }
89 }
90
91 // Finally release the old buffer.
92 status_t status =
93 st.releaseBufferLocked(st.mCurrentTexture,
94 st.mSlots[st.mCurrentTexture].mGraphicBuffer, display,
95 mImageSlots[st.mCurrentTexture].eglFence());
96 if (status < NO_ERROR) {
97 IMG_LOGE("dequeueImage: failed to release buffer: %s (%d)", strerror(-status), status);
98 err = status;
99 // Keep going, with error raised.
100 }
101 }
102
103 // Update the state.
104 st.mCurrentTexture = slot;
105 st.mCurrentCrop = item.mCrop;
106 st.mCurrentTransform = item.mTransform;
107 st.mCurrentScalingMode = item.mScalingMode;
108 st.mCurrentTimestamp = item.mTimestamp;
109 st.mCurrentDataSpace = item.mDataSpace;
110 st.mCurrentFence = item.mFence;
111 st.mCurrentFenceTime = item.mFenceTime;
112 st.mCurrentFrameNumber = item.mFrameNumber;
113 st.computeCurrentTransformMatrixLocked();
114
115 *outQueueEmpty = false;
116 *outDataspace = item.mDataSpace;
117 *outSlotid = slot;
118 return st.mSlots[slot].mGraphicBuffer;
119 }
120
121 } /* namespace android */
122