Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / cc / playback / filter_display_item.cc
blobc155b68a64259e97e466eec07a940ea15f39de6a
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "cc/playback/filter_display_item.h"
7 #include "base/strings/stringprintf.h"
8 #include "base/trace_event/trace_event_argument.h"
9 #include "cc/output/render_surface_filters.h"
10 #include "skia/ext/refptr.h"
11 #include "third_party/skia/include/core/SkCanvas.h"
12 #include "third_party/skia/include/core/SkImageFilter.h"
13 #include "third_party/skia/include/core/SkPaint.h"
14 #include "third_party/skia/include/core/SkXfermode.h"
15 #include "ui/gfx/skia_util.h"
17 namespace cc {
19 FilterDisplayItem::FilterDisplayItem() {
22 FilterDisplayItem::~FilterDisplayItem() {
25 void FilterDisplayItem::SetNew(const FilterOperations& filters,
26 const gfx::RectF& bounds) {
27 filters_ = filters;
28 bounds_ = bounds;
30 // FilterOperations doesn't expose its capacity, but size is probably good
31 // enough.
32 size_t external_memory_usage = filters_.size() * sizeof(filters_.at(0));
33 DisplayItem::SetNew(true /* suitable_for_gpu_raster */, 1 /* op_count */,
34 external_memory_usage);
37 void FilterDisplayItem::Raster(SkCanvas* canvas,
38 const gfx::Rect& canvas_target_playback_rect,
39 SkPicture::AbortCallback* callback) const {
40 canvas->save();
41 canvas->translate(bounds_.x(), bounds_.y());
43 skia::RefPtr<SkImageFilter> image_filter =
44 RenderSurfaceFilters::BuildImageFilter(
45 filters_, gfx::SizeF(bounds_.width(), bounds_.height()));
46 #ifdef SK_SAVE_LAYER_BOUNDS_ARE_FILTERED
47 // TODO(senorblanco): remove this once callsites updated (crbug.com/525748)
48 SkRect boundaries;
49 image_filter->computeFastBounds(
50 SkRect::MakeWH(bounds_.width(), bounds_.height()), &boundaries);
51 #else
52 SkRect boundaries = SkRect::MakeWH(bounds_.width(), bounds_.height());
53 #endif
55 SkPaint paint;
56 paint.setXfermodeMode(SkXfermode::kSrcOver_Mode);
57 paint.setImageFilter(image_filter.get());
58 canvas->saveLayer(&boundaries, &paint);
60 canvas->translate(-bounds_.x(), -bounds_.y());
63 void FilterDisplayItem::AsValueInto(
64 base::trace_event::TracedValue* array) const {
65 array->AppendString(base::StringPrintf("FilterDisplayItem bounds: [%s]",
66 bounds_.ToString().c_str()));
69 EndFilterDisplayItem::EndFilterDisplayItem() {
70 DisplayItem::SetNew(true /* suitable_for_gpu_raster */, 0 /* op_count */,
71 0 /* external_memory_usage */);
74 EndFilterDisplayItem::~EndFilterDisplayItem() {
77 void EndFilterDisplayItem::Raster(SkCanvas* canvas,
78 const gfx::Rect& canvas_target_playback_rect,
79 SkPicture::AbortCallback* callback) const {
80 canvas->restore();
81 canvas->restore();
84 void EndFilterDisplayItem::AsValueInto(
85 base::trace_event::TracedValue* array) const {
86 array->AppendString("EndFilterDisplayItem");
89 } // namespace cc