summaryrefslogtreecommitdiffstats
path: root/platform/android/java/lib/src/org/godotengine/godot/vulkan/VkSurfaceView.kt
blob: 7e451067e5a3869bbe33152618e08a47e948d95f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/**************************************************************************/
/*  VkSurfaceView.kt                                                      */
/**************************************************************************/
/*                         This file is part of:                          */
/*                             GODOT ENGINE                               */
/*                        https://godotengine.org                         */
/**************************************************************************/
/* Copyright (c) 2024-present Redot Engine contributors                   */
/*                                          (see REDOT_AUTHORS.md)        */
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur.                  */
/*                                                                        */
/* Permission is hereby granted, free of charge, to any person obtaining  */
/* a copy of this software and associated documentation files (the        */
/* "Software"), to deal in the Software without restriction, including    */
/* without limitation the rights to use, copy, modify, merge, publish,    */
/* distribute, sublicense, and/or sell copies of the Software, and to     */
/* permit persons to whom the Software is furnished to do so, subject to  */
/* the following conditions:                                              */
/*                                                                        */
/* The above copyright notice and this permission notice shall be         */
/* included in all copies or substantial portions of the Software.        */
/*                                                                        */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,        */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF     */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY   */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,   */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE      */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                 */
/**************************************************************************/

@file:JvmName("VkSurfaceView")
package org.godotengine.godot.vulkan

import android.content.Context
import android.view.SurfaceHolder
import android.view.SurfaceView

/**
 * An implementation of SurfaceView that uses the dedicated surface for
 * displaying Vulkan rendering.
 * <p>
 * A [VkSurfaceView] provides the following features:
 * <p>
 * <ul>
 * <li>Manages a surface, which is a special piece of memory that can be
 * composited into the Android view system.
 * <li>Accepts a user-provided [VkRenderer] object that does the actual rendering.
 * <li>Renders on a dedicated [VkThread] thread to decouple rendering performance from the
 * UI thread.
 * </ul>
 */
open internal class VkSurfaceView(context: Context) : SurfaceView(context), SurfaceHolder.Callback {
	companion object {
		fun checkState(expression: Boolean, errorMessage: Any) {
			check(expression) { errorMessage.toString() }
		}
	}

	/**
	 * Thread used to drive the vulkan logic.
	 */
	private val vkThread: VkThread by lazy {
		VkThread(this, renderer)
	}

	/**
	 * Performs the actual rendering.
	 */
	private lateinit var renderer: VkRenderer

	init {
		isClickable = true
		holder.addCallback(this)
	}

	/**
	 * Set the [VkRenderer] associated with the view, and starts the thread that will drive the vulkan
	 * rendering.
	 *
	 * This method should be called once and only once in the life-cycle of [VkSurfaceView].
	 */
	fun startRenderer(renderer: VkRenderer) {
		checkState(!this::renderer.isInitialized, "startRenderer must only be invoked once")
		this.renderer = renderer
		vkThread.start()
	}

	/**
	 * Queues a runnable to be run on the Vulkan rendering thread.
	 *
	 * Must not be called before a [VkRenderer] has been set.
	 */
	fun queueOnVkThread(runnable: Runnable) {
		vkThread.queueEvent(runnable)
	}

	/**
	 * Resumes the rendering thread.
	 *
	 * Must not be called before a [VkRenderer] has been set.
	 */
	protected fun resumeRenderThread() {
		vkThread.onResume()
	}

	/**
	 * Pauses the rendering thread.
	 *
	 * Must not be called before a [VkRenderer] has been set.
	 */
	protected fun pauseRenderThread() {
		vkThread.onPause()
	}

	/**
	 * Requests the render thread to exit and block until it does.
	 */
	fun requestRenderThreadExitAndWait() {
		vkThread.requestExitAndWait()
	}

	override fun surfaceChanged(holder: SurfaceHolder, format: Int, width: Int, height: Int) {
		vkThread.onSurfaceChanged(width, height)
	}

	override fun surfaceDestroyed(holder: SurfaceHolder) {
		vkThread.onSurfaceDestroyed()
	}

	override fun surfaceCreated(holder: SurfaceHolder) {
		vkThread.onSurfaceCreated()
	}
}