1 /*
2 * Copyright (C) 2006 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
17 #ifndef SkUtils_DEFINED
18 #define SkUtils_DEFINED
19
20 #include "SkTypes.h"
21
22 ///////////////////////////////////////////////////////////////////////////
23
24 /** Similar to memset(), but it assigns a 16bit value into the buffer.
25 @param buffer The memory to have value copied into it
26 @param value The 16bit value to be copied into buffer
27 @param count The number of times value should be copied into the buffer.
28 */
29 void sk_memset16_portable(uint16_t dst[], uint16_t value, int count);
30
31 /** Similar to memset(), but it assigns a 32bit value into the buffer.
32 @param buffer The memory to have value copied into it
33 @param value The 32bit value to be copied into buffer
34 @param count The number of times value should be copied into the buffer.
35 */
36 void sk_memset32_portable(uint32_t dst[], uint32_t value, int count);
37
38 #ifdef ANDROID
39 #include "cutils/memory.h"
40
41 #define sk_memset16(dst, value, count) android_memset16(dst, value, (count) << 1)
42 #define sk_memset32(dst, value, count) android_memset32(dst, value, (count) << 2)
43 #endif
44
45 #ifndef sk_memset16
46 #define sk_memset16(dst, value, count) sk_memset16_portable(dst, value, count)
47 #endif
48
49 #ifndef sk_memset32
50 #define sk_memset32(dst, value, count) sk_memset32_portable(dst, value, count)
51 #endif
52
53
54 ///////////////////////////////////////////////////////////////////////////
55
56 #define kMaxBytesInUTF8Sequence 4
57
58 #ifdef SK_DEBUG
59 int SkUTF8_LeadByteToCount(unsigned c);
60 #else
61 #define SkUTF8_LeadByteToCount(c) ((((0xE5 << 24) >> ((unsigned)c >> 4 << 1)) & 3) + 1)
62 #endif
63
SkUTF8_CountUTF8Bytes(const char utf8[])64 inline int SkUTF8_CountUTF8Bytes(const char utf8[])
65 {
66 SkASSERT(utf8);
67 return SkUTF8_LeadByteToCount(*(const uint8_t*)utf8);
68 }
69
70 int SkUTF8_CountUnichars(const char utf8[]);
71 int SkUTF8_CountUnichars(const char utf8[], size_t byteLength);
72 SkUnichar SkUTF8_ToUnichar(const char utf8[]);
73 SkUnichar SkUTF8_NextUnichar(const char**);
74 SkUnichar SkUTF8_PrevUnichar(const char**);
75
76 /** Return the number of bytes need to convert a unichar
77 into a utf8 sequence. Will be 1..kMaxBytesInUTF8Sequence,
78 or 0 if uni is illegal.
79 */
80 size_t SkUTF8_FromUnichar(SkUnichar uni, char utf8[] = NULL);
81
82 ///////////////////////////////////////////////////////////////////////////////
83
84 #define SkUTF16_IsHighSurrogate(c) (((c) & 0xFC00) == 0xD800)
85 #define SkUTF16_IsLowSurrogate(c) (((c) & 0xFC00) == 0xDC00)
86
87 int SkUTF16_CountUnichars(const uint16_t utf16[]);
88 int SkUTF16_CountUnichars(const uint16_t utf16[],
89 int numberOf16BitValues);
90 // returns the current unichar and then moves past it (*p++)
91 SkUnichar SkUTF16_NextUnichar(const uint16_t**);
92 // this guy backs up to the previus unichar value, and returns it (*--p)
93 SkUnichar SkUTF16_PrevUnichar(const uint16_t**);
94 size_t SkUTF16_FromUnichar(SkUnichar uni, uint16_t utf16[] = NULL);
95
96 size_t SkUTF16_ToUTF8(const uint16_t utf16[], int numberOf16BitValues,
97 char utf8[] = NULL);
98
99 ///////////////////////////////////////////////////////////////////////////////
100
101 class SkAutoTrace {
102 public:
103 /** NOTE: label contents are not copied, just the ptr is
104 retained, so DON'T DELETE IT.
105 */
SkAutoTrace(const char label[])106 SkAutoTrace(const char label[]) : fLabel(label) {
107 SkDebugf("--- trace: %s Enter\n", fLabel);
108 }
~SkAutoTrace()109 ~SkAutoTrace() {
110 SkDebugf("--- trace: %s Leave\n", fLabel);
111 }
112 private:
113 const char* fLabel;
114 };
115
116 ///////////////////////////////////////////////////////////////////////////////
117
118 class SkAutoMemoryUsageProbe {
119 public:
120 /** Record memory usage in constructor, and dump the result
121 (delta and current total) in the destructor, with the optional
122 label. NOTE: label contents are not copied, just the ptr is
123 retained, so DON'T DELETE IT.
124 */
125 SkAutoMemoryUsageProbe(const char label[]);
126 ~SkAutoMemoryUsageProbe();
127 private:
128 const char* fLabel;
129 size_t fBytesAllocated;
130 };
131
132 #endif
133
134