Skip to content

Commit

Permalink
Adds BasicRenderScript sample.
Browse files Browse the repository at this point in the history
Change-Id: Ib4b5c2e7e6c3e73a70698ff37d87b462f6651b95
  • Loading branch information
Jeremy Walker committed Jul 25, 2019
1 parent a921f57 commit 41c8265
Show file tree
Hide file tree
Showing 38 changed files with 1,135 additions and 0 deletions.
18 changes: 18 additions & 0 deletions BasicRenderScript/.google/packaging.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

# GOOGLE SAMPLE PACKAGING DATA
#
# This file is used by Google as part of our samples packaging process.
# End users may safely ignore this file. It has no relevance to other systems.
---
status: PUBLISHED
technologies: [Android]
categories: [RenderScript]
languages: [Java]
solutions: [Mobile]
github: android/renderscript
level: EXPERT
icon: screenshots/icon-web.png
apiRefs:
- android:android.renderscript.RenderScript
- android:android.renderscript.Allocation
license: apache2
55 changes: 55 additions & 0 deletions BasicRenderScript/Application/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@

buildscript {
repositories {
google()
jcenter()
}

dependencies {
classpath 'com.android.tools.build:gradle:3.4.2'
}
}

apply plugin: 'com.android.application'

repositories {
google()
jcenter()
}

dependencies {


implementation "com.android.support:support-v4:28.0.0"
implementation "com.android.support:support-v13:28.0.0"
implementation "com.android.support:cardview-v7:28.0.0"
implementation "com.android.support:appcompat-v7:28.0.0"






}

// The sample build uses multiple directories to
// keep boilerplate and common code separate from
// the main sample code.
List<String> dirs = [
'main', // main sample code; look here for the interesting stuff.
'common', // components that are reused by multiple samples
'template'] // boilerplate code that is generated by the sample template process

android {
compileSdkVersion 28

defaultConfig {
minSdkVersion 14
targetSdkVersion 28

renderscriptTargetApi 24
renderscriptSupportModeEnabled true

}

}
15 changes: 15 additions & 0 deletions BasicRenderScript/Application/project.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# This file is automatically generated by Android Tools.
# Do not modify this file -- YOUR CHANGES WILL BE ERASED!
#
# This file must be checked in Version Control Systems.
#
# To customize properties used by the Ant build system edit
# "ant.properties", and override values to adapt the script to your
# project structure.
#
# To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home):
#proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt

# Project target.
target=android-19
renderscript.support.mode=true
37 changes: 37 additions & 0 deletions BasicRenderScript/Application/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright 2014 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<manifest package="com.example.android.basicrenderscript"
xmlns:android="http://schemas.android.com/apk/res/android">

<!-- Min/target SDK versions (<uses-sdk>) managed by build.gradle -->

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/Theme.BasicRenderScript">
<activity
android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
/*
* Copyright (C) 2014 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example.android.basicrenderscript;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v8.renderscript.Allocation;
import android.support.v8.renderscript.RenderScript;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;

public class MainActivity extends AppCompatActivity {

/**
* Number of bitmaps that is used for RenderScript thread and UI thread synchronization.
*/
private final int NUM_BITMAPS = 2;
private int mCurrentBitmap = 0;
private Bitmap mBitmapIn;
private Bitmap[] mBitmapsOut;
private ImageView mImageView;

private Allocation mInAllocation;
private Allocation[] mOutAllocations;
private ScriptC_saturation mScript;
private RenderScriptTask mCurrentTask;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.main_layout);

// Initialize UI
mBitmapIn = loadBitmap(R.drawable.data);
mBitmapsOut = new Bitmap[NUM_BITMAPS];
for (int i = 0; i < NUM_BITMAPS; ++i) {
mBitmapsOut[i] = Bitmap.createBitmap(mBitmapIn.getWidth(),
mBitmapIn.getHeight(), mBitmapIn.getConfig());
}

mImageView = (ImageView) findViewById(R.id.imageView);
mImageView.setImageBitmap(mBitmapsOut[mCurrentBitmap]);
mCurrentBitmap += (mCurrentBitmap + 1) % NUM_BITMAPS;

SeekBar seekbar = (SeekBar) findViewById(R.id.seekBar1);
seekbar.setProgress(50);
seekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
float max = 2.0f;
float min = 0.0f;
float f = (float) ((max - min) * (progress / 100.0) + min);
updateImage(f);
}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}

@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});

// Create renderScript
createScript();

// Invoke renderScript kernel and update imageView
updateImage(1.0f);
}

/**
* Initialize RenderScript.
*
* <p>In the sample, it creates RenderScript kernel that performs saturation manipulation.</p>
*/
private void createScript() {
// Initialize RS
RenderScript rs = RenderScript.create(this);

// Allocate buffers
mInAllocation = Allocation.createFromBitmap(rs, mBitmapIn);
mOutAllocations = new Allocation[NUM_BITMAPS];
for (int i = 0; i < NUM_BITMAPS; ++i) {
mOutAllocations[i] = Allocation.createFromBitmap(rs, mBitmapsOut[i]);
}

// Load script
mScript = new ScriptC_saturation(rs);
}

/*
* In the AsyncTask, it invokes RenderScript intrinsics to do a filtering.
* After the filtering is done, an operation blocks at Allocation.copyTo() in AsyncTask thread.
* Once all operation is finished at onPostExecute() in UI thread, it can invalidate and update
* ImageView UI.
*/
private class RenderScriptTask extends AsyncTask<Float, Void, Integer> {
Boolean issued = false;

protected Integer doInBackground(Float... values) {
int index = -1;
if (!isCancelled()) {
issued = true;
index = mCurrentBitmap;

// Set global variable in RS
mScript.set_saturationValue(values[0]);

// Invoke saturation filter kernel
mScript.forEach_saturation(mInAllocation, mOutAllocations[index]);

// Copy to bitmap and invalidate image view
mOutAllocations[index].copyTo(mBitmapsOut[index]);
mCurrentBitmap = (mCurrentBitmap + 1) % NUM_BITMAPS;
}
return index;
}

void updateView(Integer result) {
if (result != -1) {
// Request UI update
mImageView.setImageBitmap(mBitmapsOut[result]);
mImageView.invalidate();
}
}

protected void onPostExecute(Integer result) {
updateView(result);
}

protected void onCancelled(Integer result) {
if (issued) {
updateView(result);
}
}
}

/**
* Invoke AsyncTask and cancel previous task. When AsyncTasks are piled up (typically in slow
* device with heavy kernel), Only the latest (and already started) task invokes RenderScript
* operation.
*/
private void updateImage(final float f) {
if (mCurrentTask != null) {
mCurrentTask.cancel(false);
}
mCurrentTask = new RenderScriptTask();
mCurrentTask.execute(f);
}

/**
* Helper to load Bitmap from resource
*/
private Bitmap loadBitmap(int resource) {
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
return BitmapFactory.decodeResource(getResources(), resource, options);
}

}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
39 changes: 39 additions & 0 deletions BasicRenderScript/Application/src/main/res/layout/main_layout.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright 2016 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="@null"
android:scaleType="fitCenter"
android:src="@drawable/data"/>

<SeekBar
android:id="@+id/seekBar1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_marginBottom="16dp"/>

</FrameLayout>
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<!--
Copyright 2013 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->

<resources>

<!-- Semantic definitions -->

<dimen name="horizontal_page_margin">@dimen/margin_huge</dimen>
<dimen name="vertical_page_margin">@dimen/margin_medium</dimen>

</resources>
Loading

0 comments on commit 41c8265

Please sign in to comment.