SkiaSampleRenderer.java revision dcdd57faf02fb4fd23bb8265392b9c22e068907e
1/*
2 * Copyright 2012 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8package com.skia;
9
10import android.opengl.GLSurfaceView;
11import android.os.Handler;
12
13import javax.microedition.khronos.egl.EGLConfig;
14import javax.microedition.khronos.opengles.GL10;
15
16public class SkiaSampleRenderer implements GLSurfaceView.Renderer {
17
18    private final SkiaSampleView mSampleView;
19    private Handler mHandler = new Handler();
20
21    SkiaSampleRenderer(SkiaSampleView view) {
22        mSampleView = view;
23    }
24
25    @Override
26    public void onDrawFrame(GL10 gl) {
27        draw();
28    }
29
30    @Override
31    public void onSurfaceChanged(GL10 gl, int width, int height) {
32        updateSize(width, height);
33    }
34
35    @Override
36    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
37        gl.glClearStencil(0);
38        gl.glClear(GL10.GL_STENCIL_BUFFER_BIT);
39        init((SkiaSampleActivity)mSampleView.getContext());
40    }
41
42    // Called by JNI
43    private void startTimer(int ms) {
44        // After the delay, queue an event to the Renderer's thread
45        // to handle the event on the timer queue
46        mHandler.postDelayed(new Runnable() {
47            @Override
48            public void run() {
49                mSampleView.queueEvent(new Runnable() {
50                    @Override
51                    public void run() {
52                        serviceQueueTimer();
53                    }
54                });
55            }
56        }, ms);
57    }
58
59    // Called by JNI
60    private void queueSkEvent() {
61        mSampleView.queueEvent(new Runnable() {
62            @Override
63            public void run() {
64                processSkEvent();
65            }
66        });
67    }
68
69    // Called by JNI
70    private void requestRender() {
71        mSampleView.requestRender();
72    }
73
74    native void init(SkiaSampleActivity activity);
75    native void term();
76    native void draw();
77    native void updateSize(int w, int h);
78    native void handleClick(int owner, float x, float y, int state);
79    native void showOverview();
80    native void nextSample();
81    native void previousSample();
82    native void goToSample(int position);
83    native void toggleRenderingMode();
84    native void toggleSlideshow();
85    native void toggleFPS();
86    native void toggleTiling();
87    native void toggleBBox();
88    native void processSkEvent();
89    native void serviceQueueTimer();
90    native void saveToPDF();
91    native void postInval();
92}