| 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" |
| 7 #include "base/macros.h" | 8 #include "base/macros.h" |
| 8 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
| 9 #include "base/time/time.h" | 10 #include "base/time/time.h" |
| 10 #include "mojo/public/cpp/application/application_delegate.h" | 11 #include "mojo/public/cpp/application/application_delegate.h" |
| 11 #include "mojo/public/cpp/application/interface_factory.h" | 12 #include "mojo/public/cpp/application/interface_factory.h" |
| 12 #include "mojo/services/public/cpp/geometry/geometry_type_converters.h" | 13 #include "mojo/services/public/cpp/geometry/geometry_type_converters.h" |
| 13 #include "mojo/services/public/cpp/input_events/input_events_type_converters.h" | 14 #include "mojo/services/public/cpp/input_events/input_events_type_converters.h" |
| 14 #include "ui/events/event.h" | 15 #include "ui/events/event.h" |
| 15 | 16 |
| 16 namespace mojo { | 17 namespace mojo { |
| (...skipping 14 matching lines...) Expand all Loading... |
| 31 | 32 |
| 32 NativeViewportImpl::~NativeViewportImpl() { | 33 NativeViewportImpl::~NativeViewportImpl() { |
| 33 // Destroy the NativeViewport early on as it may call us back during | 34 // Destroy the NativeViewport early on as it may call us back during |
| 34 // destruction and we want to be in a known state. | 35 // destruction and we want to be in a known state. |
| 35 platform_viewport_.reset(); | 36 platform_viewport_.reset(); |
| 36 } | 37 } |
| 37 | 38 |
| 38 void NativeViewportImpl::Create(RectPtr bounds) { | 39 void NativeViewportImpl::Create(RectPtr bounds) { |
| 39 platform_viewport_ = PlatformViewport::Create(this); | 40 platform_viewport_ = PlatformViewport::Create(this); |
| 40 platform_viewport_->Init(bounds.To<gfx::Rect>()); | 41 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(); | |
| 55 DCHECK(platform_viewport_); | 54 DCHECK(platform_viewport_); |
| 56 platform_viewport_->Close(); | 55 platform_viewport_->Close(); |
| 57 } | 56 } |
| 58 | 57 |
| 59 void NativeViewportImpl::SetBounds(RectPtr bounds) { | 58 void NativeViewportImpl::SetBounds(RectPtr bounds) { |
| 60 platform_viewport_->SetBounds(bounds.To<gfx::Rect>()); | 59 platform_viewport_->SetBounds(bounds.To<gfx::Rect>()); |
| 61 } | 60 } |
| 62 | 61 |
| 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 | |
| 73 void NativeViewportImpl::OnBoundsChanged(const gfx::Rect& bounds) { | 62 void NativeViewportImpl::OnBoundsChanged(const gfx::Rect& bounds) { |
| 74 CreateCommandBufferIfNeeded(); | |
| 75 client()->OnBoundsChanged(Rect::From(bounds)); | 63 client()->OnBoundsChanged(Rect::From(bounds)); |
| 76 } | 64 } |
| 77 | 65 |
| 78 void NativeViewportImpl::OnAcceleratedWidgetAvailable( | 66 void NativeViewportImpl::OnAcceleratedWidgetAvailable( |
| 79 gfx::AcceleratedWidget widget) { | 67 gfx::AcceleratedWidget widget) { |
| 80 widget_ = widget; | 68 widget_ = widget; |
| 81 CreateCommandBufferIfNeeded(); | 69 uintptr_t widget_ptr = bit_cast<uintptr_t>(widget); |
| 70 client()->OnCreated(static_cast<uint64_t>(widget_ptr)); |
| 82 } | 71 } |
| 83 | 72 |
| 84 bool NativeViewportImpl::OnEvent(ui::Event* ui_event) { | 73 bool NativeViewportImpl::OnEvent(ui::Event* ui_event) { |
| 85 // Must not return early before updating capture. | 74 // Must not return early before updating capture. |
| 86 switch (ui_event->type()) { | 75 switch (ui_event->type()) { |
| 87 case ui::ET_MOUSE_PRESSED: | 76 case ui::ET_MOUSE_PRESSED: |
| 88 case ui::ET_TOUCH_PRESSED: | 77 case ui::ET_TOUCH_PRESSED: |
| 89 platform_viewport_->SetCapture(); | 78 platform_viewport_->SetCapture(); |
| 90 break; | 79 break; |
| 91 case ui::ET_MOUSE_RELEASED: | 80 case ui::ET_MOUSE_RELEASED: |
| 92 case ui::ET_TOUCH_RELEASED: | 81 case ui::ET_TOUCH_RELEASED: |
| 93 platform_viewport_->ReleaseCapture(); | 82 platform_viewport_->ReleaseCapture(); |
| 94 break; | 83 break; |
| 95 default: | 84 default: |
| 96 break; | 85 break; |
| 97 } | 86 } |
| 98 | 87 |
| 99 if (waiting_for_event_ack_ && IsRateLimitedEventType(ui_event)) | 88 if (waiting_for_event_ack_ && IsRateLimitedEventType(ui_event)) |
| 100 return false; | 89 return false; |
| 101 | 90 |
| 102 client()->OnEvent( | 91 client()->OnEvent( |
| 103 TypeConverter<EventPtr, ui::Event>::ConvertFrom(*ui_event), | 92 TypeConverter<EventPtr, ui::Event>::ConvertFrom(*ui_event), |
| 104 base::Bind(&NativeViewportImpl::AckEvent, | 93 base::Bind(&NativeViewportImpl::AckEvent, |
| 105 weak_factory_.GetWeakPtr())); | 94 weak_factory_.GetWeakPtr())); |
| 106 waiting_for_event_ack_ = true; | 95 waiting_for_event_ack_ = true; |
| 107 return false; | 96 return false; |
| 108 } | 97 } |
| 109 | 98 |
| 110 void NativeViewportImpl::OnDestroyed() { | 99 void NativeViewportImpl::OnDestroyed() { |
| 111 client()->OnDestroyed(base::Bind(&NativeViewportImpl::AckDestroyed, | 100 client()->OnDestroyed(); |
| 112 base::Unretained(this))); | |
| 113 } | 101 } |
| 114 | 102 |
| 115 void NativeViewportImpl::AckEvent() { | 103 void NativeViewportImpl::AckEvent() { |
| 116 waiting_for_event_ack_ = false; | 104 waiting_for_event_ack_ = false; |
| 117 } | 105 } |
| 118 | 106 |
| 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 | |
| 137 } // namespace mojo | 107 } // namespace mojo |
| 138 | 108 |
| OLD | NEW |