Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
  2. * Copyright 2016-2020 Pierre Ossman for Cendio AB
  3. *
  4. * This is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This software is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this software; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  17. * USA.
  18. */
  19. #include <rfb/Region.h>
  20. #include <rfb/LogWriter.h>
  21. extern "C" {
  22. #include <pixman.h>
  23. }
  24. static rfb::LogWriter vlog("Region");
  25. rfb::Region::Region() {
  26. rgn = new struct pixman_region16;
  27. pixman_region_init(rgn);
  28. }
  29. rfb::Region::Region(const Rect& r) {
  30. rgn = new struct pixman_region16;
  31. pixman_region_init_rect(rgn, r.tl.x, r.tl.y, r.width(), r.height());
  32. }
  33. rfb::Region::Region(const rfb::Region& r) {
  34. rgn = new struct pixman_region16;
  35. pixman_region_init(rgn);
  36. pixman_region_copy(rgn, r.rgn);
  37. }
  38. rfb::Region::~Region() {
  39. pixman_region_fini(rgn);
  40. delete rgn;
  41. }
  42. rfb::Region& rfb::Region::operator=(const rfb::Region& r) {
  43. pixman_region_copy(rgn, r.rgn);
  44. return *this;
  45. }
  46. void rfb::Region::clear() {
  47. // pixman_region_clear() isn't available on some older systems
  48. pixman_region_fini(rgn);
  49. pixman_region_init(rgn);
  50. }
  51. void rfb::Region::reset(const Rect& r) {
  52. pixman_region_fini(rgn);
  53. pixman_region_init_rect(rgn, r.tl.x, r.tl.y, r.width(), r.height());
  54. }
  55. void rfb::Region::translate(const Point& delta) {
  56. pixman_region_translate(rgn, delta.x, delta.y);
  57. }
  58. void rfb::Region::assign_intersect(const rfb::Region& r) {
  59. pixman_region_intersect(rgn, rgn, r.rgn);
  60. }
  61. void rfb::Region::assign_union(const rfb::Region& r) {
  62. pixman_region_union(rgn, rgn, r.rgn);
  63. }
  64. void rfb::Region::assign_subtract(const rfb::Region& r) {
  65. pixman_region_subtract(rgn, rgn, r.rgn);
  66. }
  67. rfb::Region rfb::Region::intersect(const rfb::Region& r) const {
  68. rfb::Region ret;
  69. pixman_region_intersect(ret.rgn, rgn, r.rgn);
  70. return ret;
  71. }
  72. rfb::Region rfb::Region::union_(const rfb::Region& r) const {
  73. rfb::Region ret;
  74. pixman_region_union(ret.rgn, rgn, r.rgn);
  75. return ret;
  76. }
  77. rfb::Region rfb::Region::subtract(const rfb::Region& r) const {
  78. rfb::Region ret;
  79. pixman_region_subtract(ret.rgn, rgn, r.rgn);
  80. return ret;
  81. }
  82. bool rfb::Region::equals(const rfb::Region& r) const {
  83. return pixman_region_equal(rgn, r.rgn);
  84. }
  85. int rfb::Region::numRects() const {
  86. return pixman_region_n_rects(rgn);
  87. }
  88. bool rfb::Region::get_rects(std::vector<Rect>* rects,
  89. bool left2right, bool topdown) const
  90. {
  91. int nRects;
  92. const pixman_box16_t* boxes;
  93. int xInc, yInc, i;
  94. boxes = pixman_region_rectangles(rgn, &nRects);
  95. rects->clear();
  96. rects->reserve(nRects);
  97. xInc = left2right ? 1 : -1;
  98. yInc = topdown ? 1 : -1;
  99. i = topdown ? 0 : nRects-1;
  100. while (nRects > 0) {
  101. int firstInNextBand = i;
  102. int nRectsInBand = 0;
  103. while (nRects > 0 && boxes[firstInNextBand].y1 == boxes[i].y1)
  104. {
  105. firstInNextBand += yInc;
  106. nRects--;
  107. nRectsInBand++;
  108. }
  109. if (xInc != yInc)
  110. i = firstInNextBand - yInc;
  111. while (nRectsInBand > 0) {
  112. Rect r(boxes[i].x1, boxes[i].y1, boxes[i].x2, boxes[i].y2);
  113. rects->push_back(r);
  114. i += xInc;
  115. nRectsInBand--;
  116. }
  117. i = firstInNextBand;
  118. }
  119. return !rects->empty();
  120. }
  121. rfb::Rect rfb::Region::get_bounding_rect() const {
  122. const pixman_box16_t* extents;
  123. extents = pixman_region_extents(rgn);
  124. return Rect(extents->x1, extents->y1, extents->x2, extents->y2);
  125. }
  126. void rfb::Region::debug_print(const char* prefix) const
  127. {
  128. Rect extents;
  129. std::vector<Rect> rects;
  130. std::vector<Rect>::const_iterator iter;
  131. extents = get_bounding_rect();
  132. get_rects(&rects);
  133. vlog.debug("%s num rects %3ld extents %3d,%3d %3dx%3d",
  134. prefix, (long)rects.size(), extents.tl.x, extents.tl.y,
  135. extents.width(), extents.height());
  136. for (iter = rects.begin(); iter != rects.end(); ++iter) {
  137. vlog.debug(" rect %3d,%3d %3dx%3d",
  138. iter->tl.x, iter->tl.y, iter->width(), iter->height());
  139. }
  140. }