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