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