blob: 49ffc8135980eada1ec83100818cc81574b4090b [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 */
16
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -080017// TODO(b/129481165): remove the #pragma below and fix conversion issues
18#pragma clang diagnostic push
19#pragma clang diagnostic ignored "-Wconversion"
Marin Shalamanovbed7fd32020-12-21 20:02:20 +010020#pragma clang diagnostic ignored "-Wextra"
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -080021
Marin Shalamanovf7f6b3c2020-12-09 13:19:38 +010022#include <algorithm>
23
Ady Abraham03b02dd2019-03-21 15:40:11 -070024#include "RefreshRateOverlay.h"
25#include "Client.h"
26#include "Layer.h"
27
Ady Abraham2cb8b622019-12-02 18:55:33 -080028#include <gui/IProducerListener.h>
29
30#undef LOG_TAG
31#define LOG_TAG "RefreshRateOverlay"
32
Ady Abraham03b02dd2019-03-21 15:40:11 -070033namespace android {
34
Ady Abraham2cb8b622019-12-02 18:55:33 -080035void RefreshRateOverlay::SevenSegmentDrawer::drawRect(const Rect& r, const half4& color,
36 const sp<GraphicBuffer>& buffer,
37 uint8_t* pixels) {
38 for (int32_t j = r.top; j < r.bottom; j++) {
39 if (j >= buffer->getHeight()) {
40 break;
41 }
42
43 for (int32_t i = r.left; i < r.right; i++) {
44 if (i >= buffer->getWidth()) {
45 break;
46 }
47
48 uint8_t* iter = pixels + 4 * (i + (buffer->getStride() * j));
49 iter[0] = uint8_t(color.r * 255);
50 iter[1] = uint8_t(color.g * 255);
51 iter[2] = uint8_t(color.b * 255);
52 iter[3] = uint8_t(color.a * 255);
53 }
54 }
55}
56
57void RefreshRateOverlay::SevenSegmentDrawer::drawSegment(Segment segment, int left,
58 const half4& color,
59 const sp<GraphicBuffer>& buffer,
60 uint8_t* pixels) {
61 const Rect rect = [&]() {
62 switch (segment) {
63 case Segment::Upper:
64 return Rect(left, 0, left + DIGIT_WIDTH, DIGIT_SPACE);
65 case Segment::UpperLeft:
66 return Rect(left, 0, left + DIGIT_SPACE, DIGIT_HEIGHT / 2);
67 case Segment::UpperRight:
68 return Rect(left + DIGIT_WIDTH - DIGIT_SPACE, 0, left + DIGIT_WIDTH,
69 DIGIT_HEIGHT / 2);
70 case Segment::Middle:
71 return Rect(left, DIGIT_HEIGHT / 2 - DIGIT_SPACE / 2, left + DIGIT_WIDTH,
72 DIGIT_HEIGHT / 2 + DIGIT_SPACE / 2);
73 case Segment::LowerLeft:
74 return Rect(left, DIGIT_HEIGHT / 2, left + DIGIT_SPACE, DIGIT_HEIGHT);
75 case Segment::LowerRight:
76 return Rect(left + DIGIT_WIDTH - DIGIT_SPACE, DIGIT_HEIGHT / 2, left + DIGIT_WIDTH,
77 DIGIT_HEIGHT);
78 case Segment::Buttom:
79 return Rect(left, DIGIT_HEIGHT - DIGIT_SPACE, left + DIGIT_WIDTH, DIGIT_HEIGHT);
80 }
81 }();
82
83 drawRect(rect, color, buffer, pixels);
84}
85
86void RefreshRateOverlay::SevenSegmentDrawer::drawDigit(int digit, int left, const half4& color,
87 const sp<GraphicBuffer>& buffer,
88 uint8_t* pixels) {
89 if (digit < 0 || digit > 9) return;
90
91 if (digit == 0 || digit == 2 || digit == 3 || digit == 5 || digit == 6 || digit == 7 ||
92 digit == 8 || digit == 9)
93 drawSegment(Segment::Upper, left, color, buffer, pixels);
94 if (digit == 0 || digit == 4 || digit == 5 || digit == 6 || digit == 8 || digit == 9)
95 drawSegment(Segment::UpperLeft, left, color, buffer, pixels);
96 if (digit == 0 || digit == 1 || digit == 2 || digit == 3 || digit == 4 || digit == 7 ||
97 digit == 8 || digit == 9)
98 drawSegment(Segment::UpperRight, left, color, buffer, pixels);
99 if (digit == 2 || digit == 3 || digit == 4 || digit == 5 || digit == 6 || digit == 8 ||
100 digit == 9)
101 drawSegment(Segment::Middle, left, color, buffer, pixels);
102 if (digit == 0 || digit == 2 || digit == 6 || digit == 8)
103 drawSegment(Segment::LowerLeft, left, color, buffer, pixels);
104 if (digit == 0 || digit == 1 || digit == 3 || digit == 4 || digit == 5 || digit == 6 ||
105 digit == 7 || digit == 8 || digit == 9)
106 drawSegment(Segment::LowerRight, left, color, buffer, pixels);
107 if (digit == 0 || digit == 2 || digit == 3 || digit == 5 || digit == 6 || digit == 8 ||
108 digit == 9)
109 drawSegment(Segment::Buttom, left, color, buffer, pixels);
110}
111
Ady Abraham29d0da32020-07-16 18:39:33 -0700112std::vector<sp<GraphicBuffer>> RefreshRateOverlay::SevenSegmentDrawer::drawNumber(
113 int number, const half4& color, bool showSpinner) {
114 if (number < 0 || number > 1000) return {};
Ady Abraham2cb8b622019-12-02 18:55:33 -0800115
116 const auto hundreds = number / 100;
117 const auto tens = (number / 10) % 10;
118 const auto ones = number % 10;
119
Ady Abraham29d0da32020-07-16 18:39:33 -0700120 std::vector<sp<GraphicBuffer>> buffers;
121 const auto loopCount = showSpinner ? 6 : 1;
122 for (int i = 0; i < loopCount; i++) {
123 sp<GraphicBuffer> buffer =
124 new GraphicBuffer(BUFFER_WIDTH, BUFFER_HEIGHT, HAL_PIXEL_FORMAT_RGBA_8888, 1,
125 GRALLOC_USAGE_SW_WRITE_RARELY | GRALLOC_USAGE_HW_COMPOSER |
126 GRALLOC_USAGE_HW_TEXTURE,
127 "RefreshRateOverlayBuffer");
128 uint8_t* pixels;
129 buffer->lock(GRALLOC_USAGE_SW_WRITE_RARELY, reinterpret_cast<void**>(&pixels));
130 // Clear buffer content
131 drawRect(Rect(BUFFER_WIDTH, BUFFER_HEIGHT), half4(0), buffer, pixels);
132 int left = 0;
133 if (hundreds != 0) {
134 drawDigit(hundreds, left, color, buffer, pixels);
135 }
Ady Abraham2cb8b622019-12-02 18:55:33 -0800136 left += DIGIT_WIDTH + DIGIT_SPACE;
Ady Abraham2cb8b622019-12-02 18:55:33 -0800137
Ady Abraham29d0da32020-07-16 18:39:33 -0700138 if (tens != 0) {
139 drawDigit(tens, left, color, buffer, pixels);
140 }
Ady Abraham2cb8b622019-12-02 18:55:33 -0800141 left += DIGIT_WIDTH + DIGIT_SPACE;
Ady Abraham2cb8b622019-12-02 18:55:33 -0800142
Ady Abraham29d0da32020-07-16 18:39:33 -0700143 drawDigit(ones, left, color, buffer, pixels);
144 left += DIGIT_WIDTH + DIGIT_SPACE;
145
146 if (showSpinner) {
147 switch (i) {
148 case 0:
149 drawSegment(Segment::Upper, left, color, buffer, pixels);
150 break;
151 case 1:
152 drawSegment(Segment::UpperRight, left, color, buffer, pixels);
153 break;
154 case 2:
155 drawSegment(Segment::LowerRight, left, color, buffer, pixels);
156 break;
157 case 3:
158 drawSegment(Segment::Buttom, left, color, buffer, pixels);
159 break;
160 case 4:
161 drawSegment(Segment::LowerLeft, left, color, buffer, pixels);
162 break;
163 case 5:
164 drawSegment(Segment::UpperLeft, left, color, buffer, pixels);
165 break;
166 }
167 }
168
169 buffer->unlock();
170 buffers.emplace_back(buffer);
171 }
172 return buffers;
Ady Abraham2cb8b622019-12-02 18:55:33 -0800173}
174
Ady Abraham29d0da32020-07-16 18:39:33 -0700175RefreshRateOverlay::RefreshRateOverlay(SurfaceFlinger& flinger, bool showSpinner)
176 : mFlinger(flinger), mClient(new Client(&mFlinger)), mShowSpinner(showSpinner) {
Ady Abraham03b02dd2019-03-21 15:40:11 -0700177 createLayer();
Marin Shalamanovf7f6b3c2020-12-09 13:19:38 +0100178 reset();
Ady Abraham03b02dd2019-03-21 15:40:11 -0700179}
180
181bool RefreshRateOverlay::createLayer() {
Ady Abraham9f0a4002020-10-05 15:47:26 -0700182 int32_t layerId;
Ady Abraham03b02dd2019-03-21 15:40:11 -0700183 const status_t ret =
Ady Abraham2cb8b622019-12-02 18:55:33 -0800184 mFlinger.createLayer(String8("RefreshRateOverlay"), mClient,
185 SevenSegmentDrawer::getWidth(), SevenSegmentDrawer::getHeight(),
186 PIXEL_FORMAT_RGBA_8888,
187 ISurfaceComposerClient::eFXSurfaceBufferState, LayerMetadata(),
Ady Abraham9f0a4002020-10-05 15:47:26 -0700188 &mIBinder, &mGbp, nullptr, &layerId);
Ady Abraham03b02dd2019-03-21 15:40:11 -0700189 if (ret) {
Ady Abraham2cb8b622019-12-02 18:55:33 -0800190 ALOGE("failed to create buffer state layer");
Ady Abraham03b02dd2019-03-21 15:40:11 -0700191 return false;
192 }
193
Ady Abrahamfed164b2019-05-10 17:12:54 -0700194 Mutex::Autolock _l(mFlinger.mStateLock);
Ady Abraham03b02dd2019-03-21 15:40:11 -0700195 mLayer = mClient->getLayerUser(mIBinder);
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100196 mLayer->setFrameRate(Layer::FrameRate(Fps(0.0f), Layer::FrameRateCompatibility::NoVote));
Ady Abrahamfed164b2019-05-10 17:12:54 -0700197
198 // setting Layer's Z requires resorting layersSortedByZ
199 ssize_t idx = mFlinger.mCurrentState.layersSortedByZ.indexOf(mLayer);
200 if (mLayer->setLayer(INT32_MAX - 2) && idx >= 0) {
201 mFlinger.mCurrentState.layersSortedByZ.removeAt(idx);
202 mFlinger.mCurrentState.layersSortedByZ.add(mLayer);
203 }
Ady Abraham03b02dd2019-03-21 15:40:11 -0700204
205 return true;
206}
207
Marin Shalamanovf7f6b3c2020-12-09 13:19:38 +0100208const std::vector<sp<GraphicBuffer>>& RefreshRateOverlay::getOrCreateBuffers(uint32_t fps) {
209 if (mBufferCache.find(fps) == mBufferCache.end()) {
210 // Ensure the range is > 0, so we don't divide by 0.
211 const auto rangeLength = std::max(1u, mHighFps - mLowFps);
212 // Clip values outside the range [mLowFps, mHighFps]. The current fps may be outside
213 // of this range if the display has changed its set of supported refresh rates.
214 fps = std::max(fps, mLowFps);
215 fps = std::min(fps, mHighFps);
216 const auto fpsScale = static_cast<float>(fps - mLowFps) / rangeLength;
Ady Abraham2cb8b622019-12-02 18:55:33 -0800217 half4 color;
218 color.r = HIGH_FPS_COLOR.r * fpsScale + LOW_FPS_COLOR.r * (1 - fpsScale);
219 color.g = HIGH_FPS_COLOR.g * fpsScale + LOW_FPS_COLOR.g * (1 - fpsScale);
220 color.b = HIGH_FPS_COLOR.b * fpsScale + LOW_FPS_COLOR.b * (1 - fpsScale);
221 color.a = ALPHA;
Ady Abraham29d0da32020-07-16 18:39:33 -0700222 mBufferCache.emplace(fps, SevenSegmentDrawer::drawNumber(fps, color, mShowSpinner));
Ady Abraham2cb8b622019-12-02 18:55:33 -0800223 }
Marin Shalamanovf7f6b3c2020-12-09 13:19:38 +0100224
225 return mBufferCache[fps];
Ady Abraham2cb8b622019-12-02 18:55:33 -0800226}
227
Dominik Laskowski20134642020-04-20 22:36:44 -0700228void RefreshRateOverlay::setViewport(ui::Size viewport) {
Ady Abraham29d0da32020-07-16 18:39:33 -0700229 Rect frame((3 * viewport.width) >> 4, viewport.height >> 5);
Dominik Laskowski20134642020-04-20 22:36:44 -0700230 frame.offsetBy(viewport.width >> 5, viewport.height >> 4);
231 mLayer->setFrame(frame);
Ady Abraham2cb8b622019-12-02 18:55:33 -0800232
Ady Abrahamfe57b762019-04-17 20:06:14 -0700233 mFlinger.mTransactionFlags.fetch_or(eTransactionMask);
Ady Abraham03b02dd2019-03-21 15:40:11 -0700234}
235
Marin Shalamanoveadf2e72020-12-10 15:35:28 +0100236void RefreshRateOverlay::changeRefreshRate(const Fps& fps) {
237 mCurrentFps = fps.getIntValue();
Marin Shalamanovf7f6b3c2020-12-09 13:19:38 +0100238 auto buffer = getOrCreateBuffers(*mCurrentFps)[mFrame];
Ady Abrahamf0c56492020-12-17 18:04:15 -0800239 mLayer->setBuffer(buffer, Fence::NO_FENCE, 0, 0, true, {},
Vishnu Nairadf632b2021-01-07 14:05:08 -0800240 mLayer->getHeadFrameNumber(-1 /* expectedPresentTime */),
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +0000241 std::nullopt /* dequeueTime */, FrameTimelineInfo{});
Ady Abraham29d0da32020-07-16 18:39:33 -0700242
243 mFlinger.mTransactionFlags.fetch_or(eTransactionMask);
244}
245
246void RefreshRateOverlay::onInvalidate() {
247 if (!mCurrentFps.has_value()) return;
248
Marin Shalamanovf7f6b3c2020-12-09 13:19:38 +0100249 const auto& buffers = getOrCreateBuffers(*mCurrentFps);
Ady Abraham29d0da32020-07-16 18:39:33 -0700250 mFrame = (mFrame + 1) % buffers.size();
251 auto buffer = buffers[mFrame];
Ady Abrahamf0c56492020-12-17 18:04:15 -0800252 mLayer->setBuffer(buffer, Fence::NO_FENCE, 0, 0, true, {},
Vishnu Nairadf632b2021-01-07 14:05:08 -0800253 mLayer->getHeadFrameNumber(-1 /* expectedPresentTime */),
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +0000254 std::nullopt /* dequeueTime */, FrameTimelineInfo{});
Dominik Laskowski20134642020-04-20 22:36:44 -0700255
256 mFlinger.mTransactionFlags.fetch_or(eTransactionMask);
257}
258
Marin Shalamanovf7f6b3c2020-12-09 13:19:38 +0100259void RefreshRateOverlay::reset() {
260 mBufferCache.clear();
Marin Shalamanoveadf2e72020-12-10 15:35:28 +0100261 const auto range = mFlinger.mRefreshRateConfigs->getSupportedRefreshRateRange();
262 mLowFps = range.min.getIntValue();
263 mHighFps = range.max.getIntValue();
Marin Shalamanovf7f6b3c2020-12-09 13:19:38 +0100264}
265
Dominik Laskowski20134642020-04-20 22:36:44 -0700266} // namespace android
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800267
268// TODO(b/129481165): remove the #pragma below and fix conversion issues
Marin Shalamanovbed7fd32020-12-21 20:02:20 +0100269#pragma clang diagnostic pop // ignored "-Wconversion -Wextra"