| OLD | NEW |
| (Empty) |
| 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 | |
| 5 #include "remoting/host/capturer_helper.h" | |
| 6 | |
| 7 #include <algorithm> | |
| 8 | |
| 9 #include "base/logging.h" | |
| 10 #include "base/memory/scoped_ptr.h" | |
| 11 | |
| 12 namespace remoting { | |
| 13 | |
| 14 CapturerHelper::CapturerHelper() : | |
| 15 size_most_recent_(SkISize::Make(0, 0)), | |
| 16 log_grid_size_(0) { | |
| 17 } | |
| 18 | |
| 19 CapturerHelper::~CapturerHelper() { | |
| 20 } | |
| 21 | |
| 22 void CapturerHelper::ClearInvalidRegion() { | |
| 23 base::AutoLock auto_invalid_region_lock(invalid_region_lock_); | |
| 24 invalid_region_.setEmpty(); | |
| 25 } | |
| 26 | |
| 27 void CapturerHelper::InvalidateRegion(const SkRegion& invalid_region) { | |
| 28 base::AutoLock auto_invalid_region_lock(invalid_region_lock_); | |
| 29 invalid_region_.op(invalid_region, SkRegion::kUnion_Op); | |
| 30 } | |
| 31 | |
| 32 void CapturerHelper::InvalidateScreen(const SkISize& size) { | |
| 33 base::AutoLock auto_invalid_region_lock(invalid_region_lock_); | |
| 34 invalid_region_.op(SkIRect::MakeWH(size.width(), size.height()), | |
| 35 SkRegion::kUnion_Op); | |
| 36 } | |
| 37 | |
| 38 void CapturerHelper::InvalidateFullScreen() { | |
| 39 if (!size_most_recent_.isZero()) | |
| 40 InvalidateScreen(size_most_recent_); | |
| 41 } | |
| 42 | |
| 43 void CapturerHelper::SwapInvalidRegion(SkRegion* invalid_region) { | |
| 44 { | |
| 45 base::AutoLock auto_invalid_region_lock(invalid_region_lock_); | |
| 46 invalid_region->swap(invalid_region_); | |
| 47 } | |
| 48 if (log_grid_size_ > 0) { | |
| 49 scoped_ptr<SkRegion> expanded_region( | |
| 50 ExpandToGrid(*invalid_region, log_grid_size_)); | |
| 51 invalid_region->swap(*expanded_region); | |
| 52 invalid_region->op(SkRegion(SkIRect::MakeSize(size_most_recent_)), | |
| 53 SkRegion::kIntersect_Op); | |
| 54 } | |
| 55 } | |
| 56 | |
| 57 void CapturerHelper::SetLogGridSize(int log_grid_size) { | |
| 58 log_grid_size_ = log_grid_size; | |
| 59 } | |
| 60 | |
| 61 const SkISize& CapturerHelper::size_most_recent() const { | |
| 62 return size_most_recent_; | |
| 63 } | |
| 64 | |
| 65 void CapturerHelper::set_size_most_recent(const SkISize& size) { | |
| 66 size_most_recent_ = size; | |
| 67 } | |
| 68 | |
| 69 // Returns the largest multiple of |n| that is <= |x|. | |
| 70 // |n| must be a power of 2. |nMask| is ~(|n| - 1). | |
| 71 static int DownToMultiple(int x, int nMask) { | |
| 72 return (x & nMask); | |
| 73 } | |
| 74 | |
| 75 // Returns the smallest multiple of |n| that is >= |x|. | |
| 76 // |n| must be a power of 2. |nMask| is ~(|n| - 1). | |
| 77 static int UpToMultiple(int x, int n, int nMask) { | |
| 78 return ((x + n - 1) & nMask); | |
| 79 } | |
| 80 | |
| 81 scoped_ptr<SkRegion> CapturerHelper::ExpandToGrid(const SkRegion& region, | |
| 82 int log_grid_size) { | |
| 83 DCHECK(log_grid_size >= 1); | |
| 84 int grid_size = 1 << log_grid_size; | |
| 85 int grid_size_mask = ~(grid_size - 1); | |
| 86 // Count the rects in the region. | |
| 87 int rectNum = 0; | |
| 88 SkRegion::Iterator iter(region); | |
| 89 while (!iter.done()) { | |
| 90 iter.next(); | |
| 91 ++rectNum; | |
| 92 } | |
| 93 // Expand each rect. | |
| 94 scoped_array<SkIRect> rects(new SkIRect[rectNum]); | |
| 95 iter.rewind(); | |
| 96 int rectI = 0; | |
| 97 while (!iter.done()) { | |
| 98 SkIRect rect = iter.rect(); | |
| 99 iter.next(); | |
| 100 int left = std::min(rect.left(), rect.right()); | |
| 101 int right = std::max(rect.left(), rect.right()); | |
| 102 int top = std::min(rect.top(), rect.bottom()); | |
| 103 int bottom = std::max(rect.top(), rect.bottom()); | |
| 104 left = DownToMultiple(left, grid_size_mask); | |
| 105 right = UpToMultiple(right, grid_size, grid_size_mask); | |
| 106 top = DownToMultiple(top, grid_size_mask); | |
| 107 bottom = UpToMultiple(bottom, grid_size, grid_size_mask); | |
| 108 rects[rectI++] = SkIRect::MakeLTRB(left, top, right, bottom); | |
| 109 } | |
| 110 // Make the union of the expanded rects. | |
| 111 scoped_ptr<SkRegion> regionNew(new SkRegion()); | |
| 112 regionNew->setRects(rects.get(), rectNum); | |
| 113 return regionNew.Pass(); | |
| 114 } | |
| 115 | |
| 116 } // namespace remoting | |
| OLD | NEW |