blob: f3e33417f1d75c2b40175f23b17422884ffe8466 [file] [log] [blame]
reed@android.com8a1c16f2008-12-17 15:59:43 +00001/*
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*/
29void 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*/
36void 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
64inline int SkUTF8_CountUTF8Bytes(const char utf8[])
65{
66 SkASSERT(utf8);
67 return SkUTF8_LeadByteToCount(*(const uint8_t*)utf8);
68}
69
70int SkUTF8_CountUnichars(const char utf8[]);
71int SkUTF8_CountUnichars(const char utf8[], size_t byteLength);
72SkUnichar SkUTF8_ToUnichar(const char utf8[]);
73SkUnichar SkUTF8_NextUnichar(const char**);
74SkUnichar 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*/
80size_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
87int SkUTF16_CountUnichars(const uint16_t utf16[]);
88int SkUTF16_CountUnichars(const uint16_t utf16[],
89 int numberOf16BitValues);
90// returns the current unichar and then moves past it (*p++)
91SkUnichar SkUTF16_NextUnichar(const uint16_t**);
92// this guy backs up to the previus unichar value, and returns it (*--p)
93SkUnichar SkUTF16_PrevUnichar(const uint16_t**);
94size_t SkUTF16_FromUnichar(SkUnichar uni, uint16_t utf16[] = NULL);
95
96size_t SkUTF16_ToUTF8(const uint16_t utf16[], int numberOf16BitValues,
97 char utf8[] = NULL);
98
99class SkUtils {
100public:
101#ifdef SK_DEBUG
102 static void UnitTest();
103#endif
104};
105
106///////////////////////////////////////////////////////////////////////////////
107
108class SkAutoTrace {
109public:
110 /** NOTE: label contents are not copied, just the ptr is
111 retained, so DON'T DELETE IT.
112 */
113 SkAutoTrace(const char label[]) : fLabel(label) {
114 SkDebugf("--- trace: %s Enter\n", fLabel);
115 }
116 ~SkAutoTrace() {
117 SkDebugf("--- trace: %s Leave\n", fLabel);
118 }
119private:
120 const char* fLabel;
121};
122
123///////////////////////////////////////////////////////////////////////////////
124
125class SkAutoMemoryUsageProbe {
126public:
127 /** Record memory usage in constructor, and dump the result
128 (delta and current total) in the destructor, with the optional
129 label. NOTE: label contents are not copied, just the ptr is
130 retained, so DON'T DELETE IT.
131 */
132 SkAutoMemoryUsageProbe(const char label[]);
133 ~SkAutoMemoryUsageProbe();
134private:
135 const char* fLabel;
136 size_t fBytesAllocated;
137};
138
139#endif
140