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 "glapitable.h"
41
42 extern struct _glapi_table * __ogl_framework_api;
43
44 struct apple_xgl_saved_state
45 {
46 bool swapped;
47 };
48
49 static void
SetRead(struct apple_xgl_saved_state * saved)50 SetRead(struct apple_xgl_saved_state *saved)
51 {
52 struct glx_context *gc = __glXGetCurrentContext();
53
54 /*
55 * By default indicate that the state was not swapped, so that UnsetRead
56 * functions correctly.
57 */
58 saved->swapped = false;
59
60 /*
61 * If the readable drawable isn't the same as the drawable then
62 * the user has requested a readable drawable with glXMakeContextCurrent().
63 * We emulate this behavior by switching the current drawable.
64 */
65 if (None != gc->currentReadable
66 && gc->currentReadable != gc->currentDrawable) {
67 Display *dpy = glXGetCurrentDisplay();
68
69 saved->swapped = true;
70
71 if (apple_glx_make_current_context(dpy, gc->driContext, gc->driContext,
72 gc->currentReadable)) {
73 /* An error occurred, so try to restore the old context state. */
74 (void) apple_glx_make_current_context(dpy, gc->driContext, gc->driContext,
75 gc->currentDrawable);
76 saved->swapped = false;
77 }
78 }
79 }
80
81 static void
UnsetRead(struct apple_xgl_saved_state * saved)82 UnsetRead(struct apple_xgl_saved_state *saved)
83 {
84 if (saved->swapped) {
85 struct glx_context *gc = __glXGetCurrentContext();
86 Display *dpy = glXGetCurrentDisplay();
87
88 if (apple_glx_make_current_context(dpy, gc->driContext, gc->driContext,
89 gc->currentDrawable)) {
90 /*
91 * An error occurred restoring the drawable.
92 * It's unclear what to do about that.
93 */
94 }
95 }
96 }
97
98 void
__applegl_glReadPixels(GLint x,GLint y,GLsizei width,GLsizei height,GLenum format,GLenum type,void * pixels)99 __applegl_glReadPixels(GLint x, GLint y, GLsizei width, GLsizei height,
100 GLenum format, GLenum type, void *pixels)
101 {
102 struct apple_xgl_saved_state saved;
103
104 SetRead(&saved);
105
106 __ogl_framework_api->ReadPixels(x, y, width, height, format, type, pixels);
107
108 UnsetRead(&saved);
109 }
110
111 void
__applegl_glCopyPixels(GLint x,GLint y,GLsizei width,GLsizei height,GLenum type)112 __applegl_glCopyPixels(GLint x, GLint y, GLsizei width, GLsizei height, GLenum type)
113 {
114 struct apple_xgl_saved_state saved;
115
116 SetRead(&saved);
117
118 __ogl_framework_api->CopyPixels(x, y, width, height, type);
119
120 UnsetRead(&saved);
121 }
122
123 void
__applegl_glCopyColorTable(GLenum target,GLenum internalformat,GLint x,GLint y,GLsizei width)124 __applegl_glCopyColorTable(GLenum target, GLenum internalformat, GLint x, GLint y,
125 GLsizei width)
126 {
127 struct apple_xgl_saved_state saved;
128
129 SetRead(&saved);
130
131 __ogl_framework_api->CopyColorTable(target, internalformat, x, y, width);
132
133 UnsetRead(&saved);
134 }
135