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/layers/surface_layer_impl.h"
7 #include "base/trace_event/trace_event_argument.h"
8 #include "cc/debug/debug_colors.h"
9 #include "cc/quads/solid_color_draw_quad.h"
10 #include "cc/quads/surface_draw_quad.h"
11 #include "cc/trees/occlusion.h"
15 SurfaceLayerImpl::SurfaceLayerImpl(LayerTreeImpl
* tree_impl
, int id
)
16 : LayerImpl(tree_impl
, id
), surface_scale_(0.f
) {
19 SurfaceLayerImpl::~SurfaceLayerImpl() {}
21 scoped_ptr
<LayerImpl
> SurfaceLayerImpl::CreateLayerImpl(
22 LayerTreeImpl
* tree_impl
) {
23 return SurfaceLayerImpl::Create(tree_impl
, id());
26 void SurfaceLayerImpl::SetSurfaceId(SurfaceId surface_id
) {
27 if (surface_id_
== surface_id
)
30 surface_id_
= surface_id
;
31 NoteLayerPropertyChanged();
34 void SurfaceLayerImpl::SetSurfaceScale(float scale
) {
35 if (surface_scale_
== scale
)
38 surface_scale_
= scale
;
39 NoteLayerPropertyChanged();
42 void SurfaceLayerImpl::SetSurfaceSize(const gfx::Size
& size
) {
43 if (surface_size_
== size
)
47 NoteLayerPropertyChanged();
50 void SurfaceLayerImpl::PushPropertiesTo(LayerImpl
* layer
) {
51 LayerImpl::PushPropertiesTo(layer
);
52 SurfaceLayerImpl
* layer_impl
= static_cast<SurfaceLayerImpl
*>(layer
);
54 layer_impl
->SetSurfaceId(surface_id_
);
55 layer_impl
->SetSurfaceSize(surface_size_
);
56 layer_impl
->SetSurfaceScale(surface_scale_
);
59 void SurfaceLayerImpl::AppendQuads(RenderPass
* render_pass
,
60 AppendQuadsData
* append_quads_data
) {
61 AppendRainbowDebugBorder(render_pass
);
63 SharedQuadState
* shared_quad_state
=
64 render_pass
->CreateAndAppendSharedQuadState();
65 PopulateScaledSharedQuadState(shared_quad_state
, surface_scale_
);
67 if (surface_id_
.is_null())
70 gfx::Rect
quad_rect(surface_size_
);
71 gfx::Rect visible_quad_rect
=
72 draw_properties().occlusion_in_content_space
.GetUnoccludedContentRect(
74 if (visible_quad_rect
.IsEmpty())
76 SurfaceDrawQuad
* quad
=
77 render_pass
->CreateAndAppendDrawQuad
<SurfaceDrawQuad
>();
78 quad
->SetNew(shared_quad_state
, quad_rect
, visible_quad_rect
, surface_id_
);
79 render_pass
->referenced_surfaces
.push_back(surface_id_
);
82 void SurfaceLayerImpl::GetDebugBorderProperties(SkColor
* color
,
84 *color
= DebugColors::SurfaceLayerBorderColor();
85 *width
= DebugColors::SurfaceLayerBorderWidth(layer_tree_impl());
88 void SurfaceLayerImpl::AppendRainbowDebugBorder(RenderPass
* render_pass
) {
89 if (!ShowDebugBorders())
92 SharedQuadState
* shared_quad_state
=
93 render_pass
->CreateAndAppendSharedQuadState();
94 PopulateSharedQuadState(shared_quad_state
);
98 GetDebugBorderProperties(&color
, &border_width
);
102 0x80ffa500, // Orange.
103 0x80ffff00, // Yellow.
104 0x80008000, // Green.
106 0x80ee82ee, // Violet.
108 const int kNumColors
= arraysize(colors
);
110 const int kStripeWidth
= 300;
111 const int kStripeHeight
= 300;
113 for (int i
= 0;; ++i
) {
114 // For horizontal lines.
115 int x
= kStripeWidth
* i
;
116 int width
= std::min(kStripeWidth
, bounds().width() - x
- 1);
118 // For vertical lines.
119 int y
= kStripeHeight
* i
;
120 int height
= std::min(kStripeHeight
, bounds().height() - y
- 1);
122 gfx::Rect
top(x
, 0, width
, border_width
);
123 gfx::Rect
bottom(x
, bounds().height() - border_width
, width
, border_width
);
124 gfx::Rect
left(0, y
, border_width
, height
);
125 gfx::Rect
right(bounds().width() - border_width
, y
, border_width
, height
);
127 if (top
.IsEmpty() && left
.IsEmpty())
130 if (!top
.IsEmpty()) {
131 bool force_anti_aliasing_off
= false;
132 SolidColorDrawQuad
* top_quad
=
133 render_pass
->CreateAndAppendDrawQuad
<SolidColorDrawQuad
>();
134 top_quad
->SetNew(shared_quad_state
, top
, top
, colors
[i
% kNumColors
],
135 force_anti_aliasing_off
);
137 SolidColorDrawQuad
* bottom_quad
=
138 render_pass
->CreateAndAppendDrawQuad
<SolidColorDrawQuad
>();
139 bottom_quad
->SetNew(shared_quad_state
, bottom
, bottom
,
140 colors
[kNumColors
- 1 - (i
% kNumColors
)],
141 force_anti_aliasing_off
);
143 if (contents_opaque()) {
144 // Draws a stripe filling the layer vertically with the same color and
145 // width as the horizontal stipes along the layer's top border.
146 SolidColorDrawQuad
* solid_quad
=
147 render_pass
->CreateAndAppendDrawQuad
<SolidColorDrawQuad
>();
148 // The inner fill is more transparent then the border.
149 static const float kFillOpacity
= 0.1f
;
150 SkColor fill_color
= SkColorSetA(
151 colors
[i
% kNumColors
],
152 static_cast<uint8_t>(SkColorGetA(colors
[i
% kNumColors
]) *
154 gfx::Rect
fill_rect(x
, 0, width
, bounds().height());
155 solid_quad
->SetNew(shared_quad_state
, fill_rect
, fill_rect
, fill_color
,
156 force_anti_aliasing_off
);
159 if (!left
.IsEmpty()) {
160 bool force_anti_aliasing_off
= false;
161 SolidColorDrawQuad
* left_quad
=
162 render_pass
->CreateAndAppendDrawQuad
<SolidColorDrawQuad
>();
163 left_quad
->SetNew(shared_quad_state
, left
, left
,
164 colors
[kNumColors
- 1 - (i
% kNumColors
)],
165 force_anti_aliasing_off
);
167 SolidColorDrawQuad
* right_quad
=
168 render_pass
->CreateAndAppendDrawQuad
<SolidColorDrawQuad
>();
169 right_quad
->SetNew(shared_quad_state
, right
, right
,
170 colors
[i
% kNumColors
], force_anti_aliasing_off
);
175 void SurfaceLayerImpl::AsValueInto(base::trace_event::TracedValue
* dict
) const {
176 LayerImpl::AsValueInto(dict
);
177 dict
->SetInteger("surface_id", surface_id_
.id
);
180 const char* SurfaceLayerImpl::LayerTypeAsString() const {
181 return "cc::SurfaceLayerImpl";