diff options
author | Stan Iliev <stani@google.com> | 2019-11-22 18:00:01 -0500 |
---|---|---|
committer | Stan Iliev <stani@google.com> | 2019-12-03 11:38:29 -0500 |
commit | 6867fc8778dff60f68f9cff1f82d5fd0f899ba55 (patch) | |
tree | 9414aff13916b7c0af6048d266621e49f3a5eeba /graphics/java | |
parent | 5605a0fe1190ceb9d7070b59f3b4e2d3a730808c (diff) |
Implement a new Shader API, which can run custom code on GPU
Add RuntimeShader hidden API, which calculates pixel output with
a fragment shader running on GPU.
Extend ColorFiltersMutateActivity HWUI test to use new API and
show how to animate uniforms on UI thread.
Test: Updated HwAccelerationTest
Change-Id: Ia26e44259b12099924facba250880cbbd9be21c7
Diffstat (limited to 'graphics/java')
-rw-r--r-- | graphics/java/android/graphics/RuntimeShader.java | 88 |
1 files changed, 88 insertions, 0 deletions
diff --git a/graphics/java/android/graphics/RuntimeShader.java b/graphics/java/android/graphics/RuntimeShader.java new file mode 100644 index 000000000000..613ce9042056 --- /dev/null +++ b/graphics/java/android/graphics/RuntimeShader.java @@ -0,0 +1,88 @@ +/* + * Copyright 2019 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 android.graphics; + +import android.annotation.NonNull; +import android.annotation.Nullable; + +import libcore.util.NativeAllocationRegistry; + +/** + * Shader that calculates pixel output with a program (fragment shader) running on a GPU. + * @hide + */ +public class RuntimeShader extends Shader { + + private static class NoImagePreloadHolder { + public static final NativeAllocationRegistry sRegistry = + NativeAllocationRegistry.createMalloced( + RuntimeShader.class.getClassLoader(), nativeGetFinalizer()); + } + + private byte[] mUniforms; + + /** + * Current native shader factory instance. + */ + private long mNativeInstanceRuntimeShaderFactory; + + /** + * Creates a new RuntimeShader. + * + * @param sksl The text of SKSL program to run on the GPU. + * @param uniforms Array of parameters passed by the SKSL shader. Array size depends + * on number of uniforms declared by sksl. + * @param isOpaque True if all pixels have alpha 1.0f. + */ + public RuntimeShader(@NonNull String sksl, @Nullable byte[] uniforms, boolean isOpaque) { + this(sksl, uniforms, isOpaque, ColorSpace.get(ColorSpace.Named.SRGB)); + } + + private RuntimeShader(@NonNull String sksl, @Nullable byte[] uniforms, boolean isOpaque, + ColorSpace colorSpace) { + super(colorSpace); + mUniforms = uniforms; + mNativeInstanceRuntimeShaderFactory = nativeCreateShaderFactory(sksl, isOpaque); + NoImagePreloadHolder.sRegistry.registerNativeAllocation(this, + mNativeInstanceRuntimeShaderFactory); + } + + /** + * Sets new value for shader parameters. + * + * @param uniforms Array of parameters passed by the SKSL shader. Array size depends + * on number of uniforms declared by mSksl. + */ + public void updateUniforms(@Nullable byte[] uniforms) { + mUniforms = uniforms; + discardNativeInstance(); + } + + @Override + long createNativeInstance(long nativeMatrix) { + return nativeCreate(mNativeInstanceRuntimeShaderFactory, nativeMatrix, mUniforms, + colorSpace().getNativeInstance()); + } + + private static native long nativeCreate(long shaderFactory, long matrix, byte[] inputs, + long colorSpaceHandle); + + private static native long nativeCreateShaderFactory(String sksl, boolean isOpaque); + + private static native long nativeGetFinalizer(); +} + |