Plasma.java revision 6a5fd5f0c6aa00bfbc4473007a349710e026bb10
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 */
16package com.example.plasma.llvm;
17
18import android.app.Activity;
19import android.os.Bundle;
20import android.content.Context;
21import android.view.View;
22import android.view.Menu;
23import android.view.MenuItem;
24import android.view.MenuInflater;
25import android.graphics.Bitmap;
26import android.graphics.Canvas;
27import android.graphics.Paint;
28
29import java.io.InputStream;
30import java.io.FileInputStream;
31import java.io.FileOutputStream;
32import java.io.BufferedOutputStream;
33import java.io.IOException;
34
35import android.content.res.Resources;
36
37public class Plasma extends Activity
38{
39    /** Called when the activity is first created. */
40    @Override
41    public void onCreate(Bundle savedInstanceState)
42    {
43        super.onCreate(savedInstanceState);
44        view = new PlasmaView(this);
45        setContentView(view);
46    }
47
48    @Override
49    public boolean onCreateOptionsMenu(Menu menu) {
50        MenuInflater inflater = getMenuInflater();
51        inflater.inflate(R.menu.menu, menu);
52        return true;
53    }
54
55    @Override
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
75class PlasmaView extends View {
76    private Bitmap mBitmap;
77    private long mStartTime;
78
79    /* implementend by libplasma.so */
80    private static native boolean gdk();
81    private static native int nativeRenderPlasma(Bitmap  bitmap, long time_ms, byte[] script, int scriptLength, boolean useLLVM);
82
83    private byte[] pgm;
84    private int pgmLength;
85
86    private boolean llvm_mode = false;
87    private Paint paint = null;
88
89    public void switchMode() {
90        if (gdk())
91            llvm_mode = !llvm_mode;
92    }
93
94    public PlasmaView(Context context) {
95        super(context);
96
97        llvm_mode = gdk();
98
99        mStartTime = System.currentTimeMillis();
100        if (llvm_mode)
101        {
102            InputStream is = null;
103            is = getResources().openRawResource(R.raw.libplasma_portable);
104            try {
105                try {
106                    pgm = new byte[1024];
107                    pgmLength = 0;
108                    while(true) {
109                        int bytesLeft = pgm.length - pgmLength;
110                        if (bytesLeft == 0) {
111                            byte[] buf2 = new byte[pgm.length * 2];
112                            System.arraycopy(pgm, 0, buf2, 0, pgm.length);
113                            pgm = buf2;
114                            bytesLeft = pgm.length - pgmLength;
115                        }
116                        int bytesRead = is.read(pgm, pgmLength, bytesLeft);
117                        if (bytesRead <= 0) {
118                            break;
119                        }
120                        pgmLength += bytesRead;
121                    }
122                } finally {
123                    is.close();
124                }
125            } catch(IOException e) {
126                throw new Resources.NotFoundException();
127            }
128        }
129
130        paint = new Paint();
131        paint.setTextSize(40);
132    }
133
134    @Override protected void onDraw(Canvas canvas) {
135        if (mBitmap == null || mBitmap.getWidth() != getWidth() || mBitmap.getHeight() != getHeight())
136            mBitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.RGB_565);
137
138        int frameRate = nativeRenderPlasma(mBitmap, System.currentTimeMillis() - mStartTime, pgm, pgmLength, llvm_mode);
139        canvas.drawBitmap(mBitmap, 0, 0, null);
140        canvas.drawText((llvm_mode ? "LLVM/GDK" : "Native") + " Frame: " + Integer.toString(frameRate), 100, 100, paint);
141
142        // force a redraw, with a different time-based pattern.
143        invalidate();
144    }
145}
146