RsRenderStatesView.java revision e5f2f66f8c802d64ecf869081036ae13d4e9e19c
1/*
2 * Copyright (C) 2008 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.miscsamples;
18
19import android.renderscript.RSSurfaceView;
20import android.renderscript.RenderScriptGL;
21
22import android.content.Context;
23import android.view.MotionEvent;
24import android.view.SurfaceHolder;
25
26public class RsRenderStatesView extends RSSurfaceView {
27
28    public RsRenderStatesView(Context context) {
29        super(context);
30        ensureRenderScript();
31    }
32
33    private RenderScriptGL mRS;
34    private RsRenderStatesRS mRender;
35
36    private void ensureRenderScript() {
37        if (mRS == null) {
38            RenderScriptGL.SurfaceConfig sc = new RenderScriptGL.SurfaceConfig();
39            sc.setDepth(16, 24);
40            mRS = createRenderScriptGL(sc);
41            mRender = new RsRenderStatesRS();
42            mRender.init(mRS, getResources());
43        }
44    }
45
46    @Override
47    protected void onAttachedToWindow() {
48        super.onAttachedToWindow();
49        ensureRenderScript();
50    }
51
52    @Override
53    public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
54        super.surfaceChanged(holder, format, w, h);
55        mRender.surfaceChanged();
56    }
57
58    @Override
59    protected void onDetachedFromWindow() {
60        mRender = null;
61        if (mRS != null) {
62            mRS = null;
63            destroyRenderScriptGL();
64        }
65    }
66
67    @Override
68    public boolean onTouchEvent(MotionEvent ev) {
69        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
70            mRender.onActionDown((int)ev.getX(), (int)ev.getY());
71            return true;
72        }
73
74        return false;
75    }
76}
77
78
79