2012-05-21 04:12:37 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
2012-04-27 13:04:47 -07:00
|
|
|
package org.mozilla.gecko.gfx;
|
|
|
|
|
2013-05-24 09:21:01 -07:00
|
|
|
import org.mozilla.gecko.GeckoAppShell;
|
2012-07-27 21:57:47 -07:00
|
|
|
import org.mozilla.gecko.util.FloatUtils;
|
2013-03-15 03:52:53 -07:00
|
|
|
import org.mozilla.gecko.util.ThreadUtils;
|
2012-07-27 17:53:54 -07:00
|
|
|
|
2012-04-27 13:04:47 -07:00
|
|
|
import android.graphics.Rect;
|
|
|
|
import android.graphics.RectF;
|
|
|
|
import android.view.SurfaceView;
|
2012-07-27 17:53:54 -07:00
|
|
|
import android.view.View;
|
2012-04-27 13:04:47 -07:00
|
|
|
import android.widget.AbsoluteLayout;
|
2012-07-27 17:53:54 -07:00
|
|
|
|
|
|
|
public class PluginLayer extends TileLayer {
|
2012-04-27 13:04:47 -07:00
|
|
|
private static final String LOGTAG = "PluginLayer";
|
|
|
|
|
|
|
|
private View mView;
|
|
|
|
private SurfaceView mSurfaceView;
|
|
|
|
private PluginLayoutParams mLayoutParams;
|
|
|
|
private AbsoluteLayout mContainer;
|
|
|
|
|
|
|
|
private boolean mDestroyed;
|
2012-12-10 07:00:17 -08:00
|
|
|
private boolean mViewVisible;
|
2012-04-27 13:04:47 -07:00
|
|
|
|
|
|
|
private RectF mLastViewport;
|
|
|
|
private float mLastZoomFactor;
|
|
|
|
|
|
|
|
private static final float TEXTURE_MAP[] = {
|
|
|
|
0.0f, 1.0f, // top left
|
|
|
|
0.0f, 0.0f, // bottom left
|
|
|
|
1.0f, 1.0f, // top right
|
|
|
|
1.0f, 0.0f, // bottom right
|
|
|
|
};
|
|
|
|
|
|
|
|
public PluginLayer(View view, Rect rect, int maxDimension) {
|
|
|
|
super(new BufferedCairoImage(null, 0, 0, 0), TileLayer.PaintMode.NORMAL);
|
|
|
|
|
|
|
|
mView = view;
|
2013-05-24 09:21:01 -07:00
|
|
|
mContainer = GeckoAppShell.getGeckoInterface().getPluginContainer();
|
2012-04-27 13:04:47 -07:00
|
|
|
|
|
|
|
mView.setWillNotDraw(false);
|
|
|
|
if (mView instanceof SurfaceView) {
|
|
|
|
mSurfaceView = (SurfaceView)view;
|
|
|
|
mSurfaceView.setZOrderOnTop(false);
|
|
|
|
mSurfaceView.setZOrderMediaOverlay(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
mLayoutParams = new PluginLayoutParams(rect, maxDimension);
|
|
|
|
}
|
|
|
|
|
2012-12-10 07:00:17 -08:00
|
|
|
public void setVisible(boolean visible) {
|
|
|
|
if (visible) {
|
2012-04-27 13:04:47 -07:00
|
|
|
showView();
|
|
|
|
} else {
|
|
|
|
hideView();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void hideView() {
|
|
|
|
if (mViewVisible) {
|
2013-03-15 03:52:53 -07:00
|
|
|
ThreadUtils.postToUiThread(new Runnable() {
|
2013-02-26 21:48:00 -08:00
|
|
|
@Override
|
2012-04-27 13:04:47 -07:00
|
|
|
public void run() {
|
|
|
|
mView.setVisibility(View.GONE);
|
2012-12-10 07:00:17 -08:00
|
|
|
mViewVisible = false;
|
2012-04-27 13:04:47 -07:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void showView() {
|
2013-03-15 03:52:53 -07:00
|
|
|
ThreadUtils.postToUiThread(new Runnable() {
|
2013-02-26 21:48:00 -08:00
|
|
|
@Override
|
2012-12-10 07:00:17 -08:00
|
|
|
public void run() {
|
|
|
|
if (mContainer.indexOfChild(mView) < 0) {
|
|
|
|
mContainer.addView(mView, mLayoutParams);
|
|
|
|
} else {
|
|
|
|
mContainer.updateViewLayout(mView, mLayoutParams);
|
|
|
|
mView.setVisibility(View.VISIBLE);
|
2012-04-27 13:04:47 -07:00
|
|
|
}
|
2012-12-10 07:00:17 -08:00
|
|
|
mViewVisible = true;
|
|
|
|
}
|
|
|
|
});
|
2012-04-27 13:04:47 -07:00
|
|
|
}
|
|
|
|
|
2013-02-26 21:48:00 -08:00
|
|
|
@Override
|
2012-04-27 13:04:47 -07:00
|
|
|
public void destroy() {
|
|
|
|
mDestroyed = true;
|
|
|
|
|
|
|
|
mContainer.removeView(mView);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void reset(Rect rect) {
|
|
|
|
mLayoutParams.reset(rect);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void performUpdates(RenderContext context) {
|
|
|
|
if (mDestroyed)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!RectUtils.fuzzyEquals(context.viewport, mLastViewport) ||
|
|
|
|
!FloatUtils.fuzzyEquals(context.zoomFactor, mLastZoomFactor)) {
|
|
|
|
|
|
|
|
mLastZoomFactor = context.zoomFactor;
|
|
|
|
mLastViewport = context.viewport;
|
|
|
|
mLayoutParams.reposition(context.viewport, context.zoomFactor);
|
|
|
|
|
2012-12-10 07:00:17 -08:00
|
|
|
showView();
|
2012-04-27 13:04:47 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void draw(RenderContext context) {
|
|
|
|
}
|
|
|
|
|
2012-07-17 17:54:54 -07:00
|
|
|
class PluginLayoutParams extends AbsoluteLayout.LayoutParams
|
2012-04-27 13:04:47 -07:00
|
|
|
{
|
|
|
|
private static final String LOGTAG = "GeckoApp.PluginLayoutParams";
|
|
|
|
|
|
|
|
private RectF mRect;
|
|
|
|
private int mMaxDimension;
|
|
|
|
private float mLastResolution;
|
|
|
|
|
|
|
|
public PluginLayoutParams(Rect rect, int maxDimension) {
|
|
|
|
super(0, 0, 0, 0);
|
|
|
|
|
|
|
|
mMaxDimension = maxDimension;
|
|
|
|
reset(rect);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void clampToMaxSize() {
|
|
|
|
if (width > mMaxDimension || height > mMaxDimension) {
|
|
|
|
if (width > height) {
|
|
|
|
height = Math.round(((float)height/(float)width) * mMaxDimension);
|
|
|
|
width = mMaxDimension;
|
|
|
|
} else {
|
|
|
|
width = Math.round(((float)width/(float)height) * mMaxDimension);
|
|
|
|
height = mMaxDimension;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void reset(Rect rect) {
|
|
|
|
mRect = new RectF(rect);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void reposition(RectF viewport, float zoomFactor) {
|
|
|
|
|
|
|
|
RectF scaled = RectUtils.scale(mRect, zoomFactor);
|
|
|
|
|
|
|
|
this.x = Math.round(scaled.left - viewport.left);
|
|
|
|
this.y = Math.round(scaled.top - viewport.top);
|
|
|
|
|
|
|
|
if (!FloatUtils.fuzzyEquals(mLastResolution, zoomFactor)) {
|
|
|
|
width = Math.round(mRect.width() * zoomFactor);
|
|
|
|
height = Math.round(mRect.height() * zoomFactor);
|
|
|
|
mLastResolution = zoomFactor;
|
|
|
|
|
|
|
|
clampToMaxSize();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-04-27 21:55:16 -07:00
|
|
|
}
|