Skip to content

Commit

Permalink
Merge pull request flutter#1031 from abarth/sky_scene_compositor
Browse files Browse the repository at this point in the history
SceneBuilder should build a tree of layers
  • Loading branch information
abarth committed Sep 3, 2015
2 parents 0b98d88 + bc8f7a0 commit 735890c
Show file tree
Hide file tree
Showing 9 changed files with 405 additions and 55 deletions.
16 changes: 16 additions & 0 deletions sky/compositor/BUILD.gn
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# 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.

source_set("compositor") {
sources = [
"layer.cc",
"layer.h",
]

deps = [
"//base",
"//skia",
"//sky/engine/wtf",
]
}
130 changes: 130 additions & 0 deletions sky/compositor/layer.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
// 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 "sky/compositor/layer.h"

#include "third_party/skia/include/core/SkColorFilter.h"

namespace sky {

Layer::Layer() {
}

Layer::~Layer() {
}

PictureLayer::PictureLayer() {
}

PictureLayer::~PictureLayer() {
}

void PictureLayer::Paint(SkCanvas* canvas) {
canvas->save();
canvas->translate(offset_.x(), offset_.y());
canvas->drawPicture(picture_.get());
canvas->restore();
}

ContainerLayer::ContainerLayer() {
}

ContainerLayer::~ContainerLayer() {
}

void ContainerLayer::Add(std::unique_ptr<Layer> layer) {
layer->set_parent(this);
layers_.push_back(std::move(layer));
}

void ContainerLayer::PaintChildren(SkCanvas* canvas) {
for (auto& layer : layers_)
layer->Paint(canvas);
}

TransformLayer::TransformLayer() {
}

TransformLayer::~TransformLayer() {
}

void TransformLayer::Paint(SkCanvas* canvas) {
canvas->save();
canvas->concat(transform_);
PaintChildren(canvas);
canvas->restore();
}

ClipRectLayer::ClipRectLayer() {
}

ClipRectLayer::~ClipRectLayer() {
}

void ClipRectLayer::Paint(SkCanvas* canvas) {
canvas->save();
canvas->clipRect(clip_rect_);
PaintChildren(canvas);
canvas->restore();
}

ClipRRectLayer::ClipRRectLayer() {
}

ClipRRectLayer::~ClipRRectLayer() {
}

void ClipRRectLayer::Paint(SkCanvas* canvas) {
canvas->saveLayer(&clip_rrect_.getBounds(), nullptr);
canvas->clipRRect(clip_rrect_);
PaintChildren(canvas);
canvas->restore();
}

ClipPathLayer::ClipPathLayer() {
}

ClipPathLayer::~ClipPathLayer() {
}

void ClipPathLayer::Paint(SkCanvas* canvas) {
canvas->saveLayer(&clip_path_.getBounds(), nullptr);
canvas->clipPath(clip_path_);
PaintChildren(canvas);
canvas->restore();
}

OpacityLayer::OpacityLayer() {
}

OpacityLayer::~OpacityLayer() {
}

void OpacityLayer::Paint(SkCanvas* canvas) {
SkColor color = SkColorSetARGB(alpha_, 0, 0, 0);
RefPtr<SkColorFilter> colorFilter = adoptRef(SkColorFilter::CreateModeFilter(color, SkXfermode::kSrcOver_Mode));
SkPaint paint;
paint.setColorFilter(colorFilter.get());
canvas->saveLayer(&paint_bounds(), &paint);
PaintChildren(canvas);
canvas->restore();
}

ColorFilterLayer::ColorFilterLayer() {
}

ColorFilterLayer::~ColorFilterLayer() {
}

void ColorFilterLayer::Paint(SkCanvas* canvas) {
RefPtr<SkColorFilter> color_filter =
adoptRef(SkColorFilter::CreateModeFilter(color_, transfer_mode_));
SkPaint paint;
paint.setColorFilter(color_filter.get());
canvas->saveLayer(&paint_bounds(), &paint);
PaintChildren(canvas);
canvas->restore();
}

} // namespace sky
173 changes: 173 additions & 0 deletions sky/compositor/layer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
// 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 SKY_COMPOSITOR_H_
#define SKY_COMPOSITOR_H_

#include <memory>
#include <vector>

#include "base/macros.h"
#include "sky/engine/wtf/PassRefPtr.h"
#include "sky/engine/wtf/RefPtr.h"
#include "third_party/skia/include/core/SkCanvas.h"
#include "third_party/skia/include/core/SkColor.h"
#include "third_party/skia/include/core/SkColorFilter.h"
#include "third_party/skia/include/core/SkMatrix.h"
#include "third_party/skia/include/core/SkPath.h"
#include "third_party/skia/include/core/SkPicture.h"
#include "third_party/skia/include/core/SkRect.h"
#include "third_party/skia/include/core/SkRRect.h"
#include "third_party/skia/include/core/SkXfermode.h"

namespace sky {
class ContainerLayer;

class Layer {
public:
Layer();
virtual ~Layer();

virtual void Paint(SkCanvas* canvas) = 0;

ContainerLayer* parent() const { return parent_; }
void set_parent(ContainerLayer* parent) { parent_ = parent; }

const SkRect& paint_bounds() const { return paint_bounds_; }
void set_paint_bounds(const SkRect& paint_bounds) { paint_bounds_ = paint_bounds; }

private:
ContainerLayer* parent_;
SkRect paint_bounds_;

DISALLOW_COPY_AND_ASSIGN(Layer);
};

class PictureLayer : public Layer {
public:
PictureLayer();
~PictureLayer() override;

void Paint(SkCanvas* canvas) override;

void set_offset(const SkPoint& offset) { offset_ = offset; }
void set_picture(PassRefPtr<SkPicture> picture) { picture_ = picture; }

private:
SkPoint offset_;
RefPtr<SkPicture> picture_;

DISALLOW_COPY_AND_ASSIGN(PictureLayer);
};

class ContainerLayer : public Layer {
public:
ContainerLayer();
~ContainerLayer() override;

void Add(std::unique_ptr<Layer> layer);

void PaintChildren(SkCanvas* canvas);

private:
std::vector<std::unique_ptr<Layer>> layers_;

DISALLOW_COPY_AND_ASSIGN(ContainerLayer);
};

class TransformLayer : public ContainerLayer {
public:
TransformLayer();
~TransformLayer() override;

void Paint(SkCanvas* canvas) override;

void set_transform(const SkMatrix& transform) { transform_ = transform; }

private:
SkMatrix transform_;

DISALLOW_COPY_AND_ASSIGN(TransformLayer);
};

class ClipRectLayer : public ContainerLayer {
public:
ClipRectLayer();
~ClipRectLayer() override;

void Paint(SkCanvas* canvas) override;

void set_clip_rect(const SkRect& clip_rect) { clip_rect_ = clip_rect; }

private:
SkRect clip_rect_;

DISALLOW_COPY_AND_ASSIGN(ClipRectLayer);
};

class ClipRRectLayer : public ContainerLayer {
public:
ClipRRectLayer();
~ClipRRectLayer() override;

void Paint(SkCanvas* canvas) override;

void set_clip_rrect(const SkRRect& clip_rrect) { clip_rrect_ = clip_rrect; }

private:
SkRRect clip_rrect_;

DISALLOW_COPY_AND_ASSIGN(ClipRRectLayer);
};

class ClipPathLayer : public ContainerLayer {
public:
ClipPathLayer();
~ClipPathLayer() override;

void Paint(SkCanvas* canvas) override;

void set_clip_path(const SkPath& clip_path) { clip_path_ = clip_path; }

private:
SkPath clip_path_;

DISALLOW_COPY_AND_ASSIGN(ClipPathLayer);
};

class OpacityLayer : public ContainerLayer {
public:
OpacityLayer();
~OpacityLayer() override;

void Paint(SkCanvas* canvas) override;

void set_alpha(int alpha) { alpha_ = alpha; }

private:
int alpha_;

DISALLOW_COPY_AND_ASSIGN(OpacityLayer);
};

class ColorFilterLayer : public ContainerLayer {
public:
ColorFilterLayer();
~ColorFilterLayer() override;

void Paint(SkCanvas* canvas) override;

void set_color(SkColor color) { color_ = color; }
void set_transfer_mode(SkXfermode::Mode transfer_mode) { transfer_mode_ = transfer_mode; }

private:
SkColor color_;
SkXfermode::Mode transfer_mode_;

DISALLOW_COPY_AND_ASSIGN(ColorFilterLayer);
};

} // namespace sky

#endif // SKY_COMPOSITOR_H_
1 change: 1 addition & 0 deletions sky/engine/core/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ source_set("libraries") {
"//mojo/public/cpp/utility",
"//mojo/public/interfaces/application",
"//skia",
"//sky/compositor",
"//sky/engine/tonic:tonic",
"//sky/engine/wtf",
"//third_party/iccjpeg",
Expand Down
23 changes: 18 additions & 5 deletions sky/engine/core/compositing/Scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,34 @@

#include "sky/engine/core/compositing/Scene.h"

#include "third_party/skia/include/core/SkCanvas.h"
#include "third_party/skia/include/core/SkPictureRecorder.h"

namespace blink {

PassRefPtr<Scene> Scene::create(PassRefPtr<SkPicture> picture)
PassRefPtr<Scene> Scene::create(std::unique_ptr<sky::Layer> rootLayer)
{
ASSERT(picture);
return adoptRef(new Scene(picture));
ASSERT(rootLayer);
return adoptRef(new Scene(std::move(rootLayer)));
}

Scene::Scene(PassRefPtr<SkPicture> picture)
: m_picture(picture)
Scene::Scene(std::unique_ptr<sky::Layer> rootLayer)
: m_rootLayer(std::move(rootLayer))
{
}

Scene::~Scene()
{
}

PassRefPtr<SkPicture> Scene::createPicture() const
{
SkRTreeFactory rtreeFactory;
SkPictureRecorder pictureRecorder;
SkCanvas* canvas = pictureRecorder.beginRecording(m_rootLayer->paint_bounds(),
&rtreeFactory, SkPictureRecorder::kComputeSaveLayerInfo_RecordFlag);
m_rootLayer->Paint(canvas);
return adoptRef(pictureRecorder.endRecording());
}

} // namespace blink
Loading

0 comments on commit 735890c

Please sign in to comment.