diff --git a/sky/compositor/BUILD.gn b/sky/compositor/BUILD.gn index c9aeb72968e1fec56540be136b7f597a60afcceb..774d7c38ff32e00292f37651f1de06bc3fd78cca 100644 --- a/sky/compositor/BUILD.gn +++ b/sky/compositor/BUILD.gn @@ -4,10 +4,26 @@ source_set("compositor") { sources = [ - "layer_tree.cc", - "layer_tree.h", + "clip_path_layer.cc", + "clip_path_layer.h", + "clip_rect_layer.cc", + "clip_rect_layer.h", + "clip_rrect_layer.cc", + "clip_rrect_layer.h", + "color_filter_layer.cc", + "color_filter_layer.h", + "container_layer.cc", + "container_layer.h", "layer.cc", "layer.h", + "layer_tree.cc", + "layer_tree.h", + "opacity_layer.cc", + "opacity_layer.h", + "picture_layer.cc", + "picture_layer.h", + "transform_layer.cc", + "transform_layer.h", ] deps = [ diff --git a/sky/compositor/clip_path_layer.cc b/sky/compositor/clip_path_layer.cc new file mode 100644 index 0000000000000000000000000000000000000000..4dc2a52489ac18cf9d915600da09b21771b938ec --- /dev/null +++ b/sky/compositor/clip_path_layer.cc @@ -0,0 +1,24 @@ +// 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/clip_path_layer.h" + +namespace sky { +namespace compositor { + +ClipPathLayer::ClipPathLayer() { +} + +ClipPathLayer::~ClipPathLayer() { +} + +void ClipPathLayer::Paint(GrContext* context, SkCanvas* canvas) { + canvas->saveLayer(&clip_path_.getBounds(), nullptr); + canvas->clipPath(clip_path_); + PaintChildren(context, canvas); + canvas->restore(); +} + +} // namespace compositor +} // namespace sky diff --git a/sky/compositor/clip_path_layer.h b/sky/compositor/clip_path_layer.h new file mode 100644 index 0000000000000000000000000000000000000000..1948aa249018ad3c27c826da48dc3feb28e1e4c8 --- /dev/null +++ b/sky/compositor/clip_path_layer.h @@ -0,0 +1,31 @@ +// 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_CLIP_PATH_LAYER_H_ +#define SKY_COMPOSITOR_CLIP_PATH_LAYER_H_ + +#include "sky/compositor/container_layer.h" + +namespace sky { +namespace compositor { + +class ClipPathLayer : public ContainerLayer { + public: + ClipPathLayer(); + ~ClipPathLayer() override; + + void set_clip_path(const SkPath& clip_path) { clip_path_ = clip_path; } + + void Paint(GrContext* context, SkCanvas* canvas) override; + + private: + SkPath clip_path_; + + DISALLOW_COPY_AND_ASSIGN(ClipPathLayer); +}; + +} // namespace compositor +} // namespace sky + +#endif // SKY_COMPOSITOR_CLIP_PATH_LAYER_H_ diff --git a/sky/compositor/clip_rect_layer.cc b/sky/compositor/clip_rect_layer.cc new file mode 100644 index 0000000000000000000000000000000000000000..fc2f3a69e5b6701ef73a447d17434ccf84e31672 --- /dev/null +++ b/sky/compositor/clip_rect_layer.cc @@ -0,0 +1,24 @@ +// 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/clip_rect_layer.h" + +namespace sky { +namespace compositor { + +ClipRectLayer::ClipRectLayer() { +} + +ClipRectLayer::~ClipRectLayer() { +} + +void ClipRectLayer::Paint(GrContext* context, SkCanvas* canvas) { + canvas->save(); + canvas->clipRect(clip_rect_); + PaintChildren(context, canvas); + canvas->restore(); +} + +} // namespace compositor +} // namespace sky diff --git a/sky/compositor/clip_rect_layer.h b/sky/compositor/clip_rect_layer.h new file mode 100644 index 0000000000000000000000000000000000000000..c7437dbaf929a3fe7cb9b5098155d061fb40254e --- /dev/null +++ b/sky/compositor/clip_rect_layer.h @@ -0,0 +1,31 @@ +// 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_CLIP_RECT_LAYER_H_ +#define SKY_COMPOSITOR_CLIP_RECT_LAYER_H_ + +#include "sky/compositor/container_layer.h" + +namespace sky { +namespace compositor { + +class ClipRectLayer : public ContainerLayer { + public: + ClipRectLayer(); + ~ClipRectLayer() override; + + void set_clip_rect(const SkRect& clip_rect) { clip_rect_ = clip_rect; } + + void Paint(GrContext* context, SkCanvas* canvas) override; + + private: + SkRect clip_rect_; + + DISALLOW_COPY_AND_ASSIGN(ClipRectLayer); +}; + +} // namespace compositor +} // namespace sky + +#endif // SKY_COMPOSITOR_CLIP_RECT_LAYER_H_ diff --git a/sky/compositor/clip_rrect_layer.cc b/sky/compositor/clip_rrect_layer.cc new file mode 100644 index 0000000000000000000000000000000000000000..7b1af7907049658a91e9457ddc9f34344d5077c6 --- /dev/null +++ b/sky/compositor/clip_rrect_layer.cc @@ -0,0 +1,24 @@ +// 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/clip_rrect_layer.h" + +namespace sky { +namespace compositor { + +ClipRRectLayer::ClipRRectLayer() { +} + +ClipRRectLayer::~ClipRRectLayer() { +} + +void ClipRRectLayer::Paint(GrContext* context, SkCanvas* canvas) { + canvas->saveLayer(&clip_rrect_.getBounds(), nullptr); + canvas->clipRRect(clip_rrect_); + PaintChildren(context, canvas); + canvas->restore(); +} + +} // namespace compositor +} // namespace sky diff --git a/sky/compositor/clip_rrect_layer.h b/sky/compositor/clip_rrect_layer.h new file mode 100644 index 0000000000000000000000000000000000000000..cd742a88b77b4256f6db3db6e61efad89fb42481 --- /dev/null +++ b/sky/compositor/clip_rrect_layer.h @@ -0,0 +1,31 @@ +// 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_CLIP_RRECT_LAYER_H_ +#define SKY_COMPOSITOR_CLIP_RRECT_LAYER_H_ + +#include "sky/compositor/container_layer.h" + +namespace sky { +namespace compositor { + +class ClipRRectLayer : public ContainerLayer { + public: + ClipRRectLayer(); + ~ClipRRectLayer() override; + + void set_clip_rrect(const SkRRect& clip_rrect) { clip_rrect_ = clip_rrect; } + + void Paint(GrContext* context, SkCanvas* canvas) override; + + private: + SkRRect clip_rrect_; + + DISALLOW_COPY_AND_ASSIGN(ClipRRectLayer); +}; + +} // namespace compositor +} // namespace sky + +#endif // SKY_COMPOSITOR_CLIP_RRECT_LAYER_H_ diff --git a/sky/compositor/color_filter_layer.cc b/sky/compositor/color_filter_layer.cc new file mode 100644 index 0000000000000000000000000000000000000000..9e530bd8c3e864a27144a118b5d2ec5f054c3110 --- /dev/null +++ b/sky/compositor/color_filter_layer.cc @@ -0,0 +1,27 @@ +// 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/color_filter_layer.h" + +namespace sky { +namespace compositor { + +ColorFilterLayer::ColorFilterLayer() { +} + +ColorFilterLayer::~ColorFilterLayer() { +} + +void ColorFilterLayer::Paint(GrContext* context, SkCanvas* canvas) { + RefPtr color_filter = + adoptRef(SkColorFilter::CreateModeFilter(color_, transfer_mode_)); + SkPaint paint; + paint.setColorFilter(color_filter.get()); + canvas->saveLayer(&paint_bounds(), &paint); + PaintChildren(context, canvas); + canvas->restore(); +} + +} // namespace compositor +} // namespace sky diff --git a/sky/compositor/color_filter_layer.h b/sky/compositor/color_filter_layer.h new file mode 100644 index 0000000000000000000000000000000000000000..67fab941d6b262cedd5be0457560dc24e4d3f13a --- /dev/null +++ b/sky/compositor/color_filter_layer.h @@ -0,0 +1,36 @@ +// 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_COLOR_FILTER_LAYER_H_ +#define SKY_COMPOSITOR_COLOR_FILTER_LAYER_H_ + +#include "sky/compositor/container_layer.h" + +namespace sky { +namespace compositor { + +class ColorFilterLayer : public ContainerLayer { + public: + ColorFilterLayer(); + ~ColorFilterLayer() override; + + void set_color(SkColor color) { color_ = color; } + + void set_transfer_mode(SkXfermode::Mode transfer_mode) { + transfer_mode_ = transfer_mode; + } + + void Paint(GrContext* context, SkCanvas* canvas) override; + + private: + SkColor color_; + SkXfermode::Mode transfer_mode_; + + DISALLOW_COPY_AND_ASSIGN(ColorFilterLayer); +}; + +} // namespace compositor +} // namespace sky + +#endif // SKY_COMPOSITOR_COLOR_FILTER_LAYER_H_ diff --git a/sky/compositor/container_layer.cc b/sky/compositor/container_layer.cc new file mode 100644 index 0000000000000000000000000000000000000000..9676125bc4001c4e52cd24bb89850f9b02617673 --- /dev/null +++ b/sky/compositor/container_layer.cc @@ -0,0 +1,27 @@ +// 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/container_layer.h" + +namespace sky { +namespace compositor { + +ContainerLayer::ContainerLayer() { +} + +ContainerLayer::~ContainerLayer() { +} + +void ContainerLayer::Add(std::unique_ptr layer) { + layer->set_parent(this); + layers_.push_back(std::move(layer)); +} + +void ContainerLayer::PaintChildren(GrContext* context, SkCanvas* canvas) const { + for (auto& layer : layers_) + layer->Paint(context, canvas); +} + +} // namespace compositor +} // namespace sky diff --git a/sky/compositor/container_layer.h b/sky/compositor/container_layer.h new file mode 100644 index 0000000000000000000000000000000000000000..6ba93e8f6278ea9011d36451c07ed6c0229a3170 --- /dev/null +++ b/sky/compositor/container_layer.h @@ -0,0 +1,31 @@ +// 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_CONTAINER_LAYER_H_ +#define SKY_COMPOSITOR_CONTAINER_LAYER_H_ + +#include "sky/compositor/layer.h" + +namespace sky { +namespace compositor { + +class ContainerLayer : public Layer { + public: + ContainerLayer(); + ~ContainerLayer() override; + + void Add(std::unique_ptr layer); + + void PaintChildren(GrContext* context, SkCanvas* canvas) const; + + private: + std::vector> layers_; + + DISALLOW_COPY_AND_ASSIGN(ContainerLayer); +}; + +} // namespace compositor +} // namespace sky + +#endif // SKY_COMPOSITOR_CONTAINER_LAYER_H_ diff --git a/sky/compositor/layer.cc b/sky/compositor/layer.cc index d43bbcd2ee9078df2a6550f08245a0fe87daedf2..85721e79d15a8ead810db6276c8ce7e211345c5e 100644 --- a/sky/compositor/layer.cc +++ b/sky/compositor/layer.cc @@ -7,6 +7,7 @@ #include "third_party/skia/include/core/SkColorFilter.h" namespace sky { +namespace compositor { Layer::Layer() { } @@ -14,117 +15,9 @@ 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->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 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 color_filter = - adoptRef(SkColorFilter::CreateModeFilter(color_, transfer_mode_)); - SkPaint paint; - paint.setColorFilter(color_filter.get()); - canvas->saveLayer(&paint_bounds(), &paint); - PaintChildren(canvas); - canvas->restore(); +SkMatrix Layer::model_view_matrix(const SkMatrix& model_matrix) const { + return model_matrix; } +} // namespace compositor } // namespace sky diff --git a/sky/compositor/layer.h b/sky/compositor/layer.h index e365475f413cc5b63496a6e3dd75ebc146c325f8..e919aa3bcf18717ada9304e474e4d8ee1bddf0ee 100644 --- a/sky/compositor/layer.h +++ b/sky/compositor/layer.h @@ -22,20 +22,27 @@ #include "third_party/skia/include/core/SkXfermode.h" namespace sky { -class ContainerLayer; +namespace compositor { +class ContainerLayer; class Layer { public: Layer(); virtual ~Layer(); - virtual void Paint(SkCanvas* canvas) = 0; + virtual void Paint(GrContext* context, SkCanvas* canvas) = 0; + + virtual SkMatrix model_view_matrix(const SkMatrix& model_matrix) const; 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; } + + void set_paint_bounds(const SkRect& paint_bounds) { + paint_bounds_ = paint_bounds; + } private: ContainerLayer* parent_; @@ -44,134 +51,7 @@ class Layer { DISALLOW_COPY_AND_ASSIGN(Layer); }; -class PictureLayer : public Layer { - public: - PictureLayer(); - ~PictureLayer() override; - - SkPicture* picture() const { return picture_.get(); } - - void Paint(SkCanvas* canvas) override; - - void set_offset(const SkPoint& offset) { offset_ = offset; } - void set_picture(PassRefPtr picture) { picture_ = picture; } - - private: - SkPoint offset_; - RefPtr picture_; - - DISALLOW_COPY_AND_ASSIGN(PictureLayer); -}; - -class ContainerLayer : public Layer { - public: - ContainerLayer(); - ~ContainerLayer() override; - - const std::vector>& layers() const { return layers_; } - - void Add(std::unique_ptr layer); - - void PaintChildren(SkCanvas* canvas); - - private: - std::vector> 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 compositor } // namespace sky #endif // SKY_COMPOSITOR_LAYER_H_ diff --git a/sky/compositor/layer_tree.cc b/sky/compositor/layer_tree.cc index 4f1f574539ca3882cad805cbd93f0e3d943ccb3c..c479203eec2a2b6c33c2bdd6b4c3b8b675cc7dbc 100644 --- a/sky/compositor/layer_tree.cc +++ b/sky/compositor/layer_tree.cc @@ -7,6 +7,7 @@ #include "sky/compositor/layer.h" namespace sky { +namespace compositor { LayerTree::LayerTree() { } @@ -14,4 +15,5 @@ LayerTree::LayerTree() { LayerTree::~LayerTree() { } +} // namespace compositor } // namespace sky diff --git a/sky/compositor/layer_tree.h b/sky/compositor/layer_tree.h index f3bde5a3f37efc22d6057830f1faa8bba0e98f49..fc7235720f6109405fd152e454a3fd675f14f4d4 100644 --- a/sky/compositor/layer_tree.h +++ b/sky/compositor/layer_tree.h @@ -12,6 +12,7 @@ #include "third_party/skia/include/core/SkSize.h" namespace sky { +namespace compositor { class LayerTree { public: @@ -19,20 +20,23 @@ class LayerTree { ~LayerTree(); Layer* root_layer() const { return root_layer_.get(); } + void set_root_layer(std::unique_ptr root_layer) { root_layer_ = std::move(root_layer); } const SkISize& frame_size() const { return frame_size_; } + void set_frame_size(const SkISize& frame_size) { frame_size_ = frame_size; } private: - SkISize frame_size_; // Physical pixels. + SkISize frame_size_; // Physical pixels. std::unique_ptr root_layer_; DISALLOW_COPY_AND_ASSIGN(LayerTree); }; +} // namespace compositor } // namespace sky #endif // SKY_COMPOSITOR_LAYER_TREE_H_ diff --git a/sky/compositor/opacity_layer.cc b/sky/compositor/opacity_layer.cc new file mode 100644 index 0000000000000000000000000000000000000000..6fd6504274305fbaf57fb32fd43f59252c5bc26d --- /dev/null +++ b/sky/compositor/opacity_layer.cc @@ -0,0 +1,28 @@ +// 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/opacity_layer.h" + +namespace sky { +namespace compositor { + +OpacityLayer::OpacityLayer() { +} + +OpacityLayer::~OpacityLayer() { +} + +void OpacityLayer::Paint(GrContext* context, SkCanvas* canvas) { + SkColor color = SkColorSetARGB(alpha_, 0, 0, 0); + RefPtr colorFilter = adoptRef( + SkColorFilter::CreateModeFilter(color, SkXfermode::kSrcOver_Mode)); + SkPaint paint; + paint.setColorFilter(colorFilter.get()); + canvas->saveLayer(&paint_bounds(), &paint); + PaintChildren(context, canvas); + canvas->restore(); +} + +} // namespace compositor +} // namespace sky diff --git a/sky/compositor/opacity_layer.h b/sky/compositor/opacity_layer.h new file mode 100644 index 0000000000000000000000000000000000000000..ca16693fd246d3f17c7dcb7c15dfadce67907468 --- /dev/null +++ b/sky/compositor/opacity_layer.h @@ -0,0 +1,31 @@ +// 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_OPACITY_LAYER_H_ +#define SKY_COMPOSITOR_OPACITY_LAYER_H_ + +#include "sky/compositor/container_layer.h" + +namespace sky { +namespace compositor { + +class OpacityLayer : public ContainerLayer { + public: + OpacityLayer(); + ~OpacityLayer() override; + + void set_alpha(int alpha) { alpha_ = alpha; } + + void Paint(GrContext* context, SkCanvas* canvas) override; + + private: + int alpha_; + + DISALLOW_COPY_AND_ASSIGN(OpacityLayer); +}; + +} // namespace compositor +} // namespace sky + +#endif // SKY_COMPOSITOR_OPACITY_LAYER_H_ diff --git a/sky/compositor/picture_layer.cc b/sky/compositor/picture_layer.cc new file mode 100644 index 0000000000000000000000000000000000000000..88f276797d72b1c8786cb408c50b1e4435df5623 --- /dev/null +++ b/sky/compositor/picture_layer.cc @@ -0,0 +1,110 @@ +// 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/picture_layer.h" +#include "base/logging.h" +#include "third_party/skia/include/gpu/GrContext.h" +#include "third_party/skia/include/core/SkSurface.h" + +namespace sky { +namespace compositor { + +PictureLayer::PictureLayer() : last_picture_id_(UINT32_MAX) { +} + +PictureLayer::~PictureLayer() { +} + +SkMatrix PictureLayer::model_view_matrix(const SkMatrix& model_matrix) const { + SkMatrix modelView = model_matrix; + modelView.postTranslate(offset_.x(), offset_.y()); + return modelView; +} + +static void ImageReleaseProc(SkImage::ReleaseContext texture) { + DCHECK(texture); + reinterpret_cast(texture)->unref(); +} + +static RefPtr ImageFromPicture(GrContext* context, + SkPicture* picture, + const SkRect& paintBounds) { + // Step 1: Create a texture from the context's texture provider + + GrSurfaceDesc desc; + desc.fWidth = paintBounds.width(); + desc.fHeight = paintBounds.height(); + desc.fFlags = kRenderTarget_GrSurfaceFlag; + desc.fConfig = kRGBA_8888_GrPixelConfig; + + GrTexture* texture = context->textureProvider()->createTexture(desc, true); + + if (!texture) { + // The texture provider could not allocate a texture backing. Render + // directly to the surface from the picture till the memory pressure + // subsides + return nullptr; + } + + // Step 2: Create a backend render target description for the created texture + + GrBackendTextureDesc backendDesc; + backendDesc.fConfig = desc.fConfig; + backendDesc.fWidth = desc.fWidth; + backendDesc.fHeight = desc.fHeight; + backendDesc.fSampleCnt = desc.fSampleCnt; + backendDesc.fFlags = kRenderTarget_GrBackendTextureFlag; + backendDesc.fConfig = desc.fConfig; + backendDesc.fTextureHandle = texture->getTextureHandle(); + + // Step 3: Render the picture into the offscreen texture + + GrRenderTarget* renderTarget = texture->asRenderTarget(); + DCHECK(renderTarget); + + PassRefPtr surface = + adoptRef(SkSurface::NewRenderTargetDirect(renderTarget)); + DCHECK(surface); + + SkCanvas* canvas = surface->getCanvas(); + DCHECK(canvas); + + canvas->drawPicture(picture); + + // Step 4: Create an image representation from the texture + + RefPtr image = adoptRef(SkImage::NewFromTexture( + context, backendDesc, kPremul_SkAlphaType, &ImageReleaseProc, texture)); + return image; +} + +void PictureLayer::Paint(GrContext* context, SkCanvas* canvas) { + DCHECK(picture_); + + if (last_picture_id_ == picture_->uniqueID()) { + // The last picture painted was the same as this one. Cache this into an + // offscreen surface and render that instead + if (!cached_image_) { + // Generate the cached image + cached_image_ = ImageFromPicture(context, picture_.get(), paint_bounds()); + } + } else { + // Release the cached image if one is present + cached_image_.release(); + } + + last_picture_id_ = picture_->uniqueID(); + + if (cached_image_) { + canvas->drawImage(cached_image_.get(), offset_.x(), offset_.y()); + } else { + canvas->save(); + canvas->translate(offset_.x(), offset_.y()); + canvas->drawPicture(picture_.get()); + canvas->restore(); + } +} + +} // namespace compositor +} // namespace sky diff --git a/sky/compositor/picture_layer.h b/sky/compositor/picture_layer.h new file mode 100644 index 0000000000000000000000000000000000000000..706e88bd56534f6e3010a687429c45851b8792b4 --- /dev/null +++ b/sky/compositor/picture_layer.h @@ -0,0 +1,38 @@ +// 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_PICTURE_LAYER_H_ +#define SKY_COMPOSITOR_PICTURE_LAYER_H_ + +#include "sky/compositor/layer.h" + +namespace sky { +namespace compositor { + +class PictureLayer : public Layer { + public: + PictureLayer(); + ~PictureLayer() override; + + void set_offset(const SkPoint& offset) { offset_ = offset; } + + void set_picture(PassRefPtr picture) { picture_ = picture; } + + SkMatrix model_view_matrix(const SkMatrix& model_matrix) const override; + + void Paint(GrContext* context, SkCanvas* canvas) override; + + private: + SkPoint offset_; + RefPtr picture_; + RefPtr cached_image_; + uint32_t last_picture_id_; + + DISALLOW_COPY_AND_ASSIGN(PictureLayer); +}; + +} // namespace compositor +} // namespace sky + +#endif // SKY_COMPOSITOR_PICTURE_LAYER_H_ diff --git a/sky/compositor/transform_layer.cc b/sky/compositor/transform_layer.cc new file mode 100644 index 0000000000000000000000000000000000000000..9df6cd4ff90e75eb929a2ea8f4c3af8b8c4f23ed --- /dev/null +++ b/sky/compositor/transform_layer.cc @@ -0,0 +1,30 @@ +// 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/transform_layer.h" + +namespace sky { +namespace compositor { + +TransformLayer::TransformLayer() { +} + +TransformLayer::~TransformLayer() { +} + +SkMatrix TransformLayer::model_view_matrix(const SkMatrix& model_matrix) const { + SkMatrix modelView = model_matrix; + modelView.postConcat(transform_); + return modelView; +} + +void TransformLayer::Paint(GrContext* context, SkCanvas* canvas) { + canvas->save(); + canvas->concat(transform_); + PaintChildren(context, canvas); + canvas->restore(); +} + +} // namespace compositor +} // namespace sky diff --git a/sky/compositor/transform_layer.h b/sky/compositor/transform_layer.h new file mode 100644 index 0000000000000000000000000000000000000000..57018bd6c53c07488df27602cc877b01c66fbcc0 --- /dev/null +++ b/sky/compositor/transform_layer.h @@ -0,0 +1,33 @@ +// 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_TRANSFORM_LAYER_H_ +#define SKY_COMPOSITOR_TRANSFORM_LAYER_H_ + +#include "sky/compositor/container_layer.h" + +namespace sky { +namespace compositor { + +class TransformLayer : public ContainerLayer { + public: + TransformLayer(); + ~TransformLayer() override; + + void set_transform(const SkMatrix& transform) { transform_ = transform; } + + SkMatrix model_view_matrix(const SkMatrix& model_matrix) const override; + + void Paint(GrContext* context, SkCanvas* canvas) override; + + private: + SkMatrix transform_; + + DISALLOW_COPY_AND_ASSIGN(TransformLayer); +}; + +} // namespace compositor +} // namespace sky + +#endif // SKY_COMPOSITOR_TRANSFORM_LAYER_H_ diff --git a/sky/engine/core/compositing/Scene.cpp b/sky/engine/core/compositing/Scene.cpp index f81cf261e64e84fa52b7bf75fa80a7d6baae2cd5..f6fbe38d2163d8b44e2342d6408663f2adfad245 100644 --- a/sky/engine/core/compositing/Scene.cpp +++ b/sky/engine/core/compositing/Scene.cpp @@ -9,23 +9,20 @@ namespace blink { -PassRefPtr Scene::create(std::unique_ptr rootLayer) -{ - ASSERT(rootLayer); - return adoptRef(new Scene(std::move(rootLayer))); +PassRefPtr Scene::create( + std::unique_ptr rootLayer) { + ASSERT(rootLayer); + return adoptRef(new Scene(std::move(rootLayer))); } -Scene::Scene(std::unique_ptr rootLayer) - : m_layerTree(new sky::LayerTree()) -{ - m_layerTree->set_root_layer(std::move(rootLayer)); +Scene::Scene(std::unique_ptr rootLayer) + : m_layerTree(new sky::compositor::LayerTree()) { + m_layerTree->set_root_layer(std::move(rootLayer)); } -Scene::~Scene() -{ -} +Scene::~Scene() {} -std::unique_ptr Scene::takeLayerTree() { +std::unique_ptr Scene::takeLayerTree() { return std::move(m_layerTree); } diff --git a/sky/engine/core/compositing/Scene.h b/sky/engine/core/compositing/Scene.h index 94e4a5aa20978536163d5488d2a568066b0e1ec4..d8b9b74ec23e76d5fc7e7f0eb2770440cfa0ae37 100644 --- a/sky/engine/core/compositing/Scene.h +++ b/sky/engine/core/compositing/Scene.h @@ -17,17 +17,19 @@ namespace blink { class Scene : public RefCounted, public DartWrappable { - DEFINE_WRAPPERTYPEINFO(); -public: - ~Scene() override; - static PassRefPtr create(std::unique_ptr rootLayer); + DEFINE_WRAPPERTYPEINFO(); - std::unique_ptr takeLayerTree(); + public: + ~Scene() override; + static PassRefPtr create( + std::unique_ptr rootLayer); -private: - explicit Scene(std::unique_ptr rootLayer); + std::unique_ptr takeLayerTree(); - std::unique_ptr m_layerTree; + private: + explicit Scene(std::unique_ptr rootLayer); + + std::unique_ptr m_layerTree; }; } // namespace blink diff --git a/sky/engine/core/compositing/SceneBuilder.cpp b/sky/engine/core/compositing/SceneBuilder.cpp index 82def61dd038bac8050264cdc30dc1f35bd65529..d41b9b7798acef37fa2655db3d5dd58c786e999f 100644 --- a/sky/engine/core/compositing/SceneBuilder.cpp +++ b/sky/engine/core/compositing/SceneBuilder.cpp @@ -6,6 +6,14 @@ #include "sky/engine/core/painting/Matrix.h" #include "third_party/skia/include/core/SkColorFilter.h" +#include "sky/compositor/transform_layer.h" +#include "sky/compositor/clip_path_layer.h" +#include "sky/compositor/clip_rect_layer.h" +#include "sky/compositor/clip_rrect_layer.h" +#include "sky/compositor/color_filter_layer.h" +#include "sky/compositor/container_layer.h" +#include "sky/compositor/opacity_layer.h" +#include "sky/compositor/picture_layer.h" namespace blink { @@ -24,35 +32,35 @@ void SceneBuilder::pushTransform(const Float32List& matrix4, ExceptionState& es) SkMatrix sk_matrix = toSkMatrix(matrix4, es); if (es.had_exception()) return; - std::unique_ptr layer(new sky::TransformLayer()); + std::unique_ptr layer(new sky::compositor::TransformLayer()); layer->set_transform(sk_matrix); addLayer(std::move(layer)); } void SceneBuilder::pushClipRect(const Rect& rect) { - std::unique_ptr layer(new sky::ClipRectLayer()); + std::unique_ptr layer(new sky::compositor::ClipRectLayer()); layer->set_clip_rect(rect.sk_rect); addLayer(std::move(layer)); } void SceneBuilder::pushClipRRect(const RRect* rrect, const Rect& bounds) { - std::unique_ptr layer(new sky::ClipRRectLayer()); + std::unique_ptr layer(new sky::compositor::ClipRRectLayer()); layer->set_clip_rrect(rrect->rrect()); addLayer(std::move(layer)); } void SceneBuilder::pushClipPath(const CanvasPath* path, const Rect& bounds) { - std::unique_ptr layer(new sky::ClipPathLayer()); + std::unique_ptr layer(new sky::compositor::ClipPathLayer()); layer->set_clip_path(path->path()); addLayer(std::move(layer)); } void SceneBuilder::pushOpacity(int alpha, const Rect& bounds) { - std::unique_ptr layer(new sky::OpacityLayer()); + std::unique_ptr layer(new sky::compositor::OpacityLayer()); layer->set_paint_bounds(bounds.sk_rect); layer->set_alpha(alpha); addLayer(std::move(layer)); @@ -60,14 +68,14 @@ void SceneBuilder::pushOpacity(int alpha, const Rect& bounds) void SceneBuilder::pushColorFilter(SkColor color, SkXfermode::Mode transferMode, const Rect& bounds) { - std::unique_ptr layer(new sky::ColorFilterLayer()); + std::unique_ptr layer(new sky::compositor::ColorFilterLayer()); layer->set_paint_bounds(bounds.sk_rect); layer->set_color(color); layer->set_transfer_mode(transferMode); addLayer(std::move(layer)); } -void SceneBuilder::addLayer(std::unique_ptr layer) +void SceneBuilder::addLayer(std::unique_ptr layer) { DCHECK(layer); if (!m_rootLayer) { @@ -79,7 +87,7 @@ void SceneBuilder::addLayer(std::unique_ptr layer) } if (!m_currentLayer) return; - sky::ContainerLayer* newLayer = layer.get(); + sky::compositor::ContainerLayer* newLayer = layer.get(); m_currentLayer->Add(std::move(layer)); m_currentLayer = newLayer; } @@ -95,7 +103,7 @@ void SceneBuilder::addPicture(const Offset& offset, Picture* picture, const Rect { if (!m_currentLayer) return; - std::unique_ptr layer(new sky::PictureLayer()); + std::unique_ptr layer(new sky::compositor::PictureLayer()); layer->set_offset(SkPoint::Make(offset.sk_size.width(), offset.sk_size.height())); layer->set_picture(picture->toSkia()); layer->set_paint_bounds(paintBounds.sk_rect); diff --git a/sky/engine/core/compositing/SceneBuilder.h b/sky/engine/core/compositing/SceneBuilder.h index 099d1f5b476fd8f11525b1fb6d83602d97b183fc..053ac62bb45f611a8f1af5aefb699ef9b54558f4 100644 --- a/sky/engine/core/compositing/SceneBuilder.h +++ b/sky/engine/core/compositing/SceneBuilder.h @@ -48,11 +48,11 @@ public: private: explicit SceneBuilder(const Rect& bounds); - void addLayer(std::unique_ptr layer); + void addLayer(std::unique_ptr layer); SkRect m_rootPaintBounds; - std::unique_ptr m_rootLayer; - sky::ContainerLayer* m_currentLayer; + std::unique_ptr m_rootLayer; + sky::compositor::ContainerLayer* m_currentLayer; }; } // namespace blink diff --git a/sky/engine/core/view/View.cpp b/sky/engine/core/view/View.cpp index dcb4b39407f7e2659e351d6109978aa9af3d217f..9089d389e077244db6652a191060c20e86f95914 100644 --- a/sky/engine/core/view/View.cpp +++ b/sky/engine/core/view/View.cpp @@ -65,8 +65,8 @@ void View::handleInputEvent(PassRefPtr event) m_eventCallback->handleEvent(event.get()); } -std::unique_ptr View::beginFrame(base::TimeTicks frameTime) -{ +std::unique_ptr View::beginFrame( + base::TimeTicks frameTime) { if (!m_frameCallback) return nullptr; double frameTimeMS = (frameTime - base::TimeTicks()).InMillisecondsF(); diff --git a/sky/engine/core/view/View.h b/sky/engine/core/view/View.h index aa89a147b12c3a733fa1d3f708b51bab17fc994f..b5219594ff639643104406f460effa61e3b3b758 100644 --- a/sky/engine/core/view/View.h +++ b/sky/engine/core/view/View.h @@ -47,7 +47,8 @@ public: void setDisplayMetrics(const SkyDisplayMetrics& metrics); void handleInputEvent(PassRefPtr event); - std::unique_ptr beginFrame(base::TimeTicks frameTime); + std::unique_ptr beginFrame( + base::TimeTicks frameTime); private: explicit View(const base::Closure& scheduleFrameCallback); diff --git a/sky/engine/public/sky/sky_view.cc b/sky/engine/public/sky/sky_view.cc index 54809acb9d2d3035da2fa0a40b2b7a839005d2d5..21ea2fc13cb2fabe90afe527f85eb3d6589ba95f 100644 --- a/sky/engine/public/sky/sky_view.cc +++ b/sky/engine/public/sky/sky_view.cc @@ -66,7 +66,8 @@ void SkyView::RunFromSnapshot(const WebString& name, dart_controller_->RunFromSnapshot(snapshot.Pass()); } -std::unique_ptr SkyView::BeginFrame(base::TimeTicks frame_time) { +std::unique_ptr SkyView::BeginFrame( + base::TimeTicks frame_time) { return view_->beginFrame(frame_time); } diff --git a/sky/engine/public/sky/sky_view.h b/sky/engine/public/sky/sky_view.h index a5b023102a5335cf705582f99f6bed944b618311..38d595b09a8dcaf1d3dcdb970a258fad5ea736fd 100644 --- a/sky/engine/public/sky/sky_view.h +++ b/sky/engine/public/sky/sky_view.h @@ -35,7 +35,8 @@ class SkyView { const SkyDisplayMetrics& display_metrics() const { return display_metrics_; } void SetDisplayMetrics(const SkyDisplayMetrics& metrics); - std::unique_ptr BeginFrame(base::TimeTicks frame_time); + std::unique_ptr BeginFrame( + base::TimeTicks frame_time); void CreateView(const String& name); diff --git a/sky/shell/gpu/rasterizer.cc b/sky/shell/gpu/rasterizer.cc index ec899112ac90deb02c33c95b1fd17544ebb444e6..b4b2001b52ccd8e25f9d6bf26fc37fa3b2501ba1 100644 --- a/sky/shell/gpu/rasterizer.cc +++ b/sky/shell/gpu/rasterizer.cc @@ -53,7 +53,7 @@ void Rasterizer::OnAcceleratedWidgetAvailable(gfx::AcceleratedWidget widget) { CHECK(surface_) << "GLSurface required."; } -void Rasterizer::Draw(scoped_ptr layer_tree) { +void Rasterizer::Draw(scoped_ptr layer_tree) { TRACE_EVENT0("sky", "Rasterizer::Draw"); if (!surface_) @@ -71,7 +71,7 @@ void Rasterizer::Draw(scoped_ptr layer_tree) { SkCanvas* canvas = ganesh_surface_->canvas(); canvas->clear(SK_ColorBLACK); - layer_tree->root_layer()->Paint(canvas); + layer_tree->root_layer()->Paint(ganesh_context_->gr(), canvas); canvas->flush(); surface_->SwapBuffers(); diff --git a/sky/shell/gpu/rasterizer.h b/sky/shell/gpu/rasterizer.h index 21dcd0da2f49563a6ce435e21585772bb99ca5a7..8a69eb9b2c891ce718b804a3d29ed06f93ef8c84 100644 --- a/sky/shell/gpu/rasterizer.h +++ b/sky/shell/gpu/rasterizer.h @@ -34,7 +34,7 @@ class Rasterizer : public GPUDelegate { void OnAcceleratedWidgetAvailable(gfx::AcceleratedWidget widget) override; void OnOutputSurfaceDestroyed() override; - void Draw(scoped_ptr layer_tree) override; + void Draw(scoped_ptr layer_tree) override; private: void EnsureGLContext(); diff --git a/sky/shell/gpu_delegate.h b/sky/shell/gpu_delegate.h index b3bbe549fe21eade069d283e85f6eaab7a77d035..e0d67ecd5b024160dcd5572d6edb12b0959b34a8 100644 --- a/sky/shell/gpu_delegate.h +++ b/sky/shell/gpu_delegate.h @@ -21,7 +21,7 @@ class GPUDelegate { public: virtual void OnAcceleratedWidgetAvailable(gfx::AcceleratedWidget widget) = 0; virtual void OnOutputSurfaceDestroyed() = 0; - virtual void Draw(scoped_ptr layer_tree) = 0; + virtual void Draw(scoped_ptr layer_tree) = 0; protected: virtual ~GPUDelegate(); diff --git a/sky/shell/ui/animator.cc b/sky/shell/ui/animator.cc index 9ee517dfdd0f403239c4c0a4808ba0eb9db0564b..f639f1d255f2696fb99ff80999225348f7285ca1 100644 --- a/sky/shell/ui/animator.cc +++ b/sky/shell/ui/animator.cc @@ -71,7 +71,7 @@ void Animator::BeginFrame(int64_t time_stamp) { base::TimeTicks frame_time = time_stamp ? base::TimeTicks::FromInternalValue(time_stamp) : base::TimeTicks::Now(); - scoped_ptr layer_tree = + scoped_ptr layer_tree = make_scoped_ptr(engine_->BeginFrame(frame_time).release()); if (!layer_tree) { diff --git a/sky/shell/ui/engine.cc b/sky/shell/ui/engine.cc index f3f4251478d843c8d299812c3365fbc7ae6ff20e..29c134beb836c8f1196a017101a241aa738da363 100644 --- a/sky/shell/ui/engine.cc +++ b/sky/shell/ui/engine.cc @@ -96,13 +96,15 @@ void Engine::Init() { blink::initialize(g_platform_impl); } -std::unique_ptr Engine::BeginFrame(base::TimeTicks frame_time) { +std::unique_ptr Engine::BeginFrame( + base::TimeTicks frame_time) { TRACE_EVENT0("sky", "Engine::BeginFrame"); if (!sky_view_) return nullptr; - std::unique_ptr layer_tree = sky_view_->BeginFrame(frame_time); + std::unique_ptr layer_tree = + sky_view_->BeginFrame(frame_time); if (layer_tree) { layer_tree->set_frame_size(SkISize::Make(physical_size_.width(), physical_size_.height())); diff --git a/sky/shell/ui/engine.h b/sky/shell/ui/engine.h index 794089f7a4219f10fdc4c0f87d1421fa50c46dd0..438fa2f923264a6c0a8b5d51e2721a49dfeb9872 100644 --- a/sky/shell/ui/engine.h +++ b/sky/shell/ui/engine.h @@ -55,7 +55,7 @@ class Engine : public UIDelegate, static void Init(); - std::unique_ptr BeginFrame(base::TimeTicks frame_time); + std::unique_ptr BeginFrame(base::TimeTicks frame_time); void StartDartTracing(); void StopDartTracing(mojo::ScopedDataPipeProducerHandle producer);