Switch SurfaceFlinger to HWC 2.0
Enables SurfaceFlinger to speak to version 2.0 of the Hardware Composer
HAL instead of version 1.x (also removing support for the framebuffer
HAL). By default, however, this functionality is disabled. In order to
enable it, USE_HWC2 must be set to true in Android.mk.
Change-Id: I4589e02ac2165236b10ff2f7cb772f87e0d3daab
diff --git a/services/surfaceflinger/SurfaceFlinger.cpp b/services/surfaceflinger/SurfaceFlinger.cpp
index 737cc82..0a3534f 100644
--- a/services/surfaceflinger/SurfaceFlinger.cpp
+++ b/services/surfaceflinger/SurfaceFlinger.cpp
@@ -14,6 +14,7 @@
* limitations under the License.
*/
+// #define LOG_NDEBUG 0
#define ATRACE_TAG ATRACE_TAG_GRAPHICS
#include <stdint.h>
@@ -134,8 +135,9 @@
mRepaintEverything(0),
mRenderEngine(NULL),
mBootTime(systemTime()),
+ mBuiltinDisplays(),
mVisibleRegionsDirty(false),
- mHwWorkListDirty(false),
+ mGeometryInvalid(false),
mAnimCompositionPending(false),
mDebugRegion(0),
mDebugDDMS(0),
@@ -263,6 +265,7 @@
}
void SurfaceFlinger::createBuiltinDisplayLocked(DisplayDevice::DisplayType type) {
+ ALOGV("createBuiltinDisplayLocked(%d)", type);
ALOGW_IF(mBuiltinDisplays[type],
"Overwriting display token for display type %d", type);
mBuiltinDisplays[type] = new BBinder();
@@ -442,80 +445,48 @@
ALOGI( "SurfaceFlinger's main thread ready to run. "
"Initializing graphics H/W...");
+ { // Autolock scope
+ Mutex::Autolock _l(mStateLock);
+
+ // initialize EGL for the default display
+ mEGLDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
+ eglInitialize(mEGLDisplay, NULL, NULL);
+
+ // start the EventThread
+ sp<VSyncSource> vsyncSrc = new DispSyncSource(&mPrimaryDispSync,
+ vsyncPhaseOffsetNs, true, "app");
+ mEventThread = new EventThread(vsyncSrc);
+ sp<VSyncSource> sfVsyncSrc = new DispSyncSource(&mPrimaryDispSync,
+ sfVsyncPhaseOffsetNs, true, "sf");
+ mSFEventThread = new EventThread(sfVsyncSrc);
+ mEventQueue.setEventThread(mSFEventThread);
+
+ // Get a RenderEngine for the given display / config (can't fail)
+ mRenderEngine = RenderEngine::create(mEGLDisplay,
+ HAL_PIXEL_FORMAT_RGBA_8888);
+ }
+
+ // Drop the state lock while we initialize the hardware composer. We drop
+ // the lock because on creation, it will call back into SurfaceFlinger to
+ // initialize the primary display.
+ mHwc = new HWComposer(this);
+ mHwc->setEventHandler(static_cast<HWComposer::EventHandler*>(this));
+
Mutex::Autolock _l(mStateLock);
- // initialize EGL for the default display
- mEGLDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
- eglInitialize(mEGLDisplay, NULL, NULL);
-
- // start the EventThread
- sp<VSyncSource> vsyncSrc = new DispSyncSource(&mPrimaryDispSync,
- vsyncPhaseOffsetNs, true, "app");
- mEventThread = new EventThread(vsyncSrc);
- sp<VSyncSource> sfVsyncSrc = new DispSyncSource(&mPrimaryDispSync,
- sfVsyncPhaseOffsetNs, true, "sf");
- mSFEventThread = new EventThread(sfVsyncSrc);
- mEventQueue.setEventThread(mSFEventThread);
-
- // Initialize the H/W composer object. There may or may not be an
- // actual hardware composer underneath.
- mHwc = new HWComposer(this,
- *static_cast<HWComposer::EventHandler *>(this));
-
- // get a RenderEngine for the given display / config (can't fail)
- mRenderEngine = RenderEngine::create(mEGLDisplay, mHwc->getVisualID());
-
// retrieve the EGL context that was selected/created
mEGLContext = mRenderEngine->getEGLContext();
LOG_ALWAYS_FATAL_IF(mEGLContext == EGL_NO_CONTEXT,
"couldn't create EGLContext");
- // initialize our non-virtual displays
- for (size_t i=0 ; i<DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES ; i++) {
- DisplayDevice::DisplayType type((DisplayDevice::DisplayType)i);
- // set-up the displays that are already connected
- if (mHwc->isConnected(i) || type==DisplayDevice::DISPLAY_PRIMARY) {
- // All non-virtual displays are currently considered secure.
- bool isSecure = true;
- createBuiltinDisplayLocked(type);
- wp<IBinder> token = mBuiltinDisplays[i];
-
- sp<IGraphicBufferProducer> producer;
- sp<IGraphicBufferConsumer> consumer;
- BufferQueue::createBufferQueue(&producer, &consumer,
- new GraphicBufferAlloc());
-
- sp<FramebufferSurface> fbs = new FramebufferSurface(*mHwc, i,
- consumer);
- int32_t hwcId = allocateHwcDisplayId(type);
- sp<DisplayDevice> hw = new DisplayDevice(this,
- type, hwcId, mHwc->getFormat(hwcId), isSecure, token,
- fbs, producer,
- mRenderEngine->getEGLConfig());
- if (i > DisplayDevice::DISPLAY_PRIMARY) {
- // FIXME: currently we don't get blank/unblank requests
- // for displays other than the main display, so we always
- // assume a connected display is unblanked.
- ALOGD("marking display %zu as acquired/unblanked", i);
- hw->setPowerMode(HWC_POWER_MODE_NORMAL);
- }
- mDisplays.add(token, hw);
- }
- }
-
- // make the GLContext current so that we can create textures when creating Layers
- // (which may happens before we render something)
+ // make the GLContext current so that we can create textures when creating
+ // Layers (which may happens before we render something)
getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
mEventControlThread = new EventControlThread(this);
mEventControlThread->run("EventControl", PRIORITY_URGENT_DISPLAY);
- // set a fake vsync period if there is no HWComposer
- if (mHwc->initCheck() != NO_ERROR) {
- mPrimaryDispSync.setPeriod(16666667);
- }
-
// initialize our drawing state
mDrawingState = mCurrentState;
@@ -524,11 +495,8 @@
// start boot animation
startBootAnim();
-}
-int32_t SurfaceFlinger::allocateHwcDisplayId(DisplayDevice::DisplayType type) {
- return (uint32_t(type) < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) ?
- type : mHwc->allocateDisplayId();
+ ALOGV("Done initializing");
}
void SurfaceFlinger::startBootAnim() {
@@ -594,14 +562,11 @@
configs->clear();
- const Vector<HWComposer::DisplayConfig>& hwConfigs =
- getHwComposer().getConfigs(type);
- for (size_t c = 0; c < hwConfigs.size(); ++c) {
- const HWComposer::DisplayConfig& hwConfig = hwConfigs[c];
+ for (const auto& hwConfig : getHwComposer().getConfigs(type)) {
DisplayInfo info = DisplayInfo();
- float xdpi = hwConfig.xdpi;
- float ydpi = hwConfig.ydpi;
+ float xdpi = hwConfig->getDpiX();
+ float ydpi = hwConfig->getDpiY();
if (type == DisplayDevice::DISPLAY_PRIMARY) {
// The density of the device is provided by a build property
@@ -629,13 +594,15 @@
info.orientation = 0;
}
- info.w = hwConfig.width;
- info.h = hwConfig.height;
+ info.w = hwConfig->getWidth();
+ info.h = hwConfig->getHeight();
info.xdpi = xdpi;
info.ydpi = ydpi;
- info.fps = float(1e9 / hwConfig.refresh);
+ info.fps = 1e9 / hwConfig->getVsyncPeriod();
info.appVsyncOffset = VSYNC_EVENT_PHASE_OFFSET_NS;
- info.colorTransform = hwConfig.colorTransform;
+
+ // TODO: Hook this back up
+ info.colorTransform = 0;
// This is how far in advance a buffer must be queued for
// presentation at a given time. If you want a buffer to appear
@@ -649,8 +616,8 @@
//
// We add an additional 1ms to allow for processing time and
// differences between the ideal and actual refresh rate.
- info.presentationDeadline =
- hwConfig.refresh - SF_VSYNC_EVENT_PHASE_OFFSET_NS + 1000000;
+ info.presentationDeadline = hwConfig->getVsyncPeriod() -
+ SF_VSYNC_EVENT_PHASE_OFFSET_NS + 1000000;
// All non-virtual displays are currently considered secure.
info.secure = true;
@@ -812,8 +779,8 @@
return;
}
- const nsecs_t period =
- getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
+ const auto& activeConfig = mHwc->getActiveConfig(HWC_DISPLAY_PRIMARY);
+ const nsecs_t period = activeConfig->getVsyncPeriod();
mPrimaryDispSync.reset();
mPrimaryDispSync.setPeriod(period);
@@ -839,7 +806,7 @@
}
}
-void SurfaceFlinger::onVSyncReceived(int type, nsecs_t timestamp) {
+void SurfaceFlinger::onVSyncReceived(int32_t type, nsecs_t timestamp) {
bool needsHwVsync = false;
{ // Scope for the lock
@@ -856,19 +823,34 @@
}
}
-void SurfaceFlinger::onHotplugReceived(int type, bool connected) {
- if (mEventThread == NULL) {
- // This is a temporary workaround for b/7145521. A non-null pointer
- // does not mean EventThread has finished initializing, so this
- // is not a correct fix.
- ALOGW("WARNING: EventThread not started, ignoring hotplug");
- return;
- }
+void SurfaceFlinger::onHotplugReceived(int32_t disp, bool connected) {
+ ALOGV("onHotplugReceived(%d, %s)", disp, connected ? "true" : "false");
+ if (disp == DisplayDevice::DISPLAY_PRIMARY) {
+ Mutex::Autolock lock(mStateLock);
- if (uint32_t(type) < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
+ // All non-virtual displays are currently considered secure.
+ bool isSecure = true;
+
+ int32_t type = DisplayDevice::DISPLAY_PRIMARY;
+ createBuiltinDisplayLocked(DisplayDevice::DISPLAY_PRIMARY);
+ wp<IBinder> token = mBuiltinDisplays[type];
+
+ sp<IGraphicBufferProducer> producer;
+ sp<IGraphicBufferConsumer> consumer;
+ BufferQueue::createBufferQueue(&producer, &consumer,
+ new GraphicBufferAlloc());
+
+ sp<FramebufferSurface> fbs = new FramebufferSurface(*mHwc,
+ DisplayDevice::DISPLAY_PRIMARY, consumer);
+ sp<DisplayDevice> hw = new DisplayDevice(this,
+ DisplayDevice::DISPLAY_PRIMARY, disp, isSecure, token, fbs,
+ producer, mRenderEngine->getEGLConfig());
+ mDisplays.add(token, hw);
+ } else {
+ auto type = DisplayDevice::DISPLAY_EXTERNAL;
Mutex::Autolock _l(mStateLock);
if (connected) {
- createBuiltinDisplayLocked((DisplayDevice::DisplayType)type);
+ createBuiltinDisplayLocked(type);
} else {
mCurrentState.displays.removeItem(mBuiltinDisplays[type]);
mBuiltinDisplays[type].clear();
@@ -879,9 +861,10 @@
}
}
-void SurfaceFlinger::eventControl(int disp, int event, int enabled) {
+void SurfaceFlinger::setVsyncEnabled(int disp, int enabled) {
ATRACE_CALL();
- getHwComposer().eventControl(disp, event, enabled);
+ getHwComposer().setVsyncEnabled(disp,
+ enabled ? HWC2::Vsync::Enable : HWC2::Vsync::Disable);
}
void SurfaceFlinger::onMessageReceived(int32_t what) {
@@ -950,6 +933,12 @@
postComposition();
}
+ // Release any buffers which were replaced this frame
+ for (auto& layer : mLayersWithQueuedFrames) {
+ layer->releasePendingBuffer();
+ }
+ mLayersWithQueuedFrames.clear();
+
previousExpectedPresent = mPrimaryDispSync.computeNextRefresh(0);
}
@@ -974,7 +963,6 @@
RenderEngine& engine(getRenderEngine());
engine.fillRegionWithColor(dirtyRegion, height, 1, 0, 1, 1);
- hw->compositionComplete();
hw->swapBuffers(getHwComposer());
}
}
@@ -986,15 +974,18 @@
usleep(mDebugRegion * 1000);
}
- HWComposer& hwc(getHwComposer());
- if (hwc.initCheck() == NO_ERROR) {
- status_t err = hwc.prepare();
- ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
+ for (size_t displayId = 0; displayId < mDisplays.size(); ++displayId) {
+ status_t result = mDisplays[displayId]->prepareFrame(*mHwc);
+ ALOGE_IF(result != NO_ERROR, "prepareFrame for display %zd failed:"
+ " %d (%s)", displayId, result, strerror(-result));
}
}
void SurfaceFlinger::preComposition()
{
+ ATRACE_CALL();
+ ALOGV("preComposition");
+
bool needExtraInvalidate = false;
const LayerVector& layers(mDrawingState.layersSortedByZ);
const size_t count = layers.size();
@@ -1010,14 +1001,16 @@
void SurfaceFlinger::postComposition()
{
+ ATRACE_CALL();
+ ALOGV("postComposition");
+
const LayerVector& layers(mDrawingState.layersSortedByZ);
const size_t count = layers.size();
for (size_t i=0 ; i<count ; i++) {
layers[i]->onPostComposition();
}
- const HWComposer& hwc = getHwComposer();
- sp<Fence> presentFence = hwc.getDisplayFence(HWC_DISPLAY_PRIMARY);
+ sp<Fence> presentFence = mHwc->getRetireFence(HWC_DISPLAY_PRIMARY);
if (presentFence->isValid()) {
if (mPrimaryDispSync.addPresentFence(presentFence)) {
@@ -1042,7 +1035,8 @@
} else {
// The HWC doesn't support present fences, so use the refresh
// timestamp instead.
- nsecs_t presentTime = hwc.getRefreshTimestamp(HWC_DISPLAY_PRIMARY);
+ nsecs_t presentTime =
+ mHwc->getRefreshTimestamp(HWC_DISPLAY_PRIMARY);
mAnimFrameTracker.setActualPresentTime(presentTime);
}
mAnimFrameTracker.advanceFrame();
@@ -1070,6 +1064,9 @@
}
void SurfaceFlinger::rebuildLayerStacks() {
+ ATRACE_CALL();
+ ALOGV("rebuildLayerStacks");
+
// rebuild the visible layer list per screen
if (CC_UNLIKELY(mVisibleRegionsDirty)) {
ATRACE_CALL();
@@ -1080,37 +1077,47 @@
for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Region opaqueRegion;
Region dirtyRegion;
- Vector< sp<Layer> > layersSortedByZ;
- const sp<DisplayDevice>& hw(mDisplays[dpy]);
- const Transform& tr(hw->getTransform());
- const Rect bounds(hw->getBounds());
- if (hw->isDisplayOn()) {
+ Vector<sp<Layer>> layersSortedByZ;
+ const sp<DisplayDevice>& displayDevice(mDisplays[dpy]);
+ const Transform& tr(displayDevice->getTransform());
+ const Rect bounds(displayDevice->getBounds());
+ if (displayDevice->isDisplayOn()) {
SurfaceFlinger::computeVisibleRegions(layers,
- hw->getLayerStack(), dirtyRegion, opaqueRegion);
+ displayDevice->getLayerStack(), dirtyRegion,
+ opaqueRegion);
const size_t count = layers.size();
for (size_t i=0 ; i<count ; i++) {
const sp<Layer>& layer(layers[i]);
const Layer::State& s(layer->getDrawingState());
- if (s.layerStack == hw->getLayerStack()) {
+ if (s.layerStack == displayDevice->getLayerStack()) {
Region drawRegion(tr.transform(
layer->visibleNonTransparentRegion));
drawRegion.andSelf(bounds);
if (!drawRegion.isEmpty()) {
layersSortedByZ.add(layer);
+ } else {
+ // Clear out the HWC layer if this layer was
+ // previously visible, but no longer is
+ layer->setHwcLayer(displayDevice->getHwcDisplayId(),
+ nullptr);
}
}
}
}
- hw->setVisibleLayersSortedByZ(layersSortedByZ);
- hw->undefinedRegion.set(bounds);
- hw->undefinedRegion.subtractSelf(tr.transform(opaqueRegion));
- hw->dirtyRegion.orSelf(dirtyRegion);
+ displayDevice->setVisibleLayersSortedByZ(layersSortedByZ);
+ displayDevice->undefinedRegion.set(bounds);
+ displayDevice->undefinedRegion.subtractSelf(
+ tr.transform(opaqueRegion));
+ displayDevice->dirtyRegion.orSelf(dirtyRegion);
}
}
}
void SurfaceFlinger::setUpHWComposer() {
+ ATRACE_CALL();
+ ALOGV("setUpHWComposer");
+
for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
bool dirty = !mDisplays[dpy]->getDirtyRegion(false).isEmpty();
bool empty = mDisplays[dpy]->getVisibleLayersSortedByZ().size() == 0;
@@ -1140,86 +1147,61 @@
}
}
- HWComposer& hwc(getHwComposer());
- if (hwc.initCheck() == NO_ERROR) {
- // build the h/w work list
- if (CC_UNLIKELY(mHwWorkListDirty)) {
- mHwWorkListDirty = false;
- for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
- sp<const DisplayDevice> hw(mDisplays[dpy]);
- const int32_t id = hw->getHwcDisplayId();
- if (id >= 0) {
- const Vector< sp<Layer> >& currentLayers(
- hw->getVisibleLayersSortedByZ());
- const size_t count = currentLayers.size();
- if (hwc.createWorkList(id, count) == NO_ERROR) {
- HWComposer::LayerListIterator cur = hwc.begin(id);
- const HWComposer::LayerListIterator end = hwc.end(id);
- for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
- const sp<Layer>& layer(currentLayers[i]);
- layer->setGeometry(hw, *cur);
- if (mDebugDisableHWC || mDebugRegion || mDaltonize || mHasColorMatrix) {
- cur->setSkip(true);
- }
+ // build the h/w work list
+ if (CC_UNLIKELY(mGeometryInvalid)) {
+ mGeometryInvalid = false;
+ for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
+ sp<const DisplayDevice> displayDevice(mDisplays[dpy]);
+ const auto hwcId = displayDevice->getHwcDisplayId();
+ if (hwcId >= 0) {
+ const Vector<sp<Layer>>& currentLayers(
+ displayDevice->getVisibleLayersSortedByZ());
+ bool foundLayerWithoutHwc = false;
+ for (auto& layer : currentLayers) {
+ if (!layer->hasHwcLayer(hwcId)) {
+ auto hwcLayer = mHwc->createLayer(hwcId);
+ if (hwcLayer) {
+ layer->setHwcLayer(hwcId, std::move(hwcLayer));
+ } else {
+ layer->forceClientComposition(hwcId);
+ foundLayerWithoutHwc = true;
+ continue;
}
}
- }
- }
- }
- // set the per-frame data
- for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
- sp<const DisplayDevice> hw(mDisplays[dpy]);
- const int32_t id = hw->getHwcDisplayId();
- if (id >= 0) {
- const Vector< sp<Layer> >& currentLayers(
- hw->getVisibleLayersSortedByZ());
- const size_t count = currentLayers.size();
- HWComposer::LayerListIterator cur = hwc.begin(id);
- const HWComposer::LayerListIterator end = hwc.end(id);
- for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
- /*
- * update the per-frame h/w composer data for each layer
- * and build the transparent region of the FB
- */
- const sp<Layer>& layer(currentLayers[i]);
- layer->setPerFrameData(hw, *cur);
- }
- }
- }
-
- // If possible, attempt to use the cursor overlay on each display.
- for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
- sp<const DisplayDevice> hw(mDisplays[dpy]);
- const int32_t id = hw->getHwcDisplayId();
- if (id >= 0) {
- const Vector< sp<Layer> >& currentLayers(
- hw->getVisibleLayersSortedByZ());
- const size_t count = currentLayers.size();
- HWComposer::LayerListIterator cur = hwc.begin(id);
- const HWComposer::LayerListIterator end = hwc.end(id);
- for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
- const sp<Layer>& layer(currentLayers[i]);
- if (layer->isPotentialCursor()) {
- cur->setIsCursorLayerHint();
- break;
+ layer->setGeometry(displayDevice);
+ if (mDebugDisableHWC || mDebugRegion || mDaltonize ||
+ mHasColorMatrix) {
+ layer->forceClientComposition(hwcId);
}
}
}
}
+ }
- status_t err = hwc.prepare();
- ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
-
- for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
- sp<const DisplayDevice> hw(mDisplays[dpy]);
- hw->prepareFrame(hwc);
+ // Set the per-frame data
+ for (size_t displayId = 0; displayId < mDisplays.size(); ++displayId) {
+ auto& displayDevice = mDisplays[displayId];
+ const auto hwcId = displayDevice->getHwcDisplayId();
+ if (hwcId < 0) {
+ continue;
}
+ for (auto& layer : displayDevice->getVisibleLayersSortedByZ()) {
+ layer->setPerFrameData(displayDevice);
+ }
+ }
+
+ for (size_t displayId = 0; displayId < mDisplays.size(); ++displayId) {
+ status_t result = mDisplays[displayId]->prepareFrame(*mHwc);
+ ALOGE_IF(result != NO_ERROR, "prepareFrame for display %zd failed:"
+ " %d (%s)", displayId, result, strerror(-result));
}
}
void SurfaceFlinger::doComposition() {
ATRACE_CALL();
+ ALOGV("doComposition");
+
const bool repaintEverything = android_atomic_and(0, &mRepaintEverything);
for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
const sp<DisplayDevice>& hw(mDisplays[dpy]);
@@ -1234,8 +1216,6 @@
hw->flip(hw->swapRegion);
hw->swapRegion.clear();
}
- // inform the h/w that we're done compositing
- hw->compositionComplete();
}
postFramebuffer();
}
@@ -1243,43 +1223,37 @@
void SurfaceFlinger::postFramebuffer()
{
ATRACE_CALL();
+ ALOGV("postFramebuffer");
const nsecs_t now = systemTime();
mDebugInSwapBuffers = now;
- HWComposer& hwc(getHwComposer());
- if (hwc.initCheck() == NO_ERROR) {
- if (!hwc.supportsFramebufferTarget()) {
- // EGL spec says:
- // "surface must be bound to the calling thread's current context,
- // for the current rendering API."
- getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
+ for (size_t displayId = 0; displayId < mDisplays.size(); ++displayId) {
+ auto& displayDevice = mDisplays[displayId];
+ const auto hwcId = displayDevice->getHwcDisplayId();
+ if (hwcId >= 0) {
+ mHwc->commit(hwcId);
}
- hwc.commit();
- }
-
- // make the default display current because the VirtualDisplayDevice code cannot
- // deal with dequeueBuffer() being called outside of the composition loop; however
- // the code below can call glFlush() which is allowed (and does in some case) call
- // dequeueBuffer().
- getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
-
- for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
- sp<const DisplayDevice> hw(mDisplays[dpy]);
- const Vector< sp<Layer> >& currentLayers(hw->getVisibleLayersSortedByZ());
- hw->onSwapBuffersCompleted(hwc);
- const size_t count = currentLayers.size();
- int32_t id = hw->getHwcDisplayId();
- if (id >=0 && hwc.initCheck() == NO_ERROR) {
- HWComposer::LayerListIterator cur = hwc.begin(id);
- const HWComposer::LayerListIterator end = hwc.end(id);
- for (size_t i = 0; cur != end && i < count; ++i, ++cur) {
- currentLayers[i]->onLayerDisplayed(hw, &*cur);
+ if (displayId == 0) {
+ // Make the default display current because the VirtualDisplayDevice
+ // code cannot deal with dequeueBuffer() being called outside of the
+ // composition loop; however the code below can call glFlush() which
+ // is allowed to (and does in some case) call dequeueBuffer().
+ displayDevice->makeCurrent(mEGLDisplay, mEGLContext);
+ }
+ displayDevice->onSwapBuffersCompleted();
+ for (auto& layer : displayDevice->getVisibleLayersSortedByZ()) {
+ sp<Fence> releaseFence = Fence::NO_FENCE;
+ if (layer->getCompositionType(hwcId) == HWC2::Composition::Client) {
+ releaseFence = displayDevice->getClientTargetAcquireFence();
+ } else {
+ auto hwcLayer = layer->getHwcLayer(hwcId);
+ releaseFence = mHwc->getLayerReleaseFence(hwcId, hwcLayer);
}
- } else {
- for (size_t i = 0; i < count; i++) {
- currentLayers[i]->onLayerDisplayed(hw, NULL);
- }
+ layer->onLayerDisplayed(releaseFence);
+ }
+ if (hwcId >= 0) {
+ mHwc->clearReleaseFences(hwcId);
}
}
@@ -1439,7 +1413,7 @@
BufferQueue::createBufferQueue(&bqProducer, &bqConsumer,
new GraphicBufferAlloc());
- int32_t hwcDisplayId = -1;
+ int32_t hwcId = -1;
if (state.isVirtualDisplay()) {
// Virtual displays without a surface are dormant:
// they have external state (layer stack, projection,
@@ -1456,15 +1430,14 @@
NATIVE_WINDOW_HEIGHT, &height);
ALOGE_IF(status != NO_ERROR,
"Unable to query height (%d)", status);
- if (MAX_VIRTUAL_DISPLAY_DIMENSION == 0 ||
- (width <= MAX_VIRTUAL_DISPLAY_DIMENSION &&
- height <= MAX_VIRTUAL_DISPLAY_DIMENSION)) {
- hwcDisplayId = allocateHwcDisplayId(state.type);
- }
- sp<VirtualDisplaySurface> vds = new VirtualDisplaySurface(
- *mHwc, hwcDisplayId, state.surface,
- bqProducer, bqConsumer, state.displayName);
+ mHwc->allocateVirtualDisplay(width, height,
+ &hwcId);
+
+ sp<VirtualDisplaySurface> vds =
+ new VirtualDisplaySurface(*mHwc,
+ hwcId, state.surface, bqProducer,
+ bqConsumer, state.displayName);
dispSurface = vds;
producer = vds;
@@ -1474,33 +1447,30 @@
"adding a supported display, but rendering "
"surface is provided (%p), ignoring it",
state.surface.get());
- hwcDisplayId = allocateHwcDisplayId(state.type);
- // for supported (by hwc) displays we provide our
- // own rendering surface
- dispSurface = new FramebufferSurface(*mHwc, state.type,
- bqConsumer);
- producer = bqProducer;
+ if (state.type == DisplayDevice::DISPLAY_EXTERNAL) {
+ hwcId = DisplayDevice::DISPLAY_EXTERNAL;
+ dispSurface = new FramebufferSurface(*mHwc,
+ DisplayDevice::DISPLAY_EXTERNAL,
+ bqConsumer);
+ producer = bqProducer;
+ } else {
+ ALOGE("Attempted to add non-external non-virtual"
+ " display");
+ }
}
const wp<IBinder>& display(curr.keyAt(i));
if (dispSurface != NULL) {
sp<DisplayDevice> hw = new DisplayDevice(this,
- state.type, hwcDisplayId,
- mHwc->getFormat(hwcDisplayId), state.isSecure,
- display, dispSurface, producer,
+ state.type, hwcId, state.isSecure, display,
+ dispSurface, producer,
mRenderEngine->getEGLConfig());
hw->setLayerStack(state.layerStack);
hw->setProjection(state.orientation,
state.viewport, state.frame);
hw->setDisplayName(state.displayName);
mDisplays.add(display, hw);
- if (state.isVirtualDisplay()) {
- if (hwcDisplayId >= 0) {
- mHwc->setVirtualDisplayProperties(hwcDisplayId,
- hw->getWidth(), hw->getHeight(),
- hw->getFormat());
- }
- } else {
+ if (!state.isVirtualDisplay()) {
mEventThread->onHotplugReceived(state.type, true);
}
}
@@ -1607,26 +1577,14 @@
void SurfaceFlinger::updateCursorAsync()
{
- HWComposer& hwc(getHwComposer());
- for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
- sp<const DisplayDevice> hw(mDisplays[dpy]);
- const int32_t id = hw->getHwcDisplayId();
- if (id < 0) {
+ for (size_t displayId = 0; displayId < mDisplays.size(); ++displayId) {
+ auto& displayDevice = mDisplays[displayId];
+ if (displayDevice->getHwcDisplayId() < 0) {
continue;
}
- const Vector< sp<Layer> >& currentLayers(
- hw->getVisibleLayersSortedByZ());
- const size_t count = currentLayers.size();
- HWComposer::LayerListIterator cur = hwc.begin(id);
- const HWComposer::LayerListIterator end = hwc.end(id);
- for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
- if (cur->getCompositionType() != HWC_CURSOR_OVERLAY) {
- continue;
- }
- const sp<Layer>& layer(currentLayers[i]);
- Rect cursorPos = layer->getPosition(hw);
- hwc.setCursorPositionAsync(id, cursorPos);
- break;
+
+ for (auto& layer : displayDevice->getVisibleLayersSortedByZ()) {
+ layer->updateCursorPosition(displayDevice);
}
}
}
@@ -1656,6 +1614,7 @@
Region& outDirtyRegion, Region& outOpaqueRegion)
{
ATRACE_CALL();
+ ALOGV("computeVisibleRegions");
Region aboveOpaqueLayers;
Region aboveCoveredLayers;
@@ -1729,7 +1688,7 @@
// compute the opaque region
const int32_t layerOrientation = s.transform.getOrientation();
- if (s.alpha==255 && !translucent &&
+ if (s.alpha == 1.0f && !translucent &&
((layerOrientation & Transform::ROT_INVALID) == false)) {
// the opaque region is the layer's footprint
opaqueRegion = visibleRegion;
@@ -1802,6 +1761,8 @@
bool SurfaceFlinger::handlePageFlip()
{
+ ALOGV("handlePageFlip");
+
Region dirtyRegion;
bool visibleRegions = false;
@@ -1817,13 +1778,12 @@
// 3.) Layer 1 is latched.
// Display is now waiting on Layer 1's frame, which is behind layer 0's
// second frame. But layer 0's second frame could be waiting on display.
- Vector<Layer*> layersWithQueuedFrames;
for (size_t i = 0, count = layers.size(); i<count ; i++) {
const sp<Layer>& layer(layers[i]);
if (layer->hasQueuedFrame()) {
frameQueued = true;
if (layer->shouldPresentNow(mPrimaryDispSync)) {
- layersWithQueuedFrames.push_back(layer.get());
+ mLayersWithQueuedFrames.push_back(layer.get());
} else {
layer->useEmptyDamage();
}
@@ -1831,8 +1791,7 @@
layer->useEmptyDamage();
}
}
- for (size_t i = 0, count = layersWithQueuedFrames.size() ; i<count ; i++) {
- Layer* layer = layersWithQueuedFrames[i];
+ for (auto& layer : mLayersWithQueuedFrames) {
const Region dirty(layer->latchBuffer(visibleRegions));
layer->useSurfaceDamage();
const Layer::State& s(layer->getDrawingState());
@@ -1844,17 +1803,17 @@
// If we will need to wake up at some time in the future to deal with a
// queued frame that shouldn't be displayed during this vsync period, wake
// up during the next vsync period to check again.
- if (frameQueued && layersWithQueuedFrames.empty()) {
+ if (frameQueued && mLayersWithQueuedFrames.empty()) {
signalLayerUpdate();
}
// Only continue with the refresh if there is actually new work to do
- return !layersWithQueuedFrames.empty();
+ return !mLayersWithQueuedFrames.empty();
}
void SurfaceFlinger::invalidateHwcGeometry()
{
- mHwWorkListDirty = true;
+ mGeometryInvalid = true;
}
@@ -1867,9 +1826,12 @@
// 2) There is work to be done (the dirty region isn't empty)
bool isHwcDisplay = hw->getHwcDisplayId() >= 0;
if (!isHwcDisplay && inDirtyRegion.isEmpty()) {
+ ALOGV("Skipping display composition");
return;
}
+ ALOGV("doDisplayComposition");
+
Region dirtyRegion(inDirtyRegion);
// compute the invalid region
@@ -1915,19 +1877,19 @@
hw->swapBuffers(getHwComposer());
}
-bool SurfaceFlinger::doComposeSurfaces(const sp<const DisplayDevice>& hw, const Region& dirty)
+bool SurfaceFlinger::doComposeSurfaces(
+ const sp<const DisplayDevice>& displayDevice, const Region& dirty)
{
- RenderEngine& engine(getRenderEngine());
- const int32_t id = hw->getHwcDisplayId();
- HWComposer& hwc(getHwComposer());
- HWComposer::LayerListIterator cur = hwc.begin(id);
- const HWComposer::LayerListIterator end = hwc.end(id);
+ ALOGV("doComposeSurfaces");
- bool hasGlesComposition = hwc.hasGlesComposition(id);
- if (hasGlesComposition) {
- if (!hw->makeCurrent(mEGLDisplay, mEGLContext)) {
+ const auto hwcId = displayDevice->getHwcDisplayId();
+ bool hasClientComposition = mHwc->hasClientComposition(hwcId);
+ if (hasClientComposition) {
+ ALOGV("hasClientComposition");
+
+ if (!displayDevice->makeCurrent(mEGLDisplay, mEGLContext)) {
ALOGW("DisplayDevice::makeCurrent failed. Aborting surface composition for display %s",
- hw->getDisplayName().string());
+ displayDevice->getDisplayName().string());
eglMakeCurrent(mEGLDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
if(!getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext)) {
ALOGE("DisplayDevice::makeCurrent on default display failed. Aborting.");
@@ -1936,25 +1898,25 @@
}
// Never touch the framebuffer if we don't have any framebuffer layers
- const bool hasHwcComposition = hwc.hasHwcComposition(id);
- if (hasHwcComposition) {
+ const bool hasDeviceComposition = mHwc->hasDeviceComposition(hwcId);
+ if (hasDeviceComposition) {
// when using overlays, we assume a fully transparent framebuffer
// NOTE: we could reduce how much we need to clear, for instance
// remove where there are opaque FB layers. however, on some
// GPUs doing a "clean slate" clear might be more efficient.
// We'll revisit later if needed.
- engine.clearWithColor(0, 0, 0, 0);
+ mRenderEngine->clearWithColor(0, 0, 0, 0);
} else {
// we start with the whole screen area
- const Region bounds(hw->getBounds());
+ const Region bounds(displayDevice->getBounds());
// we remove the scissor part
// we're left with the letterbox region
// (common case is that letterbox ends-up being empty)
- const Region letterbox(bounds.subtract(hw->getScissor()));
+ const Region letterbox(bounds.subtract(displayDevice->getScissor()));
// compute the area to clear
- Region region(hw->undefinedRegion.merge(letterbox));
+ Region region(displayDevice->undefinedRegion.merge(letterbox));
// but limit it to the dirty region
region.andSelf(dirty);
@@ -1962,24 +1924,24 @@
// screen is already cleared here
if (!region.isEmpty()) {
// can happen with SurfaceView
- drawWormhole(hw, region);
+ drawWormhole(displayDevice, region);
}
}
- if (hw->getDisplayType() != DisplayDevice::DISPLAY_PRIMARY) {
+ if (displayDevice->getDisplayType() != DisplayDevice::DISPLAY_PRIMARY) {
// just to be on the safe side, we don't set the
// scissor on the main display. It should never be needed
// anyways (though in theory it could since the API allows it).
- const Rect& bounds(hw->getBounds());
- const Rect& scissor(hw->getScissor());
+ const Rect& bounds(displayDevice->getBounds());
+ const Rect& scissor(displayDevice->getScissor());
if (scissor != bounds) {
// scissor doesn't match the screen's dimensions, so we
// need to clear everything outside of it and enable
// the GL scissor so we don't draw anything where we shouldn't
// enable scissor for this frame
- const uint32_t height = hw->getHeight();
- engine.setScissor(scissor.left, height - scissor.bottom,
+ const uint32_t height = displayDevice->getHeight();
+ mRenderEngine->setScissor(scissor.left, height - scissor.bottom,
scissor.getWidth(), scissor.getHeight());
}
}
@@ -1989,57 +1951,57 @@
* and then, render the layers targeted at the framebuffer
*/
- const Vector< sp<Layer> >& layers(hw->getVisibleLayersSortedByZ());
- const size_t count = layers.size();
- const Transform& tr = hw->getTransform();
- if (cur != end) {
+ ALOGV("Rendering client layers");
+ const Transform& displayTransform = displayDevice->getTransform();
+ if (hwcId >= 0) {
// we're using h/w composer
- for (size_t i=0 ; i<count && cur!=end ; ++i, ++cur) {
- const sp<Layer>& layer(layers[i]);
- const Region clip(dirty.intersect(tr.transform(layer->visibleRegion)));
+ bool firstLayer = true;
+ for (auto& layer : displayDevice->getVisibleLayersSortedByZ()) {
+ const Region clip(dirty.intersect(
+ displayTransform.transform(layer->visibleRegion)));
+ ALOGV("Layer: %s", layer->getName().string());
+ ALOGV(" Composition type: %s",
+ to_string(layer->getCompositionType(hwcId)).c_str());
if (!clip.isEmpty()) {
- switch (cur->getCompositionType()) {
- case HWC_CURSOR_OVERLAY:
- case HWC_OVERLAY: {
+ switch (layer->getCompositionType(hwcId)) {
+ case HWC2::Composition::Cursor:
+ case HWC2::Composition::Device:
+ case HWC2::Composition::SolidColor: {
const Layer::State& state(layer->getDrawingState());
- if ((cur->getHints() & HWC_HINT_CLEAR_FB)
- && i
- && layer->isOpaque(state) && (state.alpha == 0xFF)
- && hasGlesComposition) {
+ if (layer->getClearClientTarget(hwcId) && !firstLayer &&
+ layer->isOpaque(state) && (state.alpha == 1.0f)
+ && hasClientComposition) {
// never clear the very first layer since we're
// guaranteed the FB is already cleared
- layer->clearWithOpenGL(hw, clip);
+ layer->clearWithOpenGL(displayDevice, clip);
}
break;
}
- case HWC_FRAMEBUFFER: {
- layer->draw(hw, clip);
+ case HWC2::Composition::Client: {
+ layer->draw(displayDevice, clip);
break;
}
- case HWC_FRAMEBUFFER_TARGET: {
- // this should not happen as the iterator shouldn't
- // let us get there.
- ALOGW("HWC_FRAMEBUFFER_TARGET found in hwc list (index=%zu)", i);
+ default:
break;
- }
}
+ } else {
+ ALOGV(" Skipping for empty clip");
}
- layer->setAcquireFence(hw, *cur);
+ firstLayer = false;
}
} else {
// we're not using h/w composer
- for (size_t i=0 ; i<count ; ++i) {
- const sp<Layer>& layer(layers[i]);
+ for (auto& layer : displayDevice->getVisibleLayersSortedByZ()) {
const Region clip(dirty.intersect(
- tr.transform(layer->visibleRegion)));
+ displayTransform.transform(layer->visibleRegion)));
if (!clip.isEmpty()) {
- layer->draw(hw, clip);
+ layer->draw(displayDevice, clip);
}
}
}
// disable scissor at the end of the frame
- engine.disableScissor();
+ mRenderEngine->disableScissor();
return true;
}
@@ -2255,7 +2217,7 @@
}
}
if (what & layer_state_t::eAlphaChanged) {
- if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
+ if (layer->setAlpha(s.alpha))
flags |= eTraversalNeeded;
}
if (what & layer_state_t::eMatrixChanged) {
@@ -2423,8 +2385,8 @@
setTransactionState(state, displays, 0);
setPowerModeInternal(getDisplayDevice(d.token), HWC_POWER_MODE_NORMAL);
- const nsecs_t period =
- getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
+ const auto& activeConfig = mHwc->getActiveConfig(HWC_DISPLAY_PRIMARY);
+ const nsecs_t period = activeConfig->getVsyncPeriod();
mAnimFrameTracker.setDisplayRefreshPeriod(period);
}
@@ -2611,8 +2573,8 @@
index++;
}
- const nsecs_t period =
- getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
+ const auto& activeConfig = mHwc->getActiveConfig(HWC_DISPLAY_PRIMARY);
+ const nsecs_t period = activeConfig->getVsyncPeriod();
result.appendFormat("%" PRId64 "\n", period);
if (name.isEmpty()) {
@@ -2734,13 +2696,15 @@
result.append(SyncFeatures::getInstance().toString());
result.append("\n");
+ const auto& activeConfig = mHwc->getActiveConfig(HWC_DISPLAY_PRIMARY);
+
colorizer.bold(result);
result.append("DispSync configuration: ");
colorizer.reset(result);
result.appendFormat("app phase %" PRId64 " ns, sf phase %" PRId64 " ns, "
"present offset %d ns (refresh %" PRId64 " ns)",
- vsyncPhaseOffsetNs, sfVsyncPhaseOffsetNs, PRESENT_TIME_OFFSET_FROM_VSYNC_NS,
- mHwc->getRefreshPeriod(HWC_DISPLAY_PRIMARY));
+ vsyncPhaseOffsetNs, sfVsyncPhaseOffsetNs,
+ PRESENT_TIME_OFFSET_FROM_VSYNC_NS, activeConfig->getVsyncPeriod());
result.append("\n");
// Dump static screen stats
@@ -2808,9 +2772,9 @@
mLastSwapBufferTime/1000.0,
mLastTransactionTime/1000.0,
mTransactionFlags,
- 1e9 / hwc.getRefreshPeriod(HWC_DISPLAY_PRIMARY),
- hwc.getDpiX(HWC_DISPLAY_PRIMARY),
- hwc.getDpiY(HWC_DISPLAY_PRIMARY),
+ 1e9 / activeConfig->getVsyncPeriod(),
+ activeConfig->getDpiX(),
+ activeConfig->getDpiY(),
!mGpuToCpuSupported);
result.appendFormat(" eglSwapBuffers time: %f us\n",
@@ -2830,10 +2794,10 @@
colorizer.bold(result);
result.append("h/w composer state:\n");
colorizer.reset(result);
- result.appendFormat(" h/w composer %s and %s\n",
- hwc.initCheck()==NO_ERROR ? "present" : "not present",
- (mDebugDisableHWC || mDebugRegion || mDaltonize
- || mHasColorMatrix) ? "disabled" : "enabled");
+ bool hwcDisabled = mDebugDisableHWC || mDebugRegion || mDaltonize ||
+ mHasColorMatrix;
+ result.appendFormat(" h/w composer %s\n",
+ hwcDisabled ? "disabled" : "enabled");
hwc.dump(result);
/*
@@ -3338,8 +3302,6 @@
}
}
- // compositionComplete is needed for older driver
- hw->compositionComplete();
hw->setViewportAndProjection();
}
@@ -3516,7 +3478,7 @@
const bool visible = (state.layerStack == hw->getLayerStack())
&& (state.z >= minLayerZ && state.z <= maxLayerZ)
&& (layer->isVisible());
- ALOGE("%c index=%zu, name=%s, layerStack=%d, z=%d, visible=%d, flags=%x, alpha=%x",
+ ALOGE("%c index=%zu, name=%s, layerStack=%d, z=%d, visible=%d, flags=%x, alpha=%.3f",
visible ? '+' : '-',
i, layer->getName().string(), state.layerStack, state.z,
layer->isVisible(), state.flags, state.alpha);