blob: 181d4bd5aec36202ec29abd4a9047de0264729e6 [file] [log] [blame]
Jason Sams704ff642010-02-09 16:05:07 -08001/*
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.lang.reflect.Field;
20
Jason Sams11c8af92010-10-13 15:31:10 -070021import android.graphics.PixelFormat;
Jason Sams704ff642010-02-09 16:05:07 -080022import android.graphics.Bitmap;
23import android.graphics.BitmapFactory;
24import android.util.Config;
25import android.util.Log;
26import android.view.Surface;
Jason Sams2222aa92010-10-10 17:58:25 -070027import android.view.SurfaceHolder;
28import android.view.SurfaceView;
Jason Sams704ff642010-02-09 16:05:07 -080029
30/**
31 * @hide
32 *
Jason Sams27676fe2010-11-10 17:00:59 -080033 * The Graphics derivitive of RenderScript. Extends the basic context to add a
34 * root script which is the display window for graphical output. When the
35 * system needs to update the display the currently bound root script will be
36 * called. This script is expected to issue the rendering commands to repaint
37 * the screen.
Jason Sams704ff642010-02-09 16:05:07 -080038 **/
39public class RenderScriptGL extends RenderScript {
40 private Surface mSurface;
41 int mWidth;
42 int mHeight;
43
Jason Sams27676fe2010-11-10 17:00:59 -080044 /**
45 * Class which is used to describe a pixel format for a graphical buffer.
46 * This is used to describe the intended format of the display surface.
47 *
48 */
Jason Sams2222aa92010-10-10 17:58:25 -070049 public static class SurfaceConfig {
50 int mDepthMin = 0;
51 int mDepthPref = 0;
52 int mStencilMin = 0;
53 int mStencilPref = 0;
54 int mColorMin = 8;
55 int mColorPref = 8;
56 int mAlphaMin = 0;
57 int mAlphaPref = 0;
58 int mSamplesMin = 1;
59 int mSamplesPref = 1;
60 float mSamplesQ = 1.f;
Jason Sams704ff642010-02-09 16:05:07 -080061
Jason Sams2222aa92010-10-10 17:58:25 -070062 public SurfaceConfig() {
63 }
64
65 public SurfaceConfig(SurfaceConfig sc) {
66 mDepthMin = sc.mDepthMin;
67 mDepthPref = sc.mDepthPref;
68 mStencilMin = sc.mStencilMin;
69 mStencilPref = sc.mStencilPref;
70 mColorMin = sc.mColorMin;
71 mColorPref = sc.mColorPref;
72 mAlphaMin = sc.mAlphaMin;
73 mAlphaPref = sc.mAlphaPref;
74 mSamplesMin = sc.mSamplesMin;
75 mSamplesPref = sc.mSamplesPref;
76 mSamplesQ = sc.mSamplesQ;
77 }
78
79 private void validateRange(int umin, int upref, int rmin, int rmax) {
80 if (umin < rmin || umin > rmax) {
Jason Samsc1d62102010-11-04 14:32:19 -070081 throw new RSIllegalArgumentException("Minimum value provided out of range.");
Jason Sams2222aa92010-10-10 17:58:25 -070082 }
83 if (upref < umin) {
Jason Samsc1d62102010-11-04 14:32:19 -070084 throw new RSIllegalArgumentException("Prefered must be >= Minimum.");
Jason Sams2222aa92010-10-10 17:58:25 -070085 }
86 }
87
88 public void setColor(int minimum, int prefered) {
89 validateRange(minimum, prefered, 5, 8);
90 mColorMin = minimum;
91 mColorPref = prefered;
92 }
93 public void setAlpha(int minimum, int prefered) {
94 validateRange(minimum, prefered, 0, 8);
95 mAlphaMin = minimum;
96 mAlphaPref = prefered;
97 }
98 public void setDepth(int minimum, int prefered) {
99 validateRange(minimum, prefered, 0, 24);
100 mDepthMin = minimum;
101 mDepthPref = prefered;
102 }
103 public void setSamples(int minimum, int prefered, float Q) {
104 validateRange(minimum, prefered, 0, 24);
105 if (Q < 0.0f || Q > 1.0f) {
Jason Samsc1d62102010-11-04 14:32:19 -0700106 throw new RSIllegalArgumentException("Quality out of 0-1 range.");
Jason Sams2222aa92010-10-10 17:58:25 -0700107 }
108 mSamplesMin = minimum;
109 mSamplesPref = prefered;
110 mSamplesQ = Q;
111 }
112 };
113
114 SurfaceConfig mSurfaceConfig;
115
116 public void configureSurface(SurfaceHolder sh) {
Jason Sams11c8af92010-10-13 15:31:10 -0700117 if (mSurfaceConfig.mAlphaMin > 1) {
118 sh.setFormat(PixelFormat.RGBA_8888);
119 } else {
120 sh.setFormat(PixelFormat.RGBX_8888);
121 }
Jason Sams2222aa92010-10-10 17:58:25 -0700122 }
123
124 public void checkSurface(SurfaceHolder sh) {
125 }
126
127 public RenderScriptGL(SurfaceConfig sc) {
128 mSurfaceConfig = new SurfaceConfig(sc);
129
Jason Sams704ff642010-02-09 16:05:07 -0800130 mSurface = null;
131 mWidth = 0;
132 mHeight = 0;
133 mDev = nDeviceCreate();
Jason Sams11c8af92010-10-13 15:31:10 -0700134 mContext = nContextCreateGL(mDev, 0,
135 mSurfaceConfig.mColorMin, mSurfaceConfig.mColorPref,
136 mSurfaceConfig.mAlphaMin, mSurfaceConfig.mAlphaPref,
137 mSurfaceConfig.mDepthMin, mSurfaceConfig.mDepthPref,
138 mSurfaceConfig.mStencilMin, mSurfaceConfig.mStencilPref,
139 mSurfaceConfig.mSamplesMin, mSurfaceConfig.mSamplesPref,
140 mSurfaceConfig.mSamplesQ);
Jason Samsd5f06302010-11-03 14:27:11 -0700141 if (mContext == 0) {
142 throw new RSDriverException("Failed to create RS context.");
143 }
Jason Sams704ff642010-02-09 16:05:07 -0800144 mMessageThread = new MessageThread(this);
145 mMessageThread.start();
146 Element.initPredefined(this);
147 }
148
149 public void contextSetSurface(int w, int h, Surface sur) {
150 mSurface = sur;
151 mWidth = w;
152 mHeight = h;
153 validate();
154 nContextSetSurface(w, h, mSurface);
155 }
156
Jason Sams5585e362010-10-29 10:19:21 -0700157 public int getHeight() {
158 return mHeight;
159 }
160
161 public int getWidth() {
162 return mWidth;
163 }
Jason Sams704ff642010-02-09 16:05:07 -0800164
165 void pause() {
166 validate();
167 nContextPause();
168 }
169
170 void resume() {
171 validate();
172 nContextResume();
173 }
174
175
176 public void contextBindRootScript(Script s) {
177 validate();
178 nContextBindRootScript(safeID(s));
179 }
180
Jason Sams54db59c2010-05-13 18:30:11 -0700181 public void contextBindProgramStore(ProgramStore p) {
Jason Sams704ff642010-02-09 16:05:07 -0800182 validate();
Jason Sams54db59c2010-05-13 18:30:11 -0700183 nContextBindProgramStore(safeID(p));
Jason Sams704ff642010-02-09 16:05:07 -0800184 }
185
186 public void contextBindProgramFragment(ProgramFragment p) {
187 validate();
188 nContextBindProgramFragment(safeID(p));
189 }
190
191 public void contextBindProgramRaster(ProgramRaster p) {
192 validate();
193 nContextBindProgramRaster(safeID(p));
194 }
195
196 public void contextBindProgramVertex(ProgramVertex p) {
197 validate();
198 nContextBindProgramVertex(safeID(p));
199 }
200
Jason Sams704ff642010-02-09 16:05:07 -0800201}
202
203