Home | History | Annotate | Download | only in renderscript
      1 /*
      2  * Copyright (C) 2008 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package android.renderscript;
     18 
     19 import android.content.res.Resources;
     20 import android.graphics.Bitmap;
     21 import android.graphics.BitmapFactory;
     22 import android.util.TypedValue;
     23 
     24 /**
     25  *
     26  **/
     27 public class AllocationAdapter extends Allocation {
     28     AllocationAdapter(int id, RenderScript rs, Allocation alloc) {
     29         super(id, rs, alloc.mType, alloc.mUsage);
     30         mAdaptedAllocation = alloc;
     31     }
     32 
     33     int getID(RenderScript rs) {
     34         throw new RSInvalidStateException(
     35             "This operation is not supported with adapters at this time.");
     36     }
     37 
     38     /**
     39      * @hide
     40      */
     41     public void subData(int xoff, FieldPacker fp) {
     42         super.setFromFieldPacker(xoff, fp);
     43     }
     44     /**
     45      * @hide
     46      */
     47     public void subElementData(int xoff, int component_number, FieldPacker fp) {
     48         super.setFromFieldPacker(xoff, component_number, fp);
     49     }
     50     /**
     51      * @hide
     52      */
     53     public void subData1D(int off, int count, int[] d) {
     54         super.copy1DRangeFrom(off, count, d);
     55     }
     56     /**
     57      * @hide
     58      */
     59     public void subData1D(int off, int count, short[] d) {
     60         super.copy1DRangeFrom(off, count, d);
     61     }
     62     /**
     63      * @hide
     64      */
     65     public void subData1D(int off, int count, byte[] d) {
     66         super.copy1DRangeFrom(off, count, d);
     67     }
     68     /**
     69      * @hide
     70      */
     71     public void subData1D(int off, int count, float[] d) {
     72         super.copy1DRangeFrom(off, count, d);
     73     }
     74     /**
     75      * @hide
     76      */
     77     public void subData2D(int xoff, int yoff, int w, int h, int[] d) {
     78         super.copy2DRangeFrom(xoff, yoff, w, h, d);
     79     }
     80     /**
     81      * @hide
     82      */
     83     public void subData2D(int xoff, int yoff, int w, int h, float[] d) {
     84         super.copy2DRangeFrom(xoff, yoff, w, h, d);
     85     }
     86     /**
     87      * @hide
     88      */
     89     public void readData(int[] d) {
     90         super.copyTo(d);
     91     }
     92     /**
     93      * @hide
     94      */
     95     public void readData(float[] d) {
     96         super.copyTo(d);
     97     }
     98 
     99     void initLOD(int lod) {
    100         if (lod < 0) {
    101             throw new RSIllegalArgumentException("Attempting to set negative lod (" + lod + ").");
    102         }
    103 
    104         int tx = mAdaptedAllocation.mType.getX();
    105         int ty = mAdaptedAllocation.mType.getY();
    106         int tz = mAdaptedAllocation.mType.getZ();
    107 
    108         for (int ct=0; ct < lod; ct++) {
    109             if ((tx==1) && (ty == 1) && (tz == 1)) {
    110                 throw new RSIllegalArgumentException("Attempting to set lod (" + lod + ") out of range.");
    111             }
    112 
    113             if (tx > 1) tx >>= 1;
    114             if (ty > 1) ty >>= 1;
    115             if (tz > 1) tz >>= 1;
    116         }
    117 
    118         mCurrentDimX = tx;
    119         mCurrentDimY = ty;
    120         mCurrentDimZ = tz;
    121         mCurrentCount = mCurrentDimX;
    122         if (mCurrentDimY > 1) {
    123             mCurrentCount *= mCurrentDimY;
    124         }
    125         if (mCurrentDimZ > 1) {
    126             mCurrentCount *= mCurrentDimZ;
    127         }
    128         mSelectedY = 0;
    129         mSelectedZ = 0;
    130     }
    131 
    132     /**
    133      * Set the active LOD.  The LOD must be within the range for the
    134      * type being adapted.  The base allocation must have mipmaps.
    135      *
    136      * Because this changes the dimensions of the adapter the
    137      * current Y and Z will be reset.
    138      *
    139      * @param lod The LOD to make active.
    140      */
    141     public void setLOD(int lod) {
    142         if (!mAdaptedAllocation.getType().hasMipmaps()) {
    143             throw new RSInvalidStateException("Cannot set LOD when the allocation type does not include mipmaps.");
    144         }
    145         if (!mConstrainedLOD) {
    146             throw new RSInvalidStateException("Cannot set LOD when the adapter includes mipmaps.");
    147         }
    148 
    149         initLOD(lod);
    150     }
    151 
    152     /**
    153      * Set the active Face.  The base allocation must be of a type
    154      * that includes faces.
    155      *
    156      * @param cf The face to make active.
    157      */
    158     public void setFace(Type.CubemapFace cf) {
    159         if (!mAdaptedAllocation.getType().hasFaces()) {
    160             throw new RSInvalidStateException("Cannot set Face when the allocation type does not include faces.");
    161         }
    162         if (!mConstrainedFace) {
    163             throw new RSInvalidStateException("Cannot set LOD when the adapter includes mipmaps.");
    164         }
    165         if (cf == null) {
    166             throw new RSIllegalArgumentException("Cannot set null face.");
    167         }
    168 
    169         mSelectedFace = cf;
    170     }
    171 
    172     /**
    173      * Set the active Y.  The y value must be within the range for
    174      * the allocation being adapted.  The base allocation must
    175      * contain the Y dimension.
    176      *
    177      * @param y The y to make active.
    178      */
    179     public void setY(int y) {
    180         if (mAdaptedAllocation.getType().getY() == 0) {
    181             throw new RSInvalidStateException("Cannot set Y when the allocation type does not include Y dim.");
    182         }
    183         if (mAdaptedAllocation.getType().getY() <= y) {
    184             throw new RSInvalidStateException("Cannot set Y greater than dimension of allocation.");
    185         }
    186         if (!mConstrainedY) {
    187             throw new RSInvalidStateException("Cannot set Y when the adapter includes Y.");
    188         }
    189 
    190         mSelectedY = y;
    191     }
    192 
    193     /**
    194      * Set the active Z.  The z value must be within the range for
    195      * the allocation being adapted.  The base allocation must
    196      * contain the Z dimension.
    197      *
    198      * @param z The z to make active.
    199      */
    200     public void setZ(int z) {
    201         if (mAdaptedAllocation.getType().getZ() == 0) {
    202             throw new RSInvalidStateException("Cannot set Z when the allocation type does not include Z dim.");
    203         }
    204         if (mAdaptedAllocation.getType().getZ() <= z) {
    205             throw new RSInvalidStateException("Cannot set Z greater than dimension of allocation.");
    206         }
    207         if (!mConstrainedZ) {
    208             throw new RSInvalidStateException("Cannot set Z when the adapter includes Z.");
    209         }
    210 
    211         mSelectedZ = z;
    212     }
    213 
    214     static public AllocationAdapter create1D(RenderScript rs, Allocation a) {
    215         rs.validate();
    216         AllocationAdapter aa = new AllocationAdapter(0, rs, a);
    217         aa.mConstrainedLOD = true;
    218         aa.mConstrainedFace = true;
    219         aa.mConstrainedY = true;
    220         aa.mConstrainedZ = true;
    221         aa.initLOD(0);
    222         return aa;
    223     }
    224 
    225     static public AllocationAdapter create2D(RenderScript rs, Allocation a) {
    226         android.util.Log.e("rs", "create2d " + a);
    227         rs.validate();
    228         AllocationAdapter aa = new AllocationAdapter(0, rs, a);
    229         aa.mConstrainedLOD = true;
    230         aa.mConstrainedFace = true;
    231         aa.mConstrainedY = false;
    232         aa.mConstrainedZ = true;
    233         aa.initLOD(0);
    234         return aa;
    235     }
    236 
    237 
    238     /**
    239      * Override the Allocation resize.  Resizing adapters is not
    240      * allowed and will throw a RSInvalidStateException.
    241      *
    242      * @param dimX ignored.
    243      */
    244     public synchronized void resize(int dimX) {
    245         throw new RSInvalidStateException("Resize not allowed for Adapters.");
    246     }
    247 
    248 }
    249 
    250 
    251