1package com.android.galaxy4;
2
3import android.content.Context;
4import android.graphics.PixelFormat;
5import android.renderscript.RSSurfaceView;
6import android.renderscript.RenderScriptGL;
7import android.renderscript.RenderScriptGL.SurfaceConfig;
8import android.view.SurfaceHolder;
9import android.view.WindowManager;
10import android.app.Service;
11import android.util.Log;
12import android.util.DisplayMetrics;
13
14public class GalaxyView extends RSSurfaceView {
15
16    private RenderScriptGL mRS;
17    private GalaxyRS mRender;
18
19    public GalaxyView(Context context) {
20        super(context);
21        setFocusable(true);
22        setFocusableInTouchMode(true);
23    }
24
25    public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
26        super.surfaceChanged(holder, format, w, h);
27        if (mRS == null) {
28            RenderScriptGL.SurfaceConfig sc = new RenderScriptGL.SurfaceConfig();
29            mRS = createRenderScriptGL(sc);
30            mRS.setSurface(holder, w, h);
31
32            DisplayMetrics metrics = new DisplayMetrics();
33            ((WindowManager) getContext()
34                    .getSystemService(Service.WINDOW_SERVICE))
35                    .getDefaultDisplay().getMetrics(metrics);
36
37            mRender = new GalaxyRS();
38            mRender.init(metrics.densityDpi, mRS, getResources(), w, h);
39        } else {
40            mRender.createProgramVertex();
41        }
42    }
43
44    @Override
45    protected void onDetachedFromWindow() {
46        if (mRS != null) {
47            mRS.setSurface(null, 0, 0);
48            mRS = null;
49            destroyRenderScriptGL();
50        }
51    }
52
53}
54