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
Mesa's freedreno on the phone's Adreno: SpringBoard drawn by the GPU
vcvkk6f4e96dac450
diff --git a/README.md b/README.mdindex f30a849..c6ba07e 100644--- a/README.md+++ b/README.md@@ -200,9 +200,12 @@ REST=1 userland/build.shОстальные переменные (`JOBS`, `OSLOG`, `INJECT`, `RC_JOBS`) описаны в начале `userland/build.sh`.`GL=1 REST=1 userland/build.sh` — образ, в котором оконный сервер рисует через OpenGL ES, а не программным-растеризатором CoreAnimation: за `EAGLContext` встаёт Mesa (пока с программным же драйвером softpipe — проверка-пути, а не ускорение). Mesa в репозитории нет: `tools/mk-mesa.sh` один раз скачивает и собирает её (нужны meson,-ninja, bison, flex); прослойка — `userland/src/eagl.c`.+растеризатором CoreAnimation: за `EAGLContext` встаёт Mesa (прослойка — `userland/src/eagl.c`). На телефоне с+параметрами ядра `adreno=21 adreno_skip=0x1c00 adreno_pause=0 adreno_keep=1` рисует сам Adreno: драйвер freedreno+из Mesa говорит с ядром (`iokit/Kernel/RedwoodGPU.cpp`) на языке линуксового msm DRM. Без этих параметров и в+QEMU — программный softpipe. После первого запуска с момента включения телефона добавляйте `adreno_zap=0`.+Mesa в репозитории нет: `tools/mk-mesa.sh` один раз скачивает и собирает её (нужны meson, ninja, bison, flex).+`GL=test RC_JOBS=rc.jobs.gltest` — только библиотека и `gl_test`: один треугольник на экран.После пересборки ядра/загрузчика/программ `tools/update-prebuilt.sh` обновляет `prebuilt/`.diff --git a/tools/mesa/freedreno-darwin.patch b/tools/mesa/freedreno-darwin.patchnew file mode 100644index 0000000..ab0b73c--- /dev/null+++ b/tools/mesa/freedreno-darwin.patch@@ -0,0 +1,194 @@+--- a/meson.build++++ b/meson.build+@@ -1716,7 +1716,15 @@+ # dependencies (such as vk-runtime) linking to it+ dep_libdrm = null_dep+ endif+-if dep_libdrm.found()++if host_machine.system() == 'darwin' and with_gallium_freedreno++ # No DRM on this system: freedreno's requests go to the kernel's driver through a stand-in++ # for libdrm (src/redwood-drm, put there by Darwin on redwood's tools/mk-mesa.sh; its one++ # source file is built with freedreno's drm layer, src/freedreno/drm).++ dep_libdrm = declare_dependency(++ include_directories : include_directories('src/redwood-drm'),++ link_args : ['-framework', 'IOKit', '-framework', 'CoreFoundation'],++ )++elif dep_libdrm.found()+ 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 @@+ fd_bo_fini_fences(bo);++ if (bo->map)++#ifdef __APPLE__++ redwood_drm_unmap(bo->dev->fd, bo->handle, bo->map);++#else+ os_munmap(bo->map, bo->size);++#endif++ if (handle) {+ simple_mtx_lock(&table_lock);+@@ -618,8 +622,13 @@+ if (ret) {+ return NULL;+ }++#ifdef __APPLE__++ /* no mmap() on a device here: the kernel's driver maps the buffer (src/redwood-drm) */++ return redwood_drm_map(bo->dev->fd, bo->handle, bo->size);++#else+ return os_mmap(0, bo->size, PROT_READ | PROT_WRITE, MAP_SHARED,+ bo->dev->fd, offset);++#endif+ }++ /* For internal use only, does not check FD_BO_NOMAP: */+--- a/src/freedreno/ir3/ir3_disk_cache.c++++ b/src/freedreno/ir3/ir3_disk_cache.c+@@ -33,6 +33,10 @@+ if (ir3_shader_debug & IR3_DBG_NOCACHE)+ return;+++#ifdef __APPLE__++ /* no build id in a Mach-O library to tell one build's shaders from another's: no cache */++ return;++#else+ const char *renderer = fd_dev_name(compiler->dev_id);+ const struct build_id_note *note =+ build_id_find_nhdr_for_addr(ir3_disk_cache_init);+@@ -46,6 +50,7 @@++ uint64_t driver_flags = ir3_shader_debug_hash_key();+ compiler->disk_cache = disk_cache_create(renderer, timestamp, driver_flags);++#endif+ }++ void+--- a/src/gallium/drivers/freedreno/freedreno_screen.c++++ b/src/gallium/drivers/freedreno/freedreno_screen.c+@@ -25,7 +25,9 @@+ #include <stdio.h>+ #include <stdlib.h>+ #include "drm-uapi/drm_fourcc.h"++#ifndef __APPLE__+ #include <sys/sysinfo.h>++#endif++ #include "freedreno_fence.h"+ #include "freedreno_perfetto.h"+@@ -1004,9 +1006,15 @@+ screen->driconf.dual_color_blend_by_location =+ driQueryOptionb(config->options, "dual_color_blend_by_location");+++#ifdef __APPLE__++ uint64_t memory = 0;++ os_get_total_physical_memory(&memory);++ screen->ram_size = memory > UINT32_MAX ? UINT32_MAX : memory;++#else+ struct sysinfo si;+ sysinfo(&si);+ screen->ram_size = si.totalram;++#endif++ DBG("Pipe Info:");+ DBG(" GPU-id: %s", fd_dev_name(screen->dev_id));+--- a/src/gallium/targets/osmesa/osmesa_target.c++++ b/src/gallium/targets/osmesa/osmesa_target.c+@@ -26,6 +26,39 @@++ #include "sw/null/null_sw_winsys.h"+++#ifdef GALLIUM_FREEDRENO++#include "freedreno/drm/freedreno_drm_public.h"++#include "util/driconf.h"++#include "util/xmlconfig.h"++#include <xf86drm.h>++++static const driOptionDescription freedreno_options[] = {++#include "freedreno/driinfo_freedreno.h"++};++++/* The GPU's driver, where the kernel has a GPU to drive (Darwin on redwood); NULL: none. */++static struct pipe_screen *++osmesa_create_gpu_screen(void)++{++ static driOptionCache info, cache;++ static struct pipe_screen_config config;++ struct pipe_screen *screen;++ int fd;++++ if (debug_get_bool_option("OSMESA_SOFTWARE", false))++ return NULL;++ fd = drmOpenWithType("msm", NULL, DRM_NODE_RENDER);++ if (fd < 0)++ return NULL;++ driParseOptionInfo(&info, freedreno_options, ARRAY_SIZE(freedreno_options));++ driParseConfigFiles(&cache, &info, 0, "msm", NULL, NULL, NULL, 0, NULL, 0);++ config.options = &cache;++ config.options_info = &info;++ screen = fd_drm_screen_create_renderonly(fd, NULL, &config);++ return screen ? debug_screen_wrap(screen) : NULL;++}++#endif++++ struct pipe_screen *+ osmesa_create_screen(void);+@@ -37,6 +70,12 @@+ struct sw_winsys *winsys;+ struct pipe_screen *screen;+++#ifdef GALLIUM_FREEDRENO++ screen = osmesa_create_gpu_screen();++ if (screen)++ return screen;++#endif+++ /* 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.cnew file mode 100644index 0000000..b231b1c--- /dev/null+++ b/tools/mesa/redwood-drm/redwood_drm.c@@ -0,0 +1,198 @@+/*+ * The libdrm of xf86drm.h (see there): Linux's DRM requests to the kernel's GPU driver as calls+ * of its IOKit user client. A request's number (the low byte of the ioctl code) is the call's+ * selector, its structure goes in and comes back as it is; a buffer's memory is the client+ * memory of the type that is the buffer's handle.+ *+ * freedreno knows its device by a file descriptor. It gets one that means nothing (/dev/null):+ * there is one GPU and one connection to it in a process.+ */+#include <errno.h>+#include <stdlib.h>+#include <string.h>+#include <unistd.h>++#include <IOKit/IOKitLib.h>++#include "xf86drm.h"++#define REDWOOD_GPU_SERVICE "RedwoodGPU"++static io_connect_t connection;+static int connection_fd = -1;++int+drmOpenWithType(const char *name, const char *busid, int type)+{+ io_service_t service;++ (void)busid;+ (void)type;+ if (strcmp(name, "msm") != 0) {+ return -1;+ }+ if (connection_fd >= 0) {+ return dup(connection_fd);+ }+ service = IOServiceGetMatchingService(0, IOServiceMatching(REDWOOD_GPU_SERVICE));+ if (service == 0) {+ return -1;+ }+ if (IOServiceOpen(service, mach_task_self(), 0, &connection) != 0) {+ IOObjectRelease(service);+ return -1;+ }+ IOObjectRelease(service);+ connection_fd = open("/dev/null", O_RDWR);+ return dup(connection_fd);+}++static int+call(unsigned number, void *data, size_t size)+{+ size_t returned = size;+ kern_return_t result = IOConnectCallStructMethod(connection, number, data, size, data, &returned);++ switch (result) {+ case 0:+ return 0;+ case kIOReturnNoMemory:+ return ENOMEM;+ case kIOReturnBadArgument:+ return EINVAL;+ case kIOReturnTimeout:+ return ETIMEDOUT;+ case kIOReturnBusy:+ return EBUSY;+ case kIOReturnNotFound:+ return ENOENT;+ case kIOReturnUnsupported:+ return ENOSYS;+ default:+ return EIO;+ }+}++int+drmIoctl(int fd, unsigned long request, void *arg)+{+ int error = call((unsigned)(request & 0xff), arg, IOCPARM_LEN(request));++ (void)fd;+ if (error != 0) {+ errno = error;+ return -1;+ }+ return 0;+}++int+drmCommandWrite(int fd, unsigned long index, const void *data, unsigned long size)+{+ (void)fd;+ return -call((unsigned)(DRM_COMMAND_BASE + index), (void *)(uintptr_t)data, size);+}++int+drmCommandWriteRead(int fd, unsigned long index, void *data, unsigned long size)+{+ (void)fd;+ return -call((unsigned)(DRM_COMMAND_BASE + index), data, size);+}++/*+ * The driver calls itself what Linux's is called, and is of the version whose interface it has:+ * 1.7, buffers at addresses the kernel chooses and tells ("softpin"), nothing newer.+ */+drmVersionPtr+drmGetVersion(int fd)+{+ drmVersionPtr version = calloc(1, sizeof(*version));++ (void)fd;+ if (version != NULL) {+ version->version_major = 1;+ version->version_minor = 7;+ version->name = strdup("msm");+ version->name_len = 3;+ version->date = strdup("");+ version->desc = strdup("Adreno on Darwin");+ version->desc_len = (int)strlen(version->desc);+ }+ return version;+}++void+drmFreeVersion(drmVersionPtr version)+{+ if (version != NULL) {+ free(version->name);+ free(version->date);+ free(version->desc);+ free(version);+ }+}++int+drmGetCap(int fd, uint64_t capability, uint64_t *value)+{+ (void)fd;+ (void)capability;+ *value = 0;+ return 0;+}++void *+redwood_drm_map(int fd, uint32_t handle, uint64_t size)+{+ mach_vm_address_t address = 0;+ mach_vm_size_t mapped = 0;++ (void)fd;+ (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.+ */+ if (IOConnectMapMemory64(connection, handle, mach_task_self(), &address, &mapped, kIOMapAnywhere) != 0) {+ return (void *)-1; /* mmap()'s MAP_FAILED */+ }+ return (void *)(uintptr_t)address;+}++void+redwood_drm_unmap(int fd, uint32_t handle, void *address)+{+ (void)fd;+ IOConnectUnmapMemory64(connection, handle, mach_task_self(), (mach_vm_address_t)(uintptr_t)address);+}++static int+none(void)+{+ errno = ENOSYS;+ return -1;+}++/* Buffers and fences as file descriptors, for handing them to other programs and drivers: none of that here. */+#define NONE(name, ...) int name(__VA_ARGS__) { return none(); }+NONE(drmPrimeHandleToFD, __attribute__((unused)) int fd, __attribute__((unused)) uint32_t handle,+ __attribute__((unused)) uint32_t flags, __attribute__((unused)) int *prime_fd)+NONE(drmPrimeFDToHandle, __attribute__((unused)) int fd, __attribute__((unused)) int prime_fd,+ __attribute__((unused)) uint32_t *handle)+NONE(drmSyncobjCreate, __attribute__((unused)) int fd, __attribute__((unused)) uint32_t flags,+ __attribute__((unused)) uint32_t *handle)+NONE(drmSyncobjDestroy, __attribute__((unused)) int fd, __attribute__((unused)) uint32_t handle)+NONE(drmSyncobjReset, __attribute__((unused)) int fd, __attribute__((unused)) const uint32_t *handles,+ __attribute__((unused)) uint32_t count)+NONE(drmSyncobjFDToHandle, __attribute__((unused)) int fd, __attribute__((unused)) int obj_fd,+ __attribute__((unused)) uint32_t *handle)+NONE(drmSyncobjHandleToFD, __attribute__((unused)) int fd, __attribute__((unused)) uint32_t handle,+ __attribute__((unused)) int *obj_fd)+NONE(drmSyncobjImportSyncFile, __attribute__((unused)) int fd, __attribute__((unused)) uint32_t handle,+ __attribute__((unused)) int sync_file_fd)+NONE(drmSyncobjExportSyncFile, __attribute__((unused)) int fd, __attribute__((unused)) uint32_t handle,+ __attribute__((unused)) int *sync_file_fd)diff --git a/tools/mesa/redwood-drm/xf86drm.h b/tools/mesa/redwood-drm/xf86drm.hnew file mode 100644index 0000000..126597d--- /dev/null+++ b/tools/mesa/redwood-drm/xf86drm.h@@ -0,0 +1,56 @@+/*+ * What Mesa's freedreno takes from libdrm's xf86drm.h, for a system without Linux's DRM: the+ * declarations here, the functions in redwood_drm.c, which carry the requests to the kernel's+ * GPU driver (the service RedwoodGPU; iokit/Kernel/RedwoodGPU.cpp) as calls of its user client.+ * tools/mk-mesa.sh puts both into Mesa's tree, where they are the "libdrm" freedreno is built+ * against. The request numbers and their structures are Linux's (Mesa's include/drm-uapi).+ */+#ifndef REDWOOD_XF86DRM_H+#define REDWOOD_XF86DRM_H++#include <fcntl.h>+#include <stddef.h>+#include <stdint.h>+#include <sys/types.h>++#include "drm-uapi/drm.h"++#define DRM_NODE_PRIMARY 0+#define DRM_NODE_RENDER 2+#define DRM_CLOEXEC O_CLOEXEC+#define DRM_RDWR O_RDWR++typedef struct _drmVersion {+ int version_major, version_minor, version_patchlevel;+ int name_len;+ char *name;+ int date_len;+ char *date;+ int desc_len;+ char *desc;+} drmVersion, *drmVersionPtr;++int drmOpenWithType(const char *name, const char *busid, int type);+int drmIoctl(int fd, unsigned long request, void *arg);+int drmCommandWrite(int fd, unsigned long index, const void *data, unsigned long size);+int drmCommandWriteRead(int fd, unsigned long index, void *data, unsigned long size);+drmVersionPtr drmGetVersion(int fd);+void drmFreeVersion(drmVersionPtr version);+int drmGetCap(int fd, uint64_t capability, uint64_t *value);++/* buffers and fences as file descriptors: there are none here */+int drmPrimeHandleToFD(int fd, uint32_t handle, uint32_t flags, int *prime_fd);+int drmPrimeFDToHandle(int fd, int prime_fd, uint32_t *handle);+int drmSyncobjCreate(int fd, uint32_t flags, uint32_t *handle);+int drmSyncobjDestroy(int fd, uint32_t handle);+int drmSyncobjReset(int fd, const uint32_t *handles, uint32_t count);+int drmSyncobjFDToHandle(int fd, int obj_fd, uint32_t *handle);+int drmSyncobjHandleToFD(int fd, uint32_t handle, int *obj_fd);+int drmSyncobjImportSyncFile(int fd, uint32_t handle, int sync_file_fd);+int drmSyncobjExportSyncFile(int fd, uint32_t handle, int *sync_file_fd);++/* Not libdrm's: a buffer's memory in this process, in place of mmap() on the device (freedreno_bo.c is patched to call these). */+void *redwood_drm_map(int fd, uint32_t handle, uint64_t size);+void redwood_drm_unmap(int fd, uint32_t handle, void *address);++#endifdiff --git a/tools/mk-mesa.sh b/tools/mk-mesa.shindex 69e2694..5a8fe3d 100755--- a/tools/mk-mesa.sh+++ b/tools/mk-mesa.sh@@ -4,12 +4,17 @@# built with the cross compiler of the kernel (xnu-deps/host/bin/xnu-clang, see xnu-deps/setup.sh)# against the iOS SDK that userland/build.sh uses; nothing of it is in the repository.#-# DRIVERS="softpipe" the gallium drivers to build (softpipe: a rasterizer in plain C; it needs-# no LLVM and nothing of the machine but its processor)+# DRIVERS="softpipe,freedreno"+# the gallium drivers to build. softpipe: a rasterizer in plain C; it needs no+# LLVM and nothing of the machine but its processor. freedreno: the Adreno's;+# it talks to the kernel's GPU driver (the service RedwoodGPU) through+# tools/mesa/redwood-drm, which stands in for libdrm (tools/mesa/freedreno-darwin.patch+# makes Mesa build it where there is no DRM). The library takes the GPU when the+# kernel has one and the environment does not say OSMESA_SOFTWARE=1.## What comes out, in xnu-deps/build/mesa/out:# libOSMesa.8.dylib everything in one library (install name /redwood/lib/libOSMesa.8.dylib): the-# OpenGL (ES) entry points gl*, the compiler of the shading language, the driver,+# OpenGL (ES) entry points gl*, the compiler of the shading language, the drivers,# and the off-screen interface that hands out contexts# exported-gl.txt the gl* functions it exports, and# gl.xml their prototypes (the Khronos registry as Mesa has it): for userland/mkeagl.py@@ -32,6 +37,9 @@ if [ ! -d mesa-$VERSION ]; thentar xJf mesa-$VERSION.tar.xzfor p in "$ROOT"/tools/mesa/*.patch; do patch -s -d mesa-$VERSION -p1 < "$p"; donefi+# ours, kept in the repository: always the current ones+rm -rf mesa-$VERSION/src/redwood-drm+cp -r "$ROOT/tools/mesa/redwood-drm" mesa-$VERSION/src/redwood-drmif [ ! -x venv/bin/python3 ]; thenpython3 -m venv venvvenv/bin/pip -q install mako pyyaml packaging@@ -59,18 +67,20 @@ endian = 'little'[properties]needs_exe_wrapper = trueINI-[ -f build/build.ninja ] || meson setup build mesa-$VERSION --cross-file cross.ini --prefix=/redwood --libdir=lib \- --buildtype=release -Db_ndebug=true \- -Dplatforms= -Dgallium-drivers="${DRIVERS:-softpipe}" -Dvulkan-drivers= -Dglx=disabled -Degl=disabled -Dgbm=disabled \+DRIVERS=${DRIVERS:-softpipe,freedreno}+BUILD=build-${DRIVERS//,/-}+[ -f $BUILD/build.ninja ] || meson setup $BUILD mesa-$VERSION --cross-file cross.ini --wrap-mode=nofallback \+ --prefix=/redwood --libdir=lib --buildtype=release -Db_ndebug=true \+ -Dplatforms= -Dgallium-drivers="$DRIVERS" -Dfreedreno-kmds=msm -Dvulkan-drivers= -Dglx=disabled -Degl=disabled -Dgbm=disabled \-Dosmesa=true -Dllvm=disabled -Dshared-glapi=enabled -Dgles1=disabled -Dgles2=enabled -Dopengl=true \-Dzstd=disabled -Dxmlconfig=disabled -Dexpat=disabled -Dshader-cache=disabled -Dvalgrind=disabled \-Dlibunwind=disabled -Dglvnd=disabled -Dvideo-codecs= -Dtools= -Dgallium-vdpau=disabled -Dgallium-va=disabled \-Dgallium-xa=disabled -Dlmsensors=disabled -Dbuild-tests=false > meson-setup.log-ninja -C build > ninja.log+ninja -C $BUILD > ninja.log-llvm-strip -x -o out/libOSMesa.8.dylib build/src/gallium/targets/osmesa/libOSMesa.8.dylib+llvm-strip -x -o out/libOSMesa.8.dylib $BUILD/src/gallium/targets/osmesa/libOSMesa.8.dylibinstall_name_tool -id /redwood/lib/libOSMesa.8.dylib out/libOSMesa.8.dylibnm -gU out/libOSMesa.8.dylib | awk '$3 ~ /^_gl/ { print substr($3, 2) }' > out/exported-gl.txtcp mesa-$VERSION/src/mapi/glapi/registry/gl.xml out/gl.xml-echo "mk-mesa.sh: Mesa $VERSION (${DRIVERS:-softpipe}): $B/out/libOSMesa.8.dylib, $(stat -c %s out/libOSMesa.8.dylib) bytes," \+echo "mk-mesa.sh: Mesa $VERSION ($DRIVERS): $B/out/libOSMesa.8.dylib, $(stat -c %s out/libOSMesa.8.dylib) bytes," \"$(wc -l < out/exported-gl.txt) gl* functions"diff --git a/userland/build.sh b/userland/build.shindex 416a8f9..5a41bfd 100755--- a/userland/build.sh+++ b/userland/build.sh@@ -19,8 +19,12 @@# where nothing else can touch the screen yet)# GL=1 with an OpenGL ES implementation for the window server: Mesa# (tools/mk-mesa.sh fetches and builds it, once) behind src/eagl.c, as-# lib/libOSMesa.8.dylib (13 MB) and lib/libredwood-gl.dylib. CoreAnimation-# then draws with its OpenGL ES renderer instead of its software one.+# lib/libOSMesa.8.dylib (15 MB) and lib/libredwood-gl.dylib. CoreAnimation+# then draws with its OpenGL ES renderer instead of its software one --+# by the phone's GPU where the kernel keeps it on (boot-arg adreno_keep=1),+# by Mesa's software driver elsewhere.+# GL=test: only the library and bin/gl_test (a triangle, src/gl_test.c;+# RC_JOBS=rc.jobs.gltest runs it); the window server stays as it is.# system.packfs the system image, the root filesystem (second RAM disk, /dev/md1): the part of# iOS 16.5 chosen by ../ios165/select-system.py at its real paths, the dyld# shared cache, and src/stage0.c as /sbin/launchd (the real one is@@ -105,6 +109,7 @@ if [ -d $IOS/rootfs/usr/lib ]; then./mkeagl.py src/eagl_slots.txt $M/gl.xml $M/exported-gl.txt src/eagl.c > obj/eagl_gen.h$DYN -dynamiclib -install_name /redwood/lib/libredwood-gl.dylib -Iobj -framework CoreFoundation \-framework IOSurface -lobjc $M/libOSMesa.8.dylib -o obj/libredwood-gl.dylib src/eagl.c src/gl_osmesa.c+ $DYN -framework CoreFoundation -framework IOKit $M/libOSMesa.8.dylib -o obj/gl_test src/gl_test.ccp $M/libOSMesa.8.dylib obj/fifi@@ -112,7 +117,11 @@ if [ -d $IOS/rootfs/usr/lib ]; then[ -f obj/cpu_test ] && cp obj/cpu_test root/bin/ # (a prebuilt/ from before this program has none)mkdir -p root/libcp obj/libredwood.dylib obj/libredwood-sb.dylib root/lib/- [ -n "$GL" ] && cp obj/libredwood-gl.dylib obj/libOSMesa.8.dylib root/lib/+ if [ -n "$GL" ]; then+ cp obj/libOSMesa.8.dylib root/lib/+ cp obj/gl_test root/bin/+ [ "$GL" = test ] || cp obj/libredwood-gl.dylib root/lib/+ fiecho "$INJECT" > root/etc/inject.confcp $IOS/var.skel root/etc/var.skel # the directories init creates in /private/var# time zones: the template's links go in a circle until the time zone daemon has run (/usr/share/zoneinfodiff --git a/userland/rc.jobs.gltest b/userland/rc.jobs.gltestnew file mode 100644index 0000000..b016aac--- /dev/null+++ b/userland/rc.jobs.gltest@@ -0,0 +1,5 @@+# Commands run by the launchd job com.redwood.demo (see com.redwood.demo.plist).+# A triangle by the OpenGL ES library of the image (build.sh GL=test or GL=1), once the lock screen+# is up and has stopped drawing over the panel; see src/gl_test.c.+/redwood/bin/rwtouch wait 14+/redwood/bin/gl_test 8diff --git a/userland/src/eagl.c b/userland/src/eagl.cindex f012eb0..9586663 100644--- a/userland/src/eagl.c+++ b/userland/src/eagl.c@@ -1125,10 +1125,32 @@ static const char fragment_start[] ="#define textureCube texture\n""#define sampler2DRect sampler2D\n""#define texture2DRect(s, c) texture(s, (c) / vec2(textureSize(s, 0)))\n"- "layout(location = 0) out highp vec4 redwood_colour[1];\n"- "#define gl_FragData redwood_colour\n"- "#define gl_FragColor redwood_colour[0]\n"+ "highp vec4 redwood_data[1];\n"+ "#define gl_FragData redwood_data\n"+ "#define gl_FragColor redwood_data[0]\n";+/*+ * What a fragment shader of the first version writes to (gl_FragData), and with the extension a+ * GPU's driver offers and CoreAnimation then uses (EXT_shader_framebuffer_fetch) reads what is+ * there already from (gl_LastFragData), are in 3.00 one variable of the shader's own, declared+ * "out" or "inout". The shader's main() is given another name and called from one that reads+ * that variable before and writes it after.+ */+static const char fragment_plain[] =+ "layout(location = 0) out highp vec4 redwood_colour;\n"+ "#define main redwood_main\n""#line 1\n";+static const char fragment_plain_end[] =+ "\n#undef main\n"+ "void main() { redwood_main(); redwood_colour = redwood_data[0]; }\n";+static const char fragment_fetch[] =+ "layout(location = 0) inout highp vec4 redwood_colour;\n"+ "highp vec4 redwood_last[1];\n"+ "#define gl_LastFragData redwood_last\n"+ "#define main redwood_main\n"+ "#line 1\n";+static const char fragment_fetch_end[] =+ "\n#undef main\n"+ "void main() { redwood_last[0] = redwood_colour; redwood_main(); redwood_colour = redwood_data[0]; }\n";static const char vertex_start[] ="#define attribute in\n""#define varying out\n"@@ -1157,8 +1179,8 @@ static voidmy_glShaderSource(void *context, GLuint shader, GLsizei count, const GLchar *const *strings, const GLint *lengths){struct eagl *e = context;- const char *start = e->fragment[shader % 64] ? fragment_start : vertex_start;- const GLchar *parts[3] = { "#version 300 es\n", start, NULL };+ bool fragment = e->fragment[shader % 64], fetch;+ const GLchar *parts[5] = { "#version 300 es\n", fragment ? fragment_start : vertex_start, "", NULL, "" };size_t size = 1, extensions = 0;char *text, *line, *head;@@ -1166,7 +1188,7 @@ my_glShaderSource(void *context, GLuint shader, GLsizei count, const GLchar *confor (GLsizei i = 0; i < count; i++) {size += lengths != NULL && lengths[i] >= 0 ? (size_t)lengths[i] : strlen(strings[i]);}- if ((text = calloc(1, 2 * size + sizeof(fragment_start) + 64)) == NULL) {+ if ((text = calloc(1, 2 * size + 64)) == NULL) {glShaderSource(shader, count, strings, lengths);return;}@@ -1185,10 +1207,15 @@ my_glShaderSource(void *context, GLuint shader, GLsizei count, const GLchar *conextensions++;}parts[0] = head;- parts[2] = text;+ parts[3] = text;+ if (fragment) {+ fetch = strstr(text, "gl_LastFragData") != NULL;+ parts[2] = fetch ? fragment_fetch : fragment_plain;+ parts[4] = fetch ? fragment_fetch_end : fragment_plain_end;+ }free((void *)e->sources[shader % 64]);e->sources[shader % 64] = text;- glShaderSource(shader, 3, parts, NULL);+ glShaderSource(shader, 5, parts, NULL);EAGL_LEAVE(e, 0x12a0);}diff --git a/userland/src/gl_test.c b/userland/src/gl_test.cnew file mode 100644index 0000000..a8b6c37--- /dev/null+++ b/userland/src/gl_test.c@@ -0,0 +1,227 @@+/*+ * gl_test: one triangle through the OpenGL ES library of the image (lib/libOSMesa.8.dylib: Mesa,+ * with its Adreno driver when the kernel has kept the GPU on -- boot-arg adreno_keep=1 -- and its+ * software one when not), read back and put on the panel. It says which driver drew, how long+ * it took and what a few pixels are, so that the picture need not be seen to be believed.+ *+ * gl_test [SECONDS [WIDTH HEIGHT]] keep the picture on the panel for so long (default 6: the+ * window server draws over it with its next frame)+ */+#include <CoreFoundation/CoreFoundation.h>+#include <IOKit/IOKitLib.h>+#include <mach/mach_time.h>+#include <stdint.h>+#include <stdio.h>+#include <stdlib.h>+#include <string.h>+#include <unistd.h>++/* GL/osmesa.h and GLES2/gl2.h of Mesa, as far as they are used here */+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 const unsigned char *glGetString(unsigned name);+extern unsigned glGetError(void);+extern void glGenTextures(int n, unsigned *names);+extern void glBindTexture(unsigned target, unsigned texture);+extern void glTexImage2D(unsigned target, int level, int internal, int width, int height, int border, unsigned format,+ unsigned type, const void *pixels);+extern void glTexParameteri(unsigned target, unsigned name, int value);+extern void glGenFramebuffers(int n, unsigned *names);+extern void glBindFramebuffer(unsigned target, unsigned framebuffer);+extern void glFramebufferTexture2D(unsigned target, unsigned attachment, unsigned textarget, unsigned texture, int level);+extern unsigned glCheckFramebufferStatus(unsigned target);+extern void glViewport(int x, int y, int width, int height);+extern void glClearColor(float red, float green, float blue, float alpha);+extern void glClear(unsigned mask);+extern unsigned glCreateShader(unsigned type);+extern void glShaderSource(unsigned shader, int count, const char *const *strings, const int *lengths);+extern void glCompileShader(unsigned shader);+extern void glGetShaderiv(unsigned shader, unsigned name, int *value);+extern unsigned glCreateProgram(void);+extern void glAttachShader(unsigned program, unsigned shader);+extern void glBindAttribLocation(unsigned program, unsigned index, const char *name);+extern void glLinkProgram(unsigned program);+extern void glGetProgramiv(unsigned program, unsigned name, int *value);+extern void glUseProgram(unsigned program);+extern void glVertexAttribPointer(unsigned index, int size, unsigned type, unsigned char normalized, int stride,+ const void *pointer);+extern void glEnableVertexAttribArray(unsigned index);+extern void glDrawArrays(unsigned mode, int first, int count);+extern void glFinish(void);+extern void glReadPixels(int x, int y, int width, int height, unsigned format, unsigned type, void *pixels);++#define OSMESA_RGBA 0x1908+#define OSMESA_FORMAT 0x22+#define OSMESA_PROFILE 0x33+#define OSMESA_CONTEXT_MAJOR_VERSION 0x36+#define OSMESA_ES_PROFILE 0x38+#define GL_TRIANGLES 0x0004+#define GL_TEXTURE_2D 0x0de1+#define GL_UNSIGNED_BYTE 0x1401+#define GL_FLOAT 0x1406+#define GL_RGBA 0x1908+#define GL_VENDOR 0x1f00+#define GL_RENDERER 0x1f01+#define GL_VERSION 0x1f02+#define GL_NEAREST 0x2600+#define GL_TEXTURE_MAG_FILTER 0x2800+#define GL_TEXTURE_MIN_FILTER 0x2801+#define GL_COLOR_BUFFER_BIT 0x4000+#define GL_FRAGMENT_SHADER 0x8b30+#define GL_VERTEX_SHADER 0x8b31+#define GL_COMPILE_STATUS 0x8b81+#define GL_LINK_STATUS 0x8b82+#define GL_FRAMEBUFFER_COMPLETE 0x8cd5+#define GL_COLOR_ATTACHMENT0 0x8ce0+#define GL_FRAMEBUFFER 0x8d40++/* the kernel's GPU service takes pixels for the panel (iokit/Kernel/RedwoodGPU.cpp: kRedwoodShow) */+struct show {+ uint64_t pixels;+ uint32_t width, height, row_bytes, pad;+};++static uint64_t+now_ms(void)+{+ static mach_timebase_info_data_t base;++ if (base.denom == 0) {+ mach_timebase_info(&base);+ }+ return mach_absolute_time() * base.numer / base.denom / 1000000;+}++static unsigned+shader(unsigned type, const char *text)+{+ unsigned name = glCreateShader(type);+ int good = 0;++ glShaderSource(name, 1, &text, NULL);+ glCompileShader(name);+ glGetShaderiv(name, GL_COMPILE_STATUS, &good);+ if (!good) {+ printf("gl_test: a shader does not compile\n");+ }+ return name;+}++int+main(int argc, char **argv)+{+ static const int attributes[] = { OSMESA_FORMAT, OSMESA_RGBA, OSMESA_PROFILE, OSMESA_ES_PROFILE,+ OSMESA_CONTEXT_MAJOR_VERSION, 2, 0 };+ static const float corners[] = {+ /* where, and what colour */+ -0.8f, -0.6f, 1, 0, 0,+ 0.8f, -0.6f, 0, 1, 0,+ 0.0f, 0.7f, 0, 0, 1,+ };+ static unsigned char window[16 * 16 * 4];+ int seconds = argc > 1 ? atoi(argv[1]) : 6, width = argc > 3 ? atoi(argv[2]) : 1080;+ int height = argc > 3 ? atoi(argv[3]) : 2400;+ unsigned texture = 0, framebuffer = 0, program, coloured = 0;+ uint8_t *drawn, *shown;+ uint64_t began, drew, read;+ io_service_t service;+ io_connect_t connection = 0;+ OSMesaContext context;+ int good = 0;++ context = OSMesaCreateContextAttribs(attributes, NULL);+ if (context == NULL || !OSMesaMakeCurrent(context, window, GL_UNSIGNED_BYTE, 16, 16)) {+ printf("gl_test: no OpenGL ES context\n");+ return 1;+ }+ printf("gl_test: %s; %s; %s\n", (const char *)glGetString(GL_VENDOR), (const char *)glGetString(GL_RENDERER),+ (const char *)glGetString(GL_VERSION));++ began = now_ms();+ glGenTextures(1, &texture);+ glBindTexture(GL_TEXTURE_2D, texture);+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);+ glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);+ glGenFramebuffers(1, &framebuffer);+ glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);+ glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture, 0);+ if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {+ printf("gl_test: the framebuffer is not complete\n");+ return 1;+ }+ program = glCreateProgram();+ glAttachShader(program, shader(GL_VERTEX_SHADER,+ "attribute vec2 where; attribute vec3 colour; varying vec3 c;\n"+ "void main() { gl_Position = vec4(where, 0.0, 1.0); c = colour; }\n"));+ glAttachShader(program, shader(GL_FRAGMENT_SHADER,+ "precision mediump float; varying vec3 c;\n"+ "void main() { gl_FragColor = vec4(c, 1.0); }\n"));+ glBindAttribLocation(program, 0, "where");+ glBindAttribLocation(program, 1, "colour");+ glLinkProgram(program);+ glGetProgramiv(program, GL_LINK_STATUS, &good);+ if (!good) {+ printf("gl_test: the program does not link\n");+ return 1;+ }+ glUseProgram(program);+ glViewport(0, 0, width, height);+ glClearColor(0.1f, 0.1f, 0.2f, 1.0f);+ glClear(GL_COLOR_BUFFER_BIT);+ glVertexAttribPointer(0, 2, GL_FLOAT, 0, 5 * sizeof(float), corners);+ glVertexAttribPointer(1, 3, GL_FLOAT, 0, 5 * sizeof(float), corners + 2);+ glEnableVertexAttribArray(0);+ glEnableVertexAttribArray(1);+ glDrawArrays(GL_TRIANGLES, 0, 3);+ glFinish();+ drew = now_ms();++ drawn = malloc((size_t)width * (size_t)height * 4);+ shown = malloc((size_t)width * (size_t)height * 4);+ if (drawn == NULL || shown == NULL) {+ return 1;+ }+ memset(drawn, 0x55, (size_t)width * (size_t)height * 4);+ glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, drawn);+ read = now_ms();+ /* the panel's rows go down and its pixels are blue first */+ for (int y = 0; y < height; y++) {+ const uint8_t *from = drawn + (size_t)(height - 1 - y) * (size_t)width * 4;+ uint8_t *to = shown + (size_t)y * (size_t)width * 4;++ for (int x = 0; x < width; x++) {+ to[4 * x] = from[4 * x + 2];+ to[4 * x + 1] = from[4 * x + 1];+ to[4 * x + 2] = from[4 * x];+ to[4 * x + 3] = 255;+ coloured += from[4 * x] > 60 || from[4 * x + 1] > 60 || from[4 * x + 2] > 60;+ }+ }+#define PIXEL(x, y) drawn[((size_t)(y) * (size_t)width + (size_t)(x)) * 4], drawn[((size_t)(y) * (size_t)width + (size_t)(x)) * 4 + 1], \+ drawn[((size_t)(y) * (size_t)width + (size_t)(x)) * 4 + 2], drawn[((size_t)(y) * (size_t)width + (size_t)(x)) * 4 + 3]+ printf("gl_test: %d x %d drawn in %llu ms, read back in %llu ms; error %#x; red green blue alpha: a corner %u %u %u %u, "+ "the middle %u %u %u %u, near the red corner %u %u %u %u, near the green %u %u %u %u, near the blue %u %u %u %u; "+ "%u of %u pixels inside the triangle (%u%%; 26%% by geometry)\n", width, height, drew - began, read - drew,+ glGetError(), PIXEL(2, 2), PIXEL(width / 2, height / 2), PIXEL(width * 15 / 100, height * 22 / 100),+ PIXEL(width * 85 / 100, height * 22 / 100), PIXEL(width / 2, height * 80 / 100), coloured,+ (unsigned)(width * height), (unsigned)((uint64_t)coloured * 100 / (uint64_t)(width * height)));++ service = IOServiceGetMatchingService(0, IOServiceMatching("RedwoodGPU"));+ if (service == 0 || IOServiceOpen(service, mach_task_self(), 0, &connection) != 0) {+ printf("gl_test: no GPU service in this kernel to put the picture on the panel with\n");+ return 0;+ }+ for (int i = 0; i < seconds * 5; i++) {+ struct show show = { (uint64_t)(uintptr_t)shown, (uint32_t)width, (uint32_t)height, (uint32_t)width * 4, 0 };+ kern_return_t result = IOConnectCallStructMethod(connection, 0x7f, &show, sizeof(show), NULL, NULL);++ if (i == 0) {+ printf("gl_test: the picture is on the panel (%#x), for %d seconds\n", result, seconds);+ }+ usleep(200000);+ }+ return 0;+}