Index: gpu/demos/framework/window.cc |
=================================================================== |
--- gpu/demos/framework/window.cc (revision 0) |
+++ gpu/demos/framework/window.cc (working copy) |
@@ -1,13 +1,14 @@ |
-// Copyright (c) 2006-2009 The Chromium Authors. All rights reserved. |
+// Copyright (c) 2010 The Chromium Authors. All rights reserved. |
// Use of this source code is governed by a BSD-style license that can be |
// found in the LICENSE file. |
-#include "gpu/demos/app_framework/application.h" |
+#include "gpu/demos/framework/window.h" |
#include "gpu/command_buffer/client/gles2_implementation.h" |
#include "gpu/command_buffer/client/gles2_lib.h" |
#include "gpu/command_buffer/service/command_buffer_service.h" |
#include "gpu/command_buffer/service/gpu_processor.h" |
+#include "gpu/demos/framework/demo_factory.h" |
using gpu::Buffer; |
using gpu::CommandBufferService; |
@@ -15,15 +16,13 @@ |
using gpu::gles2::GLES2CmdHelper; |
using gpu::gles2::GLES2Implementation; |
-// TODO(alokp): Implement it on mac and linux when gpu process is functional |
-// on these OS'. |
-#if defined(OS_WIN) |
+// TODO(alokp): Make this class cross-platform. Investigate using SDL. |
namespace { |
-static const int32 kCommandBufferSize = 1024 * 1024; |
-static const int32 kTransferBufferSize = 512 * 1024; |
+const int32 kCommandBufferSize = 1024 * 1024; |
+const int32 kTransferBufferSize = 512 * 1024; |
-static LRESULT CALLBACK WindowProc(HWND hwnd, UINT msg, |
- WPARAM w_param, LPARAM l_param) { |
+LRESULT CALLBACK WindowProc(HWND hwnd, UINT msg, |
+ WPARAM w_param, LPARAM l_param) { |
LRESULT result = 0; |
switch (msg) { |
case WM_CLOSE: |
@@ -32,11 +31,12 @@ |
case WM_DESTROY: |
::PostQuitMessage(0); |
break; |
+ case WM_ERASEBKGND: |
+ break; |
case WM_PAINT: { |
- using gpu_demos::Application; |
- Application* app = reinterpret_cast<Application*>( |
+ gpu::demos::Window* window = reinterpret_cast<gpu::demos::Window*>( |
GetWindowLongPtr(hwnd, GWL_USERDATA)); |
- if (app != NULL) app->OnPaint(); |
+ if (window != NULL) window->OnPaint(); |
::ValidateRect(hwnd, NULL); |
break; |
} |
@@ -46,51 +46,50 @@ |
} |
return result; |
} |
-} // namespace. |
-namespace gpu_demos { |
+HWND CreateNativeWindow(const wchar_t* title, int width, int height, |
+ LONG_PTR user_data) { |
+ WNDCLASS wnd_class = {0}; |
+ HINSTANCE instance = GetModuleHandle(NULL); |
+ wnd_class.style = CS_OWNDC; |
+ wnd_class.lpfnWndProc = WindowProc; |
+ wnd_class.hInstance = instance; |
+ wnd_class.hbrBackground = |
+ reinterpret_cast<HBRUSH>(GetStockObject(BLACK_BRUSH)); |
+ wnd_class.lpszClassName = L"gpu_demo"; |
+ if (!RegisterClass(&wnd_class)) return NULL; |
-Application::Application() |
- : width_(512), |
- height_(512), |
- window_handle_(NULL) { |
-} |
+ DWORD wnd_style = WS_VISIBLE | WS_POPUP | WS_BORDER | WS_SYSMENU | WS_CAPTION; |
+ RECT wnd_rect; |
+ wnd_rect.left = 0; |
+ wnd_rect.top = 0; |
+ wnd_rect.right = width; |
+ wnd_rect.bottom = height; |
+ AdjustWindowRect(&wnd_rect, wnd_style, FALSE); |
-Application::~Application() { |
-} |
+ HWND hwnd = CreateWindow( |
+ wnd_class.lpszClassName, |
+ title, |
+ wnd_style, |
+ 0, |
+ 0, |
+ wnd_rect.right - wnd_rect.left, |
+ wnd_rect.bottom - wnd_rect.top, |
+ NULL, |
+ NULL, |
+ instance, |
+ NULL); |
+ if (hwnd == NULL) return NULL; |
-void Application::MainLoop() { |
- MSG msg; |
- bool done = false; |
- while (!done) { |
- while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) { |
- if (msg.message == WM_QUIT) done = true; |
- TranslateMessage(&msg); |
- DispatchMessage(&msg); |
- } |
- // Message queue is empty and application has not quit yet - keep painting. |
- if (!done) SendMessage(window_handle_, WM_PAINT, 0, 0); |
- } |
-} |
+ ShowWindow(hwnd, SW_SHOWNORMAL); |
+ // Set this to the GWL_USERDATA so that it is available to WindowProc. |
+ SetWindowLongPtr(hwnd, GWL_USERDATA, user_data); |
-void Application::OnPaint() { |
- float elapsed_sec = 0.0f; |
- const base::Time current_time = base::Time::Now(); |
- if (!last_draw_time_.is_null()) { |
- base::TimeDelta time_delta = current_time - last_draw_time_; |
- elapsed_sec = static_cast<float>(time_delta.InSecondsF()); |
- } |
- last_draw_time_ = current_time; |
- |
- Draw(elapsed_sec); |
- gles2::GetGLContext()->SwapBuffers(); |
+ return hwnd; |
} |
-bool Application::InitRenderContext() { |
- window_handle_ = CreateNativeWindow(); |
- if (window_handle_ == NULL) { |
- return false; |
- } |
+bool InitRenderContext(HWND hwnd) { |
+ CHECK(hwnd); |
scoped_ptr<CommandBufferService> command_buffer(new CommandBufferService); |
if (!command_buffer->Initialize(kCommandBufferSize)) { |
@@ -99,7 +98,7 @@ |
scoped_refptr<GPUProcessor> gpu_processor( |
new GPUProcessor(command_buffer.get())); |
- if (!gpu_processor->Initialize(window_handle_)) { |
+ if (!gpu_processor->Initialize(hwnd)) { |
return false; |
} |
@@ -118,52 +117,53 @@ |
command_buffer->GetTransferBuffer(transfer_buffer_id); |
if (transfer_buffer.ptr == NULL) return false; |
- gles2::g_gl_impl = new GLES2Implementation(helper, |
- transfer_buffer.size, |
- transfer_buffer.ptr, |
- transfer_buffer_id); |
- |
+ gles2::SetGLContext(new GLES2Implementation(helper, |
+ transfer_buffer.size, |
+ transfer_buffer.ptr, |
+ transfer_buffer_id)); |
return command_buffer.release() != NULL; |
} |
+} // namespace. |
-NativeWindowHandle Application::CreateNativeWindow() { |
- WNDCLASS wnd_class = {0}; |
- HINSTANCE instance = GetModuleHandle(NULL); |
- wnd_class.style = CS_OWNDC; |
- wnd_class.lpfnWndProc = (WNDPROC)WindowProc; |
- wnd_class.hInstance = instance; |
- wnd_class.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH); |
- wnd_class.lpszClassName = L"opengles2.0"; |
- if (!RegisterClass(&wnd_class)) return NULL; |
+namespace gpu { |
+namespace demos { |
- DWORD wnd_style = WS_VISIBLE | WS_POPUP | WS_BORDER | WS_SYSMENU | WS_CAPTION; |
- RECT wnd_rect; |
- wnd_rect.left = 0; |
- wnd_rect.top = 0; |
- wnd_rect.right = width_; |
- wnd_rect.bottom = height_; |
- AdjustWindowRect(&wnd_rect, wnd_style, FALSE); |
+Window::Window() |
+ : window_handle_(NULL), |
+ demo_(CreateDemo()) { |
+} |
- HWND hwnd = CreateWindow( |
- wnd_class.lpszClassName, |
- wnd_class.lpszClassName, |
- wnd_style, |
- 0, |
- 0, |
- wnd_rect.right - wnd_rect.left, |
- wnd_rect.bottom - wnd_rect.top, |
- NULL, |
- NULL, |
- instance, |
- NULL); |
- if (hwnd == NULL) return NULL; |
+Window::~Window() { |
+} |
- ShowWindow(hwnd, SW_SHOWNORMAL); |
- // Set this to the GWL_USERDATA so that it is available to WindowProc. |
- SetWindowLongPtr(hwnd, GWL_USERDATA, (LONG_PTR)this); |
+bool Window::Init(int width, int height) { |
+ window_handle_ = CreateNativeWindow(demo_->Title(), width, height, |
+ reinterpret_cast<LONG_PTR>(this)); |
+ if (window_handle_ == NULL) return false; |
+ if (!InitRenderContext(window_handle_)) return false; |
- return hwnd; |
+ demo_->InitWindowSize(width, height); |
+ return demo_->InitGL(); |
} |
-} // namespace gpu_demos |
-#endif // defined(OS_WIN) |
+void Window::MainLoop() { |
+ MSG msg; |
+ bool done = false; |
+ while (!done) { |
+ while (::PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) { |
+ if (msg.message == WM_QUIT) done = true; |
+ ::TranslateMessage(&msg); |
+ ::DispatchMessage(&msg); |
+ } |
+ // Message queue is empty and application has not quit yet - keep painting. |
+ if (!done) ::UpdateWindow(window_handle_); |
+ } |
+} |
+ |
+void Window::OnPaint() { |
+ demo_->Draw(); |
+ ::gles2::GetGLContext()->SwapBuffers(); |
+} |
+ |
+} // namespace demos |
+} // namespace gpu |