Flutter Engine
The Flutter Engine
Gradients.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2021 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include <jni.h>
9
11#include "include/core/SkM44.h"
14
15namespace {
16
17// Helper for common gradient data access.
18class GradientData {
19public:
20 GradientData(JNIEnv* env, const jfloatArray& jcolors, const jfloatArray& jpos,
21 jint jtm, jlong native_lm)
22 : fEnv(env)
23 , fJColors(jcolors)
24 , fJPos(jpos)
25 , fColors(env->GetFloatArrayElements(jcolors, nullptr))
26 , fPos(env->GetFloatArrayElements(jpos, nullptr))
27 , fCount(env->GetArrayLength(jpos))
28 , fTileMode(jetski::utils::TileMode(jtm))
29 , fLocalMatrix(native_lm ? reinterpret_cast<const SkM44*>(native_lm)->asM33() : SkMatrix())
30 {
31 SkASSERT(env->GetArrayLength(jcolors) == 4*fCount);
32 }
33
34 ~GradientData() {
35 fEnv->ReleaseFloatArrayElements(fJPos, fPos, 0);
36 fEnv->ReleaseFloatArrayElements(fJColors, fColors, 0);
37 }
38
39 int count() const { return fCount; }
40
41 const SkColor4f* colors() const { return reinterpret_cast<const SkColor4f*>(fColors); }
42 const float* pos() const { return fPos; }
43 const SkTileMode& tileMode() const { return fTileMode; }
44 const SkMatrix& localMatrix() const { return fLocalMatrix; }
45
46private:
47 JNIEnv* fEnv;
48 const jfloatArray& fJColors;
49 const jfloatArray& fJPos;
50 float* fColors;
51 float* fPos;
52 const int fCount;
53 const SkTileMode fTileMode;
54 const SkMatrix fLocalMatrix;
55};
56
57static jlong MakeLinear(JNIEnv* env, jobject, jfloat x0, jfloat y0, jfloat x1, jfloat y1,
58 jfloatArray jcolors, jfloatArray jpos, jint jtm, jlong native_lm) {
59 const GradientData gdata(env, jcolors, jpos, jtm, native_lm);
60 const SkPoint pts[] = {{x0, y0}, {x1, y1}};
61
62 auto shader = SkGradientShader::MakeLinear(pts,
63 gdata.colors(),
64 nullptr,
65 gdata.pos(),
66 gdata.count(),
67 gdata.tileMode(),
68 0,
69 &gdata.localMatrix());
70
71 return reinterpret_cast<jlong>(shader.release());
72}
73
74static jlong MakeRadial(JNIEnv* env, jobject, jfloat x, jfloat y, jfloat r,
75 jfloatArray jcolors, jfloatArray jpos, jint jtm, jlong native_lm) {
76 const GradientData gdata(env, jcolors, jpos, jtm, native_lm);
77
78 auto shader = SkGradientShader::MakeRadial({x,y}, r,
79 gdata.colors(),
80 nullptr,
81 gdata.pos(),
82 gdata.count(),
83 gdata.tileMode(),
84 0,
85 &gdata.localMatrix());
86
87 return reinterpret_cast<jlong>(shader.release());
88}
89
90static jlong MakeTwoPointConical(JNIEnv* env, jobject,
91 jfloat x0, jfloat y0, jfloat r0,
92 jfloat x1, jfloat y1, jfloat r1,
93 jfloatArray jcolors, jfloatArray jpos, jint jtm, jlong native_lm) {
94 const GradientData gdata(env, jcolors, jpos, jtm, native_lm);
95
96 auto shader = SkGradientShader::MakeTwoPointConical({x0,y0}, r0,
97 {x1,y1}, r1,
98 gdata.colors(),
99 nullptr,
100 gdata.pos(),
101 gdata.count(),
102 gdata.tileMode(),
103 0,
104 &gdata.localMatrix());
105
106 return reinterpret_cast<jlong>(shader.release());
107}
108
109static jlong MakeSweep(JNIEnv* env, jobject, jfloat x, jfloat y, jfloat sa, jfloat ea,
110 jfloatArray jcolors, jfloatArray jpos, jint jtm, jlong native_lm) {
111 const GradientData gdata(env, jcolors, jpos, jtm, native_lm);
112
113 auto shader = SkGradientShader::MakeSweep(x, y,
114 gdata.colors(),
115 nullptr,
116 gdata.pos(),
117 gdata.count(),
118 gdata.tileMode(),
119 sa, ea,
120 0,
121 &gdata.localMatrix());
122
123 return reinterpret_cast<jlong>(shader.release());
124}
125
126} // namespace
127
129 static const JNINativeMethod methods[] = {
130 {"nMakeLinear", "(FFFF[F[FIJ)J", reinterpret_cast<void*>(MakeLinear)},
131 };
132
133 const auto clazz = env->FindClass("org/skia/jetski/LinearGradient");
134 return clazz
135 ? env->RegisterNatives(clazz, methods, std::size(methods))
136 : JNI_ERR;
137}
138
140 static const JNINativeMethod methods[] = {
141 {"nMakeRadial", "(FFF[F[FIJ)J", reinterpret_cast<void*>(MakeRadial)},
142 };
143
144 const auto clazz = env->FindClass("org/skia/jetski/RadialGradient");
145 return clazz
146 ? env->RegisterNatives(clazz, methods, std::size(methods))
147 : JNI_ERR;
148}
149
151 static const JNINativeMethod methods[] = {
152 {"nMakeTwoPointConical", "(FFFFFF[F[FIJ)J", reinterpret_cast<void*>(MakeTwoPointConical)},
153 };
154
155 const auto clazz = env->FindClass("org/skia/jetski/TwoPointConicalGradient");
156 return clazz
157 ? env->RegisterNatives(clazz, methods, std::size(methods))
158 : JNI_ERR;
159}
160
162 static const JNINativeMethod methods[] = {
163 {"nMakeSweep", "(FFFF[F[FIJ)J", reinterpret_cast<void*>(MakeSweep)},
164 };
165
166 const auto clazz = env->FindClass("org/skia/jetski/SweepGradient");
167 return clazz
168 ? env->RegisterNatives(clazz, methods, std::size(methods))
169 : JNI_ERR;
170}
int count
Definition: FontMgrTest.cpp:50
static sk_sp< SkShader > MakeSweep(const SkPoint pts[2], const GradData &data, SkTileMode tm, float scale)
Ignores scale.
static sk_sp< SkShader > MakeLinear(const SkPoint pts[2], const GradData &data, SkTileMode tm, float scale)
Ignores scale.
static sk_sp< SkShader > MakeRadial(const SkPoint pts[2], const GradData &data, SkTileMode tm, float scale)
int register_jetski_RadialGradient(JNIEnv *env)
Definition: Gradients.cpp:139
int register_jetski_TwoPointConicalGradient(JNIEnv *env)
Definition: Gradients.cpp:150
int register_jetski_LinearGradient(JNIEnv *env)
Definition: Gradients.cpp:128
int register_jetski_SweepGradient(JNIEnv *env)
Definition: Gradients.cpp:161
SkPoint pos
#define SkASSERT(cond)
Definition: SkAssert.h:116
SkTileMode
Definition: SkTileMode.h:13
static sk_sp< SkShader > MakeTwoPointConical(const SkPoint &start, SkScalar startRadius, const SkPoint &end, SkScalar endRadius, const SkColor colors[], const SkScalar pos[], int count, SkTileMode mode, uint32_t flags=0, const SkMatrix *localMatrix=nullptr)
static sk_sp< SkShader > MakeSweep(SkScalar cx, SkScalar cy, const SkColor colors[], const SkScalar pos[], int count, SkTileMode mode, SkScalar startAngle, SkScalar endAngle, uint32_t flags, const SkMatrix *localMatrix)
static sk_sp< SkShader > MakeRadial(const SkPoint &center, SkScalar radius, const SkColor colors[], const SkScalar pos[], int count, SkTileMode mode, uint32_t flags=0, const SkMatrix *localMatrix=nullptr)
static sk_sp< SkShader > MakeLinear(const SkPoint pts[2], const SkColor colors[], const SkScalar pos[], int count, SkTileMode mode, uint32_t flags=0, const SkMatrix *localMatrix=nullptr)
Definition: SkM44.h:150
double y
double x
PODArray< SkColor > colors
Definition: SkRecords.h:276
Definition: __init__.py:1
it will be possible to load the file into Perfetto s trace viewer disable asset Prevents usage of any non test fonts unless they were explicitly Loaded via prefetched default font Indicates whether the embedding started a prefetch of the default font manager before creating the engine run In non interactive keep the shell running after the Dart script has completed enable serial On low power devices with low core running concurrent GC tasks on threads can cause them to contend with the UI thread which could potentially lead to jank This option turns off all concurrent GC activities domain network JSON encoded network policy per domain This overrides the DisallowInsecureConnections switch Embedder can specify whether to allow or disallow insecure connections at a domain level old gen heap size
Definition: switches.h:259
SkTileMode TileMode(jint tm)
Definition: Utils.cpp:26
Definition: Utils.cpp:10
Definition: utils.py:1