From e2c851372a8c62556690900deea15a8c542d2167 Mon Sep 17 00:00:00 2001 From: Austin Eng Date: Mon, 11 Feb 2019 21:50:16 +0000 Subject: [PATCH] Create wire Client and Server classes with private impl This creates proper Client and Server interfaces which will be necessary for adding additional features to the Wire for chrome integration Bug: dawn:103 Change-Id: I181e95079b0bac85c2c6152ad8066a94b80c8d28 Reviewed-on: https://dawn-review.googlesource.com/c/4002 Commit-Queue: Austin Eng Reviewed-by: Kai Ninomiya --- BUILD.gn | 5 ++- examples/SampleUtils.cpp | 16 ++++--- .../{DawnWire.cpp => WireClient.cpp} | 27 ++++++++---- src/dawn_wire/WireServer.cpp | 34 +++++++++++++++ src/dawn_wire/client/Client.cpp | 4 +- src/dawn_wire/client/Client.h | 8 +++- src/dawn_wire/server/Server.h | 2 +- .../DawnWireServerAndFrontendFuzzer.cpp | 6 +-- src/include/dawn_wire/Wire.h | 7 --- src/include/dawn_wire/WireClient.h | 43 +++++++++++++++++++ src/include/dawn_wire/WireServer.h | 41 ++++++++++++++++++ src/tests/DawnTest.cpp | 12 +++--- src/tests/DawnTest.h | 7 +-- src/tests/unittests/WireTests.cpp | 15 ++++--- 14 files changed, 178 insertions(+), 49 deletions(-) rename src/dawn_wire/{DawnWire.cpp => WireClient.cpp} (55%) create mode 100644 src/dawn_wire/WireServer.cpp create mode 100644 src/include/dawn_wire/WireClient.h create mode 100644 src/include/dawn_wire/WireServer.h diff --git a/BUILD.gn b/BUILD.gn index 8d0d8ed7c1..bdd9141ff6 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -760,6 +760,8 @@ source_set("libdawn_wire_headers") { public_configs = [ ":dawn_public" ] sources = [ "src/include/dawn_wire/Wire.h", + "src/include/dawn_wire/WireClient.h", + "src/include/dawn_wire/WireServer.h", "src/include/dawn_wire/dawn_wire_export.h", ] } @@ -797,9 +799,10 @@ dawn_component("libdawn_wire") { configs = [ ":dawn_internal" ] sources = get_target_outputs(":libdawn_wire_gen") sources += [ - "src/dawn_wire/DawnWire.cpp", + "src/dawn_wire/WireClient.cpp", "src/dawn_wire/WireDeserializeAllocator.cpp", "src/dawn_wire/WireDeserializeAllocator.h", + "src/dawn_wire/WireServer.cpp", "src/dawn_wire/client/ApiObjects.h", "src/dawn_wire/client/ApiProcs.cpp", "src/dawn_wire/client/Buffer.cpp", diff --git a/examples/SampleUtils.cpp b/examples/SampleUtils.cpp index ecb20c278f..7e1e7311f4 100644 --- a/examples/SampleUtils.cpp +++ b/examples/SampleUtils.cpp @@ -20,9 +20,11 @@ #include "utils/TerribleCommandBuffer.h" #include -#include #include +#include #include +#include +#include #include "GLFW/glfw3.h" #include @@ -61,8 +63,8 @@ static utils::BackendBinding* binding = nullptr; static GLFWwindow* window = nullptr; -static dawn_wire::CommandHandler* wireServer = nullptr; -static dawn_wire::CommandHandler* wireClient = nullptr; +static dawn_wire::WireServer* wireServer = nullptr; +static dawn_wire::WireClient* wireClient = nullptr; static utils::TerribleCommandBuffer* c2sBuf = nullptr; static utils::TerribleCommandBuffer* s2cBuf = nullptr; @@ -101,12 +103,12 @@ dawn::Device CreateCppDawnDevice() { c2sBuf = new utils::TerribleCommandBuffer(); s2cBuf = new utils::TerribleCommandBuffer(); - wireServer = dawn_wire::NewServerCommandHandler(backendDevice, backendProcs, s2cBuf); + wireServer = new dawn_wire::WireServer(backendDevice, backendProcs, s2cBuf); c2sBuf->SetHandler(wireServer); - dawnDevice clientDevice; - dawnProcTable clientProcs; - wireClient = dawn_wire::NewClientDevice(&clientProcs, &clientDevice, c2sBuf); + wireClient = new dawn_wire::WireClient(c2sBuf); + dawnDevice clientDevice = wireClient->GetDevice(); + dawnProcTable clientProcs = wireClient->GetProcs(); s2cBuf->SetHandler(wireClient); procs = clientProcs; diff --git a/src/dawn_wire/DawnWire.cpp b/src/dawn_wire/WireClient.cpp similarity index 55% rename from src/dawn_wire/DawnWire.cpp rename to src/dawn_wire/WireClient.cpp index 56792d0b6e..e56348dea1 100644 --- a/src/dawn_wire/DawnWire.cpp +++ b/src/dawn_wire/WireClient.cpp @@ -12,19 +12,28 @@ // See the License for the specific language governing permissions and // limitations under the License. +#include "dawn_wire/WireClient.h" #include "dawn_wire/client/Client.h" -#include "dawn_wire/server/Server.h" namespace dawn_wire { - CommandHandler* NewClientDevice(dawnProcTable* procs, - dawnDevice* device, - CommandSerializer* serializer) { - return new client::Client(procs, device, serializer); + + WireClient::WireClient(CommandSerializer* serializer) : mImpl(new client::Client(serializer)) { } - CommandHandler* NewServerCommandHandler(dawnDevice device, - const dawnProcTable& procs, - CommandSerializer* serializer) { - return new server::Server(device, procs, serializer); + WireClient::~WireClient() { + mImpl.reset(); } + + dawnDevice WireClient::GetDevice() const { + return mImpl->GetDevice(); + } + + dawnProcTable WireClient::GetProcs() const { + return client::GetProcs(); + } + + const char* WireClient::HandleCommands(const char* commands, size_t size) { + return mImpl->HandleCommands(commands, size); + } + } // namespace dawn_wire diff --git a/src/dawn_wire/WireServer.cpp b/src/dawn_wire/WireServer.cpp new file mode 100644 index 0000000000..2c7ce1c539 --- /dev/null +++ b/src/dawn_wire/WireServer.cpp @@ -0,0 +1,34 @@ +// Copyright 2019 The Dawn Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "dawn_wire/WireServer.h" +#include "dawn_wire/server/Server.h" + +namespace dawn_wire { + + WireServer::WireServer(dawnDevice device, + const dawnProcTable& procs, + CommandSerializer* serializer) + : mImpl(new server::Server(device, procs, serializer)) { + } + + WireServer::~WireServer() { + mImpl.reset(); + } + + const char* WireServer::HandleCommands(const char* commands, size_t size) { + return mImpl->HandleCommands(commands, size); + } + +} // namespace dawn_wire diff --git a/src/dawn_wire/client/Client.cpp b/src/dawn_wire/client/Client.cpp index ecd332ec94..ca2fe94367 100644 --- a/src/dawn_wire/client/Client.cpp +++ b/src/dawn_wire/client/Client.cpp @@ -17,12 +17,10 @@ namespace dawn_wire { namespace client { - Client::Client(dawnProcTable* procs, dawnDevice* device, CommandSerializer* serializer) + Client::Client(CommandSerializer* serializer) : ClientBase(), mDevice(DeviceAllocator().New(this)->object.get()), mSerializer(serializer) { - *device = reinterpret_cast(mDevice); - *procs = client::GetProcs(); } Client::~Client() { diff --git a/src/dawn_wire/client/Client.h b/src/dawn_wire/client/Client.h index 3f8a7034ec..99f382cadb 100644 --- a/src/dawn_wire/client/Client.h +++ b/src/dawn_wire/client/Client.h @@ -25,9 +25,9 @@ namespace dawn_wire { namespace client { class Device; - class Client : public ClientBase, public CommandHandler { + class Client : public ClientBase { public: - Client(dawnProcTable* procs, dawnDevice* device, CommandSerializer* serializer); + Client(CommandSerializer* serializer); ~Client(); const char* HandleCommands(const char* commands, size_t size); @@ -36,6 +36,10 @@ namespace dawn_wire { namespace client { return mSerializer->GetCmdSpace(size); } + dawnDevice GetDevice() const { + return reinterpret_cast(mDevice); + } + private: #include "dawn_wire/client/ClientPrototypes_autogen.inl" diff --git a/src/dawn_wire/server/Server.h b/src/dawn_wire/server/Server.h index bc7b99d20f..aeb8107aa4 100644 --- a/src/dawn_wire/server/Server.h +++ b/src/dawn_wire/server/Server.h @@ -35,7 +35,7 @@ namespace dawn_wire { namespace server { uint64_t value; }; - class Server : public ServerBase, public CommandHandler { + class Server : public ServerBase { public: Server(dawnDevice device, const dawnProcTable& procs, CommandSerializer* serializer); ~Server(); diff --git a/src/fuzzers/DawnWireServerAndFrontendFuzzer.cpp b/src/fuzzers/DawnWireServerAndFrontendFuzzer.cpp index 9160b299ce..91c91a9cba 100644 --- a/src/fuzzers/DawnWireServerAndFrontendFuzzer.cpp +++ b/src/fuzzers/DawnWireServerAndFrontendFuzzer.cpp @@ -15,7 +15,7 @@ #include "common/Assert.h" #include "dawn/dawncpp.h" #include "dawn_native/DawnNative.h" -#include "dawn_wire/Wire.h" +#include "dawn_wire/WireServer.h" #include @@ -61,8 +61,8 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { ASSERT(nullDevice.Get() != nullptr); DevNull devNull; - std::unique_ptr wireServer( - dawn_wire::NewServerCommandHandler(nullDevice.Get(), procs, &devNull)); + std::unique_ptr wireServer( + new dawn_wire::WireServer(nullDevice.Get(), procs, &devNull)); wireServer->HandleCommands(reinterpret_cast(data), size); diff --git a/src/include/dawn_wire/Wire.h b/src/include/dawn_wire/Wire.h index f9cdd6ef0b..b5ee54d15c 100644 --- a/src/include/dawn_wire/Wire.h +++ b/src/include/dawn_wire/Wire.h @@ -35,13 +35,6 @@ namespace dawn_wire { virtual const char* HandleCommands(const char* commands, size_t size) = 0; }; - DAWN_WIRE_EXPORT CommandHandler* NewClientDevice(dawnProcTable* procs, - dawnDevice* device, - CommandSerializer* serializer); - DAWN_WIRE_EXPORT CommandHandler* NewServerCommandHandler(dawnDevice device, - const dawnProcTable& procs, - CommandSerializer* serializer); - } // namespace dawn_wire #endif // DAWNWIRE_WIRE_H_ diff --git a/src/include/dawn_wire/WireClient.h b/src/include/dawn_wire/WireClient.h new file mode 100644 index 0000000000..941ad51271 --- /dev/null +++ b/src/include/dawn_wire/WireClient.h @@ -0,0 +1,43 @@ +// Copyright 2019 The Dawn Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef DAWNWIRE_WIRECLIENT_H_ +#define DAWNWIRE_WIRECLIENT_H_ + +#include + +#include "dawn_wire/Wire.h" + +namespace dawn_wire { + + namespace client { + class Client; + } + + class DAWN_WIRE_EXPORT WireClient : public CommandHandler { + public: + WireClient(CommandSerializer* serializer); + ~WireClient(); + + dawnDevice GetDevice() const; + dawnProcTable GetProcs() const; + const char* HandleCommands(const char* commands, size_t size) override final; + + private: + std::unique_ptr mImpl; + }; + +} // namespace dawn_wire + +#endif // DAWNWIRE_WIRECLIENT_H_ diff --git a/src/include/dawn_wire/WireServer.h b/src/include/dawn_wire/WireServer.h new file mode 100644 index 0000000000..47a70560e6 --- /dev/null +++ b/src/include/dawn_wire/WireServer.h @@ -0,0 +1,41 @@ +// Copyright 2019 The Dawn Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef DAWNWIRE_WIRESERVER_H_ +#define DAWNWIRE_WIRESERVER_H_ + +#include + +#include "dawn_wire/Wire.h" + +namespace dawn_wire { + + namespace server { + class Server; + } + + class DAWN_WIRE_EXPORT WireServer : public CommandHandler { + public: + WireServer(dawnDevice device, const dawnProcTable& procs, CommandSerializer* serializer); + ~WireServer(); + + const char* HandleCommands(const char* commands, size_t size) override final; + + private: + std::unique_ptr mImpl; + }; + +} // namespace dawn_wire + +#endif // DAWNWIRE_WIRESERVER_H_ diff --git a/src/tests/DawnTest.cpp b/src/tests/DawnTest.cpp index 0637495a2d..c643047555 100644 --- a/src/tests/DawnTest.cpp +++ b/src/tests/DawnTest.cpp @@ -19,7 +19,8 @@ #include "common/Math.h" #include "common/Platform.h" #include "dawn_native/DawnNative.h" -#include "dawn_wire/Wire.h" +#include "dawn_wire/WireClient.h" +#include "dawn_wire/WireServer.h" #include "utils/BackendBinding.h" #include "utils/DawnHelpers.h" #include "utils/SystemUtils.h" @@ -192,13 +193,12 @@ void DawnTest::SetUp() { mC2sBuf = std::make_unique(); mS2cBuf = std::make_unique(); - mWireServer.reset( - dawn_wire::NewServerCommandHandler(backendDevice, backendProcs, mS2cBuf.get())); + mWireServer.reset(new dawn_wire::WireServer(backendDevice, backendProcs, mS2cBuf.get())); mC2sBuf->SetHandler(mWireServer.get()); - dawnDevice clientDevice; - dawnProcTable clientProcs; - mWireClient.reset(dawn_wire::NewClientDevice(&clientProcs, &clientDevice, mC2sBuf.get())); + mWireClient.reset(new dawn_wire::WireClient(mC2sBuf.get())); + dawnDevice clientDevice = mWireClient->GetDevice(); + dawnProcTable clientProcs = mWireClient->GetProcs(); mS2cBuf->SetHandler(mWireClient.get()); procs = clientProcs; diff --git a/src/tests/DawnTest.h b/src/tests/DawnTest.h index b77777e207..7bce5ec46e 100644 --- a/src/tests/DawnTest.h +++ b/src/tests/DawnTest.h @@ -72,7 +72,8 @@ namespace detail { } namespace dawn_wire { - class CommandHandler; + class WireClient; + class WireServer; } // namespace dawn_wire class DawnTest : public ::testing::TestWithParam { @@ -129,8 +130,8 @@ class DawnTest : public ::testing::TestWithParam { private: // Things used to set up testing through the Wire. - std::unique_ptr mWireServer; - std::unique_ptr mWireClient; + std::unique_ptr mWireServer; + std::unique_ptr mWireClient; std::unique_ptr mC2sBuf; std::unique_ptr mS2cBuf; void FlushWire(); diff --git a/src/tests/unittests/WireTests.cpp b/src/tests/unittests/WireTests.cpp index 9d59a42394..72532b0bf2 100644 --- a/src/tests/unittests/WireTests.cpp +++ b/src/tests/unittests/WireTests.cpp @@ -17,13 +17,13 @@ #include "common/Assert.h" #include "common/Constants.h" -#include "dawn_wire/Wire.h" +#include "dawn_wire/WireClient.h" +#include "dawn_wire/WireServer.h" #include "utils/TerribleCommandBuffer.h" #include using namespace testing; -using namespace dawn_wire; // Definition of a "Lambda predicate matcher" for GMock to allow checking deep structures // are passed correctly by the wire. @@ -156,11 +156,12 @@ class WireTestsBase : public Test { mS2cBuf = std::make_unique(); mC2sBuf = std::make_unique(mWireServer.get()); - mWireServer.reset(NewServerCommandHandler(mockDevice, mockProcs, mS2cBuf.get())); + mWireServer.reset(new dawn_wire::WireServer(mockDevice, mockProcs, mS2cBuf.get())); mC2sBuf->SetHandler(mWireServer.get()); - dawnProcTable clientProcs; - mWireClient.reset(NewClientDevice(&clientProcs, &device, mC2sBuf.get())); + mWireClient.reset(new dawn_wire::WireClient(mC2sBuf.get())); + dawnProcTable clientProcs = mWireClient->GetProcs(); + device = mWireClient->GetDevice(); dawnSetProcs(&clientProcs); mS2cBuf->SetHandler(mWireClient.get()); @@ -197,8 +198,8 @@ class WireTestsBase : public Test { private: bool mIgnoreSetCallbackCalls = false; - std::unique_ptr mWireServer; - std::unique_ptr mWireClient; + std::unique_ptr mWireServer; + std::unique_ptr mWireClient; std::unique_ptr mS2cBuf; std::unique_ptr mC2sBuf; };