| Nader Jawad | 390d6e8 | 2020-09-24 21:35:03 -0700 | [diff] [blame] | 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 |  | 
|  | 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 |  | 
|  | 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 |  | 
|  | 51 | static void RenderEffect_safeUnref(SkImageFilter* filter) { | 
|  | 52 | SkSafeUnref(filter); | 
|  | 53 | } | 
|  | 54 |  | 
|  | 55 | static jlong getRenderEffectFinalizer(JNIEnv*, jobject) { | 
|  | 56 | return static_cast<jlong>(reinterpret_cast<uintptr_t>(&RenderEffect_safeUnref)); | 
|  | 57 | } | 
|  | 58 |  | 
|  | 59 | static const JNINativeMethod gRenderEffectMethods[] = { | 
|  | 60 | {"nativeGetFinalizer", "()J", (void*)getRenderEffectFinalizer}, | 
|  | 61 | {"nativeCreateOffsetEffect", "(FFJ)J", (void*)createOffsetEffect}, | 
|  | 62 | {"nativeCreateBlurEffect", "(FFJI)J", (void*)createBlurEffect} | 
|  | 63 | }; | 
|  | 64 |  | 
|  | 65 | int register_android_graphics_RenderEffect(JNIEnv* env) { | 
|  | 66 | android::RegisterMethodsOrDie(env, "android/graphics/RenderEffect", | 
|  | 67 | gRenderEffectMethods, NELEM(gRenderEffectMethods)); | 
|  | 68 | return 0; | 
|  | 69 | } |