skia_util.cc 5.3 KB

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