• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <math/vec4.h>
20 #include <renderengine/RenderEngine.h>
21 #include <ui/Rect.h>
22 #include <ui/Size.h>
23 #include <utils/StrongPointer.h>
24 
25 #include <unordered_map>
26 
27 #include "Fps.h"
28 
29 namespace android {
30 
31 class Client;
32 class GraphicBuffer;
33 class IBinder;
34 class IGraphicBufferProducer;
35 class Layer;
36 class SurfaceFlinger;
37 
38 class RefreshRateOverlay {
39 public:
40     RefreshRateOverlay(SurfaceFlinger&, bool showSpinner);
41 
42     void setViewport(ui::Size);
43     void changeRefreshRate(const Fps&);
44     void onInvalidate();
45     void reset();
46 
47 private:
48     class SevenSegmentDrawer {
49     public:
50         static std::vector<sp<GraphicBuffer>> drawNumber(int number, const half4& color,
51                                                          bool showSpinner);
getHeight()52         static uint32_t getHeight() { return BUFFER_HEIGHT; }
getWidth()53         static uint32_t getWidth() { return BUFFER_WIDTH; }
54 
55     private:
56         enum class Segment { Upper, UpperLeft, UpperRight, Middle, LowerLeft, LowerRight, Buttom };
57 
58         static void drawRect(const Rect& r, const half4& color, const sp<GraphicBuffer>& buffer,
59                              uint8_t* pixels);
60         static void drawSegment(Segment segment, int left, const half4& color,
61                                 const sp<GraphicBuffer>& buffer, uint8_t* pixels);
62         static void drawDigit(int digit, int left, const half4& color,
63                               const sp<GraphicBuffer>& buffer, uint8_t* pixels);
64 
65         static constexpr uint32_t DIGIT_HEIGHT = 100;
66         static constexpr uint32_t DIGIT_WIDTH = 64;
67         static constexpr uint32_t DIGIT_SPACE = 16;
68         static constexpr uint32_t BUFFER_HEIGHT = DIGIT_HEIGHT;
69         static constexpr uint32_t BUFFER_WIDTH =
70                 4 * DIGIT_WIDTH + 3 * DIGIT_SPACE; // Digit|Space|Digit|Space|Digit|Space|Spinner
71     };
72 
73     bool createLayer();
74     const std::vector<std::shared_ptr<renderengine::ExternalTexture>>& getOrCreateBuffers(
75             uint32_t fps);
76 
77     SurfaceFlinger& mFlinger;
78     const sp<Client> mClient;
79     sp<Layer> mLayer;
80     sp<IBinder> mIBinder;
81     sp<IGraphicBufferProducer> mGbp;
82 
83     std::unordered_map<int, std::vector<std::shared_ptr<renderengine::ExternalTexture>>>
84             mBufferCache;
85     std::optional<int> mCurrentFps;
86     int mFrame = 0;
87     static constexpr float ALPHA = 0.8f;
88     const half3 LOW_FPS_COLOR = half3(1.0f, 0.0f, 0.0f);
89     const half3 HIGH_FPS_COLOR = half3(0.0f, 1.0f, 0.0f);
90 
91     const bool mShowSpinner;
92 
93     // Interpolate the colors between these values.
94     uint32_t mLowFps;
95     uint32_t mHighFps;
96 };
97 
98 } // namespace android
99