OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "content/browser/compositor/browser_compositor_view_private_mac.h" | 5 #include "content/browser/compositor/browser_compositor_view_private_mac.h" |
6 | 6 |
7 #include "base/debug/trace_event.h" | 7 #include "base/debug/trace_event.h" |
8 #include "content/browser/compositor/gpu_process_transport_factory.h" | 8 #include "content/browser/compositor/gpu_process_transport_factory.h" |
9 #include "content/browser/renderer_host/compositing_iosurface_context_mac.h" | 9 #include "content/browser/renderer_host/compositing_iosurface_context_mac.h" |
10 #include "content/browser/renderer_host/compositing_iosurface_mac.h" | 10 #include "content/browser/renderer_host/compositing_iosurface_mac.h" |
11 #include "content/browser/renderer_host/software_layer_mac.h" | 11 #include "content/browser/renderer_host/software_layer_mac.h" |
12 #include "content/public/browser/context_factory.h" | 12 #include "content/public/browser/context_factory.h" |
13 #include "ui/base/cocoa/animation_utils.h" | 13 #include "ui/base/cocoa/animation_utils.h" |
14 #include "ui/gl/scoped_cgl.h" | 14 #include "ui/gl/scoped_cgl.h" |
15 | 15 |
16 //////////////////////////////////////////////////////////////////////////////// | 16 //////////////////////////////////////////////////////////////////////////////// |
17 // BrowserCompositorViewCocoa | 17 // BrowserCompositorViewCocoa |
18 | 18 |
19 @implementation BrowserCompositorViewCocoa : NSView | 19 @implementation BrowserCompositorViewCocoa : NSView |
20 | 20 |
21 - (id)init { | 21 - (id)init { |
22 if (self = [super init]) { | 22 if (self = [super init]) { |
23 accelerated_layer_output_surface_id_ = 0; | 23 accelerated_layer_output_surface_id_ = 0; |
24 client_ = NULL; | 24 client_ = NULL; |
25 helper_.reset(new content::BrowserCompositorViewCocoaHelper(self)); | 25 helper_.reset(new content::BrowserCompositorViewCocoaHelper(self)); |
26 | 26 |
27 // Disable the fade-in animation as the layer and view are added. | 27 // Disable the fade-in animation as the layer and view are added. |
28 ScopedCAActionDisabler disabler; | 28 ScopedCAActionDisabler disabler; |
29 | 29 |
30 // Make this view host a transparent layer. | 30 // Add a flipped transparent layer as a child, so that we don't need to |
31 background_layer_.reset([[CALayer alloc] init]); | 31 // fiddle with the position of sub-layers -- they will always be at the |
32 [background_layer_ setContentsGravity:kCAGravityTopLeft]; | 32 // origin. |
33 [self setLayer:background_layer_]; | 33 flipped_layer_.reset([[CALayer alloc] init]); |
34 [self setWantsLayer:YES]; | 34 [flipped_layer_ setGeometryFlipped:YES]; |
| 35 [flipped_layer_ setAnchorPoint:CGPointMake(0, 0)]; |
| 36 [flipped_layer_ |
| 37 setAutoresizingMask:kCALayerWidthSizable|kCALayerHeightSizable]; |
35 | 38 |
36 compositor_.reset(new ui::Compositor(self, content::GetContextFactory())); | 39 compositor_.reset(new ui::Compositor(self, content::GetContextFactory())); |
37 } | 40 } |
38 return self; | 41 return self; |
39 } | 42 } |
40 | 43 |
41 - (void)setClient:(content::BrowserCompositorViewMacClient*)client { | 44 - (void)setClient:(content::BrowserCompositorViewMacClient*)client { |
42 // Disable the fade-out as layers are removed. | 45 // Disable the fade-out as layers are removed. |
43 ScopedCAActionDisabler disabler; | 46 ScopedCAActionDisabler disabler; |
44 [self removeFromSuperview]; | |
45 | 47 |
46 // Reset all state. | 48 // Reset all state. |
| 49 [flipped_layer_ removeFromSuperlayer]; |
47 [accelerated_layer_ removeFromSuperlayer]; | 50 [accelerated_layer_ removeFromSuperlayer]; |
48 [accelerated_layer_ resetClient]; | 51 [accelerated_layer_ resetClient]; |
49 accelerated_layer_.reset(); | 52 accelerated_layer_.reset(); |
50 accelerated_layer_output_surface_id_ = 0; | 53 accelerated_layer_output_surface_id_ = 0; |
51 [software_layer_ removeFromSuperlayer]; | 54 [software_layer_ removeFromSuperlayer]; |
52 software_layer_.reset(); | 55 software_layer_.reset(); |
53 compositor_->SetScaleAndSize(1.0, gfx::Size(0, 0)); | 56 compositor_->SetScaleAndSize(1.0, gfx::Size(0, 0)); |
54 | 57 |
55 client_ = client; | 58 client_ = client; |
56 if (client_) { | 59 if (client_) { |
57 DCHECK(compositor_); | 60 DCHECK(compositor_); |
58 compositor_->SetRootLayer(client_->BrowserCompositorRootLayer()); | 61 compositor_->SetRootLayer(client_->BrowserCompositorRootLayer()); |
59 [client_->BrowserCompositorSuperview() addSubview:self]; | 62 CALayer* background_layer = [client_->BrowserCompositorSuperview() layer]; |
| 63 DCHECK(background_layer); |
| 64 [flipped_layer_ setBounds:[background_layer bounds]]; |
| 65 [background_layer addSublayer:flipped_layer_]; |
60 } else { | 66 } else { |
61 compositor_->SetRootLayer(NULL); | 67 compositor_->SetRootLayer(NULL); |
62 } | 68 } |
63 } | 69 } |
64 | 70 |
65 - (void)destroyCompositor { | 71 - (void)destroyCompositor { |
66 DCHECK(!client_); | 72 DCHECK(!client_); |
67 compositor_.reset(); | 73 compositor_.reset(); |
68 } | 74 } |
69 | 75 |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
104 // If there is not a layer for accelerated frames, create one. | 110 // If there is not a layer for accelerated frames, create one. |
105 if (!accelerated_layer_) { | 111 if (!accelerated_layer_) { |
106 // Disable the fade-in animation as the layer is added. | 112 // Disable the fade-in animation as the layer is added. |
107 ScopedCAActionDisabler disabler; | 113 ScopedCAActionDisabler disabler; |
108 scoped_refptr<content::CompositingIOSurfaceMac> iosurface = | 114 scoped_refptr<content::CompositingIOSurfaceMac> iosurface = |
109 content::CompositingIOSurfaceMac::Create(); | 115 content::CompositingIOSurfaceMac::Create(); |
110 accelerated_layer_.reset([[CompositingIOSurfaceLayer alloc] | 116 accelerated_layer_.reset([[CompositingIOSurfaceLayer alloc] |
111 initWithIOSurface:iosurface | 117 initWithIOSurface:iosurface |
112 withScaleFactor:scale_factor | 118 withScaleFactor:scale_factor |
113 withClient:helper_.get()]); | 119 withClient:helper_.get()]); |
114 [[self layer] addSublayer:accelerated_layer_]; | 120 [flipped_layer_ addSublayer:accelerated_layer_]; |
115 } | 121 } |
116 | 122 |
117 { | 123 { |
118 bool result = true; | 124 bool result = true; |
119 gfx::ScopedCGLSetCurrentContext scoped_set_current_context( | 125 gfx::ScopedCGLSetCurrentContext scoped_set_current_context( |
120 [accelerated_layer_ context]->cgl_context()); | 126 [accelerated_layer_ context]->cgl_context()); |
121 result = [accelerated_layer_ iosurface]->SetIOSurfaceWithContextCurrent( | 127 result = [accelerated_layer_ iosurface]->SetIOSurfaceWithContextCurrent( |
122 [accelerated_layer_ context], surface_handle, pixel_size, scale_factor); | 128 [accelerated_layer_ context], surface_handle, pixel_size, scale_factor); |
123 if (!result) | 129 if (!result) |
124 LOG(ERROR) << "Failed SetIOSurface on CompositingIOSurfaceMac"; | 130 LOG(ERROR) << "Failed SetIOSurface on CompositingIOSurfaceMac"; |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
156 withScaleFactor:(float)scale_factor | 162 withScaleFactor:(float)scale_factor |
157 withCanvas:(SkCanvas*)canvas { | 163 withCanvas:(SkCanvas*)canvas { |
158 if (!frame_data || !canvas) | 164 if (!frame_data || !canvas) |
159 return; | 165 return; |
160 | 166 |
161 // If there is not a layer for software frames, create one. | 167 // If there is not a layer for software frames, create one. |
162 if (!software_layer_) { | 168 if (!software_layer_) { |
163 // Disable the fade-in animation as the layer is added. | 169 // Disable the fade-in animation as the layer is added. |
164 ScopedCAActionDisabler disabler; | 170 ScopedCAActionDisabler disabler; |
165 software_layer_.reset([[SoftwareLayer alloc] init]); | 171 software_layer_.reset([[SoftwareLayer alloc] init]); |
166 [[self layer] addSublayer:software_layer_]; | 172 [flipped_layer_ addSublayer:software_layer_]; |
167 } | 173 } |
168 | 174 |
169 SkImageInfo info; | 175 SkImageInfo info; |
170 size_t row_bytes; | 176 size_t row_bytes; |
171 const void* pixels = canvas->peekPixels(&info, &row_bytes); | 177 const void* pixels = canvas->peekPixels(&info, &row_bytes); |
172 [software_layer_ setContentsToData:pixels | 178 [software_layer_ setContentsToData:pixels |
173 withRowBytes:row_bytes | 179 withRowBytes:row_bytes |
174 withPixelSize:gfx::Size(info.fWidth, info.fHeight) | 180 withPixelSize:gfx::Size(info.fWidth, info.fHeight) |
175 withScaleFactor:scale_factor]; | 181 withScaleFactor:scale_factor]; |
176 | 182 |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
212 | 218 |
213 void BrowserCompositorViewCocoaHelper::AcceleratedLayerDidDrawFrame( | 219 void BrowserCompositorViewCocoaHelper::AcceleratedLayerDidDrawFrame( |
214 bool succeeded) { | 220 bool succeeded) { |
215 [view_ layerDidDrawFrame]; | 221 [view_ layerDidDrawFrame]; |
216 if (!succeeded) | 222 if (!succeeded) |
217 [view_ gotAcceleratedLayerError]; | 223 [view_ gotAcceleratedLayerError]; |
218 } | 224 } |
219 | 225 |
220 } | 226 } |
221 | 227 |
OLD | NEW |