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 |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "base/mac/mac_util.h" | 12 #include "base/mac/mac_util.h" |
13 #include "base/mac/scoped_cftyperef.h" | 13 #include "base/mac/scoped_cftyperef.h" |
14 #include "content/browser/renderer_host/render_process_host_impl.h" | 14 #include "content/browser/renderer_host/render_process_host_impl.h" |
15 #include "content/browser/renderer_host/render_widget_host_impl.h" | 15 #include "content/browser/renderer_host/render_widget_host_impl.h" |
16 #include "content/public/browser/render_widget_host_view.h" | 16 #include "content/public/browser/render_widget_host_view.h" |
17 #include "skia/ext/platform_canvas.h" | 17 #include "skia/ext/platform_canvas.h" |
18 #include "third_party/skia/include/core/SkBitmap.h" | 18 #include "third_party/skia/include/core/SkBitmap.h" |
19 #include "third_party/skia/include/core/SkCanvas.h" | 19 #include "third_party/skia/include/core/SkCanvas.h" |
20 #include "ui/gfx/rect.h" | 20 #include "ui/gfx/rect.h" |
| 21 #include "ui/gfx/size_conversions.h" |
21 #include "ui/gfx/scoped_cg_context_save_gstate_mac.h" | 22 #include "ui/gfx/scoped_cg_context_save_gstate_mac.h" |
22 #include "ui/surface/transport_dib.h" | 23 #include "ui/surface/transport_dib.h" |
23 | 24 |
24 namespace { | 25 namespace { |
25 | 26 |
26 // Returns a Rect obtained by flooring the values of the given RectF. | 27 // Returns a Rect obtained by flooring the values of the given RectF. |
27 gfx::Rect ToFlooredRect(const gfx::RectF& rect) { | 28 gfx::Rect ToFlooredRect(const gfx::RectF& rect) { |
28 return gfx::Rect(static_cast<int>(std::floor(rect.x())), | 29 return gfx::Rect(static_cast<int>(std::floor(rect.x())), |
29 static_cast<int>(std::floor(rect.y())), | 30 static_cast<int>(std::floor(rect.y())), |
30 static_cast<int>(std::floor(rect.width())), | 31 static_cast<int>(std::floor(rect.width())), |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
73 | 74 |
74 cg_layer_.swap(new_layer); | 75 cg_layer_.swap(new_layer); |
75 if (!cg_layer_) { | 76 if (!cg_layer_) { |
76 // The view isn't in a window yet. Use a CGBitmapContext for now. | 77 // The view isn't in a window yet. Use a CGBitmapContext for now. |
77 cg_bitmap_.reset(CreateCGBitmapContext()); | 78 cg_bitmap_.reset(CreateCGBitmapContext()); |
78 CGContextScaleCTM(cg_bitmap_, device_scale_factor_, device_scale_factor_); | 79 CGContextScaleCTM(cg_bitmap_, device_scale_factor_, device_scale_factor_); |
79 } | 80 } |
80 } | 81 } |
81 | 82 |
82 size_t BackingStoreMac::MemorySize() { | 83 size_t BackingStoreMac::MemorySize() { |
83 return size().Scale(device_scale_factor_).GetArea() * 4; | 84 return gfx::ToFlooredSize(size().Scale(device_scale_factor_)).GetArea() * 4; |
84 } | 85 } |
85 | 86 |
86 void BackingStoreMac::PaintToBackingStore( | 87 void BackingStoreMac::PaintToBackingStore( |
87 RenderProcessHost* process, | 88 RenderProcessHost* process, |
88 TransportDIB::Id bitmap, | 89 TransportDIB::Id bitmap, |
89 const gfx::Rect& bitmap_rect, | 90 const gfx::Rect& bitmap_rect, |
90 const std::vector<gfx::Rect>& copy_rects, | 91 const std::vector<gfx::Rect>& copy_rects, |
91 float scale_factor, | 92 float scale_factor, |
92 const base::Closure& completion_callback, | 93 const base::Closure& completion_callback, |
93 bool* scheduled_completion_callback) { | 94 bool* scheduled_completion_callback) { |
94 *scheduled_completion_callback = false; | 95 *scheduled_completion_callback = false; |
95 DCHECK_NE(static_cast<bool>(cg_layer()), static_cast<bool>(cg_bitmap())); | 96 DCHECK_NE(static_cast<bool>(cg_layer()), static_cast<bool>(cg_bitmap())); |
96 | 97 |
97 TransportDIB* dib = process->GetTransportDIB(bitmap); | 98 TransportDIB* dib = process->GetTransportDIB(bitmap); |
98 if (!dib) | 99 if (!dib) |
99 return; | 100 return; |
100 | 101 |
101 gfx::Size pixel_size = size().Scale(device_scale_factor_); | 102 gfx::Size pixel_size = gfx::ToFlooredSize( |
| 103 size().Scale(device_scale_factor_)); |
102 gfx::Rect pixel_bitmap_rect = | 104 gfx::Rect pixel_bitmap_rect = |
103 ToFlooredRect(bitmap_rect.Scale(scale_factor)); | 105 ToFlooredRect(bitmap_rect.Scale(scale_factor)); |
104 | 106 |
105 size_t bitmap_byte_count = | 107 size_t bitmap_byte_count = |
106 pixel_bitmap_rect.width() * pixel_bitmap_rect.height() * 4; | 108 pixel_bitmap_rect.width() * pixel_bitmap_rect.height() * 4; |
107 DCHECK_GE(dib->size(), bitmap_byte_count); | 109 DCHECK_GE(dib->size(), bitmap_byte_count); |
108 | 110 |
109 base::mac::ScopedCFTypeRef<CGDataProviderRef> data_provider( | 111 base::mac::ScopedCFTypeRef<CGDataProviderRef> data_provider( |
110 CGDataProviderCreateWithData(NULL, dib->memory(), | 112 CGDataProviderCreateWithData(NULL, dib->memory(), |
111 bitmap_byte_count, NULL)); | 113 bitmap_byte_count, NULL)); |
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
261 // bitmap backing |layer| with be size().Scale(2) in HiDPI mode automatically. | 263 // bitmap backing |layer| with be size().Scale(2) in HiDPI mode automatically. |
262 CGLayerRef layer = CGLayerCreateWithContext(cg_context, | 264 CGLayerRef layer = CGLayerCreateWithContext(cg_context, |
263 size().ToCGSize(), | 265 size().ToCGSize(), |
264 NULL); | 266 NULL); |
265 DCHECK(layer); | 267 DCHECK(layer); |
266 | 268 |
267 return layer; | 269 return layer; |
268 } | 270 } |
269 | 271 |
270 CGContextRef BackingStoreMac::CreateCGBitmapContext() { | 272 CGContextRef BackingStoreMac::CreateCGBitmapContext() { |
271 gfx::Size pixel_size = size().Scale(device_scale_factor_); | 273 gfx::Size pixel_size = gfx::ToFlooredSize(size().Scale(device_scale_factor_)); |
272 // A CGBitmapContext serves as a stand-in for the layer before the view is | 274 // A CGBitmapContext serves as a stand-in for the layer before the view is |
273 // in a containing window. | 275 // in a containing window. |
274 CGContextRef context = CGBitmapContextCreate(NULL, | 276 CGContextRef context = CGBitmapContextCreate(NULL, |
275 pixel_size.width(), | 277 pixel_size.width(), |
276 pixel_size.height(), | 278 pixel_size.height(), |
277 8, pixel_size.width() * 4, | 279 8, pixel_size.width() * 4, |
278 base::mac::GetSystemColorSpace(), | 280 base::mac::GetSystemColorSpace(), |
279 kCGImageAlphaPremultipliedFirst | | 281 kCGImageAlphaPremultipliedFirst | |
280 kCGBitmapByteOrder32Host); | 282 kCGBitmapByteOrder32Host); |
281 DCHECK(context); | 283 DCHECK(context); |
282 | 284 |
283 return context; | 285 return context; |
284 } | 286 } |
285 | 287 |
286 } // namespace content | 288 } // namespace content |
OLD | NEW |