gfx/skia/trunk/src/views/mac/SkNSView.mm

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial