SkUtils.h 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. * Copyright 2006 The Android Open Source Project
  3. *
  4. * Use of this source code is governed by a BSD-style license that can be
  5. * found in the LICENSE file.
  6. */
  7. #ifndef SkUtils_DEFINED
  8. #define SkUtils_DEFINED
  9. #include "include/core/SkFontTypes.h"
  10. #include "src/core/SkOpts.h"
  11. #include "src/utils/SkUTF.h"
  12. /** Similar to memset(), but it assigns a 16, 32, or 64-bit value into the buffer.
  13. @param buffer The memory to have value copied into it
  14. @param value The value to be copied into buffer
  15. @param count The number of times value should be copied into the buffer.
  16. */
  17. static inline void sk_memset16(uint16_t buffer[], uint16_t value, int count) {
  18. SkOpts::memset16(buffer, value, count);
  19. }
  20. static inline void sk_memset32(uint32_t buffer[], uint32_t value, int count) {
  21. SkOpts::memset32(buffer, value, count);
  22. }
  23. static inline void sk_memset64(uint64_t buffer[], uint64_t value, int count) {
  24. SkOpts::memset64(buffer, value, count);
  25. }
  26. ///////////////////////////////////////////////////////////////////////////////
  27. // Unlike the functions in SkUTF.h, these two functions do not take an array
  28. // length parameter. When possible, use SkUTF::NextUTF{8,16} instead.
  29. SkUnichar SkUTF8_NextUnichar(const char**);
  30. SkUnichar SkUTF16_NextUnichar(const uint16_t**);
  31. ///////////////////////////////////////////////////////////////////////////////
  32. static inline bool SkUTF16_IsLeadingSurrogate(uint16_t c) { return ((c) & 0xFC00) == 0xD800; }
  33. static inline bool SkUTF16_IsTrailingSurrogate (uint16_t c) { return ((c) & 0xFC00) == 0xDC00; }
  34. ///////////////////////////////////////////////////////////////////////////////
  35. static inline int SkUTFN_CountUnichars(SkTextEncoding enc, const void* utfN, size_t bytes) {
  36. switch (enc) {
  37. case SkTextEncoding::kUTF8: return SkUTF::CountUTF8((const char*)utfN, bytes);
  38. case SkTextEncoding::kUTF16: return SkUTF::CountUTF16((const uint16_t*)utfN, bytes);
  39. case SkTextEncoding::kUTF32: return SkUTF::CountUTF32((const int32_t*)utfN, bytes);
  40. default: SkDEBUGFAIL("unknown text encoding"); return -1;
  41. }
  42. }
  43. static inline SkUnichar SkUTFN_Next(SkTextEncoding enc, const void** ptr, const void* stop) {
  44. switch (enc) {
  45. case SkTextEncoding::kUTF8:
  46. return SkUTF::NextUTF8((const char**)ptr, (const char*)stop);
  47. case SkTextEncoding::kUTF16:
  48. return SkUTF::NextUTF16((const uint16_t**)ptr, (const uint16_t*)stop);
  49. case SkTextEncoding::kUTF32:
  50. return SkUTF::NextUTF32((const int32_t**)ptr, (const int32_t*)stop);
  51. default: SkDEBUGFAIL("unknown text encoding"); return -1;
  52. }
  53. }
  54. ///////////////////////////////////////////////////////////////////////////////
  55. namespace SkHexadecimalDigits {
  56. extern const char gUpper[16]; // 0-9A-F
  57. extern const char gLower[16]; // 0-9a-f
  58. }
  59. ///////////////////////////////////////////////////////////////////////////////
  60. // If T is an 8-byte GCC or Clang vector extension type, it would naturally
  61. // pass or return in the MMX mm0 register on 32-bit x86 builds. This has the
  62. // fun side effect of clobbering any state in the x87 st0 register. (There is
  63. // no ABI governing who should preserve mm?/st? registers, so no one does!)
  64. //
  65. // We force-inline sk_unaligned_load() and sk_unaligned_store() to avoid that,
  66. // making them safe to use for all types on all platforms, thus solving the
  67. // problem once and for all!
  68. template <typename T, typename P>
  69. static SK_ALWAYS_INLINE T sk_unaligned_load(const P* ptr) {
  70. // TODO: static_assert desirable things about T here so as not to be totally abused.
  71. T val;
  72. memcpy(&val, ptr, sizeof(val));
  73. return val;
  74. }
  75. template <typename T, typename P>
  76. static SK_ALWAYS_INLINE void sk_unaligned_store(P* ptr, T val) {
  77. // TODO: ditto
  78. memcpy(ptr, &val, sizeof(val));
  79. }
  80. #endif