[Android] Implement 3-way sensor fallback for Device Orientation.
[chromium-blink-merge.git] / content / common / cc_messages.cc
blobf05a253a2506c0188947ee9cb4f6c62fe6d41b95
1 // Copyright (c) 2012 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 "content/common/cc_messages.h"
7 #include "cc/output/compositor_frame.h"
8 #include "cc/output/filter_operations.h"
9 #include "cc/quads/draw_quad.h"
10 #include "cc/quads/largest_draw_quad.h"
11 #include "cc/quads/render_pass_id.h"
12 #include "content/public/common/common_param_traits.h"
13 #include "third_party/skia/include/core/SkData.h"
14 #include "third_party/skia/include/core/SkFlattenableSerialization.h"
15 #include "ui/gfx/transform.h"
17 namespace IPC {
19 void ParamTraits<cc::FilterOperation>::Write(
20 Message* m, const param_type& p) {
21 WriteParam(m, p.type());
22 switch (p.type()) {
23 case cc::FilterOperation::GRAYSCALE:
24 case cc::FilterOperation::SEPIA:
25 case cc::FilterOperation::SATURATE:
26 case cc::FilterOperation::HUE_ROTATE:
27 case cc::FilterOperation::INVERT:
28 case cc::FilterOperation::BRIGHTNESS:
29 case cc::FilterOperation::SATURATING_BRIGHTNESS:
30 case cc::FilterOperation::CONTRAST:
31 case cc::FilterOperation::OPACITY:
32 case cc::FilterOperation::BLUR:
33 WriteParam(m, p.amount());
34 break;
35 case cc::FilterOperation::DROP_SHADOW:
36 WriteParam(m, p.drop_shadow_offset());
37 WriteParam(m, p.amount());
38 WriteParam(m, p.drop_shadow_color());
39 break;
40 case cc::FilterOperation::COLOR_MATRIX:
41 for (int i = 0; i < 20; ++i)
42 WriteParam(m, p.matrix()[i]);
43 break;
44 case cc::FilterOperation::ZOOM:
45 WriteParam(m, p.amount());
46 WriteParam(m, p.zoom_inset());
47 break;
48 case cc::FilterOperation::REFERENCE:
49 WriteParam(m, p.image_filter());
50 break;
51 case cc::FilterOperation::ALPHA_THRESHOLD:
52 NOTREACHED();
53 break;
57 bool ParamTraits<cc::FilterOperation>::Read(const Message* m,
58 base::PickleIterator* iter,
59 param_type* r) {
60 cc::FilterOperation::FilterType type;
61 float amount;
62 gfx::Point drop_shadow_offset;
63 SkColor drop_shadow_color;
64 SkScalar matrix[20];
65 int zoom_inset;
67 if (!ReadParam(m, iter, &type))
68 return false;
69 r->set_type(type);
71 bool success = false;
72 switch (type) {
73 case cc::FilterOperation::GRAYSCALE:
74 case cc::FilterOperation::SEPIA:
75 case cc::FilterOperation::SATURATE:
76 case cc::FilterOperation::HUE_ROTATE:
77 case cc::FilterOperation::INVERT:
78 case cc::FilterOperation::BRIGHTNESS:
79 case cc::FilterOperation::SATURATING_BRIGHTNESS:
80 case cc::FilterOperation::CONTRAST:
81 case cc::FilterOperation::OPACITY:
82 case cc::FilterOperation::BLUR:
83 if (ReadParam(m, iter, &amount)) {
84 r->set_amount(amount);
85 success = true;
87 break;
88 case cc::FilterOperation::DROP_SHADOW:
89 if (ReadParam(m, iter, &drop_shadow_offset) &&
90 ReadParam(m, iter, &amount) &&
91 ReadParam(m, iter, &drop_shadow_color)) {
92 r->set_drop_shadow_offset(drop_shadow_offset);
93 r->set_amount(amount);
94 r->set_drop_shadow_color(drop_shadow_color);
95 success = true;
97 break;
98 case cc::FilterOperation::COLOR_MATRIX: {
99 int i;
100 for (i = 0; i < 20; ++i) {
101 if (!ReadParam(m, iter, &matrix[i]))
102 break;
104 if (i == 20) {
105 r->set_matrix(matrix);
106 success = true;
108 break;
110 case cc::FilterOperation::ZOOM:
111 if (ReadParam(m, iter, &amount) &&
112 ReadParam(m, iter, &zoom_inset) &&
113 amount >= 0.f &&
114 zoom_inset >= 0) {
115 r->set_amount(amount);
116 r->set_zoom_inset(zoom_inset);
117 success = true;
119 break;
120 case cc::FilterOperation::REFERENCE: {
121 skia::RefPtr<SkImageFilter> filter;
122 if (!ReadParam(m, iter, &filter)) {
123 success = false;
124 break;
126 r->set_image_filter(filter);
127 success = true;
128 break;
130 case cc::FilterOperation::ALPHA_THRESHOLD:
131 break;
133 return success;
136 void ParamTraits<cc::FilterOperation>::Log(
137 const param_type& p, std::string* l) {
138 l->append("(");
139 LogParam(static_cast<unsigned>(p.type()), l);
140 l->append(", ");
142 switch (p.type()) {
143 case cc::FilterOperation::GRAYSCALE:
144 case cc::FilterOperation::SEPIA:
145 case cc::FilterOperation::SATURATE:
146 case cc::FilterOperation::HUE_ROTATE:
147 case cc::FilterOperation::INVERT:
148 case cc::FilterOperation::BRIGHTNESS:
149 case cc::FilterOperation::SATURATING_BRIGHTNESS:
150 case cc::FilterOperation::CONTRAST:
151 case cc::FilterOperation::OPACITY:
152 case cc::FilterOperation::BLUR:
153 LogParam(p.amount(), l);
154 break;
155 case cc::FilterOperation::DROP_SHADOW:
156 LogParam(p.drop_shadow_offset(), l);
157 l->append(", ");
158 LogParam(p.amount(), l);
159 l->append(", ");
160 LogParam(p.drop_shadow_color(), l);
161 break;
162 case cc::FilterOperation::COLOR_MATRIX:
163 for (int i = 0; i < 20; ++i) {
164 if (i)
165 l->append(", ");
166 LogParam(p.matrix()[i], l);
168 break;
169 case cc::FilterOperation::ZOOM:
170 LogParam(p.amount(), l);
171 l->append(", ");
172 LogParam(p.zoom_inset(), l);
173 break;
174 case cc::FilterOperation::REFERENCE:
175 LogParam(p.image_filter(), l);
176 break;
177 case cc::FilterOperation::ALPHA_THRESHOLD:
178 NOTREACHED();
179 break;
181 l->append(")");
184 void ParamTraits<cc::FilterOperations>::Write(
185 Message* m, const param_type& p) {
186 WriteParam(m, p.size());
187 for (std::size_t i = 0; i < p.size(); ++i) {
188 WriteParam(m, p.at(i));
192 bool ParamTraits<cc::FilterOperations>::Read(const Message* m,
193 base::PickleIterator* iter,
194 param_type* r) {
195 size_t count;
196 if (!ReadParam(m, iter, &count))
197 return false;
199 for (std::size_t i = 0; i < count; ++i) {
200 cc::FilterOperation op = cc::FilterOperation::CreateEmptyFilter();
201 if (!ReadParam(m, iter, &op))
202 return false;
203 r->Append(op);
205 return true;
208 void ParamTraits<cc::FilterOperations>::Log(
209 const param_type& p, std::string* l) {
210 l->append("(");
211 for (std::size_t i = 0; i < p.size(); ++i) {
212 if (i)
213 l->append(", ");
214 LogParam(p.at(i), l);
216 l->append(")");
219 void ParamTraits<skia::RefPtr<SkImageFilter> >::Write(
220 Message* m, const param_type& p) {
221 SkImageFilter* filter = p.get();
222 if (filter) {
223 skia::RefPtr<SkData> data =
224 skia::AdoptRef(SkValidatingSerializeFlattenable(filter));
225 m->WriteData(static_cast<const char*>(data->data()), data->size());
226 } else {
227 m->WriteData(0, 0);
231 bool ParamTraits<skia::RefPtr<SkImageFilter>>::Read(const Message* m,
232 base::PickleIterator* iter,
233 param_type* r) {
234 const char* data = 0;
235 int length = 0;
236 if (!iter->ReadData(&data, &length))
237 return false;
238 if (length > 0) {
239 SkFlattenable* flattenable = SkValidatingDeserializeFlattenable(
240 data, length, SkImageFilter::GetFlattenableType());
241 *r = skia::AdoptRef(static_cast<SkImageFilter*>(flattenable));
242 } else {
243 r->clear();
245 return true;
248 void ParamTraits<skia::RefPtr<SkImageFilter> >::Log(
249 const param_type& p, std::string* l) {
250 l->append("(");
251 LogParam(p.get() ? p->countInputs() : 0, l);
252 l->append(")");
255 void ParamTraits<gfx::Transform>::Write(
256 Message* m, const param_type& p) {
257 #ifdef SK_MSCALAR_IS_FLOAT
258 float column_major_data[16];
259 p.matrix().asColMajorf(column_major_data);
260 #else
261 double column_major_data[16];
262 p.matrix().asColMajord(column_major_data);
263 #endif
264 m->WriteBytes(&column_major_data, sizeof(SkMScalar) * 16);
267 bool ParamTraits<gfx::Transform>::Read(const Message* m,
268 base::PickleIterator* iter,
269 param_type* r) {
270 const char* column_major_data;
271 if (!iter->ReadBytes(&column_major_data, sizeof(SkMScalar) * 16))
272 return false;
273 r->matrix().setColMajor(
274 reinterpret_cast<const SkMScalar*>(column_major_data));
275 return true;
278 void ParamTraits<gfx::Transform>::Log(
279 const param_type& p, std::string* l) {
280 #ifdef SK_MSCALAR_IS_FLOAT
281 float row_major_data[16];
282 p.matrix().asRowMajorf(row_major_data);
283 #else
284 double row_major_data[16];
285 p.matrix().asRowMajord(row_major_data);
286 #endif
287 l->append("(");
288 for (int i = 0; i < 16; ++i) {
289 if (i > 0)
290 l->append(", ");
291 LogParam(row_major_data[i], l);
293 l->append(") ");
296 void ParamTraits<cc::RenderPass>::Write(
297 Message* m, const param_type& p) {
298 WriteParam(m, p.id);
299 WriteParam(m, p.output_rect);
300 WriteParam(m, p.damage_rect);
301 WriteParam(m, p.transform_to_root_target);
302 WriteParam(m, p.has_transparent_background);
303 WriteParam(m, p.referenced_surfaces);
304 WriteParam(m, p.quad_list.size());
306 cc::SharedQuadStateList::ConstIterator shared_quad_state_iter =
307 p.shared_quad_state_list.begin();
308 cc::SharedQuadStateList::ConstIterator last_shared_quad_state_iter =
309 p.shared_quad_state_list.end();
310 for (const auto& quad : p.quad_list) {
311 DCHECK(quad->rect.Contains(quad->visible_rect))
312 << quad->material << " rect: " << quad->rect.ToString()
313 << " visible_rect: " << quad->visible_rect.ToString();
314 DCHECK(quad->opaque_rect.IsEmpty() ||
315 quad->rect.Contains(quad->opaque_rect))
316 << quad->material << " rect: " << quad->rect.ToString()
317 << " opaque_rect: " << quad->opaque_rect.ToString();
319 switch (quad->material) {
320 case cc::DrawQuad::DEBUG_BORDER:
321 WriteParam(m, *cc::DebugBorderDrawQuad::MaterialCast(quad));
322 break;
323 case cc::DrawQuad::IO_SURFACE_CONTENT:
324 WriteParam(m, *cc::IOSurfaceDrawQuad::MaterialCast(quad));
325 break;
326 case cc::DrawQuad::PICTURE_CONTENT:
327 NOTREACHED();
328 break;
329 case cc::DrawQuad::TEXTURE_CONTENT:
330 WriteParam(m, *cc::TextureDrawQuad::MaterialCast(quad));
331 break;
332 case cc::DrawQuad::RENDER_PASS:
333 WriteParam(m, *cc::RenderPassDrawQuad::MaterialCast(quad));
334 break;
335 case cc::DrawQuad::SOLID_COLOR:
336 WriteParam(m, *cc::SolidColorDrawQuad::MaterialCast(quad));
337 break;
338 case cc::DrawQuad::SURFACE_CONTENT:
339 WriteParam(m, *cc::SurfaceDrawQuad::MaterialCast(quad));
340 break;
341 case cc::DrawQuad::TILED_CONTENT:
342 WriteParam(m, *cc::TileDrawQuad::MaterialCast(quad));
343 break;
344 case cc::DrawQuad::STREAM_VIDEO_CONTENT:
345 WriteParam(m, *cc::StreamVideoDrawQuad::MaterialCast(quad));
346 break;
347 case cc::DrawQuad::YUV_VIDEO_CONTENT:
348 WriteParam(m, *cc::YUVVideoDrawQuad::MaterialCast(quad));
349 break;
350 case cc::DrawQuad::INVALID:
351 break;
354 // Null shared quad states should not occur.
355 DCHECK(quad->shared_quad_state);
357 // SharedQuadStates should appear in the order they are used by DrawQuads.
358 // Find the SharedQuadState for this DrawQuad.
359 while (shared_quad_state_iter != p.shared_quad_state_list.end() &&
360 quad->shared_quad_state != *shared_quad_state_iter)
361 ++shared_quad_state_iter;
363 DCHECK(shared_quad_state_iter != p.shared_quad_state_list.end());
365 if (shared_quad_state_iter != last_shared_quad_state_iter) {
366 WriteParam(m, true);
367 WriteParam(m, **shared_quad_state_iter);
368 last_shared_quad_state_iter = shared_quad_state_iter;
369 } else {
370 WriteParam(m, false);
375 static size_t ReserveSizeForRenderPassWrite(const cc::RenderPass& p) {
376 size_t to_reserve = sizeof(cc::RenderPass);
378 // Whether the quad points to a new shared quad state for each quad.
379 to_reserve += p.quad_list.size() * sizeof(bool);
381 // Shared quad state is only written when a quad contains a shared quad state
382 // that has not been written.
383 to_reserve += p.shared_quad_state_list.size() * sizeof(cc::SharedQuadState);
385 // The largest quad type, verified by a unit test.
386 to_reserve += p.quad_list.size() * cc::LargestDrawQuadSize();
388 // The actual list of referenced surfaces.
389 to_reserve += p.referenced_surfaces.size() * sizeof(cc::SurfaceId);
390 return to_reserve;
393 template <typename QuadType>
394 static cc::DrawQuad* ReadDrawQuad(const Message* m,
395 base::PickleIterator* iter,
396 cc::RenderPass* render_pass) {
397 QuadType* quad = render_pass->CreateAndAppendDrawQuad<QuadType>();
398 if (!ReadParam(m, iter, quad))
399 return NULL;
400 return quad;
403 bool ParamTraits<cc::RenderPass>::Read(const Message* m,
404 base::PickleIterator* iter,
405 param_type* p) {
406 cc::RenderPassId id;
407 gfx::Rect output_rect;
408 gfx::Rect damage_rect;
409 gfx::Transform transform_to_root_target;
410 bool has_transparent_background;
411 std::vector<cc::SurfaceId> referenced_surfaces;
412 size_t quad_list_size;
414 if (!ReadParam(m, iter, &id) || !ReadParam(m, iter, &output_rect) ||
415 !ReadParam(m, iter, &damage_rect) ||
416 !ReadParam(m, iter, &transform_to_root_target) ||
417 !ReadParam(m, iter, &has_transparent_background) ||
418 !ReadParam(m, iter, &referenced_surfaces) ||
419 !ReadParam(m, iter, &quad_list_size))
420 return false;
422 p->SetAll(id,
423 output_rect,
424 damage_rect,
425 transform_to_root_target,
426 has_transparent_background);
427 p->referenced_surfaces.swap(referenced_surfaces);
429 for (size_t i = 0; i < quad_list_size; ++i) {
430 cc::DrawQuad::Material material;
431 base::PickleIterator temp_iter = *iter;
432 if (!ReadParam(m, &temp_iter, &material))
433 return false;
435 cc::DrawQuad* draw_quad = NULL;
436 switch (material) {
437 case cc::DrawQuad::DEBUG_BORDER:
438 draw_quad = ReadDrawQuad<cc::DebugBorderDrawQuad>(m, iter, p);
439 break;
440 case cc::DrawQuad::IO_SURFACE_CONTENT:
441 draw_quad = ReadDrawQuad<cc::IOSurfaceDrawQuad>(m, iter, p);
442 break;
443 case cc::DrawQuad::PICTURE_CONTENT:
444 NOTREACHED();
445 return false;
446 case cc::DrawQuad::SURFACE_CONTENT:
447 draw_quad = ReadDrawQuad<cc::SurfaceDrawQuad>(m, iter, p);
448 break;
449 case cc::DrawQuad::TEXTURE_CONTENT:
450 draw_quad = ReadDrawQuad<cc::TextureDrawQuad>(m, iter, p);
451 break;
452 case cc::DrawQuad::RENDER_PASS:
453 draw_quad = ReadDrawQuad<cc::RenderPassDrawQuad>(m, iter, p);
454 break;
455 case cc::DrawQuad::SOLID_COLOR:
456 draw_quad = ReadDrawQuad<cc::SolidColorDrawQuad>(m, iter, p);
457 break;
458 case cc::DrawQuad::TILED_CONTENT:
459 draw_quad = ReadDrawQuad<cc::TileDrawQuad>(m, iter, p);
460 break;
461 case cc::DrawQuad::STREAM_VIDEO_CONTENT:
462 draw_quad = ReadDrawQuad<cc::StreamVideoDrawQuad>(m, iter, p);
463 break;
464 case cc::DrawQuad::YUV_VIDEO_CONTENT:
465 draw_quad = ReadDrawQuad<cc::YUVVideoDrawQuad>(m, iter, p);
466 break;
467 case cc::DrawQuad::INVALID:
468 break;
470 if (!draw_quad)
471 return false;
472 if (!draw_quad->rect.Contains(draw_quad->visible_rect)) {
473 LOG(ERROR) << "Quad with invalid visible rect " << draw_quad->material
474 << " rect: " << draw_quad->rect.ToString()
475 << " visible_rect: " << draw_quad->visible_rect.ToString();
476 return false;
478 if (!draw_quad->opaque_rect.IsEmpty() &&
479 !draw_quad->rect.Contains(draw_quad->opaque_rect)) {
480 LOG(ERROR) << "Quad with invalid opaque rect " << draw_quad->material
481 << " rect: " << draw_quad->rect.ToString()
482 << " opaque_rect: " << draw_quad->opaque_rect.ToString();
483 return false;
486 bool has_new_shared_quad_state;
487 if (!ReadParam(m, iter, &has_new_shared_quad_state))
488 return false;
490 // If the quad has a new shared quad state, read it in.
491 if (has_new_shared_quad_state) {
492 cc::SharedQuadState* state = p->CreateAndAppendSharedQuadState();
493 if (!ReadParam(m, iter, state))
494 return false;
497 draw_quad->shared_quad_state = p->shared_quad_state_list.back();
500 return true;
503 void ParamTraits<cc::RenderPass>::Log(
504 const param_type& p, std::string* l) {
505 l->append("RenderPass((");
506 LogParam(p.id, l);
507 l->append("), ");
508 LogParam(p.output_rect, l);
509 l->append(", ");
510 LogParam(p.damage_rect, l);
511 l->append(", ");
512 LogParam(p.transform_to_root_target, l);
513 l->append(", ");
514 LogParam(p.has_transparent_background, l);
515 l->append(", ");
516 LogParam(p.referenced_surfaces, l);
517 l->append(", ");
519 l->append("[");
520 for (const auto& shared_quad_state : p.shared_quad_state_list) {
521 if (shared_quad_state != p.shared_quad_state_list.front())
522 l->append(", ");
523 LogParam(*shared_quad_state, l);
525 l->append("], [");
526 for (const auto& quad : p.quad_list) {
527 if (quad != p.quad_list.front())
528 l->append(", ");
529 switch (quad->material) {
530 case cc::DrawQuad::DEBUG_BORDER:
531 LogParam(*cc::DebugBorderDrawQuad::MaterialCast(quad), l);
532 break;
533 case cc::DrawQuad::IO_SURFACE_CONTENT:
534 LogParam(*cc::IOSurfaceDrawQuad::MaterialCast(quad), l);
535 break;
536 case cc::DrawQuad::PICTURE_CONTENT:
537 NOTREACHED();
538 break;
539 case cc::DrawQuad::TEXTURE_CONTENT:
540 LogParam(*cc::TextureDrawQuad::MaterialCast(quad), l);
541 break;
542 case cc::DrawQuad::RENDER_PASS:
543 LogParam(*cc::RenderPassDrawQuad::MaterialCast(quad), l);
544 break;
545 case cc::DrawQuad::SOLID_COLOR:
546 LogParam(*cc::SolidColorDrawQuad::MaterialCast(quad), l);
547 break;
548 case cc::DrawQuad::SURFACE_CONTENT:
549 LogParam(*cc::SurfaceDrawQuad::MaterialCast(quad), l);
550 break;
551 case cc::DrawQuad::TILED_CONTENT:
552 LogParam(*cc::TileDrawQuad::MaterialCast(quad), l);
553 break;
554 case cc::DrawQuad::STREAM_VIDEO_CONTENT:
555 LogParam(*cc::StreamVideoDrawQuad::MaterialCast(quad), l);
556 break;
557 case cc::DrawQuad::YUV_VIDEO_CONTENT:
558 LogParam(*cc::YUVVideoDrawQuad::MaterialCast(quad), l);
559 break;
560 case cc::DrawQuad::INVALID:
561 break;
564 l->append("])");
567 namespace {
568 enum CompositorFrameType {
569 NO_FRAME,
570 DELEGATED_FRAME,
571 GL_FRAME,
575 void ParamTraits<cc::CompositorFrame>::Write(Message* m,
576 const param_type& p) {
577 WriteParam(m, p.metadata);
578 if (p.delegated_frame_data) {
579 DCHECK(!p.gl_frame_data);
580 WriteParam(m, static_cast<int>(DELEGATED_FRAME));
581 WriteParam(m, *p.delegated_frame_data);
582 } else if (p.gl_frame_data) {
583 WriteParam(m, static_cast<int>(GL_FRAME));
584 WriteParam(m, *p.gl_frame_data);
585 } else {
586 WriteParam(m, static_cast<int>(NO_FRAME));
590 bool ParamTraits<cc::CompositorFrame>::Read(const Message* m,
591 base::PickleIterator* iter,
592 param_type* p) {
593 if (!ReadParam(m, iter, &p->metadata))
594 return false;
596 int compositor_frame_type;
597 if (!ReadParam(m, iter, &compositor_frame_type))
598 return false;
600 switch (compositor_frame_type) {
601 case DELEGATED_FRAME:
602 p->delegated_frame_data.reset(new cc::DelegatedFrameData());
603 if (!ReadParam(m, iter, p->delegated_frame_data.get()))
604 return false;
605 break;
606 case GL_FRAME:
607 p->gl_frame_data.reset(new cc::GLFrameData());
608 if (!ReadParam(m, iter, p->gl_frame_data.get()))
609 return false;
610 break;
611 case NO_FRAME:
612 break;
613 default:
614 return false;
616 return true;
619 void ParamTraits<cc::CompositorFrame>::Log(const param_type& p,
620 std::string* l) {
621 l->append("CompositorFrame(");
622 LogParam(p.metadata, l);
623 l->append(", ");
624 if (p.delegated_frame_data)
625 LogParam(*p.delegated_frame_data, l);
626 else if (p.gl_frame_data)
627 LogParam(*p.gl_frame_data, l);
628 l->append(")");
631 void ParamTraits<cc::CompositorFrameAck>::Write(Message* m,
632 const param_type& p) {
633 WriteParam(m, p.resources);
634 if (p.gl_frame_data) {
635 WriteParam(m, static_cast<int>(GL_FRAME));
636 WriteParam(m, *p.gl_frame_data);
637 } else {
638 WriteParam(m, static_cast<int>(NO_FRAME));
642 bool ParamTraits<cc::CompositorFrameAck>::Read(const Message* m,
643 base::PickleIterator* iter,
644 param_type* p) {
645 if (!ReadParam(m, iter, &p->resources))
646 return false;
648 int compositor_frame_type;
649 if (!ReadParam(m, iter, &compositor_frame_type))
650 return false;
652 switch (compositor_frame_type) {
653 case NO_FRAME:
654 break;
655 case GL_FRAME:
656 p->gl_frame_data.reset(new cc::GLFrameData());
657 if (!ReadParam(m, iter, p->gl_frame_data.get()))
658 return false;
659 break;
660 default:
661 return false;
663 return true;
666 void ParamTraits<cc::CompositorFrameAck>::Log(const param_type& p,
667 std::string* l) {
668 l->append("CompositorFrameAck(");
669 LogParam(p.resources, l);
670 l->append(", ");
671 if (p.gl_frame_data)
672 LogParam(*p.gl_frame_data, l);
673 l->append(")");
676 void ParamTraits<cc::DelegatedFrameData>::Write(Message* m,
677 const param_type& p) {
678 DCHECK_NE(0u, p.render_pass_list.size());
680 size_t to_reserve = sizeof(p.device_scale_factor);
681 to_reserve += p.resource_list.size() * sizeof(cc::TransferableResource);
682 for (size_t i = 0; i < p.render_pass_list.size(); ++i) {
683 const cc::RenderPass* pass = p.render_pass_list[i];
684 to_reserve += sizeof(size_t) * 2;
685 to_reserve += ReserveSizeForRenderPassWrite(*pass);
687 m->Reserve(to_reserve);
689 WriteParam(m, p.device_scale_factor);
690 WriteParam(m, p.resource_list);
691 WriteParam(m, p.render_pass_list.size());
692 for (const auto* pass : p.render_pass_list) {
693 WriteParam(m, pass->quad_list.size());
694 WriteParam(m, pass->shared_quad_state_list.size());
695 WriteParam(m, *pass);
699 bool ParamTraits<cc::DelegatedFrameData>::Read(const Message* m,
700 base::PickleIterator* iter,
701 param_type* p) {
702 if (!ReadParam(m, iter, &p->device_scale_factor))
703 return false;
705 const static size_t kMaxRenderPasses = 10000;
706 const static size_t kMaxSharedQuadStateListSize = 100000;
707 const static size_t kMaxQuadListSize = 1000000;
709 std::set<cc::RenderPassId> pass_set;
711 size_t num_render_passes;
712 if (!ReadParam(m, iter, &p->resource_list) ||
713 !ReadParam(m, iter, &num_render_passes) ||
714 num_render_passes > kMaxRenderPasses || num_render_passes == 0)
715 return false;
716 for (size_t i = 0; i < num_render_passes; ++i) {
717 size_t quad_list_size;
718 size_t shared_quad_state_list_size;
719 if (!ReadParam(m, iter, &quad_list_size) ||
720 !ReadParam(m, iter, &shared_quad_state_list_size) ||
721 quad_list_size > kMaxQuadListSize ||
722 shared_quad_state_list_size > kMaxSharedQuadStateListSize)
723 return false;
724 scoped_ptr<cc::RenderPass> render_pass =
725 cc::RenderPass::Create(shared_quad_state_list_size, quad_list_size);
726 if (!ReadParam(m, iter, render_pass.get()))
727 return false;
728 // Validate that each RenderPassDrawQuad points at a valid RenderPass
729 // earlier in the frame.
730 for (const auto* quad : render_pass->quad_list) {
731 if (quad->material != cc::DrawQuad::RENDER_PASS)
732 continue;
733 const cc::RenderPassDrawQuad* rpdq =
734 cc::RenderPassDrawQuad::MaterialCast(quad);
735 if (!pass_set.count(rpdq->render_pass_id))
736 return false;
738 pass_set.insert(render_pass->id);
739 p->render_pass_list.push_back(render_pass.Pass());
741 return true;
744 void ParamTraits<cc::DelegatedFrameData>::Log(const param_type& p,
745 std::string* l) {
746 l->append("DelegatedFrameData(");
747 LogParam(p.device_scale_factor, l);
748 LogParam(p.resource_list, l);
749 l->append(", [");
750 for (size_t i = 0; i < p.render_pass_list.size(); ++i) {
751 if (i)
752 l->append(", ");
753 LogParam(*p.render_pass_list[i], l);
755 l->append("])");
758 void ParamTraits<cc::DrawQuad::Resources>::Write(Message* m,
759 const param_type& p) {
760 DCHECK_LE(p.count, cc::DrawQuad::Resources::kMaxResourceIdCount);
761 WriteParam(m, p.count);
762 for (size_t i = 0; i < p.count; ++i)
763 WriteParam(m, p.ids[i]);
766 bool ParamTraits<cc::DrawQuad::Resources>::Read(const Message* m,
767 base::PickleIterator* iter,
768 param_type* p) {
769 if (!ReadParam(m, iter, &p->count))
770 return false;
771 if (p->count > cc::DrawQuad::Resources::kMaxResourceIdCount)
772 return false;
773 for (size_t i = 0; i < p->count; ++i) {
774 if (!ReadParam(m, iter, &p->ids[i]))
775 return false;
777 return true;
780 void ParamTraits<cc::DrawQuad::Resources>::Log(const param_type& p,
781 std::string* l) {
782 l->append("DrawQuad::Resources(");
783 LogParam(p.count, l);
784 l->append(", [");
785 if (p.count > cc::DrawQuad::Resources::kMaxResourceIdCount) {
786 l->append("])");
787 return;
790 for (size_t i = 0; i < p.count; ++i) {
791 LogParam(p.ids[i], l);
792 if (i < (p.count - 1))
793 l->append(", ");
795 l->append("])");
798 void ParamTraits<cc::StreamVideoDrawQuad::OverlayResources>::Write(
799 Message* m,
800 const param_type& p) {
801 for (size_t i = 0; i < cc::DrawQuad::Resources::kMaxResourceIdCount; ++i) {
802 WriteParam(m, p.size_in_pixels[i]);
803 WriteParam(m, p.allow_overlay[i]);
807 bool ParamTraits<cc::StreamVideoDrawQuad::OverlayResources>::Read(
808 const Message* m,
809 base::PickleIterator* iter,
810 param_type* p) {
811 for (size_t i = 0; i < cc::DrawQuad::Resources::kMaxResourceIdCount; ++i) {
812 if (!ReadParam(m, iter, &p->size_in_pixels[i]))
813 return false;
814 if (!ReadParam(m, iter, &p->allow_overlay[i]))
815 return false;
817 return true;
820 void ParamTraits<cc::StreamVideoDrawQuad::OverlayResources>::Log(
821 const param_type& p,
822 std::string* l) {
823 l->append("StreamVideoDrawQuad::OverlayResources([");
824 for (size_t i = 0; i < cc::DrawQuad::Resources::kMaxResourceIdCount; ++i) {
825 LogParam(p.size_in_pixels[i], l);
826 l->append(", ");
827 LogParam(p.allow_overlay[i], l);
828 if (i < (cc::DrawQuad::Resources::kMaxResourceIdCount - 1))
829 l->append(", ");
831 l->append("])");
834 void ParamTraits<cc::TextureDrawQuad::OverlayResources>::Write(
835 Message* m,
836 const param_type& p) {
837 for (size_t i = 0; i < cc::DrawQuad::Resources::kMaxResourceIdCount; ++i) {
838 WriteParam(m, p.size_in_pixels[i]);
839 WriteParam(m, p.allow_overlay[i]);
843 bool ParamTraits<cc::TextureDrawQuad::OverlayResources>::Read(
844 const Message* m,
845 base::PickleIterator* iter,
846 param_type* p) {
847 for (size_t i = 0; i < cc::DrawQuad::Resources::kMaxResourceIdCount; ++i) {
848 if (!ReadParam(m, iter, &p->size_in_pixels[i]))
849 return false;
850 if (!ReadParam(m, iter, &p->allow_overlay[i]))
851 return false;
853 return true;
856 void ParamTraits<cc::TextureDrawQuad::OverlayResources>::Log(
857 const param_type& p,
858 std::string* l) {
859 l->append("TextureDrawQuad::OverlayResources([");
860 for (size_t i = 0; i < cc::DrawQuad::Resources::kMaxResourceIdCount; ++i) {
861 LogParam(p.size_in_pixels[i], l);
862 l->append(", ");
863 LogParam(p.allow_overlay[i], l);
864 if (i < (cc::DrawQuad::Resources::kMaxResourceIdCount - 1))
865 l->append(", ");
867 l->append("])");
870 } // namespace IPC