OSDN Git Service

Merge "Clean up USAGE_SHARED and add syncing for USAGE_SHARED." into jb-mr2-dev
authorTim Murray <timmurray@google.com>
Thu, 11 Apr 2013 04:50:31 +0000 (04:50 +0000)
committerAndroid (Google) Code Review <android-gerrit@google.com>
Thu, 11 Apr 2013 04:50:32 +0000 (04:50 +0000)
1  2 
graphics/java/android/renderscript/Allocation.java

@@@ -370,11 -370,20 +370,20 @@@ public class Allocation extends BaseOb
       */
      public void syncAll(int srcLocation) {
          switch (srcLocation) {
+         case USAGE_GRAPHICS_TEXTURE:
          case USAGE_SCRIPT:
+             if ((mUsage & USAGE_SHARED) != 0) {
+                 copyFrom(mBitmap);
+             }
+             break;
          case USAGE_GRAPHICS_CONSTANTS:
-         case USAGE_GRAPHICS_TEXTURE:
          case USAGE_GRAPHICS_VERTEX:
              break;
+         case USAGE_SHARED:
+             if ((mUsage & USAGE_SHARED) != 0) {
+                 copyTo(mBitmap);
+             }
+             break;
          default:
              throw new RSIllegalArgumentException("Source must be exactly one usage type.");
          }
       */
      public void copyFromUnchecked(int[] d) {
          mRS.validate();
 -        if (mCurrentDimY > 0) {
 +        if (mCurrentDimZ > 0) {
 +            copy3DRangeFromUnchecked(0, 0, 0, mCurrentDimX, mCurrentDimY, mCurrentDimZ, d);
 +        } else if (mCurrentDimY > 0) {
              copy2DRangeFromUnchecked(0, 0, mCurrentDimX, mCurrentDimY, d);
          } else {
              copy1DRangeFromUnchecked(0, mCurrentCount, d);
       */
      public void copyFromUnchecked(short[] d) {
          mRS.validate();
 -        if (mCurrentDimY > 0) {
 +        if (mCurrentDimZ > 0) {
 +            copy3DRangeFromUnchecked(0, 0, 0, mCurrentDimX, mCurrentDimY, mCurrentDimZ, d);
 +        } else if (mCurrentDimY > 0) {
              copy2DRangeFromUnchecked(0, 0, mCurrentDimX, mCurrentDimY, d);
          } else {
              copy1DRangeFromUnchecked(0, mCurrentCount, d);
       */
      public void copyFromUnchecked(byte[] d) {
          mRS.validate();
 -        if (mCurrentDimY > 0) {
 +        if (mCurrentDimZ > 0) {
 +            copy3DRangeFromUnchecked(0, 0, 0, mCurrentDimX, mCurrentDimY, mCurrentDimZ, d);
 +        } else if (mCurrentDimY > 0) {
              copy2DRangeFromUnchecked(0, 0, mCurrentDimX, mCurrentDimY, d);
          } else {
              copy1DRangeFromUnchecked(0, mCurrentCount, d);
       */
      public void copyFromUnchecked(float[] d) {
          mRS.validate();
 -        if (mCurrentDimY > 0) {
 +        if (mCurrentDimZ > 0) {
 +            copy3DRangeFromUnchecked(0, 0, 0, mCurrentDimX, mCurrentDimY, mCurrentDimZ, d);
 +        } else if (mCurrentDimY > 0) {
              copy2DRangeFromUnchecked(0, 0, mCurrentDimX, mCurrentDimY, d);
          } else {
              copy1DRangeFromUnchecked(0, mCurrentCount, d);
       */
      public void copyFrom(int[] d) {
          mRS.validate();
 -        if (mCurrentDimY > 0) {
 +        if (mCurrentDimZ > 0) {
 +            copy3DRangeFrom(0, 0, 0, mCurrentDimX, mCurrentDimY, mCurrentDimZ, d);
 +        } else if (mCurrentDimY > 0) {
              copy2DRangeFrom(0, 0, mCurrentDimX, mCurrentDimY, d);
          } else {
              copy1DRangeFrom(0, mCurrentCount, d);
       */
      public void copyFrom(short[] d) {
          mRS.validate();
 -        if (mCurrentDimY > 0) {
 +        if (mCurrentDimZ > 0) {
 +            copy3DRangeFrom(0, 0, 0, mCurrentDimX, mCurrentDimY, mCurrentDimZ, d);
 +        } else if (mCurrentDimY > 0) {
              copy2DRangeFrom(0, 0, mCurrentDimX, mCurrentDimY, d);
          } else {
              copy1DRangeFrom(0, mCurrentCount, d);
       */
      public void copyFrom(byte[] d) {
          mRS.validate();
 -        if (mCurrentDimY > 0) {
 +        if (mCurrentDimZ > 0) {
 +            copy3DRangeFrom(0, 0, 0, mCurrentDimX, mCurrentDimY, mCurrentDimZ, d);
 +        } else if (mCurrentDimY > 0) {
              copy2DRangeFrom(0, 0, mCurrentDimX, mCurrentDimY, d);
          } else {
              copy1DRangeFrom(0, mCurrentCount, d);
       */
      public void copyFrom(float[] d) {
          mRS.validate();
 -        if (mCurrentDimY > 0) {
 +        if (mCurrentDimZ > 0) {
 +            copy3DRangeFrom(0, 0, 0, mCurrentDimX, mCurrentDimY, mCurrentDimZ, d);
 +        } else if (mCurrentDimY > 0) {
              copy2DRangeFrom(0, 0, mCurrentDimX, mCurrentDimY, d);
          } else {
              copy1DRangeFrom(0, mCurrentCount, d);
              Canvas c = new Canvas(newBitmap);
              c.drawBitmap(data, 0, 0, null);
              copy2DRangeFrom(xoff, yoff, newBitmap);
 +            return;
          }
          validateBitmapFormat(data);
          validate2DRange(xoff, yoff, data.getWidth(), data.getHeight());
          mRS.nAllocationData2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID, data);
      }
  
 +    private void validate3DRange(int xoff, int yoff, int zoff, int w, int h, int d) {
 +        if (mAdaptedAllocation != null) {
 +
 +        } else {
 +
 +            if (xoff < 0 || yoff < 0 || zoff < 0) {
 +                throw new RSIllegalArgumentException("Offset cannot be negative.");
 +            }
 +            if (h < 0 || w < 0 || d < 0) {
 +                throw new RSIllegalArgumentException("Height or width cannot be negative.");
 +            }
 +            if (((xoff + w) > mCurrentDimX) || ((yoff + h) > mCurrentDimY) || ((zoff + d) > mCurrentDimZ)) {
 +                throw new RSIllegalArgumentException("Updated region larger than allocation.");
 +            }
 +        }
 +    }
 +
 +    /**
 +     * @hide
 +     *
 +     */
 +    void copy3DRangeFromUnchecked(int xoff, int yoff, int zoff, int w, int h, int d, byte[] data) {
 +        mRS.validate();
 +        validate3DRange(xoff, yoff, zoff, w, h, d);
 +        mRS.nAllocationData3D(getIDSafe(), xoff, yoff, zoff, mSelectedLOD,
 +                              w, h, d, data, data.length);
 +    }
 +
 +    /**
 +     * @hide
 +     *
 +     */
 +    void copy3DRangeFromUnchecked(int xoff, int yoff, int zoff, int w, int h, int d, short[] data) {
 +        mRS.validate();
 +        validate3DRange(xoff, yoff, zoff, w, h, d);
 +        mRS.nAllocationData3D(getIDSafe(), xoff, yoff, zoff, mSelectedLOD,
 +                              w, h, d, data, data.length * 2);
 +    }
 +
 +    /**
 +     * @hide
 +     *
 +     */
 +    void copy3DRangeFromUnchecked(int xoff, int yoff, int zoff, int w, int h, int d, int[] data) {
 +        mRS.validate();
 +        validate3DRange(xoff, yoff, zoff, w, h, d);
 +        mRS.nAllocationData3D(getIDSafe(), xoff, yoff, zoff, mSelectedLOD,
 +                              w, h, d, data, data.length * 4);
 +    }
 +
 +    /**
 +     * @hide
 +     *
 +     */
 +    void copy3DRangeFromUnchecked(int xoff, int yoff, int zoff, int w, int h, int d, float[] data) {
 +        mRS.validate();
 +        validate3DRange(xoff, yoff, zoff, w, h, d);
 +        mRS.nAllocationData3D(getIDSafe(), xoff, yoff, zoff, mSelectedLOD,
 +                              w, h, d, data, data.length * 4);
 +    }
 +
 +
 +    /**
 +     * @hide
 +     * Copy a rectangular region from the array into the allocation.
 +     * The incoming array is assumed to be tightly packed.
 +     *
 +     * @param xoff X offset of the region to update
 +     * @param yoff Y offset of the region to update
 +     * @param zoff Z offset of the region to update
 +     * @param w Width of the incoming region to update
 +     * @param h Height of the incoming region to update
 +     * @param d Depth of the incoming region to update
 +     * @param data to be placed into the allocation
 +     */
 +    public void copy3DRangeFrom(int xoff, int yoff, int zoff, int w, int h, int d, byte[] data) {
 +        validateIsInt8();
 +        copy3DRangeFromUnchecked(xoff, yoff, zoff, w, h, d, data);
 +    }
 +
 +    /**
 +     * @hide
 +     *
 +     */
 +    public void copy3DRangeFrom(int xoff, int yoff, int zoff, int w, int h, int d, short[] data) {
 +        validateIsInt16();
 +        copy3DRangeFromUnchecked(xoff, yoff, zoff, w, h, d, data);
 +    }
 +
 +    /**
 +     * @hide
 +     *
 +     */
 +    public void copy3DRangeFrom(int xoff, int yoff, int zoff, int w, int h, int d, int[] data) {
 +        validateIsInt32();
 +        copy3DRangeFromUnchecked(xoff, yoff, zoff, w, h, d, data);
 +    }
 +
 +    /**
 +     * @hide
 +     *
 +     */
 +    public void copy3DRangeFrom(int xoff, int yoff, int zoff, int w, int h, int d, float[] data) {
 +        validateIsFloat32();
 +        copy3DRangeFromUnchecked(xoff, yoff, zoff, w, h, d, data);
 +    }
 +
 +    /**
 +     * @hide
 +     * Copy a rectangular region into the allocation from another
 +     * allocation.
 +     *
 +     * @param xoff X offset of the region to update.
 +     * @param yoff Y offset of the region to update.
 +     * @param w Width of the incoming region to update.
 +     * @param h Height of the incoming region to update.
 +     * @param d Depth of the incoming region to update.
 +     * @param data source allocation.
 +     * @param dataXoff X offset in data of the region to update.
 +     * @param dataYoff Y offset in data of the region to update.
 +     * @param dataZoff Z offset in data of the region to update
 +     */
 +    public void copy3DRangeFrom(int xoff, int yoff, int zoff, int w, int h, int d,
 +                                Allocation data, int dataXoff, int dataYoff, int dataZoff) {
 +        mRS.validate();
 +        validate3DRange(xoff, yoff, zoff, w, h, d);
 +        mRS.nAllocationData3D(getIDSafe(), xoff, yoff, zoff, mSelectedLOD,
 +                              w, h, d, data.getID(mRS), dataXoff, dataYoff, dataZoff,
 +                              data.mSelectedLOD);
 +    }
 +
  
      /**
       * Copy from the Allocation into a Bitmap.  The bitmap must
       * A new type will be created with the new dimension.
       *
       * @param dimX The new size of the allocation.
 +     *
 +     * @deprecated Renderscript objects should be immutable once
 +     * created.  The replacement is to create a new allocation and copy the
 +     * contents.
       */
      public synchronized void resize(int dimX) {
          if ((mType.getY() > 0)|| (mType.getZ() > 0) || mType.hasFaces() || mType.hasMipmaps()) {
          updateCacheInfo(mType);
      }
  
 -    /**
 -     * Resize a 2D allocation.  The contents of the allocation are
 -     * preserved.  If new elements are allocated objects are created
 -     * with null contents and the new region is otherwise undefined.
 -     *
 -     * If the new region is smaller the references of any objects
 -     * outside the new region will be released.
 -     *
 -     * A new type will be created with the new dimension.
 -     *
 -     * @param dimX The new size of the allocation.
 -     * @param dimY The new size of the allocation.
 -     */
 -    public synchronized void resize(int dimX, int dimY) {
 -        if ((mType.getZ() > 0) || mType.hasFaces() || mType.hasMipmaps()) {
 -            throw new RSInvalidStateException(
 -                "Resize only support for 2D allocations at this time.");
 -        }
 -        if (mType.getY() == 0) {
 -            throw new RSInvalidStateException(
 -                "Resize only support for 2D allocations at this time.");
 -        }
 -        mRS.nAllocationResize2D(getID(mRS), dimX, dimY);
 -        mRS.finish();  // Necessary because resize is fifoed and update is async.
 -
 -        int typeID = mRS.nAllocationGetType(getID(mRS));
 -        mType = new Type(typeID, mRS);
 -        mType.updateFromNative();
 -        updateCacheInfo(mType);
 -    }
 -
 -
  
      // creation
  
          // enable optimized bitmap path only with no mipmap and script-only usage
          if (mips == MipmapControl.MIPMAP_NONE &&
              t.getElement().isCompatible(Element.RGBA_8888(rs)) &&
-             usage == (USAGE_SHARED | USAGE_SCRIPT)) {
+             usage == (USAGE_SHARED | USAGE_SCRIPT | USAGE_GRAPHICS_TEXTURE)) {
              int id = rs.nAllocationCreateBitmapBackedAllocation(t.getID(rs), mips.mID, b, usage);
              if (id == 0) {
                  throw new RSRuntimeException("Load failed.");
      static public Allocation createFromBitmap(RenderScript rs, Bitmap b) {
          if (rs.getApplicationContext().getApplicationInfo().targetSdkVersion >= 18) {
              return createFromBitmap(rs, b, MipmapControl.MIPMAP_NONE,
-                                     USAGE_SHARED | USAGE_SCRIPT);
+                                     USAGE_SHARED | USAGE_SCRIPT | USAGE_GRAPHICS_TEXTURE);
          }
          return createFromBitmap(rs, b, MipmapControl.MIPMAP_NONE,
                                  USAGE_GRAPHICS_TEXTURE);
          if (rs.getApplicationContext().getApplicationInfo().targetSdkVersion >= 18) {
              return createFromBitmapResource(rs, res, id,
                                              MipmapControl.MIPMAP_NONE,
-                                             USAGE_SHARED | USAGE_SCRIPT);
+                                             USAGE_SHARED | USAGE_SCRIPT | USAGE_GRAPHICS_TEXTURE);
          }
          return createFromBitmapResource(rs, res, id,
                                          MipmapControl.MIPMAP_NONE,