skia_util.cc 5.4 KB

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