Skip to content

Commit

Permalink
Add SurfaceReference class.
Browse files Browse the repository at this point in the history
Add a simple class SurfaceReference that holds a parent and child
SurfaceId. Also add Mojo struct and StructTraits for use over IPC.

Update SurfaceId and LocalFrameId struct traits slightly to avoid
unnecessary copies. This requires a friend declaration but it seems
reasonable and is done elsewhere.

BUG=659227
CQ_INCLUDE_TRYBOTS=master.tryserver.blink:linux_trusty_blink_rel

Review-Url: https://codereview.chromium.org/2537343004
Cr-Commit-Position: refs/heads/master@{#436423}
  • Loading branch information
kylechar authored and Commit bot committed Dec 5, 2016
1 parent e9559d0 commit c142d1d
Show file tree
Hide file tree
Showing 15 changed files with 201 additions and 16 deletions.
2 changes: 2 additions & 0 deletions cc/ipc/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ mojom("interfaces") {
"selection.mojom",
"shared_quad_state.mojom",
"surface_id.mojom",
"surface_reference.mojom",
"surface_sequence.mojom",
"transferable_resource.mojom",
]
Expand Down Expand Up @@ -111,6 +112,7 @@ cc_source_set("struct_traits") {
"selection_struct_traits.h",
"shared_quad_state_struct_traits.h",
"surface_id_struct_traits.h",
"surface_reference_struct_traits.h",
"surface_sequence_struct_traits.h",
"transferable_resource_struct_traits.cc",
"transferable_resource_struct_traits.h",
Expand Down
8 changes: 2 additions & 6 deletions cc/ipc/local_frame_id_struct_traits.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,8 @@ struct StructTraits<cc::mojom::LocalFrameIdDataView, cc::LocalFrameId> {

static bool Read(cc::mojom::LocalFrameIdDataView data,
cc::LocalFrameId* out) {
base::UnguessableToken nonce;
if (!data.ReadNonce(&nonce))
return false;

*out = cc::LocalFrameId(data.local_id(), nonce);
return true;
out->local_id_ = data.local_id();
return data.ReadNonce(&out->nonce_);
}
};

Expand Down
22 changes: 22 additions & 0 deletions cc/ipc/struct_traits_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,12 @@ class StructTraitsTest : public testing::Test, public mojom::TraitsTestService {
callback.Run(s);
}

void EchoSurfaceReference(
const SurfaceReference& s,
const EchoSurfaceReferenceCallback& callback) override {
callback.Run(s);
}

void EchoSurfaceSequence(
const SurfaceSequence& s,
const EchoSurfaceSequenceCallback& callback) override {
Expand Down Expand Up @@ -779,6 +785,22 @@ TEST_F(StructTraitsTest, SurfaceId) {
EXPECT_EQ(local_frame_id, output.local_frame_id());
}

TEST_F(StructTraitsTest, SurfaceReference) {
const SurfaceId parent_id(
FrameSinkId(2016, 1234),
LocalFrameId(0xfbadbeef, base::UnguessableToken::Deserialize(123, 456)));
const SurfaceId child_id(
FrameSinkId(1111, 9999),
LocalFrameId(0xabcdabcd, base::UnguessableToken::Deserialize(333, 333)));
const SurfaceReference input(parent_id, child_id);

mojom::TraitsTestServicePtr proxy = GetTraitsTestProxy();
SurfaceReference output;
proxy->EchoSurfaceReference(input, &output);
EXPECT_EQ(parent_id, output.parent_id());
EXPECT_EQ(child_id, output.child_id());
}

TEST_F(StructTraitsTest, SurfaceSequence) {
const FrameSinkId frame_sink_id(2016, 1234);
const uint32_t sequence = 0xfbadbeef;
Expand Down
13 changes: 3 additions & 10 deletions cc/ipc/surface_id_struct_traits.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "cc/ipc/local_frame_id_struct_traits.h"
#include "cc/ipc/surface_id.mojom-shared.h"
#include "cc/surfaces/frame_sink_id.h"
#include "cc/surfaces/local_frame_id.h"
#include "cc/surfaces/surface_id.h"

namespace mojo {
Expand All @@ -24,16 +25,8 @@ struct StructTraits<cc::mojom::SurfaceIdDataView, cc::SurfaceId> {
}

static bool Read(cc::mojom::SurfaceIdDataView data, cc::SurfaceId* out) {
cc::FrameSinkId frame_sink_id;
if (!data.ReadFrameSinkId(&frame_sink_id))
return false;

cc::LocalFrameId local_frame_id;
if (!data.ReadLocalFrameId(&local_frame_id))
return false;

*out = cc::SurfaceId(frame_sink_id, local_frame_id);
return true;
return data.ReadFrameSinkId(&out->frame_sink_id_) &&
data.ReadLocalFrameId(&out->local_frame_id_);
}
};

Expand Down
12 changes: 12 additions & 0 deletions cc/ipc/surface_reference.mojom
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// 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.

module cc.mojom;

import "cc/ipc/surface_id.mojom";

struct SurfaceReference {
SurfaceId parent_id;
SurfaceId child_id;
};
11 changes: 11 additions & 0 deletions cc/ipc/surface_reference.typemap
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# 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.

mojom = "//cc/ipc/surface_reference.mojom"
public_headers = [ "//cc/surfaces/surface_reference.h" ]
traits_headers = [ "//cc/ipc/surface_reference_struct_traits.h" ]
deps = [
"//cc/ipc:struct_traits",
]
type_mappings = [ "cc.mojom.SurfaceReference=cc::SurfaceReference" ]
34 changes: 34 additions & 0 deletions cc/ipc/surface_reference_struct_traits.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// 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_SURFACE_REFERENCE_STRUCT_TRAITS_H_
#define CC_IPC_SURFACE_REFERENCE_STRUCT_TRAITS_H_

#include "cc/ipc/surface_id_struct_traits.h"
#include "cc/ipc/surface_reference.mojom-shared.h"
#include "cc/surfaces/surface_id.h"
#include "cc/surfaces/surface_reference.h"

namespace mojo {

template <>
struct StructTraits<cc::mojom::SurfaceReferenceDataView, cc::SurfaceReference> {
static const cc::SurfaceId& parent_id(const cc::SurfaceReference& ref) {
return ref.parent_id();
}

static const cc::SurfaceId& child_id(const cc::SurfaceReference& ref) {
return ref.child_id();
}

static bool Read(cc::mojom::SurfaceReferenceDataView data,
cc::SurfaceReference* out) {
return data.ReadParentId(&out->parent_id_) &&
data.ReadChildId(&out->child_id_);
}
};

} // namespace mojo

#endif // CC_IPC_SURFACE_REFERENCE_STRUCT_TRAITS_H_
4 changes: 4 additions & 0 deletions cc/ipc/traits_test_service.mojom
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import "cc/ipc/returned_resource.mojom";
import "cc/ipc/selection.mojom";
import "cc/ipc/shared_quad_state.mojom";
import "cc/ipc/surface_id.mojom";
import "cc/ipc/surface_reference.mojom";
import "cc/ipc/surface_sequence.mojom";
import "cc/ipc/transferable_resource.mojom";

Expand Down Expand Up @@ -56,6 +57,9 @@ interface TraitsTestService {
[Sync]
EchoSurfaceId(SurfaceId s) => (SurfaceId pass);

[Sync]
EchoSurfaceReference(SurfaceReference r) => (SurfaceReference pass);

[Sync]
EchoSurfaceSequence(SurfaceSequence s) => (SurfaceSequence pass);

Expand Down
1 change: 1 addition & 0 deletions cc/ipc/typemaps.gni
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ typemaps = [
"//cc/ipc/selection.typemap",
"//cc/ipc/shared_quad_state.typemap",
"//cc/ipc/surface_id.typemap",
"//cc/ipc/surface_reference.typemap",
"//cc/ipc/surface_sequence.typemap",
"//cc/ipc/transferable_resource.typemap",
]
3 changes: 3 additions & 0 deletions cc/surfaces/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@ cc_source_set("surface_id") {
"local_frame_id.h",
"surface_id.cc",
"surface_id.h",
"surface_reference.cc",
"surface_reference.h",
"surface_sequence.h",
"surface_sequence_generator.cc",
"surface_sequence_generator.h",
]

deps = [
"//base",
"//mojo/public/cpp/bindings:struct_traits",
]
}

Expand Down
3 changes: 3 additions & 0 deletions cc/surfaces/DEPS
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
include_rules = [
"+mojo/public/cpp/bindings/struct_traits.h",
]
6 changes: 6 additions & 0 deletions cc/surfaces/local_frame_id.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,12 @@

#include "base/hash.h"
#include "base/unguessable_token.h"
#include "mojo/public/cpp/bindings/struct_traits.h"

namespace cc {
namespace mojom {
class LocalFrameIdDataView;
}

class LocalFrameId {
public:
Expand Down Expand Up @@ -53,6 +57,8 @@ class LocalFrameId {
std::string ToString() const;

private:
friend struct mojo::StructTraits<mojom::LocalFrameIdDataView, LocalFrameId>;

uint32_t local_id_;
base::UnguessableToken nonce_;
};
Expand Down
6 changes: 6 additions & 0 deletions cc/surfaces/surface_id.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@
#include "base/hash.h"
#include "cc/surfaces/frame_sink_id.h"
#include "cc/surfaces/local_frame_id.h"
#include "mojo/public/cpp/bindings/struct_traits.h"

namespace cc {
namespace mojom {
class SurfaceIdDataView;
}

class SurfaceId {
public:
Expand Down Expand Up @@ -62,6 +66,8 @@ class SurfaceId {
}

private:
friend struct mojo::StructTraits<mojom::SurfaceIdDataView, SurfaceId>;

// See SurfaceIdAllocator::GenerateId.
FrameSinkId frame_sink_id_;
LocalFrameId local_frame_id_;
Expand Down
27 changes: 27 additions & 0 deletions cc/surfaces/surface_reference.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// 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.

#include "cc/surfaces/surface_reference.h"

#include "base/strings/stringprintf.h"

namespace cc {

SurfaceReference::SurfaceReference() = default;

SurfaceReference::SurfaceReference(const SurfaceId& parent_id,
const SurfaceId& child_id)
: parent_id_(parent_id), child_id_(child_id) {}

SurfaceReference::SurfaceReference(const SurfaceReference& other) = default;

SurfaceReference::~SurfaceReference() = default;

std::string SurfaceReference::ToString() const {
return base::StringPrintf("parent=%s, child=%s",
parent_id_.ToString().c_str(),
child_id_.ToString().c_str());
}

} // namespace cc
65 changes: 65 additions & 0 deletions cc/surfaces/surface_reference.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// 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_SURFACES_SURFACE_REFERENCE_H_
#define CC_SURFACES_SURFACE_REFERENCE_H_

#include <string>

#include "base/hash.h"
#include "cc/surfaces/surface_id.h"
#include "mojo/public/cpp/bindings/struct_traits.h"

namespace cc {
namespace mojom {
class SurfaceReferenceDataView;
}

// Hold a reference from an embedding (parent) to embedded (child) surface.
class SurfaceReference {
public:
SurfaceReference();
SurfaceReference(const SurfaceId& parent_id, const SurfaceId& child_id);
SurfaceReference(const SurfaceReference& other);

~SurfaceReference();

const SurfaceId& parent_id() const { return parent_id_; }
const SurfaceId& child_id() const { return child_id_; }

size_t hash() const {
return base::HashInts(static_cast<uint64_t>(parent_id_.hash()),
static_cast<uint64_t>(child_id_.hash()));
}

bool operator==(const SurfaceReference& other) const {
return parent_id_ == other.parent_id_ && child_id_ == other.child_id_;
}

bool operator!=(const SurfaceReference& other) const {
return !(*this == other);
}

bool operator<(const SurfaceReference& other) const {
return std::tie(parent_id_, child_id_) <
std::tie(other.parent_id_, other.child_id_);
}

std::string ToString() const;

private:
friend struct mojo::StructTraits<mojom::SurfaceReferenceDataView,
SurfaceReference>;

SurfaceId parent_id_;
SurfaceId child_id_;
};

struct SurfaceReferenceHash {
size_t operator()(const SurfaceReference& ref) const { return ref.hash(); }
};

} // namespace cc

#endif // CC_SURFACES_SURFACE_REFERENCE_H_

0 comments on commit c142d1d

Please sign in to comment.