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 
8 package com.skia;
9 
10 import android.opengl.GLSurfaceView;
11 import android.os.Handler;
12 import android.util.Log;
13 
14 import javax.microedition.khronos.egl.EGLConfig;
15 import javax.microedition.khronos.opengles.GL10;
16 import javax.microedition.khronos.opengles.GL11;
17 
18 public class SkiaSampleRenderer implements GLSurfaceView.Renderer {
19 
20     private final SkiaSampleView mSampleView;
21     private Handler mHandler = new Handler();
22     private int mMSAASampleCount;
23     private String mCmdLineFlags;
24 
SkiaSampleRenderer(SkiaSampleView view, String cmdLineFlags)25     SkiaSampleRenderer(SkiaSampleView view, String cmdLineFlags) {
26         mSampleView = view;
27         mCmdLineFlags = cmdLineFlags;
28     }
29 
30     @Override
onDrawFrame(GL10 gl)31     public void onDrawFrame(GL10 gl) {
32         draw();
33     }
34 
35     @Override
onSurfaceChanged(GL10 gl, int width, int height)36     public void onSurfaceChanged(GL10 gl, int width, int height) {
37         updateSize(width, height);
38     }
39 
40     @Override
onSurfaceCreated(GL10 gl, EGLConfig config)41     public void onSurfaceCreated(GL10 gl, EGLConfig config) {
42         if (gl instanceof GL11) {
43             int value[] = new int[1];
44             ((GL11) gl).glGetIntegerv(GL11.GL_SAMPLES, value, 0);
45             if (value[0] == 1) {
46                 mMSAASampleCount = 0;
47             } else {
48                 mMSAASampleCount = value[0];
49             }
50         }
51 
52         gl.glClearStencil(0);
53         gl.glClear(GL10.GL_STENCIL_BUFFER_BIT);
54 
55         init((SkiaSampleActivity)mSampleView.getContext(), mCmdLineFlags, mMSAASampleCount);
56     }
57 
58     // Called by JNI and the view.
getMSAASampleCount()59     synchronized public int getMSAASampleCount() {
60         return mMSAASampleCount;
61     }
62 
63     // Called by JNI
startTimer(int ms)64     private void startTimer(int ms) {
65         // After the delay, queue an event to the Renderer's thread
66         // to handle the event on the timer queue
67         mHandler.postDelayed(new Runnable() {
68             @Override
69             public void run() {
70                 mSampleView.queueEvent(new Runnable() {
71                     @Override
72                     public void run() {
73                         serviceQueueTimer();
74                     }
75                 });
76             }
77         }, ms);
78     }
79 
80     // Called by JNI
queueSkEvent()81     private void queueSkEvent() {
82         mSampleView.queueEvent(new Runnable() {
83             @Override
84             public void run() {
85                 processSkEvent();
86             }
87         });
88     }
89 
90     // Called by JNI
requestRender()91     private void requestRender() {
92         mSampleView.requestRender();
93     }
94 
init(SkiaSampleActivity activity, String flags, int msaaSampleCount)95     native void init(SkiaSampleActivity activity, String flags, int msaaSampleCount);
term()96     native void term();
draw()97     native void draw();
updateSize(int w, int h)98     native void updateSize(int w, int h);
handleClick(int owner, float x, float y, int state)99     native void handleClick(int owner, float x, float y, int state);
showOverview()100     native void showOverview();
nextSample()101     native void nextSample();
previousSample()102     native void previousSample();
goToSample(int position)103     native void goToSample(int position);
toggleRenderingMode()104     native void toggleRenderingMode();
toggleSlideshow()105     native void toggleSlideshow();
toggleFPS()106     native void toggleFPS();
toggleTiling()107     native void toggleTiling();
toggleBBox()108     native void toggleBBox();
processSkEvent()109     native void processSkEvent();
serviceQueueTimer()110     native void serviceQueueTimer();
saveToPDF()111     native void saveToPDF();
postInval()112     native void postInval();
113 }