2012-05-21 11:12:37 +00: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 20:04:47 +00:00
|
|
|
package org.mozilla.gecko.gfx;
|
|
|
|
|
2013-05-24 16:21:01 +00:00
|
|
|
import org.mozilla.gecko.GeckoAppShell;
|
2012-07-28 04:57:47 +00:00
|
|
|
import org.mozilla.gecko.util.FloatUtils;
|
2013-03-15 10:52:53 +00:00
|
|
|
import org.mozilla.gecko.util.ThreadUtils;
|
2012-07-28 00:53:54 +00:00
|
|
|
|
2013-12-18 17:17:59 +00:00
|
|
|
import android.graphics.PointF;
|
2012-04-27 20:04:47 +00:00
|
|
|
import android.graphics.Rect;
|
|
|
|
import android.graphics.RectF;
|
|
|
|
import android.view.SurfaceView;
|
2012-07-28 00:53:54 +00:00
|
|
|
import android.view.View;
|
2012-04-27 20:04:47 +00:00
|
|
|
import android.widget.AbsoluteLayout;
|
2012-07-28 00:53:54 +00:00
|
|
|
|
|
|
|
public class PluginLayer extends TileLayer {
|
2012-04-27 20:04:47 +00: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 15:00:17 +00:00
|
|
|
private boolean mViewVisible;
|
2012-04-27 20:04:47 +00: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
|
|
|
|
};
|
|
|
|
|
2013-08-13 15:49:21 +00:00
|
|
|
public PluginLayer(View view, RectF rect, int maxDimension) {
|
2012-04-27 20:04:47 +00:00
|
|
|
super(new BufferedCairoImage(null, 0, 0, 0), TileLayer.PaintMode.NORMAL);
|
|
|
|
|
|
|
|
mView = view;
|
2013-05-24 16:21:01 +00:00
|
|
|
mContainer = GeckoAppShell.getGeckoInterface().getPluginContainer();
|
2012-04-27 20:04:47 +00: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 15:00:17 +00:00
|
|
|
public void setVisible(boolean visible) {
|
|
|
|
if (visible) {
|
2012-04-27 20:04:47 +00:00
|
|
|
showView();
|
|
|
|
} else {
|
|
|
|
hideView();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void hideView() {
|
|
|
|
if (mViewVisible) {
|
2013-03-15 10:52:53 +00:00
|
|
|
ThreadUtils.postToUiThread(new Runnable() {
|
2013-02-27 05:48:00 +00:00
|
|
|
@Override
|
2012-04-27 20:04:47 +00:00
|
|
|
public void run() {
|
|
|
|
mView.setVisibility(View.GONE);
|
2012-12-10 15:00:17 +00:00
|
|
|
mViewVisible = false;
|
2012-04-27 20:04:47 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void showView() {
|
2013-03-15 10:52:53 +00:00
|
|
|
ThreadUtils.postToUiThread(new Runnable() {
|
2013-02-27 05:48:00 +00:00
|
|
|
@Override
|
2012-12-10 15:00:17 +00: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 20:04:47 +00:00
|
|
|
}
|
2012-12-10 15:00:17 +00:00
|
|
|
mViewVisible = true;
|
|
|
|
}
|
|
|
|
});
|
2012-04-27 20:04:47 +00:00
|
|
|
}
|
|
|
|
|
2013-02-27 05:48:00 +00:00
|
|
|
@Override
|
2012-04-27 20:04:47 +00:00
|
|
|
public void destroy() {
|
|
|
|
mDestroyed = true;
|
|
|
|
|
|
|
|
mContainer.removeView(mView);
|
|
|
|
}
|
|
|
|
|
2013-08-13 15:49:21 +00:00
|
|
|
public void reset(RectF rect) {
|
2012-04-27 20:04:47 +00:00
|
|
|
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;
|
2013-12-18 17:17:59 +00:00
|
|
|
mLayoutParams.reposition(context.viewport, context.offset, context.zoomFactor);
|
2012-04-27 20:04:47 +00:00
|
|
|
|
2012-12-10 15:00:17 +00:00
|
|
|
showView();
|
2012-04-27 20:04:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void draw(RenderContext context) {
|
|
|
|
}
|
|
|
|
|
2012-07-18 00:54:54 +00:00
|
|
|
class PluginLayoutParams extends AbsoluteLayout.LayoutParams
|
2012-04-27 20:04:47 +00:00
|
|
|
{
|
|
|
|
private static final String LOGTAG = "GeckoApp.PluginLayoutParams";
|
|
|
|
|
|
|
|
private RectF mRect;
|
|
|
|
private int mMaxDimension;
|
|
|
|
private float mLastResolution;
|
|
|
|
|
2013-08-13 15:49:21 +00:00
|
|
|
public PluginLayoutParams(RectF rect, int maxDimension) {
|
2012-04-27 20:04:47 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-13 15:49:21 +00:00
|
|
|
public void reset(RectF rect) {
|
|
|
|
mRect = rect;
|
2012-04-27 20:04:47 +00:00
|
|
|
}
|
|
|
|
|
2013-12-18 17:17:59 +00:00
|
|
|
public void reposition(RectF viewport, PointF offset, float zoomFactor) {
|
2012-04-27 20:04:47 +00:00
|
|
|
|
|
|
|
RectF scaled = RectUtils.scale(mRect, zoomFactor);
|
2013-12-18 17:17:59 +00:00
|
|
|
scaled.offset(offset.x, offset.y);
|
2012-04-27 20:04:47 +00:00
|
|
|
|
|
|
|
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-28 04:55:16 +00:00
|
|
|
}
|