OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "media/tools/shader_bench/window.h" |
| 6 |
| 7 #include "base/task.h" |
| 8 #include "media/tools/shader_bench/painter.h" |
| 9 |
| 10 namespace { |
| 11 |
| 12 LRESULT CALLBACK WindowProc(HWND hwnd, UINT msg, |
| 13 WPARAM w_param, LPARAM l_param) { |
| 14 LRESULT result = 0; |
| 15 switch (msg) { |
| 16 case WM_CLOSE: |
| 17 ::DestroyWindow(hwnd); |
| 18 break; |
| 19 case WM_DESTROY: |
| 20 ::PostQuitMessage(0); |
| 21 break; |
| 22 case WM_ERASEBKGND: |
| 23 // Return a non-zero value to indicate that the background has been |
| 24 // erased. |
| 25 result = 1; |
| 26 break; |
| 27 case WM_PAINT: { |
| 28 media::Window* window = |
| 29 reinterpret_cast<media::Window*>( |
| 30 GetWindowLongPtr(hwnd, GWL_USERDATA)); |
| 31 if (window != NULL) |
| 32 window->OnPaint(); |
| 33 ::ValidateRect(hwnd, NULL); |
| 34 break; |
| 35 } |
| 36 default: |
| 37 result = ::DefWindowProc(hwnd, msg, w_param, l_param); |
| 38 break; |
| 39 } |
| 40 return result; |
| 41 } |
| 42 |
| 43 } // namespace |
| 44 |
| 45 namespace media { |
| 46 |
| 47 gfx::NativeWindow Window::CreateNativeWindow(int width, int height) { |
| 48 WNDCLASS wnd_class = {0}; |
| 49 HINSTANCE instance = GetModuleHandle(NULL); |
| 50 wnd_class.style = CS_OWNDC; |
| 51 wnd_class.lpfnWndProc = WindowProc; |
| 52 wnd_class.hInstance = instance; |
| 53 wnd_class.hbrBackground = |
| 54 reinterpret_cast<HBRUSH>(GetStockObject(BLACK_BRUSH)); |
| 55 wnd_class.lpszClassName = L"gpu_demo"; |
| 56 if (!RegisterClass(&wnd_class)) |
| 57 return NULL; |
| 58 |
| 59 DWORD wnd_style = WS_OVERLAPPED | WS_SYSMENU; |
| 60 RECT wnd_rect; |
| 61 wnd_rect.left = 0; |
| 62 wnd_rect.top = 0; |
| 63 wnd_rect.right = width; |
| 64 wnd_rect.bottom = height; |
| 65 AdjustWindowRect(&wnd_rect, wnd_style, FALSE); |
| 66 |
| 67 HWND hwnd = CreateWindow( |
| 68 wnd_class.lpszClassName, |
| 69 L"", |
| 70 wnd_style, |
| 71 0, |
| 72 0, |
| 73 wnd_rect.right - wnd_rect.left, |
| 74 wnd_rect.bottom - wnd_rect.top, |
| 75 NULL, |
| 76 NULL, |
| 77 instance, |
| 78 NULL); |
| 79 if (hwnd == NULL) |
| 80 return NULL; |
| 81 |
| 82 return hwnd; |
| 83 } |
| 84 |
| 85 gfx::PluginWindowHandle Window::PluginWindow() { |
| 86 return window_handle_; |
| 87 } |
| 88 |
| 89 void Window::Start(int limit, Task* done_task, Painter* painter) { |
| 90 running_ = true; |
| 91 count_ = 0; |
| 92 limit_ = limit; |
| 93 done_task_ = done_task; |
| 94 painter_ = painter; |
| 95 |
| 96 SetWindowLongPtr(window_handle_, GWL_USERDATA, |
| 97 reinterpret_cast<LONG_PTR>(this)); |
| 98 |
| 99 ShowWindow(window_handle_, SW_SHOWNORMAL); |
| 100 |
| 101 // Post first invalidate call to kick off painting. |
| 102 ::InvalidateRect(window_handle_, NULL, FALSE); |
| 103 |
| 104 MainLoop(); |
| 105 } |
| 106 |
| 107 void Window::OnPaint() { |
| 108 if (!running_) |
| 109 return; |
| 110 |
| 111 if (count_ < limit_) { |
| 112 painter_->OnPaint(); |
| 113 count_++; |
| 114 } else { |
| 115 running_ = false; |
| 116 if (done_task_) { |
| 117 ShowWindow(window_handle_, SW_HIDE); |
| 118 done_task_->Run(); |
| 119 delete done_task_; |
| 120 } |
| 121 } |
| 122 } |
| 123 |
| 124 void Window::MainLoop() { |
| 125 MSG msg; |
| 126 bool done = false; |
| 127 while (!done) { |
| 128 while (::PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) { |
| 129 if (msg.message == WM_QUIT || !running_) |
| 130 done = true; |
| 131 ::TranslateMessage(&msg); |
| 132 ::DispatchMessage(&msg); |
| 133 if (!done) |
| 134 ::InvalidateRect(window_handle_, NULL, FALSE); |
| 135 } |
| 136 } |
| 137 } |
| 138 |
| 139 } // namespace media |
OLD | NEW |