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