Skip to content

Commit

Permalink
Expose RENDERMODE_CONTINUOUSLY and RENDERMODE_WHEN_DIRTY
Browse files Browse the repository at this point in the history
  • Loading branch information
alasram committed Sep 11, 2024
1 parent 9caab9b commit 583ace9
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,28 @@ public void setMaximumFps(int maximumFps) {
}
}

/**
* Set the rendering mode. When renderMode is
* RENDERMODE_CONTINUOUSLY, the renderer is called
* repeatedly to re-render the scene. When renderMode
* is RENDERMODE_WHEN_DIRTY, the renderer only rendered when the surface
* is created, or when {@link #requestRender} is called. Defaults to RENDERMODE_WHEN_DIRTY.
* <p>
* Using RENDERMODE_WHEN_DIRTY can improve battery life and overall system performance
* by allowing the GPU and CPU to idle when the view does not need to be updated.
* <p>
* This method only affects GLSurfaceView. It will ignored when a TextureView surface is used
*
* @param renderMode one of the RENDERMODE_X constants
*/
public void setRenderMode(int renderMode) {
if (mapRenderer != null) {
mapRenderer.setRenderMode(renderMode);
} else {
throw new IllegalStateException("Calling MapView#setRenderMode before mapRenderer is created.");
}
}

/**
* Returns if the map has been destroyed.
* <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ public void onDestroy() {
// Implement if needed
}

public void setRenderMode(int renderMode) {
// Implement if needed
}

public void setOnFpsChangedListener(MapLibreMap.OnFpsChangedListener listener) {
onFpsChangedListener = listener;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public void setRenderer(SurfaceViewMapRenderer renderer) {
* RENDERMODE_CONTINUOUSLY, the renderer is called
* repeatedly to re-render the scene. When renderMode
* is RENDERMODE_WHEN_DIRTY, the renderer only rendered when the surface
* is created, or when {@link #requestRender} is called. Defaults to RENDERMODE_CONTINUOUSLY.
* is created, or when {@link #requestRender} is called. Defaults to RENDERMODE_WHEN_DIRTY.
* <p>
* Using RENDERMODE_WHEN_DIRTY can improve battery life and overall system performance
* by allowing the GPU and CPU to idle when the view does not need to be updated.
Expand Down Expand Up @@ -249,12 +249,12 @@ public void waitForEmpty() {
protected void onAttachedToWindow() {
super.onAttachedToWindow();
if (detached && (renderer != null)) {
int renderMode = RENDERMODE_CONTINUOUSLY;
int renderMode = RENDERMODE_WHEN_DIRTY;
if (renderThread != null) {
renderMode = renderThread.getRenderMode();
}
createRenderThread();
if (renderMode != RENDERMODE_CONTINUOUSLY) {
if (renderMode != RENDERMODE_WHEN_DIRTY) {
renderThread.setRenderMode(renderMode);
}
renderThread.start();
Expand Down Expand Up @@ -289,7 +289,7 @@ abstract static class RenderThread extends Thread {
width = 0;
height = 0;
requestRender = true;
renderMode = RENDERMODE_CONTINUOUSLY;
renderMode = RENDERMODE_WHEN_DIRTY;
wantRenderNotification = false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,10 @@ public void queueEvent(Runnable runnable) {
public void waitForEmpty() {
surfaceView.waitForEmpty();
}

/**
* {@inheritDoc}
*/
@Override
public void setRenderMode(int renderMode) { surfaceView.setRenderMode(renderMode); }
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import org.maplibre.android.maps.renderer.egl.EGLContextFactory;
import org.maplibre.android.maps.renderer.egl.EGLWindowSurfaceFactory;

import static android.opengl.GLSurfaceView.RENDERMODE_WHEN_DIRTY;
import static org.maplibre.android.maps.renderer.surfaceview.MapLibreSurfaceView.RENDERMODE_WHEN_DIRTY;

public class GLSurfaceViewMapRenderer extends SurfaceViewMapRenderer {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import com.google.android.material.floatingactionbutton.FloatingActionButton
import org.maplibre.android.maps.*
import org.maplibre.android.maps.MapLibreMap.OnCameraMoveListener
import org.maplibre.android.maps.MapLibreMap.OnFpsChangedListener
import org.maplibre.android.maps.renderer.surfaceview.MapLibreSurfaceView.RENDERMODE_CONTINUOUSLY
import org.maplibre.android.maps.renderer.surfaceview.MapLibreSurfaceView.RENDERMODE_WHEN_DIRTY
import org.maplibre.android.style.layers.Layer
import org.maplibre.android.style.layers.Property
import org.maplibre.android.style.layers.PropertyFactory
Expand All @@ -29,6 +31,7 @@ open class DebugModeActivity : AppCompatActivity(), OnMapReadyCallback, OnFpsCha
private var actionBarDrawerToggle: ActionBarDrawerToggle? = null
private var currentStyleIndex = 0
private var isReportFps = true
private var isContinuousRendering = false
private var fpsView: TextView? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand Down Expand Up @@ -167,6 +170,13 @@ open class DebugModeActivity : AppCompatActivity(), OnMapReadyCallback, OnFpsCha
mapView.setMaximumFps(30)
} else if (itemId == R.id.menu_action_limit_to_60_fps) {
mapView.setMaximumFps(60)
} else if (itemId == R.id.menu_action_toggle_continuous_rendering) {
isContinuousRendering = !isContinuousRendering
if (isContinuousRendering) {
mapView.setRenderMode(RENDERMODE_CONTINUOUSLY)
} else {
mapView.setRenderMode(RENDERMODE_WHEN_DIRTY)
}
}
return actionBarDrawerToggle!!.onOptionsItemSelected(item) || super.onOptionsItemSelected(
item
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,9 @@
android:id="@+id/menu_action_limit_to_60_fps"
android:title="Limit FPS to 60"
app:showAsAction="never" />
<item
android:id="@+id/menu_action_toggle_continuous_rendering"
android:title="Toggle continuous rendering"
app:showAsAction="never" />

</menu>

0 comments on commit 583ace9

Please sign in to comment.