tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

desktop_frame_win.cc (2704B)


      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 "modules/desktop_capture/desktop_frame_win.h"
     12 
     13 #include <cstdint>
     14 #include <memory>
     15 #include <utility>
     16 
     17 #include "modules/desktop_capture/desktop_frame.h"
     18 #include "modules/desktop_capture/desktop_geometry.h"
     19 #include "modules/desktop_capture/shared_memory.h"
     20 #include "rtc_base/logging.h"
     21 
     22 namespace webrtc {
     23 
     24 DesktopFrameWin::DesktopFrameWin(DesktopSize size,
     25                                 int stride,
     26                                 uint8_t* data,
     27                                 std::unique_ptr<SharedMemory> shared_memory,
     28                                 HBITMAP bitmap)
     29    : DesktopFrame(size, stride, data, shared_memory.get()),
     30      bitmap_(bitmap),
     31      owned_shared_memory_(std::move(shared_memory)) {}
     32 
     33 DesktopFrameWin::~DesktopFrameWin() {
     34  DeleteObject(bitmap_);
     35 }
     36 
     37 // static
     38 std::unique_ptr<DesktopFrameWin> DesktopFrameWin::Create(
     39    DesktopSize size,
     40    SharedMemoryFactory* shared_memory_factory,
     41    HDC hdc) {
     42  int bytes_per_row = size.width() * kBytesPerPixel;
     43  int buffer_size = bytes_per_row * size.height();
     44 
     45  // Describe a device independent bitmap (DIB) that is the size of the desktop.
     46  BITMAPINFO bmi = {};
     47  bmi.bmiHeader.biHeight = -size.height();
     48  bmi.bmiHeader.biWidth = size.width();
     49  bmi.bmiHeader.biPlanes = 1;
     50  bmi.bmiHeader.biBitCount = DesktopFrameWin::kBytesPerPixel * 8;
     51  bmi.bmiHeader.biSize = sizeof(bmi.bmiHeader);
     52  bmi.bmiHeader.biSizeImage = bytes_per_row * size.height();
     53 
     54  std::unique_ptr<SharedMemory> shared_memory;
     55  HANDLE section_handle = nullptr;
     56  if (shared_memory_factory) {
     57    shared_memory = shared_memory_factory->CreateSharedMemory(buffer_size);
     58    if (!shared_memory) {
     59      RTC_LOG(LS_WARNING) << "Failed to allocate shared memory";
     60      return nullptr;
     61    }
     62    section_handle = shared_memory->handle();
     63  }
     64  void* data = nullptr;
     65  HBITMAP bitmap =
     66      CreateDIBSection(hdc, &bmi, DIB_RGB_COLORS, &data, section_handle, 0);
     67  if (!bitmap) {
     68    RTC_LOG(LS_WARNING) << "Failed to allocate new window frame "
     69                        << GetLastError();
     70    return nullptr;
     71  }
     72 
     73  return std::unique_ptr<DesktopFrameWin>(
     74      new DesktopFrameWin(size, bytes_per_row, reinterpret_cast<uint8_t*>(data),
     75                          std::move(shared_memory), bitmap));
     76 }
     77 
     78 }  // namespace webrtc