Commit 318e9c81 authored by danakj's avatar danakj Committed by Commit bot

Make View::Paint use ui::PaintRecorder to access PaintContext's canvas

This adds a ui::PaintRecorder that will be the gateway to the
PaintContext internals. The rules are:

1) Make a PaintRecorder when you want to access the canvas.
2) Can not nest PaintRecorders.

In the future PaintRecorder will also know how to deal with a
DisplayItemList and provider a Canvas that records to a DisplayItem
and each scoped PaintRecorder will produce DisplayItems to be cached.

For now, just restructuring code to go through PaintRecorder and not
nest recording.

R=sadrul, sky
BUG=466426

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

Cr-Commit-Position: refs/heads/master@{#323936}
parent 6c70479c
...@@ -7,6 +7,8 @@ import("//testing/test.gni") ...@@ -7,6 +7,8 @@ import("//testing/test.gni")
component("compositor") { component("compositor") {
sources = [ sources = [
"clip_transform_recorder.cc",
"clip_transform_recorder.h",
"closure_animation_observer.cc", "closure_animation_observer.cc",
"closure_animation_observer.h", "closure_animation_observer.h",
"compositor.cc", "compositor.cc",
...@@ -44,6 +46,8 @@ component("compositor") { ...@@ -44,6 +46,8 @@ component("compositor") {
"layer_tree_owner.h", "layer_tree_owner.h",
"layer_type.h", "layer_type.h",
"paint_context.h", "paint_context.h",
"paint_recorder.cc",
"paint_recorder.h",
"reflector.cc", "reflector.cc",
"reflector.h", "reflector.h",
"scoped_animation_duration_scale_mode.cc", "scoped_animation_duration_scale_mode.cc",
......
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ui/compositor/clip_transform_recorder.h"
#include "ui/compositor/paint_context.h"
#include "ui/gfx/canvas.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/transform.h"
namespace ui {
ClipTransformRecorder::ClipTransformRecorder(const PaintContext& context,
const gfx::Rect& clip_rect)
: canvas_(context.canvas()) {
canvas_->Save();
canvas_->ClipRect(clip_rect);
}
ClipTransformRecorder::ClipTransformRecorder(const PaintContext& context,
const gfx::Transform& transform)
: canvas_(context.canvas()) {
canvas_->Save();
canvas_->Transform(transform);
}
ClipTransformRecorder::ClipTransformRecorder(const PaintContext& context,
const gfx::Rect& clip_rect,
const gfx::Transform& transform)
: canvas_(context.canvas()) {
canvas_->Save();
canvas_->ClipRect(clip_rect);
canvas_->Transform(transform);
}
ClipTransformRecorder::~ClipTransformRecorder() {
canvas_->Restore();
}
} // namespace ui
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef UI_COMPOSITOR_CLIP_TRANSFORM_RECORDER_H_
#define UI_COMPOSITOR_CLIP_TRANSFORM_RECORDER_H_
#include "base/macros.h"
#include "ui/compositor/compositor_export.h"
namespace gfx {
class Canvas;
class Rect;
class Transform;
}
namespace ui {
class PaintContext;
// A class to provide scoped clips and transforms of painting to a
// DisplayItemList. The the clip/transform provided will be applied to any
// DisplayItems added to the DisplayItemList while this object is alive. In
// other words, any nested PaintRecorders or other ClipTransformRecorders will
// be clipped/transformed.
class COMPOSITOR_EXPORT ClipTransformRecorder {
public:
ClipTransformRecorder(const PaintContext& context,
const gfx::Rect& clip_rect);
ClipTransformRecorder(const PaintContext& context,
const gfx::Transform& transform);
ClipTransformRecorder(const PaintContext& context,
const gfx::Rect& clip_rect,
const gfx::Transform& transform);
~ClipTransformRecorder();
private:
gfx::Canvas* canvas_;
DISALLOW_COPY_AND_ASSIGN(ClipTransformRecorder);
};
} // namespace ui
#endif // UI_COMPOSITOR_CLIP_TRANSFORM_RECORDER_H_
...@@ -25,6 +25,8 @@ ...@@ -25,6 +25,8 @@
'COMPOSITOR_IMPLEMENTATION', 'COMPOSITOR_IMPLEMENTATION',
], ],
'sources': [ 'sources': [
'clip_transform_recorder.cc',
'clip_transform_recorder.h',
'closure_animation_observer.cc', 'closure_animation_observer.cc',
'closure_animation_observer.h', 'closure_animation_observer.h',
'compositor.cc', 'compositor.cc',
...@@ -62,6 +64,8 @@ ...@@ -62,6 +64,8 @@
'layer_tree_owner.h', 'layer_tree_owner.h',
'layer_type.h', 'layer_type.h',
'paint_context.h', 'paint_context.h',
'paint_recorder.cc',
'paint_recorder.h',
'reflector.cc', 'reflector.cc',
'reflector.h', 'reflector.h',
'scoped_animation_duration_scale_mode.cc', 'scoped_animation_duration_scale_mode.cc',
......
...@@ -13,6 +13,7 @@ class Canvas; ...@@ -13,6 +13,7 @@ class Canvas;
} }
namespace ui { namespace ui {
class PaintRecorder;
class PaintContext { class PaintContext {
public: public:
...@@ -67,6 +68,11 @@ class PaintContext { ...@@ -67,6 +68,11 @@ class PaintContext {
#endif #endif
private: private:
// The PaintRecorder needs access to the internal canvas and friends, but we
// don't want to expose them on this class so that people must go through the
// recorder to access them.
friend class PaintRecorder;
// Clone a PaintContext with an additional |offset|. // Clone a PaintContext with an additional |offset|.
PaintContext(const PaintContext& other, const gfx::Vector2d& offset) PaintContext(const PaintContext& other, const gfx::Vector2d& offset)
: canvas_(other.canvas_), : canvas_(other.canvas_),
......
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ui/compositor/paint_recorder.h"
#include "ui/compositor/paint_context.h"
namespace ui {
PaintRecorder::PaintRecorder(const PaintContext& context)
: canvas_(context.canvas()) {
}
PaintRecorder::~PaintRecorder() {
}
} // namespace ui
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef UI_COMPOSITOR_PAINT_RECORDER_H_
#define UI_COMPOSITOR_PAINT_RECORDER_H_
#include "base/macros.h"
#include "ui/compositor/compositor_export.h"
namespace gfx {
class Canvas;
}
namespace ui {
class PaintContext;
// A class to hide the complexity behind setting up a recording into a
// DisplayItem. This is meant to be short-lived within the scope of recording
// taking place, the DisplayItem should be removed from the PaintRecorder once
// recording is complete and can be cached.
class COMPOSITOR_EXPORT PaintRecorder {
public:
explicit PaintRecorder(const PaintContext& context);
~PaintRecorder();
// Gets a gfx::Canvas for painting into.
gfx::Canvas* canvas() { return canvas_; }
private:
gfx::Canvas* canvas_;
DISALLOW_COPY_AND_ASSIGN(PaintRecorder);
};
} // namespace ui
#endif // UI_COMPOSITOR_PAINT_RECORDER_H_
...@@ -19,11 +19,13 @@ ...@@ -19,11 +19,13 @@
#include "ui/accessibility/ax_enums.h" #include "ui/accessibility/ax_enums.h"
#include "ui/base/cursor/cursor.h" #include "ui/base/cursor/cursor.h"
#include "ui/base/dragdrop/drag_drop_types.h" #include "ui/base/dragdrop/drag_drop_types.h"
#include "ui/compositor/clip_transform_recorder.h"
#include "ui/compositor/compositor.h" #include "ui/compositor/compositor.h"
#include "ui/compositor/dip_util.h" #include "ui/compositor/dip_util.h"
#include "ui/compositor/layer.h" #include "ui/compositor/layer.h"
#include "ui/compositor/layer_animator.h" #include "ui/compositor/layer_animator.h"
#include "ui/compositor/paint_context.h" #include "ui/compositor/paint_context.h"
#include "ui/compositor/paint_recorder.h"
#include "ui/events/event_target_iterator.h" #include "ui/events/event_target_iterator.h"
#include "ui/gfx/canvas.h" #include "ui/gfx/canvas.h"
#include "ui/gfx/geometry/point3_f.h" #include "ui/gfx/geometry/point3_f.h"
...@@ -774,35 +776,42 @@ void View::Paint(const ui::PaintContext& parent_context) { ...@@ -774,35 +776,42 @@ void View::Paint(const ui::PaintContext& parent_context) {
TRACE_EVENT1("views", "View::Paint", "class", GetClassName()); TRACE_EVENT1("views", "View::Paint", "class", GetClassName());
gfx::Canvas* canvas = context.canvas();
gfx::ScopedCanvas scoped_canvas(canvas);
// If the view is backed by a layer, it should paint with itself as the origin // If the view is backed by a layer, it should paint with itself as the origin
// rather than relative to its parent. // rather than relative to its parent.
scoped_ptr<ui::ClipTransformRecorder> clip_transform_recorder;
if (!layer()) { if (!layer()) {
// Set the clip rect to the bounds of this View. Note that the X (or left) // Set the clip rect to the bounds of this View. Note that the X (or left)
// position we pass to ClipRect takes into consideration whether or not the // position we pass to ClipRect takes into consideration whether or not the
// View uses a right-to-left layout so that we paint the View in its // View uses a right-to-left layout so that we paint the View in its
// mirrored position if need be. // mirrored position if need be.
gfx::Rect clip_rect = bounds(); gfx::Rect clip_rect_in_parent = bounds();
clip_rect.Inset(clip_insets_); clip_rect_in_parent.Inset(clip_insets_);
if (parent_) if (parent_)
clip_rect.set_x(parent_->GetMirroredXForRect(clip_rect)); clip_rect_in_parent.set_x(
canvas->ClipRect(clip_rect); parent_->GetMirroredXForRect(clip_rect_in_parent));
// Translate the graphics such that 0,0 corresponds to where // Translate the graphics such that 0,0 corresponds to where
// this View is located relative to its parent. // this View is located relative to its parent.
canvas->Translate(GetMirroredPosition().OffsetFromOrigin()); gfx::Transform transform_from_parent;
canvas->Transform(GetTransform()); gfx::Vector2d offset_from_parent = GetMirroredPosition().OffsetFromOrigin();
transform_from_parent.Translate(offset_from_parent.x(),
offset_from_parent.y());
transform_from_parent.PreconcatTransform(GetTransform());
clip_transform_recorder = make_scoped_ptr(new ui::ClipTransformRecorder(
context, clip_rect_in_parent, transform_from_parent));
} }
{ {
ui::PaintRecorder recorder(context);
gfx::Canvas* canvas = recorder.canvas();
gfx::ScopedCanvas scoped_canvas(canvas);
// If the View we are about to paint requested the canvas to be flipped, we // If the View we are about to paint requested the canvas to be flipped, we
// should change the transform appropriately. // should change the transform appropriately.
// The canvas mirroring is undone once the View is done painting so that we // The canvas mirroring is undone once the View is done painting so that we
// don't pass the canvas with the mirrored transform to Views that didn't // don't pass the canvas with the mirrored transform to Views that didn't
// request the canvas to be flipped. // request the canvas to be flipped.
gfx::ScopedCanvas scoped(canvas);
if (FlipCanvasOnPaintForRTLUI()) { if (FlipCanvasOnPaintForRTLUI()) {
canvas->Translate(gfx::Vector2d(width(), 0)); canvas->Translate(gfx::Vector2d(width(), 0));
canvas->Scale(-1, 1); canvas->Scale(-1, 1);
...@@ -812,6 +821,7 @@ void View::Paint(const ui::PaintContext& parent_context) { ...@@ -812,6 +821,7 @@ void View::Paint(const ui::PaintContext& parent_context) {
OnPaint(canvas); OnPaint(canvas);
} }
// View::Paint() recursion over the subtree.
PaintChildren(context); PaintChildren(context);
} }
......
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