yuweih | 2495ff0 | 2016-07-21 23:38:03 | [diff] [blame] | 1 | // Copyright 2016 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 | |
nicholss | 9949f5b | 2017-01-05 19:29:17 | [diff] [blame] | 5 | #include "remoting/client/display/gl_cursor.h" |
yuweih | 2495ff0 | 2016-07-21 23:38:03 | [diff] [blame] | 6 | |
| 7 | #include "remoting/base/util.h" |
nicholss | 9949f5b | 2017-01-05 19:29:17 | [diff] [blame] | 8 | #include "remoting/client/display/gl_canvas.h" |
| 9 | #include "remoting/client/display/gl_math.h" |
| 10 | #include "remoting/client/display/gl_render_layer.h" |
| 11 | #include "remoting/client/display/gl_texture_ids.h" |
yuweih | 2495ff0 | 2016-07-21 23:38:03 | [diff] [blame] | 12 | #include "remoting/proto/control.pb.h" |
| 13 | #include "third_party/libyuv/include/libyuv/convert_argb.h" |
| 14 | |
| 15 | namespace remoting { |
| 16 | |
| 17 | namespace { |
yuweih | 2495ff0 | 2016-07-21 23:38:03 | [diff] [blame] | 18 | const int kDefaultCursorDataSize = 32 * 32 * GlRenderLayer::kBytesPerPixel; |
yuweih | 2495ff0 | 2016-07-21 23:38:03 | [diff] [blame] | 19 | } // namespace |
| 20 | |
Jeremy Roman | 7c5cfabd | 2019-08-12 15:45:27 | [diff] [blame] | 21 | GlCursor::GlCursor() {} |
yuweih | 2495ff0 | 2016-07-21 23:38:03 | [diff] [blame] | 22 | |
joedow | 4d4db58 | 2017-05-15 18:34:50 | [diff] [blame] | 23 | GlCursor::~GlCursor() { |
| 24 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 25 | } |
yuweih | 2495ff0 | 2016-07-21 23:38:03 | [diff] [blame] | 26 | |
| 27 | void GlCursor::SetCursorShape(const protocol::CursorShapeInfo& cursor_shape) { |
| 28 | int data_size = cursor_shape.width() * cursor_shape.height() * |
nicholss | 9949f5b | 2017-01-05 19:29:17 | [diff] [blame] | 29 | GlRenderLayer::kBytesPerPixel; |
yuweih | 2495ff0 | 2016-07-21 23:38:03 | [diff] [blame] | 30 | if (current_cursor_data_size_ < data_size) { |
| 31 | current_cursor_data_size_ = |
| 32 | kDefaultCursorDataSize > data_size ? kDefaultCursorDataSize : data_size; |
| 33 | current_cursor_data_.reset(new uint8_t[current_cursor_data_size_]); |
| 34 | } |
| 35 | int stride = cursor_shape.width() * GlRenderLayer::kBytesPerPixel; |
| 36 | libyuv::ABGRToARGB( |
| 37 | reinterpret_cast<const uint8_t*>(cursor_shape.data().data()), stride, |
| 38 | current_cursor_data_.get(), stride, cursor_shape.width(), |
| 39 | cursor_shape.height()); |
| 40 | |
| 41 | bool size_changed = current_cursor_width_ != cursor_shape.width() || |
| 42 | current_cursor_height_ != cursor_shape.height(); |
| 43 | |
| 44 | current_cursor_width_ = cursor_shape.width(); |
| 45 | current_cursor_height_ = cursor_shape.height(); |
| 46 | current_cursor_hotspot_x_ = cursor_shape.hotspot_x(); |
| 47 | current_cursor_hotspot_y_ = cursor_shape.hotspot_y(); |
| 48 | |
| 49 | SetCurrentCursorShape(size_changed); |
| 50 | |
| 51 | SetCursorPosition(cursor_x_, cursor_y_); |
| 52 | } |
| 53 | |
yuweih | b70334d | 2016-08-18 19:23:15 | [diff] [blame] | 54 | void GlCursor::SetCursorPosition(float x, float y) { |
yuweih | 2495ff0 | 2016-07-21 23:38:03 | [diff] [blame] | 55 | cursor_x_ = x; |
| 56 | cursor_y_ = y; |
yuweih | 8d1912fd | 2016-08-08 21:26:51 | [diff] [blame] | 57 | if (!current_cursor_data_) { |
yuweih | 2495ff0 | 2016-07-21 23:38:03 | [diff] [blame] | 58 | return; |
| 59 | } |
| 60 | std::array<float, 8> positions; |
| 61 | FillRectangleVertexPositions( |
yuweih | 8d1912fd | 2016-08-08 21:26:51 | [diff] [blame] | 62 | x - current_cursor_hotspot_x_, y - current_cursor_hotspot_y_, |
| 63 | current_cursor_width_, current_cursor_height_, &positions); |
yuweih | 2495ff0 | 2016-07-21 23:38:03 | [diff] [blame] | 64 | if (layer_) { |
| 65 | layer_->SetVertexPositions(positions); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | void GlCursor::SetCursorVisible(bool visible) { |
| 70 | visible_ = visible; |
| 71 | } |
| 72 | |
nicholss | f62cc8a | 2017-01-12 16:08:41 | [diff] [blame] | 73 | void GlCursor::SetCanvas(base::WeakPtr<Canvas> canvas) { |
yuweih | 2495ff0 | 2016-07-21 23:38:03 | [diff] [blame] | 74 | if (!canvas) { |
| 75 | layer_.reset(); |
| 76 | return; |
| 77 | } |
yuweih | cf627b98 | 2016-08-09 22:13:25 | [diff] [blame] | 78 | layer_.reset(new GlRenderLayer(kGlCursorTextureId, canvas)); |
yuweih | 2495ff0 | 2016-07-21 23:38:03 | [diff] [blame] | 79 | if (current_cursor_data_) { |
| 80 | SetCurrentCursorShape(true); |
| 81 | } |
| 82 | SetCursorPosition(cursor_x_, cursor_y_); |
| 83 | } |
| 84 | |
nicholss | f62cc8a | 2017-01-12 16:08:41 | [diff] [blame] | 85 | bool GlCursor::Draw() { |
| 86 | DCHECK(thread_checker_.CalledOnValidThread()); |
yuweih | 2495ff0 | 2016-07-21 23:38:03 | [diff] [blame] | 87 | if (layer_ && current_cursor_data_ && visible_) { |
| 88 | layer_->Draw(1.f); |
| 89 | } |
nicholss | f62cc8a | 2017-01-12 16:08:41 | [diff] [blame] | 90 | return false; |
| 91 | } |
| 92 | |
| 93 | int GlCursor::GetZIndex() { |
| 94 | return Drawable::ZIndex::CURSOR; |
yuweih | 2495ff0 | 2016-07-21 23:38:03 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | void GlCursor::SetCurrentCursorShape(bool size_changed) { |
| 98 | if (layer_) { |
| 99 | if (size_changed) { |
| 100 | layer_->SetTexture(current_cursor_data_.get(), current_cursor_width_, |
yuweih | cf627b98 | 2016-08-09 22:13:25 | [diff] [blame] | 101 | current_cursor_height_, 0); |
yuweih | 2495ff0 | 2016-07-21 23:38:03 | [diff] [blame] | 102 | } else { |
| 103 | layer_->UpdateTexture(current_cursor_data_.get(), 0, 0, |
| 104 | current_cursor_width_, current_cursor_width_, 0); |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | |
nicholss | f62cc8a | 2017-01-12 16:08:41 | [diff] [blame] | 109 | base::WeakPtr<Drawable> GlCursor::GetWeakPtr() { |
| 110 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 111 | return weak_factory_.GetWeakPtr(); |
| 112 | } |
| 113 | |
yuweih | 2495ff0 | 2016-07-21 23:38:03 | [diff] [blame] | 114 | } // namespace remoting |