OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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.h" | 5 #include "mojo/services/native_viewport/native_viewport.h" |
6 | 6 |
| 7 #include <X11/Xlib.h> |
| 8 |
| 9 #include "base/bind.h" |
| 10 #include "base/logging.h" |
| 11 #include "base/message_loop/message_loop.h" |
| 12 #include "base/message_loop/message_pump_x11.h" |
| 13 #include "gpu/command_buffer/client/gl_in_process_context.h" |
| 14 #include "gpu/command_buffer/client/gles2_implementation.h" |
| 15 #include "ui/gfx/rect.h" |
| 16 #include "ui/gfx/x/x11_types.h" |
| 17 |
7 namespace mojo { | 18 namespace mojo { |
8 namespace services { | 19 namespace services { |
9 | 20 |
10 class NativeViewportX11 : public NativeViewport { | 21 class NativeViewportX11 : public NativeViewport, |
| 22 public base::MessagePumpDispatcher { |
11 public: | 23 public: |
12 NativeViewportX11(NativeViewportDelegate* delegate) | 24 NativeViewportX11(NativeViewportDelegate* delegate) |
13 : delegate_(delegate) { | 25 : delegate_(delegate), |
| 26 bounds_(10, 10, 500, 500) { |
| 27 XDisplay* display = gfx::GetXDisplay(); |
| 28 XSetWindowAttributes swa; |
| 29 memset(&swa, 0, sizeof(swa)); |
| 30 swa.override_redirect = False; |
| 31 window_ = XCreateWindow( |
| 32 display, |
| 33 DefaultRootWindow(display), |
| 34 bounds_.x(), bounds_.y(), bounds_.width(), bounds_.height(), |
| 35 0, // border width |
| 36 CopyFromParent, // depth |
| 37 InputOutput, |
| 38 CopyFromParent, // visual |
| 39 CWBackPixmap | CWOverrideRedirect, &swa); |
| 40 |
| 41 base::MessagePumpX11::Current()->AddDispatcherForWindow(this, window_); |
| 42 base::MessagePumpX11::Current()->AddDispatcherForRootWindow(this); |
| 43 |
| 44 XMapWindow(display, window_); |
| 45 XFlush(display); |
| 46 |
| 47 gpu::GLInProcessContextAttribs attribs; |
| 48 gl_context_.reset(gpu::GLInProcessContext::CreateContext( |
| 49 false, window_, bounds_.size(), false, |
| 50 attribs, gfx::PreferDiscreteGpu)); |
| 51 gl_context_->SetContextLostCallback(base::Bind( |
| 52 &NativeViewportX11::OnGLContextLost, base::Unretained(this))); |
| 53 |
| 54 delegate_->OnGLContextAvailable(gl_context_->GetImplementation()); |
14 } | 55 } |
| 56 |
15 virtual ~NativeViewportX11() { | 57 virtual ~NativeViewportX11() { |
| 58 base::MessagePumpX11::Current()->RemoveDispatcherForRootWindow(this); |
| 59 base::MessagePumpX11::Current()->RemoveDispatcherForWindow(window_); |
| 60 |
| 61 XDestroyWindow(gfx::GetXDisplay(), window_); |
16 } | 62 } |
17 | 63 |
18 private: | 64 private: |
19 // Overridden from NativeViewport: | 65 // Overridden from NativeViewport: |
20 virtual void Close() OVERRIDE { | 66 virtual void Close() OVERRIDE { |
21 // TODO(beng): perform this in response to XWindow destruction. | 67 // TODO(beng): perform this in response to XWindow destruction. |
22 delegate_->OnDestroyed(); | 68 delegate_->OnDestroyed(); |
23 } | 69 } |
24 | 70 |
| 71 // Overridden from base::MessagePumpDispatcher: |
| 72 virtual bool Dispatch(const base::NativeEvent& event) OVERRIDE { |
| 73 return true; |
| 74 } |
| 75 |
| 76 void OnGLContextLost() { |
| 77 gl_context_.reset(); |
| 78 delegate_->OnGLContextLost(); |
| 79 } |
| 80 |
25 NativeViewportDelegate* delegate_; | 81 NativeViewportDelegate* delegate_; |
| 82 gfx::Rect bounds_; |
| 83 XID window_; |
| 84 scoped_ptr<gpu::GLInProcessContext> gl_context_; |
26 | 85 |
27 DISALLOW_COPY_AND_ASSIGN(NativeViewportX11); | 86 DISALLOW_COPY_AND_ASSIGN(NativeViewportX11); |
28 }; | 87 }; |
29 | 88 |
30 // static | 89 // static |
31 scoped_ptr<NativeViewport> NativeViewport::Create( | 90 scoped_ptr<NativeViewport> NativeViewport::Create( |
32 shell::Context* context, | 91 shell::Context* context, |
33 NativeViewportDelegate* delegate) { | 92 NativeViewportDelegate* delegate) { |
34 return scoped_ptr<NativeViewport>(new NativeViewportX11(delegate)).Pass(); | 93 return scoped_ptr<NativeViewport>(new NativeViewportX11(delegate)).Pass(); |
35 } | 94 } |
36 | 95 |
37 } // namespace services | 96 } // namespace services |
38 } // namespace mojo | 97 } // namespace mojo |
OLD | NEW |