blob: ea86753f8b31843163c46af831adb3eb1024556f [file] [log] [blame]
[email protected]538f9562012-01-17 17:46:041// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]81585f32011-07-29 19:32:062// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]81585f32011-07-29 19:32:065#include "base/at_exit.h"
6#include "base/command_line.h"
7#include "base/i18n/icu_util.h"
avi4ca294112015-12-24 08:04:098#include "base/macros.h"
[email protected]81585f32011-07-29 19:32:069#include "base/memory/scoped_ptr.h"
[email protected]59e69e742013-06-18 20:27:5210#include "base/message_loop/message_loop.h"
ckocagil8a064972015-02-09 17:05:4311#include "base/power_monitor/power_monitor.h"
12#include "base/power_monitor/power_monitor_device_source.h"
avi4ca294112015-12-24 08:04:0913#include "build/build_config.h"
[email protected]81585f32011-07-29 19:32:0614#include "third_party/skia/include/core/SkXfermode.h"
[email protected]48dea152012-11-02 20:26:4615#include "ui/aura/client/default_capture_client.h"
[email protected]e3225e02013-10-23 20:44:3716#include "ui/aura/client/window_tree_client.h"
[email protected]e050ef142012-03-21 01:04:2417#include "ui/aura/env.h"
[email protected]980969542013-10-18 22:07:3418#include "ui/aura/test/test_focus_client.h"
[email protected]6bdf7952012-11-14 10:10:5819#include "ui/aura/test/test_screen.h"
[email protected]dbf3d402011-09-14 21:32:1620#include "ui/aura/window.h"
21#include "ui/aura/window_delegate.h"
[email protected]2f2620332014-02-28 10:07:3822#include "ui/aura/window_tree_host.h"
[email protected]912be0e2011-11-09 19:05:2423#include "ui/base/hit_test.h"
danakj533d7342015-04-07 22:32:2924#include "ui/compositor/paint_recorder.h"
[email protected]27a80e752014-03-13 20:36:4125#include "ui/compositor/test/in_process_context_factory.h"
[email protected]86ccbd42013-09-18 18:11:5426#include "ui/events/event.h"
[email protected]94a0d2582012-03-09 00:30:5927#include "ui/gfx/canvas.h"
tfarina3b0452d2014-12-31 15:20:0928#include "ui/gfx/geometry/rect.h"
danakjc9631122015-03-02 23:50:3229#include "ui/gfx/skia_util.h"
[email protected]af7c5d92014-02-03 19:53:1530#include "ui/gl/gl_surface.h"
[email protected]81585f32011-07-29 19:32:0631
[email protected]4a6bef32011-09-07 02:06:0132#if defined(USE_X11)
kylechar4abed71f2016-04-06 14:46:4233#include "ui/gfx/x/x11_connection.h" // nogncheck
[email protected]4a6bef32011-09-07 02:06:0134#endif
35
[email protected]804f4792014-04-22 22:54:3736#if defined(OS_WIN)
37#include "ui/gfx/win/dpi.h"
38#endif
39
[email protected]81585f32011-07-29 19:32:0640namespace {
41
[email protected]8d8c7732011-08-25 22:35:1342// Trivial WindowDelegate implementation that draws a colored background.
[email protected]81585f32011-07-29 19:32:0643class DemoWindowDelegate : public aura::WindowDelegate {
44 public:
[email protected]8d8c7732011-08-25 22:35:1345 explicit DemoWindowDelegate(SkColor color) : color_(color) {}
[email protected]81585f32011-07-29 19:32:0646
[email protected]8dd791d2011-09-16 16:37:3047 // Overridden from WindowDelegate:
dcheng5bdeb6b2014-10-27 20:58:0548 gfx::Size GetMinimumSize() const override { return gfx::Size(); }
[email protected]fa574b62012-11-26 04:11:3849
dcheng5bdeb6b2014-10-27 20:58:0550 gfx::Size GetMaximumSize() const override { return gfx::Size(); }
[email protected]fa574b62012-11-26 04:11:3851
dcheng5bdeb6b2014-10-27 20:58:0552 void OnBoundsChanged(const gfx::Rect& old_bounds,
weiliangca032f93b2015-07-13 22:39:4753 const gfx::Rect& new_bounds) override {
54 window_bounds_ = new_bounds;
55 }
dcheng5bdeb6b2014-10-27 20:58:0556 gfx::NativeCursor GetCursor(const gfx::Point& point) override {
[email protected]62c9f102014-03-27 06:07:0457 return gfx::kNullCursor;
58 }
dcheng5bdeb6b2014-10-27 20:58:0559 int GetNonClientComponent(const gfx::Point& point) const override {
[email protected]eaf6dd52011-09-02 00:39:2660 return HTCAPTION;
61 }
dcheng5bdeb6b2014-10-27 20:58:0562 bool ShouldDescendIntoChildForEventHandling(
[email protected]334b81892012-06-16 16:38:1463 aura::Window* child,
mostynb3b3d52b2014-10-09 10:54:2764 const gfx::Point& location) override {
[email protected]334b81892012-06-16 16:38:1465 return true;
66 }
dcheng5bdeb6b2014-10-27 20:58:0567 bool CanFocus() override { return true; }
68 void OnCaptureLost() override {}
danakj85d970e2015-04-04 00:15:2469 void OnPaint(const ui::PaintContext& context) override {
weiliangca032f93b2015-07-13 22:39:4770 ui::PaintRecorder recorder(context, window_bounds_.size());
danakj533d7342015-04-07 22:32:2971 recorder.canvas()->DrawColor(color_, SkXfermode::kSrc_Mode);
danakjc9631122015-03-02 23:50:3272 gfx::Rect r;
danakj533d7342015-04-07 22:32:2973 recorder.canvas()->GetClipBounds(&r);
danakjc9631122015-03-02 23:50:3274 // Fill with a non-solid color so that the compositor will exercise its
75 // texture upload path.
76 while (!r.IsEmpty()) {
77 r.Inset(2, 2);
danakj533d7342015-04-07 22:32:2978 recorder.canvas()->FillRect(r, color_, SkXfermode::kXor_Mode);
danakjc9631122015-03-02 23:50:3279 }
[email protected]8d8c7732011-08-25 22:35:1380 }
dcheng5bdeb6b2014-10-27 20:58:0581 void OnDeviceScaleFactorChanged(float device_scale_factor) override {}
82 void OnWindowDestroying(aura::Window* window) override {}
83 void OnWindowDestroyed(aura::Window* window) override {}
84 void OnWindowTargetVisibilityChanged(bool visible) override {}
85 bool HasHitTestMask() const override { return false; }
86 void GetHitTestMask(gfx::Path* mask) const override {}
[email protected]970aa362011-08-30 20:03:3487
[email protected]81585f32011-07-29 19:32:0688 private:
[email protected]b1b155512011-08-18 22:47:5089 SkColor color_;
weiliangca032f93b2015-07-13 22:39:4790 gfx::Rect window_bounds_;
[email protected]b1b155512011-08-18 22:47:5091
[email protected]81585f32011-07-29 19:32:0692 DISALLOW_COPY_AND_ASSIGN(DemoWindowDelegate);
93};
94
[email protected]e3225e02013-10-23 20:44:3795class DemoWindowTreeClient : public aura::client::WindowTreeClient {
[email protected]b370f4f2012-03-12 20:16:2396 public:
[email protected]e3225e02013-10-23 20:44:3797 explicit DemoWindowTreeClient(aura::Window* window) : window_(window) {
98 aura::client::SetWindowTreeClient(window_, this);
[email protected]b370f4f2012-03-12 20:16:2399 }
100
dcheng5bdeb6b2014-10-27 20:58:05101 ~DemoWindowTreeClient() override {
yukishiino907eabf2015-01-28 04:31:12102 aura::client::SetWindowTreeClient(window_, nullptr);
[email protected]b370f4f2012-03-12 20:16:23103 }
104
[email protected]e3225e02013-10-23 20:44:37105 // Overridden from aura::client::WindowTreeClient:
dcheng5bdeb6b2014-10-27 20:58:05106 aura::Window* GetDefaultParent(aura::Window* context,
107 aura::Window* window,
108 const gfx::Rect& bounds) override {
[email protected]363e7542013-04-23 22:33:14109 if (!capture_client_) {
[email protected]be2a0ac2012-07-24 11:23:22110 capture_client_.reset(
[email protected]e3225e02013-10-23 20:44:37111 new aura::client::DefaultCaptureClient(window_->GetRootWindow()));
[email protected]be2a0ac2012-07-24 11:23:22112 }
[email protected]e3225e02013-10-23 20:44:37113 return window_;
[email protected]b370f4f2012-03-12 20:16:23114 }
115
116 private:
[email protected]e3225e02013-10-23 20:44:37117 aura::Window* window_;
[email protected]b370f4f2012-03-12 20:16:23118
[email protected]48dea152012-11-02 20:26:46119 scoped_ptr<aura::client::DefaultCaptureClient> capture_client_;
[email protected]be2a0ac2012-07-24 11:23:22120
[email protected]e3225e02013-10-23 20:44:37121 DISALLOW_COPY_AND_ASSIGN(DemoWindowTreeClient);
[email protected]b370f4f2012-03-12 20:16:23122};
[email protected]81585f32011-07-29 19:32:06123
[email protected]f7a0b4d22012-09-18 16:31:01124int DemoMain() {
[email protected]27a80e752014-03-13 20:36:41125#if defined(USE_X11)
126 // This demo uses InProcessContextFactory which uses X on a separate Gpu
127 // thread.
[email protected]82f884d6ffb2014-04-15 03:34:54128 gfx::InitializeThreadedX11();
[email protected]27a80e752014-03-13 20:36:41129#endif
[email protected]f321f80b2013-09-11 22:59:14130
[email protected]af7c5d92014-02-03 19:53:15131 gfx::GLSurface::InitializeOneOff();
132
[email protected]804f4792014-04-22 22:54:37133#if defined(OS_WIN)
pkasting91475b02015-12-17 08:35:24134 gfx::SetDefaultDeviceScaleFactor(1.0f);
[email protected]804f4792014-04-22 22:54:37135#endif
136
[email protected]f321f80b2013-09-11 22:59:14137 // The ContextFactory must exist before any Compositors are created.
weiliangc5efa0a12015-01-29 19:56:46138 bool context_factory_for_test = false;
[email protected]27a80e752014-03-13 20:36:41139 scoped_ptr<ui::InProcessContextFactory> context_factory(
jbaumanf400ce532015-02-03 04:54:59140 new ui::InProcessContextFactory(context_factory_for_test, nullptr));
skybd253e82014-11-11 01:49:07141 context_factory->set_use_test_surface(false);
[email protected]27a80e752014-03-13 20:36:41142
143 // Create the message-loop here before creating the root window.
144 base::MessageLoopForUI message_loop;
[email protected]f321f80b2013-09-11 22:59:14145
ckocagil8a064972015-02-09 17:05:43146 base::PowerMonitor power_monitor(make_scoped_ptr(
147 new base::PowerMonitorDeviceSource));
148
fsamuel58ea5a72015-08-18 22:46:11149 aura::Env::CreateInstance(true);
150 aura::Env::GetInstance()->set_context_factory(context_factory.get());
[email protected]ad3a62c2014-06-20 13:18:34151 scoped_ptr<aura::TestScreen> test_screen(
152 aura::TestScreen::Create(gfx::Size()));
scottmgfb33c342016-01-27 01:30:36153 gfx::Screen::SetScreenInstance(test_screen.get());
[email protected]2f2620332014-02-28 10:07:38154 scoped_ptr<aura::WindowTreeHost> host(
155 test_screen->CreateHostForPrimaryDisplay());
156 scoped_ptr<DemoWindowTreeClient> window_tree_client(
157 new DemoWindowTreeClient(host->window()));
[email protected]980969542013-10-18 22:07:34158 aura::test::TestFocusClient focus_client;
[email protected]2f2620332014-02-28 10:07:38159 aura::client::SetFocusClient(host->window(), &focus_client);
[email protected]81585f32011-07-29 19:32:06160
[email protected]b1b155512011-08-18 22:47:50161 // Create a hierarchy of test windows.
weiliangca032f93b2015-07-13 22:39:47162 gfx::Rect window1_bounds(100, 100, 400, 400);
[email protected]8d8c7732011-08-25 22:35:13163 DemoWindowDelegate window_delegate1(SK_ColorBLUE);
164 aura::Window window1(&window_delegate1);
[email protected]b1b155512011-08-18 22:47:50165 window1.set_id(1);
danakjb161836d2015-04-03 05:14:18166 window1.Init(ui::LAYER_TEXTURED);
weiliangca032f93b2015-07-13 22:39:47167 window1.SetBounds(window1_bounds);
[email protected]a3929aba2011-09-30 18:30:17168 window1.Show();
[email protected]2f2620332014-02-28 10:07:38169 aura::client::ParentWindowWithContext(&window1, host->window(), gfx::Rect());
[email protected]b1b155512011-08-18 22:47:50170
weiliangca032f93b2015-07-13 22:39:47171 gfx::Rect window2_bounds(200, 200, 350, 350);
[email protected]8d8c7732011-08-25 22:35:13172 DemoWindowDelegate window_delegate2(SK_ColorRED);
173 aura::Window window2(&window_delegate2);
[email protected]b1b155512011-08-18 22:47:50174 window2.set_id(2);
danakjb161836d2015-04-03 05:14:18175 window2.Init(ui::LAYER_TEXTURED);
weiliangca032f93b2015-07-13 22:39:47176 window2.SetBounds(window2_bounds);
[email protected]a3929aba2011-09-30 18:30:17177 window2.Show();
[email protected]2f2620332014-02-28 10:07:38178 aura::client::ParentWindowWithContext(&window2, host->window(), gfx::Rect());
[email protected]b1b155512011-08-18 22:47:50179
weiliangca032f93b2015-07-13 22:39:47180 gfx::Rect window3_bounds(10, 10, 50, 50);
[email protected]8d8c7732011-08-25 22:35:13181 DemoWindowDelegate window_delegate3(SK_ColorGREEN);
182 aura::Window window3(&window_delegate3);
[email protected]b1b155512011-08-18 22:47:50183 window3.set_id(3);
danakjb161836d2015-04-03 05:14:18184 window3.Init(ui::LAYER_TEXTURED);
weiliangca032f93b2015-07-13 22:39:47185 window3.SetBounds(window3_bounds);
[email protected]a3929aba2011-09-30 18:30:17186 window3.Show();
[email protected]5ebe6102012-11-28 21:00:03187 window2.AddChild(&window3);
[email protected]b1b155512011-08-18 22:47:50188
[email protected]2f2620332014-02-28 10:07:38189 host->Show();
[email protected]7060d6592013-04-29 19:01:48190 base::MessageLoopForUI::current()->Run();
[email protected]9783633c2011-10-27 15:41:52191
[email protected]81585f32011-07-29 19:32:06192 return 0;
193}
[email protected]f7a0b4d22012-09-18 16:31:01194
[email protected]f7a0b4d22012-09-18 16:31:01195} // namespace
196
197int main(int argc, char** argv) {
avi6b10fd02014-12-23 05:51:23198 base::CommandLine::Init(argc, argv);
[email protected]f7a0b4d22012-09-18 16:31:01199
200 // The exit manager is in charge of calling the dtors of singleton objects.
201 base::AtExitManager exit_manager;
202
[email protected]d7477f02013-08-23 00:39:09203 base::i18n::InitializeICU();
[email protected]f7a0b4d22012-09-18 16:31:01204
[email protected]56deede2012-10-01 21:25:12205 return DemoMain();
[email protected]f7a0b4d22012-09-18 16:31:01206}