Skip to content

Apple's open-source XNU kernel on Android phones: its own loader and drivers, Metal on the Adreno. Runs from RAM; a reboot gives the phone back to Android.

C

GPU path: draw into the IOSurfaces themselves, no copies

vcvkk9466546d1889
diff --git a/tools/mesa/freedreno-darwin.patch b/tools/mesa/freedreno-darwin.patch
index ab0b73c..bc717ec 100644
--- a/tools/mesa/freedreno-darwin.patch
+++ b/tools/mesa/freedreno-darwin.patch
@@ -17,39 +17,6 @@
pre_args += '-DHAVE_LIBDRM'
if with_dri_platform == 'drm' and with_dri
with_gallium_drisw_kms = true
---- a/src/freedreno/meson.build
-+++ b/src/freedreno/meson.build
-@@ -22,7 +22,7 @@
- subdir('ir3')
- subdir('fdl')
-
--if system_has_kms_drm
-+if system_has_kms_drm or host_machine.system() == 'darwin'
- subdir('drm')
- endif
-
---- a/src/freedreno/drm/meson.build
-+++ b/src/freedreno/drm/meson.build
-@@ -45,6 +45,9 @@
- 'msm/msm_ringbuffer_sp.c',
- )
- libfreedreno_drm_files += libfreedreno_drm_msm_files
-+if host_machine.system() == 'darwin'
-+ libfreedreno_drm_files += files('../../redwood-drm/redwood_drm.c')
-+endif
-
- libfreedreno_drm_virtio_files = files(
- 'virtio/virtio_bo.c',
-@@ -72,8 +75,7 @@
- c_args : [no_override_init_args, libfreedreno_drm_flags],
- gnu_symbol_visibility : 'hidden',
- dependencies : libfreedreno_drm_deps,
-- # TODO
-- link_with : [libvdrm],
-+ link_with : freedreno_kmds.contains('virtio') ? [libvdrm] : [],
- build_by_default : false,
- )
-
--- a/src/freedreno/drm/freedreno_bo.c
+++ b/src/freedreno/drm/freedreno_bo.c
@@ -474,7 +474,11 @@
@@ -78,6 +45,28 @@
}
/* For internal use only, does not check FD_BO_NOMAP: */
+--- a/src/freedreno/drm/meson.build
++++ b/src/freedreno/drm/meson.build
+@@ -45,6 +45,9 @@
+ 'msm/msm_ringbuffer_sp.c',
+ )
+ libfreedreno_drm_files += libfreedreno_drm_msm_files
++if host_machine.system() == 'darwin'
++ libfreedreno_drm_files += files('../../redwood-drm/redwood_drm.c')
++endif
+
+ libfreedreno_drm_virtio_files = files(
+ 'virtio/virtio_bo.c',
+@@ -72,8 +75,7 @@
+ c_args : [no_override_init_args, libfreedreno_drm_flags],
+ gnu_symbol_visibility : 'hidden',
+ dependencies : libfreedreno_drm_deps,
+- # TODO
+- link_with : [libvdrm],
++ link_with : freedreno_kmds.contains('virtio') ? [libvdrm] : [],
+ build_by_default : false,
+ )
+
--- a/src/freedreno/ir3/ir3_disk_cache.c
+++ b/src/freedreno/ir3/ir3_disk_cache.c
@@ -33,6 +33,10 @@
@@ -99,6 +88,17 @@
}
void
+--- a/src/freedreno/meson.build
++++ b/src/freedreno/meson.build
+@@ -22,7 +22,7 @@
+ subdir('ir3')
+ subdir('fdl')
+
+-if system_has_kms_drm
++if system_has_kms_drm or host_machine.system() == 'darwin'
+ subdir('drm')
+ endif
+
--- a/src/gallium/drivers/freedreno/freedreno_screen.c
+++ b/src/gallium/drivers/freedreno/freedreno_screen.c
@@ -25,7 +25,9 @@
@@ -111,6 +111,15 @@
#include "freedreno_fence.h"
#include "freedreno_perfetto.h"
+@@ -838,7 +840,7 @@
+ if (whandle->type == WINSYS_HANDLE_TYPE_SHARED) {
+ bo = fd_bo_from_name(screen->dev, whandle->handle);
+ } else if (whandle->type == WINSYS_HANDLE_TYPE_KMS) {
+- bo = fd_bo_from_handle(screen->dev, whandle->handle, 0);
++ bo = fd_bo_from_handle(screen->dev, whandle->handle, whandle->size);
+ } else if (whandle->type == WINSYS_HANDLE_TYPE_FD) {
+ bo = fd_bo_from_dmabuf(screen->dev, whandle->handle);
+ } else {
@@ -1004,9 +1006,15 @@
screen->driconf.dual_color_blend_by_location =
driQueryOptionb(config->options, "dual_color_blend_by_location");
@@ -127,18 +136,86 @@
DBG("Pipe Info:");
DBG(" GPU-id: %s", fd_dev_name(screen->dev_id));
+--- a/src/gallium/frontends/osmesa/osmesa.c
++++ b/src/gallium/frontends/osmesa/osmesa.c
+@@ -86,6 +86,9 @@
+ extern struct pipe_screen *
+ osmesa_create_screen(void);
+
++extern struct pipe_resource *
++osmesa_surface_resource(struct pipe_screen *screen, uint32_t surface);
++
+
+
+ struct osmesa_buffer
+@@ -152,6 +155,28 @@
+ }
+ }
+
++/*
++ * There is no EGL here. What glEGLImageTargetTexture2DOES is given as an image is the ID of an
++ * IOSurface, whose memory becomes the texture's where the driver can do that (Darwin on redwood).
++ */
++static bool
++osmesa_st_get_egl_image(struct pipe_frontend_screen *fscreen, void *image,
++ struct st_egl_image *out)
++{
++ out->texture = osmesa_surface_resource(fscreen->screen,
++ (uint32_t)(uintptr_t)image);
++ if (!out->texture)
++ return false;
++ out->format = out->texture->format;
++ return true;
++}
++
++static bool
++osmesa_st_validate_egl_image(struct pipe_frontend_screen *fscreen, void *image)
++{
++ return true;
++}
++
+ static void
+ create_st_manager(void)
+ {
+@@ -162,7 +187,8 @@
+ if (global_fscreen) {
+ global_fscreen->screen = osmesa_create_screen();
+ global_fscreen->get_param = osmesa_st_get_param;
+- global_fscreen->get_egl_image = NULL;
++ global_fscreen->get_egl_image = osmesa_st_get_egl_image;
++ global_fscreen->validate_egl_image = osmesa_st_validate_egl_image;
+ }
+ }
+
+Только в b2/src/gallium/frontends/osmesa: osmesa.c.orig
+--- a/src/gallium/targets/osmesa/meson.build
++++ b/src/gallium/targets/osmesa/meson.build
+@@ -39,6 +39,7 @@
+ ],
+ dependencies : [
+ dep_ws2_32, dep_thread, dep_clock, dep_unwind, driver_swrast, idep_mesautil,
++ driver_freedreno, idep_xmlconfig, dep_libdrm,
+ ],
+ name_prefix : host_machine.system() == 'windows' ? '' : [], # otherwise mingw will create libosmesa.dll
+ soversion : host_machine.system() == 'windows' ? '' : '8',
--- a/src/gallium/targets/osmesa/osmesa_target.c
+++ b/src/gallium/targets/osmesa/osmesa_target.c
-@@ -26,6 +26,39 @@
+@@ -26,6 +26,87 @@
#include "sw/null/null_sw_winsys.h"
++#include "frontend/winsys_handle.h"
++#include "pipe/p_screen.h"
++#include "pipe/p_state.h"
++
+#ifdef GALLIUM_FREEDRENO
+#include "freedreno/drm/freedreno_drm_public.h"
+#include "util/driconf.h"
+#include "util/xmlconfig.h"
+#include <xf86drm.h>
+
++static int gpu_fd = -1;
++
+static const driOptionDescription freedreno_options[] = {
+#include "freedreno/driinfo_freedreno.h"
+};
@@ -162,14 +239,56 @@
+ config.options = &cache;
+ config.options_info = &info;
+ screen = fd_drm_screen_create_renderonly(fd, NULL, &config);
++ if (screen)
++ gpu_fd = fd;
+ return screen ? debug_screen_wrap(screen) : NULL;
+}
+#endif
++
++
++struct pipe_resource *
++osmesa_surface_resource(struct pipe_screen *screen, uint32_t surface);
++
++/*
++ * The memory of an IOSurface as a texture, to draw into and to draw with (Darwin on redwood):
++ * the kernel's GPU driver makes a buffer of the surface. NULL: the screen is not the GPU's, or
++ * the surface is not one plane of 32-bit pixels, blue first, in rows the GPU can address.
++ */
++struct pipe_resource *
++osmesa_surface_resource(struct pipe_screen *screen, uint32_t surface)
++{
++#ifdef GALLIUM_FREEDRENO
++ struct redwood_drm_surface s = { .surface = surface };
++ struct pipe_resource templ = {
++ .target = PIPE_TEXTURE_2D,
++ .format = PIPE_FORMAT_B8G8R8A8_UNORM,
++ .depth0 = 1,
++ .array_size = 1,
++ .bind = PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_RENDER_TARGET,
++ };
++ struct winsys_handle handle = { .type = WINSYS_HANDLE_TYPE_KMS };
++
++ if (gpu_fd < 0 || redwood_drm_surface(gpu_fd, &s) != 0)
++ return NULL;
++ /* (a buffer that is refused stays the kernel's until the surface is asked for again, or for good) */
++ if (s.pixel_format != 0x42475241 /* 'BGRA' */ || s.planes != 0)
++ return NULL;
++ templ.width0 = s.width;
++ templ.height0 = s.height;
++ handle.handle = s.handle;
++ handle.stride = s.row_bytes;
++ handle.offset = s.offset;
++ handle.size = s.size;
++ return screen->resource_from_handle(screen, &templ, &handle, 0);
++#else
++ return NULL;
++#endif
++}
+
struct pipe_screen *
osmesa_create_screen(void);
-@@ -37,6 +70,12 @@
+@@ -37,6 +118,12 @@
struct sw_winsys *winsys;
struct pipe_screen *screen;
@@ -182,13 +301,3 @@
/* We use a null software winsys since we always just render to ordinary
* driver resources.
*/
---- a/src/gallium/targets/osmesa/meson.build
-+++ b/src/gallium/targets/osmesa/meson.build
-@@ -39,6 +39,7 @@
- ],
- dependencies : [
- dep_ws2_32, dep_thread, dep_clock, dep_unwind, driver_swrast, idep_mesautil,
-+ driver_freedreno, idep_xmlconfig, dep_libdrm,
- ],
- name_prefix : host_machine.system() == 'windows' ? '' : [], # otherwise mingw will create libosmesa.dll
- soversion : host_machine.system() == 'windows' ? '' : '8',
diff --git a/tools/mesa/redwood-drm/redwood_drm.c b/tools/mesa/redwood-drm/redwood_drm.c
index b231b1c..b0b5d3f 100644
--- a/tools/mesa/redwood-drm/redwood_drm.c
+++ b/tools/mesa/redwood-drm/redwood_drm.c
@@ -17,6 +17,7 @@
#include "xf86drm.h"
#define REDWOOD_GPU_SERVICE "RedwoodGPU"
+#define REDWOOD_GPU_SURFACE 0x7e /* the request that is not Linux's */
static io_connect_t connection;
static int connection_fd = -1;
@@ -152,10 +153,8 @@ redwood_drm_map(int fd, uint32_t handle, uint64_t size)
(void)size;
/*
* Linux gives freedreno its buffers write-combined, with no cache of this processor's between
- * the program and the memory the GPU sees. This kernel cannot change how a buffer's pages are
- * cached once they are there (it panics in pmap_set_cache_attributes), so the mapping is an
- * ordinary one and the kernel's driver empties the cache of every buffer of a submission
- * as it takes the submission.
+ * the program and the memory the GPU sees. So does the kernel's driver here: the memory is
+ * made that way, and this mapping is as the memory is.
*/
if (IOConnectMapMemory64(connection, handle, mach_task_self(), &address, &mapped, kIOMapAnywhere) != 0) {
return (void *)-1; /* mmap()'s MAP_FAILED */
@@ -170,6 +169,19 @@ redwood_drm_unmap(int fd, uint32_t handle, void *address)
IOConnectUnmapMemory64(connection, handle, mach_task_self(), (mach_vm_address_t)(uintptr_t)address);
}
+int
+redwood_drm_surface(int fd, struct redwood_drm_surface *surface)
+{
+ int error = call(REDWOOD_GPU_SURFACE, surface, sizeof(*surface));
+
+ (void)fd;
+ if (error != 0) {
+ errno = error;
+ return -1;
+ }
+ return 0;
+}
+
static int
none(void)
{
diff --git a/tools/mesa/redwood-drm/xf86drm.h b/tools/mesa/redwood-drm/xf86drm.h
index 126597d..e132952 100644
--- a/tools/mesa/redwood-drm/xf86drm.h
+++ b/tools/mesa/redwood-drm/xf86drm.h
@@ -53,4 +53,18 @@ int drmSyncobjExportSyncFile(int fd, uint32_t handle, int *sync_file_fd);
void *redwood_drm_map(int fd, uint32_t handle, uint64_t size);
void redwood_drm_unmap(int fd, uint32_t handle, void *address);
+/*
+ * Not libdrm's either: the memory of an IOSurface as a buffer, in place of the file descriptors
+ * Linux passes such memory around in. The kernel's request and what it answers (struct
+ * redwood_surface in iokit/Kernel/RedwoodGPU.cpp); 0, or -1 with errno.
+ */
+struct redwood_drm_surface {
+ uint32_t surface; /* the IOSurface's ID */
+ uint32_t handle; /* back: the buffer (the same one each time while it is open), */
+ uint64_t size; /* how big it is, */
+ uint32_t offset, row_bytes; /* where the pixels start in it and how long a row of them is, */
+ uint32_t width, height, pixel_format, planes; /* and what they are */
+};
+int redwood_drm_surface(int fd, struct redwood_drm_surface *surface);
+
#endif
diff --git a/tools/mk-mesa.sh b/tools/mk-mesa.sh
index 5a8fe3d..8744dae 100755
--- a/tools/mk-mesa.sh
+++ b/tools/mk-mesa.sh
@@ -35,7 +35,7 @@ if [ ! -d mesa-$VERSION ]; then
[ -f mesa-$VERSION.tar.xz ] || curl -sL --fail -o mesa-$VERSION.tar.xz https://archive.mesa3d.org/mesa-$VERSION.tar.xz
echo "$SHA256 mesa-$VERSION.tar.xz" | sha256sum -c --quiet
tar xJf mesa-$VERSION.tar.xz
- for p in "$ROOT"/tools/mesa/*.patch; do patch -s -d mesa-$VERSION -p1 < "$p"; done
+ for p in "$ROOT"/tools/mesa/*.patch; do patch -s --no-backup-if-mismatch -d mesa-$VERSION -p1 < "$p"; done
fi
# ours, kept in the repository: always the current ones
rm -rf mesa-$VERSION/src/redwood-drm
diff --git a/userland/src/eagl.c b/userland/src/eagl.c
index 9586663..b898b1d 100644
--- a/userland/src/eagl.c
+++ b/userland/src/eagl.c
@@ -20,12 +20,13 @@
* what is drawn into it (it becomes a framebuffer's colour
* attachment) is the screen; or it is a picture to be shown
*
- * A GPU's driver does the last two by using the IOSurface's memory as the texture's. Nothing of
- * the kind is assumed here: the texture is an ordinary one, filled from the IOSurface when that
- * has changed (its seed) and read back into it when a frame ends (glFlush, glFinish), with
- * nothing but OpenGL ES calls. So this file does not know what draws: the library is reached
- * through its gl* functions and the four functions of redwood_gl.h. A driver that can share the
- * memory needs surface_to_texture() and target_to_surface() replaced, and nothing else.
+ * A GPU's driver does the last two by using the IOSurface's memory as the texture's, and so it
+ * is done here where the library can (redwood_gl_surface_texture(); surface_shared()): nothing
+ * is copied, and a frame's end waits for the GPU. Where it cannot (a software driver, a surface
+ * the GPU cannot address, a picture to be turned over) the texture is an ordinary one, filled
+ * from the IOSurface when that has changed (its seed) and read back into it when a frame ends
+ * (glFlush, glFinish), with nothing but OpenGL ES calls. Either way this file does not know
+ * what draws: the library is reached through its gl* functions and the functions of redwood_gl.h.
*
* Where CoreAnimation relies on iOS's library being what it is, this stands in between:
* blue-first pixels into a texture called GL_RGBA (internal_format()), shaders that read
@@ -35,9 +36,13 @@
* What CoreAnimation asks of OpenGL ES is written down as it goes (the log lines "redwood-gl:"):
* strings and limits it reads, texture formats and sizes, calls and triangles per frame. More
* with the kernel's boot-args: gltrace=FIRST-LAST writes down every call of those frames (lines
- * "gl:", with a pixel of the framebuffer after each draw), glshaders=1 the text of every shader,
- * glshow=TEXTURE puts that texture over the corner of the picture, glplain=0 leaves the draws
- * without a program alone.
+ * "gl:"; after each draw a pixel or two of the framebuffer and how much of it is not black);
+ * gltrace_at=SECONDS does so for the first frame that long after the window server started,
+ * gltrace_draws=N for the first frame after one of N draws; glshaders=1 writes down the text of
+ * every shader, glshow=TEXTURE puts that texture over the corner of the picture, glplain=0
+ * leaves the draws without a program alone, glcopy=1 copies every IOSurface as if the library
+ * could not share one, glenv=NAME=VALUE puts that into the environment for the library to find
+ * (its own switches: FD_MESA_DEBUG=sysmem, say).
*/
#include <CoreFoundation/CoreFoundation.h>
#include <IOSurface/IOSurfaceRef.h>
@@ -178,6 +183,7 @@ struct image {
IOSurfaceRef surface;
bool target; /* drawn into (attachImage) rather than shown (texImageIOSurface) */
bool invert, drawn;
+ bool shared; /* the texture is the surface's memory: nothing to copy */
GLenum to, internal, format, type;
uint32_t plane, seed;
GLsizei width, height;
@@ -205,11 +211,11 @@ struct eagl {
} arrays[8]; /* the vertex arrays, as given */
bool in_frame;
/* a frame's worth */
- unsigned calls, draws, triangles, uploads, clears;
- uint64_t upload_bytes, began;
+ unsigned calls, draws, triangles, uploads, clears, last_draws;
+ uint64_t upload_bytes, began, attaching, flushing;
/* everything so far */
- unsigned frames, shaders, programs, errors;
- uint64_t all_calls, all_draws, all_triangles, readback_us, frame_us;
+ unsigned frames, shaders, programs, errors, shared, refused;
+ uint64_t all_calls, all_draws, all_triangles, readback_us, wait_us, attach_us, flush_us, frame_us;
unsigned most_calls, most_triangles;
const char *sources[64]; /* the text of the shaders being made, by name modulo this */
bool fragment[64]; /* ... and which of the two kinds each is */
@@ -217,11 +223,14 @@ struct eagl {
};
static unsigned trace_first, trace_last; /* the frames whose calls are written down */
+static uint64_t trace_at; /* ... or the first frame from this time on (now_us()) */
+static unsigned trace_draws; /* ... or the first frame after one of so many draws */
static int all_shaders; /* boot-arg glshaders=1: the text of every shader, not of the first two */
static __thread struct eagl *current;
static struct eagl *contexts[8];
static uint32_t slot_calls[SLOTS];
static int check_errors = 1; /* ask the library for an error after every call (the first frames) */
+static int share_surfaces = 1; /* boot-arg glcopy=1: 0 */
static const char *eagl_slot_name(unsigned offset);
@@ -327,10 +336,12 @@ summary(struct eagl *e)
unsigned frames = e->frames ? e->frames : 1;
say("redwood-gl: after %u frames: %llu calls, %llu draws and %llu triangles a frame on average; the most in one: "
- "%u calls, %u triangles; %llu ms a frame, of which %llu copying the picture out; %u shaders, %u programs; "
- "biggest texture %u x %u; %u errors\n", e->frames, e->all_calls / frames, e->all_draws / frames,
- e->all_triangles / frames, e->most_calls, e->most_triangles, e->frame_us / frames / 1000,
- e->readback_us / frames / 1000, e->shaders, e->programs, widest_texture, tallest_texture, e->errors);
+ "%u calls, %u triangles; %llu us a frame, of which %llu handing the commands on, %llu waiting for the picture "
+ "to be drawn and %llu copying it out, and before each %llu making textures of IOSurfaces; %u IOSurfaces were "
+ "the textures' memory, %u had to be copied; %u shaders, %u programs; biggest texture %u x %u; %u errors\n",
+ e->frames, e->all_calls / frames, e->all_draws / frames, e->all_triangles / frames, e->most_calls,
+ e->most_triangles, e->frame_us / frames, e->flush_us / frames, e->wait_us / frames, e->readback_us / frames,
+ e->attach_us / frames, e->shared, e->refused, e->shaders, e->programs, widest_texture, tallest_texture, e->errors);
for (unsigned i = 0; i < sizeof(formats) / sizeof(formats[0]) && formats[i].count; i++) {
say("redwood-gl: textures: %s %#x/%#x/%#x: %u times, up to %u x %u\n", formats[i].how, formats[i].internal,
formats[i].format, formats[i].type, formats[i].count, formats[i].widest, formats[i].tallest);
@@ -414,9 +425,27 @@ bytes_a_pixel(GLenum format, GLenum type)
}
/*
- * Fill a texture, which is bound, from its IOSurface. This and target_to_surface() are where a
- * driver that maps the IOSurface's memory would do something else.
+ * Make a texture, which is bound, of its IOSurface's own memory, if that is what is asked for
+ * (the whole of a surface of blue-first pixels, the same way up) and the library can do it.
*/
+static bool
+surface_shared(struct eagl *e, struct image *image)
+{
+ IOSurfaceRef surface = image->surface;
+
+ if (!share_surfaces || image->invert || image->format != GL_BGRA || image->type != GL_UNSIGNED_BYTE ||
+ image->plane != 0 || (size_t)image->width != IOSurfaceGetWidth(surface) ||
+ (size_t)image->height != IOSurfaceGetHeight(surface) ||
+ !redwood_gl_surface_texture(e->gl, IOSurfaceGetID(surface))) {
+ e->refused++;
+ return false;
+ }
+ image->shared = true;
+ e->shared++;
+ return true;
+}
+
+/* Fill a texture, which is bound, from its IOSurface, unless it is the surface. */
static void
surface_to_texture(struct eagl *e, struct image *image)
{
@@ -427,6 +456,9 @@ surface_to_texture(struct eagl *e, struct image *image)
GLint row_length = 0, alignment = 4;
uint8_t *base, *flipped = NULL;
+ if (image->shared) {
+ return;
+ }
if (IOSurfaceLock(surface, kIOSurfaceLockReadOnly, NULL) != 0) {
say("redwood-gl: an IOSurface cannot be locked\n");
return;
@@ -453,7 +485,7 @@ surface_to_texture(struct eagl *e, struct image *image)
e->upload_bytes += row * (size_t)image->height;
}
-/* What has been drawn into a texture, into its IOSurface. */
+/* What has been drawn into a texture, into its IOSurface, unless it is there (frame_end() waits for it). */
static void
target_to_surface(struct eagl *e, struct image *image)
{
@@ -461,6 +493,14 @@ target_to_surface(struct eagl *e, struct image *image)
size_t row = IOSurfaceGetBytesPerRow(surface);
uint8_t *base;
+ image->drawn = false;
+ if (image->shared) {
+ /* whoever looks at the surface's seed is to see that it has changed */
+ if (IOSurfaceLock(surface, 0, NULL) == 0) {
+ IOSurfaceUnlock(surface, 0, NULL);
+ }
+ return;
+ }
if (e->reader == 0) {
glGenFramebuffers(1, &e->reader);
}
@@ -481,7 +521,6 @@ target_to_surface(struct eagl *e, struct image *image)
IOSurfaceUnlock(surface, 0, NULL);
}
glBindFramebuffer(GL_FRAMEBUFFER, e->framebuffer);
- image->drawn = false;
}
/*
@@ -538,13 +577,18 @@ frame_begin(struct eagl *e)
e->in_frame = true;
e->began = now_us();
eagl_tracing = e->frames + 1 >= trace_first && e->frames + 1 <= trace_last;
+ if ((trace_at != 0 && e->began >= trace_at) || (trace_draws != 0 && e->last_draws == trace_draws)) {
+ eagl_tracing = 1; /* (once) */
+ trace_at = 0;
+ trace_draws = 0;
+ }
if (eagl_tracing) {
eagl_trace("---- frame %u (framebuffer %u)\n", e->frames + 1, e->framebuffer);
}
for (unsigned i = 0; i < e->image_count; i++) {
struct image *image = &e->images[i];
- if (image->target || IOSurfaceGetSeed(image->surface) == image->seed) {
+ if (image->target || image->shared || IOSurfaceGetSeed(image->surface) == image->seed) {
continue;
}
if (!rebound) {
@@ -559,20 +603,32 @@ frame_begin(struct eagl *e)
}
}
-/* glFlush, glFinish: what was drawn must be where the display takes it from. */
+/*
+ * glFlush, glFinish: what was drawn must be where the display takes it from. `finished`: the
+ * library has been waited for already.
+ */
static void
-frame_end(struct eagl *e)
+frame_end(struct eagl *e, bool finished)
{
- uint64_t before = now_us(), after;
- unsigned copied = 0;
+ uint64_t before = now_us(), waited, after;
+ unsigned copied = 0, in_place = 0;
+ for (unsigned i = 0; i < e->image_count; i++) {
+ if (e->images[i].target && e->images[i].drawn && e->images[i].shared) {
+ in_place++;
+ }
+ }
+ if (in_place != 0 && !finished) {
+ glFinish(); /* the display's driver reads the surface as soon as it is told of it */
+ }
+ waited = now_us();
for (unsigned i = 0; i < e->image_count; i++) {
if (e->images[i].target && e->images[i].drawn) {
+ copied += !e->images[i].shared;
target_to_surface(e, &e->images[i]);
if (shown_texture != 0) {
show_texture(e, &e->images[i]);
}
- copied++;
}
}
if (!e->in_frame) {
@@ -584,7 +640,10 @@ frame_end(struct eagl *e)
e->all_calls += e->calls;
e->all_draws += e->draws;
e->all_triangles += e->triangles;
- e->readback_us += after - before;
+ e->wait_us += waited - before;
+ e->readback_us += after - waited;
+ e->attach_us += e->attaching;
+ e->flush_us += e->flushing;
e->frame_us += after - e->began;
if (e->calls > e->most_calls) {
e->most_calls = e->calls;
@@ -594,8 +653,10 @@ frame_end(struct eagl *e)
}
if (e->frames <= 40 || e->frames % 20 == 0) {
say("redwood-gl: frame %u: %u GL calls, %u draws, %u triangles, %u clears; %u textures refilled from IOSurfaces "
- "(%llu KB); %u picture%s copied out; %llu ms\n", e->frames, e->calls, e->draws, e->triangles, e->clears,
- e->uploads, e->upload_bytes >> 10, copied, copied == 1 ? "" : "s", (after - e->began) / 1000);
+ "(%llu KB); %u picture%s copied out, %u drawn in place; %llu us (%llu handing on, %llu waiting, %llu copying "
+ "out), %llu before it for the IOSurfaces\n", e->frames, e->calls, e->draws, e->triangles, e->clears, e->uploads,
+ e->upload_bytes >> 10, copied, copied == 1 ? "" : "s", in_place, after - e->began, e->flushing, waited - before,
+ after - waited, e->attaching);
}
if (e->frames == 30 || e->frames % 200 == 0) {
summary(e);
@@ -603,8 +664,9 @@ frame_end(struct eagl *e)
if (e->frames == 600 && e->errors == 0) {
check_errors = 0;
}
+ e->last_draws = e->draws;
e->calls = e->draws = e->triangles = e->uploads = e->clears = 0;
- e->upload_bytes = 0;
+ e->upload_bytes = e->attaching = e->flushing = 0;
}
/* Something is about to be drawn into the framebuffer that is bound. */
@@ -637,7 +699,7 @@ my_glBindTexture(void *context, GLenum target, GLuint texture)
e->bound[e->unit] = texture;
}
if (texture != 0 && e->image_count != 0 && (image = image_of(e, texture)) != NULL && !image->target &&
- IOSurfaceGetSeed(image->surface) != image->seed) {
+ !image->shared && IOSurfaceGetSeed(image->surface) != image->seed) {
surface_to_texture(e, image);
}
EAGL_LEAVE(e, 0x30);
@@ -665,13 +727,35 @@ static void
trace_pixels(struct eagl *e)
{
GLint view[4] = { 0, 0, 0, 0 };
- uint8_t middle[4] = { 0, 0, 0, 0 }, low[4] = { 0, 0, 0, 0 };
+ uint8_t middle[4] = { 0, 0, 0, 0 }, low[4] = { 0, 0, 0, 0 }, *all;
+ unsigned lit = 0, left = ~0u, bottom = ~0u, right = 0, top = 0;
glGetIntegerv(GL_VIEWPORT, view);
glReadPixels(view[0] + view[2] / 2, view[1] + view[3] / 2, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, middle);
glReadPixels(view[0] + view[2] / 2, view[1] + view[3] / 8, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, low);
- eagl_trace(" framebuffer %u (%d x %d): middle %u %u %u %u, an eighth up %u %u %u %u (red green blue alpha)%s\n",
- e->framebuffer, view[2], view[3], middle[0], middle[1], middle[2], middle[3], low[0], low[1], low[2], low[3],
+ /* ... and how much of the picture is anything but black, and where */
+ if (view[2] > 0 && view[3] > 0 && (all = malloc((size_t)view[2] * (size_t)view[3] * 4)) != NULL) {
+ glPixelStorei(GL_PACK_ALIGNMENT, 1);
+ glReadPixels(view[0], view[1], view[2], view[3], GL_RGBA, GL_UNSIGNED_BYTE, all);
+ glPixelStorei(GL_PACK_ALIGNMENT, 4);
+ for (unsigned y = 0; y < (unsigned)view[3]; y++) {
+ const uint8_t *row = all + (size_t)y * (size_t)view[2] * 4;
+
+ for (unsigned x = 0; x < (unsigned)view[2]; x++) {
+ if (row[4 * x] | row[4 * x + 1] | row[4 * x + 2]) {
+ lit++;
+ left = x < left ? x : left;
+ right = x > right ? x : right;
+ bottom = y < bottom ? y : bottom;
+ top = y > top ? y : top;
+ }
+ }
+ }
+ free(all);
+ }
+ eagl_trace(" framebuffer %u (%d x %d): middle %u %u %u %u, an eighth up %u %u %u %u (red green blue alpha); %u pixels "
+ "not black, within %d,%d to %d,%d%s\n", e->framebuffer, view[2], view[3], middle[0], middle[1], middle[2], middle[3],
+ low[0], low[1], low[2], low[3], lit, lit ? (int)left : 0, lit ? (int)bottom : 0, (int)right, (int)top,
glGetError() ? " (not readable)" : "");
}
@@ -913,7 +997,7 @@ my_glFinish(void *context)
eagl_trace("glFinish()\n");
}
glFinish();
- frame_end(e);
+ frame_end(e, true);
EAGL_LEAVE(e, 0x2d0);
}
@@ -921,13 +1005,16 @@ static void
my_glFlush(void *context)
{
struct eagl *e = context;
+ uint64_t began;
EAGL_ENTER(e, 0x2d8);
if (eagl_tracing) {
eagl_trace("glFlush()\n");
}
+ began = now_us();
glFlush();
- frame_end(e);
+ e->flushing += now_us() - began;
+ frame_end(e, false);
EAGL_LEAVE(e, 0x2d8);
}
@@ -941,7 +1028,7 @@ my_glFinishObjectAPPLE(void *context, GLenum object, GLint name)
(void)name;
EAGL_ENTER(e, 0xeb8);
glFinish();
- frame_end(e);
+ frame_end(e, true);
EAGL_LEAVE(e, 0xeb8);
}
@@ -1361,6 +1448,7 @@ my_texImageIOSurface(id self, SEL selector, IOSurfaceRef surface, unsigned long
struct eagl *e = context_of(self);
struct image *image;
GLint texture = 0;
+ uint64_t began;
(void)selector;
if (e == NULL || surface == NULL) {
@@ -1388,7 +1476,11 @@ my_texImageIOSurface(id self, SEL selector, IOSurfaceRef surface, unsigned long
IOSurfaceGetWidth(surface), IOSurfaceGetHeight(surface), IOSurfaceGetPlaneCount(surface), texture, width,
height, plane, invert ? ", inverted" : "");
}
- surface_to_texture(e, image);
+ began = now_us();
+ if (!surface_shared(e, image)) {
+ surface_to_texture(e, image);
+ }
+ e->attaching += now_us() - began;
eagl_leave(e, 0x970);
return YES;
}
@@ -1402,6 +1494,7 @@ my_attachImage(id self, SEL selector, unsigned long target, IOSurfaceRef surface
struct image *image;
GLint texture = 0;
uint32_t pixels;
+ uint64_t began;
(void)selector;
if (e == NULL || surface == NULL) {
@@ -1422,19 +1515,23 @@ my_attachImage(id self, SEL selector, unsigned long target, IOSurfaceRef surface
image->invert = inverted;
image->width = (GLsizei)IOSurfaceGetWidth(surface);
image->height = (GLsizei)IOSurfaceGetHeight(surface);
- if (attached++ < 24) {
- say("redwood-gl: -attachImage: texture %d is IOSurface %u ('%.4s' %d x %d, %zu bytes a row)%s\n", texture,
- IOSurfaceGetID(surface), (const char *)&(uint32_t){ __builtin_bswap32(pixels) }, image->width, image->height,
- IOSurfaceGetBytesPerRow(surface), inverted ? ", drawn inverted" : "");
- }
+ image->internal = image->format = GL_BGRA;
+ image->type = GL_UNSIGNED_BYTE;
note_format("IOSurface attached", GL_BGRA, GL_BGRA, GL_UNSIGNED_BYTE, image->width, image->height);
/*
* What is attached is as often read as drawn into (a picture CoreAnimation has made a surface
- * around, to be shown): the texture starts with what the surface holds.
+ * around, to be shown): a texture that is not the surface's memory starts with what the surface holds.
*/
- image->internal = image->format = GL_BGRA;
- image->type = GL_UNSIGNED_BYTE;
- surface_to_texture(e, image);
+ began = now_us();
+ if (!surface_shared(e, image)) {
+ surface_to_texture(e, image);
+ }
+ e->attaching += now_us() - began;
+ if (attached++ < 24) {
+ say("redwood-gl: -attachImage: texture %d is IOSurface %u ('%.4s' %d x %d, %zu bytes a row)%s%s\n", texture,
+ IOSurfaceGetID(surface), (const char *)&(uint32_t){ __builtin_bswap32(pixels) }, image->width, image->height,
+ IOSurfaceGetBytesPerRow(surface), inverted ? ", drawn inverted" : "", image->shared ? ": its memory" : ": a copy");
+ }
eagl_leave(e, 0x970);
return YES;
}
@@ -1492,6 +1589,12 @@ install(void)
trace_last = (unsigned)strtoul(argument + 1, NULL, 10);
}
}
+ if ((argument = strstr(arguments, "gltrace_at=")) != NULL) {
+ trace_at = now_us() + strtoull(argument + 11, NULL, 10) * 1000000;
+ }
+ if ((argument = strstr(arguments, "gltrace_draws=")) != NULL) {
+ trace_draws = (unsigned)strtoul(argument + 14, NULL, 10);
+ }
all_shaders = strstr(arguments, "glshaders=1") != NULL;
if ((argument = strstr(arguments, "glplain=")) != NULL) {
plain_draws = (int)strtoul(argument + 8, NULL, 10);
@@ -1499,6 +1602,22 @@ install(void)
if ((argument = strstr(arguments, "glshow=")) != NULL) {
shown_texture = (GLuint)strtoul(argument + 7, NULL, 10);
}
+ share_surfaces = strstr(arguments, "glcopy=1") == NULL;
+ for (argument = arguments; (argument = strstr(argument, "glenv=")) != NULL; argument += 6) {
+ char setting[128];
+ size_t length = strcspn(argument + 6, " ");
+ char *value;
+
+ if (length < sizeof(setting)) {
+ memcpy(setting, argument + 6, length);
+ setting[length] = 0;
+ if ((value = strchr(setting, '=')) != NULL) {
+ *value = 0;
+ setenv(setting, value + 1, 1);
+ say("redwood-gl: in the environment: %s=%s\n", setting, value + 1);
+ }
+ }
+ }
}
if (class == Nil) {
diff --git a/userland/src/gl_osmesa.c b/userland/src/gl_osmesa.c
index 89c43aa..b7c280c 100644
--- a/userland/src/gl_osmesa.c
+++ b/userland/src/gl_osmesa.c
@@ -9,7 +9,9 @@
* its contexts gets another file like this one.
*/
#include <stddef.h>
+#include <stdint.h>
#include <stdlib.h>
+#include <string.h>
#include "redwood_gl.h"
@@ -23,11 +25,16 @@
#define OSMESA_CONTEXT_MINOR_VERSION 0x37
#define OSMESA_ES_PROFILE 0x38
#define GL_UNSIGNED_BYTE 0x1401
+#define GL_TEXTURE_2D 0x0de1
+#define GL_RENDERER 0x1f01
typedef struct osmesa_context *OSMesaContext;
extern OSMesaContext OSMesaCreateContextAttribs(const int *attributes, OSMesaContext share);
extern unsigned char OSMesaMakeCurrent(OSMesaContext context, void *buffer, unsigned type, int width, int height);
extern void OSMesaDestroyContext(OSMesaContext context);
+extern void (*OSMesaGetProcAddress(const char *name))(void);
+extern const unsigned char *glGetString(unsigned name);
+extern unsigned glGetError(void);
/*
* OSMesa draws its "window" into memory handed to it. Nothing here draws into a window (the
@@ -78,3 +85,31 @@ redwood_gl_library(void)
{
return "Mesa, off-screen (OSMesa)";
}
+
+/*
+ * OES_EGL_image, with the IOSurface's ID as the image (tools/mesa/freedreno-darwin.patch: the
+ * kernel's GPU driver makes a buffer of the surface's memory). The drivers that draw in memory
+ * of their own with this processor are not asked: they refuse every time.
+ */
+int
+redwood_gl_surface_texture(struct redwood_gl *gl, uint32_t surface)
+{
+ static void (*image_target)(unsigned target, void *image);
+ static int asked, software;
+
+ (void)gl;
+ if (!asked) {
+ const char *renderer = (const char *)glGetString(GL_RENDERER);
+
+ asked = 1;
+ image_target = (void (*)(unsigned, void *))OSMesaGetProcAddress("glEGLImageTargetTexture2DOES");
+ software = renderer == NULL || strstr(renderer, "softpipe") != NULL || strstr(renderer, "llvmpipe") != NULL;
+ }
+ if (image_target == NULL || software) {
+ return 0;
+ }
+ while (glGetError() != 0) {
+ }
+ image_target(GL_TEXTURE_2D, (void *)(uintptr_t)surface);
+ return glGetError() == 0;
+}
diff --git a/userland/src/redwood_gl.h b/userland/src/redwood_gl.h
index 211ff23..f3ae625 100644
--- a/userland/src/redwood_gl.h
+++ b/userland/src/redwood_gl.h
@@ -12,4 +12,12 @@ int redwood_gl_make_current(struct redwood_gl *gl); /* ... the calli
void redwood_gl_destroy(struct redwood_gl *gl);
const char *redwood_gl_library(void); /* for the log */
+/*
+ * The texture bound to GL_TEXTURE_2D made of an IOSurface's own memory: what is drawn into the
+ * texture is in the surface, what is in the surface is what the texture shows, and nobody copies.
+ * For a library whose driver can do that (a GPU's) and a surface it can do it with. 0: not
+ * done; the texture is to be filled and read back with OpenGL ES calls.
+ */
+int redwood_gl_surface_texture(struct redwood_gl *gl, uint32_t surface);
+
#endif