paint_aggregator.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // Copyright (c) 2010 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. #ifndef PDF_PAINT_AGGREGATOR_H_
  5. #define PDF_PAINT_AGGREGATOR_H_
  6. #include <vector>
  7. #include "pdf/paint_ready_rect.h"
  8. #include "ui/gfx/geometry/rect.h"
  9. #include "ui/gfx/geometry/vector2d.h"
  10. namespace chrome_pdf {
  11. // This class is responsible for aggregating multiple invalidation and scroll
  12. // commands to produce a scroll and repaint sequence. You can use this manually
  13. // to track your updates, but most applications will use the PaintManager to
  14. // additionally handle the necessary callbacks on top of the PaintAggregator
  15. // functionality.
  16. //
  17. // See http://code.google.com/p/ppapi/wiki/2DPaintingModel
  18. class PaintAggregator {
  19. public:
  20. struct PaintUpdate {
  21. PaintUpdate();
  22. PaintUpdate(const PaintUpdate& that);
  23. ~PaintUpdate();
  24. // True if there is a scroll applied. This indicates that the scroll delta
  25. // and scroll_rect are nonzero (just as a convenience).
  26. bool has_scroll;
  27. // The amount to scroll by. Either the X or Y may be nonzero to indicate a
  28. // scroll in that direction, but there will never be a scroll in both
  29. // directions at the same time (this will be converted to a paint of the
  30. // region instead).
  31. //
  32. // If there is no scroll, this will be (0, 0).
  33. gfx::Vector2d scroll_delta;
  34. // The rectangle that should be scrolled by the scroll_delta. If there is no
  35. // scroll, this will be (0, 0, 0, 0). We only track one scroll command at
  36. // once. If there are multiple ones, they will be converted to invalidates.
  37. gfx::Rect scroll_rect;
  38. // A list of all the individual dirty rectangles. This is an aggregated list
  39. // of all invalidate calls. Different rectangles may be unified to produce a
  40. // minimal list with no overlap that is more efficient to paint. This list
  41. // also contains the region exposed by any scroll command.
  42. std::vector<gfx::Rect> paint_rects;
  43. };
  44. PaintAggregator();
  45. // There is a PendingUpdate if InvalidateRect or ScrollRect were called and
  46. // ClearPendingUpdate was not called.
  47. bool HasPendingUpdate() const;
  48. void ClearPendingUpdate();
  49. PaintUpdate GetPendingUpdate();
  50. // Sets the result of a call to the plugin to paint. This includes rects that
  51. // are finished painting (ready), and ones that are still in-progress
  52. // (pending).
  53. void SetIntermediateResults(const std::vector<PaintReadyRect>& ready,
  54. const std::vector<gfx::Rect>& pending);
  55. // Returns the rectangles that are ready to be painted.
  56. std::vector<PaintReadyRect> GetReadyRects() const;
  57. // The given rect should be repainted.
  58. void InvalidateRect(const gfx::Rect& rect);
  59. // The given rect should be scrolled by the given amounts.
  60. void ScrollRect(const gfx::Rect& clip_rect, const gfx::Vector2d& amount);
  61. private:
  62. // This structure is an internal version of PaintUpdate. It's different in
  63. // two respects:
  64. //
  65. // - The scroll damange (area exposed by the scroll operation, if any) is
  66. // maintained separately from the dirty rects generated by calling
  67. // InvalidateRect. We need to know this distinction for some operations.
  68. //
  69. // - The paint bounds union is computed on the fly so we don't have to keep
  70. // a rectangle up to date as we do different operations.
  71. class InternalPaintUpdate {
  72. public:
  73. InternalPaintUpdate();
  74. ~InternalPaintUpdate();
  75. // Computes the rect damaged by scrolling within `scroll_rect` by
  76. // `scroll_delta`. This rect must be repainted. It is not included in
  77. // paint_rects.
  78. gfx::Rect GetScrollDamage() const;
  79. gfx::Vector2d scroll_delta;
  80. gfx::Rect scroll_rect;
  81. // Does not include the scroll damage rect unless
  82. // synthesized_scroll_damage_rect_ is set.
  83. std::vector<gfx::Rect> paint_rects;
  84. // Rectangles that are finished painting.
  85. std::vector<PaintReadyRect> ready_rects;
  86. // Whether we have added the scroll damage rect to paint_rects yet or not.
  87. bool synthesized_scroll_damage_rect_;
  88. };
  89. gfx::Rect ScrollPaintRect(const gfx::Rect& paint_rect,
  90. const gfx::Vector2d& amount) const;
  91. void InvalidateScrollRect();
  92. // Internal method used by InvalidateRect. If `check_scroll` is true, then the
  93. // method checks if there's a pending scroll and if so also invalidates `rect`
  94. // in the new scroll position.
  95. void InvalidateRectInternal(const gfx::Rect& rect, bool check_scroll);
  96. InternalPaintUpdate update_;
  97. };
  98. } // namespace chrome_pdf
  99. #endif // PDF_PAINT_AGGREGATOR_H_