SlideShare a Scribd company logo
1 of 44
Download to read offline
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 DevelopmentOpersys 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 - 201707Clarence 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 k3sHaggai 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 AnDevCon3Opersys 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 debuggingThe Linux Foundation
 
Inside Android's UI
Inside Android's UIInside Android's UI
Inside Android's UIOpersys 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 2012Opersys inc.
 
Настройка окружения для кросскомпиляции проектов на основе docker'a
Настройка окружения для кросскомпиляции проектов на основе docker'aНастройка окружения для кросскомпиляции проектов на основе docker'a
Настройка окружения для кросскомпиляции проектов на основе docker'acorehard_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 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.
 
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 2013dotCloud
 
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 WinDbgSam Bowne
 
Android for Embedded Linux Developers
Android for Embedded Linux DevelopersAndroid for Embedded Linux Developers
Android for Embedded Linux DevelopersOpersys 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 WinDbgSam 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 cheapedlangley
 

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 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
 
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
 
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

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 JITIgalia
 
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 GStreamerIgalia
 
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 MesaIgalia
 
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 IgaliaIgalia
 
2023 in Chimera Linux
2023 in Chimera                    Linux2023 in Chimera                    Linux
2023 in Chimera LinuxIgalia
 
Building a Linux distro with LLVM
Building a Linux distro        with LLVMBuilding a Linux distro        with LLVM
Building a Linux distro with LLVMIgalia
 
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 GPUsIgalia
 
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 devicesIgalia
 
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 ChromeOSIgalia
 
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 webIgalia
 
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 shadersIgalia
 
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 RaspberryIgalia
 
Enable hardware acceleration for GL applications without glamor on Xorg modes...
Enable hardware acceleration for GL applications without glamor on Xorg modes...Enable hardware acceleration for GL applications without glamor on Xorg modes...
Enable hardware acceleration for GL applications without glamor on Xorg modes...Igalia
 
Async page flip in DRM atomic API
Async page flip in DRM  atomic APIAsync page flip in DRM  atomic API
Async page flip in DRM atomic APIIgalia
 
From the proposal to ECMAScript – Step by Step
From the proposal to ECMAScript – Step by StepFrom the proposal to ECMAScript – Step by Step
From the proposal to ECMAScript – Step by StepIgalia
 
Migrating Babel from CommonJS to ESM
Migrating Babel     from CommonJS to ESMMigrating Babel     from CommonJS to ESM
Migrating Babel from CommonJS to ESMIgalia
 
The rainbow treasure map: Advanced color management on Linux with AMD/Steam D...
The rainbow treasure map: Advanced color management on Linux with AMD/Steam D...The rainbow treasure map: Advanced color management on Linux with AMD/Steam D...
The rainbow treasure map: Advanced color management on Linux with AMD/Steam D...Igalia
 
On-going challenges in the Raspberry Pi driver stack – XDC 2023
On-going challenges in the Raspberry Pi driver stack – XDC 2023On-going challenges in the Raspberry Pi driver stack – XDC 2023
On-going challenges in the Raspberry Pi driver stack – XDC 2023Igalia
 

More from Igalia (20)

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
 
Enable hardware acceleration for GL applications without glamor on Xorg modes...
Enable hardware acceleration for GL applications without glamor on Xorg modes...Enable hardware acceleration for GL applications without glamor on Xorg modes...
Enable hardware acceleration for GL applications without glamor on Xorg modes...
 
Async page flip in DRM atomic API
Async page flip in DRM  atomic APIAsync page flip in DRM  atomic API
Async page flip in DRM atomic API
 
From the proposal to ECMAScript – Step by Step
From the proposal to ECMAScript – Step by StepFrom the proposal to ECMAScript – Step by Step
From the proposal to ECMAScript – Step by Step
 
Migrating Babel from CommonJS to ESM
Migrating Babel     from CommonJS to ESMMigrating Babel     from CommonJS to ESM
Migrating Babel from CommonJS to ESM
 
The rainbow treasure map: Advanced color management on Linux with AMD/Steam D...
The rainbow treasure map: Advanced color management on Linux with AMD/Steam D...The rainbow treasure map: Advanced color management on Linux with AMD/Steam D...
The rainbow treasure map: Advanced color management on Linux with AMD/Steam D...
 
On-going challenges in the Raspberry Pi driver stack – XDC 2023
On-going challenges in the Raspberry Pi driver stack – XDC 2023On-going challenges in the Raspberry Pi driver stack – XDC 2023
On-going challenges in the Raspberry Pi driver stack – XDC 2023
 

Recently uploaded

CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostZilliz
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024The Digital Insurer
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Manik S Magar
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxNavinnSomaal
 
The Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfThe Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfSeasiaInfotech2
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 

Recently uploaded (20)

CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptx
 
The Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfThe Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdf
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 

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