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.h"
12
13 #include <assert.h>
14 #include <string.h>
15
16 namespace webrtc {
17
DesktopFrame(DesktopSize size,int stride,uint8_t * data,SharedMemory * shared_memory)18 DesktopFrame::DesktopFrame(DesktopSize size,
19 int stride,
20 uint8_t* data,
21 SharedMemory* shared_memory)
22 : size_(size),
23 stride_(stride),
24 data_(data),
25 shared_memory_(shared_memory),
26 capture_time_ms_(0) {
27 }
28
~DesktopFrame()29 DesktopFrame::~DesktopFrame() {}
30
CopyPixelsFrom(uint8_t * src_buffer,int src_stride,const DesktopRect & dest_rect)31 void DesktopFrame::CopyPixelsFrom(uint8_t* src_buffer, int src_stride,
32 const DesktopRect& dest_rect) {
33 assert(DesktopRect::MakeSize(size()).ContainsRect(dest_rect));
34
35 uint8_t* dest = data() + stride() * dest_rect.top() +
36 DesktopFrame::kBytesPerPixel * dest_rect.left();
37 for (int y = 0; y < dest_rect.height(); ++y) {
38 memcpy(dest, src_buffer, DesktopFrame::kBytesPerPixel * dest_rect.width());
39 src_buffer += src_stride;
40 dest += stride();
41 }
42 }
43
CopyPixelsFrom(const DesktopFrame & src_frame,const DesktopVector & src_pos,const DesktopRect & dest_rect)44 void DesktopFrame::CopyPixelsFrom(const DesktopFrame& src_frame,
45 const DesktopVector& src_pos,
46 const DesktopRect& dest_rect) {
47 assert(DesktopRect::MakeSize(src_frame.size()).ContainsRect(
48 DesktopRect::MakeOriginSize(src_pos, dest_rect.size())));
49
50 CopyPixelsFrom(src_frame.data() + src_frame.stride() * src_pos.y() +
51 DesktopFrame::kBytesPerPixel * src_pos.x(),
52 src_frame.stride(), dest_rect);
53 }
54
BasicDesktopFrame(DesktopSize size)55 BasicDesktopFrame::BasicDesktopFrame(DesktopSize size)
56 : DesktopFrame(size, kBytesPerPixel * size.width(),
57 new uint8_t[kBytesPerPixel * size.width() * size.height()],
58 NULL) {
59 }
60
~BasicDesktopFrame()61 BasicDesktopFrame::~BasicDesktopFrame() {
62 delete[] data_;
63 }
64
CopyOf(const DesktopFrame & frame)65 DesktopFrame* BasicDesktopFrame::CopyOf(const DesktopFrame& frame) {
66 DesktopFrame* result = new BasicDesktopFrame(frame.size());
67 for (int y = 0; y < frame.size().height(); ++y) {
68 memcpy(result->data() + y * result->stride(),
69 frame.data() + y * frame.stride(),
70 frame.size().width() * kBytesPerPixel);
71 }
72 result->set_dpi(frame.dpi());
73 result->set_capture_time_ms(frame.capture_time_ms());
74 *result->mutable_updated_region() = frame.updated_region();
75 return result;
76 }
77
78
SharedMemoryDesktopFrame(DesktopSize size,int stride,SharedMemory * shared_memory)79 SharedMemoryDesktopFrame::SharedMemoryDesktopFrame(
80 DesktopSize size,
81 int stride,
82 SharedMemory* shared_memory)
83 : DesktopFrame(size, stride,
84 reinterpret_cast<uint8_t*>(shared_memory->data()),
85 shared_memory) {
86 }
87
~SharedMemoryDesktopFrame()88 SharedMemoryDesktopFrame::~SharedMemoryDesktopFrame() {
89 delete shared_memory_;
90 }
91
92 } // namespace webrtc
93