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 #if defined(ANDROID) && !defined(SK_BUILD_FOR_ANDROID_NDK)
     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     SkASSERT(utf8);
     69     return SkUTF8_LeadByteToCount(*(const uint8_t*)utf8);
     70 }
     71 
     72 int         SkUTF8_CountUnichars(const char utf8[]);
     73 int         SkUTF8_CountUnichars(const char utf8[], size_t byteLength);
     74 SkUnichar   SkUTF8_ToUnichar(const char utf8[]);
     75 SkUnichar   SkUTF8_NextUnichar(const char**);
     76 SkUnichar   SkUTF8_PrevUnichar(const char**);
     77 
     78 /** Return the number of bytes need to convert a unichar
     79     into a utf8 sequence. Will be 1..kMaxBytesInUTF8Sequence,
     80     or 0 if uni is illegal.
     81 */
     82 size_t      SkUTF8_FromUnichar(SkUnichar uni, char utf8[] = NULL);
     83 
     84 ///////////////////////////////////////////////////////////////////////////////
     85 
     86 #define SkUTF16_IsHighSurrogate(c)  (((c) & 0xFC00) == 0xD800)
     87 #define SkUTF16_IsLowSurrogate(c)   (((c) & 0xFC00) == 0xDC00)
     88 
     89 int SkUTF16_CountUnichars(const uint16_t utf16[]);
     90 int SkUTF16_CountUnichars(const uint16_t utf16[],
     91                                   int numberOf16BitValues);
     92 // returns the current unichar and then moves past it (*p++)
     93 SkUnichar SkUTF16_NextUnichar(const uint16_t**);
     94 // this guy backs up to the previus unichar value, and returns it (*--p)
     95 SkUnichar SkUTF16_PrevUnichar(const uint16_t**);
     96 size_t SkUTF16_FromUnichar(SkUnichar uni, uint16_t utf16[] = NULL);
     97 
     98 size_t SkUTF16_ToUTF8(const uint16_t utf16[], int numberOf16BitValues,
     99                            char utf8[] = NULL);
    100 
    101 ///////////////////////////////////////////////////////////////////////////////
    102 
    103 class SkAutoTrace {
    104 public:
    105     /** NOTE: label contents are not copied, just the ptr is
    106         retained, so DON'T DELETE IT.
    107     */
    108     SkAutoTrace(const char label[]) : fLabel(label) {
    109         SkDebugf("--- trace: %s Enter\n", fLabel);
    110     }
    111     ~SkAutoTrace() {
    112         SkDebugf("--- trace: %s Leave\n", fLabel);
    113     }
    114 private:
    115     const char* fLabel;
    116 };
    117 
    118 ///////////////////////////////////////////////////////////////////////////////
    119 
    120 class SkAutoMemoryUsageProbe {
    121 public:
    122     /** Record memory usage in constructor, and dump the result
    123         (delta and current total) in the destructor, with the optional
    124         label. NOTE: label contents are not copied, just the ptr is
    125         retained, so DON'T DELETE IT.
    126     */
    127     SkAutoMemoryUsageProbe(const char label[]);
    128     ~SkAutoMemoryUsageProbe();
    129 private:
    130     const char* fLabel;
    131     size_t      fBytesAllocated;
    132 };
    133 
    134 #endif
    135 
    136