mobile/android/base/gfx/PluginLayer.java

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mobile/android/base/gfx/PluginLayer.java	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,169 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +package org.mozilla.gecko.gfx;
     1.9 +
    1.10 +import org.mozilla.gecko.GeckoAppShell;
    1.11 +import org.mozilla.gecko.util.FloatUtils;
    1.12 +import org.mozilla.gecko.util.ThreadUtils;
    1.13 +
    1.14 +import android.graphics.PointF;
    1.15 +import android.graphics.Rect;
    1.16 +import android.graphics.RectF;
    1.17 +import android.view.SurfaceView;
    1.18 +import android.view.View;
    1.19 +import android.widget.AbsoluteLayout;
    1.20 +
    1.21 +public class PluginLayer extends TileLayer {
    1.22 +    private static final String LOGTAG = "PluginLayer";
    1.23 +
    1.24 +    private View mView;
    1.25 +    private SurfaceView mSurfaceView;
    1.26 +    private PluginLayoutParams mLayoutParams;
    1.27 +    private AbsoluteLayout mContainer;
    1.28 +
    1.29 +    private boolean mDestroyed;
    1.30 +    private boolean mViewVisible;
    1.31 +
    1.32 +    private RectF mLastViewport;
    1.33 +    private float mLastZoomFactor;
    1.34 +
    1.35 +    private static final float TEXTURE_MAP[] = {
    1.36 +                0.0f, 1.0f, // top left
    1.37 +                0.0f, 0.0f, // bottom left
    1.38 +                1.0f, 1.0f, // top right
    1.39 +                1.0f, 0.0f, // bottom right
    1.40 +    };
    1.41 +
    1.42 +    public PluginLayer(View view, RectF rect, int maxDimension) {
    1.43 +        super(new BufferedCairoImage(null, 0, 0, 0), TileLayer.PaintMode.NORMAL);
    1.44 +
    1.45 +        mView = view;
    1.46 +        mContainer = GeckoAppShell.getGeckoInterface().getPluginContainer();
    1.47 +
    1.48 +        mView.setWillNotDraw(false);
    1.49 +        if (mView instanceof SurfaceView) {
    1.50 +            mSurfaceView = (SurfaceView)view;
    1.51 +            mSurfaceView.setZOrderOnTop(false);
    1.52 +            mSurfaceView.setZOrderMediaOverlay(true);
    1.53 +        }
    1.54 +
    1.55 +        mLayoutParams = new PluginLayoutParams(rect, maxDimension);
    1.56 +    }
    1.57 +
    1.58 +    public void setVisible(boolean visible) {
    1.59 +        if (visible) {
    1.60 +            showView();
    1.61 +        } else {
    1.62 +            hideView();
    1.63 +        }
    1.64 +    }
    1.65 +
    1.66 +    private void hideView() {
    1.67 +        if (mViewVisible) {
    1.68 +            ThreadUtils.postToUiThread(new Runnable() {
    1.69 +                @Override
    1.70 +                public void run() {
    1.71 +                    mView.setVisibility(View.GONE);
    1.72 +                    mViewVisible = false;
    1.73 +                }
    1.74 +            });
    1.75 +        }
    1.76 +    }
    1.77 +
    1.78 +    public void showView() {
    1.79 +        ThreadUtils.postToUiThread(new Runnable() {
    1.80 +            @Override
    1.81 +            public void run() {
    1.82 +                if (mContainer.indexOfChild(mView) < 0) {
    1.83 +                    mContainer.addView(mView, mLayoutParams);
    1.84 +                } else {
    1.85 +                    mContainer.updateViewLayout(mView, mLayoutParams);
    1.86 +                    mView.setVisibility(View.VISIBLE);
    1.87 +                }
    1.88 +                mViewVisible = true;
    1.89 +            }
    1.90 +        });
    1.91 +    }
    1.92 +
    1.93 +    @Override
    1.94 +    public void destroy() {
    1.95 +        mDestroyed = true;
    1.96 +
    1.97 +        mContainer.removeView(mView);
    1.98 +    }
    1.99 +
   1.100 +    public void reset(RectF rect) {
   1.101 +        mLayoutParams.reset(rect);
   1.102 +    }
   1.103 +
   1.104 +    @Override
   1.105 +    protected void performUpdates(RenderContext context) {
   1.106 +        if (mDestroyed)
   1.107 +            return;
   1.108 +
   1.109 +        if (!RectUtils.fuzzyEquals(context.viewport, mLastViewport) ||
   1.110 +            !FloatUtils.fuzzyEquals(context.zoomFactor, mLastZoomFactor)) {
   1.111 +
   1.112 +            mLastZoomFactor = context.zoomFactor;
   1.113 +            mLastViewport = context.viewport;
   1.114 +            mLayoutParams.reposition(context.viewport, context.offset, context.zoomFactor);
   1.115 +
   1.116 +            showView();
   1.117 +        }
   1.118 +    }
   1.119 +
   1.120 +    @Override
   1.121 +    public void draw(RenderContext context) {
   1.122 +    }
   1.123 +
   1.124 +    class PluginLayoutParams extends AbsoluteLayout.LayoutParams
   1.125 +    {
   1.126 +        private static final String LOGTAG = "GeckoApp.PluginLayoutParams";
   1.127 +
   1.128 +        private RectF mRect;
   1.129 +        private int mMaxDimension;
   1.130 +        private float mLastResolution;
   1.131 +
   1.132 +        public PluginLayoutParams(RectF rect, int maxDimension) {
   1.133 +            super(0, 0, 0, 0);
   1.134 +
   1.135 +            mMaxDimension = maxDimension;
   1.136 +            reset(rect);
   1.137 +        }
   1.138 +
   1.139 +        private void clampToMaxSize() {
   1.140 +            if (width > mMaxDimension || height > mMaxDimension) {
   1.141 +                if (width > height) {
   1.142 +                    height = Math.round(((float)height/(float)width) * mMaxDimension);
   1.143 +                    width = mMaxDimension;
   1.144 +                } else {
   1.145 +                    width = Math.round(((float)width/(float)height) * mMaxDimension);
   1.146 +                    height = mMaxDimension;
   1.147 +                }
   1.148 +            }
   1.149 +        }
   1.150 +
   1.151 +        public void reset(RectF rect) {
   1.152 +            mRect = rect;
   1.153 +        }
   1.154 +
   1.155 +        public void reposition(RectF viewport, PointF offset, float zoomFactor) {
   1.156 +
   1.157 +            RectF scaled = RectUtils.scale(mRect, zoomFactor);
   1.158 +            scaled.offset(offset.x, offset.y);
   1.159 +
   1.160 +            this.x = Math.round(scaled.left - viewport.left);
   1.161 +            this.y = Math.round(scaled.top - viewport.top);
   1.162 +
   1.163 +            if (!FloatUtils.fuzzyEquals(mLastResolution, zoomFactor)) {
   1.164 +                width = Math.round(mRect.width() * zoomFactor);
   1.165 +                height = Math.round(mRect.height() * zoomFactor);
   1.166 +                mLastResolution = zoomFactor;
   1.167 +
   1.168 +                clampToMaxSize();
   1.169 +            }
   1.170 +        }
   1.171 +    }
   1.172 +}

mercurial