• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
10 #include <string>
11 #include "include/core/SkBlendMode.h"
12 #include "include/core/SkSamplingOptions.h"
13 #include "include/core/SkTileMode.h"
14 
15 namespace androidkit {
16 namespace utils {
17 
18 // RAII helper for jstring -> cstring conversions
19 class CString {
20 public:
CString(JNIEnv * env,const jstring & jstr)21     CString(JNIEnv* env, const jstring& jstr)
22         : fEnv(env)
23         , fJString(jstr)
24         , fCString(env->GetStringUTFChars(jstr, nullptr))
25     {}
26 
~CString()27     ~CString() {
28         fEnv->ReleaseStringUTFChars(fJString, fCString);
29     }
30 
31     operator const char*() const { return fCString; }
32 
33 private:
34     JNIEnv*        fEnv;
35     const jstring& fJString;
36     const char*    fCString;
37 
38 
39     CString(CString&&) = delete;
40     CString(const CString&) = delete;
41     CString& operator=(CString&&) = delete;
42     CString& operator=(const CString&) = delete;
43 };
44 
45 // RAII helper for jstring -> u16String conversions
46 class U16String {
47 public:
U16String(JNIEnv * env,const jstring & jstr)48     U16String(JNIEnv* env, const jstring& jstr)
49         : fEnv(env)
50         , fJString(jstr)
51         , fU16String(env->GetStringChars(jstr, nullptr))
52     {}
53 
~U16String()54     ~U16String() {
55         fEnv->ReleaseStringChars(fJString, fU16String);
56     }
57 
58     operator const char16_t*() const { return reinterpret_cast<const char16_t*>(fU16String); }
59 
60 private:
61     JNIEnv*        fEnv;
62     const jstring& fJString;
63     const jchar*   fU16String;
64 
65 
66     U16String(U16String&&) = delete;
67     U16String(const U16String&) = delete;
68     U16String& operator=(U16String&&) = delete;
69     U16String& operator=(const U16String&) = delete;
70 };
71 
72 // RAII helper for float array access
73 class CFloats {
74 public:
CFloats(JNIEnv * env,const jfloatArray & jfloats)75     CFloats(JNIEnv* env, const jfloatArray& jfloats)
76         : fEnv(env)
77         , fJFloats(jfloats)
78         , fCFloats(env->GetFloatArrayElements(jfloats, nullptr))
79     {}
80 
~CFloats()81     ~CFloats() {
82         fEnv->ReleaseFloatArrayElements(fJFloats, fCFloats, 0);
83     }
84 
85     operator const float*() const { return fCFloats; }
86 
87 private:
88     JNIEnv*            fEnv;
89     const jfloatArray& fJFloats;
90     float*             fCFloats;
91 
92 
93     CFloats(CFloats&&) = delete;
94     CFloats(const CFloats&) = delete;
95     CFloats& operator=(CFloats&&) = delete;
96     CFloats& operator=(const CFloats&) = delete;
97 };
98 
99 SkSamplingOptions SamplingOptions(jint, jfloat, jfloat);
100 SkTileMode TileMode(jint);
101 SkBlendMode BlendMode(jint);
102 
103 } // namespace utils
104 } // namespace androidkit
105