Use events instead of method invokes for window state updates and others

This commit is contained in:
Simon Fels 2016-11-25 08:50:43 +01:00
commit a40fe51654
14 changed files with 152 additions and 133 deletions

View file

@ -23,7 +23,7 @@
#include <unistd.h>
#include <errno.h>
#define LOG_TAG "Anbox"
#define LOG_TAG "Anboxd"
#include <cutils/log.h>
namespace {

View file

@ -21,59 +21,44 @@
#include "anbox_rpc.pb.h"
#include "anbox_bridge.pb.h"
#define LOG_TAG "Anbox"
#include <cutils/log.h>
namespace anbox {
PlatformApiStub::PlatformApiStub(const std::shared_ptr<rpc::Channel> &rpc_channel) :
rpc_channel_(rpc_channel) {
}
void PlatformApiStub::boot_finished() {
auto c = std::make_shared<Request<protobuf::rpc::Void>>();
protobuf::bridge::EventSequence seq;
auto event = seq.mutable_boot_finished();
(void) event;
rpc_channel_->send_event(seq);
}
ALOGI("Boot finished");
void PlatformApiStub::update_window_state(const WindowStateUpdate &state) {
protobuf::bridge::EventSequence seq;
auto event = seq.mutable_window_state_update();
{
std::lock_guard<decltype(mutex_)> lock(mutex_);
boot_finished_wait_handle_.expect_result();
auto convert_window = [](WindowStateUpdate::Window in, anbox::protobuf::bridge::WindowStateUpdateEvent_WindowState *out) {
out->set_display_id(in.display_id);
out->set_has_surface(in.has_surface);
out->set_package_name(in.package_name);
out->set_frame_left(in.frame.left);
out->set_frame_top(in.frame.top);
out->set_frame_right(in.frame.right);
out->set_frame_bottom(in.frame.bottom);
out->set_task_id(in.task_id);
out->set_stack_id(in.stack_id);
};
for (const auto &window : state.updated_windows) {
auto w = event->add_windows();
convert_window(window, w);
}
protobuf::rpc::Void message;
rpc_channel_->call_method(
"boot_finished",
&message, c->response.get(),
google::protobuf::NewCallback(this, &PlatformApiStub::handle_boot_finished_response, c.get()));
boot_finished_wait_handle_.wait_for_all();
ALOGI("Boot finished sent successfully!");
}
void PlatformApiStub::handle_boot_finished_response(Request<protobuf::rpc::Void>*) {
boot_finished_wait_handle_.result_received();
}
void PlatformApiStub::update_window_state(const anbox::protobuf::bridge::WindowStateUpdate &window_state) {
auto c = std::make_shared<Request<protobuf::rpc::Void>>();
ALOGI("Updating window state");
{
std::lock_guard<decltype(mutex_)> lock(mutex_);
update_window_state_wait_handle_.expect_result();
for (const auto &window : state.removed_windows) {
auto w = event->add_removed_windows();
convert_window(window, w);
}
rpc_channel_->call_method(
"update_window_state",
&window_state, c->response.get(),
google::protobuf::NewCallback(this, &PlatformApiStub::handle_update_window_state_response, c.get()));
update_window_state_wait_handle_.wait_for_all();
}
void PlatformApiStub::handle_update_window_state_response(Request<protobuf::rpc::Void> *request) {
update_window_state_wait_handle_.result_received();
rpc_channel_->send_event(seq);
}
} // namespace anbox

View file

@ -21,16 +21,14 @@
#include "anbox/common/wait_handle.h"
#include <memory>
#include <vector>
#include <string>
namespace anbox {
namespace protobuf {
namespace rpc {
class Void;
class WindowStateUpdate;
} // namespace rpc
namespace bridge {
class WindowStateUpdate;
} // namespace bridge
} // namespace protobuf
namespace rpc {
class Channel;
@ -40,7 +38,27 @@ public:
PlatformApiStub(const std::shared_ptr<rpc::Channel> &rpc_channel);
void boot_finished();
void update_window_state(const anbox::protobuf::bridge::WindowStateUpdate &window_state);
struct WindowStateUpdate {
struct Window {
int display_id;
bool has_surface;
std::string package_name;
struct Frame {
int left;
int top;
int right;
int bottom;
};
Frame frame;
int task_id;
int stack_id;
};
std::vector<Window> updated_windows;
std::vector<Window> removed_windows;
};
void update_window_state(const WindowStateUpdate &state);
private:
template<typename Response>
@ -50,12 +68,7 @@ private:
bool success;
};
void handle_boot_finished_response(Request<protobuf::rpc::Void> *request);
void handle_update_window_state_response(Request<protobuf::rpc::Void> *request);
mutable std::mutex mutex_;
common::WaitHandle boot_finished_wait_handle_;
common::WaitHandle update_window_state_wait_handle_;
std::shared_ptr<rpc::Channel> rpc_channel_;
};

View file

@ -16,7 +16,6 @@
*/
#include "android/service/platform_service.h"
#include "android/service/platform_api_stub.h"
#include "anbox/rpc/channel.h"
#include "anbox_rpc.pb.h"
@ -39,8 +38,8 @@ status_t PlatformService::boot_finished() {
return OK;
}
void PlatformService::unpack_window_state(anbox::protobuf::bridge::WindowStateUpdate_WindowState *window, const Parcel &data) {
window->set_has_surface(data.readByte() != 0);
anbox::PlatformApiStub::WindowStateUpdate::Window PlatformService::unpack_window_state(const Parcel &data) {
bool has_surface = data.readByte() != 0;
String8 package_name(data.readString16());
@ -51,38 +50,45 @@ void PlatformService::unpack_window_state(anbox::protobuf::bridge::WindowStateUp
auto task_id = data.readInt32();
auto stack_id = data.readInt32();
window->set_package_name(package_name.string());
window->set_frame_left(frame_left);
window->set_frame_top(frame_top);
window->set_frame_right(frame_right);
window->set_frame_bottom(frame_bottom);
window->set_task_id(task_id);
window->set_stack_id(stack_id);
ALOGI(" Window: package=%s frame={%d,%d,%d,%d} task=%d stack=%d",
package_name.string(), frame_left, frame_top, frame_right, frame_bottom,
task_id, stack_id);
return anbox::PlatformApiStub::WindowStateUpdate::Window{
-1, // Display id will be added by the caller
has_surface,
package_name.string(),
{frame_left, frame_top, frame_right, frame_bottom},
task_id,
stack_id,
};
}
status_t PlatformService::update_window_state(const Parcel &data) {
anbox::protobuf::bridge::WindowStateUpdate window_state;
anbox::PlatformApiStub::WindowStateUpdate state;
ALOGI("Udated windows:");
const auto num_displays = data.readInt32();
for (auto n = 0; n < num_displays; n++) {
const auto display_id = data.readInt32();
const auto num_windows = data.readInt32();
ALOGI(" Display: id=%d", display_id);
for (auto m = 0; m < num_windows; m++) {
auto window = window_state.add_windows();
window->set_display_id(display_id);
unpack_window_state(window, data);
auto window = unpack_window_state(data);
window.display_id = display_id;
state.updated_windows.push_back(window);
}
}
ALOGI("Removed windows:");
const auto num_removed_windows = data.readInt32();
for (auto n = 0; n < num_removed_windows; n++) {
auto window = window_state.add_removed_windows();
window->set_display_id(0);
unpack_window_state(window, data);
auto window = unpack_window_state(data);
state.removed_windows.push_back(window);
}
platform_api_stub_->update_window_state(window_state);
platform_api_stub_->update_window_state(state);
return OK;
}

View file

@ -19,20 +19,12 @@
#define ANBOX_ANDROID_PLATFORM_SERVICE_H_
#include "android/service/platform_service_interface.h"
#include "android/service/platform_api_stub.h"
#include <binder/Parcel.h>
#include <memory>
namespace anbox {
class PlatformApiStub;
namespace protobuf {
namespace bridge {
class WindowStateUpdate_WindowState;
} // namespace bridge
} // namespace protobuf
} // namespace anbox
namespace android {
class PlatformService : public BnPlatformService {
public:
@ -44,7 +36,7 @@ public:
status_t update_window_state(const Parcel &data) override;
private:
void unpack_window_state(anbox::protobuf::bridge::WindowStateUpdate_WindowState *window, const Parcel &data);
anbox::PlatformApiStub::WindowStateUpdate::Window unpack_window_state(const Parcel &data);
std::shared_ptr<anbox::PlatformApiStub> platform_api_stub_;
};
} // namespace android