File: android.rst

package info (click to toggle)
mesa 24.3.4-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 279,568 kB
  • sloc: ansic: 2,122,763; xml: 1,010,303; cpp: 513,137; python: 70,351; asm: 38,315; lisp: 20,211; yacc: 12,036; lex: 3,372; sh: 841; makefile: 256
file content (426 lines) | stat: -rw-r--r-- 14,590 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
Android
=======

Mesa hardware drivers can be built for Android one of two ways: built
into the Android OS using the ndk-build build system on older versions
of Android, or out-of-tree using the Meson build system and the
Android NDK.

The ndk-build build system has proven to be hard to maintain, as one
needs a built Android tree to build against, and it has never been
tested in CI.  The Meson build system flow is frequently used by
Chrome OS developers for building and testing Android drivers.

When building llvmpipe or lavapipe for Android the ndk-build workflow
is also used, but there are additional steps required to add the driver
to the Android OS image.

Building using the Android NDK
------------------------------

Download and install the NDK using whatever method you normally would.
Then, create your Meson cross file to use it, something like this
``~/.local/share/meson/cross/android-aarch64`` file:

.. code-block:: ini

    [binaries]
    ar = 'NDKDIR/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android-ar'
    c = ['ccache', 'NDKDIR/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android29-clang']
    cpp = ['ccache', 'NDKDIR/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android29-clang++', '-fno-exceptions', '-fno-unwind-tables', '-fno-asynchronous-unwind-tables', '-static-libstdc++']
    c_ld = 'lld'
    cpp_ld = 'lld'
    strip = 'NDKDIR/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android-strip'
    # Android doesn't come with a pkg-config, but we need one for Meson to be happy not
    # finding all the optional deps it looks for.  Use system pkg-config pointing at a
    # directory we get to populate with any .pc files we want to add for Android
    pkg-config = ['env', 'PKG_CONFIG_LIBDIR=NDKDIR/pkgconfig', '/usr/bin/pkg-config']

    [host_machine]
    system = 'android'
    cpu_family = 'aarch64'
    cpu = 'armv8'
    endian = 'little'

Now, use that cross file for your Android build directory (as in this
one cross-compiling the turnip driver for a stock Pixel phone)

.. code-block:: sh

    meson setup build-android-aarch64 \
        --cross-file android-aarch64 \
	-Dplatforms=android \
	-Dplatform-sdk-version=26 \
	-Dandroid-stub=true \
	-Dgallium-drivers= \
	-Dvulkan-drivers=freedreno \
	-Dfreedreno-kmds=kgsl
    meson compile -C build-android-aarch64

Replacing Android drivers on stock Android
------------------------------------------

The vendor partition with the drivers is normally mounted from a
read-only disk image on ``/vendor``.  To be able to replace them for
driver development, we need to unlock the device and remount
``/vendor`` read/write.

.. code-block:: sh

    adb disable-verity
    adb reboot
    adb remount -R
    adb remount

Now you can replace drivers as in:

.. code-block:: sh

    adb push build-android-aarch64/src/freedreno/vulkan/libvulkan_freedreno.so /vendor/lib64/hw/vulkan.sdm710.so

Note this command doesn't quite work because libvulkan wants the
SONAME to match. You can use ``patchelf`` to fix this:

.. code-block:: sh

   cp build-android-aarch64/src/freedreno/vulkan/libvulkan_freedreno.so /tmp/vulkan.sdm710.so
   patchelf --set-soname vulkan.sdm710.so /tmp/vulkan.sdm710.so
   adb push /tmp/vulkan.sdm710.so /vendor/lib64/hw/

Replacing Android drivers on Chrome OS
--------------------------------------

Chrome OS's ARC++ is an Android container with hardware drivers inside
of it.  The vendor partition with the drivers is normally mounted from
a read-only squashfs image on disk.  For doing rapid driver
development, you don't want to regenerate that image.  So, we'll take
the existing squashfs image, copy it out on the host, and then use a
bind mount instead of a loopback mount so we can update our drivers
using scp from outside the container.

On your device, you'll want to make ``/`` read-write.  ssh in as root
and run:

.. code-block:: sh

    crossystem dev_boot_signed_only=0
    /usr/share/vboot/bin/make_dev_ssd.sh --remove_rootfs_verification --partitions 4
    reboot

Then, we'll switch Android from using an image for ``/vendor`` to using a
bind-mount from a directory we control.

.. code-block:: sh

    cd /opt/google/containers/android/
    mkdir vendor-ro
    mount -o loop vendor.raw.img vendor-ro
    cp -a vendor-ro vendor-rw
    emacs config.json

In the ``config.json``, you want to find the block for ``/vendor`` and
change it to::

            {
                "destination": "/vendor",
                "type": "bind",
                "source": "/opt/google/containers/android/vendor-rw",
                "options": [
                    "bind",
                    "rw"
                ]
            },

Now, restart the UI to do a full reload:

.. code-block:: sh

    restart ui

At this point, your android container is restarted with your new
bind-mount ``/vendor``, and if you use ``android-sh`` to shell into it
then the ``mount`` command should show::

    /dev/root on /vendor type ext2 (rw,seclabel,relatime)

Now, replacing your DRI driver with a new one built for Android should
be a matter of:

.. code-block:: sh

    scp msm_dri.so $HOST:/opt/google/containers/android/vendor-rw/lib64/dri/

You can do your build of your DRI driver using ``emerge-$BOARD
arc-mesa-freedreno`` (for example) if you have a source tree with
ARC++, but it should also be possible to build using the NDK as
described above.  There are currently rough edges with this, for
example the build will require that you have your arc-libdrm build
available to the NDK, assuming you're building anything but the
Freedreno Vulkan driver for KGSL.  You can mostly put things in place
with:

.. code-block:: sh

    scp $HOST:/opt/google/containers/android/vendor-rw/lib64/libdrm.so \
        NDKDIR/sysroot/usr/lib/aarch64-linux-android/lib/

    ln -s \
        /usr/include/xf86drm.h \
	/usr/include/libsync.h \
	/usr/include/libdrm \
	NDKDIR/sysroot/usr/include/

It seems that new invocations of an application will often reload the
DRI driver, but depending on the component you're working on you may
find you need to reload the whole Android container.  To do so without
having to log in to Chrome again every time, you can just kill the
container and let it restart:

.. code-block:: sh

    kill $(cat /run/containers/android-run_oci/container.pid )

Adding drivers to Android OS image
----------------------------------

When building your own Android OS images it's possible to add
drivers built out of tree directly into the OS image. For
running llvmpipe and lavapipe on Android this step is required
to ensure Android is able to load the drivers correctly.

The following steps provide and example for building
the android cuttlefish image following the official Android
documentation from https://source.android.com/docs/setup

When building llvmpipe or lavapipe for Android, it is required
to do this so that the permissions for accessing the library
are set correctly.

Following the Android documentation, we can run the following
commands

.. code-block:: sh

   repo init -b main -u https://android.googlesource.com/platform/manifest
   repo sync -c -j8

   source build/envsetup.sh
   lunch aosp_cf_x86_64_phone-trunk_staging-userdebug

Be aware that the sync command can take a long time to run as
it will download all of the source code. This will set up
the ``aosp_cf_x86_64_phone-trunk_staging-userdebug`` build target
for Android. Please note that the x86_64 cuttlefish target will require
you to build mesa for 32bit and 64bit. Next we need to copy the build
driver libraries into the source tree of Android and patch the binary names.
Note that as of ``9b7bb6cc9fa``, libgallium will include the build tag in the
name, so the name of that library will need to match the tag used in the build.

.. code-block:: sh

   mkdir prebuilts/mesa
   mkdir prebuilts/mesa/x86_64
   mkdir prebuilts/mesa/x86
   cp ${INSTALL_PREFIX_64}/lib/libEGL.so prebuilts/mesa/x86_64/
   cp ${INSTALL_PREFIX_64}/lib/libglapi.so prebuilts/mesa/x86_64/
   cp ${INSTALL_PREFIX_64}/lib/libgallium-24.3.0-devel.so prebuilts/mesa/x86_64/
   cp ${INSTALL_PREFIX_64}/lib/libGLESv1_CM.so  prebuilts/mesa/x86_64/
   cp ${INSTALL_PREFIX_64}/lib/libGLESv2.so  prebuilts/mesa/x86_64/
   cp ${INSTALL_PREFIX_64}/lib/libvulkan_lvp.so prebuilts/mesa/x86_64/
   cp ${INSTALL_PREFIX_32}/lib/libEGL.so prebuilts/mesa/x86
   cp ${INSTALL_PREFIX_32}/lib/libglapi.so prebuilts/mesa/x86
   cp ${INSTALL_PREFIX_32}/lib/libgallium-24.3.0-devel.so prebuilts/mesa/x86/
   cp ${INSTALL_PREFIX_32}/lib/libGLESv1_CM.so  prebuilts/mesa/x86
   cp ${INSTALL_PREFIX_32}/lib/libGLESv2.so  prebuilts/mesa/x86
   cp ${INSTALL_PREFIX_32}/lib/libvulkan_lvp.so prebuilts/mesa/x86

   patchelf --set-soname libEGL_lp.so prebuilts/mesa/x86_64/libEGL.so
   patchelf --set-soname libGLESv1_CM_lp.so prebuilts/mesa/x86_64/libGLESv1_CM.so
   patchelf --set-soname libGLESv2_lp.so prebuilts/mesa/x86_64/libGLESv2.so
   patchelf --set-soname vulkan.lvp.so prebuilts/mesa/x86_64/libvulkan_lvp.so
   patchelf --set-soname libEGL_lp.so prebuilts/mesa/x86/libEGL.so
   patchelf --set-soname libGLESv1_CM_lp.so prebuilts/mesa/x86/libGLESv1_CM.so
   patchelf --set-soname libGLESv2_lp.so prebuilts/mesa/x86/libGLESv2.so
   patchelf --set-soname vulkan.lvp.so prebuilts/mesa/x86/libvulkan_lvp.so

We then need to create an ``prebuilts/mesa/Android.bp`` build file to include
the libraries in the build.

.. code-block::

   cc_prebuilt_library_shared {
       name: "libglapi",
       arch: {
           x86_64: {
               srcs: ["x86_64/libglapi.so"],
           },
           x86: {
               srcs: ["x86/libglapi.so"],
           },
       },
       strip: {
           none: true,
       },
       relative_install_path: "egl",
       shared_libs: ["libc", "libdl", "liblog", "libm"],
       vendor: true
   }

   cc_prebuilt_library_shared {
       name: "libgallium-24.3.0-devel",
       arch: {
           x86_64: {
               srcs: ["x86_64/libgallium-24.3.0-devel.so"],
           },
           x86: {
               srcs: ["x86/libgallium-24.3.0-devel.so"],
           },
       },
       strip: {
           none: true,
       },
       relative_install_path: "egl",
       shared_libs: ["libc", "libdl", "liblog", "libm"],
       check_elf_files: false,
       vendor: true
   }

   cc_prebuilt_library_shared {
       name: "libEGL_lp",
       arch: {
           x86_64: {
               srcs: ["x86_64/libEGL.so"],
           },
           x86: {
               srcs: ["x86/libEGL.so"],
           },
       },
       strip: {
           none: true,
       },
       relative_install_path: "egl",
       shared_libs: ["libc", "libdl", "liblog", "libm", "libcutils", "libdrm", "libhardware", "liblog", "libnativewindow", "libsync"],
       check_elf_files: false,
       vendor: true
   }

   cc_prebuilt_library_shared {
       name: "libGLESv1_CM_lp",
       arch: {
           x86_64: {
               srcs: ["x86_64/libGLESv1_CM.so"],
           },
           x86: {
               srcs: ["x86/libGLESv1_CM.so"],
           },
       },
       strip: {
           none: true,
       },
       relative_install_path: "egl",
       shared_libs: ["libc", "libdl", "liblog", "libm"],
       check_elf_files: false,
       vendor: true
   }

   cc_prebuilt_library_shared {
       name: "libGLESv2_lp",
       arch: {
           x86_64: {
               srcs: ["x86_64/libGLESv2.so"],
           },
           x86: {
               srcs: ["x86_64/libGLESv2.so"],
           },
       },
       strip: {
           none: true,
       },
       relative_install_path: "egl",
       shared_libs: ["libc", "libdl", "liblog", "libm"],
       check_elf_files: false,
       vendor: true
   }

   cc_prebuilt_library_shared {
       name: "vulkan.lvp",
       arch: {
           x86_64: {
               srcs: ["x86_64/libvulkan_lvp.so"],
           },
           x86: {
               srcs: ["x86/libvulkan_lvp.so"],
           },
       },
       strip: {
           none: true,
       },
       relative_install_path: "hw",
       shared_libs: ["libc", "libdl", "liblog", "libm", "libcutils", "libdrm", "liblog", "libnativewindow", "libsync", "libz"],
       vendor: true
   }


Next we need to update the device configuration to include the libraries
in the build, as well as set the appropriate system properties. We can
create the file
``device/google/cuttlefish/shared/mesa/device_vendor.mk``


.. code-block:: makefile

   PRODUCT_SOONG_NAMESPACES += prebuilts/mesa
   PRODUCT_PACKAGES += libglapi \
                       libGLESv1_CM_lp \
                       libGLESv2_lp \
                       libEGL_lp \
                       libgallium-24.3.0-devel.so \
                       vulkan.lvp
   PRODUCT_VENDOR_PROPERTIES += \
           ro.hardware.egl=lp \
           ro.hardware.vulkan=lvp \
           mesa.libgl.always.software=true \
           mesa.android.no.kms.swrast=true \
           debug.hwui.renderer=opengl \
           ro.gfx.angle.supported=false \
           debug.sf.disable_hwc_vds=1 \
           ro.vendor.hwcomposer.mode=client

Also the file ``device/google/cuttlefish/shared/mesa/BoardConfig.mk``

.. code-block:: makefile

   BOARD_VENDOR_SEPOLICY_DIRS += \
           device/google/cuttlefish/shared/mesa/sepolicy

Next the file ``device/google/cuttlefish/shared/mesa/sepolicy/file_contexts``

.. code-block:: sh

   /vendor/lib(64)?/egl/libEGL_lp\.so u:object_r:same_process_hal_file:s0
   /vendor/lib(64)?/egl/libGLESv1_CM_lp\.so u:object_r:same_process_hal_file:s0
   /vendor/lib(64)?/egl/libGLESv2_lp\.so u:object_r:same_process_hal_file:s0
   /vendor/lib(64)?/egl/libglapi\.so u:object_r:same_process_hal_file:s0
   /vendor/lib(64)?/egl/libgallium\-24.3.0\-devel\.so u:object_r:same_process_hal_file:s0
   /vendor/lib(64)?/hw/vulkan\.lvp\.so u:object_r:same_process_hal_file:s0

After creating these files we need to modify the existing config files
to include these build files. First we modify
``device/google/cuttlefish/shared/phone/device_vendor.mk``
to add the below code in the spot where other device_vendor
files are included.

.. code-block:: sh

   $(call inherit-product, device/google/cuttlefish/shared/mesa/device_vendor.mk)

Lastly we modify
``device/google/cuttlefish/vsoc_x86_64/BoardConfig.mk`` to include
the following line where the other BoardConfig files are included

.. code-block:: sh

   -include device/google/cuttlefish/shared/mesa/BoardConfig.mk

Then we are set to continue following the official instructions to
build the cuttlefish target and run it in the cuttlefish emulator.