blob: c49c6d38c6aa24a3c768168fad45ab14a8fcec57 [file] [log] [blame]
Mathias Agopian3e876012012-06-07 17:52:54 -07001/*
2 **
Jamie Gennis1a4d8832012-08-02 20:11:05 -07003 ** Copyright 2012 The Android Open Source Project
Mathias Agopian3e876012012-06-07 17:52:54 -07004 **
5 ** Licensed under the Apache License Version 2.0(the "License");
6 ** you may not use this file except in compliance with the License.
7 ** You may obtain a copy of the License at
8 **
9 ** http://www.apache.org/licenses/LICENSE-2.0
10 **
11 ** Unless required by applicable law or agreed to in writing software
12 ** distributed under the License is distributed on an "AS IS" BASIS
13 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND either express or implied.
14 ** See the License for the specific language governing permissions and
15 ** limitations under the License.
16 */
17
Dan Stoza9e56aa02015-11-02 13:00:03 -080018// #define LOG_NDEBUG 0
19#undef LOG_TAG
20#define LOG_TAG "FramebufferSurface"
21
Mathias Agopian3e876012012-06-07 17:52:54 -070022#include <errno.h>
Mark Salyzyna5e161b2016-09-29 08:08:05 -070023#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
Mathias Agopian3e876012012-06-07 17:52:54 -070026
Mathias Agopian3e876012012-06-07 17:52:54 -070027#include <utils/String8.h>
Mark Salyzyn7823e122016-09-29 08:08:05 -070028#include <log/log.h>
Mathias Agopian3e876012012-06-07 17:52:54 -070029
Mathias Agopian3e876012012-06-07 17:52:54 -070030#include <EGL/egl.h>
31
32#include <hardware/hardware.h>
Dan Stoza84493cd2015-03-12 15:12:44 -070033#include <gui/BufferItem.h>
Mathias Agopiana9347642017-02-13 16:42:28 -080034#include <gui/BufferQueue.h>
Dan Stoza84493cd2015-03-12 15:12:44 -070035#include <gui/Surface.h>
Mathias Agopiana9347642017-02-13 16:42:28 -080036
Courtney Goeltzenleuchter19987a82017-07-14 12:16:56 -060037#include <ui/DebugUtils.h>
Mathias Agopian3e876012012-06-07 17:52:54 -070038#include <ui/GraphicBuffer.h>
Mathias Agopiana9347642017-02-13 16:42:28 -080039#include <ui/Rect.h>
Mathias Agopian3e876012012-06-07 17:52:54 -070040
Mathias Agopian33ceeb32013-04-01 16:54:58 -070041#include "FramebufferSurface.h"
42#include "HWComposer.h"
Fabien Sanglard1971b632017-03-10 14:50:03 -080043#include "../SurfaceFlinger.h"
Jamie Genniscdbaecb2012-10-12 14:18:10 -070044
Mathias Agopian3e876012012-06-07 17:52:54 -070045// ----------------------------------------------------------------------------
46namespace android {
47// ----------------------------------------------------------------------------
48
Mathias Agopian3e876012012-06-07 17:52:54 -070049/*
50 * This implements the (main) framebuffer management. This class is used
51 * mostly by SurfaceFlinger, but also by command line GL application.
52 *
53 */
54
Mathias Agopiandb89edc2013-08-02 01:40:18 -070055FramebufferSurface::FramebufferSurface(HWComposer& hwc, int disp,
56 const sp<IGraphicBufferConsumer>& consumer) :
57 ConsumerBase(consumer),
Mathias Agopianf5a33922012-09-19 18:16:22 -070058 mDisplayType(disp),
Jamie Gennis1a4d8832012-08-02 20:11:05 -070059 mCurrentBufferSlot(-1),
Dan Stoza9e56aa02015-11-02 13:00:03 -080060 mCurrentBuffer(),
61 mCurrentFence(Fence::NO_FENCE),
Fabien Sanglard9d96de42016-10-11 00:15:18 +000062#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -080063 mHwc(hwc),
64 mHasPendingRelease(false),
65 mPreviousBufferSlot(BufferQueue::INVALID_BUFFER_SLOT),
66 mPreviousBuffer()
Fabien Sanglard9d96de42016-10-11 00:15:18 +000067#else
68 mHwc(hwc)
69#endif
Mathias Agopian3e876012012-06-07 17:52:54 -070070{
Fabien Sanglard9d96de42016-10-11 00:15:18 +000071#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -080072 ALOGV("Creating for display %d", disp);
Fabien Sanglard9d96de42016-10-11 00:15:18 +000073#endif
74
Andy McFaddenb0d1dd32012-09-10 14:08:09 -070075 mName = "FramebufferSurface";
Mathias Agopiandb89edc2013-08-02 01:40:18 -070076 mConsumer->setConsumerName(mName);
77 mConsumer->setConsumerUsageBits(GRALLOC_USAGE_HW_FB |
Mathias Agopianf5a33922012-09-19 18:16:22 -070078 GRALLOC_USAGE_HW_RENDER |
79 GRALLOC_USAGE_HW_COMPOSER);
Fabien Sanglard9d96de42016-10-11 00:15:18 +000080#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -080081 const auto& activeConfig = mHwc.getActiveConfig(disp);
82 mConsumer->setDefaultBufferSize(activeConfig->getWidth(),
83 activeConfig->getHeight());
Fabien Sanglard9d96de42016-10-11 00:15:18 +000084#else
85 mConsumer->setDefaultBufferFormat(mHwc.getFormat(disp));
86 mConsumer->setDefaultBufferSize(mHwc.getWidth(disp), mHwc.getHeight(disp));
87#endif
Fabien Sanglard1971b632017-03-10 14:50:03 -080088 mConsumer->setMaxAcquiredBufferCount(
89 SurfaceFlinger::maxFrameBufferAcquiredBuffers - 1);
Jesse Hall99c7dbb2013-03-14 14:29:29 -070090}
91
Jesse Hall7cd85972014-08-07 22:48:06 -070092status_t FramebufferSurface::beginFrame(bool /*mustRecompose*/) {
Jesse Hall028dc8f2013-08-20 16:35:32 -070093 return NO_ERROR;
94}
95
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -070096status_t FramebufferSurface::prepareFrame(CompositionType /*compositionType*/) {
Jesse Hall38efe862013-04-06 23:12:29 -070097 return NO_ERROR;
98}
99
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700100status_t FramebufferSurface::advanceFrame() {
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000101#ifdef USE_HWC2
Chia-I Wu06d63de2017-01-04 14:58:51 +0800102 uint32_t slot = 0;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800103 sp<GraphicBuffer> buf;
104 sp<Fence> acquireFence(Fence::NO_FENCE);
105 android_dataspace_t dataspace = HAL_DATASPACE_UNKNOWN;
Chia-I Wu06d63de2017-01-04 14:58:51 +0800106 status_t result = nextBuffer(slot, buf, acquireFence, dataspace);
Courtney Goeltzenleuchter19987a82017-07-14 12:16:56 -0600107 mDataSpace = dataspace;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800108 if (result != NO_ERROR) {
109 ALOGE("error latching next FramebufferSurface buffer: %s (%d)",
110 strerror(-result), result);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800111 }
112 return result;
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000113#else
114 // Once we remove FB HAL support, we can call nextBuffer() from here
115 // instead of using onFrameAvailable(). No real benefit, except it'll be
116 // more like VirtualDisplaySurface.
117 return NO_ERROR;
118#endif
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700119}
120
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000121#ifdef USE_HWC2
Chia-I Wu06d63de2017-01-04 14:58:51 +0800122status_t FramebufferSurface::nextBuffer(uint32_t& outSlot,
123 sp<GraphicBuffer>& outBuffer, sp<Fence>& outFence,
124 android_dataspace_t& outDataspace) {
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000125#else
126status_t FramebufferSurface::nextBuffer(sp<GraphicBuffer>& outBuffer, sp<Fence>& outFence) {
127#endif
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700128 Mutex::Autolock lock(mMutex);
Mathias Agopian3e876012012-06-07 17:52:54 -0700129
Dan Stoza84493cd2015-03-12 15:12:44 -0700130 BufferItem item;
Andy McFadden1585c4d2013-06-28 13:52:40 -0700131 status_t err = acquireBufferLocked(&item, 0);
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700132 if (err == BufferQueue::NO_BUFFER_AVAILABLE) {
Chia-I Wu06d63de2017-01-04 14:58:51 +0800133#ifdef USE_HWC2
Chia-I Wuaaff73f2017-02-13 12:28:24 -0800134 mHwcBufferCache.getHwcBuffer(mCurrentBufferSlot, mCurrentBuffer,
Chia-I Wu06d63de2017-01-04 14:58:51 +0800135 &outSlot, &outBuffer);
136#else
Mathias Agopianda27af92012-09-13 18:17:13 -0700137 outBuffer = mCurrentBuffer;
Chia-I Wu06d63de2017-01-04 14:58:51 +0800138#endif
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700139 return NO_ERROR;
140 } else if (err != NO_ERROR) {
141 ALOGE("error acquiring buffer: %s (%d)", strerror(-err), err);
142 return err;
143 }
144
145 // If the BufferQueue has freed and reallocated a buffer in mCurrentSlot
146 // then we may have acquired the slot we already own. If we had released
147 // our current buffer before we call acquireBuffer then that release call
148 // would have returned STALE_BUFFER_SLOT, and we would have called
149 // freeBufferLocked on that slot. Because the buffer slot has already
150 // been overwritten with the new buffer all we have to do is skip the
151 // releaseBuffer call and we should be in the same state we'd be in if we
152 // had released the old buffer first.
153 if (mCurrentBufferSlot != BufferQueue::INVALID_BUFFER_SLOT &&
Pablo Ceballos47650f42015-08-04 16:38:17 -0700154 item.mSlot != mCurrentBufferSlot) {
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000155#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -0800156 mHasPendingRelease = true;
157 mPreviousBufferSlot = mCurrentBufferSlot;
158 mPreviousBuffer = mCurrentBuffer;
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000159#else
160 // Release the previous buffer.
161 err = releaseBufferLocked(mCurrentBufferSlot, mCurrentBuffer,
162 EGL_NO_DISPLAY, EGL_NO_SYNC_KHR);
163 if (err < NO_ERROR) {
164 ALOGE("error releasing buffer: %s (%d)", strerror(-err), err);
165 return err;
166 }
167#endif
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700168 }
Pablo Ceballos47650f42015-08-04 16:38:17 -0700169 mCurrentBufferSlot = item.mSlot;
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700170 mCurrentBuffer = mSlots[mCurrentBufferSlot].mGraphicBuffer;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800171 mCurrentFence = item.mFence;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800172
Mathias Agopianda27af92012-09-13 18:17:13 -0700173 outFence = item.mFence;
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000174#ifdef USE_HWC2
Chia-I Wuaaff73f2017-02-13 12:28:24 -0800175 mHwcBufferCache.getHwcBuffer(mCurrentBufferSlot, mCurrentBuffer,
Chia-I Wu06d63de2017-01-04 14:58:51 +0800176 &outSlot, &outBuffer);
Courtney Goeltzenleuchter79d27242017-07-13 17:54:01 -0600177 status_t result =
178 mHwc.setClientTarget(mDisplayType, outSlot, outFence, outBuffer, outDataspace);
179 if (result != NO_ERROR) {
180 ALOGE("error posting framebuffer: %d", result);
Courtney Goeltzenleuchtercd939aa2017-07-25 10:34:27 -0600181 return result;
Courtney Goeltzenleuchter79d27242017-07-13 17:54:01 -0600182 }
Courtney Goeltzenleuchtercd939aa2017-07-25 10:34:27 -0600183#else
184 outBuffer = mCurrentBuffer;
185#endif
Courtney Goeltzenleuchter281e8112017-07-13 17:54:01 -0600186 status_t result =
187 mHwc.setClientTarget(mDisplayType, outSlot, outFence, outBuffer, outDataspace);
188 if (result != NO_ERROR) {
189 ALOGE("error posting framebuffer: %d", result);
190 }
191
192 return result;
Mathias Agopian3e876012012-06-07 17:52:54 -0700193}
194
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000195#ifndef USE_HWC2
196// Overrides ConsumerBase::onFrameAvailable(), does not call base class impl.
197void FramebufferSurface::onFrameAvailable(const BufferItem& /* item */) {
198 sp<GraphicBuffer> buf;
199 sp<Fence> acquireFence;
200 status_t err = nextBuffer(buf, acquireFence);
201 if (err != NO_ERROR) {
202 ALOGE("error latching nnext FramebufferSurface buffer: %s (%d)",
203 strerror(-err), err);
204 return;
205 }
206 err = mHwc.fbPost(mDisplayType, acquireFence, buf);
207 if (err != NO_ERROR) {
208 ALOGE("error posting framebuffer: %d", err);
209 }
210}
211#endif
212
Jamie Gennis1a4d8832012-08-02 20:11:05 -0700213void FramebufferSurface::freeBufferLocked(int slotIndex) {
214 ConsumerBase::freeBufferLocked(slotIndex);
215 if (slotIndex == mCurrentBufferSlot) {
216 mCurrentBufferSlot = BufferQueue::INVALID_BUFFER_SLOT;
217 }
218}
219
Jesse Hall851cfe82013-03-20 13:44:00 -0700220void FramebufferSurface::onFrameCommitted() {
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000221#ifdef USE_HWC2
Dan Stoza9e56aa02015-11-02 13:00:03 -0800222 if (mHasPendingRelease) {
Fabien Sanglard11d0fc32016-12-01 15:43:01 -0800223 sp<Fence> fence = mHwc.getPresentFence(mDisplayType);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800224 if (fence->isValid()) {
225 status_t result = addReleaseFence(mPreviousBufferSlot,
226 mPreviousBuffer, fence);
227 ALOGE_IF(result != NO_ERROR, "onFrameCommitted: failed to add the"
228 " fence: %s (%d)", strerror(-result), result);
229 }
230 status_t result = releaseBufferLocked(mPreviousBufferSlot,
231 mPreviousBuffer, EGL_NO_DISPLAY, EGL_NO_SYNC_KHR);
232 ALOGE_IF(result != NO_ERROR, "onFrameCommitted: error releasing buffer:"
233 " %s (%d)", strerror(-result), result);
234
235 mPreviousBuffer.clear();
236 mHasPendingRelease = false;
237 }
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000238#else
239 sp<Fence> fence = mHwc.getAndResetReleaseFence(mDisplayType);
240 if (fence->isValid() &&
241 mCurrentBufferSlot != BufferQueue::INVALID_BUFFER_SLOT) {
242 status_t err = addReleaseFence(mCurrentBufferSlot,
243 mCurrentBuffer, fence);
244 ALOGE_IF(err, "setReleaseFenceFd: failed to add the fence: %s (%d)",
245 strerror(-err), err);
246 }
247#endif
Mathias Agopianda27af92012-09-13 18:17:13 -0700248}
249
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000250#ifndef USE_HWC2
251status_t FramebufferSurface::compositionComplete()
252{
253 return mHwc.fbCompositionComplete();
254}
255#endif
256
Dan Stozaf10c46e2014-11-11 10:32:31 -0800257void FramebufferSurface::dumpAsString(String8& result) const {
Courtney Goeltzenleuchter19987a82017-07-14 12:16:56 -0600258 Mutex::Autolock lock(mMutex);
259 result.appendFormat("FramebufferSurface: dataspace: %s(%d)\n",
260 dataspaceDetails(mDataSpace).c_str(), mDataSpace);
261 ConsumerBase::dumpLocked(result, "");
Mathias Agopian3e876012012-06-07 17:52:54 -0700262}
263
Mathias Agopian74d211a2013-04-22 16:55:35 +0200264void FramebufferSurface::dumpLocked(String8& result, const char* prefix) const
Jesse Hall7adb0f82013-03-06 16:13:49 -0800265{
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000266#ifndef USE_HWC2
267 mHwc.fbDump(result);
268#endif
Mathias Agopian74d211a2013-04-22 16:55:35 +0200269 ConsumerBase::dumpLocked(result, prefix);
Jesse Hall7adb0f82013-03-06 16:13:49 -0800270}
271
Dan Stoza9e56aa02015-11-02 13:00:03 -0800272const sp<Fence>& FramebufferSurface::getClientTargetAcquireFence() const {
273 return mCurrentFence;
274}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800275
Mathias Agopian3e876012012-06-07 17:52:54 -0700276// ----------------------------------------------------------------------------
277}; // namespace android
278// ----------------------------------------------------------------------------