RSSurfaceView.java revision 38da5086a65e8065e85bbca3638da752dba1dbe7
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 android.renderscript;
18
19import java.io.Writer;
20import java.util.ArrayList;
21import java.util.concurrent.Semaphore;
22
23import android.content.Context;
24import android.os.Handler;
25import android.os.Message;
26import android.util.AttributeSet;
27import android.util.Log;
28import android.view.Surface;
29import android.view.SurfaceHolder;
30import android.view.SurfaceView;
31
32/**
33 * The Surface View for a graphics renderscript (RenderScriptGL) to draw on.
34 */
35public class RSSurfaceView extends SurfaceView implements SurfaceHolder.Callback {
36    private SurfaceHolder mSurfaceHolder;
37    private RenderScriptGL mRS;
38
39    /**
40     * Standard View constructor. In order to render something, you
41     * must call {@link android.opengl.GLSurfaceView#setRenderer} to
42     * register a renderer.
43     */
44    public RSSurfaceView(Context context) {
45        super(context);
46        init();
47        //Log.v(RenderScript.LOG_TAG, "RSSurfaceView");
48    }
49
50    /**
51     * Standard View constructor. In order to render something, you
52     * must call {@link android.opengl.GLSurfaceView#setRenderer} to
53     * register a renderer.
54     */
55    public RSSurfaceView(Context context, AttributeSet attrs) {
56        super(context, attrs);
57        init();
58        //Log.v(RenderScript.LOG_TAG, "RSSurfaceView");
59    }
60
61    private void init() {
62        // Install a SurfaceHolder.Callback so we get notified when the
63        // underlying surface is created and destroyed
64        SurfaceHolder holder = getHolder();
65        holder.addCallback(this);
66    }
67
68    /**
69     * This method is part of the SurfaceHolder.Callback interface, and is
70     * not normally called or subclassed by clients of RSSurfaceView.
71     */
72    public void surfaceCreated(SurfaceHolder holder) {
73        mSurfaceHolder = holder;
74    }
75
76    /**
77     * This method is part of the SurfaceHolder.Callback interface, and is
78     * not normally called or subclassed by clients of RSSurfaceView.
79     */
80    public void surfaceDestroyed(SurfaceHolder holder) {
81        synchronized (this) {
82            // Surface will be destroyed when we return
83            if (mRS != null) {
84                mRS.setSurface(null, 0, 0);
85            }
86        }
87    }
88
89    /**
90     * This method is part of the SurfaceHolder.Callback interface, and is
91     * not normally called or subclassed by clients of RSSurfaceView.
92     */
93    public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
94        synchronized (this) {
95            if (mRS != null) {
96                mRS.setSurface(holder, w, h);
97            }
98        }
99    }
100
101   /**
102     * Inform the view that the activity is paused. The owner of this view must
103     * call this method when the activity is paused. Calling this method will
104     * pause the rendering thread.
105     * Must not be called before a renderer has been set.
106     */
107    public void pause() {
108        if(mRS != null) {
109            mRS.pause();
110        }
111    }
112
113    /**
114     * Inform the view that the activity is resumed. The owner of this view must
115     * call this method when the activity is resumed. Calling this method will
116     * recreate the OpenGL display and resume the rendering
117     * thread.
118     * Must not be called before a renderer has been set.
119     */
120    public void resume() {
121        if(mRS != null) {
122            mRS.resume();
123        }
124    }
125
126    public RenderScriptGL createRenderScriptGL(RenderScriptGL.SurfaceConfig sc) {
127      RenderScriptGL rs = new RenderScriptGL(this.getContext(), sc);
128        setRenderScriptGL(rs);
129        return rs;
130    }
131
132    public void destroyRenderScriptGL() {
133        synchronized (this) {
134            mRS.destroy();
135            mRS = null;
136        }
137    }
138
139    public void setRenderScriptGL(RenderScriptGL rs) {
140        mRS = rs;
141    }
142
143    public RenderScriptGL getRenderScriptGL() {
144        return mRS;
145    }
146}
147