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