diff options
Diffstat (limited to 'graphics/java/android')
34 files changed, 1774 insertions, 383 deletions
diff --git a/graphics/java/android/graphics/Canvas.java b/graphics/java/android/graphics/Canvas.java index 76cde73..aec977a 100644 --- a/graphics/java/android/graphics/Canvas.java +++ b/graphics/java/android/graphics/Canvas.java @@ -16,11 +16,10 @@ package android.graphics; -import android.text.TextUtils; -import android.text.SpannedString; -import android.text.SpannableString; import android.text.GraphicsOperations; -import android.util.DisplayMetrics; +import android.text.SpannableString; +import android.text.SpannedString; +import android.text.TextUtils; import javax.microedition.khronos.opengles.GL; @@ -59,6 +58,18 @@ public class Canvas { private int mSurfaceFormat; /** + * Flag for drawTextRun indicating left-to-right run direction. + * @hide + */ + public static final int DIRECTION_LTR = 0; + + /** + * Flag for drawTextRun indicating right-to-left run direction. + * @hide + */ + public static final int DIRECTION_RTL = 1; + + /** * Construct an empty raster canvas. Use setBitmap() to specify a bitmap to * draw into. The initial target density is {@link Bitmap#DENSITY_NONE}; * this will typically be replaced when a target bitmap is set for the @@ -1246,8 +1257,8 @@ public class Canvas { (text.length - index - count)) < 0) { throw new IndexOutOfBoundsException(); } - native_drawText(mNativeCanvas, text, index, count, x, y, - paint.mNativePaint); + native_drawText(mNativeCanvas, text, index, count, x, y, paint.mBidiFlags, + paint.mNativePaint); } /** @@ -1259,7 +1270,10 @@ public class Canvas { * @param y The y-coordinate of the origin of the text being drawn * @param paint The paint used for the text (e.g. color, size, style) */ - public native void drawText(String text, float x, float y, Paint paint); + public void drawText(String text, float x, float y, Paint paint) { + native_drawText(mNativeCanvas, text, 0, text.length(), x, y, paint.mBidiFlags, + paint.mNativePaint); + } /** * Draw the text, with origin at (x,y), using the specified paint. @@ -1277,8 +1291,8 @@ public class Canvas { if ((start | end | (end - start) | (text.length() - end)) < 0) { throw new IndexOutOfBoundsException(); } - native_drawText(mNativeCanvas, text, start, end, x, y, - paint.mNativePaint); + native_drawText(mNativeCanvas, text, start, end, x, y, paint.mBidiFlags, + paint.mNativePaint); } /** @@ -1299,16 +1313,100 @@ public class Canvas { if (text instanceof String || text instanceof SpannedString || text instanceof SpannableString) { native_drawText(mNativeCanvas, text.toString(), start, end, x, y, - paint.mNativePaint); - } - else if (text instanceof GraphicsOperations) { + paint.mBidiFlags, paint.mNativePaint); + } else if (text instanceof GraphicsOperations) { ((GraphicsOperations) text).drawText(this, start, end, x, y, paint); + } else { + char[] buf = TemporaryBuffer.obtain(end - start); + TextUtils.getChars(text, start, end, buf, 0); + native_drawText(mNativeCanvas, buf, 0, end - start, x, y, + paint.mBidiFlags, paint.mNativePaint); + TemporaryBuffer.recycle(buf); } - else { + } + + /** + * Render a run of all LTR or all RTL text, with shaping. This does not run + * bidi on the provided text, but renders it as a uniform right-to-left or + * left-to-right run, as indicated by dir. Alignment of the text is as + * determined by the Paint's TextAlign value. + * + * @param text the text to render + * @param index the start of the text to render. Data before this position + * can be used for shaping context. + * @param length the length of the text to render. Data at or after this + * position (start + length) can be used for shaping context. + * @param x the x position at which to draw the text + * @param y the y position at which to draw the text + * @param dir the run direction, either {@link DIRECTION_LTR} or + * {@link DIRECTION_RTL}. + * @param paint the paint + * @hide + */ + public void drawTextRun(char[] text, int index, int length, float x, float y, int dir, + Paint paint) { + + if (text == null) { + throw new NullPointerException("text is null"); + } + if (paint == null) { + throw new NullPointerException("paint is null"); + } + if ((index | length | text.length - index - length) < 0) { + throw new IndexOutOfBoundsException(); + } + if (dir != DIRECTION_LTR && dir != DIRECTION_RTL) { + throw new IllegalArgumentException("unknown dir: " + dir); + } + + native_drawTextRun(mNativeCanvas, text, index, length, x, y, dir, paint.mNativePaint); + } + + /** + * Render a run of all LTR or all RTL text, with shaping. This does not run + * bidi on the provided text, but renders it as a uniform right-to-left or + * left-to-right run, as indicated by dir. Alignment of the text is as + * determined by the Paint's TextAlign value. + * + * @param text the text to render + * @param start the start of the text to render. Data before this position + * can be used for shaping context. + * @param end the end of the text to render. Data at or after this + * position can be used for shaping context. + * @param x the x position at which to draw the text + * @param y the y position at which to draw the text + * @param dir the run direction, either 0 for LTR or 1 for RTL. + * @param paint the paint + * @hide + */ + public void drawTextRun(CharSequence text, int start, int end, float x, + float y, int dir, Paint paint) { + + if (text == null) { + throw new NullPointerException("text is null"); + } + if (paint == null) { + throw new NullPointerException("paint is null"); + } + if ((start | end | end - start | text.length() - end) < 0) { + throw new IndexOutOfBoundsException(); + } + + int flags = dir == 0 ? 0 : 1; + + if (text instanceof String || text instanceof SpannedString || + text instanceof SpannableString) { + native_drawTextRun(mNativeCanvas, text.toString(), start, end, x, y, + flags, paint.mNativePaint); + } else if (text instanceof GraphicsOperations) { + ((GraphicsOperations) text).drawTextRun(this, start, end, x, y, flags, + paint); + } else { char[] buf = TemporaryBuffer.obtain(end - start); TextUtils.getChars(text, start, end, buf, 0); - drawText(buf, 0, end - start, x, y, paint); + native_drawTextRun(mNativeCanvas, buf, 0, end - start, x, y, + flags, paint.mNativePaint); TemporaryBuffer.recycle(buf); } } @@ -1555,10 +1653,17 @@ public class Canvas { private static native void native_drawText(int nativeCanvas, char[] text, int index, int count, float x, - float y, int paint); + float y, int flags, int paint); private static native void native_drawText(int nativeCanvas, String text, int start, int end, float x, - float y, int paint); + float y, int flags, int paint); + + private static native void native_drawTextRun(int nativeCanvas, String + text, int start, int end, float x, float y, int flags, int paint); + + private static native void native_drawTextRun(int nativeCanvas, char[] + text, int start, int len, float x, float y, int flags, int paint); + private static native void native_drawPosText(int nativeCanvas, char[] text, int index, int count, float[] pos, diff --git a/graphics/java/android/graphics/Paint.java b/graphics/java/android/graphics/Paint.java index 3e3f87b..b564929 100644 --- a/graphics/java/android/graphics/Paint.java +++ b/graphics/java/android/graphics/Paint.java @@ -16,10 +16,10 @@ package android.graphics; -import android.text.TextUtils; +import android.text.GraphicsOperations; import android.text.SpannableString; import android.text.SpannedString; -import android.text.GraphicsOperations; +import android.text.TextUtils; /** * The Paint class holds the style and color information about how to draw @@ -39,6 +39,7 @@ public class Paint { private boolean mHasCompatScaling; private float mCompatScaling; private float mInvCompatScaling; + /* package */ int mBidiFlags = BIDI_DEFAULT_LTR; private static final Style[] sStyleArray = { Style.FILL, Style.STROKE, Style.FILL_AND_STROKE @@ -76,8 +77,64 @@ public class Paint { private static final int DEFAULT_PAINT_FLAGS = DEV_KERN_TEXT_FLAG; /** - * The Style specifies if the primitive being drawn is filled, - * stroked, or both (in the same color). The default is FILL. + * Bidi flag to set LTR paragraph direction. + * + * @hide + */ + public static final int BIDI_LTR = 0x0; + + /** + * Bidi flag to set RTL paragraph direction. + * + * @hide + */ + public static final int BIDI_RTL = 0x1; + + /** + * Bidi flag to detect paragraph direction via heuristics, defaulting to + * LTR. + * + * @hide + */ + public static final int BIDI_DEFAULT_LTR = 0x2; + + /** + * Bidi flag to detect paragraph direction via heuristics, defaulting to + * RTL. + * + * @hide + */ + public static final int BIDI_DEFAULT_RTL = 0x3; + + /** + * Bidi flag to override direction to all LTR (ignore bidi). + * + * @hide + */ + public static final int BIDI_FORCE_LTR = 0x4; + + /** + * Bidi flag to override direction to all RTL (ignore bidi). + * + * @hide + */ + public static final int BIDI_FORCE_RTL = 0x5; + + /** + * Maximum Bidi flag value. + * @hide + */ + private static final int BIDI_MAX_FLAG_VALUE = BIDI_FORCE_RTL; + + /** + * Mask for bidi flags. + * @hide + */ + private static final int BIDI_FLAG_MASK = 0x7; + + /** + * The Style specifies if the primitive being drawn is filled, stroked, or + * both (in the same color). The default is FILL. */ public enum Style { /** @@ -93,7 +150,9 @@ public class Paint { /** * Geometry and text drawn with this style will be both filled and * stroked at the same time, respecting the stroke-related fields on - * the paint. + * the paint. This mode can give unexpected results if the geometry + * is oriented counter-clockwise. This restriction does not apply to + * either FILL or STROKE. */ FILL_AND_STROKE (2); @@ -208,6 +267,7 @@ public class Paint { mHasCompatScaling = paint.mHasCompatScaling; mCompatScaling = paint.mCompatScaling; mInvCompatScaling = paint.mInvCompatScaling; + mBidiFlags = paint.mBidiFlags; } /** Restores the paint to its default settings. */ @@ -216,6 +276,7 @@ public class Paint { setFlags(DEFAULT_PAINT_FLAGS); mHasCompatScaling = false; mCompatScaling = mInvCompatScaling = 1; + mBidiFlags = BIDI_DEFAULT_LTR; } /** @@ -238,6 +299,7 @@ public class Paint { mHasCompatScaling = src.mHasCompatScaling; mCompatScaling = src.mCompatScaling; mInvCompatScaling = src.mInvCompatScaling; + mBidiFlags = src.mBidiFlags; } } @@ -252,10 +314,33 @@ public class Paint { mInvCompatScaling = 1.0f/factor; } } - + + /** + * Return the bidi flags on the paint. + * + * @return the bidi flags on the paint + * @hide + */ + public int getBidiFlags() { + return mBidiFlags; + } + + /** + * Set the bidi flags on the paint. + * @hide + */ + public void setBidiFlags(int flags) { + // only flag value is the 3-bit BIDI control setting + flags &= BIDI_FLAG_MASK; + if (flags > BIDI_MAX_FLAG_VALUE) { + throw new IllegalArgumentException("unknown bidi flag: " + flags); + } + mBidiFlags = flags; + } + /** * Return the paint's flags. Use the Flag enum to test flag values. - * + * * @return the paint's flags (see enums ending in _Flag for bit masks) */ public native int getFlags(); diff --git a/graphics/java/android/graphics/PorterDuff.java b/graphics/java/android/graphics/PorterDuff.java index 3904234..0e405c2 100644 --- a/graphics/java/android/graphics/PorterDuff.java +++ b/graphics/java/android/graphics/PorterDuff.java @@ -53,7 +53,10 @@ public class PorterDuff { /** [Sa * Da, Sc * Dc] */ MULTIPLY (14), /** [Sa + Da - Sa * Da, Sc + Dc - Sc * Dc] */ - SCREEN (15); + SCREEN (15), + /** Saturate(S + D) */ + ADD (16), + OVERLAY (17); Mode(int nativeInt) { this.nativeInt = nativeInt; diff --git a/graphics/java/android/graphics/drawable/Drawable.java b/graphics/java/android/graphics/drawable/Drawable.java index 6a7b2d1..4c1d243 100644 --- a/graphics/java/android/graphics/drawable/Drawable.java +++ b/graphics/java/android/graphics/drawable/Drawable.java @@ -16,21 +16,30 @@ package android.graphics.drawable; -import java.io.InputStream; -import java.io.IOException; -import java.util.Arrays; - import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParserException; import android.content.res.Resources; import android.content.res.TypedArray; -import android.graphics.*; +import android.graphics.Bitmap; +import android.graphics.BitmapFactory; +import android.graphics.Canvas; +import android.graphics.ColorFilter; +import android.graphics.NinePatch; +import android.graphics.PixelFormat; +import android.graphics.PorterDuff; +import android.graphics.PorterDuffColorFilter; +import android.graphics.Rect; +import android.graphics.Region; import android.util.AttributeSet; import android.util.DisplayMetrics; import android.util.StateSet; -import android.util.Xml; import android.util.TypedValue; +import android.util.Xml; + +import java.io.IOException; +import java.io.InputStream; +import java.util.Arrays; /** * A Drawable is a general abstraction for "something that can be drawn." Most @@ -645,6 +654,8 @@ public abstract class Drawable { * Calling this method on a mutable Drawable will have no effect. * * @return This drawable. + * @see ConstantState + * @see #getConstantState() */ public Drawable mutate() { return this; @@ -750,6 +761,8 @@ public abstract class Drawable { drawable = new StateListDrawable(); } else if (name.equals("level-list")) { drawable = new LevelListDrawable(); + } else if (name.equals("mipmap")) { + drawable = new MipmapDrawable(); } else if (name.equals("layer-list")) { drawable = new LayerDrawable(); } else if (name.equals("transition")) { @@ -771,7 +784,7 @@ public abstract class Drawable { } else if (name.equals("inset")) { drawable = new InsetDrawable(); } else if (name.equals("bitmap")) { - drawable = new BitmapDrawable(); + drawable = new BitmapDrawable(r); if (r != null) { ((BitmapDrawable) drawable).setTargetDensity(r.getDisplayMetrics()); } @@ -806,6 +819,9 @@ public abstract class Drawable { return null; } + /** + * Inflate this Drawable from an XML resource. + */ public void inflate(Resources r, XmlPullParser parser, AttributeSet attrs) throws XmlPullParserException, IOException { @@ -814,6 +830,12 @@ public abstract class Drawable { a.recycle(); } + /** + * Inflate a Drawable from an XML resource. + * + * @throws XmlPullParserException + * @throws IOException + */ void inflateWithAttributes(Resources r, XmlPullParser parser, TypedArray attrs, int visibleAttr) throws XmlPullParserException, IOException { @@ -821,12 +843,27 @@ public abstract class Drawable { mVisible = attrs.getBoolean(visibleAttr, mVisible); } + /** + * This abstract class is used by {@link Drawable}s to store shared constant state and data + * between Drawables. {@link BitmapDrawable}s created from the same resource will for instance + * share a unique bitmap stored in their ConstantState. + * + * <p> + * {@link #newDrawable(Resources)} can be used as a factory to create new Drawable instances + * from this ConstantState. + * </p> + * + * Use {@link Drawable#getConstantState()} to retrieve the ConstantState of a Drawable. Calling + * {@link Drawable#mutate()} on a Drawable should typically create a new ConstantState for that + * Drawable. + */ public static abstract class ConstantState { /** * Create a new drawable without supplying resources the caller * is running in. Note that using this means the density-dependent * drawables (like bitmaps) will not be able to update their target - * density correctly. + * density correctly. One should use {@link #newDrawable(Resources)} + * instead to provide a resource. */ public abstract Drawable newDrawable(); /** @@ -845,6 +882,13 @@ public abstract class Drawable { public abstract int getChangingConfigurations(); } + /** + * Return a {@link ConstantState} instance that holds the shared state of this Drawable. + *q + * @return The ConstantState associated to that Drawable. + * @see ConstantState + * @see Drawable#mutate() + */ public ConstantState getConstantState() { return null; } diff --git a/graphics/java/android/graphics/drawable/DrawableContainer.java b/graphics/java/android/graphics/drawable/DrawableContainer.java index c6f57d4..124d907 100644 --- a/graphics/java/android/graphics/drawable/DrawableContainer.java +++ b/graphics/java/android/graphics/drawable/DrawableContainer.java @@ -17,8 +17,16 @@ package android.graphics.drawable; import android.content.res.Resources; -import android.graphics.*; +import android.graphics.Canvas; +import android.graphics.ColorFilter; +import android.graphics.PixelFormat; +import android.graphics.Rect; +/** + * A helper class that contains several {@link Drawable}s and selects which one to use. + * + * You can subclass it to create your own DrawableContainers or directly use one its child classes. + */ public class DrawableContainer extends Drawable implements Drawable.Callback { /** @@ -196,8 +204,7 @@ public class DrawableContainer extends Drawable implements Drawable.Callback { mDrawableContainerState.getOpacity(); } - public boolean selectDrawable(int idx) - { + public boolean selectDrawable(int idx) { if (idx == mCurIndex) { return false; } @@ -255,6 +262,12 @@ public class DrawableContainer extends Drawable implements Drawable.Callback { return this; } + /** + * A ConstantState that can contain several {@link Drawable}s. + * + * This class was made public to enable testing, and its visibility may change in a future + * release. + */ public abstract static class DrawableContainerState extends ConstantState { final DrawableContainer mOwner; @@ -443,12 +456,12 @@ public class DrawableContainer extends Drawable implements Drawable.Callback { return mConstantMinimumHeight; } - private void computeConstantSize() { + protected void computeConstantSize() { mComputedConstantSize = true; final int N = getChildCount(); final Drawable[] drawables = mDrawables; - mConstantWidth = mConstantHeight = 0; + mConstantWidth = mConstantHeight = -1; mConstantMinimumWidth = mConstantMinimumHeight = 0; for (int i = 0; i < N; i++) { Drawable dr = drawables[i]; diff --git a/graphics/java/android/graphics/drawable/LayerDrawable.java b/graphics/java/android/graphics/drawable/LayerDrawable.java index 389fd40..b727d47 100644 --- a/graphics/java/android/graphics/drawable/LayerDrawable.java +++ b/graphics/java/android/graphics/drawable/LayerDrawable.java @@ -265,6 +265,7 @@ public class LayerDrawable extends Drawable implements Drawable.Callback { */ public boolean setDrawableByLayerId(int id, Drawable drawable) { final ChildDrawable[] layers = mLayerState.mChildren; + drawable.setCallback(this); for (int i = mLayerState.mNum - 1; i >= 0; i--) { if (layers[i].mId == id) { diff --git a/graphics/java/android/graphics/drawable/MipmapDrawable.java b/graphics/java/android/graphics/drawable/MipmapDrawable.java new file mode 100644 index 0000000..75fdeed --- /dev/null +++ b/graphics/java/android/graphics/drawable/MipmapDrawable.java @@ -0,0 +1,309 @@ +/* + * Copyright (C) 2006 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.graphics.drawable; + +import org.xmlpull.v1.XmlPullParser; +import org.xmlpull.v1.XmlPullParserException; + +import android.content.res.Resources; +import android.content.res.TypedArray; +import android.graphics.Rect; +import android.util.AttributeSet; + +import java.io.IOException; + +/** + * A resource that manages a number of alternate Drawables, and which actually draws the one which + * size matches the most closely the drawing bounds. Providing several pre-scaled version of the + * drawable helps minimizing the aliasing artifacts that can be introduced by the scaling. + * + * <p> + * Use {@link #addDrawable(Drawable)} to define the different Drawables that will represent the + * mipmap levels of this MipmapDrawable. The mipmap Drawable that will actually be used when this + * MipmapDrawable is drawn is the one which has the smallest intrinsic height greater or equal than + * the bounds' height. This selection ensures that the best available mipmap level is scaled down to + * draw this MipmapDrawable. + * </p> + * + * If the bounds' height is larger than the largest mipmap, the largest mipmap will be scaled up. + * Note that Drawables without intrinsic height (i.e. with a negative value, such as Color) will + * only be used if no other mipmap Drawable are provided. The Drawables' intrinsic heights should + * not be changed after the Drawable has been added to this MipmapDrawable. + * + * <p> + * The different mipmaps' parameters (opacity, padding, color filter, gravity...) should typically + * be similar to ensure a continuous visual appearance when the MipmapDrawable is scaled. The aspect + * ratio of the different mipmaps should especially be equal. + * </p> + * + * A typical example use of a MipmapDrawable would be for an image which is intended to be scaled at + * various sizes, and for which one wants to provide pre-scaled versions to precisely control its + * appearance. + * + * <p> + * The intrinsic size of a MipmapDrawable are inferred from those of the largest mipmap (in terms of + * {@link Drawable#getIntrinsicHeight()}). On the opposite, its minimum + * size is defined by the smallest provided mipmap. + * </p> + + * It can be defined in an XML file with the <code><mipmap></code> element. + * Each mipmap Drawable is defined in a nested <code><item></code>. For example: + * <pre> + * <mipmap xmlns:android="http://schemas.android.com/apk/res/android"> + * <item android:drawable="@drawable/my_image_8" /> + * <item android:drawable="@drawable/my_image_32" /> + * <item android:drawable="@drawable/my_image_128" /> + * </mipmap> + *</pre> + * <p> + * With this XML saved into the res/drawable/ folder of the project, it can be referenced as + * the drawable for an {@link android.widget.ImageView}. Assuming that the heights of the provided + * drawables are respectively 8, 32 and 128 pixels, the first one will be scaled down when the + * bounds' height is lower or equal than 8 pixels. The second drawable will then be used up to a + * height of 32 pixels and the largest drawable will be used for greater heights. + * </p> + * @attr ref android.R.styleable#MipmapDrawableItem_drawable + */ +public class MipmapDrawable extends DrawableContainer { + private final MipmapContainerState mMipmapContainerState; + private boolean mMutated; + + public MipmapDrawable() { + this(null, null); + } + + /** + * Adds a Drawable to the list of available mipmap Drawables. The Drawable actually used when + * this MipmapDrawable is drawn is determined from its bounds. + * + * This method has no effect if drawable is null. + * + * @param drawable The Drawable that will be added to list of available mipmap Drawables. + */ + + public void addDrawable(Drawable drawable) { + if (drawable != null) { + mMipmapContainerState.addDrawable(drawable); + onDrawableAdded(); + } + } + + private void onDrawableAdded() { + // selectDrawable assumes that the container content does not change. + // When a Drawable is added, the same index can correspond to a new Drawable, and since + // selectDrawable has a fast exit case when oldIndex==newIndex, the new drawable could end + // up not being used in place of the previous one if they happen to share the same index. + // This make sure the new computed index can actually replace the previous one. + selectDrawable(-1); + onBoundsChange(getBounds()); + } + + // overrides from Drawable + + @Override + protected void onBoundsChange(Rect bounds) { + final int index = mMipmapContainerState.indexForBounds(bounds); + + // Will call invalidateSelf() if needed + selectDrawable(index); + + super.onBoundsChange(bounds); + } + + @Override + public void inflate(Resources r, XmlPullParser parser, AttributeSet attrs) + throws XmlPullParserException, IOException { + + super.inflate(r, parser, attrs); + + int type; + + final int innerDepth = parser.getDepth() + 1; + int depth; + while ((type = parser.next()) != XmlPullParser.END_DOCUMENT + && ((depth = parser.getDepth()) >= innerDepth + || type != XmlPullParser.END_TAG)) { + if (type != XmlPullParser.START_TAG) { + continue; + } + + if (depth > innerDepth || !parser.getName().equals("item")) { + continue; + } + + TypedArray a = r.obtainAttributes(attrs, + com.android.internal.R.styleable.MipmapDrawableItem); + + int drawableRes = a.getResourceId( + com.android.internal.R.styleable.MipmapDrawableItem_drawable, 0); + + a.recycle(); + + Drawable dr; + if (drawableRes != 0) { + dr = r.getDrawable(drawableRes); + } else { + while ((type = parser.next()) == XmlPullParser.TEXT) { + } + if (type != XmlPullParser.START_TAG) { + throw new XmlPullParserException( + parser.getPositionDescription() + + ": <item> tag requires a 'drawable' attribute or " + + "child tag defining a drawable"); + } + dr = Drawable.createFromXmlInner(r, parser, attrs); + } + + mMipmapContainerState.addDrawable(dr); + } + + onDrawableAdded(); + } + + @Override + public Drawable mutate() { + if (!mMutated && super.mutate() == this) { + mMipmapContainerState.mMipmapHeights = mMipmapContainerState.mMipmapHeights.clone(); + mMutated = true; + } + return this; + } + + private final static class MipmapContainerState extends DrawableContainerState { + private int[] mMipmapHeights; + + MipmapContainerState(MipmapContainerState orig, MipmapDrawable owner, Resources res) { + super(orig, owner, res); + + if (orig != null) { + mMipmapHeights = orig.mMipmapHeights; + } else { + mMipmapHeights = new int[getChildren().length]; + } + + // Change the default value + setConstantSize(true); + } + + /** + * Returns the index of the child mipmap drawable that will best fit the provided bounds. + * This index is determined by comparing bounds' height and children intrinsic heights. + * The returned mipmap index is the smallest mipmap which height is greater or equal than + * the bounds' height. If the bounds' height is larger than the largest mipmap, the largest + * mipmap index is returned. + * + * @param bounds The bounds of the MipMapDrawable. + * @return The index of the child Drawable that will best fit these bounds, or -1 if there + * are no children mipmaps. + */ + public int indexForBounds(Rect bounds) { + final int boundsHeight = bounds.height(); + final int N = getChildCount(); + for (int i = 0; i < N; i++) { + if (boundsHeight <= mMipmapHeights[i]) { + return i; + } + } + + // No mipmap larger than bounds found. Use largest one which will be scaled up. + if (N > 0) { + return N - 1; + } + // No Drawable mipmap at all + return -1; + } + + /** + * Adds a Drawable to the list of available mipmap Drawables. This list can be retrieved + * using {@link DrawableContainer.DrawableContainerState#getChildren()} and this method + * ensures that it is always sorted by increasing {@link Drawable#getIntrinsicHeight()}. + * + * @param drawable The Drawable that will be added to children list + */ + public void addDrawable(Drawable drawable) { + // Insert drawable in last position, correctly resetting cached values and + // especially mComputedConstantSize + int pos = addChild(drawable); + + // Bubble sort the last drawable to restore the sort by intrinsic height + final int drawableHeight = drawable.getIntrinsicHeight(); + + while (pos > 0) { + final Drawable previousDrawable = mDrawables[pos-1]; + final int previousIntrinsicHeight = previousDrawable.getIntrinsicHeight(); + + if (drawableHeight < previousIntrinsicHeight) { + mDrawables[pos] = previousDrawable; + mMipmapHeights[pos] = previousIntrinsicHeight; + + mDrawables[pos-1] = drawable; + mMipmapHeights[pos-1] = drawableHeight; + pos--; + } else { + break; + } + } + } + + /** + * Intrinsic sizes are those of the largest available mipmap. + * Minimum sizes are those of the smallest available mipmap. + */ + @Override + protected void computeConstantSize() { + final int N = getChildCount(); + if (N > 0) { + final Drawable smallestDrawable = mDrawables[0]; + mConstantMinimumWidth = smallestDrawable.getMinimumWidth(); + mConstantMinimumHeight = smallestDrawable.getMinimumHeight(); + + final Drawable largestDrawable = mDrawables[N-1]; + mConstantWidth = largestDrawable.getIntrinsicWidth(); + mConstantHeight = largestDrawable.getIntrinsicHeight(); + } else { + mConstantWidth = mConstantHeight = -1; + mConstantMinimumWidth = mConstantMinimumHeight = 0; + } + mComputedConstantSize = true; + } + + @Override + public Drawable newDrawable() { + return new MipmapDrawable(this, null); + } + + @Override + public Drawable newDrawable(Resources res) { + return new MipmapDrawable(this, res); + } + + @Override + public void growArray(int oldSize, int newSize) { + super.growArray(oldSize, newSize); + int[] newInts = new int[newSize]; + System.arraycopy(mMipmapHeights, 0, newInts, 0, oldSize); + mMipmapHeights = newInts; + } + } + + private MipmapDrawable(MipmapContainerState state, Resources res) { + MipmapContainerState as = new MipmapContainerState(state, this, res); + mMipmapContainerState = as; + setConstantState(as); + onDrawableAdded(); + } +} diff --git a/graphics/java/android/renderscript/Allocation.java b/graphics/java/android/renderscript/Allocation.java index 17c0778..d32a0b5 100644 --- a/graphics/java/android/renderscript/Allocation.java +++ b/graphics/java/android/renderscript/Allocation.java @@ -76,10 +76,30 @@ public class Allocation extends BaseObj { subData1D(0, mType.getElementCount(), d); } + public void subData(int off, FieldPacker fp) { + int eSize = mType.mElement.getSizeBytes(); + final byte[] data = fp.getData(); + + int count = data.length / eSize; + if ((eSize * count) != data.length) { + throw new IllegalArgumentException("Field packer length " + data.length + + " not divisible by element size " + eSize + "."); + } + data1DChecks(off, count, data.length, data.length); + mRS.nAllocationSubData1D(mID, off, count, data, data.length); + } + private void data1DChecks(int off, int count, int len, int dataSize) { mRS.validate(); - if((off < 0) || (count < 1) || ((off + count) > mType.getElementCount())) { - throw new IllegalArgumentException("Offset or Count out of bounds."); + if(off < 0) { + throw new IllegalArgumentException("Offset must be >= 0."); + } + if(count < 1) { + throw new IllegalArgumentException("Count must be >= 1."); + } + if((off + count) > mType.getElementCount()) { + throw new IllegalArgumentException("Overflow, Available count " + mType.getElementCount() + + ", got " + count + " at offset " + off + "."); } if((len) < dataSize) { throw new IllegalArgumentException("Array too small for allocation type."); diff --git a/graphics/java/android/renderscript/Byte2.java b/graphics/java/android/renderscript/Byte2.java new file mode 100644 index 0000000..95cf88c --- /dev/null +++ b/graphics/java/android/renderscript/Byte2.java @@ -0,0 +1,37 @@ +/* + * Copyright (C) 2009 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.renderscript; + +import java.lang.Math; +import android.util.Log; + + +/** + * @hide + * + **/ +public class Byte2 { + public Byte2() { + } + + public byte x; + public byte y; +} + + + + diff --git a/graphics/java/android/renderscript/Byte3.java b/graphics/java/android/renderscript/Byte3.java new file mode 100644 index 0000000..a6c0ca9 --- /dev/null +++ b/graphics/java/android/renderscript/Byte3.java @@ -0,0 +1,38 @@ +/* + * Copyright (C) 2009 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.renderscript; + +import java.lang.Math; +import android.util.Log; + + +/** + * @hide + * + **/ +public class Byte3 { + public Byte3() { + } + + public byte x; + public byte y; + public byte z; +} + + + + diff --git a/graphics/java/android/renderscript/Byte4.java b/graphics/java/android/renderscript/Byte4.java new file mode 100644 index 0000000..a5bfc61 --- /dev/null +++ b/graphics/java/android/renderscript/Byte4.java @@ -0,0 +1,38 @@ +/* + * Copyright (C) 2009 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.renderscript; + +import java.lang.Math; +import android.util.Log; + + +/** + * @hide + * + **/ +public class Byte4 { + public Byte4() { + } + + public byte x; + public byte y; + public byte z; + public byte w; +} + + + diff --git a/graphics/java/android/renderscript/Element.java b/graphics/java/android/renderscript/Element.java index 10ef05a..974a36e 100644 --- a/graphics/java/android/renderscript/Element.java +++ b/graphics/java/android/renderscript/Element.java @@ -91,41 +91,112 @@ public class Element extends BaseObj { } } - public static Element USER_U8(RenderScript rs) { - if(rs.mElement_USER_U8 == null) { - rs.mElement_USER_U8 = createUser(rs, DataType.UNSIGNED_8); + public static Element U8(RenderScript rs) { + if(rs.mElement_U8 == null) { + rs.mElement_U8 = createUser(rs, DataType.UNSIGNED_8); } - return rs.mElement_USER_U8; + return rs.mElement_U8; } - public static Element USER_I8(RenderScript rs) { - if(rs.mElement_USER_I8 == null) { - rs.mElement_USER_I8 = createUser(rs, DataType.SIGNED_8); + public static Element I8(RenderScript rs) { + if(rs.mElement_I8 == null) { + rs.mElement_I8 = createUser(rs, DataType.SIGNED_8); } - return rs.mElement_USER_I8; + return rs.mElement_I8; } - public static Element USER_U32(RenderScript rs) { - if(rs.mElement_USER_U32 == null) { - rs.mElement_USER_U32 = createUser(rs, DataType.UNSIGNED_32); + public static Element U32(RenderScript rs) { + if(rs.mElement_U32 == null) { + rs.mElement_U32 = createUser(rs, DataType.UNSIGNED_32); } - return rs.mElement_USER_U32; + return rs.mElement_U32; } - public static Element USER_I32(RenderScript rs) { - if(rs.mElement_USER_I32 == null) { - rs.mElement_USER_I32 = createUser(rs, DataType.SIGNED_32); + public static Element I32(RenderScript rs) { + if(rs.mElement_I32 == null) { + rs.mElement_I32 = createUser(rs, DataType.SIGNED_32); } - return rs.mElement_USER_I32; + return rs.mElement_I32; } - public static Element USER_F32(RenderScript rs) { - if(rs.mElement_USER_F32 == null) { - rs.mElement_USER_F32 = createUser(rs, DataType.FLOAT_32); + public static Element F32(RenderScript rs) { + if(rs.mElement_F32 == null) { + rs.mElement_F32 = createUser(rs, DataType.FLOAT_32); } - return rs.mElement_USER_F32; + return rs.mElement_F32; } + public static Element ELEMENT(RenderScript rs) { + if(rs.mElement_ELEMENT == null) { + rs.mElement_ELEMENT = createUser(rs, DataType.RS_ELEMENT); + } + return rs.mElement_ELEMENT; + } + + public static Element TYPE(RenderScript rs) { + if(rs.mElement_TYPE == null) { + rs.mElement_TYPE = createUser(rs, DataType.RS_TYPE); + } + return rs.mElement_TYPE; + } + + public static Element ALLOCATION(RenderScript rs) { + if(rs.mElement_ALLOCATION == null) { + rs.mElement_ALLOCATION = createUser(rs, DataType.RS_ALLOCATION); + } + return rs.mElement_ALLOCATION; + } + + public static Element SAMPLER(RenderScript rs) { + if(rs.mElement_SAMPLER == null) { + rs.mElement_SAMPLER = createUser(rs, DataType.RS_SAMPLER); + } + return rs.mElement_SAMPLER; + } + + public static Element SCRIPT(RenderScript rs) { + if(rs.mElement_SCRIPT == null) { + rs.mElement_SCRIPT = createUser(rs, DataType.RS_SCRIPT); + } + return rs.mElement_SCRIPT; + } + + public static Element MESH(RenderScript rs) { + if(rs.mElement_MESH == null) { + rs.mElement_MESH = createUser(rs, DataType.RS_MESH); + } + return rs.mElement_MESH; + } + + public static Element PROGRAM_FRAGMENT(RenderScript rs) { + if(rs.mElement_PROGRAM_FRAGMENT == null) { + rs.mElement_PROGRAM_FRAGMENT = createUser(rs, DataType.RS_PROGRAM_FRAGMENT); + } + return rs.mElement_PROGRAM_FRAGMENT; + } + + public static Element PROGRAM_VERTEX(RenderScript rs) { + if(rs.mElement_PROGRAM_VERTEX == null) { + rs.mElement_PROGRAM_VERTEX = createUser(rs, DataType.RS_PROGRAM_VERTEX); + } + return rs.mElement_PROGRAM_VERTEX; + } + + public static Element PROGRAM_RASTER(RenderScript rs) { + if(rs.mElement_PROGRAM_RASTER == null) { + rs.mElement_PROGRAM_RASTER = createUser(rs, DataType.RS_PROGRAM_RASTER); + } + return rs.mElement_PROGRAM_RASTER; + } + + public static Element PROGRAM_STORE(RenderScript rs) { + if(rs.mElement_PROGRAM_STORE == null) { + rs.mElement_PROGRAM_STORE = createUser(rs, DataType.RS_PROGRAM_STORE); + } + return rs.mElement_PROGRAM_STORE; + } + + public static Element A_8(RenderScript rs) { if(rs.mElement_A_8 == null) { rs.mElement_A_8 = createPixel(rs, DataType.UNSIGNED_8, DataKind.PIXEL_A); @@ -175,47 +246,34 @@ public class Element extends BaseObj { return rs.mElement_INDEX_16; } - public static Element ATTRIB_POSITION_2(RenderScript rs) { - if(rs.mElement_POSITION_2 == null) { - rs.mElement_POSITION_2 = createAttrib(rs, DataType.FLOAT_32, DataKind.POSITION, 2); + public static Element F32_2(RenderScript rs) { + if(rs.mElement_FLOAT_2 == null) { + rs.mElement_FLOAT_2 = createVector(rs, DataType.FLOAT_32, 2); } - return rs.mElement_POSITION_2; + return rs.mElement_FLOAT_2; } - public static Element ATTRIB_POSITION_3(RenderScript rs) { - if(rs.mElement_POSITION_3 == null) { - rs.mElement_POSITION_3 = createAttrib(rs, DataType.FLOAT_32, DataKind.POSITION, 3); + public static Element F32_3(RenderScript rs) { + if(rs.mElement_FLOAT_3 == null) { + rs.mElement_FLOAT_3 = createVector(rs, DataType.FLOAT_32, 3); } - return rs.mElement_POSITION_3; + return rs.mElement_FLOAT_3; } - public static Element ATTRIB_TEXTURE_2(RenderScript rs) { - if(rs.mElement_TEXTURE_2 == null) { - rs.mElement_TEXTURE_2 = createAttrib(rs, DataType.FLOAT_32, DataKind.TEXTURE, 2); + public static Element F32_4(RenderScript rs) { + if(rs.mElement_FLOAT_4 == null) { + rs.mElement_FLOAT_4 = createVector(rs, DataType.FLOAT_32, 4); } - return rs.mElement_TEXTURE_2; + return rs.mElement_FLOAT_4; } - public static Element ATTRIB_NORMAL_3(RenderScript rs) { - if(rs.mElement_NORMAL_3 == null) { - rs.mElement_NORMAL_3 = createAttrib(rs, DataType.FLOAT_32, DataKind.NORMAL, 3); + public static Element U8_4(RenderScript rs) { + if(rs.mElement_UCHAR_4 == null) { + rs.mElement_UCHAR_4 = createVector(rs, DataType.UNSIGNED_8, 4); } - return rs.mElement_NORMAL_3; + return rs.mElement_UCHAR_4; } - public static Element ATTRIB_COLOR_U8_4(RenderScript rs) { - if(rs.mElement_COLOR_U8_4 == null) { - rs.mElement_COLOR_U8_4 = createAttrib(rs, DataType.UNSIGNED_8, DataKind.COLOR, 4); - } - return rs.mElement_COLOR_U8_4; - } - - public static Element ATTRIB_COLOR_F32_4(RenderScript rs) { - if(rs.mElement_COLOR_F32_4 == null) { - rs.mElement_COLOR_F32_4 = createAttrib(rs, DataType.FLOAT_32, DataKind.COLOR, 4); - } - return rs.mElement_COLOR_F32_4; - } Element(RenderScript rs, Element[] e, String[] n) { super(rs); @@ -244,29 +302,6 @@ public class Element extends BaseObj { super.destroy(); } - public static Element createFromClass(RenderScript rs, Class c) { - rs.validate(); - Field[] fields = c.getFields(); - Builder b = new Builder(rs); - - for(Field f: fields) { - Class fc = f.getType(); - if(fc == int.class) { - b.add(createUser(rs, DataType.SIGNED_32), f.getName()); - } else if(fc == short.class) { - b.add(createUser(rs, DataType.SIGNED_16), f.getName()); - } else if(fc == byte.class) { - b.add(createUser(rs, DataType.SIGNED_8), f.getName()); - } else if(fc == float.class) { - b.add(createUser(rs, DataType.FLOAT_32), f.getName()); - } else { - throw new IllegalArgumentException("Unkown field type"); - } - } - return b.create(); - } - - ///////////////////////////////////////// public static Element createUser(RenderScript rs, DataType dt) { return new Element(rs, dt, DataKind.USER, false, 1); @@ -283,55 +318,6 @@ public class Element extends BaseObj { return new Element(rs, DataType.UNSIGNED_16, DataKind.INDEX, false, 1); } - public static Element createAttrib(RenderScript rs, DataType dt, DataKind dk, int size) { - if (!(dt == DataType.FLOAT_32 || - dt == DataType.UNSIGNED_8 || - dt == DataType.UNSIGNED_16 || - dt == DataType.UNSIGNED_32 || - dt == DataType.SIGNED_8 || - dt == DataType.SIGNED_16 || - dt == DataType.SIGNED_32)) { - throw new IllegalArgumentException("Unsupported DataType"); - } - - if (!(dk == DataKind.COLOR || - dk == DataKind.POSITION || - dk == DataKind.TEXTURE || - dk == DataKind.NORMAL || - dk == DataKind.POINT_SIZE || - dk == DataKind.USER)) { - throw new IllegalArgumentException("Unsupported DataKind"); - } - - if (dk == DataKind.COLOR && - ((dt != DataType.FLOAT_32 && dt != DataType.UNSIGNED_8) || - size < 3 || size > 4)) { - throw new IllegalArgumentException("Bad combo"); - } - if (dk == DataKind.POSITION && (size < 1 || size > 4)) { - throw new IllegalArgumentException("Bad combo"); - } - if (dk == DataKind.TEXTURE && - (dt != DataType.FLOAT_32 || size < 1 || size > 4)) { - throw new IllegalArgumentException("Bad combo"); - } - if (dk == DataKind.NORMAL && - (dt != DataType.FLOAT_32 || size != 3)) { - throw new IllegalArgumentException("Bad combo"); - } - if (dk == DataKind.POINT_SIZE && - (dt != DataType.FLOAT_32 || size != 1)) { - throw new IllegalArgumentException("Bad combo"); - } - - boolean norm = false; - if (dk == DataKind.COLOR && dt == DataType.UNSIGNED_8) { - norm = true; - } - - return new Element(rs, dt, dk, norm, size); - } - public static Element createPixel(RenderScript rs, DataType dt, DataKind dk) { if (!(dk == DataKind.PIXEL_L || dk == DataKind.PIXEL_A || diff --git a/graphics/java/android/renderscript/FieldPacker.java b/graphics/java/android/renderscript/FieldPacker.java index b26e47d..6d55c7e 100644 --- a/graphics/java/android/renderscript/FieldPacker.java +++ b/graphics/java/android/renderscript/FieldPacker.java @@ -33,21 +33,24 @@ public class FieldPacker { } } - void reset() { + public void reset() { mPos = 0; } + public void reset(int i) { + mPos = i; + } - void addI8(byte v) { + public void addI8(byte v) { mData[mPos++] = v; } - void addI16(short v) { + public void addI16(short v) { align(2); mData[mPos++] = (byte)(v & 0xff); mData[mPos++] = (byte)(v >> 8); } - void addI32(int v) { + public void addI32(int v) { align(4); mData[mPos++] = (byte)(v & 0xff); mData[mPos++] = (byte)((v >> 8) & 0xff); @@ -55,7 +58,7 @@ public class FieldPacker { mData[mPos++] = (byte)((v >> 24) & 0xff); } - void addI64(long v) { + public void addI64(long v) { align(8); mData[mPos++] = (byte)(v & 0xff); mData[mPos++] = (byte)((v >> 8) & 0xff); @@ -67,14 +70,14 @@ public class FieldPacker { mData[mPos++] = (byte)((v >> 56) & 0xff); } - void addU8(short v) { + public void addU8(short v) { if ((v < 0) || (v > 0xff)) { throw new IllegalArgumentException("Saving value out of range for type"); } mData[mPos++] = (byte)v; } - void addU16(int v) { + public void addU16(int v) { if ((v < 0) || (v > 0xffff)) { throw new IllegalArgumentException("Saving value out of range for type"); } @@ -83,7 +86,7 @@ public class FieldPacker { mData[mPos++] = (byte)(v >> 8); } - void addU32(long v) { + public void addU32(long v) { if ((v < 0) || (v > 0xffffffff)) { throw new IllegalArgumentException("Saving value out of range for type"); } @@ -94,7 +97,7 @@ public class FieldPacker { mData[mPos++] = (byte)((v >> 24) & 0xff); } - void addU64(long v) { + public void addU64(long v) { if (v < 0) { throw new IllegalArgumentException("Saving value out of range for type"); } @@ -109,15 +112,135 @@ public class FieldPacker { mData[mPos++] = (byte)((v >> 56) & 0xff); } - void addF32(float v) { + public void addF32(float v) { addI32(Float.floatToRawIntBits(v)); } - void addF64(float v) { + public void addF64(float v) { addI64(Double.doubleToRawLongBits(v)); } - final byte[] getData() { + public void addObj(BaseObj obj) { + if (obj != null) { + addI32(obj.getID()); + } else { + addI32(0); + } + } + + public void addF32(Float2 v) { + addF32(v.x); + addF32(v.y); + } + public void addF32(Float3 v) { + addF32(v.x); + addF32(v.y); + addF32(v.z); + } + public void addF32(Float4 v) { + addF32(v.x); + addF32(v.y); + addF32(v.z); + addF32(v.w); + } + + public void addI8(Byte2 v) { + addI8(v.x); + addI8(v.y); + } + public void addI8(Byte3 v) { + addI8(v.x); + addI8(v.y); + addI8(v.z); + } + public void addI8(Byte4 v) { + addI8(v.x); + addI8(v.y); + addI8(v.z); + addI8(v.w); + } + + public void addU8(Short2 v) { + addU8(v.x); + addU8(v.y); + } + public void addU8(Short3 v) { + addU8(v.x); + addU8(v.y); + addU8(v.z); + } + public void addU8(Short4 v) { + addU8(v.x); + addU8(v.y); + addU8(v.z); + addU8(v.w); + } + + public void addI16(Short2 v) { + addI16(v.x); + addI16(v.y); + } + public void addI16(Short3 v) { + addI16(v.x); + addI16(v.y); + addI16(v.z); + } + public void addI16(Short4 v) { + addI16(v.x); + addI16(v.y); + addI16(v.z); + addI16(v.w); + } + + public void addU16(Int2 v) { + addU16(v.x); + addU16(v.y); + } + public void addU16(Int3 v) { + addU16(v.x); + addU16(v.y); + addU16(v.z); + } + public void addU16(Int4 v) { + addU16(v.x); + addU16(v.y); + addU16(v.z); + addU16(v.w); + } + + public void addI32(Int2 v) { + addI32(v.x); + addI32(v.y); + } + public void addI32(Int3 v) { + addI32(v.x); + addI32(v.y); + addI32(v.z); + } + public void addI32(Int4 v) { + addI32(v.x); + addI32(v.y); + addI32(v.z); + addI32(v.w); + } + + public void addU32(Int2 v) { + addU32(v.x); + addU32(v.y); + } + public void addU32(Int3 v) { + addU32(v.x); + addU32(v.y); + addU32(v.z); + } + public void addU32(Int4 v) { + addU32(v.x); + addU32(v.y); + addU32(v.z); + addU32(v.w); + } + + public final byte[] getData() { return mData; } diff --git a/graphics/java/android/renderscript/FileA3D.java b/graphics/java/android/renderscript/FileA3D.java new file mode 100644 index 0000000..fb36f1f --- /dev/null +++ b/graphics/java/android/renderscript/FileA3D.java @@ -0,0 +1,214 @@ +/* + * Copyright (C) 2008 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.renderscript; + +import java.io.IOException; +import java.io.InputStream; + +import android.content.res.Resources; +import android.content.res.AssetManager; +import android.graphics.Bitmap; +import android.graphics.BitmapFactory; +import android.util.Log; +import android.util.TypedValue; + +/** + * @hide + * + **/ +public class FileA3D extends BaseObj { + + public enum ClassID { + + UNKNOWN, + MESH, + SIMPLE_MESH, + TYPE, + ELEMENT, + ALLOCATION, + PROGRAM_VERTEX, + PROGRAM_RASTER, + PROGRAM_FRAGMENT, + PROGRAM_STORE, + SAMPLER, + ANIMATION, + LIGHT, + ADAPTER_1D, + ADAPTER_2D, + SCRIPT_C; + + public static ClassID toClassID(int intID) { + return ClassID.values()[intID]; + } + } + + // Read only class with index entries + public class IndexEntry { + RenderScript mRS; + int mIndex; + int mID; + String mName; + ClassID mClassID; + BaseObj mLoadedObj; + + public String getName() { + return mName; + } + + public ClassID getClassID() { + return mClassID; + } + + public BaseObj getObject() { + if(mLoadedObj != null) { + return mLoadedObj; + } + + if(mClassID == ClassID.UNKNOWN) { + return null; + } + + int objectID = mRS.nFileA3DGetEntryByIndex(mID, mIndex); + if(objectID == 0) { + return null; + } + + switch (mClassID) { + case MESH: + mLoadedObj = null; + break; + case SIMPLE_MESH: + mLoadedObj = new SimpleMesh(objectID, mRS); + break; + case TYPE: + mLoadedObj = new Type(objectID, mRS); + break; + case ELEMENT: + mLoadedObj = null; + break; + case ALLOCATION: + mLoadedObj = null; + break; + case PROGRAM_VERTEX: + mLoadedObj = new ProgramVertex(objectID, mRS); + break; + case PROGRAM_RASTER: + break; + case PROGRAM_FRAGMENT: + break; + case PROGRAM_STORE: + break; + case SAMPLER: + break; + case ANIMATION: + break; + case LIGHT: + break; + case ADAPTER_1D: + break; + case ADAPTER_2D: + break; + case SCRIPT_C: + break; + } + + return mLoadedObj; + } + + IndexEntry(RenderScript rs, int index, int id, String name, ClassID classID) { + mRS = rs; + mIndex = index; + mID = id; + mName = name; + mClassID = classID; + mLoadedObj = null; + } + } + + IndexEntry[] mFileEntries; + + FileA3D(int id, RenderScript rs) { + super(rs); + mID = id; + } + + private void initEntries() { + int numFileEntries = mRS.nFileA3DGetNumIndexEntries(mID); + if(numFileEntries <= 0) { + return; + } + + mFileEntries = new IndexEntry[numFileEntries]; + int[] ids = new int[numFileEntries]; + String[] names = new String[numFileEntries]; + + mRS.nFileA3DGetIndexEntries(mID, numFileEntries, ids, names); + + for(int i = 0; i < numFileEntries; i ++) { + mFileEntries[i] = new IndexEntry(mRS, i, mID, names[i], ClassID.toClassID(ids[i])); + } + } + + public int getNumIndexEntries() { + if(mFileEntries == null) { + return 0; + } + return mFileEntries.length; + } + + public IndexEntry getIndexEntry(int index) { + if(getNumIndexEntries() == 0 || index < 0 || index >= mFileEntries.length) { + return null; + } + return mFileEntries[index]; + } + + static public FileA3D createFromResource(RenderScript rs, Resources res, int id) + throws IllegalArgumentException { + + rs.validate(); + InputStream is = null; + try { + final TypedValue value = new TypedValue(); + is = res.openRawResource(id, value); + + int asset = ((AssetManager.AssetInputStream) is).getAssetInt(); + + int fileId = rs.nFileA3DCreateFromAssetStream(asset); + + if(fileId == 0) { + throw new IllegalStateException("Load failed."); + } + FileA3D fa3d = new FileA3D(fileId, rs); + fa3d.initEntries(); + return fa3d; + + } catch (Exception e) { + // Ignore + } finally { + if (is != null) { + try { + is.close(); + } catch (IOException e) { + // Ignore + } + } + } + + return null; + } +} diff --git a/graphics/java/android/renderscript/Vector2f.java b/graphics/java/android/renderscript/Float2.java index 567d57f..8fea91f 100644 --- a/graphics/java/android/renderscript/Vector2f.java +++ b/graphics/java/android/renderscript/Float2.java @@ -24,8 +24,8 @@ import android.util.Log; * @hide * **/ -public class Vector2f { - public Vector2f() { +public class Float2 { + public Float2() { } public float x; diff --git a/graphics/java/android/renderscript/Vector3f.java b/graphics/java/android/renderscript/Float3.java index f2842f3..9d9e406 100644 --- a/graphics/java/android/renderscript/Vector3f.java +++ b/graphics/java/android/renderscript/Float3.java @@ -24,8 +24,8 @@ import android.util.Log; * @hide * **/ -public class Vector3f { - public Vector3f() { +public class Float3 { + public Float3() { } public float x; diff --git a/graphics/java/android/renderscript/Vector4f.java b/graphics/java/android/renderscript/Float4.java index fabd959..a703e80 100644 --- a/graphics/java/android/renderscript/Vector4f.java +++ b/graphics/java/android/renderscript/Float4.java @@ -24,8 +24,8 @@ import android.util.Log; * @hide * **/ -public class Vector4f { - public Vector4f() { +public class Float4 { + public Float4() { } public float x; diff --git a/graphics/java/android/renderscript/Int2.java b/graphics/java/android/renderscript/Int2.java new file mode 100644 index 0000000..56e2fe9 --- /dev/null +++ b/graphics/java/android/renderscript/Int2.java @@ -0,0 +1,37 @@ +/* + * Copyright (C) 2009 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.renderscript; + +import java.lang.Math; +import android.util.Log; + + +/** + * @hide + * + **/ +public class Int2 { + public Int2() { + } + + public int x; + public int y; +} + + + + diff --git a/graphics/java/android/renderscript/Int3.java b/graphics/java/android/renderscript/Int3.java new file mode 100644 index 0000000..1b27509 --- /dev/null +++ b/graphics/java/android/renderscript/Int3.java @@ -0,0 +1,38 @@ +/* + * Copyright (C) 2009 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.renderscript; + +import java.lang.Math; +import android.util.Log; + + +/** + * @hide + * + **/ +public class Int3 { + public Int3() { + } + + public int x; + public int y; + public int z; +} + + + + diff --git a/graphics/java/android/renderscript/Int4.java b/graphics/java/android/renderscript/Int4.java new file mode 100644 index 0000000..3d6f3f5 --- /dev/null +++ b/graphics/java/android/renderscript/Int4.java @@ -0,0 +1,38 @@ +/* + * Copyright (C) 2009 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.renderscript; + +import java.lang.Math; +import android.util.Log; + + +/** + * @hide + * + **/ +public class Int4 { + public Int4() { + } + + public int x; + public int y; + public int z; + public int w; +} + + + diff --git a/graphics/java/android/renderscript/Long2.java b/graphics/java/android/renderscript/Long2.java new file mode 100644 index 0000000..11ead2f --- /dev/null +++ b/graphics/java/android/renderscript/Long2.java @@ -0,0 +1,37 @@ +/* + * Copyright (C) 2009 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.renderscript; + +import java.lang.Math; +import android.util.Log; + + +/** + * @hide + * + **/ +public class Long2 { + public Long2() { + } + + public long x; + public long y; +} + + + + diff --git a/graphics/java/android/renderscript/Long3.java b/graphics/java/android/renderscript/Long3.java new file mode 100644 index 0000000..1604532 --- /dev/null +++ b/graphics/java/android/renderscript/Long3.java @@ -0,0 +1,38 @@ +/* + * Copyright (C) 2009 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.renderscript; + +import java.lang.Math; +import android.util.Log; + + +/** + * @hide + * + **/ +public class Long3 { + public Long3() { + } + + public long x; + public long y; + public long z; +} + + + + diff --git a/graphics/java/android/renderscript/Long4.java b/graphics/java/android/renderscript/Long4.java new file mode 100644 index 0000000..2fd2747 --- /dev/null +++ b/graphics/java/android/renderscript/Long4.java @@ -0,0 +1,38 @@ +/* + * Copyright (C) 2009 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.renderscript; + +import java.lang.Math; +import android.util.Log; + + +/** + * @hide + * + **/ +public class Long4 { + public Long4() { + } + + public long x; + public long y; + public long z; + public long w; +} + + + diff --git a/graphics/java/android/renderscript/ProgramStore.java b/graphics/java/android/renderscript/ProgramStore.java index 69be245..f558117 100644 --- a/graphics/java/android/renderscript/ProgramStore.java +++ b/graphics/java/android/renderscript/ProgramStore.java @@ -147,17 +147,17 @@ public class ProgramStore extends BaseObj { if (b.mOut != null) { outID = b.mOut.mID; } - rs.nProgramFragmentStoreBegin(inID, outID); - rs.nProgramFragmentStoreDepthFunc(b.mDepthFunc.mID); - rs.nProgramFragmentStoreDepthMask(b.mDepthMask); - rs.nProgramFragmentStoreColorMask(b.mColorMaskR, + rs.nProgramStoreBegin(inID, outID); + rs.nProgramStoreDepthFunc(b.mDepthFunc.mID); + rs.nProgramStoreDepthMask(b.mDepthMask); + rs.nProgramStoreColorMask(b.mColorMaskR, b.mColorMaskG, b.mColorMaskB, b.mColorMaskA); - rs.nProgramFragmentStoreBlendFunc(b.mBlendSrc.mID, b.mBlendDst.mID); - rs.nProgramFragmentStoreDither(b.mDither); + rs.nProgramStoreBlendFunc(b.mBlendSrc.mID, b.mBlendDst.mID); + rs.nProgramStoreDither(b.mDither); - int id = rs.nProgramFragmentStoreCreate(); + int id = rs.nProgramStoreCreate(); return new ProgramStore(id, rs); } diff --git a/graphics/java/android/renderscript/RenderScript.java b/graphics/java/android/renderscript/RenderScript.java index a935243..d35cf8b 100644 --- a/graphics/java/android/renderscript/RenderScript.java +++ b/graphics/java/android/renderscript/RenderScript.java @@ -71,7 +71,7 @@ public class RenderScript { native void nContextBindRootScript(int script); native void nContextBindSampler(int sampler, int slot); - native void nContextBindProgramFragmentStore(int pfs); + native void nContextBindProgramStore(int pfs); native void nContextBindProgramFragment(int pf); native void nContextBindProgramVertex(int pf); native void nContextBindProgramRaster(int pr); @@ -117,6 +117,11 @@ public class RenderScript { native void nAllocationSubDataFromObject(int id, Type t, int offset, Object o); native void nAllocationSubReadFromObject(int id, Type t, int offset, Object o); + native int nFileA3DCreateFromAssetStream(int assetStream); + native int nFileA3DGetNumIndexEntries(int fileA3D); + native void nFileA3DGetIndexEntries(int fileA3D, int numEntries, int[] IDs, String[] names); + native int nFileA3DGetEntryByIndex(int fileA3D, int index); + native void nAdapter1DBindAllocation(int ad, int alloc); native void nAdapter1DSetConstraint(int ad, int dim, int value); native void nAdapter1DData(int ad, int[] d); @@ -134,32 +139,29 @@ public class RenderScript { native int nAdapter2DCreate(); native void nScriptBindAllocation(int script, int alloc, int slot); - native void nScriptSetClearColor(int script, float r, float g, float b, float a); - native void nScriptSetClearDepth(int script, float depth); - native void nScriptSetClearStencil(int script, int stencil); native void nScriptSetTimeZone(int script, byte[] timeZone); - native void nScriptSetType(int type, boolean writable, String name, int slot); - native void nScriptSetRoot(boolean isRoot); - native void nScriptSetInvokable(String name, int slot); native void nScriptInvoke(int id, int slot); + native void nScriptInvokeData(int id, int slot); + native void nScriptInvokeV(int id, int slot, byte[] params); + native void nScriptSetVarI(int id, int slot, int val); + native void nScriptSetVarF(int id, int slot, float val); + native void nScriptSetVarV(int id, int slot, byte[] val); native void nScriptCBegin(); native void nScriptCSetScript(byte[] script, int offset, int length); native int nScriptCCreate(); - native void nScriptCAddDefineI32(String name, int value); - native void nScriptCAddDefineF(String name, float value); native void nSamplerBegin(); native void nSamplerSet(int param, int value); native int nSamplerCreate(); - native void nProgramFragmentStoreBegin(int in, int out); - native void nProgramFragmentStoreDepthFunc(int func); - native void nProgramFragmentStoreDepthMask(boolean enable); - native void nProgramFragmentStoreColorMask(boolean r, boolean g, boolean b, boolean a); - native void nProgramFragmentStoreBlendFunc(int src, int dst); - native void nProgramFragmentStoreDither(boolean enable); - native int nProgramFragmentStoreCreate(); + native void nProgramStoreBegin(int in, int out); + native void nProgramStoreDepthFunc(int func); + native void nProgramStoreDepthMask(boolean enable); + native void nProgramStoreColorMask(boolean r, boolean g, boolean b, boolean a); + native void nProgramStoreBlendFunc(int src, int dst); + native void nProgramStoreDither(boolean enable); + native int nProgramStoreCreate(); native int nProgramRasterCreate(int in, int out, boolean pointSmooth, boolean lineSmooth, boolean pointSprite); native void nProgramRasterSetLineWidth(int pr, float v); @@ -195,13 +197,24 @@ public class RenderScript { @SuppressWarnings({"FieldCanBeLocal"}) protected MessageThread mMessageThread; - Element mElement_USER_U8; - Element mElement_USER_I8; - Element mElement_USER_U16; - Element mElement_USER_I16; - Element mElement_USER_U32; - Element mElement_USER_I32; - Element mElement_USER_F32; + Element mElement_U8; + Element mElement_I8; + Element mElement_U16; + Element mElement_I16; + Element mElement_U32; + Element mElement_I32; + Element mElement_F32; + + Element mElement_ELEMENT; + Element mElement_TYPE; + Element mElement_ALLOCATION; + Element mElement_SAMPLER; + Element mElement_SCRIPT; + Element mElement_MESH; + Element mElement_PROGRAM_FRAGMENT; + Element mElement_PROGRAM_VERTEX; + Element mElement_PROGRAM_RASTER; + Element mElement_PROGRAM_STORE; Element mElement_A_8; Element mElement_RGB_565; @@ -211,12 +224,17 @@ public class RenderScript { Element mElement_RGBA_8888; Element mElement_INDEX_16; - Element mElement_POSITION_2; - Element mElement_POSITION_3; - Element mElement_TEXTURE_2; - Element mElement_NORMAL_3; - Element mElement_COLOR_U8_4; - Element mElement_COLOR_F32_4; + Element mElement_FLOAT_2; + Element mElement_FLOAT_3; + Element mElement_FLOAT_4; + Element mElement_UCHAR_4; + + Sampler mSampler_CLAMP_NEAREST; + Sampler mSampler_CLAMP_LINEAR; + Sampler mSampler_CLAMP_LINEAR_MIP_LINEAR; + Sampler mSampler_WRAP_NEAREST; + Sampler mSampler_WRAP_LINEAR; + Sampler mSampler_WRAP_LINEAR_MIP_LINEAR; /////////////////////////////////////////////////////////////////////////////////// // @@ -282,7 +300,6 @@ public class RenderScript { mRS.mMessageCallback.mID = msg; mRS.mMessageCallback.run(); } - //Log.d(LOG_TAG, "MessageThread msg " + msg + " v1 " + rbuf[0] + " v2 " + rbuf[1] + " v3 " +rbuf[2]); } Log.d(LOG_TAG, "MessageThread exiting."); } diff --git a/graphics/java/android/renderscript/RenderScriptGL.java b/graphics/java/android/renderscript/RenderScriptGL.java index d1df23d..e90b4fc 100644 --- a/graphics/java/android/renderscript/RenderScriptGL.java +++ b/graphics/java/android/renderscript/RenderScriptGL.java @@ -74,9 +74,9 @@ public class RenderScriptGL extends RenderScript { nContextBindRootScript(safeID(s)); } - public void contextBindProgramFragmentStore(ProgramStore p) { + public void contextBindProgramStore(ProgramStore p) { validate(); - nContextBindProgramFragmentStore(safeID(p)); + nContextBindProgramStore(safeID(p)); } public void contextBindProgramFragment(ProgramFragment p) { diff --git a/graphics/java/android/renderscript/Sampler.java b/graphics/java/android/renderscript/Sampler.java index 40ba722..da83d04 100644 --- a/graphics/java/android/renderscript/Sampler.java +++ b/graphics/java/android/renderscript/Sampler.java @@ -51,6 +51,86 @@ public class Sampler extends BaseObj { mID = id; } + Sampler mSampler_CLAMP_NEAREST; + Sampler mSampler_CLAMP_LINEAR; + Sampler mSampler_CLAMP_LINEAR_MIP; + Sampler mSampler_WRAP_NEAREST; + Sampler mSampler_WRAP_LINEAR; + Sampler mSampler_WRAP_LINEAR_MIP; + + public static Sampler CLAMP_NEAREST(RenderScript rs) { + if(rs.mSampler_CLAMP_NEAREST == null) { + Builder b = new Builder(rs); + b.setMin(Value.NEAREST); + b.setMag(Value.NEAREST); + b.setWrapS(Value.CLAMP); + b.setWrapT(Value.CLAMP); + rs.mSampler_CLAMP_NEAREST = b.create(); + } + return rs.mSampler_CLAMP_NEAREST; + } + + public static Sampler CLAMP_LINEAR(RenderScript rs) { + if(rs.mSampler_CLAMP_LINEAR == null) { + Builder b = new Builder(rs); + b.setMin(Value.LINEAR); + b.setMag(Value.LINEAR); + b.setWrapS(Value.CLAMP); + b.setWrapT(Value.CLAMP); + rs.mSampler_CLAMP_LINEAR = b.create(); + } + return rs.mSampler_CLAMP_LINEAR; + } + + public static Sampler CLAMP_LINEAR_MIP_LINEAR(RenderScript rs) { + if(rs.mSampler_CLAMP_LINEAR_MIP_LINEAR == null) { + Builder b = new Builder(rs); + b.setMin(Value.LINEAR_MIP_LINEAR); + b.setMag(Value.LINEAR_MIP_LINEAR); + b.setWrapS(Value.CLAMP); + b.setWrapT(Value.CLAMP); + rs.mSampler_CLAMP_LINEAR_MIP_LINEAR = b.create(); + } + return rs.mSampler_CLAMP_LINEAR_MIP_LINEAR; + } + + public static Sampler WRAP_NEAREST(RenderScript rs) { + if(rs.mSampler_WRAP_NEAREST == null) { + Builder b = new Builder(rs); + b.setMin(Value.NEAREST); + b.setMag(Value.NEAREST); + b.setWrapS(Value.WRAP); + b.setWrapT(Value.WRAP); + rs.mSampler_WRAP_NEAREST = b.create(); + } + return rs.mSampler_WRAP_NEAREST; + } + + public static Sampler WRAP_LINEAR(RenderScript rs) { + if(rs.mSampler_WRAP_LINEAR == null) { + Builder b = new Builder(rs); + b.setMin(Value.LINEAR); + b.setMag(Value.LINEAR); + b.setWrapS(Value.WRAP); + b.setWrapT(Value.WRAP); + rs.mSampler_WRAP_LINEAR = b.create(); + } + return rs.mSampler_WRAP_LINEAR; + } + + public static Sampler WRAP_LINEAR_MIP_LINEAR(RenderScript rs) { + if(rs.mSampler_WRAP_LINEAR_MIP_LINEAR == null) { + Builder b = new Builder(rs); + b.setMin(Value.LINEAR_MIP_LINEAR); + b.setMag(Value.LINEAR_MIP_LINEAR); + b.setWrapS(Value.WRAP); + b.setWrapT(Value.WRAP); + rs.mSampler_WRAP_LINEAR_MIP_LINEAR = b.create(); + } + return rs.mSampler_WRAP_LINEAR_MIP_LINEAR; + } + + public static class Builder { RenderScript mRS; Value mMin; diff --git a/graphics/java/android/renderscript/Script.java b/graphics/java/android/renderscript/Script.java index 57ccfa3..4c4071a 100644 --- a/graphics/java/android/renderscript/Script.java +++ b/graphics/java/android/renderscript/Script.java @@ -42,6 +42,19 @@ public class Script extends BaseObj { } } + protected void invoke(int slot) { + mRS.nScriptInvoke(mID, slot); + } + + protected void invokeData(int slot) { + mRS.nScriptInvokeData(mID, slot); + } + + protected void invokeV(int slot, FieldPacker v) { + mRS.nScriptInvokeV(mID, slot, v.getData()); + } + + Script(int id, RenderScript rs) { super(rs); mID = id; @@ -49,22 +62,23 @@ public class Script extends BaseObj { public void bindAllocation(Allocation va, int slot) { mRS.validate(); - mRS.nScriptBindAllocation(mID, va.mID, slot); + if (va != null) { + mRS.nScriptBindAllocation(mID, va.mID, slot); + } else { + mRS.nScriptBindAllocation(mID, 0, slot); + } } - public void setClearColor(float r, float g, float b, float a) { - mRS.validate(); - mRS.nScriptSetClearColor(mID, r, g, b, a); + public void setVar(int index, float v) { + mRS.nScriptSetVarF(mID, index, v); } - public void setClearDepth(float d) { - mRS.validate(); - mRS.nScriptSetClearDepth(mID, d); + public void setVar(int index, int v) { + mRS.nScriptSetVarI(mID, index, v); } - public void setClearStencil(int stencil) { - mRS.validate(); - mRS.nScriptSetClearStencil(mID, stencil); + public void setVar(int index, FieldPacker v) { + mRS.nScriptSetVarV(mID, index, v.getData()); } public void setTimeZone(String timeZone) { @@ -78,72 +92,54 @@ public class Script extends BaseObj { public static class Builder { RenderScript mRS; - boolean mIsRoot = false; - Type[] mTypes; - String[] mNames; - boolean[] mWritable; - int mInvokableCount = 0; - Invokable[] mInvokables; Builder(RenderScript rs) { mRS = rs; - mTypes = new Type[MAX_SLOT]; - mNames = new String[MAX_SLOT]; - mWritable = new boolean[MAX_SLOT]; - mInvokables = new Invokable[MAX_SLOT]; } + } + - public void setType(Type t, int slot) { - mTypes[slot] = t; - mNames[slot] = null; + public static class FieldBase { + protected Element mElement; + protected Type mType; + protected Allocation mAllocation; + + protected void init(RenderScript rs, int dimx) { + mAllocation = Allocation.createSized(rs, mElement, dimx); + mType = mAllocation.getType(); } - public void setType(Type t, String name, int slot) { - mTypes[slot] = t; - mNames[slot] = name; + protected FieldBase() { } - public Invokable addInvokable(String func) { - Invokable i = new Invokable(); - i.mName = func; - i.mRS = mRS; - i.mSlot = mInvokableCount; - mInvokables[mInvokableCount++] = i; - return i; + public Element getElement() { + return mElement; } - public void setType(boolean writable, int slot) { - mWritable[slot] = writable; + public Type getType() { + return mType; } - void transferCreate() { - mRS.nScriptSetRoot(mIsRoot); - for(int ct=0; ct < mTypes.length; ct++) { - if(mTypes[ct] != null) { - mRS.nScriptSetType(mTypes[ct].mID, mWritable[ct], mNames[ct], ct); - } - } - for(int ct=0; ct < mInvokableCount; ct++) { - mRS.nScriptSetInvokable(mInvokables[ct].mName, ct); - } + public Allocation getAllocation() { + return mAllocation; } - void transferObject(Script s) { - s.mIsRoot = mIsRoot; - s.mTypes = mTypes; - s.mInvokables = new Invokable[mInvokableCount]; - for(int ct=0; ct < mInvokableCount; ct++) { - s.mInvokables[ct] = mInvokables[ct]; - s.mInvokables[ct].mScript = s; - } - s.mInvokables = null; + //@Override + public void updateAllocation() { } - public void setRoot(boolean r) { - mIsRoot = r; + + // + /* + public class ScriptField_UserField + extends android.renderscript.Script.FieldBase { + + protected + } - } + */ + } } diff --git a/graphics/java/android/renderscript/ScriptC.java b/graphics/java/android/renderscript/ScriptC.java index bb99e23..5959be4 100644 --- a/graphics/java/android/renderscript/ScriptC.java +++ b/graphics/java/android/renderscript/ScriptC.java @@ -37,11 +37,49 @@ public class ScriptC extends Script { super(id, rs); } + protected ScriptC(RenderScript rs, Resources resources, int resourceID, boolean isRoot) { + super(0, rs); + mID = internalCreate(rs, resources, resourceID); + } + + + private static synchronized int internalCreate(RenderScript rs, Resources resources, int resourceID) { + byte[] pgm; + int pgmLength; + InputStream is = resources.openRawResource(resourceID); + try { + try { + pgm = new byte[1024]; + pgmLength = 0; + while(true) { + int bytesLeft = pgm.length - pgmLength; + if (bytesLeft == 0) { + byte[] buf2 = new byte[pgm.length * 2]; + System.arraycopy(pgm, 0, buf2, 0, pgm.length); + pgm = buf2; + bytesLeft = pgm.length - pgmLength; + } + int bytesRead = is.read(pgm, pgmLength, bytesLeft); + if (bytesRead <= 0) { + break; + } + pgmLength += bytesRead; + } + } finally { + is.close(); + } + } catch(IOException e) { + throw new Resources.NotFoundException(); + } + + rs.nScriptCBegin(); + rs.nScriptCSetScript(pgm, 0, pgmLength); + return rs.nScriptCCreate(); + } + public static class Builder extends Script.Builder { byte[] mProgram; int mProgramLength; - HashMap<String,Integer> mIntDefines = new HashMap(); - HashMap<String,Float> mFloatDefines = new HashMap(); public Builder(RenderScript rs) { super(rs); @@ -92,66 +130,20 @@ public class ScriptC extends Script { static synchronized ScriptC internalCreate(Builder b) { b.mRS.nScriptCBegin(); - b.transferCreate(); - - for (Entry<String,Integer> e: b.mIntDefines.entrySet()) { - b.mRS.nScriptCAddDefineI32(e.getKey(), e.getValue().intValue()); - } - for (Entry<String,Float> e: b.mFloatDefines.entrySet()) { - b.mRS.nScriptCAddDefineF(e.getKey(), e.getValue().floatValue()); - } + android.util.Log.e("rs", "len = " + b.mProgramLength); b.mRS.nScriptCSetScript(b.mProgram, 0, b.mProgramLength); int id = b.mRS.nScriptCCreate(); ScriptC obj = new ScriptC(id, b.mRS); - b.transferObject(obj); - return obj; } - public void addDefine(String name, int value) { - mIntDefines.put(name, value); - } - - public void addDefine(String name, float value) { - mFloatDefines.put(name, value); - } - - /** - * Takes the all public static final fields for a class, and adds defines - * for them, using the name of the field as the name of the define. - */ - public void addDefines(Class cl) { - addDefines(cl.getFields(), (Modifier.STATIC | Modifier.FINAL | Modifier.PUBLIC), null); - } - - /** - * Takes the all public fields for an object, and adds defines - * for them, using the name of the field as the name of the define. - */ - public void addDefines(Object o) { - addDefines(o.getClass().getFields(), Modifier.PUBLIC, o); - } - - void addDefines(Field[] fields, int mask, Object o) { - for (Field f: fields) { - try { - if ((f.getModifiers() & mask) == mask) { - Class t = f.getType(); - if (t == int.class) { - mIntDefines.put(f.getName(), f.getInt(o)); - } - else if (t == float.class) { - mFloatDefines.put(f.getName(), f.getFloat(o)); - } - } - } catch (IllegalAccessException ex) { - // TODO: Do we want this log? - Log.d(TAG, "addDefines skipping field " + f.getName()); - } - } - } + public void addDefine(String name, int value) {} + public void addDefine(String name, float value) {} + public void addDefines(Class cl) {} + public void addDefines(Object o) {} + void addDefines(Field[] fields, int mask, Object o) {} public ScriptC create() { return internalCreate(this); diff --git a/graphics/java/android/renderscript/Short2.java b/graphics/java/android/renderscript/Short2.java new file mode 100644 index 0000000..426801f --- /dev/null +++ b/graphics/java/android/renderscript/Short2.java @@ -0,0 +1,37 @@ +/* + * Copyright (C) 2009 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.renderscript; + +import java.lang.Math; +import android.util.Log; + + +/** + * @hide + * + **/ +public class Short2 { + public Short2() { + } + + public short x; + public short y; +} + + + + diff --git a/graphics/java/android/renderscript/Short3.java b/graphics/java/android/renderscript/Short3.java new file mode 100644 index 0000000..7b9c305 --- /dev/null +++ b/graphics/java/android/renderscript/Short3.java @@ -0,0 +1,38 @@ +/* + * Copyright (C) 2009 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.renderscript; + +import java.lang.Math; +import android.util.Log; + + +/** + * @hide + * + **/ +public class Short3 { + public Short3() { + } + + public short x; + public short y; + public short z; +} + + + + diff --git a/graphics/java/android/renderscript/Short4.java b/graphics/java/android/renderscript/Short4.java new file mode 100644 index 0000000..9a474e2 --- /dev/null +++ b/graphics/java/android/renderscript/Short4.java @@ -0,0 +1,38 @@ +/* + * Copyright (C) 2009 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.renderscript; + +import java.lang.Math; +import android.util.Log; + + +/** + * @hide + * + **/ +public class Short4 { + public Short4() { + } + + public short x; + public short y; + public short z; + public short w; +} + + + diff --git a/graphics/java/android/renderscript/SimpleMesh.java b/graphics/java/android/renderscript/SimpleMesh.java index 4a217a9..fc011b5 100644 --- a/graphics/java/android/renderscript/SimpleMesh.java +++ b/graphics/java/android/renderscript/SimpleMesh.java @@ -313,30 +313,20 @@ public class SimpleMesh extends BaseObj { public SimpleMesh create() { Element.Builder b = new Element.Builder(mRS); int floatCount = mVtxSize; - b.add(Element.createAttrib(mRS, + b.add(Element.createVector(mRS, Element.DataType.FLOAT_32, - Element.DataKind.POSITION, mVtxSize), "position"); if ((mFlags & COLOR) != 0) { floatCount += 4; - b.add(Element.createAttrib(mRS, - Element.DataType.FLOAT_32, - Element.DataKind.COLOR, - 4), "color"); + b.add(Element.F32_4(mRS), "color"); } if ((mFlags & TEXTURE_0) != 0) { floatCount += 2; - b.add(Element.createAttrib(mRS, - Element.DataType.FLOAT_32, - Element.DataKind.TEXTURE, - 2), "texture"); + b.add(Element.F32_2(mRS), "texture0"); } if ((mFlags & NORMAL) != 0) { floatCount += 3; - b.add(Element.createAttrib(mRS, - Element.DataType.FLOAT_32, - Element.DataKind.NORMAL, - 3), "normal"); + b.add(Element.F32_3(mRS), "normal"); } mElement = b.create(); diff --git a/graphics/java/android/renderscript/Type.java b/graphics/java/android/renderscript/Type.java index 62d3867..14422b2 100644 --- a/graphics/java/android/renderscript/Type.java +++ b/graphics/java/android/renderscript/Type.java @@ -108,48 +108,9 @@ public class Type extends BaseObj { super.finalize(); } - public static Type createFromClass(RenderScript rs, Class c, int size) { - Element e = Element.createFromClass(rs, c); - Builder b = new Builder(rs, e); - b.add(Dimension.X, size); - Type t = b.create(); - e.destroy(); - - // native fields - { - Field[] fields = c.getFields(); - int[] arTypes = new int[fields.length]; - int[] arBits = new int[fields.length]; - - for(int ct=0; ct < fields.length; ct++) { - Field f = fields[ct]; - Class fc = f.getType(); - if(fc == int.class) { - arTypes[ct] = Element.DataType.SIGNED_32.mID; - arBits[ct] = 32; - } else if(fc == short.class) { - arTypes[ct] = Element.DataType.SIGNED_16.mID; - arBits[ct] = 16; - } else if(fc == byte.class) { - arTypes[ct] = Element.DataType.SIGNED_8.mID; - arBits[ct] = 8; - } else if(fc == float.class) { - arTypes[ct] = Element.DataType.FLOAT_32.mID; - arBits[ct] = 32; - } else { - throw new IllegalArgumentException("Unkown field type"); - } - } - rs.nTypeSetupFields(t, arTypes, arBits, fields); - } - t.mJavaClass = c; - return t; - } - public static Type createFromClass(RenderScript rs, Class c, int size, String scriptName) { - Type t = createFromClass(rs, c, size); - t.setName(scriptName); - return t; + android.util.Log.e("RenderScript", "Calling depricated createFromClass"); + return null; } |
