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