Eric Anholt | 0f82c99 | 2020-10-16 15:50:40 -0700 | [diff] [blame] | 1 | Android |
| 2 | ======= |
| 3 | |
| 4 | Mesa hardware drivers can be built for Android one of two ways: built |
| 5 | into the Android OS using the Android.mk build sytem on older versions |
| 6 | of Android, or out-of-tree using the Meson build system and the |
| 7 | Android NDK. |
| 8 | |
| 9 | The Android.mk build system has proven to be hard to maintain, as one |
| 10 | needs a built Android tree to build against, and it has never been |
| 11 | tested in CI. The meson build system flow is frequently used by |
| 12 | Chrome OS developers for building and testing Android drivers. |
| 13 | |
| 14 | Building using the Android NDK |
| 15 | ------------------------------ |
| 16 | |
| 17 | Download and install the NDK using whatever method you normally would. |
| 18 | Then, 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 | |
| 37 | Now, use that cross file for your Android build directory (as in this |
| 38 | one 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 | |
| 52 | Replacing Android drivers on stock Android |
| 53 | ------------------------------------------ |
| 54 | |
| 55 | The vendor partition with the drivers is normally mounted from a |
| 56 | read-only disk image on ``/vendor``. To be able to replace them for |
| 57 | driver 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 | |
| 66 | Now 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 | |
| 72 | Note this command doesn't quite work because libvulkan wants the |
| 73 | SONAME to match. For now, in turnip we have been using a hack to the |
| 74 | meson.build to change the SONAME. |
| 75 | |
| 76 | Replacing Android drivers on Chrome OS |
| 77 | -------------------------------------- |
| 78 | |
| 79 | Chrome OS's ARC++ is an Android container with hardware drivers inside |
| 80 | of it. The vendor partition with the drivers is normally mounted from |
| 81 | a read-only squashfs image on disk. For doing rapid driver |
| 82 | development, you don't want to regenerate that image. So, we'll take |
| 83 | the existing squashfs image, copy it out on the host, and then use a |
| 84 | bind mount instead of a loopback mount so we can update our drivers |
| 85 | using scp from outside the container. |
| 86 | |
| 87 | On your device, you'll want to make ``/`` read-write. ssh in as root |
| 88 | and 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 | |
| 96 | Then, we'll switch Android from using an image for ``/vendor`` to using a |
| 97 | bind-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 | |
| 107 | In the ``config.json``, you want to find the block for ``/vendor`` and |
| 108 | change 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 | |
| 120 | Now, restart the UI to do a full reload: |
| 121 | |
| 122 | .. code-block:: console |
| 123 | |
| 124 | restart ui |
| 125 | |
| 126 | At this point, your android container is restarted with your new |
| 127 | bind-mount ``/vendor``, and if you use ``android-sh`` to shell into it |
| 128 | then the ``mount`` command should show:: |
| 129 | |
| 130 | /dev/root on /vendor type ext2 (rw,seclabel,relatime) |
| 131 | |
| 132 | Now, replacing your DRI driver with a new one built for Android should |
| 133 | be a matter of: |
| 134 | |
| 135 | .. code-block:: console |
| 136 | |
| 137 | scp msm_dri.so $HOST:/opt/google/containers/android/vendor-rw/lib64/dri/ |
| 138 | |
| 139 | You can do your build of your DRI driver using ``emerge-$BOARD |
| 140 | arc-mesa-freedreno`` (for example) if you have a source tree with |
| 141 | ARC++, but it should also be possible to build using the NDK as |
| 142 | described above. There are currently rough edges with this, for |
| 143 | example the build will require that you have your arc-libdrm build |
| 144 | available to the NDK, assuming you're building anything but the |
| 145 | freedreno vulkan driver for KGSL. You can mostly put things in place |
| 146 | with: |
| 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 | |
| 159 | It seems that new invocations of an application will often reload the |
| 160 | DRI driver, but depending on the component you're working on you may |
| 161 | find you need to reload the whole Android container. To do so without |
| 162 | having to log in to Chrome again every time, you can just kill the |
| 163 | container and let it restart: |
| 164 | |
| 165 | .. code-block:: console |
| 166 | |
| 167 | kill $(cat /run/containers/android-run_oci/container.pid ) |