Chromium Code Reviews| Index: content/browser/renderer_host/compositing_iosurface_layer_mac.mm |
| diff --git a/content/browser/renderer_host/compositing_iosurface_layer_mac.mm b/content/browser/renderer_host/compositing_iosurface_layer_mac.mm |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..e242a5e3d31f4a0402fd43f8335df2dcf1f63bfb |
| --- /dev/null |
| +++ b/content/browser/renderer_host/compositing_iosurface_layer_mac.mm |
| @@ -0,0 +1,133 @@ |
| +// Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "content/browser/renderer_host/compositing_iosurface_layer_mac.h" |
| + |
| +#include <CoreFoundation/CoreFoundation.h> |
| + |
| +#include "content/browser/renderer_host/render_widget_host_view_mac.h" |
| +#include "content/browser/renderer_host/compositing_iosurface_context_mac.h" |
| +#include "content/browser/renderer_host/compositing_iosurface_mac.h" |
| +#include "ui/base/cocoa/animation_utils.h" |
| +#include "ui/gfx/size_conversions.h" |
| + |
| +@interface CALayer (LionAPI) |
| +- (CGFloat)contentsScale; |
| +- (void)setContentsScale:(CGFloat)contentsScale; |
| +@end |
| + |
| +@implementation CompositingIOSurfaceLayer |
| + |
| +@synthesize context = context_; |
| + |
| +- (id)initWithRenderWidgetHostViewMac:(content::RenderWidgetHostViewMac*)r { |
| + if (self = [super init]) { |
| + renderWidgetHostView_ = r; |
| + |
| + NSView* view = renderWidgetHostView_->cocoa_view(); |
| + CGRect rect; |
| + rect.origin.x = 0; |
| + rect.origin.y = 0; |
| + rect.size.width = [view bounds].size.width; |
| + rect.size.height = [view bounds].size.height; |
|
sail
2013/05/31 23:59:14
use NSRectToCGRect?
ccameron
2013/06/01 00:29:45
Done.
|
| + |
| + ScopedCAActionDisabler disabler; |
| + [self setAutoresizingMask:kCALayerWidthSizable | kCALayerHeightSizable]; |
| + [self setContentsGravity:kCAGravityTopLeft]; |
| + [self setFrame:rect]; |
| + [self setNeedsDisplay]; |
| + [self updateScaleFactor]; |
| + [[view layer] addSublayer:self]; |
| + } |
| + return self; |
| +} |
| + |
| +- (BOOL)ensureContext { |
| + if (context_) |
| + return YES; |
| + |
| + if (!renderWidgetHostView_) |
| + return NO; |
| + |
| + if (renderWidgetHostView_->compositing_iosurface_) |
| + context_ = renderWidgetHostView_->compositing_iosurface_->context(); |
| + |
| + if (!context_) { |
| + context_ = content::CompositingIOSurfaceContext::Get( |
| + renderWidgetHostView_->window_number(), |
| + content::CompositingIOSurfaceMac::SURFACE_ORDER_ABOVE_WINDOW); |
| + } |
| + |
| + return context_ ? YES : NO; |
| +} |
| + |
| +- (void)updateScaleFactor { |
| + if (!renderWidgetHostView_ || |
| + ![self respondsToSelector:(@selector(contentsScale))] || |
| + ![self respondsToSelector:(@selector(setContentsScale:))]) |
| + return; |
| + |
| + float current_scale_factor = [self contentsScale]; |
| + float new_scale_factor = current_scale_factor; |
| + if (renderWidgetHostView_->compositing_iosurface_) { |
| + new_scale_factor = |
| + renderWidgetHostView_->compositing_iosurface_->scale_factor(); |
| + } |
| + |
| + if (new_scale_factor == current_scale_factor) |
| + return; |
| + |
| + ScopedCAActionDisabler disabler; |
| + [self setContentsScale:new_scale_factor]; |
| +} |
| + |
| +- (void)disableCompositing{ |
| + ScopedCAActionDisabler disabler; |
| + [self removeFromSuperlayer]; |
| + renderWidgetHostView_ = nil; |
| +} |
| + |
| +// The remaining methods implement the CAOpenGLLayer interface. |
| + |
| +- (CGLContextObj)copyCGLContextForPixelFormat:(CGLPixelFormatObj)pixelFormat { |
| + if ([self ensureContext]) |
| + return context_->cgl_context(); |
| + return nil; |
| +} |
| + |
| +- (void)releaseCGLContext:(CGLContextObj)glContext { |
| + if (!context_) |
| + return; |
| + |
| + DCHECK(glContext == context_->cgl_context()); |
| + context_ = nil; |
| +} |
| + |
| +- (void)drawInCGLContext:(CGLContextObj)glContext |
| + pixelFormat:(CGLPixelFormatObj)pixelFormat |
| + forLayerTime:(CFTimeInterval)timeInterval |
| + displayTime:(const CVTimeStamp*)timeStamp { |
| + if (!context_ || !renderWidgetHostView_ || |
| + !renderWidgetHostView_->compositing_iosurface_) { |
| + glClearColor(1, 1, 1, 1); |
| + glClear(GL_COLOR_BUFFER_BIT); |
| + return; |
| + } |
| + |
| + DCHECK(glContext == context_->cgl_context()); |
| + |
| + gfx::Size window_size([self frame].size); |
| + float window_scale_factor = 1.f; |
| + if ([self respondsToSelector:(@selector(contentsScale))]) |
| + window_scale_factor = [self contentsScale]; |
| + |
| + renderWidgetHostView_->compositing_iosurface_->DrawIOSurface( |
| + window_size, |
| + window_scale_factor, |
| + renderWidgetHostView_->frame_subscriber()); |
| + |
| + renderWidgetHostView_->AckPendingSwapBuffers(); |
| +} |
| + |
| +@end |