我正在尝试使用SurfaceView
以原始分辨率在4K Android TV设备(运行Android 9的Nvidia Shield TV)上渲染3840x2160位图。
根据Android 6 release notes,我应该能够将preferredDisplayModeId
设置为支持4K分辨率的Display.Mode
,以便使我的SurfaceView
以4K渲染。但是,看来即使设置了preferredDisplayModeId
,我视图中提供给Canvas
方法的onDraw
也仅为1920x1080。尝试在其上绘制3840x2160位图会导致图像缩小到1080p,然后再放大回到2160p,在此过程中丢失信息。
这是我的Activity
中的相关代码:
windowManager.defaultDisplay.supportedModes.forEach {
if (it.physicalHeight == 2160 && it.refreshRate > 60) {
val params = window.attributes
params.preferredDisplayModeId = it.modeId
window.attributes = params
}
}
setContentView(R.layout.main)
我的布局包含扩展了SurfaceView
的自定义视图。自定义视图具有onDraw
方法,如下所示:
override fun onDraw(canvas: Canvas) {
canvas.drawBitmap(
currentImage,
Rect(0, 0, currentImage!!.width, currentImage!!.height),
canvas.clipBounds,
null
)
}
[canvas.clipBounds
是Rect(0, 0 - 1920, 1080)
,因此当它尝试呈现完整的位图时,必须将其缩小以适合。
[渲染我的adb shell dumpsys SurfaceFlinger
时,这是SurfaceView
的一些输出:
Display 0 HWC layers:
-------------------------------------------------------------------------------
Layer name
Z | Comp Type | Disp Frame (LTRB) | Source Crop (LTRB)
-------------------------------------------------------------------------------
com.google.android.tvlauncher/com.google.android.tvlauncher.MainActivity#0
rel 0 | Device | 0 0 3840 2160 | 0.0 0.0 1920.0 1080.0
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Dim Layer for - Task=580#0
rel -1 | Client | 0 0 3840 2160 | 0.0 0.0 -1.0 -1.0
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
com.android.tv.settings/com.android.tv.settings.MainSettings#0
rel 0 | Client | 0 0 3840 2160 | 0.0 0.0 1920.0 1080.0
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Background for -SurfaceView - Sys2023:dream#0
rel -3 | Client | 0 0 3840 2160 | 0.0 0.0 -1.0 -1.0
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Sys2023:dream#0
rel 0 | Client | 0 0 3840 2160 | 0.0 0.0 1920.0 1080.0
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Allocated buffers:
0x513aff00: 8100.00 KiB | 1920 (1920) x 1080 | 1 | 1 | 0xb00 | Sys2023:dream#0
0x5333f300: 4.00 KiB | 16 ( 64) x 16 | 1 | 1 | 0x1a00 | FakeFramebuffer
0x53340f80: 8100.00 KiB | 1920 (1920) x 1080 | 1 | 1 | 0xb00 | com.android.tv.settings/com.android.tv.settings.MainSettings#0
0x53341280: 32400.00 KiB | 3840 (3840) x 2160 | 1 | 1 | 0x1b00 | FramebufferSurface
0x53341700: 8100.00 KiB | 1920 (1920) x 1080 | 1 | 1 | 0xb00 | com.google.android.tvlauncher/com.google.android.tvlauncher.MainActivity#0
0x53341880: 8100.00 KiB | 1920 (1920) x 1080 | 1 | 1 | 0xb00 | com.google.android.tvlauncher/com.google.android.tvlauncher.MainActivity#0
0x53341d00: 8100.00 KiB | 1920 (1920) x 1080 | 1 | 1 | 0xb00 | com.android.tv.settings/com.android.tv.settings.MainSettings#0
0x53341e80: 8100.00 KiB | 1920 (1920) x 1080 | 1 | 1 | 0xb00 | com.google.android.tvlauncher/com.google.android.tvlauncher.MainActivity#0
0x55d35480: 8100.00 KiB | 1920 (1920) x 1080 | 1 | 1 | 0xb00 | com.android.systemui.ImageWallpaper#0
0x55d35900: 8100.00 KiB | 1920 (1920) x 1080 | 1 | 1 | 0xb00 | com.android.tv.settings/com.android.tv.settings.MainSettings#0
0x55d35d80: 8100.00 KiB | 1920 (1920) x 1080 | 1 | 1 | 0xb00 | Sys2023:dream#0
0x55d37580: 32400.00 KiB | 3840 (3840) x 2160 | 1 | 1 | 0x1b00 | FramebufferSurface
Total allocated (estimate): 137704.00 KB
为了进行比较,当我的应用程序渲染ExoPlayer
视图时,这是相同的输出,它正确地渲染了4K SurfaceView
:
Display 0 HWC layers:
-------------------------------------------------------------------------------
Layer name
Z | Comp Type | Disp Frame (LTRB) | Source Crop (LTRB)
-------------------------------------------------------------------------------
SurfaceView - Sys2023:dream#0
rel -2 | Device | 0 0 3840 2160 | 0.0 0.0 3840.0 2160.0
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Sys2023:dream#0
rel 0 | Device | 0 0 3840 2160 | 0.0 0.0 1920.0 1080.0
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Allocated buffers:
0x513af180: unknown | 3840 (3840) x 2160 | 1 | 12B | 0x900 | SurfaceView - Sys2023:dream#0
0x513af300: unknown | 3840 (3840) x 2160 | 1 | 12B | 0x900 | SurfaceView - Sys2023:dream#0
0x513afc00: unknown | 3840 (3840) x 2160 | 1 | 12B | 0x900 | SurfaceView - Sys2023:dream#0
0x513afd80: 8100.00 KiB | 1920 (1920) x 1080 | 1 | 1 | 0xb00 | Sys2023:dream#0
0x5333f300: 4.00 KiB | 16 ( 64) x 16 | 1 | 1 | 0x1a00 | FakeFramebuffer
0x53340f80: 8100.00 KiB | 1920 (1920) x 1080 | 1 | 1 | 0xb00 | com.android.tv.settings/com.android.tv.settings.MainSettings#0
0x53341280: unknown | 3840 (3840) x 2160 | 1 | 12B | 0x900 | SurfaceView - Sys2023:dream#0
0x53341400: unknown | 3840 (3840) x 2160 | 1 | 12B | 0x900 | SurfaceView - Sys2023:dream#0
0x53341700: 8100.00 KiB | 1920 (1920) x 1080 | 1 | 1 | 0xb00 | com.google.android.tvlauncher/com.google.android.tvlauncher.MainActivity#0
0x53341880: 8100.00 KiB | 1920 (1920) x 1080 | 1 | 1 | 0xb00 | com.google.android.tvlauncher/com.google.android.tvlauncher.MainActivity#0
0x53341a00: 8100.00 KiB | 1920 (1920) x 1080 | 1 | 1 | 0xb00 | Sys2023:dream#0
0x53341d00: 8100.00 KiB | 1920 (1920) x 1080 | 1 | 1 | 0xb00 | com.android.tv.settings/com.android.tv.settings.MainSettings#0
0x53341e80: 8100.00 KiB | 1920 (1920) x 1080 | 1 | 1 | 0xb00 | com.google.android.tvlauncher/com.google.android.tvlauncher.MainActivity#0
0x55d35480: 8100.00 KiB | 1920 (1920) x 1080 | 1 | 1 | 0xb00 | com.android.systemui.ImageWallpaper#0
0x55d35900: 8100.00 KiB | 1920 (1920) x 1080 | 1 | 1 | 0xb00 | com.android.tv.settings/com.android.tv.settings.MainSettings#0
0x55d35f00: unknown | 3840 (3840) x 2160 | 1 | 12B | 0x900 | SurfaceView - Sys2023:dream#0
0x55d36080: unknown | 3840 (3840) x 2160 | 1 | 12B | 0x900 | SurfaceView - Sys2023:dream#0
0x55d36200: unknown | 3840 (3840) x 2160 | 1 | 12B | 0x900 | SurfaceView - Sys2023:dream#0
0x55d36380: unknown | 3840 (3840) x 2160 | 1 | 12B | 0x900 | SurfaceView - Sys2023:dream#0
0x55d36500: unknown | 3840 (3840) x 2160 | 1 | 12B | 0x900 | SurfaceView - Sys2023:dream#0
0x55d36680: unknown | 3840 (3840) x 2160 | 1 | 12B | 0x900 | SurfaceView - Sys2023:dream#0
0x55d36800: unknown | 3840 (3840) x 2160 | 1 | 12B | 0x900 | SurfaceView - Sys2023:dream#0
0x55d36f80: unknown | 3840 (3840) x 2160 | 1 | 12B | 0x900 | SurfaceView - Sys2023:dream#0
0x55d37100: unknown | 3840 (3840) x 2160 | 1 | 12B | 0x900 | SurfaceView - Sys2023:dream#0
0x55d37280: unknown | 3840 (3840) x 2160 | 1 | 12B | 0x900 | SurfaceView - Sys2023:dream#0
Total allocated (estimate): 72904.00 KB
[您可以看到显示SurfaceView
时的ExoPlayer
缓冲区正确为3840x2160,但是使用我的自定义视图时,似乎没有分配任何SurfaceView
缓冲区。
好吧,我将开始使用ExoPlayer展示您需要的东西。应该有可能。
但是,我认为您没有正确分配画布,我想您是否正在使用包含4k的rect(args)?看来您创建的画布错误,或者是因为如果您在nvidia盾上运行并且afaik我的护盾在1080p上运行android的功能,并且如果getter请求仍然很旧,则画布将获得该大小。
在我看来,最简单的解决方案是使用ExoPlayer。