1 // Copyright 2011 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/quads/render_pass.h"
9 #include "base/debug/trace_event_argument.h"
10 #include "base/values.h"
11 #include "cc/base/math_util.h"
12 #include "cc/debug/traced_value.h"
13 #include "cc/output/copy_output_request.h"
14 #include "cc/quads/checkerboard_draw_quad.h"
15 #include "cc/quads/debug_border_draw_quad.h"
16 #include "cc/quads/draw_quad.h"
17 #include "cc/quads/io_surface_draw_quad.h"
18 #include "cc/quads/largest_draw_quad.h"
19 #include "cc/quads/picture_draw_quad.h"
20 #include "cc/quads/render_pass_draw_quad.h"
21 #include "cc/quads/shared_quad_state.h"
22 #include "cc/quads/solid_color_draw_quad.h"
23 #include "cc/quads/stream_video_draw_quad.h"
24 #include "cc/quads/surface_draw_quad.h"
25 #include "cc/quads/texture_draw_quad.h"
26 #include "cc/quads/tile_draw_quad.h"
27 #include "cc/quads/yuv_video_draw_quad.h"
30 const size_t kDefaultNumSharedQuadStatesToReserve
= 32;
31 const size_t kDefaultNumQuadsToReserve
= 128;
36 QuadList::QuadList(size_t default_size_to_reserve
)
37 : ListContainer
<DrawQuad
>(sizeof(kLargestDrawQuad
),
38 default_size_to_reserve
) {
41 scoped_ptr
<RenderPass
> RenderPass::Create() {
42 return make_scoped_ptr(new RenderPass());
45 scoped_ptr
<RenderPass
> RenderPass::Create(size_t num_layers
) {
46 return make_scoped_ptr(new RenderPass(num_layers
));
49 scoped_ptr
<RenderPass
> RenderPass::Create(size_t shared_quad_state_list_size
,
50 size_t quad_list_size
) {
51 return make_scoped_ptr(
52 new RenderPass(shared_quad_state_list_size
, quad_list_size
));
55 RenderPass::RenderPass()
56 : id(RenderPassId(-1, -1)),
57 has_transparent_background(true),
58 quad_list(kDefaultNumQuadsToReserve
),
59 shared_quad_state_list(sizeof(SharedQuadState
),
60 kDefaultNumSharedQuadStatesToReserve
) {
63 // Each layer usually produces one shared quad state, so the number of layers
64 // is a good hint for what to reserve here.
65 RenderPass::RenderPass(size_t num_layers
)
66 : id(RenderPassId(-1, -1)),
67 has_transparent_background(true),
68 quad_list(kDefaultNumQuadsToReserve
),
69 shared_quad_state_list(sizeof(SharedQuadState
), num_layers
) {
72 RenderPass::RenderPass(size_t shared_quad_state_list_size
,
73 size_t quad_list_size
)
74 : id(RenderPassId(-1, -1)),
75 has_transparent_background(true),
76 quad_list(quad_list_size
),
77 shared_quad_state_list(sizeof(SharedQuadState
),
78 shared_quad_state_list_size
) {
81 RenderPass::~RenderPass() {
82 TRACE_EVENT_OBJECT_DELETED_WITH_ID(
83 TRACE_DISABLED_BY_DEFAULT("cc.debug.quads"),
84 "cc::RenderPass", id
.AsTracingId());
87 scoped_ptr
<RenderPass
> RenderPass::Copy(RenderPassId new_id
) const {
88 scoped_ptr
<RenderPass
> copy_pass(
89 Create(shared_quad_state_list
.size(), quad_list
.size()));
90 copy_pass
->SetAll(new_id
,
93 transform_to_root_target
,
94 has_transparent_background
);
95 return copy_pass
.Pass();
99 void RenderPass::CopyAll(const ScopedPtrVector
<RenderPass
>& in
,
100 ScopedPtrVector
<RenderPass
>* out
) {
101 for (size_t i
= 0; i
< in
.size(); ++i
) {
102 RenderPass
* source
= in
[i
];
104 // Since we can't copy these, it's wrong to use CopyAll in a situation where
105 // you may have copy_requests present.
106 DCHECK_EQ(source
->copy_requests
.size(), 0u);
108 scoped_ptr
<RenderPass
> copy_pass(Create(
109 source
->shared_quad_state_list
.size(), source
->quad_list
.size()));
110 copy_pass
->SetAll(source
->id
,
113 source
->transform_to_root_target
,
114 source
->has_transparent_background
);
115 for (const auto& shared_quad_state
: source
->shared_quad_state_list
) {
116 SharedQuadState
* copy_shared_quad_state
=
117 copy_pass
->CreateAndAppendSharedQuadState();
118 copy_shared_quad_state
->CopyFrom(shared_quad_state
);
120 SharedQuadStateList::Iterator sqs_iter
=
121 source
->shared_quad_state_list
.begin();
122 SharedQuadStateList::Iterator copy_sqs_iter
=
123 copy_pass
->shared_quad_state_list
.begin();
124 for (const auto& quad
: source
->quad_list
) {
125 while (quad
->shared_quad_state
!= *sqs_iter
) {
128 DCHECK(sqs_iter
!= source
->shared_quad_state_list
.end());
130 DCHECK(quad
->shared_quad_state
== *sqs_iter
);
132 SharedQuadState
* copy_shared_quad_state
= *copy_sqs_iter
;
134 if (quad
->material
== DrawQuad::RENDER_PASS
) {
135 const RenderPassDrawQuad
* pass_quad
=
136 RenderPassDrawQuad::MaterialCast(quad
);
137 copy_pass
->CopyFromAndAppendRenderPassDrawQuad(
138 pass_quad
, copy_shared_quad_state
, pass_quad
->render_pass_id
);
140 copy_pass
->CopyFromAndAppendDrawQuad(quad
, copy_shared_quad_state
);
143 out
->push_back(copy_pass
.Pass());
147 void RenderPass::SetNew(RenderPassId id
,
148 const gfx::Rect
& output_rect
,
149 const gfx::Rect
& damage_rect
,
150 const gfx::Transform
& transform_to_root_target
) {
151 DCHECK_GT(id
.layer_id
, 0);
152 DCHECK_GE(id
.index
, 0);
153 DCHECK(damage_rect
.IsEmpty() || output_rect
.Contains(damage_rect
))
154 << "damage_rect: " << damage_rect
.ToString()
155 << " output_rect: " << output_rect
.ToString();
158 this->output_rect
= output_rect
;
159 this->damage_rect
= damage_rect
;
160 this->transform_to_root_target
= transform_to_root_target
;
162 DCHECK(quad_list
.empty());
163 DCHECK(shared_quad_state_list
.empty());
166 void RenderPass::SetAll(RenderPassId id
,
167 const gfx::Rect
& output_rect
,
168 const gfx::Rect
& damage_rect
,
169 const gfx::Transform
& transform_to_root_target
,
170 bool has_transparent_background
) {
171 DCHECK_GT(id
.layer_id
, 0);
172 DCHECK_GE(id
.index
, 0);
175 this->output_rect
= output_rect
;
176 this->damage_rect
= damage_rect
;
177 this->transform_to_root_target
= transform_to_root_target
;
178 this->has_transparent_background
= has_transparent_background
;
180 DCHECK(quad_list
.empty());
181 DCHECK(shared_quad_state_list
.empty());
184 void RenderPass::AsValueInto(base::debug::TracedValue
* value
) const {
185 value
->BeginArray("output_rect");
186 MathUtil::AddToTracedValue(output_rect
, value
);
189 value
->BeginArray("damage_rect");
190 MathUtil::AddToTracedValue(damage_rect
, value
);
193 value
->SetBoolean("has_transparent_background", has_transparent_background
);
194 value
->SetInteger("copy_requests", copy_requests
.size());
196 value
->BeginArray("shared_quad_state_list");
197 for (const auto& shared_quad_state
: shared_quad_state_list
) {
198 value
->BeginDictionary();
199 shared_quad_state
->AsValueInto(value
);
200 value
->EndDictionary();
204 value
->BeginArray("quad_list");
205 for (const auto& quad
: quad_list
) {
206 value
->BeginDictionary();
207 quad
->AsValueInto(value
);
208 value
->EndDictionary();
212 TracedValue::MakeDictIntoImplicitSnapshotWithCategory(
213 TRACE_DISABLED_BY_DEFAULT("cc.debug.quads"),
219 SharedQuadState
* RenderPass::CreateAndAppendSharedQuadState() {
220 return shared_quad_state_list
.AllocateAndConstruct
<SharedQuadState
>();
223 RenderPassDrawQuad
* RenderPass::CopyFromAndAppendRenderPassDrawQuad(
224 const RenderPassDrawQuad
* quad
,
225 const SharedQuadState
* shared_quad_state
,
226 RenderPassId render_pass_id
) {
227 RenderPassDrawQuad
* copy_quad
=
228 CopyFromAndAppendTypedDrawQuad
<RenderPassDrawQuad
>(quad
);
229 copy_quad
->shared_quad_state
= shared_quad_state
;
230 copy_quad
->render_pass_id
= render_pass_id
;
234 DrawQuad
* RenderPass::CopyFromAndAppendDrawQuad(
235 const DrawQuad
* quad
,
236 const SharedQuadState
* shared_quad_state
) {
237 switch (quad
->material
) {
238 case DrawQuad::CHECKERBOARD
:
239 CopyFromAndAppendTypedDrawQuad
<CheckerboardDrawQuad
>(quad
);
241 case DrawQuad::DEBUG_BORDER
:
242 CopyFromAndAppendTypedDrawQuad
<DebugBorderDrawQuad
>(quad
);
244 case DrawQuad::IO_SURFACE_CONTENT
:
245 CopyFromAndAppendTypedDrawQuad
<IOSurfaceDrawQuad
>(quad
);
247 case DrawQuad::PICTURE_CONTENT
:
248 CopyFromAndAppendTypedDrawQuad
<PictureDrawQuad
>(quad
);
250 case DrawQuad::TEXTURE_CONTENT
:
251 CopyFromAndAppendTypedDrawQuad
<TextureDrawQuad
>(quad
);
253 case DrawQuad::SOLID_COLOR
:
254 CopyFromAndAppendTypedDrawQuad
<SolidColorDrawQuad
>(quad
);
256 case DrawQuad::TILED_CONTENT
:
257 CopyFromAndAppendTypedDrawQuad
<TileDrawQuad
>(quad
);
259 case DrawQuad::STREAM_VIDEO_CONTENT
:
260 CopyFromAndAppendTypedDrawQuad
<StreamVideoDrawQuad
>(quad
);
262 case DrawQuad::SURFACE_CONTENT
:
263 CopyFromAndAppendTypedDrawQuad
<SurfaceDrawQuad
>(quad
);
265 case DrawQuad::YUV_VIDEO_CONTENT
:
266 CopyFromAndAppendTypedDrawQuad
<YUVVideoDrawQuad
>(quad
);
268 // RenderPass quads need to use specific CopyFrom function.
269 case DrawQuad::RENDER_PASS
:
270 case DrawQuad::INVALID
:
271 LOG(FATAL
) << "Invalid DrawQuad material " << quad
->material
;
274 quad_list
.back()->shared_quad_state
= shared_quad_state
;
275 return quad_list
.back();