nine_image_painter.cc 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. // Copyright 2014 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/nine_image_painter.h"
  5. #include <stddef.h>
  6. #include <limits>
  7. #include "base/numerics/safe_conversions.h"
  8. #include "cc/paint/paint_flags.h"
  9. #include "third_party/skia/include/core/SkRect.h"
  10. #include "third_party/skia/include/core/SkScalar.h"
  11. #include "ui/gfx/canvas.h"
  12. #include "ui/gfx/geometry/insets.h"
  13. #include "ui/gfx/geometry/rect.h"
  14. #include "ui/gfx/geometry/rect_conversions.h"
  15. #include "ui/gfx/geometry/skia_conversions.h"
  16. #include "ui/gfx/image/image_skia_operations.h"
  17. #include "ui/gfx/image/image_skia_rep.h"
  18. #include "ui/gfx/scoped_canvas.h"
  19. namespace gfx {
  20. namespace {
  21. int ImageRepWidthInPixels(const ImageSkiaRep& rep) {
  22. if (rep.is_null())
  23. return 0;
  24. return rep.pixel_width();
  25. }
  26. int ImageRepHeightInPixels(const ImageSkiaRep& rep) {
  27. if (rep.is_null())
  28. return 0;
  29. return rep.pixel_height();
  30. }
  31. void Fill(Canvas* c,
  32. const ImageSkiaRep& rep,
  33. int x,
  34. int y,
  35. int w,
  36. int h,
  37. const cc::PaintFlags& flags) {
  38. if (rep.is_null())
  39. return;
  40. c->DrawImageIntInPixel(rep, x, y, w, h, false, flags);
  41. }
  42. } // namespace
  43. NineImagePainter::NineImagePainter(const std::vector<ImageSkia>& images) {
  44. DCHECK_EQ(std::size(images_), images.size());
  45. for (size_t i = 0; i < std::size(images_); ++i)
  46. images_[i] = images[i];
  47. }
  48. NineImagePainter::NineImagePainter(const ImageSkia& image,
  49. const Insets& insets) {
  50. std::vector<gfx::Rect> regions;
  51. GetSubsetRegions(image, insets, &regions);
  52. DCHECK_EQ(9u, regions.size());
  53. for (size_t i = 0; i < 9; ++i)
  54. images_[i] = ImageSkiaOperations::ExtractSubset(image, regions[i]);
  55. }
  56. NineImagePainter::~NineImagePainter() {
  57. }
  58. bool NineImagePainter::IsEmpty() const {
  59. return images_[0].isNull();
  60. }
  61. Size NineImagePainter::GetMinimumSize() const {
  62. return IsEmpty() ? Size() : Size(
  63. images_[0].width() + images_[1].width() + images_[2].width(),
  64. images_[0].height() + images_[3].height() + images_[6].height());
  65. }
  66. void NineImagePainter::Paint(Canvas* canvas, const Rect& bounds) {
  67. // When no alpha value is specified, use default value of 100% opacity.
  68. Paint(canvas, bounds, std::numeric_limits<uint8_t>::max());
  69. }
  70. void NineImagePainter::Paint(Canvas* canvas,
  71. const Rect& bounds,
  72. const uint8_t alpha) {
  73. if (IsEmpty())
  74. return;
  75. ScopedCanvas scoped_canvas(canvas);
  76. // Painting and doing layout at physical device pixels to avoid cracks or
  77. // overlap.
  78. const float scale = canvas->UndoDeviceScaleFactor();
  79. // Since the drawing from the following Fill() calls assumes the mapped origin
  80. // is at (0,0), we need to translate the canvas to the mapped origin.
  81. const int left_in_pixels = base::ClampRound(bounds.x() * scale);
  82. const int top_in_pixels = base::ClampRound(bounds.y() * scale);
  83. const int right_in_pixels = base::ClampRound(bounds.right() * scale);
  84. const int bottom_in_pixels = base::ClampRound(bounds.bottom() * scale);
  85. const int width_in_pixels = right_in_pixels - left_in_pixels;
  86. const int height_in_pixels = bottom_in_pixels - top_in_pixels;
  87. // Since the drawing from the following Fill() calls assumes the mapped origin
  88. // is at (0,0), we need to translate the canvas to the mapped origin.
  89. canvas->Translate(gfx::Vector2d(left_in_pixels, top_in_pixels));
  90. ImageSkiaRep image_reps[9];
  91. static_assert(std::size(image_reps) == std::extent<decltype(images_)>(), "");
  92. for (size_t i = 0; i < std::size(image_reps); ++i) {
  93. image_reps[i] = images_[i].GetRepresentation(scale);
  94. DCHECK(image_reps[i].is_null() || image_reps[i].scale() == scale);
  95. }
  96. // In case the corners and edges don't all have the same width/height, we draw
  97. // the center first, and extend it out in all directions to the edges of the
  98. // images with the smallest widths/heights. This way there will be no
  99. // unpainted areas, though some corners or edges might overlap the center.
  100. int i0w = ImageRepWidthInPixels(image_reps[0]);
  101. int i2w = ImageRepWidthInPixels(image_reps[2]);
  102. int i3w = ImageRepWidthInPixels(image_reps[3]);
  103. int i5w = ImageRepWidthInPixels(image_reps[5]);
  104. int i6w = ImageRepWidthInPixels(image_reps[6]);
  105. int i8w = ImageRepWidthInPixels(image_reps[8]);
  106. int i0h = ImageRepHeightInPixels(image_reps[0]);
  107. int i1h = ImageRepHeightInPixels(image_reps[1]);
  108. int i2h = ImageRepHeightInPixels(image_reps[2]);
  109. int i6h = ImageRepHeightInPixels(image_reps[6]);
  110. int i7h = ImageRepHeightInPixels(image_reps[7]);
  111. int i8h = ImageRepHeightInPixels(image_reps[8]);
  112. i0w = std::min(i0w, width_in_pixels);
  113. i2w = std::min(i2w, width_in_pixels - i0w);
  114. i3w = std::min(i3w, width_in_pixels);
  115. i5w = std::min(i5w, width_in_pixels - i3w);
  116. i6w = std::min(i6w, width_in_pixels);
  117. i8w = std::min(i8w, width_in_pixels - i6w);
  118. i0h = std::min(i0h, height_in_pixels);
  119. i1h = std::min(i1h, height_in_pixels);
  120. i2h = std::min(i2h, height_in_pixels);
  121. i6h = std::min(i6h, height_in_pixels - i0h);
  122. i7h = std::min(i7h, height_in_pixels - i1h);
  123. i8h = std::min(i8h, height_in_pixels - i2h);
  124. int i4x = std::min({i0w, i3w, i6w});
  125. int i4y = std::min({i0h, i1h, i2h});
  126. int i4w = std::max(width_in_pixels - i4x - std::min({i2w, i5w, i8w}), 0);
  127. int i4h = std::max(height_in_pixels - i4y - std::min({i6h, i7h, i8h}), 0);
  128. cc::PaintFlags flags;
  129. flags.setAlpha(alpha);
  130. Fill(canvas, image_reps[4], i4x, i4y, i4w, i4h, flags);
  131. Fill(canvas, image_reps[0], 0, 0, i0w, i0h, flags);
  132. Fill(canvas, image_reps[1], i0w, 0, width_in_pixels - i0w - i2w, i1h, flags);
  133. Fill(canvas, image_reps[2], width_in_pixels - i2w, 0, i2w, i2h, flags);
  134. Fill(canvas, image_reps[3], 0, i0h, i3w, height_in_pixels - i0h - i6h, flags);
  135. Fill(canvas, image_reps[5], width_in_pixels - i5w, i2h, i5w,
  136. height_in_pixels - i2h - i8h, flags);
  137. Fill(canvas, image_reps[6], 0, height_in_pixels - i6h, i6w, i6h, flags);
  138. Fill(canvas, image_reps[7], i6w, height_in_pixels - i7h,
  139. width_in_pixels - i6w - i8w, i7h, flags);
  140. Fill(canvas, image_reps[8], width_in_pixels - i8w, height_in_pixels - i8h,
  141. i8w, i8h, flags);
  142. }
  143. // static
  144. void NineImagePainter::GetSubsetRegions(const ImageSkia& image,
  145. const Insets& insets,
  146. std::vector<Rect>* regions) {
  147. DCHECK_GE(image.width(), insets.width());
  148. DCHECK_GE(image.height(), insets.height());
  149. std::vector<Rect> result(9);
  150. const int x[] = {
  151. 0, insets.left(), image.width() - insets.right(), image.width()};
  152. const int y[] = {
  153. 0, insets.top(), image.height() - insets.bottom(), image.height()};
  154. for (size_t j = 0; j < 3; ++j) {
  155. for (size_t i = 0; i < 3; ++i) {
  156. result[i + j * 3] = Rect(x[i], y[j], x[i + 1] - x[i], y[j + 1] - y[j]);
  157. }
  158. }
  159. result.swap(*regions);
  160. }
  161. } // namespace gfx