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 "ColorFilter.h"
18 #include "GraphicsJNI.h"
19 #include "SkBlendMode.h"
20 #include "SkImageFilter.h"
21 #include "SkImageFilters.h"
22 #include "graphics_jni_helpers.h"
23 #include "utils/Blur.h"
24
25 using namespace android::uirenderer;
26
createOffsetEffect(JNIEnv * env,jobject,jfloat offsetX,jfloat offsetY,jlong inputFilterHandle)27 static jlong createOffsetEffect(
28 JNIEnv* env,
29 jobject,
30 jfloat offsetX,
31 jfloat offsetY,
32 jlong inputFilterHandle
33 ) {
34 auto* inputFilter = reinterpret_cast<const SkImageFilter*>(inputFilterHandle);
35 sk_sp<SkImageFilter> offset = SkImageFilters::Offset(offsetX, offsetY, sk_ref_sp(inputFilter));
36 return reinterpret_cast<jlong>(offset.release());
37 }
38
createBlurEffect(JNIEnv * env,jobject,jfloat radiusX,jfloat radiusY,jlong inputFilterHandle,jint edgeTreatment)39 static jlong createBlurEffect(JNIEnv* env , jobject, jfloat radiusX,
40 jfloat radiusY, jlong inputFilterHandle, jint edgeTreatment) {
41 auto* inputImageFilter = reinterpret_cast<SkImageFilter*>(inputFilterHandle);
42 sk_sp<SkImageFilter> blurFilter =
43 SkImageFilters::Blur(
44 Blur::convertRadiusToSigma(radiusX),
45 Blur::convertRadiusToSigma(radiusY),
46 static_cast<SkTileMode>(edgeTreatment),
47 sk_ref_sp(inputImageFilter),
48 nullptr);
49 return reinterpret_cast<jlong>(blurFilter.release());
50 }
51
createBitmapEffect(JNIEnv * env,jobject,jlong bitmapHandle,jfloat srcLeft,jfloat srcTop,jfloat srcRight,jfloat srcBottom,jfloat dstLeft,jfloat dstTop,jfloat dstRight,jfloat dstBottom)52 static jlong createBitmapEffect(
53 JNIEnv* env,
54 jobject,
55 jlong bitmapHandle,
56 jfloat srcLeft,
57 jfloat srcTop,
58 jfloat srcRight,
59 jfloat srcBottom,
60 jfloat dstLeft,
61 jfloat dstTop,
62 jfloat dstRight,
63 jfloat dstBottom
64 ) {
65 sk_sp<SkImage> image = android::bitmap::toBitmap(bitmapHandle).makeImage();
66 SkRect srcRect = SkRect::MakeLTRB(srcLeft, srcTop, srcRight, srcBottom);
67 SkRect dstRect = SkRect::MakeLTRB(dstLeft, dstTop, dstRight, dstBottom);
68 sk_sp<SkImageFilter> bitmapFilter = SkImageFilters::Image(
69 image, srcRect, dstRect, SkSamplingOptions(SkFilterMode::kLinear));
70 return reinterpret_cast<jlong>(bitmapFilter.release());
71 }
72
createColorFilterEffect(JNIEnv * env,jobject,jlong colorFilterHandle,jlong inputFilterHandle)73 static jlong createColorFilterEffect(
74 JNIEnv* env,
75 jobject,
76 jlong colorFilterHandle,
77 jlong inputFilterHandle
78 ) {
79 auto colorFilter = android::uirenderer::ColorFilter::fromJava(colorFilterHandle);
80 auto skColorFilter =
81 colorFilter != nullptr ? colorFilter->getInstance() : sk_sp<SkColorFilter>();
82 auto* inputFilter = reinterpret_cast<const SkImageFilter*>(inputFilterHandle);
83 sk_sp<SkImageFilter> colorFilterImageFilter =
84 SkImageFilters::ColorFilter(skColorFilter, sk_ref_sp(inputFilter), nullptr);
85 return reinterpret_cast<jlong>(colorFilterImageFilter.release());
86 }
87
createBlendModeEffect(JNIEnv * env,jobject,jlong backgroundImageFilterHandle,jlong foregroundImageFilterHandle,jint blendmodeHandle)88 static jlong createBlendModeEffect(
89 JNIEnv* env,
90 jobject,
91 jlong backgroundImageFilterHandle,
92 jlong foregroundImageFilterHandle,
93 jint blendmodeHandle
94 ) {
95 auto* backgroundFilter = reinterpret_cast<const SkImageFilter*>(backgroundImageFilterHandle);
96 auto* foregroundFilter = reinterpret_cast<const SkImageFilter*>(foregroundImageFilterHandle);
97 SkBlendMode blendMode = static_cast<SkBlendMode>(blendmodeHandle);
98 sk_sp<SkImageFilter> xfermodeFilter = SkImageFilters::Blend(
99 blendMode,
100 sk_ref_sp(backgroundFilter),
101 sk_ref_sp(foregroundFilter)
102 );
103 return reinterpret_cast<jlong>(xfermodeFilter.release());
104 }
105
createChainEffect(JNIEnv * env,jobject,jlong outerFilterHandle,jlong innerFilterHandle)106 static jlong createChainEffect(
107 JNIEnv* env,
108 jobject,
109 jlong outerFilterHandle,
110 jlong innerFilterHandle
111 ) {
112 auto* outerImageFilter = reinterpret_cast<const SkImageFilter*>(outerFilterHandle);
113 auto* innerImageFilter = reinterpret_cast<const SkImageFilter*>(innerFilterHandle);
114 sk_sp<SkImageFilter> composeFilter = SkImageFilters::Compose(
115 sk_ref_sp(outerImageFilter),
116 sk_ref_sp(innerImageFilter)
117 );
118 return reinterpret_cast<jlong>(composeFilter.release());
119 }
120
createShaderEffect(JNIEnv * env,jobject,jlong shaderHandle)121 static jlong createShaderEffect(
122 JNIEnv* env,
123 jobject,
124 jlong shaderHandle
125 ) {
126 auto* shader = reinterpret_cast<const SkShader*>(shaderHandle);
127 sk_sp<SkImageFilter> shaderFilter = SkImageFilters::Shader(
128 sk_ref_sp(shader), nullptr
129 );
130 return reinterpret_cast<jlong>(shaderFilter.release());
131 }
132
ThrowIAEFmt(JNIEnv * env,const char * fmt,...)133 static inline int ThrowIAEFmt(JNIEnv* env, const char* fmt, ...) {
134 va_list args;
135 va_start(args, fmt);
136 int ret = jniThrowExceptionFmt(env, "java/lang/IllegalArgumentException", fmt, args);
137 va_end(args);
138 return ret;
139 }
140
createRuntimeShaderEffect(JNIEnv * env,jobject,jlong shaderBuilderHandle,jstring inputShaderName)141 static jlong createRuntimeShaderEffect(JNIEnv* env, jobject, jlong shaderBuilderHandle,
142 jstring inputShaderName) {
143 SkRuntimeShaderBuilder* builder =
144 reinterpret_cast<SkRuntimeShaderBuilder*>(shaderBuilderHandle);
145 ScopedUtfChars name(env, inputShaderName);
146
147 if (builder->child(name.c_str()).fChild == nullptr) {
148 ThrowIAEFmt(env,
149 "unable to find a uniform with the name '%s' of the correct "
150 "type defined by the provided RuntimeShader",
151 name.c_str());
152 return 0;
153 }
154
155 sk_sp<SkImageFilter> filter = SkImageFilters::RuntimeShader(*builder, name.c_str(), nullptr);
156 return reinterpret_cast<jlong>(filter.release());
157 }
158
RenderEffect_safeUnref(SkImageFilter * filter)159 static void RenderEffect_safeUnref(SkImageFilter* filter) {
160 SkSafeUnref(filter);
161 }
162
getRenderEffectFinalizer(JNIEnv *,jobject)163 static jlong getRenderEffectFinalizer(JNIEnv*, jobject) {
164 return static_cast<jlong>(reinterpret_cast<uintptr_t>(&RenderEffect_safeUnref));
165 }
166
167 static const JNINativeMethod gRenderEffectMethods[] = {
168 {"nativeGetFinalizer", "()J", (void*)getRenderEffectFinalizer},
169 {"nativeCreateOffsetEffect", "(FFJ)J", (void*)createOffsetEffect},
170 {"nativeCreateBlurEffect", "(FFJI)J", (void*)createBlurEffect},
171 {"nativeCreateBitmapEffect", "(JFFFFFFFF)J", (void*)createBitmapEffect},
172 {"nativeCreateColorFilterEffect", "(JJ)J", (void*)createColorFilterEffect},
173 {"nativeCreateBlendModeEffect", "(JJI)J", (void*)createBlendModeEffect},
174 {"nativeCreateChainEffect", "(JJ)J", (void*)createChainEffect},
175 {"nativeCreateShaderEffect", "(J)J", (void*)createShaderEffect},
176 {"nativeCreateRuntimeShaderEffect", "(JLjava/lang/String;)J",
177 (void*)createRuntimeShaderEffect}};
178
register_android_graphics_RenderEffect(JNIEnv * env)179 int register_android_graphics_RenderEffect(JNIEnv* env) {
180 android::RegisterMethodsOrDie(env, "android/graphics/RenderEffect",
181 gRenderEffectMethods, NELEM(gRenderEffectMethods));
182 return 0;
183 }
184