You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DeviceContext.cxx 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
  2. * Copyright 2014 Pierre Ossman for Cendio AB
  3. *
  4. * This is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This software is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this software; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  17. * USA.
  18. */
  19. #include <rfb_win32/DeviceContext.h>
  20. #include <rfb_win32/CompatibleBitmap.h>
  21. #include <rfb_win32/BitmapInfo.h>
  22. #include <rdr/Exception.h>
  23. #include <rfb/LogWriter.h>
  24. using namespace rfb;
  25. using namespace win32;
  26. static LogWriter vlog("DeviceContext");
  27. PixelFormat DeviceContext::getPF() const {
  28. return getPF(dc);
  29. }
  30. PixelFormat DeviceContext::getPF(HDC dc) {
  31. bool trueColour, bigEndian;
  32. int bpp, depth;
  33. int redMax, greenMax, blueMax;
  34. int redShift, greenShift, blueShift;
  35. CompatibleBitmap bitmap(dc, 1, 1);
  36. // -=- Get the bitmap format information
  37. BitmapInfo bi;
  38. memset(&bi, 0, sizeof(bi));
  39. bi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
  40. bi.bmiHeader.biBitCount = 0;
  41. if (!::GetDIBits(dc, bitmap, 0, 1, NULL, (BITMAPINFO*)&bi, DIB_RGB_COLORS)) {
  42. throw rdr::SystemException("unable to determine device pixel format", GetLastError());
  43. }
  44. if (!::GetDIBits(dc, bitmap, 0, 1, NULL, (BITMAPINFO*)&bi, DIB_RGB_COLORS)) {
  45. throw rdr::SystemException("unable to determine pixel shifts/palette", GetLastError());
  46. }
  47. // Set the initial format information
  48. trueColour = bi.bmiHeader.biBitCount > 8;
  49. bigEndian = 0;
  50. bpp = bi.bmiHeader.biBitCount;
  51. if (trueColour) {
  52. DWORD rMask=0, gMask=0, bMask=0;
  53. // Which true colour format is the DIB section using?
  54. switch (bi.bmiHeader.biCompression) {
  55. case BI_RGB:
  56. // Default RGB layout
  57. switch (bi.bmiHeader.biBitCount) {
  58. case 16:
  59. // RGB 555 - High Colour
  60. vlog.info("16-bit High Colour");
  61. rMask = 0x7c00;
  62. bMask = 0x001f;
  63. gMask = 0x03e0;
  64. break;
  65. case 24:
  66. case 32:
  67. // RGB 888 - True Colour
  68. vlog.info("24/32-bit High Colour");
  69. rMask = 0xff0000;
  70. gMask = 0x00ff00;
  71. bMask = 0x0000ff;
  72. break;
  73. default:
  74. vlog.error("bits per pixel %u not supported", bi.bmiHeader.biBitCount);
  75. throw rdr::Exception("unknown bits per pixel specified");
  76. };
  77. break;
  78. case BI_BITFIELDS:
  79. // Custom RGB layout
  80. rMask = bi.mask.red;
  81. gMask = bi.mask.green;
  82. bMask = bi.mask.blue;
  83. vlog.info("%d-bit BitFields: (%lx, %lx, %lx)",
  84. bi.bmiHeader.biBitCount, rMask, gMask, bMask);
  85. break;
  86. };
  87. // Convert the data we just retrieved
  88. initMaxAndShift(rMask, &redMax, &redShift);
  89. initMaxAndShift(gMask, &greenMax, &greenShift);
  90. initMaxAndShift(bMask, &blueMax, &blueShift);
  91. // Calculate the depth from the colour shifts
  92. depth = 0;
  93. Pixel bits = rMask | gMask | bMask;
  94. while (bits) {
  95. depth++;
  96. bits = bits >> 1;
  97. }
  98. // Check that the depth & bpp are valid
  99. if (depth > bpp) {
  100. vlog.error("depth exceeds bits per pixel!");
  101. bpp = depth;
  102. }
  103. // Correct the bits-per-pixel to something we're happy with
  104. if (bpp <= 16)
  105. bpp = 16;
  106. else if (bpp <= 32)
  107. bpp = 32;
  108. } else {
  109. // Palettised format - depth reflects number of colours,
  110. // but bits-per-pixel is ALWAYS 8
  111. depth = bpp;
  112. if (bpp < 8)
  113. bpp = 8;
  114. vlog.info("%d-colour palettised", 1<<depth);
  115. // Aren't really used, but set them to keep the compiler happy
  116. redMax = redShift = 0;
  117. greenMax = greenShift = 0;
  118. blueMax = blueShift = 0;
  119. }
  120. return PixelFormat(bpp, depth, bigEndian, trueColour,
  121. redMax, greenMax, blueMax,
  122. redShift, greenShift, blueShift);
  123. }
  124. Rect DeviceContext::getClipBox() const {
  125. return getClipBox(dc);
  126. }
  127. Rect DeviceContext::getClipBox(HDC dc) {
  128. // Get the display dimensions
  129. RECT cr;
  130. if (!GetClipBox(dc, &cr))
  131. throw rdr::SystemException("GetClipBox", GetLastError());
  132. return Rect(cr.left, cr.top, cr.right, cr.bottom);
  133. }
  134. DeviceDC::DeviceDC(const TCHAR* deviceName) {
  135. dc = ::CreateDC(_T("DISPLAY"), deviceName, NULL, NULL);
  136. if (!dc)
  137. throw rdr::SystemException("failed to create DeviceDC", GetLastError());
  138. }
  139. DeviceDC::~DeviceDC() {
  140. if (dc)
  141. DeleteDC(dc);
  142. }
  143. WindowDC::WindowDC(HWND wnd) : hwnd(wnd) {
  144. dc = GetDC(wnd);
  145. if (!dc)
  146. throw rdr::SystemException("GetDC failed", GetLastError());
  147. }
  148. WindowDC::~WindowDC() {
  149. if (dc)
  150. ReleaseDC(hwnd, dc);
  151. }
  152. CompatibleDC::CompatibleDC(HDC existing) {
  153. dc = CreateCompatibleDC(existing);
  154. if (!dc)
  155. throw rdr::SystemException("CreateCompatibleDC failed", GetLastError());
  156. }
  157. CompatibleDC::~CompatibleDC() {
  158. if (dc)
  159. DeleteDC(dc);
  160. }
  161. BitmapDC::BitmapDC(HDC hdc, HBITMAP hbitmap) : CompatibleDC(hdc){
  162. oldBitmap = (HBITMAP)SelectObject(dc, hbitmap);
  163. if (!oldBitmap)
  164. throw rdr::SystemException("SelectObject to CompatibleDC failed",
  165. GetLastError());
  166. }
  167. BitmapDC::~BitmapDC() {
  168. SelectObject(dc, oldBitmap);
  169. }