| Jason Sams | 69f0d31 | 2009-08-03 18:11:17 -0700 | [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.io.IOException; |
| 20 | import java.io.InputStream; |
| 21 | |
| 22 | import android.content.res.Resources; |
| 23 | |
| 24 | |
| 25 | /** |
| 26 | * @hide |
| 27 | **/ |
| 28 | public class ScriptC extends Script { |
| 29 | ScriptC(int id, RenderScript rs) { |
| 30 | super(id, rs); |
| 31 | } |
| 32 | |
| 33 | |
| 34 | |
| 35 | |
| 36 | public static class Builder extends Script.Builder { |
| 37 | byte[] mProgram; |
| 38 | int mProgramLength; |
| 39 | |
| 40 | public Builder(RenderScript rs) { |
| 41 | super(rs); |
| 42 | } |
| 43 | |
| 44 | public void setScript(String s) { |
| 45 | try { |
| 46 | mProgram = s.getBytes("UTF-8"); |
| 47 | mProgramLength = mProgram.length; |
| 48 | } catch (java.io.UnsupportedEncodingException e) { |
| 49 | throw new RuntimeException(e); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | public void setScript(Resources resources, int id) { |
| 54 | InputStream is = resources.openRawResource(id); |
| 55 | try { |
| 56 | try { |
| 57 | setScript(is); |
| 58 | } finally { |
| 59 | is.close(); |
| 60 | } |
| 61 | } catch(IOException e) { |
| 62 | throw new Resources.NotFoundException(); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | public void setScript(InputStream is) throws IOException { |
| 67 | byte[] buf = new byte[1024]; |
| 68 | int currentPos = 0; |
| 69 | while(true) { |
| 70 | int bytesLeft = buf.length - currentPos; |
| 71 | if (bytesLeft == 0) { |
| 72 | byte[] buf2 = new byte[buf.length * 2]; |
| 73 | System.arraycopy(buf, 0, buf2, 0, buf.length); |
| 74 | buf = buf2; |
| 75 | bytesLeft = buf.length - currentPos; |
| 76 | } |
| 77 | int bytesRead = is.read(buf, currentPos, bytesLeft); |
| 78 | if (bytesRead <= 0) { |
| 79 | break; |
| 80 | } |
| 81 | currentPos += bytesRead; |
| 82 | } |
| 83 | mProgram = buf; |
| 84 | mProgramLength = currentPos; |
| 85 | } |
| 86 | |
| 87 | static synchronized ScriptC internalCreate(Builder b) { |
| 88 | b.mRS.nScriptCBegin(); |
| 89 | b.transferCreate(); |
| 90 | |
| 91 | b.mRS.nScriptCSetScript(b.mProgram, 0, b.mProgramLength); |
| 92 | |
| 93 | |
| 94 | int id = b.mRS.nScriptCCreate(); |
| 95 | ScriptC obj = new ScriptC(id, b.mRS); |
| 96 | b.transferObject(obj); |
| 97 | return obj; |
| 98 | } |
| 99 | |
| 100 | public ScriptC create() { |
| 101 | return internalCreate(this); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | } |
| 106 | |