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