3 * Copyright 2011 Google Inc.
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
11 #include "SkCGUtils.h"
13 SK_COMPILE_ASSERT(SK_SUPPORT_GPU, not_implemented_for_non_gpu_build);
15 //#define FORCE_REDRAW
16 // Can be dropped when we no longer support 10.6.
17 #define RETINA_API_AVAILABLE (defined(MAC_OS_X_VERSION_10_7) && \
18 MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_7)
19 @implementation SkNSView
20 @synthesize fWind, fTitle, fOptionsDelegate, fGLContext;
22 - (id)initWithCoder:(NSCoder*)coder {
23 if ((self = [super initWithCoder:coder])) {
24 self = [self initWithDefaults];
30 - (id)initWithFrame:(NSRect)frameRect {
31 if ((self = [super initWithFrame:frameRect])) {
32 self = [self initWithDefaults];
38 - (id)initWithDefaults {
39 #if RETINA_API_AVAILABLE
40 [self setWantsBestResolutionOpenGLSurface:YES];
42 fRedrawRequestPending = false;
48 [[NSNotificationCenter defaultCenter] addObserver:self
49 selector:@selector(backingPropertiesChanged:)
50 name:@"NSWindowDidChangeBackingPropertiesNotification"
51 object:[self window]];
53 fWind->setVisibleP(true);
54 NSSize size = self.frame.size;
55 #if RETINA_API_AVAILABLE
56 size = [self convertSizeToBacking:self.frame.size];
58 fWind->resize((int) size.width, (int) size.height,
59 SkBitmap::kARGB_8888_Config);
67 - (BOOL)acceptsFirstResponder {
71 - (float)scaleFactor {
72 NSWindow *window = [self window];
73 #if RETINA_API_AVAILABLE
75 return [window backingScaleFactor];
77 return [[NSScreen mainScreen] backingScaleFactor];
80 return [window userSpaceScaleFactor];
82 return [[NSScreen mainScreen] userSpaceScaleFactor];
86 - (void)backingPropertiesChanged:(NSNotification *)notification {
87 CGFloat oldBackingScaleFactor = (CGFloat)[
88 [notification.userInfo objectForKey:@"NSBackingPropertyOldScaleFactorKey"] doubleValue
90 CGFloat newBackingScaleFactor = [self scaleFactor];
91 if (oldBackingScaleFactor == newBackingScaleFactor) {
95 // TODO: need a better way to force a refresh (that works).
96 // [fGLContext update] does not appear to update if the point size has not changed,
97 // even if the backing size has changed.
98 [self setFrameSize:NSMakeSize(self.frame.size.width + 1, self.frame.size.height + 1)];
101 - (void)resizeSkView:(NSSize)newSize {
102 #if RETINA_API_AVAILABLE
103 newSize = [self convertSizeToBacking:newSize];
106 (fWind->width() != newSize.width ||
107 fWind->height() != newSize.height))
109 fWind->resize((int) newSize.width, (int) newSize.height);
110 if (NULL != fGLContext) {
111 glClear(GL_STENCIL_BUFFER_BIT);
117 - (void) setFrameSize:(NSSize)newSize {
118 [super setFrameSize:newSize];
119 [self resizeSkView:newSize];
124 self.fGLContext = nil;
129 ////////////////////////////////////////////////////////////////////////////////
132 fRedrawRequestPending = false;
134 SkAutoTUnref<SkCanvas> canvas(fWind->createCanvas());
142 - (void)setSkTitle:(const char *)title {
143 self.fTitle = [NSString stringWithUTF8String:title];
144 [[self window] setTitle:self.fTitle];
147 - (BOOL)onHandleEvent:(const SkEvent&)evt {
151 #include "SkOSMenu.h"
152 - (void)onAddMenu:(const SkOSMenu*)menu {
153 [self.fOptionsDelegate view:self didAddMenu:menu];
156 - (void)onUpdateMenu:(const SkOSMenu*)menu {
157 [self.fOptionsDelegate view:self didUpdateMenu:menu];
160 - (void)postInvalWithRect:(const SkIRect*)r {
161 if (!fRedrawRequestPending) {
162 fRedrawRequestPending = true;
163 [self setNeedsDisplay:YES];
164 [self performSelector:@selector(drawSkia) withObject:nil afterDelay:0];
167 ///////////////////////////////////////////////////////////////////////////////
171 SK_MacReturnKey = 36,
172 SK_MacDeleteKey = 51,
175 SK_MacRightKey = 124,
190 static SkKey raw2key(UInt32 raw)
192 static const struct {
196 { SK_MacUpKey, kUp_SkKey },
197 { SK_MacDownKey, kDown_SkKey },
198 { SK_MacLeftKey, kLeft_SkKey },
199 { SK_MacRightKey, kRight_SkKey },
200 { SK_MacReturnKey, kOK_SkKey },
201 { SK_MacDeleteKey, kBack_SkKey },
202 { SK_MacEndKey, kEnd_SkKey },
203 { SK_Mac0Key, k0_SkKey },
204 { SK_Mac1Key, k1_SkKey },
205 { SK_Mac2Key, k2_SkKey },
206 { SK_Mac3Key, k3_SkKey },
207 { SK_Mac4Key, k4_SkKey },
208 { SK_Mac5Key, k5_SkKey },
209 { SK_Mac6Key, k6_SkKey },
210 { SK_Mac7Key, k7_SkKey },
211 { SK_Mac8Key, k8_SkKey },
212 { SK_Mac9Key, k9_SkKey }
215 for (unsigned i = 0; i < SK_ARRAY_COUNT(gKeys); i++)
216 if (gKeys[i].fRaw == raw)
217 return gKeys[i].fKey;
221 - (void)keyDown:(NSEvent *)event {
225 SkKey key = raw2key([event keyCode]);
226 if (kNONE_SkKey != key)
227 fWind->handleKey(key);
229 unichar c = [[event characters] characterAtIndex:0];
230 fWind->handleChar((SkUnichar)c);
234 - (void)keyUp:(NSEvent *)event {
238 SkKey key = raw2key([event keyCode]);
239 if (kNONE_SkKey != key)
240 fWind->handleKeyUp(key);
242 // unichar c = [[event characters] characterAtIndex:0];
245 static const struct {
246 unsigned fNSModifierMask;
247 unsigned fSkModifierMask;
248 } gModifierMasks[] = {
249 { NSAlphaShiftKeyMask, kShift_SkModifierKey },
250 { NSShiftKeyMask, kShift_SkModifierKey },
251 { NSControlKeyMask, kControl_SkModifierKey },
252 { NSAlternateKeyMask, kOption_SkModifierKey },
253 { NSCommandKeyMask, kCommand_SkModifierKey },
256 static unsigned convertNSModifiersToSk(NSUInteger nsModi) {
258 for (size_t i = 0; i < SK_ARRAY_COUNT(gModifierMasks); ++i) {
259 if (nsModi & gModifierMasks[i].fNSModifierMask) {
260 skModi |= gModifierMasks[i].fSkModifierMask;
266 - (void)mouseDown:(NSEvent *)event {
267 NSPoint p = [event locationInWindow];
268 unsigned modi = convertNSModifiersToSk([event modifierFlags]);
270 if ([self mouse:p inRect:[self bounds]] && NULL != fWind) {
271 NSPoint loc = [self convertPoint:p fromView:nil];
272 #if RETINA_API_AVAILABLE
273 loc = [self convertPointToBacking:loc]; //y-up
276 fWind->handleClick((int) loc.x, (int) loc.y,
277 SkView::Click::kDown_State, self, modi);
281 - (void)mouseDragged:(NSEvent *)event {
282 NSPoint p = [event locationInWindow];
283 unsigned modi = convertNSModifiersToSk([event modifierFlags]);
285 if ([self mouse:p inRect:[self bounds]] && NULL != fWind) {
286 NSPoint loc = [self convertPoint:p fromView:nil];
287 #if RETINA_API_AVAILABLE
288 loc = [self convertPointToBacking:loc]; //y-up
291 fWind->handleClick((int) loc.x, (int) loc.y,
292 SkView::Click::kMoved_State, self, modi);
296 - (void)mouseMoved:(NSEvent *)event {
297 NSPoint p = [event locationInWindow];
298 unsigned modi = convertNSModifiersToSk([event modifierFlags]);
300 if ([self mouse:p inRect:[self bounds]] && NULL != fWind) {
301 NSPoint loc = [self convertPoint:p fromView:nil];
302 #if RETINA_API_AVAILABLE
303 loc = [self convertPointToBacking:loc]; //y-up
306 fWind->handleClick((int) loc.x, (int) loc.y,
307 SkView::Click::kMoved_State, self, modi);
311 - (void)mouseUp:(NSEvent *)event {
312 NSPoint p = [event locationInWindow];
313 unsigned modi = convertNSModifiersToSk([event modifierFlags]);
315 if ([self mouse:p inRect:[self bounds]] && NULL != fWind) {
316 NSPoint loc = [self convertPoint:p fromView:nil];
317 #if RETINA_API_AVAILABLE
318 loc = [self convertPointToBacking:loc]; //y-up
321 fWind->handleClick((int) loc.x, (int) loc.y,
322 SkView::Click::kUp_State, self, modi);
326 ///////////////////////////////////////////////////////////////////////////////
327 #include <OpenGL/OpenGL.h>
330 CGLContextObj createGLContext(int msaaSampleCount) {
332 CGLGetVersion(&major, &minor);
334 static const CGLPixelFormatAttribute attributes[] = {
335 kCGLPFAStencilSize, (CGLPixelFormatAttribute) 8,
338 (CGLPixelFormatAttribute)0
341 CGLPixelFormatObj format;
343 if (msaaSampleCount > 0) {
344 static int kAttributeCount = SK_ARRAY_COUNT(attributes);
345 CGLPixelFormatAttribute msaaAttributes[kAttributeCount + 5];
346 memcpy(msaaAttributes, attributes, sizeof(attributes));
347 SkASSERT(0 == msaaAttributes[kAttributeCount - 1]);
348 msaaAttributes[kAttributeCount - 1] = kCGLPFASampleBuffers;
349 msaaAttributes[kAttributeCount + 0] = (CGLPixelFormatAttribute)1;
350 msaaAttributes[kAttributeCount + 1] = kCGLPFAMultisample;
351 msaaAttributes[kAttributeCount + 2] = kCGLPFASamples;
352 msaaAttributes[kAttributeCount + 3] =
353 (CGLPixelFormatAttribute)msaaSampleCount;
354 msaaAttributes[kAttributeCount + 4] = (CGLPixelFormatAttribute)0;
355 CGLChoosePixelFormat(msaaAttributes, &format, &npix);
358 CGLChoosePixelFormat(attributes, &format, &npix);
361 CGLCreateContext(format, NULL, &ctx);
362 CGLDestroyPixelFormat(format);
364 static const GLint interval = 1;
365 CGLSetParameter(ctx, kCGLCPSwapInterval, &interval);
366 CGLSetCurrentContext(ctx);
371 - (void)viewDidMoveToWindow {
372 [super viewDidMoveToWindow];
374 //Attaching view to fGLContext requires that the view to be part of a window,
375 //and that the NSWindow instance must have a CoreGraphics counterpart (or
376 //it must NOT be deferred or should have been on screen at least once)
377 if ([fGLContext view] != self && nil != self.window) {
378 [fGLContext setView:self];
381 - (bool)attach:(SkOSWindow::SkBackEndTypes)attachType
382 withMSAASampleCount:(int) sampleCount
383 andGetInfo:(SkOSWindow::AttachmentInfo*) info {
384 if (nil == fGLContext) {
385 CGLContextObj ctx = createGLContext(sampleCount);
386 fGLContext = [[NSOpenGLContext alloc] initWithCGLContextObj:ctx];
387 CGLReleaseContext(ctx);
388 if (NULL == fGLContext) {
391 [fGLContext setView:self];
394 [fGLContext makeCurrentContext];
395 CGLPixelFormatObj format = CGLGetPixelFormat((CGLContextObj)[fGLContext CGLContextObj]);
396 CGLDescribePixelFormat(format, 0, kCGLPFASamples, &info->fSampleCount);
397 CGLDescribePixelFormat(format, 0, kCGLPFAStencilSize, &info->fStencilBits);
398 NSSize size = self.bounds.size;
399 #if RETINA_API_AVAILABLE
400 size = [self convertSizeToBacking:size];
402 glViewport(0, 0, (int) size.width, (int) size.height);
403 glClearColor(0, 0, 0, 0);
405 glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
410 [fGLContext release];
415 if (nil != fGLContext) {
416 [fGLContext flushBuffer];