Commit 8aa00fc2 authored by ccameron's avatar ccameron Committed by Commit bot

Mac overlays: Isolate partial swap support

The logic to maintain a CALayer tree for partial swap support is
complicated and should be removed, but that would compromise battery
usage. Once the CoreAniamtion renderer is handling ~99% of the frames,
we can remove it. In the mean time, isolate it.

Create a CALayerPartialDamageTree to track the layer tree of partially
swapped backbuffers. Do this in-place to minimize diffs, so that this
patch can be reasonably reviewed. A follow-on patch will move it to
a separate file.

Slightly reorganize the logic so that CALayerPartialDamageTree matches
the general structure of CALayerTree, where this a pending (not swapped)
tree, a tree for each pending swap, and a tree for what is currently
being displayed.

BUG=533681

Review URL: https://codereview.chromium.org/1640093002

Cr-Commit-Position: refs/heads/master@{#371831}
parent 95e79fa8
......@@ -22,6 +22,7 @@
namespace content {
class CALayerTree;
class CALayerPartialDamageTree;
class ImageTransportSurfaceOverlayMac : public gfx::GLSurface,
public ImageTransportSurface,
......@@ -78,13 +79,6 @@ class ImageTransportSurfaceOverlayMac : public gfx::GLSurface,
gfx::SwapResult SwapBuffersInternal(const gfx::Rect& pixel_damage_rect);
void UpdateRootAndPartialDamagePlanes(
const linked_ptr<OverlayPlane>& new_root_plane,
const gfx::RectF& pixel_damage_rect);
void UpdateRootAndPartialDamageCALayers(float scale_factor);
void UpdateCALayerTree(scoped_ptr<CALayerTree> ca_layer_tree,
float scale_factor);
// Returns true if the front of |pending_swaps_| has completed, or has timed
// out by |now|.
bool IsFirstPendingSwapReadyToDisplay(
......@@ -125,7 +119,7 @@ class ImageTransportSurfaceOverlayMac : public gfx::GLSurface,
// Planes that have been scheduled, but have not had a subsequent SwapBuffers
// call made yet.
linked_ptr<OverlayPlane> pending_root_plane_;
scoped_ptr<CALayerPartialDamageTree> pending_partial_damage_tree_;
scoped_ptr<CALayerTree> pending_ca_layer_tree_;
// A queue of all frames that have been created by SwapBuffersInternal but
......@@ -134,8 +128,7 @@ class ImageTransportSurfaceOverlayMac : public gfx::GLSurface,
std::deque<linked_ptr<PendingSwap>> pending_swaps_;
// The planes that are currently being displayed on the screen.
linked_ptr<OverlayPlane> current_root_plane_;
std::list<linked_ptr<OverlayPlane>> current_partial_damage_planes_;
scoped_ptr<CALayerPartialDamageTree> current_partial_damage_tree_;
scoped_ptr<CALayerTree> current_ca_layer_tree_;
// The time of the last swap was issued. If this is more than two vsyncs, then
......@@ -147,10 +140,6 @@ class ImageTransportSurfaceOverlayMac : public gfx::GLSurface,
base::TimeTicks vsync_timebase_;
base::TimeDelta vsync_interval_;
// Calls to ScheduleCALayer come in back-to-front. This is reset to 1 at each
// swap and increments with each call to ScheduleCALayer.
int next_ca_layer_z_order_;
base::Timer display_pending_swap_timer_;
base::WeakPtrFactory<ImageTransportSurfaceOverlayMac> weak_factory_;
};
......
......@@ -94,7 +94,33 @@ scoped_refptr<gfx::GLSurface> ImageTransportSurfaceCreateNativeSurface(
return new ImageTransportSurfaceOverlayMac(manager, stub, handle);
}
class ImageTransportSurfaceOverlayMac::OverlayPlane {
class CALayerPartialDamageTree {
public:
CALayerPartialDamageTree(bool allow_partial_swap,
base::ScopedCFTypeRef<IOSurfaceRef> io_surface,
const gfx::Rect& pixel_frame_rect);
~CALayerPartialDamageTree();
base::ScopedCFTypeRef<IOSurfaceRef> RootLayerIOSurface();
void CommitCALayers(CALayer* superlayer,
scoped_ptr<CALayerPartialDamageTree> old_tree,
float scale_factor,
const gfx::Rect& pixel_damage_rect);
private:
class OverlayPlane;
void UpdateRootAndPartialDamagePlanes(CALayerPartialDamageTree* old_tree,
const gfx::RectF& pixel_damage_rect);
void UpdateRootAndPartialDamageCALayers(CALayer* superlayer,
float scale_factor);
const bool allow_partial_swap_;
linked_ptr<OverlayPlane> root_plane_;
std::list<linked_ptr<OverlayPlane>> partial_damage_planes_;
};
class CALayerPartialDamageTree::OverlayPlane {
public:
static linked_ptr<OverlayPlane> CreateWithFrameRect(
int z_order,
......@@ -185,7 +211,7 @@ class ImageTransportSurfaceOverlayMac::PendingSwap {
float scale_factor;
gfx::Rect pixel_damage_rect;
linked_ptr<OverlayPlane> root_plane;
scoped_ptr<CALayerPartialDamageTree> partial_damage_tree;
scoped_ptr<CALayerTree> ca_layer_tree;
std::vector<ui::LatencyInfo> latency_info;
......@@ -211,7 +237,6 @@ ImageTransportSurfaceOverlayMac::ImageTransportSurfaceOverlayMac(
scale_factor_(1),
gl_renderer_id_(0),
vsync_parameters_valid_(false),
next_ca_layer_z_order_(1),
display_pending_swap_timer_(true, false),
weak_factory_(this) {
helper_.reset(new ImageTransportHelper(this, manager, stub, handle));
......@@ -244,8 +269,9 @@ bool ImageTransportSurfaceOverlayMac::Initialize(
void ImageTransportSurfaceOverlayMac::Destroy() {
DisplayAndClearAllPendingSwaps();
current_partial_damage_planes_.clear();
current_root_plane_.reset();
current_partial_damage_tree_.reset();
current_ca_layer_tree_.reset();
}
bool ImageTransportSurfaceOverlayMac::IsOffscreen() {
......@@ -255,7 +281,6 @@ bool ImageTransportSurfaceOverlayMac::IsOffscreen() {
gfx::SwapResult ImageTransportSurfaceOverlayMac::SwapBuffersInternal(
const gfx::Rect& pixel_damage_rect) {
TRACE_EVENT0("gpu", "ImageTransportSurfaceOverlayMac::SwapBuffersInternal");
next_ca_layer_z_order_ = 1;
// Use the same concept of 'now' for the entire function. The duration of
// this function only affect the result if this function lasts across a vsync
......@@ -286,8 +311,7 @@ gfx::SwapResult ImageTransportSurfaceOverlayMac::SwapBuffersInternal(
new_swap->pixel_size = pixel_size_;
new_swap->scale_factor = scale_factor_;
new_swap->pixel_damage_rect = pixel_damage_rect;
new_swap->root_plane = pending_root_plane_;
pending_root_plane_ = linked_ptr<OverlayPlane>();
new_swap->partial_damage_tree.swap(pending_partial_damage_tree_);
new_swap->ca_layer_tree.swap(pending_ca_layer_tree_);
new_swap->latency_info.swap(latency_info_);
......@@ -367,15 +391,27 @@ void ImageTransportSurfaceOverlayMac::DisplayFirstPendingSwapImmediately() {
CheckGLErrors("while deleting active fence");
}
// Update the plane lists.
// Update the CALayer hierarchy.
{
// Sort the input planes by z-index, and remove any overlays from the
// damage rect.
gfx::RectF pixel_damage_rect = gfx::RectF(swap->pixel_damage_rect);
ScopedCAActionDisabler disabler;
UpdateRootAndPartialDamagePlanes(swap->root_plane, pixel_damage_rect);
UpdateRootAndPartialDamageCALayers(swap->scale_factor);
UpdateCALayerTree(std::move(swap->ca_layer_tree), swap->scale_factor);
if (swap->ca_layer_tree) {
swap->ca_layer_tree->CommitScheduledCALayers(
ca_root_layer_.get(), std::move(current_ca_layer_tree_),
swap->scale_factor);
current_ca_layer_tree_.swap(swap->ca_layer_tree);
current_partial_damage_tree_.reset();
} else if (swap->partial_damage_tree) {
swap->partial_damage_tree->CommitCALayers(
ca_root_layer_.get(), std::move(current_partial_damage_tree_),
swap->scale_factor, swap->pixel_damage_rect);
current_partial_damage_tree_.swap(swap->partial_damage_tree);
current_ca_layer_tree_.reset();
} else {
[ca_root_layer_ setSublayers:nil];
}
swap->ca_layer_tree.reset();
swap->partial_damage_tree.reset();
}
// Update the latency info to reflect the swap time.
......@@ -392,9 +428,9 @@ void ImageTransportSurfaceOverlayMac::DisplayFirstPendingSwapImmediately() {
GpuHostMsg_AcceleratedSurfaceBuffersSwapped_Params params;
if (use_remote_layer_api_) {
params.ca_context_id = [ca_context_ contextId];
} else if (current_root_plane_.get()) {
params.io_surface.reset(
IOSurfaceCreateMachPort(current_root_plane_->io_surface));
} else if (current_partial_damage_tree_) {
params.io_surface.reset(IOSurfaceCreateMachPort(
current_partial_damage_tree_->RootLayerIOSurface()));
}
params.size = swap->pixel_size;
params.scale_factor = swap->scale_factor;
......@@ -405,32 +441,25 @@ void ImageTransportSurfaceOverlayMac::DisplayFirstPendingSwapImmediately() {
pending_swaps_.pop_front();
}
void ImageTransportSurfaceOverlayMac::UpdateRootAndPartialDamagePlanes(
const linked_ptr<OverlayPlane>& new_root_plane,
void CALayerPartialDamageTree::UpdateRootAndPartialDamagePlanes(
CALayerPartialDamageTree* old_tree,
const gfx::RectF& pixel_damage_rect) {
std::list<linked_ptr<OverlayPlane>> old_partial_damage_planes;
old_partial_damage_planes.swap(current_partial_damage_planes_);
// This is the plane that will be updated this frame. It may be the root plane
// or a child plane.
linked_ptr<OverlayPlane> plane_for_swap;
// If there is no new root plane, remove everything.
if (!new_root_plane.get()) {
old_partial_damage_planes.clear();
current_root_plane_.reset();
return;
}
// If the frame's size changed, if we haven't updated the root layer, if
// we have full damage, or if we don't support remote layers, then use the
// root layer directly.
if (!use_remote_layer_api_ || !current_root_plane_.get() ||
current_root_plane_->pixel_frame_rect !=
new_root_plane->pixel_frame_rect ||
pixel_damage_rect == new_root_plane->pixel_frame_rect) {
plane_for_swap = new_root_plane;
if (!allow_partial_swap_ || !old_tree ||
old_tree->root_plane_->pixel_frame_rect !=
root_plane_->pixel_frame_rect ||
pixel_damage_rect == root_plane_->pixel_frame_rect) {
plane_for_swap = root_plane_;
}
// Walk though the existing partial damage layers and see if there is one that
// is appropriate to re-use.
// Walk though the old tree's partial damage layers and see if there is one
// that is appropriate to re-use.
if (!plane_for_swap.get() && !pixel_damage_rect.IsEmpty()) {
gfx::RectF plane_to_reuse_dip_enlarged_rect;
......@@ -438,7 +467,7 @@ void ImageTransportSurfaceOverlayMac::UpdateRootAndPartialDamagePlanes(
// new rect for this layer to include this damage, and all nearby partial
// damage layers.
linked_ptr<OverlayPlane> plane_to_reuse;
for (auto& old_plane : old_partial_damage_planes) {
for (auto& old_plane : old_tree->partial_damage_planes_) {
gfx::RectF dip_enlarged_rect = old_plane->pixel_frame_rect;
dip_enlarged_rect.Union(pixel_damage_rect);
......@@ -455,16 +484,15 @@ void ImageTransportSurfaceOverlayMac::UpdateRootAndPartialDamagePlanes(
if (plane_to_reuse.get()) {
gfx::RectF enlarged_contents_rect = plane_to_reuse_dip_enlarged_rect;
enlarged_contents_rect.Scale(
1. / new_root_plane->pixel_frame_rect.width(),
1. / new_root_plane->pixel_frame_rect.height());
enlarged_contents_rect.Scale(1. / root_plane_->pixel_frame_rect.width(),
1. / root_plane_->pixel_frame_rect.height());
plane_for_swap = OverlayPlane::CreateWithFrameRect(
0, new_root_plane->io_surface, plane_to_reuse_dip_enlarged_rect,
0, root_plane_->io_surface, plane_to_reuse_dip_enlarged_rect,
enlarged_contents_rect);
plane_for_swap->TakeCALayerFrom(plane_to_reuse.get());
if (plane_to_reuse != old_partial_damage_planes.back())
if (plane_to_reuse != old_tree->partial_damage_planes_.back())
[plane_for_swap->ca_layer removeFromSuperlayer];
}
}
......@@ -472,26 +500,26 @@ void ImageTransportSurfaceOverlayMac::UpdateRootAndPartialDamagePlanes(
// If we haven't found an appropriate layer to re-use, create a new one, if
// we haven't already created too many.
if (!plane_for_swap.get() && !pixel_damage_rect.IsEmpty() &&
old_partial_damage_planes.size() < kMaximumPartialDamageLayers) {
old_tree->partial_damage_planes_.size() < kMaximumPartialDamageLayers) {
gfx::RectF contents_rect = gfx::RectF(pixel_damage_rect);
contents_rect.Scale(1. / new_root_plane->pixel_frame_rect.width(),
1. / new_root_plane->pixel_frame_rect.height());
contents_rect.Scale(1. / root_plane_->pixel_frame_rect.width(),
1. / root_plane_->pixel_frame_rect.height());
plane_for_swap = OverlayPlane::CreateWithFrameRect(
0, new_root_plane->io_surface, pixel_damage_rect, contents_rect);
0, root_plane_->io_surface, pixel_damage_rect, contents_rect);
}
// And if we still don't have a layer, use the root layer.
if (!plane_for_swap.get() && !pixel_damage_rect.IsEmpty())
plane_for_swap = new_root_plane;
plane_for_swap = root_plane_;
// Walk all old partial damage planes. Remove anything that is now completely
// covered, and move everything else into the new
// |current_partial_damage_planes_|.
for (auto& old_plane : old_partial_damage_planes) {
// covered, and move everything else into the new |partial_damage_planes_|.
if (old_tree) {
for (auto& old_plane : old_tree->partial_damage_planes_) {
// Intersect the planes' frames with the new root plane to ensure that
// they don't get kept alive inappropriately.
gfx::RectF old_plane_frame_rect = old_plane->pixel_frame_rect;
old_plane_frame_rect.Intersect(new_root_plane->pixel_frame_rect);
old_plane_frame_rect.Intersect(root_plane_->pixel_frame_rect);
bool old_plane_covered_by_swap = false;
if (plane_for_swap.get() &&
......@@ -500,62 +528,48 @@ void ImageTransportSurfaceOverlayMac::UpdateRootAndPartialDamagePlanes(
}
if (!old_plane_covered_by_swap) {
DCHECK(old_plane->ca_layer);
current_partial_damage_planes_.push_back(old_plane);
partial_damage_planes_.push_back(old_plane);
}
}
if (plane_for_swap != root_plane_)
root_plane_ = old_tree->root_plane_;
}
// Finally, add the new swap's plane at the back of the list, if it exists.
if (plane_for_swap == new_root_plane) {
current_root_plane_ = new_root_plane;
} else if (plane_for_swap.get()) {
current_partial_damage_planes_.push_back(plane_for_swap);
if (plane_for_swap.get() && plane_for_swap != root_plane_) {
partial_damage_planes_.push_back(plane_for_swap);
}
}
void ImageTransportSurfaceOverlayMac::UpdateRootAndPartialDamageCALayers(
void CALayerPartialDamageTree::UpdateRootAndPartialDamageCALayers(
CALayer* superlayer,
float scale_factor) {
if (!use_remote_layer_api_) {
DCHECK(current_partial_damage_planes_.empty());
if (!allow_partial_swap_) {
DCHECK(partial_damage_planes_.empty());
return;
}
// Allocate and update CALayers for the backbuffer and partial damage layers.
if (current_root_plane_.get()) {
if (!current_root_plane_->ca_layer) {
current_root_plane_->ca_layer.reset([[CALayer alloc] init]);
[ca_root_layer_ setSublayers:nil];
[ca_root_layer_ addSublayer:current_root_plane_->ca_layer];
if (!root_plane_->ca_layer) {
root_plane_->ca_layer.reset([[CALayer alloc] init]);
[superlayer setSublayers:nil];
[superlayer addSublayer:root_plane_->ca_layer];
}
}
for (auto& plane : current_partial_damage_planes_) {
for (auto& plane : partial_damage_planes_) {
if (!plane->ca_layer) {
DCHECK(plane == current_partial_damage_planes_.back());
DCHECK(plane == partial_damage_planes_.back());
plane->ca_layer.reset([[CALayer alloc] init]);
}
if (![plane->ca_layer superlayer]) {
DCHECK(plane == current_partial_damage_planes_.back());
[ca_root_layer_ addSublayer:plane->ca_layer];
DCHECK(plane == partial_damage_planes_.back());
[superlayer addSublayer:plane->ca_layer];
}
}
if (current_root_plane_.get())
current_root_plane_->UpdateProperties(scale_factor);
for (auto& plane : current_partial_damage_planes_)
root_plane_->UpdateProperties(scale_factor);
for (auto& plane : partial_damage_planes_)
plane->UpdateProperties(scale_factor);
}
void ImageTransportSurfaceOverlayMac::UpdateCALayerTree(
scoped_ptr<CALayerTree> ca_layer_tree,
float scale_factor) {
if (ca_layer_tree) {
ca_layer_tree->CommitScheduledCALayers(
ca_root_layer_.get(), std::move(current_ca_layer_tree_), scale_factor);
current_ca_layer_tree_.swap(ca_layer_tree);
ca_layer_tree.reset();
} else {
current_ca_layer_tree_.reset();
}
}
void ImageTransportSurfaceOverlayMac::DisplayAndClearAllPendingSwaps() {
TRACE_EVENT0("gpu",
"ImageTransportSurfaceOverlayMac::DisplayAndClearAllPendingSwaps");
......@@ -646,10 +660,14 @@ bool ImageTransportSurfaceOverlayMac::ScheduleOverlayPlane(
DLOG(ERROR) << "Invalid non-zero Z order.";
return false;
}
pending_root_plane_ = OverlayPlane::CreateWithFrameRect(
z_order, static_cast<gl::GLImageIOSurface*>(image)->io_surface(),
gfx::RectF(pixel_frame_rect), crop_rect);
if (pending_partial_damage_tree_) {
DLOG(ERROR) << "Only one overlay per swap is allowed.";
return false;
}
pending_partial_damage_tree_.reset(new CALayerPartialDamageTree(
use_remote_layer_api_,
static_cast<gl::GLImageIOSurface*>(image)->io_surface(),
pixel_frame_rect));
return true;
}
......@@ -747,4 +765,30 @@ base::TimeTicks ImageTransportSurfaceOverlayMac::GetNextVSyncTimeAfter(
return previous_vsync + (1 + interval_fraction) * vsync_interval_;
}
CALayerPartialDamageTree::CALayerPartialDamageTree(
bool allow_partial_swap,
base::ScopedCFTypeRef<IOSurfaceRef> io_surface,
const gfx::Rect& pixel_frame_rect)
: allow_partial_swap_(allow_partial_swap) {
root_plane_ = OverlayPlane::CreateWithFrameRect(
0, io_surface, gfx::RectF(pixel_frame_rect), gfx::RectF(0, 0, 1, 1));
}
CALayerPartialDamageTree::~CALayerPartialDamageTree() {}
base::ScopedCFTypeRef<IOSurfaceRef>
CALayerPartialDamageTree::RootLayerIOSurface() {
return root_plane_->io_surface;
}
void CALayerPartialDamageTree::CommitCALayers(
CALayer* superlayer,
scoped_ptr<CALayerPartialDamageTree> old_tree,
float scale_factor,
const gfx::Rect& pixel_damage_rect) {
UpdateRootAndPartialDamagePlanes(old_tree.get(),
gfx::RectF(pixel_damage_rect));
UpdateRootAndPartialDamageCALayers(superlayer, scale_factor);
}
} // namespace content
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment