1/*
2 * Copyright (C) 2011 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
17package com.example.android.rs.helloworld;
18
19import android.renderscript.RSSurfaceView;
20import android.renderscript.RenderScriptGL;
21
22import android.content.Context;
23import android.view.MotionEvent;
24
25public class HelloWorldView extends RSSurfaceView {
26    // Renderscipt context
27    private RenderScriptGL mRS;
28    // Script that does the rendering
29    private HelloWorldRS mRender;
30
31    public HelloWorldView(Context context) {
32        super(context);
33        ensureRenderScript();
34    }
35
36    private void ensureRenderScript() {
37        if (mRS == null) {
38            // Initialize renderscript with desired surface characteristics.
39            // In this case, just use the defaults
40            RenderScriptGL.SurfaceConfig sc = new RenderScriptGL.SurfaceConfig();
41            mRS = createRenderScriptGL(sc);
42            // Create an instance of the script that does the rendering
43            mRender = new HelloWorldRS();
44            mRender.init(mRS, getResources());
45        }
46    }
47
48    @Override
49    protected void onAttachedToWindow() {
50        super.onAttachedToWindow();
51        ensureRenderScript();
52    }
53
54    @Override
55    protected void onDetachedFromWindow() {
56        // Handle the system event and clean up
57        mRender = null;
58        if (mRS != null) {
59            mRS = null;
60            destroyRenderScriptGL();
61        }
62    }
63
64    @Override
65    public boolean onTouchEvent(MotionEvent ev) {
66        // Pass touch events from the system to the rendering script
67        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
68            mRender.onActionDown((int)ev.getX(), (int)ev.getY());
69            return true;
70        }
71
72        return false;
73    }
74}
75
76
77