Home | History | Annotate | Download | only in livepreview
      1 /*
      2  * Copyright (C) 2013 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.Surface;
     31 import android.view.View;
     32 
     33 import android.content.res.Resources;
     34 import android.renderscript.*;
     35 
     36 import android.graphics.Bitmap;
     37 
     38 public class RsYuv implements TextureView.SurfaceTextureListener
     39 {
     40     private int mHeight;
     41     private int mWidth;
     42     private RenderScript mRS;
     43     private Allocation mAllocationOut;
     44     private Allocation mAllocationIn;
     45     private ScriptC_yuv mScript;
     46     private ScriptIntrinsicYuvToRGB mYuv;
     47     private boolean mHaveSurface;
     48     private Surface mSurface;
     49     private ScriptGroup mGroup;
     50 
     51     RsYuv(RenderScript rs) {
     52         mRS = rs;
     53         mScript = new ScriptC_yuv(mRS);
     54         mYuv = ScriptIntrinsicYuvToRGB.create(rs, Element.RGBA_8888(mRS));
     55     }
     56 
     57     void setupSurface() {
     58         if (mAllocationOut != null) {
     59             mAllocationOut.setSurface(mSurface);
     60         }
     61         if (mSurface != null) {
     62             mHaveSurface = true;
     63         } else {
     64             mHaveSurface = false;
     65         }
     66     }
     67 
     68     void reset(int width, int height) {
     69         if (mAllocationOut != null) {
     70             mAllocationOut.destroy();
     71         }
     72 
     73         android.util.Log.v("cpa", "reset " + width + ", " + height);
     74         mHeight = height;
     75         mWidth = width;
     76         mScript.invoke_setSize(mWidth, mHeight);
     77 
     78         Type.Builder tb = new Type.Builder(mRS, Element.RGBA_8888(mRS));
     79         tb.setX(mWidth);
     80         tb.setY(mHeight);
     81         Type t = tb.create();
     82         mAllocationOut = Allocation.createTyped(mRS, t, Allocation.USAGE_SCRIPT |
     83                                                         Allocation.USAGE_IO_OUTPUT);
     84 
     85 
     86         tb = new Type.Builder(mRS, Element.createPixel(mRS, Element.DataType.UNSIGNED_8, Element.DataKind.PIXEL_YUV));
     87         tb.setX(mWidth);
     88         tb.setY(mHeight);
     89         tb.setYuvFormat(android.graphics.ImageFormat.NV21);
     90         mAllocationIn = Allocation.createTyped(mRS, tb.create(), Allocation.USAGE_SCRIPT);
     91         mYuv.setInput(mAllocationIn);
     92         setupSurface();
     93 
     94 
     95         ScriptGroup.Builder b = new ScriptGroup.Builder(mRS);
     96         b.addKernel(mScript.getKernelID_root());
     97         b.addKernel(mYuv.getKernelID());
     98         b.addConnection(t, mYuv.getKernelID(), mScript.getKernelID_root());
     99         mGroup = b.create();
    100     }
    101 
    102     public int getWidth() {
    103         return mWidth;
    104     }
    105     public int getHeight() {
    106         return mHeight;
    107     }
    108 
    109     private long mTiming[] = new long[50];
    110     private int mTimingSlot = 0;
    111 
    112     void execute(byte[] yuv) {
    113         mAllocationIn.copyFrom(yuv);
    114         if (mHaveSurface) {
    115             mGroup.setOutput(mScript.getKernelID_root(), mAllocationOut);
    116             mGroup.execute();
    117 
    118             //mYuv.forEach(mAllocationOut);
    119             //mScript.forEach_root(mAllocationOut, mAllocationOut);
    120             mAllocationOut.ioSend();
    121         }
    122     }
    123 
    124 
    125 
    126     @Override
    127     public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
    128         android.util.Log.v("cpa", "onSurfaceTextureAvailable " + surface);
    129         mSurface = new Surface(surface);
    130         setupSurface();
    131     }
    132 
    133     @Override
    134     public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
    135         android.util.Log.v("cpa", "onSurfaceTextureSizeChanged " + surface);
    136         mSurface = new Surface(surface);
    137         setupSurface();
    138     }
    139 
    140     @Override
    141     public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
    142         android.util.Log.v("cpa", "onSurfaceTextureDestroyed " + surface);
    143         mSurface = null;
    144         setupSurface();
    145         return true;
    146     }
    147 
    148     @Override
    149     public void onSurfaceTextureUpdated(SurfaceTexture surface) {
    150     }
    151 }
    152 
    153