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