sergeyu@chromium.org | 85a1dab | 2013-04-29 20:10:57 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
| 11 | #include "webrtc/modules/desktop_capture/desktop_frame_win.h" |
| 12 | |
sergeyu@chromium.org | d0737d9 | 2013-09-12 19:17:26 +0000 | [diff] [blame] | 13 | #include "webrtc/system_wrappers/interface/logging.h" |
| 14 | |
sergeyu@chromium.org | 85a1dab | 2013-04-29 20:10:57 +0000 | [diff] [blame] | 15 | namespace webrtc { |
| 16 | |
| 17 | DesktopFrameWin::DesktopFrameWin(DesktopSize size, |
| 18 | int stride, |
| 19 | uint8_t* data, |
| 20 | SharedMemory* shared_memory, |
| 21 | HBITMAP bitmap) |
| 22 | : DesktopFrame(size, stride, data, shared_memory), |
| 23 | bitmap_(bitmap), |
| 24 | owned_shared_memory_(shared_memory_) { |
| 25 | } |
| 26 | |
| 27 | DesktopFrameWin::~DesktopFrameWin() { |
| 28 | DeleteObject(bitmap_); |
| 29 | } |
| 30 | |
| 31 | // static |
| 32 | DesktopFrameWin* DesktopFrameWin::Create(DesktopSize size, |
| 33 | SharedMemory* shared_memory, |
| 34 | HDC hdc) { |
| 35 | int bytes_per_row = size.width() * kBytesPerPixel; |
| 36 | |
| 37 | // Describe a device independent bitmap (DIB) that is the size of the desktop. |
| 38 | BITMAPINFO bmi = {0}; |
| 39 | bmi.bmiHeader.biHeight = -size.height(); |
| 40 | bmi.bmiHeader.biWidth = size.width(); |
| 41 | bmi.bmiHeader.biPlanes = 1; |
| 42 | bmi.bmiHeader.biBitCount = DesktopFrameWin::kBytesPerPixel * 8; |
| 43 | bmi.bmiHeader.biSize = sizeof(bmi.bmiHeader); |
| 44 | bmi.bmiHeader.biSizeImage = bytes_per_row * size.height(); |
| 45 | |
| 46 | HANDLE section_handle = NULL; |
| 47 | if (shared_memory) |
| 48 | section_handle = shared_memory->handle(); |
| 49 | void* data = NULL; |
| 50 | HBITMAP bitmap = CreateDIBSection(hdc, &bmi, DIB_RGB_COLORS, &data, |
| 51 | section_handle, 0); |
| 52 | if (!bitmap) { |
sergeyu@chromium.org | d0737d9 | 2013-09-12 19:17:26 +0000 | [diff] [blame] | 53 | LOG(LS_WARNING) << "Failed to allocate new window frame " << GetLastError(); |
sergeyu@chromium.org | 85a1dab | 2013-04-29 20:10:57 +0000 | [diff] [blame] | 54 | delete shared_memory; |
| 55 | return NULL; |
| 56 | } |
| 57 | |
| 58 | return new DesktopFrameWin(size, bytes_per_row, |
| 59 | reinterpret_cast<uint8_t*>(data), |
| 60 | shared_memory, bitmap); |
| 61 | } |
| 62 | |
| 63 | } // namespace webrtc |