From f8b4e8684d9749b3fec11527517be8aca55b61c3 Mon Sep 17 00:00:00 2001 From: Dennis BORDET Date: Wed, 27 Dec 2023 16:06:31 +0100 Subject: [PATCH 1/2] fix: map rendering issue Copy from [commit on google maps library](https://github.com/flutter/packages/commit/e393d452beaf4b216e2567bc2cee0225087f4662) --- .../mapbox/mapboxgl/MapboxMapController.java | 69 +++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/android/src/main/java/com/mapbox/mapboxgl/MapboxMapController.java b/android/src/main/java/com/mapbox/mapboxgl/MapboxMapController.java index 7e47d5021..a1e966094 100644 --- a/android/src/main/java/com/mapbox/mapboxgl/MapboxMapController.java +++ b/android/src/main/java/com/mapbox/mapboxgl/MapboxMapController.java @@ -13,6 +13,8 @@ import android.graphics.BitmapFactory; import android.graphics.PointF; import android.graphics.RectF; +import android.graphics.SurfaceTexture; +import android.view.TextureView.SurfaceTextureListener; import android.location.Location; import android.os.Build; import android.util.DisplayMetrics; @@ -21,6 +23,7 @@ import android.view.MotionEvent; import android.view.TextureView; import android.view.View; +import android.view.ViewGroup; import android.widget.FrameLayout; import androidx.annotation.NonNull; @@ -221,6 +224,7 @@ private CameraPosition getCameraPosition() { @Override public void onMapReady(MapboxMap mapboxMap) { this.mapboxMap = mapboxMap; + installInvalidator(); if (mapReadyResult != null) { mapReadyResult.success(null); mapReadyResult = null; @@ -256,6 +260,71 @@ public boolean onTouch(View v, MotionEvent event) { setStyleString(styleStringInitial); } + // Returns the first TextureView found in the view hierarchy. + private static TextureView findTextureView(ViewGroup group) { + final int n = group.getChildCount(); + for (int i = 0; i < n; i++) { + View view = group.getChildAt(i); + if (view instanceof TextureView) { + return (TextureView) view; + } + if (view instanceof ViewGroup) { + TextureView r = findTextureView((ViewGroup) view); + if (r != null) { + return r; + } + } + } + return null; + } + + private void installInvalidator() { + if (mapView == null) { + // This should only happen in tests. + return; + } + TextureView textureView = findTextureView(mapView); + if (textureView == null) { + Log.i(TAG, "No TextureView found. Likely using the LEGACY renderer."); + return; + } + Log.i(TAG, "Installing custom TextureView driven invalidator."); + SurfaceTextureListener internalListener = textureView.getSurfaceTextureListener(); + // Override the Maps internal SurfaceTextureListener with our own. Our listener + // mostly just invokes the internal listener callbacks but in onSurfaceTextureUpdated + // the mapView is invalidated which ensures that all map updates are presented to the + // screen. + final MapView mapView = this.mapView; + textureView.setSurfaceTextureListener( + new TextureView.SurfaceTextureListener() { + public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) { + if (internalListener != null) { + internalListener.onSurfaceTextureAvailable(surface, width, height); + } + } + + public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) { + if (internalListener != null) { + return internalListener.onSurfaceTextureDestroyed(surface); + } + return true; + } + + public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) { + if (internalListener != null) { + internalListener.onSurfaceTextureSizeChanged(surface, width, height); + } + } + + public void onSurfaceTextureUpdated(SurfaceTexture surface) { + if (internalListener != null) { + internalListener.onSurfaceTextureUpdated(surface); + } + mapView.invalidate(); + } + }); + } + @Override public void setStyleString(@NonNull String styleString) { // clear old layer id from the location Component From 65b19beafa59ee70882de045fb720a95cf8e9110 Mon Sep 17 00:00:00 2001 From: Dennis BORDET Date: Wed, 27 Dec 2023 16:07:39 +0100 Subject: [PATCH 2/2] fix: invalidate does not always works PostInvalidate ensures invalidation to run on UI thread --- .../src/main/java/com/mapbox/mapboxgl/MapboxMapController.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/android/src/main/java/com/mapbox/mapboxgl/MapboxMapController.java b/android/src/main/java/com/mapbox/mapboxgl/MapboxMapController.java index a1e966094..6bfc52270 100644 --- a/android/src/main/java/com/mapbox/mapboxgl/MapboxMapController.java +++ b/android/src/main/java/com/mapbox/mapboxgl/MapboxMapController.java @@ -320,7 +320,7 @@ public void onSurfaceTextureUpdated(SurfaceTexture surface) { if (internalListener != null) { internalListener.onSurfaceTextureUpdated(surface); } - mapView.invalidate(); + mapView.postInvalidate(); } }); }