blob: c694f55b3163f5d3a81784eb601e83c6889d7a38 [file] [log] [blame]
sergeyu@chromium.org15e32cc2013-04-29 20:10:57 +00001/*
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
sergeyucc9669c2016-02-09 15:13:26 -080013#include <utility>
14
Edward Lemurc20978e2017-07-06 19:44:34 +020015#include "webrtc/rtc_base/logging.h"
sergeyu@chromium.org6a5cc9d2013-09-12 19:17:26 +000016
sergeyu@chromium.org15e32cc2013-04-29 20:10:57 +000017namespace webrtc {
18
19DesktopFrameWin::DesktopFrameWin(DesktopSize size,
20 int stride,
21 uint8_t* data,
kwiberg2bb3afa2016-03-16 15:58:08 -070022 std::unique_ptr<SharedMemory> shared_memory,
sergeyu@chromium.org15e32cc2013-04-29 20:10:57 +000023 HBITMAP bitmap)
sergeyucc9669c2016-02-09 15:13:26 -080024 : DesktopFrame(size, stride, data, shared_memory.get()),
sergeyu@chromium.org15e32cc2013-04-29 20:10:57 +000025 bitmap_(bitmap),
sergeyucc9669c2016-02-09 15:13:26 -080026 owned_shared_memory_(std::move(shared_memory)) {}
sergeyu@chromium.org15e32cc2013-04-29 20:10:57 +000027
28DesktopFrameWin::~DesktopFrameWin() {
29 DeleteObject(bitmap_);
30}
31
32// static
sergeyu5d910282016-06-07 16:41:58 -070033std::unique_ptr<DesktopFrameWin> DesktopFrameWin::Create(
sergeyucc9669c2016-02-09 15:13:26 -080034 DesktopSize size,
35 SharedMemoryFactory* shared_memory_factory,
36 HDC hdc) {
sergeyu@chromium.org15e32cc2013-04-29 20:10:57 +000037 int bytes_per_row = size.width() * kBytesPerPixel;
sergeyucc9669c2016-02-09 15:13:26 -080038 int buffer_size = bytes_per_row * size.height();
sergeyu@chromium.org15e32cc2013-04-29 20:10:57 +000039
40 // Describe a device independent bitmap (DIB) that is the size of the desktop.
dchenga771bf82015-07-01 17:52:10 -070041 BITMAPINFO bmi = {};
sergeyu@chromium.org15e32cc2013-04-29 20:10:57 +000042 bmi.bmiHeader.biHeight = -size.height();
43 bmi.bmiHeader.biWidth = size.width();
44 bmi.bmiHeader.biPlanes = 1;
45 bmi.bmiHeader.biBitCount = DesktopFrameWin::kBytesPerPixel * 8;
46 bmi.bmiHeader.biSize = sizeof(bmi.bmiHeader);
47 bmi.bmiHeader.biSizeImage = bytes_per_row * size.height();
48
kwiberg2bb3afa2016-03-16 15:58:08 -070049 std::unique_ptr<SharedMemory> shared_memory;
sergeyucc9669c2016-02-09 15:13:26 -080050 HANDLE section_handle = nullptr;
51 if (shared_memory_factory) {
kwiberg1c7fdd82016-04-26 08:18:04 -070052 shared_memory = shared_memory_factory->CreateSharedMemory(buffer_size);
sergeyu6acd9f42016-04-21 09:46:22 -070053 section_handle = shared_memory->handle();
sergeyucc9669c2016-02-09 15:13:26 -080054 }
55 void* data = nullptr;
sergeyu@chromium.org15e32cc2013-04-29 20:10:57 +000056 HBITMAP bitmap = CreateDIBSection(hdc, &bmi, DIB_RGB_COLORS, &data,
57 section_handle, 0);
58 if (!bitmap) {
sergeyu@chromium.org6a5cc9d2013-09-12 19:17:26 +000059 LOG(LS_WARNING) << "Failed to allocate new window frame " << GetLastError();
sergeyucc9669c2016-02-09 15:13:26 -080060 return nullptr;
sergeyu@chromium.org15e32cc2013-04-29 20:10:57 +000061 }
62
sergeyu5d910282016-06-07 16:41:58 -070063 return std::unique_ptr<DesktopFrameWin>(
64 new DesktopFrameWin(size, bytes_per_row, reinterpret_cast<uint8_t*>(data),
65 std::move(shared_memory), bitmap));
sergeyu@chromium.org15e32cc2013-04-29 20:10:57 +000066}
67
68} // namespace webrtc