15bbcc3861c44435f89481f80946ef5c9c49968f2Luke Drummond/*
25bbcc3861c44435f89481f80946ef5c9c49968f2Luke Drummond* Copyright (C) 2016 The Android Open Source Project
35bbcc3861c44435f89481f80946ef5c9c49968f2Luke Drummond*
45bbcc3861c44435f89481f80946ef5c9c49968f2Luke Drummond* Licensed under the Apache License, Version 2.0 (the "License");
55bbcc3861c44435f89481f80946ef5c9c49968f2Luke Drummond* you may not use this file except in compliance with the License.
65bbcc3861c44435f89481f80946ef5c9c49968f2Luke Drummond* You may obtain a copy of the License at
75bbcc3861c44435f89481f80946ef5c9c49968f2Luke Drummond*
85bbcc3861c44435f89481f80946ef5c9c49968f2Luke Drummond*      http://www.apache.org/licenses/LICENSE-2.0
95bbcc3861c44435f89481f80946ef5c9c49968f2Luke Drummond*
105bbcc3861c44435f89481f80946ef5c9c49968f2Luke Drummond* Unless required by applicable law or agreed to in writing, software
115bbcc3861c44435f89481f80946ef5c9c49968f2Luke Drummond* distributed under the License is distributed on an "AS IS" BASIS,
125bbcc3861c44435f89481f80946ef5c9c49968f2Luke Drummond* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
135bbcc3861c44435f89481f80946ef5c9c49968f2Luke Drummond* See the License for the specific language governing permissions and
145bbcc3861c44435f89481f80946ef5c9c49968f2Luke Drummond* limitations under the License.
155bbcc3861c44435f89481f80946ef5c9c49968f2Luke Drummond*/
165bbcc3861c44435f89481f80946ef5c9c49968f2Luke Drummond
17dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leopackage com.android.rs.kernelvariables;
18dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo
19dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leoimport android.app.Activity;
20dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leoimport android.graphics.Bitmap;
21dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leoimport android.graphics.ImageFormat;
22dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leoimport android.os.Bundle;
23dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leoimport android.widget.ImageView;
24dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leoimport android.renderscript.*;
25dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo
26dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leopublic class MainActivity extends Activity {
27dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo    private Bitmap mBitmapIn;
28dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo    private Bitmap mBitmapOut;
29dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo    private ImageView mImageView;
30dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo
31dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo    private RenderScript mRS;
32dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo    private Allocation mInAllocation;
33dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo    private Allocation mOutAllocation;
34dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo    private ScriptC_simple mScript;
35dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo
36dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo    @Override
37dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo    protected void onCreate(Bundle savedInstanceState) {
38dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo        super.onCreate(savedInstanceState);
39dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo
40dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo        setContentView(R.layout.main_layout);
41dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo
42dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo        mBitmapIn = Bitmap.createBitmap(500, 500, Bitmap.Config.ARGB_8888);
43dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo        mBitmapOut = Bitmap.createBitmap(mBitmapIn.getWidth(),
44dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo                    mBitmapIn.getHeight(), mBitmapIn.getConfig());
45dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo
468467325c6d80de30bb86b7665cc41dff0c276e8dAlan Viverette        mImageView = findViewById(R.id.imageView);
47dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo        mImageView.setImageBitmap(mBitmapOut);
48dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo
49dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo        createScript();
50dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo        updateImage();
51dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo    }
52dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo
53dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo    private void createScript() {
54dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo        mRS = RenderScript.create(this,
55dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo            RenderScript.ContextType.NORMAL,
56dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo            RenderScript.CREATE_FLAG_LOW_LATENCY |
57dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo            RenderScript.CREATE_FLAG_WAIT_FOR_ATTACH);
58dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo
59dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo        mInAllocation = Allocation.createFromBitmap(mRS, mBitmapIn);
60dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo        mOutAllocation = Allocation.createFromBitmap(mRS, mBitmapOut);
61dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo
62dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo        mScript = new ScriptC_simple(mRS);
63dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo    }
64dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo
65dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo    private void updateImage() {
66dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo        int[] buffer_int = {1, 2, 3, 4};
67dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo        Allocation int_allocation = Allocation.createSized(mRS, Element.I32(mRS), 4);
68dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo        int_allocation.copyFrom(buffer_int);
69dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo        mScript.set_allocation_1D_global(int_allocation);
70dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo
71dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo        int[] buffer_int2 = {5, 6, 7, 8};
72dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo
73dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo        Type.Builder typeI32Builder2D = new Type.Builder(mRS, Element.I32(mRS));
74dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo        typeI32Builder2D.setX(2);
75dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo        typeI32Builder2D.setY(2);
76dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo
77dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo        Allocation int_allocation2 = Allocation.createTyped(mRS, typeI32Builder2D.create());
78dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo        int_allocation2.copyFrom(buffer_int2);
79dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo        mScript.set_allocation_1D_global2(int_allocation2);
80dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo
81dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo        mScript.set_allocation_2D_global(mInAllocation);
82dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo        mScript.set_allocation_2D_global2(mOutAllocation);
83dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo
84dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo        int[] buffer_int3 = new int[64];
85dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo
86dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo        for (int i=0; i<4*4*4; ++i)
87dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo            buffer_int3[i] = 9 + i;
88dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo
89dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo        Type.Builder typeI32Builder3D = new Type.Builder(mRS, Element.I32(mRS));
90dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo        typeI32Builder3D.setX(4);
91dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo        typeI32Builder3D.setY(4);
92dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo        typeI32Builder3D.setZ(4);
93dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo
94dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo        Allocation int_allocation3 = Allocation.createTyped(mRS, typeI32Builder3D.create());
95dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo        int_allocation3.copyFrom(buffer_int3);
96dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo        mScript.set_allocation_3D_global(int_allocation3);
97dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo
98dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo        Type.Builder yuvTypeBuilder = new Type.Builder(mRS, Element.YUV(mRS));
99dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo        yuvTypeBuilder.setX(4);
100dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo        yuvTypeBuilder.setY(4);
101dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo        yuvTypeBuilder.setYuvFormat(ImageFormat.YV12);
102dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo        Allocation yuv_allocation = Allocation.createTyped(mRS, yuvTypeBuilder.create());
103dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo        mScript.set_allocation_YUV_2D_global(yuv_allocation);
104dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo
105dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo        mScript.set_sampler_global(Sampler.CLAMP_LINEAR(mRS));
106dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo
107dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo        mScript.forEach_kernel(mInAllocation, mOutAllocation);
108dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo        mOutAllocation.copyTo(mBitmapOut);
109dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo    }
110dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo}
111