| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "ui/snapshot/snapshot.h" | 5 #include "ui/snapshot/snapshot.h" |
| 6 | 6 |
| 7 #import <Cocoa/Cocoa.h> | 7 #import <Cocoa/Cocoa.h> |
| 8 | 8 |
| 9 #include "base/callback.h" | 9 #include "base/callback.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/mac/scoped_cftyperef.h" | 11 #include "base/mac/scoped_cftyperef.h" |
| 12 #include "base/mac/scoped_nsobject.h" |
| 12 #include "base/mac/sdk_forward_declarations.h" | 13 #include "base/mac/sdk_forward_declarations.h" |
| 13 #include "base/task_runner.h" | 14 #include "base/task_runner.h" |
| 14 #include "ui/gfx/geometry/rect.h" | 15 #include "ui/gfx/geometry/rect.h" |
| 15 #include "ui/gfx/image/image.h" | 16 #include "ui/gfx/image/image.h" |
| 16 | 17 |
| 17 namespace ui { | 18 namespace ui { |
| 18 | 19 |
| 19 bool GrabViewSnapshot(gfx::NativeView view, | 20 bool GrabViewSnapshot(gfx::NativeView view, |
| 20 const gfx::Rect& snapshot_bounds, | 21 std::vector<unsigned char>* png_representation, |
| 21 gfx::Image* image) { | 22 const gfx::Rect& snapshot_bounds) { |
| 22 NSWindow* window = [view window]; | 23 NSWindow* window = [view window]; |
| 23 NSScreen* screen = [[NSScreen screens] firstObject]; | 24 NSScreen* screen = [[NSScreen screens] firstObject]; |
| 24 gfx::Rect screen_bounds = gfx::Rect(NSRectToCGRect([screen frame])); | 25 gfx::Rect screen_bounds = gfx::Rect(NSRectToCGRect([screen frame])); |
| 25 | 26 |
| 26 | 27 |
| 27 // Get the view bounds relative to the screen | 28 // Get the view bounds relative to the screen |
| 28 NSRect frame = [view convertRect:[view bounds] toView:nil]; | 29 NSRect frame = [view convertRect:[view bounds] toView:nil]; |
| 29 frame = [window convertRectToScreen:frame]; | 30 frame = [window convertRectToScreen:frame]; |
| 30 | 31 |
| 31 gfx::Rect view_bounds = gfx::Rect(NSRectToCGRect(frame)); | 32 gfx::Rect view_bounds = gfx::Rect(NSRectToCGRect(frame)); |
| 32 | 33 |
| 33 // Flip window coordinates based on the primary screen. | 34 // Flip window coordinates based on the primary screen. |
| 34 view_bounds.set_y( | 35 view_bounds.set_y( |
| 35 screen_bounds.height() - view_bounds.y() - view_bounds.height()); | 36 screen_bounds.height() - view_bounds.y() - view_bounds.height()); |
| 36 | 37 |
| 37 // Convert snapshot bounds relative to window into bounds relative to | 38 // Convert snapshot bounds relative to window into bounds relative to |
| 38 // screen. | 39 // screen. |
| 39 gfx::Rect screen_snapshot_bounds = snapshot_bounds; | 40 gfx::Rect screen_snapshot_bounds = snapshot_bounds; |
| 40 screen_snapshot_bounds.Offset(view_bounds.OffsetFromOrigin()); | 41 screen_snapshot_bounds.Offset(view_bounds.OffsetFromOrigin()); |
| 41 | 42 |
| 42 DCHECK_LE(screen_snapshot_bounds.right(), view_bounds.right()); | 43 DCHECK_LE(screen_snapshot_bounds.right(), view_bounds.right()); |
| 43 DCHECK_LE(screen_snapshot_bounds.bottom(), view_bounds.bottom()); | 44 DCHECK_LE(screen_snapshot_bounds.bottom(), view_bounds.bottom()); |
| 44 | 45 |
| 46 png_representation->clear(); |
| 47 |
| 45 base::ScopedCFTypeRef<CGImageRef> windowSnapshot( | 48 base::ScopedCFTypeRef<CGImageRef> windowSnapshot( |
| 46 CGWindowListCreateImage(screen_snapshot_bounds.ToCGRect(), | 49 CGWindowListCreateImage(screen_snapshot_bounds.ToCGRect(), |
| 47 kCGWindowListOptionIncludingWindow, | 50 kCGWindowListOptionIncludingWindow, |
| 48 [window windowNumber], | 51 [window windowNumber], |
| 49 kCGWindowImageBoundsIgnoreFraming)); | 52 kCGWindowImageBoundsIgnoreFraming)); |
| 50 if (CGImageGetWidth(windowSnapshot) <= 0) | 53 if (CGImageGetWidth(windowSnapshot) <= 0) |
| 51 return false; | 54 return false; |
| 52 | 55 |
| 53 NSImage* nsImage = | 56 base::scoped_nsobject<NSBitmapImageRep> rep( |
| 54 [[NSImage alloc] initWithCGImage:windowSnapshot size:NSZeroSize]; | 57 [[NSBitmapImageRep alloc] initWithCGImage:windowSnapshot]); |
| 55 *image = gfx::Image(nsImage); | 58 NSData* data = [rep representationUsingType:NSPNGFileType properties:@{}]; |
| 59 const unsigned char* buf = static_cast<const unsigned char*>([data bytes]); |
| 60 NSUInteger length = [data length]; |
| 61 if (buf == NULL || length == 0) |
| 62 return false; |
| 63 |
| 64 png_representation->assign(buf, buf + length); |
| 65 DCHECK(!png_representation->empty()); |
| 66 |
| 56 return true; | 67 return true; |
| 57 } | 68 } |
| 58 | 69 |
| 59 bool GrabWindowSnapshot(gfx::NativeWindow window, | 70 bool GrabWindowSnapshot(gfx::NativeWindow window, |
| 60 const gfx::Rect& snapshot_bounds, | 71 std::vector<unsigned char>* png_representation, |
| 61 gfx::Image* image) { | 72 const gfx::Rect& snapshot_bounds) { |
| 62 // Make sure to grab the "window frame" view so we get current tab + | 73 // Make sure to grab the "window frame" view so we get current tab + |
| 63 // tabstrip. | 74 // tabstrip. |
| 64 return GrabViewSnapshot([[window contentView] superview], snapshot_bounds, | 75 return GrabViewSnapshot([[window contentView] superview], png_representation, |
| 65 image); | 76 snapshot_bounds); |
| 66 } | 77 } |
| 67 | 78 |
| 68 void GrabWindowSnapshotAndScaleAsync( | 79 void GrabWindowSnapshotAndScaleAsync( |
| 69 gfx::NativeWindow window, | 80 gfx::NativeWindow window, |
| 70 const gfx::Rect& snapshot_bounds, | 81 const gfx::Rect& snapshot_bounds, |
| 71 const gfx::Size& target_size, | 82 const gfx::Size& target_size, |
| 72 scoped_refptr<base::TaskRunner> background_task_runner, | 83 scoped_refptr<base::TaskRunner> background_task_runner, |
| 73 GrabWindowSnapshotAsyncCallback callback) { | 84 GrabWindowSnapshotAsyncCallback callback) { |
| 74 callback.Run(gfx::Image()); | 85 callback.Run(gfx::Image()); |
| 75 } | 86 } |
| 76 | 87 |
| 77 void GrabViewSnapshotAsync(gfx::NativeView view, | 88 void GrabViewSnapshotAsync( |
| 78 const gfx::Rect& source_rect, | 89 gfx::NativeView view, |
| 79 const GrabWindowSnapshotAsyncCallback& callback) { | 90 const gfx::Rect& source_rect, |
| 80 callback.Run(gfx::Image()); | 91 scoped_refptr<base::TaskRunner> background_task_runner, |
| 92 const GrabWindowSnapshotAsyncPNGCallback& callback) { |
| 93 callback.Run(scoped_refptr<base::RefCountedBytes>()); |
| 81 } | 94 } |
| 82 | 95 |
| 83 void GrabWindowSnapshotAsync(gfx::NativeWindow window, | 96 void GrabWindowSnapshotAsync( |
| 84 const gfx::Rect& source_rect, | 97 gfx::NativeWindow window, |
| 85 const GrabWindowSnapshotAsyncCallback& callback) { | 98 const gfx::Rect& source_rect, |
| 99 scoped_refptr<base::TaskRunner> background_task_runner, |
| 100 const GrabWindowSnapshotAsyncPNGCallback& callback) { |
| 86 return GrabViewSnapshotAsync([[window contentView] superview], source_rect, | 101 return GrabViewSnapshotAsync([[window contentView] superview], source_rect, |
| 87 callback); | 102 background_task_runner, callback); |
| 88 } | 103 } |
| 89 | 104 |
| 90 } // namespace ui | 105 } // namespace ui |
| OLD | NEW |