Skip to content

Commit

Permalink
Try HDR
Browse files Browse the repository at this point in the history
  • Loading branch information
MarijnS95 committed Nov 8, 2023
1 parent 6eae641 commit 944007c
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 8 deletions.
3 changes: 3 additions & 0 deletions android_native_surface/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,6 @@ rustix = { version = "0.38", default-features = false, features = ["std", "pipe"

[build-dependencies]
gl_generator = "0.14"

[patch.crates-io]
glutin = { path = "../../glutin/glutin" }
11 changes: 11 additions & 0 deletions android_native_surface/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::{
};

use glutin::{
config::ConfigTemplate,
context::{ContextApi, ContextAttributesBuilder},
prelude::*,
};
Expand All @@ -29,11 +30,20 @@ fn render_to_native_window(window: NativeWindow) {
let gl_display = support::create_display(raw_display_handle);

let template = support::config_template(raw_window_handle);
dbg!(unsafe {
gl_display
.find_configs(ConfigTemplate::default())
.unwrap()
.collect::<Vec<_>>()
});
let config = unsafe {
gl_display
.find_configs(template)
.unwrap()
.reduce(|accum, config| {
dbg!(config.float_pixels());
dbg!(config.color_space());
dbg!(config.color_buffer_type());
// Find the config with the maximum number of samples.
//
// In general if you're not sure what you want in template you can request or
Expand All @@ -52,6 +62,7 @@ fn render_to_native_window(window: NativeWindow) {
};

println!("Picked a config with {} samples", config.num_samples());
dbg!(config.color_buffer_type());
// Create a wrapper for GL window and surface.
let gl_window = support::GlWindow::from_existing(&gl_display, window, &config);

Expand Down
19 changes: 12 additions & 7 deletions android_native_surface/src/support.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use glutin::{
config::{Config, ConfigSurfaceTypes, ConfigTemplate, ConfigTemplateBuilder},
display::{Display, DisplayApiPreference},
prelude::*,
surface::{Surface, SurfaceAttributes, SurfaceAttributesBuilder, WindowSurface},
surface::{ColorSpace, Surface, SurfaceAttributes, SurfaceAttributesBuilder, WindowSurface},
};
use ndk::native_window::NativeWindow;
use raw_window_handle::{HasRawWindowHandle, RawDisplayHandle, RawWindowHandle};
Expand All @@ -34,14 +34,16 @@ impl GlWindow {
pub fn from_existing(display: &Display, window: NativeWindow, config: &Config) -> Self {
let attrs = surface_attributes(&window);
let surface = unsafe { display.create_window_surface(config, &attrs).unwrap() };
dbg!(surface.color_space());
Self { window, surface }
}
}

/// Create template to find OpenGL config.
pub fn config_template(raw_window_handle: RawWindowHandle) -> ConfigTemplate {
let builder = ConfigTemplateBuilder::new()
.with_alpha_size(8)
// .with_alpha_size(8)
// .with_float_pixels(true)
.compatible_with_native_window(raw_window_handle)
.with_surface_type(ConfigSurfaceTypes::WINDOW);

Expand All @@ -51,11 +53,14 @@ pub fn config_template(raw_window_handle: RawWindowHandle) -> ConfigTemplate {
/// Create surface attributes for window surface.
pub fn surface_attributes(window: &NativeWindow) -> SurfaceAttributes<WindowSurface> {
let raw_window_handle = window.raw_window_handle();
SurfaceAttributesBuilder::<WindowSurface>::new().build(
raw_window_handle,
NonZeroU32::new(window.width().try_into().unwrap()).unwrap(),
NonZeroU32::new(window.height().try_into().unwrap()).unwrap(),
)
SurfaceAttributesBuilder::<WindowSurface>::new()
// .with_srgb(Some(true))
.with_color_space(Some(ColorSpace::ScrgbLinear))
.build(
raw_window_handle,
NonZeroU32::new(window.width().try_into().unwrap()).unwrap(),
NonZeroU32::new(window.height().try_into().unwrap()).unwrap(),
)
}

/// Create the display.
Expand Down
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ android {

defaultConfig {
applicationId "rust.androidnativesurface"
minSdk 28 // Should remain in sync with the ndk api-level-xx feature in Rust
minSdk 33 // Should remain in sync with the ndk api-level-xx feature in Rust
targetSdk 34
versionCode 1
versionName "1.0"
Expand Down
47 changes: 47 additions & 0 deletions app/src/main/java/rust/androidnativesurface/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@ package rust.androidnativesurface

import android.app.Activity
import android.graphics.SurfaceTexture
import android.hardware.DataSpace
import android.hardware.display.DisplayManager
import android.os.Bundle
import android.view.Display.HdrCapabilities
import android.view.Surface
import android.view.SurfaceControl
import android.view.SurfaceHolder
import android.view.SurfaceView
import android.view.TextureView
import android.view.WindowManager

class MainActivity : Activity() {
companion object {
Expand All @@ -24,12 +29,54 @@ class MainActivity : Activity() {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

val display = display!!
println("Display ${display}")
println("Display ${display.displayId}")
println("Display ${display.isHdr}")
// println("Display ${display.hdrSdrRatio}")
// println("Display ${display.isHdrSdrRatioAvailable}")
println("Display ${display.hdrCapabilities}")
println("Display ${display.hdrCapabilities.desiredMaxAverageLuminance}")
println("Display ${display.hdrCapabilities.desiredMinLuminance}")
println("Display ${display.hdrCapabilities.desiredMaxLuminance}")
display.hdrCapabilities.supportedHdrTypes.forEach {
val name = if (it == HdrCapabilities.HDR_TYPE_DOLBY_VISION) {
"HDR_TYPE_DOLBY_VISION"
} else if (it == HdrCapabilities.HDR_TYPE_HDR10) {
"HDR_TYPE_HDR10"
} else if (it == HdrCapabilities.HDR_TYPE_HDR10_PLUS) {
"HDR_TYPE_HDR10_PLUS"
} else if (it == HdrCapabilities.HDR_TYPE_HLG) {
"HDR_TYPE_HLG"
} else if (it == HdrCapabilities.HDR_TYPE_INVALID) {
"HDR_TYPE_INVALID"
} else {
}

println(name)
}

// println("${display.mode.supportedHdrTypes}")
var dm = getSystemService(DisplayManager::class.java)
// println(dm.hdrConversionMode)

val surfaceView: SurfaceView = findViewById(R.id.surface_view)
println("SurfaceView: ${surfaceView.holder.surface}")
surfaceView.holder.addCallback(object : SurfaceHolder.Callback {
override fun surfaceCreated(holder: SurfaceHolder) {
println("SurfaceView created: ${holder.surface}")

SurfaceControl.Transaction()
// .setExtendedRangeBrightness(surfaceView.surfaceControl, 10.0f, 10.0f)
.setDataSpace(surfaceView.surfaceControl, DataSpace.DATASPACE_DISPLAY_P3)
.apply()

renderToSurface(holder.surface)

SurfaceControl.Transaction()
// .setExtendedRangeBrightness(surfaceView.surfaceControl, 10.0f, 10.0f)
.setDataSpace(surfaceView.surfaceControl, DataSpace.DATASPACE_DISPLAY_P3)
.apply()
}

override fun surfaceChanged(holder: SurfaceHolder, p1: Int, p2: Int, p3: Int) {
Expand Down

0 comments on commit 944007c

Please sign in to comment.