blit.cc 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // Copyright (c) 2012 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/blit.h"
  5. #include <stddef.h>
  6. #include "base/check.h"
  7. #include "build/build_config.h"
  8. #include "skia/ext/platform_canvas.h"
  9. #include "third_party/skia/include/core/SkPixmap.h"
  10. #include "ui/gfx/geometry/point.h"
  11. #include "ui/gfx/geometry/rect.h"
  12. #include "ui/gfx/geometry/vector2d.h"
  13. namespace gfx {
  14. namespace {
  15. // Returns true if the given canvas has any part of itself clipped out or
  16. // any non-identity tranform.
  17. bool HasClipOrTransform(SkCanvas& canvas) {
  18. if (!canvas.getTotalMatrix().isIdentity())
  19. return true;
  20. if (!canvas.isClipRect())
  21. return true;
  22. // Now we know the clip is a regular rectangle, make sure it covers the
  23. // entire canvas.
  24. SkIRect clip_bounds = canvas.getDeviceClipBounds();
  25. SkImageInfo info;
  26. size_t row_bytes;
  27. void* pixels = canvas.accessTopLayerPixels(&info, &row_bytes);
  28. DCHECK(pixels);
  29. if (!pixels)
  30. return true;
  31. if (clip_bounds.fLeft != 0 || clip_bounds.fTop != 0 ||
  32. clip_bounds.fRight != info.width() ||
  33. clip_bounds.fBottom != info.height())
  34. return true;
  35. return false;
  36. }
  37. } // namespace
  38. void ScrollCanvas(SkCanvas* canvas,
  39. const gfx::Rect& in_clip,
  40. const gfx::Vector2d& offset) {
  41. DCHECK(!HasClipOrTransform(*canvas)); // Don't support special stuff.
  42. SkPixmap pixmap;
  43. bool success = skia::GetWritablePixels(canvas, &pixmap);
  44. DCHECK(success);
  45. // We expect all coords to be inside the canvas, so clip here.
  46. gfx::Rect clip = gfx::IntersectRects(
  47. in_clip, gfx::Rect(0, 0, pixmap.width(), pixmap.height()));
  48. // Compute the set of pixels we'll actually end up painting.
  49. gfx::Rect dest_rect = gfx::IntersectRects(clip + offset, clip);
  50. if (dest_rect.size().IsEmpty())
  51. return; // Nothing to do.
  52. // Compute the source pixels that will map to the dest_rect
  53. gfx::Rect src_rect = dest_rect - offset;
  54. size_t row_bytes = dest_rect.width() * 4;
  55. if (offset.y() > 0) {
  56. // Data is moving down, copy from the bottom up.
  57. for (int y = dest_rect.height() - 1; y >= 0; y--) {
  58. memcpy(pixmap.writable_addr32(dest_rect.x(), dest_rect.y() + y),
  59. pixmap.addr32(src_rect.x(), src_rect.y() + y),
  60. row_bytes);
  61. }
  62. } else if (offset.y() < 0) {
  63. // Data is moving up, copy from the top down.
  64. for (int y = 0; y < dest_rect.height(); y++) {
  65. memcpy(pixmap.writable_addr32(dest_rect.x(), dest_rect.y() + y),
  66. pixmap.addr32(src_rect.x(), src_rect.y() + y),
  67. row_bytes);
  68. }
  69. } else if (offset.x() != 0) {
  70. // Horizontal-only scroll. We can do it in either top-to-bottom or bottom-
  71. // to-top, but have to be careful about the order for copying each row.
  72. // Fortunately, memmove already handles this for us.
  73. for (int y = 0; y < dest_rect.height(); y++) {
  74. memmove(pixmap.writable_addr32(dest_rect.x(), dest_rect.y() + y),
  75. pixmap.addr32(src_rect.x(), src_rect.y() + y),
  76. row_bytes);
  77. }
  78. }
  79. }
  80. } // namespace gfx