forked from flutter/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Each platform view created (by a plugin supplied factory) is attached to a virtual display. The virtual displays are controlled by VirtualDisplayController objects. The PlatformViewsController maintains a mapping from a platform view's id to its VirtualDisplayController, which allows it to operate on the virtual display for a given platform view ID when asked so over the method channel. This is using API level 20 APIs, on lower API levels all platform views method channel calls are noops. We can make this work on API 19 with some refactoring to the TextureRegistry (allow the engine Java code to recycle a texture entry id). This CL also adds a platform view id parameter to the PlatformViewFactory#create() method. This allows plugins to route platform channel messages to specific instances of a platform view. TBD in future CLs: * Forward touch events to the platform views. * Support accessibility for platform views. flutter/flutter#19030
- Loading branch information
Showing
6 changed files
with
299 additions
and
23 deletions.
There are no files selected for viewing
This file contains 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 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 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
shell/platform/android/io/flutter/plugin/platform/SingleViewPresentation.java
This file contains 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.plugin.platform; | ||
|
||
import android.annotation.TargetApi; | ||
import android.app.Presentation; | ||
import android.content.Context; | ||
import android.os.Build; | ||
import android.os.Bundle; | ||
import android.view.Display; | ||
import android.view.View; | ||
import android.widget.FrameLayout; | ||
|
||
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1) | ||
class SingleViewPresentation extends Presentation { | ||
private final PlatformViewFactory mViewFactory; | ||
|
||
private View mView; | ||
private String mViewId; | ||
|
||
// As the root view of a display cannot be detached, we use this mContainer | ||
// as the root, and attach mView to it. This allows us to detach mView. | ||
private FrameLayout mContainer; | ||
|
||
/** | ||
* Creates a presentation that will use the view factory to create a new | ||
* platform view in the presentation's onCreate, and attach it. | ||
*/ | ||
public SingleViewPresentation(Context outerContext, Display display, PlatformViewFactory viewFactory, String viewId) { | ||
super(outerContext, display); | ||
mViewFactory = viewFactory; | ||
mViewId = viewId; | ||
} | ||
|
||
/** | ||
* Creates a presentation that will attach an already existing view as | ||
* its root view. | ||
* | ||
* <p>The display's density must match the density of the context used | ||
* when the view was created. | ||
*/ | ||
public SingleViewPresentation(Context outerContext, Display display, View view) { | ||
super(outerContext, display); | ||
mViewFactory = null; | ||
mView = view; | ||
} | ||
|
||
@Override | ||
protected void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
if (mView == null) { | ||
mView = mViewFactory.create(getContext(), mViewId).getView(); | ||
} | ||
mContainer = new FrameLayout(getContext()); | ||
mContainer.addView(mView); | ||
setContentView(mContainer); | ||
} | ||
|
||
public View detachView() { | ||
mContainer.removeView(mView); | ||
return mView; | ||
} | ||
} |
97 changes: 97 additions & 0 deletions
97
shell/platform/android/io/flutter/plugin/platform/VirtualDisplayController.java
This file contains 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,97 @@ | ||
package io.flutter.plugin.platform; | ||
|
||
import android.annotation.TargetApi; | ||
import android.content.Context; | ||
import android.graphics.SurfaceTexture; | ||
import android.hardware.display.DisplayManager; | ||
import android.hardware.display.VirtualDisplay; | ||
import android.os.Build; | ||
import android.view.Surface; | ||
import android.view.View; | ||
|
||
@TargetApi(Build.VERSION_CODES.KITKAT_WATCH) | ||
class VirtualDisplayController { | ||
|
||
public static VirtualDisplayController create( | ||
Context context, | ||
PlatformViewFactory viewFactory, | ||
SurfaceTexture surfaceTexture, | ||
int width, | ||
int height, | ||
String viewId | ||
) { | ||
surfaceTexture.setDefaultBufferSize(width, height); | ||
Surface surface = new Surface(surfaceTexture); | ||
DisplayManager displayManager = (DisplayManager) context.getSystemService(Context.DISPLAY_SERVICE); | ||
|
||
int densityDpi = context.getResources().getDisplayMetrics().densityDpi; | ||
VirtualDisplay virtualDisplay = displayManager.createVirtualDisplay( | ||
"flutter-vd", | ||
width, | ||
height, | ||
densityDpi, | ||
surface, | ||
0 | ||
); | ||
|
||
if (virtualDisplay == null) { | ||
return null; | ||
} | ||
|
||
return new VirtualDisplayController(context, virtualDisplay, viewFactory, surface, surfaceTexture, viewId); | ||
} | ||
|
||
private final Context mContext; | ||
private final int mDensityDpi; | ||
private final SurfaceTexture mSurfaceTexture; | ||
private VirtualDisplay mVirtualDisplay; | ||
private SingleViewPresentation mPresentation; | ||
private Surface mSurface; | ||
|
||
|
||
VirtualDisplayController( | ||
Context context, | ||
VirtualDisplay virtualDisplay, | ||
PlatformViewFactory viewFactory, | ||
Surface surface, | ||
SurfaceTexture surfaceTexture, | ||
String viewId | ||
) { | ||
mSurfaceTexture = surfaceTexture; | ||
mSurface = surface; | ||
mContext = context; | ||
mVirtualDisplay = virtualDisplay; | ||
mDensityDpi = context.getResources().getDisplayMetrics().densityDpi; | ||
mPresentation = new SingleViewPresentation(context, mVirtualDisplay.getDisplay(), viewFactory, viewId); | ||
mPresentation.show(); | ||
} | ||
|
||
public void resize(int width, int height) { | ||
View view = mPresentation.detachView(); | ||
mPresentation.hide(); | ||
// We detach the surface to prevent it being destroyed when releasing the vd. | ||
// | ||
// setSurface is only available starting API 20. We could support API 19 by re-creating a new | ||
// SurfaceTexture here. This will require refactoring the TextureRegistry to allow recycling texture | ||
// entry IDs. | ||
mVirtualDisplay.setSurface(null); | ||
mVirtualDisplay.release(); | ||
|
||
mSurfaceTexture.setDefaultBufferSize(width, height); | ||
DisplayManager displayManager = (DisplayManager) mContext.getSystemService(Context.DISPLAY_SERVICE); | ||
mVirtualDisplay = displayManager.createVirtualDisplay( | ||
"flutter-vd", | ||
width, | ||
height, | ||
mDensityDpi, | ||
mSurface, | ||
0 | ||
); | ||
mPresentation = new SingleViewPresentation(mContext, mVirtualDisplay.getDisplay(), view); | ||
mPresentation.show(); | ||
} | ||
|
||
public void dispose() { | ||
mVirtualDisplay.release(); | ||
} | ||
} |
Oops, something went wrong.