Upstream version 11.39.266.0
[platform/framework/web/crosswalk.git] / src / ui / ozone / platform / dri / gpu_platform_support_host_gbm.cc
1 // Copyright 2014 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 #include "ui/ozone/platform/dri/gpu_platform_support_host_gbm.h"
6
7 #include "base/debug/trace_event.h"
8 #include "ui/ozone/common/gpu/ozone_gpu_message_params.h"
9 #include "ui/ozone/common/gpu/ozone_gpu_messages.h"
10 #include "ui/ozone/platform/dri/channel_observer.h"
11
12 namespace ui {
13
14 GpuPlatformSupportHostGbm::GpuPlatformSupportHostGbm()
15     : host_id_(-1), sender_(NULL) {
16 }
17
18 GpuPlatformSupportHostGbm::~GpuPlatformSupportHostGbm() {}
19
20 bool GpuPlatformSupportHostGbm::IsConnected() const {
21   return sender_ != NULL;
22 }
23
24 void GpuPlatformSupportHostGbm::RegisterHandler(
25     GpuPlatformSupportHost* handler) {
26   handlers_.push_back(handler);
27 }
28
29 void GpuPlatformSupportHostGbm::UnregisterHandler(
30     GpuPlatformSupportHost* handler) {
31   std::vector<GpuPlatformSupportHost*>::iterator it =
32       std::find(handlers_.begin(), handlers_.end(), handler);
33   if (it != handlers_.end())
34     handlers_.erase(it);
35 }
36
37 void GpuPlatformSupportHostGbm::AddChannelObserver(ChannelObserver* observer) {
38   channel_observers_.AddObserver(observer);
39
40   if (sender_)
41     observer->OnChannelEstablished();
42 }
43
44 void GpuPlatformSupportHostGbm::RemoveChannelObserver(
45     ChannelObserver* observer) {
46   channel_observers_.RemoveObserver(observer);
47 }
48
49 void GpuPlatformSupportHostGbm::OnChannelEstablished(int host_id,
50                                                      IPC::Sender* sender) {
51   TRACE_EVENT1("dri",
52                "GpuPlatformSupportHostGbm::OnChannelEstablished",
53                "host_id",
54                host_id);
55   host_id_ = host_id;
56   sender_ = sender;
57
58   while (!queued_messages_.empty()) {
59     Send(queued_messages_.front());
60     queued_messages_.pop();
61   }
62
63   for (size_t i = 0; i < handlers_.size(); ++i)
64     handlers_[i]->OnChannelEstablished(host_id, sender);
65
66   FOR_EACH_OBSERVER(
67       ChannelObserver, channel_observers_, OnChannelEstablished());
68 }
69
70 void GpuPlatformSupportHostGbm::OnChannelDestroyed(int host_id) {
71   TRACE_EVENT1("dri",
72                "GpuPlatformSupportHostGbm::OnChannelDestroyed",
73                "host_id",
74                host_id);
75   if (host_id_ == host_id) {
76     host_id_ = -1;
77     sender_ = NULL;
78
79     FOR_EACH_OBSERVER(
80         ChannelObserver, channel_observers_, OnChannelDestroyed());
81   }
82
83   for (size_t i = 0; i < handlers_.size(); ++i)
84     handlers_[i]->OnChannelDestroyed(host_id);
85 }
86
87 bool GpuPlatformSupportHostGbm::OnMessageReceived(const IPC::Message& message) {
88   for (size_t i = 0; i < handlers_.size(); ++i)
89     if (handlers_[i]->OnMessageReceived(message))
90       return true;
91
92   return false;
93 }
94
95 bool GpuPlatformSupportHostGbm::Send(IPC::Message* message) {
96   if (sender_)
97     return sender_->Send(message);
98
99   queued_messages_.push(message);
100   return true;
101 }
102
103 void GpuPlatformSupportHostGbm::SetHardwareCursor(
104     gfx::AcceleratedWidget widget,
105     const std::vector<SkBitmap>& bitmaps,
106     const gfx::Point& location,
107     int frame_delay_ms) {
108   Send(new OzoneGpuMsg_CursorSet(widget, bitmaps, location, frame_delay_ms));
109 }
110
111 void GpuPlatformSupportHostGbm::MoveHardwareCursor(
112     gfx::AcceleratedWidget widget,
113     const gfx::Point& location) {
114   Send(new OzoneGpuMsg_CursorMove(widget, location));
115 }
116
117 }  // namespace ui