Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 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. | |
| 4 | |
| 5 #ifndef CC_IPC_QUADS_STRUCT_TRAITS_H_ | |
| 6 #define CC_IPC_QUADS_STRUCT_TRAITS_H_ | |
| 7 | |
| 8 #include "cc/ipc/quads.mojom.h" | |
| 9 #include "cc/quads/debug_border_draw_quad.h" | |
| 10 #include "cc/quads/picture_draw_quad.h" | |
| 11 #include "cc/quads/render_pass_draw_quad.h" | |
| 12 #include "cc/quads/solid_color_draw_quad.h" | |
| 13 #include "cc/quads/stream_video_draw_quad.h" | |
| 14 #include "cc/quads/surface_draw_quad.h" | |
| 15 #include "cc/quads/texture_draw_quad.h" | |
| 16 #include "cc/quads/tile_draw_quad.h" | |
| 17 #include "cc/quads/yuv_video_draw_quad.h" | |
| 18 | |
| 19 namespace mojo { | |
| 20 | |
| 21 namespace { | |
| 22 | |
| 23 cc::mojom::Material CCMaterialToMojo(cc::DrawQuad::Material material) { | |
| 24 switch (material) { | |
| 25 case cc::DrawQuad::INVALID: | |
| 26 break; | |
| 27 case cc::DrawQuad::DEBUG_BORDER: | |
| 28 return cc::mojom::Material::DEBUG_BORDER; | |
| 29 case cc::DrawQuad::PICTURE_CONTENT: | |
| 30 return cc::mojom::Material::PICTURE_CONTENT; | |
| 31 case cc::DrawQuad::RENDER_PASS: | |
| 32 return cc::mojom::Material::RENDER_PASS; | |
| 33 case cc::DrawQuad::SOLID_COLOR: | |
| 34 return cc::mojom::Material::SOLID_COLOR; | |
| 35 case cc::DrawQuad::STREAM_VIDEO_CONTENT: | |
| 36 return cc::mojom::Material::STREAM_VIDEO_CONTENT; | |
| 37 case cc::DrawQuad::SURFACE_CONTENT: | |
| 38 return cc::mojom::Material::SURFACE_CONTENT; | |
| 39 case cc::DrawQuad::TEXTURE_CONTENT: | |
| 40 return cc::mojom::Material::TEXTURE_CONTENT; | |
| 41 case cc::DrawQuad::TILED_CONTENT: | |
| 42 return cc::mojom::Material::TILED_CONTENT; | |
| 43 case cc::DrawQuad::YUV_VIDEO_CONTENT: | |
| 44 return cc::mojom::Material::YUV_VIDEO_CONTENT; | |
| 45 } | |
| 46 NOTREACHED(); | |
| 47 return cc::mojom::Material::MATERIAL_LAST; | |
| 48 } | |
| 49 | |
| 50 cc::DrawQuad::Material MojoMaterialToCC(cc::mojom::Material material) { | |
| 51 switch (material) { | |
| 52 case cc::mojom::Material::DEBUG_BORDER: | |
| 53 return cc::DrawQuad::DEBUG_BORDER; | |
| 54 case cc::mojom::Material::PICTURE_CONTENT: | |
| 55 return cc::DrawQuad::PICTURE_CONTENT; | |
| 56 case cc::mojom::Material::RENDER_PASS: | |
| 57 return cc::DrawQuad::RENDER_PASS; | |
| 58 case cc::mojom::Material::SOLID_COLOR: | |
| 59 return cc::DrawQuad::SOLID_COLOR; | |
| 60 case cc::mojom::Material::STREAM_VIDEO_CONTENT: | |
| 61 return cc::DrawQuad::STREAM_VIDEO_CONTENT; | |
| 62 case cc::mojom::Material::SURFACE_CONTENT: | |
| 63 return cc::DrawQuad::SURFACE_CONTENT; | |
| 64 case cc::mojom::Material::TEXTURE_CONTENT: | |
| 65 return cc::DrawQuad::TEXTURE_CONTENT; | |
| 66 case cc::mojom::Material::TILED_CONTENT: | |
| 67 return cc::DrawQuad::TILED_CONTENT; | |
| 68 case cc::mojom::Material::YUV_VIDEO_CONTENT: | |
| 69 return cc::DrawQuad::YUV_VIDEO_CONTENT; | |
| 70 } | |
| 71 NOTREACHED(); | |
| 72 return cc::DrawQuad::INVALID; | |
| 73 } | |
| 74 | |
| 75 bool ReadDrawQuad(cc::mojom::DrawQuadDataView data, cc::DrawQuad* quad) { | |
| 76 quad->material = MojoMaterialToCC(data.material()); | |
| 77 if (!data.ReadRect(&quad->rect) || !data.ReadOpaqueRect(&quad->opaque_rect) || | |
| 78 !data.ReadVisibleRect(&quad->visible_rect)) { | |
| 79 return false; | |
| 80 } | |
| 81 quad->needs_blending = data.needs_blending(); | |
| 82 return true; | |
| 83 } | |
| 84 | |
| 85 void AllocateAndConstruct(cc::DrawQuad::Material material, cc::QuadList* list) { | |
| 86 switch (material) { | |
| 87 case cc::DrawQuad::INVALID: | |
| 88 break; | |
| 89 case cc::DrawQuad::DEBUG_BORDER: | |
| 90 list->AllocateAndConstruct<cc::DebugBorderDrawQuad>(); | |
| 91 return; | |
| 92 case cc::DrawQuad::PICTURE_CONTENT: | |
| 93 list->AllocateAndConstruct<cc::PictureDrawQuad>(); | |
| 94 return; | |
| 95 case cc::DrawQuad::RENDER_PASS: | |
| 96 list->AllocateAndConstruct<cc::RenderPassDrawQuad>(); | |
| 97 return; | |
| 98 case cc::DrawQuad::SOLID_COLOR: | |
| 99 list->AllocateAndConstruct<cc::SolidColorDrawQuad>(); | |
| 100 return; | |
| 101 case cc::DrawQuad::STREAM_VIDEO_CONTENT: | |
| 102 list->AllocateAndConstruct<cc::StreamVideoDrawQuad>(); | |
| 103 return; | |
| 104 case cc::DrawQuad::SURFACE_CONTENT: | |
| 105 list->AllocateAndConstruct<cc::SurfaceDrawQuad>(); | |
| 106 return; | |
| 107 case cc::DrawQuad::TEXTURE_CONTENT: | |
| 108 list->AllocateAndConstruct<cc::TextureDrawQuad>(); | |
| 109 return; | |
| 110 case cc::DrawQuad::TILED_CONTENT: | |
| 111 list->AllocateAndConstruct<cc::TileDrawQuad>(); | |
| 112 return; | |
| 113 case cc::DrawQuad::YUV_VIDEO_CONTENT: | |
| 114 list->AllocateAndConstruct<cc::YUVVideoDrawQuad>(); | |
| 115 return; | |
| 116 } | |
| 117 NOTREACHED(); | |
| 118 } | |
| 119 | |
| 120 } // namespace | |
| 121 | |
| 122 template <> | |
| 123 struct StructTraits<cc::mojom::DebugBorderQuadState, cc::DrawQuad> { | |
| 124 static bool IsNull(const cc::DrawQuad& input) { | |
| 125 return input.material != cc::DrawQuad::DEBUG_BORDER; | |
|
dcheng
2016/06/15 12:27:19
This is an interesting application of nullable typ
Fady Samuel
2016/06/15 14:46:25
I'm not a fan either but I can't think of a better
| |
| 126 } | |
| 127 | |
| 128 static void SetToNull(cc::DrawQuad* output) {} | |
| 129 | |
| 130 static uint32_t color(const cc::DrawQuad& input) { | |
| 131 const cc::DebugBorderDrawQuad* quad = | |
| 132 cc::DebugBorderDrawQuad::MaterialCast(&input); | |
| 133 return quad->color; | |
| 134 } | |
| 135 | |
| 136 static int32_t width(const cc::DrawQuad& input) { | |
| 137 const cc::DebugBorderDrawQuad* quad = | |
| 138 cc::DebugBorderDrawQuad::MaterialCast(&input); | |
| 139 return quad->width; | |
| 140 } | |
| 141 | |
| 142 static bool Read(cc::mojom::DebugBorderQuadStateDataView data, | |
| 143 cc::DrawQuad* out) { | |
| 144 cc::DebugBorderDrawQuad* quad = static_cast<cc::DebugBorderDrawQuad*>(out); | |
| 145 quad->color = data.color(); | |
| 146 quad->width = data.width(); | |
| 147 return true; | |
| 148 } | |
| 149 }; | |
| 150 | |
| 151 template <> | |
| 152 struct StructTraits<cc::mojom::RenderPassQuadState, cc::DrawQuad> { | |
| 153 static bool IsNull(const cc::DrawQuad& quad) { | |
| 154 return quad.material != cc::DrawQuad::RENDER_PASS; | |
| 155 } | |
| 156 | |
| 157 static void SetToNull(cc::DrawQuad* output) {} | |
| 158 | |
| 159 static const cc::RenderPassId& render_pass_id(const cc::DrawQuad& input) { | |
| 160 const cc::RenderPassDrawQuad* quad = | |
| 161 cc::RenderPassDrawQuad::MaterialCast(&input); | |
| 162 return quad->render_pass_id; | |
| 163 } | |
| 164 | |
| 165 static uint32_t mask_resource_id(const cc::DrawQuad& input) { | |
| 166 const cc::RenderPassDrawQuad* quad = | |
| 167 cc::RenderPassDrawQuad::MaterialCast(&input); | |
| 168 return quad->mask_resource_id(); | |
| 169 } | |
| 170 | |
| 171 static const gfx::Vector2dF& mask_uv_scale(const cc::DrawQuad& input) { | |
| 172 const cc::RenderPassDrawQuad* quad = | |
| 173 cc::RenderPassDrawQuad::MaterialCast(&input); | |
| 174 return quad->mask_uv_scale; | |
| 175 } | |
| 176 | |
| 177 static const gfx::Size& mask_texture_size(const cc::DrawQuad& input) { | |
| 178 const cc::RenderPassDrawQuad* quad = | |
| 179 cc::RenderPassDrawQuad::MaterialCast(&input); | |
| 180 return quad->mask_texture_size; | |
| 181 } | |
| 182 | |
| 183 static const cc::FilterOperations& filters(const cc::DrawQuad& input) { | |
| 184 const cc::RenderPassDrawQuad* quad = | |
| 185 cc::RenderPassDrawQuad::MaterialCast(&input); | |
| 186 return quad->filters; | |
| 187 } | |
| 188 | |
| 189 static const gfx::Vector2dF& filters_scale(const cc::DrawQuad& input) { | |
| 190 const cc::RenderPassDrawQuad* quad = | |
| 191 cc::RenderPassDrawQuad::MaterialCast(&input); | |
| 192 return quad->filters_scale; | |
| 193 } | |
| 194 | |
| 195 static const cc::FilterOperations& background_filters( | |
| 196 const cc::DrawQuad& input) { | |
| 197 const cc::RenderPassDrawQuad* quad = | |
| 198 cc::RenderPassDrawQuad::MaterialCast(&input); | |
| 199 return quad->background_filters; | |
| 200 } | |
| 201 | |
| 202 static bool Read(cc::mojom::RenderPassQuadStateDataView data, | |
| 203 cc::DrawQuad* out) { | |
|
dcheng
2016/06/15 12:27:19
Let's out-of-line the non-trivial methods (in this
Fady Samuel
2016/06/15 14:46:25
Done.
| |
| 204 cc::RenderPassDrawQuad* quad = static_cast<cc::RenderPassDrawQuad*>(out); | |
| 205 quad->resources.ids[cc::RenderPassDrawQuad::kMaskResourceIdIndex] = | |
| 206 data.mask_resource_id(); | |
| 207 return data.ReadRenderPassId(&quad->render_pass_id) && | |
| 208 data.ReadMaskUvScale(&quad->mask_uv_scale) && | |
| 209 data.ReadMaskTextureSize(&quad->mask_texture_size) && | |
| 210 data.ReadFilters(&quad->filters) && | |
| 211 data.ReadFiltersScale(&quad->filters_scale) && | |
| 212 data.ReadBackgroundFilters(&quad->background_filters); | |
| 213 } | |
| 214 }; | |
| 215 | |
| 216 template <> | |
| 217 struct StructTraits<cc::mojom::SolidColorQuadState, cc::DrawQuad> { | |
| 218 static bool IsNull(const cc::DrawQuad& input) { | |
| 219 return input.material != cc::DrawQuad::SOLID_COLOR; | |
| 220 } | |
| 221 | |
| 222 static void SetToNull(cc::DrawQuad* output) {} | |
| 223 | |
| 224 static uint32_t color(const cc::DrawQuad& input) { | |
| 225 const cc::SolidColorDrawQuad* quad = | |
| 226 cc::SolidColorDrawQuad::MaterialCast(&input); | |
| 227 return quad->color; | |
| 228 } | |
| 229 | |
| 230 static bool force_anti_aliasing_off(const cc::DrawQuad& input) { | |
| 231 const cc::SolidColorDrawQuad* quad = | |
| 232 cc::SolidColorDrawQuad::MaterialCast(&input); | |
| 233 return quad->force_anti_aliasing_off; | |
| 234 } | |
| 235 | |
| 236 static bool Read(cc::mojom::SolidColorQuadStateDataView data, | |
| 237 cc::DrawQuad* out) { | |
| 238 cc::SolidColorDrawQuad* quad = static_cast<cc::SolidColorDrawQuad*>(out); | |
| 239 quad->force_anti_aliasing_off = data.force_anti_aliasing_off(); | |
| 240 quad->color = data.color(); | |
| 241 return true; | |
| 242 } | |
| 243 }; | |
| 244 | |
| 245 template <> | |
| 246 struct StructTraits<cc::mojom::SurfaceQuadState, cc::DrawQuad> { | |
| 247 static bool IsNull(const cc::DrawQuad& input) { | |
| 248 return input.material != cc::DrawQuad::SURFACE_CONTENT; | |
| 249 } | |
| 250 | |
| 251 static void SetToNull(cc::DrawQuad* output) {} | |
| 252 | |
| 253 static const cc::SurfaceId& surface(const cc::DrawQuad& input) { | |
| 254 const cc::SurfaceDrawQuad* quad = cc::SurfaceDrawQuad::MaterialCast(&input); | |
| 255 return quad->surface_id; | |
| 256 } | |
| 257 | |
| 258 static bool Read(cc::mojom::SurfaceQuadStateDataView data, | |
| 259 cc::DrawQuad* out) { | |
| 260 cc::SurfaceDrawQuad* quad = static_cast<cc::SurfaceDrawQuad*>(out); | |
| 261 return data.ReadSurface(&quad->surface_id); | |
| 262 } | |
| 263 }; | |
| 264 | |
| 265 template <> | |
| 266 struct StructTraits<cc::mojom::DrawQuad, cc::DrawQuad> { | |
| 267 static cc::mojom::Material material(const cc::DrawQuad& quad) { | |
| 268 return CCMaterialToMojo(quad.material); | |
| 269 } | |
| 270 | |
| 271 static const gfx::Rect& rect(const cc::DrawQuad& quad) { return quad.rect; } | |
| 272 | |
| 273 static const gfx::Rect& opaque_rect(const cc::DrawQuad& quad) { | |
| 274 return quad.opaque_rect; | |
| 275 } | |
| 276 | |
| 277 static const gfx::Rect& visible_rect(const cc::DrawQuad& quad) { | |
| 278 return quad.visible_rect; | |
| 279 } | |
| 280 | |
| 281 static bool needs_blending(const cc::DrawQuad& quad) { | |
| 282 return quad.needs_blending; | |
| 283 } | |
| 284 | |
| 285 static uint32_t shared_quad_state_index(const cc::DrawQuad& quad) { | |
| 286 return 0; | |
| 287 } | |
| 288 | |
| 289 static const cc::DrawQuad& debug_border_quad_state(const cc::DrawQuad& quad) { | |
| 290 return quad; | |
| 291 } | |
| 292 | |
| 293 static const cc::DrawQuad& render_pass_quad_state(const cc::DrawQuad& quad) { | |
| 294 return quad; | |
| 295 } | |
| 296 | |
| 297 static const cc::DrawQuad& solid_color_quad_state(const cc::DrawQuad& quad) { | |
| 298 return quad; | |
| 299 } | |
| 300 | |
| 301 static const cc::DrawQuad& surface_quad_state(const cc::DrawQuad& quad) { | |
| 302 return quad; | |
| 303 } | |
| 304 | |
| 305 static cc::mojom::TextureQuadStatePtr texture_quad_state( | |
| 306 const cc::DrawQuad& quad) { | |
| 307 return nullptr; | |
| 308 } | |
| 309 | |
| 310 static cc::mojom::TileQuadStatePtr tile_quad_state(const cc::DrawQuad& quad) { | |
| 311 return nullptr; | |
| 312 } | |
| 313 | |
| 314 static cc::mojom::StreamVideoQuadStatePtr stream_video_quad_state( | |
| 315 const cc::DrawQuad& data) { | |
| 316 return nullptr; | |
| 317 } | |
| 318 | |
| 319 static cc::mojom::YUVVideoQuadStatePtr yuv_video_quad_state( | |
| 320 const cc::DrawQuad& data) { | |
| 321 return nullptr; | |
| 322 } | |
| 323 | |
| 324 static bool Read(cc::mojom::DrawQuadDataView data, cc::DrawQuad* out) { | |
| 325 if (!ReadDrawQuad(data, out)) | |
| 326 return false; | |
| 327 switch (data.material()) { | |
| 328 case cc::mojom::Material::DEBUG_BORDER: | |
| 329 return data.ReadDebugBorderQuadState(out); | |
| 330 case cc::mojom::Material::PICTURE_CONTENT: | |
| 331 // TODO(fsamuel): Implement PictureDrawQuad | |
| 332 // serialization/deserialization. | |
| 333 break; | |
| 334 case cc::mojom::Material::RENDER_PASS: | |
| 335 return data.ReadRenderPassQuadState(out); | |
| 336 case cc::mojom::Material::SOLID_COLOR: | |
| 337 return data.ReadSolidColorQuadState(out); | |
| 338 case cc::mojom::Material::STREAM_VIDEO_CONTENT: | |
| 339 // TODO(fsamuel): Implement StreamVideoContentDrawQuad | |
| 340 // serialization/deserialization. | |
| 341 break; | |
| 342 case cc::mojom::Material::SURFACE_CONTENT: | |
| 343 return data.ReadSurfaceQuadState(out); | |
| 344 case cc::mojom::Material::TEXTURE_CONTENT: | |
| 345 // TODO(fsamuel): Implement TextureDrawQuad | |
| 346 // serialization/deserialization. | |
| 347 break; | |
| 348 case cc::mojom::Material::TILED_CONTENT: | |
| 349 // TODO(fsamuel): Implement TileDrawQuad | |
| 350 // serialization/deserialization. | |
| 351 break; | |
| 352 case cc::mojom::Material::YUV_VIDEO_CONTENT: | |
| 353 // TODO(fsamuel): Implement YUVVideoDrawQuad | |
| 354 // serialization/deserialization. | |
| 355 break; | |
| 356 } | |
| 357 NOTREACHED(); | |
| 358 return false; | |
| 359 } | |
| 360 }; | |
| 361 | |
| 362 struct QuadListArray { | |
| 363 cc::QuadList* list; | |
| 364 }; | |
| 365 | |
| 366 template <> | |
| 367 struct ArrayTraits<QuadListArray> { | |
| 368 using Element = cc::DrawQuad; | |
| 369 using Iterator = cc::QuadList::Iterator; | |
| 370 using ConstIterator = cc::QuadList::ConstIterator; | |
| 371 | |
| 372 static ConstIterator GetBegin(const QuadListArray& input) { | |
| 373 return input.list->begin(); | |
| 374 } | |
| 375 static Iterator GetBegin(QuadListArray& input) { return input.list->begin(); } | |
| 376 | |
| 377 static void AdvanceIterator(ConstIterator& iterator) { iterator++; } | |
| 378 static void AdvanceIterator(Iterator& iterator) { iterator++; } | |
| 379 | |
| 380 static const Element& GetValue(ConstIterator& iterator) { return **iterator; } | |
| 381 static Element& GetValue(Iterator& iterator) { return **iterator; } | |
| 382 | |
| 383 static size_t GetSize(const QuadListArray& input) { | |
| 384 return input.list->size(); | |
| 385 } | |
| 386 | |
| 387 static bool Resize(QuadListArray& input, size_t size) { return true; } | |
| 388 }; | |
| 389 | |
| 390 template <> | |
| 391 struct StructTraits<cc::mojom::QuadList, cc::QuadList> { | |
| 392 static mojo::Array<cc::mojom::Material> quad_types( | |
| 393 const cc::QuadList& quad_list) { | |
| 394 mojo::Array<cc::mojom::Material> materials = | |
| 395 mojo::Array<cc::mojom::Material>::New(quad_list.size()); | |
| 396 for (size_t i = 0; i < quad_list.size(); ++i) | |
| 397 materials[i] = CCMaterialToMojo(quad_list.ElementAt(i)->material); | |
| 398 return materials; | |
| 399 } | |
| 400 | |
| 401 static QuadListArray quads(const cc::QuadList& quad_list) { | |
| 402 return {const_cast<cc::QuadList*>(&quad_list)}; | |
| 403 } | |
| 404 | |
| 405 static bool Read(cc::mojom::QuadListDataView data, cc::QuadList* out) { | |
| 406 // TODO(fsamuel): Once we have ArrayTraits DataViews we can delete | |
| 407 // this field. This field exists so we can pre-allocate DrawQuads | |
| 408 // in the QuadList according to their material. | |
| 409 mojo::Array<cc::mojom::Material> materials; | |
| 410 if (!data.ReadQuadTypes(&materials)) | |
| 411 return false; | |
| 412 for (size_t i = 0; i < materials.size(); ++i) { | |
| 413 cc::DrawQuad::Material material = MojoMaterialToCC(materials[i]); | |
| 414 AllocateAndConstruct(material, out); | |
| 415 } | |
| 416 QuadListArray quad_list_array = {out}; | |
| 417 return data.ReadQuads(&quad_list_array); | |
| 418 } | |
| 419 }; | |
| 420 | |
| 421 } // namespace mojo | |
| 422 | |
| 423 #endif // CC_IPC_QUADS_STRUCT_TRAITS_H_ | |
| OLD | NEW |