wip - import hostgl from abi v0 and adapt to abi v1/GL.library ABI
[AROS.git] / arch / all-hosted / libs / hostgl / hostgl_glaswapbuffers.c
blob7611e91677f257dcfa61dea9ca2043a8111e86f7
1 /*
2 Copyright 2011-2015, The AROS Development Team. All rights reserved.
3 $Id$
4 */
6 #include "hostgl_ctx_manager.h"
7 #include "hostgl_funcs.h"
8 #include <proto/exec.h>
9 #include <aros/debug.h>
10 #if defined(RENDERER_PBUFFER_WPA)
11 #include <proto/cybergraphics.h>
12 #include <proto/graphics.h>
13 #include <cybergraphx/cybergraphics.h>
14 static struct SignalSemaphore * GetX11SemaphoreFromBitmap(struct BitMap * bm);
15 #endif
16 #if defined(RENDERER_PIXMAP_BLIT)
17 #include <proto/graphics.h>
18 #endif
19 /*****************************************************************************
21 NAME */
23 void glASwapBuffers(
25 /* SYNOPSIS */
26 GLAContext ctx)
28 /* FUNCTION
29 Swaps the back with front buffers. MUST BE used to display the effect
30 of rendering onto the target RastPort, since GLA always work in
31 double buffer mode.
33 INPUTS
34 ctx - GL rendering context on which swap is to be performed.
36 RESULT
38 BUGS
40 INTERNALS
42 HISTORY
44 *****************************************************************************/
46 struct hostgl_context *_ctx = (struct hostgl_context *)ctx;
48 D(bug("[HostGL] TASK: 0x%x, SWAP 0x%x\n", FindTask(NULL), _ctx->glXctx));
50 if (_ctx)
52 #if defined(RENDERER_SEPARATE_X_WINDOW)
53 Display * dsp = NULL;
54 HostGL_Lock();
55 HostGL_UpdateGlobalGLXContext();
56 dsp = HostGL_GetGlobalX11Display();
57 GLXCALL(glXSwapBuffers, dsp, _ctx->glXWindow);
58 HostGL_UnLock();
59 #endif
60 #if defined(RENDERER_PBUFFER_WPA)
61 LONG line = 0;
62 LONG width = _ctx->framebuffer->width;
63 LONG height = _ctx->framebuffer->height;
65 /* This codes works correct with both 16bpp and 32 bpp pixel buffers */
67 HostGL_Lock();
68 HostGL_UpdateGlobalGLXContext();
69 ObtainSemaphore(GetX11SemaphoreFromBitmap(_ctx->visible_rp->BitMap));
70 GLCALL(glReadPixels, 0, 0, width, height, GL_BGRA, GL_UNSIGNED_BYTE, _ctx->swapbuffer);
71 ReleaseSemaphore(GetX11SemaphoreFromBitmap(_ctx->visible_rp->BitMap));
72 HostGL_UnLock();
74 /* Flip image */
75 for (line = 0; line < height / 2; line++)
77 ULONG * start = _ctx->swapbuffer + (line * width);
78 ULONG * end = _ctx->swapbuffer + ((height - line - 1) * width);
79 CopyMem(start, _ctx->swapbufferline, width * SWAPBUFFER_BPP);
80 CopyMem(end, start, width * SWAPBUFFER_BPP);
81 CopyMem(_ctx->swapbufferline, end, width * SWAPBUFFER_BPP);
84 WritePixelArray(_ctx->swapbuffer, 0, 0, width * SWAPBUFFER_BPP, _ctx->visible_rp, _ctx->left, _ctx->top,
85 width, height, RECTFMT_BGRA32);
86 #endif
87 #if defined(RENDERER_PIXMAP_BLIT)
88 HostGL_Lock();
89 HostGL_UpdateGlobalGLXContext();
90 GLXCALL(glXWaitGL);
91 HostGL_UnLock();
93 BltBitMapRastPort(_ctx->glXPixmapBM, 0, 0,
94 _ctx->visible_rp, _ctx->left, _ctx->top,
95 _ctx->framebuffer->width, _ctx->framebuffer->height,
96 192);
98 HostGL_Lock();
99 HostGL_UpdateGlobalGLXContext();
100 GLXCALL(glXWaitX);
101 HostGL_UnLock();
102 #endif
103 HostGL_CheckAndUpdateBufferSize(_ctx);
107 #if defined(RENDERER_PBUFFER_WPA)
108 /* HACK: EVIL HACK TO GET ACCESS TO X11 HIDD INTERNALS */
109 #include <proto/oop.h>
110 #define HIDD_BM_OBJ(bitmap) (*(OOP_Object **)&((bitmap)->Planes[0]))
112 struct bitmap_data
114 union
116 Window xwindow;
117 Pixmap pixmap;
118 } drawable;
119 Window masterxwindow;
120 Cursor cursor;
121 unsigned long sysplanemask;
122 Colormap colmap;
123 GC gc; /* !!! This is an X11 GC, NOT a HIDD gc */
124 Display *display;
125 int screen;
126 int flags;
129 struct fakefb_data
131 OOP_Object *framebuffer;
132 OOP_Object *fakegfxhidd;
135 struct x11_staticdata
137 /* These two members MUST be in the beginning of this structure
138 because they are exposed to disk-based part (see x11_class.h) */
139 UBYTE keycode2rawkey[256];
140 BOOL havetable;
142 struct SignalSemaphore sema; /* Protecting this whole struct */
143 struct SignalSemaphore x11sema;
146 struct x11clbase
148 struct Library library;
150 struct x11_staticdata xsd;
153 #define XSD(cl) (&((struct x11clbase *)cl->UserData)->xsd)
155 static struct SignalSemaphore * GetX11SemaphoreFromBitmap(struct BitMap * bm)
157 static struct SignalSemaphore * x11sem = NULL;
159 if (x11sem == NULL)
161 OOP_Object * hiddbm = HIDD_BM_OBJ(bm);
162 struct fakefb_data * fakefb = (struct fakefb_data *)OOP_INST_DATA(OOP_OCLASS(hiddbm), hiddbm);
163 x11sem = (&XSD(OOP_OCLASS(fakefb->framebuffer))->x11sema);
166 return x11sem;
168 #endif