daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 1 | // |
Nicolas Capens | 3501c16 | 2014-05-21 13:27:15 -0400 | [diff] [blame] | 2 | // Copyright (c) 2002-2014 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 | |
Scott Graham | 86f601c | 2013-09-17 13:28:00 -0700 | [diff] [blame] | 13 | #include <algorithm> |
| 14 | |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 15 | #include "libEGL/Surface.h" |
| 16 | |
| 17 | #include "common/debug.h" |
| 18 | #include "libGLESv2/Texture.h" |
daniel@transgaming.com | 3c72078 | 2012-10-31 18:42:34 +0000 | [diff] [blame] | 19 | #include "libGLESv2/renderer/SwapChain.h" |
daniel@transgaming.com | cfa8efd | 2012-11-28 19:30:55 +0000 | [diff] [blame] | 20 | #include "libGLESv2/main.h" |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 21 | |
| 22 | #include "libEGL/main.h" |
| 23 | #include "libEGL/Display.h" |
| 24 | |
Cooper Partin | eeb1f53 | 2014-09-23 10:25:02 -0700 | [diff] [blame] | 25 | #include "common/NativeWindow.h" |
| 26 | |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 27 | namespace egl |
| 28 | { |
| 29 | |
Cooper Partin | eeb1f53 | 2014-09-23 10:25:02 -0700 | [diff] [blame] | 30 | Surface::Surface(Display *display, const Config *config, EGLNativeWindowType window, EGLint fixedSize, EGLint width, EGLint height, EGLint postSubBufferSupported) |
| 31 | : mDisplay(display), mConfig(config), mNativeWindow(window), mPostSubBufferSupported(postSubBufferSupported) |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 32 | { |
daniel@transgaming.com | b9bb279 | 2012-11-28 19:36:49 +0000 | [diff] [blame] | 33 | mRenderer = mDisplay->getRenderer(); |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 34 | mSwapChain = NULL; |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 35 | mShareHandle = NULL; |
| 36 | mTexture = NULL; |
| 37 | mTextureFormat = EGL_NO_TEXTURE; |
| 38 | mTextureTarget = EGL_NO_TEXTURE; |
| 39 | |
| 40 | mPixelAspectRatio = (EGLint)(1.0 * EGL_DISPLAY_SCALING); // FIXME: Determine actual pixel aspect ratio |
| 41 | mRenderBuffer = EGL_BACK_BUFFER; |
| 42 | mSwapBehavior = EGL_BUFFER_PRESERVED; |
| 43 | mSwapInterval = -1; |
John Bauman | 3dc300a | 2014-01-28 15:30:35 -0800 | [diff] [blame] | 44 | mWidth = width; |
| 45 | mHeight = height; |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 46 | setSwapInterval(1); |
John Bauman | 3dc300a | 2014-01-28 15:30:35 -0800 | [diff] [blame] | 47 | mFixedSize = fixedSize; |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 48 | |
| 49 | subclassWindow(); |
| 50 | } |
| 51 | |
| 52 | Surface::Surface(Display *display, const Config *config, HANDLE shareHandle, EGLint width, EGLint height, EGLenum textureFormat, EGLenum textureType) |
Cooper Partin | eeb1f53 | 2014-09-23 10:25:02 -0700 | [diff] [blame] | 53 | : mDisplay(display), mNativeWindow(NULL), mConfig(config), mShareHandle(shareHandle), mWidth(width), mHeight(height), mPostSubBufferSupported(EGL_FALSE) |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 54 | { |
daniel@transgaming.com | 28e3692 | 2012-11-28 21:02:47 +0000 | [diff] [blame] | 55 | mRenderer = mDisplay->getRenderer(); |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 56 | mSwapChain = NULL; |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 57 | mWindowSubclassed = false; |
| 58 | mTexture = NULL; |
| 59 | mTextureFormat = textureFormat; |
| 60 | mTextureTarget = textureType; |
| 61 | |
| 62 | mPixelAspectRatio = (EGLint)(1.0 * EGL_DISPLAY_SCALING); // FIXME: Determine actual pixel aspect ratio |
| 63 | mRenderBuffer = EGL_BACK_BUFFER; |
| 64 | mSwapBehavior = EGL_BUFFER_PRESERVED; |
| 65 | mSwapInterval = -1; |
| 66 | setSwapInterval(1); |
John Bauman | 3dc300a | 2014-01-28 15:30:35 -0800 | [diff] [blame] | 67 | // This constructor is for offscreen surfaces, which are always fixed-size. |
| 68 | mFixedSize = EGL_TRUE; |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | Surface::~Surface() |
| 72 | { |
| 73 | unsubclassWindow(); |
| 74 | release(); |
| 75 | } |
| 76 | |
Geoff Lang | 6b0cf99 | 2014-10-06 10:28:07 -0400 | [diff] [blame^] | 77 | Error Surface::initialize() |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 78 | { |
Cooper Partin | eeb1f53 | 2014-09-23 10:25:02 -0700 | [diff] [blame] | 79 | if (mNativeWindow.getNativeWindow()) |
| 80 | { |
| 81 | if (!mNativeWindow.initialize()) |
| 82 | { |
Geoff Lang | 6b0cf99 | 2014-10-06 10:28:07 -0400 | [diff] [blame^] | 83 | return Error(EGL_BAD_SURFACE); |
Cooper Partin | eeb1f53 | 2014-09-23 10:25:02 -0700 | [diff] [blame] | 84 | } |
| 85 | } |
| 86 | |
Geoff Lang | 6b0cf99 | 2014-10-06 10:28:07 -0400 | [diff] [blame^] | 87 | Error error = resetSwapChain(); |
| 88 | if (error.isError()) |
| 89 | { |
| 90 | return error; |
| 91 | } |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 92 | |
Geoff Lang | 6b0cf99 | 2014-10-06 10:28:07 -0400 | [diff] [blame^] | 93 | return Error(EGL_SUCCESS); |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | void Surface::release() |
| 97 | { |
daniel@transgaming.com | b9bb279 | 2012-11-28 19:36:49 +0000 | [diff] [blame] | 98 | delete mSwapChain; |
daniel@transgaming.com | 3c72078 | 2012-10-31 18:42:34 +0000 | [diff] [blame] | 99 | mSwapChain = NULL; |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 100 | |
| 101 | if (mTexture) |
| 102 | { |
| 103 | mTexture->releaseTexImage(); |
| 104 | mTexture = NULL; |
| 105 | } |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 106 | } |
| 107 | |
Geoff Lang | 6b0cf99 | 2014-10-06 10:28:07 -0400 | [diff] [blame^] | 108 | Error Surface::resetSwapChain() |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 109 | { |
daniel@transgaming.com | 3c72078 | 2012-10-31 18:42:34 +0000 | [diff] [blame] | 110 | ASSERT(!mSwapChain); |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 111 | |
daniel@transgaming.com | 3c72078 | 2012-10-31 18:42:34 +0000 | [diff] [blame] | 112 | int width; |
| 113 | int height; |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 114 | |
John Bauman | 3dc300a | 2014-01-28 15:30:35 -0800 | [diff] [blame] | 115 | if (!mFixedSize) |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 116 | { |
daniel@transgaming.com | 3c72078 | 2012-10-31 18:42:34 +0000 | [diff] [blame] | 117 | RECT windowRect; |
Cooper Partin | eeb1f53 | 2014-09-23 10:25:02 -0700 | [diff] [blame] | 118 | if (!mNativeWindow.getClientRect(&windowRect)) |
apatrick@chromium.org | 85e4419 | 2012-08-17 20:58:01 +0000 | [diff] [blame] | 119 | { |
daniel@transgaming.com | 3c72078 | 2012-10-31 18:42:34 +0000 | [diff] [blame] | 120 | ASSERT(false); |
| 121 | |
Geoff Lang | 6b0cf99 | 2014-10-06 10:28:07 -0400 | [diff] [blame^] | 122 | return Error(EGL_BAD_SURFACE, "Could not retrieve the window dimensions"); |
apatrick@chromium.org | 85e4419 | 2012-08-17 20:58:01 +0000 | [diff] [blame] | 123 | } |
apatrick@chromium.org | 0c71fd4 | 2012-08-10 18:08:47 +0000 | [diff] [blame] | 124 | |
daniel@transgaming.com | 3c72078 | 2012-10-31 18:42:34 +0000 | [diff] [blame] | 125 | width = windowRect.right - windowRect.left; |
| 126 | height = windowRect.bottom - windowRect.top; |
| 127 | } |
| 128 | else |
| 129 | { |
| 130 | // non-window surface - size is determined at creation |
| 131 | width = mWidth; |
| 132 | height = mHeight; |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 133 | } |
| 134 | |
Cooper Partin | eeb1f53 | 2014-09-23 10:25:02 -0700 | [diff] [blame] | 135 | mSwapChain = mRenderer->createSwapChain(mNativeWindow, mShareHandle, |
daniel@transgaming.com | b9bb279 | 2012-11-28 19:36:49 +0000 | [diff] [blame] | 136 | mConfig->mRenderTargetFormat, |
| 137 | mConfig->mDepthStencilFormat); |
daniel@transgaming.com | 3c72078 | 2012-10-31 18:42:34 +0000 | [diff] [blame] | 138 | if (!mSwapChain) |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 139 | { |
Geoff Lang | 6b0cf99 | 2014-10-06 10:28:07 -0400 | [diff] [blame^] | 140 | return Error(EGL_BAD_ALLOC); |
daniel@transgaming.com | 3c72078 | 2012-10-31 18:42:34 +0000 | [diff] [blame] | 141 | } |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 142 | |
Geoff Lang | 6b0cf99 | 2014-10-06 10:28:07 -0400 | [diff] [blame^] | 143 | Error error = resetSwapChain(width, height); |
| 144 | if (error.isError()) |
daniel@transgaming.com | 3c72078 | 2012-10-31 18:42:34 +0000 | [diff] [blame] | 145 | { |
Geoff Lang | 6b0cf99 | 2014-10-06 10:28:07 -0400 | [diff] [blame^] | 146 | SafeDelete(mSwapChain); |
| 147 | return error; |
daniel@transgaming.com | 3c72078 | 2012-10-31 18:42:34 +0000 | [diff] [blame] | 148 | } |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 149 | |
Geoff Lang | 6b0cf99 | 2014-10-06 10:28:07 -0400 | [diff] [blame^] | 150 | return Error(EGL_SUCCESS); |
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 | |
Geoff Lang | 6b0cf99 | 2014-10-06 10:28:07 -0400 | [diff] [blame^] | 153 | Error Surface::resizeSwapChain(int backbufferWidth, int backbufferHeight) |
shannon.woods@transgaming.com | c71ca75 | 2013-02-28 23:06:50 +0000 | [diff] [blame] | 154 | { |
| 155 | ASSERT(backbufferWidth >= 0 && backbufferHeight >= 0); |
| 156 | ASSERT(mSwapChain); |
| 157 | |
John Bauman | 3dc300a | 2014-01-28 15:30:35 -0800 | [diff] [blame] | 158 | EGLint status = mSwapChain->resize(std::max(1, backbufferWidth), std::max(1, backbufferHeight)); |
shannon.woods@transgaming.com | c71ca75 | 2013-02-28 23:06:50 +0000 | [diff] [blame] | 159 | |
| 160 | if (status == EGL_CONTEXT_LOST) |
| 161 | { |
| 162 | mDisplay->notifyDeviceLost(); |
Geoff Lang | 6b0cf99 | 2014-10-06 10:28:07 -0400 | [diff] [blame^] | 163 | return Error(status); |
shannon.woods@transgaming.com | c71ca75 | 2013-02-28 23:06:50 +0000 | [diff] [blame] | 164 | } |
| 165 | else if (status != EGL_SUCCESS) |
| 166 | { |
Geoff Lang | 6b0cf99 | 2014-10-06 10:28:07 -0400 | [diff] [blame^] | 167 | return Error(status); |
shannon.woods@transgaming.com | c71ca75 | 2013-02-28 23:06:50 +0000 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | mWidth = backbufferWidth; |
| 171 | mHeight = backbufferHeight; |
| 172 | |
Geoff Lang | 6b0cf99 | 2014-10-06 10:28:07 -0400 | [diff] [blame^] | 173 | return Error(EGL_SUCCESS); |
shannon.woods@transgaming.com | c71ca75 | 2013-02-28 23:06:50 +0000 | [diff] [blame] | 174 | } |
| 175 | |
Geoff Lang | 6b0cf99 | 2014-10-06 10:28:07 -0400 | [diff] [blame^] | 176 | Error Surface::resetSwapChain(int backbufferWidth, int backbufferHeight) |
daniel@transgaming.com | 3c72078 | 2012-10-31 18:42:34 +0000 | [diff] [blame] | 177 | { |
| 178 | ASSERT(backbufferWidth >= 0 && backbufferHeight >= 0); |
| 179 | ASSERT(mSwapChain); |
| 180 | |
John Bauman | 3dc300a | 2014-01-28 15:30:35 -0800 | [diff] [blame] | 181 | EGLint status = mSwapChain->reset(std::max(1, backbufferWidth), std::max(1, backbufferHeight), mSwapInterval); |
daniel@transgaming.com | 3c72078 | 2012-10-31 18:42:34 +0000 | [diff] [blame] | 182 | |
| 183 | if (status == EGL_CONTEXT_LOST) |
| 184 | { |
shannon.woods@transgaming.com | eb049e2 | 2013-02-28 23:04:49 +0000 | [diff] [blame] | 185 | mRenderer->notifyDeviceLost(); |
Geoff Lang | 6b0cf99 | 2014-10-06 10:28:07 -0400 | [diff] [blame^] | 186 | return Error(status); |
daniel@transgaming.com | 3c72078 | 2012-10-31 18:42:34 +0000 | [diff] [blame] | 187 | } |
| 188 | else if (status != EGL_SUCCESS) |
| 189 | { |
Geoff Lang | 6b0cf99 | 2014-10-06 10:28:07 -0400 | [diff] [blame^] | 190 | return Error(status); |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 191 | } |
| 192 | |
apatrick@chromium.org | 0c71fd4 | 2012-08-10 18:08:47 +0000 | [diff] [blame] | 193 | mWidth = backbufferWidth; |
| 194 | mHeight = backbufferHeight; |
daniel@transgaming.com | 3c72078 | 2012-10-31 18:42:34 +0000 | [diff] [blame] | 195 | mSwapIntervalDirty = false; |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 196 | |
Geoff Lang | 6b0cf99 | 2014-10-06 10:28:07 -0400 | [diff] [blame^] | 197 | return Error(EGL_SUCCESS); |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 198 | } |
| 199 | |
Geoff Lang | 6b0cf99 | 2014-10-06 10:28:07 -0400 | [diff] [blame^] | 200 | Error Surface::swapRect(EGLint x, EGLint y, EGLint width, EGLint height) |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 201 | { |
| 202 | if (!mSwapChain) |
| 203 | { |
Geoff Lang | 6b0cf99 | 2014-10-06 10:28:07 -0400 | [diff] [blame^] | 204 | return Error(EGL_SUCCESS); |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | if (x + width > mWidth) |
| 208 | { |
| 209 | width = mWidth - x; |
| 210 | } |
| 211 | |
| 212 | if (y + height > mHeight) |
| 213 | { |
| 214 | height = mHeight - y; |
| 215 | } |
| 216 | |
| 217 | if (width == 0 || height == 0) |
| 218 | { |
Geoff Lang | 6b0cf99 | 2014-10-06 10:28:07 -0400 | [diff] [blame^] | 219 | return Error(EGL_SUCCESS); |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 220 | } |
| 221 | |
daniel@transgaming.com | 3c72078 | 2012-10-31 18:42:34 +0000 | [diff] [blame] | 222 | EGLint status = mSwapChain->swapRect(x, y, width, height); |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 223 | |
daniel@transgaming.com | 3c72078 | 2012-10-31 18:42:34 +0000 | [diff] [blame] | 224 | if (status == EGL_CONTEXT_LOST) |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 225 | { |
shannon.woods@transgaming.com | eb049e2 | 2013-02-28 23:04:49 +0000 | [diff] [blame] | 226 | mRenderer->notifyDeviceLost(); |
Geoff Lang | 6b0cf99 | 2014-10-06 10:28:07 -0400 | [diff] [blame^] | 227 | return Error(status); |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 228 | } |
daniel@transgaming.com | 3c72078 | 2012-10-31 18:42:34 +0000 | [diff] [blame] | 229 | else if (status != EGL_SUCCESS) |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 230 | { |
Geoff Lang | 6b0cf99 | 2014-10-06 10:28:07 -0400 | [diff] [blame^] | 231 | return Error(status); |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 232 | } |
| 233 | |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 234 | checkForOutOfDateSwapChain(); |
| 235 | |
Geoff Lang | 6b0cf99 | 2014-10-06 10:28:07 -0400 | [diff] [blame^] | 236 | return Error(EGL_SUCCESS); |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 237 | } |
| 238 | |
Cooper Partin | eeb1f53 | 2014-09-23 10:25:02 -0700 | [diff] [blame] | 239 | EGLNativeWindowType Surface::getWindowHandle() |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 240 | { |
Cooper Partin | eeb1f53 | 2014-09-23 10:25:02 -0700 | [diff] [blame] | 241 | return mNativeWindow.getNativeWindow(); |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 242 | } |
| 243 | |
Cooper Partin | 88d3b8c | 2014-10-08 10:41:56 -0700 | [diff] [blame] | 244 | #if !defined(ANGLE_ENABLE_WINDOWS_STORE) |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 245 | #define kSurfaceProperty _TEXT("Egl::SurfaceOwner") |
| 246 | #define kParentWndProc _TEXT("Egl::SurfaceParentWndProc") |
| 247 | |
| 248 | static LRESULT CALLBACK SurfaceWindowProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam) |
| 249 | { |
| 250 | if (message == WM_SIZE) |
| 251 | { |
| 252 | Surface* surf = reinterpret_cast<Surface*>(GetProp(hwnd, kSurfaceProperty)); |
| 253 | if(surf) |
| 254 | { |
| 255 | surf->checkForOutOfDateSwapChain(); |
| 256 | } |
| 257 | } |
| 258 | WNDPROC prevWndFunc = reinterpret_cast<WNDPROC >(GetProp(hwnd, kParentWndProc)); |
| 259 | return CallWindowProc(prevWndFunc, hwnd, message, wparam, lparam); |
| 260 | } |
Cooper Partin | 88d3b8c | 2014-10-08 10:41:56 -0700 | [diff] [blame] | 261 | #endif |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 262 | |
| 263 | void Surface::subclassWindow() |
| 264 | { |
Cooper Partin | 88d3b8c | 2014-10-08 10:41:56 -0700 | [diff] [blame] | 265 | #if !defined(ANGLE_ENABLE_WINDOWS_STORE) |
Cooper Partin | eeb1f53 | 2014-09-23 10:25:02 -0700 | [diff] [blame] | 266 | HWND window = mNativeWindow.getNativeWindow(); |
| 267 | if (!window) |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 268 | { |
| 269 | return; |
| 270 | } |
| 271 | |
| 272 | DWORD processId; |
Cooper Partin | eeb1f53 | 2014-09-23 10:25:02 -0700 | [diff] [blame] | 273 | DWORD threadId = GetWindowThreadProcessId(window, &processId); |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 274 | if (processId != GetCurrentProcessId() || threadId != GetCurrentThreadId()) |
| 275 | { |
| 276 | return; |
| 277 | } |
| 278 | |
| 279 | SetLastError(0); |
Cooper Partin | eeb1f53 | 2014-09-23 10:25:02 -0700 | [diff] [blame] | 280 | LONG_PTR oldWndProc = SetWindowLongPtr(window, GWLP_WNDPROC, reinterpret_cast<LONG_PTR>(SurfaceWindowProc)); |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 281 | if(oldWndProc == 0 && GetLastError() != ERROR_SUCCESS) |
| 282 | { |
| 283 | mWindowSubclassed = false; |
| 284 | return; |
| 285 | } |
| 286 | |
Cooper Partin | eeb1f53 | 2014-09-23 10:25:02 -0700 | [diff] [blame] | 287 | SetProp(window, kSurfaceProperty, reinterpret_cast<HANDLE>(this)); |
| 288 | SetProp(window, kParentWndProc, reinterpret_cast<HANDLE>(oldWndProc)); |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 289 | mWindowSubclassed = true; |
Cooper Partin | 88d3b8c | 2014-10-08 10:41:56 -0700 | [diff] [blame] | 290 | #endif |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | void Surface::unsubclassWindow() |
| 294 | { |
| 295 | if(!mWindowSubclassed) |
| 296 | { |
| 297 | return; |
| 298 | } |
| 299 | |
Cooper Partin | 88d3b8c | 2014-10-08 10:41:56 -0700 | [diff] [blame] | 300 | #if !defined(ANGLE_ENABLE_WINDOWS_STORE) |
Cooper Partin | eeb1f53 | 2014-09-23 10:25:02 -0700 | [diff] [blame] | 301 | HWND window = mNativeWindow.getNativeWindow(); |
| 302 | if (!window) |
| 303 | { |
| 304 | return; |
| 305 | } |
| 306 | |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 307 | // un-subclass |
Cooper Partin | eeb1f53 | 2014-09-23 10:25:02 -0700 | [diff] [blame] | 308 | LONG_PTR parentWndFunc = reinterpret_cast<LONG_PTR>(GetProp(window, kParentWndProc)); |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 309 | |
| 310 | // Check the windowproc is still SurfaceWindowProc. |
| 311 | // If this assert fails, then it is likely the application has subclassed the |
| 312 | // hwnd as well and did not unsubclass before destroying its EGL context. The |
| 313 | // application should be modified to either subclass before initializing the |
| 314 | // EGL context, or to unsubclass before destroying the EGL context. |
| 315 | if(parentWndFunc) |
| 316 | { |
Cooper Partin | eeb1f53 | 2014-09-23 10:25:02 -0700 | [diff] [blame] | 317 | LONG_PTR prevWndFunc = SetWindowLongPtr(window, GWLP_WNDPROC, parentWndFunc); |
Geoff Lang | 9cd1915 | 2014-05-28 15:54:34 -0400 | [diff] [blame] | 318 | UNUSED_ASSERTION_VARIABLE(prevWndFunc); |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 319 | ASSERT(prevWndFunc == reinterpret_cast<LONG_PTR>(SurfaceWindowProc)); |
| 320 | } |
| 321 | |
Cooper Partin | eeb1f53 | 2014-09-23 10:25:02 -0700 | [diff] [blame] | 322 | RemoveProp(window, kSurfaceProperty); |
| 323 | RemoveProp(window, kParentWndProc); |
Cooper Partin | 88d3b8c | 2014-10-08 10:41:56 -0700 | [diff] [blame] | 324 | #endif |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 325 | mWindowSubclassed = false; |
| 326 | } |
| 327 | |
| 328 | bool Surface::checkForOutOfDateSwapChain() |
| 329 | { |
| 330 | RECT client; |
John Bauman | 3dc300a | 2014-01-28 15:30:35 -0800 | [diff] [blame] | 331 | int clientWidth = getWidth(); |
| 332 | int clientHeight = getHeight(); |
| 333 | bool sizeDirty = false; |
Cooper Partin | eeb1f53 | 2014-09-23 10:25:02 -0700 | [diff] [blame] | 334 | if (!mFixedSize && !mNativeWindow.isIconic()) |
John Bauman | 827a471 | 2013-10-29 16:03:11 -0700 | [diff] [blame] | 335 | { |
| 336 | // The window is automatically resized to 150x22 when it's minimized, but the swapchain shouldn't be resized |
| 337 | // because that's not a useful size to render to. |
Cooper Partin | eeb1f53 | 2014-09-23 10:25:02 -0700 | [diff] [blame] | 338 | if (!mNativeWindow.getClientRect(&client)) |
John Bauman | 3dc300a | 2014-01-28 15:30:35 -0800 | [diff] [blame] | 339 | { |
| 340 | ASSERT(false); |
| 341 | return false; |
| 342 | } |
| 343 | |
| 344 | // Grow the buffer now, if the window has grown. We need to grow now to avoid losing information. |
| 345 | clientWidth = client.right - client.left; |
| 346 | clientHeight = client.bottom - client.top; |
| 347 | sizeDirty = clientWidth != getWidth() || clientHeight != getHeight(); |
John Bauman | 827a471 | 2013-10-29 16:03:11 -0700 | [diff] [blame] | 348 | } |
| 349 | |
Jamie Madill | 58e6032 | 2013-12-02 11:09:36 -0500 | [diff] [blame] | 350 | bool wasDirty = (mSwapIntervalDirty || sizeDirty); |
| 351 | |
shannon.woods@transgaming.com | c71ca75 | 2013-02-28 23:06:50 +0000 | [diff] [blame] | 352 | if (mSwapIntervalDirty) |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 353 | { |
| 354 | resetSwapChain(clientWidth, clientHeight); |
shannon.woods@transgaming.com | c71ca75 | 2013-02-28 23:06:50 +0000 | [diff] [blame] | 355 | } |
| 356 | else if (sizeDirty) |
| 357 | { |
| 358 | resizeSwapChain(clientWidth, clientHeight); |
| 359 | } |
| 360 | |
Jamie Madill | 58e6032 | 2013-12-02 11:09:36 -0500 | [diff] [blame] | 361 | if (wasDirty) |
shannon.woods@transgaming.com | c71ca75 | 2013-02-28 23:06:50 +0000 | [diff] [blame] | 362 | { |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 363 | if (static_cast<egl::Surface*>(getCurrentDrawSurface()) == this) |
| 364 | { |
| 365 | glMakeCurrent(glGetCurrentContext(), static_cast<egl::Display*>(getCurrentDisplay()), this); |
| 366 | } |
| 367 | |
| 368 | return true; |
| 369 | } |
shannon.woods@transgaming.com | c71ca75 | 2013-02-28 23:06:50 +0000 | [diff] [blame] | 370 | |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 371 | return false; |
| 372 | } |
| 373 | |
Geoff Lang | 6b0cf99 | 2014-10-06 10:28:07 -0400 | [diff] [blame^] | 374 | Error Surface::swap() |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 375 | { |
| 376 | return swapRect(0, 0, mWidth, mHeight); |
| 377 | } |
| 378 | |
Geoff Lang | 6b0cf99 | 2014-10-06 10:28:07 -0400 | [diff] [blame^] | 379 | Error Surface::postSubBuffer(EGLint x, EGLint y, EGLint width, EGLint height) |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 380 | { |
| 381 | if (!mPostSubBufferSupported) |
| 382 | { |
| 383 | // Spec is not clear about how this should be handled. |
Geoff Lang | 6b0cf99 | 2014-10-06 10:28:07 -0400 | [diff] [blame^] | 384 | return Error(EGL_SUCCESS); |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 385 | } |
Nicolas Capens | 3501c16 | 2014-05-21 13:27:15 -0400 | [diff] [blame] | 386 | |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 387 | return swapRect(x, y, width, height); |
| 388 | } |
| 389 | |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 390 | EGLint Surface::isPostSubBufferSupported() const |
| 391 | { |
| 392 | return mPostSubBufferSupported; |
| 393 | } |
| 394 | |
daniel@transgaming.com | 76d3e6e | 2012-10-31 19:55:33 +0000 | [diff] [blame] | 395 | rx::SwapChain *Surface::getSwapChain() const |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 396 | { |
daniel@transgaming.com | 3c72078 | 2012-10-31 18:42:34 +0000 | [diff] [blame] | 397 | return mSwapChain; |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | void Surface::setSwapInterval(EGLint interval) |
| 401 | { |
| 402 | if (mSwapInterval == interval) |
| 403 | { |
| 404 | return; |
| 405 | } |
Nicolas Capens | 3501c16 | 2014-05-21 13:27:15 -0400 | [diff] [blame] | 406 | |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 407 | mSwapInterval = interval; |
daniel@transgaming.com | 114bd46 | 2012-10-31 18:42:47 +0000 | [diff] [blame] | 408 | mSwapInterval = std::max(mSwapInterval, mRenderer->getMinSwapInterval()); |
| 409 | mSwapInterval = std::min(mSwapInterval, mRenderer->getMaxSwapInterval()); |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 410 | |
daniel@transgaming.com | 3c72078 | 2012-10-31 18:42:34 +0000 | [diff] [blame] | 411 | mSwapIntervalDirty = true; |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 412 | } |
| 413 | |
Nicolas Capens | 3501c16 | 2014-05-21 13:27:15 -0400 | [diff] [blame] | 414 | EGLint Surface::getConfigID() const |
| 415 | { |
| 416 | return mConfig->mConfigID; |
| 417 | } |
| 418 | |
| 419 | EGLint Surface::getWidth() const |
| 420 | { |
| 421 | return mWidth; |
| 422 | } |
| 423 | |
| 424 | EGLint Surface::getHeight() const |
| 425 | { |
| 426 | return mHeight; |
| 427 | } |
| 428 | |
| 429 | EGLint Surface::getPixelAspectRatio() const |
| 430 | { |
| 431 | return mPixelAspectRatio; |
| 432 | } |
| 433 | |
| 434 | EGLenum Surface::getRenderBuffer() const |
| 435 | { |
| 436 | return mRenderBuffer; |
| 437 | } |
| 438 | |
| 439 | EGLenum Surface::getSwapBehavior() const |
| 440 | { |
| 441 | return mSwapBehavior; |
| 442 | } |
| 443 | |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 444 | EGLenum Surface::getTextureFormat() const |
| 445 | { |
| 446 | return mTextureFormat; |
| 447 | } |
| 448 | |
| 449 | EGLenum Surface::getTextureTarget() const |
| 450 | { |
| 451 | return mTextureTarget; |
| 452 | } |
| 453 | |
| 454 | void Surface::setBoundTexture(gl::Texture2D *texture) |
| 455 | { |
| 456 | mTexture = texture; |
| 457 | } |
| 458 | |
| 459 | gl::Texture2D *Surface::getBoundTexture() const |
| 460 | { |
| 461 | return mTexture; |
| 462 | } |
| 463 | |
John Bauman | 3dc300a | 2014-01-28 15:30:35 -0800 | [diff] [blame] | 464 | EGLint Surface::isFixedSize() const |
| 465 | { |
| 466 | return mFixedSize; |
| 467 | } |
| 468 | |
daniel@transgaming.com | 106e1f7 | 2012-10-31 18:38:36 +0000 | [diff] [blame] | 469 | EGLenum Surface::getFormat() const |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 470 | { |
| 471 | return mConfig->mRenderTargetFormat; |
| 472 | } |
| 473 | } |