| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "cc/playback/compositing_display_item.h" | 5 #include "cc/playback/compositing_display_item.h" |
| 6 | 6 |
| 7 #include "base/strings/stringprintf.h" | 7 #include "base/strings/stringprintf.h" |
| 8 #include "base/trace_event/trace_event_argument.h" | 8 #include "base/trace_event/trace_event_argument.h" |
| 9 #include "cc/proto/display_item.pb.h" | 9 #include "cc/proto/display_item.pb.h" |
| 10 #include "cc/proto/gfx_conversions.h" | 10 #include "cc/proto/gfx_conversions.h" |
| 11 #include "cc/proto/skia_conversions.h" | 11 #include "cc/proto/skia_conversions.h" |
| 12 #include "third_party/skia/include/core/SkCanvas.h" | 12 #include "third_party/skia/include/core/SkCanvas.h" |
| 13 #include "third_party/skia/include/core/SkData.h" | 13 #include "third_party/skia/include/core/SkData.h" |
| 14 #include "third_party/skia/include/core/SkFlattenable.h" | 14 #include "third_party/skia/include/core/SkFlattenable.h" |
| 15 #include "third_party/skia/include/core/SkFlattenableSerialization.h" | 15 #include "third_party/skia/include/core/SkFlattenableSerialization.h" |
| 16 #include "third_party/skia/include/core/SkPaint.h" | 16 #include "third_party/skia/include/core/SkPaint.h" |
| 17 #include "third_party/skia/include/core/SkXfermode.h" | 17 #include "third_party/skia/include/core/SkXfermode.h" |
| 18 #include "ui/gfx/skia_util.h" | 18 #include "ui/gfx/skia_util.h" |
| 19 | 19 |
| 20 namespace cc { | 20 namespace cc { |
| 21 | 21 |
| 22 CompositingDisplayItem::CompositingDisplayItem(uint8_t alpha, | 22 CompositingDisplayItem::CompositingDisplayItem() { |
| 23 SkXfermode::Mode xfermode, | |
| 24 SkRect* bounds, | |
| 25 skia::RefPtr<SkColorFilter> cf) { | |
| 26 SetNew(alpha, xfermode, bounds, std::move(cf)); | |
| 27 } | 23 } |
| 28 | 24 |
| 29 CompositingDisplayItem::CompositingDisplayItem( | 25 CompositingDisplayItem::~CompositingDisplayItem() { |
| 30 const proto::DisplayItem& proto) { | 26 } |
| 27 |
| 28 void CompositingDisplayItem::SetNew(uint8_t alpha, |
| 29 SkXfermode::Mode xfermode, |
| 30 SkRect* bounds, |
| 31 skia::RefPtr<SkColorFilter> cf) { |
| 32 alpha_ = alpha; |
| 33 xfermode_ = xfermode; |
| 34 has_bounds_ = !!bounds; |
| 35 if (bounds) |
| 36 bounds_ = SkRect(*bounds); |
| 37 color_filter_ = cf; |
| 38 |
| 39 // TODO(pdr): Include color_filter's memory here. |
| 40 size_t external_memory_usage = 0; |
| 41 DisplayItem::SetNew(true /* suitable_for_gpu_raster */, 1 /* op_count */, |
| 42 external_memory_usage); |
| 43 } |
| 44 |
| 45 void CompositingDisplayItem::ToProtobuf(proto::DisplayItem* proto) const { |
| 46 proto->set_type(proto::DisplayItem::Type_Compositing); |
| 47 |
| 48 proto::CompositingDisplayItem* details = proto->mutable_compositing_item(); |
| 49 details->set_alpha(static_cast<uint32_t>(alpha_)); |
| 50 details->set_mode(SkXfermodeModeToProto(xfermode_)); |
| 51 if (has_bounds_) |
| 52 RectFToProto(gfx::SkRectToRectF(bounds_), details->mutable_bounds()); |
| 53 |
| 54 if (color_filter_) { |
| 55 skia::RefPtr<SkData> data = |
| 56 skia::AdoptRef(SkValidatingSerializeFlattenable(color_filter_.get())); |
| 57 if (data->size() > 0) |
| 58 details->set_color_filter(data->data(), data->size()); |
| 59 } |
| 60 } |
| 61 |
| 62 void CompositingDisplayItem::FromProtobuf(const proto::DisplayItem& proto) { |
| 31 DCHECK_EQ(proto::DisplayItem::Type_Compositing, proto.type()); | 63 DCHECK_EQ(proto::DisplayItem::Type_Compositing, proto.type()); |
| 32 | 64 |
| 33 const proto::CompositingDisplayItem& details = proto.compositing_item(); | 65 const proto::CompositingDisplayItem& details = proto.compositing_item(); |
| 34 uint8_t alpha = static_cast<uint8_t>(details.alpha()); | 66 uint8_t alpha = static_cast<uint8_t>(details.alpha()); |
| 35 SkXfermode::Mode xfermode = SkXfermodeModeFromProto(details.mode()); | 67 SkXfermode::Mode xfermode = SkXfermodeModeFromProto(details.mode()); |
| 36 scoped_ptr<SkRect> bounds; | 68 scoped_ptr<SkRect> bounds; |
| 37 if (details.has_bounds()) { | 69 if (details.has_bounds()) { |
| 38 bounds.reset( | 70 bounds.reset( |
| 39 new SkRect(gfx::RectFToSkRect(ProtoToRectF(details.bounds())))); | 71 new SkRect(gfx::RectFToSkRect(ProtoToRectF(details.bounds())))); |
| 40 } | 72 } |
| 41 | 73 |
| 42 skia::RefPtr<SkColorFilter> filter; | 74 skia::RefPtr<SkColorFilter> filter; |
| 43 if (details.has_color_filter()) { | 75 if (details.has_color_filter()) { |
| 44 SkFlattenable* flattenable = SkValidatingDeserializeFlattenable( | 76 SkFlattenable* flattenable = SkValidatingDeserializeFlattenable( |
| 45 details.color_filter().c_str(), details.color_filter().size(), | 77 details.color_filter().c_str(), details.color_filter().size(), |
| 46 SkColorFilter::GetFlattenableType()); | 78 SkColorFilter::GetFlattenableType()); |
| 47 filter = skia::AdoptRef(static_cast<SkColorFilter*>(flattenable)); | 79 filter = skia::AdoptRef(static_cast<SkColorFilter*>(flattenable)); |
| 48 } | 80 } |
| 49 | 81 |
| 50 SetNew(alpha, xfermode, bounds.get(), std::move(filter)); | 82 SetNew(alpha, xfermode, bounds.get(), std::move(filter)); |
| 51 } | 83 } |
| 52 | 84 |
| 53 CompositingDisplayItem::~CompositingDisplayItem() { | |
| 54 } | |
| 55 | |
| 56 void CompositingDisplayItem::SetNew(uint8_t alpha, | |
| 57 SkXfermode::Mode xfermode, | |
| 58 SkRect* bounds, | |
| 59 skia::RefPtr<SkColorFilter> cf) { | |
| 60 alpha_ = alpha; | |
| 61 xfermode_ = xfermode; | |
| 62 has_bounds_ = !!bounds; | |
| 63 if (bounds) | |
| 64 bounds_ = SkRect(*bounds); | |
| 65 color_filter_ = std::move(cf); | |
| 66 } | |
| 67 | |
| 68 void CompositingDisplayItem::ToProtobuf(proto::DisplayItem* proto) const { | |
| 69 proto->set_type(proto::DisplayItem::Type_Compositing); | |
| 70 | |
| 71 proto::CompositingDisplayItem* details = proto->mutable_compositing_item(); | |
| 72 details->set_alpha(static_cast<uint32_t>(alpha_)); | |
| 73 details->set_mode(SkXfermodeModeToProto(xfermode_)); | |
| 74 if (has_bounds_) | |
| 75 RectFToProto(gfx::SkRectToRectF(bounds_), details->mutable_bounds()); | |
| 76 | |
| 77 if (color_filter_) { | |
| 78 skia::RefPtr<SkData> data = | |
| 79 skia::AdoptRef(SkValidatingSerializeFlattenable(color_filter_.get())); | |
| 80 if (data->size() > 0) | |
| 81 details->set_color_filter(data->data(), data->size()); | |
| 82 } | |
| 83 } | |
| 84 | |
| 85 void CompositingDisplayItem::Raster( | 85 void CompositingDisplayItem::Raster( |
| 86 SkCanvas* canvas, | 86 SkCanvas* canvas, |
| 87 const gfx::Rect& canvas_target_playback_rect, | 87 const gfx::Rect& canvas_target_playback_rect, |
| 88 SkPicture::AbortCallback* callback) const { | 88 SkPicture::AbortCallback* callback) const { |
| 89 SkPaint paint; | 89 SkPaint paint; |
| 90 paint.setXfermodeMode(xfermode_); | 90 paint.setXfermodeMode(xfermode_); |
| 91 paint.setAlpha(alpha_); | 91 paint.setAlpha(alpha_); |
| 92 paint.setColorFilter(color_filter_.get()); | 92 paint.setColorFilter(color_filter_.get()); |
| 93 canvas->saveLayer(has_bounds_ ? &bounds_ : nullptr, &paint); | 93 canvas->saveLayer(has_bounds_ ? &bounds_ : nullptr, &paint); |
| 94 } | 94 } |
| 95 | 95 |
| 96 void CompositingDisplayItem::AsValueInto( | 96 void CompositingDisplayItem::AsValueInto( |
| 97 const gfx::Rect& visual_rect, | 97 const gfx::Rect& visual_rect, |
| 98 base::trace_event::TracedValue* array) const { | 98 base::trace_event::TracedValue* array) const { |
| 99 array->AppendString(base::StringPrintf( | 99 array->AppendString(base::StringPrintf( |
| 100 "CompositingDisplayItem alpha: %d, xfermode: %d, visualRect: [%s]", | 100 "CompositingDisplayItem alpha: %d, xfermode: %d, visualRect: [%s]", |
| 101 alpha_, xfermode_, visual_rect.ToString().c_str())); | 101 alpha_, xfermode_, visual_rect.ToString().c_str())); |
| 102 if (has_bounds_) | 102 if (has_bounds_) |
| 103 array->AppendString(base::StringPrintf( | 103 array->AppendString(base::StringPrintf( |
| 104 ", bounds: [%f, %f, %f, %f]", static_cast<float>(bounds_.x()), | 104 ", bounds: [%f, %f, %f, %f]", static_cast<float>(bounds_.x()), |
| 105 static_cast<float>(bounds_.y()), static_cast<float>(bounds_.width()), | 105 static_cast<float>(bounds_.y()), static_cast<float>(bounds_.width()), |
| 106 static_cast<float>(bounds_.height()))); | 106 static_cast<float>(bounds_.height()))); |
| 107 } | 107 } |
| 108 | 108 |
| 109 size_t CompositingDisplayItem::ExternalMemoryUsage() const { | 109 EndCompositingDisplayItem::EndCompositingDisplayItem() { |
| 110 // TODO(pdr): Include color_filter's memory here. | 110 DisplayItem::SetNew(true /* suitable_for_gpu_raster */, 0 /* op_count */, |
| 111 return 0; | 111 0 /* external_memory_usage */); |
| 112 } | |
| 113 | |
| 114 EndCompositingDisplayItem::EndCompositingDisplayItem() {} | |
| 115 | |
| 116 EndCompositingDisplayItem::EndCompositingDisplayItem( | |
| 117 const proto::DisplayItem& proto) { | |
| 118 DCHECK_EQ(proto::DisplayItem::Type_EndCompositing, proto.type()); | |
| 119 } | 112 } |
| 120 | 113 |
| 121 EndCompositingDisplayItem::~EndCompositingDisplayItem() { | 114 EndCompositingDisplayItem::~EndCompositingDisplayItem() { |
| 122 } | 115 } |
| 123 | 116 |
| 124 void EndCompositingDisplayItem::ToProtobuf(proto::DisplayItem* proto) const { | 117 void EndCompositingDisplayItem::ToProtobuf(proto::DisplayItem* proto) const { |
| 125 proto->set_type(proto::DisplayItem::Type_EndCompositing); | 118 proto->set_type(proto::DisplayItem::Type_EndCompositing); |
| 126 } | 119 } |
| 127 | 120 |
| 121 void EndCompositingDisplayItem::FromProtobuf(const proto::DisplayItem& proto) { |
| 122 DCHECK_EQ(proto::DisplayItem::Type_EndCompositing, proto.type()); |
| 123 } |
| 124 |
| 128 void EndCompositingDisplayItem::Raster( | 125 void EndCompositingDisplayItem::Raster( |
| 129 SkCanvas* canvas, | 126 SkCanvas* canvas, |
| 130 const gfx::Rect& canvas_target_playback_rect, | 127 const gfx::Rect& canvas_target_playback_rect, |
| 131 SkPicture::AbortCallback* callback) const { | 128 SkPicture::AbortCallback* callback) const { |
| 132 canvas->restore(); | 129 canvas->restore(); |
| 133 } | 130 } |
| 134 | 131 |
| 135 void EndCompositingDisplayItem::AsValueInto( | 132 void EndCompositingDisplayItem::AsValueInto( |
| 136 const gfx::Rect& visual_rect, | 133 const gfx::Rect& visual_rect, |
| 137 base::trace_event::TracedValue* array) const { | 134 base::trace_event::TracedValue* array) const { |
| 138 array->AppendString( | 135 array->AppendString( |
| 139 base::StringPrintf("EndCompositingDisplayItem visualRect: [%s]", | 136 base::StringPrintf("EndCompositingDisplayItem visualRect: [%s]", |
| 140 visual_rect.ToString().c_str())); | 137 visual_rect.ToString().c_str())); |
| 141 } | 138 } |
| 142 | 139 |
| 143 size_t EndCompositingDisplayItem::ExternalMemoryUsage() const { | |
| 144 return 0; | |
| 145 } | |
| 146 | |
| 147 } // namespace cc | 140 } // namespace cc |
| OLD | NEW |