bsalomon@google.com | f75b84e | 2011-09-29 14:58:28 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2011 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
| 8 | |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame^] | 9 | #import "SkNSView.h" |
yangsu@google.com | 12d177d | 2011-08-01 17:07:12 +0000 | [diff] [blame] | 10 | #include "SkCanvas.h" |
yangsu@google.com | 12d177d | 2011-08-01 17:07:12 +0000 | [diff] [blame] | 11 | #include "SkCGUtils.h" |
| 12 | #include "SkEvent.h" |
yangsu@google.com | 12d177d | 2011-08-01 17:07:12 +0000 | [diff] [blame] | 13 | |
| 14 | //#define FORCE_REDRAW |
| 15 | @implementation SkNSView |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame^] | 16 | @synthesize fWind, fTitle, fOptionsDelegate; |
| 17 | |
| 18 | #if SK_SUPPORT_GPU |
| 19 | @synthesize fGLContext; |
| 20 | #endif |
yangsu@google.com | 12d177d | 2011-08-01 17:07:12 +0000 | [diff] [blame] | 21 | |
| 22 | - (id)initWithCoder:(NSCoder*)coder { |
| 23 | if ((self = [super initWithCoder:coder])) { |
yangsu@google.com | a854041 | 2011-08-30 14:40:49 +0000 | [diff] [blame] | 24 | self = [self initWithDefaults]; |
| 25 | [self setUpWindow]; |
yangsu@google.com | 12d177d | 2011-08-01 17:07:12 +0000 | [diff] [blame] | 26 | } |
| 27 | return self; |
| 28 | } |
| 29 | |
| 30 | - (id)initWithFrame:(NSRect)frameRect { |
caryclark@google.com | 679ab31 | 2012-06-06 12:04:00 +0000 | [diff] [blame] | 31 | if ((self = [super initWithFrame:frameRect])) { |
yangsu@google.com | a854041 | 2011-08-30 14:40:49 +0000 | [diff] [blame] | 32 | self = [self initWithDefaults]; |
| 33 | [self setUpWindow]; |
yangsu@google.com | 12d177d | 2011-08-01 17:07:12 +0000 | [diff] [blame] | 34 | } |
| 35 | return self; |
| 36 | } |
| 37 | |
yangsu@google.com | a854041 | 2011-08-30 14:40:49 +0000 | [diff] [blame] | 38 | - (id)initWithDefaults { |
yangsu@google.com | 12d177d | 2011-08-01 17:07:12 +0000 | [diff] [blame] | 39 | fRedrawRequestPending = false; |
yangsu@google.com | a854041 | 2011-08-30 14:40:49 +0000 | [diff] [blame] | 40 | fWind = NULL; |
yangsu@google.com | 12d177d | 2011-08-01 17:07:12 +0000 | [diff] [blame] | 41 | return self; |
| 42 | } |
| 43 | |
yangsu@google.com | a854041 | 2011-08-30 14:40:49 +0000 | [diff] [blame] | 44 | - (void)setUpWindow { |
| 45 | if (NULL != fWind) { |
| 46 | fWind->setVisibleP(true); |
caryclark@google.com | 679ab31 | 2012-06-06 12:04:00 +0000 | [diff] [blame] | 47 | fWind->resize((int) self.frame.size.width, (int) self.frame.size.height, |
yangsu@google.com | a854041 | 2011-08-30 14:40:49 +0000 | [diff] [blame] | 48 | SkBitmap::kARGB_8888_Config); |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame^] | 49 | [self attach:SkOSWindow::kNone_BackEndType withMSAASampleCount:0]; |
yangsu@google.com | a854041 | 2011-08-30 14:40:49 +0000 | [diff] [blame] | 50 | } |
| 51 | } |
| 52 | |
yangsu@google.com | 12d177d | 2011-08-01 17:07:12 +0000 | [diff] [blame] | 53 | -(BOOL) isFlipped { |
| 54 | return YES; |
| 55 | } |
| 56 | |
| 57 | - (BOOL)acceptsFirstResponder { |
| 58 | return YES; |
| 59 | } |
| 60 | |
suyang1 | bb3f4a8 | 2011-10-10 16:38:58 +0000 | [diff] [blame] | 61 | - (void)resizeSkView:(NSSize)newSize { |
| 62 | if (NULL != fWind && (fWind->width() != newSize.width || fWind->height() != newSize.height)) { |
caryclark@google.com | 679ab31 | 2012-06-06 12:04:00 +0000 | [diff] [blame] | 63 | fWind->resize((int) newSize.width, (int) newSize.height); |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame^] | 64 | #if SK_SUPPORT_GPU |
suyang1 | bb3f4a8 | 2011-10-10 16:38:58 +0000 | [diff] [blame] | 65 | glClear(GL_STENCIL_BUFFER_BIT); |
| 66 | [fGLContext update]; |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame^] | 67 | #endif |
yangsu@google.com | 12d177d | 2011-08-01 17:07:12 +0000 | [diff] [blame] | 68 | } |
yangsu@google.com | 12d177d | 2011-08-01 17:07:12 +0000 | [diff] [blame] | 69 | } |
| 70 | |
suyang1 | bb3f4a8 | 2011-10-10 16:38:58 +0000 | [diff] [blame] | 71 | - (void) setFrameSize:(NSSize)newSize { |
| 72 | [super setFrameSize:newSize]; |
| 73 | [self resizeSkView:newSize]; |
| 74 | } |
| 75 | |
yangsu@google.com | 12d177d | 2011-08-01 17:07:12 +0000 | [diff] [blame] | 76 | - (void)dealloc { |
| 77 | delete fWind; |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame^] | 78 | #if SK_SUPPORT_GPU |
yangsu@google.com | 12d177d | 2011-08-01 17:07:12 +0000 | [diff] [blame] | 79 | self.fGLContext = nil; |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame^] | 80 | #endif |
yangsu@google.com | 12d177d | 2011-08-01 17:07:12 +0000 | [diff] [blame] | 81 | self.fTitle = nil; |
yangsu@google.com | 12d177d | 2011-08-01 17:07:12 +0000 | [diff] [blame] | 82 | [super dealloc]; |
| 83 | } |
| 84 | |
suyang1 | bb3f4a8 | 2011-10-10 16:38:58 +0000 | [diff] [blame] | 85 | //////////////////////////////////////////////////////////////////////////////// |
yangsu@google.com | 12d177d | 2011-08-01 17:07:12 +0000 | [diff] [blame] | 86 | |
yangsu@google.com | f3493f0 | 2011-08-08 15:12:05 +0000 | [diff] [blame] | 87 | - (void)drawSkia { |
yangsu@google.com | 12d177d | 2011-08-01 17:07:12 +0000 | [diff] [blame] | 88 | fRedrawRequestPending = false; |
yangsu@google.com | a854041 | 2011-08-30 14:40:49 +0000 | [diff] [blame] | 89 | if (NULL != fWind) { |
| 90 | SkCanvas canvas(fWind->getBitmap()); |
| 91 | fWind->draw(&canvas); |
yangsu@google.com | 12d177d | 2011-08-01 17:07:12 +0000 | [diff] [blame] | 92 | #ifdef FORCE_REDRAW |
yangsu@google.com | a854041 | 2011-08-30 14:40:49 +0000 | [diff] [blame] | 93 | fWind->inval(NULL); |
yangsu@google.com | 12d177d | 2011-08-01 17:07:12 +0000 | [diff] [blame] | 94 | #endif |
yangsu@google.com | a854041 | 2011-08-30 14:40:49 +0000 | [diff] [blame] | 95 | } |
yangsu@google.com | 12d177d | 2011-08-01 17:07:12 +0000 | [diff] [blame] | 96 | } |
| 97 | |
yangsu@google.com | f3493f0 | 2011-08-08 15:12:05 +0000 | [diff] [blame] | 98 | - (void)setSkTitle:(const char *)title { |
| 99 | self.fTitle = [NSString stringWithUTF8String:title]; |
| 100 | [[self window] setTitle:self.fTitle]; |
| 101 | } |
| 102 | |
| 103 | - (BOOL)onHandleEvent:(const SkEvent&)evt { |
| 104 | return false; |
| 105 | } |
| 106 | |
| 107 | #include "SkOSMenu.h" |
| 108 | - (void)onAddMenu:(const SkOSMenu*)menu { |
| 109 | [self.fOptionsDelegate view:self didAddMenu:menu]; |
| 110 | } |
| 111 | |
| 112 | - (void)onUpdateMenu:(const SkOSMenu*)menu { |
| 113 | [self.fOptionsDelegate view:self didUpdateMenu:menu]; |
| 114 | } |
| 115 | |
| 116 | - (void)postInvalWithRect:(const SkIRect*)r { |
| 117 | if (!fRedrawRequestPending) { |
| 118 | fRedrawRequestPending = true; |
yangsu@google.com | a854041 | 2011-08-30 14:40:49 +0000 | [diff] [blame] | 119 | [self setNeedsDisplay:YES]; |
yangsu@google.com | f3493f0 | 2011-08-08 15:12:05 +0000 | [diff] [blame] | 120 | [self performSelector:@selector(drawSkia) withObject:nil afterDelay:0]; |
| 121 | } |
yangsu@google.com | 12d177d | 2011-08-01 17:07:12 +0000 | [diff] [blame] | 122 | } |
| 123 | /////////////////////////////////////////////////////////////////////////////// |
| 124 | |
| 125 | #include "SkKey.h" |
| 126 | enum { |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame^] | 127 | SK_MacReturnKey = 36, |
| 128 | SK_MacDeleteKey = 51, |
| 129 | SK_MacEndKey = 119, |
| 130 | SK_MacLeftKey = 123, |
| 131 | SK_MacRightKey = 124, |
| 132 | SK_MacDownKey = 125, |
| 133 | SK_MacUpKey = 126, |
yangsu@google.com | 12d177d | 2011-08-01 17:07:12 +0000 | [diff] [blame] | 134 | SK_Mac0Key = 0x52, |
| 135 | SK_Mac1Key = 0x53, |
| 136 | SK_Mac2Key = 0x54, |
| 137 | SK_Mac3Key = 0x55, |
| 138 | SK_Mac4Key = 0x56, |
| 139 | SK_Mac5Key = 0x57, |
| 140 | SK_Mac6Key = 0x58, |
| 141 | SK_Mac7Key = 0x59, |
| 142 | SK_Mac8Key = 0x5b, |
| 143 | SK_Mac9Key = 0x5c |
| 144 | }; |
| 145 | |
| 146 | static SkKey raw2key(UInt32 raw) |
| 147 | { |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame^] | 148 | static const struct { |
| 149 | UInt32 fRaw; |
| 150 | SkKey fKey; |
| 151 | } gKeys[] = { |
| 152 | { SK_MacUpKey, kUp_SkKey }, |
| 153 | { SK_MacDownKey, kDown_SkKey }, |
| 154 | { SK_MacLeftKey, kLeft_SkKey }, |
| 155 | { SK_MacRightKey, kRight_SkKey }, |
| 156 | { SK_MacReturnKey, kOK_SkKey }, |
| 157 | { SK_MacDeleteKey, kBack_SkKey }, |
| 158 | { SK_MacEndKey, kEnd_SkKey }, |
yangsu@google.com | 12d177d | 2011-08-01 17:07:12 +0000 | [diff] [blame] | 159 | { SK_Mac0Key, k0_SkKey }, |
| 160 | { SK_Mac1Key, k1_SkKey }, |
| 161 | { SK_Mac2Key, k2_SkKey }, |
| 162 | { SK_Mac3Key, k3_SkKey }, |
yangsu@google.com | f3493f0 | 2011-08-08 15:12:05 +0000 | [diff] [blame] | 163 | { SK_Mac4Key, k4_SkKey }, |
yangsu@google.com | 12d177d | 2011-08-01 17:07:12 +0000 | [diff] [blame] | 164 | { SK_Mac5Key, k5_SkKey }, |
| 165 | { SK_Mac6Key, k6_SkKey }, |
| 166 | { SK_Mac7Key, k7_SkKey }, |
| 167 | { SK_Mac8Key, k8_SkKey }, |
| 168 | { SK_Mac9Key, k9_SkKey } |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame^] | 169 | }; |
yangsu@google.com | 12d177d | 2011-08-01 17:07:12 +0000 | [diff] [blame] | 170 | |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame^] | 171 | for (unsigned i = 0; i < SK_ARRAY_COUNT(gKeys); i++) |
| 172 | if (gKeys[i].fRaw == raw) |
| 173 | return gKeys[i].fKey; |
| 174 | return kNONE_SkKey; |
yangsu@google.com | 12d177d | 2011-08-01 17:07:12 +0000 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | - (void)keyDown:(NSEvent *)event { |
yangsu@google.com | 849ad5a | 2011-08-30 15:27:57 +0000 | [diff] [blame] | 178 | if (NULL == fWind) |
yangsu@google.com | a854041 | 2011-08-30 14:40:49 +0000 | [diff] [blame] | 179 | return; |
| 180 | |
yangsu@google.com | 12d177d | 2011-08-01 17:07:12 +0000 | [diff] [blame] | 181 | SkKey key = raw2key([event keyCode]); |
| 182 | if (kNONE_SkKey != key) |
| 183 | fWind->handleKey(key); |
| 184 | else{ |
| 185 | unichar c = [[event characters] characterAtIndex:0]; |
| 186 | fWind->handleChar((SkUnichar)c); |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | - (void)keyUp:(NSEvent *)event { |
yangsu@google.com | 849ad5a | 2011-08-30 15:27:57 +0000 | [diff] [blame] | 191 | if (NULL == fWind) |
yangsu@google.com | a854041 | 2011-08-30 14:40:49 +0000 | [diff] [blame] | 192 | return; |
| 193 | |
yangsu@google.com | 12d177d | 2011-08-01 17:07:12 +0000 | [diff] [blame] | 194 | SkKey key = raw2key([event keyCode]); |
| 195 | if (kNONE_SkKey != key) |
| 196 | fWind->handleKeyUp(key); |
caryclark@google.com | 679ab31 | 2012-06-06 12:04:00 +0000 | [diff] [blame] | 197 | // else |
| 198 | // unichar c = [[event characters] characterAtIndex:0]; |
yangsu@google.com | 12d177d | 2011-08-01 17:07:12 +0000 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | - (void)mouseDown:(NSEvent *)event { |
| 202 | NSPoint p = [event locationInWindow]; |
yangsu@google.com | a854041 | 2011-08-30 14:40:49 +0000 | [diff] [blame] | 203 | if ([self mouse:p inRect:[self bounds]] && NULL != fWind) { |
yangsu@google.com | 12d177d | 2011-08-01 17:07:12 +0000 | [diff] [blame] | 204 | NSPoint loc = [self convertPoint:p fromView:nil]; |
caryclark@google.com | 679ab31 | 2012-06-06 12:04:00 +0000 | [diff] [blame] | 205 | fWind->handleClick((int) loc.x, (int) loc.y, SkView::Click::kDown_State, self); |
yangsu@google.com | 12d177d | 2011-08-01 17:07:12 +0000 | [diff] [blame] | 206 | } |
| 207 | } |
| 208 | |
| 209 | - (void)mouseDragged:(NSEvent *)event { |
| 210 | NSPoint p = [event locationInWindow]; |
yangsu@google.com | a854041 | 2011-08-30 14:40:49 +0000 | [diff] [blame] | 211 | if ([self mouse:p inRect:[self bounds]] && NULL != fWind) { |
yangsu@google.com | 12d177d | 2011-08-01 17:07:12 +0000 | [diff] [blame] | 212 | NSPoint loc = [self convertPoint:p fromView:nil]; |
caryclark@google.com | 679ab31 | 2012-06-06 12:04:00 +0000 | [diff] [blame] | 213 | fWind->handleClick((int) loc.x, (int) loc.y, SkView::Click::kMoved_State, self); |
yangsu@google.com | 12d177d | 2011-08-01 17:07:12 +0000 | [diff] [blame] | 214 | } |
| 215 | } |
| 216 | |
suyang1 | bb3f4a8 | 2011-10-10 16:38:58 +0000 | [diff] [blame] | 217 | - (void)mouseMoved:(NSEvent *)event { |
| 218 | NSPoint p = [event locationInWindow]; |
| 219 | if ([self mouse:p inRect:[self bounds]] && NULL != fWind) { |
| 220 | NSPoint loc = [self convertPoint:p fromView:nil]; |
caryclark@google.com | 679ab31 | 2012-06-06 12:04:00 +0000 | [diff] [blame] | 221 | fWind->handleClick((int) loc.x, (int) loc.y, SkView::Click::kMoved_State, self); |
suyang1 | bb3f4a8 | 2011-10-10 16:38:58 +0000 | [diff] [blame] | 222 | } |
| 223 | } |
| 224 | |
yangsu@google.com | 12d177d | 2011-08-01 17:07:12 +0000 | [diff] [blame] | 225 | - (void)mouseUp:(NSEvent *)event { |
| 226 | NSPoint p = [event locationInWindow]; |
yangsu@google.com | a854041 | 2011-08-30 14:40:49 +0000 | [diff] [blame] | 227 | if ([self mouse:p inRect:[self bounds]] && NULL != fWind) { |
yangsu@google.com | 12d177d | 2011-08-01 17:07:12 +0000 | [diff] [blame] | 228 | NSPoint loc = [self convertPoint:p fromView:nil]; |
caryclark@google.com | 679ab31 | 2012-06-06 12:04:00 +0000 | [diff] [blame] | 229 | fWind->handleClick((int) loc.x, (int) loc.y, SkView::Click::kUp_State, self); |
yangsu@google.com | 12d177d | 2011-08-01 17:07:12 +0000 | [diff] [blame] | 230 | } |
| 231 | } |
| 232 | |
yangsu@google.com | 12d177d | 2011-08-01 17:07:12 +0000 | [diff] [blame] | 233 | /////////////////////////////////////////////////////////////////////////////// |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame^] | 234 | #if SK_SUPPORT_GPU |
yangsu@google.com | 12d177d | 2011-08-01 17:07:12 +0000 | [diff] [blame] | 235 | #include <OpenGL/OpenGL.h> |
bsalomon@google.com | ed16483 | 2012-04-03 18:06:20 +0000 | [diff] [blame] | 236 | namespace { |
bsalomon@google.com | 1195925 | 2012-04-06 20:13:38 +0000 | [diff] [blame] | 237 | CGLContextObj createGLContext(int msaaSampleCount) { |
yangsu@google.com | 12d177d | 2011-08-01 17:07:12 +0000 | [diff] [blame] | 238 | GLint major, minor; |
yangsu@google.com | 12d177d | 2011-08-01 17:07:12 +0000 | [diff] [blame] | 239 | CGLGetVersion(&major, &minor); |
yangsu@google.com | 12d177d | 2011-08-01 17:07:12 +0000 | [diff] [blame] | 240 | |
bsalomon@google.com | 1195925 | 2012-04-06 20:13:38 +0000 | [diff] [blame] | 241 | static const CGLPixelFormatAttribute attributes[] = { |
| 242 | kCGLPFAStencilSize, (CGLPixelFormatAttribute) 8, |
yangsu@google.com | 12d177d | 2011-08-01 17:07:12 +0000 | [diff] [blame] | 243 | kCGLPFAAccelerated, |
| 244 | kCGLPFADoubleBuffer, |
| 245 | (CGLPixelFormatAttribute)0 |
| 246 | }; |
| 247 | |
| 248 | CGLPixelFormatObj format; |
bsalomon@google.com | 1195925 | 2012-04-06 20:13:38 +0000 | [diff] [blame] | 249 | GLint npix = 0; |
| 250 | if (msaaSampleCount > 0) { |
| 251 | static int kAttributeCount = SK_ARRAY_COUNT(attributes); |
| 252 | CGLPixelFormatAttribute msaaAttributes[kAttributeCount + 5]; |
| 253 | memcpy(msaaAttributes, attributes, sizeof(attributes)); |
| 254 | SkASSERT(0 == msaaAttributes[kAttributeCount - 1]); |
| 255 | msaaAttributes[kAttributeCount - 1] = kCGLPFASampleBuffers; |
| 256 | msaaAttributes[kAttributeCount + 0] = (CGLPixelFormatAttribute)1; |
| 257 | msaaAttributes[kAttributeCount + 1] = kCGLPFAMultisample; |
| 258 | msaaAttributes[kAttributeCount + 2] = kCGLPFASamples; |
| 259 | msaaAttributes[kAttributeCount + 3] = |
| 260 | (CGLPixelFormatAttribute)msaaSampleCount; |
| 261 | msaaAttributes[kAttributeCount + 4] = (CGLPixelFormatAttribute)0; |
| 262 | CGLChoosePixelFormat(msaaAttributes, &format, &npix); |
| 263 | } |
| 264 | if (!npix) { |
| 265 | CGLChoosePixelFormat(attributes, &format, &npix); |
| 266 | } |
yangsu@google.com | 12d177d | 2011-08-01 17:07:12 +0000 | [diff] [blame] | 267 | |
yangsu@google.com | ef7bdfa | 2011-08-12 14:27:47 +0000 | [diff] [blame] | 268 | CGLContextObj ctx; |
yangsu@google.com | 12d177d | 2011-08-01 17:07:12 +0000 | [diff] [blame] | 269 | CGLCreateContext(format, NULL, &ctx); |
yangsu@google.com | 12d177d | 2011-08-01 17:07:12 +0000 | [diff] [blame] | 270 | CGLDestroyPixelFormat(format); |
| 271 | |
| 272 | static const GLint interval = 1; |
| 273 | CGLSetParameter(ctx, kCGLCPSwapInterval, &interval); |
| 274 | CGLSetCurrentContext(ctx); |
| 275 | return ctx; |
| 276 | } |
bsalomon@google.com | ed16483 | 2012-04-03 18:06:20 +0000 | [diff] [blame] | 277 | } |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame^] | 278 | #endif |
yangsu@google.com | 12d177d | 2011-08-01 17:07:12 +0000 | [diff] [blame] | 279 | |
| 280 | - (void)viewDidMoveToWindow { |
| 281 | [super viewDidMoveToWindow]; |
| 282 | |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame^] | 283 | #if SK_SUPPORT_GPU |
yangsu@google.com | 12d177d | 2011-08-01 17:07:12 +0000 | [diff] [blame] | 284 | //Attaching view to fGLContext requires that the view to be part of a window, |
| 285 | //and that the NSWindow instance must have a CoreGraphics counterpart (or |
| 286 | //it must NOT be deferred or should have been on screen at least once) |
| 287 | if ([fGLContext view] != self && nil != self.window) { |
| 288 | [fGLContext setView:self]; |
yangsu@google.com | 12d177d | 2011-08-01 17:07:12 +0000 | [diff] [blame] | 289 | } |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame^] | 290 | #endif |
yangsu@google.com | 12d177d | 2011-08-01 17:07:12 +0000 | [diff] [blame] | 291 | } |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame^] | 292 | - (bool)attach:(SkOSWindow::SkBackEndTypes)attachType withMSAASampleCount:(int) sampleCount { |
| 293 | #if SK_SUPPORT_GPU |
| 294 | if (SkOSWindow::kNativeGL_BackEndType == attachType) { |
| 295 | [self setWantsLayer:NO]; |
| 296 | self.layer = nil; |
| 297 | if (nil == fGLContext) { |
| 298 | CGLContextObj ctx = createGLContext(sampleCount); |
| 299 | fGLContext = [[NSOpenGLContext alloc] initWithCGLContextObj:ctx]; |
| 300 | CGLReleaseContext(ctx); |
| 301 | if (NULL == fGLContext) { |
| 302 | return false; |
| 303 | } |
| 304 | [fGLContext setView:self]; |
caryclark@google.com | 4ee8aea | 2011-11-23 14:54:19 +0000 | [diff] [blame] | 305 | } |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame^] | 306 | |
| 307 | [fGLContext makeCurrentContext]; |
| 308 | |
| 309 | glViewport(0, 0, (int) self.bounds.size.width, (int) self.bounds.size.width); |
| 310 | glClearColor(0, 0, 0, 0); |
| 311 | glClearStencil(0); |
| 312 | glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); |
| 313 | return true; |
caryclark@google.com | 4ee8aea | 2011-11-23 14:54:19 +0000 | [diff] [blame] | 314 | } |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame^] | 315 | #endif |
| 316 | if (SkOSWindow::kNone_BackEndType == attachType) { |
| 317 | [self detach]; |
| 318 | [self setLayer:[CALayer layer]]; |
| 319 | [self setWantsLayer:YES]; |
| 320 | return true; |
| 321 | } |
| 322 | return false; |
yangsu@google.com | 12d177d | 2011-08-01 17:07:12 +0000 | [diff] [blame] | 323 | } |
| 324 | |
robertphillips@google.com | d5b05ef | 2012-04-02 20:19:28 +0000 | [diff] [blame] | 325 | - (void)detach { |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame^] | 326 | #if SK_SUPPORT_GPU |
bsalomon@google.com | ed16483 | 2012-04-03 18:06:20 +0000 | [diff] [blame] | 327 | [fGLContext release]; |
| 328 | fGLContext = nil; |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame^] | 329 | #endif |
yangsu@google.com | 12d177d | 2011-08-01 17:07:12 +0000 | [diff] [blame] | 330 | } |
| 331 | |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame^] | 332 | #include "SkCGUtils.h" |
| 333 | |
robertphillips@google.com | d5b05ef | 2012-04-02 20:19:28 +0000 | [diff] [blame] | 334 | - (void)present { |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame^] | 335 | #if SK_SUPPORT_GPU |
bsalomon@google.com | ed16483 | 2012-04-03 18:06:20 +0000 | [diff] [blame] | 336 | if (nil != fGLContext) { |
| 337 | [fGLContext flushBuffer]; |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame^] | 338 | return; |
bsalomon@google.com | ed16483 | 2012-04-03 18:06:20 +0000 | [diff] [blame] | 339 | } |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame^] | 340 | #endif |
| 341 | const SkBitmap& bmp = fWind->getBitmap(); |
| 342 | SkASSERT(self.layer); |
| 343 | // FIXME: This causes the layer to flicker during animation. Making a copy of the CGImage does |
| 344 | // not help. |
| 345 | CGImageRef img = SkCreateCGImageRef(bmp); |
| 346 | self.layer.contents = (id) img; |
| 347 | CGImageRelease(img); |
yangsu@google.com | 12d177d | 2011-08-01 17:07:12 +0000 | [diff] [blame] | 348 | } |
robertphillips@google.com | d5b05ef | 2012-04-02 20:19:28 +0000 | [diff] [blame] | 349 | @end |