Roll src/third_party/skia 7b05ff1:dab1f60
[chromium-blink-merge.git] / cc / quads / render_pass.cc
blob5c4d98a33c96cbbf5f03f4b4f5740befe78057d9
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 <algorithm>
9 #include "base/numerics/safe_conversions.h"
10 #include "base/trace_event/trace_event_argument.h"
11 #include "base/values.h"
12 #include "cc/base/math_util.h"
13 #include "cc/debug/traced_value.h"
14 #include "cc/output/copy_output_request.h"
15 #include "cc/quads/checkerboard_draw_quad.h"
16 #include "cc/quads/debug_border_draw_quad.h"
17 #include "cc/quads/draw_quad.h"
18 #include "cc/quads/io_surface_draw_quad.h"
19 #include "cc/quads/largest_draw_quad.h"
20 #include "cc/quads/picture_draw_quad.h"
21 #include "cc/quads/render_pass_draw_quad.h"
22 #include "cc/quads/shared_quad_state.h"
23 #include "cc/quads/solid_color_draw_quad.h"
24 #include "cc/quads/stream_video_draw_quad.h"
25 #include "cc/quads/surface_draw_quad.h"
26 #include "cc/quads/texture_draw_quad.h"
27 #include "cc/quads/tile_draw_quad.h"
28 #include "cc/quads/yuv_video_draw_quad.h"
30 namespace {
31 const size_t kDefaultNumSharedQuadStatesToReserve = 32;
32 const size_t kDefaultNumQuadsToReserve = 128;
35 namespace cc {
37 QuadList::QuadList(size_t default_size_to_reserve)
38 : ListContainer<DrawQuad>(LargestDrawQuadSize(), 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 : has_transparent_background(true),
57 quad_list(kDefaultNumQuadsToReserve),
58 shared_quad_state_list(sizeof(SharedQuadState),
59 kDefaultNumSharedQuadStatesToReserve) {
62 // Each layer usually produces one shared quad state, so the number of layers
63 // is a good hint for what to reserve here.
64 RenderPass::RenderPass(size_t num_layers)
65 : has_transparent_background(true),
66 quad_list(kDefaultNumQuadsToReserve),
67 shared_quad_state_list(sizeof(SharedQuadState), num_layers) {
70 RenderPass::RenderPass(size_t shared_quad_state_list_size,
71 size_t quad_list_size)
72 : has_transparent_background(true),
73 quad_list(quad_list_size),
74 shared_quad_state_list(sizeof(SharedQuadState),
75 shared_quad_state_list_size) {
78 RenderPass::~RenderPass() {
79 TRACE_EVENT_OBJECT_DELETED_WITH_ID(
80 TRACE_DISABLED_BY_DEFAULT("cc.debug.quads"),
81 "cc::RenderPass", id.AsTracingId());
84 scoped_ptr<RenderPass> RenderPass::Copy(RenderPassId new_id) const {
85 scoped_ptr<RenderPass> copy_pass(
86 Create(shared_quad_state_list.size(), quad_list.size()));
87 copy_pass->SetAll(new_id,
88 output_rect,
89 damage_rect,
90 transform_to_root_target,
91 has_transparent_background);
92 return copy_pass.Pass();
95 // static
96 void RenderPass::CopyAll(const ScopedPtrVector<RenderPass>& in,
97 ScopedPtrVector<RenderPass>* out) {
98 for (size_t i = 0; i < in.size(); ++i) {
99 RenderPass* source = in[i];
101 // Since we can't copy these, it's wrong to use CopyAll in a situation where
102 // you may have copy_requests present.
103 DCHECK_EQ(source->copy_requests.size(), 0u);
105 scoped_ptr<RenderPass> copy_pass(Create(
106 source->shared_quad_state_list.size(), source->quad_list.size()));
107 copy_pass->SetAll(source->id,
108 source->output_rect,
109 source->damage_rect,
110 source->transform_to_root_target,
111 source->has_transparent_background);
112 for (const auto& shared_quad_state : source->shared_quad_state_list) {
113 SharedQuadState* copy_shared_quad_state =
114 copy_pass->CreateAndAppendSharedQuadState();
115 copy_shared_quad_state->CopyFrom(shared_quad_state);
117 SharedQuadStateList::Iterator sqs_iter =
118 source->shared_quad_state_list.begin();
119 SharedQuadStateList::Iterator copy_sqs_iter =
120 copy_pass->shared_quad_state_list.begin();
121 for (const auto& quad : source->quad_list) {
122 while (quad->shared_quad_state != *sqs_iter) {
123 ++sqs_iter;
124 ++copy_sqs_iter;
125 DCHECK(sqs_iter != source->shared_quad_state_list.end());
127 DCHECK(quad->shared_quad_state == *sqs_iter);
129 SharedQuadState* copy_shared_quad_state = *copy_sqs_iter;
131 if (quad->material == DrawQuad::RENDER_PASS) {
132 const RenderPassDrawQuad* pass_quad =
133 RenderPassDrawQuad::MaterialCast(quad);
134 copy_pass->CopyFromAndAppendRenderPassDrawQuad(
135 pass_quad, copy_shared_quad_state, pass_quad->render_pass_id);
136 } else {
137 copy_pass->CopyFromAndAppendDrawQuad(quad, copy_shared_quad_state);
140 out->push_back(copy_pass.Pass());
144 void RenderPass::SetNew(RenderPassId id,
145 const gfx::Rect& output_rect,
146 const gfx::Rect& damage_rect,
147 const gfx::Transform& transform_to_root_target) {
148 DCHECK_GT(id.layer_id, 0);
149 DCHECK(damage_rect.IsEmpty() || output_rect.Contains(damage_rect))
150 << "damage_rect: " << damage_rect.ToString()
151 << " output_rect: " << output_rect.ToString();
153 this->id = id;
154 this->output_rect = output_rect;
155 this->damage_rect = damage_rect;
156 this->transform_to_root_target = transform_to_root_target;
158 DCHECK(quad_list.empty());
159 DCHECK(shared_quad_state_list.empty());
162 void RenderPass::SetAll(RenderPassId id,
163 const gfx::Rect& output_rect,
164 const gfx::Rect& damage_rect,
165 const gfx::Transform& transform_to_root_target,
166 bool has_transparent_background) {
167 DCHECK_GT(id.layer_id, 0);
169 this->id = id;
170 this->output_rect = output_rect;
171 this->damage_rect = damage_rect;
172 this->transform_to_root_target = transform_to_root_target;
173 this->has_transparent_background = has_transparent_background;
175 DCHECK(quad_list.empty());
176 DCHECK(shared_quad_state_list.empty());
179 void RenderPass::AsValueInto(base::trace_event::TracedValue* value) const {
180 MathUtil::AddToTracedValue("output_rect", output_rect, value);
181 MathUtil::AddToTracedValue("damage_rect", damage_rect, value);
183 value->SetBoolean("has_transparent_background", has_transparent_background);
184 value->SetInteger("copy_requests",
185 base::saturated_cast<int>(copy_requests.size()));
187 value->BeginArray("shared_quad_state_list");
188 for (const auto& shared_quad_state : shared_quad_state_list) {
189 value->BeginDictionary();
190 shared_quad_state->AsValueInto(value);
191 value->EndDictionary();
193 value->EndArray();
195 value->BeginArray("quad_list");
196 for (const auto& quad : quad_list) {
197 value->BeginDictionary();
198 quad->AsValueInto(value);
199 value->EndDictionary();
201 value->EndArray();
203 TracedValue::MakeDictIntoImplicitSnapshotWithCategory(
204 TRACE_DISABLED_BY_DEFAULT("cc.debug.quads"),
205 value,
206 "cc::RenderPass",
207 id.AsTracingId());
210 SharedQuadState* RenderPass::CreateAndAppendSharedQuadState() {
211 return shared_quad_state_list.AllocateAndConstruct<SharedQuadState>();
214 RenderPassDrawQuad* RenderPass::CopyFromAndAppendRenderPassDrawQuad(
215 const RenderPassDrawQuad* quad,
216 const SharedQuadState* shared_quad_state,
217 RenderPassId render_pass_id) {
218 RenderPassDrawQuad* copy_quad =
219 CopyFromAndAppendTypedDrawQuad<RenderPassDrawQuad>(quad);
220 copy_quad->shared_quad_state = shared_quad_state;
221 copy_quad->render_pass_id = render_pass_id;
222 return copy_quad;
225 DrawQuad* RenderPass::CopyFromAndAppendDrawQuad(
226 const DrawQuad* quad,
227 const SharedQuadState* shared_quad_state) {
228 switch (quad->material) {
229 case DrawQuad::CHECKERBOARD:
230 CopyFromAndAppendTypedDrawQuad<CheckerboardDrawQuad>(quad);
231 break;
232 case DrawQuad::DEBUG_BORDER:
233 CopyFromAndAppendTypedDrawQuad<DebugBorderDrawQuad>(quad);
234 break;
235 case DrawQuad::IO_SURFACE_CONTENT:
236 CopyFromAndAppendTypedDrawQuad<IOSurfaceDrawQuad>(quad);
237 break;
238 case DrawQuad::PICTURE_CONTENT:
239 CopyFromAndAppendTypedDrawQuad<PictureDrawQuad>(quad);
240 break;
241 case DrawQuad::TEXTURE_CONTENT:
242 CopyFromAndAppendTypedDrawQuad<TextureDrawQuad>(quad);
243 break;
244 case DrawQuad::SOLID_COLOR:
245 CopyFromAndAppendTypedDrawQuad<SolidColorDrawQuad>(quad);
246 break;
247 case DrawQuad::TILED_CONTENT:
248 CopyFromAndAppendTypedDrawQuad<TileDrawQuad>(quad);
249 break;
250 case DrawQuad::STREAM_VIDEO_CONTENT:
251 CopyFromAndAppendTypedDrawQuad<StreamVideoDrawQuad>(quad);
252 break;
253 case DrawQuad::SURFACE_CONTENT:
254 CopyFromAndAppendTypedDrawQuad<SurfaceDrawQuad>(quad);
255 break;
256 case DrawQuad::YUV_VIDEO_CONTENT:
257 CopyFromAndAppendTypedDrawQuad<YUVVideoDrawQuad>(quad);
258 break;
259 // RenderPass quads need to use specific CopyFrom function.
260 case DrawQuad::RENDER_PASS:
261 case DrawQuad::INVALID:
262 LOG(FATAL) << "Invalid DrawQuad material " << quad->material;
263 break;
265 quad_list.back()->shared_quad_state = shared_quad_state;
266 return quad_list.back();
269 } // namespace cc