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