-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathFramebufferSurface.cpp
More file actions
282 lines (246 loc) · 9.01 KB
/
FramebufferSurface.cpp
File metadata and controls
282 lines (246 loc) · 9.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/*
** Copyright (C) 2020 KAI OS TECHNOLOGIES (HONG KONG) LIMITED. All rights reserved.
** Copyright 2012 The Android Open Source Project
**
** Licensed under the Apache License Version 2.0(the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
**
** http://www.apache.org/licenses/LICENSE-2.0
**
** Unless required by applicable law or agreed to in writing software
** distributed under the License is distributed on an "AS IS" BASIS
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
*/
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <cutils/log.h>
#include <EGL/egl.h>
#include <gui/BufferItem.h>
#include <gui/BufferQueue.h>
#include <gui/Surface.h>
#include <hardware/hardware.h>
#include <ui/GraphicBuffer.h>
#include <ui/Rect.h>
#include <utils/String8.h>
#include <vndk/hardware_buffer.h>
#include "GonkDisplayWorkThread.h"
#include "FramebufferSurface.h"
#include "ComposerHal_stub.h"
#ifndef NUM_FRAMEBUFFER_SURFACE_BUFFERS
#define NUM_FRAMEBUFFER_SURFACE_BUFFERS (3)
#endif
#ifdef LOG_TAG
#undef LOG_TAG
#define LOG_TAG "FramebufferSurface"
#endif
// ----------------------------------------------------------------------------
namespace android {
// ----------------------------------------------------------------------------
/*
* This implements the (main) framebuffer management. This class
* was adapted from the version in SurfaceFlinger
*/
FramebufferSurface::FramebufferSurface(
uint32_t width, uint32_t height, uint32_t format,
const sp<IGraphicBufferConsumer>& consumer,
HWC2::Display *aHwcDisplay, HWC2::Layer *aLayer,
NativeFramebufferDevice *ExtFBDevice)
: DisplaySurface(consumer)
, mCurrentSlot(BufferQueue::INVALID_BUFFER_SLOT)
, mCurrentBuffer()
, mPrevFBAcquireFence(Fence::NO_FENCE)
, mHasPendingRelease(false)
, mPreviousBufferSlot(BufferQueue::INVALID_BUFFER_SLOT)
, mPreviousBuffer()
, hwcDisplay(aHwcDisplay)
, layer(aLayer)
, mExtFBDevice(ExtFBDevice)
, mLastPresentFence(Fence::NO_FENCE)
{
mName = "FramebufferSurface";
mConsumer->setConsumerName(mName);
mConsumer->setConsumerUsageBits(
#if ANDROID_EMULATOR
GRALLOC_USAGE_HW_FB |
#endif
GRALLOC_USAGE_HW_RENDER |
GRALLOC_USAGE_HW_COMPOSER);
mConsumer->setDefaultBufferFormat(format);
mConsumer->setDefaultBufferSize(width, height);
mConsumer->setMaxAcquiredBufferCount(NUM_FRAMEBUFFER_SURFACE_BUFFERS);
}
void FramebufferSurface::resizeBuffers(const uint32_t width,
const uint32_t height)
{
mConsumer->setDefaultBufferSize(width, height);
}
status_t FramebufferSurface::beginFrame(bool /*mustRecompose*/)
{
return NO_ERROR;
}
status_t FramebufferSurface::prepareFrame(CompositionType /*compositionType*/)
{
return NO_ERROR;
}
status_t FramebufferSurface::advanceFrame()
{
sp<GraphicBuffer> buf;
sp<Fence> acquireFence;
status_t result = nextBuffer(buf, acquireFence);
if (result != NO_ERROR) {
ALOGE("error latching next FramebufferSurface buffer: %s (%d)",
strerror(-result), result);
}
if (acquireFence.get() && acquireFence->isValid()) {
mPrevFBAcquireFence = new Fence(acquireFence->dup());
} else {
mPrevFBAcquireFence = Fence::NO_FENCE;
}
lastHandle = buf->handle;
return result;
}
status_t FramebufferSurface::nextBuffer(sp<GraphicBuffer>& outBuffer,
sp<Fence>& outFence)
{
Mutex::Autolock lock(mMutex);
BufferItem item;
status_t err = acquireBufferLocked(&item, 0);
if (err == BufferQueue::NO_BUFFER_AVAILABLE) {
outBuffer = mCurrentBuffer;
return NO_ERROR;
} else if (err != NO_ERROR) {
ALOGE("error acquiring buffer: %s (%d)", strerror(-err), err);
return err;
}
const auto slot = item.mSlot;
const auto buffer = mSlots[item.mSlot].mGraphicBuffer;
const auto acquireFence = item.mFence;
presentLocked(slot, buffer, acquireFence);
// If the BufferQueue has freed and reallocated a buffer in mCurrentSlot
// then we may have acquired the slot we already own. If we had released
// our current buffer before we call acquireBuffer then that release call
// would have returned STALE_BUFFER_SLOT, and we would have called
// freeBufferLocked on that slot. Because the buffer slot has already
// been overwritten with the new buffer all we have to do is skip the
// releaseBuffer call and we should be in the same state we'd be in if we
// had released the old buffer first.
if (mCurrentSlot != BufferQueue::INVALID_BUFFER_SLOT && slot != mCurrentSlot) {
mHasPendingRelease = true;
mPreviousBufferSlot = mCurrentSlot;
mPreviousBuffer = mCurrentBuffer;
}
mCurrentSlot = slot;
outBuffer = mCurrentBuffer = buffer;
outFence = acquireFence;
return NO_ERROR;
}
// Overrides ConsumerBase::onFrameAvailable(), does not call base class impl.
void FramebufferSurface::onFrameAvailable(const BufferItem &item) {
(void)item;
carthage::GonkDisplayWorkThread::Get()->Post([=] {
sp<GraphicBuffer> buf;
sp<Fence> acquireFence;
status_t err = nextBuffer(buf, acquireFence);
if (err != NO_ERROR) {
ALOGE("error latching nnext FramebufferSurface buffer: %s (%d)",
strerror(-err), err);
return;
}
if (acquireFence.get() && acquireFence->isValid()) {
mPrevFBAcquireFence = acquireFence;
} else {
mPrevFBAcquireFence = Fence::NO_FENCE;
}
lastHandle = buf->handle;
});
}
void FramebufferSurface::presentLocked(const int slot,
const sp<GraphicBuffer>& buffer,
const sp<Fence>& acquireFence)
{
uint32_t numTypes = 0;
uint32_t numRequests = 0;
HWC2::Error error = HWC2::Error::None;
ui::Dataspace dataspace = ui::Dataspace::UNKNOWN;
// TODO user HWC::Device path:
//layer->setCompositionType(HWC2::Composition::Device);
//layer->setBuffer(slot, buffer, acquireFence);
// this line is used to avoid unused variable layer warning.
(void)layer;
if (mExtFBDevice) {
if (acquireFence.get() && acquireFence->isValid()) {
android::sp<Fence> fenceObj = new Fence(acquireFence->dup());
fenceObj->waitForever("FramebufferSurface::Post");
}
mExtFBDevice->Post(buffer->handle);
} else {
error = hwcDisplay->validate(&numTypes, &numRequests);
if (error != HWC2::Error::None && error != HWC2::Error::HasChanges) {
ALOGE("prepare: validate failed : %s (%d)",
to_string(error).c_str(), static_cast<int32_t>(error));
goto FrameCommitted;
}
if (numTypes || numRequests) {
ALOGE("prepare: validate required changes : %s (%d)",
to_string(error).c_str(), static_cast<int32_t>(error));
goto FrameCommitted;
}
error = hwcDisplay->acceptChanges();
if (error != HWC2::Error::None) {
ALOGE("prepare: acceptChanges failed: %s", to_string(error).c_str());
goto FrameCommitted;
}
(void)hwcDisplay->setClientTarget(slot, buffer, acquireFence, dataspace);
error = hwcDisplay->present(&mLastPresentFence);
if (error != HWC2::Error::None) {
ALOGE("present: failed : %s (%d)",
to_string(error).c_str(), static_cast<int32_t>(error));
}
}
FrameCommitted:
onFrameCommitted();
}
void FramebufferSurface::freeBufferLocked(int slotIndex)
{
ConsumerBase::freeBufferLocked(slotIndex);
if (slotIndex == mCurrentSlot) {
mCurrentSlot = BufferQueue::INVALID_BUFFER_SLOT;
}
}
status_t FramebufferSurface::setReleaseFenceFd(int fenceFd)
{
status_t err = NO_ERROR;
if (fenceFd >= 0) {
sp<Fence> fence(new Fence(fenceFd));
if (mCurrentSlot != BufferQueue::INVALID_BUFFER_SLOT) {
status_t err = addReleaseFence(mCurrentSlot,
mCurrentBuffer, fence);
ALOGE_IF(err, "setReleaseFenceFd: failed to add the fence: %s (%d)",
strerror(-err), err);
}
}
return err;
}
int FramebufferSurface::GetPrevDispAcquireFd()
{
if (mPrevFBAcquireFence.get() && mPrevFBAcquireFence->isValid()) {
return mPrevFBAcquireFence->dup();
}
return -1;
}
void FramebufferSurface::onFrameCommitted()
{
if (mHasPendingRelease) {
addReleaseFenceLocked(mPreviousBufferSlot, mPreviousBuffer, mLastPresentFence);
releaseBufferLocked(mPreviousBufferSlot, mPreviousBuffer);
mHasPendingRelease = false;
}
}
// ----------------------------------------------------------------------------
}; // namespace android
// ----------------------------------------------------------------------------