Debian Codegraph @

docs/android.rst

raw
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 ndk-build build system on older versions
6 of Android, or out-of-tree using the Meson build system and the
7 Android NDK.
8
9 The ndk-build 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 When building llvmpipe or lavapipe for Android the ndk-build workflow
15 is also used, but there are additional steps required to add the driver
16 to the Android OS image.
17
18 Preparing offline compilers
19 ---------------------------
20
21 For cross-compiling the nvk driver, mesa_clc compiler binary needs to be
22 prepared first:
23
24 .. code-block:: sh
25
26 meson setup build-compiler \
27 -Dprefix=/tmp/mesa-compiler \
28 -Dbuildtype=release \
29 -Dstrip=true \
30 -Dplatforms= \
31 -Dgallium-drivers= \
32 -Dvulkan-drivers= \
33 -Dmesa-clc=enabled \
34 -Dinstall-mesa-clc=true
35 meson install -C build-compiler
36 export PATH=/tmp/mesa-compiler/bin:$PATH
37
38 For panvk, ``-Dtools=panfrost -Dinstall-precomp-compiler=true`` is
39 additionally needed.
40
41 Building using the Android NDK
42 ------------------------------
43
44 Download and install the NDK using whatever method you normally would.
45 Then, create your Meson cross file to use it, something like this
46 ``~/.local/share/meson/cross/android-aarch64`` file:
47
48 .. code-block:: ini
49
50 [constants]
51 ndk_path = <absolute path to NDK>
52
53 [binaries]
54 ar = ndk_path / 'toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android-ar'
55 c = ['ccache', ndk_path / 'toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android34-clang']
56 cpp = ['ccache', ndk_path / 'toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android34-clang++', '-fno-exceptions', '-fno-unwind-tables', '-fno-asynchronous-unwind-tables', '--start-no-unused-arguments', '-static-libstdc++', '--end-no-unused-arguments']
57 c_ld = 'lld'
58 cpp_ld = 'lld'
59 strip = ndk_path / 'toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android-strip'
60
61 [host_machine]
62 system = 'android'
63 cpu_family = 'aarch64'
64 cpu = 'armv8'
65 endian = 'little'
66
67 For nvk, below rust toolchain preparation and cross file additions are needed:
68
69 .. code-block:: sh
70
71 rustup target add aarch64-linux-android
72
73 .. code-block:: ini
74
75 [properties]
76 bindgen_clang_arguments = ['-target', 'aarch64-linux-android', '--sysroot', ndk_path / 'toolchains/llvm/prebuilt/linux-x86_64/sysroot']
77
78 [binaries]
79 rust = ['rustc', '--target', 'aarch64-linux-android']
80
81 Now, use that cross file for your Android build directory (as in this
82 one cross-compiling the turnip driver for a stock Pixel phone)
83
84 .. code-block:: sh
85
86 meson setup build-android-aarch64 \
87 --cross-file android-aarch64 \
88 -Dplatforms=android \
89 -Dplatform-sdk-version=34 \
90 -Dandroid-stub=true \
91 -Dandroid-libbacktrace=disabled \
92 -Degl=disabled \
93 -Dgallium-drivers= \
94 -Dvulkan-drivers=freedreno \
95 -Dfreedreno-kmds=kgsl
96 meson compile -C build-android-aarch64
97
98 For drm drivers, ``-Dallow-fallback-for=libdrm`` is needed. Besides,
99 ``-Dallow-fallback-for=libdrm -Dmesa-clc=system`` is needed by nvk, and
100 ``-Dprecomp-compiler=system`` is additionally needed by panvk.
101
102 Replacing Android drivers on stock Android
103 ------------------------------------------
104
105 The vendor partition with the drivers is normally mounted from a
106 read-only disk image on ``/vendor``. To be able to replace them for
107 driver development, we need to unlock the device and remount
108 ``/vendor`` read/write.
109
110 .. code-block:: sh
111
112 adb disable-verity
113 adb reboot
114 adb remount -R
115 adb remount
116
117 Now you can replace drivers as in:
118
119 .. code-block:: sh
120
121 adb push build-android-aarch64/src/freedreno/vulkan/libvulkan_freedreno.so /vendor/lib64/hw/vulkan.sdm710.so
122
123 Note this command doesn't quite work because libvulkan wants the
124 SONAME to match. You can use ``patchelf`` to fix this:
125
126 .. code-block:: sh
127
128 cp build-android-aarch64/src/freedreno/vulkan/libvulkan_freedreno.so /tmp/vulkan.sdm710.so
129 patchelf --set-soname vulkan.sdm710.so /tmp/vulkan.sdm710.so
130 adb push /tmp/vulkan.sdm710.so /vendor/lib64/hw/
131
132 Replacing Android drivers on Chrome OS
133 --------------------------------------
134
135 Chrome OS's ARC++ is an Android container with hardware drivers inside
136 of it. The vendor partition with the drivers is normally mounted from
137 a read-only squashfs image on disk. For doing rapid driver
138 development, you don't want to regenerate that image. So, we'll take
139 the existing squashfs image, copy it out on the host, and then use a
140 bind mount instead of a loopback mount so we can update our drivers
141 using scp from outside the container.
142
143 On your device, you'll want to make ``/`` read-write. ssh in as root
144 and run:
145
146 .. code-block:: sh
147
148 crossystem dev_boot_signed_only=0
149 /usr/share/vboot/bin/make_dev_ssd.sh --remove_rootfs_verification --partitions 4
150 reboot
151
152 Then, we'll switch Android from using an image for ``/vendor`` to using a
153 bind-mount from a directory we control.
154
155 .. code-block:: sh
156
157 cd /opt/google/containers/android/
158 mkdir vendor-ro
159 mount -o loop vendor.raw.img vendor-ro
160 cp -a vendor-ro vendor-rw
161 emacs config.json
162
163 In the ``config.json``, you want to find the block for ``/vendor`` and
164 change it to::
165
166 {
167 "destination": "/vendor",
168 "type": "bind",
169 "source": "/opt/google/containers/android/vendor-rw",
170 "options": [
171 "bind",
172 "rw"
173 ]
174 },
175
176 Now, restart the UI to do a full reload:
177
178 .. code-block:: sh
179
180 restart ui
181
182 At this point, your android container is restarted with your new
183 bind-mount ``/vendor``, and if you use ``android-sh`` to shell into it
184 then the ``mount`` command should show::
185
186 /dev/root on /vendor type ext2 (rw,seclabel,relatime)
187
188 Now, replacing your DRI driver with a new one built for Android should
189 be a matter of:
190
191 .. code-block:: sh
192
193 scp msm_dri.so $HOST:/opt/google/containers/android/vendor-rw/lib64/dri/
194
195 You can do your build of your DRI driver using ``emerge-$BOARD
196 arc-mesa-freedreno`` (for example) if you have a source tree with
197 ARC++, but it should also be possible to build using the NDK as
198 described above. There are currently rough edges with this, for
199 example the build will require that you have your arc-libdrm build
200 available to the NDK, assuming you're building anything but the
201 Freedreno Vulkan driver for KGSL. You can mostly put things in place
202 with:
203
204 .. code-block:: sh
205
206 scp $HOST:/opt/google/containers/android/vendor-rw/lib64/libdrm.so \
207 NDKDIR/sysroot/usr/lib/aarch64-linux-android/lib/
208
209 ln -s \
210 /usr/include/xf86drm.h \
211 /usr/include/libsync.h \
212 /usr/include/libdrm \
213 NDKDIR/sysroot/usr/include/
214
215 It seems that new invocations of an application will often reload the
216 DRI driver, but depending on the component you're working on you may
217 find you need to reload the whole Android container. To do so without
218 having to log in to Chrome again every time, you can just kill the
219 container and let it restart:
220
221 .. code-block:: sh
222
223 kill $(cat /run/containers/android-run_oci/container.pid )
224
225 Adding out-of-tree drivers to Android OS image
226 ----------------------------------------------
227
228 When building your own Android OS images it's possible to add
229 drivers built out of tree directly into the OS image. For
230 running llvmpipe and lavapipe on Android this step is required
231 to ensure Android is able to load the drivers correctly.
232
233 The following steps provide and example for building
234 the android cuttlefish image following the official Android
235 documentation from https://source.android.com/docs/setup
236
237 When building llvmpipe or lavapipe for Android, it is required
238 to do this so that the permissions for accessing the library
239 are set correctly.
240
241 Following the Android documentation, we can run the following
242 commands
243
244 .. code-block:: sh
245
246 repo init -b main -u https://android.googlesource.com/platform/manifest
247 repo sync -c -j8
248
249 source build/envsetup.sh
250 lunch aosp_cf_x86_64_phone-trunk_staging-userdebug
251
252 Be aware that the sync command can take a long time to run as
253 it will download all of the source code. This will set up
254 the ``aosp_cf_x86_64_phone-trunk_staging-userdebug`` build target
255 for Android. Please note that the x86_64 cuttlefish target will require
256 you to build mesa for 32bit and 64bit. Next we need to copy the build
257 driver libraries into the source tree of Android and patch the binary names.
258
259 .. code-block:: sh
260
261 mkdir prebuilts/mesa
262 mkdir prebuilts/mesa/x86_64
263 mkdir prebuilts/mesa/x86
264 cp ${INSTALL_PREFIX_64}/lib/libEGL.so prebuilts/mesa/x86_64/
265 cp ${INSTALL_PREFIX_64}/lib/libgallium_dri.so prebuilts/mesa/x86_64/
266 cp ${INSTALL_PREFIX_64}/lib/libGLESv1_CM.so prebuilts/mesa/x86_64/
267 cp ${INSTALL_PREFIX_64}/lib/libGLESv2.so prebuilts/mesa/x86_64/
268 cp ${INSTALL_PREFIX_64}/lib/libvulkan_lvp.so prebuilts/mesa/x86_64/
269 cp ${INSTALL_PREFIX_32}/lib/libEGL.so prebuilts/mesa/x86
270 cp ${INSTALL_PREFIX_32}/lib/libgallium_dri.so prebuilts/mesa/x86/
271 cp ${INSTALL_PREFIX_32}/lib/libGLESv1_CM.so prebuilts/mesa/x86
272 cp ${INSTALL_PREFIX_32}/lib/libGLESv2.so prebuilts/mesa/x86
273 cp ${INSTALL_PREFIX_32}/lib/libvulkan_lvp.so prebuilts/mesa/x86
274
275 patchelf --set-soname libEGL_lp.so prebuilts/mesa/x86_64/libEGL.so
276 patchelf --set-soname libGLESv1_CM_lp.so prebuilts/mesa/x86_64/libGLESv1_CM.so
277 patchelf --set-soname libGLESv2_lp.so prebuilts/mesa/x86_64/libGLESv2.so
278 patchelf --set-soname vulkan.lvp.so prebuilts/mesa/x86_64/libvulkan_lvp.so
279 patchelf --set-soname libEGL_lp.so prebuilts/mesa/x86/libEGL.so
280 patchelf --set-soname libGLESv1_CM_lp.so prebuilts/mesa/x86/libGLESv1_CM.so
281 patchelf --set-soname libGLESv2_lp.so prebuilts/mesa/x86/libGLESv2.so
282 patchelf --set-soname vulkan.lvp.so prebuilts/mesa/x86/libvulkan_lvp.so
283
284 We then need to create an ``prebuilts/mesa/Android.bp`` build file to include
285 the libraries in the build.
286
287 .. code-block::
288
289 cc_prebuilt_library_shared {
290 name: "libgallium_dri",
291 arch: {
292 x86_64: {
293 srcs: ["x86_64/libgallium_dri.so"],
294 },
295 x86: {
296 srcs: ["x86/libgallium_dri.so"],
297 },
298 },
299 strip: {
300 none: true,
301 },
302 relative_install_path: "egl",
303 shared_libs: ["libc", "libdl", "liblog", "libm"],
304 check_elf_files: false,
305 vendor: true
306 }
307
308 cc_prebuilt_library_shared {
309 name: "libEGL_lp",
310 arch: {
311 x86_64: {
312 srcs: ["x86_64/libEGL.so"],
313 },
314 x86: {
315 srcs: ["x86/libEGL.so"],
316 },
317 },
318 strip: {
319 none: true,
320 },
321 relative_install_path: "egl",
322 shared_libs: ["libc", "libdl", "liblog", "libm", "libcutils", "libdrm", "libhardware", "liblog", "libnativewindow", "libsync"],
323 check_elf_files: false,
324 vendor: true
325 }
326
327 cc_prebuilt_library_shared {
328 name: "libGLESv1_CM_lp",
329 arch: {
330 x86_64: {
331 srcs: ["x86_64/libGLESv1_CM.so"],
332 },
333 x86: {
334 srcs: ["x86/libGLESv1_CM.so"],
335 },
336 },
337 strip: {
338 none: true,
339 },
340 relative_install_path: "egl",
341 shared_libs: ["libc", "libdl", "liblog", "libm"],
342 check_elf_files: false,
343 vendor: true
344 }
345
346 cc_prebuilt_library_shared {
347 name: "libGLESv2_lp",
348 arch: {
349 x86_64: {
350 srcs: ["x86_64/libGLESv2.so"],
351 },
352 x86: {
353 srcs: ["x86_64/libGLESv2.so"],
354 },
355 },
356 strip: {
357 none: true,
358 },
359 relative_install_path: "egl",
360 shared_libs: ["libc", "libdl", "liblog", "libm"],
361 check_elf_files: false,
362 vendor: true
363 }
364
365 cc_prebuilt_library_shared {
366 name: "vulkan.lvp",
367 arch: {
368 x86_64: {
369 srcs: ["x86_64/libvulkan_lvp.so"],
370 },
371 x86: {
372 srcs: ["x86/libvulkan_lvp.so"],
373 },
374 },
375 strip: {
376 none: true,
377 },
378 relative_install_path: "hw",
379 shared_libs: ["libc", "libdl", "liblog", "libm", "libcutils", "libdrm", "liblog", "libnativewindow", "libsync", "libz"],
380 vendor: true
381 }
382
383
384 Next we need to update the device configuration to include the libraries
385 in the build, as well as set the appropriate system properties. We can
386 create the file
387 ``device/google/cuttlefish/shared/mesa/device_vendor.mk``
388
389
390 .. code-block:: makefile
391
392 PRODUCT_SOONG_NAMESPACES += prebuilts/mesa
393 PRODUCT_PACKAGES += libglapi \
394 libGLESv1_CM_lp \
395 libGLESv2_lp \
396 libEGL_lp \
397 libgallium_dri.so \
398 vulkan.lvp
399 PRODUCT_VENDOR_PROPERTIES += \
400 ro.hardware.egl=lp \
401 ro.hardware.vulkan=lvp \
402 mesa.libgl.always.software=true \
403 mesa.android.no.kms.swrast=true \
404 debug.hwui.renderer=opengl \
405 ro.gfx.angle.supported=false \
406 debug.sf.disable_hwc_vds=1 \
407 ro.vendor.hwcomposer.mode=client
408
409 Also the file ``device/google/cuttlefish/shared/mesa/BoardConfig.mk``
410
411 .. code-block:: makefile
412
413 BOARD_VENDOR_SEPOLICY_DIRS += \
414 device/google/cuttlefish/shared/mesa/sepolicy
415
416 Next the file ``device/google/cuttlefish/shared/mesa/sepolicy/file_contexts``
417
418 .. code-block:: sh
419
420 /vendor/lib(64)?/egl/libEGL_lp\.so u:object_r:same_process_hal_file:s0
421 /vendor/lib(64)?/egl/libGLESv1_CM_lp\.so u:object_r:same_process_hal_file:s0
422 /vendor/lib(64)?/egl/libGLESv2_lp\.so u:object_r:same_process_hal_file:s0
423 /vendor/lib(64)?/libglapi\.so u:object_r:same_process_hal_file:s0
424 /vendor/lib(64)?/libgallium_dri\.so u:object_r:same_process_hal_file:s0
425 /vendor/lib(64)?/hw/vulkan\.lvp\.so u:object_r:same_process_hal_file:s0
426
427 After creating these files we need to modify the existing config files
428 to include these build files. First we modify
429 ``device/google/cuttlefish/shared/phone/device_vendor.mk``
430 to add the below code in the spot where other device_vendor
431 files are included.
432
433 .. code-block:: sh
434
435 $(call inherit-product, device/google/cuttlefish/shared/mesa/device_vendor.mk)
436
437 Lastly we modify
438 ``device/google/cuttlefish/vsoc_x86_64/BoardConfig.mk`` to include
439 the following line where the other BoardConfig files are included
440
441 .. code-block:: sh
442
443 -include device/google/cuttlefish/shared/mesa/BoardConfig.mk
444
445 Then we are set to continue following the official instructions to
446 build the cuttlefish target and run it in the cuttlefish emulator.
447
448 .. _android-android-system-properties:
449
450 Android System Properties
451 -------------------------
452
453 Android (generally) uses system properties rather than
454 :doc:`environment variables <envvars>` to control Mesa/Gallium behavior,
455 although there are some exceptions to this for
456 :ref:`Android app developers <envvars-android-app-developers>`.
457
458 With the ``os_get_option()`` helper, the environment variable names are
459 automatically translated to the corresponding system property name by:
460
461 - converting UPPER case to lower case
462 - replacing ``_`` with ``.``
463 - adding the ``mesa.`` prefix to ``<property_name>`` if it's not present already
464 - and then querying the system property name with the following prefixes, in
465 order:
466
467 #. ``debug.<property_name>``
468 #. ``vendor.<property_name>``
469 #. ``<property_name>``
470
471 For example, ``LIBGL_DEBUG`` will be queried as:
472
473 #. ``debug.mesa.libgl.debug``
474 #. ``vendor.mesa.libgl.debug``
475 #. ``mesa.libgl.debug``
476
477 This allows for default ``vendor.`` / ``mesa.`` properties to be overridden by
478 users at run-time with ``debug.`` values.
479
480 System properties can be queried with:
481
482 .. code-block:: sh
483
484 $ adb shell getprop <property_name>
485
486 System properties can be set with:
487
488 .. code-block:: sh
489
490 $ adb shell setprop <property_name> <value>
491
492 For example:
493
494 .. code-block:: sh
495
496 $ adb shell setprop debug.mesa.libgl.debug verbose
497 $ adb shell getprop debug.mesa.libgl.debug
498 verbose
499
500 NOTE: Any driver that wishes to support Android system properties should replace
501 any calls to ``getenv()`` with ``os_get_option()``, which automatically handles
502 both environment variables and Android system properties.
503
504 .. _envvars-android-app-developers:
505
506 Android App Developers
507 ^^^^^^^^^^^^^^^^^^^^^^
508
509 Android app developers have two options to control Mesa behavior on un-rooted
510 devices:
511
512 - Environment variables, using the wrap shell script
513
514 - https://developer.android.com/ndk/guides/wrap-script.html
515
516 - ``debug.<property_name>`` system properties
517
518 App developers with access to rooted devices can also use ``vendor.`` and
519 ``mesa.`` values, although ``debug.`` prefixes are recommended.
520
521 While the system properties values are used for each app invocation once set,
522 they do not persist across device reboots.
523
524
525 Android Driver Developers
526 ^^^^^^^^^^^^^^^^^^^^^^^^^
527
528 Android driver developers have three options to control Mesa behavior on
529 devices with ``root`` access:
530
531 #. ``debug.<property_name>``
532 #. ``vendor.<property_name>``
533 #. ``<property_name>``
534
535 The ``debug.`` prefix can be used without ``root``, while ``vendor.`` and
536 ``mesa.`` prefixes require ``root``.
537
538 Any of the values can be set in the device's makefile to control Mesa
539 behavior, although ``vendor.`` and ``mesa.`` are typically used for this
540 purpose.
541
542 While the system properties values are used for each app invocation once set
543 at runtime, they do not persist across device reboots if configured with
544 ``setprop``.