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

Side by Side Diff: ui/snapshot/snapshot_mac.mm

Issue 2592983002: [devtools] Support different encodings for Page.CaptureScreenshot. (Closed)
Patch Set: Encode in ui snapshot methods instead. Created 3 years, 11 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 // 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/scoped_nsobject.h"
13 #include "base/mac/sdk_forward_declarations.h" 13 #include "base/mac/sdk_forward_declarations.h"
14 #include "base/task_runner.h" 14 #include "base/task_runner.h"
15 #include "ui/gfx/geometry/rect.h" 15 #include "ui/gfx/geometry/rect.h"
16 #include "ui/gfx/image/image.h" 16 #include "ui/gfx/image/image.h"
17 17
18 namespace ui { 18 namespace ui {
19 19
20 bool GrabViewSnapshot(gfx::NativeView view, 20 bool GrabViewSnapshot(gfx::NativeView view,
21 std::vector<unsigned char>* png_representation, 21 std::vector<unsigned char>* png_representation,
22 const gfx::Rect& snapshot_bounds) { 22 const gfx::Rect& snapshot_bounds,
23 SnapshotEncoding encoding,
24 SnapshotQuality quality) {
23 NSWindow* window = [view window]; 25 NSWindow* window = [view window];
24 NSScreen* screen = [[NSScreen screens] firstObject]; 26 NSScreen* screen = [[NSScreen screens] firstObject];
25 gfx::Rect screen_bounds = gfx::Rect(NSRectToCGRect([screen frame])); 27 gfx::Rect screen_bounds = gfx::Rect(NSRectToCGRect([screen frame]));
26 28
27 29
28 // Get the view bounds relative to the screen 30 // Get the view bounds relative to the screen
29 NSRect frame = [view convertRect:[view bounds] toView:nil]; 31 NSRect frame = [view convertRect:[view bounds] toView:nil];
30 frame = [window convertRectToScreen:frame]; 32 frame = [window convertRectToScreen:frame];
31 33
32 gfx::Rect view_bounds = gfx::Rect(NSRectToCGRect(frame)); 34 gfx::Rect view_bounds = gfx::Rect(NSRectToCGRect(frame));
(...skipping 15 matching lines...) Expand all
48 base::ScopedCFTypeRef<CGImageRef> windowSnapshot( 50 base::ScopedCFTypeRef<CGImageRef> windowSnapshot(
49 CGWindowListCreateImage(screen_snapshot_bounds.ToCGRect(), 51 CGWindowListCreateImage(screen_snapshot_bounds.ToCGRect(),
50 kCGWindowListOptionIncludingWindow, 52 kCGWindowListOptionIncludingWindow,
51 [window windowNumber], 53 [window windowNumber],
52 kCGWindowImageBoundsIgnoreFraming)); 54 kCGWindowImageBoundsIgnoreFraming));
53 if (CGImageGetWidth(windowSnapshot) <= 0) 55 if (CGImageGetWidth(windowSnapshot) <= 0)
54 return false; 56 return false;
55 57
56 base::scoped_nsobject<NSBitmapImageRep> rep( 58 base::scoped_nsobject<NSBitmapImageRep> rep(
57 [[NSBitmapImageRep alloc] initWithCGImage:windowSnapshot]); 59 [[NSBitmapImageRep alloc] initWithCGImage:windowSnapshot]);
58 NSData* data = [rep representationUsingType:NSPNGFileType properties:@{}]; 60 NSData* data;
61 float compression_factor = 0;
62 NSDictionary* options = nullptr;
63 switch (encoding) {
64 case SnapshotEncoding::PNG:
65 data = [rep representationUsingType:NSPNGFileType properties:@{}];
66 break;
67 case SnapshotEncoding::JPEG:
68 compression_factor = static_cast<float>(quality) / 100;
69 options = [NSDictionary
70 dictionaryWithObject:[NSNumber numberWithFloat:compression_factor]
71 forKey:NSImageCompressionFactor];
72 data = [rep representationUsingType:NSJPEGFileType properties:options];
73 break;
74 default:
75 NOTREACHED();
76 }
59 const unsigned char* buf = static_cast<const unsigned char*>([data bytes]); 77 const unsigned char* buf = static_cast<const unsigned char*>([data bytes]);
60 NSUInteger length = [data length]; 78 NSUInteger length = [data length];
61 if (buf == NULL || length == 0) 79 if (buf == NULL || length == 0)
62 return false; 80 return false;
63 81
64 png_representation->assign(buf, buf + length); 82 png_representation->assign(buf, buf + length);
65 DCHECK(!png_representation->empty()); 83 DCHECK(!png_representation->empty());
66 84
67 return true; 85 return true;
68 } 86 }
69 87
70 bool GrabWindowSnapshot(gfx::NativeWindow window, 88 bool GrabWindowSnapshot(gfx::NativeWindow window,
71 std::vector<unsigned char>* png_representation, 89 std::vector<unsigned char>* png_representation,
72 const gfx::Rect& snapshot_bounds) { 90 const gfx::Rect& snapshot_bounds,
91 SnapshotEncoding encoding,
92 SnapshotQuality quality) {
73 // Make sure to grab the "window frame" view so we get current tab + 93 // Make sure to grab the "window frame" view so we get current tab +
74 // tabstrip. 94 // tabstrip.
75 return GrabViewSnapshot([[window contentView] superview], png_representation, 95 return GrabViewSnapshot([[window contentView] superview], png_representation,
76 snapshot_bounds); 96 snapshot_bounds, encoding, quality);
77 } 97 }
78 98
79 void GrabWindowSnapshotAndScaleAsync( 99 void GrabWindowSnapshotAndScaleAsync(
80 gfx::NativeWindow window, 100 gfx::NativeWindow window,
81 const gfx::Rect& snapshot_bounds, 101 const gfx::Rect& snapshot_bounds,
82 const gfx::Size& target_size, 102 const gfx::Size& target_size,
83 scoped_refptr<base::TaskRunner> background_task_runner, 103 scoped_refptr<base::TaskRunner> background_task_runner,
84 GrabWindowSnapshotAsyncCallback callback) { 104 GrabWindowSnapshotAsyncCallback callback) {
85 callback.Run(gfx::Image()); 105 callback.Run(gfx::Image());
86 } 106 }
87 107
88 void GrabViewSnapshotAsync( 108 void GrabViewSnapshotAsync(
89 gfx::NativeView view, 109 gfx::NativeView view,
90 const gfx::Rect& source_rect, 110 const gfx::Rect& source_rect,
91 scoped_refptr<base::TaskRunner> background_task_runner, 111 scoped_refptr<base::TaskRunner> background_task_runner,
92 const GrabWindowSnapshotAsyncPNGCallback& callback) { 112 const GrabWindowSnapshotAsyncEncodedCallback& callback,
113 SnapshotEncoding encoding,
114 SnapshotQuality quality) {
93 callback.Run(scoped_refptr<base::RefCountedBytes>()); 115 callback.Run(scoped_refptr<base::RefCountedBytes>());
94 } 116 }
95 117
96 void GrabWindowSnapshotAsync( 118 void GrabWindowSnapshotAsync(
97 gfx::NativeWindow window, 119 gfx::NativeWindow window,
98 const gfx::Rect& source_rect, 120 const gfx::Rect& source_rect,
99 scoped_refptr<base::TaskRunner> background_task_runner, 121 scoped_refptr<base::TaskRunner> background_task_runner,
100 const GrabWindowSnapshotAsyncPNGCallback& callback) { 122 const GrabWindowSnapshotAsyncEncodedCallback& callback,
123 SnapshotEncoding encoding,
124 SnapshotQuality quality) {
101 return GrabViewSnapshotAsync([[window contentView] superview], source_rect, 125 return GrabViewSnapshotAsync([[window contentView] superview], source_rect,
102 background_task_runner, callback); 126 background_task_runner, callback, encoding,
127 quality);
103 } 128 }
104 129
105 } // namespace ui 130 } // namespace ui
OLDNEW
« content/browser/renderer_host/render_widget_host_impl.cc ('K') | « ui/snapshot/snapshot_ios.mm ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698