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