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 Leo#include <RenderScript.h>
18dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo
19a3c6f62775506c95afd556e617f14d7a28839f01Luke Drummond#include "ScriptC_scalars.h"
20dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo
21dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leoint main()
22dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo{
23dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo    static const int size = 64;
24dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo    sp<RS> rs = new RS();
25dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo
26dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo    rs->init("/data/rscache", RS_INIT_LOW_LATENCY | RS_INIT_WAIT_FOR_ATTACH);
27dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo
28dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo    auto e = Element::I32(rs);
29dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo    Type::Builder tb(rs, e);
30dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo    tb.setX(size);
31dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo    tb.setY(size);
32dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo    auto t = tb.create();
33dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo
34dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo    auto a = Allocation::createTyped(rs, t);
35dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo    auto b = Allocation::createTyped(rs, t);
36dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo
37dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo    int * input = new int[size*size];
38dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo    for(int i = 0; i < size*size; ++i) {
39dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo        input[i] = i - (size*size / 2);
40dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo    }
41dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo    a->copy2DRangeFrom(0, 0, size, size, input);
42dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo    delete [] input;
43dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo
44dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo    // Script is executed once, then the data is copied back when finished
45a3c6f62775506c95afd556e617f14d7a28839f01Luke Drummond    sp<ScriptC_scalars> s = new ScriptC_scalars(rs);
46dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo    s->invoke_addToGlobal(234);
47dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo    s->forEach_simple_kernel(a, b);
48dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo    rs->finish();
49dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo    int32_t * output = new int32_t[size*size];
50dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo    b->copy2DRangeTo(0, 0, size, size, output);
51dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo    delete [] output;
52dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo
53dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo    return 0;
54dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo}
55dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo
56