Home | History | Annotate | Download | only in core
      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 typedef void (*SkMemset16Proc)(uint16_t dst[], uint16_t value, int count);
     31 SkMemset16Proc SkMemset16GetPlatformProc();
     32 
     33 /** Similar to memset(), but it assigns a 32bit value into the buffer.
     34     @param buffer   The memory to have value copied into it
     35     @param value    The 32bit value to be copied into buffer
     36     @param count    The number of times value should be copied into the buffer.
     37 */
     38 void sk_memset32_portable(uint32_t dst[], uint32_t value, int count);
     39 typedef void (*SkMemset32Proc)(uint32_t dst[], uint32_t value, int count);
     40 SkMemset32Proc SkMemset32GetPlatformProc();
     41 
     42 #ifdef ANDROID
     43     #include "cutils/memory.h"
     44 
     45     #define sk_memset16(dst, value, count)    android_memset16(dst, value, (count) << 1)
     46     #define sk_memset32(dst, value, count)    android_memset32(dst, value, (count) << 2)
     47 #endif
     48 
     49 #ifndef sk_memset16
     50 extern SkMemset16Proc sk_memset16;
     51 #endif
     52 
     53 #ifndef sk_memset32
     54 extern SkMemset32Proc sk_memset32;
     55 #endif
     56 
     57 ///////////////////////////////////////////////////////////////////////////
     58 
     59 #define kMaxBytesInUTF8Sequence     4
     60 
     61 #ifdef SK_DEBUG
     62     int SkUTF8_LeadByteToCount(unsigned c);
     63 #else
     64     #define SkUTF8_LeadByteToCount(c)   ((((0xE5 << 24) >> ((unsigned)c >> 4 << 1)) & 3) + 1)
     65 #endif
     66 
     67 inline int SkUTF8_CountUTF8Bytes(const char utf8[])
     68 {
     69     SkASSERT(utf8);
     70     return SkUTF8_LeadByteToCount(*(const uint8_t*)utf8);
     71 }
     72 
     73 int         SkUTF8_CountUnichars(const char utf8[]);
     74 int         SkUTF8_CountUnichars(const char utf8[], size_t byteLength);
     75 SkUnichar   SkUTF8_ToUnichar(const char utf8[]);
     76 SkUnichar   SkUTF8_NextUnichar(const char**);
     77 SkUnichar   SkUTF8_PrevUnichar(const char**);
     78 
     79 /** Return the number of bytes need to convert a unichar
     80     into a utf8 sequence. Will be 1..kMaxBytesInUTF8Sequence,
     81     or 0 if uni is illegal.
     82 */
     83 size_t      SkUTF8_FromUnichar(SkUnichar uni, char utf8[] = NULL);
     84 
     85 ///////////////////////////////////////////////////////////////////////////////
     86 
     87 #define SkUTF16_IsHighSurrogate(c)  (((c) & 0xFC00) == 0xD800)
     88 #define SkUTF16_IsLowSurrogate(c)   (((c) & 0xFC00) == 0xDC00)
     89 
     90 int SkUTF16_CountUnichars(const uint16_t utf16[]);
     91 int SkUTF16_CountUnichars(const uint16_t utf16[],
     92                                   int numberOf16BitValues);
     93 // returns the current unichar and then moves past it (*p++)
     94 SkUnichar SkUTF16_NextUnichar(const uint16_t**);
     95 // this guy backs up to the previus unichar value, and returns it (*--p)
     96 SkUnichar SkUTF16_PrevUnichar(const uint16_t**);
     97 size_t SkUTF16_FromUnichar(SkUnichar uni, uint16_t utf16[] = NULL);
     98 
     99 size_t SkUTF16_ToUTF8(const uint16_t utf16[], int numberOf16BitValues,
    100                            char utf8[] = NULL);
    101 
    102 ///////////////////////////////////////////////////////////////////////////////
    103 
    104 class SkAutoTrace {
    105 public:
    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     }
    115 private:
    116     const char* fLabel;
    117 };
    118 
    119 ///////////////////////////////////////////////////////////////////////////////
    120 
    121 class SkAutoMemoryUsageProbe {
    122 public:
    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();
    130 private:
    131     const char* fLabel;
    132     size_t      fBytesAllocated;
    133 };
    134 
    135 #endif
    136 
    137