Break dependency from Layer to PaintContext (flutter/engine#2609)

Instead, make Layer::Paint take a new PaintContext that has just exactly
the state that it needs, mirroring PrerollContext. Also, rename
PaintContext to CompositorContext because it holds the context for the
whole compositor.
This commit is contained in:
Adam Barth
2016-04-21 23:05:41 -07:00
parent 785c11d6d9
commit 75b5912bd2
32 changed files with 160 additions and 148 deletions

View File

@@ -6,6 +6,8 @@ source_set("flow") {
sources = [
"checkerboard.cc",
"checkerboard.h",
"compositor_context.cc",
"compositor_context.h",
"instrumentation.cc",
"instrumentation.h",
"layers/backdrop_filter_layer.cc",
@@ -36,8 +38,6 @@ source_set("flow") {
"layers/shader_mask_layer.h",
"layers/transform_layer.cc",
"layers/transform_layer.h",
"paint_context.cc",
"paint_context.h",
"raster_cache.cc",
"raster_cache.h",
]

View File

@@ -0,0 +1,58 @@
// 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 "flow/compositor_context.h"
#include "base/logging.h"
#include "third_party/skia/include/core/SkCanvas.h"
namespace flow {
CompositorContext::CompositorContext() {
}
CompositorContext::~CompositorContext() {
}
void CompositorContext::BeginFrame(ScopedFrame& frame,
bool enable_instrumentation) {
if (enable_instrumentation) {
frame_count_.Increment();
frame_time_.Start();
}
}
void CompositorContext::EndFrame(ScopedFrame& frame,
bool enable_instrumentation) {
raster_cache_.SweepAfterFrame();
if (enable_instrumentation) {
frame_time_.Stop();
}
}
CompositorContext::ScopedFrame CompositorContext::AcquireFrame(
GrContext* gr_context, SkCanvas& canvas, bool instrumentation_enabled) {
return ScopedFrame(*this, gr_context, canvas, instrumentation_enabled);
}
CompositorContext::ScopedFrame::ScopedFrame(CompositorContext& context,
GrContext* gr_context,
SkCanvas& canvas,
bool instrumentation_enabled)
: context_(context), gr_context_(gr_context), canvas_(&canvas),
instrumentation_enabled_(instrumentation_enabled) {
context_.BeginFrame(*this, instrumentation_enabled_);
}
CompositorContext::ScopedFrame::ScopedFrame(ScopedFrame&& frame) = default;
CompositorContext::ScopedFrame::~ScopedFrame() {
context_.EndFrame(*this, instrumentation_enabled_);
}
void CompositorContext::OnGrContextDestroyed() {
raster_cache_.Clear();
}
} // namespace flow

View File

@@ -2,8 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef FLOW_PAINT_CONTEXT_H_
#define FLOW_PAINT_CONTEXT_H_
#ifndef FLOW_COMPOSITOR_CONTEXT_H_
#define FLOW_COMPOSITOR_CONTEXT_H_
#include <memory>
#include <string>
@@ -17,35 +17,35 @@
namespace flow {
class PaintContext {
class CompositorContext {
public:
class ScopedFrame {
public:
SkCanvas& canvas() { return *canvas_; }
PaintContext& context() const { return context_; }
CompositorContext& context() const { return context_; }
GrContext* gr_context() const { return gr_context_; }
ScopedFrame(ScopedFrame&& frame);
~ScopedFrame();
private:
PaintContext& context_;
CompositorContext& context_;
GrContext* gr_context_;
SkCanvas* canvas_;
const bool instrumentation_enabled_;
ScopedFrame(PaintContext& context,
ScopedFrame(CompositorContext& context,
GrContext* gr_context,
SkCanvas& canvas,
bool instrumentation_enabled);
friend class PaintContext;
friend class CompositorContext;
DISALLOW_COPY_AND_ASSIGN(ScopedFrame);
};
PaintContext();
~PaintContext();
CompositorContext();
~CompositorContext();
ScopedFrame AcquireFrame(GrContext* gr_context,
SkCanvas& canvas,
@@ -65,12 +65,12 @@ class PaintContext {
Stopwatch frame_time_;
Stopwatch engine_time_;
void BeginFrame(ScopedFrame& frame, bool enableInstrumentation);
void EndFrame(ScopedFrame& frame, bool enableInstrumentation);
void BeginFrame(ScopedFrame& frame, bool enable_instrumentation);
void EndFrame(ScopedFrame& frame, bool enable_instrumentation);
DISALLOW_COPY_AND_ASSIGN(PaintContext);
DISALLOW_COPY_AND_ASSIGN(CompositorContext);
};
} // namespace flow
#endif // FLOW_PAINT_CONTEXT_H_
#endif // FLOW_COMPOSITOR_CONTEXT_H_

View File

@@ -4,6 +4,8 @@
#include "flow/layers/backdrop_filter_layer.h"
#include "third_party/skia/include/core/SkImageFilter.h"
namespace flow {
BackdropFilterLayer::BackdropFilterLayer() {
@@ -12,12 +14,11 @@ BackdropFilterLayer::BackdropFilterLayer() {
BackdropFilterLayer::~BackdropFilterLayer() {
}
void BackdropFilterLayer::Paint(PaintContext::ScopedFrame& frame) {
SkCanvas& canvas = frame.canvas();
SkAutoCanvasRestore save(&canvas, false);
canvas.saveLayer(SkCanvas::SaveLayerRec{
void BackdropFilterLayer::Paint(PaintContext& context) {
SkAutoCanvasRestore save(&context.canvas, false);
context.canvas.saveLayer(SkCanvas::SaveLayerRec{
&paint_bounds(), nullptr, filter_.get(), 0});
PaintChildren(frame);
PaintChildren(context);
}
} // namespace flow

View File

@@ -17,7 +17,7 @@ class BackdropFilterLayer : public ContainerLayer {
void set_filter(SkImageFilter* filter) { filter_ = skia::SharePtr(filter); }
protected:
void Paint(PaintContext::ScopedFrame& frame) override;
void Paint(PaintContext& context) override;
private:
skia::RefPtr<SkImageFilter> filter_;

View File

@@ -24,7 +24,7 @@ void ChildSceneLayer::Preroll(PrerollContext* context, const SkMatrix& matrix) {
transform_.preScale(inverse_device_pixel_ratio, inverse_device_pixel_ratio);
}
void ChildSceneLayer::Paint(PaintContext::ScopedFrame& frame) {
void ChildSceneLayer::Paint(PaintContext& context) {
}
void ChildSceneLayer::UpdateScene(mojo::gfx::composition::SceneUpdate* update,

View File

@@ -30,7 +30,7 @@ class ChildSceneLayer : public Layer {
}
void Preroll(PrerollContext* context, const SkMatrix& matrix) override;
void Paint(PaintContext::ScopedFrame& frame) override;
void Paint(PaintContext& context) override;
void UpdateScene(mojo::gfx::composition::SceneUpdate* update,
mojo::gfx::composition::Node* container) override;

View File

@@ -19,12 +19,11 @@ void ClipPathLayer::Preroll(PrerollContext* context, const SkMatrix& matrix) {
set_paint_bounds(context->child_paint_bounds);
}
void ClipPathLayer::Paint(PaintContext::ScopedFrame& frame) {
SkCanvas& canvas = frame.canvas();
SkAutoCanvasRestore save(&canvas, false);
canvas.saveLayer(&paint_bounds(), nullptr);
canvas.clipPath(clip_path_);
PaintChildren(frame);
void ClipPathLayer::Paint(PaintContext& context) {
SkAutoCanvasRestore save(&context.canvas, false);
context.canvas.saveLayer(&paint_bounds(), nullptr);
context.canvas.clipPath(clip_path_);
PaintChildren(context);
}
} // namespace flow

View File

@@ -18,7 +18,7 @@ class ClipPathLayer : public ContainerLayer {
protected:
void Preroll(PrerollContext* context, const SkMatrix& matrix) override;
void Paint(PaintContext::ScopedFrame& frame) override;
void Paint(PaintContext& context) override;
private:
SkPath clip_path_;

View File

@@ -19,11 +19,10 @@ void ClipRectLayer::Preroll(PrerollContext* context, const SkMatrix& matrix) {
set_paint_bounds(context->child_paint_bounds);
}
void ClipRectLayer::Paint(PaintContext::ScopedFrame& frame) {
SkCanvas& canvas = frame.canvas();
SkAutoCanvasRestore save(&canvas, true);
canvas.clipRect(paint_bounds());
PaintChildren(frame);
void ClipRectLayer::Paint(PaintContext& context) {
SkAutoCanvasRestore save(&context.canvas, true);
context.canvas.clipRect(paint_bounds());
PaintChildren(context);
}
} // namespace flow

View File

@@ -18,7 +18,7 @@ class ClipRectLayer : public ContainerLayer {
protected:
void Preroll(PrerollContext* context, const SkMatrix& matrix) override;
void Paint(PaintContext::ScopedFrame& frame) override;
void Paint(PaintContext& context) override;
private:
SkRect clip_rect_;

View File

@@ -19,12 +19,11 @@ void ClipRRectLayer::Preroll(PrerollContext* context, const SkMatrix& matrix) {
set_paint_bounds(context->child_paint_bounds);
}
void ClipRRectLayer::Paint(PaintContext::ScopedFrame& frame) {
SkCanvas& canvas = frame.canvas();
SkAutoCanvasRestore save(&canvas, false);
canvas.saveLayer(&paint_bounds(), nullptr);
canvas.clipRRect(clip_rrect_);
PaintChildren(frame);
void ClipRRectLayer::Paint(PaintContext& context) {
SkAutoCanvasRestore save(&context.canvas, false);
context.canvas.saveLayer(&paint_bounds(), nullptr);
context.canvas.clipRRect(clip_rrect_);
PaintChildren(context);
}
} // namespace flow

View File

@@ -18,7 +18,7 @@ class ClipRRectLayer : public ContainerLayer {
protected:
void Preroll(PrerollContext* context, const SkMatrix& matrix) override;
void Paint(PaintContext::ScopedFrame& frame) override;
void Paint(PaintContext& context) override;
private:
SkRRect clip_rrect_;

View File

@@ -12,16 +12,15 @@ ColorFilterLayer::ColorFilterLayer() {
ColorFilterLayer::~ColorFilterLayer() {
}
void ColorFilterLayer::Paint(PaintContext::ScopedFrame& frame) {
void ColorFilterLayer::Paint(PaintContext& context) {
skia::RefPtr<SkColorFilter> color_filter =
skia::AdoptRef(SkColorFilter::CreateModeFilter(color_, transfer_mode_));
SkPaint paint;
paint.setColorFilter(color_filter.get());
SkCanvas& canvas = frame.canvas();
SkAutoCanvasRestore save(&canvas, false);
canvas.saveLayer(&paint_bounds(), &paint);
PaintChildren(frame);
SkAutoCanvasRestore save(&context.canvas, false);
context.canvas.saveLayer(&paint_bounds(), &paint);
PaintChildren(context);
}
} // namespace flow

View File

@@ -21,7 +21,7 @@ class ColorFilterLayer : public ContainerLayer {
}
protected:
void Paint(PaintContext::ScopedFrame& frame) override;
void Paint(PaintContext& context) override;
private:
SkColor color_;

View File

@@ -33,9 +33,9 @@ void ContainerLayer::PrerollChildren(PrerollContext* context,
context->child_paint_bounds = child_paint_bounds;
}
void ContainerLayer::PaintChildren(PaintContext::ScopedFrame& frame) const {
void ContainerLayer::PaintChildren(PaintContext& context) const {
for (auto& layer : layers_)
layer->Paint(frame);
layer->Paint(context);
}
void ContainerLayer::UpdateScene(mojo::gfx::composition::SceneUpdate* update,

View File

@@ -20,7 +20,7 @@ class ContainerLayer : public Layer {
void Preroll(PrerollContext* context, const SkMatrix& matrix) override;
void PrerollChildren(PrerollContext* context, const SkMatrix& matrix);
void PaintChildren(PaintContext::ScopedFrame& frame) const;
void PaintChildren(PaintContext& context) const;
void UpdateScene(mojo::gfx::composition::SceneUpdate* update,
mojo::gfx::composition::Node* container) override;

View File

@@ -10,7 +10,8 @@
#include "base/logging.h"
#include "base/macros.h"
#include "flow/paint_context.h"
#include "flow/instrumentation.h"
#include "flow/raster_cache.h"
#include "skia/ext/refptr.h"
#include "third_party/skia/include/core/SkCanvas.h"
#include "third_party/skia/include/core/SkColor.h"
@@ -46,7 +47,14 @@ class Layer {
};
virtual void Preroll(PrerollContext* context, const SkMatrix& matrix);
virtual void Paint(PaintContext::ScopedFrame& frame) = 0;
struct PaintContext {
SkCanvas& canvas;
const Stopwatch& frame_time;
const Stopwatch& engine_time;
};
virtual void Paint(PaintContext& context) = 0;
virtual void UpdateScene(mojo::gfx::composition::SceneUpdate* update,
mojo::gfx::composition::Node* container);

View File

@@ -15,7 +15,7 @@ LayerTree::LayerTree() : scene_version_(0), rasterizer_tracing_threshold_(0) {
LayerTree::~LayerTree() {
}
void LayerTree::Raster(PaintContext::ScopedFrame& frame) {
void LayerTree::Raster(CompositorContext::ScopedFrame& frame) {
{
TRACE_EVENT0("flutter", "LayerTree::Preroll");
Layer::PrerollContext context = {
@@ -27,8 +27,13 @@ void LayerTree::Raster(PaintContext::ScopedFrame& frame) {
}
{
Layer::PaintContext context = {
frame.canvas(),
frame.context().frame_time(),
frame.context().engine_time(),
};
TRACE_EVENT0("flutter", "LayerTree::Paint");
root_layer_->Paint(frame);
root_layer_->Paint(context);
}
}

View File

@@ -10,6 +10,7 @@
#include "base/macros.h"
#include "base/time/time.h"
#include "flow/compositor_context.h"
#include "flow/layers/layer.h"
#include "third_party/skia/include/core/SkSize.h"
@@ -20,7 +21,7 @@ class LayerTree {
LayerTree();
~LayerTree();
void Raster(PaintContext::ScopedFrame& frame);
void Raster(CompositorContext::ScopedFrame& frame);
// TODO(abarth): Integrate scene updates with the rasterization pass so that
// we can draw on top of child scenes (and so that we can apply clips and

View File

@@ -12,14 +12,13 @@ OpacityLayer::OpacityLayer() {
OpacityLayer::~OpacityLayer() {
}
void OpacityLayer::Paint(PaintContext::ScopedFrame& frame) {
void OpacityLayer::Paint(PaintContext& context) {
SkPaint paint;
paint.setAlpha(alpha_);
SkCanvas& canvas = frame.canvas();
SkAutoCanvasRestore save(&canvas, false);
canvas.saveLayer(&paint_bounds(), &paint);
PaintChildren(frame);
SkAutoCanvasRestore save(&context.canvas, false);
context.canvas.saveLayer(&paint_bounds(), &paint);
PaintChildren(context);
}
} // namespace flow

View File

@@ -17,7 +17,7 @@ class OpacityLayer : public ContainerLayer {
void set_alpha(int alpha) { alpha_ = alpha; }
protected:
void Paint(PaintContext::ScopedFrame& frame) override;
void Paint(PaintContext& context) override;
private:
int alpha_;

View File

@@ -64,7 +64,7 @@ PerformanceOverlayLayer::PerformanceOverlayLayer(uint64_t options)
}
void PerformanceOverlayLayer::Paint(PaintContext::ScopedFrame& frame) {
void PerformanceOverlayLayer::Paint(PaintContext& context) {
if (!options_)
return;
@@ -72,15 +72,15 @@ void PerformanceOverlayLayer::Paint(PaintContext::ScopedFrame& frame) {
SkScalar y = paint_bounds().y();
SkScalar width = paint_bounds().width();
SkScalar height = paint_bounds().height() / 2;
SkAutoCanvasRestore save(&frame.canvas(), true);
SkAutoCanvasRestore save(&context.canvas, true);
VisualizeStopWatch(frame.canvas(), frame.context().frame_time(),
VisualizeStopWatch(context.canvas, context.frame_time,
x, y, width, height,
options_ & kVisualizeRasterizerStatistics,
options_ & kDisplayRasterizerStatistics,
"Rasterizer");
VisualizeStopWatch(frame.canvas(), frame.context().engine_time(),
VisualizeStopWatch(context.canvas, context.engine_time,
x, y + height, width, height,
options_ & kVisualizeEngineStatistics,
options_ & kDisplayEngineStatistics,

View File

@@ -19,7 +19,7 @@ class PerformanceOverlayLayer : public Layer {
public:
explicit PerformanceOverlayLayer(uint64_t options);
void Paint(PaintContext::ScopedFrame& frame) override;
void Paint(PaintContext& context) override;
private:
int options_;

View File

@@ -23,22 +23,23 @@ void PictureLayer::Preroll(PrerollContext* context,
const SkMatrix& matrix) {
image_ = context->raster_cache.GetPrerolledImage(
context->gr_context, picture_.get(), matrix);
context->child_paint_bounds = picture_->cullRect().makeOffset(offset_.x(), offset_.y());
context->child_paint_bounds =
picture_->cullRect().makeOffset(offset_.x(), offset_.y());
}
void PictureLayer::Paint(PaintContext::ScopedFrame& frame) {
void PictureLayer::Paint(PaintContext& context) {
DCHECK(picture_);
SkCanvas& canvas = frame.canvas();
if (image_) {
SkRect rect = picture_->cullRect().makeOffset(offset_.x(), offset_.y());
canvas.drawImageRect(image_.get(), rect, nullptr, SkCanvas::kFast_SrcRectConstraint);
context.canvas.drawImageRect(image_.get(), rect, nullptr,
SkCanvas::kFast_SrcRectConstraint);
if (kDebugCheckerboardRasterizedLayers)
DrawCheckerboard(&canvas, rect);
DrawCheckerboard(&context.canvas, rect);
} else {
SkAutoCanvasRestore save(&canvas, true);
canvas.translate(offset_.x(), offset_.y());
canvas.drawPicture(picture_.get());
SkAutoCanvasRestore save(&context.canvas, true);
context.canvas.translate(offset_.x(), offset_.y());
context.canvas.drawPicture(picture_.get());
}
}

View File

@@ -21,7 +21,7 @@ class PictureLayer : public Layer {
SkPicture* picture() const { return picture_.get(); }
void Preroll(PrerollContext* frame, const SkMatrix& matrix) override;
void Paint(PaintContext::ScopedFrame& frame) override;
void Paint(PaintContext& context) override;
private:
SkPoint offset_;

View File

@@ -12,17 +12,17 @@ ShaderMaskLayer::ShaderMaskLayer() {
ShaderMaskLayer::~ShaderMaskLayer() {
}
void ShaderMaskLayer::Paint(PaintContext::ScopedFrame& frame) {
SkCanvas& canvas = frame.canvas();
SkAutoCanvasRestore save(&canvas, false);
canvas.saveLayer(&paint_bounds(), nullptr);
PaintChildren(frame);
void ShaderMaskLayer::Paint(PaintContext& context) {
SkAutoCanvasRestore save(&context.canvas, false);
context.canvas.saveLayer(&paint_bounds(), nullptr);
PaintChildren(context);
SkPaint paint;
paint.setXfermodeMode(transfer_mode_);
paint.setShader(shader_);
canvas.translate(mask_rect_.left(), mask_rect_.top());
canvas.drawRect(SkRect::MakeWH(mask_rect_.width(), mask_rect_.height()), paint);
context.canvas.translate(mask_rect_.left(), mask_rect_.top());
context.canvas.drawRect(
SkRect::MakeWH(mask_rect_.width(), mask_rect_.height()), paint);
}
} // namespace flow

View File

@@ -27,7 +27,7 @@ class ShaderMaskLayer : public ContainerLayer {
}
protected:
void Paint(PaintContext::ScopedFrame& frame) override;
void Paint(PaintContext& context) override;
private:
sk_sp<SkShader> shader_;

View File

@@ -19,11 +19,10 @@ void TransformLayer::Preroll(PrerollContext* context, const SkMatrix& matrix) {
transform_.mapRect(&context->child_paint_bounds);
}
void TransformLayer::Paint(PaintContext::ScopedFrame& frame) {
SkCanvas& canvas = frame.canvas();
SkAutoCanvasRestore save(&canvas, true);
canvas.concat(transform_);
PaintChildren(frame);
void TransformLayer::Paint(PaintContext& context) {
SkAutoCanvasRestore save(&context.canvas, true);
context.canvas.concat(transform_);
PaintChildren(context);
}
} // namespace flow

View File

@@ -17,7 +17,7 @@ class TransformLayer : public ContainerLayer {
void set_transform(const SkMatrix& transform) { transform_ = transform; }
void Preroll(PrerollContext* context, const SkMatrix& matrix) override;
void Paint(PaintContext::ScopedFrame& frame) override;
void Paint(PaintContext& context) override;
private:
SkMatrix transform_;

View File

@@ -1,56 +0,0 @@
// 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 "flow/paint_context.h"
#include "base/logging.h"
#include "third_party/skia/include/core/SkCanvas.h"
namespace flow {
PaintContext::PaintContext() {
}
void PaintContext::BeginFrame(ScopedFrame& frame, bool enableInstrumentation) {
if (enableInstrumentation) {
frame_count_.Increment();
frame_time_.Start();
}
}
void PaintContext::EndFrame(ScopedFrame& frame, bool enableInstrumentation) {
raster_cache_.SweepAfterFrame();
if (enableInstrumentation) {
frame_time_.Stop();
}
}
PaintContext::ScopedFrame PaintContext::AcquireFrame(
GrContext* gr_context, SkCanvas& canvas, bool instrumentation_enabled) {
return ScopedFrame(*this, gr_context, canvas, instrumentation_enabled);
}
PaintContext::ScopedFrame::ScopedFrame(PaintContext& context,
GrContext* gr_context,
SkCanvas& canvas,
bool instrumentation_enabled)
: context_(context), gr_context_(gr_context), canvas_(&canvas),
instrumentation_enabled_(instrumentation_enabled) {
context_.BeginFrame(*this, instrumentation_enabled_);
}
PaintContext::ScopedFrame::ScopedFrame(ScopedFrame&& frame) = default;
PaintContext::ScopedFrame::~ScopedFrame() {
context_.EndFrame(*this, instrumentation_enabled_);
}
PaintContext::~PaintContext() {
}
void PaintContext::OnGrContextDestroyed() {
raster_cache_.Clear();
}
} // namespace flow

View File

@@ -6,9 +6,9 @@
#include "base/logging.h"
#include "base/trace_event/trace_event.h"
#include "flow/paint_context.h"
#include "third_party/skia/include/core/SkCanvas.h"
#include "third_party/skia/include/core/SkImage.h"
#include "third_party/skia/include/core/SkPicture.h"
#include "third_party/skia/include/core/SkSurface.h"
#define ENABLE_RASTER_CACHE 1