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.

204 lines
6.8 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  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. #include "modules/desktop_capture/desktop_frame.h"
  11. #include <string.h>
  12. #include <cmath>
  13. #include <memory>
  14. #include <utility>
  15. #include "modules/desktop_capture/desktop_capture_types.h"
  16. #include "modules/desktop_capture/desktop_geometry.h"
  17. #include "rtc_base/checks.h"
  18. namespace webrtc {
  19. DesktopFrame::DesktopFrame(DesktopSize size,
  20. int stride,
  21. uint8_t* data,
  22. SharedMemory* shared_memory)
  23. : data_(data),
  24. shared_memory_(shared_memory),
  25. size_(size),
  26. stride_(stride),
  27. capture_time_ms_(0),
  28. capturer_id_(DesktopCapturerId::kUnknown) {
  29. RTC_DCHECK(size_.width() >= 0);
  30. RTC_DCHECK(size_.height() >= 0);
  31. }
  32. DesktopFrame::~DesktopFrame() = default;
  33. void DesktopFrame::CopyPixelsFrom(const uint8_t* src_buffer,
  34. int src_stride,
  35. const DesktopRect& dest_rect) {
  36. RTC_CHECK(DesktopRect::MakeSize(size()).ContainsRect(dest_rect));
  37. uint8_t* dest = GetFrameDataAtPos(dest_rect.top_left());
  38. for (int y = 0; y < dest_rect.height(); ++y) {
  39. memcpy(dest, src_buffer, DesktopFrame::kBytesPerPixel * dest_rect.width());
  40. src_buffer += src_stride;
  41. dest += stride();
  42. }
  43. }
  44. void DesktopFrame::CopyPixelsFrom(const DesktopFrame& src_frame,
  45. const DesktopVector& src_pos,
  46. const DesktopRect& dest_rect) {
  47. RTC_CHECK(DesktopRect::MakeSize(src_frame.size())
  48. .ContainsRect(
  49. DesktopRect::MakeOriginSize(src_pos, dest_rect.size())));
  50. CopyPixelsFrom(src_frame.GetFrameDataAtPos(src_pos), src_frame.stride(),
  51. dest_rect);
  52. }
  53. bool DesktopFrame::CopyIntersectingPixelsFrom(const DesktopFrame& src_frame,
  54. double horizontal_scale,
  55. double vertical_scale) {
  56. const DesktopVector& origin = top_left();
  57. const DesktopVector& src_frame_origin = src_frame.top_left();
  58. DesktopVector src_frame_offset = src_frame_origin.subtract(origin);
  59. // Determine the intersection, first adjusting its origin to account for any
  60. // DPI scaling.
  61. DesktopRect intersection_rect = src_frame.rect();
  62. if (horizontal_scale != 1.0 || vertical_scale != 1.0) {
  63. DesktopVector origin_adjustment(
  64. static_cast<int>(
  65. std::round((horizontal_scale - 1.0) * src_frame_offset.x())),
  66. static_cast<int>(
  67. std::round((vertical_scale - 1.0) * src_frame_offset.y())));
  68. intersection_rect.Translate(origin_adjustment);
  69. src_frame_offset = src_frame_offset.add(origin_adjustment);
  70. }
  71. intersection_rect.IntersectWith(rect());
  72. if (intersection_rect.is_empty()) {
  73. return false;
  74. }
  75. // Translate the intersection rect to be relative to the outer rect.
  76. intersection_rect.Translate(-origin.x(), -origin.y());
  77. // Determine source position for the copy (offsets of outer frame from
  78. // source origin, if positive).
  79. int32_t src_pos_x = std::max(0, -src_frame_offset.x());
  80. int32_t src_pos_y = std::max(0, -src_frame_offset.y());
  81. CopyPixelsFrom(src_frame, DesktopVector(src_pos_x, src_pos_y),
  82. intersection_rect);
  83. return true;
  84. }
  85. DesktopRect DesktopFrame::rect() const {
  86. const float scale = scale_factor();
  87. // Only scale the size.
  88. return DesktopRect::MakeXYWH(top_left().x(), top_left().y(),
  89. size().width() / scale, size().height() / scale);
  90. }
  91. float DesktopFrame::scale_factor() const {
  92. float scale = 1.0f;
  93. #if defined(WEBRTC_MAC)
  94. // At least on Windows the logical and physical pixel are the same
  95. // See http://crbug.com/948362.
  96. if (!dpi().is_zero() && dpi().x() == dpi().y())
  97. scale = dpi().x() / kStandardDPI;
  98. #endif
  99. return scale;
  100. }
  101. uint8_t* DesktopFrame::GetFrameDataAtPos(const DesktopVector& pos) const {
  102. return data() + stride() * pos.y() + DesktopFrame::kBytesPerPixel * pos.x();
  103. }
  104. void DesktopFrame::CopyFrameInfoFrom(const DesktopFrame& other) {
  105. set_dpi(other.dpi());
  106. set_capture_time_ms(other.capture_time_ms());
  107. set_capturer_id(other.capturer_id());
  108. *mutable_updated_region() = other.updated_region();
  109. set_top_left(other.top_left());
  110. set_icc_profile(other.icc_profile());
  111. }
  112. void DesktopFrame::MoveFrameInfoFrom(DesktopFrame* other) {
  113. set_dpi(other->dpi());
  114. set_capture_time_ms(other->capture_time_ms());
  115. set_capturer_id(other->capturer_id());
  116. mutable_updated_region()->Swap(other->mutable_updated_region());
  117. set_top_left(other->top_left());
  118. set_icc_profile(other->icc_profile());
  119. }
  120. BasicDesktopFrame::BasicDesktopFrame(DesktopSize size)
  121. : DesktopFrame(size,
  122. kBytesPerPixel * size.width(),
  123. new uint8_t[kBytesPerPixel * size.width() * size.height()](),
  124. nullptr) {}
  125. BasicDesktopFrame::~BasicDesktopFrame() {
  126. delete[] data_;
  127. }
  128. // static
  129. DesktopFrame* BasicDesktopFrame::CopyOf(const DesktopFrame& frame) {
  130. DesktopFrame* result = new BasicDesktopFrame(frame.size());
  131. for (int y = 0; y < frame.size().height(); ++y) {
  132. memcpy(result->data() + y * result->stride(),
  133. frame.data() + y * frame.stride(),
  134. frame.size().width() * kBytesPerPixel);
  135. }
  136. result->CopyFrameInfoFrom(frame);
  137. return result;
  138. }
  139. // static
  140. std::unique_ptr<DesktopFrame> SharedMemoryDesktopFrame::Create(
  141. DesktopSize size,
  142. SharedMemoryFactory* shared_memory_factory) {
  143. RTC_DCHECK(shared_memory_factory);
  144. size_t buffer_size = size.height() * size.width() * kBytesPerPixel;
  145. std::unique_ptr<SharedMemory> shared_memory =
  146. shared_memory_factory->CreateSharedMemory(buffer_size);
  147. if (!shared_memory)
  148. return nullptr;
  149. return std::make_unique<SharedMemoryDesktopFrame>(
  150. size, size.width() * kBytesPerPixel, std::move(shared_memory));
  151. }
  152. SharedMemoryDesktopFrame::SharedMemoryDesktopFrame(DesktopSize size,
  153. int stride,
  154. SharedMemory* shared_memory)
  155. : DesktopFrame(size,
  156. stride,
  157. reinterpret_cast<uint8_t*>(shared_memory->data()),
  158. shared_memory) {}
  159. SharedMemoryDesktopFrame::SharedMemoryDesktopFrame(
  160. DesktopSize size,
  161. int stride,
  162. std::unique_ptr<SharedMemory> shared_memory)
  163. : SharedMemoryDesktopFrame(size, stride, shared_memory.release()) {}
  164. SharedMemoryDesktopFrame::~SharedMemoryDesktopFrame() {
  165. delete shared_memory_;
  166. }
  167. } // namespace webrtc