This repository was archived by the owner on Feb 25, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6k
Add SurfaceTextureSurfaceProducer
#49653
Merged
auto-submit
merged 16 commits into
flutter:main
from
matanlurey:engine-android-SurfaceTextureSurfaceProducer
Jan 11, 2024
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
266eaae
Delint FlutterRenderer following Java best practices.
matanlurey 1f4eaf6
++
matanlurey 97824e0
WIP.
matanlurey 2e13a13
Merge.
matanlurey 741483f
++
matanlurey b01aa2a
++
matanlurey e848fa8
++
matanlurey c3160b1
Add a minimal test.
matanlurey b6bd9a0
++
matanlurey 5d72f3c
++
matanlurey 8b1965c
++
matanlurey 4b40ec4
++
matanlurey 5dcaf8a
++
matanlurey 6e28083
++
matanlurey 8222ee5
++
matanlurey e28529e
++
matanlurey File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
.../platform/android/io/flutter/embedding/engine/renderer/SurfaceTextureSurfaceProducer.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package io.flutter.embedding.engine.renderer; | ||
|
||
import android.graphics.SurfaceTexture; | ||
import android.os.Handler; | ||
import android.view.Surface; | ||
import androidx.annotation.NonNull; | ||
import androidx.annotation.Nullable; | ||
import io.flutter.embedding.engine.FlutterJNI; | ||
import io.flutter.view.TextureRegistry; | ||
|
||
/** Uses a {@link android.graphics.SurfaceTexture} to populate the texture registry. */ | ||
final class SurfaceTextureSurfaceProducer | ||
implements TextureRegistry.SurfaceProducer, TextureRegistry.GLTextureConsumer { | ||
private final long id; | ||
private int requestBufferWidth; | ||
private int requestedBufferHeight; | ||
private boolean released; | ||
@Nullable private Surface surface; | ||
@NonNull private final SurfaceTexture texture; | ||
@NonNull private final Handler handler; | ||
@NonNull private final FlutterJNI flutterJNI; | ||
|
||
SurfaceTextureSurfaceProducer(long id, @NonNull Handler handler, @NonNull FlutterJNI flutterJNI) { | ||
this.id = id; | ||
this.handler = handler; | ||
this.flutterJNI = flutterJNI; | ||
this.texture = new SurfaceTexture(0); | ||
} | ||
|
||
@Override | ||
protected void finalize() throws Throwable { | ||
try { | ||
if (released) { | ||
return; | ||
} | ||
release(); | ||
handler.post(new FlutterRenderer.TextureFinalizerRunnable(id, flutterJNI)); | ||
} finally { | ||
super.finalize(); | ||
} | ||
} | ||
|
||
@Override | ||
public long id() { | ||
return id; | ||
} | ||
|
||
@Override | ||
public void release() { | ||
texture.release(); | ||
released = true; | ||
} | ||
|
||
@Override | ||
@NonNull | ||
public SurfaceTexture getSurfaceTexture() { | ||
return texture; | ||
} | ||
|
||
@Override | ||
public void setSize(int width, int height) { | ||
requestBufferWidth = width; | ||
requestedBufferHeight = height; | ||
getSurfaceTexture().setDefaultBufferSize(width, height); | ||
} | ||
|
||
@Override | ||
public int getWidth() { | ||
return requestBufferWidth; | ||
} | ||
|
||
@Override | ||
public int getHeight() { | ||
return requestedBufferHeight; | ||
} | ||
|
||
@Override | ||
public Surface getSurface() { | ||
if (surface == null) { | ||
surface = new Surface(texture); | ||
} | ||
return surface; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
.../android/test/io/flutter/embedding/engine/renderer/SurfaceTextureSurfaceProducerTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package io.flutter.embedding.engine.renderer; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.mockito.Mockito.mock; | ||
import static org.robolectric.Shadows.shadowOf; | ||
|
||
import android.annotation.TargetApi; | ||
import android.graphics.Canvas; | ||
import android.os.Handler; | ||
import android.os.Looper; | ||
import android.view.Surface; | ||
import androidx.test.ext.junit.runners.AndroidJUnit4; | ||
import io.flutter.embedding.engine.FlutterJNI; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
|
||
@RunWith(AndroidJUnit4.class) | ||
@TargetApi(26) | ||
public final class SurfaceTextureSurfaceProducerTest { | ||
private final FlutterJNI fakeJNI = mock(FlutterJNI.class); | ||
|
||
@Test | ||
public void createsSurfaceTextureOfGivenSizeAndResizesWhenRequested() { | ||
// Create a surface and set the initial size. | ||
final Handler handler = new Handler(Looper.getMainLooper()); | ||
final SurfaceTextureSurfaceProducer producer = | ||
new SurfaceTextureSurfaceProducer(0, handler, fakeJNI); | ||
final Surface surface = producer.getSurface(); | ||
AtomicInteger frames = new AtomicInteger(); | ||
producer | ||
.getSurfaceTexture() | ||
.setOnFrameAvailableListener( | ||
(texture) -> { | ||
if (texture.isReleased()) { | ||
return; | ||
} | ||
frames.getAndIncrement(); | ||
}); | ||
producer.setSize(100, 200); | ||
|
||
// Draw. | ||
Canvas canvas = surface.lockHardwareCanvas(); | ||
canvas.drawARGB(255, 255, 0, 0); | ||
surface.unlockCanvasAndPost(canvas); | ||
shadowOf(Looper.getMainLooper()).idle(); | ||
assertEquals(frames.get(), 1); | ||
|
||
// Resize and redraw. | ||
producer.setSize(400, 800); | ||
canvas = surface.lockHardwareCanvas(); | ||
canvas.drawARGB(255, 255, 0, 0); | ||
surface.unlockCanvasAndPost(canvas); | ||
shadowOf(Looper.getMainLooper()).idle(); | ||
assertEquals(frames.get(), 2); | ||
|
||
// Done. | ||
fakeJNI.detachFromNativeAndReleaseResources(); | ||
producer.release(); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.