X-Git-Url: http://git.hungrycats.org/cgi-bin/gitweb.cgi?p=xscreensaver;a=blobdiff_plain;f=jwxyz%2Fjwxyz-gl.c;h=41006033b7ebeb2a83da6fedf21e8413bce6d85c;hp=0766a4521f884f73dc107b3eacf9e87ed4f8036f;hb=39809ded547bdbb08207d3e514950425215b4410;hpb=4361b69d3178d7fc98d0388f9a223af6c2651aba diff --git a/jwxyz/jwxyz-gl.c b/jwxyz/jwxyz-gl.c index 0766a452..41006033 100644 --- a/jwxyz/jwxyz-gl.c +++ b/jwxyz/jwxyz-gl.c @@ -19,27 +19,11 @@ and iOS is in jwxyz.m. */ -/* Be advised, this is all very much a work in progress. - - TODO: The following should be implemented before OpenGL can be considered - practical here: - - Above all, pick the smallest not-yet working hack that utilizes the - needed functionality. - - Half-ass the drawing functions. - - [OK] What Interference needs - - Fast Pixmaps - - Whatever clipping is used in XScreenSaver (shape and/or bitmap clipping) - - Delayed context creation to support anti-aliasing/multisampling - - Everything these hacks need: - - FuzzyFlakes (needs wide lines) - - Greynetic - - [OK] Deluxe - - [OK] Get DangerBall going. - - [OK] iOS. - - [Interference, so far...] And fast, too. - - And text really needs to work for the FPS display. */ - -/* Also, Take note that OS X can actually run with 256 colors. */ +/* Be advised, this is all very much a work in progress. */ + +/* FWIW, at one point macOS 10.x could actually run with 256 colors. It might + not be able to do this anymore, though. + */ /* TODO: - malloc error checking @@ -51,7 +35,6 @@ - iOS: OpenGL ES 2.0 isn't always available. Use OpenGL ES 1.1. - OS X: Drivers can go back to OpenGL 1.1 (GeForce 2 MX on 10.5.8). - Use stencil buffers (OpenGL 1.0+) for bitmap clipping masks. - - glLogicOp is an actual thing that should work for GCs. - Pixmaps can be any of the following, depending on GL implementation. - This requires offscreen rendering. Fortunately, this is always available. @@ -74,10 +57,11 @@ */ /* OpenGL hacks call a number of X11 functions, including - * XCopyArea, XDrawString, XGetImage - * XCreatePixmap, XCreateGC, XCreateImage - * XPutPixel - * Check these, of course. */ + XCopyArea, XDrawString, XGetImage + XCreatePixmap, XCreateGC, XCreateImage + XPutPixel + Check these, of course. + */ #ifdef JWXYZ_GL /* entire file */ @@ -94,30 +78,6 @@ # import # include # include - -# define NSView UIView -# define NSRect CGRect -# define NSPoint CGPoint -# define NSSize CGSize -# define NSColor UIColor -# define NSImage UIImage -# define NSEvent UIEvent -# define NSFont UIFont -# define NSGlyph CGGlyph -# define NSWindow UIWindow -# define NSMakeSize CGSizeMake -# define NSBezierPath UIBezierPath -# define colorWithDeviceRed colorWithRed - -# define NSFontTraitMask UIFontDescriptorSymbolicTraits -// The values for the flags for NSFontTraitMask and -// UIFontDescriptorSymbolicTraits match up, not that it really matters here. -# define NSBoldFontMask UIFontDescriptorTraitBold -# define NSFixedPitchFontMask UIFontDescriptorTraitMonoSpace -# define NSItalicFontMask UIFontDescriptorTraitItalic - -# define NSOpenGLContext EAGLContext - # else # include # endif @@ -135,10 +95,6 @@ # include "jwzglesI.h" #endif -#ifdef HAVE_ANDROID -# include -#endif - #include "jwxyzI.h" #include "jwxyz-timers.h" #include "yarandom.h" @@ -146,46 +102,17 @@ #include "xft.h" #include "pow2.h" -#if defined HAVE_COCOA -# include -#else - -struct CGPoint { - float x; - float y; -}; -typedef struct CGPoint CGPoint; - -struct CGSize { - float width; - float height; -}; -typedef struct CGSize CGSize; - -struct CGRect { - CGPoint origin; - CGSize size; -}; -typedef struct CGRect CGRect; - -#endif - -# undef MAX -# undef MIN -# define MAX(a,b) ((a)>(b)?(a):(b)) -# define MIN(a,b) ((a)<(b)?(a):(b)) - -union color_bytes -{ - /* On 64-bit systems, high bits of the 32-bit pixel are available as scratch - space. I doubt if any screen savers need it, but just in case... */ - unsigned long pixel; +union color_bytes { + uint32_t pixel; uint8_t bytes[4]; }; struct jwxyz_Display { + const struct jwxyz_vtbl *vtbl; // Must come first. + Window main_window; - Screen *screen; + GLenum pixel_format, pixel_type; + Visual visual; struct jwxyz_sources_data *timers_data; Bool gl_texture_npot_p; @@ -197,23 +124,22 @@ struct jwxyz_Display { // #endif unsigned long window_background; -}; -struct jwxyz_Screen { - Display *dpy; - GLenum pixel_format, pixel_type; - unsigned long black, white; - Visual *visual; + int gc_function; + Bool gc_alpha_allowed_p; + + // Alternately, there could be one queue per pixmap. + size_t queue_size, queue_capacity; + Drawable queue_drawable; + GLint queue_mode; + GLshort *queue_vertex; + uint32_t *queue_color; + Bool queue_line_cap; }; struct jwxyz_GC { XGCValues gcv; unsigned int depth; - // CGImageRef clip_mask; // CGImage copy of the Pixmap in gcv.clip_mask -}; - -struct jwxyz_XFontSet { - XFontStruct *font; }; struct jwxyz_linked_point { @@ -221,66 +147,6 @@ struct jwxyz_linked_point { linked_point *next; }; -/* XGetImage in CoreGraphics JWXYZ has to deal with funky pixel formats - necessitating fast & flexible pixel conversion. OpenGL does image format - conversion itself, so alloc_color and query_color are mercifully simple. - */ -uint32_t -jwxyz_alloc_color (Display *dpy, - uint16_t r, uint16_t g, uint16_t b, uint16_t a) -{ - union color_bytes color; - - /* Instead of (int)(c / 256.0), another possibility is - (int)(c * 255.0 / 65535.0 + 0.5). This can be calculated using only - uint8_t integer_math(uint16_t c) { - unsigned c0 = c + 128; - return (c0 - (c0 >> 8)) >> 8; - } - */ - - color.bytes[0] = r >> 8; - color.bytes[1] = g >> 8; - color.bytes[2] = b >> 8; - color.bytes[3] = a >> 8; - - if (dpy->screen->pixel_format == GL_BGRA_EXT) { - color.pixel = color.bytes[2] | - (color.bytes[1] << 8) | - (color.bytes[0] << 16) | - (color.bytes[3] << 24); - } else { - Assert(dpy->screen->pixel_format == GL_RGBA, - "jwxyz_alloc_color: Unknown pixel_format"); - } - - return (uint32_t)color.pixel; -} - -// Converts an array of pixels ('src') from one format to another, placing the -// result in 'dest', according to the pixel conversion mode 'mode'. -void -jwxyz_query_color (Display *dpy, unsigned long pixel, uint8_t *rgba) -{ - union color_bytes color; - - if(dpy->screen->pixel_format == GL_RGBA) - { - color.pixel = pixel; - for (unsigned i = 0; i != 4; ++i) - rgba[i] = color.bytes[i]; - return; - } - - Assert (dpy->screen->pixel_format == GL_BGRA_EXT, - "jwxyz_query_color: Unknown pixel format"); - /* TODO: Cross-check with XAllocColor. */ - rgba[0] = (pixel >> 16) & 0xFF; - rgba[1] = (pixel >> 8) & 0xFF; - rgba[2] = (pixel >> 0) & 0xFF; - rgba[3] = (pixel >> 24) & 0xFF; -} - void jwxyz_assert_display(Display *dpy) @@ -344,30 +210,16 @@ gl_check_ver (const struct gl_version *caps, (caps->major == gl_major && caps->minor >= gl_minor); } -/* -static GLboolean gl_check_ext(const struct gl_caps *caps, - unsigned gl_major, - unsigned gl_minor, - const char *extension) -{ - return - gl_check_ver(caps, gl_major, gl_minor) || - gluCheckExtension(extension, caps->extensions); -} -*/ - #endif -// NSOpenGLContext *jwxyz_debug_context; - +extern const struct jwxyz_vtbl gl_vtbl; Display * -jwxyz_make_display (Window w) +jwxyz_gl_make_display (Window w) { Display *d = (Display *) calloc (1, sizeof(*d)); - d->screen = (Screen *) calloc (1, sizeof(Screen)); - d->screen->dpy = d; + d->vtbl = &gl_vtbl; # ifndef HAVE_JWZGLES struct gl_version version; @@ -397,29 +249,30 @@ jwxyz_make_display (Window w) // you're gonna get for getting a texture onto the screen. # ifdef HAVE_JWZGLES /* TODO: Make BGRA work on iOS. As it is, it breaks XPutImage. (glTexImage2D, AFAIK) */ - d->screen->pixel_format = GL_RGBA; /* + d->pixel_format = GL_RGBA; /* gluCheckExtension ((const GLubyte *) "GL_APPLE_texture_format_BGRA8888", extensions) ? GL_BGRA_EXT : GL_RGBA; */ - d->screen->pixel_type = GL_UNSIGNED_BYTE; + d->pixel_type = GL_UNSIGNED_BYTE; // See also OES_read_format. # else // !HAVE_JWZGLES if (gl_check_ver (&version, 1, 2) || (gluCheckExtension ((const GLubyte *) "GL_EXT_bgra", extensions) && gluCheckExtension ((const GLubyte *) "GL_APPLE_packed_pixels", extensions))) { - d->screen->pixel_format = GL_BGRA_EXT; + // APPLE_packed_pixels is only ever available on iOS, never Android. + d->pixel_format = GL_BGRA_EXT; // Both Intel and PowerPC-era docs say to use GL_UNSIGNED_INT_8_8_8_8_REV. - d->screen->pixel_type = GL_UNSIGNED_INT_8_8_8_8_REV; + d->pixel_type = GL_UNSIGNED_INT_8_8_8_8_REV; } else { - d->screen->pixel_format = GL_RGBA; - d->screen->pixel_type = GL_UNSIGNED_BYTE; + d->pixel_format = GL_RGBA; + d->pixel_type = GL_UNSIGNED_BYTE; } // GL_ABGR_EXT/GL_UNSIGNED_BYTE is another possibilty that may have made more // sense on PowerPC. # endif // !HAVE_JWZGLES - // On really old systems, it would make sense to split the texture - // into subsections. + // On really old systems, it would make sense to split textures + // into subsections, to work around the maximum texture size. # ifndef HAVE_JWZGLES d->gl_texture_npot_p = gluCheckExtension ((const GLubyte *) "GL_ARB_texture_rectangle", @@ -438,15 +291,23 @@ jwxyz_make_display (Window w) d->gl_texture_target = GL_TEXTURE_2D; # endif - d->screen->black = jwxyz_alloc_color (d, 0x0000, 0x0000, 0x0000, 0xFFFF); - d->screen->white = jwxyz_alloc_color (d, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF); - - Visual *v = (Visual *) calloc (1, sizeof(Visual)); + Visual *v = &d->visual; v->class = TrueColor; - v->red_mask = jwxyz_alloc_color (d, 0xFFFF, 0x0000, 0x0000, 0x0000); - v->green_mask = jwxyz_alloc_color (d, 0x0000, 0xFFFF, 0x0000, 0x0000); - v->blue_mask = jwxyz_alloc_color (d, 0x0000, 0x0000, 0xFFFF, 0x0000); - d->screen->visual = v; + if (d->pixel_format == GL_BGRA_EXT) { + v->rgba_masks[0] = 0x00ff0000; + v->rgba_masks[1] = 0x0000ff00; + v->rgba_masks[2] = 0x000000ff; + v->rgba_masks[3] = 0xff000000; + } else { + Assert(d->pixel_format == GL_RGBA, + "jwxyz_gl_make_display: Unknown pixel_format"); + for (unsigned i = 0; i != 4; ++i) { + union color_bytes color; + color.pixel = 0; + color.bytes[i] = 0xff; + v->rgba_masks[i] = color.pixel; + } + } d->timers_data = jwxyz_sources_init (XtDisplayToApplicationContext (d)); @@ -454,16 +315,9 @@ jwxyz_make_display (Window w) d->main_window = w; { - fputs((char *)glGetString(GL_VENDOR), stderr); - fputc(' ', stderr); - fputs((char *)glGetString(GL_RENDERER), stderr); - fputc(' ', stderr); - fputs((char *)glGetString(GL_VERSION), stderr); - fputc('\n', stderr); -// puts(caps.extensions); GLint max_texture_size; glGetIntegerv (GL_MAX_TEXTURE_SIZE, &max_texture_size); - printf ("GL_MAX_TEXTURE_SIZE: %d\n", max_texture_size); + Log ("GL_MAX_TEXTURE_SIZE: %d\n", max_texture_size); } // In case a GL hack wants to use X11 to draw offscreen, the rect_texture is available. @@ -490,44 +344,27 @@ jwxyz_make_display (Window w) glTexParameteri (d->gl_texture_target, GL_TEXTURE_WRAP_S, wrap); glTexParameteri (d->gl_texture_target, GL_TEXTURE_WRAP_T, wrap); + d->gc_function = GXcopy; + d->gc_alpha_allowed_p = False; + jwxyz_assert_display(d); return d; } void -jwxyz_free_display (Display *dpy) +jwxyz_gl_free_display (Display *dpy) { /* TODO: Go over everything. */ + free (dpy->queue_vertex); + free (dpy->queue_color); + jwxyz_sources_free (dpy->timers_data); - free (dpy->screen->visual); - free (dpy->screen); free (dpy); } -/* Call this after any modification to the bits on a Pixmap or Window. - Most Pixmaps are used frequently as sources and infrequently as - destinations, so it pays to cache the data as a CGImage as needed. - */ -static void -invalidate_drawable_cache (Drawable d) -{ - /* TODO: Kill this outright. jwxyz_bind_drawable handles any potential - invalidation. - */ - - /* - if (d && d->cgi) { - abort(); - CGImageRelease (d->cgi); - d->cgi = 0; - } - */ -} - - /* Call this when the View changes size or position. */ void @@ -545,6 +382,7 @@ jwxyz_window_resized (Display *dpy) Log("resize: %d, %d\n", new_width, new_height); + jwxyz_gl_flush (dpy); jwxyz_bind_drawable (dpy, dpy->main_window, dpy->main_window); // TODO: What does the iPhone need? @@ -569,125 +407,114 @@ jwxyz_window_resized (Display *dpy) // Stylish and attractive purple! // glClearColor (1, 0, 1, 0.5); // glClear (GL_COLOR_BUFFER_BIT); - - invalidate_drawable_cache (dpy->main_window); } -jwxyz_sources_data * +static jwxyz_sources_data * display_sources_data (Display *dpy) { return dpy->timers_data; } -Window -XRootWindow (Display *dpy, int screen) +static Window +root (Display *dpy) { - return dpy ? dpy->main_window : 0; + return dpy->main_window; } -Screen * -XDefaultScreenOfDisplay (Display *dpy) +static Visual * +visual (Display *dpy) { - return dpy ? dpy->screen : 0; + return &dpy->visual; } -Visual * -XDefaultVisualOfScreen (Screen *screen) -{ - return screen ? screen->visual : 0; -} -Display * -XDisplayOfScreen (Screen *s) -{ - return s ? s->dpy : 0; -} +/* GC attributes by usage and OpenGL implementation: -int -XDisplayNumberOfScreen (Screen *s) -{ - return 0; -} + All drawing functions: + function | glLogicOp w/ GL_COLOR_LOGIC_OP + clip_x_origin, clip_y_origin, clip_mask | Stencil mask -int -XScreenNumberOfScreen (Screen *s) -{ - return 0; -} + Shape drawing functions: + foreground, background | glColor* -unsigned long -XBlackPixelOfScreen(Screen *screen) -{ - return screen->black; -} + XDrawLines, XDrawRectangles, XDrawSegments: + line_width, cap_style, join_style | Lotsa vertices -unsigned long -XWhitePixelOfScreen(Screen *screen) -{ - return screen->white; -} + XFillPolygon: + fill_rule | Multiple GL_TRIANGLE_FANs -unsigned long -XCellsOfScreen(Screen *screen) -{ - Visual *v = screen->visual; - return v->red_mask | v->green_mask | v->blue_mask; -} + XDrawText: + font | Cocoa, then OpenGL display lists. + alpha_allowed_p | GL_BLEND -/* GC attributes by usage and OpenGL implementation: - * - * All drawing functions: - * function | glLogicOp w/ GL_COLOR_LOGIC_OP - * clip_x_origin, clip_y_origin, clip_mask | Stencil mask - * - * Shape drawing functions: - * foreground, background | glColor* - * - * XDrawLines, XDrawRectangles, XDrawSegments: - * line_width, cap_style, join_style | Lotsa vertices - * - * XFillPolygon: - * fill_rule | Multiple GL_TRIANGLE_FANs - * - * XDrawText: - * font | Cocoa, then OpenGL display lists. - * - * alpha_allowed_p | TODO - * antialias_p | TODO - * - * Nothing, really: - * subwindow_mode -*/ + antialias_p | Well, there's options: + * Multisampling would work, but that's something that would need to be set + per-Pixmap, not per-GC. + * GL_POINT, LINE, and POLYGON_SMOOTH are the old-school way of doing + this, but POINT_SMOOTH is unnecessary, and POLYGON_SMOOTH is missing from + GLES 1. All three are missing from GLES 2. Word on the street is that + these are deprecated anyway. + * Tiny textures with bilinear filtering to get the same effect as LINE_ and + POLYGON_SMOOTH. A bit tricky. + * Do nothing. Android hardware is very often high-DPI enough that + anti-aliasing doesn't matter all that much. -static void -set_clip_mask (GC gc) + Nothing, really: + subwindow_mode + */ + +static GLshort * +enqueue (Display *dpy, Drawable d, GC gc, int mode, size_t count) { - Assert (!gc->gcv.clip_mask, "set_gc: TODO"); + if (dpy->queue_size && + (dpy->gc_function != gc->gcv.function || + dpy->gc_alpha_allowed_p != gc->gcv.alpha_allowed_p || + dpy->queue_mode != mode || + dpy->queue_drawable != d)) { + jwxyz_gl_flush (dpy); + } + + jwxyz_bind_drawable (dpy, dpy->main_window, d); + jwxyz_gl_set_gc (dpy, gc); + + // TODO: Use glColor when we can get away with it. + size_t old_size = dpy->queue_size; + dpy->queue_size += count; + if (dpy->queue_size > dpy->queue_capacity) { + dpy->queue_capacity = dpy->queue_size * 2; + + uint32_t *new_color = realloc ( + dpy->queue_color, sizeof(*dpy->queue_color) * dpy->queue_capacity); + GLshort *new_vertex = realloc ( + dpy->queue_vertex, sizeof(*dpy->queue_vertex) * 2 * dpy->queue_capacity); + + if (!new_color || !new_vertex) + return NULL; + + dpy->queue_color = new_color; + dpy->queue_vertex = new_vertex; + } + + dpy->queue_mode = mode; + dpy->queue_drawable = d; + + union color_bytes color; + // TODO: validate color + JWXYZ_QUERY_COLOR (dpy, gc->gcv.foreground, 0xffull, color.bytes); + for (size_t i = 0; i != count; ++i) // TODO: wmemset when applicable. + dpy->queue_color[i + old_size] = color.pixel; + + return dpy->queue_vertex + old_size * 2; } + static void -set_function (int function) +set_clip_mask (GC gc) { - Assert (function == GXcopy, "set_gc: (TODO) Stubbed gcv function"); - - /* TODO: The GL_COLOR_LOGIC_OP extension is exactly what is needed here. (OpenGL 1.1) - Fun fact: The glLogicOp opcode constants are the same as the X11 GX* function constants | GL_CLEAR. - */ - -#if 0 - switch (gc->gcv.function) { - case GXset: - case GXclear: - case GXcopy:/*CGContextSetBlendMode (cgc, kCGBlendModeNormal);*/ break; - case GXxor: CGContextSetBlendMode (cgc, kCGBlendModeDifference); break; - case GXor: CGContextSetBlendMode (cgc, kCGBlendModeLighten); break; - case GXand: CGContextSetBlendMode (cgc, kCGBlendModeDarken); break; - default: Assert(0, "unknown gcv function"); break; - } -#endif + Assert (!gc->gcv.clip_mask, "set_clip_mask: TODO"); } @@ -701,55 +528,92 @@ set_color (Display *dpy, unsigned long pixel, unsigned int depth, GLfloat f = pixel; glColor4f (f, f, f, 1); } else { - /* TODO: alpha_allowed_p */ - uint8_t rgba[4]; - jwxyz_query_color (dpy, pixel, rgba); -#ifdef HAVE_JWZGLES - glColor4f (rgba[0] / 255.0f, rgba[1] / 255.0f, - rgba[2] / 255.0f, rgba[3] / 255.0f); -#else - glColor4ubv (rgba); -#endif + GLfloat rgba[4]; + JWXYZ_QUERY_COLOR (dpy, pixel, 1.0f, rgba); + glColor4f (rgba[0], rgba[1], rgba[2], rgba[3]); } } -/* Pushes a GC context; sets Function, ClipMask, and color. - */ -static void -set_color_gc (Display *dpy, GC gc, unsigned long color) +/* Pushes a GC context; sets Function and ClipMask. */ +void +jwxyz_gl_set_gc (Display *dpy, GC gc) { - // GC is NULL for XClearArea and XClearWindow. - unsigned int depth; int function; + Bool alpha_allowed_p; + + // GC is NULL for XClearArea and XClearWindow. if (gc) { function = gc->gcv.function; - depth = gc->depth; + alpha_allowed_p = gc->gcv.alpha_allowed_p; set_clip_mask (gc); } else { function = GXcopy; - depth = visual_depth (NULL, NULL); - // TODO: Set null clip mask here. + alpha_allowed_p = False; + // TODO: Set null clip mask for NULL GC here. + } + + /* GL_COLOR_LOGIC_OP: OpenGL 1.1. */ + if (function != dpy->gc_function) { + dpy->gc_function = function; + if (function != GXcopy) { + /* Fun fact: The glLogicOp opcode constants are the same as the X11 GX* + function constants | GL_CLEAR. + */ + glEnable (GL_COLOR_LOGIC_OP); + glLogicOp (gc->gcv.function | GL_CLEAR); + } else { + glDisable (GL_COLOR_LOGIC_OP); + } + } + + /* Cocoa uses add/subtract/difference blending in place of logical ops. + It looks nice, but implementing difference blending in OpenGL appears to + require GL_KHR_blend_equation_advanced, and support for this is not + widespread. + */ + + if (alpha_allowed_p != dpy->gc_alpha_allowed_p) { + dpy->gc_alpha_allowed_p = alpha_allowed_p; + if (gc && gc->gcv.alpha_allowed_p) { + // TODO: Maybe move glBlendFunc to XCreatePixmap? + glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + glEnable (GL_BLEND); + } else { + glDisable (GL_BLEND); + } } +} - set_function (function); - switch (function) { - case GXset: color = (depth == 1 ? 1 : WhitePixel(dpy,0)); break; - case GXclear: color = (depth == 1 ? 0 : BlackPixel(dpy,0)); break; +static void +set_color_gc (Display *dpy, Drawable d, GC gc, unsigned long color) +{ + jwxyz_gl_flush (dpy); + jwxyz_bind_drawable (dpy, dpy->main_window, d); + jwxyz_gl_set_gc (dpy, gc); + + unsigned int depth; + + if (gc) { + depth = gc->depth; + + switch (gc->gcv.function) { + case GXset: color = (depth == 1 ? 1 : WhitePixel(dpy,0)); break; + case GXclear: color = (depth == 1 ? 0 : BlackPixel(dpy,0)); break; + } + } else { + depth = visual_depth (NULL, NULL); } set_color (dpy, color, depth, gc ? gc->gcv.alpha_allowed_p : False); - - /* TODO: Antialiasing. */ - /* CGContextSetShouldAntialias (cgc, antialias_p); */ } /* Pushes a GC context; sets color to the foreground color. */ static void -set_fg_gc (Display *dpy, GC gc) +set_fg_gc (Display *dpy, Drawable d, GC gc) { - set_color_gc (dpy, gc, gc->gcv.foreground); + set_color_gc (dpy, d, gc, gc->gcv.foreground); } static void @@ -770,72 +634,49 @@ next_point(short *v, XPoint p, int mode) } } -int -XDrawPoints (Display *dpy, Drawable d, GC gc, - XPoint *points, int count, int mode) +static int +DrawPoints (Display *dpy, Drawable d, GC gc, + XPoint *points, int count, int mode) { - jwxyz_bind_drawable (dpy, dpy->main_window, d); - set_fg_gc (dpy, gc); - -/* - - glBegin(GL_POINTS); - for (unsigned i = 0; i < count; i++) { - next_point(v, points[i], mode); - glVertex2f(v[0] + 0.5f, v[1] + 0.5f); - } - glEnd(); - */ - short v[2] = {0, 0}; // TODO: XPoints can be fed directly to OpenGL. - GLshort *gl_points = malloc (count * 2 * sizeof(GLshort)); // TODO: malloc returns NULL. + GLshort *gl_points = enqueue (dpy, d, gc, GL_POINTS, count); // TODO: enqueue returns NULL. for (unsigned i = 0; i < count; i++) { next_point (v, points[i], mode); gl_points[2 * i] = v[0]; gl_points[2 * i + 1] = v[1]; } - - glMatrixMode (GL_MODELVIEW); - glTranslatef (0.5, 0.5, 0); - - glEnableClientState (GL_VERTEX_ARRAY); - glDisableClientState (GL_TEXTURE_COORD_ARRAY); - glVertexPointer (2, GL_SHORT, 0, gl_points); - glDrawArrays (GL_POINTS, 0, count); - - free (gl_points); - - glLoadIdentity (); - + return 0; } static GLint -texture_internalformat(Display *dpy) +texture_internalformat (Display *dpy) { #ifdef HAVE_JWZGLES - return dpy->screen->pixel_format; + return dpy->pixel_format; #else return GL_RGBA; #endif } -static GLenum gl_pixel_type(const Display *dpy) +static GLenum +gl_pixel_type (const Display *dpy) { - return dpy->screen->pixel_type; + return dpy->pixel_type; } static void clear_texture (Display *dpy) { glTexImage2D (dpy->gl_texture_target, 0, texture_internalformat(dpy), 0, 0, - 0, dpy->screen->pixel_format, gl_pixel_type (dpy), NULL); + 0, dpy->pixel_format, gl_pixel_type (dpy), NULL); } -static void set_white (void) +static void +set_white (void) { #ifdef HAVE_JWZGLES glColor4f (1, 1, 1, 1); @@ -845,6 +686,51 @@ static void set_white (void) } +void +jwxyz_gl_flush (Display *dpy) +{ + if (!dpy->queue_size) + return; + + // jwxyz_bind_drawable() and jwxyz_gl_set_gc() is called in enqueue(). + + glEnableClientState (GL_COLOR_ARRAY); + glEnableClientState (GL_VERTEX_ARRAY); + glDisableClientState (GL_TEXTURE_COORD_ARRAY); + + // TODO: Use glColor instead of glColorPointer if there's just one color. + // TODO: Does OpenGL use both GL_COLOR_ARRAY and glColor at the same time? + set_white(); + + Bool shifted = dpy->queue_mode == GL_POINTS || dpy->queue_mode == GL_LINES; + if (shifted) { + glMatrixMode (GL_MODELVIEW); + glTranslatef (0.5, 0.5, 0); + } + + glColorPointer (4, GL_UNSIGNED_BYTE, 0, dpy->queue_color); + glVertexPointer (2, GL_SHORT, 0, dpy->queue_vertex); + glDrawArrays (dpy->queue_mode, 0, dpy->queue_size); + + // TODO: This is right, right? + if (dpy->queue_mode == GL_LINES && dpy->queue_line_cap) { + Assert (!(dpy->queue_size % 2), "bad count for GL_LINES"); + glColorPointer (4, GL_UNSIGNED_BYTE, sizeof(GLubyte) * 8, + dpy->queue_color); + glVertexPointer (2, GL_SHORT, sizeof(GLshort) * 4, dpy->queue_vertex + 2); + glDrawArrays (GL_POINTS, 0, dpy->queue_size / 2); + } + + if (shifted) + glLoadIdentity (); + + glDisableClientState (GL_COLOR_ARRAY); + glDisableClientState (GL_VERTEX_ARRAY); + + dpy->queue_size = 0; +} + + void jwxyz_gl_copy_area_read_tex_image (Display *dpy, unsigned src_height, int src_x, int src_y, @@ -873,7 +759,7 @@ jwxyz_gl_copy_area_read_tex_image (Display *dpy, unsigned src_height, src_x, src_height - src_y - height, width, height, 0); } else { glTexImage2D (dpy->gl_texture_target, 0, internalformat, tex_w, tex_h, - 0, dpy->screen->pixel_format, gl_pixel_type(dpy), NULL); + 0, dpy->pixel_format, gl_pixel_type(dpy), NULL); glCopyTexSubImage2D (dpy->gl_texture_target, 0, 0, 0, src_x, src_height - src_y - height, width, height); } @@ -884,7 +770,7 @@ jwxyz_gl_copy_area_write_tex_image (Display *dpy, GC gc, int src_x, int src_y, unsigned int width, unsigned int height, int dst_x, int dst_y) { - Assert(gc->gcv.function == GXcopy, "XCopyArea: Unknown function"); + jwxyz_gl_set_gc (dpy, gc); /* TODO: Copy-pasted from read_tex_image. */ unsigned tex_w = width, tex_h = height; @@ -967,82 +853,11 @@ jwxyz_gl_copy_area_read_pixels (Display *dpy, Drawable src, Drawable dst, } -#if 0 -// TODO: Make sure offset works in super-sampled mode. -static void -adjust_point_for_line (GC gc, CGPoint *p) -{ - // Here's the authoritative discussion on how X draws lines: - // http://www.x.org/releases/current/doc/xproto/x11protocol.html#requests:CreateGC:line-width - if (gc->gcv.line_width <= 1) { - /* Thin lines are "drawn using an unspecified, device-dependent - algorithm", but seriously though, Bresenham's algorithm. Bresenham's - algorithm runs to and from pixel centers. - - There's a few screenhacks (Maze, at the very least) that set line_width - to 1 when it probably should be set to 0, so it's line_width <= 1 - instead of < 1. - */ - p->x += 0.5; - p->y -= 0.5; - } else { - /* Thick lines OTOH run from the upper-left corners of pixels. This means - that a horizontal thick line of width 1 straddles two scan lines. - Aliasing requires one of these scan lines be chosen; the following - nudges the point so that the right choice is made. */ - p->y -= 1e-3; - } -} -#endif - - -int -XDrawLine (Display *dpy, Drawable d, GC gc, int x1, int y1, int x2, int y2) -{ - // TODO: XDrawLine == XDrawSegments(nlines == 1), also in jwxyz.m - XSegment segment; - segment.x1 = x1; - segment.y1 = y1; - segment.x2 = x2; - segment.y2 = y2; - XDrawSegments (dpy, d, gc, &segment, 1); - - // when drawing a zero-length line, obey line-width and cap-style. -/* if (x1 == x2 && y1 == y2) { - int w = gc->gcv.line_width; - x1 -= w/2; - y1 -= w/2; - if (gc->gcv.line_width > 1 && gc->gcv.cap_style == CapRound) - return XFillArc (dpy, d, gc, x1, y1, w, w, 0, 360*64); - else { - if (!w) - w = 1; // Actually show zero-length lines. - return XFillRectangle (dpy, d, gc, x1, y1, w, w); - } - } - - CGPoint p = point_for_line (d, gc, x1, y1); - - push_fg_gc (dpy, d, gc, NO); - - CGContextRef cgc = d->cgc; - set_line_mode (cgc, &gc->gcv); - CGContextBeginPath (cgc); - CGContextMoveToPoint (cgc, p.x, p.y); - p = point_for_line(d, gc, x2, y2); - CGContextAddLineToPoint (cgc, p.x, p.y); - CGContextStrokePath (cgc); - pop_gc (d, gc); - invalidate_drawable_cache (d); */ - return 0; -} - -int -XDrawLines (Display *dpy, Drawable d, GC gc, XPoint *points, int count, - int mode) +static int +DrawLines (Display *dpy, Drawable d, GC gc, XPoint *points, int count, + int mode) { - jwxyz_bind_drawable (dpy, dpy->main_window, d); - set_fg_gc (dpy, gc); + set_fg_gc (dpy, d, gc); /* TODO: Thick lines * Zero-length line segments @@ -1085,84 +900,121 @@ XDrawLines (Display *dpy, Drawable d, GC gc, XPoint *points, int count, } -int -XDrawSegments (Display *dpy, Drawable d, GC gc, XSegment *segments, int count) +// Turn line segment into parallelogram based on line_width +// +// TODO: Fix epicycle hack with large thickness, and truchet line segment ends +// +static void drawThickLine(int line_width, XSegment *segments) { - jwxyz_bind_drawable (dpy, dpy->main_window, d); - set_fg_gc (dpy, gc); - - /* TODO: Thick lines. */ + double dx, dy, di, m, angle; + int sx1, sx2, sy1, sy2; + + sx1 = segments->x1; + sy1 = segments->y1; + sx2 = segments->x2; + sy2 = segments->y2; + + dx = sx1 - sx2; + dy = sy1 - sy2; + di = sqrt(dx * dx + dy * dy); + dx = dx / di; + dy = dy / di; + m = dy / dx; + + angle = atan(m); + + float sn = sin(angle); + float cs = cos(angle); + float line_width_f = (float) line_width; + + float wsn = line_width_f * (sn/2); + float csn = line_width_f * (cs/2); + + float x3 = sx1 - wsn; + float y3 = sy1 + csn; + float x4 = sx1 + wsn; + float y4 = sy1 - csn; + + float x5 = sx2 - wsn; + float y5 = sy2 + csn; + float x6 = sx2 + wsn; + float y6 = sy2 - csn; + + GLfloat coords[4][2] = + { + {x3, y3}, + {x4, y4}, + {x6, y6}, + {x5, y5} + }; + + glEnableClientState (GL_VERTEX_ARRAY); + glVertexPointer (2, GL_FLOAT, 0, coords); + jwxyz_assert_gl (); + glDrawArrays (GL_TRIANGLE_FAN, 0, 4); + jwxyz_assert_gl (); +} + + +static int +DrawSegments (Display *dpy, Drawable d, GC gc, XSegment *segments, int count) +{ + /* TODO: Caps on thick lines. */ /* Thin lines <= 1px are offset by +0.5; thick lines are not. */ - - glMatrixMode (GL_MODELVIEW); - glTranslatef (0.5, 0.5, 0); - glEnableClientState (GL_VERTEX_ARRAY); - glDisableClientState (GL_TEXTURE_COORD_ARRAY); - - Assert (sizeof(XSegment) == sizeof(short) * 4, "XDrawSegments: Data alignment mix-up."); // TODO: Static assert here. - Assert (sizeof(GLshort) == sizeof(short), "XDrawSegments: Data alignment mix-up."); // TODO: Static assert here. - Assert (offsetof(XSegment, x1) == 0, "XDrawSegments: Data alignment mix-up."); - Assert (offsetof(XSegment, x2) == 4, "XDrawSegments: Data alignment mix-up."); - glVertexPointer (2, GL_SHORT, 0, segments); - glDrawArrays (GL_LINES, 0, count * 2); - - if (gc->gcv.cap_style != CapNotLast) { - glVertexPointer (2, GL_SHORT, sizeof(GLshort) * 4, (const GLshort *)segments + 2); - glDrawArrays (GL_POINTS, 0, count); + if (count == 1 && gc->gcv.line_width > 1) { + set_fg_gc (dpy, d, gc); + drawThickLine(gc->gcv.line_width,segments); } - - glLoadIdentity (); - -/* CGRect wr = d->frame; - push_fg_gc (dpy, d, gc, NO); - set_line_mode (cgc, &gc->gcv); - CGContextBeginPath (cgc); - for (i = 0; i < count; i++) { - CGPoint p = point_for_line (d, gc, segments->x1, segments->y1); - CGContextMoveToPoint (cgc, p.x, p.y); - p = point_for_line (d, gc, segments->x2, segments->y2); - CGContextAddLineToPoint (cgc, p.x, p.y); - segments++; + else { + if (dpy->queue_line_cap != (gc->gcv.cap_style != CapNotLast)) + jwxyz_gl_flush (dpy); + dpy->queue_line_cap = gc->gcv.cap_style != CapNotLast; + + // TODO: Static assert here. + Assert (sizeof(XSegment) == sizeof(short) * 4 && + sizeof(GLshort) == sizeof(short) && + offsetof(XSegment, x1) == 0 && + offsetof(XSegment, x2) == 4, + "XDrawSegments: Data alignment mix-up."); + + memcpy (enqueue(dpy, d, gc, GL_LINES, count * 2), segments, + count * sizeof(XSegment)); } - CGContextStrokePath (cgc); - pop_gc (d, gc); - invalidate_drawable_cache (d); */ + return 0; } -int -XClearWindow (Display *dpy, Window win) +static int +ClearWindow (Display *dpy, Window win) { Assert (win == dpy->main_window, "not a window"); - const XRectangle *wr = jwxyz_frame (win); - /* TODO: Use glClear if there's no background pixmap. */ - return XClearArea (dpy, win, 0, 0, wr->width, wr->height, 0); -} -unsigned long -jwxyz_window_background (Display *dpy) -{ - return dpy->window_background; + jwxyz_gl_flush (dpy); + jwxyz_bind_drawable (dpy, win, win); + + GLfloat color[4]; + JWXYZ_QUERY_COLOR (dpy, dpy->window_background, 1.0f, color); + + glClearColor (color[0], color[1], color[2], 1); + glClear (GL_COLOR_BUFFER_BIT); + return True; } -int -XSetWindowBackground (Display *dpy, Window w, unsigned long pixel) +static unsigned long * +window_background (Display *dpy) { - Assert (w == dpy->main_window, "not a window"); - jwxyz_validate_pixel (dpy, pixel, visual_depth (NULL, NULL), False); - dpy->window_background = pixel; - return 0; + return &dpy->window_background; } -void -jwxyz_fill_rects (Display *dpy, Drawable d, GC gc, - const XRectangle *rectangles, unsigned long nrectangles, - unsigned long pixel) +static void +fill_rects (Display *dpy, Drawable d, GC gc, + const XRectangle *rectangles, unsigned long nrectangles, + unsigned long pixel) { - jwxyz_bind_drawable (dpy, dpy->main_window, d); - set_color_gc (dpy, gc, pixel); + set_color_gc (dpy, d, gc, pixel); + /* glBegin(GL_QUADS); for (unsigned i = 0; i != nrectangles; ++i) { @@ -1172,7 +1024,8 @@ jwxyz_fill_rects (Display *dpy, Drawable d, GC gc, glVertex2i(r->x + r->width, r->y + r->height); glVertex2i(r->x + r->width, r->y); } - glEnd(); */ + glEnd(); +*/ glEnableClientState (GL_VERTEX_ARRAY); glDisableClientState (GL_TEXTURE_COORD_ARRAY); @@ -1198,23 +1051,11 @@ jwxyz_fill_rects (Display *dpy, Drawable d, GC gc, } -int -XClearArea (Display *dpy, Window win, int x, int y, int w, int h, Bool exp) +static int +FillPolygon (Display *dpy, Drawable d, GC gc, + XPoint *points, int npoints, int shape, int mode) { - Assert(win == dpy->main_window, "XClearArea: not a window"); - Assert(!exp, "XClearArea: exposures unsupported"); - - jwxyz_fill_rect (dpy, win, 0, x, y, w, h, dpy->window_background); - return 0; -} - - -int -XFillPolygon (Display *dpy, Drawable d, GC gc, - XPoint *points, int npoints, int shape, int mode) -{ - jwxyz_bind_drawable (dpy, dpy->main_window, d); - set_fg_gc(dpy, gc); + set_fg_gc(dpy, d, gc); // TODO: Re-implement the GLU tesselation functions. @@ -1232,36 +1073,36 @@ XFillPolygon (Display *dpy, Drawable d, GC gc, if (shape == Convex) { - GLshort *vertices = malloc(npoints * sizeof(GLshort) * 2); // TODO: Oh look, another unchecked malloc. - short v[2] = {0, 0}; + GLshort *vertices = malloc(npoints * sizeof(GLshort) * 2); // TODO: Oh look, another unchecked malloc. + short v[2] = {0, 0}; - for (unsigned i = 0; i < npoints; i++) { - next_point(v, points[i], mode); - vertices[2 * i] = v[0]; - vertices[2 * i + 1] = v[1]; - } + for (unsigned i = 0; i < npoints; i++) { + next_point(v, points[i], mode); + vertices[2 * i] = v[0]; + vertices[2 * i + 1] = v[1]; + } - glEnableClientState (GL_VERTEX_ARRAY); - glDisableClientState (GL_TEXTURE_COORD_ARRAY); - - glVertexPointer (2, GL_SHORT, 0, vertices); - glDrawArrays (GL_TRIANGLE_FAN, 0, npoints); - - free(vertices); + glEnableClientState (GL_VERTEX_ARRAY); + glDisableClientState (GL_TEXTURE_COORD_ARRAY); + + glVertexPointer (2, GL_SHORT, 0, vertices); + glDrawArrays (GL_TRIANGLE_FAN, 0, npoints); + + free(vertices); } else if (shape == Nonconvex) { - // TODO: assert that x,y of first and last point match, as that is assumed + // TODO: assert that x,y of first and last point match, as that is assumed - linked_point *root; - root = (linked_point *) malloc( sizeof(linked_point) ); - set_points_list(points,npoints,root); - traverse_points_list(root); + linked_point *root; + root = (linked_point *) malloc( sizeof(linked_point) ); + set_points_list(points,npoints,root); + traverse_points_list(root); } else { - Assert((shape == Convex || shape == Nonconvex), "XFillPolygon: (TODO) Unimplemented shape"); + Assert((shape == Convex || shape == Nonconvex), + "XFillPolygon: (TODO) Unimplemented shape"); } - return 0; } @@ -1285,13 +1126,42 @@ mod_neg(int a, unsigned b) return a < 0 ? (b - 1) - (-(a + 1) % b) : a % b; } +/* TODO: Fill in arcs with line width > 1 */ +static int +draw_arc (Display *dpy, Drawable d, GC gc, int x, int y, + unsigned int width, unsigned int height, + int angle1, int angle2, Bool fill_p) +{ + int gglw = gc->gcv.line_width; + + if (fill_p || gglw <= 1) { + draw_arc_gl (dpy, d, gc, x, y, width, height, angle1, angle2, fill_p); + } + else { + int w1, w2, h1, h2, gglwh; + w1 = width + gglw; + h1 = height + gglw; + h2 = height - gglw; + w2 = width - gglw; + gglwh = gglw / 2; + int x1 = x - gglwh; + int x2 = x + gglwh; + int y1 = y - gglwh; + int y2 = y + gglwh; + //draw_arc_gl (dpy, d, gc, x, y, width, height, angle1, angle2, fill_p); + draw_arc_gl (dpy, d, gc, x1, y1, w1, h1, angle1, angle2, fill_p); + draw_arc_gl (dpy, d, gc, x2, y2, w2, h2, angle1, angle2, fill_p); + } + return 0; +} + + int -jwxyz_draw_arc (Display *dpy, Drawable d, GC gc, int x, int y, - unsigned int width, unsigned int height, - int angle1, int angle2, Bool fill_p) +draw_arc_gl (Display *dpy, Drawable d, GC gc, int x, int y, + unsigned int width, unsigned int height, + int angle1, int angle2, Bool fill_p) { - jwxyz_bind_drawable (dpy, dpy->main_window, d); - set_fg_gc(dpy, gc); + set_fg_gc(dpy, d, gc); /* Let's say the number of line segments needed to make a convincing circle is 4*sqrt(radius). (But these arcs aren't necessarily circular arcs...) */ @@ -1415,22 +1285,22 @@ jwxyz_draw_arc (Display *dpy, Drawable d, GC gc, int x, int y, } -XGCValues * -jwxyz_gc_gcv (GC gc) +static XGCValues * +gc_gcv (GC gc) { return &gc->gcv; } -unsigned int -jwxyz_gc_depth (GC gc) +static unsigned int +gc_depth (GC gc) { return gc->depth; } -GC -XCreateGC (Display *dpy, Drawable d, unsigned long mask, XGCValues *xgcv) +static GC +CreateGC (Display *dpy, Drawable d, unsigned long mask, XGCValues *xgcv) { struct jwxyz_GC *gc = (struct jwxyz_GC *) calloc (1, sizeof(*gc)); gc->depth = jwxyz_drawable_depth (d); @@ -1441,8 +1311,8 @@ XCreateGC (Display *dpy, Drawable d, unsigned long mask, XGCValues *xgcv) } -int -XFreeGC (Display *dpy, GC gc) +static int +FreeGC (Display *dpy, GC gc) { if (gc->gcv.font) XUnloadFont (dpy, gc->gcv.font); @@ -1461,54 +1331,10 @@ XFreeGC (Display *dpy, GC gc) } -/* -static void -flipbits (unsigned const char *in, unsigned char *out, int length) -{ - static const unsigned char table[256] = { - 0x00, 0x80, 0x40, 0xC0, 0x20, 0xA0, 0x60, 0xE0, - 0x10, 0x90, 0x50, 0xD0, 0x30, 0xB0, 0x70, 0xF0, - 0x08, 0x88, 0x48, 0xC8, 0x28, 0xA8, 0x68, 0xE8, - 0x18, 0x98, 0x58, 0xD8, 0x38, 0xB8, 0x78, 0xF8, - 0x04, 0x84, 0x44, 0xC4, 0x24, 0xA4, 0x64, 0xE4, - 0x14, 0x94, 0x54, 0xD4, 0x34, 0xB4, 0x74, 0xF4, - 0x0C, 0x8C, 0x4C, 0xCC, 0x2C, 0xAC, 0x6C, 0xEC, - 0x1C, 0x9C, 0x5C, 0xDC, 0x3C, 0xBC, 0x7C, 0xFC, - 0x02, 0x82, 0x42, 0xC2, 0x22, 0xA2, 0x62, 0xE2, - 0x12, 0x92, 0x52, 0xD2, 0x32, 0xB2, 0x72, 0xF2, - 0x0A, 0x8A, 0x4A, 0xCA, 0x2A, 0xAA, 0x6A, 0xEA, - 0x1A, 0x9A, 0x5A, 0xDA, 0x3A, 0xBA, 0x7A, 0xFA, - 0x06, 0x86, 0x46, 0xC6, 0x26, 0xA6, 0x66, 0xE6, - 0x16, 0x96, 0x56, 0xD6, 0x36, 0xB6, 0x76, 0xF6, - 0x0E, 0x8E, 0x4E, 0xCE, 0x2E, 0xAE, 0x6E, 0xEE, - 0x1E, 0x9E, 0x5E, 0xDE, 0x3E, 0xBE, 0x7E, 0xFE, - 0x01, 0x81, 0x41, 0xC1, 0x21, 0xA1, 0x61, 0xE1, - 0x11, 0x91, 0x51, 0xD1, 0x31, 0xB1, 0x71, 0xF1, - 0x09, 0x89, 0x49, 0xC9, 0x29, 0xA9, 0x69, 0xE9, - 0x19, 0x99, 0x59, 0xD9, 0x39, 0xB9, 0x79, 0xF9, - 0x05, 0x85, 0x45, 0xC5, 0x25, 0xA5, 0x65, 0xE5, - 0x15, 0x95, 0x55, 0xD5, 0x35, 0xB5, 0x75, 0xF5, - 0x0D, 0x8D, 0x4D, 0xCD, 0x2D, 0xAD, 0x6D, 0xED, - 0x1D, 0x9D, 0x5D, 0xDD, 0x3D, 0xBD, 0x7D, 0xFD, - 0x03, 0x83, 0x43, 0xC3, 0x23, 0xA3, 0x63, 0xE3, - 0x13, 0x93, 0x53, 0xD3, 0x33, 0xB3, 0x73, 0xF3, - 0x0B, 0x8B, 0x4B, 0xCB, 0x2B, 0xAB, 0x6B, 0xEB, - 0x1B, 0x9B, 0x5B, 0xDB, 0x3B, 0xBB, 0x7B, 0xFB, - 0x07, 0x87, 0x47, 0xC7, 0x27, 0xA7, 0x67, 0xE7, - 0x17, 0x97, 0x57, 0xD7, 0x37, 0xB7, 0x77, 0xF7, - 0x0F, 0x8F, 0x4F, 0xCF, 0x2F, 0xAF, 0x6F, 0xEF, - 0x1F, 0x9F, 0x5F, 0xDF, 0x3F, 0xBF, 0x7F, 0xFF - }; - while (length-- > 0) - *out++ = table[*in++]; -} -*/ - - -int -XPutImage (Display *dpy, Drawable d, GC gc, XImage *ximage, - int src_x, int src_y, int dest_x, int dest_y, - unsigned int w, unsigned int h) +static int +PutImage (Display *dpy, Drawable d, GC gc, XImage *ximage, + int src_x, int src_y, int dest_x, int dest_y, + unsigned int w, unsigned int h) { jwxyz_assert_display (dpy); @@ -1557,181 +1383,169 @@ XPutImage (Display *dpy, Drawable d, GC gc, XImage *ximage, if (jwxyz_dumb_drawing_mode(dpy, d, gc, dest_x, dest_y, w, h)) return 0; + jwxyz_gl_flush (dpy); jwxyz_bind_drawable (dpy, dpy->main_window, d); + jwxyz_gl_set_gc (dpy, gc); + int bpl = ximage->bytes_per_line; int bpp = ximage->bits_per_pixel; - /* int bsize = bpl * h; */ - char *data = ximage->data; -/* - CGRect r; - r.origin.x = wr->x + dest_x; - r.origin.y = wr->y + wr->height - dest_y - h; - r.size.width = w; - r.size.height = h; -*/ + char *tex_data; + unsigned src_w; + GLint tex_internalformat; + GLenum tex_format, tex_type; - Assert (gc->gcv.function == GXcopy, "XPutImage: (TODO) GC function not supported"); - Assert (!gc->gcv.clip_mask, "XPutImage: (TODO) GC clip mask not supported"); - if (bpp == 32) { - - /* Take advantage of the fact that it's ok for (bpl != w * bpp) - to create a CGImage from a sub-rectagle of the XImage. - */ - data += (src_y * bpl) + (src_x * 4); + tex_data = ximage->data + src_y * bpl + (src_x * 4); jwxyz_assert_display(dpy); /* There probably won't be any hacks that do this, but... */ Assert (!(bpl % 4), "XPutImage: bytes_per_line not divisible by four."); - unsigned src_w = bpl / 4; + tex_internalformat = texture_internalformat(dpy); + tex_format = dpy->pixel_format; + tex_type = gl_pixel_type(dpy); /* GL_UNPACK_ROW_LENGTH is not allowed to be negative. (sigh) */ # ifndef HAVE_JWZGLES - glPixelStorei (GL_UNPACK_ROW_LENGTH, src_w); src_w = w; + glPixelStorei (GL_UNPACK_ROW_LENGTH, src_w); +# else + src_w = bpl / 4; # endif - glPixelStorei (GL_UNPACK_ALIGNMENT, 4); + // glPixelStorei (GL_UNPACK_ALIGNMENT, 4); // Probably unnecessary. -# if 1 // defined HAVE_JWZGLES - // Regular OpenGL uses GL_TEXTURE_RECTANGLE_EXT in place of GL_TEXTURE_2D. - // TODO: Make use of OES_draw_texture. - // TODO: Coords might be wrong; things might be upside-down or backwards - // or whatever. - - unsigned tex_w = src_w, tex_h = h; - if (!dpy->gl_texture_npot_p) { - tex_w = to_pow2(tex_w); - tex_h = to_pow2(tex_h); - } + set_white (); + } else { + Assert (bpp == 1, "expected 1 or 32 bpp"); + Assert ((src_x % 8) == 0, + "XPutImage with non-byte-aligned 1bpp X offset not implemented"); - GLint internalformat = texture_internalformat(dpy); + const char *src_data = ximage->data + src_y * bpl + (src_x / 8); + unsigned w8 = (w + 7) / 8; - glBindTexture (dpy->gl_texture_target, dpy->rect_texture); + src_w = w8 * 8; - if (tex_w == src_w && tex_h == h) { - glTexImage2D (dpy->gl_texture_target, 0, internalformat, tex_w, tex_h, - 0, dpy->screen->pixel_format, gl_pixel_type(dpy), data); - } else { - // TODO: Sampling the last row might be a problem if src_x != 0. - glTexImage2D (dpy->gl_texture_target, 0, internalformat, tex_w, tex_h, - 0, dpy->screen->pixel_format, gl_pixel_type(dpy), NULL); - glTexSubImage2D (dpy->gl_texture_target, 0, 0, 0, src_w, h, - dpy->screen->pixel_format, gl_pixel_type(dpy), data); + tex_data = malloc(src_w * h); + + uint32_t *data_out = (uint32_t *)tex_data; + for(unsigned y = h; y; --y) { + for(unsigned x = 0; x != w8; ++x) { + // TODO: Does big endian work here? + uint8_t byte = src_data[x]; + uint32_t word = byte; + word = (word & 0x3) | ((word & 0xc) << 14); + word = (word & 0x00010001) | ((word & 0x00020002) << 7); + data_out[x << 1] = (word << 8) - word; + + word = byte >> 4; + word = (word & 0x3) | ((word & 0xc) << 14); + word = (word & 0x00010001) | ((word & 0x00020002) << 7); + data_out[(x << 1) | 1] = (word << 8) - word; + } + src_data += bpl; + data_out += src_w / 4; } - - set_white (); - // glEnable (dpy->gl_texture_target); - // glColor4f (0.5, 0, 1, 1); - glEnable (dpy->gl_texture_target); - glEnableClientState (GL_VERTEX_ARRAY); - glEnableClientState (GL_TEXTURE_COORD_ARRAY); - // TODO: Why are these ever turned on in the first place? - glDisableClientState (GL_COLOR_ARRAY); - glDisableClientState (GL_NORMAL_ARRAY); - // glDisableClientState (GL_TEXTURE_COORD_ARRAY); + tex_internalformat = GL_LUMINANCE; + tex_format = GL_LUMINANCE; + tex_type = GL_UNSIGNED_BYTE; - GLfloat vertices[4][2] = - { - {dest_x, dest_y}, - {dest_x, dest_y + h}, - {dest_x + w, dest_y + h}, - {dest_x + w, dest_y} - }; + // glPixelStorei (GL_UNPACK_ALIGNMENT, 1); + + set_color (dpy, gc->gcv.foreground, gc->depth, gc->gcv.alpha_allowed_p); + // TODO: Deal with the background color. + } + +# if 1 // defined HAVE_JWZGLES + // Regular OpenGL uses GL_TEXTURE_RECTANGLE_EXT in place of GL_TEXTURE_2D. + // TODO: Make use of OES_draw_texture. + + unsigned tex_w = src_w, tex_h = h; + if (!dpy->gl_texture_npot_p) { + tex_w = to_pow2(tex_w); + tex_h = to_pow2(tex_h); + } - GLfloat texcoord_w, texcoord_h; + glBindTexture (dpy->gl_texture_target, dpy->rect_texture); + + // A fun project: reimplement xshm.c by means of a PBO using + // GL_MAP_UNSYNCHRONIZED_BIT. + + // TODO: Would using glTexSubImage2D exclusively be faster? + if (tex_w == src_w && tex_h == h) { + glTexImage2D (dpy->gl_texture_target, 0, tex_internalformat, tex_w, tex_h, + 0, tex_format, tex_type, tex_data); + } else { + // TODO: Sampling the last row might be a problem if src_x != 0. + glTexImage2D (dpy->gl_texture_target, 0, tex_internalformat, tex_w, tex_h, + 0, tex_format, tex_type, NULL); + glTexSubImage2D (dpy->gl_texture_target, 0, 0, 0, src_w, h, + tex_format, tex_type, tex_data); + } + + if (bpp == 1) + free(tex_data); + + // TODO: This looks a lot like jwxyz_gl_draw_image. Refactor. + + // glEnable (dpy->gl_texture_target); + // glColor4f (0.5, 0, 1, 1); + glEnable (dpy->gl_texture_target); + glEnableClientState (GL_VERTEX_ARRAY); + glEnableClientState (GL_TEXTURE_COORD_ARRAY); + + // TODO: Why are these ever turned on in the first place? + glDisableClientState (GL_COLOR_ARRAY); + glDisableClientState (GL_NORMAL_ARRAY); + // glDisableClientState (GL_TEXTURE_COORD_ARRAY); + + GLfloat vertices[4][2] = + { + {dest_x, dest_y}, + {dest_x, dest_y + h}, + {dest_x + w, dest_y + h}, + {dest_x + w, dest_y} + }; + + GLfloat texcoord_w, texcoord_h; # ifndef HAVE_JWZGLES - if (dpy->gl_texture_target == GL_TEXTURE_RECTANGLE_EXT) { - texcoord_w = w; - texcoord_h = h; - } else + if (dpy->gl_texture_target == GL_TEXTURE_RECTANGLE_EXT) { + texcoord_w = w; + texcoord_h = h; + } else # endif /* HAVE_JWZGLES */ - { - texcoord_w = (double)w / tex_w; - texcoord_h = (double)h / tex_h; - } - - GLfloat tex_coords[4][2]; - tex_coords[0][0] = 0; - tex_coords[0][1] = 0; - tex_coords[1][0] = 0; - tex_coords[1][1] = texcoord_h; - tex_coords[2][0] = texcoord_w; - tex_coords[2][1] = texcoord_h; - tex_coords[3][0] = texcoord_w; - tex_coords[3][1] = 0; - - glVertexPointer (2, GL_FLOAT, 0, vertices); - glTexCoordPointer (2, GL_FLOAT, 0, tex_coords); - - // Respect the alpha channel in the XImage if we're using alpha. - if (gc->gcv.alpha_allowed_p) { - glEnable (GL_BLEND); - glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - } + { + texcoord_w = (double)w / tex_w; + texcoord_h = (double)h / tex_h; + } - glDrawArrays (GL_TRIANGLE_FAN, 0, 4); + GLfloat tex_coords[4][2]; + tex_coords[0][0] = 0; + tex_coords[0][1] = 0; + tex_coords[1][0] = 0; + tex_coords[1][1] = texcoord_h; + tex_coords[2][0] = texcoord_w; + tex_coords[2][1] = texcoord_h; + tex_coords[3][0] = texcoord_w; + tex_coords[3][1] = 0; - if (gc->gcv.alpha_allowed_p) - glDisable (GL_BLEND); + glVertexPointer (2, GL_FLOAT, 0, vertices); + glTexCoordPointer (2, GL_FLOAT, 0, tex_coords); + glDrawArrays (GL_TRIANGLE_FAN, 0, 4); -// clear_texture(); - glDisable (dpy->gl_texture_target); +//clear_texture(); + glDisable (dpy->gl_texture_target); # else - glRasterPos2i (dest_x, dest_y); - glPixelZoom (1, -1); - jwxyz_assert_display (dpy); - glDrawPixels (w, h, dpy->screen->pixel_format, gl_pixel_type(dpy), data); + glRasterPos2i (dest_x, dest_y); + glPixelZoom (1, -1); + jwxyz_assert_display (dpy); + glDrawPixels (w, h, dpy->pixel_format, gl_pixel_type(dpy), data); # endif - } else { // (bpp == 1) - - // Assert(FALSE, "XPutImage: TODO"); - // Check out ximage_(get|put)pixel_1 - -#if 0 - /* To draw a 1bpp image, we use it as a mask and fill two rectangles. - - #### However, the bit order within a byte in a 1bpp XImage is - the wrong way around from what Quartz expects, so first we - have to copy the data to reverse it. Shit! Maybe it - would be worthwhile to go through the hacks and #ifdef - each one that diddles 1bpp XImage->data directly... - */ - Assert ((src_x % 8) == 0, - "XPutImage with non-byte-aligned 1bpp X offset not implemented"); - data += (src_y * bpl) + (src_x / 8); // move to x,y within the data - unsigned char *flipped = (unsigned char *) malloc (bsize); - - flipbits ((unsigned char *) data, flipped, bsize); - - CGDataProviderRef prov = - CGDataProviderCreateWithData (NULL, flipped, bsize, NULL); - CGImageRef mask = CGImageMaskCreate (w, h, - 1, bpp, bpl, - prov, - NULL, /* decode[] */ - GL_FALSE); /* interpolate */ - push_fg_gc (dpy, d, gc, GL_TRUE); - - CGContextFillRect (cgc, r); // foreground color - CGContextClipToMask (cgc, r, mask); - set_color (dpy, cgc, gc->gcv.background, gc->depth, GL_FALSE, GL_TRUE); - CGContextFillRect (cgc, r); // background color - pop_gc (d, gc); - - free (flipped); - CGDataProviderRelease (prov); - CGImageRelease (mask); -#endif - } - jwxyz_assert_gl (); - invalidate_drawable_cache (d); return 0; } @@ -1740,17 +1554,18 @@ XPutImage (Display *dpy, Drawable d, GC gc, XImage *ximage, /* #### Twang calls XGetImage on the window intending to get a buffer full of black. This is returning a buffer full of white instead of black for some reason. */ -XImage * -XGetSubImage (Display *dpy, Drawable d, int x, int y, - unsigned int width, unsigned int height, - unsigned long plane_mask, int format, - XImage *dest_image, int dest_x, int dest_y) +static XImage * +GetSubImage (Display *dpy, Drawable d, int x, int y, + unsigned int width, unsigned int height, + unsigned long plane_mask, int format, + XImage *dest_image, int dest_x, int dest_y) { Assert ((width < 65535), "improbably large width"); Assert ((height < 65535), "improbably large height"); Assert ((x < 65535 && x > -65535), "improbably large x"); Assert ((y < 65535 && y > -65535), "improbably large y"); + jwxyz_gl_flush (dpy); jwxyz_bind_drawable (dpy, dpy->main_window, d); // TODO: What if this reads off the edge? What is supposed to happen? @@ -1785,7 +1600,7 @@ XGetSubImage (Display *dpy, Drawable d, int x, int y, uint32_t *dest_data = (uint32_t *)dest_image->data + pixels_per_line * dest_y + dest_x; glReadPixels (x, jwxyz_frame (d)->height - (y + height), width, height, - dpy->screen->pixel_format, gl_pixel_type(dpy), dest_data); + dpy->pixel_format, gl_pixel_type(dpy), dest_data); /* Flip this upside down. :( */ uint32_t *top = dest_data; @@ -1817,178 +1632,6 @@ XGetSubImage (Display *dpy, Drawable d, int x, int y, } -/* Returns a transformation matrix to do rotation as per the provided - EXIF "Orientation" value. - */ -/* -static CGAffineTransform -exif_rotate (int rot, CGSize rect) -{ - CGAffineTransform trans = CGAffineTransformIdentity; - switch (rot) { - case 2: // flip horizontal - trans = CGAffineTransformMakeTranslation (rect.width, 0); - trans = CGAffineTransformScale (trans, -1, 1); - break; - - case 3: // rotate 180 - trans = CGAffineTransformMakeTranslation (rect.width, rect.height); - trans = CGAffineTransformRotate (trans, M_PI); - break; - - case 4: // flip vertical - trans = CGAffineTransformMakeTranslation (0, rect.height); - trans = CGAffineTransformScale (trans, 1, -1); - break; - - case 5: // transpose (UL-to-LR axis) - trans = CGAffineTransformMakeTranslation (rect.height, rect.width); - trans = CGAffineTransformScale (trans, -1, 1); - trans = CGAffineTransformRotate (trans, 3 * M_PI / 2); - break; - - case 6: // rotate 90 - trans = CGAffineTransformMakeTranslation (0, rect.width); - trans = CGAffineTransformRotate (trans, 3 * M_PI / 2); - break; - - case 7: // transverse (UR-to-LL axis) - trans = CGAffineTransformMakeScale (-1, 1); - trans = CGAffineTransformRotate (trans, M_PI / 2); - break; - - case 8: // rotate 270 - trans = CGAffineTransformMakeTranslation (rect.height, 0); - trans = CGAffineTransformRotate (trans, M_PI / 2); - break; - - default: - break; - } - - return trans; -} -*/ - -void -jwxyz_draw_NSImage_or_CGImage (Display *dpy, Drawable d, - Bool nsimg_p, void *img_arg, - XRectangle *geom_ret, int exif_rotation) -{ - Assert (False, "jwxyz_draw_NSImage_or_CGImage: TODO stub"); -#if 0 - CGImageRef cgi; -# ifndef USE_IPHONE - CGImageSourceRef cgsrc; -# endif // USE_IPHONE - NSSize imgr; - - CGContextRef cgc = d->cgc; - - if (nsimg_p) { - - NSImage *nsimg = (NSImage *) img_arg; - imgr = [nsimg size]; - -# ifndef USE_IPHONE - // convert the NSImage to a CGImage via the toll-free-bridging - // of NSData and CFData... - // - NSData *nsdata = [NSBitmapImageRep - TIFFRepresentationOfImageRepsInArray: - [nsimg representations]]; - CFDataRef cfdata = (CFDataRef) nsdata; - cgsrc = CGImageSourceCreateWithData (cfdata, NULL); - cgi = CGImageSourceCreateImageAtIndex (cgsrc, 0, NULL); -# else // USE_IPHONE - cgi = nsimg.CGImage; -# endif // USE_IPHONE - - } else { - cgi = (CGImageRef) img_arg; - imgr.width = CGImageGetWidth (cgi); - imgr.height = CGImageGetHeight (cgi); - } - - Bool rot_p = (exif_rotation >= 5); - - if (rot_p) - imgr = NSMakeSize (imgr.height, imgr.width); - - CGRect winr = d->frame; - float rw = winr.size.width / imgr.width; - float rh = winr.size.height / imgr.height; - float r = (rw < rh ? rw : rh); - - CGRect dst, dst2; - dst.size.width = imgr.width * r; - dst.size.height = imgr.height * r; - dst.origin.x = (winr.size.width - dst.size.width) / 2; - dst.origin.y = (winr.size.height - dst.size.height) / 2; - - dst2.origin.x = dst2.origin.y = 0; - if (rot_p) { - dst2.size.width = dst.size.height; - dst2.size.height = dst.size.width; - } else { - dst2.size = dst.size; - } - - // Clear the part not covered by the image to background or black. - // - if (d->type == WINDOW) - XClearWindow (dpy, d); - else { - jwxyz_fill_rect (dpy, d, 0, 0, 0, winr.size.width, winr.size.height, - drawable_depth (d) == 1 ? 0 : BlackPixel(dpy,0)); - } - - CGAffineTransform trans = - exif_rotate (exif_rotation, rot_p ? dst2.size : dst.size); - - CGContextSaveGState (cgc); - CGContextConcatCTM (cgc, - CGAffineTransformMakeTranslation (dst.origin.x, - dst.origin.y)); - CGContextConcatCTM (cgc, trans); - //Assert (CGImageGetColorSpace (cgi) == dpy->colorspace, "bad colorspace"); - CGContextDrawImage (cgc, dst2, cgi); - CGContextRestoreGState (cgc); - -# ifndef USE_IPHONE - if (nsimg_p) { - CFRelease (cgsrc); - CGImageRelease (cgi); - } -# endif // USE_IPHONE - - if (geom_ret) { - geom_ret->x = dst.origin.x; - geom_ret->y = dst.origin.y; - geom_ret->width = dst.size.width; - geom_ret->height = dst.size.height; - } - - invalidate_drawable_cache (d); -#endif -} - -#ifndef HAVE_JWZGLES - -/* -static void -create_rectangle_texture (GLuint *texture) -{ - glGenTextures(1, texture); - glBindTexture(GL_TEXTURE_RECTANGLE_EXT, *texture); - glTexParameteri(GL_TEXTURE_RECTANGLE_EXT, GL_TEXTURE_MIN_FILTER, GL_NEAREST); - glTexParameteri(GL_TEXTURE_RECTANGLE_EXT, GL_TEXTURE_MAG_FILTER, GL_NEAREST); -} -*/ - -#endif - - #if 0 static Pixmap copy_pixmap (Display *dpy, Pixmap p) @@ -2021,91 +1664,20 @@ copy_pixmap (Display *dpy, Pixmap p) #endif -int -jwxyz_draw_string (Display *dpy, Drawable d, GC gc, int x, int y, - const char *str, size_t len, int utf8_p) +static int +SetClipMask (Display *dpy, GC gc, Pixmap m) { - Font ff = gc->gcv.font; - XCharStruct cs; - - char *data = 0; - jwxyz_render_text (dpy, jwxyz_native_font (ff), str, len, utf8_p, &cs, &data); - int w = cs.rbearing - cs.lbearing; - int h = cs.ascent + cs.descent; - - if (w < 0 || h < 0) abort(); - if (w == 0 || h == 0) { - if (data) free(data); - return 0; - } - - XImage *img = XCreateImage (dpy, dpy->screen->visual, 32, - ZPixmap, 0, data, w, h, 0, 0); - - /* The image of text is a 32-bit image, in white. - Take the red channel for intensity and use that as alpha. - replace RGB with the GC's foreground color. - This expects that XPutImage respects alpha and only writes - the bits that are not masked out. - This also assumes that XPutImage expects ARGB. + Log ("TODO: No clip masks yet"); + /* Protip: Do glClear(GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); + clearing just the stencil buffer in a packed depth/stencil arrangement is + slower than the above. Adreno recommends this, but other GPUs probably + benefit as well. */ - { - char *s = data; - char *end = s + (w * h * 4); - uint8_t rgba[4]; - jwxyz_query_color (dpy, gc->gcv.foreground, rgba); - while (s < end) { - - s[3] = s[1]; - s[0] = rgba[0]; - s[1] = rgba[1]; - s[2] = rgba[2]; - s += 4; - } - } - - { - Bool old_alpha = gc->gcv.alpha_allowed_p; - jwxyz_XSetAlphaAllowed (dpy, gc, True); - XPutImage (dpy, d, gc, img, 0, 0, - x + cs.lbearing, - y - cs.ascent, - w, h); - jwxyz_XSetAlphaAllowed (dpy, gc, old_alpha); - XDestroyImage (img); - } - - return 0; -} - - -int -XSetClipMask (Display *dpy, GC gc, Pixmap m) -{ -//#### abort(); -/* - TODO - - Assert (!!gc->gcv.clip_mask == !!gc->clip_mask, "GC clip mask mixup"); - - if (gc->gcv.clip_mask) { - XFreePixmap (dpy, gc->gcv.clip_mask); - CGImageRelease (gc->clip_mask); - } - - gc->gcv.clip_mask = copy_pixmap (dpy, m); - if (gc->gcv.clip_mask) - gc->clip_mask = - CGBitmapContextCreateImage (gc->gcv.clip_mask->cgc); - else - gc->clip_mask = 0; -*/ - return 0; } -int -XSetClipOrigin (Display *dpy, GC gc, int x, int y) +static int +SetClipOrigin (Display *dpy, GC gc, int x, int y) { gc->gcv.clip_x_origin = x; gc->gcv.clip_y_origin = y; @@ -2273,5 +1845,31 @@ void traverse_points_list(linked_point * root) } +const struct jwxyz_vtbl gl_vtbl = { + root, + visual, + display_sources_data, + + window_background, + draw_arc, + fill_rects, + gc_gcv, + gc_depth, + jwxyz_draw_string, + + jwxyz_gl_copy_area, + + DrawPoints, + DrawSegments, + CreateGC, + FreeGC, + ClearWindow, + SetClipMask, + SetClipOrigin, + FillPolygon, + DrawLines, + PutImage, + GetSubImage +}; #endif /* JWXYZ_GL -- entire file */