skia_util.cc 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. // Copyright (c) 2019 GitHub, Inc.
  2. // Use of this source code is governed by the MIT license that can be
  3. // found in the LICENSE file.
  4. #include <string>
  5. #include "base/files/file_util.h"
  6. #include "base/strings/pattern.h"
  7. #include "base/strings/string_util.h"
  8. #include "base/threading/thread_restrictions.h"
  9. #include "net/base/data_url.h"
  10. #include "shell/common/asar/asar_util.h"
  11. #include "shell/common/node_includes.h"
  12. #include "shell/common/skia_util.h"
  13. #include "third_party/skia/include/core/SkBitmap.h"
  14. #include "third_party/skia/include/core/SkImageInfo.h"
  15. #include "third_party/skia/include/core/SkPixelRef.h"
  16. #include "ui/gfx/codec/jpeg_codec.h"
  17. #include "ui/gfx/codec/png_codec.h"
  18. #include "ui/gfx/geometry/size.h"
  19. #include "ui/gfx/image/image_skia.h"
  20. #include "ui/gfx/image/image_skia_operations.h"
  21. #include "ui/gfx/image/image_util.h"
  22. #if defined(OS_WIN)
  23. #include "ui/gfx/icon_util.h"
  24. #endif
  25. namespace electron {
  26. namespace util {
  27. struct ScaleFactorPair {
  28. const char* name;
  29. float scale;
  30. };
  31. ScaleFactorPair kScaleFactorPairs[] = {
  32. // The "@2x" is put as first one to make scale matching faster.
  33. {"@2x", 2.0f}, {"@3x", 3.0f}, {"@1x", 1.0f}, {"@4x", 4.0f},
  34. {"@5x", 5.0f}, {"@1.25x", 1.25f}, {"@1.33x", 1.33f}, {"@1.4x", 1.4f},
  35. {"@1.5x", 1.5f}, {"@1.8x", 1.8f}, {"@2.5x", 2.5f},
  36. };
  37. float GetScaleFactorFromPath(const base::FilePath& path) {
  38. std::string filename(path.BaseName().RemoveExtension().AsUTF8Unsafe());
  39. // We don't try to convert string to float here because it is very very
  40. // expensive.
  41. for (const auto& kScaleFactorPair : kScaleFactorPairs) {
  42. if (base::EndsWith(filename, kScaleFactorPair.name,
  43. base::CompareCase::INSENSITIVE_ASCII))
  44. return kScaleFactorPair.scale;
  45. }
  46. return 1.0f;
  47. }
  48. bool AddImageSkiaRepFromPNG(gfx::ImageSkia* image,
  49. const unsigned char* data,
  50. size_t size,
  51. double scale_factor) {
  52. SkBitmap bitmap;
  53. if (!gfx::PNGCodec::Decode(data, size, &bitmap))
  54. return false;
  55. image->AddRepresentation(gfx::ImageSkiaRep(bitmap, scale_factor));
  56. return true;
  57. }
  58. bool AddImageSkiaRepFromJPEG(gfx::ImageSkia* image,
  59. const unsigned char* data,
  60. size_t size,
  61. double scale_factor) {
  62. auto bitmap = gfx::JPEGCodec::Decode(data, size);
  63. if (!bitmap)
  64. return false;
  65. // `JPEGCodec::Decode()` doesn't tell `SkBitmap` instance it creates
  66. // that all of its pixels are opaque, that's why the bitmap gets
  67. // an alpha type `kPremul_SkAlphaType` instead of `kOpaque_SkAlphaType`.
  68. // Let's fix it here.
  69. // TODO(alexeykuzmin): This workaround should be removed
  70. // when the `JPEGCodec::Decode()` code is fixed.
  71. // See https://github.com/electron/electron/issues/11294.
  72. bitmap->setAlphaType(SkAlphaType::kOpaque_SkAlphaType);
  73. image->AddRepresentation(gfx::ImageSkiaRep(*bitmap, scale_factor));
  74. return true;
  75. }
  76. bool AddImageSkiaRepFromBuffer(gfx::ImageSkia* image,
  77. const unsigned char* data,
  78. size_t size,
  79. int width,
  80. int height,
  81. double scale_factor) {
  82. // Try PNG first.
  83. if (AddImageSkiaRepFromPNG(image, data, size, scale_factor))
  84. return true;
  85. // Try JPEG second.
  86. if (AddImageSkiaRepFromJPEG(image, data, size, scale_factor))
  87. return true;
  88. if (width == 0 || height == 0)
  89. return false;
  90. auto info = SkImageInfo::MakeN32(width, height, kPremul_SkAlphaType);
  91. if (size < info.computeMinByteSize())
  92. return false;
  93. SkBitmap bitmap;
  94. bitmap.allocN32Pixels(width, height, false);
  95. bitmap.writePixels({info, data, bitmap.rowBytes()});
  96. image->AddRepresentation(gfx::ImageSkiaRep(bitmap, scale_factor));
  97. return true;
  98. }
  99. bool AddImageSkiaRepFromPath(gfx::ImageSkia* image,
  100. const base::FilePath& path,
  101. double scale_factor) {
  102. std::string file_contents;
  103. {
  104. base::ThreadRestrictions::ScopedAllowIO allow_io;
  105. if (!asar::ReadFileToString(path, &file_contents))
  106. return false;
  107. }
  108. const auto* data =
  109. reinterpret_cast<const unsigned char*>(file_contents.data());
  110. size_t size = file_contents.size();
  111. return AddImageSkiaRepFromBuffer(image, data, size, 0, 0, scale_factor);
  112. }
  113. bool PopulateImageSkiaRepsFromPath(gfx::ImageSkia* image,
  114. const base::FilePath& path) {
  115. bool succeed = false;
  116. std::string filename(path.BaseName().RemoveExtension().AsUTF8Unsafe());
  117. if (base::MatchPattern(filename, "*@*x"))
  118. // Don't search for other representations if the DPI has been specified.
  119. return AddImageSkiaRepFromPath(image, path, GetScaleFactorFromPath(path));
  120. else
  121. succeed |= AddImageSkiaRepFromPath(image, path, 1.0f);
  122. for (const ScaleFactorPair& pair : kScaleFactorPairs)
  123. succeed |= AddImageSkiaRepFromPath(
  124. image, path.InsertBeforeExtensionASCII(pair.name), pair.scale);
  125. return succeed;
  126. }
  127. #if defined(OS_WIN)
  128. bool ReadImageSkiaFromICO(gfx::ImageSkia* image, HICON icon) {
  129. // Convert the icon from the Windows specific HICON to gfx::ImageSkia.
  130. SkBitmap bitmap = IconUtil::CreateSkBitmapFromHICON(icon);
  131. if (bitmap.isNull())
  132. return false;
  133. image->AddRepresentation(gfx::ImageSkiaRep(bitmap, 1.0f));
  134. return true;
  135. }
  136. #endif
  137. } // namespace util
  138. } // namespace electron