Skip to content

Commit

Permalink
Queue setview calls until the surface is ready (#2865)
Browse files Browse the repository at this point in the history
  • Loading branch information
keianhzo committed Feb 25, 2020
1 parent c3bcf52 commit 73113ea
Showing 1 changed file with 38 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ public class WindowWidget extends UIWidget implements SessionChangeListener,
private PromptDelegate mPromptDelegate;
private Executor mUIThreadExecutor;
private WindowViewModel mViewModel;
private CopyOnWriteArrayList<Runnable> mSetViewQueuedCalls;

public interface WindowListener {
default void onFocusRequest(@NonNull WindowWidget aWindow) {}
Expand All @@ -158,6 +159,8 @@ public WindowWidget(Context aContext, int windowId, Session aSession) {
}

private void initialize(Context aContext) {
mSetViewQueuedCalls = new CopyOnWriteArrayList<>();

mWidgetManager = (WidgetManagerDelegate) aContext;
mBorderWidth = SettingsStore.getInstance(aContext).getTransparentBorderWidth();

Expand Down Expand Up @@ -364,28 +367,40 @@ protected void setRestored(boolean restored) {
}

private void setView(View view, boolean switchSurface) {
if (switchSurface) {
pauseCompositor();
}

mView = view;
removeView(view);
mView.setVisibility(VISIBLE);
addView(mView);

if (switchSurface) {
mWidgetPlacement.density = getContext().getResources().getDisplayMetrics().density;
if (mTexture != null && mSurface != null && mRenderer == null) {
// Create the UI Renderer for the current surface.
// Surface must be released when switching back to WebView surface or the browser
// will not render it correctly. See release code in unsetView().
mRenderer = new UISurfaceTextureRenderer(mSurface, mWidgetPlacement.textureWidth(), mWidgetPlacement.textureHeight());
Runnable setView = new Runnable() {
@Override
public void run() {
if (switchSurface) {
pauseCompositor();
}

mView = view;
removeView(view);
mView.setVisibility(VISIBLE);
addView(mView);

if (switchSurface) {
mWidgetPlacement.density = getContext().getResources().getDisplayMetrics().density;
if (mTexture != null && mSurface != null && mRenderer == null) {
// Create the UI Renderer for the current surface.
// Surface must be released when switching back to WebView surface or the browser
// will not render it correctly. See release code in unsetView().
mRenderer = new UISurfaceTextureRenderer(mSurface, mWidgetPlacement.textureWidth(), mWidgetPlacement.textureHeight());
}
mWidgetManager.updateWidget(WindowWidget.this);
mWidgetManager.pushWorldBrightness(this, WidgetManagerDelegate.DEFAULT_DIM_BRIGHTNESS);
mWidgetManager.pushBackHandler(mBackHandler);
setWillNotDraw(false);
postInvalidate();
}
}
mWidgetManager.updateWidget(this);
mWidgetManager.pushWorldBrightness(this, WidgetManagerDelegate.DEFAULT_DIM_BRIGHTNESS);
mWidgetManager.pushBackHandler(mBackHandler);
setWillNotDraw(false);
postInvalidate();
};

if (mAfterFirstPaint) {
setView.run();

} else {
mSetViewQueuedCalls.add(setView);
}
}

Expand Down Expand Up @@ -917,6 +932,7 @@ public void releaseWidget() {
cleanListeners(mSession);
GeckoSession session = mSession.getGeckoSession();

mSetViewQueuedCalls.clear();
if (mSession != null) {
mSession.releaseDisplay();
}
Expand Down Expand Up @@ -1562,6 +1578,7 @@ public void onFirstContentfulPaint(@NonNull GeckoSession session) {
mUIThreadExecutor.execute(mFirstDrawCallback);
mFirstDrawCallback = null;
mAfterFirstPaint = true;
mSetViewQueuedCalls.forEach(Runnable::run);
}
}

Expand Down

0 comments on commit 73113ea

Please sign in to comment.