1 /*
2 * Copyright (C) 2020 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 #include "Bitmap.h"
17 #include "GraphicsJNI.h"
18 #include "SkImageFilter.h"
19 #include "SkImageFilters.h"
20 #include "graphics_jni_helpers.h"
21 #include "utils/Blur.h"
22 #include <utils/Log.h>
23
24 using namespace android::uirenderer;
25
createOffsetEffect(JNIEnv * env,jobject,jfloat offsetX,jfloat offsetY,jlong inputFilterHandle)26 static jlong createOffsetEffect(
27 JNIEnv* env,
28 jobject,
29 jfloat offsetX,
30 jfloat offsetY,
31 jlong inputFilterHandle
32 ) {
33 auto* inputFilter = reinterpret_cast<const SkImageFilter*>(inputFilterHandle);
34 sk_sp<SkImageFilter> offset = SkImageFilters::Offset(offsetX, offsetY, sk_ref_sp(inputFilter));
35 return reinterpret_cast<jlong>(offset.release());
36 }
37
createBlurEffect(JNIEnv * env,jobject,jfloat radiusX,jfloat radiusY,jlong inputFilterHandle,jint edgeTreatment)38 static jlong createBlurEffect(JNIEnv* env , jobject, jfloat radiusX,
39 jfloat radiusY, jlong inputFilterHandle, jint edgeTreatment) {
40 auto* inputImageFilter = reinterpret_cast<SkImageFilter*>(inputFilterHandle);
41 sk_sp<SkImageFilter> blurFilter =
42 SkImageFilters::Blur(
43 Blur::convertRadiusToSigma(radiusX),
44 Blur::convertRadiusToSigma(radiusY),
45 static_cast<SkTileMode>(edgeTreatment),
46 sk_ref_sp(inputImageFilter),
47 nullptr);
48 return reinterpret_cast<jlong>(blurFilter.release());
49 }
50
createBitmapEffect(JNIEnv * env,jobject,jlong bitmapHandle,jfloat srcLeft,jfloat srcTop,jfloat srcRight,jfloat srcBottom,jfloat dstLeft,jfloat dstTop,jfloat dstRight,jfloat dstBottom)51 static jlong createBitmapEffect(
52 JNIEnv* env,
53 jobject,
54 jlong bitmapHandle,
55 jfloat srcLeft,
56 jfloat srcTop,
57 jfloat srcRight,
58 jfloat srcBottom,
59 jfloat dstLeft,
60 jfloat dstTop,
61 jfloat dstRight,
62 jfloat dstBottom
63 ) {
64 sk_sp<SkImage> image = android::bitmap::toBitmap(bitmapHandle).makeImage();
65 SkRect srcRect = SkRect::MakeLTRB(srcLeft, srcTop, srcRight, srcBottom);
66 SkRect dstRect = SkRect::MakeLTRB(dstLeft, dstTop, dstRight, dstBottom);
67 sk_sp<SkImageFilter> bitmapFilter = SkImageFilters::Image(
68 image, srcRect, dstRect, SkSamplingOptions(SkFilterMode::kLinear));
69 return reinterpret_cast<jlong>(bitmapFilter.release());
70 }
71
createColorFilterEffect(JNIEnv * env,jobject,jlong colorFilterHandle,jlong inputFilterHandle)72 static jlong createColorFilterEffect(
73 JNIEnv* env,
74 jobject,
75 jlong colorFilterHandle,
76 jlong inputFilterHandle
77 ) {
78 auto* colorFilter = reinterpret_cast<const SkColorFilter*>(colorFilterHandle);
79 auto* inputFilter = reinterpret_cast<const SkImageFilter*>(inputFilterHandle);
80 sk_sp<SkImageFilter> colorFilterImageFilter = SkImageFilters::ColorFilter(
81 sk_ref_sp(colorFilter), sk_ref_sp(inputFilter), nullptr);
82 return reinterpret_cast<jlong>(colorFilterImageFilter.release());
83 }
84
createBlendModeEffect(JNIEnv * env,jobject,jlong backgroundImageFilterHandle,jlong foregroundImageFilterHandle,jint blendmodeHandle)85 static jlong createBlendModeEffect(
86 JNIEnv* env,
87 jobject,
88 jlong backgroundImageFilterHandle,
89 jlong foregroundImageFilterHandle,
90 jint blendmodeHandle
91 ) {
92 auto* backgroundFilter = reinterpret_cast<const SkImageFilter*>(backgroundImageFilterHandle);
93 auto* foregroundFilter = reinterpret_cast<const SkImageFilter*>(foregroundImageFilterHandle);
94 SkBlendMode blendMode = static_cast<SkBlendMode>(blendmodeHandle);
95 sk_sp<SkImageFilter> xfermodeFilter = SkImageFilters::Blend(
96 blendMode,
97 sk_ref_sp(backgroundFilter),
98 sk_ref_sp(foregroundFilter)
99 );
100 return reinterpret_cast<jlong>(xfermodeFilter.release());
101 }
102
createChainEffect(JNIEnv * env,jobject,jlong outerFilterHandle,jlong innerFilterHandle)103 static jlong createChainEffect(
104 JNIEnv* env,
105 jobject,
106 jlong outerFilterHandle,
107 jlong innerFilterHandle
108 ) {
109 auto* outerImageFilter = reinterpret_cast<const SkImageFilter*>(outerFilterHandle);
110 auto* innerImageFilter = reinterpret_cast<const SkImageFilter*>(innerFilterHandle);
111 sk_sp<SkImageFilter> composeFilter = SkImageFilters::Compose(
112 sk_ref_sp(outerImageFilter),
113 sk_ref_sp(innerImageFilter)
114 );
115 return reinterpret_cast<jlong>(composeFilter.release());
116 }
117
createShaderEffect(JNIEnv * env,jobject,jlong shaderHandle)118 static jlong createShaderEffect(
119 JNIEnv* env,
120 jobject,
121 jlong shaderHandle
122 ) {
123 auto* shader = reinterpret_cast<const SkShader*>(shaderHandle);
124 sk_sp<SkImageFilter> shaderFilter = SkImageFilters::Shader(
125 sk_ref_sp(shader), nullptr
126 );
127 return reinterpret_cast<jlong>(shaderFilter.release());
128 }
129
ThrowIAEFmt(JNIEnv * env,const char * fmt,...)130 static inline int ThrowIAEFmt(JNIEnv* env, const char* fmt, ...) {
131 va_list args;
132 va_start(args, fmt);
133 int ret = jniThrowExceptionFmt(env, "java/lang/IllegalArgumentException", fmt, args);
134 va_end(args);
135 return ret;
136 }
137
createRuntimeShaderEffect(JNIEnv * env,jobject,jlong shaderBuilderHandle,jstring inputShaderName)138 static jlong createRuntimeShaderEffect(JNIEnv* env, jobject, jlong shaderBuilderHandle,
139 jstring inputShaderName) {
140 SkRuntimeShaderBuilder* builder =
141 reinterpret_cast<SkRuntimeShaderBuilder*>(shaderBuilderHandle);
142 ScopedUtfChars name(env, inputShaderName);
143
144 if (builder->child(name.c_str()).fChild == nullptr) {
145 ThrowIAEFmt(env,
146 "unable to find a uniform with the name '%s' of the correct "
147 "type defined by the provided RuntimeShader",
148 name.c_str());
149 return 0;
150 }
151
152 sk_sp<SkImageFilter> filter = SkImageFilters::RuntimeShader(*builder, name.c_str(), nullptr);
153 return reinterpret_cast<jlong>(filter.release());
154 }
155
RenderEffect_safeUnref(SkImageFilter * filter)156 static void RenderEffect_safeUnref(SkImageFilter* filter) {
157 SkSafeUnref(filter);
158 }
159
getRenderEffectFinalizer(JNIEnv *,jobject)160 static jlong getRenderEffectFinalizer(JNIEnv*, jobject) {
161 return static_cast<jlong>(reinterpret_cast<uintptr_t>(&RenderEffect_safeUnref));
162 }
163
164 static const JNINativeMethod gRenderEffectMethods[] = {
165 {"nativeGetFinalizer", "()J", (void*)getRenderEffectFinalizer},
166 {"nativeCreateOffsetEffect", "(FFJ)J", (void*)createOffsetEffect},
167 {"nativeCreateBlurEffect", "(FFJI)J", (void*)createBlurEffect},
168 {"nativeCreateBitmapEffect", "(JFFFFFFFF)J", (void*)createBitmapEffect},
169 {"nativeCreateColorFilterEffect", "(JJ)J", (void*)createColorFilterEffect},
170 {"nativeCreateBlendModeEffect", "(JJI)J", (void*)createBlendModeEffect},
171 {"nativeCreateChainEffect", "(JJ)J", (void*)createChainEffect},
172 {"nativeCreateShaderEffect", "(J)J", (void*)createShaderEffect},
173 {"nativeCreateRuntimeShaderEffect", "(JLjava/lang/String;)J",
174 (void*)createRuntimeShaderEffect}};
175
register_android_graphics_RenderEffect(JNIEnv * env)176 int register_android_graphics_RenderEffect(JNIEnv* env) {
177 android::RegisterMethodsOrDie(env, "android/graphics/RenderEffect",
178 gRenderEffectMethods, NELEM(gRenderEffectMethods));
179 return 0;
180 }
181