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 java.lang.reflect.Field;
     20 
     21 import android.content.Context;
     22 import android.graphics.PixelFormat;
     23 import android.graphics.Bitmap;
     24 import android.graphics.BitmapFactory;
     25 import android.graphics.SurfaceTexture;
     26 import android.util.Log;
     27 import android.view.Surface;
     28 import android.view.SurfaceHolder;
     29 import android.view.SurfaceView;
     30 
     31 /**
     32  * The Graphics derivitive of RenderScript.  Extends the basic context to add a
     33  * root script which is the display window for graphical output.  When the
     34  * system needs to update the display the currently bound root script will be
     35  * called.  This script is expected to issue the rendering commands to repaint
     36  * the screen.
     37  **/
     38 public class RenderScriptGL extends RenderScript {
     39     int mWidth;
     40     int mHeight;
     41 
     42     /**
     43      * Class which is used to describe a pixel format for a graphical buffer.
     44      * This is used to describe the intended format of the display surface.
     45      *
     46      * The configuration is described by pairs of minimum and preferred bit
     47      * depths for each component within the config and additional structural
     48      * information.
     49      */
     50     public static class SurfaceConfig {
     51         int mDepthMin       = 0;
     52         int mDepthPref      = 0;
     53         int mStencilMin     = 0;
     54         int mStencilPref    = 0;
     55         int mColorMin       = 8;
     56         int mColorPref      = 8;
     57         int mAlphaMin       = 0;
     58         int mAlphaPref      = 0;
     59         int mSamplesMin     = 1;
     60         int mSamplesPref    = 1;
     61         float mSamplesQ     = 1.f;
     62 
     63         public SurfaceConfig() {
     64         }
     65 
     66         public SurfaceConfig(SurfaceConfig sc) {
     67             mDepthMin = sc.mDepthMin;
     68             mDepthPref = sc.mDepthPref;
     69             mStencilMin = sc.mStencilMin;
     70             mStencilPref = sc.mStencilPref;
     71             mColorMin = sc.mColorMin;
     72             mColorPref = sc.mColorPref;
     73             mAlphaMin = sc.mAlphaMin;
     74             mAlphaPref = sc.mAlphaPref;
     75             mSamplesMin = sc.mSamplesMin;
     76             mSamplesPref = sc.mSamplesPref;
     77             mSamplesQ = sc.mSamplesQ;
     78         }
     79 
     80         private void validateRange(int umin, int upref, int rmin, int rmax) {
     81             if (umin < rmin || umin > rmax) {
     82                 throw new RSIllegalArgumentException("Minimum value provided out of range.");
     83             }
     84             if (upref < umin) {
     85                 throw new RSIllegalArgumentException("preferred must be >= Minimum.");
     86             }
     87         }
     88 
     89         /**
     90          * Set the per-component bit depth for color (red, green, blue).  This
     91          * configures the surface for an unsigned integer buffer type.
     92          *
     93          * @param minimum
     94          * @param preferred
     95          */
     96         public void setColor(int minimum, int preferred) {
     97             validateRange(minimum, preferred, 5, 8);
     98             mColorMin = minimum;
     99             mColorPref = preferred;
    100         }
    101 
    102         /**
    103          * Set the bit depth for alpha. This configures the surface for
    104          * an unsigned integer buffer type.
    105          *
    106          * @param minimum
    107          * @param preferred
    108          */
    109         public void setAlpha(int minimum, int preferred) {
    110             validateRange(minimum, preferred, 0, 8);
    111             mAlphaMin = minimum;
    112             mAlphaPref = preferred;
    113         }
    114 
    115          /**
    116          * Set the bit depth for the depth buffer. This configures the
    117          * surface for an unsigned integer buffer type.  If a minimum of 0
    118          * is specified then its possible no depth buffer will be
    119          * allocated.
    120          *
    121          * @param minimum
    122          * @param preferred
    123          */
    124         public void setDepth(int minimum, int preferred) {
    125             validateRange(minimum, preferred, 0, 24);
    126             mDepthMin = minimum;
    127             mDepthPref = preferred;
    128         }
    129 
    130         /**
    131          * Configure the multisample rendering.
    132          *
    133          * @param minimum The required number of samples, must be at least 1.
    134          * @param preferred The targe number of samples, must be at least
    135          *                  minimum
    136          * @param Q  The quality of samples, range 0-1.  Used to decide between
    137          *           different formats which have the same number of samples but
    138          *           different rendering quality.
    139          */
    140         public void setSamples(int minimum, int preferred, float Q) {
    141             validateRange(minimum, preferred, 1, 32);
    142             if (Q < 0.0f || Q > 1.0f) {
    143                 throw new RSIllegalArgumentException("Quality out of 0-1 range.");
    144             }
    145             mSamplesMin = minimum;
    146             mSamplesPref = preferred;
    147             mSamplesQ = Q;
    148         }
    149     };
    150 
    151     SurfaceConfig mSurfaceConfig;
    152 
    153     /**
    154      * Construct a new RenderScriptGL context.
    155      *
    156      * @param ctx The context.
    157      * @param sc The desired format of the primary rendering surface.
    158      */
    159     public RenderScriptGL(Context ctx, SurfaceConfig sc) {
    160         super(ctx);
    161         mSurfaceConfig = new SurfaceConfig(sc);
    162 
    163         int sdkVersion = getTargetSdkVersion(ctx);
    164 
    165         mWidth = 0;
    166         mHeight = 0;
    167         mDev = nDeviceCreate();
    168         int dpi = ctx.getResources().getDisplayMetrics().densityDpi;
    169         mContext = nContextCreateGL(mDev, 0, sdkVersion,
    170                                     mSurfaceConfig.mColorMin, mSurfaceConfig.mColorPref,
    171                                     mSurfaceConfig.mAlphaMin, mSurfaceConfig.mAlphaPref,
    172                                     mSurfaceConfig.mDepthMin, mSurfaceConfig.mDepthPref,
    173                                     mSurfaceConfig.mStencilMin, mSurfaceConfig.mStencilPref,
    174                                     mSurfaceConfig.mSamplesMin, mSurfaceConfig.mSamplesPref,
    175                                     mSurfaceConfig.mSamplesQ, dpi);
    176         if (mContext == 0) {
    177             throw new RSDriverException("Failed to create RS context.");
    178         }
    179         mMessageThread = new MessageThread(this);
    180         mMessageThread.start();
    181     }
    182 
    183     /**
    184      * Bind an os surface
    185      *
    186      *
    187      * @param w
    188      * @param h
    189      * @param sur
    190      */
    191     public void setSurface(SurfaceHolder sur, int w, int h) {
    192         validate();
    193         Surface s = null;
    194         if (sur != null) {
    195             s = sur.getSurface();
    196         }
    197         mWidth = w;
    198         mHeight = h;
    199         nContextSetSurface(w, h, s);
    200     }
    201 
    202     /**
    203      * Bind an os surface
    204      *
    205      * @param w
    206      * @param h
    207      * @param sur
    208      */
    209     public void setSurfaceTexture(SurfaceTexture sur, int w, int h) {
    210         validate();
    211         //android.util.Log.v("rs", "set surface " + sur + " w=" + w + ", h=" + h);
    212 
    213         mWidth = w;
    214         mHeight = h;
    215         nContextSetSurfaceTexture(w, h, sur);
    216     }
    217 
    218     /**
    219      * return the height of the last set surface.
    220      *
    221      * @return int
    222      */
    223     public int getHeight() {
    224         return mHeight;
    225     }
    226 
    227     /**
    228      * return the width of the last set surface.
    229      *
    230      * @return int
    231      */
    232     public int getWidth() {
    233         return mWidth;
    234     }
    235 
    236     /**
    237      * Temporarly halt calls to the root rendering script.
    238      *
    239      */
    240     public void pause() {
    241         validate();
    242         nContextPause();
    243     }
    244 
    245     /**
    246      * Resume calls to the root rendering script.
    247      *
    248      */
    249     public void resume() {
    250         validate();
    251         nContextResume();
    252     }
    253 
    254 
    255     /**
    256      * Set the script to handle calls to render the primary surface.
    257      *
    258      * @param s Graphics script to process rendering requests.
    259      */
    260     public void bindRootScript(Script s) {
    261         validate();
    262         nContextBindRootScript(safeID(s));
    263     }
    264 
    265     /**
    266      * Set the default ProgramStore object seen as the parent state by the root
    267      * rendering script.
    268      *
    269      * @param p
    270      */
    271     public void bindProgramStore(ProgramStore p) {
    272         validate();
    273         nContextBindProgramStore(safeID(p));
    274     }
    275 
    276     /**
    277      * Set the default ProgramFragment object seen as the parent state by the
    278      * root rendering script.
    279      *
    280      * @param p
    281      */
    282     public void bindProgramFragment(ProgramFragment p) {
    283         validate();
    284         nContextBindProgramFragment(safeID(p));
    285     }
    286 
    287     /**
    288      * Set the default ProgramRaster object seen as the parent state by the
    289      * root rendering script.
    290      *
    291      * @param p
    292      */
    293     public void bindProgramRaster(ProgramRaster p) {
    294         validate();
    295         nContextBindProgramRaster(safeID(p));
    296     }
    297 
    298     /**
    299      * Set the default ProgramVertex object seen as the parent state by the
    300      * root rendering script.
    301      *
    302      * @param p
    303      */
    304     public void bindProgramVertex(ProgramVertex p) {
    305         validate();
    306         nContextBindProgramVertex(safeID(p));
    307     }
    308 
    309 }
    310