| 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 "base/mac/sdk_forward_declarations.h" |
| 10 #include "content/browser/renderer_host/render_widget_host_view_mac.h" |
| 11 #include "content/browser/renderer_host/compositing_iosurface_context_mac.h" |
| 12 #include "content/browser/renderer_host/compositing_iosurface_mac.h" |
| 13 #include "ui/base/cocoa/animation_utils.h" |
| 14 #include "ui/gfx/size_conversions.h" |
| 15 |
| 16 @implementation CompositingIOSurfaceLayer |
| 17 |
| 18 @synthesize context = context_; |
| 19 |
| 20 - (id)initWithRenderWidgetHostViewMac:(content::RenderWidgetHostViewMac*)r { |
| 21 if (self = [super init]) { |
| 22 renderWidgetHostView_ = r; |
| 23 |
| 24 ScopedCAActionDisabler disabler; |
| 25 [self setAutoresizingMask:kCALayerWidthSizable | kCALayerHeightSizable]; |
| 26 [self setContentsGravity:kCAGravityTopLeft]; |
| 27 [self setFrame:NSRectToCGRect( |
| 28 [renderWidgetHostView_->cocoa_view() bounds])]; |
| 29 [self setNeedsDisplay]; |
| 30 [self updateScaleFactor]; |
| 31 [[renderWidgetHostView_->cocoa_view() layer] addSublayer:self]; |
| 32 } |
| 33 return self; |
| 34 } |
| 35 |
| 36 - (BOOL)ensureContext { |
| 37 if (context_) |
| 38 return YES; |
| 39 |
| 40 if (!renderWidgetHostView_) |
| 41 return NO; |
| 42 |
| 43 if (renderWidgetHostView_->compositing_iosurface_) |
| 44 context_ = renderWidgetHostView_->compositing_iosurface_->context(); |
| 45 |
| 46 if (!context_) { |
| 47 context_ = content::CompositingIOSurfaceContext::Get( |
| 48 renderWidgetHostView_->window_number()); |
| 49 } |
| 50 |
| 51 return context_ ? YES : NO; |
| 52 } |
| 53 |
| 54 - (void)updateScaleFactor { |
| 55 if (!renderWidgetHostView_ || |
| 56 ![self respondsToSelector:(@selector(contentsScale))] || |
| 57 ![self respondsToSelector:(@selector(setContentsScale:))]) |
| 58 return; |
| 59 |
| 60 float current_scale_factor = [self contentsScale]; |
| 61 float new_scale_factor = current_scale_factor; |
| 62 if (renderWidgetHostView_->compositing_iosurface_) { |
| 63 new_scale_factor = |
| 64 renderWidgetHostView_->compositing_iosurface_->scale_factor(); |
| 65 } |
| 66 |
| 67 if (new_scale_factor == current_scale_factor) |
| 68 return; |
| 69 |
| 70 ScopedCAActionDisabler disabler; |
| 71 [self setContentsScale:new_scale_factor]; |
| 72 } |
| 73 |
| 74 - (void)disableCompositing{ |
| 75 ScopedCAActionDisabler disabler; |
| 76 [self removeFromSuperlayer]; |
| 77 renderWidgetHostView_ = nil; |
| 78 } |
| 79 |
| 80 // The remaining methods implement the CAOpenGLLayer interface. |
| 81 |
| 82 - (CGLContextObj)copyCGLContextForPixelFormat:(CGLPixelFormatObj)pixelFormat { |
| 83 if ([self ensureContext]) |
| 84 return context_->cgl_context(); |
| 85 return nil; |
| 86 } |
| 87 |
| 88 - (void)releaseCGLContext:(CGLContextObj)glContext { |
| 89 if (!context_) |
| 90 return; |
| 91 |
| 92 DCHECK(glContext == context_->cgl_context()); |
| 93 context_ = nil; |
| 94 } |
| 95 |
| 96 - (void)drawInCGLContext:(CGLContextObj)glContext |
| 97 pixelFormat:(CGLPixelFormatObj)pixelFormat |
| 98 forLayerTime:(CFTimeInterval)timeInterval |
| 99 displayTime:(const CVTimeStamp*)timeStamp { |
| 100 if (!context_ || !renderWidgetHostView_ || |
| 101 !renderWidgetHostView_->compositing_iosurface_) { |
| 102 glClearColor(1, 1, 1, 1); |
| 103 glClear(GL_COLOR_BUFFER_BIT); |
| 104 return; |
| 105 } |
| 106 |
| 107 DCHECK(glContext == context_->cgl_context()); |
| 108 |
| 109 gfx::Size window_size([self frame].size); |
| 110 float window_scale_factor = 1.f; |
| 111 if ([self respondsToSelector:(@selector(contentsScale))]) |
| 112 window_scale_factor = [self contentsScale]; |
| 113 |
| 114 renderWidgetHostView_->compositing_iosurface_->DrawIOSurface( |
| 115 window_size, |
| 116 window_scale_factor, |
| 117 renderWidgetHostView_->frame_subscriber(), |
| 118 true); |
| 119 |
| 120 renderWidgetHostView_->AckPendingSwapBuffers(); |
| 121 } |
| 122 |
| 123 @end |
| OLD | NEW |