1Android 2======= 3 4Mesa hardware drivers can be built for Android one of two ways: built 5into the Android OS using the Android.mk build sytem on older versions 6of Android, or out-of-tree using the Meson build system and the 7Android NDK. 8 9The Android.mk build system has proven to be hard to maintain, as one 10needs a built Android tree to build against, and it has never been 11tested in CI. The meson build system flow is frequently used by 12Chrome OS developers for building and testing Android drivers. 13 14Building using the Android NDK 15------------------------------ 16 17Download and install the NDK using whatever method you normally would. 18Then, create your meson cross file to use it, something like this 19``~/.local/share/meson/cross/android-aarch64`` file:: 20 21 [binaries] 22 ar = 'NDKDIR/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android-ar' 23 c = ['ccache', 'NDKDIR/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android29-clang', '-fuse-ld=lld'] 24 cpp = ['ccache', 'NDKDIR/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android29-clang++', '-fuse-ld=lld', '-fno-exceptions', '-fno-unwind-tables', '-fno-asynchronous-unwind-tables', '-static-libstdc++'] 25 strip = 'NDKDIR/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android-strip' 26 # Android doesn't come with a pkg-config, but we need one for meson to be happy not 27 # finding all the optional deps it looks for. Use system pkg-config pointing at a 28 # directory we get to populate with any .pc files we want to add for Android 29 pkgconfig = ['env', 'PKG_CONFIG_LIBDIR=NDKDIR/pkgconfig', '/usr/bin/pkg-config'] 30 31 [host_machine] 32 system = 'linux' 33 cpu_family = 'arm' 34 cpu = 'armv8' 35 endian = 'little' 36 37Now, use that cross file for your Android build directory (as in this 38one cross-compiling the turnip driver for a stock Pixel phone) 39 40.. code-block:: console 41 42 meson build-android-aarch64 \ 43 --cross-file android-aarch64 \ 44 -Dplatforms=android \ 45 -Dplatform-sdk-version=26 \ 46 -Dandroid-stub=true \ 47 -Dgallium-drivers= \ 48 -Dvulkan-drivers=freedreno \ 49 -Dfreedreno-kgsl=true 50 ninja -C build-android-aarch64 51 52Replacing Android drivers on stock Android 53------------------------------------------ 54 55The vendor partition with the drivers is normally mounted from a 56read-only disk image on ``/vendor``. To be able to replace them for 57driver development, we need to unlock the device and remount 58``/vendor`` read/write. 59 60.. code-block:: console 61 62 adb disable-verity 63 adb reboot 64 adb remount -R 65 66Now you can replace drivers as in: 67 68.. code-block:: console 69 70 adb push build-android-aarch64/src/freedreno/vulkan/libvulkan_freedreno.so /vendor/lib64/hw/vulkan.sdm710.so 71 72Note this command doesn't quite work because libvulkan wants the 73SONAME to match. For now, in turnip we have been using a hack to the 74meson.build to change the SONAME. 75 76Replacing Android drivers on Chrome OS 77-------------------------------------- 78 79Chrome OS's ARC++ is an Android container with hardware drivers inside 80of it. The vendor partition with the drivers is normally mounted from 81a read-only squashfs image on disk. For doing rapid driver 82development, you don't want to regenerate that image. So, we'll take 83the existing squashfs image, copy it out on the host, and then use a 84bind mount instead of a loopback mount so we can update our drivers 85using scp from outside the container. 86 87On your device, you'll want to make ``/`` read-write. ssh in as root 88and run: 89 90.. code-block:: console 91 92 crossystem dev_boot_signed_only=0 93 /usr/share/vboot/bin/make_dev_ssd.sh --remove_rootfs_verification --partitions 4 94 reboot 95 96Then, we'll switch Android from using an image for ``/vendor`` to using a 97bind-mount from a directory we control. 98 99.. code-block:: console 100 101 cd /opt/google/containers/android/ 102 mkdir vendor-ro 103 mount -o loop vendor.raw.img vendor-ro 104 cp -a vendor-ro vendor-rw 105 emacs config.json 106 107In the ``config.json``, you want to find the block for ``/vendor`` and 108change it to:: 109 110 { 111 "destination": "/vendor", 112 "type": "bind", 113 "source": "/opt/google/containers/android/vendor-rw", 114 "options": [ 115 "bind", 116 "rw" 117 ] 118 }, 119 120Now, restart the UI to do a full reload: 121 122.. code-block:: console 123 124 restart ui 125 126At this point, your android container is restarted with your new 127bind-mount ``/vendor``, and if you use ``android-sh`` to shell into it 128then the ``mount`` command should show:: 129 130 /dev/root on /vendor type ext2 (rw,seclabel,relatime) 131 132Now, replacing your DRI driver with a new one built for Android should 133be a matter of: 134 135.. code-block:: console 136 137 scp msm_dri.so $HOST:/opt/google/containers/android/vendor-rw/lib64/dri/ 138 139You can do your build of your DRI driver using ``emerge-$BOARD 140arc-mesa-freedreno`` (for example) if you have a source tree with 141ARC++, but it should also be possible to build using the NDK as 142described above. There are currently rough edges with this, for 143example the build will require that you have your arc-libdrm build 144available to the NDK, assuming you're building anything but the 145freedreno vulkan driver for KGSL. You can mostly put things in place 146with: 147 148.. code-block:: console 149 150 scp $HOST:/opt/google/containers/android/vendor-rw/lib64/libdrm.so \ 151 NDKDIR/sysroot/usr/lib/aarch64-linux-android/lib/ 152 153 ln -s \ 154 /usr/include/xf86drm.h \ 155 /usr/include/libsync.h \ 156 /usr/include/libdrm \ 157 NDKDIR/sysroot/usr/include/ 158 159It seems that new invocations of an application will often reload the 160DRI driver, but depending on the component you're working on you may 161find you need to reload the whole Android container. To do so without 162having to log in to Chrome again every time, you can just kill the 163container and let it restart: 164 165.. code-block:: console 166 167 kill $(cat /run/containers/android-run_oci/container.pid ) 168