Chromium Code Reviews| Index: cc/ipc/quads_struct_traits.h |
| diff --git a/cc/ipc/quads_struct_traits.h b/cc/ipc/quads_struct_traits.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..c9dba11ba4657c2a156bb3a19fe114cc5eb452d5 |
| --- /dev/null |
| +++ b/cc/ipc/quads_struct_traits.h |
| @@ -0,0 +1,423 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#ifndef CC_IPC_QUADS_STRUCT_TRAITS_H_ |
| +#define CC_IPC_QUADS_STRUCT_TRAITS_H_ |
| + |
| +#include "cc/ipc/quads.mojom.h" |
| +#include "cc/quads/debug_border_draw_quad.h" |
| +#include "cc/quads/picture_draw_quad.h" |
| +#include "cc/quads/render_pass_draw_quad.h" |
| +#include "cc/quads/solid_color_draw_quad.h" |
| +#include "cc/quads/stream_video_draw_quad.h" |
| +#include "cc/quads/surface_draw_quad.h" |
| +#include "cc/quads/texture_draw_quad.h" |
| +#include "cc/quads/tile_draw_quad.h" |
| +#include "cc/quads/yuv_video_draw_quad.h" |
| + |
| +namespace mojo { |
| + |
| +namespace { |
| + |
| +cc::mojom::Material CCMaterialToMojo(cc::DrawQuad::Material material) { |
| + switch (material) { |
| + case cc::DrawQuad::INVALID: |
| + break; |
| + case cc::DrawQuad::DEBUG_BORDER: |
| + return cc::mojom::Material::DEBUG_BORDER; |
| + case cc::DrawQuad::PICTURE_CONTENT: |
| + return cc::mojom::Material::PICTURE_CONTENT; |
| + case cc::DrawQuad::RENDER_PASS: |
| + return cc::mojom::Material::RENDER_PASS; |
| + case cc::DrawQuad::SOLID_COLOR: |
| + return cc::mojom::Material::SOLID_COLOR; |
| + case cc::DrawQuad::STREAM_VIDEO_CONTENT: |
| + return cc::mojom::Material::STREAM_VIDEO_CONTENT; |
| + case cc::DrawQuad::SURFACE_CONTENT: |
| + return cc::mojom::Material::SURFACE_CONTENT; |
| + case cc::DrawQuad::TEXTURE_CONTENT: |
| + return cc::mojom::Material::TEXTURE_CONTENT; |
| + case cc::DrawQuad::TILED_CONTENT: |
| + return cc::mojom::Material::TILED_CONTENT; |
| + case cc::DrawQuad::YUV_VIDEO_CONTENT: |
| + return cc::mojom::Material::YUV_VIDEO_CONTENT; |
| + } |
| + NOTREACHED(); |
| + return cc::mojom::Material::MATERIAL_LAST; |
| +} |
| + |
| +cc::DrawQuad::Material MojoMaterialToCC(cc::mojom::Material material) { |
| + switch (material) { |
| + case cc::mojom::Material::DEBUG_BORDER: |
| + return cc::DrawQuad::DEBUG_BORDER; |
| + case cc::mojom::Material::PICTURE_CONTENT: |
| + return cc::DrawQuad::PICTURE_CONTENT; |
| + case cc::mojom::Material::RENDER_PASS: |
| + return cc::DrawQuad::RENDER_PASS; |
| + case cc::mojom::Material::SOLID_COLOR: |
| + return cc::DrawQuad::SOLID_COLOR; |
| + case cc::mojom::Material::STREAM_VIDEO_CONTENT: |
| + return cc::DrawQuad::STREAM_VIDEO_CONTENT; |
| + case cc::mojom::Material::SURFACE_CONTENT: |
| + return cc::DrawQuad::SURFACE_CONTENT; |
| + case cc::mojom::Material::TEXTURE_CONTENT: |
| + return cc::DrawQuad::TEXTURE_CONTENT; |
| + case cc::mojom::Material::TILED_CONTENT: |
| + return cc::DrawQuad::TILED_CONTENT; |
| + case cc::mojom::Material::YUV_VIDEO_CONTENT: |
| + return cc::DrawQuad::YUV_VIDEO_CONTENT; |
| + } |
| + NOTREACHED(); |
| + return cc::DrawQuad::INVALID; |
| +} |
| + |
| +bool ReadDrawQuad(cc::mojom::DrawQuadDataView data, cc::DrawQuad* quad) { |
| + quad->material = MojoMaterialToCC(data.material()); |
| + if (!data.ReadRect(&quad->rect) || !data.ReadOpaqueRect(&quad->opaque_rect) || |
| + !data.ReadVisibleRect(&quad->visible_rect)) { |
| + return false; |
| + } |
| + quad->needs_blending = data.needs_blending(); |
| + return true; |
| +} |
| + |
| +void AllocateAndConstruct(cc::DrawQuad::Material material, cc::QuadList* list) { |
| + switch (material) { |
| + case cc::DrawQuad::INVALID: |
| + break; |
| + case cc::DrawQuad::DEBUG_BORDER: |
| + list->AllocateAndConstruct<cc::DebugBorderDrawQuad>(); |
| + return; |
| + case cc::DrawQuad::PICTURE_CONTENT: |
| + list->AllocateAndConstruct<cc::PictureDrawQuad>(); |
| + return; |
| + case cc::DrawQuad::RENDER_PASS: |
| + list->AllocateAndConstruct<cc::RenderPassDrawQuad>(); |
| + return; |
| + case cc::DrawQuad::SOLID_COLOR: |
| + list->AllocateAndConstruct<cc::SolidColorDrawQuad>(); |
| + return; |
| + case cc::DrawQuad::STREAM_VIDEO_CONTENT: |
| + list->AllocateAndConstruct<cc::StreamVideoDrawQuad>(); |
| + return; |
| + case cc::DrawQuad::SURFACE_CONTENT: |
| + list->AllocateAndConstruct<cc::SurfaceDrawQuad>(); |
| + return; |
| + case cc::DrawQuad::TEXTURE_CONTENT: |
| + list->AllocateAndConstruct<cc::TextureDrawQuad>(); |
| + return; |
| + case cc::DrawQuad::TILED_CONTENT: |
| + list->AllocateAndConstruct<cc::TileDrawQuad>(); |
| + return; |
| + case cc::DrawQuad::YUV_VIDEO_CONTENT: |
| + list->AllocateAndConstruct<cc::YUVVideoDrawQuad>(); |
| + return; |
| + } |
| + NOTREACHED(); |
| +} |
| + |
| +} // namespace |
| + |
| +template <> |
| +struct StructTraits<cc::mojom::DebugBorderQuadState, cc::DrawQuad> { |
| + static bool IsNull(const cc::DrawQuad& input) { |
| + 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
|
| + } |
| + |
| + static void SetToNull(cc::DrawQuad* output) {} |
| + |
| + static uint32_t color(const cc::DrawQuad& input) { |
| + const cc::DebugBorderDrawQuad* quad = |
| + cc::DebugBorderDrawQuad::MaterialCast(&input); |
| + return quad->color; |
| + } |
| + |
| + static int32_t width(const cc::DrawQuad& input) { |
| + const cc::DebugBorderDrawQuad* quad = |
| + cc::DebugBorderDrawQuad::MaterialCast(&input); |
| + return quad->width; |
| + } |
| + |
| + static bool Read(cc::mojom::DebugBorderQuadStateDataView data, |
| + cc::DrawQuad* out) { |
| + cc::DebugBorderDrawQuad* quad = static_cast<cc::DebugBorderDrawQuad*>(out); |
| + quad->color = data.color(); |
| + quad->width = data.width(); |
| + return true; |
| + } |
| +}; |
| + |
| +template <> |
| +struct StructTraits<cc::mojom::RenderPassQuadState, cc::DrawQuad> { |
| + static bool IsNull(const cc::DrawQuad& quad) { |
| + return quad.material != cc::DrawQuad::RENDER_PASS; |
| + } |
| + |
| + static void SetToNull(cc::DrawQuad* output) {} |
| + |
| + static const cc::RenderPassId& render_pass_id(const cc::DrawQuad& input) { |
| + const cc::RenderPassDrawQuad* quad = |
| + cc::RenderPassDrawQuad::MaterialCast(&input); |
| + return quad->render_pass_id; |
| + } |
| + |
| + static uint32_t mask_resource_id(const cc::DrawQuad& input) { |
| + const cc::RenderPassDrawQuad* quad = |
| + cc::RenderPassDrawQuad::MaterialCast(&input); |
| + return quad->mask_resource_id(); |
| + } |
| + |
| + static const gfx::Vector2dF& mask_uv_scale(const cc::DrawQuad& input) { |
| + const cc::RenderPassDrawQuad* quad = |
| + cc::RenderPassDrawQuad::MaterialCast(&input); |
| + return quad->mask_uv_scale; |
| + } |
| + |
| + static const gfx::Size& mask_texture_size(const cc::DrawQuad& input) { |
| + const cc::RenderPassDrawQuad* quad = |
| + cc::RenderPassDrawQuad::MaterialCast(&input); |
| + return quad->mask_texture_size; |
| + } |
| + |
| + static const cc::FilterOperations& filters(const cc::DrawQuad& input) { |
| + const cc::RenderPassDrawQuad* quad = |
| + cc::RenderPassDrawQuad::MaterialCast(&input); |
| + return quad->filters; |
| + } |
| + |
| + static const gfx::Vector2dF& filters_scale(const cc::DrawQuad& input) { |
| + const cc::RenderPassDrawQuad* quad = |
| + cc::RenderPassDrawQuad::MaterialCast(&input); |
| + return quad->filters_scale; |
| + } |
| + |
| + static const cc::FilterOperations& background_filters( |
| + const cc::DrawQuad& input) { |
| + const cc::RenderPassDrawQuad* quad = |
| + cc::RenderPassDrawQuad::MaterialCast(&input); |
| + return quad->background_filters; |
| + } |
| + |
| + static bool Read(cc::mojom::RenderPassQuadStateDataView data, |
| + 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.
|
| + cc::RenderPassDrawQuad* quad = static_cast<cc::RenderPassDrawQuad*>(out); |
| + quad->resources.ids[cc::RenderPassDrawQuad::kMaskResourceIdIndex] = |
| + data.mask_resource_id(); |
| + return data.ReadRenderPassId(&quad->render_pass_id) && |
| + data.ReadMaskUvScale(&quad->mask_uv_scale) && |
| + data.ReadMaskTextureSize(&quad->mask_texture_size) && |
| + data.ReadFilters(&quad->filters) && |
| + data.ReadFiltersScale(&quad->filters_scale) && |
| + data.ReadBackgroundFilters(&quad->background_filters); |
| + } |
| +}; |
| + |
| +template <> |
| +struct StructTraits<cc::mojom::SolidColorQuadState, cc::DrawQuad> { |
| + static bool IsNull(const cc::DrawQuad& input) { |
| + return input.material != cc::DrawQuad::SOLID_COLOR; |
| + } |
| + |
| + static void SetToNull(cc::DrawQuad* output) {} |
| + |
| + static uint32_t color(const cc::DrawQuad& input) { |
| + const cc::SolidColorDrawQuad* quad = |
| + cc::SolidColorDrawQuad::MaterialCast(&input); |
| + return quad->color; |
| + } |
| + |
| + static bool force_anti_aliasing_off(const cc::DrawQuad& input) { |
| + const cc::SolidColorDrawQuad* quad = |
| + cc::SolidColorDrawQuad::MaterialCast(&input); |
| + return quad->force_anti_aliasing_off; |
| + } |
| + |
| + static bool Read(cc::mojom::SolidColorQuadStateDataView data, |
| + cc::DrawQuad* out) { |
| + cc::SolidColorDrawQuad* quad = static_cast<cc::SolidColorDrawQuad*>(out); |
| + quad->force_anti_aliasing_off = data.force_anti_aliasing_off(); |
| + quad->color = data.color(); |
| + return true; |
| + } |
| +}; |
| + |
| +template <> |
| +struct StructTraits<cc::mojom::SurfaceQuadState, cc::DrawQuad> { |
| + static bool IsNull(const cc::DrawQuad& input) { |
| + return input.material != cc::DrawQuad::SURFACE_CONTENT; |
| + } |
| + |
| + static void SetToNull(cc::DrawQuad* output) {} |
| + |
| + static const cc::SurfaceId& surface(const cc::DrawQuad& input) { |
| + const cc::SurfaceDrawQuad* quad = cc::SurfaceDrawQuad::MaterialCast(&input); |
| + return quad->surface_id; |
| + } |
| + |
| + static bool Read(cc::mojom::SurfaceQuadStateDataView data, |
| + cc::DrawQuad* out) { |
| + cc::SurfaceDrawQuad* quad = static_cast<cc::SurfaceDrawQuad*>(out); |
| + return data.ReadSurface(&quad->surface_id); |
| + } |
| +}; |
| + |
| +template <> |
| +struct StructTraits<cc::mojom::DrawQuad, cc::DrawQuad> { |
| + static cc::mojom::Material material(const cc::DrawQuad& quad) { |
| + return CCMaterialToMojo(quad.material); |
| + } |
| + |
| + static const gfx::Rect& rect(const cc::DrawQuad& quad) { return quad.rect; } |
| + |
| + static const gfx::Rect& opaque_rect(const cc::DrawQuad& quad) { |
| + return quad.opaque_rect; |
| + } |
| + |
| + static const gfx::Rect& visible_rect(const cc::DrawQuad& quad) { |
| + return quad.visible_rect; |
| + } |
| + |
| + static bool needs_blending(const cc::DrawQuad& quad) { |
| + return quad.needs_blending; |
| + } |
| + |
| + static uint32_t shared_quad_state_index(const cc::DrawQuad& quad) { |
| + return 0; |
| + } |
| + |
| + static const cc::DrawQuad& debug_border_quad_state(const cc::DrawQuad& quad) { |
| + return quad; |
| + } |
| + |
| + static const cc::DrawQuad& render_pass_quad_state(const cc::DrawQuad& quad) { |
| + return quad; |
| + } |
| + |
| + static const cc::DrawQuad& solid_color_quad_state(const cc::DrawQuad& quad) { |
| + return quad; |
| + } |
| + |
| + static const cc::DrawQuad& surface_quad_state(const cc::DrawQuad& quad) { |
| + return quad; |
| + } |
| + |
| + static cc::mojom::TextureQuadStatePtr texture_quad_state( |
| + const cc::DrawQuad& quad) { |
| + return nullptr; |
| + } |
| + |
| + static cc::mojom::TileQuadStatePtr tile_quad_state(const cc::DrawQuad& quad) { |
| + return nullptr; |
| + } |
| + |
| + static cc::mojom::StreamVideoQuadStatePtr stream_video_quad_state( |
| + const cc::DrawQuad& data) { |
| + return nullptr; |
| + } |
| + |
| + static cc::mojom::YUVVideoQuadStatePtr yuv_video_quad_state( |
| + const cc::DrawQuad& data) { |
| + return nullptr; |
| + } |
| + |
| + static bool Read(cc::mojom::DrawQuadDataView data, cc::DrawQuad* out) { |
| + if (!ReadDrawQuad(data, out)) |
| + return false; |
| + switch (data.material()) { |
| + case cc::mojom::Material::DEBUG_BORDER: |
| + return data.ReadDebugBorderQuadState(out); |
| + case cc::mojom::Material::PICTURE_CONTENT: |
| + // TODO(fsamuel): Implement PictureDrawQuad |
| + // serialization/deserialization. |
| + break; |
| + case cc::mojom::Material::RENDER_PASS: |
| + return data.ReadRenderPassQuadState(out); |
| + case cc::mojom::Material::SOLID_COLOR: |
| + return data.ReadSolidColorQuadState(out); |
| + case cc::mojom::Material::STREAM_VIDEO_CONTENT: |
| + // TODO(fsamuel): Implement StreamVideoContentDrawQuad |
| + // serialization/deserialization. |
| + break; |
| + case cc::mojom::Material::SURFACE_CONTENT: |
| + return data.ReadSurfaceQuadState(out); |
| + case cc::mojom::Material::TEXTURE_CONTENT: |
| + // TODO(fsamuel): Implement TextureDrawQuad |
| + // serialization/deserialization. |
| + break; |
| + case cc::mojom::Material::TILED_CONTENT: |
| + // TODO(fsamuel): Implement TileDrawQuad |
| + // serialization/deserialization. |
| + break; |
| + case cc::mojom::Material::YUV_VIDEO_CONTENT: |
| + // TODO(fsamuel): Implement YUVVideoDrawQuad |
| + // serialization/deserialization. |
| + break; |
| + } |
| + NOTREACHED(); |
| + return false; |
| + } |
| +}; |
| + |
| +struct QuadListArray { |
| + cc::QuadList* list; |
| +}; |
| + |
| +template <> |
| +struct ArrayTraits<QuadListArray> { |
| + using Element = cc::DrawQuad; |
| + using Iterator = cc::QuadList::Iterator; |
| + using ConstIterator = cc::QuadList::ConstIterator; |
| + |
| + static ConstIterator GetBegin(const QuadListArray& input) { |
| + return input.list->begin(); |
| + } |
| + static Iterator GetBegin(QuadListArray& input) { return input.list->begin(); } |
| + |
| + static void AdvanceIterator(ConstIterator& iterator) { iterator++; } |
| + static void AdvanceIterator(Iterator& iterator) { iterator++; } |
| + |
| + static const Element& GetValue(ConstIterator& iterator) { return **iterator; } |
| + static Element& GetValue(Iterator& iterator) { return **iterator; } |
| + |
| + static size_t GetSize(const QuadListArray& input) { |
| + return input.list->size(); |
| + } |
| + |
| + static bool Resize(QuadListArray& input, size_t size) { return true; } |
| +}; |
| + |
| +template <> |
| +struct StructTraits<cc::mojom::QuadList, cc::QuadList> { |
| + static mojo::Array<cc::mojom::Material> quad_types( |
| + const cc::QuadList& quad_list) { |
| + mojo::Array<cc::mojom::Material> materials = |
| + mojo::Array<cc::mojom::Material>::New(quad_list.size()); |
| + for (size_t i = 0; i < quad_list.size(); ++i) |
| + materials[i] = CCMaterialToMojo(quad_list.ElementAt(i)->material); |
| + return materials; |
| + } |
| + |
| + static QuadListArray quads(const cc::QuadList& quad_list) { |
| + return {const_cast<cc::QuadList*>(&quad_list)}; |
| + } |
| + |
| + static bool Read(cc::mojom::QuadListDataView data, cc::QuadList* out) { |
| + // TODO(fsamuel): Once we have ArrayTraits DataViews we can delete |
| + // this field. This field exists so we can pre-allocate DrawQuads |
| + // in the QuadList according to their material. |
| + mojo::Array<cc::mojom::Material> materials; |
| + if (!data.ReadQuadTypes(&materials)) |
| + return false; |
| + for (size_t i = 0; i < materials.size(); ++i) { |
| + cc::DrawQuad::Material material = MojoMaterialToCC(materials[i]); |
| + AllocateAndConstruct(material, out); |
| + } |
| + QuadListArray quad_list_array = {out}; |
| + return data.ReadQuads(&quad_list_array); |
| + } |
| +}; |
| + |
| +} // namespace mojo |
| + |
| +#endif // CC_IPC_QUADS_STRUCT_TRAITS_H_ |