Home | History | Annotate | Download | only in waitattachnodebug
      1 /*
      2 * Copyright (C) 2016 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 com.android.rs.waitattachnodebug;
     18 
     19 import android.app.Activity;
     20 import android.graphics.Bitmap;
     21 import android.os.Bundle;
     22 import android.widget.ImageView;
     23 import android.renderscript.*;
     24 
     25 public class MainActivity extends Activity {
     26     private Bitmap mBitmapIn;
     27     private Bitmap mBitmapOut;
     28     private ImageView mImageView;
     29 
     30     private RenderScript mRS;
     31     private Allocation mInAllocation;
     32     private Allocation mOutAllocation;
     33     private ScriptC_simple mScript;
     34 
     35     @Override
     36     protected void onCreate(Bundle savedInstanceState) {
     37         super.onCreate(savedInstanceState);
     38 
     39         setContentView(R.layout.main_layout);
     40 
     41         mBitmapIn = Bitmap.createBitmap(8, 8, Bitmap.Config.ARGB_8888);
     42         mBitmapOut = Bitmap.createBitmap(mBitmapIn.getWidth(),
     43                     mBitmapIn.getHeight(), mBitmapIn.getConfig());
     44 
     45         mImageView = findViewById(R.id.imageView);
     46         mImageView.setImageBitmap(mBitmapOut);
     47 
     48         createScript();
     49         updateImage(1.0f);
     50     }
     51 
     52     private void createScript() {
     53         mRS = RenderScript.create(this,
     54             RenderScript.ContextType.NORMAL,
     55             RenderScript.CREATE_FLAG_LOW_LATENCY |
     56             RenderScript.CREATE_FLAG_WAIT_FOR_ATTACH);
     57 
     58         mInAllocation = Allocation.createFromBitmap(mRS, mBitmapIn);
     59         mOutAllocation = Allocation.createFromBitmap(mRS, mBitmapOut);
     60 
     61         mScript = new ScriptC_simple(mRS);
     62     }
     63 
     64 
     65     private void updateImage(final float f) {
     66         mScript.set_gColor(new Float4(0.9f, 0.8f, 0.5f, 1.0f));
     67         mScript.forEach_simple_kernel(mInAllocation, mOutAllocation);
     68         mOutAllocation.copyTo(mBitmapOut);
     69     }
     70 }
     71 
     72