diff options
Diffstat (limited to 'graphics/java')
17 files changed, 836 insertions, 78 deletions
diff --git a/graphics/java/android/graphics/Bitmap.java b/graphics/java/android/graphics/Bitmap.java index c726d0e..6ba57809 100644 --- a/graphics/java/android/graphics/Bitmap.java +++ b/graphics/java/android/graphics/Bitmap.java @@ -27,7 +27,6 @@ import java.nio.IntBuffer; import java.nio.ShortBuffer; public final class Bitmap implements Parcelable { - /** * Indicates that the bitmap was created for an unknown pixel density. * @@ -64,7 +63,7 @@ public final class Bitmap implements Parcelable { private boolean mRecycled; // Package-scoped for fast access. - /*package*/ int mDensity = sDefaultDensity = getDefaultDensity(); + int mDensity = getDefaultDensity(); private static volatile Matrix sScaleMatrix; @@ -78,15 +77,15 @@ public final class Bitmap implements Parcelable { public static void setDefaultDensity(int density) { sDefaultDensity = density; } - - /*package*/ static int getDefaultDensity() { + + static int getDefaultDensity() { if (sDefaultDensity >= 0) { return sDefaultDensity; } sDefaultDensity = DisplayMetrics.DENSITY_DEVICE; return sDefaultDensity; } - + /** * @noinspection UnusedDeclaration */ @@ -95,7 +94,7 @@ public final class Bitmap implements Parcelable { This can be called from JNI code. */ - /*package*/ Bitmap(int nativeBitmap, byte[] buffer, boolean isMutable, byte[] ninePatchChunk, + Bitmap(int nativeBitmap, byte[] buffer, boolean isMutable, byte[] ninePatchChunk, int density) { this(nativeBitmap, buffer, isMutable, ninePatchChunk, null, density); } @@ -108,7 +107,7 @@ public final class Bitmap implements Parcelable { This can be called from JNI code. */ - /*package*/ Bitmap(int nativeBitmap, byte[] buffer, boolean isMutable, byte[] ninePatchChunk, + Bitmap(int nativeBitmap, byte[] buffer, boolean isMutable, byte[] ninePatchChunk, int[] layoutBounds, int density) { if (nativeBitmap == 0) { throw new RuntimeException("internal error: native bitmap is 0"); @@ -347,11 +346,15 @@ public final class Bitmap implements Parcelable { } /** - * Copy the bitmap's pixels into the specified buffer (allocated by the + * <p>Copy the bitmap's pixels into the specified buffer (allocated by the * caller). An exception is thrown if the buffer is not large enough to * hold all of the pixels (taking into account the number of bytes per * pixel) or if the Buffer subclass is not one of the support types - * (ByteBuffer, ShortBuffer, IntBuffer). + * (ByteBuffer, ShortBuffer, IntBuffer).</p> + * <p>The content of the bitmap is copied into the buffer as-is. This means + * that if this bitmap stores its pixels pre-multiplied + * (see {@link #isPremultiplied()}, the values in the buffer will also be + * pre-multiplied.</p> */ public void copyPixelsToBuffer(Buffer dst) { int elements = dst.remaining(); @@ -382,10 +385,10 @@ public final class Bitmap implements Parcelable { } /** - * Copy the pixels from the buffer, beginning at the current position, + * <p>Copy the pixels from the buffer, beginning at the current position, * overwriting the bitmap's pixels. The data in the buffer is not changed * in any way (unlike setPixels(), which converts from unpremultipled 32bit - * to whatever the bitmap's native format is. + * to whatever the bitmap's native format is.</p> */ public void copyPixelsFromBuffer(Buffer src) { checkRecycled("copyPixelsFromBuffer called on recycled bitmap"); @@ -402,7 +405,7 @@ public final class Bitmap implements Parcelable { throw new RuntimeException("unsupported Buffer subclass"); } - long bufferBytes = (long)elements << shift; + long bufferBytes = (long) elements << shift; long bitmapBytes = getByteCount(); if (bufferBytes < bitmapBytes) { @@ -622,6 +625,22 @@ public final class Bitmap implements Parcelable { /** * Returns a mutable bitmap with the specified width and height. Its + * initial density is determined from the given {@link DisplayMetrics}. + * + * @param display Display metrics for the display this bitmap will be + * drawn on. + * @param width The width of the bitmap + * @param height The height of the bitmap + * @param config The bitmap config to create. + * @throws IllegalArgumentException if the width or height are <= 0 + */ + public static Bitmap createBitmap(DisplayMetrics display, int width, + int height, Config config) { + return createBitmap(display, width, height, config, true); + } + + /** + * Returns a mutable bitmap with the specified width and height. Its * initial density is as per {@link #getDensity}. * * @param width The width of the bitmap @@ -634,10 +653,33 @@ public final class Bitmap implements Parcelable { * @throws IllegalArgumentException if the width or height are <= 0 */ private static Bitmap createBitmap(int width, int height, Config config, boolean hasAlpha) { + return createBitmap(null, width, height, config, hasAlpha); + } + + /** + * Returns a mutable bitmap with the specified width and height. Its + * initial density is determined from the given {@link DisplayMetrics}. + * + * @param display Display metrics for the display this bitmap will be + * drawn on. + * @param width The width of the bitmap + * @param height The height of the bitmap + * @param config The bitmap config to create. + * @param hasAlpha If the bitmap is ARGB_8888 this flag can be used to mark the + * bitmap as opaque. Doing so will clear the bitmap in black + * instead of transparent. + * + * @throws IllegalArgumentException if the width or height are <= 0 + */ + private static Bitmap createBitmap(DisplayMetrics display, int width, int height, + Config config, boolean hasAlpha) { if (width <= 0 || height <= 0) { throw new IllegalArgumentException("width and height must be > 0"); } Bitmap bm = nativeCreate(null, 0, width, width, height, config.nativeInt, true); + if (display != null) { + bm.mDensity = display.densityDpi; + } if (config == Config.ARGB_8888 && !hasAlpha) { nativeErase(bm.mNativeBitmap, 0xff000000); nativeSetHasAlpha(bm.mNativeBitmap, hasAlpha); @@ -670,6 +712,31 @@ public final class Bitmap implements Parcelable { */ public static Bitmap createBitmap(int colors[], int offset, int stride, int width, int height, Config config) { + return createBitmap(null, colors, offset, stride, width, height, config); + } + + /** + * Returns a immutable bitmap with the specified width and height, with each + * pixel value set to the corresponding value in the colors array. Its + * initial density is determined from the given {@link DisplayMetrics}. + * + * @param display Display metrics for the display this bitmap will be + * drawn on. + * @param colors Array of {@link Color} used to initialize the pixels. + * @param offset Number of values to skip before the first color in the + * array of colors. + * @param stride Number of colors in the array between rows (must be >= + * width or <= -width). + * @param width The width of the bitmap + * @param height The height of the bitmap + * @param config The bitmap config to create. If the config does not + * support per-pixel alpha (e.g. RGB_565), then the alpha + * bytes in the colors[] will be ignored (assumed to be FF) + * @throws IllegalArgumentException if the width or height are <= 0, or if + * the color array's length is less than the number of pixels. + */ + public static Bitmap createBitmap(DisplayMetrics display, int colors[], + int offset, int stride, int width, int height, Config config) { checkWidthHeight(width, height); if (Math.abs(stride) < width) { @@ -684,8 +751,12 @@ public final class Bitmap implements Parcelable { if (width <= 0 || height <= 0) { throw new IllegalArgumentException("width and height must be > 0"); } - return nativeCreate(colors, offset, stride, width, height, + Bitmap bm = nativeCreate(colors, offset, stride, width, height, config.nativeInt, false); + if (display != null) { + bm.mDensity = display.densityDpi; + } + return bm; } /** @@ -704,7 +775,29 @@ public final class Bitmap implements Parcelable { * the color array's length is less than the number of pixels. */ public static Bitmap createBitmap(int colors[], int width, int height, Config config) { - return createBitmap(colors, 0, width, width, height, config); + return createBitmap(null, colors, 0, width, width, height, config); + } + + /** + * Returns a immutable bitmap with the specified width and height, with each + * pixel value set to the corresponding value in the colors array. Its + * initial density is determined from the given {@link DisplayMetrics}. + * + * @param display Display metrics for the display this bitmap will be + * drawn on. + * @param colors Array of {@link Color} used to initialize the pixels. + * This array must be at least as large as width * height. + * @param width The width of the bitmap + * @param height The height of the bitmap + * @param config The bitmap config to create. If the config does not + * support per-pixel alpha (e.g. RGB_565), then the alpha + * bytes in the colors[] will be ignored (assumed to be FF) + * @throws IllegalArgumentException if the width or height are <= 0, or if + * the color array's length is less than the number of pixels. + */ + public static Bitmap createBitmap(DisplayMetrics display, int colors[], + int width, int height, Config config) { + return createBitmap(display, colors, 0, width, width, height, config); } /** @@ -780,6 +873,27 @@ public final class Bitmap implements Parcelable { return mIsMutable; } + /** + * <p>Indicates whether pixels stored in this bitmaps are stored pre-multiplied. + * When a pixel is pre-multiplied, the RGB components have been multiplied by + * the alpha component. For instance, if the original color is a 50% + * translucent red <code>(128, 255, 0, 0)</code>, the pre-multiplied form is + * <code>(128, 128, 0, 0)</code>.</p> + * + * <p>This method always returns false if {@link #getConfig()} is + * {@link Bitmap.Config#RGB_565}.</p> + * + * <p>This method only returns true if {@link #hasAlpha()} returns true. + * A bitmap with no alpha channel can be used both as a pre-multiplied and + * as a non pre-multiplied bitmap.</p> + * + * @return true if the underlying pixels have been pre-multiplied, false + * otherwise + */ + public final boolean isPremultiplied() { + return getConfig() != Config.RGB_565 && hasAlpha(); + } + /** Returns the bitmap's width */ public final int getWidth() { return mWidth == -1 ? mWidth = nativeWidth(mNativeBitmap) : mWidth; @@ -926,7 +1040,7 @@ public final class Bitmap implements Parcelable { /** * Returns the {@link Color} at the specified location. Throws an exception * if x or y are out of bounds (negative or >= to the width or height - * respectively). + * respectively). The returned color is a non-premultiplied ARGB value. * * @param x The x coordinate (0...width-1) of the pixel to return * @param y The y coordinate (0...height-1) of the pixel to return @@ -944,6 +1058,7 @@ public final class Bitmap implements Parcelable { * a packed int representing a {@link Color}. The stride parameter allows * the caller to allow for gaps in the returned pixels array between * rows. For normal packed results, just pass width for the stride value. + * The returned colors are non-premultiplied ARGB values. * * @param pixels The array to receive the bitmap's colors * @param offset The first index to write into pixels[] @@ -955,6 +1070,7 @@ public final class Bitmap implements Parcelable { * the bitmap * @param width The number of pixels to read from each row * @param height The number of rows to read + * * @throws IllegalArgumentException if x, y, width, height exceed the * bounds of the bitmap, or if abs(stride) < width. * @throws ArrayIndexOutOfBoundsException if the pixels array is too small @@ -974,6 +1090,7 @@ public final class Bitmap implements Parcelable { /** * Shared code to check for illegal arguments passed to getPixel() * or setPixel() + * * @param x x coordinate of the pixel * @param y y coordinate of the pixel */ @@ -1029,12 +1146,14 @@ public final class Bitmap implements Parcelable { } /** - * Write the specified {@link Color} into the bitmap (assuming it is - * mutable) at the x,y coordinate. + * <p>Write the specified {@link Color} into the bitmap (assuming it is + * mutable) at the x,y coordinate. The color must be a + * non-premultiplied ARGB value.</p> * * @param x The x coordinate of the pixel to replace (0...width-1) * @param y The y coordinate of the pixel to replace (0...height-1) - * @param color The {@link Color} to write into the bitmap + * @param color The ARGB color to write into the bitmap + * * @throws IllegalStateException if the bitmap is not mutable * @throws IllegalArgumentException if x, y are outside of the bitmap's * bounds. @@ -1049,8 +1168,9 @@ public final class Bitmap implements Parcelable { } /** - * Replace pixels in the bitmap with the colors in the array. Each element - * in the array is a packed int prepresenting a {@link Color} + * <p>Replace pixels in the bitmap with the colors in the array. Each element + * in the array is a packed int prepresenting a non-premultiplied ARGB + * {@link Color}.</p> * * @param pixels The colors to write to the bitmap * @param offset The index of the first color to read from pixels[] @@ -1063,6 +1183,7 @@ public final class Bitmap implements Parcelable { * the bitmap. * @param width The number of colors to copy from pixels[] per row * @param height The number of rows to write to the bitmap + * * @throws IllegalStateException if the bitmap is not mutable * @throws IllegalArgumentException if x, y, width, height are outside of * the bitmap's bounds. @@ -1070,7 +1191,7 @@ public final class Bitmap implements Parcelable { * to receive the specified number of pixels. */ public void setPixels(int[] pixels, int offset, int stride, - int x, int y, int width, int height) { + int x, int y, int width, int height) { checkRecycled("Can't call setPixels() on a recycled bitmap"); if (!isMutable()) { throw new IllegalStateException(); diff --git a/graphics/java/android/graphics/BitmapFactory.java b/graphics/java/android/graphics/BitmapFactory.java index 5094df18..381e65b 100644 --- a/graphics/java/android/graphics/BitmapFactory.java +++ b/graphics/java/android/graphics/BitmapFactory.java @@ -19,6 +19,7 @@ package android.graphics; import android.content.res.AssetManager; import android.content.res.Resources; import android.util.DisplayMetrics; +import android.util.Log; import android.util.TypedValue; import java.io.BufferedInputStream; @@ -303,6 +304,7 @@ public class BitmapFactory { /* do nothing. If the exception happened on open, bm will be null. */ + Log.e("BitmapFactory", "Unable to decode stream: " + e); } finally { if (stream != null) { try { diff --git a/graphics/java/android/graphics/Paint.java b/graphics/java/android/graphics/Paint.java index f9b8a5f..f68f9dc 100644 --- a/graphics/java/android/graphics/Paint.java +++ b/graphics/java/android/graphics/Paint.java @@ -1055,7 +1055,6 @@ public class Paint { * Get the text Locale. * * @return the paint's Locale used for drawing text, never null. - * @hide */ public Locale getTextLocale() { return mLocale; @@ -1086,7 +1085,6 @@ public class Paint { * job in certain ambiguous cases * * @param locale the paint's locale value for drawing text, must not be null. - * @hide */ public void setTextLocale(Locale locale) { if (locale == null) { diff --git a/graphics/java/android/graphics/drawable/BitmapDrawable.java b/graphics/java/android/graphics/drawable/BitmapDrawable.java index 87421b1..e82ccd4 100644 --- a/graphics/java/android/graphics/drawable/BitmapDrawable.java +++ b/graphics/java/android/graphics/drawable/BitmapDrawable.java @@ -385,7 +385,7 @@ public class BitmapDrawable extends Drawable { Shader shader = state.mPaint.getShader(); if (shader == null) { if (mApplyGravity) { - final int layoutDirection = getResolvedLayoutDirectionSelf(); + final int layoutDirection = getLayoutDirection(); Gravity.apply(state.mGravity, mBitmapWidth, mBitmapHeight, getBounds(), mDstRect, layoutDirection); mApplyGravity = false; diff --git a/graphics/java/android/graphics/drawable/ClipDrawable.java b/graphics/java/android/graphics/drawable/ClipDrawable.java index c41dd07..bade9b4 100644 --- a/graphics/java/android/graphics/drawable/ClipDrawable.java +++ b/graphics/java/android/graphics/drawable/ClipDrawable.java @@ -209,7 +209,7 @@ public class ClipDrawable extends Drawable implements Drawable.Callback { if ((mClipState.mOrientation & VERTICAL) != 0) { h -= (h - ih) * (10000 - level) / 10000; } - final int layoutDirection = getResolvedLayoutDirectionSelf(); + final int layoutDirection = getLayoutDirection(); Gravity.apply(mClipState.mGravity, w, h, bounds, r, layoutDirection); if (w > 0 && h > 0) { diff --git a/graphics/java/android/graphics/drawable/Drawable.java b/graphics/java/android/graphics/drawable/Drawable.java index 785582c..4bc5a5a 100644 --- a/graphics/java/android/graphics/drawable/Drawable.java +++ b/graphics/java/android/graphics/drawable/Drawable.java @@ -124,6 +124,8 @@ public abstract class Drawable { private WeakReference<Callback> mCallback = null; private boolean mVisible = true; + private int mLayoutDirection; + /** * Draw in its bounds (set via setBounds) respecting optional effects such * as alpha (set via setAlpha) and color filter (set via setColorFilter). @@ -296,19 +298,6 @@ public abstract class Drawable { } /** - * Implement this interface if you want to create an drawable that is RTL aware - * @hide - */ - public static interface Callback2 extends Callback { - /** - * A Drawable can call this to get the resolved layout direction of the <var>who</var>. - * - * @param who The drawable being queried. - */ - public int getResolvedLayoutDirection(Drawable who); - } - - /** * Bind a {@link Callback} object to this Drawable. Required for clients * that want to support animated drawables. * @@ -385,15 +374,27 @@ public abstract class Drawable { } /** - * Get the resolved layout direction of this Drawable. - * @hide + * Returns the resolved layout direction for this Drawable. + * + * @return One of {@link View#LAYOUT_DIRECTION_LTR}, + * {@link View#LAYOUT_DIRECTION_RTL} */ - public int getResolvedLayoutDirectionSelf() { - final Callback callback = getCallback(); - if (callback == null || !(callback instanceof Callback2)) { - return View.LAYOUT_DIRECTION_LTR; + public int getLayoutDirection() { + return mLayoutDirection; + } + + /** + * Set the layout direction for this drawable. Should be a resolved direction as the + * Drawable as no capacity to do the resolution on his own. + * + * @param layoutDirection One of {@link View#LAYOUT_DIRECTION_LTR}, + * {@link View#LAYOUT_DIRECTION_RTL}, + * + */ + public void setLayoutDirection(int layoutDirection) { + if (getLayoutDirection() != layoutDirection) { + mLayoutDirection = layoutDirection; } - return ((Callback2) callback).getResolvedLayoutDirection(this); } /** @@ -777,7 +778,8 @@ public abstract class Drawable { // to the compatibility density only to have them scaled back up when // drawn to the screen. if (opts == null) opts = new BitmapFactory.Options(); - opts.inScreenDensity = DisplayMetrics.DENSITY_DEVICE; + opts.inScreenDensity = res != null + ? res.getDisplayMetrics().noncompatDensityDpi : DisplayMetrics.DENSITY_DEVICE; Bitmap bm = BitmapFactory.decodeResourceStream(res, value, is, pad, opts); if (bm != null) { byte[] np = bm.getNinePatchChunk(); diff --git a/graphics/java/android/graphics/drawable/NinePatchDrawable.java b/graphics/java/android/graphics/drawable/NinePatchDrawable.java index b68b267..62aea71 100644 --- a/graphics/java/android/graphics/drawable/NinePatchDrawable.java +++ b/graphics/java/android/graphics/drawable/NinePatchDrawable.java @@ -296,7 +296,7 @@ public class NinePatchDrawable extends Drawable { if (dither) { options.inDither = false; } - options.inScreenDensity = DisplayMetrics.DENSITY_DEVICE; + options.inScreenDensity = r.getDisplayMetrics().noncompatDensityDpi; final Rect padding = new Rect(); final Rect layoutInsets = new Rect(); diff --git a/graphics/java/android/graphics/drawable/ScaleDrawable.java b/graphics/java/android/graphics/drawable/ScaleDrawable.java index ccad250..bd2b2f0 100644 --- a/graphics/java/android/graphics/drawable/ScaleDrawable.java +++ b/graphics/java/android/graphics/drawable/ScaleDrawable.java @@ -221,7 +221,7 @@ public class ScaleDrawable extends Drawable implements Drawable.Callback { final int ih = min ? mScaleState.mDrawable.getIntrinsicHeight() : 0; h -= (int) ((h - ih) * (10000 - level) * mScaleState.mScaleHeight / 10000); } - final int layoutDirection = getResolvedLayoutDirectionSelf(); + final int layoutDirection = getLayoutDirection(); Gravity.apply(mScaleState.mGravity, w, h, bounds, r, layoutDirection); if (w > 0 && h > 0) { diff --git a/graphics/java/android/renderscript/Matrix2f.java b/graphics/java/android/renderscript/Matrix2f.java index acc5bd8..39abd4f 100644 --- a/graphics/java/android/renderscript/Matrix2f.java +++ b/graphics/java/android/renderscript/Matrix2f.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2009 The Android Open Source Project + * Copyright (C) 2009-2012 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. @@ -59,23 +59,23 @@ public class Matrix2f { /** * Returns the value for a given row and column * - * @param i row of the value to return - * @param j column of the value to return + * @param x column of the value to return + * @param y row of the value to return * - * @return value in the ith row and jth column + * @return value in the yth row and xth column */ - public float get(int i, int j) { - return mMat[i*2 + j]; + public float get(int x, int y) { + return mMat[x*2 + y]; } /** * Sets the value for a given row and column * - * @param i row of the value to set - * @param j column of the value to set + * @param x column of the value to set + * @param y row of the value to set */ - public void set(int i, int j, float v) { - mMat[i*2 + j] = v; + public void set(int x, int y, float v) { + mMat[x*2 + y] = v; } /** diff --git a/graphics/java/android/renderscript/Matrix3f.java b/graphics/java/android/renderscript/Matrix3f.java index 253506d..66f2c81 100644 --- a/graphics/java/android/renderscript/Matrix3f.java +++ b/graphics/java/android/renderscript/Matrix3f.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2009 The Android Open Source Project + * Copyright (C) 2009-2012 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. @@ -59,23 +59,23 @@ public class Matrix3f { /** * Returns the value for a given row and column * - * @param i row of the value to return - * @param j column of the value to return + * @param x column of the value to return + * @param y row of the value to return * - * @return value in the ith row and jth column + * @return value in the yth row and xth column */ - public float get(int i, int j) { - return mMat[i*3 + j]; + public float get(int x, int y) { + return mMat[x*3 + y]; } /** * Sets the value for a given row and column * - * @param i row of the value to set - * @param j column of the value to set + * @param x column of the value to set + * @param y row of the value to set */ - public void set(int i, int j, float v) { - mMat[i*3 + j] = v; + public void set(int x, int y, float v) { + mMat[x*3 + y] = v; } /** diff --git a/graphics/java/android/renderscript/Matrix4f.java b/graphics/java/android/renderscript/Matrix4f.java index adc1806..a85d464 100644 --- a/graphics/java/android/renderscript/Matrix4f.java +++ b/graphics/java/android/renderscript/Matrix4f.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2009 The Android Open Source Project + * Copyright (C) 2009-2012 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. @@ -59,23 +59,23 @@ public class Matrix4f { /** * Returns the value for a given row and column * - * @param i row of the value to return - * @param j column of the value to return + * @param x column of the value to return + * @param y row of the value to return * - * @return value in the ith row and jth column + * @return value in the yth row and xth column */ - public float get(int i, int j) { - return mMat[i*4 + j]; + public float get(int x, int y) { + return mMat[x*4 + y]; } /** * Sets the value for a given row and column * - * @param i row of the value to set - * @param j column of the value to set + * @param x column of the value to set + * @param y row of the value to set */ - public void set(int i, int j, float v) { - mMat[i*4 + j] = v; + public void set(int x, int y, float v) { + mMat[x*4 + y] = v; } /** diff --git a/graphics/java/android/renderscript/RenderScript.java b/graphics/java/android/renderscript/RenderScript.java index 2032f67..af8b0c2 100644 --- a/graphics/java/android/renderscript/RenderScript.java +++ b/graphics/java/android/renderscript/RenderScript.java @@ -561,6 +561,12 @@ public class RenderScript { return rsnScriptCCreate(mContext, resName, cacheDir, script, length); } + native int rsnScriptIntrinsicCreate(int con, int id, int eid); + synchronized int nScriptIntrinsicCreate(int id, int eid) { + validate(); + return rsnScriptIntrinsicCreate(mContext, id, eid); + } + native int rsnSamplerCreate(int con, int magFilter, int minFilter, int wrapS, int wrapT, int wrapR, float aniso); synchronized int nSamplerCreate(int magFilter, int minFilter, diff --git a/graphics/java/android/renderscript/ScriptGroup.java b/graphics/java/android/renderscript/ScriptGroup.java new file mode 100644 index 0000000..c4064b5 --- /dev/null +++ b/graphics/java/android/renderscript/ScriptGroup.java @@ -0,0 +1,391 @@ +/* + * Copyright (C) 2012 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.reflect.Method; + +/** + * @hide + **/ +public class ScriptGroup extends BaseObj { + Node mNodes[]; + Connection mConnections[]; + Node mFirstNode; + IO mOutputs[]; + IO mInputs[]; + + static class IO { + Script mScript; + Allocation mAllocation; + String mName; + + IO(Script s) { + mScript = s; + } + IO(Script s, String n) { + mScript = s; + mName = n; + } + } + + static class Connection { + Node mTo[]; + String mToName[]; + Node mFrom; + Type mAllocationType; + Allocation mInternalAllocation; + + Connection(Node out, Type t) { + mFrom = out; + mAllocationType = t; + } + + void addTo(Node n, String name) { + if (mTo == null) { + mTo = new Node[1]; + mToName = new String[1]; + } else { + Node nt[] = new Node[mTo.length + 1]; + String ns[] = new String[mTo.length + 1]; + System.arraycopy(mTo, 0, nt, 0, mTo.length); + System.arraycopy(mToName, 0, ns, 0, mTo.length); + mTo = nt; + mToName = ns; + } + mTo[mTo.length - 1] = n; + mToName[mTo.length - 1] = name; + } + } + + static class Node { + Script mScript; + Connection mInput[] = new Connection[8]; + Connection mOutput[] = new Connection[1]; + int mInputCount; + int mOutputCount; + int mDepth; + boolean mSeen; + + Node mNext; + + Node(Script s) { + mScript = s; + } + + void addInput(Connection c) { + if (mInput.length <= mInputCount) { + Connection[] nc = new Connection[mInput.length + 8]; + System.arraycopy(mInput, 0, nc, 0, mInputCount); + mInput = nc; + } + mInput[mInputCount++] = c; + } + + void addOutput(Connection c) { + if (mOutput.length <= mOutputCount) { + Connection[] nc = new Connection[mOutput.length + 8]; + System.arraycopy(mOutput, 0, nc, 0, mOutputCount); + mOutput = nc; + } + mOutput[mOutputCount++] = c; + } + } + + + ScriptGroup(int id, RenderScript rs) { + super(id, rs); + } + + void init(int nodeCount, int connectionCount) { + mNodes = new Node[nodeCount]; + mConnections = new Connection[connectionCount]; + + android.util.Log.v("RSR", "init" + nodeCount + ", " + connectionCount); + + // Count outputs and create array. + Node n = mFirstNode; + int outputCount = 0; + int inputCount = 0; + int connectionIndex = 0; + int nodeNum = 0; + while (n != null) { + mNodes[nodeNum++] = n; + + // Look for unattached kernel inputs + boolean hasInput = false; + for (int ct=0; ct < n.mInput.length; ct++) { + if (n.mInput[ct] != null) { + if (n.mInput[ct].mToName == null) { + hasInput = true; + } + } + } + if (!hasInput) { + if (mInputs == null) { + mInputs = new IO[1]; + } + if (mInputs.length <= inputCount) { + IO t[] = new IO[mInputs.length + 1]; + System.arraycopy(mInputs, 0, t, 0, mInputs.length); + mInputs = t; + } + mInputs[inputCount++] = new IO(n.mScript); + } + + // Look for unattached kernel outputs + boolean hasOutput = false; + for (int ct=0; ct < n.mOutput.length; ct++) { + if (n.mOutput[ct] != null) { + hasOutput = true; + } + } + if (!hasOutput) { + if (mOutputs == null) { + mOutputs = new IO[1]; + } + if (mOutputs.length <= outputCount) { + IO t[] = new IO[mOutputs.length + 1]; + System.arraycopy(mOutputs, 0, t, 0, mOutputs.length); + mOutputs = t; + } + mOutputs[outputCount++] = new IO(n.mScript); + } + + // Make allocations for internal connections + // Since script outputs are unique, use those to avoid duplicates. + for (int ct=0; ct < n.mOutput.length; ct++) { + android.util.Log.v("RSR", "init out2 " + n.mOutput[ct]); + if (n.mOutput[ct] != null) { + Connection t = n.mOutput[ct]; + mConnections[connectionIndex++] = t; + t.mInternalAllocation = Allocation.createTyped(mRS, t.mAllocationType); + } + } + + n = n.mNext; + } + } + + public void setInput(Script s, Allocation a) { + for (int ct=0; ct < mInputs.length; ct++) { + if (mInputs[ct].mScript == s) { + mInputs[ct].mAllocation = a; + return; + } + } + throw new RSIllegalArgumentException("Script not found"); + } + + public void setOutput(Script s, Allocation a) { + for (int ct=0; ct < mOutputs.length; ct++) { + if (mOutputs[ct].mScript == s) { + mOutputs[ct].mAllocation = a; + return; + } + } + throw new RSIllegalArgumentException("Script not found"); + } + + public void execute() { + android.util.Log.v("RSR", "execute"); + boolean more = true; + int depth = 0; + while (more) { + more = false; + for (int ct=0; ct < mNodes.length; ct++) { + if (mNodes[ct].mDepth == depth) { + more = true; + + Allocation kernelIn = null; + for (int ct2=0; ct2 < mNodes[ct].mInputCount; ct2++) { + android.util.Log.v("RSR", " kin " + ct2 + ", to " + mNodes[ct].mInput[ct2].mTo[0] + ", name " + mNodes[ct].mInput[ct2].mToName[0]); + if (mNodes[ct].mInput[ct2].mToName[0] == null) { + kernelIn = mNodes[ct].mInput[ct2].mInternalAllocation; + break; + } + } + + Allocation kernelOut= null; + for (int ct2=0; ct2 < mNodes[ct].mOutputCount; ct2++) { + android.util.Log.v("RSR", " kout " + ct2 + ", from " + mNodes[ct].mOutput[ct2].mFrom); + if (mNodes[ct].mOutput[ct2].mFrom != null) { + kernelOut = mNodes[ct].mOutput[ct2].mInternalAllocation; + break; + } + } + if (kernelOut == null) { + for (int ct2=0; ct2 < mOutputs.length; ct2++) { + if (mOutputs[ct2].mScript == mNodes[ct].mScript) { + kernelOut = mOutputs[ct2].mAllocation; + break; + } + } + } + + android.util.Log.v("RSR", "execute calling " + mNodes[ct] + ", with " + kernelIn); + if (kernelIn != null) { + try { + + Method m = mNodes[ct].mScript.getClass().getMethod("forEach_root", + new Class[] { Allocation.class, Allocation.class }); + m.invoke(mNodes[ct].mScript, new Object[] {kernelIn, kernelOut} ); + } catch (Throwable t) { + android.util.Log.e("RSR", "execute error " + t); + } + } else { + try { + Method m = mNodes[ct].mScript.getClass().getMethod("forEach_root", + new Class[] { Allocation.class }); + m.invoke(mNodes[ct].mScript, new Object[] {kernelOut} ); + } catch (Throwable t) { + android.util.Log.e("RSR", "execute error " + t); + } + } + + } + } + depth ++; + } + + } + + + public static class Builder { + RenderScript mRS; + Node mFirstNode; + int mConnectionCount = 0; + int mNodeCount = 0; + + public Builder(RenderScript rs) { + mRS = rs; + } + + private void validateRecurse(Node n, int depth) { + n.mSeen = true; + if (depth > n.mDepth) { + n.mDepth = depth; + } + + android.util.Log.v("RSR", " validateRecurse outputCount " + n.mOutputCount); + for (int ct=0; ct < n.mOutputCount; ct++) { + for (int ct2=0; ct2 < n.mOutput[ct].mTo.length; ct2++) { + if (n.mOutput[ct].mTo[ct2].mSeen) { + throw new RSInvalidStateException("Loops in group not allowed."); + } + validateRecurse(n.mOutput[ct].mTo[ct2], depth + 1); + } + } + } + + private void validate() { + android.util.Log.v("RSR", "validate"); + Node n = mFirstNode; + while (n != null) { + n.mSeen = false; + n.mDepth = 0; + n = n.mNext; + } + + n = mFirstNode; + while (n != null) { + android.util.Log.v("RSR", "validate n= " + n); + if ((n.mSeen == false) && (n.mInputCount == 0)) { + android.util.Log.v("RSR", " recursing " + n); + validateRecurse(n, 0); + } + n = n.mNext; + } + } + + private Node findScript(Script s) { + Node n = mFirstNode; + while (n != null) { + if (n.mScript == s) { + return n; + } + n = n.mNext; + } + return null; + } + + private void addNode(Node n) { + n.mNext = mFirstNode; + mFirstNode = n; + } + + public Builder addConnection(Type t, Script output, Script input, String inputName) { + android.util.Log.v("RSR", "addConnection " + t +", " + output + ", " + input); + + // Look for existing output + Node nout = findScript(output); + Connection c; + if (nout == null) { + // Make new node + android.util.Log.v("RSR", "addConnection new output node"); + nout = new Node(output); + mNodeCount++; + c = new Connection(nout, t); + mConnectionCount++; + nout.addOutput(c); + addNode(nout); + } else { + // Add to existing node + android.util.Log.v("RSR", "addConnection reuse output node"); + if (nout.mOutput[0] != null) { + if (nout.mOutput[0].mFrom.mScript != output) { + throw new RSInvalidStateException("Changed output of existing node"); + } + if (nout.mOutput[0].mAllocationType != t) { + throw new RSInvalidStateException("Changed output type of existing node"); + } + } + c = nout.mOutput[0]; + } + // At this point we should have a connection attached to a script ouput. + + // Find input + Node nin = findScript(input); + if (nin == null) { + android.util.Log.v("RSR", "addConnection new input node"); + nin = new Node(input); + mNodeCount++; + addNode(nin); + } + c.addTo(nin, inputName); + nin.addInput(c); + + validate(); + return this; + } + + public ScriptGroup create() { + ScriptGroup sg = new ScriptGroup(0, mRS); + sg.mFirstNode = mFirstNode; + mFirstNode = null; + + android.util.Log.v("RSR", "create nodes= " + mNodeCount + ", Connections= " + mConnectionCount); + + sg.init(mNodeCount, mConnectionCount); + return sg; + } + + } + + +} + + diff --git a/graphics/java/android/renderscript/ScriptIntrinsic.java b/graphics/java/android/renderscript/ScriptIntrinsic.java new file mode 100644 index 0000000..f275fee --- /dev/null +++ b/graphics/java/android/renderscript/ScriptIntrinsic.java @@ -0,0 +1,31 @@ +/* + * 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 android.content.Context; +import android.content.res.Resources; +import android.util.Log; + + +/** + * @hide + **/ +public class ScriptIntrinsic extends Script { + ScriptIntrinsic(int id, RenderScript rs) { + super(id, rs); + } +} diff --git a/graphics/java/android/renderscript/ScriptIntrinsicColorMatrix.java b/graphics/java/android/renderscript/ScriptIntrinsicColorMatrix.java new file mode 100644 index 0000000..41e7e00 --- /dev/null +++ b/graphics/java/android/renderscript/ScriptIntrinsicColorMatrix.java @@ -0,0 +1,68 @@ +/* + * Copyright (C) 2012 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 android.content.Context; +import android.content.res.Resources; +import android.util.Log; + +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.util.Map.Entry; +import java.util.HashMap; + + +/** + * @hide + **/ +public class ScriptIntrinsicColorMatrix extends ScriptIntrinsic { + private Matrix4f mMatrix = new Matrix4f(); + private Allocation mInput; + + ScriptIntrinsicColorMatrix(int id, RenderScript rs) { + super(id, rs); + } + + /** + * Supported elements types are float, float4, uchar, uchar4 + * + * + * @param rs + * @param e + * + * @return ScriptIntrinsicColorMatrix + */ + public static ScriptIntrinsicColorMatrix create(RenderScript rs, Element e) { + int id = rs.nScriptIntrinsicCreate(2, e.getID(rs)); + return new ScriptIntrinsicColorMatrix(id, rs); + + } + + public void setColorMatrix(Matrix4f m) { + mMatrix.load(m); + FieldPacker fp = new FieldPacker(16*4); + fp.addMatrix(m); + setVar(0, fp); + } + + public void forEach(Allocation ain, Allocation aout) { + forEach(0, ain, aout, null); + } + +} + diff --git a/graphics/java/android/renderscript/ScriptIntrinsicConvolve3x3.java b/graphics/java/android/renderscript/ScriptIntrinsicConvolve3x3.java new file mode 100644 index 0000000..c7465a7 --- /dev/null +++ b/graphics/java/android/renderscript/ScriptIntrinsicConvolve3x3.java @@ -0,0 +1,75 @@ +/* + * Copyright (C) 2012 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 android.content.Context; +import android.content.res.Resources; +import android.util.Log; + +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.util.Map.Entry; +import java.util.HashMap; + + +/** + * @hide + **/ +public class ScriptIntrinsicConvolve3x3 extends ScriptIntrinsic { + private float[] mValues = new float[9]; + private Allocation mInput; + + ScriptIntrinsicConvolve3x3(int id, RenderScript rs) { + super(id, rs); + } + + /** + * Supported elements types are float, float4, uchar, uchar4 + * + * + * @param rs + * @param e + * + * @return ScriptIntrinsicConvolve3x3 + */ + public static ScriptIntrinsicConvolve3x3 create(RenderScript rs, Element e) { + int id = rs.nScriptIntrinsicCreate(1, e.getID(rs)); + return new ScriptIntrinsicConvolve3x3(id, rs); + + } + + public void setInput(Allocation ain) { + mInput = ain; + bindAllocation(ain, 1); + } + + public void setColorMatrix(float v[]) { + FieldPacker fp = new FieldPacker(9*4); + for (int ct=0; ct < mValues.length; ct++) { + mValues[ct] = v[ct]; + fp.addF32(mValues[ct]); + } + setVar(0, fp); + } + + public void forEach(Allocation aout) { + forEach(0, null, aout, null); + } + +} + diff --git a/graphics/java/android/renderscript/ScriptIntrinsicYuvToRGB.java b/graphics/java/android/renderscript/ScriptIntrinsicYuvToRGB.java new file mode 100644 index 0000000..ee5f938 --- /dev/null +++ b/graphics/java/android/renderscript/ScriptIntrinsicYuvToRGB.java @@ -0,0 +1,64 @@ +/* + * 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 android.content.Context; +import android.content.res.Resources; +import android.util.Log; + +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.util.Map.Entry; +import java.util.HashMap; + +import java.lang.reflect.Field; +import java.lang.reflect.Modifier; + +/** + * @hide + **/ +public class ScriptIntrinsicYuvToRGB extends ScriptIntrinsic { + ScriptIntrinsicYuvToRGB(int id, RenderScript rs) { + super(id, rs); + } + + + + public static class Builder { + RenderScript mRS; + + public Builder(RenderScript rs) { + mRS = rs; + } + + public void setInputFormat(int inputFormat) { + + } + + public void setOutputFormat(Element e) { + + } + + public ScriptIntrinsicYuvToRGB create() { + return null; + + } + + } + +} |
