| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "sky_surface.h" | 5 #import "sky_surface.h" |
| 6 | 6 |
| 7 #import <QuartzCore/QuartzCore.h> | 7 #import <QuartzCore/QuartzCore.h> |
| 8 #import <OpenGLES/EAGL.h> | 8 #import <OpenGLES/EAGL.h> |
| 9 #import <OpenGLES/EAGLDrawable.h> | 9 #import <OpenGLES/EAGLDrawable.h> |
| 10 | 10 |
| 11 #include "base/time/time.h" |
| 12 #include "mojo/public/cpp/bindings/interface_request.h" |
| 13 #include "sky/services/engine/input_event.mojom.h" |
| 11 #include "sky/shell/ios/platform_view_ios.h" | 14 #include "sky/shell/ios/platform_view_ios.h" |
| 15 #include "sky/shell/shell_view.h" |
| 12 #include "sky/shell/shell.h" | 16 #include "sky/shell/shell.h" |
| 13 #include "sky/shell/shell_view.h" | |
| 14 #include "sky/shell/ui_delegate.h" | 17 #include "sky/shell/ui_delegate.h" |
| 15 | 18 |
| 16 #include "mojo/public/cpp/bindings/interface_request.h" | |
| 17 #include "sky/services/viewport/input_event.mojom.h" | |
| 18 #include "base/time/time.h" | |
| 19 | |
| 20 static inline sky::EventType EventTypeFromUITouchPhase(UITouchPhase phase) { | 19 static inline sky::EventType EventTypeFromUITouchPhase(UITouchPhase phase) { |
| 21 switch (phase) { | 20 switch (phase) { |
| 22 case UITouchPhaseBegan: | 21 case UITouchPhaseBegan: |
| 23 return sky::EVENT_TYPE_POINTER_DOWN; | 22 return sky::EVENT_TYPE_POINTER_DOWN; |
| 24 case UITouchPhaseMoved: | 23 case UITouchPhaseMoved: |
| 25 case UITouchPhaseStationary: | 24 case UITouchPhaseStationary: |
| 26 // There is no EVENT_TYPE_POINTER_STATIONARY. So we just pass a move type | 25 // There is no EVENT_TYPE_POINTER_STATIONARY. So we just pass a move type |
| 27 // with the same coordinates | 26 // with the same coordinates |
| 28 return sky::EVENT_TYPE_POINTER_MOVE; | 27 return sky::EVENT_TYPE_POINTER_MOVE; |
| 29 case UITouchPhaseEnded: | 28 case UITouchPhaseEnded: |
| (...skipping 23 matching lines...) Expand all Loading... |
| 53 CGPoint windowCoordinates = [recognizer locationInView:recognizer.view]; | 52 CGPoint windowCoordinates = [recognizer locationInView:recognizer.view]; |
| 54 const CGFloat scale = [UIScreen mainScreen].scale; | 53 const CGFloat scale = [UIScreen mainScreen].scale; |
| 55 input->gesture_data->x = windowCoordinates.x * scale; | 54 input->gesture_data->x = windowCoordinates.x * scale; |
| 56 input->gesture_data->y = windowCoordinates.y * scale; | 55 input->gesture_data->y = windowCoordinates.y * scale; |
| 57 return input.Pass(); | 56 return input.Pass(); |
| 58 } | 57 } |
| 59 | 58 |
| 60 @implementation SkySurface { | 59 @implementation SkySurface { |
| 61 BOOL _platformViewInitialized; | 60 BOOL _platformViewInitialized; |
| 62 | 61 |
| 63 sky::ViewportObserverPtr _viewport_observer; | 62 sky::SkyEnginePtr _sky_engine; |
| 64 scoped_ptr<sky::shell::ShellView> _shell_view; | 63 scoped_ptr<sky::shell::ShellView> _shell_view; |
| 65 } | 64 } |
| 66 | 65 |
| 67 -(instancetype) initWithShellView:(sky::shell::ShellView *) shellView { | 66 -(instancetype) initWithShellView:(sky::shell::ShellView *) shellView { |
| 68 self = [super init]; | 67 self = [super init]; |
| 69 if (self) { | 68 if (self) { |
| 70 _shell_view.reset(shellView); | 69 _shell_view.reset(shellView); |
| 71 [self installGestureRecognizers]; | 70 [self installGestureRecognizers]; |
| 72 } | 71 } |
| 73 return self; | 72 return self; |
| 74 } | 73 } |
| 75 | 74 |
| 76 - (gfx::AcceleratedWidget)acceleratedWidget { | 75 - (gfx::AcceleratedWidget)acceleratedWidget { |
| 77 return (gfx::AcceleratedWidget)self.layer; | 76 return (gfx::AcceleratedWidget)self.layer; |
| 78 } | 77 } |
| 79 | 78 |
| 80 - (void)layoutSubviews { | 79 - (void)layoutSubviews { |
| 81 [super layoutSubviews]; | 80 [super layoutSubviews]; |
| 82 | 81 |
| 83 [self configureLayerDefaults]; | 82 [self configureLayerDefaults]; |
| 84 | 83 |
| 85 [self setupPlatformViewIfNecessary]; | 84 [self setupPlatformViewIfNecessary]; |
| 86 | 85 |
| 87 CGSize size = self.bounds.size; | 86 CGSize size = self.bounds.size; |
| 88 CGFloat scale = [UIScreen mainScreen].scale; | 87 CGFloat scale = [UIScreen mainScreen].scale; |
| 89 | 88 |
| 90 _viewport_observer->OnViewportMetricsChanged(size.width * scale, | 89 _sky_engine->OnViewportMetricsChanged(size.width * scale, |
| 91 size.height * scale, scale); | 90 size.height * scale, scale); |
| 92 } | 91 } |
| 93 | 92 |
| 94 - (void)configureLayerDefaults { | 93 - (void)configureLayerDefaults { |
| 95 CAEAGLLayer* layer = reinterpret_cast<CAEAGLLayer*>(self.layer); | 94 CAEAGLLayer* layer = reinterpret_cast<CAEAGLLayer*>(self.layer); |
| 96 layer.allowsGroupOpacity = YES; | 95 layer.allowsGroupOpacity = YES; |
| 97 layer.opaque = YES; | 96 layer.opaque = YES; |
| 98 CGFloat screenScale = [UIScreen mainScreen].scale; | 97 CGFloat screenScale = [UIScreen mainScreen].scale; |
| 99 layer.contentsScale = screenScale; | 98 layer.contentsScale = screenScale; |
| 100 // Note: shouldRasterize is still NO. This is just a defensive measure | 99 // Note: shouldRasterize is still NO. This is just a defensive measure |
| 101 layer.rasterizationScale = screenScale; | 100 layer.rasterizationScale = screenScale; |
| 102 } | 101 } |
| 103 | 102 |
| 104 - (void)setupPlatformViewIfNecessary { | 103 - (void)setupPlatformViewIfNecessary { |
| 105 if (_platformViewInitialized) { | 104 if (_platformViewInitialized) { |
| 106 return; | 105 return; |
| 107 } | 106 } |
| 108 | 107 |
| 109 _platformViewInitialized = YES; | 108 _platformViewInitialized = YES; |
| 110 | 109 |
| 111 [self notifySurfaceCreation]; | 110 [self notifySurfaceCreation]; |
| 112 [self connectToViewportObserverAndLoad]; | 111 [self connectToEngineAndLoad]; |
| 113 } | 112 } |
| 114 | 113 |
| 115 - (sky::shell::PlatformViewIOS*)platformView { | 114 - (sky::shell::PlatformViewIOS*)platformView { |
| 116 auto view = static_cast<sky::shell::PlatformViewIOS*>(_shell_view->view()); | 115 auto view = static_cast<sky::shell::PlatformViewIOS*>(_shell_view->view()); |
| 117 DCHECK(view); | 116 DCHECK(view); |
| 118 return view; | 117 return view; |
| 119 } | 118 } |
| 120 | 119 |
| 121 - (void)notifySurfaceCreation { | 120 - (void)notifySurfaceCreation { |
| 122 self.platformView->SurfaceCreated(self.acceleratedWidget); | 121 self.platformView->SurfaceCreated(self.acceleratedWidget); |
| 123 } | 122 } |
| 124 | 123 |
| 125 - (NSString*)skyInitialLoadURL { | 124 - (NSString*)skyInitialLoadURL { |
| 126 return [NSBundle mainBundle].infoDictionary[@"com.google.sky.load_url"]; | 125 return [NSBundle mainBundle].infoDictionary[@"com.google.sky.load_url"]; |
| 127 } | 126 } |
| 128 | 127 |
| 129 - (void)connectToViewportObserverAndLoad { | 128 - (void)connectToEngineAndLoad { |
| 130 auto interface_request = mojo::GetProxy(&_viewport_observer); | 129 auto interface_request = mojo::GetProxy(&_sky_engine); |
| 131 self.platformView->ConnectToViewportObserver(interface_request.Pass()); | 130 self.platformView->ConnectToEngine(interface_request.Pass()); |
| 132 | 131 |
| 133 mojo::String string(self.skyInitialLoadURL.UTF8String); | 132 mojo::String string(self.skyInitialLoadURL.UTF8String); |
| 134 _viewport_observer->RunFromNetwork(string); | 133 _sky_engine->RunFromNetwork(string); |
| 135 } | 134 } |
| 136 | 135 |
| 137 - (void)notifySurfaceDestruction { | 136 - (void)notifySurfaceDestruction { |
| 138 self.platformView->SurfaceDestroyed(); | 137 self.platformView->SurfaceDestroyed(); |
| 139 } | 138 } |
| 140 | 139 |
| 141 #pragma mark - UIResponder overrides for raw touches | 140 #pragma mark - UIResponder overrides for raw touches |
| 142 | 141 |
| 143 - (void)dispatchTouches:(NSSet*)touches phase:(UITouchPhase)phase { | 142 - (void)dispatchTouches:(NSSet*)touches phase:(UITouchPhase)phase { |
| 144 auto eventType = EventTypeFromUITouchPhase(phase); | 143 auto eventType = EventTypeFromUITouchPhase(phase); |
| 145 const CGFloat scale = [UIScreen mainScreen].scale; | 144 const CGFloat scale = [UIScreen mainScreen].scale; |
| 146 | 145 |
| 147 for (UITouch* touch in touches) { | 146 for (UITouch* touch in touches) { |
| 148 auto input = sky::InputEvent::New(); | 147 auto input = sky::InputEvent::New(); |
| 149 input->type = eventType; | 148 input->type = eventType; |
| 150 input->time_stamp = InputEventTimestampFromNSTimeInterval(touch.timestamp); | 149 input->time_stamp = InputEventTimestampFromNSTimeInterval(touch.timestamp); |
| 151 | 150 |
| 152 input->pointer_data = sky::PointerData::New(); | 151 input->pointer_data = sky::PointerData::New(); |
| 153 input->pointer_data->kind = sky::POINTER_KIND_TOUCH; | 152 input->pointer_data->kind = sky::POINTER_KIND_TOUCH; |
| 154 | 153 |
| 155 CGPoint windowCoordinates = [touch locationInView:nil]; | 154 CGPoint windowCoordinates = [touch locationInView:nil]; |
| 156 | 155 |
| 157 input->pointer_data->x = windowCoordinates.x * scale; | 156 input->pointer_data->x = windowCoordinates.x * scale; |
| 158 input->pointer_data->y = windowCoordinates.y * scale; | 157 input->pointer_data->y = windowCoordinates.y * scale; |
| 159 | 158 |
| 160 _viewport_observer->OnInputEvent(input.Pass()); | 159 _sky_engine->OnInputEvent(input.Pass()); |
| 161 } | 160 } |
| 162 } | 161 } |
| 163 | 162 |
| 164 - (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event { | 163 - (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event { |
| 165 [self dispatchTouches:touches phase:UITouchPhaseBegan]; | 164 [self dispatchTouches:touches phase:UITouchPhaseBegan]; |
| 166 } | 165 } |
| 167 | 166 |
| 168 - (void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event { | 167 - (void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event { |
| 169 [self dispatchTouches:touches phase:UITouchPhaseMoved]; | 168 [self dispatchTouches:touches phase:UITouchPhaseMoved]; |
| 170 } | 169 } |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 216 } | 215 } |
| 217 | 216 |
| 218 -(void) onFling:(UISwipeGestureRecognizer *) recognizer { | 217 -(void) onFling:(UISwipeGestureRecognizer *) recognizer { |
| 219 // Swipes are discrete gestures already. So there is no equivalent to a cancel | 218 // Swipes are discrete gestures already. So there is no equivalent to a cancel |
| 220 if (recognizer.state != UIGestureRecognizerStateEnded) { | 219 if (recognizer.state != UIGestureRecognizerStateEnded) { |
| 221 return; | 220 return; |
| 222 } | 221 } |
| 223 | 222 |
| 224 auto input = BasicInputEventFromRecognizer( | 223 auto input = BasicInputEventFromRecognizer( |
| 225 sky::EVENT_TYPE_GESTURE_FLING_START, recognizer); | 224 sky::EVENT_TYPE_GESTURE_FLING_START, recognizer); |
| 226 _viewport_observer->OnInputEvent(input.Pass()); | 225 _sky_engine->OnInputEvent(input.Pass()); |
| 227 } | 226 } |
| 228 | 227 |
| 229 -(void) onLongPress:(UILongPressGestureRecognizer *) recognizer { | 228 -(void) onLongPress:(UILongPressGestureRecognizer *) recognizer { |
| 230 if (recognizer.state != UIGestureRecognizerStateEnded) { | 229 if (recognizer.state != UIGestureRecognizerStateEnded) { |
| 231 return; | 230 return; |
| 232 } | 231 } |
| 233 | 232 |
| 234 auto input = BasicInputEventFromRecognizer(sky::EVENT_TYPE_GESTURE_LONG_PRESS, | 233 auto input = BasicInputEventFromRecognizer(sky::EVENT_TYPE_GESTURE_LONG_PRESS, |
| 235 recognizer); | 234 recognizer); |
| 236 _viewport_observer->OnInputEvent(input.Pass()); | 235 _sky_engine->OnInputEvent(input.Pass()); |
| 237 } | 236 } |
| 238 | 237 |
| 239 -(void) onScroll:(UIPanGestureRecognizer *) recognizer { | 238 -(void) onScroll:(UIPanGestureRecognizer *) recognizer { |
| 240 sky::EventType type = sky::EVENT_TYPE_UNKNOWN; | 239 sky::EventType type = sky::EVENT_TYPE_UNKNOWN; |
| 241 switch (recognizer.state) { | 240 switch (recognizer.state) { |
| 242 case UIGestureRecognizerStateBegan: | 241 case UIGestureRecognizerStateBegan: |
| 243 type = sky::EVENT_TYPE_GESTURE_SCROLL_BEGIN; | 242 type = sky::EVENT_TYPE_GESTURE_SCROLL_BEGIN; |
| 244 break; | 243 break; |
| 245 case UIGestureRecognizerStateChanged: | 244 case UIGestureRecognizerStateChanged: |
| 246 type = sky::EVENT_TYPE_GESTURE_SCROLL_UPDATE; | 245 type = sky::EVENT_TYPE_GESTURE_SCROLL_UPDATE; |
| (...skipping 14 matching lines...) Expand all Loading... |
| 261 auto input = BasicInputEventFromRecognizer(type, recognizer); | 260 auto input = BasicInputEventFromRecognizer(type, recognizer); |
| 262 auto scale = [UIScreen mainScreen].scale; | 261 auto scale = [UIScreen mainScreen].scale; |
| 263 auto translation = [recognizer translationInView: self]; | 262 auto translation = [recognizer translationInView: self]; |
| 264 auto velocity = [recognizer velocityInView: self]; | 263 auto velocity = [recognizer velocityInView: self]; |
| 265 | 264 |
| 266 input->gesture_data->dx = translation.x * scale; | 265 input->gesture_data->dx = translation.x * scale; |
| 267 input->gesture_data->dy = translation.y * scale; | 266 input->gesture_data->dy = translation.y * scale; |
| 268 input->gesture_data->velocityX = velocity.x * scale; | 267 input->gesture_data->velocityX = velocity.x * scale; |
| 269 input->gesture_data->velocityY = velocity.y * scale; | 268 input->gesture_data->velocityY = velocity.y * scale; |
| 270 | 269 |
| 271 _viewport_observer->OnInputEvent(input.Pass()); | 270 _sky_engine->OnInputEvent(input.Pass()); |
| 272 } | 271 } |
| 273 | 272 |
| 274 -(void) onTap:(UITapGestureRecognizer *) recognizer { | 273 -(void) onTap:(UITapGestureRecognizer *) recognizer { |
| 275 | 274 |
| 276 if (recognizer.state != UIGestureRecognizerStateEnded) { | 275 if (recognizer.state != UIGestureRecognizerStateEnded) { |
| 277 return; | 276 return; |
| 278 } | 277 } |
| 279 | 278 |
| 280 auto input = BasicInputEventFromRecognizer(sky::EVENT_TYPE_GESTURE_TAP, | 279 auto input = BasicInputEventFromRecognizer(sky::EVENT_TYPE_GESTURE_TAP, |
| 281 recognizer); | 280 recognizer); |
| 282 _viewport_observer->OnInputEvent(input.Pass()); | 281 _sky_engine->OnInputEvent(input.Pass()); |
| 283 } | 282 } |
| 284 | 283 |
| 285 #pragma mark - Misc. | 284 #pragma mark - Misc. |
| 286 | 285 |
| 287 + (Class)layerClass { | 286 + (Class)layerClass { |
| 288 return [CAEAGLLayer class]; | 287 return [CAEAGLLayer class]; |
| 289 } | 288 } |
| 290 | 289 |
| 291 - (void)dealloc { | 290 - (void)dealloc { |
| 292 [self notifySurfaceDestruction]; | 291 [self notifySurfaceDestruction]; |
| 293 [super dealloc]; | 292 [super dealloc]; |
| 294 } | 293 } |
| 295 | 294 |
| 296 @end | 295 @end |
| OLD | NEW |