blob: 63ae383c8c4d3bb457fc02138efc5c54e66b1678 [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
Dominik Laskowski20134642020-04-20 22:36:44 -070019#include <math/vec4.h>
Alec Mouria90a5702021-04-16 16:36:21 +000020#include <renderengine/RenderEngine.h>
Dominik Laskowski29fa1462021-04-27 15:51:50 -070021#include <ui/LayerStack.h>
Dominik Laskowski20134642020-04-20 22:36:44 -070022#include <ui/Rect.h>
23#include <ui/Size.h>
24#include <utils/StrongPointer.h>
25
rnlee756005b2021-05-27 10:46:36 -070026#include <SkCanvas.h>
27#include <SkColor.h>
Alec Mouria90a5702021-04-16 16:36:21 +000028#include <unordered_map>
29
Marin Shalamanoveadf2e72020-12-10 15:35:28 +010030#include "Fps.h"
Ady Abraham03b02dd2019-03-21 15:40:11 -070031
32namespace android {
33
Dominik Laskowski20134642020-04-20 22:36:44 -070034class Client;
35class GraphicBuffer;
36class IBinder;
37class IGraphicBufferProducer;
38class Layer;
39class SurfaceFlinger;
40
Ady Abraham03b02dd2019-03-21 15:40:11 -070041class RefreshRateOverlay {
42public:
Ady Abraham1b11bc62021-06-03 19:51:19 -070043 RefreshRateOverlay(SurfaceFlinger&, uint32_t lowFps, uint32_t highFps, bool showSpinner);
Ady Abraham03b02dd2019-03-21 15:40:11 -070044
Dominik Laskowski29fa1462021-04-27 15:51:50 -070045 void setLayerStack(ui::LayerStack);
Dominik Laskowski20134642020-04-20 22:36:44 -070046 void setViewport(ui::Size);
Marin Shalamanoveadf2e72020-12-10 15:35:28 +010047 void changeRefreshRate(const Fps&);
Ady Abraham29d0da32020-07-16 18:39:33 -070048 void onInvalidate();
Ady Abraham03b02dd2019-03-21 15:40:11 -070049
50private:
Ady Abraham2cb8b622019-12-02 18:55:33 -080051 class SevenSegmentDrawer {
52 public:
rnlee756005b2021-05-27 10:46:36 -070053 static std::vector<sp<GraphicBuffer>> draw(int number, SkColor& color,
54 ui::Transform::RotationFlags, bool showSpinner);
Ady Abraham2cb8b622019-12-02 18:55:33 -080055 static uint32_t getHeight() { return BUFFER_HEIGHT; }
56 static uint32_t getWidth() { return BUFFER_WIDTH; }
57
58 private:
rnlee756005b2021-05-27 10:46:36 -070059 enum class Segment { Upper, UpperLeft, UpperRight, Middle, LowerLeft, LowerRight, Bottom };
Ady Abraham2cb8b622019-12-02 18:55:33 -080060
rnlee756005b2021-05-27 10:46:36 -070061 static void drawSegment(Segment segment, int left, SkColor& color, SkCanvas& canvas);
62 static void drawDigit(int digit, int left, SkColor& color, SkCanvas& canvas);
Ady Abraham2cb8b622019-12-02 18:55:33 -080063
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 Mouria90a5702021-04-16 16:36:21 +000073 const std::vector<std::shared_ptr<renderengine::ExternalTexture>>& getOrCreateBuffers(
74 uint32_t fps);
Ady Abraham03b02dd2019-03-21 15:40:11 -070075
76 SurfaceFlinger& mFlinger;
Dominik Laskowski20134642020-04-20 22:36:44 -070077 const sp<Client> mClient;
Ady Abraham03b02dd2019-03-21 15:40:11 -070078 sp<Layer> mLayer;
79 sp<IBinder> mIBinder;
80 sp<IGraphicBufferProducer> mGbp;
81
rnlee756005b2021-05-27 10:46:36 -070082 std::unordered_map<
83 ui::Transform::RotationFlags,
84 std::unordered_map<int, std::vector<std::shared_ptr<renderengine::ExternalTexture>>>>
Alec Mouria90a5702021-04-16 16:36:21 +000085 mBufferCache;
Ady Abraham29d0da32020-07-16 18:39:33 -070086 std::optional<int> mCurrentFps;
87 int mFrame = 0;
Ady Abraham2cb8b622019-12-02 18:55:33 -080088 static constexpr float ALPHA = 0.8f;
rnlee756005b2021-05-27 10:46:36 -070089 const SkColor LOW_FPS_COLOR = SK_ColorRED;
90 const SkColor HIGH_FPS_COLOR = SK_ColorGREEN;
Ady Abraham29d0da32020-07-16 18:39:33 -070091
92 const bool mShowSpinner;
Marin Shalamanovf7f6b3c2020-12-09 13:19:38 +010093
94 // Interpolate the colors between these values.
Ady Abraham1b11bc62021-06-03 19:51:19 -070095 const uint32_t mLowFps;
96 const uint32_t mHighFps;
Ady Abraham03b02dd2019-03-21 15:40:11 -070097};
98
Dominik Laskowski20134642020-04-20 22:36:44 -070099} // namespace android