skia_util.cc 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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/node_includes.h"
  12. #include "shell/common/skia_util.h"
  13. #include "shell/common/thread_restrictions.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::util {
  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 AddImageSkiaRepFromPNG(gfx::ImageSkia* image,
  50. const unsigned char* data,
  51. size_t size,
  52. double scale_factor) {
  53. SkBitmap bitmap;
  54. if (!gfx::PNGCodec::Decode(data, size, &bitmap))
  55. return false;
  56. image->AddRepresentation(gfx::ImageSkiaRep(bitmap, scale_factor));
  57. return true;
  58. }
  59. bool AddImageSkiaRepFromJPEG(gfx::ImageSkia* image,
  60. const unsigned char* data,
  61. size_t size,
  62. double scale_factor) {
  63. auto bitmap = gfx::JPEGCodec::Decode(data, size);
  64. if (!bitmap)
  65. return false;
  66. // `JPEGCodec::Decode()` doesn't tell `SkBitmap` instance it creates
  67. // that all of its pixels are opaque, that's why the bitmap gets
  68. // an alpha type `kPremul_SkAlphaType` instead of `kOpaque_SkAlphaType`.
  69. // Let's fix it here.
  70. // TODO(alexeykuzmin): This workaround should be removed
  71. // when the `JPEGCodec::Decode()` code is fixed.
  72. // See https://github.com/electron/electron/issues/11294.
  73. bitmap->setAlphaType(SkAlphaType::kOpaque_SkAlphaType);
  74. image->AddRepresentation(gfx::ImageSkiaRep(*bitmap, scale_factor));
  75. return true;
  76. }
  77. bool AddImageSkiaRepFromBuffer(gfx::ImageSkia* image,
  78. const unsigned char* data,
  79. size_t size,
  80. int width,
  81. int height,
  82. double scale_factor) {
  83. // Try PNG first.
  84. if (AddImageSkiaRepFromPNG(image, data, size, scale_factor))
  85. return true;
  86. // Try JPEG second.
  87. if (AddImageSkiaRepFromJPEG(image, data, size, scale_factor))
  88. return true;
  89. if (width == 0 || height == 0)
  90. return false;
  91. auto info = SkImageInfo::MakeN32(width, height, kPremul_SkAlphaType);
  92. if (size < info.computeMinByteSize())
  93. return false;
  94. SkBitmap bitmap;
  95. bitmap.allocN32Pixels(width, height, false);
  96. bitmap.writePixels({info, data, bitmap.rowBytes()});
  97. image->AddRepresentation(gfx::ImageSkiaRep(bitmap, scale_factor));
  98. return true;
  99. }
  100. bool AddImageSkiaRepFromPath(gfx::ImageSkia* image,
  101. const base::FilePath& path,
  102. double scale_factor) {
  103. std::string file_contents;
  104. {
  105. electron::ScopedAllowBlockingForElectron allow_blocking;
  106. if (!asar::ReadFileToString(path, &file_contents))
  107. return false;
  108. }
  109. const auto* data =
  110. reinterpret_cast<const unsigned char*>(file_contents.data());
  111. size_t size = file_contents.size();
  112. return AddImageSkiaRepFromBuffer(image, data, size, 0, 0, scale_factor);
  113. }
  114. bool PopulateImageSkiaRepsFromPath(gfx::ImageSkia* image,
  115. const base::FilePath& path) {
  116. bool succeed = false;
  117. std::string filename(path.BaseName().RemoveExtension().AsUTF8Unsafe());
  118. if (base::MatchPattern(filename, "*@*x"))
  119. // Don't search for other representations if the DPI has been specified.
  120. return AddImageSkiaRepFromPath(image, path, GetScaleFactorFromPath(path));
  121. else
  122. succeed |= AddImageSkiaRepFromPath(image, path, 1.0f);
  123. for (const ScaleFactorPair& pair : kScaleFactorPairs)
  124. succeed |= AddImageSkiaRepFromPath(
  125. image, path.InsertBeforeExtensionASCII(pair.name), pair.scale);
  126. return succeed;
  127. }
  128. #if BUILDFLAG(IS_WIN)
  129. bool ReadImageSkiaFromICO(gfx::ImageSkia* image, HICON icon) {
  130. // Convert the icon from the Windows specific HICON to gfx::ImageSkia.
  131. SkBitmap bitmap = IconUtil::CreateSkBitmapFromHICON(icon);
  132. if (bitmap.isNull())
  133. return false;
  134. image->AddRepresentation(gfx::ImageSkiaRep(bitmap, 1.0f));
  135. return true;
  136. }
  137. #endif
  138. } // namespace electron::util