【发布时间】:2020-04-12 10:34:31
【问题描述】:
我正在尝试使用 SurfaceView 在 4K Android TV 设备(运行 Android 9 的 Nvidia Shield TV)上以原始分辨率呈现 3840x2160 位图。
根据Android 6 release notes,我应该能够将preferredDisplayModeId 设置为支持4K 分辨率的Display.Mode,以便让我的SurfaceView 以4K 渲染。但是,似乎即使设置了preferredDisplayModeId,我认为提供给onDraw方法的Canvas也只有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),所以当它尝试渲染完整的位图时,它必须缩小它以适应。
当我的SurfaceView 被渲染时,这是adb shell dumpsys SurfaceFlinger 的一些输出:
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
当显示ExoPlayer 时,您可以看到SurfaceView 缓冲区正确为3840x2160,但在使用我的自定义视图时,它似乎没有分配任何SurfaceView 缓冲区。
【问题讨论】:
标签: android android-canvas android-tv