Home | History | Annotate | Download | only in fbotest
      1 /*
      2  * Copyright (C) 2011 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.fbotest;
     18 
     19 import android.renderscript.RSSurfaceView;
     20 import android.renderscript.RenderScriptGL;
     21 
     22 import android.content.Context;
     23 import android.view.MotionEvent;
     24 import android.view.SurfaceHolder;
     25 import android.view.ScaleGestureDetector;
     26 import android.util.Log;
     27 
     28 public class FBOSyncView extends RSSurfaceView {
     29 
     30     private RenderScriptGL mRS;
     31     private FBOSyncRS mRender;
     32 
     33     private ScaleGestureDetector mScaleDetector;
     34 
     35     private static final int INVALID_POINTER_ID = -1;
     36     private int mActivePointerId = INVALID_POINTER_ID;
     37 
     38     public FBOSyncView(Context context) {
     39         super(context);
     40         ensureRenderScript();
     41         mScaleDetector = new ScaleGestureDetector(context, new ScaleListener());
     42     }
     43 
     44     private void ensureRenderScript() {
     45         if (mRS == null) {
     46             RenderScriptGL.SurfaceConfig sc = new RenderScriptGL.SurfaceConfig();
     47             sc.setDepth(16, 24);
     48             mRS = createRenderScriptGL(sc);
     49             mRender = new FBOSyncRS();
     50             mRender.init(mRS, getResources());
     51         }
     52     }
     53 
     54     @Override
     55     protected void onAttachedToWindow() {
     56         super.onAttachedToWindow();
     57         ensureRenderScript();
     58     }
     59 
     60     @Override
     61     public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
     62         super.surfaceChanged(holder, format, w, h);
     63         mRender.surfaceChanged();
     64     }
     65 
     66     @Override
     67     protected void onDetachedFromWindow() {
     68         mRender = null;
     69         if (mRS != null) {
     70             mRS = null;
     71             destroyRenderScriptGL();
     72         }
     73     }
     74 
     75     public void loadA3DFile(String path) {
     76         mRender.loadA3DFile(path);
     77     }
     78 
     79     @Override
     80     public boolean onTouchEvent(MotionEvent ev) {
     81         mScaleDetector.onTouchEvent(ev);
     82 
     83         boolean ret = false;
     84         float x = ev.getX();
     85         float y = ev.getY();
     86 
     87         final int action = ev.getAction();
     88 
     89         switch (action & MotionEvent.ACTION_MASK) {
     90         case MotionEvent.ACTION_DOWN: {
     91             mRender.onActionDown(x, y);
     92             mActivePointerId = ev.getPointerId(0);
     93             ret = true;
     94             break;
     95         }
     96         case MotionEvent.ACTION_MOVE: {
     97             if (!mScaleDetector.isInProgress()) {
     98                 mRender.onActionMove(x, y);
     99             }
    100             mRender.onActionDown(x, y);
    101             ret = true;
    102             break;
    103         }
    104 
    105         case MotionEvent.ACTION_UP: {
    106             mActivePointerId = INVALID_POINTER_ID;
    107             break;
    108         }
    109 
    110         case MotionEvent.ACTION_CANCEL: {
    111             mActivePointerId = INVALID_POINTER_ID;
    112             break;
    113         }
    114 
    115         case MotionEvent.ACTION_POINTER_UP: {
    116             final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK)
    117                     >> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
    118             final int pointerId = ev.getPointerId(pointerIndex);
    119             if (pointerId == mActivePointerId) {
    120                 // This was our active pointer going up. Choose a new
    121                 // active pointer and adjust accordingly.
    122                 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
    123                 x = ev.getX(newPointerIndex);
    124                 y = ev.getY(newPointerIndex);
    125                 mRender.onActionDown(x, y);
    126                 mActivePointerId = ev.getPointerId(newPointerIndex);
    127             }
    128             break;
    129         }
    130         }
    131 
    132         return ret;
    133     }
    134 
    135     private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {
    136         @Override
    137         public boolean onScale(ScaleGestureDetector detector) {
    138             mRender.onActionScale(detector.getScaleFactor());
    139             return true;
    140         }
    141     }
    142 }
    143 
    144 
    145