skia_util.cc 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "ui/gfx/skia_util.h"
  5. #include "base/check.h"
  6. #include "base/numerics/safe_conversions.h"
  7. #include "third_party/skia/include/core/SkBitmap.h"
  8. namespace gfx {
  9. bool BitmapsAreEqual(const SkBitmap& bitmap1, const SkBitmap& bitmap2) {
  10. if (bitmap1.isNull() != bitmap2.isNull() ||
  11. bitmap1.dimensions() != bitmap2.dimensions())
  12. return false;
  13. if (bitmap1.getGenerationID() == bitmap2.getGenerationID() ||
  14. (bitmap1.empty() && bitmap2.empty()))
  15. return true;
  16. // Calling getAddr32() on null or empty bitmaps will assert. The conditions
  17. // above should return early if either bitmap is empty or null.
  18. DCHECK(!bitmap1.isNull() && !bitmap2.isNull());
  19. DCHECK(!bitmap1.empty() && !bitmap2.empty());
  20. void* addr1 = bitmap1.getAddr32(0, 0);
  21. void* addr2 = bitmap2.getAddr32(0, 0);
  22. size_t size1 = bitmap1.computeByteSize();
  23. size_t size2 = bitmap2.computeByteSize();
  24. return (size1 == size2) && (0 == memcmp(addr1, addr2, size1));
  25. }
  26. // We treat HarfBuzz ints as 16.16 fixed-point.
  27. static const int kHbUnit1 = 1 << 16;
  28. int SkiaScalarToHarfBuzzUnits(SkScalar value) {
  29. return base::saturated_cast<int>(value * kHbUnit1);
  30. }
  31. SkScalar HarfBuzzUnitsToSkiaScalar(int value) {
  32. static const SkScalar kSkToHbRatio = SK_Scalar1 / kHbUnit1;
  33. return kSkToHbRatio * value;
  34. }
  35. float HarfBuzzUnitsToFloat(int value) {
  36. static const float kFloatToHbRatio = 1.0f / kHbUnit1;
  37. return kFloatToHbRatio * value;
  38. }
  39. } // namespace gfx