1 // Copyright 2013 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #include "ui/compositor/test/test_compositor_host.h" 6 7 #include <X11/Xlib.h> 8 9 #include "base/basictypes.h" 10 #include "base/bind.h" 11 #include "base/compiler_specific.h" 12 #include "base/logging.h" 13 #include "base/memory/scoped_ptr.h" 14 #include "base/memory/weak_ptr.h" 15 #include "base/message_loop/message_loop.h" 16 #include "ui/base/x/x11_util.h" 17 #include "ui/compositor/compositor.h" 18 #include "ui/gfx/rect.h" 19 20 namespace ui { 21 22 class TestCompositorHostX11 : public TestCompositorHost, 23 public CompositorDelegate { 24 public: 25 TestCompositorHostX11(const gfx::Rect& bounds); 26 virtual ~TestCompositorHostX11(); 27 28 private: 29 // Overridden from TestCompositorHost: 30 virtual void Show() OVERRIDE; 31 virtual ui::Compositor* GetCompositor() OVERRIDE; 32 33 // Overridden from CompositorDelegate: 34 virtual void ScheduleDraw() OVERRIDE; 35 36 void Draw(); 37 38 gfx::Rect bounds_; 39 40 scoped_ptr<ui::Compositor> compositor_; 41 42 XID window_; 43 44 base::WeakPtrFactory<TestCompositorHostX11> method_factory_; 45 46 DISALLOW_COPY_AND_ASSIGN(TestCompositorHostX11); 47 }; 48 49 TestCompositorHostX11::TestCompositorHostX11(const gfx::Rect& bounds) 50 : bounds_(bounds), 51 method_factory_(this) { 52 } 53 54 TestCompositorHostX11::~TestCompositorHostX11() { 55 } 56 57 void TestCompositorHostX11::Show() { 58 Display* display = GetXDisplay(); 59 XSetWindowAttributes swa; 60 swa.event_mask = StructureNotifyMask | ExposureMask; 61 swa.override_redirect = True; 62 window_ = XCreateWindow( 63 display, 64 RootWindow(display, DefaultScreen(display)), // parent 65 bounds_.x(), bounds_.y(), bounds_.width(), bounds_.height(), 66 0, // border width 67 CopyFromParent, // depth 68 InputOutput, 69 CopyFromParent, // visual 70 CWEventMask | CWOverrideRedirect, &swa); 71 XMapWindow(display, window_); 72 73 while (1) { 74 XEvent event; 75 XNextEvent(display, &event); 76 if (event.type == MapNotify && event.xmap.window == window_) 77 break; 78 } 79 compositor_.reset(new ui::Compositor(this, window_)); 80 compositor_->SetScaleAndSize(1.0f, bounds_.size()); 81 } 82 83 ui::Compositor* TestCompositorHostX11::GetCompositor() { 84 return compositor_.get(); 85 } 86 87 void TestCompositorHostX11::ScheduleDraw() { 88 DCHECK(!ui::Compositor::WasInitializedWithThread()); 89 if (!method_factory_.HasWeakPtrs()) { 90 base::MessageLoopForUI::current()->PostTask( 91 FROM_HERE, 92 base::Bind(&TestCompositorHostX11::Draw, 93 method_factory_.GetWeakPtr())); 94 } 95 } 96 97 void TestCompositorHostX11::Draw() { 98 if (compositor_.get()) 99 compositor_->Draw(); 100 } 101 102 // static 103 TestCompositorHost* TestCompositorHost::Create(const gfx::Rect& bounds) { 104 return new TestCompositorHostX11(bounds); 105 } 106 107 } // namespace ui 108