Android Vulkan Sample Layer

This directory contains a bare-minimum sample Android Vulkan layer implementation ( VK_LAYER_ANDROID_sample).

It is designed to serve as a starting point for developers wishing to implement their own Android Vulkan layers, allowing them to bypass the initial setup and focus immediately on their specific layer logic.


How to Build

The layer is integrated into the Android build system (Soong).

  1. Source the build environment:

    source build/envsetup.sh
    
  2. Lunch your target (e.g., aosp_arm64-trunk_staging-eng):

    lunch aosp_arm64-trunk_staging-eng
    
  3. Build the APK:

    m VkLayerSample
    

The build will produce VkLayerSample.apk containing the libVkLayer_sample.so shared library.

How to Install and Enable

You can deploy and enable the layer on a connected Android device.

1. Install the APK

Install the built APK to your device:

adb install -r $ANDROID_PRODUCT_OUT/system/app/VkLayerSampleAPK/VkLayerSample.apk

2. Enable the Layer for your Application

To enable the layer for a specific application (e.g., com.example.vulkanapp), run the following adb commands:

# Enable debug layers globally
adb shell settings put global enable_gpu_debug_layers 1

# Specify the target application
adb shell settings put global gpu_debug_app com.example.vulkanapp

# Specify the layer to load (VK_LAYER_ANDROID_sample)
adb shell settings put global gpu_debug_layers VK_LAYER_ANDROID_sample

# Specify the package containing the layer library (libVkLayer_sample.so)
adb shell settings put global gpu_debug_layer_app com.android.vulkan.sample_layer

For more detailed information and alternative ways to enable Vulkan layers on Android (including system-wide enablement or app-specific embedding), refer to the official Android NDK Graphics Guide on Validation Layers.

3. Verify the Layer is Loaded

Monitor logcat for the VkSampleLayer tag:

adb logcat -s VkSampleLayer

When you launch your target application (com.example.vulkanapp), you should see the following output confirming the layer was loaded and initialized successfully:

VkSampleLayer: Sample Layer: Instance initialized successfully.
VkSampleLayer: Sample Layer: Device initialized successfully. Layer initialization complete.

OEM and Platform Layers (OPL) Testing Guide

This guide details how to build, install, and test the OEM (OL) and Platform (PL) Vulkan layers on an Android userdebug device.

1. Building the Layers

Both layers are built from the same source code located in vulkan/layers/sample/. The Android.bp file configures two distinct targets:

  • libVkLayer_sample_ol builds the OEM layer (libVkLayer_sample_ol.so) targeting /product.
  • libVkLayer_sample_pl builds the Platform layer (libVkLayer_sample_pl.so) targeting /system.

To build them, run the following commands from the root of your AOSP tree:

source build/envsetup.sh
lunch <your_target>-userdebug  # e.g., aosp_arm64-trunk_staging-userdebug
m libVkLayer_sample_ol libVkLayer_sample_pl

2. Installing the Layers

To install the OL and PL layers to their official locations, you need a userdebug or eng build to gain root access and remount the partitions as read-write.

# 1. Gain root access
adb root

# 2. Remount partitions as read-write (disables verity and sets up overlayfs)
adb remount

# 3. Reboot to apply overlayfs changes (if prompted by remount)
adb reboot
adb wait-for-device
adb root
adb remount

# 4. Create the target directories if they do not exist
adb shell mkdir -p /system/lib64/vulkan /product/lib64/vulkan

# 5. Push the Platform Layer (PL) to the system partition
adb push out/target/product/<your_device>/system/lib64/vulkan/libVkLayer_sample_pl.so /system/lib64/vulkan/

# 6. Push the OEM Layer (OL) to the product partition
adb push out/target/product/<your_device>/product/lib64/vulkan/libVkLayer_sample_ol.so /product/lib64/vulkan/

3. Enabling the Layers

OPLs are implicitly loaded at boot. The Vulkan loader reads specific read-only (ro.) system properties. Important: The property value must be the full library filename (e.g., libVkLayer_*.so).

To enable the Platform Layer (PL):

adb shell "echo 'ro.vulkan.layers.platform=libVkLayer_sample_pl.so' >> /system/build.prop"

To enable the OEM Layer (OL):

adb shell "echo 'ro.vulkan.layers.oem=libVkLayer_sample_ol.so' >> /product/build.prop"

Apply Changes: After modifying build.prop, you must reboot the device for the properties to be read during boot:

adb reboot

Note: Alternatively, if the ro. property is not yet set, you can set it directly and restart the Android framework to force Zygote to reload without a full reboot:

adb shell setprop ro.vulkan.layers.platform libVkLayer_sample_pl.so
adb shell setprop ro.vulkan.layers.oem libVkLayer_sample_ol.so
adb shell stop && adb shell start

Verify Properties: Once the device has rebooted, you can verify that the system properties are correctly set by running:

adb shell getprop ro.vulkan.layers.platform
adb shell getprop ro.vulkan.layers.oem

These commands should output the library filenames you configured (e.g., libVkLayer_sample_pl.so and libVkLayer_sample_ol.so).

4. Running a Workload and Verifying

  1. Prepare your logcat to monitor the unique tags for each layer:
adb logcat -s VkSampleLayer_OEM VkSampleLayer_Platform
  1. Force-stop and launch your target Vulkan application:
adb shell am force-stop <target_app_package>
adb shell monkey -p <target_app_package> -c android.intent.category.LAUNCHER 1
  1. In your logcat output, you should see confirmation that the layers successfully hooked into the instance creation:
I VkSampleLayer_Platform: PLATFORM_LAYER: Instance initialized successfully.
I VkSampleLayer_OEM: OEM_LAYER: Instance initialized successfully.