Index: remoting/client/gl_cursor_feedback.cc |
diff --git a/remoting/client/gl_cursor_feedback.cc b/remoting/client/gl_cursor_feedback.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..5011665e912c5a278d4cf48334b58025e5e506c7 |
--- /dev/null |
+++ b/remoting/client/gl_cursor_feedback.cc |
@@ -0,0 +1,122 @@ |
+// Copyright 2016 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "remoting/client/gl_cursor_feedback.h" |
+ |
+#include <math.h> |
+#include <array> |
+ |
+#include "base/logging.h" |
+#include "remoting/base/util.h" |
+#include "remoting/client/gl_canvas.h" |
+#include "remoting/client/gl_math.h" |
+#include "remoting/client/gl_render_layer.h" |
+#include "skia/include/core/SkCanvas.h" |
+#include "skia/include/core/SkColor.h" |
+#include "skia/include/core/SkImageInfo.h" |
+#include "skia/include/core/SkPaint.h" |
+#include "skia/include/core/SkSurface.h" |
+#include "skia/include/effects/SkGradientShader.h" |
+ |
+namespace { |
+ |
+const int kTextureId = 2; |
+const int kColorRingsCount = 4; |
+const int kFeedbackTexturePixelDiameter = 256; |
+const float kAnimationDurationMs = 220.f; |
+ |
+// RGBA8888 colors. From inside to outside. |
+const SkColor kFeedbackRingColors[kColorRingsCount] = { |
+ SkColorSetARGB(0, 0, 0, 0), // transparent black |
+ SkColorSetARGB(0xff, 0xff, 0xff, 0xff), // white |
+ SkColorSetARGB(0xff, 0, 0, 0), // black |
+ SkColorSetARGB(0, 0, 0, 0), // transparent black |
+}; |
+ |
+const float kFeedbackRadiusStops[kColorRingsCount] = {0.0f, 0.8f, 0.9f, 1.0f}; |
+ |
+} // namespace |
+ |
+namespace remoting { |
+ |
+GlCursorFeedback::GlCursorFeedback() {} |
+ |
+GlCursorFeedback::~GlCursorFeedback() {} |
+ |
+void GlCursorFeedback::SetCanvas(GlCanvas* canvas) { |
+ if (!canvas) { |
+ layer_.reset(); |
+ return; |
+ } |
+ layer_.reset(new GlRenderLayer(kTextureId, canvas)); |
+ layer_->SetTexture(GetFeedbackTexture(), kFeedbackTexturePixelDiameter, |
+ kFeedbackTexturePixelDiameter); |
+} |
+ |
+void GlCursorFeedback::StartAnimation(float normalized_x, |
+ float normalized_y, |
+ float normalized_width, |
+ float normalized_height) { |
+ max_width_ = normalized_width; |
+ max_height_ = normalized_height; |
+ cursor_x_ = normalized_x; |
+ cursor_y_ = normalized_y; |
+ animation_start_time_ = base::TimeTicks::Now(); |
+} |
+ |
+bool GlCursorFeedback::Draw() { |
+ if (!layer_ || animation_start_time_.is_null()) { |
+ return false; |
+ } |
+ float progress = |
+ (base::TimeTicks::Now() - animation_start_time_).InMilliseconds() / |
+ kAnimationDurationMs; |
+ if (progress > 1) { |
+ animation_start_time_ = base::TimeTicks(); |
+ return false; |
+ } |
+ float width = progress * max_width_; |
+ float height = progress * max_height_; |
+ std::array<float, 8> positions; |
+ FillRectangleVertexPositions(&positions, cursor_x_ - width / 2, |
+ cursor_y_ - height / 2, width, height); |
+ layer_->SetVertexPositions(positions); |
+ layer_->Draw(1.f - progress); |
+ return true; |
+} |
+ |
+// static |
+const uint8_t* GlCursorFeedback::GetFeedbackTexture() { |
+ if (texture_initialized_) { |
+ return texture_; |
+ } |
+ SkImageInfo image_info = SkImageInfo::Make( |
+ kFeedbackTexturePixelDiameter, kFeedbackTexturePixelDiameter, |
+ SkColorType::kRGBA_8888_SkColorType, SkAlphaType::kPremul_SkAlphaType); |
+ sk_sp<SkSurface> surface(SkSurface::MakeRasterDirect( |
+ image_info, texture_, |
+ kFeedbackTexturePixelDiameter * kBytesPerPixelRGB32)); |
+ SkCanvas* canvas = surface->getCanvas(); |
+ |
+ SkPaint paint; |
+ int radius = kFeedbackTexturePixelDiameter / 2; |
+ paint.setShader(SkGradientShader::MakeRadial( |
+ SkPoint::Make(radius, radius), radius, kFeedbackRingColors, |
+ kFeedbackRadiusStops, kColorRingsCount, |
+ SkShader::TileMode::kClamp_TileMode)); |
+ canvas->drawPaint(paint); |
+ |
+ texture_initialized_ = true; |
+ return texture_; |
+} |
+ |
+// static |
+uint8_t GlCursorFeedback::texture_[kFeedbackTexturePixelDiameter * |
+ kFeedbackTexturePixelDiameter * |
+ kBytesPerPixelRGB32]; |
+ |
+// static |
+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.
|
+ |
+} // namespace remoting |