test_compositor_host_x11.cc revision eb525c5499e34cc9c4b825d6d9e75bb07cc06ace
1a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved.
22a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
32a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// found in the LICENSE file.
42a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
5a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "ui/compositor/test/test_compositor_host.h"
62a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
72a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include <X11/Xlib.h>
82a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
9f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "base/basictypes.h"
102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/bind.h"
112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/compiler_specific.h"
125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/logging.h"
13a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "base/memory/scoped_ptr.h"
145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/memory/weak_ptr.h"
15868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/message_loop.h"
162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "ui/base/x/x11_util.h"
17a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "ui/compositor/compositor.h"
182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "ui/gfx/rect.h"
192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)namespace ui {
212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
22a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)class TestCompositorHostX11 : public TestCompositorHost,
23a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                              public CompositorDelegate {
24a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) public:
25a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  TestCompositorHostX11(const gfx::Rect& bounds);
26a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  virtual ~TestCompositorHostX11();
2723730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)
2823730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles) private:
2923730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  // Overridden from TestCompositorHost:
3023730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  virtual void Show() OVERRIDE;
31a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  virtual ui::Compositor* GetCompositor() OVERRIDE;
32a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
33a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // Overridden from CompositorDelegate:
34a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  virtual void ScheduleDraw() OVERRIDE;
35a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
36a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  void Draw();
37a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
38a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  gfx::Rect bounds_;
39a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
40a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  scoped_ptr<ui::Compositor> compositor_;
41a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
42a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  XID window_;
435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
44a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  base::WeakPtrFactory<TestCompositorHostX11> method_factory_;
451320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
461320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  DISALLOW_COPY_AND_ASSIGN(TestCompositorHostX11);
475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)};
481320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
49868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)TestCompositorHostX11::TestCompositorHostX11(const gfx::Rect& bounds)
50c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    : bounds_(bounds),
515f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      method_factory_(this) {
52a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
53a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
54a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)TestCompositorHostX11::~TestCompositorHostX11() {
5523730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)}
560529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
571320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tuccivoid TestCompositorHostX11::Show() {
58010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  Display* display = GetXDisplay();
5923730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  XSetWindowAttributes swa;
602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  swa.event_mask = StructureNotifyMask | ExposureMask;
61a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  swa.override_redirect = True;
62a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  window_ = XCreateWindow(
63116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      display,
64116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      RootWindow(display, DefaultScreen(display)),  // parent
65116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      bounds_.x(), bounds_.y(), bounds_.width(), bounds_.height(),
66116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      0,  // border width
67116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      CopyFromParent,  // depth
68116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      InputOutput,
69116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      CopyFromParent,  // visual
70116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      CWEventMask | CWOverrideRedirect, &swa);
71116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  XMapWindow(display, window_);
72116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
73116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  while (1) {
74116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    XEvent event;
75116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    XNextEvent(display, &event);
76116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    if (event.type == MapNotify && event.xmap.window == window_)
77116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      break;
78116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  }
79116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  compositor_.reset(new ui::Compositor(this, window_));
80116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  compositor_->SetScaleAndSize(1.0f, bounds_.size());
81116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)ui::Compositor* TestCompositorHostX11::GetCompositor() {
84a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  return compositor_.get();
85a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
865c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu
87a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)void TestCompositorHostX11::ScheduleDraw() {
88a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  DCHECK(!ui::Compositor::WasInitializedWithThread());
8923730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  if (!method_factory_.HasWeakPtrs()) {
905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    base::MessageLoopForUI::current()->PostTask(
91868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        FROM_HERE,
92868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        base::Bind(&TestCompositorHostX11::Draw,
93868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                   method_factory_.GetWeakPtr()));
942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
95868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
97c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void TestCompositorHostX11::Draw() {
985c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  if (compositor_.get())
992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    compositor_->Draw();
100868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
101a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
1025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// static
1035c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo LiuTestCompositorHost* TestCompositorHost::Create(const gfx::Rect& bounds) {
104a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  return new TestCompositorHostX11(bounds);
105f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)}
106f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
1074e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)}  // namespace ui
1082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)