dwrite: Break out basic IDWriteFont creation from requiring a logfont.
[wine/multimedia.git] / dlls / winemac.drv / cocoa_opengl.m
blob32d182f3c5620a656f271035307188ac68e47967
1 /*
2  * MACDRV Cocoa OpenGL code
3  *
4  * Copyright 2012, 2013 Ken Thomases for CodeWeavers Inc.
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
21 #include <OpenGL/gl.h>
22 #import "cocoa_opengl.h"
24 #include "macdrv_cocoa.h"
25 #include "cocoa_event.h"
28 @interface WineOpenGLContext ()
29 @property (retain, nonatomic) NSView* latentView;
30 @end
33 @implementation WineOpenGLContext
34 @synthesize latentView, needsUpdate, shouldClearToBlack;
36     - (void) dealloc
37     {
38         [[self view] release];
39         [latentView release];
40         [super dealloc];
41     }
43     - (void) setView:(NSView*)newView
44     {
45         NSView* oldView = [self view];
46         [super setView:newView];
47         [newView retain];
48         [oldView release];
49     }
51     - (void) clearDrawable
52     {
53         NSView* oldView = [self view];
54         [super clearDrawable];
55         [oldView release];
56     }
58     /* On at least some versions of Mac OS X, -[NSOpenGLContext clearDrawable] has the
59        undesirable side effect of ordering the view's GL surface off-screen.  This isn't
60        done when just changing the context's view to a different view (which I would
61        think would be analogous, since the old view and surface end up without a
62        context attached).  So, we finesse things by first setting the context's view to
63        a different view (the content view of an off-screen window) and then letting the
64        original implementation proceed. */
65     - (void) clearDrawableLeavingSurfaceOnScreen
66     {
67         static NSWindow* dummyWindow;
68         static dispatch_once_t once;
70         dispatch_once(&once, ^{
71             OnMainThread(^{
72                 dummyWindow = [[NSWindow alloc] initWithContentRect:NSMakeRect(0, 0, 100, 100)
73                                                           styleMask:NSBorderlessWindowMask
74                                                             backing:NSBackingStoreBuffered
75                                                               defer:NO];
76             });
77         });
79         [self setView:[dummyWindow contentView]];
80         [self clearDrawable];
81     }
83     - (void) clearToBlackIfNeeded
84     {
85         if (shouldClearToBlack)
86         {
87             NSOpenGLContext* origContext = [NSOpenGLContext currentContext];
89             [self makeCurrentContext];
91             glPushAttrib(GL_COLOR_BUFFER_BIT | GL_SCISSOR_BIT);
92             glDrawBuffer(GL_FRONT_AND_BACK);
93             glDisable(GL_SCISSOR_TEST);
94             glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
95             glClearColor(0, 0, 0, 1);
96             glClear(GL_COLOR_BUFFER_BIT);
97             glPopAttrib();
98             glFlush();
100             if (origContext)
101                 [origContext makeCurrentContext];
102             else
103                 [NSOpenGLContext clearCurrentContext];
105             shouldClearToBlack = FALSE;
106         }
107     }
109     - (void) removeFromViews:(BOOL)removeViews
110     {
111         if ([self view])
112         {
113             macdrv_remove_view_opengl_context((macdrv_view)[self view], (macdrv_opengl_context)self);
114             if (removeViews)
115                 [self clearDrawableLeavingSurfaceOnScreen];
116         }
117         if ([self latentView])
118         {
119             macdrv_remove_view_opengl_context((macdrv_view)[self latentView], (macdrv_opengl_context)self);
120             if (removeViews)
121                 [self setLatentView:nil];
122         }
123         needsUpdate = FALSE;
124     }
126 @end
129 /***********************************************************************
130  *              macdrv_create_opengl_context
132  * Returns a Cocoa OpenGL context created from a CoreGL context.  The
133  * caller is responsible for calling macdrv_dispose_opengl_context()
134  * when done with the context object.
135  */
136 macdrv_opengl_context macdrv_create_opengl_context(void* cglctx)
138     NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
139     WineOpenGLContext *context;
141     context = [[WineOpenGLContext alloc] initWithCGLContextObj:cglctx];
143     [pool release];
144     return (macdrv_opengl_context)context;
147 /***********************************************************************
148  *              macdrv_dispose_opengl_context
150  * Destroys a Cocoa OpenGL context previously created by
151  * macdrv_create_opengl_context();
152  */
153 void macdrv_dispose_opengl_context(macdrv_opengl_context c)
155     NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
156     WineOpenGLContext *context = (WineOpenGLContext*)c;
158     [context removeFromViews:YES];
159     [context release];
161     [pool release];
164 /***********************************************************************
165  *              macdrv_make_context_current
166  */
167 void macdrv_make_context_current(macdrv_opengl_context c, macdrv_view v)
169     NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
170     WineOpenGLContext *context = (WineOpenGLContext*)c;
171     NSView* view = (NSView*)v;
173     if (context && view)
174     {
175         if (view == [context view] || view == [context latentView])
176             macdrv_update_opengl_context(c);
177         else
178         {
179             [context removeFromViews:NO];
180             macdrv_add_view_opengl_context(v, c);
182             if (context.needsUpdate)
183             {
184                 context.needsUpdate = FALSE;
185                 [context setView:view];
186                 [context setLatentView:nil];
187             }
188             else
189             {
190                 if ([context view])
191                     [context clearDrawableLeavingSurfaceOnScreen];
192                 [context setLatentView:view];
193             }
194         }
196         [context makeCurrentContext];
198         if ([context view])
199             [context clearToBlackIfNeeded];
200     }
201     else
202     {
203         WineOpenGLContext* currentContext = (WineOpenGLContext*)[WineOpenGLContext currentContext];
205         if ([currentContext isKindOfClass:[WineOpenGLContext class]])
206         {
207             [WineOpenGLContext clearCurrentContext];
208             if (currentContext != context)
209                 [currentContext removeFromViews:YES];
210         }
212         if (context)
213             [context removeFromViews:YES];
214     }
216     [pool release];
219 /***********************************************************************
220  *              macdrv_update_opengl_context
221  */
222 void macdrv_update_opengl_context(macdrv_opengl_context c)
224     NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
225     WineOpenGLContext *context = (WineOpenGLContext*)c;
227     if (context.needsUpdate)
228     {
229         context.needsUpdate = FALSE;
230         if (context.latentView)
231         {
232             [context setView:context.latentView];
233             context.latentView = nil;
235             [context clearToBlackIfNeeded];
236         }
237         else
238             [context update];
239     }
241     [pool release];
244 /***********************************************************************
245  *              macdrv_flush_opengl_context
247  * Performs an implicit glFlush() and then swaps the back buffer to the
248  * front (if the context is double-buffered).
249  */
250 void macdrv_flush_opengl_context(macdrv_opengl_context c)
252     NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
253     WineOpenGLContext *context = (WineOpenGLContext*)c;
255     macdrv_update_opengl_context(c);
256     [context flushBuffer];
258     [pool release];