OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "remoting/host/capturer.h" | 5 #include "remoting/host/capturer.h" |
6 | 6 |
7 #include <windows.h> | 7 #include <windows.h> |
8 | 8 |
9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
10 #include "remoting/host/capturer_helper.h" | 10 #include "remoting/host/capturer_helper.h" |
(...skipping 10 matching lines...) Expand all Loading... |
21 class CapturerGdi : public Capturer { | 21 class CapturerGdi : public Capturer { |
22 public: | 22 public: |
23 CapturerGdi(); | 23 CapturerGdi(); |
24 virtual ~CapturerGdi(); | 24 virtual ~CapturerGdi(); |
25 | 25 |
26 // Capturer interface. | 26 // Capturer interface. |
27 virtual void ScreenConfigurationChanged() OVERRIDE; | 27 virtual void ScreenConfigurationChanged() OVERRIDE; |
28 virtual media::VideoFrame::Format pixel_format() const OVERRIDE; | 28 virtual media::VideoFrame::Format pixel_format() const OVERRIDE; |
29 virtual void ClearInvalidRegion() OVERRIDE; | 29 virtual void ClearInvalidRegion() OVERRIDE; |
30 virtual void InvalidateRegion(const SkRegion& invalid_region) OVERRIDE; | 30 virtual void InvalidateRegion(const SkRegion& invalid_region) OVERRIDE; |
31 virtual void InvalidateScreen(const gfx::Size& size) OVERRIDE; | 31 virtual void InvalidateScreen(const SkISize& size) OVERRIDE; |
32 virtual void InvalidateFullScreen() OVERRIDE; | 32 virtual void InvalidateFullScreen() OVERRIDE; |
33 virtual void CaptureInvalidRegion(CaptureCompletedCallback* callback) | 33 virtual void CaptureInvalidRegion(CaptureCompletedCallback* callback) |
34 OVERRIDE; | 34 OVERRIDE; |
35 virtual const gfx::Size& size_most_recent() const OVERRIDE; | 35 virtual const SkISize& size_most_recent() const OVERRIDE; |
36 | 36 |
37 private: | 37 private: |
38 struct VideoFrameBuffer { | 38 struct VideoFrameBuffer { |
39 VideoFrameBuffer(void* data, const gfx::Size& size, int bytes_per_pixel, | 39 VideoFrameBuffer(void* data, const SkISize& size, int bytes_per_pixel, |
40 int bytes_per_row) | 40 int bytes_per_row) |
41 : data(data), size(size), bytes_per_pixel(bytes_per_pixel), | 41 : data(data), size(size), bytes_per_pixel(bytes_per_pixel), |
42 bytes_per_row(bytes_per_row) { | 42 bytes_per_row(bytes_per_row) { |
43 } | 43 } |
44 VideoFrameBuffer() { | 44 VideoFrameBuffer() { |
45 data = 0; | 45 data = 0; |
46 size = gfx::Size(0, 0); | 46 size = SkISize::Make(0, 0); |
47 bytes_per_pixel = 0; | 47 bytes_per_pixel = 0; |
48 bytes_per_row = 0; | 48 bytes_per_row = 0; |
49 } | 49 } |
50 void* data; | 50 void* data; |
51 gfx::Size size; | 51 SkISize size; |
52 int bytes_per_pixel; | 52 int bytes_per_pixel; |
53 int bytes_per_row; | 53 int bytes_per_row; |
54 }; | 54 }; |
55 | 55 |
56 // Make sure that the current buffer has the same size as the screen. | 56 // Make sure that the current buffer has the same size as the screen. |
57 void UpdateBufferCapture(const gfx::Size& size); | 57 void UpdateBufferCapture(const SkISize& size); |
58 | 58 |
59 // Allocate memory for a buffer of a given size, freeing any memory previously | 59 // Allocate memory for a buffer of a given size, freeing any memory previously |
60 // allocated for that buffer. | 60 // allocated for that buffer. |
61 void ReallocateBuffer(int buffer_index, const gfx::Size& size); | 61 void ReallocateBuffer(int buffer_index, const SkISize& size); |
62 | 62 |
63 void CalculateInvalidRegion(); | 63 void CalculateInvalidRegion(); |
64 void CaptureRegion(const SkRegion& region, | 64 void CaptureRegion(const SkRegion& region, |
65 CaptureCompletedCallback* callback); | 65 CaptureCompletedCallback* callback); |
66 | 66 |
67 void ReleaseBuffers(); | 67 void ReleaseBuffers(); |
68 // Generates an image in the current buffer. | 68 // Generates an image in the current buffer. |
69 void CaptureImage(); | 69 void CaptureImage(); |
70 | 70 |
71 // Gets the current screen size and calls ScreenConfigurationChanged | 71 // Gets the current screen size and calls ScreenConfigurationChanged |
72 // if the screen size has changed. | 72 // if the screen size has changed. |
73 void MaybeChangeScreenConfiguration(); | 73 void MaybeChangeScreenConfiguration(); |
74 | 74 |
75 // Gets the screen size. | 75 // Gets the screen size. |
76 gfx::Size GetScreenSize(); | 76 SkISize GetScreenSize(); |
77 | 77 |
78 // A thread-safe list of invalid rectangles, and the size of the most | 78 // A thread-safe list of invalid rectangles, and the size of the most |
79 // recently captured screen. | 79 // recently captured screen. |
80 CapturerHelper helper_; | 80 CapturerHelper helper_; |
81 | 81 |
82 // There are two buffers for the screen images, as required by Capturer. | 82 // There are two buffers for the screen images, as required by Capturer. |
83 static const int kNumBuffers = 2; | 83 static const int kNumBuffers = 2; |
84 VideoFrameBuffer buffers_[kNumBuffers]; | 84 VideoFrameBuffer buffers_[kNumBuffers]; |
85 | 85 |
86 // Gdi specific information about screen. | 86 // Gdi specific information about screen. |
87 HDC desktop_dc_; | 87 HDC desktop_dc_; |
88 HDC memory_dc_; | 88 HDC memory_dc_; |
89 HBITMAP target_bitmap_[kNumBuffers]; | 89 HBITMAP target_bitmap_[kNumBuffers]; |
90 | 90 |
91 // The screen size attached to the device contexts through which the screen | 91 // The screen size attached to the device contexts through which the screen |
92 // is captured. | 92 // is captured. |
93 gfx::Size dc_size_; | 93 SkISize dc_size_; |
94 | 94 |
95 // The current buffer with valid data for reading. | 95 // The current buffer with valid data for reading. |
96 int current_buffer_; | 96 int current_buffer_; |
97 | 97 |
98 // Format of pixels returned in buffer. | 98 // Format of pixels returned in buffer. |
99 media::VideoFrame::Format pixel_format_; | 99 media::VideoFrame::Format pixel_format_; |
100 | 100 |
101 // Class to calculate the difference between two screen bitmaps. | 101 // Class to calculate the difference between two screen bitmaps. |
102 scoped_ptr<Differ> differ_; | 102 scoped_ptr<Differ> differ_; |
103 | 103 |
104 DISALLOW_COPY_AND_ASSIGN(CapturerGdi); | 104 DISALLOW_COPY_AND_ASSIGN(CapturerGdi); |
105 }; | 105 }; |
106 | 106 |
107 // 3780 pixels per meter is equivalent to 96 DPI, typical on desktop monitors. | 107 // 3780 pixels per meter is equivalent to 96 DPI, typical on desktop monitors. |
108 static const int kPixelsPerMeter = 3780; | 108 static const int kPixelsPerMeter = 3780; |
109 // 32 bit RGBA is 4 bytes per pixel. | 109 // 32 bit RGBA is 4 bytes per pixel. |
110 static const int kBytesPerPixel = 4; | 110 static const int kBytesPerPixel = 4; |
111 | 111 |
112 CapturerGdi::CapturerGdi() | 112 CapturerGdi::CapturerGdi() |
113 : desktop_dc_(NULL), | 113 : desktop_dc_(NULL), |
114 memory_dc_(NULL), | 114 memory_dc_(NULL), |
115 dc_size_(0, 0), | 115 dc_size_(SkISize::Make(0, 0)), |
116 current_buffer_(0), | 116 current_buffer_(0), |
117 pixel_format_(media::VideoFrame::RGB32) { | 117 pixel_format_(media::VideoFrame::RGB32) { |
118 memset(target_bitmap_, 0, sizeof(target_bitmap_)); | 118 memset(target_bitmap_, 0, sizeof(target_bitmap_)); |
119 memset(buffers_, 0, sizeof(buffers_)); | 119 memset(buffers_, 0, sizeof(buffers_)); |
120 ScreenConfigurationChanged(); | 120 ScreenConfigurationChanged(); |
121 } | 121 } |
122 | 122 |
123 CapturerGdi::~CapturerGdi() { | 123 CapturerGdi::~CapturerGdi() { |
124 ReleaseBuffers(); | 124 ReleaseBuffers(); |
125 } | 125 } |
126 | 126 |
127 media::VideoFrame::Format CapturerGdi::pixel_format() const { | 127 media::VideoFrame::Format CapturerGdi::pixel_format() const { |
128 return pixel_format_; | 128 return pixel_format_; |
129 } | 129 } |
130 | 130 |
131 void CapturerGdi::ClearInvalidRegion() { | 131 void CapturerGdi::ClearInvalidRegion() { |
132 helper_.ClearInvalidRegion(); | 132 helper_.ClearInvalidRegion(); |
133 } | 133 } |
134 | 134 |
135 void CapturerGdi::InvalidateRegion(const SkRegion& invalid_region) { | 135 void CapturerGdi::InvalidateRegion(const SkRegion& invalid_region) { |
136 helper_.InvalidateRegion(invalid_region); | 136 helper_.InvalidateRegion(invalid_region); |
137 } | 137 } |
138 | 138 |
139 void CapturerGdi::InvalidateScreen(const gfx::Size& size) { | 139 void CapturerGdi::InvalidateScreen(const SkISize& size) { |
140 helper_.InvalidateScreen(size); | 140 helper_.InvalidateScreen(size); |
141 } | 141 } |
142 | 142 |
143 void CapturerGdi::InvalidateFullScreen() { | 143 void CapturerGdi::InvalidateFullScreen() { |
144 helper_.InvalidateFullScreen(); | 144 helper_.InvalidateFullScreen(); |
145 } | 145 } |
146 | 146 |
147 void CapturerGdi::CaptureInvalidRegion(CaptureCompletedCallback* callback) { | 147 void CapturerGdi::CaptureInvalidRegion(CaptureCompletedCallback* callback) { |
148 CalculateInvalidRegion(); | 148 CalculateInvalidRegion(); |
149 SkRegion invalid_region; | 149 SkRegion invalid_region; |
150 helper_.SwapInvalidRegion(&invalid_region); | 150 helper_.SwapInvalidRegion(&invalid_region); |
151 CaptureRegion(invalid_region, callback); | 151 CaptureRegion(invalid_region, callback); |
152 } | 152 } |
153 | 153 |
154 const gfx::Size& CapturerGdi::size_most_recent() const { | 154 const SkISize& CapturerGdi::size_most_recent() const { |
155 return helper_.size_most_recent(); | 155 return helper_.size_most_recent(); |
156 } | 156 } |
157 | 157 |
158 void CapturerGdi::ReleaseBuffers() { | 158 void CapturerGdi::ReleaseBuffers() { |
159 for (int i = kNumBuffers - 1; i >= 0; i--) { | 159 for (int i = kNumBuffers - 1; i >= 0; i--) { |
160 if (target_bitmap_[i]) { | 160 if (target_bitmap_[i]) { |
161 DeleteObject(target_bitmap_[i]); | 161 DeleteObject(target_bitmap_[i]); |
162 target_bitmap_[i] = NULL; | 162 target_bitmap_[i] = NULL; |
163 } | 163 } |
164 if (buffers_[i].data) { | 164 if (buffers_[i].data) { |
165 DeleteObject(buffers_[i].data); | 165 DeleteObject(buffers_[i].data); |
166 buffers_[i].data = NULL; | 166 buffers_[i].data = NULL; |
167 } | 167 } |
168 } | 168 } |
169 | 169 |
170 desktop_dc_ = NULL; | 170 desktop_dc_ = NULL; |
171 if (memory_dc_) { | 171 if (memory_dc_) { |
172 DeleteDC(memory_dc_); | 172 DeleteDC(memory_dc_); |
173 memory_dc_ = NULL; | 173 memory_dc_ = NULL; |
174 } | 174 } |
175 } | 175 } |
176 | 176 |
177 void CapturerGdi::ScreenConfigurationChanged() { | 177 void CapturerGdi::ScreenConfigurationChanged() { |
178 // We poll for screen configuration changes, so ignore notifications. | 178 // We poll for screen configuration changes, so ignore notifications. |
179 } | 179 } |
180 | 180 |
181 void CapturerGdi::UpdateBufferCapture(const gfx::Size& size) { | 181 void CapturerGdi::UpdateBufferCapture(const SkISize& size) { |
182 // Make sure the DCs have the correct dimensions. | 182 // Make sure the DCs have the correct dimensions. |
183 if (size != dc_size_) { | 183 if (size != dc_size_) { |
184 // TODO(simonmorris): screen dimensions changing isn't equivalent to needing | 184 // TODO(simonmorris): screen dimensions changing isn't equivalent to needing |
185 // a new DC, but it's good enough for now. | 185 // a new DC, but it's good enough for now. |
186 desktop_dc_ = GetDC(GetDesktopWindow()); | 186 desktop_dc_ = GetDC(GetDesktopWindow()); |
187 if (memory_dc_) | 187 if (memory_dc_) |
188 DeleteDC(memory_dc_); | 188 DeleteDC(memory_dc_); |
189 memory_dc_ = CreateCompatibleDC(desktop_dc_); | 189 memory_dc_ = CreateCompatibleDC(desktop_dc_); |
190 dc_size_ = size; | 190 dc_size_ = size; |
191 } | 191 } |
192 | 192 |
193 // Make sure the current bitmap has the correct dimensions. | 193 // Make sure the current bitmap has the correct dimensions. |
194 if (size != buffers_[current_buffer_].size) { | 194 if (size != buffers_[current_buffer_].size) { |
195 ReallocateBuffer(current_buffer_, size); | 195 ReallocateBuffer(current_buffer_, size); |
196 InvalidateFullScreen(); | 196 InvalidateFullScreen(); |
197 } | 197 } |
198 } | 198 } |
199 | 199 |
200 void CapturerGdi::ReallocateBuffer(int buffer_index, const gfx::Size& size) { | 200 void CapturerGdi::ReallocateBuffer(int buffer_index, const SkISize& size) { |
201 // Delete any previously constructed bitmap. | 201 // Delete any previously constructed bitmap. |
202 if (target_bitmap_[buffer_index]) { | 202 if (target_bitmap_[buffer_index]) { |
203 DeleteObject(target_bitmap_[buffer_index]); | 203 DeleteObject(target_bitmap_[buffer_index]); |
204 target_bitmap_[buffer_index] = NULL; | 204 target_bitmap_[buffer_index] = NULL; |
205 } | 205 } |
206 if (buffers_[buffer_index].data) { | 206 if (buffers_[buffer_index].data) { |
207 DeleteObject(buffers_[buffer_index].data); | 207 DeleteObject(buffers_[buffer_index].data); |
208 buffers_[buffer_index].data = NULL; | 208 buffers_[buffer_index].data = NULL; |
209 } | 209 } |
210 | 210 |
(...skipping 14 matching lines...) Expand all Loading... |
225 bmi.bmiHeader.biSize = sizeof(bmi.bmiHeader); | 225 bmi.bmiHeader.biSize = sizeof(bmi.bmiHeader); |
226 bmi.bmiHeader.biSizeImage = bytes_per_row * size.height(); | 226 bmi.bmiHeader.biSizeImage = bytes_per_row * size.height(); |
227 bmi.bmiHeader.biXPelsPerMeter = kPixelsPerMeter; | 227 bmi.bmiHeader.biXPelsPerMeter = kPixelsPerMeter; |
228 bmi.bmiHeader.biYPelsPerMeter = kPixelsPerMeter; | 228 bmi.bmiHeader.biYPelsPerMeter = kPixelsPerMeter; |
229 | 229 |
230 // Create memory for the buffers. | 230 // Create memory for the buffers. |
231 target_bitmap_[buffer_index] = | 231 target_bitmap_[buffer_index] = |
232 CreateDIBSection(desktop_dc_, &bmi, DIB_RGB_COLORS, | 232 CreateDIBSection(desktop_dc_, &bmi, DIB_RGB_COLORS, |
233 static_cast<void**>(&buffers_[buffer_index].data), | 233 static_cast<void**>(&buffers_[buffer_index].data), |
234 NULL, 0); | 234 NULL, 0); |
235 buffers_[buffer_index].size = gfx::Size(bmi.bmiHeader.biWidth, | 235 buffers_[buffer_index].size = SkISize::Make(bmi.bmiHeader.biWidth, |
236 std::abs(bmi.bmiHeader.biHeight)); | 236 std::abs(bmi.bmiHeader.biHeight)); |
237 buffers_[buffer_index].bytes_per_pixel = bmi.bmiHeader.biBitCount / 8; | 237 buffers_[buffer_index].bytes_per_pixel = bmi.bmiHeader.biBitCount / 8; |
238 buffers_[buffer_index].bytes_per_row = | 238 buffers_[buffer_index].bytes_per_row = |
239 bmi.bmiHeader.biSizeImage / std::abs(bmi.bmiHeader.biHeight); | 239 bmi.bmiHeader.biSizeImage / std::abs(bmi.bmiHeader.biHeight); |
240 } | 240 } |
241 | 241 |
242 void CapturerGdi::CalculateInvalidRegion() { | 242 void CapturerGdi::CalculateInvalidRegion() { |
243 CaptureImage(); | 243 CaptureImage(); |
244 | 244 |
245 const VideoFrameBuffer& current = buffers_[current_buffer_]; | 245 const VideoFrameBuffer& current = buffers_[current_buffer_]; |
246 | 246 |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
303 | 303 |
304 // Select the target bitmap into the memory dc. | 304 // Select the target bitmap into the memory dc. |
305 SelectObject(memory_dc_, target_bitmap_[current_buffer_]); | 305 SelectObject(memory_dc_, target_bitmap_[current_buffer_]); |
306 | 306 |
307 // And then copy the rect from desktop to memory. | 307 // And then copy the rect from desktop to memory. |
308 BitBlt(memory_dc_, 0, 0, buffers_[current_buffer_].size.width(), | 308 BitBlt(memory_dc_, 0, 0, buffers_[current_buffer_].size.width(), |
309 buffers_[current_buffer_].size.height(), desktop_dc_, 0, 0, | 309 buffers_[current_buffer_].size.height(), desktop_dc_, 0, 0, |
310 SRCCOPY | CAPTUREBLT); | 310 SRCCOPY | CAPTUREBLT); |
311 } | 311 } |
312 | 312 |
313 gfx::Size CapturerGdi::GetScreenSize() { | 313 SkISize CapturerGdi::GetScreenSize() { |
314 return gfx::Size(GetSystemMetrics(SM_CXSCREEN), | 314 return SkISize::Make(GetSystemMetrics(SM_CXSCREEN), |
315 GetSystemMetrics(SM_CYSCREEN)); | 315 GetSystemMetrics(SM_CYSCREEN)); |
316 } | 316 } |
317 | 317 |
318 } // namespace | 318 } // namespace |
319 | 319 |
320 // static | 320 // static |
321 Capturer* Capturer::Create() { | 321 Capturer* Capturer::Create() { |
322 return new CapturerGdi(); | 322 return new CapturerGdi(); |
323 } | 323 } |
324 | 324 |
325 } // namespace remoting | 325 } // namespace remoting |
OLD | NEW |