GPU workaround to simulate Out of Memory errors with large textures
[chromium-blink-merge.git] / cc / quads / render_pass.cc
blob567145a82d6d12565c6a835aef264d32ba94ab9c
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/trace_event/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"
29 namespace {
30 const size_t kDefaultNumSharedQuadStatesToReserve = 32;
31 const size_t kDefaultNumQuadsToReserve = 128;
34 namespace cc {
36 QuadList::QuadList(size_t default_size_to_reserve)
37 : ListContainer<DrawQuad>(LargestDrawQuadSize(), default_size_to_reserve) {
40 scoped_ptr<RenderPass> RenderPass::Create() {
41 return make_scoped_ptr(new RenderPass());
44 scoped_ptr<RenderPass> RenderPass::Create(size_t num_layers) {
45 return make_scoped_ptr(new RenderPass(num_layers));
48 scoped_ptr<RenderPass> RenderPass::Create(size_t shared_quad_state_list_size,
49 size_t quad_list_size) {
50 return make_scoped_ptr(
51 new RenderPass(shared_quad_state_list_size, quad_list_size));
54 RenderPass::RenderPass()
55 : id(RenderPassId(-1, -1)),
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 : id(RenderPassId(-1, -1)),
66 has_transparent_background(true),
67 quad_list(kDefaultNumQuadsToReserve),
68 shared_quad_state_list(sizeof(SharedQuadState), num_layers) {
71 RenderPass::RenderPass(size_t shared_quad_state_list_size,
72 size_t quad_list_size)
73 : id(RenderPassId(-1, -1)),
74 has_transparent_background(true),
75 quad_list(quad_list_size),
76 shared_quad_state_list(sizeof(SharedQuadState),
77 shared_quad_state_list_size) {
80 RenderPass::~RenderPass() {
81 TRACE_EVENT_OBJECT_DELETED_WITH_ID(
82 TRACE_DISABLED_BY_DEFAULT("cc.debug.quads"),
83 "cc::RenderPass", id.AsTracingId());
86 scoped_ptr<RenderPass> RenderPass::Copy(RenderPassId new_id) const {
87 scoped_ptr<RenderPass> copy_pass(
88 Create(shared_quad_state_list.size(), quad_list.size()));
89 copy_pass->SetAll(new_id,
90 output_rect,
91 damage_rect,
92 transform_to_root_target,
93 has_transparent_background);
94 return copy_pass.Pass();
97 // static
98 void RenderPass::CopyAll(const ScopedPtrVector<RenderPass>& in,
99 ScopedPtrVector<RenderPass>* out) {
100 for (size_t i = 0; i < in.size(); ++i) {
101 RenderPass* source = in[i];
103 // Since we can't copy these, it's wrong to use CopyAll in a situation where
104 // you may have copy_requests present.
105 DCHECK_EQ(source->copy_requests.size(), 0u);
107 scoped_ptr<RenderPass> copy_pass(Create(
108 source->shared_quad_state_list.size(), source->quad_list.size()));
109 copy_pass->SetAll(source->id,
110 source->output_rect,
111 source->damage_rect,
112 source->transform_to_root_target,
113 source->has_transparent_background);
114 for (const auto& shared_quad_state : source->shared_quad_state_list) {
115 SharedQuadState* copy_shared_quad_state =
116 copy_pass->CreateAndAppendSharedQuadState();
117 copy_shared_quad_state->CopyFrom(shared_quad_state);
119 SharedQuadStateList::Iterator sqs_iter =
120 source->shared_quad_state_list.begin();
121 SharedQuadStateList::Iterator copy_sqs_iter =
122 copy_pass->shared_quad_state_list.begin();
123 for (const auto& quad : source->quad_list) {
124 while (quad->shared_quad_state != *sqs_iter) {
125 ++sqs_iter;
126 ++copy_sqs_iter;
127 DCHECK(sqs_iter != source->shared_quad_state_list.end());
129 DCHECK(quad->shared_quad_state == *sqs_iter);
131 SharedQuadState* copy_shared_quad_state = *copy_sqs_iter;
133 if (quad->material == DrawQuad::RENDER_PASS) {
134 const RenderPassDrawQuad* pass_quad =
135 RenderPassDrawQuad::MaterialCast(quad);
136 copy_pass->CopyFromAndAppendRenderPassDrawQuad(
137 pass_quad, copy_shared_quad_state, pass_quad->render_pass_id);
138 } else {
139 copy_pass->CopyFromAndAppendDrawQuad(quad, copy_shared_quad_state);
142 out->push_back(copy_pass.Pass());
146 void RenderPass::SetNew(RenderPassId id,
147 const gfx::Rect& output_rect,
148 const gfx::Rect& damage_rect,
149 const gfx::Transform& transform_to_root_target) {
150 DCHECK_GT(id.layer_id, 0);
151 DCHECK_GE(id.index, 0);
152 DCHECK(damage_rect.IsEmpty() || output_rect.Contains(damage_rect))
153 << "damage_rect: " << damage_rect.ToString()
154 << " output_rect: " << output_rect.ToString();
156 this->id = id;
157 this->output_rect = output_rect;
158 this->damage_rect = damage_rect;
159 this->transform_to_root_target = transform_to_root_target;
161 DCHECK(quad_list.empty());
162 DCHECK(shared_quad_state_list.empty());
165 void RenderPass::SetAll(RenderPassId id,
166 const gfx::Rect& output_rect,
167 const gfx::Rect& damage_rect,
168 const gfx::Transform& transform_to_root_target,
169 bool has_transparent_background) {
170 DCHECK_GT(id.layer_id, 0);
171 DCHECK_GE(id.index, 0);
173 this->id = id;
174 this->output_rect = output_rect;
175 this->damage_rect = damage_rect;
176 this->transform_to_root_target = transform_to_root_target;
177 this->has_transparent_background = has_transparent_background;
179 DCHECK(quad_list.empty());
180 DCHECK(shared_quad_state_list.empty());
183 void RenderPass::AsValueInto(base::trace_event::TracedValue* value) const {
184 MathUtil::AddToTracedValue("output_rect", output_rect, value);
185 MathUtil::AddToTracedValue("damage_rect", damage_rect, value);
187 value->SetBoolean("has_transparent_background", has_transparent_background);
188 value->SetInteger("copy_requests", copy_requests.size());
190 value->BeginArray("shared_quad_state_list");
191 for (const auto& shared_quad_state : shared_quad_state_list) {
192 value->BeginDictionary();
193 shared_quad_state->AsValueInto(value);
194 value->EndDictionary();
196 value->EndArray();
198 value->BeginArray("quad_list");
199 for (const auto& quad : quad_list) {
200 value->BeginDictionary();
201 quad->AsValueInto(value);
202 value->EndDictionary();
204 value->EndArray();
206 TracedValue::MakeDictIntoImplicitSnapshotWithCategory(
207 TRACE_DISABLED_BY_DEFAULT("cc.debug.quads"),
208 value,
209 "cc::RenderPass",
210 id.AsTracingId());
213 SharedQuadState* RenderPass::CreateAndAppendSharedQuadState() {
214 return shared_quad_state_list.AllocateAndConstruct<SharedQuadState>();
217 RenderPassDrawQuad* RenderPass::CopyFromAndAppendRenderPassDrawQuad(
218 const RenderPassDrawQuad* quad,
219 const SharedQuadState* shared_quad_state,
220 RenderPassId render_pass_id) {
221 RenderPassDrawQuad* copy_quad =
222 CopyFromAndAppendTypedDrawQuad<RenderPassDrawQuad>(quad);
223 copy_quad->shared_quad_state = shared_quad_state;
224 copy_quad->render_pass_id = render_pass_id;
225 return copy_quad;
228 DrawQuad* RenderPass::CopyFromAndAppendDrawQuad(
229 const DrawQuad* quad,
230 const SharedQuadState* shared_quad_state) {
231 switch (quad->material) {
232 case DrawQuad::CHECKERBOARD:
233 CopyFromAndAppendTypedDrawQuad<CheckerboardDrawQuad>(quad);
234 break;
235 case DrawQuad::DEBUG_BORDER:
236 CopyFromAndAppendTypedDrawQuad<DebugBorderDrawQuad>(quad);
237 break;
238 case DrawQuad::IO_SURFACE_CONTENT:
239 CopyFromAndAppendTypedDrawQuad<IOSurfaceDrawQuad>(quad);
240 break;
241 case DrawQuad::PICTURE_CONTENT:
242 CopyFromAndAppendTypedDrawQuad<PictureDrawQuad>(quad);
243 break;
244 case DrawQuad::TEXTURE_CONTENT:
245 CopyFromAndAppendTypedDrawQuad<TextureDrawQuad>(quad);
246 break;
247 case DrawQuad::SOLID_COLOR:
248 CopyFromAndAppendTypedDrawQuad<SolidColorDrawQuad>(quad);
249 break;
250 case DrawQuad::TILED_CONTENT:
251 CopyFromAndAppendTypedDrawQuad<TileDrawQuad>(quad);
252 break;
253 case DrawQuad::STREAM_VIDEO_CONTENT:
254 CopyFromAndAppendTypedDrawQuad<StreamVideoDrawQuad>(quad);
255 break;
256 case DrawQuad::SURFACE_CONTENT:
257 CopyFromAndAppendTypedDrawQuad<SurfaceDrawQuad>(quad);
258 break;
259 case DrawQuad::YUV_VIDEO_CONTENT:
260 CopyFromAndAppendTypedDrawQuad<YUVVideoDrawQuad>(quad);
261 break;
262 // RenderPass quads need to use specific CopyFrom function.
263 case DrawQuad::RENDER_PASS:
264 case DrawQuad::INVALID:
265 LOG(FATAL) << "Invalid DrawQuad material " << quad->material;
266 break;
268 quad_list.back()->shared_quad_state = shared_quad_state;
269 return quad_list.back();
272 } // namespace cc