• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2010 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.example.plasma.llvm;
17 
18 import android.app.Activity;
19 import android.os.Bundle;
20 import android.content.Context;
21 import android.view.View;
22 import android.view.Menu;
23 import android.view.MenuItem;
24 import android.view.MenuInflater;
25 import android.graphics.Bitmap;
26 import android.graphics.Canvas;
27 import android.graphics.Paint;
28 
29 import java.io.InputStream;
30 import java.io.FileInputStream;
31 import java.io.FileOutputStream;
32 import java.io.BufferedOutputStream;
33 import java.io.IOException;
34 
35 import android.content.res.Resources;
36 
37 public class Plasma extends Activity
38 {
39     /** Called when the activity is first created. */
40     @Override
onCreate(Bundle savedInstanceState)41     public void onCreate(Bundle savedInstanceState)
42     {
43         super.onCreate(savedInstanceState);
44         view = new PlasmaView(this);
45         setContentView(view);
46     }
47 
48     @Override
onCreateOptionsMenu(Menu menu)49     public boolean onCreateOptionsMenu(Menu menu) {
50         MenuInflater inflater = getMenuInflater();
51         inflater.inflate(R.menu.menu, menu);
52         return true;
53     }
54 
55     @Override
onOptionsItemSelected(MenuItem item)56     public boolean onOptionsItemSelected(MenuItem item) {
57         switch (item.getItemId()) {
58             case R.id.switch_mode:
59                 view.switchMode();
60 
61                 return true;
62             default:
63                 return super.onOptionsItemSelected(item);
64         }
65     }
66 
67     private PlasmaView view;
68 
69     /* load our native library */
70     static {
71         System.loadLibrary("plasma");
72     }
73 }
74 
75 class PlasmaView extends View {
76     private Bitmap mBitmap;
77     private long mStartTime;
78 
79     /* implementend by libplasma.so */
nativeRenderPlasma(Bitmap bitmap, long time_ms, byte[] script, int scriptLength, boolean useLLVM)80     private static native int nativeRenderPlasma(Bitmap  bitmap, long time_ms, byte[] script, int scriptLength, boolean useLLVM);
81 
82     private byte[] pgm;
83     private int pgmLength;
84 
85     private boolean mode = true;
86     private Paint paint = null;
87 
switchMode()88     public void switchMode() {
89         mode = !mode;
90     }
91 
PlasmaView(Context context)92     public PlasmaView(Context context) {
93         super(context);
94 
95         mStartTime = System.currentTimeMillis();
96 
97         InputStream is = null;
98         is = getResources().openRawResource(R.raw.libplasma_portable);
99         try {
100             try {
101                 pgm = new byte[1024];
102                 pgmLength = 0;
103                 while(true) {
104                     int bytesLeft = pgm.length - pgmLength;
105                     if (bytesLeft == 0) {
106                         byte[] buf2 = new byte[pgm.length * 2];
107                         System.arraycopy(pgm, 0, buf2, 0, pgm.length);
108                         pgm = buf2;
109                         bytesLeft = pgm.length - pgmLength;
110                     }
111                     int bytesRead = is.read(pgm, pgmLength, bytesLeft);
112                     if (bytesRead <= 0) {
113                         break;
114                     }
115                     pgmLength += bytesRead;
116                 }
117             } finally {
118                 is.close();
119             }
120         } catch(IOException e) {
121             throw new Resources.NotFoundException();
122         }
123         paint = new Paint();
124         paint.setTextSize(40);
125     }
126 
onDraw(Canvas canvas)127     @Override protected void onDraw(Canvas canvas) {
128         if (mBitmap == null || mBitmap.getWidth() != getWidth() || mBitmap.getHeight() != getHeight())
129             mBitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.RGB_565);
130 
131         int frameRate = nativeRenderPlasma(mBitmap, System.currentTimeMillis() - mStartTime, pgm, pgmLength, mode);
132         canvas.drawBitmap(mBitmap, 0, 0, null);
133         canvas.drawText((mode ? "LLVM" : "GCC") + " Frame: " + Integer.toString(frameRate), 100, 100, paint);
134 
135         // force a redraw, with a different time-based pattern.
136         invalidate();
137     }
138 }
139