Home | History | Annotate | Download | only in livepreview
      1 /*
      2  * Copyright (C) 2012 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.livepreview;
     18 
     19 import android.app.Activity;
     20 import android.graphics.Bitmap;
     21 import android.graphics.BitmapFactory;
     22 import android.graphics.Canvas;
     23 import android.os.Bundle;
     24 import android.graphics.SurfaceTexture;
     25 import android.renderscript.Allocation;
     26 import android.renderscript.Matrix3f;
     27 import android.renderscript.RenderScript;
     28 import android.util.Log;
     29 import android.view.TextureView;
     30 import android.view.View;
     31 
     32 import android.content.res.Resources;
     33 import android.renderscript.*;
     34 
     35 import android.graphics.Bitmap;
     36 
     37 public class RsYuv
     38 {
     39     private int mHeight;
     40     private int mWidth;
     41     private RenderScript mRS;
     42     private Allocation mAllocationOut;
     43     private Allocation mAllocationIn;
     44     private ScriptC_yuv mScript;
     45     private ScriptIntrinsicYuvToRGB mYuv;
     46 
     47     RsYuv(RenderScript rs, Resources res, int width, int height) {
     48         mHeight = height;
     49         mWidth = width;
     50         mRS = rs;
     51         mScript = new ScriptC_yuv(mRS, res, R.raw.yuv);
     52         mScript.invoke_setSize(mWidth, mHeight);
     53 
     54         mYuv = ScriptIntrinsicYuvToRGB.create(rs, Element.RGBA_8888(mRS));
     55 
     56         Type.Builder tb = new Type.Builder(mRS, Element.RGBA_8888(mRS));
     57         tb.setX(mWidth);
     58         tb.setY(mHeight);
     59 
     60         mAllocationOut = Allocation.createTyped(rs, tb.create());
     61         mAllocationIn = Allocation.createSized(rs, Element.U8(mRS), (mHeight * mWidth) +
     62                                                ((mHeight / 2) * (mWidth / 2) * 2));
     63 
     64         mYuv.setInput(mAllocationIn);
     65     }
     66 
     67     private long mTiming[] = new long[50];
     68     private int mTimingSlot = 0;
     69 
     70     void execute(byte[] yuv, Bitmap b) {
     71         mAllocationIn.copyFrom(yuv);
     72         mYuv.forEach(mAllocationOut);
     73         mScript.forEach_root(mAllocationOut, mAllocationOut);
     74         mAllocationOut.copyTo(b);
     75     }
     76 
     77 }
     78 
     79