| Jason Sams | 704ff64 | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 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 | |
| 17 | package android.renderscript; |
| 18 | |
| 19 | import java.lang.reflect.Field; |
| 20 | |
| Jason Sams | 11c8af9 | 2010-10-13 15:31:10 -0700 | [diff] [blame] | 21 | import android.graphics.PixelFormat; |
| Jason Sams | 704ff64 | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 22 | import android.graphics.Bitmap; |
| 23 | import android.graphics.BitmapFactory; |
| 24 | import android.util.Config; |
| 25 | import android.util.Log; |
| 26 | import android.view.Surface; |
| Jason Sams | 2222aa9 | 2010-10-10 17:58:25 -0700 | [diff] [blame] | 27 | import android.view.SurfaceHolder; |
| 28 | import android.view.SurfaceView; |
| Jason Sams | 704ff64 | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 29 | |
| 30 | /** |
| 31 | * @hide |
| 32 | * |
| 33 | **/ |
| 34 | public class RenderScriptGL extends RenderScript { |
| 35 | private Surface mSurface; |
| 36 | int mWidth; |
| 37 | int mHeight; |
| 38 | |
| Jason Sams | 2222aa9 | 2010-10-10 17:58:25 -0700 | [diff] [blame] | 39 | public static class SurfaceConfig { |
| 40 | int mDepthMin = 0; |
| 41 | int mDepthPref = 0; |
| 42 | int mStencilMin = 0; |
| 43 | int mStencilPref = 0; |
| 44 | int mColorMin = 8; |
| 45 | int mColorPref = 8; |
| 46 | int mAlphaMin = 0; |
| 47 | int mAlphaPref = 0; |
| 48 | int mSamplesMin = 1; |
| 49 | int mSamplesPref = 1; |
| 50 | float mSamplesQ = 1.f; |
| Jason Sams | 704ff64 | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 51 | |
| Jason Sams | 2222aa9 | 2010-10-10 17:58:25 -0700 | [diff] [blame] | 52 | public SurfaceConfig() { |
| 53 | } |
| 54 | |
| 55 | public SurfaceConfig(SurfaceConfig sc) { |
| 56 | mDepthMin = sc.mDepthMin; |
| 57 | mDepthPref = sc.mDepthPref; |
| 58 | mStencilMin = sc.mStencilMin; |
| 59 | mStencilPref = sc.mStencilPref; |
| 60 | mColorMin = sc.mColorMin; |
| 61 | mColorPref = sc.mColorPref; |
| 62 | mAlphaMin = sc.mAlphaMin; |
| 63 | mAlphaPref = sc.mAlphaPref; |
| 64 | mSamplesMin = sc.mSamplesMin; |
| 65 | mSamplesPref = sc.mSamplesPref; |
| 66 | mSamplesQ = sc.mSamplesQ; |
| 67 | } |
| 68 | |
| 69 | private void validateRange(int umin, int upref, int rmin, int rmax) { |
| 70 | if (umin < rmin || umin > rmax) { |
| 71 | throw new IllegalArgumentException("Minimum value provided out of range."); |
| 72 | } |
| 73 | if (upref < umin) { |
| 74 | throw new IllegalArgumentException("Prefered must be >= Minimum."); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | public void setColor(int minimum, int prefered) { |
| 79 | validateRange(minimum, prefered, 5, 8); |
| 80 | mColorMin = minimum; |
| 81 | mColorPref = prefered; |
| 82 | } |
| 83 | public void setAlpha(int minimum, int prefered) { |
| 84 | validateRange(minimum, prefered, 0, 8); |
| 85 | mAlphaMin = minimum; |
| 86 | mAlphaPref = prefered; |
| 87 | } |
| 88 | public void setDepth(int minimum, int prefered) { |
| 89 | validateRange(minimum, prefered, 0, 24); |
| 90 | mDepthMin = minimum; |
| 91 | mDepthPref = prefered; |
| 92 | } |
| 93 | public void setSamples(int minimum, int prefered, float Q) { |
| 94 | validateRange(minimum, prefered, 0, 24); |
| 95 | if (Q < 0.0f || Q > 1.0f) { |
| 96 | throw new IllegalArgumentException("Quality out of 0-1 range."); |
| 97 | } |
| 98 | mSamplesMin = minimum; |
| 99 | mSamplesPref = prefered; |
| 100 | mSamplesQ = Q; |
| 101 | } |
| 102 | }; |
| 103 | |
| 104 | SurfaceConfig mSurfaceConfig; |
| 105 | |
| 106 | public void configureSurface(SurfaceHolder sh) { |
| Jason Sams | 11c8af9 | 2010-10-13 15:31:10 -0700 | [diff] [blame] | 107 | if (mSurfaceConfig.mAlphaMin > 1) { |
| 108 | sh.setFormat(PixelFormat.RGBA_8888); |
| 109 | } else { |
| 110 | sh.setFormat(PixelFormat.RGBX_8888); |
| 111 | } |
| Jason Sams | 2222aa9 | 2010-10-10 17:58:25 -0700 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | public void checkSurface(SurfaceHolder sh) { |
| 115 | } |
| 116 | |
| 117 | public RenderScriptGL(SurfaceConfig sc) { |
| 118 | mSurfaceConfig = new SurfaceConfig(sc); |
| 119 | |
| Jason Sams | 704ff64 | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 120 | mSurface = null; |
| 121 | mWidth = 0; |
| 122 | mHeight = 0; |
| 123 | mDev = nDeviceCreate(); |
| Jason Sams | 11c8af9 | 2010-10-13 15:31:10 -0700 | [diff] [blame] | 124 | mContext = nContextCreateGL(mDev, 0, |
| 125 | mSurfaceConfig.mColorMin, mSurfaceConfig.mColorPref, |
| 126 | mSurfaceConfig.mAlphaMin, mSurfaceConfig.mAlphaPref, |
| 127 | mSurfaceConfig.mDepthMin, mSurfaceConfig.mDepthPref, |
| 128 | mSurfaceConfig.mStencilMin, mSurfaceConfig.mStencilPref, |
| 129 | mSurfaceConfig.mSamplesMin, mSurfaceConfig.mSamplesPref, |
| 130 | mSurfaceConfig.mSamplesQ); |
| Jason Sams | d5f0630 | 2010-11-03 14:27:11 -0700 | [diff] [blame] | 131 | if (mContext == 0) { |
| 132 | throw new RSDriverException("Failed to create RS context."); |
| 133 | } |
| Jason Sams | 704ff64 | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 134 | mMessageThread = new MessageThread(this); |
| 135 | mMessageThread.start(); |
| 136 | Element.initPredefined(this); |
| 137 | } |
| 138 | |
| 139 | public void contextSetSurface(int w, int h, Surface sur) { |
| 140 | mSurface = sur; |
| 141 | mWidth = w; |
| 142 | mHeight = h; |
| 143 | validate(); |
| 144 | nContextSetSurface(w, h, mSurface); |
| 145 | } |
| 146 | |
| Jason Sams | 5585e36 | 2010-10-29 10:19:21 -0700 | [diff] [blame] | 147 | public int getHeight() { |
| 148 | return mHeight; |
| 149 | } |
| 150 | |
| 151 | public int getWidth() { |
| 152 | return mWidth; |
| 153 | } |
| Jason Sams | 704ff64 | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 154 | |
| 155 | void pause() { |
| 156 | validate(); |
| 157 | nContextPause(); |
| 158 | } |
| 159 | |
| 160 | void resume() { |
| 161 | validate(); |
| 162 | nContextResume(); |
| 163 | } |
| 164 | |
| 165 | |
| 166 | public void contextBindRootScript(Script s) { |
| 167 | validate(); |
| 168 | nContextBindRootScript(safeID(s)); |
| 169 | } |
| 170 | |
| Jason Sams | 54db59c | 2010-05-13 18:30:11 -0700 | [diff] [blame] | 171 | public void contextBindProgramStore(ProgramStore p) { |
| Jason Sams | 704ff64 | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 172 | validate(); |
| Jason Sams | 54db59c | 2010-05-13 18:30:11 -0700 | [diff] [blame] | 173 | nContextBindProgramStore(safeID(p)); |
| Jason Sams | 704ff64 | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | public void contextBindProgramFragment(ProgramFragment p) { |
| 177 | validate(); |
| 178 | nContextBindProgramFragment(safeID(p)); |
| 179 | } |
| 180 | |
| 181 | public void contextBindProgramRaster(ProgramRaster p) { |
| 182 | validate(); |
| 183 | nContextBindProgramRaster(safeID(p)); |
| 184 | } |
| 185 | |
| 186 | public void contextBindProgramVertex(ProgramVertex p) { |
| 187 | validate(); |
| 188 | nContextBindProgramVertex(safeID(p)); |
| 189 | } |
| 190 | |
| 191 | |
| 192 | |
| 193 | |
| 194 | ////////////////////////////////////////////////////////////////////////////////// |
| 195 | // File |
| 196 | |
| 197 | public class File extends BaseObj { |
| 198 | File(int id) { |
| Alex Sakhartchouk | 0de9444 | 2010-08-11 14:41:28 -0700 | [diff] [blame] | 199 | super(id, RenderScriptGL.this); |
| Jason Sams | 704ff64 | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 200 | } |
| 201 | } |
| 202 | |
| 203 | public File fileOpen(String s) throws IllegalStateException, IllegalArgumentException |
| 204 | { |
| 205 | if(s.length() < 1) { |
| 206 | throw new IllegalArgumentException("fileOpen does not accept a zero length string."); |
| 207 | } |
| 208 | |
| 209 | try { |
| 210 | byte[] bytes = s.getBytes("UTF-8"); |
| 211 | int id = nFileOpen(bytes); |
| 212 | return new File(id); |
| 213 | } catch (java.io.UnsupportedEncodingException e) { |
| 214 | throw new RuntimeException(e); |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | } |
| 219 | |
| 220 | |