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 |
| 7 #include "base/bind.h" |
| 8 #include "gpu/command_buffer/client/gl_in_process_context.h" |
| 9 #include "gpu/command_buffer/client/gles2_implementation.h" |
6 #include "ui/events/event.h" | 10 #include "ui/events/event.h" |
7 #include "ui/gfx/win/window_impl.h" | 11 #include "ui/gfx/win/window_impl.h" |
8 | 12 |
9 namespace mojo { | 13 namespace mojo { |
10 namespace services { | 14 namespace services { |
11 | 15 |
12 class NativeViewportWin : public gfx::WindowImpl, | 16 class NativeViewportWin : public gfx::WindowImpl, |
13 public NativeViewport { | 17 public NativeViewport { |
14 public: | 18 public: |
15 explicit NativeViewportWin(NativeViewportDelegate* delegate) | 19 explicit NativeViewportWin(NativeViewportDelegate* delegate) |
16 : delegate_(delegate) { | 20 : delegate_(delegate) { |
17 Init(NULL, gfx::Rect(10, 10, 500, 500)); | 21 Init(NULL, gfx::Rect(10, 10, 500, 500)); |
18 ShowWindow(hwnd(), SW_SHOWNORMAL); | 22 ShowWindow(hwnd(), SW_SHOWNORMAL); |
19 SetWindowText(hwnd(), L"native_viewport::NativeViewportWin!"); | 23 SetWindowText(hwnd(), L"native_viewport::NativeViewportWin!"); |
20 } | 24 } |
21 virtual ~NativeViewportWin() { | 25 virtual ~NativeViewportWin() { |
22 if (IsWindow(hwnd())) | 26 if (IsWindow(hwnd())) |
23 DestroyWindow(hwnd()); | 27 DestroyWindow(hwnd()); |
24 } | 28 } |
25 | 29 |
26 private: | 30 private: |
27 // Overridden from NativeViewport: | 31 // Overridden from NativeViewport: |
28 virtual void Close() OVERRIDE { | 32 virtual void Close() OVERRIDE { |
29 DestroyWindow(hwnd()); | 33 DestroyWindow(hwnd()); |
30 } | 34 } |
31 | 35 |
32 BEGIN_MSG_MAP_EX(NativeViewportWin) | 36 BEGIN_MSG_MAP_EX(NativeViewportWin) |
33 MESSAGE_RANGE_HANDLER_EX(WM_MOUSEFIRST, WM_MOUSELAST, OnMouseRange) | 37 MESSAGE_RANGE_HANDLER_EX(WM_MOUSEFIRST, WM_MOUSELAST, OnMouseRange) |
34 | 38 |
| 39 MSG_WM_CREATE(OnCreate) |
35 MSG_WM_PAINT(OnPaint) | 40 MSG_WM_PAINT(OnPaint) |
36 MSG_WM_SIZE(OnSize) | 41 MSG_WM_SIZE(OnSize) |
37 MSG_WM_DESTROY(OnDestroy) | 42 MSG_WM_DESTROY(OnDestroy) |
38 END_MSG_MAP() | 43 END_MSG_MAP() |
39 | 44 |
40 LRESULT OnMouseRange(UINT message, WPARAM w_param, LPARAM l_param) { | 45 LRESULT OnMouseRange(UINT message, WPARAM w_param, LPARAM l_param) { |
41 MSG msg = { hwnd(), message, w_param, l_param, 0, | 46 MSG msg = { hwnd(), message, w_param, l_param, 0, |
42 { GET_X_LPARAM(l_param), GET_Y_LPARAM(l_param) } }; | 47 { GET_X_LPARAM(l_param), GET_Y_LPARAM(l_param) } }; |
43 ui::MouseEvent event(msg); | 48 ui::MouseEvent event(msg); |
44 bool handled = delegate_->OnEvent(&event); | 49 bool handled = delegate_->OnEvent(&event); |
45 SetMsgHandled(handled); | 50 SetMsgHandled(handled); |
46 return 0; | 51 return 0; |
47 } | 52 } |
| 53 LRESULT OnCreate(CREATESTRUCT* create_struct) { |
| 54 RECT cr; |
| 55 GetClientRect(hwnd(), &cr); |
| 56 gpu::GLInProcessContextAttribs attribs; |
| 57 gl_context_.reset(gpu::GLInProcessContext::CreateContext( |
| 58 false, hwnd(), gfx::Size(cr.right - cr.left, cr.bottom - cr.top), |
| 59 false, attribs, gfx::PreferDiscreteGpu)); |
| 60 gl_context_->SetContextLostCallback(base::Bind( |
| 61 &NativeViewportWin::OnGLContextLost, base::Unretained(this))); |
| 62 |
| 63 delegate_->OnGLContextAvailable(gl_context_->GetImplementation()); |
| 64 return 0; |
| 65 } |
48 void OnPaint(HDC) { | 66 void OnPaint(HDC) { |
49 RECT cr; | 67 RECT cr; |
50 GetClientRect(hwnd(), &cr); | 68 GetClientRect(hwnd(), &cr); |
51 | 69 |
52 PAINTSTRUCT ps; | 70 PAINTSTRUCT ps; |
53 HDC dc = BeginPaint(hwnd(), &ps); | 71 HDC dc = BeginPaint(hwnd(), &ps); |
54 HBRUSH red_brush = CreateSolidBrush(RGB(255, 0, 0)); | 72 HBRUSH red_brush = CreateSolidBrush(RGB(255, 0, 0)); |
55 HGDIOBJ old_object = SelectObject(dc, red_brush); | 73 HGDIOBJ old_object = SelectObject(dc, red_brush); |
56 Rectangle(dc, cr.left, cr.top, cr.right, cr.bottom); | 74 Rectangle(dc, cr.left, cr.top, cr.right, cr.bottom); |
57 SelectObject(dc, old_object); | 75 SelectObject(dc, old_object); |
58 DeleteObject(red_brush); | 76 DeleteObject(red_brush); |
59 EndPaint(hwnd(), &ps); | 77 EndPaint(hwnd(), &ps); |
60 } | 78 } |
61 void OnSize(UINT param, const CSize& size) { | 79 void OnSize(UINT param, const CSize& size) { |
62 delegate_->OnResized(gfx::Size(size.cx, size.cy)); | 80 delegate_->OnResized(gfx::Size(size.cx, size.cy)); |
63 } | 81 } |
64 void OnDestroy() { | 82 void OnDestroy() { |
65 delegate_->OnDestroyed(); | 83 delegate_->OnDestroyed(); |
66 } | 84 } |
67 | 85 |
| 86 void OnGLContextLost() { |
| 87 gl_context_.reset(); |
| 88 delegate_->OnGLContextLost(); |
| 89 } |
| 90 |
68 NativeViewportDelegate* delegate_; | 91 NativeViewportDelegate* delegate_; |
| 92 scoped_ptr<gpu::GLInProcessContext> gl_context_; |
69 | 93 |
70 DISALLOW_COPY_AND_ASSIGN(NativeViewportWin); | 94 DISALLOW_COPY_AND_ASSIGN(NativeViewportWin); |
71 }; | 95 }; |
72 | 96 |
73 // static | 97 // static |
74 scoped_ptr<NativeViewport> NativeViewport::Create( | 98 scoped_ptr<NativeViewport> NativeViewport::Create( |
75 shell::Context* context, | 99 shell::Context* context, |
76 NativeViewportDelegate* delegate) { | 100 NativeViewportDelegate* delegate) { |
77 return scoped_ptr<NativeViewport>(new NativeViewportWin(delegate)).Pass(); | 101 return scoped_ptr<NativeViewport>(new NativeViewportWin(delegate)).Pass(); |
78 } | 102 } |
79 | 103 |
80 } // namespace services | 104 } // namespace services |
81 } // namespace mojo | 105 } // namespace mojo |
OLD | NEW |