Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 1 | // Copyright 2016 The SwiftShader Authors. All Rights Reserved. |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2 | // |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 6 | // |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 8 | // |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 14 | |
| 15 | #include "FrameBufferGDI.hpp" |
| 16 | |
Nicolas Capens | 708c24b | 2017-10-26 13:07:10 -0400 | [diff] [blame] | 17 | #include "Common/Debug.hpp" |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 18 | |
| 19 | namespace sw |
| 20 | { |
| 21 | extern bool forceWindowed; |
| 22 | |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 23 | FrameBufferGDI::FrameBufferGDI(HWND windowHandle, int width, int height, bool fullscreen, bool topLeftOrigin) : FrameBufferWin(windowHandle, width, height, fullscreen, topLeftOrigin) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 24 | { |
| 25 | if(!windowed) |
| 26 | { |
| 27 | SetWindowPos(windowHandle, HWND_TOPMOST, 0, 0, width, height, SWP_SHOWWINDOW); |
| 28 | |
| 29 | DEVMODE deviceMode; |
| 30 | deviceMode.dmSize = sizeof(DEVMODE); |
| 31 | deviceMode.dmPelsWidth= width; |
| 32 | deviceMode.dmPelsHeight = height; |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 33 | deviceMode.dmFields = DM_PELSWIDTH | DM_PELSHEIGHT; |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 34 | |
| 35 | ChangeDisplaySettings(&deviceMode, CDS_FULLSCREEN); |
| 36 | } |
| 37 | |
| 38 | init(this->windowHandle); |
| 39 | |
Nicolas Capens | e5a9637 | 2017-08-11 15:14:25 -0400 | [diff] [blame] | 40 | format = FORMAT_X8R8G8B8; |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | FrameBufferGDI::~FrameBufferGDI() |
| 44 | { |
| 45 | release(); |
| 46 | |
| 47 | if(!windowed) |
| 48 | { |
| 49 | ChangeDisplaySettings(0, 0); |
| 50 | |
| 51 | RECT clientRect; |
| 52 | RECT windowRect; |
| 53 | GetClientRect(windowHandle, &clientRect); |
| 54 | GetWindowRect(windowHandle, &windowRect); |
| 55 | int windowWidth = width + (windowRect.right - windowRect.left) - (clientRect.right - clientRect.left); |
| 56 | int windowHeight = height + (windowRect.bottom - windowRect.top) - (clientRect.bottom - clientRect.top); |
| 57 | int desktopWidth = GetSystemMetrics(SM_CXSCREEN); |
| 58 | int desktopHeight = GetSystemMetrics(SM_CYSCREEN); |
| 59 | SetWindowPos(windowHandle, HWND_TOP, desktopWidth / 2 - windowWidth / 2, desktopHeight / 2 - windowHeight / 2, windowWidth, windowHeight, SWP_SHOWWINDOW); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | void *FrameBufferGDI::lock() |
| 64 | { |
| 65 | stride = width * 4; |
| 66 | |
Nicolas Capens | e5a9637 | 2017-08-11 15:14:25 -0400 | [diff] [blame] | 67 | return framebuffer; |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | void FrameBufferGDI::unlock() |
| 71 | { |
| 72 | } |
| 73 | |
Nicolas Capens | 241f789 | 2015-12-30 23:40:45 -0500 | [diff] [blame] | 74 | void FrameBufferGDI::flip(sw::Surface *source) |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 75 | { |
Nicolas Capens | 241f789 | 2015-12-30 23:40:45 -0500 | [diff] [blame] | 76 | blit(source, nullptr, nullptr); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 77 | } |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 78 | |
Nicolas Capens | 241f789 | 2015-12-30 23:40:45 -0500 | [diff] [blame] | 79 | void FrameBufferGDI::blit(sw::Surface *source, const Rect *sourceRect, const Rect *destRect) |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 80 | { |
Nicolas Capens | 241f789 | 2015-12-30 23:40:45 -0500 | [diff] [blame] | 81 | copy(source); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 82 | |
| 83 | int sourceLeft = sourceRect ? sourceRect->x0 : 0; |
| 84 | int sourceTop = sourceRect ? sourceRect->y0 : 0; |
| 85 | int sourceWidth = sourceRect ? sourceRect->x1 - sourceRect->x0 : width; |
| 86 | int sourceHeight = sourceRect ? sourceRect->y1 - sourceRect->y0 : height; |
| 87 | int destLeft = destRect ? destRect->x0 : 0; |
| 88 | int destTop = destRect ? destRect->y0 : 0; |
| 89 | int destWidth = destRect ? destRect->x1 - destRect->x0 : bounds.right - bounds.left; |
| 90 | int destHeight = destRect ? destRect->y1 - destRect->y0 : bounds.bottom - bounds.top; |
| 91 | |
| 92 | StretchBlt(windowContext, destLeft, destTop, destWidth, destHeight, bitmapContext, sourceLeft, sourceTop, sourceWidth, sourceHeight, SRCCOPY); |
| 93 | } |
| 94 | |
Nicolas Capens | 241f789 | 2015-12-30 23:40:45 -0500 | [diff] [blame] | 95 | void FrameBufferGDI::flip(HWND windowOverride, sw::Surface *source) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 96 | { |
Nicolas Capens | 241f789 | 2015-12-30 23:40:45 -0500 | [diff] [blame] | 97 | blit(windowOverride, source, nullptr, nullptr); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 98 | } |
| 99 | |
Nicolas Capens | 241f789 | 2015-12-30 23:40:45 -0500 | [diff] [blame] | 100 | void FrameBufferGDI::blit(HWND windowOverride, sw::Surface *source, const Rect *sourceRect, const Rect *destRect) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 101 | { |
| 102 | if(windowed && windowOverride != 0 && windowOverride != bitmapWindow) |
| 103 | { |
| 104 | release(); |
| 105 | init(windowOverride); |
| 106 | } |
| 107 | |
Nicolas Capens | 241f789 | 2015-12-30 23:40:45 -0500 | [diff] [blame] | 108 | blit(source, sourceRect, destRect); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | void FrameBufferGDI::setGammaRamp(GammaRamp *gammaRamp, bool calibrate) |
| 112 | { |
| 113 | SetDeviceGammaRamp(windowContext, gammaRamp); |
| 114 | } |
| 115 | |
| 116 | void FrameBufferGDI::getGammaRamp(GammaRamp *gammaRamp) |
| 117 | { |
| 118 | GetDeviceGammaRamp(windowContext, gammaRamp); |
| 119 | } |
| 120 | |
| 121 | void FrameBufferGDI::screenshot(void *destBuffer) |
| 122 | { |
| 123 | UNIMPLEMENTED(); |
| 124 | } |
| 125 | |
| 126 | bool FrameBufferGDI::getScanline(bool &inVerticalBlank, unsigned int &scanline) |
| 127 | { |
| 128 | UNIMPLEMENTED(); |
| 129 | |
| 130 | return false; |
| 131 | } |
| 132 | |
| 133 | void FrameBufferGDI::init(HWND window) |
| 134 | { |
| 135 | bitmapWindow = window; |
| 136 | |
| 137 | windowContext = GetDC(window); |
| 138 | bitmapContext = CreateCompatibleDC(windowContext); |
| 139 | |
| 140 | BITMAPINFO bitmapInfo; |
| 141 | memset(&bitmapInfo, 0, sizeof(BITMAPINFO)); |
| 142 | bitmapInfo.bmiHeader.biSize = sizeof(BITMAPINFO); |
| 143 | bitmapInfo.bmiHeader.biBitCount = 32; |
| 144 | bitmapInfo.bmiHeader.biPlanes = 1; |
| 145 | bitmapInfo.bmiHeader.biHeight = -height; |
| 146 | bitmapInfo.bmiHeader.biWidth = width; |
| 147 | bitmapInfo.bmiHeader.biCompression = BI_RGB; |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 148 | |
Nicolas Capens | e5a9637 | 2017-08-11 15:14:25 -0400 | [diff] [blame] | 149 | bitmap = CreateDIBSection(bitmapContext, &bitmapInfo, DIB_RGB_COLORS, &framebuffer, 0, 0); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 150 | SelectObject(bitmapContext, bitmap); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 151 | |
| 152 | updateBounds(window); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | void FrameBufferGDI::release() |
| 156 | { |
| 157 | SelectObject(bitmapContext, 0); |
| 158 | DeleteObject(bitmap); |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 159 | ReleaseDC(bitmapWindow, windowContext); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 160 | DeleteDC(bitmapContext); |
| 161 | } |
| 162 | } |