OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "mojo/services/native_viewport/native_viewport_impl.h" | 5 #include "mojo/services/native_viewport/native_viewport_impl.h" |
6 | 6 |
7 #include "base/bind.h" | |
8 #include "base/macros.h" | 7 #include "base/macros.h" |
9 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
10 #include "base/time/time.h" | 9 #include "base/time/time.h" |
11 #include "mojo/public/cpp/application/application_delegate.h" | 10 #include "mojo/public/cpp/application/application_delegate.h" |
12 #include "mojo/public/cpp/application/interface_factory.h" | 11 #include "mojo/public/cpp/application/interface_factory.h" |
13 #include "mojo/services/public/cpp/geometry/geometry_type_converters.h" | 12 #include "mojo/services/public/cpp/geometry/geometry_type_converters.h" |
14 #include "mojo/services/public/cpp/input_events/input_events_type_converters.h" | 13 #include "mojo/services/public/cpp/input_events/input_events_type_converters.h" |
15 #include "ui/events/event.h" | 14 #include "ui/events/event.h" |
16 | 15 |
17 namespace mojo { | 16 namespace mojo { |
(...skipping 14 matching lines...) Expand all Loading... |
32 | 31 |
33 NativeViewportImpl::~NativeViewportImpl() { | 32 NativeViewportImpl::~NativeViewportImpl() { |
34 // Destroy the NativeViewport early on as it may call us back during | 33 // Destroy the NativeViewport early on as it may call us back during |
35 // destruction and we want to be in a known state. | 34 // destruction and we want to be in a known state. |
36 platform_viewport_.reset(); | 35 platform_viewport_.reset(); |
37 } | 36 } |
38 | 37 |
39 void NativeViewportImpl::Create(RectPtr bounds) { | 38 void NativeViewportImpl::Create(RectPtr bounds) { |
40 platform_viewport_ = PlatformViewport::Create(this); | 39 platform_viewport_ = PlatformViewport::Create(this); |
41 platform_viewport_->Init(bounds.To<gfx::Rect>()); | 40 platform_viewport_->Init(bounds.To<gfx::Rect>()); |
| 41 client()->OnCreated(); |
42 OnBoundsChanged(bounds.To<gfx::Rect>()); | 42 OnBoundsChanged(bounds.To<gfx::Rect>()); |
43 } | 43 } |
44 | 44 |
45 void NativeViewportImpl::Show() { | 45 void NativeViewportImpl::Show() { |
46 platform_viewport_->Show(); | 46 platform_viewport_->Show(); |
47 } | 47 } |
48 | 48 |
49 void NativeViewportImpl::Hide() { | 49 void NativeViewportImpl::Hide() { |
50 platform_viewport_->Hide(); | 50 platform_viewport_->Hide(); |
51 } | 51 } |
52 | 52 |
53 void NativeViewportImpl::Close() { | 53 void NativeViewportImpl::Close() { |
| 54 command_buffer_.reset(); |
54 DCHECK(platform_viewport_); | 55 DCHECK(platform_viewport_); |
55 platform_viewport_->Close(); | 56 platform_viewport_->Close(); |
56 } | 57 } |
57 | 58 |
58 void NativeViewportImpl::SetBounds(RectPtr bounds) { | 59 void NativeViewportImpl::SetBounds(RectPtr bounds) { |
59 platform_viewport_->SetBounds(bounds.To<gfx::Rect>()); | 60 platform_viewport_->SetBounds(bounds.To<gfx::Rect>()); |
60 } | 61 } |
61 | 62 |
| 63 void NativeViewportImpl::CreateGLES2Context( |
| 64 InterfaceRequest<CommandBuffer> command_buffer_request) { |
| 65 if (command_buffer_ || command_buffer_request_.is_pending()) { |
| 66 LOG(ERROR) << "Can't create multiple contexts on a NativeViewport"; |
| 67 return; |
| 68 } |
| 69 command_buffer_request_ = command_buffer_request.Pass(); |
| 70 CreateCommandBufferIfNeeded(); |
| 71 } |
| 72 |
62 void NativeViewportImpl::OnBoundsChanged(const gfx::Rect& bounds) { | 73 void NativeViewportImpl::OnBoundsChanged(const gfx::Rect& bounds) { |
| 74 CreateCommandBufferIfNeeded(); |
63 client()->OnBoundsChanged(Rect::From(bounds)); | 75 client()->OnBoundsChanged(Rect::From(bounds)); |
64 } | 76 } |
65 | 77 |
66 void NativeViewportImpl::OnAcceleratedWidgetAvailable( | 78 void NativeViewportImpl::OnAcceleratedWidgetAvailable( |
67 gfx::AcceleratedWidget widget) { | 79 gfx::AcceleratedWidget widget) { |
68 widget_ = widget; | 80 widget_ = widget; |
69 client()->OnCreated(reinterpret_cast<uint64_t>(widget)); | 81 CreateCommandBufferIfNeeded(); |
70 } | 82 } |
71 | 83 |
72 bool NativeViewportImpl::OnEvent(ui::Event* ui_event) { | 84 bool NativeViewportImpl::OnEvent(ui::Event* ui_event) { |
73 // Must not return early before updating capture. | 85 // Must not return early before updating capture. |
74 switch (ui_event->type()) { | 86 switch (ui_event->type()) { |
75 case ui::ET_MOUSE_PRESSED: | 87 case ui::ET_MOUSE_PRESSED: |
76 case ui::ET_TOUCH_PRESSED: | 88 case ui::ET_TOUCH_PRESSED: |
77 platform_viewport_->SetCapture(); | 89 platform_viewport_->SetCapture(); |
78 break; | 90 break; |
79 case ui::ET_MOUSE_RELEASED: | 91 case ui::ET_MOUSE_RELEASED: |
80 case ui::ET_TOUCH_RELEASED: | 92 case ui::ET_TOUCH_RELEASED: |
81 platform_viewport_->ReleaseCapture(); | 93 platform_viewport_->ReleaseCapture(); |
82 break; | 94 break; |
83 default: | 95 default: |
84 break; | 96 break; |
85 } | 97 } |
86 | 98 |
87 if (waiting_for_event_ack_ && IsRateLimitedEventType(ui_event)) | 99 if (waiting_for_event_ack_ && IsRateLimitedEventType(ui_event)) |
88 return false; | 100 return false; |
89 | 101 |
90 client()->OnEvent( | 102 client()->OnEvent( |
91 TypeConverter<EventPtr, ui::Event>::ConvertFrom(*ui_event), | 103 TypeConverter<EventPtr, ui::Event>::ConvertFrom(*ui_event), |
92 base::Bind(&NativeViewportImpl::AckEvent, | 104 base::Bind(&NativeViewportImpl::AckEvent, |
93 weak_factory_.GetWeakPtr())); | 105 weak_factory_.GetWeakPtr())); |
94 waiting_for_event_ack_ = true; | 106 waiting_for_event_ack_ = true; |
95 return false; | 107 return false; |
96 } | 108 } |
97 | 109 |
98 void NativeViewportImpl::OnDestroyed() { | 110 void NativeViewportImpl::OnDestroyed() { |
99 client()->OnDestroyed(); | 111 client()->OnDestroyed(base::Bind(&NativeViewportImpl::AckDestroyed, |
| 112 base::Unretained(this))); |
100 } | 113 } |
101 | 114 |
102 void NativeViewportImpl::AckEvent() { | 115 void NativeViewportImpl::AckEvent() { |
103 waiting_for_event_ack_ = false; | 116 waiting_for_event_ack_ = false; |
104 } | 117 } |
105 | 118 |
| 119 void NativeViewportImpl::CreateCommandBufferIfNeeded() { |
| 120 if (!command_buffer_request_.is_pending()) |
| 121 return; |
| 122 DCHECK(!command_buffer_.get()); |
| 123 if (widget_ == gfx::kNullAcceleratedWidget) |
| 124 return; |
| 125 gfx::Size size = platform_viewport_->GetSize(); |
| 126 if (size.IsEmpty()) |
| 127 return; |
| 128 command_buffer_.reset( |
| 129 new CommandBufferImpl(widget_, platform_viewport_->GetSize())); |
| 130 WeakBindToRequest(command_buffer_.get(), &command_buffer_request_); |
| 131 } |
| 132 |
| 133 void NativeViewportImpl::AckDestroyed() { |
| 134 command_buffer_.reset(); |
| 135 } |
| 136 |
106 } // namespace mojo | 137 } // namespace mojo |
107 | 138 |
OLD | NEW |