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_texture.h" | |
6 | |
7 #include "remoting/client/gl_render_layer.h" | |
8 | |
9 namespace { | |
10 | |
11 const int kColorRingsCount = 4; | |
12 const int kFeedbackTexturePixelDiameter = 512; | |
13 const int kFeedbackTexturePixelRadius = kFeedbackTexturePixelDiameter / 2; | |
14 | |
15 // RGBA8888 colors. From inside to outside. | |
16 const uint8_t | |
17 kFeedbackRingColors[kColorRingsCount] | |
18 [remoting::GlRenderLayer::kBytesPerPixelRGB32] = { | |
19 {0, 0, 0, 0}, // transparent black | |
20 {0xff, 0xff, 0xff, 0xff}, // white | |
21 {0, 0, 0, 0xff}, // black | |
22 {0, 0, 0, 0} // transparent black | |
23 }; | |
24 | |
25 const float kFeedbackRadiusStops[kColorRingsCount] = {0.0f, 0.8f, 0.9f, 1.0f}; | |
26 | |
27 } // namespace | |
28 | |
29 namespace remoting { | |
30 | |
31 uint32_t GetColorByRadius(float radius) { | |
Sergey Ulanov
2016/07/21 19:28:48
move to anonymous namespace.
Yuwei
2016/07/21 19:53:04
Also for FillFeedbackTexture?
Sergey Ulanov
2016/07/21 20:14:15
yes
Yuwei
2016/07/21 21:44:41
Done.
| |
32 int ring_index = kColorRingsCount - 1; | |
33 // Find first radius stop that is not smaller than current radius. | |
34 while (radius < kFeedbackRadiusStops[ring_index] && ring_index >= 0) { | |
35 ring_index--; | |
36 } | |
37 | |
38 if (ring_index < 0) { | |
39 NOTREACHED(); | |
40 return -1; | |
41 } | |
42 | |
43 if (ring_index == kColorRingsCount - 1) { | |
44 // Area outside the circle. Just use the outermost color. | |
45 return *reinterpret_cast<const uint32_t*>(kFeedbackRingColors[ring_index]); | |
46 } | |
47 | |
48 const uint8_t* first_color = kFeedbackRingColors[ring_index]; | |
49 const uint8_t* second_color = kFeedbackRingColors[ring_index + 1]; | |
50 float first_radius = kFeedbackRadiusStops[ring_index]; | |
51 float second_radius = kFeedbackRadiusStops[ring_index + 1]; | |
52 | |
53 uint8_t progress = | |
54 (radius - first_radius) * 256 / (second_radius - first_radius); | |
55 uint32_t color; | |
56 uint8_t* color_ptr = reinterpret_cast<uint8_t*>(&color); | |
57 for (int i = 0; i < GlRenderLayer::kBytesPerPixelRGB32; i++) { | |
58 color_ptr[i] = | |
59 (first_color[i] * (256 - progress) + second_color[i] * progress) / 256; | |
60 } | |
61 return color; | |
62 } | |
63 | |
64 void FillFeedbackTexture(uint32_t* texture) { | |
65 for (int x = 0; x < kFeedbackTexturePixelRadius; x++) { | |
66 for (int y = 0; y <= x; y++) { | |
67 float radius = sqrt(x * x + y * y) / kFeedbackTexturePixelRadius; | |
68 uint32_t color = GetColorByRadius(radius); | |
69 | |
70 int x1 = kFeedbackTexturePixelRadius + x; | |
71 int x2 = kFeedbackTexturePixelRadius - 1 - x; | |
72 | |
73 int y1 = kFeedbackTexturePixelRadius + y; | |
74 int y2 = kFeedbackTexturePixelRadius - 1 - y; | |
75 | |
76 texture[x1 + y1 * kFeedbackTexturePixelDiameter] = color; | |
77 texture[x1 + y2 * kFeedbackTexturePixelDiameter] = color; | |
78 texture[x2 + y1 * kFeedbackTexturePixelDiameter] = color; | |
79 texture[x2 + y2 * kFeedbackTexturePixelDiameter] = color; | |
80 texture[y1 + x1 * kFeedbackTexturePixelDiameter] = color; | |
81 texture[y1 + x2 * kFeedbackTexturePixelDiameter] = color; | |
82 texture[y2 + x1 * kFeedbackTexturePixelDiameter] = color; | |
83 texture[y2 + x2 * kFeedbackTexturePixelDiameter] = color; | |
84 } | |
85 } | |
86 } | |
87 | |
88 GlCursorFeedbackTexture* GlCursorFeedbackTexture::GetInstance() { | |
89 return base::Singleton<GlCursorFeedbackTexture>::get(); | |
90 } | |
91 | |
92 const uint8_t* GlCursorFeedbackTexture::GetTexture() const { | |
93 return reinterpret_cast<uint8_t*>(texture_.get()); | |
94 } | |
95 | |
96 int GlCursorFeedbackTexture::GetTextureDiameter() const { | |
97 return kFeedbackTexturePixelDiameter; | |
98 } | |
99 | |
100 GlCursorFeedbackTexture::GlCursorFeedbackTexture() | |
101 : texture_(new uint32_t[kFeedbackTexturePixelDiameter * | |
102 kFeedbackTexturePixelDiameter]) { | |
103 FillFeedbackTexture(texture_.get()); | |
104 } | |
105 | |
106 GlCursorFeedbackTexture::~GlCursorFeedbackTexture() {} | |
107 | |
108 } // namespace remoting | |
OLD | NEW |