blob: c16cfa07a4b9cda9fe71044deec87406d8fa1520 [file] [log] [blame]
Ady Abraham03b02dd2019-03-21 15:40:11 -07001/*
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 */
Dominik Laskowski20134642020-04-20 22:36:44 -070016
Ady Abraham03b02dd2019-03-21 15:40:11 -070017#pragma once
18
Alec Mouri617752f2021-04-15 16:27:01 +000019#include <unordered_map>
20
Dominik Laskowski20134642020-04-20 22:36:44 -070021#include <math/vec4.h>
22#include <ui/Rect.h>
23#include <ui/Size.h>
24#include <utils/StrongPointer.h>
25
Marin Shalamanoveadf2e72020-12-10 15:35:28 +010026#include "Fps.h"
Ady Abraham03b02dd2019-03-21 15:40:11 -070027
28namespace android {
29
Dominik Laskowski20134642020-04-20 22:36:44 -070030class Client;
31class GraphicBuffer;
32class IBinder;
33class IGraphicBufferProducer;
34class Layer;
35class SurfaceFlinger;
36
Ady Abraham03b02dd2019-03-21 15:40:11 -070037class RefreshRateOverlay {
38public:
Ady Abraham29d0da32020-07-16 18:39:33 -070039 RefreshRateOverlay(SurfaceFlinger&, bool showSpinner);
Ady Abraham03b02dd2019-03-21 15:40:11 -070040
Dominik Laskowski20134642020-04-20 22:36:44 -070041 void setViewport(ui::Size);
Marin Shalamanoveadf2e72020-12-10 15:35:28 +010042 void changeRefreshRate(const Fps&);
Ady Abraham29d0da32020-07-16 18:39:33 -070043 void onInvalidate();
Marin Shalamanovf7f6b3c2020-12-09 13:19:38 +010044 void reset();
Ady Abraham03b02dd2019-03-21 15:40:11 -070045
46private:
Ady Abraham2cb8b622019-12-02 18:55:33 -080047 class SevenSegmentDrawer {
48 public:
Ady Abraham29d0da32020-07-16 18:39:33 -070049 static std::vector<sp<GraphicBuffer>> drawNumber(int number, const half4& color,
50 bool showSpinner);
Ady Abraham2cb8b622019-12-02 18:55:33 -080051 static uint32_t getHeight() { return BUFFER_HEIGHT; }
52 static uint32_t getWidth() { return BUFFER_WIDTH; }
53
54 private:
55 enum class Segment { Upper, UpperLeft, UpperRight, Middle, LowerLeft, LowerRight, Buttom };
56
57 static void drawRect(const Rect& r, const half4& color, const sp<GraphicBuffer>& buffer,
58 uint8_t* pixels);
59 static void drawSegment(Segment segment, int left, const half4& color,
60 const sp<GraphicBuffer>& buffer, uint8_t* pixels);
61 static void drawDigit(int digit, int left, const half4& color,
62 const sp<GraphicBuffer>& buffer, uint8_t* pixels);
63
64 static constexpr uint32_t DIGIT_HEIGHT = 100;
65 static constexpr uint32_t DIGIT_WIDTH = 64;
66 static constexpr uint32_t DIGIT_SPACE = 16;
67 static constexpr uint32_t BUFFER_HEIGHT = DIGIT_HEIGHT;
68 static constexpr uint32_t BUFFER_WIDTH =
Ady Abraham29d0da32020-07-16 18:39:33 -070069 4 * DIGIT_WIDTH + 3 * DIGIT_SPACE; // Digit|Space|Digit|Space|Digit|Space|Spinner
Ady Abraham2cb8b622019-12-02 18:55:33 -080070 };
71
Ady Abraham03b02dd2019-03-21 15:40:11 -070072 bool createLayer();
Alec Mouri617752f2021-04-15 16:27:01 +000073 const std::vector<sp<GraphicBuffer>>& getOrCreateBuffers(uint32_t fps);
Ady Abraham03b02dd2019-03-21 15:40:11 -070074
75 SurfaceFlinger& mFlinger;
Dominik Laskowski20134642020-04-20 22:36:44 -070076 const sp<Client> mClient;
Ady Abraham03b02dd2019-03-21 15:40:11 -070077 sp<Layer> mLayer;
78 sp<IBinder> mIBinder;
79 sp<IGraphicBufferProducer> mGbp;
80
Alec Mouri617752f2021-04-15 16:27:01 +000081 std::unordered_map<int, std::vector<sp<GraphicBuffer>>> mBufferCache;
Ady Abraham29d0da32020-07-16 18:39:33 -070082 std::optional<int> mCurrentFps;
83 int mFrame = 0;
Ady Abraham2cb8b622019-12-02 18:55:33 -080084 static constexpr float ALPHA = 0.8f;
85 const half3 LOW_FPS_COLOR = half3(1.0f, 0.0f, 0.0f);
86 const half3 HIGH_FPS_COLOR = half3(0.0f, 1.0f, 0.0f);
Ady Abraham29d0da32020-07-16 18:39:33 -070087
88 const bool mShowSpinner;
Marin Shalamanovf7f6b3c2020-12-09 13:19:38 +010089
90 // Interpolate the colors between these values.
91 uint32_t mLowFps;
92 uint32_t mHighFps;
Ady Abraham03b02dd2019-03-21 15:40:11 -070093};
94
Dominik Laskowski20134642020-04-20 22:36:44 -070095} // namespace android