bsalomon@google.com | f75b84e | 2011-09-29 14:58:28 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
robertphillips@google.com | 5b5bba3 | 2012-09-24 14:20:00 +0000 | [diff] [blame] | 8 | #import "SkNSView.h" |
yangsu@google.com | 12d177d | 2011-08-01 17:07:12 +0000 | [diff] [blame] | 9 | #include "SkCanvas.h" |
reed | 0397e9f | 2014-09-18 11:29:01 -0700 | [diff] [blame] | 10 | #include "SkSurface.h" |
yangsu@google.com | 12d177d | 2011-08-01 17:07:12 +0000 | [diff] [blame] | 11 | #include "SkCGUtils.h" |
| 12 | #include "SkEvent.h" |
bungeman | 99fe822 | 2015-08-20 07:57:51 -0700 | [diff] [blame] | 13 | static_assert(SK_SUPPORT_GPU, "not_implemented_for_non_gpu_build"); |
commit-bot@chromium.org | 6a18079 | 2014-01-15 21:39:18 +0000 | [diff] [blame] | 14 | #include <OpenGL/gl.h> |
yangsu@google.com | 12d177d | 2011-08-01 17:07:12 +0000 | [diff] [blame] | 15 | |
Ben Wagner | 55963bc | 2017-07-24 15:00:35 +0000 | [diff] [blame] | 16 | //#define FORCE_REDRAW |
bungeman@google.com | b20a1b5 | 2013-06-28 16:44:55 +0000 | [diff] [blame] | 17 | // Can be dropped when we no longer support 10.6. |
Mike Klein | 04ef48c | 2017-03-15 09:43:04 -0700 | [diff] [blame] | 18 | #if defined(MAC_OS_X_VERSION_10_7) && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_7 |
| 19 | #define RETINA_API_AVAILABLE 1 |
| 20 | #else |
| 21 | #define RETINA_API_AVAILABLE 0 |
| 22 | #endif |
| 23 | |
yangsu@google.com | 12d177d | 2011-08-01 17:07:12 +0000 | [diff] [blame] | 24 | @implementation SkNSView |
bsalomon@google.com | 2e40173 | 2012-08-03 19:25:10 +0000 | [diff] [blame] | 25 | @synthesize fWind, fTitle, fOptionsDelegate, fGLContext; |
yangsu@google.com | 12d177d | 2011-08-01 17:07:12 +0000 | [diff] [blame] | 26 | |
Jim Van Verth | 57a98fc | 2016-10-28 13:35:50 -0400 | [diff] [blame] | 27 | BOOL fRedrawRequestPending; |
| 28 | |
yangsu@google.com | 12d177d | 2011-08-01 17:07:12 +0000 | [diff] [blame] | 29 | - (id)initWithCoder:(NSCoder*)coder { |
| 30 | if ((self = [super initWithCoder:coder])) { |
yangsu@google.com | a854041 | 2011-08-30 14:40:49 +0000 | [diff] [blame] | 31 | self = [self initWithDefaults]; |
| 32 | [self setUpWindow]; |
yangsu@google.com | 12d177d | 2011-08-01 17:07:12 +0000 | [diff] [blame] | 33 | } |
| 34 | return self; |
| 35 | } |
| 36 | |
| 37 | - (id)initWithFrame:(NSRect)frameRect { |
caryclark@google.com | 679ab31 | 2012-06-06 12:04:00 +0000 | [diff] [blame] | 38 | if ((self = [super initWithFrame:frameRect])) { |
yangsu@google.com | a854041 | 2011-08-30 14:40:49 +0000 | [diff] [blame] | 39 | self = [self initWithDefaults]; |
| 40 | [self setUpWindow]; |
yangsu@google.com | 12d177d | 2011-08-01 17:07:12 +0000 | [diff] [blame] | 41 | } |
| 42 | return self; |
| 43 | } |
| 44 | |
yangsu@google.com | a854041 | 2011-08-30 14:40:49 +0000 | [diff] [blame] | 45 | - (id)initWithDefaults { |
bungeman@google.com | b20a1b5 | 2013-06-28 16:44:55 +0000 | [diff] [blame] | 46 | #if RETINA_API_AVAILABLE |
| 47 | [self setWantsBestResolutionOpenGLSurface:YES]; |
| 48 | #endif |
yangsu@google.com | 12d177d | 2011-08-01 17:07:12 +0000 | [diff] [blame] | 49 | fRedrawRequestPending = false; |
yangsu@google.com | a854041 | 2011-08-30 14:40:49 +0000 | [diff] [blame] | 50 | fWind = NULL; |
yangsu@google.com | 12d177d | 2011-08-01 17:07:12 +0000 | [diff] [blame] | 51 | return self; |
| 52 | } |
| 53 | |
Yuqian Li | 6e3d995 | 2017-09-27 13:25:36 -0400 | [diff] [blame] | 54 | - (void)setNSViewSize:(NSSize)size { |
| 55 | NSWindow* w = [self window]; |
Chris Dalton | 1fbb07a | 2017-10-06 11:33:36 -0600 | [diff] [blame^] | 56 | NSRect f = w.frame; |
Yuqian Li | 6e3d995 | 2017-09-27 13:25:36 -0400 | [diff] [blame] | 57 | f.size = size; |
| 58 | [w setFrame:f display:YES]; |
| 59 | } |
| 60 | |
yangsu@google.com | a854041 | 2011-08-30 14:40:49 +0000 | [diff] [blame] | 61 | - (void)setUpWindow { |
bungeman@google.com | b20a1b5 | 2013-06-28 16:44:55 +0000 | [diff] [blame] | 62 | [[NSNotificationCenter defaultCenter] addObserver:self |
| 63 | selector:@selector(backingPropertiesChanged:) |
| 64 | name:@"NSWindowDidChangeBackingPropertiesNotification" |
| 65 | object:[self window]]; |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 66 | if (fWind) { |
yangsu@google.com | a854041 | 2011-08-30 14:40:49 +0000 | [diff] [blame] | 67 | fWind->setVisibleP(true); |
bungeman@google.com | b20a1b5 | 2013-06-28 16:44:55 +0000 | [diff] [blame] | 68 | NSSize size = self.frame.size; |
| 69 | #if RETINA_API_AVAILABLE |
| 70 | size = [self convertSizeToBacking:self.frame.size]; |
| 71 | #endif |
kkinnunen | 973d92c | 2016-01-18 01:18:34 -0800 | [diff] [blame] | 72 | fWind->resize((int) size.width, (int) size.height); |
bsalomon | 8419591 | 2015-06-23 14:03:47 -0700 | [diff] [blame] | 73 | [[self window] setAcceptsMouseMovedEvents:YES]; |
yangsu@google.com | a854041 | 2011-08-30 14:40:49 +0000 | [diff] [blame] | 74 | } |
| 75 | } |
| 76 | |
yangsu@google.com | 12d177d | 2011-08-01 17:07:12 +0000 | [diff] [blame] | 77 | -(BOOL) isFlipped { |
| 78 | return YES; |
| 79 | } |
| 80 | |
| 81 | - (BOOL)acceptsFirstResponder { |
| 82 | return YES; |
| 83 | } |
| 84 | |
bungeman@google.com | b20a1b5 | 2013-06-28 16:44:55 +0000 | [diff] [blame] | 85 | - (float)scaleFactor { |
| 86 | NSWindow *window = [self window]; |
| 87 | #if RETINA_API_AVAILABLE |
| 88 | if (window) { |
| 89 | return [window backingScaleFactor]; |
| 90 | } |
| 91 | return [[NSScreen mainScreen] backingScaleFactor]; |
| 92 | #else |
| 93 | if (window) { |
| 94 | return [window userSpaceScaleFactor]; |
| 95 | } |
| 96 | return [[NSScreen mainScreen] userSpaceScaleFactor]; |
| 97 | #endif |
| 98 | } |
| 99 | |
| 100 | - (void)backingPropertiesChanged:(NSNotification *)notification { |
bungeman@google.com | 44fb1fa | 2013-06-28 17:21:35 +0000 | [diff] [blame] | 101 | CGFloat oldBackingScaleFactor = (CGFloat)[ |
bungeman@google.com | b20a1b5 | 2013-06-28 16:44:55 +0000 | [diff] [blame] | 102 | [notification.userInfo objectForKey:@"NSBackingPropertyOldScaleFactorKey"] doubleValue |
| 103 | ]; |
| 104 | CGFloat newBackingScaleFactor = [self scaleFactor]; |
| 105 | if (oldBackingScaleFactor == newBackingScaleFactor) { |
| 106 | return; |
| 107 | } |
Yuqian Li | 6e3d995 | 2017-09-27 13:25:36 -0400 | [diff] [blame] | 108 | |
bungeman@google.com | b20a1b5 | 2013-06-28 16:44:55 +0000 | [diff] [blame] | 109 | // TODO: need a better way to force a refresh (that works). |
| 110 | // [fGLContext update] does not appear to update if the point size has not changed, |
| 111 | // even if the backing size has changed. |
| 112 | [self setFrameSize:NSMakeSize(self.frame.size.width + 1, self.frame.size.height + 1)]; |
| 113 | } |
| 114 | |
suyang1 | bb3f4a8 | 2011-10-10 16:38:58 +0000 | [diff] [blame] | 115 | - (void)resizeSkView:(NSSize)newSize { |
bungeman@google.com | b20a1b5 | 2013-06-28 16:44:55 +0000 | [diff] [blame] | 116 | #if RETINA_API_AVAILABLE |
| 117 | newSize = [self convertSizeToBacking:newSize]; |
| 118 | #endif |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 119 | if (fWind && (fWind->width() != newSize.width || fWind->height() != newSize.height)) { |
caryclark@google.com | 679ab31 | 2012-06-06 12:04:00 +0000 | [diff] [blame] | 120 | fWind->resize((int) newSize.width, (int) newSize.height); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 121 | if (fGLContext) { |
caryclark@google.com | 594dd3c | 2012-09-24 19:33:57 +0000 | [diff] [blame] | 122 | glClear(GL_STENCIL_BUFFER_BIT); |
bungeman@google.com | b20a1b5 | 2013-06-28 16:44:55 +0000 | [diff] [blame] | 123 | [fGLContext update]; |
caryclark@google.com | 594dd3c | 2012-09-24 19:33:57 +0000 | [diff] [blame] | 124 | } |
yangsu@google.com | 12d177d | 2011-08-01 17:07:12 +0000 | [diff] [blame] | 125 | } |
yangsu@google.com | 12d177d | 2011-08-01 17:07:12 +0000 | [diff] [blame] | 126 | } |
| 127 | |
suyang1 | bb3f4a8 | 2011-10-10 16:38:58 +0000 | [diff] [blame] | 128 | - (void) setFrameSize:(NSSize)newSize { |
| 129 | [super setFrameSize:newSize]; |
| 130 | [self resizeSkView:newSize]; |
| 131 | } |
| 132 | |
yangsu@google.com | 12d177d | 2011-08-01 17:07:12 +0000 | [diff] [blame] | 133 | - (void)dealloc { |
reed | d3ce6b4 | 2014-10-07 06:42:09 -0700 | [diff] [blame] | 134 | [self freeNativeWind]; |
yangsu@google.com | 12d177d | 2011-08-01 17:07:12 +0000 | [diff] [blame] | 135 | self.fGLContext = nil; |
| 136 | self.fTitle = nil; |
yangsu@google.com | 12d177d | 2011-08-01 17:07:12 +0000 | [diff] [blame] | 137 | [super dealloc]; |
| 138 | } |
| 139 | |
reed | d3ce6b4 | 2014-10-07 06:42:09 -0700 | [diff] [blame] | 140 | - (void)freeNativeWind { |
| 141 | delete fWind; |
| 142 | fWind = nil; |
| 143 | } |
| 144 | |
suyang1 | bb3f4a8 | 2011-10-10 16:38:58 +0000 | [diff] [blame] | 145 | //////////////////////////////////////////////////////////////////////////////// |
yangsu@google.com | 12d177d | 2011-08-01 17:07:12 +0000 | [diff] [blame] | 146 | |
yangsu@google.com | f3493f0 | 2011-08-08 15:12:05 +0000 | [diff] [blame] | 147 | - (void)drawSkia { |
yangsu@google.com | 12d177d | 2011-08-01 17:07:12 +0000 | [diff] [blame] | 148 | fRedrawRequestPending = false; |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 149 | if (fWind) { |
Ben Wagner | 55963bc | 2017-07-24 15:00:35 +0000 | [diff] [blame] | 150 | sk_sp<SkSurface> surface(fWind->makeSurface()); |
| 151 | fWind->draw(surface->getCanvas()); |
| 152 | #ifdef FORCE_REDRAW |
| 153 | fWind->inval(NULL); |
| 154 | #endif |
yangsu@google.com | a854041 | 2011-08-30 14:40:49 +0000 | [diff] [blame] | 155 | } |
yangsu@google.com | 12d177d | 2011-08-01 17:07:12 +0000 | [diff] [blame] | 156 | } |
| 157 | |
yangsu@google.com | f3493f0 | 2011-08-08 15:12:05 +0000 | [diff] [blame] | 158 | - (void)setSkTitle:(const char *)title { |
| 159 | self.fTitle = [NSString stringWithUTF8String:title]; |
| 160 | [[self window] setTitle:self.fTitle]; |
| 161 | } |
| 162 | |
| 163 | - (BOOL)onHandleEvent:(const SkEvent&)evt { |
| 164 | return false; |
| 165 | } |
| 166 | |
| 167 | #include "SkOSMenu.h" |
| 168 | - (void)onAddMenu:(const SkOSMenu*)menu { |
| 169 | [self.fOptionsDelegate view:self didAddMenu:menu]; |
| 170 | } |
| 171 | |
| 172 | - (void)onUpdateMenu:(const SkOSMenu*)menu { |
| 173 | [self.fOptionsDelegate view:self didUpdateMenu:menu]; |
| 174 | } |
| 175 | |
| 176 | - (void)postInvalWithRect:(const SkIRect*)r { |
| 177 | if (!fRedrawRequestPending) { |
| 178 | fRedrawRequestPending = true; |
yangsu@google.com | a854041 | 2011-08-30 14:40:49 +0000 | [diff] [blame] | 179 | [self setNeedsDisplay:YES]; |
yangsu@google.com | f3493f0 | 2011-08-08 15:12:05 +0000 | [diff] [blame] | 180 | [self performSelector:@selector(drawSkia) withObject:nil afterDelay:0]; |
| 181 | } |
yangsu@google.com | 12d177d | 2011-08-01 17:07:12 +0000 | [diff] [blame] | 182 | } |
| 183 | /////////////////////////////////////////////////////////////////////////////// |
| 184 | |
| 185 | #include "SkKey.h" |
| 186 | enum { |
robertphillips | ecf3dbe | 2016-07-28 15:17:34 -0700 | [diff] [blame] | 187 | SK_MacReturnKey = 36, |
| 188 | SK_MacDeleteKey = 51, |
| 189 | SK_MacEndKey = 119, |
| 190 | SK_MacLeftKey = 123, |
| 191 | SK_MacRightKey = 124, |
| 192 | SK_MacDownKey = 125, |
| 193 | SK_MacUpKey = 126, |
yangsu@google.com | 12d177d | 2011-08-01 17:07:12 +0000 | [diff] [blame] | 194 | SK_Mac0Key = 0x52, |
| 195 | SK_Mac1Key = 0x53, |
| 196 | SK_Mac2Key = 0x54, |
| 197 | SK_Mac3Key = 0x55, |
| 198 | SK_Mac4Key = 0x56, |
| 199 | SK_Mac5Key = 0x57, |
| 200 | SK_Mac6Key = 0x58, |
| 201 | SK_Mac7Key = 0x59, |
| 202 | SK_Mac8Key = 0x5b, |
| 203 | SK_Mac9Key = 0x5c |
| 204 | }; |
| 205 | |
| 206 | static SkKey raw2key(UInt32 raw) |
| 207 | { |
robertphillips | ecf3dbe | 2016-07-28 15:17:34 -0700 | [diff] [blame] | 208 | static const struct { |
| 209 | UInt32 fRaw; |
| 210 | SkKey fKey; |
| 211 | } gKeys[] = { |
| 212 | { SK_MacUpKey, kUp_SkKey }, |
| 213 | { SK_MacDownKey, kDown_SkKey }, |
| 214 | { SK_MacLeftKey, kLeft_SkKey }, |
| 215 | { SK_MacRightKey, kRight_SkKey }, |
| 216 | { SK_MacReturnKey, kOK_SkKey }, |
| 217 | { SK_MacDeleteKey, kBack_SkKey }, |
| 218 | { SK_MacEndKey, kEnd_SkKey }, |
yangsu@google.com | 12d177d | 2011-08-01 17:07:12 +0000 | [diff] [blame] | 219 | { SK_Mac0Key, k0_SkKey }, |
| 220 | { SK_Mac1Key, k1_SkKey }, |
| 221 | { SK_Mac2Key, k2_SkKey }, |
| 222 | { SK_Mac3Key, k3_SkKey }, |
yangsu@google.com | f3493f0 | 2011-08-08 15:12:05 +0000 | [diff] [blame] | 223 | { SK_Mac4Key, k4_SkKey }, |
yangsu@google.com | 12d177d | 2011-08-01 17:07:12 +0000 | [diff] [blame] | 224 | { SK_Mac5Key, k5_SkKey }, |
| 225 | { SK_Mac6Key, k6_SkKey }, |
| 226 | { SK_Mac7Key, k7_SkKey }, |
| 227 | { SK_Mac8Key, k8_SkKey }, |
| 228 | { SK_Mac9Key, k9_SkKey } |
robertphillips | ecf3dbe | 2016-07-28 15:17:34 -0700 | [diff] [blame] | 229 | }; |
Yuqian Li | 6e3d995 | 2017-09-27 13:25:36 -0400 | [diff] [blame] | 230 | |
robertphillips | ecf3dbe | 2016-07-28 15:17:34 -0700 | [diff] [blame] | 231 | for (unsigned i = 0; i < SK_ARRAY_COUNT(gKeys); i++) |
| 232 | if (gKeys[i].fRaw == raw) |
| 233 | return gKeys[i].fKey; |
| 234 | return kNONE_SkKey; |
yangsu@google.com | 12d177d | 2011-08-01 17:07:12 +0000 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | - (void)keyDown:(NSEvent *)event { |
yangsu@google.com | 849ad5a | 2011-08-30 15:27:57 +0000 | [diff] [blame] | 238 | if (NULL == fWind) |
yangsu@google.com | a854041 | 2011-08-30 14:40:49 +0000 | [diff] [blame] | 239 | return; |
Yuqian Li | 6e3d995 | 2017-09-27 13:25:36 -0400 | [diff] [blame] | 240 | |
yangsu@google.com | 12d177d | 2011-08-01 17:07:12 +0000 | [diff] [blame] | 241 | SkKey key = raw2key([event keyCode]); |
| 242 | if (kNONE_SkKey != key) |
| 243 | fWind->handleKey(key); |
| 244 | else{ |
| 245 | unichar c = [[event characters] characterAtIndex:0]; |
| 246 | fWind->handleChar((SkUnichar)c); |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | - (void)keyUp:(NSEvent *)event { |
yangsu@google.com | 849ad5a | 2011-08-30 15:27:57 +0000 | [diff] [blame] | 251 | if (NULL == fWind) |
yangsu@google.com | a854041 | 2011-08-30 14:40:49 +0000 | [diff] [blame] | 252 | return; |
Yuqian Li | 6e3d995 | 2017-09-27 13:25:36 -0400 | [diff] [blame] | 253 | |
yangsu@google.com | 12d177d | 2011-08-01 17:07:12 +0000 | [diff] [blame] | 254 | SkKey key = raw2key([event keyCode]); |
| 255 | if (kNONE_SkKey != key) |
| 256 | fWind->handleKeyUp(key); |
caryclark@google.com | 679ab31 | 2012-06-06 12:04:00 +0000 | [diff] [blame] | 257 | // else |
| 258 | // unichar c = [[event characters] characterAtIndex:0]; |
yangsu@google.com | 12d177d | 2011-08-01 17:07:12 +0000 | [diff] [blame] | 259 | } |
| 260 | |
reed@google.com | 4d5c26d | 2013-01-08 16:17:50 +0000 | [diff] [blame] | 261 | static const struct { |
| 262 | unsigned fNSModifierMask; |
| 263 | unsigned fSkModifierMask; |
| 264 | } gModifierMasks[] = { |
| 265 | { NSAlphaShiftKeyMask, kShift_SkModifierKey }, |
| 266 | { NSShiftKeyMask, kShift_SkModifierKey }, |
| 267 | { NSControlKeyMask, kControl_SkModifierKey }, |
| 268 | { NSAlternateKeyMask, kOption_SkModifierKey }, |
| 269 | { NSCommandKeyMask, kCommand_SkModifierKey }, |
| 270 | }; |
| 271 | |
| 272 | static unsigned convertNSModifiersToSk(NSUInteger nsModi) { |
| 273 | unsigned skModi = 0; |
| 274 | for (size_t i = 0; i < SK_ARRAY_COUNT(gModifierMasks); ++i) { |
| 275 | if (nsModi & gModifierMasks[i].fNSModifierMask) { |
| 276 | skModi |= gModifierMasks[i].fSkModifierMask; |
| 277 | } |
| 278 | } |
| 279 | return skModi; |
| 280 | } |
| 281 | |
yangsu@google.com | 12d177d | 2011-08-01 17:07:12 +0000 | [diff] [blame] | 282 | - (void)mouseDown:(NSEvent *)event { |
| 283 | NSPoint p = [event locationInWindow]; |
reed@google.com | 4d5c26d | 2013-01-08 16:17:50 +0000 | [diff] [blame] | 284 | unsigned modi = convertNSModifiersToSk([event modifierFlags]); |
| 285 | |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 286 | if ([self mouse:p inRect:[self bounds]] && fWind) { |
yangsu@google.com | 12d177d | 2011-08-01 17:07:12 +0000 | [diff] [blame] | 287 | NSPoint loc = [self convertPoint:p fromView:nil]; |
bungeman@google.com | b20a1b5 | 2013-06-28 16:44:55 +0000 | [diff] [blame] | 288 | #if RETINA_API_AVAILABLE |
| 289 | loc = [self convertPointToBacking:loc]; //y-up |
| 290 | loc.y = -loc.y; |
| 291 | #endif |
reed@google.com | 4d5c26d | 2013-01-08 16:17:50 +0000 | [diff] [blame] | 292 | fWind->handleClick((int) loc.x, (int) loc.y, |
| 293 | SkView::Click::kDown_State, self, modi); |
yangsu@google.com | 12d177d | 2011-08-01 17:07:12 +0000 | [diff] [blame] | 294 | } |
| 295 | } |
| 296 | |
| 297 | - (void)mouseDragged:(NSEvent *)event { |
| 298 | NSPoint p = [event locationInWindow]; |
reed@google.com | 4d5c26d | 2013-01-08 16:17:50 +0000 | [diff] [blame] | 299 | unsigned modi = convertNSModifiersToSk([event modifierFlags]); |
| 300 | |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 301 | if ([self mouse:p inRect:[self bounds]] && fWind) { |
yangsu@google.com | 12d177d | 2011-08-01 17:07:12 +0000 | [diff] [blame] | 302 | NSPoint loc = [self convertPoint:p fromView:nil]; |
bungeman@google.com | b20a1b5 | 2013-06-28 16:44:55 +0000 | [diff] [blame] | 303 | #if RETINA_API_AVAILABLE |
| 304 | loc = [self convertPointToBacking:loc]; //y-up |
| 305 | loc.y = -loc.y; |
| 306 | #endif |
reed@google.com | 4d5c26d | 2013-01-08 16:17:50 +0000 | [diff] [blame] | 307 | fWind->handleClick((int) loc.x, (int) loc.y, |
| 308 | SkView::Click::kMoved_State, self, modi); |
yangsu@google.com | 12d177d | 2011-08-01 17:07:12 +0000 | [diff] [blame] | 309 | } |
| 310 | } |
| 311 | |
suyang1 | bb3f4a8 | 2011-10-10 16:38:58 +0000 | [diff] [blame] | 312 | - (void)mouseMoved:(NSEvent *)event { |
| 313 | NSPoint p = [event locationInWindow]; |
reed@google.com | 4d5c26d | 2013-01-08 16:17:50 +0000 | [diff] [blame] | 314 | unsigned modi = convertNSModifiersToSk([event modifierFlags]); |
Yuqian Li | 6e3d995 | 2017-09-27 13:25:36 -0400 | [diff] [blame] | 315 | |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 316 | if ([self mouse:p inRect:[self bounds]] && fWind) { |
suyang1 | bb3f4a8 | 2011-10-10 16:38:58 +0000 | [diff] [blame] | 317 | NSPoint loc = [self convertPoint:p fromView:nil]; |
bungeman@google.com | b20a1b5 | 2013-06-28 16:44:55 +0000 | [diff] [blame] | 318 | #if RETINA_API_AVAILABLE |
| 319 | loc = [self convertPointToBacking:loc]; //y-up |
| 320 | loc.y = -loc.y; |
| 321 | #endif |
reed@google.com | 4d5c26d | 2013-01-08 16:17:50 +0000 | [diff] [blame] | 322 | fWind->handleClick((int) loc.x, (int) loc.y, |
| 323 | SkView::Click::kMoved_State, self, modi); |
suyang1 | bb3f4a8 | 2011-10-10 16:38:58 +0000 | [diff] [blame] | 324 | } |
| 325 | } |
| 326 | |
yangsu@google.com | 12d177d | 2011-08-01 17:07:12 +0000 | [diff] [blame] | 327 | - (void)mouseUp:(NSEvent *)event { |
| 328 | NSPoint p = [event locationInWindow]; |
reed@google.com | 4d5c26d | 2013-01-08 16:17:50 +0000 | [diff] [blame] | 329 | unsigned modi = convertNSModifiersToSk([event modifierFlags]); |
Yuqian Li | 6e3d995 | 2017-09-27 13:25:36 -0400 | [diff] [blame] | 330 | |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 331 | if ([self mouse:p inRect:[self bounds]] && fWind) { |
yangsu@google.com | 12d177d | 2011-08-01 17:07:12 +0000 | [diff] [blame] | 332 | NSPoint loc = [self convertPoint:p fromView:nil]; |
bungeman@google.com | b20a1b5 | 2013-06-28 16:44:55 +0000 | [diff] [blame] | 333 | #if RETINA_API_AVAILABLE |
| 334 | loc = [self convertPointToBacking:loc]; //y-up |
| 335 | loc.y = -loc.y; |
| 336 | #endif |
reed@google.com | 4d5c26d | 2013-01-08 16:17:50 +0000 | [diff] [blame] | 337 | fWind->handleClick((int) loc.x, (int) loc.y, |
| 338 | SkView::Click::kUp_State, self, modi); |
yangsu@google.com | 12d177d | 2011-08-01 17:07:12 +0000 | [diff] [blame] | 339 | } |
| 340 | } |
| 341 | |
yangsu@google.com | 12d177d | 2011-08-01 17:07:12 +0000 | [diff] [blame] | 342 | /////////////////////////////////////////////////////////////////////////////// |
| 343 | #include <OpenGL/OpenGL.h> |
bsalomon@google.com | 2e40173 | 2012-08-03 19:25:10 +0000 | [diff] [blame] | 344 | |
bsalomon | 241b56d | 2015-06-18 11:49:42 -0700 | [diff] [blame] | 345 | static CGLContextObj createGLContext(int msaaSampleCount) { |
yangsu@google.com | 12d177d | 2011-08-01 17:07:12 +0000 | [diff] [blame] | 346 | GLint major, minor; |
yangsu@google.com | 12d177d | 2011-08-01 17:07:12 +0000 | [diff] [blame] | 347 | CGLGetVersion(&major, &minor); |
Yuqian Li | 6e3d995 | 2017-09-27 13:25:36 -0400 | [diff] [blame] | 348 | |
bsalomon@google.com | 1195925 | 2012-04-06 20:13:38 +0000 | [diff] [blame] | 349 | static const CGLPixelFormatAttribute attributes[] = { |
| 350 | kCGLPFAStencilSize, (CGLPixelFormatAttribute) 8, |
yangsu@google.com | 12d177d | 2011-08-01 17:07:12 +0000 | [diff] [blame] | 351 | kCGLPFAAccelerated, |
| 352 | kCGLPFADoubleBuffer, |
ethannicholas | 28ef445 | 2016-03-25 09:26:03 -0700 | [diff] [blame] | 353 | kCGLPFAOpenGLProfile, (CGLPixelFormatAttribute) kCGLOGLPVersion_3_2_Core, |
yangsu@google.com | 12d177d | 2011-08-01 17:07:12 +0000 | [diff] [blame] | 354 | (CGLPixelFormatAttribute)0 |
| 355 | }; |
Yuqian Li | 6e3d995 | 2017-09-27 13:25:36 -0400 | [diff] [blame] | 356 | |
yangsu@google.com | 12d177d | 2011-08-01 17:07:12 +0000 | [diff] [blame] | 357 | CGLPixelFormatObj format; |
bsalomon@google.com | 1195925 | 2012-04-06 20:13:38 +0000 | [diff] [blame] | 358 | GLint npix = 0; |
| 359 | if (msaaSampleCount > 0) { |
dvonbeck | 115e925 | 2016-06-15 09:13:50 -0700 | [diff] [blame] | 360 | static const int kAttributeCount = SK_ARRAY_COUNT(attributes); |
bsalomon@google.com | 1195925 | 2012-04-06 20:13:38 +0000 | [diff] [blame] | 361 | CGLPixelFormatAttribute msaaAttributes[kAttributeCount + 5]; |
| 362 | memcpy(msaaAttributes, attributes, sizeof(attributes)); |
| 363 | SkASSERT(0 == msaaAttributes[kAttributeCount - 1]); |
| 364 | msaaAttributes[kAttributeCount - 1] = kCGLPFASampleBuffers; |
| 365 | msaaAttributes[kAttributeCount + 0] = (CGLPixelFormatAttribute)1; |
| 366 | msaaAttributes[kAttributeCount + 1] = kCGLPFAMultisample; |
| 367 | msaaAttributes[kAttributeCount + 2] = kCGLPFASamples; |
| 368 | msaaAttributes[kAttributeCount + 3] = |
| 369 | (CGLPixelFormatAttribute)msaaSampleCount; |
| 370 | msaaAttributes[kAttributeCount + 4] = (CGLPixelFormatAttribute)0; |
| 371 | CGLChoosePixelFormat(msaaAttributes, &format, &npix); |
| 372 | } |
| 373 | if (!npix) { |
| 374 | CGLChoosePixelFormat(attributes, &format, &npix); |
| 375 | } |
yangsu@google.com | ef7bdfa | 2011-08-12 14:27:47 +0000 | [diff] [blame] | 376 | CGLContextObj ctx; |
yangsu@google.com | 12d177d | 2011-08-01 17:07:12 +0000 | [diff] [blame] | 377 | CGLCreateContext(format, NULL, &ctx); |
yangsu@google.com | 12d177d | 2011-08-01 17:07:12 +0000 | [diff] [blame] | 378 | CGLDestroyPixelFormat(format); |
Yuqian Li | 6e3d995 | 2017-09-27 13:25:36 -0400 | [diff] [blame] | 379 | |
yangsu@google.com | 12d177d | 2011-08-01 17:07:12 +0000 | [diff] [blame] | 380 | static const GLint interval = 1; |
| 381 | CGLSetParameter(ctx, kCGLCPSwapInterval, &interval); |
| 382 | CGLSetCurrentContext(ctx); |
| 383 | return ctx; |
| 384 | } |
| 385 | |
| 386 | - (void)viewDidMoveToWindow { |
| 387 | [super viewDidMoveToWindow]; |
Yuqian Li | 6e3d995 | 2017-09-27 13:25:36 -0400 | [diff] [blame] | 388 | |
yangsu@google.com | 12d177d | 2011-08-01 17:07:12 +0000 | [diff] [blame] | 389 | //Attaching view to fGLContext requires that the view to be part of a window, |
Yuqian Li | 6e3d995 | 2017-09-27 13:25:36 -0400 | [diff] [blame] | 390 | //and that the NSWindow instance must have a CoreGraphics counterpart (or |
yangsu@google.com | 12d177d | 2011-08-01 17:07:12 +0000 | [diff] [blame] | 391 | //it must NOT be deferred or should have been on screen at least once) |
| 392 | if ([fGLContext view] != self && nil != self.window) { |
| 393 | [fGLContext setView:self]; |
yangsu@google.com | 12d177d | 2011-08-01 17:07:12 +0000 | [diff] [blame] | 394 | } |
| 395 | } |
bsalomon@google.com | 2e40173 | 2012-08-03 19:25:10 +0000 | [diff] [blame] | 396 | - (bool)attach:(SkOSWindow::SkBackEndTypes)attachType |
bsalomon@google.com | 64cc810 | 2013-03-05 20:06:05 +0000 | [diff] [blame] | 397 | withMSAASampleCount:(int) sampleCount |
| 398 | andGetInfo:(SkOSWindow::AttachmentInfo*) info { |
bsalomon@google.com | 2e40173 | 2012-08-03 19:25:10 +0000 | [diff] [blame] | 399 | if (nil == fGLContext) { |
| 400 | CGLContextObj ctx = createGLContext(sampleCount); |
reed | 789e327 | 2014-10-30 11:29:08 -0700 | [diff] [blame] | 401 | SkASSERT(ctx); |
bsalomon@google.com | 2e40173 | 2012-08-03 19:25:10 +0000 | [diff] [blame] | 402 | fGLContext = [[NSOpenGLContext alloc] initWithCGLContextObj:ctx]; |
| 403 | CGLReleaseContext(ctx); |
| 404 | if (NULL == fGLContext) { |
| 405 | return false; |
caryclark@google.com | 4ee8aea | 2011-11-23 14:54:19 +0000 | [diff] [blame] | 406 | } |
bsalomon@google.com | 2e40173 | 2012-08-03 19:25:10 +0000 | [diff] [blame] | 407 | [fGLContext setView:self]; |
caryclark@google.com | 4ee8aea | 2011-11-23 14:54:19 +0000 | [diff] [blame] | 408 | } |
bsalomon@google.com | 64cc810 | 2013-03-05 20:06:05 +0000 | [diff] [blame] | 409 | |
bsalomon@google.com | 2e40173 | 2012-08-03 19:25:10 +0000 | [diff] [blame] | 410 | [fGLContext makeCurrentContext]; |
bsalomon@google.com | 64cc810 | 2013-03-05 20:06:05 +0000 | [diff] [blame] | 411 | CGLPixelFormatObj format = CGLGetPixelFormat((CGLContextObj)[fGLContext CGLContextObj]); |
| 412 | CGLDescribePixelFormat(format, 0, kCGLPFASamples, &info->fSampleCount); |
| 413 | CGLDescribePixelFormat(format, 0, kCGLPFAStencilSize, &info->fStencilBits); |
bungeman@google.com | b20a1b5 | 2013-06-28 16:44:55 +0000 | [diff] [blame] | 414 | NSSize size = self.bounds.size; |
| 415 | #if RETINA_API_AVAILABLE |
| 416 | size = [self convertSizeToBacking:size]; |
| 417 | #endif |
| 418 | glViewport(0, 0, (int) size.width, (int) size.height); |
bsalomon@google.com | 2e40173 | 2012-08-03 19:25:10 +0000 | [diff] [blame] | 419 | glClearColor(0, 0, 0, 0); |
| 420 | glClearStencil(0); |
| 421 | glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); |
| 422 | return true; |
yangsu@google.com | 12d177d | 2011-08-01 17:07:12 +0000 | [diff] [blame] | 423 | } |
| 424 | |
robertphillips@google.com | d5b05ef | 2012-04-02 20:19:28 +0000 | [diff] [blame] | 425 | - (void)detach { |
bsalomon@google.com | ed16483 | 2012-04-03 18:06:20 +0000 | [diff] [blame] | 426 | [fGLContext release]; |
| 427 | fGLContext = nil; |
yangsu@google.com | 12d177d | 2011-08-01 17:07:12 +0000 | [diff] [blame] | 428 | } |
| 429 | |
robertphillips@google.com | d5b05ef | 2012-04-02 20:19:28 +0000 | [diff] [blame] | 430 | - (void)present { |
bsalomon@google.com | ed16483 | 2012-04-03 18:06:20 +0000 | [diff] [blame] | 431 | if (nil != fGLContext) { |
| 432 | [fGLContext flushBuffer]; |
| 433 | } |
yangsu@google.com | 12d177d | 2011-08-01 17:07:12 +0000 | [diff] [blame] | 434 | } |
bsalomon | 241b56d | 2015-06-18 11:49:42 -0700 | [diff] [blame] | 435 | |
| 436 | - (void)setVSync:(bool)enable { |
| 437 | if (fGLContext) { |
| 438 | GLint interval = enable ? 1 : 0; |
| 439 | CGLContextObj ctx = (CGLContextObj)[fGLContext CGLContextObj]; |
| 440 | CGLSetParameter(ctx, kCGLCPSwapInterval, &interval); |
| 441 | } |
| 442 | } |
robertphillips@google.com | d5b05ef | 2012-04-02 20:19:28 +0000 | [diff] [blame] | 443 | @end |