SlideShare a Scribd company logo
Freedreno on Android
Lucas Fryzek
Oct 19th, 2023
XDC 2023, A Coruña
1
Introduction
• State of drivers with Adreno GPUs
• Why would you want to do this?
• How to build and run Mesa on Android
• Driver changes necessary to run on Android
• Areas of needed improvement for Android support
• Shortcomings of my work
2
What does it look like
3
During development:
4
After:
5
A tale of two drivers
• MSM
◦ Your friendly neighborhood DRM-compliant
upstream kernel-mode driver.
• KGSL
◦ Qualcomm's kernel mode driver supported by their
proprietary userspace driver
6
Why KGSL?
• Not every SOC is upstreamed
• Would like to use an open source driver without
requiring lots of merging upstream & downstream
code
• Provides the ability to run Qualcomm's proprietary
userspace driver and Mesa on the same device with
the same KMD
◦ Run different driver in a chroot from host OS
7
• Turnip already runs on top of KGSL
◦ Basis for all my work on Freedreno & EGL
• Freedreno developers have already started
abstracting KMD interface in src/freedreno/drm
8
Getting Started
• Work done Pixel 4a
◦ Adreno 618 GPU - Already supported by Freedreno
• NDK version 25 and version 13
◦ Will talk about why two versions later
• Debug rom from
◦ Perk of using google supported devices
https://flash.android.com
9
• Make sure you use a "userdebug" rom
• If device is not supported by flashing tool you'll need
to build a ROM from scratch with system partition
unlocked
10
How to build Mesa on
Android?
• If we are targeting Android NDK (I'll get back to this
later):
◦ You can make use of Meson cross files to build
Mesa
◦ Example of how to do that here
▪ https://docs.mesa3d.org/android.html
11
Important meson flags for freedreno
• -Dplatforms=android
• -Dplatform-sdk-version=25
• -Dandroid-stub=true
• -Dgallium-drivers=freedreno
• -Dfreedreno_kmds=kgsl
12
How do you even deploy system libraries on an OS such
as Android?
• Existing Mesa documentation for Turnip talks about
replacing libraries in /vendor/lib64/, likely need
to do the same thing for OpenGL
• Also need to unlock system partition
adb disable-verity
adb reboot
adb remount -R
13
• Thankfully Android is open source, we can read
source code
• Replace following libraries in /vendor/lib64/egl
◦ libEGL_adreno.so
◦ libGLESv1_CM_adreno.so
◦ libGLESv2.so
Android's EGL loader
14
• Trying to run GL apps now, we run into another
problem
◦ Apps don't use new driver
• If we restart the device it will start using the new
driver
◦ Not the best dev environment
• Reading there is config
property to preload GL driver
ro.zygote.disable_gl_preload
◦ Need to override the default and set it to true
Android documentation
15
• Environment variables need to be set to force the
mesa loader to work properly on Android
◦ Android has a "prop" system that mesa already
abstracts for environment variable access
◦ adb setprop
"mesa.loader.driver.override" "kgsl"
16
Testing
• You can run regular Android APKs to test the driver
◦ CTS can be built as an APK
◦ Not the most convenient development environment
17
You can actually run command line apps on Android!
• has a
build environment for this
◦ Build OpenGL apps using offscreen EGL Pbuffers
◦ Can run the apps from adb shell
Freedreno reverse engineering tools repo
18
• Repo has build scripts setup already
• It expects NDK version 13
• NDK version 13 has some other goodies that are
useful for debugging applications
19
• NDK 13 was the last shipped version with gdbserver
• Can copy binary from NDK to device
• Makes debugging a lot easier
◦ Can preform debugging on adb shell launched
apps
◦ Can just use Android studio debugger to debug
NDK code in APKs
20
We can take it further!
• With a few tweaks CTS Android platform can be built
as a standalone program just like on Linux
• deqp-runner can even be built through cargo-ndk
21
Source code changes
• Only about ~900 lines of code to add support
• Majority of changes in new kgsl backend in
src/freedreno/drm
• There are also significant changes in egl/drivers
/dri2/platform_android.c
22
kgsl backend
• Handles BO allocation and mapping
• Querying properties from kernel mode driver
• Submitting command queues to hardware
• Handling synchronization
23
• Backend code mostly came from Turnip
• Lots of copy pasting
• Could have more common code added to
src/freedreno for both drivers
24
Interesting quirks
• No referencing count on buffers
◦ Need to ensure buffers are done being used by GPU
before freeing
• KGSL ignores offset argument in
kgsl_command_object
◦ Need to ensure that the GPU address contains the
offset
25
Some backend changes necessary to accommodate
KGSL quirks:
• Framebuffers allocated by Android need to mapped in
a different way
◦ Added per backend implementation of mapping
function
◦ Added per backend implementation of importing
dmabufs
26
platform_android.c changes were not as nice....
27
Dealing with gralloc
• Graphics allocator on Android
• Framebuffers for APK apps are allocated by gralloc
and passed to us
• Gralloc implementation is driver specific
• Turnip already has code to interface with Qualcomm's
version of gralloc
28
Turnip's code...
uint32_t gmsm = ('g' << 24) | ('m' << 16) | ('s' << 8) | 'm';
if (handle_data[0] != gmsm) {
return vk_errorf(device, VK_ERROR_INVALID_EXTERNAL_HANDLE,
"private_handle_t::magic is %x, expected %x",
handle_data[0], gmsm);
}
ubwc = handle_data[1] & 0x08000000;
*dma_buf = handle_fds[0];
29
• No external API to access internal data
• Data is interpreted based on
• But it works!
gralloc implementation source code
30
• Implemented same interface in egl/drivers
/dri2/platform_android.c
• Existing implementation that work with upstream
DRM drivers
31
• Other Mesa android devs have been working on a
newer "Gralloc 4" interface in
platform_android_mapper.cpp
◦ Uses newer standard API to interface with allocator
◦ Cannot be used with Android NDK
▪ c++ namespace between NDK and android tree
build are different
◦ Building Mesa in tree with Android is
▪ This is the alternative way to currently build Mesa
without the NDK
likely to be deprecated
32
Bugs encountered
• Main issue is with surface allocated by gralloc for the
framebuffer
◦ Does not necessarily allocate surfacing matching
hardware limitations
◦ For example, the blitting engine on A6xx GPUs
performs copies on 16x4 pixel chunks
◦ Causing IOMMU faults when the GPU accesses
memory beyond the framebuffer
33
• Biggest issue in preventing KGSL changes from being
merged
• Problem triggered by Android UI elements
◦ Android UI will flicker whenever GPU iommu fault is
triggered
◦ Normal APKs seem to always allocate framebuffer
equal to display size
• Qualcomm blob driver is getting the same surfaces
but somehow avoids this issue
• This is where things got kind of stuck...
34
Notes on Freedreno RE tools
35
• Freedreno has a diverse set of tools for inspecting
what the Qualcomm driver and Freedreno driver are
doing
• Most of these tools are designed to work apps
launched from command line
◦ Doesn't help a lot of problem only happens with
Android APKs
36
One key tool is libwrap
• Library that allows you to trace command streams on
Qualcomm hardware
◦ Works with both Freedreno and Qualcomm
propeitary driver
• Uses LD_PRELOAD to load library and override
system functions
37
How do you run this with APKs?
• Made some changes to libwrap
• How can you LD_PRELOAD on Android?
◦ use prop wrap.<app-name> to override
environment variables in APK processes
• Fix issues associated with tracing Android APKs
◦ APKs had multiple threads accessing the kgsl FD
38
Conclusions
39
• A lot of code in mesa already exists to make running
on android easy
• Proper DRM drivers will likely just work
• If you want to use a downstream kernel mode driver
(and gralloc implementation) some more work is
necessary
40
• Development is faster when you do more of your
work from adb shell
◦ Setting up a good development environment pays
dividends
41
• One of the biggest problem area in Mesa's android
support is window system integration
◦ Gralloc appears to be the standard for this in
Android
◦ Not clear how newer versions of the API can be
used in Mesa
◦ Not easy to get information/documentation on
Gralloc without digging through source code
◦ platform_android.cpp currently needs to be
hacked to work with non-drm drivers
42
Questions?
We're hiring!
igalia.com/jobs/
43
44

More Related Content

Similar to Freedreno on Android – XDC 2023

Android Platform Debugging and Development
Android Platform Debugging and DevelopmentAndroid Platform Debugging and Development
Android Platform Debugging and Development
Opersys inc.
 
Android Platform Debugging and Development at ELCE 2013
Android Platform Debugging and Development at ELCE 2013Android Platform Debugging and Development at ELCE 2013
Android Platform Debugging and Development at ELCE 2013Opersys inc.
 
Mesa and Its Debugging, Вадим Шовкопляс
Mesa and Its Debugging, Вадим ШовкоплясMesa and Its Debugging, Вадим Шовкопляс
Mesa and Its Debugging, Вадим Шовкопляс
Sigma Software
 
Modernize Your Drupal Development
Modernize Your Drupal DevelopmentModernize Your Drupal Development
Modernize Your Drupal DevelopmentChris Tankersley
 
Docker HK Meetup - 201707
Docker HK Meetup - 201707Docker HK Meetup - 201707
Docker HK Meetup - 201707
Clarence Ho
 
DevEx | there’s no place like k3s
DevEx | there’s no place like k3sDevEx | there’s no place like k3s
DevEx | there’s no place like k3s
Haggai Philip Zagury
 
Leveraging Android's Linux Heritage at AnDevCon3
Leveraging Android's Linux Heritage at AnDevCon3Leveraging Android's Linux Heritage at AnDevCon3
Leveraging Android's Linux Heritage at AnDevCon3
Opersys inc.
 
XPDS16: Display Handler, a Client Display Framework for Xen - Brendan Kerrig...
XPDS16:  Display Handler, a Client Display Framework for Xen - Brendan Kerrig...XPDS16:  Display Handler, a Client Display Framework for Xen - Brendan Kerrig...
XPDS16: Display Handler, a Client Display Framework for Xen - Brendan Kerrig...
The Linux Foundation
 
From printk to QEMU: Xen/Linux Kernel debugging
From printk to QEMU: Xen/Linux Kernel debuggingFrom printk to QEMU: Xen/Linux Kernel debugging
From printk to QEMU: Xen/Linux Kernel debugging
The Linux Foundation
 
Inside Android's UI
Inside Android's UIInside Android's UI
Inside Android's UI
Opersys inc.
 
Android Hacks, Variants, Tricks and Resources ESC SV 2012
Android Hacks, Variants, Tricks and Resources ESC SV 2012Android Hacks, Variants, Tricks and Resources ESC SV 2012
Android Hacks, Variants, Tricks and Resources ESC SV 2012
Opersys inc.
 
Настройка окружения для кросскомпиляции проектов на основе docker'a
Настройка окружения для кросскомпиляции проектов на основе docker'aНастройка окружения для кросскомпиляции проектов на основе docker'a
Настройка окружения для кросскомпиляции проектов на основе docker'a
corehard_by
 
Building Android for the Cloud: Android as a Server (Mobile World Congress 2014)
Building Android for the Cloud: Android as a Server (Mobile World Congress 2014)Building Android for the Cloud: Android as a Server (Mobile World Congress 2014)
Building Android for the Cloud: Android as a Server (Mobile World Congress 2014)
Ron Munitz
 
Lightweight Virtualization with Linux Containers and Docker | YaC 2013
Lightweight Virtualization with Linux Containers and Docker | YaC 2013Lightweight Virtualization with Linux Containers and Docker | YaC 2013
Lightweight Virtualization with Linux Containers and Docker | YaC 2013
dotCloud
 
Lightweight Virtualization with Linux Containers and Docker I YaC 2013
Lightweight Virtualization with Linux Containers and Docker I YaC 2013Lightweight Virtualization with Linux Containers and Docker I YaC 2013
Lightweight Virtualization with Linux Containers and Docker I YaC 2013Docker, Inc.
 
CNIT 126: 10: Kernel Debugging with WinDbg
CNIT 126: 10: Kernel Debugging with WinDbgCNIT 126: 10: Kernel Debugging with WinDbg
CNIT 126: 10: Kernel Debugging with WinDbg
Sam Bowne
 
Android for Embedded Linux Developers
Android for Embedded Linux DevelopersAndroid for Embedded Linux Developers
Android for Embedded Linux Developers
Opersys inc.
 
CNIT 126: 10: Kernel Debugging with WinDbg
CNIT 126: 10: Kernel Debugging with WinDbgCNIT 126: 10: Kernel Debugging with WinDbg
CNIT 126: 10: Kernel Debugging with WinDbg
Sam Bowne
 
Add sale davinci
Add sale davinciAdd sale davinci
Add sale davinciAkash Sahoo
 
UWE Linux Boot Camp 2007: Hacking embedded Linux on the cheap
UWE Linux Boot Camp 2007: Hacking embedded Linux on the cheapUWE Linux Boot Camp 2007: Hacking embedded Linux on the cheap
UWE Linux Boot Camp 2007: Hacking embedded Linux on the cheap
edlangley
 

Similar to Freedreno on Android – XDC 2023 (20)

Android Platform Debugging and Development
Android Platform Debugging and DevelopmentAndroid Platform Debugging and Development
Android Platform Debugging and Development
 
Android Platform Debugging and Development at ELCE 2013
Android Platform Debugging and Development at ELCE 2013Android Platform Debugging and Development at ELCE 2013
Android Platform Debugging and Development at ELCE 2013
 
Mesa and Its Debugging, Вадим Шовкопляс
Mesa and Its Debugging, Вадим ШовкоплясMesa and Its Debugging, Вадим Шовкопляс
Mesa and Its Debugging, Вадим Шовкопляс
 
Modernize Your Drupal Development
Modernize Your Drupal DevelopmentModernize Your Drupal Development
Modernize Your Drupal Development
 
Docker HK Meetup - 201707
Docker HK Meetup - 201707Docker HK Meetup - 201707
Docker HK Meetup - 201707
 
DevEx | there’s no place like k3s
DevEx | there’s no place like k3sDevEx | there’s no place like k3s
DevEx | there’s no place like k3s
 
Leveraging Android's Linux Heritage at AnDevCon3
Leveraging Android's Linux Heritage at AnDevCon3Leveraging Android's Linux Heritage at AnDevCon3
Leveraging Android's Linux Heritage at AnDevCon3
 
XPDS16: Display Handler, a Client Display Framework for Xen - Brendan Kerrig...
XPDS16:  Display Handler, a Client Display Framework for Xen - Brendan Kerrig...XPDS16:  Display Handler, a Client Display Framework for Xen - Brendan Kerrig...
XPDS16: Display Handler, a Client Display Framework for Xen - Brendan Kerrig...
 
From printk to QEMU: Xen/Linux Kernel debugging
From printk to QEMU: Xen/Linux Kernel debuggingFrom printk to QEMU: Xen/Linux Kernel debugging
From printk to QEMU: Xen/Linux Kernel debugging
 
Inside Android's UI
Inside Android's UIInside Android's UI
Inside Android's UI
 
Android Hacks, Variants, Tricks and Resources ESC SV 2012
Android Hacks, Variants, Tricks and Resources ESC SV 2012Android Hacks, Variants, Tricks and Resources ESC SV 2012
Android Hacks, Variants, Tricks and Resources ESC SV 2012
 
Настройка окружения для кросскомпиляции проектов на основе docker'a
Настройка окружения для кросскомпиляции проектов на основе docker'aНастройка окружения для кросскомпиляции проектов на основе docker'a
Настройка окружения для кросскомпиляции проектов на основе docker'a
 
Building Android for the Cloud: Android as a Server (Mobile World Congress 2014)
Building Android for the Cloud: Android as a Server (Mobile World Congress 2014)Building Android for the Cloud: Android as a Server (Mobile World Congress 2014)
Building Android for the Cloud: Android as a Server (Mobile World Congress 2014)
 
Lightweight Virtualization with Linux Containers and Docker | YaC 2013
Lightweight Virtualization with Linux Containers and Docker | YaC 2013Lightweight Virtualization with Linux Containers and Docker | YaC 2013
Lightweight Virtualization with Linux Containers and Docker | YaC 2013
 
Lightweight Virtualization with Linux Containers and Docker I YaC 2013
Lightweight Virtualization with Linux Containers and Docker I YaC 2013Lightweight Virtualization with Linux Containers and Docker I YaC 2013
Lightweight Virtualization with Linux Containers and Docker I YaC 2013
 
CNIT 126: 10: Kernel Debugging with WinDbg
CNIT 126: 10: Kernel Debugging with WinDbgCNIT 126: 10: Kernel Debugging with WinDbg
CNIT 126: 10: Kernel Debugging with WinDbg
 
Android for Embedded Linux Developers
Android for Embedded Linux DevelopersAndroid for Embedded Linux Developers
Android for Embedded Linux Developers
 
CNIT 126: 10: Kernel Debugging with WinDbg
CNIT 126: 10: Kernel Debugging with WinDbgCNIT 126: 10: Kernel Debugging with WinDbg
CNIT 126: 10: Kernel Debugging with WinDbg
 
Add sale davinci
Add sale davinciAdd sale davinci
Add sale davinci
 
UWE Linux Boot Camp 2007: Hacking embedded Linux on the cheap
UWE Linux Boot Camp 2007: Hacking embedded Linux on the cheapUWE Linux Boot Camp 2007: Hacking embedded Linux on the cheap
UWE Linux Boot Camp 2007: Hacking embedded Linux on the cheap
 

More from Igalia

A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
Igalia
 
Building End-user Applications on Embedded Devices with WPE
Building End-user Applications on Embedded Devices with WPEBuilding End-user Applications on Embedded Devices with WPE
Building End-user Applications on Embedded Devices with WPE
Igalia
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Igalia
 
Automated Testing for Web-based Systems on Embedded Devices
Automated Testing for Web-based Systems on Embedded DevicesAutomated Testing for Web-based Systems on Embedded Devices
Automated Testing for Web-based Systems on Embedded Devices
Igalia
 
Embedding WPE WebKit - from Bring-up to Maintenance
Embedding WPE WebKit - from Bring-up to MaintenanceEmbedding WPE WebKit - from Bring-up to Maintenance
Embedding WPE WebKit - from Bring-up to Maintenance
Igalia
 
Optimizing Scheduler for Linux Gaming.pdf
Optimizing Scheduler for Linux Gaming.pdfOptimizing Scheduler for Linux Gaming.pdf
Optimizing Scheduler for Linux Gaming.pdf
Igalia
 
Running JS via WASM faster with JIT
Running JS via WASM      faster with JITRunning JS via WASM      faster with JIT
Running JS via WASM faster with JIT
Igalia
 
To crash or not to crash: if you do, at least recover fast!
To crash or not to crash: if you do, at least recover fast!To crash or not to crash: if you do, at least recover fast!
To crash or not to crash: if you do, at least recover fast!
Igalia
 
Implementing a Vulkan Video Encoder From Mesa to GStreamer
Implementing a Vulkan Video Encoder From Mesa to GStreamerImplementing a Vulkan Video Encoder From Mesa to GStreamer
Implementing a Vulkan Video Encoder From Mesa to GStreamer
Igalia
 
8 Years of Open Drivers, including the State of Vulkan in Mesa
8 Years of Open Drivers, including the State of Vulkan in Mesa8 Years of Open Drivers, including the State of Vulkan in Mesa
8 Years of Open Drivers, including the State of Vulkan in Mesa
Igalia
 
Introducción a Mesa. Caso específico dos dispositivos Raspberry Pi por Igalia
Introducción a Mesa. Caso específico dos dispositivos Raspberry Pi por IgaliaIntroducción a Mesa. Caso específico dos dispositivos Raspberry Pi por Igalia
Introducción a Mesa. Caso específico dos dispositivos Raspberry Pi por Igalia
Igalia
 
2023 in Chimera Linux
2023 in Chimera                    Linux2023 in Chimera                    Linux
2023 in Chimera Linux
Igalia
 
Building a Linux distro with LLVM
Building a Linux distro        with LLVMBuilding a Linux distro        with LLVM
Building a Linux distro with LLVM
Igalia
 
turnip: Update on Open Source Vulkan Driver for Adreno GPUs
turnip: Update on Open Source Vulkan Driver for Adreno GPUsturnip: Update on Open Source Vulkan Driver for Adreno GPUs
turnip: Update on Open Source Vulkan Driver for Adreno GPUs
Igalia
 
Graphics stack updates for Raspberry Pi devices
Graphics stack updates for Raspberry Pi devicesGraphics stack updates for Raspberry Pi devices
Graphics stack updates for Raspberry Pi devices
Igalia
 
Delegated Compositing - Utilizing Wayland Protocols for Chromium on ChromeOS
Delegated Compositing - Utilizing Wayland Protocols for Chromium on ChromeOSDelegated Compositing - Utilizing Wayland Protocols for Chromium on ChromeOS
Delegated Compositing - Utilizing Wayland Protocols for Chromium on ChromeOS
Igalia
 
MessageFormat: The future of i18n on the web
MessageFormat: The future of i18n on the webMessageFormat: The future of i18n on the web
MessageFormat: The future of i18n on the web
Igalia
 
Replacing the geometry pipeline with mesh shaders
Replacing the geometry pipeline with mesh shadersReplacing the geometry pipeline with mesh shaders
Replacing the geometry pipeline with mesh shaders
Igalia
 
I'm not an AMD expert, but...
I'm not an AMD expert, but...I'm not an AMD expert, but...
I'm not an AMD expert, but...
Igalia
 
Status of Vulkan on Raspberry
Status of Vulkan on RaspberryStatus of Vulkan on Raspberry
Status of Vulkan on Raspberry
Igalia
 

More from Igalia (20)

A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Building End-user Applications on Embedded Devices with WPE
Building End-user Applications on Embedded Devices with WPEBuilding End-user Applications on Embedded Devices with WPE
Building End-user Applications on Embedded Devices with WPE
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Automated Testing for Web-based Systems on Embedded Devices
Automated Testing for Web-based Systems on Embedded DevicesAutomated Testing for Web-based Systems on Embedded Devices
Automated Testing for Web-based Systems on Embedded Devices
 
Embedding WPE WebKit - from Bring-up to Maintenance
Embedding WPE WebKit - from Bring-up to MaintenanceEmbedding WPE WebKit - from Bring-up to Maintenance
Embedding WPE WebKit - from Bring-up to Maintenance
 
Optimizing Scheduler for Linux Gaming.pdf
Optimizing Scheduler for Linux Gaming.pdfOptimizing Scheduler for Linux Gaming.pdf
Optimizing Scheduler for Linux Gaming.pdf
 
Running JS via WASM faster with JIT
Running JS via WASM      faster with JITRunning JS via WASM      faster with JIT
Running JS via WASM faster with JIT
 
To crash or not to crash: if you do, at least recover fast!
To crash or not to crash: if you do, at least recover fast!To crash or not to crash: if you do, at least recover fast!
To crash or not to crash: if you do, at least recover fast!
 
Implementing a Vulkan Video Encoder From Mesa to GStreamer
Implementing a Vulkan Video Encoder From Mesa to GStreamerImplementing a Vulkan Video Encoder From Mesa to GStreamer
Implementing a Vulkan Video Encoder From Mesa to GStreamer
 
8 Years of Open Drivers, including the State of Vulkan in Mesa
8 Years of Open Drivers, including the State of Vulkan in Mesa8 Years of Open Drivers, including the State of Vulkan in Mesa
8 Years of Open Drivers, including the State of Vulkan in Mesa
 
Introducción a Mesa. Caso específico dos dispositivos Raspberry Pi por Igalia
Introducción a Mesa. Caso específico dos dispositivos Raspberry Pi por IgaliaIntroducción a Mesa. Caso específico dos dispositivos Raspberry Pi por Igalia
Introducción a Mesa. Caso específico dos dispositivos Raspberry Pi por Igalia
 
2023 in Chimera Linux
2023 in Chimera                    Linux2023 in Chimera                    Linux
2023 in Chimera Linux
 
Building a Linux distro with LLVM
Building a Linux distro        with LLVMBuilding a Linux distro        with LLVM
Building a Linux distro with LLVM
 
turnip: Update on Open Source Vulkan Driver for Adreno GPUs
turnip: Update on Open Source Vulkan Driver for Adreno GPUsturnip: Update on Open Source Vulkan Driver for Adreno GPUs
turnip: Update on Open Source Vulkan Driver for Adreno GPUs
 
Graphics stack updates for Raspberry Pi devices
Graphics stack updates for Raspberry Pi devicesGraphics stack updates for Raspberry Pi devices
Graphics stack updates for Raspberry Pi devices
 
Delegated Compositing - Utilizing Wayland Protocols for Chromium on ChromeOS
Delegated Compositing - Utilizing Wayland Protocols for Chromium on ChromeOSDelegated Compositing - Utilizing Wayland Protocols for Chromium on ChromeOS
Delegated Compositing - Utilizing Wayland Protocols for Chromium on ChromeOS
 
MessageFormat: The future of i18n on the web
MessageFormat: The future of i18n on the webMessageFormat: The future of i18n on the web
MessageFormat: The future of i18n on the web
 
Replacing the geometry pipeline with mesh shaders
Replacing the geometry pipeline with mesh shadersReplacing the geometry pipeline with mesh shaders
Replacing the geometry pipeline with mesh shaders
 
I'm not an AMD expert, but...
I'm not an AMD expert, but...I'm not an AMD expert, but...
I'm not an AMD expert, but...
 
Status of Vulkan on Raspberry
Status of Vulkan on RaspberryStatus of Vulkan on Raspberry
Status of Vulkan on Raspberry
 

Recently uploaded

Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
Ana-Maria Mihalceanu
 
A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...
sonjaschweigert1
 
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to ProductionGenerative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Aggregage
 
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
SOFTTECHHUB
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
ControlCase
 
20240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 202420240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 2024
Matthew Sinclair
 
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
Neo4j
 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
mikeeftimakis1
 
UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5
DianaGray10
 
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdfObservability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Paige Cruz
 
Large Language Model (LLM) and it’s Geospatial Applications
Large Language Model (LLM) and it’s Geospatial ApplicationsLarge Language Model (LLM) and it’s Geospatial Applications
Large Language Model (LLM) and it’s Geospatial Applications
Rohit Gautam
 
National Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practicesNational Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practices
Quotidiano Piemontese
 
Pushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 daysPushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 days
Adtran
 
Climate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing DaysClimate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing Days
Kari Kakkonen
 
Building RAG with self-deployed Milvus vector database and Snowpark Container...
Building RAG with self-deployed Milvus vector database and Snowpark Container...Building RAG with self-deployed Milvus vector database and Snowpark Container...
Building RAG with self-deployed Milvus vector database and Snowpark Container...
Zilliz
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
DanBrown980551
 
How to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptxHow to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptx
danishmna97
 
Microsoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdfMicrosoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdf
Uni Systems S.M.S.A.
 
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
Neo4j
 
Enchancing adoption of Open Source Libraries. A case study on Albumentations.AI
Enchancing adoption of Open Source Libraries. A case study on Albumentations.AIEnchancing adoption of Open Source Libraries. A case study on Albumentations.AI
Enchancing adoption of Open Source Libraries. A case study on Albumentations.AI
Vladimir Iglovikov, Ph.D.
 

Recently uploaded (20)

Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
 
A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...
 
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to ProductionGenerative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to Production
 
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
 
20240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 202420240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 2024
 
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
 
UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5
 
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdfObservability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
 
Large Language Model (LLM) and it’s Geospatial Applications
Large Language Model (LLM) and it’s Geospatial ApplicationsLarge Language Model (LLM) and it’s Geospatial Applications
Large Language Model (LLM) and it’s Geospatial Applications
 
National Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practicesNational Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practices
 
Pushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 daysPushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 days
 
Climate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing DaysClimate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing Days
 
Building RAG with self-deployed Milvus vector database and Snowpark Container...
Building RAG with self-deployed Milvus vector database and Snowpark Container...Building RAG with self-deployed Milvus vector database and Snowpark Container...
Building RAG with self-deployed Milvus vector database and Snowpark Container...
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
 
How to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptxHow to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptx
 
Microsoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdfMicrosoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdf
 
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
 
Enchancing adoption of Open Source Libraries. A case study on Albumentations.AI
Enchancing adoption of Open Source Libraries. A case study on Albumentations.AIEnchancing adoption of Open Source Libraries. A case study on Albumentations.AI
Enchancing adoption of Open Source Libraries. A case study on Albumentations.AI
 

Freedreno on Android – XDC 2023

  • 1. Freedreno on Android Lucas Fryzek Oct 19th, 2023 XDC 2023, A Coruña 1
  • 2. Introduction • State of drivers with Adreno GPUs • Why would you want to do this? • How to build and run Mesa on Android • Driver changes necessary to run on Android • Areas of needed improvement for Android support • Shortcomings of my work 2
  • 3. What does it look like 3
  • 6. A tale of two drivers • MSM ◦ Your friendly neighborhood DRM-compliant upstream kernel-mode driver. • KGSL ◦ Qualcomm's kernel mode driver supported by their proprietary userspace driver 6
  • 7. Why KGSL? • Not every SOC is upstreamed • Would like to use an open source driver without requiring lots of merging upstream & downstream code • Provides the ability to run Qualcomm's proprietary userspace driver and Mesa on the same device with the same KMD ◦ Run different driver in a chroot from host OS 7
  • 8. • Turnip already runs on top of KGSL ◦ Basis for all my work on Freedreno & EGL • Freedreno developers have already started abstracting KMD interface in src/freedreno/drm 8
  • 9. Getting Started • Work done Pixel 4a ◦ Adreno 618 GPU - Already supported by Freedreno • NDK version 25 and version 13 ◦ Will talk about why two versions later • Debug rom from ◦ Perk of using google supported devices https://flash.android.com 9
  • 10. • Make sure you use a "userdebug" rom • If device is not supported by flashing tool you'll need to build a ROM from scratch with system partition unlocked 10
  • 11. How to build Mesa on Android? • If we are targeting Android NDK (I'll get back to this later): ◦ You can make use of Meson cross files to build Mesa ◦ Example of how to do that here ▪ https://docs.mesa3d.org/android.html 11
  • 12. Important meson flags for freedreno • -Dplatforms=android • -Dplatform-sdk-version=25 • -Dandroid-stub=true • -Dgallium-drivers=freedreno • -Dfreedreno_kmds=kgsl 12
  • 13. How do you even deploy system libraries on an OS such as Android? • Existing Mesa documentation for Turnip talks about replacing libraries in /vendor/lib64/, likely need to do the same thing for OpenGL • Also need to unlock system partition adb disable-verity adb reboot adb remount -R 13
  • 14. • Thankfully Android is open source, we can read source code • Replace following libraries in /vendor/lib64/egl ◦ libEGL_adreno.so ◦ libGLESv1_CM_adreno.so ◦ libGLESv2.so Android's EGL loader 14
  • 15. • Trying to run GL apps now, we run into another problem ◦ Apps don't use new driver • If we restart the device it will start using the new driver ◦ Not the best dev environment • Reading there is config property to preload GL driver ro.zygote.disable_gl_preload ◦ Need to override the default and set it to true Android documentation 15
  • 16. • Environment variables need to be set to force the mesa loader to work properly on Android ◦ Android has a "prop" system that mesa already abstracts for environment variable access ◦ adb setprop "mesa.loader.driver.override" "kgsl" 16
  • 17. Testing • You can run regular Android APKs to test the driver ◦ CTS can be built as an APK ◦ Not the most convenient development environment 17
  • 18. You can actually run command line apps on Android! • has a build environment for this ◦ Build OpenGL apps using offscreen EGL Pbuffers ◦ Can run the apps from adb shell Freedreno reverse engineering tools repo 18
  • 19. • Repo has build scripts setup already • It expects NDK version 13 • NDK version 13 has some other goodies that are useful for debugging applications 19
  • 20. • NDK 13 was the last shipped version with gdbserver • Can copy binary from NDK to device • Makes debugging a lot easier ◦ Can preform debugging on adb shell launched apps ◦ Can just use Android studio debugger to debug NDK code in APKs 20
  • 21. We can take it further! • With a few tweaks CTS Android platform can be built as a standalone program just like on Linux • deqp-runner can even be built through cargo-ndk 21
  • 22. Source code changes • Only about ~900 lines of code to add support • Majority of changes in new kgsl backend in src/freedreno/drm • There are also significant changes in egl/drivers /dri2/platform_android.c 22
  • 23. kgsl backend • Handles BO allocation and mapping • Querying properties from kernel mode driver • Submitting command queues to hardware • Handling synchronization 23
  • 24. • Backend code mostly came from Turnip • Lots of copy pasting • Could have more common code added to src/freedreno for both drivers 24
  • 25. Interesting quirks • No referencing count on buffers ◦ Need to ensure buffers are done being used by GPU before freeing • KGSL ignores offset argument in kgsl_command_object ◦ Need to ensure that the GPU address contains the offset 25
  • 26. Some backend changes necessary to accommodate KGSL quirks: • Framebuffers allocated by Android need to mapped in a different way ◦ Added per backend implementation of mapping function ◦ Added per backend implementation of importing dmabufs 26
  • 27. platform_android.c changes were not as nice.... 27
  • 28. Dealing with gralloc • Graphics allocator on Android • Framebuffers for APK apps are allocated by gralloc and passed to us • Gralloc implementation is driver specific • Turnip already has code to interface with Qualcomm's version of gralloc 28
  • 29. Turnip's code... uint32_t gmsm = ('g' << 24) | ('m' << 16) | ('s' << 8) | 'm'; if (handle_data[0] != gmsm) { return vk_errorf(device, VK_ERROR_INVALID_EXTERNAL_HANDLE, "private_handle_t::magic is %x, expected %x", handle_data[0], gmsm); } ubwc = handle_data[1] & 0x08000000; *dma_buf = handle_fds[0]; 29
  • 30. • No external API to access internal data • Data is interpreted based on • But it works! gralloc implementation source code 30
  • 31. • Implemented same interface in egl/drivers /dri2/platform_android.c • Existing implementation that work with upstream DRM drivers 31
  • 32. • Other Mesa android devs have been working on a newer "Gralloc 4" interface in platform_android_mapper.cpp ◦ Uses newer standard API to interface with allocator ◦ Cannot be used with Android NDK ▪ c++ namespace between NDK and android tree build are different ◦ Building Mesa in tree with Android is ▪ This is the alternative way to currently build Mesa without the NDK likely to be deprecated 32
  • 33. Bugs encountered • Main issue is with surface allocated by gralloc for the framebuffer ◦ Does not necessarily allocate surfacing matching hardware limitations ◦ For example, the blitting engine on A6xx GPUs performs copies on 16x4 pixel chunks ◦ Causing IOMMU faults when the GPU accesses memory beyond the framebuffer 33
  • 34. • Biggest issue in preventing KGSL changes from being merged • Problem triggered by Android UI elements ◦ Android UI will flicker whenever GPU iommu fault is triggered ◦ Normal APKs seem to always allocate framebuffer equal to display size • Qualcomm blob driver is getting the same surfaces but somehow avoids this issue • This is where things got kind of stuck... 34
  • 35. Notes on Freedreno RE tools 35
  • 36. • Freedreno has a diverse set of tools for inspecting what the Qualcomm driver and Freedreno driver are doing • Most of these tools are designed to work apps launched from command line ◦ Doesn't help a lot of problem only happens with Android APKs 36
  • 37. One key tool is libwrap • Library that allows you to trace command streams on Qualcomm hardware ◦ Works with both Freedreno and Qualcomm propeitary driver • Uses LD_PRELOAD to load library and override system functions 37
  • 38. How do you run this with APKs? • Made some changes to libwrap • How can you LD_PRELOAD on Android? ◦ use prop wrap.<app-name> to override environment variables in APK processes • Fix issues associated with tracing Android APKs ◦ APKs had multiple threads accessing the kgsl FD 38
  • 40. • A lot of code in mesa already exists to make running on android easy • Proper DRM drivers will likely just work • If you want to use a downstream kernel mode driver (and gralloc implementation) some more work is necessary 40
  • 41. • Development is faster when you do more of your work from adb shell ◦ Setting up a good development environment pays dividends 41
  • 42. • One of the biggest problem area in Mesa's android support is window system integration ◦ Gralloc appears to be the standard for this in Android ◦ Not clear how newer versions of the API can be used in Mesa ◦ Not easy to get information/documentation on Gralloc without digging through source code ◦ platform_android.cpp currently needs to be hacked to work with non-drm drivers 42
  • 44. 44