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"
7 #include "base/values.h"
8 #include "cc/base/math_util.h"
9 #include "cc/debug/traced_value.h"
10 #include "cc/output/copy_output_request.h"
11 #include "cc/quads/draw_quad.h"
12 #include "cc/quads/render_pass_draw_quad.h"
13 #include "cc/quads/shared_quad_state.h"
16 const size_t kDefaultNumSharedQuadStatesToReserve
= 32;
17 const size_t kDefaultNumQuadsToReserve
= 128;
22 void* RenderPass::Id::AsTracingId() const {
23 COMPILE_ASSERT(sizeof(size_t) <= sizeof(void*), // NOLINT(runtime/sizeof)
24 size_t_bigger_than_pointer
);
25 return reinterpret_cast<void*>(base::HashPair(layer_id
, index
));
28 scoped_ptr
<RenderPass
> RenderPass::Create() {
29 return make_scoped_ptr(new RenderPass());
32 scoped_ptr
<RenderPass
> RenderPass::Create(size_t num_layers
) {
33 return make_scoped_ptr(new RenderPass(num_layers
));
36 RenderPass::RenderPass()
38 has_transparent_background(true) {
39 shared_quad_state_list
.reserve(kDefaultNumSharedQuadStatesToReserve
);
40 quad_list
.reserve(kDefaultNumQuadsToReserve
);
43 RenderPass::RenderPass(size_t num_layers
)
45 has_transparent_background(true) {
46 // Each layer usually produces one shared quad state, so the number of layers
47 // is a good hint for what to reserve here.
48 shared_quad_state_list
.reserve(num_layers
);
49 quad_list
.reserve(kDefaultNumQuadsToReserve
);
52 RenderPass::~RenderPass() {
53 TRACE_EVENT_OBJECT_DELETED_WITH_ID(
54 TRACE_DISABLED_BY_DEFAULT("cc.debug.quads"),
55 "cc::RenderPass", id
.AsTracingId());
58 scoped_ptr
<RenderPass
> RenderPass::Copy(Id new_id
) const {
59 scoped_ptr
<RenderPass
> copy_pass(Create());
60 copy_pass
->SetAll(new_id
,
63 transform_to_root_target
,
64 has_transparent_background
);
65 return copy_pass
.Pass();
69 void RenderPass::CopyAll(const ScopedPtrVector
<RenderPass
>& in
,
70 ScopedPtrVector
<RenderPass
>* out
) {
71 for (size_t i
= 0; i
< in
.size(); ++i
) {
72 RenderPass
* source
= in
[i
];
74 // Since we can't copy these, it's wrong to use CopyAll in a situation where
75 // you may have copy_requests present.
76 DCHECK_EQ(source
->copy_requests
.size(), 0u);
78 scoped_ptr
<RenderPass
> copy_pass(Create());
79 copy_pass
->SetAll(source
->id
,
82 source
->transform_to_root_target
,
83 source
->has_transparent_background
);
84 for (size_t i
= 0; i
< source
->shared_quad_state_list
.size(); ++i
) {
85 copy_pass
->shared_quad_state_list
.push_back(
86 source
->shared_quad_state_list
[i
]->Copy());
88 for (size_t i
= 0, sqs_i
= 0; i
< source
->quad_list
.size(); ++i
) {
89 while (source
->quad_list
[i
]->shared_quad_state
!=
90 source
->shared_quad_state_list
[sqs_i
]) {
92 DCHECK_LT(sqs_i
, source
->shared_quad_state_list
.size());
94 DCHECK(source
->quad_list
[i
]->shared_quad_state
==
95 source
->shared_quad_state_list
[sqs_i
]);
97 DrawQuad
* quad
= source
->quad_list
[i
];
99 if (quad
->material
== DrawQuad::RENDER_PASS
) {
100 const RenderPassDrawQuad
* pass_quad
=
101 RenderPassDrawQuad::MaterialCast(quad
);
102 copy_pass
->quad_list
.push_back(
103 pass_quad
->Copy(copy_pass
->shared_quad_state_list
[sqs_i
],
104 pass_quad
->render_pass_id
).PassAs
<DrawQuad
>());
106 copy_pass
->quad_list
.push_back(source
->quad_list
[i
]->Copy(
107 copy_pass
->shared_quad_state_list
[sqs_i
]));
110 out
->push_back(copy_pass
.Pass());
114 void RenderPass::SetNew(Id id
,
115 const gfx::Rect
& output_rect
,
116 const gfx::RectF
& damage_rect
,
117 const gfx::Transform
& transform_to_root_target
) {
118 DCHECK_GT(id
.layer_id
, 0);
119 DCHECK_GE(id
.index
, 0);
122 this->output_rect
= output_rect
;
123 this->damage_rect
= damage_rect
;
124 this->transform_to_root_target
= transform_to_root_target
;
126 DCHECK(quad_list
.empty());
127 DCHECK(shared_quad_state_list
.empty());
130 void RenderPass::SetAll(Id id
,
131 const gfx::Rect
& output_rect
,
132 const gfx::RectF
& damage_rect
,
133 const gfx::Transform
& transform_to_root_target
,
134 bool has_transparent_background
) {
135 DCHECK_GT(id
.layer_id
, 0);
136 DCHECK_GE(id
.index
, 0);
139 this->output_rect
= output_rect
;
140 this->damage_rect
= damage_rect
;
141 this->transform_to_root_target
= transform_to_root_target
;
142 this->has_transparent_background
= has_transparent_background
;
144 DCHECK(quad_list
.empty());
145 DCHECK(shared_quad_state_list
.empty());
148 scoped_ptr
<base::Value
> RenderPass::AsValue() const {
149 scoped_ptr
<base::DictionaryValue
> value(new base::DictionaryValue());
150 value
->Set("output_rect", MathUtil::AsValue(output_rect
).release());
151 value
->Set("damage_rect", MathUtil::AsValue(damage_rect
).release());
152 value
->SetBoolean("has_transparent_background", has_transparent_background
);
153 value
->SetInteger("copy_requests", copy_requests
.size());
154 scoped_ptr
<base::ListValue
> shared_states_value(new base::ListValue());
155 for (size_t i
= 0; i
< shared_quad_state_list
.size(); ++i
) {
156 shared_states_value
->Append(shared_quad_state_list
[i
]->AsValue().release());
158 value
->Set("shared_quad_state_list", shared_states_value
.release());
159 scoped_ptr
<base::ListValue
> quad_list_value(new base::ListValue());
160 for (size_t i
= 0; i
< quad_list
.size(); ++i
) {
161 quad_list_value
->Append(quad_list
[i
]->AsValue().release());
163 value
->Set("quad_list", quad_list_value
.release());
165 TracedValue::MakeDictIntoImplicitSnapshotWithCategory(
166 TRACE_DISABLED_BY_DEFAULT("cc.debug.quads"),
167 value
.get(), "cc::RenderPass", id
.AsTracingId());
168 return value
.PassAs
<base::Value
>();