1 /*
2  Copyright (c) 2008-2011 Apple Inc.
3 
4  Permission is hereby granted, free of charge, to any person
5  obtaining a copy of this software and associated documentation files
6  (the "Software"), to deal in the Software without restriction,
7  including without limitation the rights to use, copy, modify, merge,
8  publish, distribute, sublicense, and/or sell copies of the Software,
9  and to permit persons to whom the Software is furnished to do so,
10  subject to the following conditions:
11 
12  The above copyright notice and this permission notice shall be
13  included in all copies or substantial portions of the Software.
14 
15  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18  NONINFRINGEMENT.  IN NO EVENT SHALL THE ABOVE LISTED COPYRIGHT
19  HOLDER(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  DEALINGS IN THE SOFTWARE.
23 
24  Except as contained in this notice, the name(s) of the above
25  copyright holders shall not be used in advertising or otherwise to
26  promote the sale, use or other dealings in this Software without
27  prior written authorization.
28 */
29 
30 /*
31  * This file works with the glXMakeContextCurrent readable drawable.
32  *
33  * The way it works is by swapping the currentDrawable for the currentReadable
34  * drawable if they are different.
35  */
36 #include <stdbool.h>
37 #include "glxclient.h"
38 #include "apple_glx_context.h"
39 #include "apple_xgl_api.h"
40 #include "main/glheader.h"
41 #include "glapitable.h"
42 
43 extern struct _glapi_table * __ogl_framework_api;
44 
45 struct apple_xgl_saved_state
46 {
47    bool swapped;
48 };
49 
50 static void
SetRead(struct apple_xgl_saved_state * saved)51 SetRead(struct apple_xgl_saved_state *saved)
52 {
53    struct glx_context *gc = __glXGetCurrentContext();
54 
55    /*
56     * By default indicate that the state was not swapped, so that UnsetRead
57     * functions correctly.
58     */
59    saved->swapped = false;
60 
61    /*
62     * If the readable drawable isn't the same as the drawable then
63     * the user has requested a readable drawable with glXMakeContextCurrent().
64     * We emulate this behavior by switching the current drawable.
65     */
66    if (None != gc->currentReadable
67        && gc->currentReadable != gc->currentDrawable) {
68       Display *dpy = glXGetCurrentDisplay();
69 
70       saved->swapped = true;
71 
72       if (apple_glx_make_current_context(dpy, gc->driContext, gc->driContext,
73                                          gc->currentReadable)) {
74          /* An error occurred, so try to restore the old context state. */
75          (void) apple_glx_make_current_context(dpy, gc->driContext, gc->driContext,
76                                                gc->currentDrawable);
77          saved->swapped = false;
78       }
79    }
80 }
81 
82 static void
UnsetRead(struct apple_xgl_saved_state * saved)83 UnsetRead(struct apple_xgl_saved_state *saved)
84 {
85    if (saved->swapped) {
86       struct glx_context *gc = __glXGetCurrentContext();
87       Display *dpy = glXGetCurrentDisplay();
88 
89       if (apple_glx_make_current_context(dpy, gc->driContext, gc->driContext,
90                                          gc->currentDrawable)) {
91          /*
92           * An error occurred restoring the drawable.
93           * It's unclear what to do about that.
94           */
95       }
96    }
97 }
98 
99 void
__applegl_glReadPixels(GLint x,GLint y,GLsizei width,GLsizei height,GLenum format,GLenum type,void * pixels)100 __applegl_glReadPixels(GLint x, GLint y, GLsizei width, GLsizei height,
101              GLenum format, GLenum type, void *pixels)
102 {
103    struct apple_xgl_saved_state saved;
104 
105    SetRead(&saved);
106 
107    __ogl_framework_api->ReadPixels(x, y, width, height, format, type, pixels);
108 
109    UnsetRead(&saved);
110 }
111 
112 void
__applegl_glCopyPixels(GLint x,GLint y,GLsizei width,GLsizei height,GLenum type)113 __applegl_glCopyPixels(GLint x, GLint y, GLsizei width, GLsizei height, GLenum type)
114 {
115    struct apple_xgl_saved_state saved;
116 
117    SetRead(&saved);
118 
119    __ogl_framework_api->CopyPixels(x, y, width, height, type);
120 
121    UnsetRead(&saved);
122 }
123 
124 void
__applegl_glCopyColorTable(GLenum target,GLenum internalformat,GLint x,GLint y,GLsizei width)125 __applegl_glCopyColorTable(GLenum target, GLenum internalformat, GLint x, GLint y,
126                  GLsizei width)
127 {
128    struct apple_xgl_saved_state saved;
129 
130    SetRead(&saved);
131 
132    __ogl_framework_api->CopyColorTable(target, internalformat, x, y, width);
133 
134    UnsetRead(&saved);
135 }
136