Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(179)

Side by Side Diff: webrtc/modules/desktop_capture/win/screen_capturer_win_magnifier.cc

Issue 1988783003: Use std::unique_ptr<> to pass frame ownership in DesktopCapturer impls. (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved. 2 * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 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 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 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
(...skipping 21 matching lines...) Expand all
32 // API. The other strings can be anything. 32 // API. The other strings can be anything.
33 static LPCTSTR kMagnifierHostClass = L"ScreenCapturerWinMagnifierHost"; 33 static LPCTSTR kMagnifierHostClass = L"ScreenCapturerWinMagnifierHost";
34 static LPCTSTR kHostWindowName = L"MagnifierHost"; 34 static LPCTSTR kHostWindowName = L"MagnifierHost";
35 static LPCTSTR kMagnifierWindowClass = L"Magnifier"; 35 static LPCTSTR kMagnifierWindowClass = L"Magnifier";
36 static LPCTSTR kMagnifierWindowName = L"MagnifierWindow"; 36 static LPCTSTR kMagnifierWindowName = L"MagnifierWindow";
37 37
38 Atomic32 ScreenCapturerWinMagnifier::tls_index_(TLS_OUT_OF_INDEXES); 38 Atomic32 ScreenCapturerWinMagnifier::tls_index_(TLS_OUT_OF_INDEXES);
39 39
40 ScreenCapturerWinMagnifier::ScreenCapturerWinMagnifier( 40 ScreenCapturerWinMagnifier::ScreenCapturerWinMagnifier(
41 std::unique_ptr<ScreenCapturer> fallback_capturer) 41 std::unique_ptr<ScreenCapturer> fallback_capturer)
42 : fallback_capturer_(std::move(fallback_capturer)), 42 : fallback_capturer_(std::move(fallback_capturer)) {}
43 fallback_capturer_started_(false),
44 callback_(NULL),
45 current_screen_id_(kFullDesktopScreenId),
46 excluded_window_(NULL),
47 set_thread_execution_state_failed_(false),
48 desktop_dc_(NULL),
49 mag_lib_handle_(NULL),
50 mag_initialize_func_(NULL),
51 mag_uninitialize_func_(NULL),
52 set_window_source_func_(NULL),
53 set_window_filter_list_func_(NULL),
54 set_image_scaling_callback_func_(NULL),
55 host_window_(NULL),
56 magnifier_window_(NULL),
57 magnifier_initialized_(false),
58 magnifier_capture_succeeded_(true) {}
59 43
60 ScreenCapturerWinMagnifier::~ScreenCapturerWinMagnifier() { 44 ScreenCapturerWinMagnifier::~ScreenCapturerWinMagnifier() {
61 // DestroyWindow must be called before MagUninitialize. magnifier_window_ is 45 // DestroyWindow must be called before MagUninitialize. magnifier_window_ is
62 // destroyed automatically when host_window_ is destroyed. 46 // destroyed automatically when host_window_ is destroyed.
63 if (host_window_) 47 if (host_window_)
64 DestroyWindow(host_window_); 48 DestroyWindow(host_window_);
65 49
66 if (magnifier_initialized_) 50 if (magnifier_initialized_)
67 mag_uninitialize_func_(); 51 mag_uninitialize_func_();
68 52
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after
155 helper_.InvalidateRegion(region); 139 helper_.InvalidateRegion(region);
156 } else { 140 } else {
157 // No previous frame is available, or the screen is resized. Invalidate the 141 // No previous frame is available, or the screen is resized. Invalidate the
158 // whole screen. 142 // whole screen.
159 helper_.InvalidateScreen(current_frame->size()); 143 helper_.InvalidateScreen(current_frame->size());
160 } 144 }
161 145
162 helper_.set_size_most_recent(current_frame->size()); 146 helper_.set_size_most_recent(current_frame->size());
163 147
164 // Emit the current frame. 148 // Emit the current frame.
165 DesktopFrame* frame = queue_.current_frame()->Share(); 149 std::unique_ptr<DesktopFrame> frame = queue_.current_frame()->Share();
166 frame->set_dpi(DesktopVector(GetDeviceCaps(desktop_dc_, LOGPIXELSX), 150 frame->set_dpi(DesktopVector(GetDeviceCaps(desktop_dc_, LOGPIXELSX),
167 GetDeviceCaps(desktop_dc_, LOGPIXELSY))); 151 GetDeviceCaps(desktop_dc_, LOGPIXELSY)));
168 frame->mutable_updated_region()->Clear(); 152 frame->mutable_updated_region()->Clear();
169 helper_.TakeInvalidRegion(frame->mutable_updated_region()); 153 helper_.TakeInvalidRegion(frame->mutable_updated_region());
170 frame->set_capture_time_ms( 154 frame->set_capture_time_ms((rtc::TimeNanos() - capture_start_time_nanos) /
171 (rtc::TimeNanos() - capture_start_time_nanos) / 155 rtc::kNumNanosecsPerMillisec);
172 rtc::kNumNanosecsPerMillisec); 156 callback_->OnCaptureResult(Result::SUCCESS, std::move(frame));
173 callback_->OnCaptureCompleted(frame);
174 } 157 }
175 158
176 bool ScreenCapturerWinMagnifier::GetScreenList(ScreenList* screens) { 159 bool ScreenCapturerWinMagnifier::GetScreenList(ScreenList* screens) {
177 return webrtc::GetScreenList(screens); 160 return webrtc::GetScreenList(screens);
178 } 161 }
179 162
180 bool ScreenCapturerWinMagnifier::SelectScreen(ScreenId id) { 163 bool ScreenCapturerWinMagnifier::SelectScreen(ScreenId id) {
181 bool valid = IsScreenValid(id, &current_device_key_); 164 bool valid = IsScreenValid(id, &current_device_key_);
182 165
183 // Set current_screen_id_ even if the fallback capturer is being used, so we 166 // Set current_screen_id_ even if the fallback capturer is being used, so we
(...skipping 13 matching lines...) Expand all
197 set_window_filter_list_func_( 180 set_window_filter_list_func_(
198 magnifier_window_, MW_FILTERMODE_EXCLUDE, 1, &excluded_window_); 181 magnifier_window_, MW_FILTERMODE_EXCLUDE, 1, &excluded_window_);
199 } 182 }
200 } 183 }
201 184
202 bool ScreenCapturerWinMagnifier::CaptureImage(const DesktopRect& rect) { 185 bool ScreenCapturerWinMagnifier::CaptureImage(const DesktopRect& rect) {
203 assert(magnifier_initialized_); 186 assert(magnifier_initialized_);
204 187
205 // Set the magnifier control to cover the captured rect. The content of the 188 // Set the magnifier control to cover the captured rect. The content of the
206 // magnifier control will be the captured image. 189 // magnifier control will be the captured image.
207 BOOL result = SetWindowPos(magnifier_window_, 190 BOOL result = SetWindowPos(magnifier_window_, NULL, rect.left(), rect.top(),
208 NULL, 191 rect.width(), rect.height(), 0);
209 rect.left(), rect.top(),
210 rect.width(), rect.height(),
211 0);
212 if (!result) { 192 if (!result) {
213 LOG_F(LS_WARNING) << "Failed to call SetWindowPos: " << GetLastError() 193 LOG_F(LS_WARNING) << "Failed to call SetWindowPos: " << GetLastError()
214 << ". Rect = {" << rect.left() << ", " << rect.top() 194 << ". Rect = {" << rect.left() << ", " << rect.top()
215 << ", " << rect.right() << ", " << rect.bottom() << "}"; 195 << ", " << rect.right() << ", " << rect.bottom() << "}";
216 return false; 196 return false;
217 } 197 }
218 198
219 magnifier_capture_succeeded_ = false; 199 magnifier_capture_succeeded_ = false;
220 200
221 RECT native_rect = {rect.left(), rect.top(), rect.right(), rect.bottom()}; 201 RECT native_rect = {rect.left(), rect.top(), rect.right(), rect.bottom()};
(...skipping 28 matching lines...) Expand all
250 TlsGetValue(tls_index_.Value())); 230 TlsGetValue(tls_index_.Value()));
251 231
252 owner->OnCaptured(srcdata, srcheader); 232 owner->OnCaptured(srcdata, srcheader);
253 233
254 return TRUE; 234 return TRUE;
255 } 235 }
256 236
257 bool ScreenCapturerWinMagnifier::InitializeMagnifier() { 237 bool ScreenCapturerWinMagnifier::InitializeMagnifier() {
258 assert(!magnifier_initialized_); 238 assert(!magnifier_initialized_);
259 239
260 desktop_dc_ = GetDC(NULL); 240 desktop_dc_ = GetDC(nullptr);
261 241
262 mag_lib_handle_ = LoadLibrary(L"Magnification.dll"); 242 mag_lib_handle_ = LoadLibrary(L"Magnification.dll");
263 if (!mag_lib_handle_) 243 if (!mag_lib_handle_)
264 return false; 244 return false;
265 245
266 // Initialize Magnification API function pointers. 246 // Initialize Magnification API function pointers.
267 mag_initialize_func_ = reinterpret_cast<MagInitializeFunc>( 247 mag_initialize_func_ = reinterpret_cast<MagInitializeFunc>(
268 GetProcAddress(mag_lib_handle_, "MagInitialize")); 248 GetProcAddress(mag_lib_handle_, "MagInitialize"));
269 mag_uninitialize_func_ = reinterpret_cast<MagUninitializeFunc>( 249 mag_uninitialize_func_ = reinterpret_cast<MagUninitializeFunc>(
270 GetProcAddress(mag_lib_handle_, "MagUninitialize")); 250 GetProcAddress(mag_lib_handle_, "MagUninitialize"));
(...skipping 13 matching lines...) Expand all
284 return false; 264 return false;
285 } 265 }
286 266
287 BOOL result = mag_initialize_func_(); 267 BOOL result = mag_initialize_func_();
288 if (!result) { 268 if (!result) {
289 LOG_F(LS_WARNING) << "Failed to initialize ScreenCapturerWinMagnifier: " 269 LOG_F(LS_WARNING) << "Failed to initialize ScreenCapturerWinMagnifier: "
290 << "error from MagInitialize " << GetLastError(); 270 << "error from MagInitialize " << GetLastError();
291 return false; 271 return false;
292 } 272 }
293 273
294 HMODULE hInstance = NULL; 274 HMODULE hInstance = nullptr;
295 result = GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | 275 result = GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS |
296 GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, 276 GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
297 reinterpret_cast<char*>(&DefWindowProc), 277 reinterpret_cast<char*>(&DefWindowProc),
298 &hInstance); 278 &hInstance);
299 if (!result) { 279 if (!result) {
300 mag_uninitialize_func_(); 280 mag_uninitialize_func_();
301 LOG_F(LS_WARNING) << "Failed to initialize ScreenCapturerWinMagnifier: " 281 LOG_F(LS_WARNING) << "Failed to initialize ScreenCapturerWinMagnifier: "
302 << "error from GetModulehandleExA " << GetLastError(); 282 << "error from GetModulehandleExA " << GetLastError();
303 return false; 283 return false;
304 } 284 }
305 285
306 // Register the host window class. See the MSDN documentation of the 286 // Register the host window class. See the MSDN documentation of the
307 // Magnification API for more infomation. 287 // Magnification API for more infomation.
308 WNDCLASSEX wcex = {}; 288 WNDCLASSEX wcex = {};
309 wcex.cbSize = sizeof(WNDCLASSEX); 289 wcex.cbSize = sizeof(WNDCLASSEX);
310 wcex.lpfnWndProc = &DefWindowProc; 290 wcex.lpfnWndProc = &DefWindowProc;
311 wcex.hInstance = hInstance; 291 wcex.hInstance = hInstance;
312 wcex.hCursor = LoadCursor(NULL, IDC_ARROW); 292 wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
313 wcex.lpszClassName = kMagnifierHostClass; 293 wcex.lpszClassName = kMagnifierHostClass;
314 294
315 // Ignore the error which may happen when the class is already registered. 295 // Ignore the error which may happen when the class is already registered.
316 RegisterClassEx(&wcex); 296 RegisterClassEx(&wcex);
317 297
318 // Create the host window. 298 // Create the host window.
319 host_window_ = CreateWindowEx(WS_EX_LAYERED, 299 host_window_ =
320 kMagnifierHostClass, 300 CreateWindowEx(WS_EX_LAYERED, kMagnifierHostClass, kHostWindowName, 0, 0,
321 kHostWindowName, 301 0, 0, 0, nullptr, nullptr, hInstance, nullptr);
322 0,
323 0, 0, 0, 0,
324 NULL,
325 NULL,
326 hInstance,
327 NULL);
328 if (!host_window_) { 302 if (!host_window_) {
329 mag_uninitialize_func_(); 303 mag_uninitialize_func_();
330 LOG_F(LS_WARNING) << "Failed to initialize ScreenCapturerWinMagnifier: " 304 LOG_F(LS_WARNING) << "Failed to initialize ScreenCapturerWinMagnifier: "
331 << "error from creating host window " << GetLastError(); 305 << "error from creating host window " << GetLastError();
332 return false; 306 return false;
333 } 307 }
334 308
335 // Create the magnifier control. 309 // Create the magnifier control.
336 magnifier_window_ = CreateWindow(kMagnifierWindowClass, 310 magnifier_window_ = CreateWindow(kMagnifierWindowClass, kMagnifierWindowName,
337 kMagnifierWindowName, 311 WS_CHILD | WS_VISIBLE, 0, 0, 0, 0,
338 WS_CHILD | WS_VISIBLE, 312 host_window_, nullptr, hInstance, nullptr);
339 0, 0, 0, 0,
340 host_window_,
341 NULL,
342 hInstance,
343 NULL);
344 if (!magnifier_window_) { 313 if (!magnifier_window_) {
345 mag_uninitialize_func_(); 314 mag_uninitialize_func_();
346 LOG_F(LS_WARNING) << "Failed to initialize ScreenCapturerWinMagnifier: " 315 LOG_F(LS_WARNING) << "Failed to initialize ScreenCapturerWinMagnifier: "
347 << "error from creating magnifier window " 316 << "error from creating magnifier window "
348 << GetLastError(); 317 << GetLastError();
349 return false; 318 return false;
350 } 319 }
351 320
352 // Hide the host window. 321 // Hide the host window.
353 ShowWindow(host_window_, SW_HIDE); 322 ShowWindow(host_window_, SW_HIDE);
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
426 const DesktopSize& size) { 395 const DesktopSize& size) {
427 // If the current buffer is from an older generation then allocate a new one. 396 // If the current buffer is from an older generation then allocate a new one.
428 // Note that we can't reallocate other buffers at this point, since the caller 397 // Note that we can't reallocate other buffers at this point, since the caller
429 // may still be reading from them. 398 // may still be reading from them.
430 if (!queue_.current_frame() || !queue_.current_frame()->size().equals(size)) { 399 if (!queue_.current_frame() || !queue_.current_frame()->size().equals(size)) {
431 std::unique_ptr<DesktopFrame> frame = 400 std::unique_ptr<DesktopFrame> frame =
432 shared_memory_factory_ 401 shared_memory_factory_
433 ? SharedMemoryDesktopFrame::Create(size, 402 ? SharedMemoryDesktopFrame::Create(size,
434 shared_memory_factory_.get()) 403 shared_memory_factory_.get())
435 : std::unique_ptr<DesktopFrame>(new BasicDesktopFrame(size)); 404 : std::unique_ptr<DesktopFrame>(new BasicDesktopFrame(size));
436 queue_.ReplaceCurrentFrame(SharedDesktopFrame::Wrap(frame.release())); 405 queue_.ReplaceCurrentFrame(SharedDesktopFrame::Wrap(std::move(frame)));
437 } 406 }
438 } 407 }
439 408
440 void ScreenCapturerWinMagnifier::StartFallbackCapturer() { 409 void ScreenCapturerWinMagnifier::StartFallbackCapturer() {
441 assert(fallback_capturer_); 410 assert(fallback_capturer_);
442 if (!fallback_capturer_started_) { 411 if (!fallback_capturer_started_) {
443 fallback_capturer_started_ = true; 412 fallback_capturer_started_ = true;
444 413
445 fallback_capturer_->Start(callback_); 414 fallback_capturer_->Start(callback_);
446 fallback_capturer_->SelectScreen(current_screen_id_); 415 fallback_capturer_->SelectScreen(current_screen_id_);
447 } 416 }
448 } 417 }
449 418
450 } // namespace webrtc 419 } // namespace webrtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698