Index: services/ui/ws/frame_generator.cc |
diff --git a/services/ui/ws/platform_display.cc b/services/ui/ws/frame_generator.cc |
similarity index 36% |
copy from services/ui/ws/platform_display.cc |
copy to services/ui/ws/frame_generator.cc |
index 0e7166a9bccd97a1b13eba2c6c9e169b5b992b26..aea380b64ecbc2158d084fb10a0c4cbd31f2a232 100644 |
--- a/services/ui/ws/platform_display.cc |
+++ b/services/ui/ws/frame_generator.cc |
@@ -1,58 +1,114 @@ |
-// Copyright 2014 The Chromium Authors. All rights reserved. |
+// Copyright 2016 The Chromium Authors. All rights reserved. |
// Use of this source code is governed by a BSD-style license that can be |
// found in the LICENSE file. |
-#include "services/ui/ws/platform_display.h" |
+#include "services/ui/ws/frame_generator.h" |
-#include "base/numerics/safe_conversions.h" |
-#include "build/build_config.h" |
-#include "cc/ipc/quads.mojom.h" |
#include "cc/output/compositor_frame.h" |
-#include "cc/output/copy_output_request.h" |
-#include "cc/output/delegated_frame_data.h" |
#include "cc/quads/render_pass.h" |
#include "cc/quads/shared_quad_state.h" |
#include "cc/quads/surface_draw_quad.h" |
-#include "services/shell/public/cpp/connection.h" |
-#include "services/shell/public/cpp/connector.h" |
-#include "services/ui/gles2/gpu_state.h" |
-#include "services/ui/public/interfaces/gpu.mojom.h" |
#include "services/ui/surfaces/display_compositor.h" |
-#include "services/ui/surfaces/surfaces_state.h" |
-#include "services/ui/ws/platform_display_factory.h" |
+#include "services/ui/ws/frame_generator_delegate.h" |
#include "services/ui/ws/server_window.h" |
#include "services/ui/ws/server_window_surface.h" |
#include "services/ui/ws/server_window_surface_manager.h" |
-#include "services/ui/ws/window_coordinate_conversions.h" |
-#include "third_party/skia/include/core/SkXfermode.h" |
-#include "ui/base/cursor/cursor_loader.h" |
-#include "ui/display/display.h" |
-#include "ui/events/event.h" |
-#include "ui/events/event_utils.h" |
-#include "ui/platform_window/platform_ime_controller.h" |
-#include "ui/platform_window/platform_window.h" |
- |
-#if defined(OS_WIN) |
-#include "ui/platform_window/win/win_window.h" |
-#elif defined(USE_X11) |
-#include "ui/platform_window/x11/x11_window.h" |
-#elif defined(OS_ANDROID) |
-#include "ui/platform_window/android/platform_window_android.h" |
-#elif defined(USE_OZONE) |
-#include "ui/ozone/public/ozone_platform.h" |
-#endif |
namespace ui { |
namespace ws { |
-namespace { |
-// DrawWindowTree recursively visits ServerWindows, creating a SurfaceDrawQuad |
-// for each that lacks one. |
-void DrawWindowTree(cc::RenderPass* pass, |
- ServerWindow* window, |
- const gfx::Vector2d& parent_to_root_origin_offset, |
- float opacity) { |
+FrameGenerator::FrameGenerator(FrameGeneratorDelegate* delegate, |
+ scoped_refptr<GpuState> gpu_state, |
+ scoped_refptr<SurfacesState> surfaces_state) |
+ : delegate_(delegate), |
+ gpu_state_(gpu_state), |
+ surfaces_state_(surfaces_state), |
+ draw_timer_(false, false), |
+ weak_factory_(this) {} |
+ |
+FrameGenerator::~FrameGenerator() { |
+ // Invalidate WeakPtrs now to avoid callbacks back into the |
+ // FrameGenerator during destruction of |display_compositor_|. |
+ weak_factory_.InvalidateWeakPtrs(); |
+ display_compositor_.reset(); |
+} |
+ |
+void FrameGenerator::RequestRedraw(const gfx::Rect& redraw_region) { |
+ dirty_rect_.Union(redraw_region); |
+ WantToDraw(); |
+} |
+ |
+void FrameGenerator::OnAcceleratedWidgetAvailable( |
+ gfx::AcceleratedWidget widget) { |
+ if (widget != gfx::kNullAcceleratedWidget) { |
+ display_compositor_.reset( |
+ new DisplayCompositor(base::ThreadTaskRunnerHandle::Get(), widget, |
+ gpu_state_, surfaces_state_)); |
+ } |
+} |
+ |
+void FrameGenerator::RequestCopyOfOutput( |
+ std::unique_ptr<cc::CopyOutputRequest> output_request) { |
+ if (display_compositor_) |
+ display_compositor_->RequestCopyOfOutput(std::move(output_request)); |
+} |
+ |
+void FrameGenerator::WantToDraw() { |
+ if (draw_timer_.IsRunning() || frame_pending_) |
+ return; |
+ |
+ // TODO(rjkroege): Use vblank to kick off Draw. |
+ draw_timer_.Start( |
+ FROM_HERE, base::TimeDelta(), |
+ base::Bind(&FrameGenerator::Draw, weak_factory_.GetWeakPtr())); |
+} |
+ |
+void FrameGenerator::Draw() { |
+ if (!delegate_->GetRootWindow()->visible()) |
+ return; |
+ |
+ // TODO(fsamuel): We should add a trace for generating a top level frame. |
+ cc::CompositorFrame frame(GenerateCompositorFrame()); |
+ frame_pending_ = true; |
+ if (display_compositor_) { |
+ display_compositor_->SubmitCompositorFrame( |
+ std::move(frame), |
+ base::Bind(&FrameGenerator::DidDraw, weak_factory_.GetWeakPtr())); |
+ } |
+ dirty_rect_ = gfx::Rect(); |
+} |
+ |
+void FrameGenerator::DidDraw(cc::SurfaceDrawStatus status) { |
+ frame_pending_ = false; |
+ delegate_->OnCompositorFrameDrawn(); |
+ if (!dirty_rect_.IsEmpty()) |
+ WantToDraw(); |
+} |
+ |
+cc::CompositorFrame FrameGenerator::GenerateCompositorFrame() { |
+ const ViewportMetrics& metrics = delegate_->GetViewportMetrics(); |
+ std::unique_ptr<cc::RenderPass> render_pass = cc::RenderPass::Create(); |
+ render_pass->damage_rect = dirty_rect_; |
+ render_pass->output_rect = gfx::Rect(metrics.size_in_pixels); |
+ |
+ DrawWindowTree(render_pass.get(), delegate_->GetRootWindow(), gfx::Vector2d(), |
+ 1.0f); |
+ |
+ std::unique_ptr<cc::DelegatedFrameData> frame_data( |
+ new cc::DelegatedFrameData); |
+ frame_data->render_pass_list.push_back(std::move(render_pass)); |
+ |
+ cc::CompositorFrame frame; |
+ frame.delegated_frame_data = std::move(frame_data); |
+ return frame; |
+} |
+ |
+void FrameGenerator::DrawWindowTree( |
+ cc::RenderPass* pass, |
+ ServerWindow* window, |
+ const gfx::Vector2d& parent_to_root_origin_offset, |
+ float opacity) { |
if (!window->visible()) |
return; |
@@ -124,291 +180,6 @@ void DrawWindowTree(cc::RenderPass* pass, |
} |
} |
-} // namespace |
- |
-// static |
-PlatformDisplayFactory* PlatformDisplay::factory_ = nullptr; |
- |
-// static |
-PlatformDisplay* PlatformDisplay::Create( |
- const PlatformDisplayInitParams& init_params) { |
- if (factory_) |
- return factory_->CreatePlatformDisplay(); |
- |
- return new DefaultPlatformDisplay(init_params); |
-} |
- |
-DefaultPlatformDisplay::DefaultPlatformDisplay( |
- const PlatformDisplayInitParams& init_params) |
- : display_id_(init_params.display_id), |
- gpu_state_(init_params.gpu_state), |
- surfaces_state_(init_params.surfaces_state), |
- delegate_(nullptr), |
- draw_timer_(false, false), |
- frame_pending_(false), |
-#if !defined(OS_ANDROID) |
- cursor_loader_(ui::CursorLoader::Create()), |
-#endif |
- weak_factory_(this) { |
- metrics_.size_in_pixels = init_params.display_bounds.size(); |
- // TODO(rjkroege): Preserve the display_id when Ozone platform can use it. |
-} |
- |
-void DefaultPlatformDisplay::Init(PlatformDisplayDelegate* delegate) { |
- delegate_ = delegate; |
- |
- gfx::Rect bounds(metrics_.size_in_pixels); |
-#if defined(OS_WIN) |
- platform_window_.reset(new ui::WinWindow(this, bounds)); |
-#elif defined(USE_X11) |
- platform_window_.reset(new ui::X11Window(this)); |
-#elif defined(OS_ANDROID) |
- platform_window_.reset(new ui::PlatformWindowAndroid(this)); |
-#elif defined(USE_OZONE) |
- platform_window_ = |
- ui::OzonePlatform::GetInstance()->CreatePlatformWindow(this, bounds); |
-#else |
- NOTREACHED() << "Unsupported platform"; |
-#endif |
- platform_window_->SetBounds(bounds); |
- platform_window_->Show(); |
-} |
- |
-DefaultPlatformDisplay::~DefaultPlatformDisplay() { |
- // Don't notify the delegate from the destructor. |
- delegate_ = nullptr; |
- |
- // Invalidate WeakPtrs now to avoid callbacks back into the |
- // DefaultPlatformDisplay during destruction of |display_compositor_|. |
- weak_factory_.InvalidateWeakPtrs(); |
- display_compositor_.reset(); |
- // Destroy the PlatformWindow early on as it may call us back during |
- // destruction and we want to be in a known state. But destroy the surface |
- // first because it can still be using the platform window. |
- platform_window_.reset(); |
-} |
- |
-void DefaultPlatformDisplay::SchedulePaint(const ServerWindow* window, |
- const gfx::Rect& bounds) { |
- DCHECK(window); |
- if (!window->IsDrawn()) |
- return; |
- const gfx::Rect root_relative_rect = |
- ConvertRectBetweenWindows(window, delegate_->GetRootWindow(), bounds); |
- if (root_relative_rect.IsEmpty()) |
- return; |
- dirty_rect_.Union(root_relative_rect); |
- WantToDraw(); |
-} |
- |
-void DefaultPlatformDisplay::SetViewportSize(const gfx::Size& size) { |
- platform_window_->SetBounds(gfx::Rect(size)); |
-} |
- |
-void DefaultPlatformDisplay::SetTitle(const base::string16& title) { |
- platform_window_->SetTitle(title); |
-} |
- |
-void DefaultPlatformDisplay::SetCapture() { |
- platform_window_->SetCapture(); |
-} |
- |
-void DefaultPlatformDisplay::ReleaseCapture() { |
- platform_window_->ReleaseCapture(); |
-} |
- |
-void DefaultPlatformDisplay::SetCursorById(int32_t cursor_id) { |
-#if !defined(OS_ANDROID) |
- // TODO(erg): This still isn't sufficient, and will only use native cursors |
- // that chrome would use, not custom image cursors. For that, we should |
- // delegate to the window manager to load images from resource packs. |
- // |
- // We probably also need to deal with different DPIs. |
- ui::Cursor cursor(cursor_id); |
- cursor_loader_->SetPlatformCursor(&cursor); |
- platform_window_->SetCursor(cursor.platform()); |
-#endif |
-} |
- |
-float DefaultPlatformDisplay::GetDeviceScaleFactor() { |
- return metrics_.device_scale_factor; |
-} |
- |
-mojom::Rotation DefaultPlatformDisplay::GetRotation() { |
- // TODO(sky): implement me. |
- return mojom::Rotation::VALUE_0; |
-} |
- |
-void DefaultPlatformDisplay::UpdateTextInputState( |
- const ui::TextInputState& state) { |
- ui::PlatformImeController* ime = platform_window_->GetPlatformImeController(); |
- if (ime) |
- ime->UpdateTextInputState(state); |
-} |
- |
-void DefaultPlatformDisplay::SetImeVisibility(bool visible) { |
- ui::PlatformImeController* ime = platform_window_->GetPlatformImeController(); |
- if (ime) |
- ime->SetImeVisibility(visible); |
-} |
- |
-void DefaultPlatformDisplay::Draw() { |
- if (!delegate_->GetRootWindow()->visible()) |
- return; |
- |
- // TODO(fsamuel): We should add a trace for generating a top level frame. |
- cc::CompositorFrame frame(GenerateCompositorFrame()); |
- frame_pending_ = true; |
- if (display_compositor_) { |
- display_compositor_->SubmitCompositorFrame( |
- std::move(frame), base::Bind(&DefaultPlatformDisplay::DidDraw, |
- weak_factory_.GetWeakPtr())); |
- } |
- dirty_rect_ = gfx::Rect(); |
-} |
- |
-void DefaultPlatformDisplay::DidDraw(cc::SurfaceDrawStatus status) { |
- frame_pending_ = false; |
- delegate_->OnCompositorFrameDrawn(); |
- if (!dirty_rect_.IsEmpty()) |
- WantToDraw(); |
-} |
- |
-bool DefaultPlatformDisplay::IsFramePending() const { |
- return frame_pending_; |
-} |
- |
-int64_t DefaultPlatformDisplay::GetDisplayId() const { |
- return display_id_; |
-} |
- |
-void DefaultPlatformDisplay::WantToDraw() { |
- if (draw_timer_.IsRunning() || frame_pending_) |
- return; |
- |
- // TODO(rjkroege): Use vblank to kick off Draw. |
- draw_timer_.Start( |
- FROM_HERE, base::TimeDelta(), |
- base::Bind(&DefaultPlatformDisplay::Draw, weak_factory_.GetWeakPtr())); |
-} |
- |
-void DefaultPlatformDisplay::UpdateMetrics(const gfx::Size& size, |
- float device_scale_factor) { |
- if (display::Display::HasForceDeviceScaleFactor()) |
- device_scale_factor = display::Display::GetForcedDeviceScaleFactor(); |
- if (metrics_.size_in_pixels == size && |
- metrics_.device_scale_factor == device_scale_factor) |
- return; |
- |
- ViewportMetrics old_metrics = metrics_; |
- metrics_.size_in_pixels = size; |
- metrics_.device_scale_factor = device_scale_factor; |
- delegate_->OnViewportMetricsChanged(old_metrics, metrics_); |
-} |
- |
-cc::CompositorFrame DefaultPlatformDisplay::GenerateCompositorFrame() { |
- std::unique_ptr<cc::RenderPass> render_pass = cc::RenderPass::Create(); |
- render_pass->damage_rect = dirty_rect_; |
- render_pass->output_rect = gfx::Rect(metrics_.size_in_pixels); |
- |
- DrawWindowTree(render_pass.get(), delegate_->GetRootWindow(), gfx::Vector2d(), |
- 1.0f); |
- |
- std::unique_ptr<cc::DelegatedFrameData> frame_data( |
- new cc::DelegatedFrameData); |
- frame_data->render_pass_list.push_back(std::move(render_pass)); |
- |
- cc::CompositorFrame frame; |
- frame.delegated_frame_data = std::move(frame_data); |
- return frame; |
-} |
- |
-void DefaultPlatformDisplay::OnBoundsChanged(const gfx::Rect& new_bounds) { |
- UpdateMetrics(new_bounds.size(), metrics_.device_scale_factor); |
-} |
- |
-void DefaultPlatformDisplay::OnDamageRect(const gfx::Rect& damaged_region) { |
- dirty_rect_.Union(damaged_region); |
- WantToDraw(); |
-} |
- |
-void DefaultPlatformDisplay::DispatchEvent(ui::Event* event) { |
- if (event->IsScrollEvent()) { |
- // TODO(moshayedi): crbug.com/602859. Dispatch scroll events as |
- // they are once we have proper support for scroll events. |
- delegate_->OnEvent(ui::MouseWheelEvent(*event->AsScrollEvent())); |
- } else if (event->IsMouseEvent() && !event->IsMouseWheelEvent()) { |
- delegate_->OnEvent(ui::PointerEvent(*event->AsMouseEvent())); |
- } else if (event->IsTouchEvent()) { |
- delegate_->OnEvent(ui::PointerEvent(*event->AsTouchEvent())); |
- } else { |
- delegate_->OnEvent(*event); |
- } |
- |
-#if defined(USE_X11) || defined(USE_OZONE) |
- // We want to emulate the WM_CHAR generation behaviour of Windows. |
- // |
- // On Linux, we've previously inserted characters by having |
- // InputMethodAuraLinux take all key down events and send a character event |
- // to the TextInputClient. This causes a mismatch in code that has to be |
- // shared between Windows and Linux, including blink code. Now that we're |
- // trying to have one way of doing things, we need to standardize on and |
- // emulate Windows character events. |
- // |
- // This is equivalent to what we're doing in the current Linux port, but |
- // done once instead of done multiple times in different places. |
- if (event->type() == ui::ET_KEY_PRESSED) { |
- ui::KeyEvent* key_press_event = event->AsKeyEvent(); |
- ui::KeyEvent char_event(key_press_event->GetCharacter(), |
- key_press_event->key_code(), |
- key_press_event->flags()); |
- DCHECK_EQ(key_press_event->GetCharacter(), char_event.GetCharacter()); |
- DCHECK_EQ(key_press_event->key_code(), char_event.key_code()); |
- DCHECK_EQ(key_press_event->flags(), char_event.flags()); |
- delegate_->OnEvent(char_event); |
- } |
-#endif |
-} |
- |
-void DefaultPlatformDisplay::OnCloseRequest() { |
- platform_window_->Close(); |
-} |
- |
-void DefaultPlatformDisplay::OnClosed() { |
- if (delegate_) |
- delegate_->OnDisplayClosed(); |
-} |
- |
-void DefaultPlatformDisplay::OnWindowStateChanged( |
- ui::PlatformWindowState new_state) {} |
- |
-void DefaultPlatformDisplay::OnLostCapture() { |
- delegate_->OnNativeCaptureLost(); |
-} |
- |
-void DefaultPlatformDisplay::OnAcceleratedWidgetAvailable( |
- gfx::AcceleratedWidget widget, |
- float device_scale_factor) { |
- if (widget != gfx::kNullAcceleratedWidget) { |
- display_compositor_.reset( |
- new DisplayCompositor(base::ThreadTaskRunnerHandle::Get(), widget, |
- gpu_state_, surfaces_state_)); |
- } |
- UpdateMetrics(metrics_.size_in_pixels, device_scale_factor); |
-} |
- |
-void DefaultPlatformDisplay::OnAcceleratedWidgetDestroyed() { |
- NOTREACHED(); |
-} |
- |
-void DefaultPlatformDisplay::OnActivationChanged(bool active) {} |
- |
-void DefaultPlatformDisplay::RequestCopyOfOutput( |
- std::unique_ptr<cc::CopyOutputRequest> output_request) { |
- if (display_compositor_) |
- display_compositor_->RequestCopyOfOutput(std::move(output_request)); |
-} |
- |
} // namespace ws |
} // namespace ui |