Lots of random cleanups, mostly for native_theme_win.cc:
[chromium-blink-merge.git] / cc / quads / render_pass.cc
blob70161667ad23d2bfbb4105bdcf2f016195534a09
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/checkerboard_draw_quad.h"
12 #include "cc/quads/debug_border_draw_quad.h"
13 #include "cc/quads/draw_quad.h"
14 #include "cc/quads/io_surface_draw_quad.h"
15 #include "cc/quads/picture_draw_quad.h"
16 #include "cc/quads/render_pass_draw_quad.h"
17 #include "cc/quads/shared_quad_state.h"
18 #include "cc/quads/solid_color_draw_quad.h"
19 #include "cc/quads/stream_video_draw_quad.h"
20 #include "cc/quads/surface_draw_quad.h"
21 #include "cc/quads/texture_draw_quad.h"
22 #include "cc/quads/tile_draw_quad.h"
23 #include "cc/quads/yuv_video_draw_quad.h"
25 namespace {
26 const size_t kDefaultNumSharedQuadStatesToReserve = 32;
27 const size_t kDefaultNumQuadsToReserve = 128;
30 namespace cc {
32 void* RenderPass::Id::AsTracingId() const {
33 COMPILE_ASSERT(sizeof(size_t) <= sizeof(void*), // NOLINT
34 size_t_bigger_than_pointer);
35 return reinterpret_cast<void*>(base::HashPair(layer_id, index));
38 scoped_ptr<RenderPass> RenderPass::Create() {
39 return make_scoped_ptr(new RenderPass());
42 scoped_ptr<RenderPass> RenderPass::Create(size_t num_layers) {
43 return make_scoped_ptr(new RenderPass(num_layers));
46 RenderPass::RenderPass() : id(Id(-1, -1)), has_transparent_background(true) {
47 shared_quad_state_list.reserve(kDefaultNumSharedQuadStatesToReserve);
48 quad_list.reserve(kDefaultNumQuadsToReserve);
51 RenderPass::RenderPass(size_t num_layers)
52 : id(Id(-1, -1)), has_transparent_background(true) {
53 // Each layer usually produces one shared quad state, so the number of layers
54 // is a good hint for what to reserve here.
55 shared_quad_state_list.reserve(num_layers);
56 quad_list.reserve(kDefaultNumQuadsToReserve);
59 RenderPass::~RenderPass() {
60 TRACE_EVENT_OBJECT_DELETED_WITH_ID(
61 TRACE_DISABLED_BY_DEFAULT("cc.debug.quads"),
62 "cc::RenderPass", id.AsTracingId());
65 scoped_ptr<RenderPass> RenderPass::Copy(Id new_id) const {
66 scoped_ptr<RenderPass> copy_pass(Create());
67 copy_pass->SetAll(new_id,
68 output_rect,
69 damage_rect,
70 transform_to_root_target,
71 has_transparent_background);
72 return copy_pass.Pass();
75 // static
76 void RenderPass::CopyAll(const ScopedPtrVector<RenderPass>& in,
77 ScopedPtrVector<RenderPass>* out) {
78 for (size_t i = 0; i < in.size(); ++i) {
79 RenderPass* source = in[i];
81 // Since we can't copy these, it's wrong to use CopyAll in a situation where
82 // you may have copy_requests present.
83 DCHECK_EQ(source->copy_requests.size(), 0u);
85 scoped_ptr<RenderPass> copy_pass(Create());
86 copy_pass->SetAll(source->id,
87 source->output_rect,
88 source->damage_rect,
89 source->transform_to_root_target,
90 source->has_transparent_background);
91 for (size_t i = 0; i < source->shared_quad_state_list.size(); ++i) {
92 SharedQuadState* copy_shared_quad_state =
93 copy_pass->CreateAndAppendSharedQuadState();
94 copy_shared_quad_state->CopyFrom(source->shared_quad_state_list[i]);
96 for (size_t i = 0, sqs_i = 0; i < source->quad_list.size(); ++i) {
97 while (source->quad_list[i]->shared_quad_state !=
98 source->shared_quad_state_list[sqs_i]) {
99 ++sqs_i;
100 DCHECK_LT(sqs_i, source->shared_quad_state_list.size());
102 DCHECK(source->quad_list[i]->shared_quad_state ==
103 source->shared_quad_state_list[sqs_i]);
105 DrawQuad* quad = source->quad_list[i];
107 if (quad->material == DrawQuad::RENDER_PASS) {
108 const RenderPassDrawQuad* pass_quad =
109 RenderPassDrawQuad::MaterialCast(quad);
110 copy_pass->quad_list.push_back(
111 pass_quad->Copy(copy_pass->shared_quad_state_list[sqs_i],
112 pass_quad->render_pass_id).PassAs<DrawQuad>());
113 } else {
114 copy_pass->quad_list.push_back(source->quad_list[i]->Copy(
115 copy_pass->shared_quad_state_list[sqs_i]));
118 out->push_back(copy_pass.Pass());
122 void RenderPass::SetNew(Id id,
123 const gfx::Rect& output_rect,
124 const gfx::Rect& damage_rect,
125 const gfx::Transform& transform_to_root_target) {
126 DCHECK_GT(id.layer_id, 0);
127 DCHECK_GE(id.index, 0);
128 DCHECK(damage_rect.IsEmpty() || output_rect.Contains(damage_rect))
129 << "damage_rect: " << damage_rect.ToString()
130 << " output_rect: " << output_rect.ToString();
132 this->id = id;
133 this->output_rect = output_rect;
134 this->damage_rect = damage_rect;
135 this->transform_to_root_target = transform_to_root_target;
137 DCHECK(quad_list.empty());
138 DCHECK(shared_quad_state_list.empty());
141 void RenderPass::SetAll(Id id,
142 const gfx::Rect& output_rect,
143 const gfx::Rect& damage_rect,
144 const gfx::Transform& transform_to_root_target,
145 bool has_transparent_background) {
146 DCHECK_GT(id.layer_id, 0);
147 DCHECK_GE(id.index, 0);
149 this->id = id;
150 this->output_rect = output_rect;
151 this->damage_rect = damage_rect;
152 this->transform_to_root_target = transform_to_root_target;
153 this->has_transparent_background = has_transparent_background;
155 DCHECK(quad_list.empty());
156 DCHECK(shared_quad_state_list.empty());
159 scoped_ptr<base::Value> RenderPass::AsValue() const {
160 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue());
161 value->Set("output_rect", MathUtil::AsValue(output_rect).release());
162 value->Set("damage_rect", MathUtil::AsValue(damage_rect).release());
163 value->SetBoolean("has_transparent_background", has_transparent_background);
164 value->SetInteger("copy_requests", copy_requests.size());
165 scoped_ptr<base::ListValue> shared_states_value(new base::ListValue());
166 for (size_t i = 0; i < shared_quad_state_list.size(); ++i) {
167 shared_states_value->Append(shared_quad_state_list[i]->AsValue().release());
169 value->Set("shared_quad_state_list", shared_states_value.release());
170 scoped_ptr<base::ListValue> quad_list_value(new base::ListValue());
171 for (size_t i = 0; i < quad_list.size(); ++i) {
172 quad_list_value->Append(quad_list[i]->AsValue().release());
174 value->Set("quad_list", quad_list_value.release());
176 TracedValue::MakeDictIntoImplicitSnapshotWithCategory(
177 TRACE_DISABLED_BY_DEFAULT("cc.debug.quads"),
178 value.get(), "cc::RenderPass", id.AsTracingId());
179 return value.PassAs<base::Value>();
182 SharedQuadState* RenderPass::CreateAndAppendSharedQuadState() {
183 shared_quad_state_list.push_back(make_scoped_ptr(new SharedQuadState));
184 return shared_quad_state_list.back();
187 RenderPassDrawQuad* RenderPass::CopyFromAndAppendRenderPassDrawQuad(
188 const RenderPassDrawQuad* quad,
189 const SharedQuadState* shared_quad_state,
190 RenderPass::Id render_pass_id) {
191 RenderPassDrawQuad* copy_quad =
192 CopyFromAndAppendTypedDrawQuad<RenderPassDrawQuad>(quad);
193 copy_quad->shared_quad_state = shared_quad_state;
194 copy_quad->render_pass_id = render_pass_id;
195 return copy_quad;
198 DrawQuad* RenderPass::CopyFromAndAppendDrawQuad(
199 const DrawQuad* quad,
200 const SharedQuadState* shared_quad_state) {
201 switch (quad->material) {
202 case DrawQuad::CHECKERBOARD:
203 CopyFromAndAppendTypedDrawQuad<CheckerboardDrawQuad>(quad);
204 break;
205 case DrawQuad::DEBUG_BORDER:
206 CopyFromAndAppendTypedDrawQuad<DebugBorderDrawQuad>(quad);
207 break;
208 case DrawQuad::IO_SURFACE_CONTENT:
209 CopyFromAndAppendTypedDrawQuad<IOSurfaceDrawQuad>(quad);
210 break;
211 case DrawQuad::PICTURE_CONTENT:
212 CopyFromAndAppendTypedDrawQuad<PictureDrawQuad>(quad);
213 break;
214 case DrawQuad::TEXTURE_CONTENT:
215 CopyFromAndAppendTypedDrawQuad<TextureDrawQuad>(quad);
216 break;
217 case DrawQuad::SOLID_COLOR:
218 CopyFromAndAppendTypedDrawQuad<SolidColorDrawQuad>(quad);
219 break;
220 case DrawQuad::TILED_CONTENT:
221 CopyFromAndAppendTypedDrawQuad<TileDrawQuad>(quad);
222 break;
223 case DrawQuad::STREAM_VIDEO_CONTENT:
224 CopyFromAndAppendTypedDrawQuad<StreamVideoDrawQuad>(quad);
225 break;
226 case DrawQuad::SURFACE_CONTENT:
227 CopyFromAndAppendTypedDrawQuad<SurfaceDrawQuad>(quad);
228 break;
229 case DrawQuad::YUV_VIDEO_CONTENT:
230 CopyFromAndAppendTypedDrawQuad<YUVVideoDrawQuad>(quad);
231 break;
232 // RenderPass quads need to use specific CopyFrom function.
233 case DrawQuad::RENDER_PASS:
234 case DrawQuad::INVALID:
235 LOG(FATAL) << "Invalid DrawQuad material " << quad->material;
236 break;
238 quad_list.back()->shared_quad_state = shared_quad_state;
239 return quad_list.back();
242 } // namespace cc