frame_worker_(this),
initialized_(false),
active_(false),
- needs_modeset_(false),
+ use_hw_overlays_(true),
framebuffer_index_(0),
squash_framebuffer_index_(0),
dump_frames_composited_(0),
if (ret)
ALOGE("Failed to acquire compositor lock %d", ret);
+ if (mode_.blob_id)
+ drm_->DestroyPropertyBlob(mode_.blob_id);
+ if (mode_.old_blob_id)
+ drm_->DestroyPropertyBlob(mode_.old_blob_id);
+
while (!composite_queue_.empty()) {
composite_queue_.front().reset();
composite_queue_.pop();
display_comp->layers().emplace_back();
DrmHwcLayer &pre_comp_layer = display_comp->layers().back();
pre_comp_layer.sf_handle = fb.buffer()->handle;
- pre_comp_layer.blending = DrmHwcBlending::kCoverage;
+ pre_comp_layer.blending = DrmHwcBlending::kPreMult;
pre_comp_layer.source_crop = DrmHwcRect<float>(0, 0, width, height);
pre_comp_layer.display_frame = DrmHwcRect<int>(0, 0, width, height);
ret = pre_comp_layer.buffer.ImportBuffer(fb.buffer()->handle,
}
int DrmDisplayCompositor::DisablePlanes(DrmDisplayComposition *display_comp) {
- drmModePropertySetPtr pset = drmModePropertySetAlloc();
+ drmModeAtomicReqPtr pset = drmModeAtomicAlloc();
if (!pset) {
ALOGE("Failed to allocate property set");
return -ENOMEM;
display_comp->composition_planes();
for (DrmCompositionPlane &comp_plane : comp_planes) {
DrmPlane *plane = comp_plane.plane;
- ret =
- drmModePropertySetAdd(pset, plane->id(), plane->crtc_property().id(),
- 0) ||
- drmModePropertySetAdd(pset, plane->id(), plane->fb_property().id(), 0);
+ ret = drmModeAtomicAddProperty(pset, plane->id(),
+ plane->crtc_property().id(), 0) < 0 ||
+ drmModeAtomicAddProperty(pset, plane->id(), plane->fb_property().id(),
+ 0) < 0;
if (ret) {
ALOGE("Failed to add plane %d disable to pset", plane->id());
- drmModePropertySetFree(pset);
+ drmModeAtomicFree(pset);
return ret;
}
}
- ret = drmModePropertySetCommit(drm_->fd(), 0, drm_, pset);
+ ret = drmModeAtomicCommit(drm_->fd(), pset, 0, drm_);
if (ret) {
ALOGE("Failed to commit pset ret=%d\n", ret);
- drmModePropertySetFree(pset);
+ drmModeAtomicFree(pset);
return ret;
}
- drmModePropertySetFree(pset);
+ drmModeAtomicFree(pset);
return 0;
}
return ret;
}
squash_layer.sf_handle = fb.buffer()->handle;
- squash_layer.blending = DrmHwcBlending::kCoverage;
+ squash_layer.blending = DrmHwcBlending::kPreMult;
squash_layer.source_crop = DrmHwcRect<float>(
0, 0, squash_layer.buffer->width, squash_layer.buffer->height);
squash_layer.display_frame = DrmHwcRect<int>(
return ret;
}
-int DrmDisplayCompositor::CommitFrame(DrmDisplayComposition *display_comp) {
+int DrmDisplayCompositor::CommitFrame(DrmDisplayComposition *display_comp,
+ bool test_only) {
ATRACE_CALL();
int ret = 0;
return -ENODEV;
}
- drmModePropertySetPtr pset = drmModePropertySetAlloc();
+ drmModeAtomicReqPtr pset = drmModeAtomicAlloc();
if (!pset) {
ALOGE("Failed to allocate property set");
return -ENOMEM;
}
- uint32_t blob_id = 0;
- uint64_t old_blob_id;
- if (needs_modeset_) {
- DrmProperty old_mode;
- ret = drm_->GetCrtcProperty(*crtc, crtc->mode_property().name().c_str(),
- &old_mode);
- if (ret) {
- ALOGE("Failed to get old mode property from crtc %d", crtc->id());
- drmModePropertySetFree(pset);
- return ret;
- }
- ret = old_mode.value(&old_blob_id);
+ if (mode_.needs_modeset) {
+ ret = drmModeAtomicAddProperty(pset, crtc->id(), crtc->mode_property().id(),
+ mode_.blob_id) < 0 ||
+ drmModeAtomicAddProperty(pset, connector->id(),
+ connector->crtc_id_property().id(),
+ crtc->id()) < 0;
if (ret) {
- ALOGE("Could not get old blob id value %d", ret);
- drmModePropertySetFree(pset);
- return ret;
- }
-
- struct drm_mode_modeinfo drm_mode;
- memset(&drm_mode, 0, sizeof(drm_mode));
- next_mode_.ToDrmModeModeInfo(&drm_mode);
-
- ret = drm_->CreatePropertyBlob(&drm_mode, sizeof(struct drm_mode_modeinfo),
- &blob_id);
- if (ret) {
- ALOGE("Failed to create mode property blob %d", ret);
- drmModePropertySetFree(pset);
- return ret;
- }
-
- ret = drmModePropertySetAdd(pset, crtc->id(), crtc->mode_property().id(),
- blob_id) ||
- drmModePropertySetAdd(pset, connector->id(),
- connector->crtc_id_property().id(), crtc->id());
- if (ret) {
- ALOGE("Failed to add blob %d to pset", blob_id);
- drmModePropertySetFree(pset);
- drm_->DestroyPropertyBlob(blob_id);
+ ALOGE("Failed to add blob %d to pset", mode_.blob_id);
+ drmModeAtomicFree(pset);
return ret;
}
}
break;
}
DrmHwcLayer &layer = layers[comp_plane.source_layer];
- if (layer.acquire_fence.get() >= 0) {
+ if (!test_only && layer.acquire_fence.get() >= 0) {
int acquire_fence = layer.acquire_fence.get();
+ int total_fence_timeout = 0;
for (int i = 0; i < kAcquireWaitTries; ++i) {
- ret = sync_wait(acquire_fence, kAcquireWaitTimeoutMs);
+ int fence_timeout = kAcquireWaitTimeoutMs * (1 << i);
+ total_fence_timeout += fence_timeout;
+ ret = sync_wait(acquire_fence, fence_timeout);
if (ret)
ALOGW("Acquire fence %d wait %d failed (%d). Total time %d",
- acquire_fence, i, ret, (i + 1) * kAcquireWaitTimeoutMs);
+ acquire_fence, i, ret, total_fence_timeout);
}
if (ret) {
ALOGE("Failed to wait for acquire %d/%d", acquire_fence, ret);
source_crop = layer.source_crop;
if (layer.blending == DrmHwcBlending::kPreMult)
alpha = layer.alpha;
- switch (layer.transform) {
- case DrmHwcTransform::kFlipH:
- rotation = 1 << DRM_REFLECT_X;
- break;
- case DrmHwcTransform::kFlipV:
- rotation = 1 << DRM_REFLECT_Y;
- break;
- case DrmHwcTransform::kRotate90:
- rotation = 1 << DRM_ROTATE_90;
- break;
- case DrmHwcTransform::kRotate180:
- rotation = 1 << DRM_ROTATE_180;
- break;
- case DrmHwcTransform::kRotate270:
- rotation = 1 << DRM_ROTATE_270;
- break;
- case DrmHwcTransform::kIdentity:
- rotation = 0;
- break;
- default:
- ALOGE("Invalid transform value 0x%x given", layer.transform);
- break;
- }
- break;
+
+ rotation = 0;
+ if (layer.transform & DrmHwcTransform::kFlipH)
+ rotation |= 1 << DRM_REFLECT_X;
+ if (layer.transform & DrmHwcTransform::kFlipV)
+ rotation |= 1 << DRM_REFLECT_Y;
+ if (layer.transform & DrmHwcTransform::kRotate90)
+ rotation |= 1 << DRM_ROTATE_90;
+ else if (layer.transform & DrmHwcTransform::kRotate180)
+ rotation |= 1 << DRM_ROTATE_180;
+ else if (layer.transform & DrmHwcTransform::kRotate270)
+ rotation |= 1 << DRM_ROTATE_270;
}
}
// Disable the plane if there's no framebuffer
if (fb_id < 0) {
- ret = drmModePropertySetAdd(pset, plane->id(),
- plane->crtc_property().id(), 0) ||
- drmModePropertySetAdd(pset, plane->id(), plane->fb_property().id(),
- 0);
+ ret = drmModeAtomicAddProperty(pset, plane->id(),
+ plane->crtc_property().id(), 0) < 0 ||
+ drmModeAtomicAddProperty(pset, plane->id(),
+ plane->fb_property().id(), 0) < 0;
if (ret) {
ALOGE("Failed to add plane %d disable to pset", plane->id());
break;
break;
}
- ret =
- drmModePropertySetAdd(pset, plane->id(), plane->crtc_property().id(),
- crtc->id()) ||
- drmModePropertySetAdd(pset, plane->id(), plane->fb_property().id(),
- fb_id) ||
- drmModePropertySetAdd(pset, plane->id(), plane->crtc_x_property().id(),
- display_frame.left) ||
- drmModePropertySetAdd(pset, plane->id(), plane->crtc_y_property().id(),
- display_frame.top) ||
- drmModePropertySetAdd(pset, plane->id(), plane->crtc_w_property().id(),
- display_frame.right - display_frame.left) ||
- drmModePropertySetAdd(pset, plane->id(), plane->crtc_h_property().id(),
- display_frame.bottom - display_frame.top) ||
- drmModePropertySetAdd(pset, plane->id(), plane->src_x_property().id(),
- (int)(source_crop.left) << 16) ||
- drmModePropertySetAdd(pset, plane->id(), plane->src_y_property().id(),
- (int)(source_crop.top) << 16) ||
- drmModePropertySetAdd(pset, plane->id(), plane->src_w_property().id(),
- (int)(source_crop.right - source_crop.left)
- << 16) ||
- drmModePropertySetAdd(pset, plane->id(), plane->src_h_property().id(),
- (int)(source_crop.bottom - source_crop.top)
- << 16);
+ ret = drmModeAtomicAddProperty(pset, plane->id(),
+ plane->crtc_property().id(), crtc->id()) < 0;
+ ret |= drmModeAtomicAddProperty(pset, plane->id(),
+ plane->fb_property().id(), fb_id) < 0;
+ ret |= drmModeAtomicAddProperty(pset, plane->id(),
+ plane->crtc_x_property().id(),
+ display_frame.left) < 0;
+ ret |= drmModeAtomicAddProperty(pset, plane->id(),
+ plane->crtc_y_property().id(),
+ display_frame.top) < 0;
+ ret |= drmModeAtomicAddProperty(
+ pset, plane->id(), plane->crtc_w_property().id(),
+ display_frame.right - display_frame.left) < 0;
+ ret |= drmModeAtomicAddProperty(
+ pset, plane->id(), plane->crtc_h_property().id(),
+ display_frame.bottom - display_frame.top) < 0;
+ ret |= drmModeAtomicAddProperty(pset, plane->id(),
+ plane->src_x_property().id(),
+ (int)(source_crop.left) << 16) < 0;
+ ret |= drmModeAtomicAddProperty(pset, plane->id(),
+ plane->src_y_property().id(),
+ (int)(source_crop.top) << 16) < 0;
+ ret |= drmModeAtomicAddProperty(
+ pset, plane->id(), plane->src_w_property().id(),
+ (int)(source_crop.right - source_crop.left) << 16) < 0;
+ ret |= drmModeAtomicAddProperty(
+ pset, plane->id(), plane->src_h_property().id(),
+ (int)(source_crop.bottom - source_crop.top) << 16) < 0;
if (ret) {
ALOGE("Failed to add plane %d to set", plane->id());
break;
}
if (plane->rotation_property().id()) {
- ret = drmModePropertySetAdd(pset, plane->id(),
- plane->rotation_property().id(), rotation);
+ ret = drmModeAtomicAddProperty(pset, plane->id(),
+ plane->rotation_property().id(),
+ rotation) < 0;
if (ret) {
ALOGE("Failed to add rotation property %d to plane %d",
plane->rotation_property().id(), plane->id());
}
if (plane->alpha_property().id()) {
- ret = drmModePropertySetAdd(pset, plane->id(),
- plane->alpha_property().id(), alpha);
+ ret = drmModeAtomicAddProperty(pset, plane->id(),
+ plane->alpha_property().id(),
+ alpha) < 0;
if (ret) {
ALOGE("Failed to add alpha property %d to plane %d",
plane->alpha_property().id(), plane->id());
out:
if (!ret) {
- ret = drmModePropertySetCommit(drm_->fd(), DRM_MODE_ATOMIC_ALLOW_MODESET,
- drm_, pset);
+ uint32_t flags = DRM_MODE_ATOMIC_ALLOW_MODESET;
+ if (test_only)
+ flags |= DRM_MODE_ATOMIC_TEST_ONLY;
+
+ ret = drmModeAtomicCommit(drm_->fd(), pset, flags, drm_);
if (ret) {
- ALOGE("Failed to commit pset ret=%d\n", ret);
- drmModePropertySetFree(pset);
- if (needs_modeset_)
- drm_->DestroyPropertyBlob(blob_id);
+ if (test_only)
+ ALOGI("Commit test pset failed ret=%d\n", ret);
+ else
+ ALOGE("Failed to commit pset ret=%d\n", ret);
+ drmModeAtomicFree(pset);
return ret;
}
}
if (pset)
- drmModePropertySetFree(pset);
+ drmModeAtomicFree(pset);
- if (needs_modeset_) {
- ret = drm_->DestroyPropertyBlob(old_blob_id);
+ if (!test_only && mode_.needs_modeset) {
+ ret = drm_->DestroyPropertyBlob(mode_.old_blob_id);
if (ret) {
- ALOGE("Failed to destroy old mode property blob %lld/%d", old_blob_id,
- ret);
+ ALOGE("Failed to destroy old mode property blob %lld/%d",
+ mode_.old_blob_id, ret);
return ret;
}
return ret;
}
- connector->set_active_mode(next_mode_);
- needs_modeset_ = false;
+ connector->set_active_mode(mode_.mode);
+ mode_.old_blob_id = mode_.blob_id;
+ mode_.blob_id = 0;
+ mode_.needs_modeset = false;
}
return ret;
return 0;
}
+std::tuple<int, uint32_t> DrmDisplayCompositor::CreateModeBlob(
+ const DrmMode &mode) {
+ struct drm_mode_modeinfo drm_mode;
+ memset(&drm_mode, 0, sizeof(drm_mode));
+ mode.ToDrmModeModeInfo(&drm_mode);
+
+ uint32_t id = 0;
+ int ret = drm_->CreatePropertyBlob(&drm_mode,
+ sizeof(struct drm_mode_modeinfo), &id);
+ if (ret) {
+ ALOGE("Failed to create mode property blob %d", ret);
+ return std::make_tuple(ret, 0);
+ }
+ ALOGE("Create blob_id %ld\n", id);
+ return std::make_tuple(ret, id);
+}
+
void DrmDisplayCompositor::ApplyFrame(
std::unique_ptr<DrmDisplayComposition> composition, int status) {
int ret = status;
if (!ret)
- ret = CommitFrame(composition.get());
+ ret = CommitFrame(composition.get(), false);
if (ret) {
ALOGE("Composite failed for display %d", display_);
switch (composition->type()) {
case DRM_COMPOSITION_TYPE_FRAME:
ret = PrepareFrame(composition.get());
+ if (ret) {
+ ALOGE("Failed to prepare frame for display %d", display_);
+ return ret;
+ }
+ if (composition->geometry_changed()) {
+ // Send the composition to the kernel to ensure we can commit it. This
+ // is just a test, it won't actually commit the frame. If rejected,
+ // squash the frame into one layer and use the squashed composition
+ ret = CommitFrame(composition.get(), true);
+ if (ret)
+ ALOGI("Commit test failed, squashing frame for display %d", display_);
+ use_hw_overlays_ = !ret;
+ }
+
+ // If use_hw_overlays_ is false, we can't use hardware to composite the
+ // frame. So squash all layers into a single composition and queue that
+ // instead.
+ if (!use_hw_overlays_) {
+ std::unique_ptr<DrmDisplayComposition> squashed = CreateComposition();
+ ret = SquashFrame(composition.get(), squashed.get());
+ if (!ret) {
+ composition = std::move(squashed);
+ } else {
+ ALOGE("Failed to squash frame for display %d", display_);
+ return ret;
+ }
+ }
frame_worker_.QueueFrame(std::move(composition), ret);
break;
case DRM_COMPOSITION_TYPE_DPMS:
ALOGE("Failed to apply dpms for display %d", display_);
return ret;
case DRM_COMPOSITION_TYPE_MODESET:
- next_mode_ = composition->display_mode();
- needs_modeset_ = true;
+ mode_.mode = composition->display_mode();
+ if (mode_.blob_id)
+ drm_->DestroyPropertyBlob(mode_.blob_id);
+ std::tie(ret, mode_.blob_id) = CreateModeBlob(mode_.mode);
+ if (ret) {
+ ALOGE("Failed to create mode blob for display %d", display_);
+ return ret;
+ }
+ mode_.needs_modeset = true;
return 0;
default:
ALOGE("Unknown composition type %d", composition->type());
if (!active_composition_)
return 0;
- if (active_composition_->type() != DRM_COMPOSITION_TYPE_FRAME)
- return 0;
+ std::unique_ptr<DrmDisplayComposition> comp = CreateComposition();
+ ret = SquashFrame(active_composition_.get(), comp.get());
- DrmDisplayComposition &active_comp = *active_composition_;
- std::vector<DrmCompositionPlane> &active_planes =
- active_comp.composition_planes();
- std::vector<DrmHwcLayer> &active_layers = active_comp.layers();
+ // ApplyFrame needs the lock
+ lock.Unlock();
+
+ if (!ret)
+ ApplyFrame(std::move(comp), 0);
+
+ return ret;
+}
+
+// Returns:
+// - 0 if src is successfully squashed into dst
+// - -EALREADY if the src is already squashed
+// - Appropriate error if the squash fails
+int DrmDisplayCompositor::SquashFrame(DrmDisplayComposition *src,
+ DrmDisplayComposition *dst) {
+ if (src->type() != DRM_COMPOSITION_TYPE_FRAME)
+ return -ENOTSUP;
+
+ std::vector<DrmCompositionPlane> &src_planes = src->composition_planes();
+ std::vector<DrmHwcLayer> &src_layers = src->layers();
// Make sure there is more than one layer to squash.
- size_t active_planes_with_layer = std::count_if(
- active_planes.begin(), active_planes.end(), [](DrmCompositionPlane &p) {
+ size_t src_planes_with_layer = std::count_if(
+ src_planes.begin(), src_planes.end(), [](DrmCompositionPlane &p) {
return p.source_layer <= DrmCompositionPlane::kSourceLayerMax;
});
- if (active_planes_with_layer <= 1)
- return 0;
+ if (src_planes_with_layer <= 1)
+ return -EALREADY;
int pre_comp_layer_index;
- std::unique_ptr<DrmDisplayComposition> comp = CreateComposition();
- ret = comp->Init(drm_, active_comp.crtc(), active_comp.importer(),
- active_comp.frame_no());
+ int ret = dst->Init(drm_, src->crtc(), src->importer(), src->frame_no());
if (ret) {
ALOGE("Failed to init squash all composition %d", ret);
return ret;
std::vector<DrmPlane *> primary_planes;
std::vector<DrmPlane *> fake_overlay_planes;
- std::vector<DrmHwcLayer> comp_layers;
- for (DrmCompositionPlane &comp_plane : active_planes) {
+ std::vector<DrmHwcLayer> dst_layers;
+ for (DrmCompositionPlane &comp_plane : src_planes) {
// Composition planes without DRM planes should never happen
if (comp_plane.plane == NULL) {
ALOGE("Skipping squash all because of NULL plane");
+ ret = -EINVAL;
goto move_layers_back;
}
// Out of range layers should never happen. If they do, somebody probably
// forgot to replace the symbolic names (kSourceSquash, kSourcePreComp) with
// real ones.
- if (comp_plane.source_layer >= active_layers.size()) {
+ if (comp_plane.source_layer >= src_layers.size()) {
ALOGE("Skipping squash all because of out of range source layer %zu",
comp_plane.source_layer);
+ ret = -EINVAL;
goto move_layers_back;
}
- DrmHwcLayer &layer = active_layers[comp_plane.source_layer];
+ DrmHwcLayer &layer = src_layers[comp_plane.source_layer];
// Squashing protected layers is impossible.
- if (layer.protected_usage())
+ if (layer.protected_usage()) {
+ ret = -ENOTSUP;
goto move_layers_back;
+ }
// The OutputFds point to freed memory after hwc_set returns. They are
// returned to the default to prevent DrmDisplayComposition::Plan from
// filling the OutputFds.
layer.release_fence = OutputFd();
- comp_layers.emplace_back(std::move(layer));
+ dst_layers.emplace_back(std::move(layer));
if (comp_plane.plane->type() == DRM_PLANE_TYPE_PRIMARY &&
primary_planes.size() == 0)
primary_planes.push_back(comp_plane.plane);
else
- comp->AddPlaneDisable(comp_plane.plane);
+ dst->AddPlaneDisable(comp_plane.plane);
}
- ret = comp->SetLayers(comp_layers.data(), comp_layers.size(), false);
+ ret = dst->SetLayers(dst_layers.data(), dst_layers.size(), false);
if (ret) {
ALOGE("Failed to set layers for squash all composition %d", ret);
goto move_layers_back;
}
ret =
- comp->Plan(NULL /* SquashState */, &primary_planes, &fake_overlay_planes);
+ dst->Plan(NULL /* SquashState */, &primary_planes, &fake_overlay_planes);
if (ret) {
ALOGE("Failed to plan for squash all composition %d", ret);
goto move_layers_back;
}
- ret = ApplyPreComposite(comp.get());
+ ret = ApplyPreComposite(dst);
if (ret) {
ALOGE("Failed to pre-composite for squash all composition %d", ret);
goto move_layers_back;
}
- pre_comp_layer_index = comp->layers().size() - 1;
+ pre_comp_layer_index = dst->layers().size() - 1;
framebuffer_index_ = (framebuffer_index_ + 1) % DRM_DISPLAY_BUFFERS;
- for (DrmCompositionPlane &plane : comp->composition_planes())
+ for (DrmCompositionPlane &plane : dst->composition_planes())
if (plane.source_layer == DrmCompositionPlane::kSourcePreComp)
plane.source_layer = pre_comp_layer_index;
- // ApplyFrame needs the lock
- lock.Unlock();
-
- ApplyFrame(std::move(comp), 0);
-
return 0;
// TODO(zachr): think of a better way to transfer ownership back to the active
// composition.
move_layers_back:
for (size_t plane_index = 0;
- plane_index < active_planes.size() && plane_index < comp_layers.size();
+ plane_index < src_planes.size() && plane_index < dst_layers.size();
plane_index++) {
- size_t source_layer_index = active_planes[plane_index].source_layer;
- active_layers[source_layer_index] = std::move(comp_layers[plane_index]);
+ size_t source_layer_index = src_planes[plane_index].source_layer;
+ src_layers[source_layer_index] = std::move(dst_layers[plane_index]);
}
return ret;