Skip to content

Commit

Permalink
Fix high DPI with Surfaces by treating Surface size as content bounds…
Browse files Browse the repository at this point in the history
… of layer

The size of the surface can be different from the bounds of the layer, so set the contents scale to convert between the two.

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

Cr-Commit-Position: refs/heads/master@{#303157}
  • Loading branch information
jbauman2 authored and Commit bot committed Nov 7, 2014
1 parent 9ea5f76 commit 5a8f0ab
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 8 deletions.
16 changes: 15 additions & 1 deletion cc/layers/surface_layer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,10 @@ SurfaceLayer::~SurfaceLayer() {
DCHECK(destroy_sequence_.is_null());
}

void SurfaceLayer::SetSurfaceId(SurfaceId surface_id) {
void SurfaceLayer::SetSurfaceId(SurfaceId surface_id, const gfx::Size& size) {
SatisfyDestroySequence();
surface_id_ = surface_id;
surface_size_ = size;
CreateNewDestroySequence();

UpdateDrawsContent(HasDrawableContent());
Expand Down Expand Up @@ -88,6 +89,19 @@ void SurfaceLayer::PushPropertiesTo(LayerImpl* layer) {
layer_impl->SetSurfaceId(surface_id_);
}

void SurfaceLayer::CalculateContentsScale(float ideal_contents_scale,
float* contents_scale_x,
float* contents_scale_y,
gfx::Size* content_bounds) {
*content_bounds = surface_size_;
*contents_scale_x =
bounds().IsEmpty() ? 1.f : static_cast<float>(content_bounds->width()) /
bounds().width();
*contents_scale_y =
bounds().IsEmpty() ? 1.f : static_cast<float>(content_bounds->height()) /
bounds().height();
}

void SurfaceLayer::CreateNewDestroySequence() {
DCHECK(destroy_sequence_.is_null());
if (layer_tree_host()) {
Expand Down
8 changes: 7 additions & 1 deletion cc/layers/surface_layer.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "cc/layers/layer.h"
#include "cc/surfaces/surface_id.h"
#include "cc/surfaces/surface_sequence.h"
#include "ui/gfx/size.h"

namespace cc {

Expand All @@ -29,12 +30,16 @@ class CC_EXPORT SurfaceLayer : public Layer {
const SatisfyCallback& satisfy_callback,
const RequireCallback& require_callback);

void SetSurfaceId(SurfaceId surface_id);
void SetSurfaceId(SurfaceId surface_id, const gfx::Size& size);

// Layer overrides.
scoped_ptr<LayerImpl> CreateLayerImpl(LayerTreeImpl* tree_impl) override;
void SetLayerTreeHost(LayerTreeHost* host) override;
void PushPropertiesTo(LayerImpl* layer) override;
void CalculateContentsScale(float ideal_contents_scale,
float* contents_scale_x,
float* contents_scale_y,
gfx::Size* content_bounds) override;

protected:
SurfaceLayer(const SatisfyCallback& satisfy_callback,
Expand All @@ -47,6 +52,7 @@ class CC_EXPORT SurfaceLayer : public Layer {
void SatisfyDestroySequence();

SurfaceId surface_id_;
gfx::Size surface_size_;
SurfaceSequence destroy_sequence_;
SatisfyCallback satisfy_callback_;
RequireCallback require_callback_;
Expand Down
33 changes: 30 additions & 3 deletions cc/layers/surface_layer_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ TEST_F(SurfaceLayerTest, MultipleFramesOneSurface) {
scoped_refptr<SurfaceLayer> layer(SurfaceLayer::Create(
base::Bind(&SatisfyCallback, &blank_change),
base::Bind(&RequireCallback, &required_id, &required_seq)));
layer->SetSurfaceId(SurfaceId(1));
layer->SetSurfaceId(SurfaceId(1), gfx::Size(1, 1));
layer_tree_host_->set_surface_id_namespace(1);
layer_tree_host_->SetRootLayer(layer);

Expand All @@ -74,7 +74,7 @@ TEST_F(SurfaceLayerTest, MultipleFramesOneSurface) {
scoped_refptr<SurfaceLayer> layer2(SurfaceLayer::Create(
base::Bind(&SatisfyCallback, &blank_change),
base::Bind(&RequireCallback, &required_id, &required_seq)));
layer2->SetSurfaceId(SurfaceId(1));
layer2->SetSurfaceId(SurfaceId(1), gfx::Size(1, 1));
layer_tree_host2->set_surface_id_namespace(2);
layer_tree_host2->SetRootLayer(layer2);

Expand Down Expand Up @@ -110,6 +110,33 @@ TEST_F(SurfaceLayerTest, MultipleFramesOneSurface) {
EXPECT_EQ(2u, required_seq.size());
}

// Check that setting content scale on the surface works.
TEST_F(SurfaceLayerTest, ScaleSurface) {
SurfaceSequence blank_change;
SurfaceId required_id;
std::set<SurfaceSequence> required_seq;
scoped_refptr<SurfaceLayer> layer(SurfaceLayer::Create(
base::Bind(&SatisfyCallback, &blank_change),
base::Bind(&RequireCallback, &required_id, &required_seq)));
gfx::Size surface_size(10, 15);
layer->SetSurfaceId(SurfaceId(1), surface_size);
layer->SetBounds(gfx::Size(25, 45));

float scale_x;
float scale_y;
gfx::Size bounds;
layer->CalculateContentsScale(2.f, &scale_x, &scale_y, &bounds);
EXPECT_EQ(10.f / 25.f, scale_x);
EXPECT_EQ(15.f / 45.f, scale_y);
EXPECT_EQ(surface_size.ToString(), bounds.ToString());

layer->SetBounds(gfx::Size(0, 0));
layer->CalculateContentsScale(2.f, &scale_x, &scale_y, &bounds);
EXPECT_EQ(1.f, scale_x);
EXPECT_EQ(1.f, scale_y);
EXPECT_EQ(surface_size.ToString(), bounds.ToString());
}

// Check that SurfaceSequence is sent through swap promise.
class SurfaceLayerSwapPromise : public LayerTreeTest {
public:
Expand All @@ -121,7 +148,7 @@ class SurfaceLayerSwapPromise : public LayerTreeTest {
layer_ = SurfaceLayer::Create(
base::Bind(&SatisfyCallback, &satisfied_sequence_),
base::Bind(&RequireCallback, &required_id_, &required_set_));
layer_->SetSurfaceId(SurfaceId(1));
layer_->SetSurfaceId(SurfaceId(1), gfx::Size(1, 1));

// Layer hasn't been added to tree so no SurfaceSequence generated yet.
EXPECT_EQ(0u, required_set_.size());
Expand Down
4 changes: 2 additions & 2 deletions content/browser/compositor/delegated_frame_host.cc
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ void DelegatedFrameHost::SwapDelegatedFrame(
client_->GetLayer()->SetShowSurface(
surface_id_,
base::Bind(&SatisfyCallback, base::Unretained(manager)),
base::Bind(&RequireCallback, base::Unretained(manager)),
base::Bind(&RequireCallback, base::Unretained(manager)), frame_size,
frame_size_in_dip);
current_surface_size_ = frame_size;
}
Expand Down Expand Up @@ -1021,7 +1021,7 @@ void DelegatedFrameHost::OnLayerRecreated(ui::Layer* old_layer,
new_layer->SetShowSurface(
surface_id_, base::Bind(&SatisfyCallback, base::Unretained(manager)),
base::Bind(&RequireCallback, base::Unretained(manager)),
current_frame_size_in_dip_);
current_surface_size_, current_frame_size_in_dip_);
}
}

Expand Down
3 changes: 2 additions & 1 deletion ui/compositor/layer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -573,12 +573,13 @@ void Layer::SetShowSurface(
cc::SurfaceId surface_id,
const cc::SurfaceLayer::SatisfyCallback& satisfy_callback,
const cc::SurfaceLayer::RequireCallback& require_callback,
gfx::Size surface_size,
gfx::Size frame_size_in_dip) {
DCHECK(type_ == LAYER_TEXTURED || type_ == LAYER_SOLID_COLOR);

scoped_refptr<cc::SurfaceLayer> new_layer =
cc::SurfaceLayer::Create(satisfy_callback, require_callback);
new_layer->SetSurfaceId(surface_id);
new_layer->SetSurfaceId(surface_id, surface_size);
SwitchToLayer(new_layer);
surface_layer_ = new_layer;

Expand Down
1 change: 1 addition & 0 deletions ui/compositor/layer.h
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ class COMPOSITOR_EXPORT Layer
void SetShowSurface(cc::SurfaceId surface_id,
const cc::SurfaceLayer::SatisfyCallback& satisfy_callback,
const cc::SurfaceLayer::RequireCallback& require_callback,
gfx::Size surface_size,
gfx::Size frame_size_in_dip);

bool has_external_content() {
Expand Down

0 comments on commit 5a8f0ab

Please sign in to comment.