Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "content/browser/renderer_host/compositing_iosurface_layer_mac.h" | |
| 6 | |
| 7 #include <CoreFoundation/CoreFoundation.h> | |
| 8 | |
| 9 #include "content/browser/renderer_host/render_widget_host_view_mac.h" | |
| 10 #include "content/browser/renderer_host/compositing_iosurface_context_mac.h" | |
| 11 #include "content/browser/renderer_host/compositing_iosurface_mac.h" | |
| 12 #include "ui/base/cocoa/animation_utils.h" | |
| 13 #include "ui/gfx/size_conversions.h" | |
| 14 | |
| 15 @interface CALayer (LionAPI) | |
| 16 - (CGFloat)contentsScale; | |
| 17 - (void)setContentsScale:(CGFloat)contentsScale; | |
| 18 @end | |
|
Nico
2013/06/05 00:13:07
Consider putting this in base/mac/sdk_forward_decl
ccameron
2013/06/05 19:24:13
Done.
| |
| 19 | |
| 20 @implementation CompositingIOSurfaceLayer | |
| 21 | |
| 22 @synthesize context = context_; | |
| 23 | |
| 24 - (id)initWithRenderWidgetHostViewMac:(content::RenderWidgetHostViewMac*)r { | |
| 25 if (self = [super init]) { | |
| 26 renderWidgetHostView_ = r; | |
| 27 | |
| 28 ScopedCAActionDisabler disabler; | |
| 29 [self setAutoresizingMask:kCALayerWidthSizable | kCALayerHeightSizable]; | |
| 30 [self setContentsGravity:kCAGravityTopLeft]; | |
| 31 [self setFrame:NSRectToCGRect( | |
| 32 [renderWidgetHostView_->cocoa_view() bounds])]; | |
| 33 [self setNeedsDisplay]; | |
| 34 [self updateScaleFactor]; | |
| 35 [[renderWidgetHostView_->cocoa_view() layer] addSublayer:self]; | |
| 36 } | |
| 37 return self; | |
| 38 } | |
| 39 | |
| 40 - (BOOL)ensureContext { | |
| 41 if (context_) | |
| 42 return YES; | |
| 43 | |
| 44 if (!renderWidgetHostView_) | |
| 45 return NO; | |
| 46 | |
| 47 if (renderWidgetHostView_->compositing_iosurface_) | |
| 48 context_ = renderWidgetHostView_->compositing_iosurface_->context(); | |
| 49 | |
| 50 if (!context_) { | |
| 51 context_ = content::CompositingIOSurfaceContext::Get( | |
| 52 renderWidgetHostView_->window_number(), | |
| 53 content::CompositingIOSurfaceMac::SURFACE_ORDER_ABOVE_WINDOW); | |
| 54 } | |
| 55 | |
| 56 return context_ ? YES : NO; | |
| 57 } | |
| 58 | |
| 59 - (void)updateScaleFactor { | |
| 60 if (!renderWidgetHostView_ || | |
| 61 ![self respondsToSelector:(@selector(contentsScale))] || | |
| 62 ![self respondsToSelector:(@selector(setContentsScale:))]) | |
| 63 return; | |
| 64 | |
| 65 float current_scale_factor = [self contentsScale]; | |
| 66 float new_scale_factor = current_scale_factor; | |
| 67 if (renderWidgetHostView_->compositing_iosurface_) { | |
| 68 new_scale_factor = | |
| 69 renderWidgetHostView_->compositing_iosurface_->scale_factor(); | |
| 70 } | |
| 71 | |
| 72 if (new_scale_factor == current_scale_factor) | |
| 73 return; | |
| 74 | |
| 75 ScopedCAActionDisabler disabler; | |
| 76 [self setContentsScale:new_scale_factor]; | |
| 77 } | |
| 78 | |
| 79 - (void)disableCompositing{ | |
| 80 ScopedCAActionDisabler disabler; | |
| 81 [self removeFromSuperlayer]; | |
| 82 renderWidgetHostView_ = nil; | |
| 83 } | |
| 84 | |
| 85 // The remaining methods implement the CAOpenGLLayer interface. | |
| 86 | |
| 87 - (CGLContextObj)copyCGLContextForPixelFormat:(CGLPixelFormatObj)pixelFormat { | |
| 88 if ([self ensureContext]) | |
| 89 return context_->cgl_context(); | |
| 90 return nil; | |
| 91 } | |
| 92 | |
| 93 - (void)releaseCGLContext:(CGLContextObj)glContext { | |
| 94 if (!context_) | |
| 95 return; | |
| 96 | |
| 97 DCHECK(glContext == context_->cgl_context()); | |
| 98 context_ = nil; | |
| 99 } | |
| 100 | |
| 101 - (void)drawInCGLContext:(CGLContextObj)glContext | |
| 102 pixelFormat:(CGLPixelFormatObj)pixelFormat | |
| 103 forLayerTime:(CFTimeInterval)timeInterval | |
| 104 displayTime:(const CVTimeStamp*)timeStamp { | |
| 105 if (!context_ || !renderWidgetHostView_ || | |
| 106 !renderWidgetHostView_->compositing_iosurface_) { | |
| 107 glClearColor(1, 1, 1, 1); | |
| 108 glClear(GL_COLOR_BUFFER_BIT); | |
| 109 return; | |
| 110 } | |
| 111 | |
| 112 DCHECK(glContext == context_->cgl_context()); | |
| 113 | |
| 114 gfx::Size window_size([self frame].size); | |
| 115 float window_scale_factor = 1.f; | |
| 116 if ([self respondsToSelector:(@selector(contentsScale))]) | |
| 117 window_scale_factor = [self contentsScale]; | |
| 118 | |
| 119 renderWidgetHostView_->compositing_iosurface_->DrawIOSurface( | |
| 120 window_size, | |
| 121 window_scale_factor, | |
| 122 renderWidgetHostView_->frame_subscriber()); | |
| 123 | |
| 124 renderWidgetHostView_->AckPendingSwapBuffers(); | |
| 125 } | |
| 126 | |
| 127 @end | |
| OLD | NEW |