• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *
3  * Copyright 2019, The Android Open Source Project
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 #include "layout.h"
18 #include <cassert>
19 #include <iostream>
20 #include <localization/ConfirmationUITranslations.h>
21 #include <teeui/example/teeui.h>
22 #include <typeinfo>
23 
24 using namespace teeui;
25 
26 static DeviceInfo sDeviceInfo;
27 static bool sMagnified;
28 static bool sInverted;
29 static std::string sConfirmationMessage;
30 
31 /*
32  * AOSP color scheme constants.
33  */
34 constexpr static const Color kShieldColor = Color(0xff778500);
35 constexpr static const Color kShieldColorInv = Color(0xffc4cb80);
36 constexpr static const Color kTextColor = Color(0xff212121);
37 constexpr static const Color kTextColorInv = Color(0xffdedede);
38 constexpr static const Color kBackGroundColor = Color(0xffffffff);
39 constexpr static const Color kBackGroundColorInv = Color(0xff212121);
40 
setConfirmationMessage(const char * confirmationMessage)41 void setConfirmationMessage(const char* confirmationMessage) {
42     sConfirmationMessage = confirmationMessage;
43 }
44 
alfaCombineChannel(uint32_t shift,double alfa,uint32_t a,uint32_t b)45 uint32_t alfaCombineChannel(uint32_t shift, double alfa, uint32_t a, uint32_t b) {
46     a >>= shift;
47     a &= 0xff;
48     b >>= shift;
49     b &= 0xff;
50     double acc = alfa * a + (1 - alfa) * b;
51     if (acc <= 0) return 0;
52     uint32_t result = acc;
53     if (result > 255) return 255 << shift;
54     return result << shift;
55 }
56 
renderPixel(uint32_t x,uint32_t y,const T & e)57 template <typename T> uint32_t renderPixel(uint32_t x, uint32_t y, const T& e) {
58     return e.bounds_.drawPoint(Point<pxs>(x, y));
59 }
60 
61 
62 struct FrameBuffer {
63     uint32_t left_;
64     uint32_t top_;
65     uint32_t width_;
66     uint32_t height_;
67     uint32_t* buffer_;
68     size_t size_in_elements_;
69     uint32_t lineStride_;
70 
drawPixelFrameBuffer71     Error drawPixel(uint32_t x, uint32_t y, uint32_t color) const {
72         size_t pos = (top_ + y) * lineStride_ + x + left_;
73         if (pos >= size_in_elements_) {
74             return Error::OutOfBoundsDrawing;
75         }
76         double alfa = (color & 0xff000000) >> 24;
77         alfa /= 255.0;
78         auto acc = buffer_[pos];
79         buffer_[pos] = alfaCombineChannel(0, alfa, color, acc) |
80                        alfaCombineChannel(8, alfa, color, acc) |
81                        alfaCombineChannel(16, alfa, color, acc);
82         return Error::OK;
83     }
84 };
85 
86 template <typename... Elements>
drawElements(std::tuple<Elements...> & layout,const PixelDrawer & drawPixel)87 Error drawElements(std::tuple<Elements...>& layout, const PixelDrawer& drawPixel) {
88     // Error::operator|| is overloaded, so we don't get short circuit evaluation.
89     // But we get the first error that occurs. We will still try and draw the remaining
90     // elements in the order they appear in the layout tuple.
91     return (std::get<Elements>(layout).draw(drawPixel) || ...);
92 }
93 
setDeviceInfo(DeviceInfo deviceInfo,bool magnified,bool inverted)94 uint32_t setDeviceInfo(DeviceInfo deviceInfo, bool magnified, bool inverted) {
95     sDeviceInfo = deviceInfo;
96     sMagnified = magnified;
97     sInverted = inverted;
98     return 0;
99 }
100 
selectLanguage(const char * language_id)101 void selectLanguage(const char* language_id) {
102     ConfirmationUITranslations_select_lang_id(language_id);
103 }
104 
translate(LabelImpl * label)105 void translate(LabelImpl* label) {
106     uint64_t textId = label->textId();
107     const char* translation = ConfirmationUITranslations_lookup(textId);
108     label->setText({&translation[0], &translation[strlen(translation)]});
109 }
110 
translateLabels(std::tuple<Elements...> & layout)111 template <typename... Elements> void translateLabels(std::tuple<Elements...>& layout) {
112     translate(&std::get<LabelOK>(layout));
113     translate(&std::get<LabelCancel>(layout));
114     translate(&std::get<LabelTitle>(layout));
115     translate(&std::get<LabelHint>(layout));
116 }
117 
renderUIIntoBuffer(uint32_t x,uint32_t y,uint32_t w,uint32_t h,uint32_t lineStride,uint32_t * buffer,size_t buffer_size_in_elements_not_bytes)118 uint32_t renderUIIntoBuffer(uint32_t x, uint32_t y, uint32_t w, uint32_t h, uint32_t lineStride,
119                             uint32_t* buffer, size_t buffer_size_in_elements_not_bytes) {
120     uint32_t afterLastPixelIndex = 0;
121     if (__builtin_add_overflow(y, h, &afterLastPixelIndex) ||
122         __builtin_add_overflow(afterLastPixelIndex, -1, &afterLastPixelIndex) ||
123         __builtin_mul_overflow(afterLastPixelIndex, lineStride, &afterLastPixelIndex) ||
124         __builtin_add_overflow(afterLastPixelIndex, x, &afterLastPixelIndex) ||
125         __builtin_add_overflow(afterLastPixelIndex, w, &afterLastPixelIndex) ||
126         afterLastPixelIndex > buffer_size_in_elements_not_bytes) {
127         return uint32_t(Error::OutOfBoundsDrawing);
128     }
129     context<ConUIParameters> ctx(sDeviceInfo.mm2px_, sDeviceInfo.dp2px_);
130     ctx.setParam<RightEdgeOfScreen>(pxs(sDeviceInfo.width_));
131     ctx.setParam<BottomOfScreen>(pxs(sDeviceInfo.height_));
132     ctx.setParam<PowerButtonTop>(mms(sDeviceInfo.powerButtonTopMm_));
133     ctx.setParam<PowerButtonBottom>(mms(sDeviceInfo.powerButtonBottomMm_));
134     ctx.setParam<VolUpButtonTop>(mms(sDeviceInfo.volUpButtonTopMm_));
135     ctx.setParam<VolUpButtonBottom>(mms(sDeviceInfo.volUpButtonBottomMm_));
136     if (sMagnified) {
137         ctx.setParam<DefaultFontSize>(18_dp);
138         ctx.setParam<BodyFontSize>(20_dp);
139     } else {
140         ctx.setParam<DefaultFontSize>(14_dp);
141         ctx.setParam<BodyFontSize>(16_dp);
142     }
143 
144     if (sInverted) {
145         ctx.setParam<ShieldColor>(kShieldColorInv);
146         ctx.setParam<ColorText>(kTextColorInv);
147         ctx.setParam<ColorBG>(kBackGroundColorInv);
148     } else {
149         ctx.setParam<ShieldColor>(kShieldColor);
150         ctx.setParam<ColorText>(kTextColor);
151         ctx.setParam<ColorBG>(kBackGroundColor);
152     }
153 
154     auto layoutInstance = instantiateLayout(ConfUILayout(), ctx);
155 
156     translateLabels(layoutInstance);
157 
158     uint32_t* begin = buffer + (y * lineStride + x);
159 
160     Color bgColor = sInverted ? kBackGroundColorInv : kBackGroundColor;
161 
162     for (uint32_t yi = 0; yi < h; ++yi) {
163         for (uint32_t xi = 0; xi < w; ++xi) {
164             begin[xi] = bgColor;
165         }
166         begin += lineStride;
167     }
168     FrameBuffer fb;
169     fb.left_ = x;
170     fb.top_ = y;
171     fb.width_ = w;
172     fb.height_ = h;
173     fb.buffer_ = buffer;
174     fb.size_in_elements_ = buffer_size_in_elements_not_bytes;
175     fb.lineStride_ = lineStride;
176 
177     auto pixelDrawer = makePixelDrawer(
178         [&fb](uint32_t x, uint32_t y, Color color) -> Error { return fb.drawPixel(x, y, color); });
179 
180     std::get<LabelBody>(layoutInstance)
181         .setText({&*sConfirmationMessage.begin(), &*sConfirmationMessage.end()});
182 
183     if (auto error = drawElements(layoutInstance, pixelDrawer)) {
184         return uint32_t(error.code());
185     }
186 
187     return 0;  // OK
188 }
189