aboutsummaryrefslogtreecommitdiffhomepage
path: root/platform_tools/android/apps/sample_app/src/main/java/com/skia/SkiaSampleView.java
blob: c33f8ae8acf3d290eac1325dee34ee6ab0b847d6 (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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/*
 * Copyright 2012 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

package com.skia;

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

import android.content.Context;
import android.opengl.EGL14;
import android.opengl.GLSurfaceView;
import android.os.Build;
import android.util.Log;
import android.view.MotionEvent;

public class SkiaSampleView extends GLSurfaceView {

    private final SkiaSampleRenderer mSampleRenderer;
    private boolean mRequestedOpenGLAPI; // true == use (desktop) OpenGL. false == use OpenGL ES.
    private int mRequestedMSAASampleCount;

    public SkiaSampleView(Context ctx, String cmdLineFlags, boolean useOpenGL, int msaaSampleCount) {
        super(ctx);

        mSampleRenderer = new SkiaSampleRenderer(this, cmdLineFlags);
        mRequestedMSAASampleCount = msaaSampleCount;

        setEGLContextClientVersion(2);
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
            setEGLConfigChooser(8, 8, 8, 8, 0, 8);
        } else {
            mRequestedOpenGLAPI = useOpenGL;
            setEGLConfigChooser(new SampleViewEGLConfigChooser());
        }
        setRenderer(mSampleRenderer);
        setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int count = event.getPointerCount();
        for (int i = 0; i < count; i++) {
            final float x = event.getX(i);
            final float y = event.getY(i);
            final int owner = event.getPointerId(i);
            int action = event.getAction() & MotionEvent.ACTION_MASK;
            switch (action) {
            case MotionEvent.ACTION_POINTER_UP:
                action = MotionEvent.ACTION_UP;
                break;
            case MotionEvent.ACTION_POINTER_DOWN:
                action = MotionEvent.ACTION_DOWN;
                break;
            default:
                break;
            }
            final int finalAction = action;
            queueEvent(new Runnable() {
                @Override
                public void run() {
                    mSampleRenderer.handleClick(owner, x, y, finalAction);
                }
            });
        }
        return true;
    }

    public void inval() {
        queueEvent(new Runnable() {
            @Override
            public void run() {
                mSampleRenderer.postInval();
            }
        });
    }

    public void terminate() {
        queueEvent(new Runnable() {
            @Override
            public void run() {
                mSampleRenderer.term();
            }
        });
    }

    public void showOverview() {
        queueEvent(new Runnable() {
            @Override
            public void run() {
                mSampleRenderer.showOverview();
            }
        });
    }

    public void nextSample() {
        queueEvent(new Runnable() {
            @Override
            public void run() {
                mSampleRenderer.nextSample();
            }
        });
    }

    public void previousSample() {
        queueEvent(new Runnable() {
            @Override
            public void run() {
                mSampleRenderer.previousSample();
            }
        });
    }

    public void goToSample(final int position) {
        queueEvent(new Runnable() {
            @Override
            public void run() {
                mSampleRenderer.goToSample(position);
            }
        });
    }

    public void toggleRenderingMode() {
        queueEvent(new Runnable() {
            @Override
            public void run() {
                mSampleRenderer.toggleRenderingMode();
            }
        });
    }

    public void toggleSlideshow() {
        queueEvent(new Runnable() {
            @Override
            public void run() {
                mSampleRenderer.toggleSlideshow();
            }
        });
    }

    public void toggleFPS() {
        queueEvent(new Runnable() {
            @Override
            public void run() {
                mSampleRenderer.toggleFPS();
            }
        });
    }

    public void toggleTiling() {
        queueEvent(new Runnable() {
            @Override
            public void run() {
                mSampleRenderer.toggleTiling();
            }
        });
    }

    public void toggleBBox() {
        queueEvent(new Runnable() {
            @Override
            public void run() {
                mSampleRenderer.toggleBBox();
            }
        });
    }
    
    public void saveToPDF() {
        queueEvent(new Runnable() {
            @Override
            public void run() {
                mSampleRenderer.saveToPDF();
            }
        });
    }

    public boolean getUsesOpenGLAPI() {
        return mRequestedOpenGLAPI;
    }

    public int getMSAASampleCount() {
        return mSampleRenderer.getMSAASampleCount();
    }

    private class SampleViewEGLConfigChooser implements GLSurfaceView.EGLConfigChooser {

        @Override
        public EGLConfig chooseConfig(EGL10 egl, EGLDisplay display) {
            int numConfigs = 0;
            int[] configSpec = null;
            int[] value = new int[1];
            
            int[] validAPIs = new int[] {
                EGL14.EGL_OPENGL_API,
                EGL14.EGL_OPENGL_ES_API
            };
            int initialAPI = mRequestedOpenGLAPI ? 0 : 1;
            
            for (int i = initialAPI; i < validAPIs.length && numConfigs == 0; i++) {
                int currentAPI = validAPIs[i];
                EGL14.eglBindAPI(currentAPI);

                // setup the renderableType which will only be included in the
                // spec if we are attempting to get access to the OpenGL APIs.
                int renderableType = EGL14.EGL_OPENGL_BIT;
                if (currentAPI == EGL14.EGL_OPENGL_API) {
                    renderableType = EGL14.EGL_OPENGL_ES2_BIT;
                }

                if (mRequestedMSAASampleCount > 0) {
                    configSpec = new int[] {
                        EGL10.EGL_RED_SIZE, 8,
                        EGL10.EGL_GREEN_SIZE, 8,
                        EGL10.EGL_BLUE_SIZE, 8,
                        EGL10.EGL_ALPHA_SIZE, 8,
                        EGL10.EGL_DEPTH_SIZE, 0,
                        EGL10.EGL_STENCIL_SIZE, 8,
                        EGL10.EGL_SAMPLE_BUFFERS, 1,
                        EGL10.EGL_SAMPLES, mRequestedMSAASampleCount,
                        EGL10.EGL_RENDERABLE_TYPE, renderableType,
                        EGL10.EGL_NONE
                    };

                    // EGL_RENDERABLE_TYPE is only needed when attempting to use
                    // the OpenGL API (not ES) and causes many EGL drivers to fail
                    // with a BAD_ATTRIBUTE error.
                    if (!mRequestedOpenGLAPI) {
                      configSpec[16] = EGL10.EGL_NONE;
                      Log.i("Skia", "spec: " + configSpec);
                    }
                    
                    if (!egl.eglChooseConfig(display, configSpec, null, 0, value)) {
                        Log.i("Skia", "Could not get MSAA context count: " + mRequestedMSAASampleCount);
                    }

                    numConfigs = value[0];
                }

                if (numConfigs <= 0) {
                    // Try without multisampling.
                    configSpec = new int[] {
                        EGL10.EGL_RED_SIZE, 8,
                        EGL10.EGL_GREEN_SIZE, 8,
                        EGL10.EGL_BLUE_SIZE, 8,
                        EGL10.EGL_ALPHA_SIZE, 8,
                        EGL10.EGL_DEPTH_SIZE, 0,
                        EGL10.EGL_STENCIL_SIZE, 8,
                        EGL10.EGL_RENDERABLE_TYPE, renderableType,
                        EGL10.EGL_NONE
                    };
                    
                    // EGL_RENDERABLE_TYPE is only needed when attempting to use
                    // the OpenGL API (not ES) and causes many EGL drivers to fail
                    // with a BAD_ATTRIBUTE error.
                    if (!mRequestedOpenGLAPI) {
                      configSpec[12] = EGL10.EGL_NONE;
                      Log.i("Skia", "spec: " + configSpec);
                    }

                    if (!egl.eglChooseConfig(display, configSpec, null, 0, value)) {
                      Log.i("Skia", "Could not get non-MSAA context count");
                    }
                    numConfigs = value[0];
                }
            }

            if (numConfigs <= 0) {
                throw new IllegalArgumentException("No configs match configSpec");
            }

            // Get all matching configurations.
            EGLConfig[] configs = new EGLConfig[numConfigs];
            if (!egl.eglChooseConfig(display, configSpec, configs, numConfigs, value)) {
                throw new IllegalArgumentException("Could not get config data");
            }

            for (int i = 0; i < configs.length; ++i) {
                EGLConfig config = configs[i];
                if (findConfigAttrib(egl, display, config , EGL10.EGL_RED_SIZE, 0) == 8 &&
                        findConfigAttrib(egl, display, config, EGL10.EGL_BLUE_SIZE, 0) == 8 &&
                        findConfigAttrib(egl, display, config, EGL10.EGL_GREEN_SIZE, 0) == 8 &&
                        findConfigAttrib(egl, display, config, EGL10.EGL_ALPHA_SIZE, 0) == 8 &&
                        findConfigAttrib(egl, display, config, EGL10.EGL_STENCIL_SIZE, 0) == 8) {
                    return config;
                }
            }

            throw new IllegalArgumentException("Could not find suitable EGL config");
        }

        private int findConfigAttrib(EGL10 egl, EGLDisplay display,
                EGLConfig config, int attribute, int defaultValue) {
            int[] value = new int[1];
            if (egl.eglGetConfigAttrib(display, config, attribute, value)) {
                return value[0];
            }
            return defaultValue;
        }

    }
}