Home | History | Annotate | Download | only in ui
      1 package com.android.gallery3d.ui;
      2 
      3 import android.os.ConditionVariable;
      4 
      5 import com.android.gallery3d.app.AbstractGalleryActivity;
      6 import com.android.gallery3d.glrenderer.GLCanvas;
      7 import com.android.gallery3d.glrenderer.RawTexture;
      8 import com.android.gallery3d.ui.GLRoot.OnGLIdleListener;
      9 
     10 public class PreparePageFadeoutTexture implements OnGLIdleListener {
     11     private static final long TIMEOUT = 200;
     12     public static final String KEY_FADE_TEXTURE = "fade_texture";
     13 
     14     private RawTexture mTexture;
     15     private ConditionVariable mResultReady = new ConditionVariable(false);
     16     private boolean mCancelled = false;
     17     private GLView mRootPane;
     18 
     19     public PreparePageFadeoutTexture(GLView rootPane) {
     20         if (rootPane == null) {
     21             mCancelled = true;
     22             return;
     23         }
     24         int w = rootPane.getWidth();
     25         int h = rootPane.getHeight();
     26         if (w == 0 || h == 0) {
     27             mCancelled = true;
     28             return;
     29         }
     30         mTexture = new RawTexture(w, h, true);
     31         mRootPane =  rootPane;
     32     }
     33 
     34     public boolean isCancelled() {
     35         return mCancelled;
     36     }
     37 
     38     public synchronized RawTexture get() {
     39         if (mCancelled) {
     40             return null;
     41         } else if (mResultReady.block(TIMEOUT)) {
     42             return mTexture;
     43         } else {
     44             mCancelled = true;
     45             return null;
     46         }
     47     }
     48 
     49     @Override
     50     public boolean onGLIdle(GLCanvas canvas, boolean renderRequested) {
     51         if (!mCancelled) {
     52             try {
     53                 canvas.beginRenderTarget(mTexture);
     54                 mRootPane.render(canvas);
     55                 canvas.endRenderTarget();
     56             } catch (RuntimeException e) {
     57                 mTexture = null;
     58             }
     59         } else {
     60             mTexture = null;
     61         }
     62         mResultReady.open();
     63         return false;
     64     }
     65 
     66     public static void prepareFadeOutTexture(AbstractGalleryActivity activity,
     67             GLView rootPane) {
     68         PreparePageFadeoutTexture task = new PreparePageFadeoutTexture(rootPane);
     69         if (task.isCancelled()) return;
     70         GLRoot root = activity.getGLRoot();
     71         RawTexture texture = null;
     72         root.unlockRenderThread();
     73         try {
     74             root.addOnGLIdleListener(task);
     75             texture = task.get();
     76         } finally {
     77             root.lockRenderThread();
     78         }
     79 
     80         if (texture == null) {
     81             return;
     82         }
     83         activity.getTransitionStore().put(KEY_FADE_TEXTURE, texture);
     84     }
     85 }
     86