blob: 186e8784c51e67434e716061c24e29f432c3ab94 [file] [log] [blame]
Sally Qi147581b2023-06-27 11:55:34 -07001/**
2 * Copyright (C) 2023 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// #define LOG_NDEBUG 0
17#include <algorithm>
18
19#include "HdrSdrRatioOverlay.h"
20
21#include <SkSurface.h>
22
23#undef LOG_TAG
24#define LOG_TAG "HdrSdrRatioOverlay"
25
26namespace android {
27
28void HdrSdrRatioOverlay::drawNumber(float number, int left, SkColor color, SkCanvas& canvas) {
29 if (!isfinite(number) || number >= 10.f) return;
30 // We assume that the number range is [1.f, 10.f)
31 // and the decimal places are 2.
32 int value = static_cast<int>(number * 100);
33 SegmentDrawer::drawDigit(value / 100, left, color, canvas);
34
35 left += kDigitWidth + kDigitSpace;
36 SegmentDrawer::drawSegment(SegmentDrawer::Segment::DecimalPoint, left, color, canvas);
37 left += kDigitWidth + kDigitSpace;
38
39 SegmentDrawer::drawDigit((value / 10) % 10, left, color, canvas);
40 left += kDigitWidth + kDigitSpace;
41 SegmentDrawer::drawDigit(value % 10, left, color, canvas);
42}
43
44sp<GraphicBuffer> HdrSdrRatioOverlay::draw(float currentHdrSdrRatio, SkColor color,
Sally Qi127d99c2023-07-26 20:31:44 +080045 ui::Transform::RotationFlags rotation,
46 sp<GraphicBuffer>& ringBuffer) {
47 const int32_t bufferWidth = kBufferWidth;
48 const int32_t bufferHeight = kBufferWidth;
Sally Qi147581b2023-06-27 11:55:34 -070049
50 const auto kUsageFlags = static_cast<uint64_t>(
51 GRALLOC_USAGE_SW_WRITE_RARELY | GRALLOC_USAGE_HW_COMPOSER | GRALLOC_USAGE_HW_TEXTURE);
Sally Qi127d99c2023-07-26 20:31:44 +080052
53 // ring buffers here to do double-buffered rendering to avoid
54 // possible tearing and also to reduce memory take-up.
55 if (ringBuffer == nullptr) {
56 ringBuffer = sp<GraphicBuffer>::make(static_cast<uint32_t>(bufferWidth),
57 static_cast<uint32_t>(bufferHeight),
58 HAL_PIXEL_FORMAT_RGBA_8888, 1u, kUsageFlags,
59 "HdrSdrRatioOverlayBuffer");
60 }
61
62 auto& buffer = ringBuffer;
63
64 SkMatrix canvasTransform = SkMatrix();
65 switch (rotation) {
66 case ui::Transform::ROT_90:
67 canvasTransform.setTranslate(bufferHeight, 0);
68 canvasTransform.preRotate(90.f);
69 break;
70 case ui::Transform::ROT_270:
71 canvasTransform.setRotate(270.f, bufferWidth / 2.f, bufferWidth / 2.f);
72 break;
73 default:
74 break;
75 }
Sally Qi147581b2023-06-27 11:55:34 -070076
77 const status_t bufferStatus = buffer->initCheck();
78 LOG_ALWAYS_FATAL_IF(bufferStatus != OK, "HdrSdrRatioOverlay: Buffer failed to allocate: %d",
79 bufferStatus);
80
81 sk_sp<SkSurface> surface =
82 SkSurfaces::Raster(SkImageInfo::MakeN32Premul(bufferWidth, bufferHeight));
83 SkCanvas* canvas = surface->getCanvas();
84 canvas->setMatrix(canvasTransform);
85
86 drawNumber(currentHdrSdrRatio, 0, color, *canvas);
87
88 void* pixels = nullptr;
89 buffer->lock(GRALLOC_USAGE_SW_WRITE_RARELY, reinterpret_cast<void**>(&pixels));
90
91 const SkImageInfo& imageInfo = surface->imageInfo();
92 const size_t dstRowBytes = buffer->getStride() * static_cast<size_t>(imageInfo.bytesPerPixel());
93
94 canvas->readPixels(imageInfo, pixels, dstRowBytes, 0, 0);
95 buffer->unlock();
96 return buffer;
97}
98
99HdrSdrRatioOverlay::HdrSdrRatioOverlay()
100 : mSurfaceControl(
101 SurfaceControlHolder::createSurfaceControlHolder(String8("HdrSdrRatioOverlay"))) {
102 if (!mSurfaceControl) {
103 ALOGE("%s: Failed to create buffer state layer", __func__);
104 return;
105 }
106 SurfaceComposerClient::Transaction()
107 .setLayer(mSurfaceControl->get(), INT32_MAX - 2)
108 .setTrustedOverlay(mSurfaceControl->get(), true)
109 .apply();
110}
111
112void HdrSdrRatioOverlay::changeHdrSdrRatio(float currentHdrSdrRatio) {
113 mCurrentHdrSdrRatio = currentHdrSdrRatio;
114 animate();
115}
116
117void HdrSdrRatioOverlay::setLayerStack(ui::LayerStack stack) {
118 SurfaceComposerClient::Transaction().setLayerStack(mSurfaceControl->get(), stack).apply();
119}
120
121void HdrSdrRatioOverlay::setViewport(ui::Size viewport) {
122 constexpr int32_t kMaxWidth = 1000;
123 const auto width = std::min({kMaxWidth, viewport.width, viewport.height});
124 const auto height = 2 * width;
125 Rect frame((5 * width) >> 4, height >> 5);
126 // set the ratio frame to the top right of the screen
127 frame.offsetBy(viewport.width - frame.width(), height >> 4);
128
129 SurfaceComposerClient::Transaction()
130 .setMatrix(mSurfaceControl->get(), frame.getWidth() / static_cast<float>(kBufferWidth),
131 0, 0, frame.getHeight() / static_cast<float>(kBufferHeight))
132 .setPosition(mSurfaceControl->get(), frame.left, frame.top)
133 .apply();
134}
135
136auto HdrSdrRatioOverlay::getOrCreateBuffers(float currentHdrSdrRatio) -> const sp<GraphicBuffer> {
137 static const sp<GraphicBuffer> kNoBuffer;
138 if (!mSurfaceControl) return kNoBuffer;
139
140 const auto transformHint =
141 static_cast<ui::Transform::RotationFlags>(mSurfaceControl->get()->getTransformHint());
142
143 // Tell SurfaceFlinger about the pre-rotation on the buffer.
144 const auto transform = [&] {
145 switch (transformHint) {
146 case ui::Transform::ROT_90:
147 return ui::Transform::ROT_270;
148 case ui::Transform::ROT_270:
149 return ui::Transform::ROT_90;
150 default:
151 return ui::Transform::ROT_0;
152 }
153 }();
154
155 SurfaceComposerClient::Transaction().setTransform(mSurfaceControl->get(), transform).apply();
156
157 constexpr SkColor kMinRatioColor = SK_ColorBLUE;
158 constexpr SkColor kMaxRatioColor = SK_ColorGREEN;
159 constexpr float kAlpha = 0.8f;
160
161 // 9.f is picked here as ratio range, given that we assume that
162 // hdr/sdr ratio is [1.f, 10.f)
163 const float scale = currentHdrSdrRatio / 9.f;
164
165 SkColor4f colorBase = SkColor4f::FromColor(kMaxRatioColor) * scale;
166 const SkColor4f minRatioColor = SkColor4f::FromColor(kMinRatioColor) * (1 - scale);
167
168 colorBase.fR = colorBase.fR + minRatioColor.fR;
169 colorBase.fG = colorBase.fG + minRatioColor.fG;
170 colorBase.fB = colorBase.fB + minRatioColor.fB;
171 colorBase.fA = kAlpha;
172
173 const SkColor color = colorBase.toSkColor();
174
Sally Qi127d99c2023-07-26 20:31:44 +0800175 auto buffer = draw(currentHdrSdrRatio, color, transformHint, mRingBuffer[mIndex]);
176 mIndex = (mIndex + 1) % 2;
Sally Qi147581b2023-06-27 11:55:34 -0700177 return buffer;
178}
179
180void HdrSdrRatioOverlay::animate() {
181 if (!std::isfinite(mCurrentHdrSdrRatio) || mCurrentHdrSdrRatio < 1.0f) return;
Sally Qi147581b2023-06-27 11:55:34 -0700182 SurfaceComposerClient::Transaction()
183 .setBuffer(mSurfaceControl->get(), getOrCreateBuffers(mCurrentHdrSdrRatio))
184 .apply();
185}
186
187} // namespace android