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 = GetFrameDataAtPos(dest_rect.top_left());
36 for (int y = 0; y < dest_rect.height(); ++y) {
37 memcpy(dest, src_buffer, DesktopFrame::kBytesPerPixel * dest_rect.width());
38 src_buffer += src_stride;
39 dest += stride();
40 }
41 }
42
CopyPixelsFrom(const DesktopFrame & src_frame,const DesktopVector & src_pos,const DesktopRect & dest_rect)43 void DesktopFrame::CopyPixelsFrom(const DesktopFrame& src_frame,
44 const DesktopVector& src_pos,
45 const DesktopRect& dest_rect) {
46 assert(DesktopRect::MakeSize(src_frame.size()).ContainsRect(
47 DesktopRect::MakeOriginSize(src_pos, dest_rect.size())));
48
49 CopyPixelsFrom(src_frame.GetFrameDataAtPos(src_pos),
50 src_frame.stride(), dest_rect);
51 }
52
GetFrameDataAtPos(const DesktopVector & pos) const53 uint8_t* DesktopFrame::GetFrameDataAtPos(const DesktopVector& pos) const {
54 return data() + stride() * pos.y() + DesktopFrame::kBytesPerPixel * pos.x();
55 }
56
BasicDesktopFrame(DesktopSize size)57 BasicDesktopFrame::BasicDesktopFrame(DesktopSize size)
58 : DesktopFrame(size, kBytesPerPixel * size.width(),
59 new uint8_t[kBytesPerPixel * size.width() * size.height()],
60 NULL) {
61 }
62
~BasicDesktopFrame()63 BasicDesktopFrame::~BasicDesktopFrame() {
64 delete[] data_;
65 }
66
CopyOf(const DesktopFrame & frame)67 DesktopFrame* BasicDesktopFrame::CopyOf(const DesktopFrame& frame) {
68 DesktopFrame* result = new BasicDesktopFrame(frame.size());
69 for (int y = 0; y < frame.size().height(); ++y) {
70 memcpy(result->data() + y * result->stride(),
71 frame.data() + y * frame.stride(),
72 frame.size().width() * kBytesPerPixel);
73 }
74 result->set_dpi(frame.dpi());
75 result->set_capture_time_ms(frame.capture_time_ms());
76 *result->mutable_updated_region() = frame.updated_region();
77 return result;
78 }
79
80
SharedMemoryDesktopFrame(DesktopSize size,int stride,SharedMemory * shared_memory)81 SharedMemoryDesktopFrame::SharedMemoryDesktopFrame(
82 DesktopSize size,
83 int stride,
84 SharedMemory* shared_memory)
85 : DesktopFrame(size, stride,
86 reinterpret_cast<uint8_t*>(shared_memory->data()),
87 shared_memory) {
88 }
89
~SharedMemoryDesktopFrame()90 SharedMemoryDesktopFrame::~SharedMemoryDesktopFrame() {
91 delete shared_memory_;
92 }
93
94 } // namespace webrtc
95