summaryrefslogtreecommitdiffstats
path: root/src/com/android/gallery3d/ui/PreparePageFadeoutTexture.java
blob: f52aa5ff2f8f5e8fe3d68adc076e6e9ebe5e688a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package com.android.gallery3d.ui;

import android.os.ConditionVariable;

import com.android.gallery3d.app.AbstractGalleryActivity;
import com.android.gallery3d.glrenderer.GLCanvas;
import com.android.gallery3d.glrenderer.RawTexture;
import com.android.gallery3d.ui.GLRoot.OnGLIdleListener;

public class PreparePageFadeoutTexture implements OnGLIdleListener {
    private static final long TIMEOUT = 200;
    public static final String KEY_FADE_TEXTURE = "fade_texture";

    private RawTexture mTexture;
    private ConditionVariable mResultReady = new ConditionVariable(false);
    private boolean mCancelled = false;
    private GLView mRootPane;

    public PreparePageFadeoutTexture(GLView rootPane) {
        int w = rootPane.getWidth();
        int h = rootPane.getHeight();
        if (w == 0 || h == 0) {
            mCancelled = true;
            return;
        }
        mTexture = new RawTexture(w, h, true);
        mRootPane =  rootPane;
    }

    public boolean isCancelled() {
        return mCancelled;
    }

    public synchronized RawTexture get() {
        if (mCancelled) {
            return null;
        } else if (mResultReady.block(TIMEOUT)) {
            return mTexture;
        } else {
            mCancelled = true;
            return null;
        }
    }

    @Override
    public boolean onGLIdle(GLCanvas canvas, boolean renderRequested) {
        if (!mCancelled) {
            try {
                canvas.beginRenderTarget(mTexture);
                mRootPane.render(canvas);
                canvas.endRenderTarget();
            } catch (RuntimeException e) {
                mTexture = null;
            }
        } else {
            mTexture = null;
        }
        mResultReady.open();
        return false;
    }

    public static void prepareFadeOutTexture(AbstractGalleryActivity activity,
            GLView rootPane) {
        PreparePageFadeoutTexture task = new PreparePageFadeoutTexture(rootPane);
        if (task.isCancelled()) return;
        GLRoot root = activity.getGLRoot();
        RawTexture texture = null;
        root.unlockRenderThread();
        try {
            root.addOnGLIdleListener(task);
            texture = task.get();
        } finally {
            root.lockRenderThread();
        }

        if (texture == null) {
            return;
        }
        activity.getTransitionStore().put(KEY_FADE_TEXTURE, texture);
    }
}