daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 1 | // |
daniel@transgaming.com | 3c72078 | 2012-10-31 18:42:34 +0000 | [diff] [blame] | 2 | // Copyright (c) 2002-2012 The ANGLE Project Authors. All rights reserved. |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 3 | // Use of this source code is governed by a BSD-style license that can be |
| 4 | // found in the LICENSE file. |
| 5 | // |
| 6 | |
| 7 | // Surface.cpp: Implements the egl::Surface class, representing a drawing surface |
| 8 | // such as the client area of a window, including any back buffers. |
| 9 | // Implements EGLSurface and related functionality. [EGL 1.4] section 2.2 page 3. |
| 10 | |
| 11 | #include <tchar.h> |
| 12 | |
| 13 | #include "libEGL/Surface.h" |
| 14 | |
| 15 | #include "common/debug.h" |
| 16 | #include "libGLESv2/Texture.h" |
daniel@transgaming.com | 3c72078 | 2012-10-31 18:42:34 +0000 | [diff] [blame] | 17 | #include "libGLESv2/renderer/SwapChain.h" |
daniel@transgaming.com | cfa8efd | 2012-11-28 19:30:55 +0000 | [diff] [blame] | 18 | #include "libGLESv2/main.h" |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 19 | |
| 20 | #include "libEGL/main.h" |
| 21 | #include "libEGL/Display.h" |
| 22 | |
| 23 | #include <dwmapi.h> |
| 24 | |
| 25 | namespace egl |
| 26 | { |
| 27 | |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 28 | Surface::Surface(Display *display, const Config *config, HWND window, EGLint postSubBufferSupported) |
| 29 | : mDisplay(display), mConfig(config), mWindow(window), mPostSubBufferSupported(postSubBufferSupported) |
| 30 | { |
daniel@transgaming.com | b9bb279 | 2012-11-28 19:36:49 +0000 | [diff] [blame^] | 31 | mRenderer = mDisplay->getRenderer(); |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 32 | mSwapChain = NULL; |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 33 | mShareHandle = NULL; |
| 34 | mTexture = NULL; |
| 35 | mTextureFormat = EGL_NO_TEXTURE; |
| 36 | mTextureTarget = EGL_NO_TEXTURE; |
| 37 | |
| 38 | mPixelAspectRatio = (EGLint)(1.0 * EGL_DISPLAY_SCALING); // FIXME: Determine actual pixel aspect ratio |
| 39 | mRenderBuffer = EGL_BACK_BUFFER; |
| 40 | mSwapBehavior = EGL_BUFFER_PRESERVED; |
| 41 | mSwapInterval = -1; |
daniel@transgaming.com | 3c72078 | 2012-10-31 18:42:34 +0000 | [diff] [blame] | 42 | mWidth = -1; |
| 43 | mHeight = -1; |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 44 | setSwapInterval(1); |
| 45 | |
| 46 | subclassWindow(); |
| 47 | } |
| 48 | |
| 49 | Surface::Surface(Display *display, const Config *config, HANDLE shareHandle, EGLint width, EGLint height, EGLenum textureFormat, EGLenum textureType) |
| 50 | : mDisplay(display), mWindow(NULL), mConfig(config), mShareHandle(shareHandle), mWidth(width), mHeight(height), mPostSubBufferSupported(EGL_FALSE) |
| 51 | { |
daniel@transgaming.com | bdfb391 | 2012-10-31 19:55:21 +0000 | [diff] [blame] | 52 | mRenderer = mDisplay->getRenderer9(); |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 53 | mSwapChain = NULL; |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 54 | mWindowSubclassed = false; |
| 55 | mTexture = NULL; |
| 56 | mTextureFormat = textureFormat; |
| 57 | mTextureTarget = textureType; |
| 58 | |
| 59 | mPixelAspectRatio = (EGLint)(1.0 * EGL_DISPLAY_SCALING); // FIXME: Determine actual pixel aspect ratio |
| 60 | mRenderBuffer = EGL_BACK_BUFFER; |
| 61 | mSwapBehavior = EGL_BUFFER_PRESERVED; |
| 62 | mSwapInterval = -1; |
| 63 | setSwapInterval(1); |
| 64 | } |
| 65 | |
| 66 | Surface::~Surface() |
| 67 | { |
| 68 | unsubclassWindow(); |
| 69 | release(); |
| 70 | } |
| 71 | |
| 72 | bool Surface::initialize() |
| 73 | { |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 74 | if (!resetSwapChain()) |
| 75 | return false; |
| 76 | |
| 77 | // Modify present parameters for this window, if we are composited, |
| 78 | // to minimize the amount of queuing done by DWM between our calls to |
| 79 | // present and the actual screen. |
| 80 | if (mWindow && (getComparableOSVersion() >= versionWindowsVista)) { |
| 81 | BOOL isComposited; |
| 82 | HRESULT result = DwmIsCompositionEnabled(&isComposited); |
| 83 | if (SUCCEEDED(result) && isComposited) { |
| 84 | DWM_PRESENT_PARAMETERS presentParams; |
| 85 | memset(&presentParams, 0, sizeof(presentParams)); |
| 86 | presentParams.cbSize = sizeof(DWM_PRESENT_PARAMETERS); |
| 87 | presentParams.cBuffer = 2; |
| 88 | |
| 89 | result = DwmSetPresentParameters(mWindow, &presentParams); |
| 90 | if (FAILED(result)) |
| 91 | ERR("Unable to set present parameters: 0x%08X", result); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | return true; |
| 96 | } |
| 97 | |
| 98 | void Surface::release() |
| 99 | { |
daniel@transgaming.com | b9bb279 | 2012-11-28 19:36:49 +0000 | [diff] [blame^] | 100 | delete mSwapChain; |
daniel@transgaming.com | 3c72078 | 2012-10-31 18:42:34 +0000 | [diff] [blame] | 101 | mSwapChain = NULL; |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 102 | |
| 103 | if (mTexture) |
| 104 | { |
| 105 | mTexture->releaseTexImage(); |
| 106 | mTexture = NULL; |
| 107 | } |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | bool Surface::resetSwapChain() |
| 111 | { |
daniel@transgaming.com | 3c72078 | 2012-10-31 18:42:34 +0000 | [diff] [blame] | 112 | ASSERT(!mSwapChain); |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 113 | |
daniel@transgaming.com | 3c72078 | 2012-10-31 18:42:34 +0000 | [diff] [blame] | 114 | int width; |
| 115 | int height; |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 116 | |
| 117 | if (mWindow) |
| 118 | { |
daniel@transgaming.com | 3c72078 | 2012-10-31 18:42:34 +0000 | [diff] [blame] | 119 | RECT windowRect; |
| 120 | if (!GetClientRect(getWindowHandle(), &windowRect)) |
apatrick@chromium.org | 85e4419 | 2012-08-17 20:58:01 +0000 | [diff] [blame] | 121 | { |
daniel@transgaming.com | 3c72078 | 2012-10-31 18:42:34 +0000 | [diff] [blame] | 122 | ASSERT(false); |
| 123 | |
| 124 | ERR("Could not retrieve the window dimensions"); |
| 125 | return error(EGL_BAD_SURFACE, false); |
apatrick@chromium.org | 85e4419 | 2012-08-17 20:58:01 +0000 | [diff] [blame] | 126 | } |
apatrick@chromium.org | 0c71fd4 | 2012-08-10 18:08:47 +0000 | [diff] [blame] | 127 | |
daniel@transgaming.com | 3c72078 | 2012-10-31 18:42:34 +0000 | [diff] [blame] | 128 | width = windowRect.right - windowRect.left; |
| 129 | height = windowRect.bottom - windowRect.top; |
| 130 | } |
| 131 | else |
| 132 | { |
| 133 | // non-window surface - size is determined at creation |
| 134 | width = mWidth; |
| 135 | height = mHeight; |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 136 | } |
| 137 | |
daniel@transgaming.com | b9bb279 | 2012-11-28 19:36:49 +0000 | [diff] [blame^] | 138 | mSwapChain = mRenderer->createSwapChain(mWindow, mShareHandle, |
| 139 | mConfig->mRenderTargetFormat, |
| 140 | mConfig->mDepthStencilFormat); |
daniel@transgaming.com | 3c72078 | 2012-10-31 18:42:34 +0000 | [diff] [blame] | 141 | if (!mSwapChain) |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 142 | { |
daniel@transgaming.com | 3c72078 | 2012-10-31 18:42:34 +0000 | [diff] [blame] | 143 | return error(EGL_BAD_ALLOC, false); |
| 144 | } |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 145 | |
daniel@transgaming.com | 3c72078 | 2012-10-31 18:42:34 +0000 | [diff] [blame] | 146 | if (!resetSwapChain(width, height)) |
| 147 | { |
daniel@transgaming.com | b9bb279 | 2012-11-28 19:36:49 +0000 | [diff] [blame^] | 148 | delete mSwapChain; |
daniel@transgaming.com | 3c72078 | 2012-10-31 18:42:34 +0000 | [diff] [blame] | 149 | mSwapChain = NULL; |
daniel@transgaming.com | 2a99bfa | 2012-10-31 19:55:50 +0000 | [diff] [blame] | 150 | return false; |
daniel@transgaming.com | 3c72078 | 2012-10-31 18:42:34 +0000 | [diff] [blame] | 151 | } |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 152 | |
daniel@transgaming.com | 3c72078 | 2012-10-31 18:42:34 +0000 | [diff] [blame] | 153 | return true; |
| 154 | } |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 155 | |
daniel@transgaming.com | 3c72078 | 2012-10-31 18:42:34 +0000 | [diff] [blame] | 156 | bool Surface::resetSwapChain(int backbufferWidth, int backbufferHeight) |
| 157 | { |
| 158 | ASSERT(backbufferWidth >= 0 && backbufferHeight >= 0); |
| 159 | ASSERT(mSwapChain); |
| 160 | |
| 161 | EGLint status = mSwapChain->reset(backbufferWidth, backbufferHeight, mSwapInterval); |
| 162 | |
| 163 | if (status == EGL_CONTEXT_LOST) |
| 164 | { |
| 165 | mDisplay->notifyDeviceLost(); |
| 166 | return false; |
| 167 | } |
| 168 | else if (status != EGL_SUCCESS) |
| 169 | { |
| 170 | return error(status, false); |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 171 | } |
| 172 | |
apatrick@chromium.org | 0c71fd4 | 2012-08-10 18:08:47 +0000 | [diff] [blame] | 173 | mWidth = backbufferWidth; |
| 174 | mHeight = backbufferHeight; |
daniel@transgaming.com | 3c72078 | 2012-10-31 18:42:34 +0000 | [diff] [blame] | 175 | mSwapIntervalDirty = false; |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 176 | |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 177 | return true; |
| 178 | } |
| 179 | |
| 180 | bool Surface::swapRect(EGLint x, EGLint y, EGLint width, EGLint height) |
| 181 | { |
| 182 | if (!mSwapChain) |
| 183 | { |
| 184 | return true; |
| 185 | } |
| 186 | |
| 187 | if (x + width > mWidth) |
| 188 | { |
| 189 | width = mWidth - x; |
| 190 | } |
| 191 | |
| 192 | if (y + height > mHeight) |
| 193 | { |
| 194 | height = mHeight - y; |
| 195 | } |
| 196 | |
| 197 | if (width == 0 || height == 0) |
| 198 | { |
| 199 | return true; |
| 200 | } |
| 201 | |
daniel@transgaming.com | 3c72078 | 2012-10-31 18:42:34 +0000 | [diff] [blame] | 202 | EGLint status = mSwapChain->swapRect(x, y, width, height); |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 203 | |
daniel@transgaming.com | 3c72078 | 2012-10-31 18:42:34 +0000 | [diff] [blame] | 204 | if (status == EGL_CONTEXT_LOST) |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 205 | { |
| 206 | mDisplay->notifyDeviceLost(); |
| 207 | return false; |
| 208 | } |
daniel@transgaming.com | 3c72078 | 2012-10-31 18:42:34 +0000 | [diff] [blame] | 209 | else if (status != EGL_SUCCESS) |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 210 | { |
daniel@transgaming.com | 3c72078 | 2012-10-31 18:42:34 +0000 | [diff] [blame] | 211 | return error(status, false); |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 212 | } |
| 213 | |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 214 | checkForOutOfDateSwapChain(); |
| 215 | |
| 216 | return true; |
| 217 | } |
| 218 | |
| 219 | HWND Surface::getWindowHandle() |
| 220 | { |
| 221 | return mWindow; |
| 222 | } |
| 223 | |
| 224 | |
| 225 | #define kSurfaceProperty _TEXT("Egl::SurfaceOwner") |
| 226 | #define kParentWndProc _TEXT("Egl::SurfaceParentWndProc") |
| 227 | |
| 228 | static LRESULT CALLBACK SurfaceWindowProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam) |
| 229 | { |
| 230 | if (message == WM_SIZE) |
| 231 | { |
| 232 | Surface* surf = reinterpret_cast<Surface*>(GetProp(hwnd, kSurfaceProperty)); |
| 233 | if(surf) |
| 234 | { |
| 235 | surf->checkForOutOfDateSwapChain(); |
| 236 | } |
| 237 | } |
| 238 | WNDPROC prevWndFunc = reinterpret_cast<WNDPROC >(GetProp(hwnd, kParentWndProc)); |
| 239 | return CallWindowProc(prevWndFunc, hwnd, message, wparam, lparam); |
| 240 | } |
| 241 | |
| 242 | void Surface::subclassWindow() |
| 243 | { |
| 244 | if (!mWindow) |
| 245 | { |
| 246 | return; |
| 247 | } |
| 248 | |
| 249 | DWORD processId; |
| 250 | DWORD threadId = GetWindowThreadProcessId(mWindow, &processId); |
| 251 | if (processId != GetCurrentProcessId() || threadId != GetCurrentThreadId()) |
| 252 | { |
| 253 | return; |
| 254 | } |
| 255 | |
| 256 | SetLastError(0); |
| 257 | LONG_PTR oldWndProc = SetWindowLongPtr(mWindow, GWLP_WNDPROC, reinterpret_cast<LONG_PTR>(SurfaceWindowProc)); |
| 258 | if(oldWndProc == 0 && GetLastError() != ERROR_SUCCESS) |
| 259 | { |
| 260 | mWindowSubclassed = false; |
| 261 | return; |
| 262 | } |
| 263 | |
| 264 | SetProp(mWindow, kSurfaceProperty, reinterpret_cast<HANDLE>(this)); |
| 265 | SetProp(mWindow, kParentWndProc, reinterpret_cast<HANDLE>(oldWndProc)); |
| 266 | mWindowSubclassed = true; |
| 267 | } |
| 268 | |
| 269 | void Surface::unsubclassWindow() |
| 270 | { |
| 271 | if(!mWindowSubclassed) |
| 272 | { |
| 273 | return; |
| 274 | } |
| 275 | |
| 276 | // un-subclass |
| 277 | LONG_PTR parentWndFunc = reinterpret_cast<LONG_PTR>(GetProp(mWindow, kParentWndProc)); |
| 278 | |
| 279 | // Check the windowproc is still SurfaceWindowProc. |
| 280 | // If this assert fails, then it is likely the application has subclassed the |
| 281 | // hwnd as well and did not unsubclass before destroying its EGL context. The |
| 282 | // application should be modified to either subclass before initializing the |
| 283 | // EGL context, or to unsubclass before destroying the EGL context. |
| 284 | if(parentWndFunc) |
| 285 | { |
| 286 | LONG_PTR prevWndFunc = SetWindowLongPtr(mWindow, GWLP_WNDPROC, parentWndFunc); |
| 287 | ASSERT(prevWndFunc == reinterpret_cast<LONG_PTR>(SurfaceWindowProc)); |
| 288 | } |
| 289 | |
| 290 | RemoveProp(mWindow, kSurfaceProperty); |
| 291 | RemoveProp(mWindow, kParentWndProc); |
| 292 | mWindowSubclassed = false; |
| 293 | } |
| 294 | |
| 295 | bool Surface::checkForOutOfDateSwapChain() |
| 296 | { |
| 297 | RECT client; |
| 298 | if (!GetClientRect(getWindowHandle(), &client)) |
| 299 | { |
| 300 | ASSERT(false); |
| 301 | return false; |
| 302 | } |
| 303 | |
| 304 | // Grow the buffer now, if the window has grown. We need to grow now to avoid losing information. |
| 305 | int clientWidth = client.right - client.left; |
| 306 | int clientHeight = client.bottom - client.top; |
| 307 | bool sizeDirty = clientWidth != getWidth() || clientHeight != getHeight(); |
| 308 | |
daniel@transgaming.com | 3c72078 | 2012-10-31 18:42:34 +0000 | [diff] [blame] | 309 | if (sizeDirty || mSwapIntervalDirty) |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 310 | { |
| 311 | resetSwapChain(clientWidth, clientHeight); |
| 312 | if (static_cast<egl::Surface*>(getCurrentDrawSurface()) == this) |
| 313 | { |
| 314 | glMakeCurrent(glGetCurrentContext(), static_cast<egl::Display*>(getCurrentDisplay()), this); |
| 315 | } |
| 316 | |
| 317 | return true; |
| 318 | } |
| 319 | return false; |
| 320 | } |
| 321 | |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 322 | bool Surface::swap() |
| 323 | { |
| 324 | return swapRect(0, 0, mWidth, mHeight); |
| 325 | } |
| 326 | |
| 327 | bool Surface::postSubBuffer(EGLint x, EGLint y, EGLint width, EGLint height) |
| 328 | { |
| 329 | if (!mPostSubBufferSupported) |
| 330 | { |
| 331 | // Spec is not clear about how this should be handled. |
| 332 | return true; |
| 333 | } |
| 334 | |
| 335 | return swapRect(x, y, width, height); |
| 336 | } |
| 337 | |
| 338 | EGLint Surface::getWidth() const |
| 339 | { |
| 340 | return mWidth; |
| 341 | } |
| 342 | |
| 343 | EGLint Surface::getHeight() const |
| 344 | { |
| 345 | return mHeight; |
| 346 | } |
| 347 | |
| 348 | EGLint Surface::isPostSubBufferSupported() const |
| 349 | { |
| 350 | return mPostSubBufferSupported; |
| 351 | } |
| 352 | |
daniel@transgaming.com | 76d3e6e | 2012-10-31 19:55:33 +0000 | [diff] [blame] | 353 | rx::SwapChain *Surface::getSwapChain() const |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 354 | { |
daniel@transgaming.com | 3c72078 | 2012-10-31 18:42:34 +0000 | [diff] [blame] | 355 | return mSwapChain; |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | void Surface::setSwapInterval(EGLint interval) |
| 359 | { |
| 360 | if (mSwapInterval == interval) |
| 361 | { |
| 362 | return; |
| 363 | } |
| 364 | |
| 365 | mSwapInterval = interval; |
daniel@transgaming.com | 114bd46 | 2012-10-31 18:42:47 +0000 | [diff] [blame] | 366 | mSwapInterval = std::max(mSwapInterval, mRenderer->getMinSwapInterval()); |
| 367 | mSwapInterval = std::min(mSwapInterval, mRenderer->getMaxSwapInterval()); |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 368 | |
daniel@transgaming.com | 3c72078 | 2012-10-31 18:42:34 +0000 | [diff] [blame] | 369 | mSwapIntervalDirty = true; |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | EGLenum Surface::getTextureFormat() const |
| 373 | { |
| 374 | return mTextureFormat; |
| 375 | } |
| 376 | |
| 377 | EGLenum Surface::getTextureTarget() const |
| 378 | { |
| 379 | return mTextureTarget; |
| 380 | } |
| 381 | |
| 382 | void Surface::setBoundTexture(gl::Texture2D *texture) |
| 383 | { |
| 384 | mTexture = texture; |
| 385 | } |
| 386 | |
| 387 | gl::Texture2D *Surface::getBoundTexture() const |
| 388 | { |
| 389 | return mTexture; |
| 390 | } |
| 391 | |
daniel@transgaming.com | 106e1f7 | 2012-10-31 18:38:36 +0000 | [diff] [blame] | 392 | EGLenum Surface::getFormat() const |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 393 | { |
| 394 | return mConfig->mRenderTargetFormat; |
| 395 | } |
| 396 | } |