OLD | NEW |
| (Empty) |
1 // Copyright (c) 2006-2009 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 "gpu/demos/app_framework/application.h" | |
6 | |
7 #include "gpu/command_buffer/client/gles2_implementation.h" | |
8 #include "gpu/command_buffer/client/gles2_lib.h" | |
9 #include "gpu/command_buffer/service/command_buffer_service.h" | |
10 #include "gpu/command_buffer/service/gpu_processor.h" | |
11 | |
12 using gpu::Buffer; | |
13 using gpu::CommandBufferService; | |
14 using gpu::GPUProcessor; | |
15 using gpu::gles2::GLES2CmdHelper; | |
16 using gpu::gles2::GLES2Implementation; | |
17 | |
18 // TODO(alokp): Implement it on mac and linux when gpu process is functional | |
19 // on these OS'. | |
20 #if defined(OS_WIN) | |
21 namespace { | |
22 static const int32 kCommandBufferSize = 1024 * 1024; | |
23 static const int32 kTransferBufferSize = 512 * 1024; | |
24 | |
25 static LRESULT CALLBACK WindowProc(HWND hwnd, UINT msg, | |
26 WPARAM w_param, LPARAM l_param) { | |
27 LRESULT result = 0; | |
28 switch (msg) { | |
29 case WM_CLOSE: | |
30 ::DestroyWindow(hwnd); | |
31 break; | |
32 case WM_DESTROY: | |
33 ::PostQuitMessage(0); | |
34 break; | |
35 case WM_PAINT: { | |
36 using gpu_demos::Application; | |
37 Application* app = reinterpret_cast<Application*>( | |
38 GetWindowLongPtr(hwnd, GWL_USERDATA)); | |
39 if (app != NULL) app->OnPaint(); | |
40 ::ValidateRect(hwnd, NULL); | |
41 break; | |
42 } | |
43 default: | |
44 result = ::DefWindowProc(hwnd, msg, w_param, l_param); | |
45 break; | |
46 } | |
47 return result; | |
48 } | |
49 } // namespace. | |
50 | |
51 namespace gpu_demos { | |
52 | |
53 Application::Application() | |
54 : width_(512), | |
55 height_(512), | |
56 window_handle_(NULL) { | |
57 } | |
58 | |
59 Application::~Application() { | |
60 } | |
61 | |
62 void Application::MainLoop() { | |
63 MSG msg; | |
64 bool done = false; | |
65 while (!done) { | |
66 while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) { | |
67 if (msg.message == WM_QUIT) done = true; | |
68 TranslateMessage(&msg); | |
69 DispatchMessage(&msg); | |
70 } | |
71 // Message queue is empty and application has not quit yet - keep painting. | |
72 if (!done) SendMessage(window_handle_, WM_PAINT, 0, 0); | |
73 } | |
74 } | |
75 | |
76 void Application::OnPaint() { | |
77 Draw(); | |
78 gles2::GetGLContext()->SwapBuffers(); | |
79 } | |
80 | |
81 bool Application::InitRenderContext() { | |
82 window_handle_ = CreateNativeWindow(); | |
83 if (window_handle_ == NULL) { | |
84 return false; | |
85 } | |
86 | |
87 scoped_ptr<CommandBufferService> command_buffer(new CommandBufferService); | |
88 if (!command_buffer->Initialize(kCommandBufferSize)) { | |
89 return false; | |
90 } | |
91 | |
92 scoped_refptr<GPUProcessor> gpu_processor( | |
93 new GPUProcessor(command_buffer.get())); | |
94 if (!gpu_processor->Initialize(window_handle_)) { | |
95 return false; | |
96 } | |
97 | |
98 command_buffer->SetPutOffsetChangeCallback( | |
99 NewCallback(gpu_processor.get(), &GPUProcessor::ProcessCommands)); | |
100 | |
101 GLES2CmdHelper* helper = new GLES2CmdHelper(command_buffer.get()); | |
102 if (!helper->Initialize()) { | |
103 // TODO(alokp): cleanup. | |
104 return false; | |
105 } | |
106 | |
107 int32 transfer_buffer_id = | |
108 command_buffer->CreateTransferBuffer(kTransferBufferSize); | |
109 Buffer transfer_buffer = | |
110 command_buffer->GetTransferBuffer(transfer_buffer_id); | |
111 if (transfer_buffer.ptr == NULL) return false; | |
112 | |
113 gles2::g_gl_impl = new GLES2Implementation(helper, | |
114 transfer_buffer.size, | |
115 transfer_buffer.ptr, | |
116 transfer_buffer_id); | |
117 | |
118 return command_buffer.release() != NULL; | |
119 } | |
120 | |
121 NativeWindowHandle Application::CreateNativeWindow() { | |
122 WNDCLASS wnd_class = {0}; | |
123 HINSTANCE instance = GetModuleHandle(NULL); | |
124 wnd_class.style = CS_OWNDC; | |
125 wnd_class.lpfnWndProc = (WNDPROC)WindowProc; | |
126 wnd_class.hInstance = instance; | |
127 wnd_class.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH); | |
128 wnd_class.lpszClassName = L"opengles2.0"; | |
129 if (!RegisterClass(&wnd_class)) return NULL; | |
130 | |
131 DWORD wnd_style = WS_VISIBLE | WS_POPUP | WS_BORDER | WS_SYSMENU | WS_CAPTION; | |
132 RECT wnd_rect; | |
133 wnd_rect.left = 0; | |
134 wnd_rect.top = 0; | |
135 wnd_rect.right = width_; | |
136 wnd_rect.bottom = height_; | |
137 AdjustWindowRect(&wnd_rect, wnd_style, FALSE); | |
138 | |
139 HWND hwnd = CreateWindow( | |
140 wnd_class.lpszClassName, | |
141 wnd_class.lpszClassName, | |
142 wnd_style, | |
143 0, | |
144 0, | |
145 wnd_rect.right - wnd_rect.left, | |
146 wnd_rect.bottom - wnd_rect.top, | |
147 NULL, | |
148 NULL, | |
149 instance, | |
150 NULL); | |
151 if (hwnd == NULL) return NULL; | |
152 | |
153 ShowWindow(hwnd, SW_SHOWNORMAL); | |
154 // Set this to the GWL_USERDATA so that it is available to WindowProc. | |
155 SetWindowLongPtr(hwnd, GWL_USERDATA, (LONG_PTR)this); | |
156 | |
157 return hwnd; | |
158 } | |
159 | |
160 } // namespace gpu_demos | |
161 #endif // defined(OS_WIN) | |
OLD | NEW |