|
| 1 | +package net.mullvad.mullvadvpn.lib.map.internal |
| 2 | + |
| 3 | +import android.content.res.Resources |
| 4 | +import android.opengl.GLES20 |
| 5 | +import android.opengl.GLSurfaceView |
| 6 | +import android.opengl.Matrix |
| 7 | +import androidx.collection.LruCache |
| 8 | +import javax.microedition.khronos.egl.EGLConfig |
| 9 | +import javax.microedition.khronos.opengles.GL10 |
| 10 | +import kotlin.math.tan |
| 11 | +import net.mullvad.mullvadvpn.lib.map.data.CameraPosition |
| 12 | +import net.mullvad.mullvadvpn.lib.map.data.LocationMarkerColors |
| 13 | +import net.mullvad.mullvadvpn.lib.map.data.MapViewState |
| 14 | +import net.mullvad.mullvadvpn.lib.map.internal.shapes.Globe |
| 15 | +import net.mullvad.mullvadvpn.lib.map.internal.shapes.LocationMarker |
| 16 | +import net.mullvad.mullvadvpn.model.COMPLETE_ANGLE |
| 17 | + |
| 18 | +internal class MapGLRenderer(private val resources: Resources) : GLSurfaceView.Renderer { |
| 19 | + |
| 20 | + private lateinit var globe: Globe |
| 21 | + |
| 22 | + // Due to location markers themselves containing colors we cache them to avoid recreating them |
| 23 | + // for every draw call. |
| 24 | + private val markerCache: LruCache<LocationMarkerColors, LocationMarker> = |
| 25 | + object : LruCache<LocationMarkerColors, LocationMarker>(100) { |
| 26 | + override fun entryRemoved( |
| 27 | + evicted: Boolean, |
| 28 | + key: LocationMarkerColors, |
| 29 | + oldValue: LocationMarker, |
| 30 | + newValue: LocationMarker? |
| 31 | + ) { |
| 32 | + oldValue.onRemove() |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + private lateinit var viewState: MapViewState |
| 37 | + |
| 38 | + override fun onSurfaceCreated(unused: GL10, config: EGLConfig) { |
| 39 | + globe = Globe(resources) |
| 40 | + markerCache.evictAll() |
| 41 | + initGLOptions() |
| 42 | + } |
| 43 | + |
| 44 | + private fun initGLOptions() { |
| 45 | + // Enable cull face (To not draw the backside of triangles) |
| 46 | + GLES20.glEnable(GLES20.GL_CULL_FACE) |
| 47 | + GLES20.glCullFace(GLES20.GL_BACK) |
| 48 | + |
| 49 | + // Enable blend |
| 50 | + GLES20.glEnable(GLES20.GL_BLEND) |
| 51 | + GLES20.glBlendFunc(GLES20.GL_SRC_ALPHA, GLES20.GL_ONE_MINUS_SRC_ALPHA) |
| 52 | + } |
| 53 | + |
| 54 | + private val projectionMatrix = newIdentityMatrix() |
| 55 | + |
| 56 | + override fun onDrawFrame(gl10: GL10) { |
| 57 | + // Clear canvas |
| 58 | + clear() |
| 59 | + |
| 60 | + val viewMatrix = newIdentityMatrix() |
| 61 | + |
| 62 | + // Adjust zoom & vertical bias |
| 63 | + val yOffset = toOffsetY(viewState.cameraPosition) |
| 64 | + Matrix.translateM(viewMatrix, 0, 0f, yOffset, -viewState.cameraPosition.zoom) |
| 65 | + |
| 66 | + // Rotate to match the camera position |
| 67 | + Matrix.rotateM(viewMatrix, 0, viewState.cameraPosition.latLong.latitude.value, 1f, 0f, 0f) |
| 68 | + Matrix.rotateM(viewMatrix, 0, viewState.cameraPosition.latLong.longitude.value, 0f, -1f, 0f) |
| 69 | + |
| 70 | + globe.draw(projectionMatrix, viewMatrix, viewState.globeColors) |
| 71 | + |
| 72 | + // Draw location markers |
| 73 | + viewState.locationMarker.forEach { |
| 74 | + val marker = |
| 75 | + markerCache[it.colors] |
| 76 | + ?: LocationMarker(it.colors).also { markerCache.put(it.colors, it) } |
| 77 | + |
| 78 | + marker.draw(projectionMatrix, viewMatrix, it.latLong, it.size) |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + private fun Float.toRadians() = this * Math.PI.toFloat() / (COMPLETE_ANGLE / 2) |
| 83 | + |
| 84 | + private fun toOffsetY(cameraPosition: CameraPosition): Float { |
| 85 | + val percent = cameraPosition.verticalBias |
| 86 | + val z = cameraPosition.zoom - 1f |
| 87 | + // Calculate the size of the plane at the current z position |
| 88 | + val planeSizeY = tan(FIELD_OF_VIEW.toRadians() / 2f) * z * 2f |
| 89 | + |
| 90 | + // Calculate the start of the plane |
| 91 | + val planeStartY = planeSizeY / 2f |
| 92 | + |
| 93 | + // Return offset based on the bias |
| 94 | + return planeStartY - planeSizeY * percent |
| 95 | + } |
| 96 | + |
| 97 | + private fun clear() { |
| 98 | + // Redraw background color |
| 99 | + GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f) |
| 100 | + GLES20.glClearDepthf(1.0f) |
| 101 | + GLES20.glEnable(GLES20.GL_DEPTH_TEST) |
| 102 | + GLES20.glDepthFunc(GLES20.GL_LEQUAL) |
| 103 | + |
| 104 | + GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT or GLES20.GL_DEPTH_BUFFER_BIT) |
| 105 | + } |
| 106 | + |
| 107 | + override fun onSurfaceChanged(unused: GL10, width: Int, height: Int) { |
| 108 | + GLES20.glViewport(0, 0, width, height) |
| 109 | + |
| 110 | + val ratio: Float = width.toFloat() / height.toFloat() |
| 111 | + |
| 112 | + Matrix.perspectiveM( |
| 113 | + projectionMatrix, |
| 114 | + 0, |
| 115 | + FIELD_OF_VIEW, |
| 116 | + if (ratio.isFinite()) ratio else 1f, |
| 117 | + PERSPECTIVE_Z_NEAR, |
| 118 | + PERSPECTIVE_Z_FAR |
| 119 | + ) |
| 120 | + } |
| 121 | + |
| 122 | + fun setViewState(viewState: MapViewState) { |
| 123 | + this.viewState = viewState |
| 124 | + } |
| 125 | + |
| 126 | + companion object { |
| 127 | + private const val PERSPECTIVE_Z_NEAR = 0.05f |
| 128 | + private const val PERSPECTIVE_Z_FAR = 10f |
| 129 | + private const val FIELD_OF_VIEW = 70f |
| 130 | + } |
| 131 | +} |
0 commit comments