This repository was archived by the owner on Oct 23, 2024. It is now read-only.
-
Couldn't load subscription status.
- Fork 81
Render effect #20
Merged
Merged
Render effect #20
Changes from all commits
Commits
Show all changes
4 commits Select commit Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions 109 ...ionSample/app/src/main/java/com/android/example/rsmigration/RenderEffectImageProcessor.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| package com.android.example.rsmigration | ||
| | ||
| import android.annotation.SuppressLint | ||
| import android.graphics.* | ||
| import android.hardware.HardwareBuffer | ||
| import android.media.ImageReader | ||
| import android.os.Build | ||
| import androidx.annotation.RequiresApi | ||
| import java.lang.RuntimeException | ||
| import kotlin.math.cos | ||
| import kotlin.math.sin | ||
| | ||
| @RequiresApi(Build.VERSION_CODES.S) | ||
| class RenderEffectImageProcessor : ImageProcessor { | ||
| override val name = "RenderEffect" | ||
| private var params: Params? = null | ||
| | ||
| override fun configureInputAndOutput(inputImage: Bitmap, numberOfOutputImages: Int) { | ||
| params = Params( | ||
| inputImage, | ||
| numberOfOutputImages | ||
| ) | ||
| } | ||
| | ||
| private fun applyEffect(it: Params, renderEffect: RenderEffect, outputIndex: Int): Bitmap { | ||
| it.renderNode.setRenderEffect(renderEffect) | ||
| val renderCanvas = it.renderNode.beginRecording() | ||
| renderCanvas.drawBitmap(it.bitmap, 0f, 0f, null) | ||
| it.renderNode.endRecording() | ||
| it.hardwareRenderer.createRenderRequest() | ||
| .setWaitForPresent(true) | ||
| .syncAndDraw() | ||
| | ||
| val image = it.imageReader.acquireNextImage() ?: throw RuntimeException("No Image") | ||
| val hardwareBuffer = image.hardwareBuffer ?: throw RuntimeException("No HardwareBuffer") | ||
| val bitmap = Bitmap.wrapHardwareBuffer(hardwareBuffer, null) | ||
| ?: throw RuntimeException("Create Bitmap Failed") | ||
| hardwareBuffer.close() | ||
| image.close() | ||
| return bitmap | ||
| } | ||
| | ||
| override fun rotateHue(radian: Float, outputIndex: Int): Bitmap { | ||
| params?.let { | ||
| val colorMatrix = if (radian == 0f) { | ||
| ColorMatrix() | ||
| } else { | ||
| val cos = cos(radian.toDouble()) | ||
| val sin = sin(radian.toDouble()) | ||
| ColorMatrix(floatArrayOf( | ||
| (.299 + .701 * cos + .168 * sin).toFloat(), //0 | ||
| (.587 - .587 * cos + .330 * sin).toFloat(), //1 | ||
| (.114 - .114 * cos - .497 * sin).toFloat(), //2 | ||
| 0f, 0f, //3,4 | ||
| (.299 - .299 * cos - .328 * sin).toFloat(), //5 | ||
| (.587 + .413 * cos + .035 * sin).toFloat(), //6 | ||
| (.114 - .114 * cos + .292 * sin).toFloat(), //7 | ||
| 0f, 0f, //8,9 | ||
| (.299 - .300 * cos + 1.25 * sin).toFloat(), //10 | ||
| (.587 - .588 * cos - 1.05 * sin).toFloat(), //11 | ||
| (.114 + .886 * cos - .203 * sin).toFloat(), //12 | ||
| 0f, 0f, 0f, 0f, 0f, //13,14,15,16,17 | ||
| 1f, 0f //18,19 | ||
| )) | ||
| } | ||
| val colorFilterEffect = | ||
| RenderEffect.createColorFilterEffect(ColorMatrixColorFilter(colorMatrix)) | ||
| return applyEffect(it, colorFilterEffect, outputIndex) | ||
| } | ||
| throw RuntimeException("Not configured!") | ||
| } | ||
| | ||
| override fun blur(radius: Float, outputIndex: Int): Bitmap { | ||
| params?.let { | ||
| val blurRenderEffect = RenderEffect.createBlurEffect( | ||
| radius, radius, | ||
| Shader.TileMode.MIRROR | ||
| ) | ||
| return applyEffect(it, blurRenderEffect, outputIndex) | ||
| } | ||
| throw RuntimeException("Not configured!") | ||
| } | ||
| | ||
| override fun cleanup() { | ||
| params?.let { | ||
| params = null | ||
| it.imageReader.close() | ||
| it.renderNode.discardDisplayList() | ||
| it.hardwareRenderer.destroy() | ||
| } | ||
| } | ||
| | ||
| inner class Params(val bitmap: Bitmap, numberOfOutputImages: Int) { | ||
| @SuppressLint("WrongConstant") | ||
| val imageReader = ImageReader.newInstance( | ||
| bitmap.width, bitmap.height, | ||
| PixelFormat.RGBA_8888, numberOfOutputImages, | ||
| HardwareBuffer.USAGE_GPU_SAMPLED_IMAGE or HardwareBuffer.USAGE_GPU_COLOR_OUTPUT | ||
| ) | ||
| val renderNode = RenderNode("RenderEffect") | ||
| val hardwareRenderer = HardwareRenderer() | ||
| | ||
| init { | ||
| hardwareRenderer.setSurface(imageReader.surface) | ||
| hardwareRenderer.setContentRoot(renderNode) | ||
| renderNode.setPosition(0, 0, imageReader.width, imageReader.height) | ||
| } | ||
| } | ||
| } | ||
10 changes: 10 additions & 0 deletions 10 RenderScriptMigrationSample/app/src/main/res/values-v31/strings.xml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| <?xml version="1.0" encoding="utf-8"?> | ||
| <resources xmlns:tools="http://schemas.android.com/tools"> | ||
| <string-array name="processor_array" tools:ignore="InconsistentArrays"> | ||
| <item>@string/renderscript_intrinsics</item> | ||
| <item>@string/renderscript_scripts</item> | ||
| <item>@string/vulkan</item> | ||
| <item>@string/render_effect</item> | ||
| </string-array> | ||
| | ||
| </resources> |
17 changes: 11 additions & 6 deletions 17 RenderScriptMigrationSample/app/src/main/res/values/strings.xml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The hardwareBuffer can be closed at this point because Bitmap.wrapHardwareBuffer already acquires another reference. If the hardwareBuffer is closed here, the hardwareBuffers field in Params can also be removed, I think.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Excellent suggestion!