1 /* xscreensaver, Copyright © 1991-2021 Jamie Zawinski <jwz@jwz.org>
3 * Permission to use, copy, modify, distribute, and sell this software and its
4 * documentation for any purpose is hereby granted without fee, provided that
5 * the above copyright notice appear in all copies and that both that
6 * copyright notice and this permission notice appear in supporting
7 * documentation. No representations are made about the suitability of this
8 * software for any purpose. It is provided "as is" without express or
20 #define Assert(C, ...) do { if (!(C)) jwxyz_abort (__VA_ARGS__); } while(0)
24 #define MAX(a,b) ((a)>(b)?(a):(b))
25 #define MIN(a,b) ((a)<(b)?(a):(b))
27 #define JWXYZ_FONT_FAMILY 0 /* i.e. -[NSFont familyName] via XLFD */
28 #define JWXYZ_FONT_FACE 1 /* -[NSFont fontName] via native */
29 #define JWXYZ_FONT_RANDOM 2
31 #define JWXYZ_STYLE_BOLD 1
32 #define JWXYZ_STYLE_ITALIC 2
33 #define JWXYZ_STYLE_MONOSPACE 4
35 #define JWXYZ_QUERY_COLOR(dpy, pixel, mult, rgba) \
37 Visual *_V = DefaultVisualOfScreen (DefaultScreenOfDisplay (dpy)); \
38 (rgba)[0] = ((pixel) & _V->red_mask) * (mult) / _V->red_mask; \
39 (rgba)[1] = ((pixel) & _V->green_mask) * (mult) / _V->green_mask; \
40 (rgba)[2] = ((pixel) & _V->blue_mask) * (mult) / _V->blue_mask; \
41 (rgba)[3] = ((pixel) & _V->alpha_mask) * (mult) / _V->alpha_mask; \
44 /* jwxyz.m, jwxyz-gl.c, jwxyz-image.c */
45 extern void jwxyz_window_resized (Display *);
47 /* jwxyz-cocoa.m, jwxyz-android.c */
48 extern const XRectangle *jwxyz_frame (Drawable d); /* XGetGeometry sux. */
49 extern unsigned int jwxyz_drawable_depth (Drawable d);
51 /* 'scale' is pixels per point, with 72 points per inch. This matches the
52 meaning of -[UIView contentScaleFactor] on iOS.
54 extern float jwxyz_scale (Window main_window);
55 extern float jwxyz_font_scale (Window main_window);
57 extern const char *jwxyz_default_font_family (int require);
58 extern void *jwxyz_load_native_font (Window main_window,
59 int traits_jwxyz, int mask_jwxyz,
60 const char *font_name_ptr,
61 size_t font_name_length,
62 int font_name_type, float size,
63 char **family_name_ret,
64 int *ascent_ret, int *descent_ret);
65 extern void jwxyz_release_native_font (Display *, void *native_font);
67 /* Text metrics for aliased and antialiased text must match. */
68 extern void jwxyz_render_text (Display *, void *native_font,
69 const char *str, size_t len, Bool utf8_p,
70 Bool antialias_p, XCharStruct *cs_ret,
72 extern void jwxyz_get_pos (Window w, XPoint *vpos, XPoint *p);
73 #ifndef current_device_rotation
74 extern double current_device_rotation (void);
75 extern Bool ignore_rotation_p (Display *);
79 extern void jwxyz_validate_pixel (Display *dpy, unsigned long pixel,
80 unsigned int depth, Bool alpha_allowed_p);
81 extern Bool jwxyz_dumb_drawing_mode(Display *dpy, Drawable d, GC gc,
83 unsigned width, unsigned height);
84 extern void jwxyz_blit (const void *src_data, ptrdiff_t src_pitch,
85 unsigned src_x, unsigned src_y,
86 void *dst_data, ptrdiff_t dst_pitch,
87 unsigned dst_x, unsigned dst_y,
88 unsigned width, unsigned height);
89 extern void jwxyz_fill_rect (Display *, Drawable, GC,
91 unsigned int width, unsigned int height,
93 extern void jwxyz_gcv_defaults (Display *dpy, XGCValues *gcv, int depth);
94 extern int jwxyz_draw_string (Display *dpy, Drawable d, GC gc, int x, int y,
95 const char *str, size_t len, int utf8_p);
96 extern void *jwxyz_native_font (Font f);
98 #define SEEK_XY(dst, dst_pitch, x, y) \
99 ((uint32_t *)((char *)dst + dst_pitch * y + x * 4))
103 # include <CoreGraphics/CGGeometry.h>
104 # include <CoreGraphics/CGContext.h>
106 extern Display *jwxyz_quartz_make_display (Window w);
107 extern void jwxyz_quartz_free_display (Display *);
108 extern void jwxyz_flush_context (Display *);
110 # define jwxyz_assert_display(dpy)
111 # define jwxyz_assert_drawable(main_window, d)
112 # define jwxyz_assert_gl()
114 /* jwxyz-cocoa.m needs these from jwxyz.m for XCopyArea. */
115 extern void invalidate_drawable_cache (Drawable d);
116 extern void set_color (Display *dpy, CGContextRef cgc, unsigned long argb,
117 unsigned int depth, Bool alpha_allowed_p, Bool fill_p);
118 extern void push_gc (Drawable d, GC gc);
119 extern void push_color_gc (Display *dpy, Drawable d, GC gc,
121 Bool antialias_p, Bool fill_p);
122 extern CGPoint map_point (Drawable d, int x, int y);
123 extern void jwxyz_quartz_copy_area (Display *dpy, Drawable src, Drawable dst,
124 GC gc, int src_x, int src_y,
125 unsigned int width, unsigned int height,
126 int dst_x, int dst_y);
128 #define pop_gc(d,gc) CGContextRestoreGState (d->cgc)
130 # endif /* JWXYZ_QUARTZ */
134 # if defined(HAVE_IPHONE)
135 # include <OpenGLES/ES1/gl.h>
136 # elif defined(HAVE_COCOA)
137 # include <OpenGL/gl.h>
138 # elif defined(HAVE_ANDROID)
139 # include <GLES/gl.h>
144 /* utils/jwxyz-gl.c */
145 extern Display *jwxyz_gl_make_display (Window w);
146 extern void jwxyz_gl_free_display (Display *);
147 extern void jwxyz_set_matrices (Display *dpy, unsigned width, unsigned height,
149 extern void jwxyz_gl_flush (Display *dpy);
150 extern void jwxyz_gl_set_gc (Display *dpy, GC gc);
151 extern void jwxyz_gl_copy_area (Display *dpy, Drawable src, Drawable dst,
152 GC gc, int src_x, int src_y,
153 unsigned int width, unsigned int height,
154 int dst_x, int dst_y);
156 /* Only works if both drawables share OpenGL objects. OpenGL context sharing
157 works on OS X, iOS, and some (but not all!) EGL implementations. This would
158 also work with FBOs and one context for everything. Surprisingly slow and
161 extern void jwxyz_gl_copy_area_read_tex_image (Display *dpy,
163 int src_x, int src_y,
166 int dst_x, int dst_y);
167 extern void jwxyz_gl_copy_area_write_tex_image (Display *dpy, GC gc,
168 int src_x, int src_y,
172 int dst_x, int dst_y);
174 extern void jwxyz_gl_draw_image (Display *dpy, GC gc,
175 GLenum gl_texture_target,
176 unsigned int tex_w, unsigned int tex_h,
177 int src_x, int src_y, int src_depth,
178 unsigned int width, unsigned int height,
179 int dst_x, int dst_y, Bool flip_y);
181 /* glReadPixels followed by glTexImage2D. This is terrible, so only use this
182 if nothing else works.
184 extern void jwxyz_gl_copy_area_read_pixels (Display *dpy, Drawable src,
186 int src_x, int src_y,
189 int dst_x, int dst_y);
191 /* Platform-specific */
192 extern void jwxyz_bind_drawable (Display *dpy, Window w, Drawable d);
193 extern void jwxyz_assert_display (Display *);
194 extern void jwxyz_assert_drawable (Window main_window, Drawable d);
195 extern void jwxyz_assert_gl (void);
197 # endif /* JWXYZ_GL */
201 extern Display *jwxyz_image_make_display (Window w,
202 const unsigned char *rgba_bytes);
203 extern void jwxyz_image_free_display (Display *);
205 extern ptrdiff_t jwxyz_image_pitch (Drawable d);
206 extern void *jwxyz_image_data (Drawable d);
208 # endif /* JWXYZ_IMAGE */