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 #import <Cocoa/Cocoa.h> | 5 #import <Cocoa/Cocoa.h> |
6 | 6 |
7 #include "content/browser/renderer_host/backing_store_mac.h" | 7 #include "content/browser/renderer_host/backing_store_mac.h" |
8 | 8 |
9 #include <cmath> | 9 #include <cmath> |
10 | 10 |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
46 | 46 |
47 BackingStoreMac::~BackingStoreMac() { | 47 BackingStoreMac::~BackingStoreMac() { |
48 } | 48 } |
49 | 49 |
50 void BackingStoreMac::ScaleFactorChanged(float device_scale_factor) { | 50 void BackingStoreMac::ScaleFactorChanged(float device_scale_factor) { |
51 if (device_scale_factor == device_scale_factor_) | 51 if (device_scale_factor == device_scale_factor_) |
52 return; | 52 return; |
53 | 53 |
54 device_scale_factor_ = device_scale_factor; | 54 device_scale_factor_ = device_scale_factor; |
55 | 55 |
56 base::mac::ScopedCFTypeRef<CGLayerRef> new_layer(CreateCGLayer()); | 56 base::ScopedCFTypeRef<CGLayerRef> new_layer(CreateCGLayer()); |
57 // If we have a layer, copy the old contents. A pixelated flash is better | 57 // If we have a layer, copy the old contents. A pixelated flash is better |
58 // than a white flash. | 58 // than a white flash. |
59 if (new_layer && cg_layer_) { | 59 if (new_layer && cg_layer_) { |
60 CGContextRef layer = CGLayerGetContext(new_layer); | 60 CGContextRef layer = CGLayerGetContext(new_layer); |
61 CGContextDrawLayerAtPoint(layer, CGPointMake(0, 0), cg_layer_); | 61 CGContextDrawLayerAtPoint(layer, CGPointMake(0, 0), cg_layer_); |
62 } | 62 } |
63 | 63 |
64 cg_layer_.swap(new_layer); | 64 cg_layer_.swap(new_layer); |
65 if (!cg_layer_) { | 65 if (!cg_layer_) { |
66 // The view isn't in a window yet. Use a CGBitmapContext for now. | 66 // The view isn't in a window yet. Use a CGBitmapContext for now. |
(...skipping 24 matching lines...) Expand all Loading... |
91 | 91 |
92 gfx::Size pixel_size = gfx::ToFlooredSize( | 92 gfx::Size pixel_size = gfx::ToFlooredSize( |
93 gfx::ScaleSize(size(), device_scale_factor_)); | 93 gfx::ScaleSize(size(), device_scale_factor_)); |
94 gfx::Rect pixel_bitmap_rect = ToFlooredRectDeprecated( | 94 gfx::Rect pixel_bitmap_rect = ToFlooredRectDeprecated( |
95 gfx::ScaleRect(bitmap_rect, scale_factor)); | 95 gfx::ScaleRect(bitmap_rect, scale_factor)); |
96 | 96 |
97 size_t bitmap_byte_count = | 97 size_t bitmap_byte_count = |
98 pixel_bitmap_rect.width() * pixel_bitmap_rect.height() * 4; | 98 pixel_bitmap_rect.width() * pixel_bitmap_rect.height() * 4; |
99 DCHECK_GE(dib->size(), bitmap_byte_count); | 99 DCHECK_GE(dib->size(), bitmap_byte_count); |
100 | 100 |
101 base::mac::ScopedCFTypeRef<CGDataProviderRef> data_provider( | 101 base::ScopedCFTypeRef<CGDataProviderRef> data_provider( |
102 CGDataProviderCreateWithData(NULL, dib->memory(), | 102 CGDataProviderCreateWithData( |
103 bitmap_byte_count, NULL)); | 103 NULL, dib->memory(), bitmap_byte_count, NULL)); |
104 | 104 |
105 base::mac::ScopedCFTypeRef<CGImageRef> bitmap_image( | 105 base::ScopedCFTypeRef<CGImageRef> bitmap_image( |
106 CGImageCreate(pixel_bitmap_rect.width(), pixel_bitmap_rect.height(), | 106 CGImageCreate(pixel_bitmap_rect.width(), |
107 8, 32, 4 * pixel_bitmap_rect.width(), | 107 pixel_bitmap_rect.height(), |
108 base::mac::GetSystemColorSpace(), | 108 8, |
109 kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Host, | 109 32, |
110 data_provider, NULL, false, kCGRenderingIntentDefault)); | 110 4 * pixel_bitmap_rect.width(), |
| 111 base::mac::GetSystemColorSpace(), |
| 112 kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Host, |
| 113 data_provider, |
| 114 NULL, |
| 115 false, |
| 116 kCGRenderingIntentDefault)); |
111 | 117 |
112 for (size_t i = 0; i < copy_rects.size(); i++) { | 118 for (size_t i = 0; i < copy_rects.size(); i++) { |
113 const gfx::Rect& copy_rect = copy_rects[i]; | 119 const gfx::Rect& copy_rect = copy_rects[i]; |
114 gfx::Rect pixel_copy_rect = ToFlooredRectDeprecated( | 120 gfx::Rect pixel_copy_rect = ToFlooredRectDeprecated( |
115 gfx::ScaleRect(copy_rect, scale_factor)); | 121 gfx::ScaleRect(copy_rect, scale_factor)); |
116 | 122 |
117 // Only the subpixels given by copy_rect have pixels to copy. | 123 // Only the subpixels given by copy_rect have pixels to copy. |
118 base::mac::ScopedCFTypeRef<CGImageRef> image( | 124 base::ScopedCFTypeRef<CGImageRef> image(CGImageCreateWithImageInRect( |
119 CGImageCreateWithImageInRect(bitmap_image, CGRectMake( | 125 bitmap_image, |
120 pixel_copy_rect.x() - pixel_bitmap_rect.x(), | 126 CGRectMake(pixel_copy_rect.x() - pixel_bitmap_rect.x(), |
121 pixel_copy_rect.y() - pixel_bitmap_rect.y(), | 127 pixel_copy_rect.y() - pixel_bitmap_rect.y(), |
122 pixel_copy_rect.width(), | 128 pixel_copy_rect.width(), |
123 pixel_copy_rect.height()))); | 129 pixel_copy_rect.height()))); |
124 | 130 |
125 if (!cg_layer()) { | 131 if (!cg_layer()) { |
126 // The view may have moved to a window. Try to get a CGLayer. | 132 // The view may have moved to a window. Try to get a CGLayer. |
127 cg_layer_.reset(CreateCGLayer()); | 133 cg_layer_.reset(CreateCGLayer()); |
128 if (cg_layer()) { | 134 if (cg_layer()) { |
129 // Now that we have a layer, copy the cached image into it. | 135 // Now that we have a layer, copy the cached image into it. |
130 base::mac::ScopedCFTypeRef<CGImageRef> bitmap_image( | 136 base::ScopedCFTypeRef<CGImageRef> bitmap_image( |
131 CGBitmapContextCreateImage(cg_bitmap_)); | 137 CGBitmapContextCreateImage(cg_bitmap_)); |
132 CGContextDrawImage(CGLayerGetContext(cg_layer()), | 138 CGContextDrawImage(CGLayerGetContext(cg_layer()), |
133 CGRectMake(0, 0, size().width(), size().height()), | 139 CGRectMake(0, 0, size().width(), size().height()), |
134 bitmap_image); | 140 bitmap_image); |
135 // Discard the cache bitmap, since we no longer need it. | 141 // Discard the cache bitmap, since we no longer need it. |
136 cg_bitmap_.reset(NULL); | 142 cg_bitmap_.reset(NULL); |
137 } | 143 } |
138 } | 144 } |
139 | 145 |
140 DCHECK_NE(static_cast<bool>(cg_layer()), static_cast<bool>(cg_bitmap())); | 146 DCHECK_NE(static_cast<bool>(cg_layer()), static_cast<bool>(cg_bitmap())); |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
197 gfx::ScopedCGContextSaveGState save_gstate(layer); | 203 gfx::ScopedCGContextSaveGState save_gstate(layer); |
198 CGContextClipToRect(layer, | 204 CGContextClipToRect(layer, |
199 CGRectMake(clip_rect.x(), | 205 CGRectMake(clip_rect.x(), |
200 size().height() - clip_rect.bottom(), | 206 size().height() - clip_rect.bottom(), |
201 clip_rect.width(), | 207 clip_rect.width(), |
202 clip_rect.height())); | 208 clip_rect.height())); |
203 CGContextDrawLayerAtPoint(layer, | 209 CGContextDrawLayerAtPoint(layer, |
204 CGPointMake(delta.x(), -delta.y()), cg_layer()); | 210 CGPointMake(delta.x(), -delta.y()), cg_layer()); |
205 } else { | 211 } else { |
206 // We don't have a layer, so scroll the contents of the CGBitmapContext. | 212 // We don't have a layer, so scroll the contents of the CGBitmapContext. |
207 base::mac::ScopedCFTypeRef<CGImageRef> bitmap_image( | 213 base::ScopedCFTypeRef<CGImageRef> bitmap_image( |
208 CGBitmapContextCreateImage(cg_bitmap_)); | 214 CGBitmapContextCreateImage(cg_bitmap_)); |
209 gfx::ScopedCGContextSaveGState save_gstate(cg_bitmap_); | 215 gfx::ScopedCGContextSaveGState save_gstate(cg_bitmap_); |
210 CGContextClipToRect(cg_bitmap_, | 216 CGContextClipToRect(cg_bitmap_, |
211 CGRectMake(clip_rect.x(), | 217 CGRectMake(clip_rect.x(), |
212 size().height() - clip_rect.bottom(), | 218 size().height() - clip_rect.bottom(), |
213 clip_rect.width(), | 219 clip_rect.width(), |
214 clip_rect.height())); | 220 clip_rect.height())); |
215 CGContextDrawImage(cg_bitmap_, | 221 CGContextDrawImage(cg_bitmap_, |
216 CGRectMake(delta.x(), -delta.y(), | 222 CGRectMake(delta.x(), -delta.y(), |
217 size().width(), size().height()), | 223 size().width(), size().height()), |
218 bitmap_image); | 224 bitmap_image); |
219 } | 225 } |
220 } | 226 } |
221 } | 227 } |
222 | 228 |
223 void BackingStoreMac::CopyFromBackingStoreToCGContext(const CGRect& dest_rect, | 229 void BackingStoreMac::CopyFromBackingStoreToCGContext(const CGRect& dest_rect, |
224 CGContextRef context) { | 230 CGContextRef context) { |
225 gfx::ScopedCGContextSaveGState save_gstate(context); | 231 gfx::ScopedCGContextSaveGState save_gstate(context); |
226 CGContextSetInterpolationQuality(context, kCGInterpolationHigh); | 232 CGContextSetInterpolationQuality(context, kCGInterpolationHigh); |
227 if (cg_layer_) { | 233 if (cg_layer_) { |
228 CGContextDrawLayerInRect(context, dest_rect, cg_layer_); | 234 CGContextDrawLayerInRect(context, dest_rect, cg_layer_); |
229 } else { | 235 } else { |
230 base::mac::ScopedCFTypeRef<CGImageRef> image( | 236 base::ScopedCFTypeRef<CGImageRef> image( |
231 CGBitmapContextCreateImage(cg_bitmap_)); | 237 CGBitmapContextCreateImage(cg_bitmap_)); |
232 CGContextDrawImage(context, dest_rect, image); | 238 CGContextDrawImage(context, dest_rect, image); |
233 } | 239 } |
234 } | 240 } |
235 | 241 |
236 CGLayerRef BackingStoreMac::CreateCGLayer() { | 242 CGLayerRef BackingStoreMac::CreateCGLayer() { |
237 // The CGLayer should be optimized for drawing into the containing window, | 243 // The CGLayer should be optimized for drawing into the containing window, |
238 // so extract a CGContext corresponding to the window to be passed to | 244 // so extract a CGContext corresponding to the window to be passed to |
239 // CGLayerCreateWithContext. | 245 // CGLayerCreateWithContext. |
240 NSWindow* window = [render_widget_host()->GetView()->GetNativeView() window]; | 246 NSWindow* window = [render_widget_host()->GetView()->GetNativeView() window]; |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
272 8, pixel_size.width() * 4, | 278 8, pixel_size.width() * 4, |
273 base::mac::GetSystemColorSpace(), | 279 base::mac::GetSystemColorSpace(), |
274 kCGImageAlphaPremultipliedFirst | | 280 kCGImageAlphaPremultipliedFirst | |
275 kCGBitmapByteOrder32Host); | 281 kCGBitmapByteOrder32Host); |
276 DCHECK(context); | 282 DCHECK(context); |
277 | 283 |
278 return context; | 284 return context; |
279 } | 285 } |
280 | 286 |
281 } // namespace content | 287 } // namespace content |
OLD | NEW |