| 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 | * |
| Jason Sams | 27676fe | 2010-11-10 17:00:59 -0800 | [diff] [blame] | 33 | * 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 Sams | 704ff64 | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 38 | **/ |
| 39 | public class RenderScriptGL extends RenderScript { |
| 40 | private Surface mSurface; |
| 41 | int mWidth; |
| 42 | int mHeight; |
| 43 | |
| Jason Sams | 27676fe | 2010-11-10 17:00:59 -0800 | [diff] [blame] | 44 | /** |
| 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 Sams | 2222aa9 | 2010-10-10 17:58:25 -0700 | [diff] [blame] | 49 | 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 Sams | 704ff64 | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 61 | |
| Jason Sams | 2222aa9 | 2010-10-10 17:58:25 -0700 | [diff] [blame] | 62 | 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 Sams | c1d6210 | 2010-11-04 14:32:19 -0700 | [diff] [blame] | 81 | throw new RSIllegalArgumentException("Minimum value provided out of range."); |
| Jason Sams | 2222aa9 | 2010-10-10 17:58:25 -0700 | [diff] [blame] | 82 | } |
| 83 | if (upref < umin) { |
| Jason Sams | c1d6210 | 2010-11-04 14:32:19 -0700 | [diff] [blame] | 84 | throw new RSIllegalArgumentException("Prefered must be >= Minimum."); |
| Jason Sams | 2222aa9 | 2010-10-10 17:58:25 -0700 | [diff] [blame] | 85 | } |
| 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 Sams | c1d6210 | 2010-11-04 14:32:19 -0700 | [diff] [blame] | 106 | throw new RSIllegalArgumentException("Quality out of 0-1 range."); |
| Jason Sams | 2222aa9 | 2010-10-10 17:58:25 -0700 | [diff] [blame] | 107 | } |
| 108 | mSamplesMin = minimum; |
| 109 | mSamplesPref = prefered; |
| 110 | mSamplesQ = Q; |
| 111 | } |
| 112 | }; |
| 113 | |
| 114 | SurfaceConfig mSurfaceConfig; |
| 115 | |
| 116 | public void configureSurface(SurfaceHolder sh) { |
| Jason Sams | 11c8af9 | 2010-10-13 15:31:10 -0700 | [diff] [blame] | 117 | if (mSurfaceConfig.mAlphaMin > 1) { |
| 118 | sh.setFormat(PixelFormat.RGBA_8888); |
| 119 | } else { |
| 120 | sh.setFormat(PixelFormat.RGBX_8888); |
| 121 | } |
| Jason Sams | 2222aa9 | 2010-10-10 17:58:25 -0700 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | public void checkSurface(SurfaceHolder sh) { |
| 125 | } |
| 126 | |
| 127 | public RenderScriptGL(SurfaceConfig sc) { |
| 128 | mSurfaceConfig = new SurfaceConfig(sc); |
| 129 | |
| Jason Sams | 704ff64 | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 130 | mSurface = null; |
| 131 | mWidth = 0; |
| 132 | mHeight = 0; |
| 133 | mDev = nDeviceCreate(); |
| Jason Sams | 11c8af9 | 2010-10-13 15:31:10 -0700 | [diff] [blame] | 134 | 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 Sams | d5f0630 | 2010-11-03 14:27:11 -0700 | [diff] [blame] | 141 | if (mContext == 0) { |
| 142 | throw new RSDriverException("Failed to create RS context."); |
| 143 | } |
| Jason Sams | 704ff64 | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 144 | 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 Sams | 5585e36 | 2010-10-29 10:19:21 -0700 | [diff] [blame] | 157 | public int getHeight() { |
| 158 | return mHeight; |
| 159 | } |
| 160 | |
| 161 | public int getWidth() { |
| 162 | return mWidth; |
| 163 | } |
| Jason Sams | 704ff64 | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 164 | |
| 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 Sams | 54db59c | 2010-05-13 18:30:11 -0700 | [diff] [blame] | 181 | public void contextBindProgramStore(ProgramStore p) { |
| Jason Sams | 704ff64 | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 182 | validate(); |
| Jason Sams | 54db59c | 2010-05-13 18:30:11 -0700 | [diff] [blame] | 183 | nContextBindProgramStore(safeID(p)); |
| Jason Sams | 704ff64 | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 184 | } |
| 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 Sams | 704ff64 | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | |