OLD | NEW |
---|---|
(Empty) | |
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 | |
5 #include "remoting/client/gl_cursor_feedback.h" | |
6 | |
7 #include <math.h> | |
8 #include <array> | |
9 | |
10 #include "base/logging.h" | |
11 #include "remoting/base/util.h" | |
12 #include "remoting/client/gl_canvas.h" | |
13 #include "remoting/client/gl_math.h" | |
14 #include "remoting/client/gl_render_layer.h" | |
15 #include "skia/include/core/SkCanvas.h" | |
16 #include "skia/include/core/SkColor.h" | |
17 #include "skia/include/core/SkImageInfo.h" | |
18 #include "skia/include/core/SkPaint.h" | |
19 #include "skia/include/core/SkSurface.h" | |
20 #include "skia/include/effects/SkGradientShader.h" | |
21 | |
22 namespace { | |
23 | |
24 const int kTextureId = 2; | |
25 const int kColorRingsCount = 4; | |
26 const int kFeedbackTexturePixelDiameter = 256; | |
27 const float kAnimationDurationMs = 220.f; | |
28 | |
29 // RGBA8888 colors. From inside to outside. | |
30 const SkColor kFeedbackRingColors[kColorRingsCount] = { | |
31 SkColorSetARGB(0, 0, 0, 0), // transparent black | |
32 SkColorSetARGB(0xff, 0xff, 0xff, 0xff), // white | |
33 SkColorSetARGB(0xff, 0, 0, 0), // black | |
34 SkColorSetARGB(0, 0, 0, 0), // transparent black | |
35 }; | |
36 | |
37 const float kFeedbackRadiusStops[kColorRingsCount] = {0.0f, 0.8f, 0.9f, 1.0f}; | |
38 | |
39 } // namespace | |
40 | |
41 namespace remoting { | |
42 | |
43 GlCursorFeedback::GlCursorFeedback() {} | |
44 | |
45 GlCursorFeedback::~GlCursorFeedback() {} | |
46 | |
47 void GlCursorFeedback::SetCanvas(GlCanvas* canvas) { | |
48 if (!canvas) { | |
49 layer_.reset(); | |
50 return; | |
51 } | |
52 layer_.reset(new GlRenderLayer(kTextureId, canvas)); | |
53 layer_->SetTexture(GetFeedbackTexture(), kFeedbackTexturePixelDiameter, | |
54 kFeedbackTexturePixelDiameter); | |
55 } | |
56 | |
57 void GlCursorFeedback::StartAnimation(float normalized_x, | |
58 float normalized_y, | |
59 float normalized_width, | |
60 float normalized_height) { | |
61 max_width_ = normalized_width; | |
62 max_height_ = normalized_height; | |
63 cursor_x_ = normalized_x; | |
64 cursor_y_ = normalized_y; | |
65 animation_start_time_ = base::TimeTicks::Now(); | |
66 } | |
67 | |
68 bool GlCursorFeedback::Draw() { | |
69 if (!layer_ || animation_start_time_.is_null()) { | |
70 return false; | |
71 } | |
72 float progress = | |
73 (base::TimeTicks::Now() - animation_start_time_).InMilliseconds() / | |
74 kAnimationDurationMs; | |
75 if (progress > 1) { | |
76 animation_start_time_ = base::TimeTicks(); | |
77 return false; | |
78 } | |
79 float width = progress * max_width_; | |
80 float height = progress * max_height_; | |
81 std::array<float, 8> positions; | |
82 FillRectangleVertexPositions(&positions, cursor_x_ - width / 2, | |
83 cursor_y_ - height / 2, width, height); | |
84 layer_->SetVertexPositions(positions); | |
85 layer_->Draw(1.f - progress); | |
86 return true; | |
87 } | |
88 | |
89 // static | |
90 const uint8_t* GlCursorFeedback::GetFeedbackTexture() { | |
91 if (texture_initialized_) { | |
92 return texture_; | |
93 } | |
94 SkImageInfo image_info = SkImageInfo::Make( | |
95 kFeedbackTexturePixelDiameter, kFeedbackTexturePixelDiameter, | |
96 SkColorType::kRGBA_8888_SkColorType, SkAlphaType::kPremul_SkAlphaType); | |
97 sk_sp<SkSurface> surface(SkSurface::MakeRasterDirect( | |
98 image_info, texture_, | |
99 kFeedbackTexturePixelDiameter * kBytesPerPixelRGB32)); | |
100 SkCanvas* canvas = surface->getCanvas(); | |
101 | |
102 SkPaint paint; | |
103 int radius = kFeedbackTexturePixelDiameter / 2; | |
104 paint.setShader(SkGradientShader::MakeRadial( | |
105 SkPoint::Make(radius, radius), radius, kFeedbackRingColors, | |
106 kFeedbackRadiusStops, kColorRingsCount, | |
107 SkShader::TileMode::kClamp_TileMode)); | |
108 canvas->drawPaint(paint); | |
109 | |
110 texture_initialized_ = true; | |
111 return texture_; | |
112 } | |
113 | |
114 // static | |
115 uint8_t GlCursorFeedback::texture_[kFeedbackTexturePixelDiameter * | |
116 kFeedbackTexturePixelDiameter * | |
117 kBytesPerPixelRGB32]; | |
118 | |
119 // static | |
120 bool GlCursorFeedback::texture_initialized_ = false; | |
Sergey Ulanov
2016/07/20 18:06:11
this flag doesn't have to be a class member. It ca
Sergey Ulanov
2016/07/20 18:06:11
Please use base::Singleton<> to make initializatio
Yuwei
2016/07/20 22:56:48
Done.
| |
121 | |
122 } // namespace remoting | |
OLD | NEW |