summaryrefslogtreecommitdiffstats
path: root/src/com/android/gallery3d/photoeditor/PhotoView.java
blob: edb36242e3fb1f7042443a7f7bf956da6be3fc8f (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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
 * Copyright (C) 2010 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.gallery3d.photoeditor;

import android.content.Context;
import android.graphics.RectF;
import android.opengl.GLSurfaceView;
import android.util.AttributeSet;

import java.util.Vector;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

/**
 * Renders and displays photo in the surface view.
 */
public class PhotoView extends GLSurfaceView {

    private final PhotoRenderer renderer;

    public PhotoView(Context context, AttributeSet attrs) {
        super(context, attrs);

        renderer = new PhotoRenderer();
        setEGLContextClientVersion(2);
        setRenderer(renderer);
        setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
    }

    public RectF getPhotoBounds() {
        RectF photoBounds;
        synchronized (renderer.photoBounds) {
            photoBounds = new RectF(renderer.photoBounds);
        }
        return photoBounds;
    }

    /**
     * Queues a runnable and renders a frame after execution. Queued runnables could be later
     * removed by remove() or flush().
     */
    public void queue(Runnable r) {
        renderer.queue.add(r);
        requestRender();
    }

    /**
     * Removes the specified queued runnable.
     */
    public void remove(Runnable runnable) {
        renderer.queue.remove(runnable);
    }

    /**
     * Flushes all queued runnables to cancel their execution.
     */
    public void flush() {
        renderer.queue.clear();
    }

    /**
     * Sets photo for display; this method must be queued for GL thread.
     */
    public void setPhoto(Photo photo, boolean clearTransform) {
        renderer.setPhoto(photo, clearTransform);
    }

    /**
     * Rotates displayed photo; this method must be queued for GL thread.
     */
    public void rotatePhoto(float degrees) {
        renderer.rotatePhoto(degrees);
    }

    /**
     * Renderer that renders the GL surface-view and only be called from the GL thread.
     */
    private class PhotoRenderer implements GLSurfaceView.Renderer {

        final Vector<Runnable> queue = new Vector<Runnable>();
        final RectF photoBounds = new RectF();
        RendererUtils.RenderContext renderContext;
        Photo photo;
        int viewWidth;
        int viewHeight;
        float rotatedDegrees;

        void setPhoto(Photo photo, boolean clearTransform) {
            int width = (photo != null) ? photo.width() : 0;
            int height = (photo != null) ? photo.height() : 0;
            boolean changed;
            synchronized (photoBounds) {
                changed = (photoBounds.width() != width) || (photoBounds.height() != height);
                if (changed) {
                    photoBounds.set(0, 0, width, height);
                }
            }
            this.photo = photo;
            updateSurface(clearTransform, changed);
        }

        void updateSurface(boolean clearTransform, boolean sizeChanged) {
            boolean transformed = (rotatedDegrees != 0);
            if ((clearTransform && transformed) || (sizeChanged && !transformed)) {
                // Fit photo when clearing existing transforms or changing surface/photo sizes.
                if (photo != null) {
                    RendererUtils.setRenderToFit(renderContext, photo.width(), photo.height(),
                            viewWidth, viewHeight);
                    rotatedDegrees = 0;
                }
            } else {
                // Restore existing transformations for orientation changes or awaking from sleep.
                if (rotatedDegrees != 0) {
                    rotatePhoto(rotatedDegrees);
                }
            }
        }

        void rotatePhoto(float degrees) {
            if (photo != null) {
                RendererUtils.setRenderToRotate(renderContext, photo.width(), photo.height(),
                        viewWidth, viewHeight, degrees);
                rotatedDegrees = degrees;
            }
        }

        @Override
        public void onDrawFrame(GL10 gl) {
            Runnable r = null;
            synchronized (queue) {
                if (!queue.isEmpty()) {
                    r = queue.remove(0);
                }
            }
            if (r != null) {
                r.run();
            }
            if (!queue.isEmpty()) {
                requestRender();
            }
            RendererUtils.renderBackground();
            if (photo != null) {
                RendererUtils.renderTexture(renderContext, photo.texture(), viewWidth, viewHeight);
            }
        }

        @Override
        public void onSurfaceChanged(GL10 gl, int width, int height) {
            viewWidth = width;
            viewHeight = height;
            updateSurface(false, true);
        }

        @Override
        public void onSurfaceCreated(GL10 gl, EGLConfig config) {
            renderContext = RendererUtils.createProgram();
        }
    }
}