| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2007 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 |  | 
| Mike Lockwood | ebf9a0d | 2014-10-02 16:08:47 -0700 | [diff] [blame] | 17 | #define LOG_NDEBUG 0 | 
| Mathias Agopian | bc72611 | 2009-09-23 15:44:05 -0700 | [diff] [blame] | 18 | #define LOG_TAG "BootAnimation" | 
| Parth Sane | 7f7b221 | 2023-04-12 17:23:02 +0000 | [diff] [blame] | 19 | #define ATRACE_TAG ATRACE_TAG_GRAPHICS | 
| Mathias Agopian | bc72611 | 2009-09-23 15:44:05 -0700 | [diff] [blame] | 20 |  | 
| Nikita Ioffe | 06c986e | 2020-01-27 17:16:03 +0000 | [diff] [blame] | 21 | #include <vector> | 
|  | 22 |  | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 23 | #include <stdint.h> | 
| Wei Wang | a90c54c | 2017-02-02 11:35:21 -0800 | [diff] [blame] | 24 | #include <inttypes.h> | 
| Damien Bargiacchi | 9748086 | 2016-03-29 14:55:55 -0700 | [diff] [blame] | 25 | #include <sys/inotify.h> | 
|  | 26 | #include <sys/poll.h> | 
|  | 27 | #include <sys/stat.h> | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 28 | #include <sys/types.h> | 
|  | 29 | #include <math.h> | 
|  | 30 | #include <fcntl.h> | 
|  | 31 | #include <utils/misc.h> | 
| Parth Sane | 7f7b221 | 2023-04-12 17:23:02 +0000 | [diff] [blame] | 32 | #include <utils/Trace.h> | 
| Mathias Agopian | b4d5a72 | 2009-09-23 17:05:19 -0700 | [diff] [blame] | 33 | #include <signal.h> | 
| Elliott Hughes | bb94f31 | 2014-10-21 10:41:33 -0700 | [diff] [blame] | 34 | #include <time.h> | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 35 |  | 
| Steven Moreland | fb7952f | 2018-02-23 14:58:50 -0800 | [diff] [blame] | 36 | #include <cutils/atomic.h> | 
| Jason parks | bd9a08d | 2011-01-31 15:04:34 -0600 | [diff] [blame] | 37 | #include <cutils/properties.h> | 
|  | 38 |  | 
| Derek Sollenberger | 22d0d4c7 | 2020-04-08 10:53:46 -0400 | [diff] [blame] | 39 | #include <android/imagedecoder.h> | 
| Mathias Agopian | b13b9bd | 2012-02-17 18:27:36 -0800 | [diff] [blame] | 40 | #include <androidfw/AssetManager.h> | 
| Mathias Agopian | ac31a3b | 2009-05-21 19:59:24 -0700 | [diff] [blame] | 41 | #include <binder/IPCThreadState.h> | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 42 | #include <utils/Errors.h> | 
|  | 43 | #include <utils/Log.h> | 
| Wei Wang | a90c54c | 2017-02-02 11:35:21 -0800 | [diff] [blame] | 44 | #include <utils/SystemClock.h> | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 45 |  | 
| Keun-young Park | b593842 | 2017-03-23 13:46:24 -0700 | [diff] [blame] | 46 | #include <android-base/properties.h> | 
|  | 47 |  | 
| Marin Shalamanov | cb783ca | 2021-01-29 21:26:07 +0100 | [diff] [blame] | 48 | #include <ui/DisplayMode.h> | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 49 | #include <ui/PixelFormat.h> | 
|  | 50 | #include <ui/Rect.h> | 
|  | 51 | #include <ui/Region.h> | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 52 |  | 
| Jeff Brown | 0b722fe | 2012-08-24 22:40:14 -0700 | [diff] [blame] | 53 | #include <gui/ISurfaceComposer.h> | 
| Marin Shalamanov | 9070c05 | 2020-05-18 19:05:17 +0200 | [diff] [blame] | 54 | #include <gui/DisplayEventReceiver.h> | 
| Mathias Agopian | 8335f1c | 2012-02-25 18:48:35 -0800 | [diff] [blame] | 55 | #include <gui/Surface.h> | 
|  | 56 | #include <gui/SurfaceComposerClient.h> | 
| Lucas Dupin | 8eedc02 | 2021-07-29 21:49:07 +0000 | [diff] [blame] | 57 | #include <GLES2/gl2.h> | 
|  | 58 | #include <GLES2/gl2ext.h> | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 59 | #include <EGL/eglext.h> | 
|  | 60 |  | 
|  | 61 | #include "BootAnimation.h" | 
|  | 62 |  | 
| Kyeongkab.Nam | 35a8a11 | 2019-07-17 16:41:48 +0900 | [diff] [blame] | 63 | #define ANIM_PATH_MAX 255 | 
|  | 64 | #define STR(x)   #x | 
|  | 65 | #define STRTO(x) STR(x) | 
|  | 66 |  | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 67 | namespace android { | 
|  | 68 |  | 
| Marin Shalamanov | cb783ca | 2021-01-29 21:26:07 +0100 | [diff] [blame] | 69 | using ui::DisplayMode; | 
|  | 70 |  | 
| Damien Bargiacchi | 9748086 | 2016-03-29 14:55:55 -0700 | [diff] [blame] | 71 | static const char OEM_BOOTANIMATION_FILE[] = "/oem/media/bootanimation.zip"; | 
| Beverly | 34ffe25 | 2019-05-17 11:03:54 -0400 | [diff] [blame] | 72 | static const char PRODUCT_BOOTANIMATION_DARK_FILE[] = "/product/media/bootanimation-dark.zip"; | 
| Jaekyun Seok | c521bb3 | 2018-01-30 11:52:47 +0900 | [diff] [blame] | 73 | static const char PRODUCT_BOOTANIMATION_FILE[] = "/product/media/bootanimation.zip"; | 
| Damien Bargiacchi | 9748086 | 2016-03-29 14:55:55 -0700 | [diff] [blame] | 74 | static const char SYSTEM_BOOTANIMATION_FILE[] = "/system/media/bootanimation.zip"; | 
| Anders Fridlund | e0b4d23 | 2018-11-06 14:23:25 +0100 | [diff] [blame] | 75 | static const char APEX_BOOTANIMATION_FILE[] = "/apex/com.android.bootanimation/etc/bootanimation.zip"; | 
| Keun-young Park | b593842 | 2017-03-23 13:46:24 -0700 | [diff] [blame] | 76 | static const char OEM_SHUTDOWNANIMATION_FILE[] = "/oem/media/shutdownanimation.zip"; | 
| Jaekyun Seok | c521bb3 | 2018-01-30 11:52:47 +0900 | [diff] [blame] | 77 | static const char PRODUCT_SHUTDOWNANIMATION_FILE[] = "/product/media/shutdownanimation.zip"; | 
| Keun-young Park | b593842 | 2017-03-23 13:46:24 -0700 | [diff] [blame] | 78 | static const char SYSTEM_SHUTDOWNANIMATION_FILE[] = "/system/media/shutdownanimation.zip"; | 
|  | 79 |  | 
| Nikita Ioffe | 06c986e | 2020-01-27 17:16:03 +0000 | [diff] [blame] | 80 | static constexpr const char* PRODUCT_USERSPACE_REBOOT_ANIMATION_FILE = "/product/media/userspace-reboot.zip"; | 
|  | 81 | static constexpr const char* OEM_USERSPACE_REBOOT_ANIMATION_FILE = "/oem/media/userspace-reboot.zip"; | 
|  | 82 | static constexpr const char* SYSTEM_USERSPACE_REBOOT_ANIMATION_FILE = "/system/media/userspace-reboot.zip"; | 
|  | 83 |  | 
| Josh Yang | f95b200 | 2021-12-23 14:32:28 -0800 | [diff] [blame] | 84 | static const char BOOTANIM_DATA_DIR_PATH[] = "/data/bootanim"; | 
|  | 85 | static const char BOOTANIM_TIME_DIR_NAME[] = "time"; | 
|  | 86 | static const char BOOTANIM_TIME_DIR_PATH[] = "/data/bootanim/time"; | 
| Damien Bargiacchi | 0e3d2ab | 2016-08-29 04:11:19 -0700 | [diff] [blame] | 87 | static const char CLOCK_FONT_ASSET[] = "images/clock_font.png"; | 
|  | 88 | static const char CLOCK_FONT_ZIP_NAME[] = "clock_font.png"; | 
| Nicolas Geoffray | 41fffb4 | 2020-12-15 10:50:29 +0000 | [diff] [blame] | 89 | static const char PROGRESS_FONT_ASSET[] = "images/progress_font.png"; | 
|  | 90 | static const char PROGRESS_FONT_ZIP_NAME[] = "progress_font.png"; | 
| Damien Bargiacchi | 9748086 | 2016-03-29 14:55:55 -0700 | [diff] [blame] | 91 | static const char LAST_TIME_CHANGED_FILE_NAME[] = "last_time_change"; | 
| Josh Yang | f95b200 | 2021-12-23 14:32:28 -0800 | [diff] [blame] | 92 | static const char LAST_TIME_CHANGED_FILE_PATH[] = "/data/bootanim/time/last_time_change"; | 
| Damien Bargiacchi | 9748086 | 2016-03-29 14:55:55 -0700 | [diff] [blame] | 93 | static const char ACCURATE_TIME_FLAG_FILE_NAME[] = "time_is_accurate"; | 
| Josh Yang | f95b200 | 2021-12-23 14:32:28 -0800 | [diff] [blame] | 94 | static const char ACCURATE_TIME_FLAG_FILE_PATH[] = "/data/bootanim/time/time_is_accurate"; | 
|  | 95 | static const char TIME_FORMAT_12_HOUR_FLAG_FILE_PATH[] = "/data/bootanim/time/time_format_12_hour"; | 
| Damien Bargiacchi | 9676281 | 2016-07-12 15:53:40 -0700 | [diff] [blame] | 96 | // Java timestamp format. Don't show the clock if the date is before 2000-01-01 00:00:00. | 
|  | 97 | static const long long ACCURATE_TIME_EPOCH = 946684800000; | 
| Damien Bargiacchi | 0e3d2ab | 2016-08-29 04:11:19 -0700 | [diff] [blame] | 98 | static constexpr char FONT_BEGIN_CHAR = ' '; | 
|  | 99 | static constexpr char FONT_END_CHAR = '~' + 1; | 
|  | 100 | static constexpr size_t FONT_NUM_CHARS = FONT_END_CHAR - FONT_BEGIN_CHAR + 1; | 
|  | 101 | static constexpr size_t FONT_NUM_COLS = 16; | 
|  | 102 | static constexpr size_t FONT_NUM_ROWS = FONT_NUM_CHARS / FONT_NUM_COLS; | 
|  | 103 | static const int TEXT_CENTER_VALUE = INT_MAX; | 
|  | 104 | static const int TEXT_MISSING_VALUE = INT_MIN; | 
| Damien Bargiacchi | 9748086 | 2016-03-29 14:55:55 -0700 | [diff] [blame] | 105 | static const char EXIT_PROP_NAME[] = "service.bootanim.exit"; | 
| Nicolas Geoffray | 41fffb4 | 2020-12-15 10:50:29 +0000 | [diff] [blame] | 106 | static const char PROGRESS_PROP_NAME[] = "service.bootanim.progress"; | 
| Huihong Luo | d4a217e | 2020-01-16 15:56:33 -0800 | [diff] [blame] | 107 | static const char DISPLAYS_PROP_NAME[] = "persist.service.bootanim.displays"; | 
| Thiébaud Weksteen | 08de443 | 2021-11-18 13:25:59 +1100 | [diff] [blame] | 108 | static const char CLOCK_ENABLED_PROP_NAME[] = "persist.sys.bootanim.clock.enabled"; | 
| Kyeongkab.Nam | 35a8a11 | 2019-07-17 16:41:48 +0900 | [diff] [blame] | 109 | static const int ANIM_ENTRY_NAME_MAX = ANIM_PATH_MAX + 1; | 
| niuwenchao | 9cd6ac8 | 2022-12-23 17:34:22 +0800 | [diff] [blame] | 110 | static const int MAX_CHECK_EXIT_INTERVAL_US = 50000; | 
| Damien Bargiacchi | 0e3d2ab | 2016-08-29 04:11:19 -0700 | [diff] [blame] | 111 | static constexpr size_t TEXT_POS_LEN_MAX = 16; | 
| Shan Huang | 2b31a36 | 2021-08-16 16:45:39 +0000 | [diff] [blame] | 112 | static const int DYNAMIC_COLOR_COUNT = 4; | 
| Lucas Dupin | 8eedc02 | 2021-07-29 21:49:07 +0000 | [diff] [blame] | 113 | static const char U_TEXTURE[] = "uTexture"; | 
|  | 114 | static const char U_FADE[] = "uFade"; | 
|  | 115 | static const char U_CROP_AREA[] = "uCropArea"; | 
| Shan Huang | 2b31a36 | 2021-08-16 16:45:39 +0000 | [diff] [blame] | 116 | static const char U_START_COLOR_PREFIX[] = "uStartColor"; | 
|  | 117 | static const char U_END_COLOR_PREFIX[] = "uEndColor"; | 
|  | 118 | static const char U_COLOR_PROGRESS[] = "uColorProgress"; | 
| Lucas Dupin | 8eedc02 | 2021-07-29 21:49:07 +0000 | [diff] [blame] | 119 | static const char A_UV[] = "aUv"; | 
|  | 120 | static const char A_POSITION[] = "aPosition"; | 
|  | 121 | static const char VERTEX_SHADER_SOURCE[] = R"( | 
|  | 122 | precision mediump float; | 
|  | 123 | attribute vec4 aPosition; | 
|  | 124 | attribute highp vec2 aUv; | 
|  | 125 | varying highp vec2 vUv; | 
|  | 126 | void main() { | 
|  | 127 | gl_Position = aPosition; | 
|  | 128 | vUv = aUv; | 
|  | 129 | })"; | 
| Shan Huang | 2b31a36 | 2021-08-16 16:45:39 +0000 | [diff] [blame] | 130 | static const char IMAGE_FRAG_DYNAMIC_COLORING_SHADER_SOURCE[] = R"( | 
|  | 131 | precision mediump float; | 
| Shan Huang | 81846ba | 2021-09-07 19:07:47 +0000 | [diff] [blame] | 132 | const float cWhiteMaskThreshold = 0.05; | 
| Shan Huang | 2b31a36 | 2021-08-16 16:45:39 +0000 | [diff] [blame] | 133 | uniform sampler2D uTexture; | 
|  | 134 | uniform float uFade; | 
|  | 135 | uniform float uColorProgress; | 
| Andrew Zeng | f9f5822 | 2022-11-08 11:10:48 -0800 | [diff] [blame] | 136 | uniform vec3 uStartColor0; | 
|  | 137 | uniform vec3 uStartColor1; | 
|  | 138 | uniform vec3 uStartColor2; | 
|  | 139 | uniform vec3 uStartColor3; | 
|  | 140 | uniform vec3 uEndColor0; | 
|  | 141 | uniform vec3 uEndColor1; | 
|  | 142 | uniform vec3 uEndColor2; | 
|  | 143 | uniform vec3 uEndColor3; | 
| Shan Huang | 2b31a36 | 2021-08-16 16:45:39 +0000 | [diff] [blame] | 144 | varying highp vec2 vUv; | 
|  | 145 | void main() { | 
|  | 146 | vec4 mask = texture2D(uTexture, vUv); | 
| Shan Huang | 488ff74 | 2021-08-25 20:10:46 +0000 | [diff] [blame] | 147 | float r = mask.r; | 
|  | 148 | float g = mask.g; | 
|  | 149 | float b = mask.b; | 
|  | 150 | float a = mask.a; | 
|  | 151 | // If all channels have values, render pixel as a shade of white. | 
|  | 152 | float useWhiteMask = step(cWhiteMaskThreshold, r) | 
|  | 153 | * step(cWhiteMaskThreshold, g) | 
|  | 154 | * step(cWhiteMaskThreshold, b) | 
|  | 155 | * step(cWhiteMaskThreshold, a); | 
| Andrew Zeng | f9f5822 | 2022-11-08 11:10:48 -0800 | [diff] [blame] | 156 | vec3 color = r * mix(uStartColor0, uEndColor0, uColorProgress) | 
| Shan Huang | 488ff74 | 2021-08-25 20:10:46 +0000 | [diff] [blame] | 157 | + g * mix(uStartColor1, uEndColor1, uColorProgress) | 
|  | 158 | + b * mix(uStartColor2, uEndColor2, uColorProgress) | 
|  | 159 | + a * mix(uStartColor3, uEndColor3, uColorProgress); | 
| Andrew Zeng | f9f5822 | 2022-11-08 11:10:48 -0800 | [diff] [blame] | 160 | color = mix(color, vec3((r + g + b + a) * 0.25), useWhiteMask); | 
|  | 161 | gl_FragColor = vec4(color.x, color.y, color.z, (1.0 - uFade)); | 
| Shan Huang | 2b31a36 | 2021-08-16 16:45:39 +0000 | [diff] [blame] | 162 | })"; | 
| Lucas Dupin | 8eedc02 | 2021-07-29 21:49:07 +0000 | [diff] [blame] | 163 | static const char IMAGE_FRAG_SHADER_SOURCE[] = R"( | 
|  | 164 | precision mediump float; | 
|  | 165 | uniform sampler2D uTexture; | 
|  | 166 | uniform float uFade; | 
|  | 167 | varying highp vec2 vUv; | 
|  | 168 | void main() { | 
|  | 169 | vec4 color = texture2D(uTexture, vUv); | 
| Shan Huang | 2b31a36 | 2021-08-16 16:45:39 +0000 | [diff] [blame] | 170 | gl_FragColor = vec4(color.x, color.y, color.z, (1.0 - uFade)) * color.a; | 
| Lucas Dupin | 8eedc02 | 2021-07-29 21:49:07 +0000 | [diff] [blame] | 171 | })"; | 
|  | 172 | static const char TEXT_FRAG_SHADER_SOURCE[] = R"( | 
|  | 173 | precision mediump float; | 
|  | 174 | uniform sampler2D uTexture; | 
|  | 175 | uniform vec4 uCropArea; | 
|  | 176 | varying highp vec2 vUv; | 
|  | 177 | void main() { | 
|  | 178 | vec2 uv = vec2(mix(uCropArea.x, uCropArea.z, vUv.x), | 
|  | 179 | mix(uCropArea.y, uCropArea.w, vUv.y)); | 
|  | 180 | gl_FragColor = texture2D(uTexture, uv); | 
|  | 181 | })"; | 
|  | 182 |  | 
|  | 183 | static GLfloat quadPositions[] = { | 
|  | 184 | -0.5f, -0.5f, | 
|  | 185 | +0.5f, -0.5f, | 
|  | 186 | +0.5f, +0.5f, | 
|  | 187 | +0.5f, +0.5f, | 
|  | 188 | -0.5f, +0.5f, | 
|  | 189 | -0.5f, -0.5f | 
|  | 190 | }; | 
|  | 191 | static GLfloat quadUVs[] = { | 
|  | 192 | 0.0f, 1.0f, | 
|  | 193 | 1.0f, 1.0f, | 
|  | 194 | 1.0f, 0.0f, | 
|  | 195 | 1.0f, 0.0f, | 
|  | 196 | 0.0f, 0.0f, | 
|  | 197 | 0.0f, 1.0f | 
|  | 198 | }; | 
| Narayan Kamath | afd31e0 | 2013-12-03 13:16:03 +0000 | [diff] [blame] | 199 |  | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 200 | // --------------------------------------------------------------------------- | 
|  | 201 |  | 
| Ed Coyne | 7464ac9 | 2017-06-08 12:26:48 -0700 | [diff] [blame] | 202 | BootAnimation::BootAnimation(sp<Callbacks> callbacks) | 
| Marin Shalamanov | 9070c05 | 2020-05-18 19:05:17 +0200 | [diff] [blame] | 203 | : Thread(false), mLooper(new Looper(false)), mClockEnabled(true), mTimeIsAccurate(false), | 
| Yi Kong | 911ac23 | 2019-03-24 01:49:02 -0700 | [diff] [blame] | 204 | mTimeFormat12Hour(false), mTimeCheckThread(nullptr), mCallbacks(callbacks) { | 
| Parth Sane | 7f7b221 | 2023-04-12 17:23:02 +0000 | [diff] [blame] | 205 | ATRACE_CALL(); | 
| Mathias Agopian | 627e7b5 | 2009-05-21 19:21:59 -0700 | [diff] [blame] | 206 | mSession = new SurfaceComposerClient(); | 
| Geoffrey Pitsch | 290c435 | 2016-08-09 14:35:10 -0400 | [diff] [blame] | 207 |  | 
| Keun-young Park | b593842 | 2017-03-23 13:46:24 -0700 | [diff] [blame] | 208 | std::string powerCtl = android::base::GetProperty("sys.powerctl", ""); | 
|  | 209 | if (powerCtl.empty()) { | 
|  | 210 | mShuttingDown = false; | 
|  | 211 | } else { | 
|  | 212 | mShuttingDown = true; | 
|  | 213 | } | 
| Huihong Luo | 50a2f36 | 2018-08-11 09:27:57 -0700 | [diff] [blame] | 214 | ALOGD("%sAnimationStartTiming start time: %" PRId64 "ms", mShuttingDown ? "Shutdown" : "Boot", | 
|  | 215 | elapsedRealtime()); | 
|  | 216 | } | 
|  | 217 |  | 
|  | 218 | BootAnimation::~BootAnimation() { | 
| Parth Sane | 7f7b221 | 2023-04-12 17:23:02 +0000 | [diff] [blame] | 219 | ATRACE_CALL(); | 
| Huihong Luo | 50a2f36 | 2018-08-11 09:27:57 -0700 | [diff] [blame] | 220 | if (mAnimation != nullptr) { | 
|  | 221 | releaseAnimation(mAnimation); | 
|  | 222 | mAnimation = nullptr; | 
|  | 223 | } | 
|  | 224 | ALOGD("%sAnimationStopTiming start time: %" PRId64 "ms", mShuttingDown ? "Shutdown" : "Boot", | 
|  | 225 | elapsedRealtime()); | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 226 | } | 
|  | 227 |  | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 228 | void BootAnimation::onFirstRef() { | 
| Parth Sane | 7f7b221 | 2023-04-12 17:23:02 +0000 | [diff] [blame] | 229 | ATRACE_CALL(); | 
| Mathias Agopian | bc72611 | 2009-09-23 15:44:05 -0700 | [diff] [blame] | 230 | status_t err = mSession->linkToComposerDeath(this); | 
| Wei Wang | 159a410 | 2018-10-23 23:15:58 -0700 | [diff] [blame] | 231 | SLOGE_IF(err, "linkToComposerDeath failed (%s) ", strerror(-err)); | 
| Mathias Agopian | 8434c53 | 2009-09-23 18:52:49 -0700 | [diff] [blame] | 232 | if (err == NO_ERROR) { | 
| Huihong Luo | 50a2f36 | 2018-08-11 09:27:57 -0700 | [diff] [blame] | 233 | // Load the animation content -- this can be slow (eg 200ms) | 
|  | 234 | // called before waitForSurfaceFlinger() in main() to avoid wait | 
|  | 235 | ALOGD("%sAnimationPreloadTiming start time: %" PRId64 "ms", | 
|  | 236 | mShuttingDown ? "Shutdown" : "Boot", elapsedRealtime()); | 
|  | 237 | preloadAnimation(); | 
|  | 238 | ALOGD("%sAnimationPreloadStopTiming start time: %" PRId64 "ms", | 
|  | 239 | mShuttingDown ? "Shutdown" : "Boot", elapsedRealtime()); | 
| Mathias Agopian | bc72611 | 2009-09-23 15:44:05 -0700 | [diff] [blame] | 240 | } | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 241 | } | 
|  | 242 |  | 
| Mathias Agopian | bc72611 | 2009-09-23 15:44:05 -0700 | [diff] [blame] | 243 | sp<SurfaceComposerClient> BootAnimation::session() const { | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 244 | return mSession; | 
|  | 245 | } | 
|  | 246 |  | 
| Marin Shalamanov | 9070c05 | 2020-05-18 19:05:17 +0200 | [diff] [blame] | 247 | void BootAnimation::binderDied(const wp<IBinder>&) { | 
| Parth Sane | 7f7b221 | 2023-04-12 17:23:02 +0000 | [diff] [blame] | 248 | ATRACE_CALL(); | 
| Mathias Agopian | bc72611 | 2009-09-23 15:44:05 -0700 | [diff] [blame] | 249 | // woah, surfaceflinger died! | 
| Wei Wang | 159a410 | 2018-10-23 23:15:58 -0700 | [diff] [blame] | 250 | SLOGD("SurfaceFlinger died, exiting..."); | 
| Mathias Agopian | bc72611 | 2009-09-23 15:44:05 -0700 | [diff] [blame] | 251 |  | 
|  | 252 | // calling requestExit() is not enough here because the Surface code | 
|  | 253 | // might be blocked on a condition variable that will never be updated. | 
|  | 254 | kill( getpid(), SIGKILL ); | 
|  | 255 | requestExit(); | 
|  | 256 | } | 
|  | 257 |  | 
| Shan Huang | 2b31a36 | 2021-08-16 16:45:39 +0000 | [diff] [blame] | 258 | static void* decodeImage(const void* encodedData, size_t dataLength, AndroidBitmapInfo* outInfo, | 
|  | 259 | bool premultiplyAlpha) { | 
| Parth Sane | 7f7b221 | 2023-04-12 17:23:02 +0000 | [diff] [blame] | 260 | ATRACE_CALL(); | 
| Derek Sollenberger | 22d0d4c7 | 2020-04-08 10:53:46 -0400 | [diff] [blame] | 261 | AImageDecoder* decoder = nullptr; | 
|  | 262 | AImageDecoder_createFromBuffer(encodedData, dataLength, &decoder); | 
|  | 263 | if (!decoder) { | 
|  | 264 | return nullptr; | 
|  | 265 | } | 
|  | 266 |  | 
|  | 267 | const AImageDecoderHeaderInfo* info = AImageDecoder_getHeaderInfo(decoder); | 
|  | 268 | outInfo->width = AImageDecoderHeaderInfo_getWidth(info); | 
|  | 269 | outInfo->height = AImageDecoderHeaderInfo_getHeight(info); | 
|  | 270 | outInfo->format = AImageDecoderHeaderInfo_getAndroidBitmapFormat(info); | 
|  | 271 | outInfo->stride = AImageDecoder_getMinimumStride(decoder); | 
|  | 272 | outInfo->flags = 0; | 
|  | 273 |  | 
| Shan Huang | 2b31a36 | 2021-08-16 16:45:39 +0000 | [diff] [blame] | 274 | if (!premultiplyAlpha) { | 
|  | 275 | AImageDecoder_setUnpremultipliedRequired(decoder, true); | 
|  | 276 | } | 
|  | 277 |  | 
| Derek Sollenberger | 22d0d4c7 | 2020-04-08 10:53:46 -0400 | [diff] [blame] | 278 | const size_t size = outInfo->stride * outInfo->height; | 
|  | 279 | void* pixels = malloc(size); | 
|  | 280 | int result = AImageDecoder_decodeImage(decoder, pixels, outInfo->stride, size); | 
|  | 281 | AImageDecoder_delete(decoder); | 
|  | 282 |  | 
|  | 283 | if (result != ANDROID_IMAGE_DECODER_SUCCESS) { | 
|  | 284 | free(pixels); | 
|  | 285 | return nullptr; | 
|  | 286 | } | 
|  | 287 | return pixels; | 
|  | 288 | } | 
|  | 289 |  | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 290 | status_t BootAnimation::initTexture(Texture* texture, AssetManager& assets, | 
| Shan Huang | 2b31a36 | 2021-08-16 16:45:39 +0000 | [diff] [blame] | 291 | const char* name, bool premultiplyAlpha) { | 
| Parth Sane | 7f7b221 | 2023-04-12 17:23:02 +0000 | [diff] [blame] | 292 | ATRACE_CALL(); | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 293 | Asset* asset = assets.open(name, Asset::ACCESS_BUFFER); | 
| Yi Kong | 911ac23 | 2019-03-24 01:49:02 -0700 | [diff] [blame] | 294 | if (asset == nullptr) | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 295 | return NO_INIT; | 
| Derek Sollenberger | 22d0d4c7 | 2020-04-08 10:53:46 -0400 | [diff] [blame] | 296 |  | 
|  | 297 | AndroidBitmapInfo bitmapInfo; | 
| Shan Huang | 2b31a36 | 2021-08-16 16:45:39 +0000 | [diff] [blame] | 298 | void* pixels = decodeImage(asset->getBuffer(false), asset->getLength(), &bitmapInfo, | 
|  | 299 | premultiplyAlpha); | 
| Derek Sollenberger | 22d0d4c7 | 2020-04-08 10:53:46 -0400 | [diff] [blame] | 300 | auto pixelDeleter = std::unique_ptr<void, decltype(free)*>{ pixels, free }; | 
|  | 301 |  | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 302 | asset->close(); | 
|  | 303 | delete asset; | 
|  | 304 |  | 
| Derek Sollenberger | 22d0d4c7 | 2020-04-08 10:53:46 -0400 | [diff] [blame] | 305 | if (!pixels) { | 
|  | 306 | return NO_INIT; | 
|  | 307 | } | 
|  | 308 |  | 
|  | 309 | const int w = bitmapInfo.width; | 
|  | 310 | const int h = bitmapInfo.height; | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 311 |  | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 312 | texture->w = w; | 
|  | 313 | texture->h = h; | 
|  | 314 |  | 
|  | 315 | glGenTextures(1, &texture->name); | 
|  | 316 | glBindTexture(GL_TEXTURE_2D, texture->name); | 
|  | 317 |  | 
| Derek Sollenberger | 22d0d4c7 | 2020-04-08 10:53:46 -0400 | [diff] [blame] | 318 | switch (bitmapInfo.format) { | 
|  | 319 | case ANDROID_BITMAP_FORMAT_A_8: | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 320 | glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, w, h, 0, GL_ALPHA, | 
| Derek Sollenberger | 22d0d4c7 | 2020-04-08 10:53:46 -0400 | [diff] [blame] | 321 | GL_UNSIGNED_BYTE, pixels); | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 322 | break; | 
| Derek Sollenberger | 22d0d4c7 | 2020-04-08 10:53:46 -0400 | [diff] [blame] | 323 | case ANDROID_BITMAP_FORMAT_RGBA_4444: | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 324 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, | 
| Derek Sollenberger | 22d0d4c7 | 2020-04-08 10:53:46 -0400 | [diff] [blame] | 325 | GL_UNSIGNED_SHORT_4_4_4_4, pixels); | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 326 | break; | 
| Derek Sollenberger | 22d0d4c7 | 2020-04-08 10:53:46 -0400 | [diff] [blame] | 327 | case ANDROID_BITMAP_FORMAT_RGBA_8888: | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 328 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, | 
| Derek Sollenberger | 22d0d4c7 | 2020-04-08 10:53:46 -0400 | [diff] [blame] | 329 | GL_UNSIGNED_BYTE, pixels); | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 330 | break; | 
| Derek Sollenberger | 22d0d4c7 | 2020-04-08 10:53:46 -0400 | [diff] [blame] | 331 | case ANDROID_BITMAP_FORMAT_RGB_565: | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 332 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB, | 
| Derek Sollenberger | 22d0d4c7 | 2020-04-08 10:53:46 -0400 | [diff] [blame] | 333 | GL_UNSIGNED_SHORT_5_6_5, pixels); | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 334 | break; | 
|  | 335 | default: | 
|  | 336 | break; | 
|  | 337 | } | 
|  | 338 |  | 
| Lucas Dupin | 8eedc02 | 2021-07-29 21:49:07 +0000 | [diff] [blame] | 339 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); | 
|  | 340 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); | 
|  | 341 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); | 
|  | 342 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); | 
| Damien Bargiacchi | 0e3d2ab | 2016-08-29 04:11:19 -0700 | [diff] [blame] | 343 |  | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 344 | return NO_ERROR; | 
|  | 345 | } | 
|  | 346 |  | 
| Shan Huang | 2b31a36 | 2021-08-16 16:45:39 +0000 | [diff] [blame] | 347 | status_t BootAnimation::initTexture(FileMap* map, int* width, int* height, | 
|  | 348 | bool premultiplyAlpha) { | 
| Parth Sane | 7f7b221 | 2023-04-12 17:23:02 +0000 | [diff] [blame] | 349 | ATRACE_CALL(); | 
| Derek Sollenberger | 22d0d4c7 | 2020-04-08 10:53:46 -0400 | [diff] [blame] | 350 | AndroidBitmapInfo bitmapInfo; | 
| Shan Huang | 2b31a36 | 2021-08-16 16:45:39 +0000 | [diff] [blame] | 351 | void* pixels = decodeImage(map->getDataPtr(), map->getDataLength(), &bitmapInfo, | 
|  | 352 | premultiplyAlpha); | 
| Derek Sollenberger | 22d0d4c7 | 2020-04-08 10:53:46 -0400 | [diff] [blame] | 353 | auto pixelDeleter = std::unique_ptr<void, decltype(free)*>{ pixels, free }; | 
| Mathias Agopian | a8826d6 | 2009-10-01 03:10:14 -0700 | [diff] [blame] | 354 |  | 
| Mykola Kondratenko | 0c1eeb3 | 2014-04-15 09:35:44 +0200 | [diff] [blame] | 355 | // FileMap memory is never released until application exit. | 
|  | 356 | // Release it now as the texture is already loaded and the memory used for | 
|  | 357 | // the packed resource can be released. | 
| Damien Bargiacchi | 0e3d2ab | 2016-08-29 04:11:19 -0700 | [diff] [blame] | 358 | delete map; | 
| Mykola Kondratenko | 0c1eeb3 | 2014-04-15 09:35:44 +0200 | [diff] [blame] | 359 |  | 
| Derek Sollenberger | 22d0d4c7 | 2020-04-08 10:53:46 -0400 | [diff] [blame] | 360 | if (!pixels) { | 
|  | 361 | return NO_INIT; | 
|  | 362 | } | 
|  | 363 |  | 
|  | 364 | const int w = bitmapInfo.width; | 
|  | 365 | const int h = bitmapInfo.height; | 
| Mathias Agopian | a8826d6 | 2009-10-01 03:10:14 -0700 | [diff] [blame] | 366 |  | 
| Mathias Agopian | a8826d6 | 2009-10-01 03:10:14 -0700 | [diff] [blame] | 367 | int tw = 1 << (31 - __builtin_clz(w)); | 
|  | 368 | int th = 1 << (31 - __builtin_clz(h)); | 
|  | 369 | if (tw < w) tw <<= 1; | 
|  | 370 | if (th < h) th <<= 1; | 
|  | 371 |  | 
| Derek Sollenberger | 22d0d4c7 | 2020-04-08 10:53:46 -0400 | [diff] [blame] | 372 | switch (bitmapInfo.format) { | 
|  | 373 | case ANDROID_BITMAP_FORMAT_RGBA_8888: | 
| Sai Kiran Korwar | 2716749 | 2015-07-07 20:00:06 +0530 | [diff] [blame] | 374 | if (!mUseNpotTextures && (tw != w || th != h)) { | 
| Mathias Agopian | a8826d6 | 2009-10-01 03:10:14 -0700 | [diff] [blame] | 375 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tw, th, 0, GL_RGBA, | 
| Yi Kong | 911ac23 | 2019-03-24 01:49:02 -0700 | [diff] [blame] | 376 | GL_UNSIGNED_BYTE, nullptr); | 
| Mathias Agopian | a8826d6 | 2009-10-01 03:10:14 -0700 | [diff] [blame] | 377 | glTexSubImage2D(GL_TEXTURE_2D, 0, | 
| Derek Sollenberger | 22d0d4c7 | 2020-04-08 10:53:46 -0400 | [diff] [blame] | 378 | 0, 0, w, h, GL_RGBA, GL_UNSIGNED_BYTE, pixels); | 
| Mathias Agopian | a8826d6 | 2009-10-01 03:10:14 -0700 | [diff] [blame] | 379 | } else { | 
| Sai Kiran Korwar | 2716749 | 2015-07-07 20:00:06 +0530 | [diff] [blame] | 380 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, | 
| Derek Sollenberger | 22d0d4c7 | 2020-04-08 10:53:46 -0400 | [diff] [blame] | 381 | GL_UNSIGNED_BYTE, pixels); | 
| Mathias Agopian | a8826d6 | 2009-10-01 03:10:14 -0700 | [diff] [blame] | 382 | } | 
|  | 383 | break; | 
|  | 384 |  | 
| Derek Sollenberger | 22d0d4c7 | 2020-04-08 10:53:46 -0400 | [diff] [blame] | 385 | case ANDROID_BITMAP_FORMAT_RGB_565: | 
| Sai Kiran Korwar | 2716749 | 2015-07-07 20:00:06 +0530 | [diff] [blame] | 386 | if (!mUseNpotTextures && (tw != w || th != h)) { | 
| Mathias Agopian | a8826d6 | 2009-10-01 03:10:14 -0700 | [diff] [blame] | 387 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, tw, th, 0, GL_RGB, | 
| Yi Kong | 911ac23 | 2019-03-24 01:49:02 -0700 | [diff] [blame] | 388 | GL_UNSIGNED_SHORT_5_6_5, nullptr); | 
| Mathias Agopian | a8826d6 | 2009-10-01 03:10:14 -0700 | [diff] [blame] | 389 | glTexSubImage2D(GL_TEXTURE_2D, 0, | 
| Derek Sollenberger | 22d0d4c7 | 2020-04-08 10:53:46 -0400 | [diff] [blame] | 390 | 0, 0, w, h, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, pixels); | 
| Mathias Agopian | a8826d6 | 2009-10-01 03:10:14 -0700 | [diff] [blame] | 391 | } else { | 
| Sai Kiran Korwar | 2716749 | 2015-07-07 20:00:06 +0530 | [diff] [blame] | 392 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB, | 
| Derek Sollenberger | 22d0d4c7 | 2020-04-08 10:53:46 -0400 | [diff] [blame] | 393 | GL_UNSIGNED_SHORT_5_6_5, pixels); | 
| Mathias Agopian | a8826d6 | 2009-10-01 03:10:14 -0700 | [diff] [blame] | 394 | } | 
|  | 395 | break; | 
|  | 396 | default: | 
|  | 397 | break; | 
|  | 398 | } | 
|  | 399 |  | 
| Lucas Dupin | 8eedc02 | 2021-07-29 21:49:07 +0000 | [diff] [blame] | 400 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); | 
|  | 401 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); | 
|  | 402 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); | 
|  | 403 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); | 
| Mathias Agopian | a8826d6 | 2009-10-01 03:10:14 -0700 | [diff] [blame] | 404 |  | 
| Damien Bargiacchi | 0e3d2ab | 2016-08-29 04:11:19 -0700 | [diff] [blame] | 405 | *width = w; | 
|  | 406 | *height = h; | 
|  | 407 |  | 
| Mathias Agopian | a8826d6 | 2009-10-01 03:10:14 -0700 | [diff] [blame] | 408 | return NO_ERROR; | 
|  | 409 | } | 
|  | 410 |  | 
| Marin Shalamanov | 9070c05 | 2020-05-18 19:05:17 +0200 | [diff] [blame] | 411 | class BootAnimation::DisplayEventCallback : public LooperCallback { | 
|  | 412 | BootAnimation* mBootAnimation; | 
|  | 413 |  | 
|  | 414 | public: | 
|  | 415 | DisplayEventCallback(BootAnimation* bootAnimation) { | 
| Parth Sane | 7f7b221 | 2023-04-12 17:23:02 +0000 | [diff] [blame] | 416 | ATRACE_CALL(); | 
| Marin Shalamanov | 9070c05 | 2020-05-18 19:05:17 +0200 | [diff] [blame] | 417 | mBootAnimation = bootAnimation; | 
|  | 418 | } | 
|  | 419 |  | 
|  | 420 | int handleEvent(int /* fd */, int events, void* /* data */) { | 
| Parth Sane | 7f7b221 | 2023-04-12 17:23:02 +0000 | [diff] [blame] | 421 | ATRACE_CALL(); | 
| Marin Shalamanov | 9070c05 | 2020-05-18 19:05:17 +0200 | [diff] [blame] | 422 | if (events & (Looper::EVENT_ERROR | Looper::EVENT_HANGUP)) { | 
|  | 423 | ALOGE("Display event receiver pipe was closed or an error occurred. events=0x%x", | 
|  | 424 | events); | 
|  | 425 | return 0; // remove the callback | 
|  | 426 | } | 
|  | 427 |  | 
|  | 428 | if (!(events & Looper::EVENT_INPUT)) { | 
|  | 429 | ALOGW("Received spurious callback for unhandled poll event.  events=0x%x", events); | 
|  | 430 | return 1; // keep the callback | 
|  | 431 | } | 
|  | 432 |  | 
|  | 433 | constexpr int kBufferSize = 100; | 
|  | 434 | DisplayEventReceiver::Event buffer[kBufferSize]; | 
|  | 435 | ssize_t numEvents; | 
|  | 436 | do { | 
|  | 437 | numEvents = mBootAnimation->mDisplayEventReceiver->getEvents(buffer, kBufferSize); | 
|  | 438 | for (size_t i = 0; i < static_cast<size_t>(numEvents); i++) { | 
|  | 439 | const auto& event = buffer[i]; | 
|  | 440 | if (event.header.type == DisplayEventReceiver::DISPLAY_EVENT_HOTPLUG) { | 
|  | 441 | SLOGV("Hotplug received"); | 
|  | 442 |  | 
|  | 443 | if (!event.hotplug.connected) { | 
|  | 444 | // ignore hotplug disconnect | 
|  | 445 | continue; | 
|  | 446 | } | 
|  | 447 | auto token = SurfaceComposerClient::getPhysicalDisplayToken( | 
|  | 448 | event.header.displayId); | 
|  | 449 |  | 
|  | 450 | if (token != mBootAnimation->mDisplayToken) { | 
|  | 451 | // ignore hotplug of a secondary display | 
|  | 452 | continue; | 
|  | 453 | } | 
|  | 454 |  | 
| Marin Shalamanov | cb783ca | 2021-01-29 21:26:07 +0100 | [diff] [blame] | 455 | DisplayMode displayMode; | 
|  | 456 | const status_t error = SurfaceComposerClient::getActiveDisplayMode( | 
|  | 457 | mBootAnimation->mDisplayToken, &displayMode); | 
| Marin Shalamanov | 9070c05 | 2020-05-18 19:05:17 +0200 | [diff] [blame] | 458 | if (error != NO_ERROR) { | 
| Marin Shalamanov | cb783ca | 2021-01-29 21:26:07 +0100 | [diff] [blame] | 459 | SLOGE("Can't get active display mode."); | 
| Marin Shalamanov | 9070c05 | 2020-05-18 19:05:17 +0200 | [diff] [blame] | 460 | } | 
| Marin Shalamanov | cb783ca | 2021-01-29 21:26:07 +0100 | [diff] [blame] | 461 | mBootAnimation->resizeSurface(displayMode.resolution.getWidth(), | 
|  | 462 | displayMode.resolution.getHeight()); | 
| Marin Shalamanov | 9070c05 | 2020-05-18 19:05:17 +0200 | [diff] [blame] | 463 | } | 
|  | 464 | } | 
|  | 465 | } while (numEvents > 0); | 
|  | 466 |  | 
|  | 467 | return 1;  // keep the callback | 
|  | 468 | } | 
|  | 469 | }; | 
|  | 470 |  | 
|  | 471 | EGLConfig BootAnimation::getEglConfig(const EGLDisplay& display) { | 
|  | 472 | const EGLint attribs[] = { | 
| Andrii Chepurnyi | d93f7b2 | 2023-03-15 11:22:07 +0200 | [diff] [blame] | 473 | EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, | 
| Marin Shalamanov | 9070c05 | 2020-05-18 19:05:17 +0200 | [diff] [blame] | 474 | EGL_RED_SIZE,   8, | 
|  | 475 | EGL_GREEN_SIZE, 8, | 
|  | 476 | EGL_BLUE_SIZE,  8, | 
|  | 477 | EGL_DEPTH_SIZE, 0, | 
|  | 478 | EGL_NONE | 
|  | 479 | }; | 
|  | 480 | EGLint numConfigs; | 
|  | 481 | EGLConfig config; | 
|  | 482 | eglChooseConfig(display, attribs, &config, 1, &numConfigs); | 
|  | 483 | return config; | 
|  | 484 | } | 
|  | 485 |  | 
| Marin Shalamanov | 047802d | 2020-05-19 23:55:12 +0200 | [diff] [blame] | 486 | ui::Size BootAnimation::limitSurfaceSize(int width, int height) const { | 
|  | 487 | ui::Size limited(width, height); | 
|  | 488 | bool wasLimited = false; | 
|  | 489 | const float aspectRatio = float(width) / float(height); | 
|  | 490 | if (mMaxWidth != 0 && width > mMaxWidth) { | 
|  | 491 | limited.height = mMaxWidth / aspectRatio; | 
|  | 492 | limited.width = mMaxWidth; | 
|  | 493 | wasLimited = true; | 
|  | 494 | } | 
|  | 495 | if (mMaxHeight != 0 && limited.height > mMaxHeight) { | 
|  | 496 | limited.height = mMaxHeight; | 
|  | 497 | limited.width = mMaxHeight * aspectRatio; | 
|  | 498 | wasLimited = true; | 
|  | 499 | } | 
|  | 500 | SLOGV_IF(wasLimited, "Surface size has been limited to [%dx%d] from [%dx%d]", | 
|  | 501 | limited.width, limited.height, width, height); | 
|  | 502 | return limited; | 
|  | 503 | } | 
|  | 504 |  | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 505 | status_t BootAnimation::readyToRun() { | 
| Parth Sane | 7f7b221 | 2023-04-12 17:23:02 +0000 | [diff] [blame] | 506 | ATRACE_CALL(); | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 507 | mAssets.addDefaultAssets(); | 
|  | 508 |  | 
| Huihong Luo | 4430510 | 2022-09-26 11:16:20 -0700 | [diff] [blame] | 509 | const std::vector<PhysicalDisplayId> ids = SurfaceComposerClient::getPhysicalDisplayIds(); | 
|  | 510 | if (ids.empty()) { | 
|  | 511 | SLOGE("Failed to get ID for any displays\n"); | 
| Dominik Laskowski | 69b281d | 2019-11-22 14:13:12 -0800 | [diff] [blame] | 512 | return NAME_NOT_FOUND; | 
| Huihong Luo | 4430510 | 2022-09-26 11:16:20 -0700 | [diff] [blame] | 513 | } | 
| Dominik Laskowski | 3316a0a | 2019-01-25 02:56:41 -0800 | [diff] [blame] | 514 |  | 
| Huihong Luo | 4430510 | 2022-09-26 11:16:20 -0700 | [diff] [blame] | 515 | // this system property specifies multi-display IDs to show the boot animation | 
| Huihong Luo | f97c9e2 | 2019-05-29 11:08:01 -0700 | [diff] [blame] | 516 | // multiple ids can be set with comma (,) as separator, for example: | 
| Huihong Luo | d4a217e | 2020-01-16 15:56:33 -0800 | [diff] [blame] | 517 | // setprop persist.boot.animation.displays 19260422155234049,19261083906282754 | 
| Marin Shalamanov | 11599ee | 2020-07-27 21:31:48 +0200 | [diff] [blame] | 518 | Vector<PhysicalDisplayId> physicalDisplayIds; | 
| Huihong Luo | f97c9e2 | 2019-05-29 11:08:01 -0700 | [diff] [blame] | 519 | char displayValue[PROPERTY_VALUE_MAX] = ""; | 
| Huihong Luo | d4a217e | 2020-01-16 15:56:33 -0800 | [diff] [blame] | 520 | property_get(DISPLAYS_PROP_NAME, displayValue, ""); | 
| Huihong Luo | f97c9e2 | 2019-05-29 11:08:01 -0700 | [diff] [blame] | 521 | bool isValid = displayValue[0] != '\0'; | 
|  | 522 | if (isValid) { | 
|  | 523 | char *p = displayValue; | 
|  | 524 | while (*p) { | 
|  | 525 | if (!isdigit(*p) && *p != ',') { | 
|  | 526 | isValid = false; | 
|  | 527 | break; | 
|  | 528 | } | 
|  | 529 | p ++; | 
|  | 530 | } | 
|  | 531 | if (!isValid) | 
| Huihong Luo | d4a217e | 2020-01-16 15:56:33 -0800 | [diff] [blame] | 532 | SLOGE("Invalid syntax for the value of system prop: %s", DISPLAYS_PROP_NAME); | 
| Huihong Luo | f97c9e2 | 2019-05-29 11:08:01 -0700 | [diff] [blame] | 533 | } | 
|  | 534 | if (isValid) { | 
|  | 535 | std::istringstream stream(displayValue); | 
| Marin Shalamanov | 11599ee | 2020-07-27 21:31:48 +0200 | [diff] [blame] | 536 | for (PhysicalDisplayId id; stream >> id.value; ) { | 
| Huihong Luo | f97c9e2 | 2019-05-29 11:08:01 -0700 | [diff] [blame] | 537 | physicalDisplayIds.add(id); | 
|  | 538 | if (stream.peek() == ',') | 
|  | 539 | stream.ignore(); | 
|  | 540 | } | 
|  | 541 |  | 
| Huihong Luo | 4430510 | 2022-09-26 11:16:20 -0700 | [diff] [blame] | 542 | // the first specified display id is used to retrieve mDisplayToken | 
|  | 543 | for (const auto id : physicalDisplayIds) { | 
|  | 544 | if (std::find(ids.begin(), ids.end(), id) != ids.end()) { | 
|  | 545 | if (const auto token = SurfaceComposerClient::getPhysicalDisplayToken(id)) { | 
|  | 546 | mDisplayToken = token; | 
|  | 547 | break; | 
|  | 548 | } | 
|  | 549 | } | 
|  | 550 | } | 
|  | 551 | } | 
|  | 552 |  | 
|  | 553 | // If the system property is not present or invalid, display 0 is used | 
|  | 554 | if (mDisplayToken == nullptr) { | 
|  | 555 | mDisplayToken = SurfaceComposerClient::getPhysicalDisplayToken(ids.front()); | 
|  | 556 | if (mDisplayToken == nullptr) { | 
|  | 557 | return NAME_NOT_FOUND; | 
|  | 558 | } | 
|  | 559 | } | 
|  | 560 |  | 
|  | 561 | DisplayMode displayMode; | 
|  | 562 | const status_t error = | 
|  | 563 | SurfaceComposerClient::getActiveDisplayMode(mDisplayToken, &displayMode); | 
|  | 564 | if (error != NO_ERROR) { | 
|  | 565 | return error; | 
|  | 566 | } | 
|  | 567 |  | 
|  | 568 | mMaxWidth = android::base::GetIntProperty("ro.surface_flinger.max_graphics_width", 0); | 
|  | 569 | mMaxHeight = android::base::GetIntProperty("ro.surface_flinger.max_graphics_height", 0); | 
|  | 570 | ui::Size resolution = displayMode.resolution; | 
|  | 571 | resolution = limitSurfaceSize(resolution.width, resolution.height); | 
|  | 572 | // create the native surface | 
|  | 573 | sp<SurfaceControl> control = session()->createSurface(String8("BootAnimation"), | 
|  | 574 | resolution.getWidth(), resolution.getHeight(), PIXEL_FORMAT_RGB_565, | 
|  | 575 | ISurfaceComposerClient::eOpaque); | 
|  | 576 |  | 
|  | 577 | SurfaceComposerClient::Transaction t; | 
|  | 578 | if (isValid) { | 
| Huihong Luo | f97c9e2 | 2019-05-29 11:08:01 -0700 | [diff] [blame] | 579 | // In the case of multi-display, boot animation shows on the specified displays | 
| Dominik Laskowski | 989de3d | 2021-07-27 21:09:34 -0700 | [diff] [blame] | 580 | for (const auto id : physicalDisplayIds) { | 
| Huihong Luo | f97c9e2 | 2019-05-29 11:08:01 -0700 | [diff] [blame] | 581 | if (std::find(ids.begin(), ids.end(), id) != ids.end()) { | 
| Dominik Laskowski | 989de3d | 2021-07-27 21:09:34 -0700 | [diff] [blame] | 582 | if (const auto token = SurfaceComposerClient::getPhysicalDisplayToken(id)) { | 
|  | 583 | t.setDisplayLayerStack(token, ui::DEFAULT_LAYER_STACK); | 
|  | 584 | } | 
| Huihong Luo | f97c9e2 | 2019-05-29 11:08:01 -0700 | [diff] [blame] | 585 | } | 
|  | 586 | } | 
| Dominik Laskowski | 989de3d | 2021-07-27 21:09:34 -0700 | [diff] [blame] | 587 | t.setLayerStack(control, ui::DEFAULT_LAYER_STACK); | 
| Huihong Luo | f97c9e2 | 2019-05-29 11:08:01 -0700 | [diff] [blame] | 588 | } | 
|  | 589 |  | 
| Robert Carr | e13b58e | 2017-08-31 14:50:44 -0700 | [diff] [blame] | 590 | t.setLayer(control, 0x40000000) | 
|  | 591 | .apply(); | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 592 |  | 
| Mathias Agopian | 17f638b | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 593 | sp<Surface> s = control->getSurface(); | 
|  | 594 |  | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 595 | // initialize opengl and egl | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 596 | EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY); | 
| Yi Kong | 911ac23 | 2019-03-24 01:49:02 -0700 | [diff] [blame] | 597 | eglInitialize(display, nullptr, nullptr); | 
| Marin Shalamanov | 9070c05 | 2020-05-18 19:05:17 +0200 | [diff] [blame] | 598 | EGLConfig config = getEglConfig(display); | 
|  | 599 | EGLSurface surface = eglCreateWindowSurface(display, config, s.get(), nullptr); | 
| Lucas Dupin | 8eedc02 | 2021-07-29 21:49:07 +0000 | [diff] [blame] | 600 | // Initialize egl context with client version number 2.0. | 
|  | 601 | EGLint contextAttributes[] = {EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE}; | 
|  | 602 | EGLContext context = eglCreateContext(display, config, nullptr, contextAttributes); | 
| Marin Shalamanov | 9070c05 | 2020-05-18 19:05:17 +0200 | [diff] [blame] | 603 | EGLint w, h; | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 604 | eglQuerySurface(display, surface, EGL_WIDTH, &w); | 
|  | 605 | eglQuerySurface(display, surface, EGL_HEIGHT, &h); | 
| Mathias Agopian | a8826d6 | 2009-10-01 03:10:14 -0700 | [diff] [blame] | 606 |  | 
| Huihong Luo | 4430510 | 2022-09-26 11:16:20 -0700 | [diff] [blame] | 607 | if (eglMakeCurrent(display, surface, surface, context) == EGL_FALSE) { | 
| Mathias Agopian | abac010 | 2009-07-31 14:47:00 -0700 | [diff] [blame] | 608 | return NO_INIT; | 
| Huihong Luo | 4430510 | 2022-09-26 11:16:20 -0700 | [diff] [blame] | 609 | } | 
| Mathias Agopian | a8826d6 | 2009-10-01 03:10:14 -0700 | [diff] [blame] | 610 |  | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 611 | mDisplay = display; | 
|  | 612 | mContext = context; | 
|  | 613 | mSurface = surface; | 
| joenchen | 764ea04 | 2022-04-11 23:58:34 +0800 | [diff] [blame] | 614 | mInitWidth = mWidth = w; | 
|  | 615 | mInitHeight = mHeight = h; | 
| Mathias Agopian | 17f638b | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 616 | mFlingerSurfaceControl = control; | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 617 | mFlingerSurface = s; | 
| Adrian Roos | 9ee5dff | 2018-08-22 20:19:49 +0200 | [diff] [blame] | 618 | mTargetInset = -1; | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 619 |  | 
| Mariia Sandrikova | a166f01 | 2022-12-10 02:32:14 +0000 | [diff] [blame] | 620 | // Rotate the boot animation according to the value specified in the sysprop | 
|  | 621 | // ro.bootanim.set_orientation_<display_id>. Four values are supported: ORIENTATION_0, | 
|  | 622 | // ORIENTATION_90, ORIENTATION_180 and ORIENTATION_270. | 
|  | 623 | // If the value isn't specified or is ORIENTATION_0, nothing will be changed. | 
|  | 624 | // This is needed to support having boot animation in orientations different from the natural | 
|  | 625 | // device orientation. For example, on tablets that may want to keep natural orientation | 
|  | 626 | // portrait for applications compatibility and to have the boot animation in landscape. | 
|  | 627 | rotateAwayFromNaturalOrientationIfNeeded(); | 
|  | 628 |  | 
| Yegor Malyshev | 6925e1b | 2020-09-10 16:42:20 +0200 | [diff] [blame] | 629 | projectSceneToWindow(); | 
|  | 630 |  | 
| Marin Shalamanov | 9070c05 | 2020-05-18 19:05:17 +0200 | [diff] [blame] | 631 | // Register a display event receiver | 
|  | 632 | mDisplayEventReceiver = std::make_unique<DisplayEventReceiver>(); | 
|  | 633 | status_t status = mDisplayEventReceiver->initCheck(); | 
|  | 634 | SLOGE_IF(status != NO_ERROR, "Initialization of DisplayEventReceiver failed with status: %d", | 
|  | 635 | status); | 
|  | 636 | mLooper->addFd(mDisplayEventReceiver->getFd(), 0, Looper::EVENT_INPUT, | 
|  | 637 | new DisplayEventCallback(this), nullptr); | 
|  | 638 |  | 
| Huihong Luo | 50a2f36 | 2018-08-11 09:27:57 -0700 | [diff] [blame] | 639 | return NO_ERROR; | 
|  | 640 | } | 
|  | 641 |  | 
| Mariia Sandrikova | a166f01 | 2022-12-10 02:32:14 +0000 | [diff] [blame] | 642 | void BootAnimation::rotateAwayFromNaturalOrientationIfNeeded() { | 
| Parth Sane | 7f7b221 | 2023-04-12 17:23:02 +0000 | [diff] [blame] | 643 | ATRACE_CALL(); | 
| Mariia Sandrikova | a166f01 | 2022-12-10 02:32:14 +0000 | [diff] [blame] | 644 | const auto orientation = parseOrientationProperty(); | 
|  | 645 |  | 
|  | 646 | if (orientation == ui::ROTATION_0) { | 
|  | 647 | // Do nothing if the sysprop isn't set or is set to ROTATION_0. | 
|  | 648 | return; | 
|  | 649 | } | 
|  | 650 |  | 
|  | 651 | if (orientation == ui::ROTATION_90 || orientation == ui::ROTATION_270) { | 
|  | 652 | std::swap(mWidth, mHeight); | 
|  | 653 | std::swap(mInitWidth, mInitHeight); | 
|  | 654 | mFlingerSurfaceControl->updateDefaultBufferSize(mWidth, mHeight); | 
|  | 655 | } | 
|  | 656 |  | 
|  | 657 | Rect displayRect(0, 0, mWidth, mHeight); | 
|  | 658 | Rect layerStackRect(0, 0, mWidth, mHeight); | 
|  | 659 |  | 
|  | 660 | SurfaceComposerClient::Transaction t; | 
|  | 661 | t.setDisplayProjection(mDisplayToken, orientation, layerStackRect, displayRect); | 
|  | 662 | t.apply(); | 
|  | 663 | } | 
|  | 664 |  | 
|  | 665 | ui::Rotation BootAnimation::parseOrientationProperty() { | 
| Parth Sane | 7f7b221 | 2023-04-12 17:23:02 +0000 | [diff] [blame] | 666 | ATRACE_CALL(); | 
| Mariia Sandrikova | a166f01 | 2022-12-10 02:32:14 +0000 | [diff] [blame] | 667 | const auto displayIds = SurfaceComposerClient::getPhysicalDisplayIds(); | 
|  | 668 | if (displayIds.size() == 0) { | 
|  | 669 | return ui::ROTATION_0; | 
|  | 670 | } | 
|  | 671 | const auto displayId = displayIds[0]; | 
|  | 672 | const auto syspropName = [displayId] { | 
|  | 673 | std::stringstream ss; | 
|  | 674 | ss << "ro.bootanim.set_orientation_" << displayId.value; | 
|  | 675 | return ss.str(); | 
|  | 676 | }(); | 
|  | 677 | const auto syspropValue = android::base::GetProperty(syspropName, "ORIENTATION_0"); | 
|  | 678 | if (syspropValue == "ORIENTATION_90") { | 
|  | 679 | return ui::ROTATION_90; | 
|  | 680 | } else if (syspropValue == "ORIENTATION_180") { | 
|  | 681 | return ui::ROTATION_180; | 
|  | 682 | } else if (syspropValue == "ORIENTATION_270") { | 
|  | 683 | return ui::ROTATION_270; | 
|  | 684 | } | 
|  | 685 | return ui::ROTATION_0; | 
|  | 686 | } | 
|  | 687 |  | 
| Yegor Malyshev | 6925e1b | 2020-09-10 16:42:20 +0200 | [diff] [blame] | 688 | void BootAnimation::projectSceneToWindow() { | 
| Parth Sane | 7f7b221 | 2023-04-12 17:23:02 +0000 | [diff] [blame] | 689 | ATRACE_CALL(); | 
| Yegor Malyshev | 6925e1b | 2020-09-10 16:42:20 +0200 | [diff] [blame] | 690 | glViewport(0, 0, mWidth, mHeight); | 
|  | 691 | glScissor(0, 0, mWidth, mHeight); | 
| Yegor Malyshev | 6925e1b | 2020-09-10 16:42:20 +0200 | [diff] [blame] | 692 | } | 
|  | 693 |  | 
| Marin Shalamanov | 9070c05 | 2020-05-18 19:05:17 +0200 | [diff] [blame] | 694 | void BootAnimation::resizeSurface(int newWidth, int newHeight) { | 
| Parth Sane | 7f7b221 | 2023-04-12 17:23:02 +0000 | [diff] [blame] | 695 | ATRACE_CALL(); | 
| Marin Shalamanov | 9070c05 | 2020-05-18 19:05:17 +0200 | [diff] [blame] | 696 | // We assume this function is called on the animation thread. | 
|  | 697 | if (newWidth == mWidth && newHeight == mHeight) { | 
|  | 698 | return; | 
|  | 699 | } | 
|  | 700 | SLOGV("Resizing the boot animation surface to %d %d", newWidth, newHeight); | 
|  | 701 |  | 
|  | 702 | eglMakeCurrent(mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT); | 
|  | 703 | eglDestroySurface(mDisplay, mSurface); | 
|  | 704 |  | 
| joenchen | 764ea04 | 2022-04-11 23:58:34 +0800 | [diff] [blame] | 705 | mFlingerSurfaceControl->updateDefaultBufferSize(newWidth, newHeight); | 
| Marin Shalamanov | 047802d | 2020-05-19 23:55:12 +0200 | [diff] [blame] | 706 | const auto limitedSize = limitSurfaceSize(newWidth, newHeight); | 
|  | 707 | mWidth = limitedSize.width; | 
|  | 708 | mHeight = limitedSize.height; | 
| Marin Shalamanov | 9070c05 | 2020-05-18 19:05:17 +0200 | [diff] [blame] | 709 |  | 
| Marin Shalamanov | 9070c05 | 2020-05-18 19:05:17 +0200 | [diff] [blame] | 710 | EGLConfig config = getEglConfig(mDisplay); | 
|  | 711 | EGLSurface surface = eglCreateWindowSurface(mDisplay, config, mFlingerSurface.get(), nullptr); | 
|  | 712 | if (eglMakeCurrent(mDisplay, surface, surface, mContext) == EGL_FALSE) { | 
|  | 713 | SLOGE("Can't make the new surface current. Error %d", eglGetError()); | 
|  | 714 | return; | 
|  | 715 | } | 
| Yegor Malyshev | 6925e1b | 2020-09-10 16:42:20 +0200 | [diff] [blame] | 716 |  | 
|  | 717 | projectSceneToWindow(); | 
| Marin Shalamanov | 9070c05 | 2020-05-18 19:05:17 +0200 | [diff] [blame] | 718 |  | 
|  | 719 | mSurface = surface; | 
|  | 720 | } | 
|  | 721 |  | 
| Huihong Luo | 50a2f36 | 2018-08-11 09:27:57 -0700 | [diff] [blame] | 722 | bool BootAnimation::preloadAnimation() { | 
| Parth Sane | 7f7b221 | 2023-04-12 17:23:02 +0000 | [diff] [blame] | 723 | ATRACE_CALL(); | 
| Huihong Luo | 50a2f36 | 2018-08-11 09:27:57 -0700 | [diff] [blame] | 724 | findBootAnimationFile(); | 
|  | 725 | if (!mZipFileName.isEmpty()) { | 
|  | 726 | mAnimation = loadAnimation(mZipFileName); | 
|  | 727 | return (mAnimation != nullptr); | 
|  | 728 | } | 
|  | 729 |  | 
|  | 730 | return false; | 
|  | 731 | } | 
|  | 732 |  | 
| Nikita Ioffe | 06c986e | 2020-01-27 17:16:03 +0000 | [diff] [blame] | 733 | bool BootAnimation::findBootAnimationFileInternal(const std::vector<std::string> &files) { | 
| Parth Sane | 7f7b221 | 2023-04-12 17:23:02 +0000 | [diff] [blame] | 734 | ATRACE_CALL(); | 
| Nikita Ioffe | 06c986e | 2020-01-27 17:16:03 +0000 | [diff] [blame] | 735 | for (const std::string& f : files) { | 
|  | 736 | if (access(f.c_str(), R_OK) == 0) { | 
|  | 737 | mZipFileName = f.c_str(); | 
|  | 738 | return true; | 
|  | 739 | } | 
|  | 740 | } | 
|  | 741 | return false; | 
|  | 742 | } | 
|  | 743 |  | 
| Huihong Luo | 50a2f36 | 2018-08-11 09:27:57 -0700 | [diff] [blame] | 744 | void BootAnimation::findBootAnimationFile() { | 
| Parth Sane | 7f7b221 | 2023-04-12 17:23:02 +0000 | [diff] [blame] | 745 | ATRACE_CALL(); | 
| Beverly | 34ffe25 | 2019-05-17 11:03:54 -0400 | [diff] [blame] | 746 | const bool playDarkAnim = android::base::GetIntProperty("ro.boot.theme", 0) == 1; | 
| Nikita Ioffe | 06c986e | 2020-01-27 17:16:03 +0000 | [diff] [blame] | 747 | static const std::vector<std::string> bootFiles = { | 
|  | 748 | APEX_BOOTANIMATION_FILE, playDarkAnim ? PRODUCT_BOOTANIMATION_DARK_FILE : PRODUCT_BOOTANIMATION_FILE, | 
|  | 749 | OEM_BOOTANIMATION_FILE, SYSTEM_BOOTANIMATION_FILE | 
|  | 750 | }; | 
|  | 751 | static const std::vector<std::string> shutdownFiles = { | 
|  | 752 | PRODUCT_SHUTDOWNANIMATION_FILE, OEM_SHUTDOWNANIMATION_FILE, SYSTEM_SHUTDOWNANIMATION_FILE, "" | 
|  | 753 | }; | 
|  | 754 | static const std::vector<std::string> userspaceRebootFiles = { | 
|  | 755 | PRODUCT_USERSPACE_REBOOT_ANIMATION_FILE, OEM_USERSPACE_REBOOT_ANIMATION_FILE, | 
|  | 756 | SYSTEM_USERSPACE_REBOOT_ANIMATION_FILE, | 
|  | 757 | }; | 
| Keun-young Park | b593842 | 2017-03-23 13:46:24 -0700 | [diff] [blame] | 758 |  | 
| Nikita Ioffe | 06c986e | 2020-01-27 17:16:03 +0000 | [diff] [blame] | 759 | if (android::base::GetBoolProperty("sys.init.userspace_reboot.in_progress", false)) { | 
|  | 760 | findBootAnimationFileInternal(userspaceRebootFiles); | 
|  | 761 | } else if (mShuttingDown) { | 
|  | 762 | findBootAnimationFileInternal(shutdownFiles); | 
|  | 763 | } else { | 
|  | 764 | findBootAnimationFileInternal(bootFiles); | 
| Andriy Naborskyy | 39218ba | 2015-08-16 21:32:50 -0700 | [diff] [blame] | 765 | } | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 766 | } | 
|  | 767 |  | 
| Lucas Dupin | 8eedc02 | 2021-07-29 21:49:07 +0000 | [diff] [blame] | 768 | GLuint compileShader(GLenum shaderType, const GLchar *source) { | 
| Parth Sane | 7f7b221 | 2023-04-12 17:23:02 +0000 | [diff] [blame] | 769 | ATRACE_CALL(); | 
| Lucas Dupin | 8eedc02 | 2021-07-29 21:49:07 +0000 | [diff] [blame] | 770 | GLuint shader = glCreateShader(shaderType); | 
|  | 771 | glShaderSource(shader, 1, &source, 0); | 
|  | 772 | glCompileShader(shader); | 
|  | 773 | GLint isCompiled = 0; | 
|  | 774 | glGetShaderiv(shader, GL_COMPILE_STATUS, &isCompiled); | 
|  | 775 | if (isCompiled == GL_FALSE) { | 
|  | 776 | SLOGE("Compile shader failed. Shader type: %d", shaderType); | 
| Shan Huang | 81846ba | 2021-09-07 19:07:47 +0000 | [diff] [blame] | 777 | GLint maxLength = 0; | 
|  | 778 | glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &maxLength); | 
|  | 779 | std::vector<GLchar> errorLog(maxLength); | 
|  | 780 | glGetShaderInfoLog(shader, maxLength, &maxLength, &errorLog[0]); | 
|  | 781 | SLOGE("Shader compilation error: %s", &errorLog[0]); | 
| Lucas Dupin | 8eedc02 | 2021-07-29 21:49:07 +0000 | [diff] [blame] | 782 | return 0; | 
|  | 783 | } | 
|  | 784 | return shader; | 
|  | 785 | } | 
|  | 786 |  | 
|  | 787 | GLuint linkShader(GLuint vertexShader, GLuint fragmentShader) { | 
| Parth Sane | 7f7b221 | 2023-04-12 17:23:02 +0000 | [diff] [blame] | 788 | ATRACE_CALL(); | 
| Lucas Dupin | 8eedc02 | 2021-07-29 21:49:07 +0000 | [diff] [blame] | 789 | GLuint program = glCreateProgram(); | 
|  | 790 | glAttachShader(program, vertexShader); | 
|  | 791 | glAttachShader(program, fragmentShader); | 
|  | 792 | glLinkProgram(program); | 
|  | 793 | GLint isLinked = 0; | 
|  | 794 | glGetProgramiv(program, GL_LINK_STATUS, (int *)&isLinked); | 
|  | 795 | if (isLinked == GL_FALSE) { | 
|  | 796 | SLOGE("Linking shader failed. Shader handles: vert %d, frag %d", | 
|  | 797 | vertexShader, fragmentShader); | 
|  | 798 | return 0; | 
|  | 799 | } | 
|  | 800 | return program; | 
|  | 801 | } | 
|  | 802 |  | 
|  | 803 | void BootAnimation::initShaders() { | 
| Parth Sane | 7f7b221 | 2023-04-12 17:23:02 +0000 | [diff] [blame] | 804 | ATRACE_CALL(); | 
| Shan Huang | 2b31a36 | 2021-08-16 16:45:39 +0000 | [diff] [blame] | 805 | bool dynamicColoringEnabled = mAnimation != nullptr && mAnimation->dynamicColoringEnabled; | 
| Lucas Dupin | 8eedc02 | 2021-07-29 21:49:07 +0000 | [diff] [blame] | 806 | GLuint vertexShader = compileShader(GL_VERTEX_SHADER, (const GLchar *)VERTEX_SHADER_SOURCE); | 
|  | 807 | GLuint imageFragmentShader = | 
| Shan Huang | 2b31a36 | 2021-08-16 16:45:39 +0000 | [diff] [blame] | 808 | compileShader(GL_FRAGMENT_SHADER, dynamicColoringEnabled | 
|  | 809 | ? (const GLchar *)IMAGE_FRAG_DYNAMIC_COLORING_SHADER_SOURCE | 
|  | 810 | : (const GLchar *)IMAGE_FRAG_SHADER_SOURCE); | 
| Lucas Dupin | 8eedc02 | 2021-07-29 21:49:07 +0000 | [diff] [blame] | 811 | GLuint textFragmentShader = | 
|  | 812 | compileShader(GL_FRAGMENT_SHADER, (const GLchar *)TEXT_FRAG_SHADER_SOURCE); | 
|  | 813 |  | 
|  | 814 | // Initialize image shader. | 
|  | 815 | mImageShader = linkShader(vertexShader, imageFragmentShader); | 
|  | 816 | GLint positionLocation = glGetAttribLocation(mImageShader, A_POSITION); | 
|  | 817 | GLint uvLocation = glGetAttribLocation(mImageShader, A_UV); | 
|  | 818 | mImageTextureLocation = glGetUniformLocation(mImageShader, U_TEXTURE); | 
|  | 819 | mImageFadeLocation = glGetUniformLocation(mImageShader, U_FADE); | 
|  | 820 | glEnableVertexAttribArray(positionLocation); | 
|  | 821 | glVertexAttribPointer(positionLocation, 2,  GL_FLOAT, GL_FALSE, 0, quadPositions); | 
|  | 822 | glVertexAttribPointer(uvLocation, 2, GL_FLOAT, GL_FALSE, 0, quadUVs); | 
|  | 823 | glEnableVertexAttribArray(uvLocation); | 
|  | 824 |  | 
|  | 825 | // Initialize text shader. | 
|  | 826 | mTextShader = linkShader(vertexShader, textFragmentShader); | 
|  | 827 | positionLocation = glGetAttribLocation(mTextShader, A_POSITION); | 
|  | 828 | uvLocation = glGetAttribLocation(mTextShader, A_UV); | 
|  | 829 | mTextTextureLocation = glGetUniformLocation(mTextShader, U_TEXTURE); | 
|  | 830 | mTextCropAreaLocation = glGetUniformLocation(mTextShader, U_CROP_AREA); | 
|  | 831 | glEnableVertexAttribArray(positionLocation); | 
|  | 832 | glVertexAttribPointer(positionLocation, 2,  GL_FLOAT, GL_FALSE, 0, quadPositions); | 
|  | 833 | glVertexAttribPointer(uvLocation, 2, GL_FLOAT, GL_FALSE, 0, quadUVs); | 
|  | 834 | glEnableVertexAttribArray(uvLocation); | 
|  | 835 | } | 
|  | 836 |  | 
| Marin Shalamanov | 9070c05 | 2020-05-18 19:05:17 +0200 | [diff] [blame] | 837 | bool BootAnimation::threadLoop() { | 
| Parth Sane | 7f7b221 | 2023-04-12 17:23:02 +0000 | [diff] [blame] | 838 | ATRACE_CALL(); | 
| Marin Shalamanov | 9070c05 | 2020-05-18 19:05:17 +0200 | [diff] [blame] | 839 | bool result; | 
| Lucas Dupin | 8eedc02 | 2021-07-29 21:49:07 +0000 | [diff] [blame] | 840 | initShaders(); | 
|  | 841 |  | 
| Narayan Kamath | afd31e0 | 2013-12-03 13:16:03 +0000 | [diff] [blame] | 842 | // We have no bootanimation file, so we use the stock android logo | 
|  | 843 | // animation. | 
| Andriy Naborskyy | 39218ba | 2015-08-16 21:32:50 -0700 | [diff] [blame] | 844 | if (mZipFileName.isEmpty()) { | 
| Yoshiaki Maruoka | 9ef19b8 | 2017-10-25 13:55:49 +0900 | [diff] [blame] | 845 | ALOGD("No animation file"); | 
| Marin Shalamanov | 9070c05 | 2020-05-18 19:05:17 +0200 | [diff] [blame] | 846 | result = android(); | 
| Mathias Agopian | a8826d6 | 2009-10-01 03:10:14 -0700 | [diff] [blame] | 847 | } else { | 
| Marin Shalamanov | 9070c05 | 2020-05-18 19:05:17 +0200 | [diff] [blame] | 848 | result = movie(); | 
| Mathias Agopian | a8826d6 | 2009-10-01 03:10:14 -0700 | [diff] [blame] | 849 | } | 
|  | 850 |  | 
| Philip Junker | 6060056 | 2020-11-25 18:16:16 +0100 | [diff] [blame] | 851 | mCallbacks->shutdown(); | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 852 | eglMakeCurrent(mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT); | 
|  | 853 | eglDestroyContext(mDisplay, mContext); | 
|  | 854 | eglDestroySurface(mDisplay, mSurface); | 
| Mathias Agopian | 6cf0db2 | 2009-04-17 19:36:26 -0700 | [diff] [blame] | 855 | mFlingerSurface.clear(); | 
| Mathias Agopian | 17f638b | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 856 | mFlingerSurfaceControl.clear(); | 
| Mathias Agopian | 627e7b5 | 2009-05-21 19:21:59 -0700 | [diff] [blame] | 857 | eglTerminate(mDisplay); | 
| Sai Kiran Korwar | 3eee9fb | 2015-06-16 17:13:35 +0530 | [diff] [blame] | 858 | eglReleaseThread(); | 
| Mathias Agopian | 627e7b5 | 2009-05-21 19:21:59 -0700 | [diff] [blame] | 859 | IPCThreadState::self()->stopProcess(); | 
| Marin Shalamanov | 9070c05 | 2020-05-18 19:05:17 +0200 | [diff] [blame] | 860 | return result; | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 861 | } | 
|  | 862 |  | 
| Marin Shalamanov | 9070c05 | 2020-05-18 19:05:17 +0200 | [diff] [blame] | 863 | bool BootAnimation::android() { | 
| Parth Sane | 7f7b221 | 2023-04-12 17:23:02 +0000 | [diff] [blame] | 864 | ATRACE_CALL(); | 
| Lucas Dupin | 8eedc02 | 2021-07-29 21:49:07 +0000 | [diff] [blame] | 865 | glActiveTexture(GL_TEXTURE0); | 
|  | 866 |  | 
| Wei Wang | 159a410 | 2018-10-23 23:15:58 -0700 | [diff] [blame] | 867 | SLOGD("%sAnimationShownTiming start time: %" PRId64 "ms", mShuttingDown ? "Shutdown" : "Boot", | 
| Keun-young Park | d1794cd | 2017-04-04 12:21:19 -0700 | [diff] [blame] | 868 | elapsedRealtime()); | 
| Mathias Agopian | b2cf954 | 2009-03-24 18:34:16 -0700 | [diff] [blame] | 869 | initTexture(&mAndroid[0], mAssets, "images/android-logo-mask.png"); | 
|  | 870 | initTexture(&mAndroid[1], mAssets, "images/android-logo-shine.png"); | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 871 |  | 
| Ed Coyne | 7464ac9 | 2017-06-08 12:26:48 -0700 | [diff] [blame] | 872 | mCallbacks->init({}); | 
|  | 873 |  | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 874 | // clear screen | 
| Mathias Agopian | b2cf954 | 2009-03-24 18:34:16 -0700 | [diff] [blame] | 875 | glDisable(GL_DITHER); | 
|  | 876 | glDisable(GL_SCISSOR_TEST); | 
| Shan Huang | bfd2d3c | 2021-09-09 23:19:04 +0000 | [diff] [blame] | 877 | glUseProgram(mImageShader); | 
|  | 878 |  | 
| Mathias Agopian | 59f19e4 | 2011-05-06 19:22:12 -0700 | [diff] [blame] | 879 | glClearColor(0,0,0,1); | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 880 | glClear(GL_COLOR_BUFFER_BIT); | 
|  | 881 | eglSwapBuffers(mDisplay, mSurface); | 
|  | 882 |  | 
| Mathias Agopian | b2cf954 | 2009-03-24 18:34:16 -0700 | [diff] [blame] | 883 | // Blend state | 
|  | 884 | glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); | 
| Mathias Agopian | b2cf954 | 2009-03-24 18:34:16 -0700 | [diff] [blame] | 885 |  | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 886 | const nsecs_t startTime = systemTime(); | 
|  | 887 | do { | 
| Marin Shalamanov | 9070c05 | 2020-05-18 19:05:17 +0200 | [diff] [blame] | 888 | processDisplayEvents(); | 
|  | 889 | const GLint xc = (mWidth  - mAndroid[0].w) / 2; | 
|  | 890 | const GLint yc = (mHeight - mAndroid[0].h) / 2; | 
|  | 891 | const Rect updateRect(xc, yc, xc + mAndroid[0].w, yc + mAndroid[0].h); | 
|  | 892 | glScissor(updateRect.left, mHeight - updateRect.bottom, updateRect.width(), | 
|  | 893 | updateRect.height()); | 
|  | 894 |  | 
| Mathias Agopian | 1379665 | 2009-03-24 22:49:21 -0700 | [diff] [blame] | 895 | nsecs_t now = systemTime(); | 
|  | 896 | double time = now - startTime; | 
| Mathias Agopian | b2cf954 | 2009-03-24 18:34:16 -0700 | [diff] [blame] | 897 | float t = 4.0f * float(time / us2ns(16667)) / mAndroid[1].w; | 
|  | 898 | GLint offset = (1 - (t - floorf(t))) * mAndroid[1].w; | 
|  | 899 | GLint x = xc - offset; | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 900 |  | 
| Mathias Agopian | 8166864 | 2009-07-28 11:41:30 -0700 | [diff] [blame] | 901 | glDisable(GL_SCISSOR_TEST); | 
|  | 902 | glClear(GL_COLOR_BUFFER_BIT); | 
|  | 903 |  | 
|  | 904 | glEnable(GL_SCISSOR_TEST); | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 905 | glDisable(GL_BLEND); | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 906 | glBindTexture(GL_TEXTURE_2D, mAndroid[1].name); | 
| Lucas Dupin | 8eedc02 | 2021-07-29 21:49:07 +0000 | [diff] [blame] | 907 | drawTexturedQuad(x,                 yc, mAndroid[1].w, mAndroid[1].h); | 
|  | 908 | drawTexturedQuad(x + mAndroid[1].w, yc, mAndroid[1].w, mAndroid[1].h); | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 909 |  | 
| Mathias Agopian | b2cf954 | 2009-03-24 18:34:16 -0700 | [diff] [blame] | 910 | glEnable(GL_BLEND); | 
|  | 911 | glBindTexture(GL_TEXTURE_2D, mAndroid[0].name); | 
| Lucas Dupin | 8eedc02 | 2021-07-29 21:49:07 +0000 | [diff] [blame] | 912 | drawTexturedQuad(xc, yc, mAndroid[0].w, mAndroid[0].h); | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 913 |  | 
| Mathias Agopian | 627e7b5 | 2009-05-21 19:21:59 -0700 | [diff] [blame] | 914 | EGLBoolean res = eglSwapBuffers(mDisplay, mSurface); | 
|  | 915 | if (res == EGL_FALSE) | 
|  | 916 | break; | 
|  | 917 |  | 
| Mathias Agopian | 1379665 | 2009-03-24 22:49:21 -0700 | [diff] [blame] | 918 | // 12fps: don't animate too fast to preserve CPU | 
|  | 919 | const nsecs_t sleepTime = 83333 - ns2us(systemTime() - now); | 
|  | 920 | if (sleepTime > 0) | 
| Mathias Agopian | a8826d6 | 2009-10-01 03:10:14 -0700 | [diff] [blame] | 921 | usleep(sleepTime); | 
| Kevin Hester | d3782b2 | 2012-04-26 10:38:55 -0700 | [diff] [blame] | 922 |  | 
|  | 923 | checkExit(); | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 924 | } while (!exitPending()); | 
|  | 925 |  | 
|  | 926 | glDeleteTextures(1, &mAndroid[0].name); | 
|  | 927 | glDeleteTextures(1, &mAndroid[1].name); | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 928 | return false; | 
|  | 929 | } | 
|  | 930 |  | 
| Kevin Hester | d3782b2 | 2012-04-26 10:38:55 -0700 | [diff] [blame] | 931 | void BootAnimation::checkExit() { | 
| Parth Sane | 7f7b221 | 2023-04-12 17:23:02 +0000 | [diff] [blame] | 932 | ATRACE_CALL(); | 
| Kevin Hester | d3782b2 | 2012-04-26 10:38:55 -0700 | [diff] [blame] | 933 | // Allow surface flinger to gracefully request shutdown | 
|  | 934 | char value[PROPERTY_VALUE_MAX]; | 
|  | 935 | property_get(EXIT_PROP_NAME, value, "0"); | 
|  | 936 | int exitnow = atoi(value); | 
|  | 937 | if (exitnow) { | 
|  | 938 | requestExit(); | 
|  | 939 | } | 
|  | 940 | } | 
|  | 941 |  | 
| Damien Bargiacchi | 0e3d2ab | 2016-08-29 04:11:19 -0700 | [diff] [blame] | 942 | bool BootAnimation::validClock(const Animation::Part& part) { | 
| Parth Sane | 7f7b221 | 2023-04-12 17:23:02 +0000 | [diff] [blame] | 943 | ATRACE_CALL(); | 
| Damien Bargiacchi | 0e3d2ab | 2016-08-29 04:11:19 -0700 | [diff] [blame] | 944 | return part.clockPosX != TEXT_MISSING_VALUE && part.clockPosY != TEXT_MISSING_VALUE; | 
|  | 945 | } | 
|  | 946 |  | 
|  | 947 | bool parseTextCoord(const char* str, int* dest) { | 
| Parth Sane | 7f7b221 | 2023-04-12 17:23:02 +0000 | [diff] [blame] | 948 | ATRACE_CALL(); | 
| Damien Bargiacchi | 0e3d2ab | 2016-08-29 04:11:19 -0700 | [diff] [blame] | 949 | if (strcmp("c", str) == 0) { | 
|  | 950 | *dest = TEXT_CENTER_VALUE; | 
|  | 951 | return true; | 
|  | 952 | } | 
|  | 953 |  | 
|  | 954 | char* end; | 
|  | 955 | int val = (int) strtol(str, &end, 0); | 
|  | 956 | if (end == str || *end != '\0' || val == INT_MAX || val == INT_MIN) { | 
|  | 957 | return false; | 
|  | 958 | } | 
|  | 959 | *dest = val; | 
|  | 960 | return true; | 
|  | 961 | } | 
|  | 962 |  | 
|  | 963 | // Parse two position coordinates. If only string is non-empty, treat it as the y value. | 
|  | 964 | void parsePosition(const char* str1, const char* str2, int* x, int* y) { | 
| Parth Sane | 7f7b221 | 2023-04-12 17:23:02 +0000 | [diff] [blame] | 965 | ATRACE_CALL(); | 
| Damien Bargiacchi | 0e3d2ab | 2016-08-29 04:11:19 -0700 | [diff] [blame] | 966 | bool success = false; | 
|  | 967 | if (strlen(str1) == 0) {  // No values were specified | 
|  | 968 | // success = false | 
|  | 969 | } else if (strlen(str2) == 0) {  // we have only one value | 
|  | 970 | if (parseTextCoord(str1, y)) { | 
|  | 971 | *x = TEXT_CENTER_VALUE; | 
|  | 972 | success = true; | 
|  | 973 | } | 
|  | 974 | } else { | 
|  | 975 | if (parseTextCoord(str1, x) && parseTextCoord(str2, y)) { | 
|  | 976 | success = true; | 
|  | 977 | } | 
|  | 978 | } | 
|  | 979 |  | 
|  | 980 | if (!success) { | 
|  | 981 | *x = TEXT_MISSING_VALUE; | 
|  | 982 | *y = TEXT_MISSING_VALUE; | 
|  | 983 | } | 
|  | 984 | } | 
|  | 985 |  | 
| Jesse Hall | 083b84c | 2014-09-22 10:51:09 -0700 | [diff] [blame] | 986 | // Parse a color represented as an HTML-style 'RRGGBB' string: each pair of | 
|  | 987 | // characters in str is a hex number in [0, 255], which are converted to | 
|  | 988 | // floating point values in the range [0.0, 1.0] and placed in the | 
|  | 989 | // corresponding elements of color. | 
|  | 990 | // | 
|  | 991 | // If the input string isn't valid, parseColor returns false and color is | 
|  | 992 | // left unchanged. | 
|  | 993 | static bool parseColor(const char str[7], float color[3]) { | 
| Parth Sane | 7f7b221 | 2023-04-12 17:23:02 +0000 | [diff] [blame] | 994 | ATRACE_CALL(); | 
| Jesse Hall | 083b84c | 2014-09-22 10:51:09 -0700 | [diff] [blame] | 995 | float tmpColor[3]; | 
|  | 996 | for (int i = 0; i < 3; i++) { | 
|  | 997 | int val = 0; | 
|  | 998 | for (int j = 0; j < 2; j++) { | 
|  | 999 | val *= 16; | 
|  | 1000 | char c = str[2*i + j]; | 
|  | 1001 | if      (c >= '0' && c <= '9') val += c - '0'; | 
|  | 1002 | else if (c >= 'A' && c <= 'F') val += (c - 'A') + 10; | 
|  | 1003 | else if (c >= 'a' && c <= 'f') val += (c - 'a') + 10; | 
|  | 1004 | else                           return false; | 
|  | 1005 | } | 
|  | 1006 | tmpColor[i] = static_cast<float>(val) / 255.0f; | 
|  | 1007 | } | 
|  | 1008 | memcpy(color, tmpColor, sizeof(tmpColor)); | 
|  | 1009 | return true; | 
|  | 1010 | } | 
|  | 1011 |  | 
| Shan Huang | 2b31a36 | 2021-08-16 16:45:39 +0000 | [diff] [blame] | 1012 | // Parse a color represented as a signed decimal int string. | 
|  | 1013 | // E.g. "-2757722" (whose hex 2's complement is 0xFFD5EBA6). | 
|  | 1014 | // If the input color string is empty, set color with values in defaultColor. | 
|  | 1015 | static void parseColorDecimalString(const std::string& colorString, | 
|  | 1016 | float color[3], float defaultColor[3]) { | 
| Parth Sane | 7f7b221 | 2023-04-12 17:23:02 +0000 | [diff] [blame] | 1017 | ATRACE_CALL(); | 
| Shan Huang | 2b31a36 | 2021-08-16 16:45:39 +0000 | [diff] [blame] | 1018 | if (colorString == "") { | 
|  | 1019 | memcpy(color, defaultColor, sizeof(float) * 3); | 
|  | 1020 | return; | 
|  | 1021 | } | 
|  | 1022 | int colorInt = atoi(colorString.c_str()); | 
|  | 1023 | color[0] = ((float)((colorInt >> 16) & 0xFF)) / 0xFF; // r | 
|  | 1024 | color[1] = ((float)((colorInt >> 8) & 0xFF)) / 0xFF; // g | 
|  | 1025 | color[2] = ((float)(colorInt & 0xFF)) / 0xFF; // b | 
|  | 1026 | } | 
| Andriy Naborskyy | 39218ba | 2015-08-16 21:32:50 -0700 | [diff] [blame] | 1027 |  | 
| Marin Shalamanov | 9070c05 | 2020-05-18 19:05:17 +0200 | [diff] [blame] | 1028 | static bool readFile(ZipFileRO* zip, const char* name, String8& outString) { | 
| Parth Sane | 7f7b221 | 2023-04-12 17:23:02 +0000 | [diff] [blame] | 1029 | ATRACE_CALL(); | 
| Andriy Naborskyy | 39218ba | 2015-08-16 21:32:50 -0700 | [diff] [blame] | 1030 | ZipEntryRO entry = zip->findEntryByName(name); | 
| Wei Wang | 159a410 | 2018-10-23 23:15:58 -0700 | [diff] [blame] | 1031 | SLOGE_IF(!entry, "couldn't find %s", name); | 
| Mike Lockwood | ebf9a0d | 2014-10-02 16:08:47 -0700 | [diff] [blame] | 1032 | if (!entry) { | 
|  | 1033 | return false; | 
|  | 1034 | } | 
|  | 1035 |  | 
| Andriy Naborskyy | 39218ba | 2015-08-16 21:32:50 -0700 | [diff] [blame] | 1036 | FileMap* entryMap = zip->createEntryFileMap(entry); | 
|  | 1037 | zip->releaseEntry(entry); | 
| Wei Wang | 159a410 | 2018-10-23 23:15:58 -0700 | [diff] [blame] | 1038 | SLOGE_IF(!entryMap, "entryMap is null"); | 
| Mike Lockwood | ebf9a0d | 2014-10-02 16:08:47 -0700 | [diff] [blame] | 1039 | if (!entryMap) { | 
|  | 1040 | return false; | 
|  | 1041 | } | 
|  | 1042 |  | 
|  | 1043 | outString.setTo((char const*)entryMap->getDataPtr(), entryMap->getDataLength()); | 
| Narayan Kamath | 688ff4c | 2015-02-23 15:47:54 +0000 | [diff] [blame] | 1044 | delete entryMap; | 
| Mike Lockwood | ebf9a0d | 2014-10-02 16:08:47 -0700 | [diff] [blame] | 1045 | return true; | 
|  | 1046 | } | 
|  | 1047 |  | 
| Damien Bargiacchi | 0e3d2ab | 2016-08-29 04:11:19 -0700 | [diff] [blame] | 1048 | // The font image should be a 96x2 array of character images.  The | 
|  | 1049 | // columns are the printable ASCII characters 0x20 - 0x7f.  The | 
|  | 1050 | // top row is regular text; the bottom row is bold. | 
|  | 1051 | status_t BootAnimation::initFont(Font* font, const char* fallback) { | 
| Parth Sane | 7f7b221 | 2023-04-12 17:23:02 +0000 | [diff] [blame] | 1052 | ATRACE_CALL(); | 
| Damien Bargiacchi | 0e3d2ab | 2016-08-29 04:11:19 -0700 | [diff] [blame] | 1053 | status_t status = NO_ERROR; | 
| Damien Bargiacchi | a704b7d | 2016-02-16 16:55:49 -0800 | [diff] [blame] | 1054 |  | 
| Damien Bargiacchi | 0e3d2ab | 2016-08-29 04:11:19 -0700 | [diff] [blame] | 1055 | if (font->map != nullptr) { | 
|  | 1056 | glGenTextures(1, &font->texture.name); | 
|  | 1057 | glBindTexture(GL_TEXTURE_2D, font->texture.name); | 
| Damien Bargiacchi | a704b7d | 2016-02-16 16:55:49 -0800 | [diff] [blame] | 1058 |  | 
| Damien Bargiacchi | 0e3d2ab | 2016-08-29 04:11:19 -0700 | [diff] [blame] | 1059 | status = initTexture(font->map, &font->texture.w, &font->texture.h); | 
|  | 1060 |  | 
| Lucas Dupin | 8eedc02 | 2021-07-29 21:49:07 +0000 | [diff] [blame] | 1061 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); | 
|  | 1062 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); | 
|  | 1063 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); | 
|  | 1064 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); | 
| Damien Bargiacchi | 0e3d2ab | 2016-08-29 04:11:19 -0700 | [diff] [blame] | 1065 | } else if (fallback != nullptr) { | 
|  | 1066 | status = initTexture(&font->texture, mAssets, fallback); | 
|  | 1067 | } else { | 
|  | 1068 | return NO_INIT; | 
| Damien Bargiacchi | a704b7d | 2016-02-16 16:55:49 -0800 | [diff] [blame] | 1069 | } | 
|  | 1070 |  | 
| Damien Bargiacchi | 0e3d2ab | 2016-08-29 04:11:19 -0700 | [diff] [blame] | 1071 | if (status == NO_ERROR) { | 
|  | 1072 | font->char_width = font->texture.w / FONT_NUM_COLS; | 
|  | 1073 | font->char_height = font->texture.h / FONT_NUM_ROWS / 2;  // There are bold and regular rows | 
|  | 1074 | } | 
|  | 1075 |  | 
|  | 1076 | return status; | 
|  | 1077 | } | 
|  | 1078 |  | 
|  | 1079 | void BootAnimation::drawText(const char* str, const Font& font, bool bold, int* x, int* y) { | 
| Parth Sane | 7f7b221 | 2023-04-12 17:23:02 +0000 | [diff] [blame] | 1080 | ATRACE_CALL(); | 
| Damien Bargiacchi | 0e3d2ab | 2016-08-29 04:11:19 -0700 | [diff] [blame] | 1081 | glEnable(GL_BLEND);  // Allow us to draw on top of the animation | 
|  | 1082 | glBindTexture(GL_TEXTURE_2D, font.texture.name); | 
| Lucas Dupin | 8eedc02 | 2021-07-29 21:49:07 +0000 | [diff] [blame] | 1083 | glUseProgram(mTextShader); | 
|  | 1084 | glUniform1i(mTextTextureLocation, 0); | 
| Damien Bargiacchi | 0e3d2ab | 2016-08-29 04:11:19 -0700 | [diff] [blame] | 1085 |  | 
|  | 1086 | const int len = strlen(str); | 
|  | 1087 | const int strWidth = font.char_width * len; | 
|  | 1088 |  | 
|  | 1089 | if (*x == TEXT_CENTER_VALUE) { | 
|  | 1090 | *x = (mWidth - strWidth) / 2; | 
|  | 1091 | } else if (*x < 0) { | 
|  | 1092 | *x = mWidth + *x - strWidth; | 
|  | 1093 | } | 
|  | 1094 | if (*y == TEXT_CENTER_VALUE) { | 
|  | 1095 | *y = (mHeight - font.char_height) / 2; | 
|  | 1096 | } else if (*y < 0) { | 
|  | 1097 | *y = mHeight + *y - font.char_height; | 
|  | 1098 | } | 
|  | 1099 |  | 
| Damien Bargiacchi | 0e3d2ab | 2016-08-29 04:11:19 -0700 | [diff] [blame] | 1100 | for (int i = 0; i < len; i++) { | 
|  | 1101 | char c = str[i]; | 
|  | 1102 |  | 
|  | 1103 | if (c < FONT_BEGIN_CHAR || c > FONT_END_CHAR) { | 
|  | 1104 | c = '?'; | 
|  | 1105 | } | 
|  | 1106 |  | 
|  | 1107 | // Crop the texture to only the pixels in the current glyph | 
|  | 1108 | const int charPos = (c - FONT_BEGIN_CHAR);  // Position in the list of valid characters | 
|  | 1109 | const int row = charPos / FONT_NUM_COLS; | 
|  | 1110 | const int col = charPos % FONT_NUM_COLS; | 
| Lucas Dupin | 8eedc02 | 2021-07-29 21:49:07 +0000 | [diff] [blame] | 1111 | // Bold fonts are expected in the second half of each row. | 
|  | 1112 | float v0 = (row + (bold ? 0.5f : 0.0f)) / FONT_NUM_ROWS; | 
|  | 1113 | float u0 = ((float)col) / FONT_NUM_COLS; | 
|  | 1114 | float v1 = v0 + 1.0f / FONT_NUM_ROWS / 2; | 
|  | 1115 | float u1 = u0 + 1.0f / FONT_NUM_COLS; | 
|  | 1116 | glUniform4f(mTextCropAreaLocation, u0, v0, u1, v1); | 
|  | 1117 | drawTexturedQuad(*x, *y, font.char_width, font.char_height); | 
| Damien Bargiacchi | 0e3d2ab | 2016-08-29 04:11:19 -0700 | [diff] [blame] | 1118 |  | 
|  | 1119 | *x += font.char_width; | 
|  | 1120 | } | 
|  | 1121 |  | 
|  | 1122 | glDisable(GL_BLEND);  // Return to the animation's default behaviour | 
|  | 1123 | glBindTexture(GL_TEXTURE_2D, 0); | 
|  | 1124 | } | 
|  | 1125 |  | 
| Damien Bargiacchi | 9071db1 | 2016-10-28 17:38:22 -0700 | [diff] [blame] | 1126 | // We render 12 or 24 hour time. | 
| Damien Bargiacchi | 0e3d2ab | 2016-08-29 04:11:19 -0700 | [diff] [blame] | 1127 | void BootAnimation::drawClock(const Font& font, const int xPos, const int yPos) { | 
| Parth Sane | 7f7b221 | 2023-04-12 17:23:02 +0000 | [diff] [blame] | 1128 | ATRACE_CALL(); | 
| Damien Bargiacchi | 9071db1 | 2016-10-28 17:38:22 -0700 | [diff] [blame] | 1129 | static constexpr char TIME_FORMAT_12[] = "%l:%M"; | 
|  | 1130 | static constexpr char TIME_FORMAT_24[] = "%H:%M"; | 
| Damien Bargiacchi | 0e3d2ab | 2016-08-29 04:11:19 -0700 | [diff] [blame] | 1131 | static constexpr int TIME_LENGTH = 6; | 
|  | 1132 |  | 
| Damien Bargiacchi | a704b7d | 2016-02-16 16:55:49 -0800 | [diff] [blame] | 1133 | time_t rawtime; | 
|  | 1134 | time(&rawtime); | 
|  | 1135 | struct tm* timeInfo = localtime(&rawtime); | 
|  | 1136 |  | 
|  | 1137 | char timeBuff[TIME_LENGTH]; | 
| Damien Bargiacchi | 9071db1 | 2016-10-28 17:38:22 -0700 | [diff] [blame] | 1138 | const char* timeFormat = mTimeFormat12Hour ? TIME_FORMAT_12 : TIME_FORMAT_24; | 
|  | 1139 | size_t length = strftime(timeBuff, TIME_LENGTH, timeFormat, timeInfo); | 
| Damien Bargiacchi | a704b7d | 2016-02-16 16:55:49 -0800 | [diff] [blame] | 1140 |  | 
|  | 1141 | if (length != TIME_LENGTH - 1) { | 
| Wei Wang | 159a410 | 2018-10-23 23:15:58 -0700 | [diff] [blame] | 1142 | SLOGE("Couldn't format time; abandoning boot animation clock"); | 
| Damien Bargiacchi | a704b7d | 2016-02-16 16:55:49 -0800 | [diff] [blame] | 1143 | mClockEnabled = false; | 
|  | 1144 | return; | 
|  | 1145 | } | 
|  | 1146 |  | 
| Damien Bargiacchi | 45a7644 | 2016-12-05 18:02:18 -0800 | [diff] [blame] | 1147 | char* out = timeBuff[0] == ' ' ? &timeBuff[1] : &timeBuff[0]; | 
| Damien Bargiacchi | 0e3d2ab | 2016-08-29 04:11:19 -0700 | [diff] [blame] | 1148 | int x = xPos; | 
|  | 1149 | int y = yPos; | 
| Damien Bargiacchi | 45a7644 | 2016-12-05 18:02:18 -0800 | [diff] [blame] | 1150 | drawText(out, font, false, &x, &y); | 
| Damien Bargiacchi | a704b7d | 2016-02-16 16:55:49 -0800 | [diff] [blame] | 1151 | } | 
|  | 1152 |  | 
| Nicolas Geoffray | 41fffb4 | 2020-12-15 10:50:29 +0000 | [diff] [blame] | 1153 | void BootAnimation::drawProgress(int percent, const Font& font, const int xPos, const int yPos) { | 
| Parth Sane | 7f7b221 | 2023-04-12 17:23:02 +0000 | [diff] [blame] | 1154 | ATRACE_CALL(); | 
| Nicolas Geoffray | 41fffb4 | 2020-12-15 10:50:29 +0000 | [diff] [blame] | 1155 | static constexpr int PERCENT_LENGTH = 5; | 
|  | 1156 |  | 
|  | 1157 | char percentBuff[PERCENT_LENGTH]; | 
|  | 1158 | // ';' has the ascii code just after ':', and the font resource contains '%' | 
|  | 1159 | // for that ascii code. | 
|  | 1160 | sprintf(percentBuff, "%d;", percent); | 
|  | 1161 | int x = xPos; | 
|  | 1162 | int y = yPos; | 
|  | 1163 | drawText(percentBuff, font, false, &x, &y); | 
|  | 1164 | } | 
|  | 1165 |  | 
| Marin Shalamanov | 9070c05 | 2020-05-18 19:05:17 +0200 | [diff] [blame] | 1166 | bool BootAnimation::parseAnimationDesc(Animation& animation)  { | 
| Parth Sane | 7f7b221 | 2023-04-12 17:23:02 +0000 | [diff] [blame] | 1167 | ATRACE_CALL(); | 
| Mike Lockwood | ebf9a0d | 2014-10-02 16:08:47 -0700 | [diff] [blame] | 1168 | String8 desString; | 
|  | 1169 |  | 
| Andriy Naborskyy | 39218ba | 2015-08-16 21:32:50 -0700 | [diff] [blame] | 1170 | if (!readFile(animation.zip, "desc.txt", desString)) { | 
| Narayan Kamath | afd31e0 | 2013-12-03 13:16:03 +0000 | [diff] [blame] | 1171 | return false; | 
|  | 1172 | } | 
| Mathias Agopian | a8826d6 | 2009-10-01 03:10:14 -0700 | [diff] [blame] | 1173 | char const* s = desString.string(); | 
| Shan Huang | 2b31a36 | 2021-08-16 16:45:39 +0000 | [diff] [blame] | 1174 | std::string dynamicColoringPartName = ""; | 
|  | 1175 | bool postDynamicColoring = false; | 
| Mathias Agopian | a8826d6 | 2009-10-01 03:10:14 -0700 | [diff] [blame] | 1176 |  | 
| Mathias Agopian | a8826d6 | 2009-10-01 03:10:14 -0700 | [diff] [blame] | 1177 | // Parse the description file | 
|  | 1178 | for (;;) { | 
|  | 1179 | const char* endl = strstr(s, "\n"); | 
| Yi Kong | 911ac23 | 2019-03-24 01:49:02 -0700 | [diff] [blame] | 1180 | if (endl == nullptr) break; | 
| Mathias Agopian | a8826d6 | 2009-10-01 03:10:14 -0700 | [diff] [blame] | 1181 | String8 line(s, endl - s); | 
|  | 1182 | const char* l = line.string(); | 
| Damien Bargiacchi | a704b7d | 2016-02-16 16:55:49 -0800 | [diff] [blame] | 1183 | int fps = 0; | 
|  | 1184 | int width = 0; | 
|  | 1185 | int height = 0; | 
|  | 1186 | int count = 0; | 
|  | 1187 | int pause = 0; | 
| Nicolas Geoffray | 41fffb4 | 2020-12-15 10:50:29 +0000 | [diff] [blame] | 1188 | int progress = 0; | 
| Yegor Malyshev | 6925e1b | 2020-09-10 16:42:20 +0200 | [diff] [blame] | 1189 | int framesToFadeCount = 0; | 
| Shan Huang | 488ff74 | 2021-08-25 20:10:46 +0000 | [diff] [blame] | 1190 | int colorTransitionStart = 0; | 
|  | 1191 | int colorTransitionEnd = 0; | 
| Narayan Kamath | afd31e0 | 2013-12-03 13:16:03 +0000 | [diff] [blame] | 1192 | char path[ANIM_ENTRY_NAME_MAX]; | 
| Jesse Hall | 083b84c | 2014-09-22 10:51:09 -0700 | [diff] [blame] | 1193 | char color[7] = "000000"; // default to black if unspecified | 
| Damien Bargiacchi | 0e3d2ab | 2016-08-29 04:11:19 -0700 | [diff] [blame] | 1194 | char clockPos1[TEXT_POS_LEN_MAX + 1] = ""; | 
|  | 1195 | char clockPos2[TEXT_POS_LEN_MAX + 1] = ""; | 
| Shan Huang | 2b31a36 | 2021-08-16 16:45:39 +0000 | [diff] [blame] | 1196 | char dynamicColoringPartNameBuffer[ANIM_ENTRY_NAME_MAX]; | 
| Kevin Hester | d3782b2 | 2012-04-26 10:38:55 -0700 | [diff] [blame] | 1197 | char pathType; | 
| Shan Huang | 2b31a36 | 2021-08-16 16:45:39 +0000 | [diff] [blame] | 1198 | // start colors default to black if unspecified | 
|  | 1199 | char start_color_0[7] = "000000"; | 
|  | 1200 | char start_color_1[7] = "000000"; | 
|  | 1201 | char start_color_2[7] = "000000"; | 
|  | 1202 | char start_color_3[7] = "000000"; | 
| Yegor Malyshev | 6925e1b | 2020-09-10 16:42:20 +0200 | [diff] [blame] | 1203 |  | 
|  | 1204 | int nextReadPos; | 
|  | 1205 |  | 
| yidong zhang | 87bb346 | 2022-12-07 15:55:31 +0800 | [diff] [blame] | 1206 | if (strlen(l) == 0) { | 
|  | 1207 | s = ++endl; | 
|  | 1208 | continue; | 
|  | 1209 | } | 
|  | 1210 |  | 
| Nicolas Geoffray | 41fffb4 | 2020-12-15 10:50:29 +0000 | [diff] [blame] | 1211 | int topLineNumbers = sscanf(l, "%d %d %d %d", &width, &height, &fps, &progress); | 
|  | 1212 | if (topLineNumbers == 3 || topLineNumbers == 4) { | 
|  | 1213 | // SLOGD("> w=%d, h=%d, fps=%d, progress=%d", width, height, fps, progress); | 
| Mathias Agopian | a8826d6 | 2009-10-01 03:10:14 -0700 | [diff] [blame] | 1214 | animation.width = width; | 
|  | 1215 | animation.height = height; | 
|  | 1216 | animation.fps = fps; | 
| Nicolas Geoffray | 41fffb4 | 2020-12-15 10:50:29 +0000 | [diff] [blame] | 1217 | if (topLineNumbers == 4) { | 
|  | 1218 | animation.progressEnabled = (progress != 0); | 
|  | 1219 | } else { | 
|  | 1220 | animation.progressEnabled = false; | 
|  | 1221 | } | 
| Shan Huang | 488ff74 | 2021-08-25 20:10:46 +0000 | [diff] [blame] | 1222 | } else if (sscanf(l, "dynamic_colors %" STRTO(ANIM_PATH_MAX) "s #%6s #%6s #%6s #%6s %d %d", | 
| Shan Huang | 2b31a36 | 2021-08-16 16:45:39 +0000 | [diff] [blame] | 1223 | dynamicColoringPartNameBuffer, | 
| Shan Huang | 488ff74 | 2021-08-25 20:10:46 +0000 | [diff] [blame] | 1224 | start_color_0, start_color_1, start_color_2, start_color_3, | 
|  | 1225 | &colorTransitionStart, &colorTransitionEnd)) { | 
| Shan Huang | 2b31a36 | 2021-08-16 16:45:39 +0000 | [diff] [blame] | 1226 | animation.dynamicColoringEnabled = true; | 
|  | 1227 | parseColor(start_color_0, animation.startColors[0]); | 
|  | 1228 | parseColor(start_color_1, animation.startColors[1]); | 
|  | 1229 | parseColor(start_color_2, animation.startColors[2]); | 
|  | 1230 | parseColor(start_color_3, animation.startColors[3]); | 
| Shan Huang | 488ff74 | 2021-08-25 20:10:46 +0000 | [diff] [blame] | 1231 | animation.colorTransitionStart = colorTransitionStart; | 
|  | 1232 | animation.colorTransitionEnd = colorTransitionEnd; | 
| Shan Huang | 2b31a36 | 2021-08-16 16:45:39 +0000 | [diff] [blame] | 1233 | dynamicColoringPartName = std::string(dynamicColoringPartNameBuffer); | 
| Yegor Malyshev | 6925e1b | 2020-09-10 16:42:20 +0200 | [diff] [blame] | 1234 | } else if (sscanf(l, "%c %d %d %" STRTO(ANIM_PATH_MAX) "s%n", | 
|  | 1235 | &pathType, &count, &pause, path, &nextReadPos) >= 4) { | 
|  | 1236 | if (pathType == 'f') { | 
|  | 1237 | sscanf(l + nextReadPos, " %d #%6s %16s %16s", &framesToFadeCount, color, clockPos1, | 
|  | 1238 | clockPos2); | 
|  | 1239 | } else { | 
|  | 1240 | sscanf(l + nextReadPos, " #%6s %16s %16s", color, clockPos1, clockPos2); | 
|  | 1241 | } | 
|  | 1242 | // SLOGD("> type=%c, count=%d, pause=%d, path=%s, framesToFadeCount=%d, color=%s, " | 
|  | 1243 | //       "clockPos1=%s, clockPos2=%s", | 
|  | 1244 | //       pathType, count, pause, path, framesToFadeCount, color, clockPos1, clockPos2); | 
| Mathias Agopian | a8826d6 | 2009-10-01 03:10:14 -0700 | [diff] [blame] | 1245 | Animation::Part part; | 
| Shan Huang | 2b31a36 | 2021-08-16 16:45:39 +0000 | [diff] [blame] | 1246 | if (path == dynamicColoringPartName) { | 
|  | 1247 | // Part is specified to use dynamic coloring. | 
|  | 1248 | part.useDynamicColoring = true; | 
|  | 1249 | part.postDynamicColoring = false; | 
|  | 1250 | postDynamicColoring = true; | 
|  | 1251 | } else { | 
|  | 1252 | // Part does not use dynamic coloring. | 
|  | 1253 | part.useDynamicColoring = false; | 
|  | 1254 | part.postDynamicColoring =  postDynamicColoring; | 
|  | 1255 | } | 
| Kevin Hester | d3782b2 | 2012-04-26 10:38:55 -0700 | [diff] [blame] | 1256 | part.playUntilComplete = pathType == 'c'; | 
| Yegor Malyshev | 6925e1b | 2020-09-10 16:42:20 +0200 | [diff] [blame] | 1257 | part.framesToFadeCount = framesToFadeCount; | 
| Mathias Agopian | a8826d6 | 2009-10-01 03:10:14 -0700 | [diff] [blame] | 1258 | part.count = count; | 
|  | 1259 | part.pause = pause; | 
|  | 1260 | part.path = path; | 
| Yi Kong | 911ac23 | 2019-03-24 01:49:02 -0700 | [diff] [blame] | 1261 | part.audioData = nullptr; | 
|  | 1262 | part.animation = nullptr; | 
| Jesse Hall | 083b84c | 2014-09-22 10:51:09 -0700 | [diff] [blame] | 1263 | if (!parseColor(color, part.backgroundColor)) { | 
| Wei Wang | 159a410 | 2018-10-23 23:15:58 -0700 | [diff] [blame] | 1264 | SLOGE("> invalid color '#%s'", color); | 
| Jesse Hall | 083b84c | 2014-09-22 10:51:09 -0700 | [diff] [blame] | 1265 | part.backgroundColor[0] = 0.0f; | 
|  | 1266 | part.backgroundColor[1] = 0.0f; | 
|  | 1267 | part.backgroundColor[2] = 0.0f; | 
|  | 1268 | } | 
| Damien Bargiacchi | 0e3d2ab | 2016-08-29 04:11:19 -0700 | [diff] [blame] | 1269 | parsePosition(clockPos1, clockPos2, &part.clockPosX, &part.clockPosY); | 
| Mathias Agopian | a8826d6 | 2009-10-01 03:10:14 -0700 | [diff] [blame] | 1270 | animation.parts.add(part); | 
|  | 1271 | } | 
| Andriy Naborskyy | 39218ba | 2015-08-16 21:32:50 -0700 | [diff] [blame] | 1272 | else if (strcmp(l, "$SYSTEM") == 0) { | 
| Wei Wang | 159a410 | 2018-10-23 23:15:58 -0700 | [diff] [blame] | 1273 | // SLOGD("> SYSTEM"); | 
| Andriy Naborskyy | 39218ba | 2015-08-16 21:32:50 -0700 | [diff] [blame] | 1274 | Animation::Part part; | 
|  | 1275 | part.playUntilComplete = false; | 
| Yegor Malyshev | 6925e1b | 2020-09-10 16:42:20 +0200 | [diff] [blame] | 1276 | part.framesToFadeCount = 0; | 
| Andriy Naborskyy | 39218ba | 2015-08-16 21:32:50 -0700 | [diff] [blame] | 1277 | part.count = 1; | 
|  | 1278 | part.pause = 0; | 
| Yi Kong | 911ac23 | 2019-03-24 01:49:02 -0700 | [diff] [blame] | 1279 | part.audioData = nullptr; | 
| Andriy Naborskyy | 39218ba | 2015-08-16 21:32:50 -0700 | [diff] [blame] | 1280 | part.animation = loadAnimation(String8(SYSTEM_BOOTANIMATION_FILE)); | 
| Yi Kong | 911ac23 | 2019-03-24 01:49:02 -0700 | [diff] [blame] | 1281 | if (part.animation != nullptr) | 
| Andriy Naborskyy | 39218ba | 2015-08-16 21:32:50 -0700 | [diff] [blame] | 1282 | animation.parts.add(part); | 
|  | 1283 | } | 
| Mathias Agopian | a8826d6 | 2009-10-01 03:10:14 -0700 | [diff] [blame] | 1284 | s = ++endl; | 
|  | 1285 | } | 
|  | 1286 |  | 
| Andriy Naborskyy | 39218ba | 2015-08-16 21:32:50 -0700 | [diff] [blame] | 1287 | return true; | 
|  | 1288 | } | 
|  | 1289 |  | 
| Marin Shalamanov | 9070c05 | 2020-05-18 19:05:17 +0200 | [diff] [blame] | 1290 | bool BootAnimation::preloadZip(Animation& animation) { | 
| Parth Sane | 7f7b221 | 2023-04-12 17:23:02 +0000 | [diff] [blame] | 1291 | ATRACE_CALL(); | 
| Mathias Agopian | a8826d6 | 2009-10-01 03:10:14 -0700 | [diff] [blame] | 1292 | // read all the data structures | 
|  | 1293 | const size_t pcount = animation.parts.size(); | 
| Yi Kong | 911ac23 | 2019-03-24 01:49:02 -0700 | [diff] [blame] | 1294 | void *cookie = nullptr; | 
| Geoffrey Pitsch | dd214a7 | 2016-06-27 17:14:30 -0400 | [diff] [blame] | 1295 | ZipFileRO* zip = animation.zip; | 
|  | 1296 | if (!zip->startIteration(&cookie)) { | 
| Narayan Kamath | afd31e0 | 2013-12-03 13:16:03 +0000 | [diff] [blame] | 1297 | return false; | 
|  | 1298 | } | 
|  | 1299 |  | 
|  | 1300 | ZipEntryRO entry; | 
|  | 1301 | char name[ANIM_ENTRY_NAME_MAX]; | 
| Yi Kong | 911ac23 | 2019-03-24 01:49:02 -0700 | [diff] [blame] | 1302 | while ((entry = zip->nextEntry(cookie)) != nullptr) { | 
| Geoffrey Pitsch | dd214a7 | 2016-06-27 17:14:30 -0400 | [diff] [blame] | 1303 | const int foundEntryName = zip->getEntryFileName(entry, name, ANIM_ENTRY_NAME_MAX); | 
| Narayan Kamath | afd31e0 | 2013-12-03 13:16:03 +0000 | [diff] [blame] | 1304 | if (foundEntryName > ANIM_ENTRY_NAME_MAX || foundEntryName == -1) { | 
| Wei Wang | 159a410 | 2018-10-23 23:15:58 -0700 | [diff] [blame] | 1305 | SLOGE("Error fetching entry file name"); | 
| Narayan Kamath | afd31e0 | 2013-12-03 13:16:03 +0000 | [diff] [blame] | 1306 | continue; | 
|  | 1307 | } | 
|  | 1308 |  | 
|  | 1309 | const String8 entryName(name); | 
|  | 1310 | const String8 path(entryName.getPathDir()); | 
|  | 1311 | const String8 leaf(entryName.getPathLeaf()); | 
|  | 1312 | if (leaf.size() > 0) { | 
| Damien Bargiacchi | 0e3d2ab | 2016-08-29 04:11:19 -0700 | [diff] [blame] | 1313 | if (entryName == CLOCK_FONT_ZIP_NAME) { | 
|  | 1314 | FileMap* map = zip->createEntryFileMap(entry); | 
|  | 1315 | if (map) { | 
|  | 1316 | animation.clockFont.map = map; | 
|  | 1317 | } | 
|  | 1318 | continue; | 
|  | 1319 | } | 
|  | 1320 |  | 
| Nicolas Geoffray | 41fffb4 | 2020-12-15 10:50:29 +0000 | [diff] [blame] | 1321 | if (entryName == PROGRESS_FONT_ZIP_NAME) { | 
|  | 1322 | FileMap* map = zip->createEntryFileMap(entry); | 
|  | 1323 | if (map) { | 
|  | 1324 | animation.progressFont.map = map; | 
|  | 1325 | } | 
|  | 1326 | continue; | 
|  | 1327 | } | 
|  | 1328 |  | 
| Geoffrey Pitsch | dd214a7 | 2016-06-27 17:14:30 -0400 | [diff] [blame] | 1329 | for (size_t j = 0; j < pcount; j++) { | 
| Narayan Kamath | afd31e0 | 2013-12-03 13:16:03 +0000 | [diff] [blame] | 1330 | if (path == animation.parts[j].path) { | 
| Narayan Kamath | 4600dd0 | 2015-06-16 12:02:57 +0100 | [diff] [blame] | 1331 | uint16_t method; | 
| Narayan Kamath | afd31e0 | 2013-12-03 13:16:03 +0000 | [diff] [blame] | 1332 | // supports only stored png files | 
| Yi Kong | 911ac23 | 2019-03-24 01:49:02 -0700 | [diff] [blame] | 1333 | if (zip->getEntryInfo(entry, &method, nullptr, nullptr, nullptr, nullptr, nullptr)) { | 
| Narayan Kamath | afd31e0 | 2013-12-03 13:16:03 +0000 | [diff] [blame] | 1334 | if (method == ZipFileRO::kCompressStored) { | 
| Geoffrey Pitsch | dd214a7 | 2016-06-27 17:14:30 -0400 | [diff] [blame] | 1335 | FileMap* map = zip->createEntryFileMap(entry); | 
| Narayan Kamath | afd31e0 | 2013-12-03 13:16:03 +0000 | [diff] [blame] | 1336 | if (map) { | 
| Narayan Kamath | afd31e0 | 2013-12-03 13:16:03 +0000 | [diff] [blame] | 1337 | Animation::Part& part(animation.parts.editItemAt(j)); | 
| Mike Lockwood | ebf9a0d | 2014-10-02 16:08:47 -0700 | [diff] [blame] | 1338 | if (leaf == "audio.wav") { | 
|  | 1339 | // a part may have at most one audio file | 
| Geoffrey Pitsch | d6d9a1d | 2016-06-08 00:38:58 -0700 | [diff] [blame] | 1340 | part.audioData = (uint8_t *)map->getDataPtr(); | 
|  | 1341 | part.audioLength = map->getDataLength(); | 
| Geoffrey Pitsch | dd214a7 | 2016-06-27 17:14:30 -0400 | [diff] [blame] | 1342 | } else if (leaf == "trim.txt") { | 
|  | 1343 | part.trimData.setTo((char const*)map->getDataPtr(), | 
|  | 1344 | map->getDataLength()); | 
| Mike Lockwood | ebf9a0d | 2014-10-02 16:08:47 -0700 | [diff] [blame] | 1345 | } else { | 
|  | 1346 | Animation::Frame frame; | 
|  | 1347 | frame.name = leaf; | 
|  | 1348 | frame.map = map; | 
| Geoffrey Pitsch | dd214a7 | 2016-06-27 17:14:30 -0400 | [diff] [blame] | 1349 | frame.trimWidth = animation.width; | 
|  | 1350 | frame.trimHeight = animation.height; | 
|  | 1351 | frame.trimX = 0; | 
|  | 1352 | frame.trimY = 0; | 
| Mike Lockwood | ebf9a0d | 2014-10-02 16:08:47 -0700 | [diff] [blame] | 1353 | part.frames.add(frame); | 
|  | 1354 | } | 
| Mathias Agopian | a8826d6 | 2009-10-01 03:10:14 -0700 | [diff] [blame] | 1355 | } | 
| Geoffrey Pitsch | d6d9a1d | 2016-06-08 00:38:58 -0700 | [diff] [blame] | 1356 | } else { | 
| Wei Wang | 159a410 | 2018-10-23 23:15:58 -0700 | [diff] [blame] | 1357 | SLOGE("bootanimation.zip is compressed; must be only stored"); | 
| Mathias Agopian | a8826d6 | 2009-10-01 03:10:14 -0700 | [diff] [blame] | 1358 | } | 
|  | 1359 | } | 
|  | 1360 | } | 
|  | 1361 | } | 
|  | 1362 | } | 
|  | 1363 | } | 
|  | 1364 |  | 
| Geoffrey Pitsch | dd214a7 | 2016-06-27 17:14:30 -0400 | [diff] [blame] | 1365 | // If there is trimData present, override the positioning defaults. | 
|  | 1366 | for (Animation::Part& part : animation.parts) { | 
|  | 1367 | const char* trimDataStr = part.trimData.string(); | 
|  | 1368 | for (size_t frameIdx = 0; frameIdx < part.frames.size(); frameIdx++) { | 
|  | 1369 | const char* endl = strstr(trimDataStr, "\n"); | 
|  | 1370 | // No more trimData for this part. | 
| Yi Kong | 911ac23 | 2019-03-24 01:49:02 -0700 | [diff] [blame] | 1371 | if (endl == nullptr) { | 
| Geoffrey Pitsch | dd214a7 | 2016-06-27 17:14:30 -0400 | [diff] [blame] | 1372 | break; | 
|  | 1373 | } | 
|  | 1374 | String8 line(trimDataStr, endl - trimDataStr); | 
|  | 1375 | const char* lineStr = line.string(); | 
|  | 1376 | trimDataStr = ++endl; | 
|  | 1377 | int width = 0, height = 0, x = 0, y = 0; | 
|  | 1378 | if (sscanf(lineStr, "%dx%d+%d+%d", &width, &height, &x, &y) == 4) { | 
|  | 1379 | Animation::Frame& frame(part.frames.editItemAt(frameIdx)); | 
|  | 1380 | frame.trimWidth = width; | 
|  | 1381 | frame.trimHeight = height; | 
|  | 1382 | frame.trimX = x; | 
|  | 1383 | frame.trimY = y; | 
|  | 1384 | } else { | 
| Wei Wang | 159a410 | 2018-10-23 23:15:58 -0700 | [diff] [blame] | 1385 | SLOGE("Error parsing trim.txt, line: %s", lineStr); | 
| Geoffrey Pitsch | dd214a7 | 2016-06-27 17:14:30 -0400 | [diff] [blame] | 1386 | break; | 
|  | 1387 | } | 
|  | 1388 | } | 
|  | 1389 | } | 
|  | 1390 |  | 
|  | 1391 | zip->endIteration(cookie); | 
| Narayan Kamath | afd31e0 | 2013-12-03 13:16:03 +0000 | [diff] [blame] | 1392 |  | 
| Andriy Naborskyy | 39218ba | 2015-08-16 21:32:50 -0700 | [diff] [blame] | 1393 | return true; | 
|  | 1394 | } | 
|  | 1395 |  | 
| Marin Shalamanov | 9070c05 | 2020-05-18 19:05:17 +0200 | [diff] [blame] | 1396 | bool BootAnimation::movie() { | 
| Parth Sane | 7f7b221 | 2023-04-12 17:23:02 +0000 | [diff] [blame] | 1397 | ATRACE_CALL(); | 
| Huihong Luo | 50a2f36 | 2018-08-11 09:27:57 -0700 | [diff] [blame] | 1398 | if (mAnimation == nullptr) { | 
|  | 1399 | mAnimation = loadAnimation(mZipFileName); | 
|  | 1400 | } | 
|  | 1401 |  | 
|  | 1402 | if (mAnimation == nullptr) | 
| Andriy Naborskyy | 39218ba | 2015-08-16 21:32:50 -0700 | [diff] [blame] | 1403 | return false; | 
|  | 1404 |  | 
| Huihong Luo | 50a2f36 | 2018-08-11 09:27:57 -0700 | [diff] [blame] | 1405 | // mCallbacks->init() may get called recursively, | 
|  | 1406 | // this loop is needed to get the same results | 
|  | 1407 | for (const Animation::Part& part : mAnimation->parts) { | 
|  | 1408 | if (part.animation != nullptr) { | 
|  | 1409 | mCallbacks->init(part.animation->parts); | 
|  | 1410 | } | 
|  | 1411 | } | 
|  | 1412 | mCallbacks->init(mAnimation->parts); | 
|  | 1413 |  | 
| Damien Bargiacchi | 9748086 | 2016-03-29 14:55:55 -0700 | [diff] [blame] | 1414 | bool anyPartHasClock = false; | 
| Huihong Luo | 50a2f36 | 2018-08-11 09:27:57 -0700 | [diff] [blame] | 1415 | for (size_t i=0; i < mAnimation->parts.size(); i++) { | 
|  | 1416 | if(validClock(mAnimation->parts[i])) { | 
| Damien Bargiacchi | 9748086 | 2016-03-29 14:55:55 -0700 | [diff] [blame] | 1417 | anyPartHasClock = true; | 
|  | 1418 | break; | 
|  | 1419 | } | 
|  | 1420 | } | 
|  | 1421 | if (!anyPartHasClock) { | 
|  | 1422 | mClockEnabled = false; | 
| Thiébaud Weksteen | 08de443 | 2021-11-18 13:25:59 +1100 | [diff] [blame] | 1423 | } else if (!android::base::GetBoolProperty(CLOCK_ENABLED_PROP_NAME, false)) { | 
|  | 1424 | mClockEnabled = false; | 
| Damien Bargiacchi | 9748086 | 2016-03-29 14:55:55 -0700 | [diff] [blame] | 1425 | } | 
|  | 1426 |  | 
| Sai Kiran Korwar | 2716749 | 2015-07-07 20:00:06 +0530 | [diff] [blame] | 1427 | // Check if npot textures are supported | 
|  | 1428 | mUseNpotTextures = false; | 
|  | 1429 | String8 gl_extensions; | 
|  | 1430 | const char* exts = reinterpret_cast<const char*>(glGetString(GL_EXTENSIONS)); | 
|  | 1431 | if (!exts) { | 
|  | 1432 | glGetError(); | 
|  | 1433 | } else { | 
|  | 1434 | gl_extensions.setTo(exts); | 
|  | 1435 | if ((gl_extensions.find("GL_ARB_texture_non_power_of_two") != -1) || | 
|  | 1436 | (gl_extensions.find("GL_OES_texture_npot") != -1)) { | 
|  | 1437 | mUseNpotTextures = true; | 
|  | 1438 | } | 
|  | 1439 | } | 
|  | 1440 |  | 
| Damien Bargiacchi | a704b7d | 2016-02-16 16:55:49 -0800 | [diff] [blame] | 1441 | // Blend required to draw time on top of animation frames. | 
|  | 1442 | glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); | 
| Mathias Agopian | a8826d6 | 2009-10-01 03:10:14 -0700 | [diff] [blame] | 1443 | glDisable(GL_DITHER); | 
|  | 1444 | glDisable(GL_SCISSOR_TEST); | 
|  | 1445 | glDisable(GL_BLEND); | 
| Mathias Agopian | a8826d6 | 2009-10-01 03:10:14 -0700 | [diff] [blame] | 1446 |  | 
| Greg Kaiser | 2ded800 | 2021-07-19 17:03:27 +0000 | [diff] [blame] | 1447 | glEnable(GL_TEXTURE_2D); | 
| Lucas Dupin | 8eedc02 | 2021-07-29 21:49:07 +0000 | [diff] [blame] | 1448 | glBindTexture(GL_TEXTURE_2D, 0); | 
|  | 1449 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); | 
|  | 1450 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); | 
|  | 1451 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); | 
|  | 1452 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); | 
| Damien Bargiacchi | 0e3d2ab | 2016-08-29 04:11:19 -0700 | [diff] [blame] | 1453 | bool clockFontInitialized = false; | 
| Damien Bargiacchi | a704b7d | 2016-02-16 16:55:49 -0800 | [diff] [blame] | 1454 | if (mClockEnabled) { | 
| Damien Bargiacchi | 0e3d2ab | 2016-08-29 04:11:19 -0700 | [diff] [blame] | 1455 | clockFontInitialized = | 
| Huihong Luo | 50a2f36 | 2018-08-11 09:27:57 -0700 | [diff] [blame] | 1456 | (initFont(&mAnimation->clockFont, CLOCK_FONT_ASSET) == NO_ERROR); | 
| Damien Bargiacchi | 0e3d2ab | 2016-08-29 04:11:19 -0700 | [diff] [blame] | 1457 | mClockEnabled = clockFontInitialized; | 
| Damien Bargiacchi | a704b7d | 2016-02-16 16:55:49 -0800 | [diff] [blame] | 1458 | } | 
|  | 1459 |  | 
| Nicolas Geoffray | 41fffb4 | 2020-12-15 10:50:29 +0000 | [diff] [blame] | 1460 | initFont(&mAnimation->progressFont, PROGRESS_FONT_ASSET); | 
|  | 1461 |  | 
| Damien Bargiacchi | 9748086 | 2016-03-29 14:55:55 -0700 | [diff] [blame] | 1462 | if (mClockEnabled && !updateIsTimeAccurate()) { | 
|  | 1463 | mTimeCheckThread = new TimeCheckThread(this); | 
|  | 1464 | mTimeCheckThread->run("BootAnimation::TimeCheckThread", PRIORITY_NORMAL); | 
|  | 1465 | } | 
|  | 1466 |  | 
| George Burgess IV | 0f8e811 | 2021-08-23 17:32:23 -0700 | [diff] [blame] | 1467 | if (mAnimation->dynamicColoringEnabled) { | 
| Shan Huang | 7fee352 | 2021-08-18 00:56:08 +0000 | [diff] [blame] | 1468 | initDynamicColors(); | 
|  | 1469 | } | 
|  | 1470 |  | 
| Huihong Luo | 50a2f36 | 2018-08-11 09:27:57 -0700 | [diff] [blame] | 1471 | playAnimation(*mAnimation); | 
| Damien Bargiacchi | 9748086 | 2016-03-29 14:55:55 -0700 | [diff] [blame] | 1472 |  | 
| Geoffrey Pitsch | a917353 | 2016-07-19 14:56:39 -0400 | [diff] [blame] | 1473 | if (mTimeCheckThread != nullptr) { | 
| Damien Bargiacchi | 9748086 | 2016-03-29 14:55:55 -0700 | [diff] [blame] | 1474 | mTimeCheckThread->requestExit(); | 
| Geoffrey Pitsch | a917353 | 2016-07-19 14:56:39 -0400 | [diff] [blame] | 1475 | mTimeCheckThread = nullptr; | 
|  | 1476 | } | 
|  | 1477 |  | 
| Damien Bargiacchi | 0e3d2ab | 2016-08-29 04:11:19 -0700 | [diff] [blame] | 1478 | if (clockFontInitialized) { | 
| Huihong Luo | 50a2f36 | 2018-08-11 09:27:57 -0700 | [diff] [blame] | 1479 | glDeleteTextures(1, &mAnimation->clockFont.texture.name); | 
| Andriy Naborskyy | 815e51d | 2016-03-24 16:43:34 -0700 | [diff] [blame] | 1480 | } | 
|  | 1481 |  | 
| Huihong Luo | 50a2f36 | 2018-08-11 09:27:57 -0700 | [diff] [blame] | 1482 | releaseAnimation(mAnimation); | 
|  | 1483 | mAnimation = nullptr; | 
| Damien Bargiacchi | 1a8a213 | 2018-07-24 15:20:26 -0700 | [diff] [blame] | 1484 |  | 
| Andriy Naborskyy | 39218ba | 2015-08-16 21:32:50 -0700 | [diff] [blame] | 1485 | return false; | 
|  | 1486 | } | 
|  | 1487 |  | 
| Nicolas Geoffray | a129875 | 2021-04-20 15:15:06 +0100 | [diff] [blame] | 1488 | bool BootAnimation::shouldStopPlayingPart(const Animation::Part& part, | 
|  | 1489 | const int fadedFramesCount, | 
|  | 1490 | const int lastDisplayedProgress) { | 
| Parth Sane | 7f7b221 | 2023-04-12 17:23:02 +0000 | [diff] [blame] | 1491 | ATRACE_CALL(); | 
| Yegor Malyshev | 6925e1b | 2020-09-10 16:42:20 +0200 | [diff] [blame] | 1492 | // stop playing only if it is time to exit and it's a partial part which has been faded out | 
| Nicolas Geoffray | a129875 | 2021-04-20 15:15:06 +0100 | [diff] [blame] | 1493 | return exitPending() && !part.playUntilComplete && fadedFramesCount >= part.framesToFadeCount && | 
|  | 1494 | (lastDisplayedProgress == 0 || lastDisplayedProgress == 100); | 
| Yegor Malyshev | 6925e1b | 2020-09-10 16:42:20 +0200 | [diff] [blame] | 1495 | } | 
|  | 1496 |  | 
| Lucas Dupin | 8eedc02 | 2021-07-29 21:49:07 +0000 | [diff] [blame] | 1497 | // Linear mapping from range <a1, a2> to range <b1, b2> | 
|  | 1498 | float mapLinear(float x, float a1, float a2, float b1, float b2) { | 
|  | 1499 | return b1 + ( x - a1 ) * ( b2 - b1 ) / ( a2 - a1 ); | 
|  | 1500 | } | 
|  | 1501 |  | 
|  | 1502 | void BootAnimation::drawTexturedQuad(float xStart, float yStart, float width, float height) { | 
| Parth Sane | 7f7b221 | 2023-04-12 17:23:02 +0000 | [diff] [blame] | 1503 | ATRACE_CALL(); | 
| Lucas Dupin | 8eedc02 | 2021-07-29 21:49:07 +0000 | [diff] [blame] | 1504 | // Map coordinates from screen space to world space. | 
|  | 1505 | float x0 = mapLinear(xStart, 0, mWidth, -1, 1); | 
|  | 1506 | float y0 = mapLinear(yStart, 0, mHeight, -1, 1); | 
|  | 1507 | float x1 = mapLinear(xStart + width, 0, mWidth, -1, 1); | 
|  | 1508 | float y1 = mapLinear(yStart + height, 0, mHeight, -1, 1); | 
|  | 1509 | // Update quad vertex positions. | 
|  | 1510 | quadPositions[0] = x0; | 
|  | 1511 | quadPositions[1] = y0; | 
|  | 1512 | quadPositions[2] = x1; | 
|  | 1513 | quadPositions[3] = y0; | 
|  | 1514 | quadPositions[4] = x1; | 
|  | 1515 | quadPositions[5] = y1; | 
|  | 1516 | quadPositions[6] = x1; | 
|  | 1517 | quadPositions[7] = y1; | 
|  | 1518 | quadPositions[8] = x0; | 
|  | 1519 | quadPositions[9] = y1; | 
|  | 1520 | quadPositions[10] = x0; | 
|  | 1521 | quadPositions[11] = y0; | 
|  | 1522 | glDrawArrays(GL_TRIANGLES, 0, | 
|  | 1523 | sizeof(quadPositions) / sizeof(quadPositions[0]) / 2); | 
|  | 1524 | } | 
|  | 1525 |  | 
| Shan Huang | 7fee352 | 2021-08-18 00:56:08 +0000 | [diff] [blame] | 1526 | void BootAnimation::initDynamicColors() { | 
| Parth Sane | 7f7b221 | 2023-04-12 17:23:02 +0000 | [diff] [blame] | 1527 | ATRACE_CALL(); | 
| Shan Huang | 7fee352 | 2021-08-18 00:56:08 +0000 | [diff] [blame] | 1528 | for (int i = 0; i < DYNAMIC_COLOR_COUNT; i++) { | 
| Lucas Dupin | cd25684 | 2022-03-29 15:46:36 -0700 | [diff] [blame] | 1529 | const auto syspropName = "persist.bootanim.color" + std::to_string(i + 1); | 
|  | 1530 | const auto syspropValue = android::base::GetProperty(syspropName, ""); | 
|  | 1531 | if (syspropValue != "") { | 
|  | 1532 | SLOGI("Loaded dynamic color: %s -> %s", syspropName.c_str(), syspropValue.c_str()); | 
|  | 1533 | mDynamicColorsApplied = true; | 
|  | 1534 | } | 
|  | 1535 | parseColorDecimalString(syspropValue, | 
| Shan Huang | 7fee352 | 2021-08-18 00:56:08 +0000 | [diff] [blame] | 1536 | mAnimation->endColors[i], mAnimation->startColors[i]); | 
|  | 1537 | } | 
|  | 1538 | glUseProgram(mImageShader); | 
| Lucas Dupin | cd25684 | 2022-03-29 15:46:36 -0700 | [diff] [blame] | 1539 | SLOGI("Dynamically coloring boot animation. Sysprops loaded? %i", mDynamicColorsApplied); | 
| Shan Huang | 7fee352 | 2021-08-18 00:56:08 +0000 | [diff] [blame] | 1540 | for (int i = 0; i < DYNAMIC_COLOR_COUNT; i++) { | 
|  | 1541 | float *startColor = mAnimation->startColors[i]; | 
|  | 1542 | float *endColor = mAnimation->endColors[i]; | 
| Andrew Zeng | f9f5822 | 2022-11-08 11:10:48 -0800 | [diff] [blame] | 1543 | glUniform3f(glGetUniformLocation(mImageShader, | 
| Shan Huang | 7fee352 | 2021-08-18 00:56:08 +0000 | [diff] [blame] | 1544 | (U_START_COLOR_PREFIX + std::to_string(i)).c_str()), | 
| Andrew Zeng | f9f5822 | 2022-11-08 11:10:48 -0800 | [diff] [blame] | 1545 | startColor[0], startColor[1], startColor[2]); | 
|  | 1546 | glUniform3f(glGetUniformLocation(mImageShader, | 
| Shan Huang | 7fee352 | 2021-08-18 00:56:08 +0000 | [diff] [blame] | 1547 | (U_END_COLOR_PREFIX + std::to_string(i)).c_str()), | 
| Andrew Zeng | f9f5822 | 2022-11-08 11:10:48 -0800 | [diff] [blame] | 1548 | endColor[0], endColor[1], endColor[2]); | 
| Shan Huang | 7fee352 | 2021-08-18 00:56:08 +0000 | [diff] [blame] | 1549 | } | 
|  | 1550 | mImageColorProgressLocation = glGetUniformLocation(mImageShader, U_COLOR_PROGRESS); | 
|  | 1551 | } | 
|  | 1552 |  | 
| Marin Shalamanov | 9070c05 | 2020-05-18 19:05:17 +0200 | [diff] [blame] | 1553 | bool BootAnimation::playAnimation(const Animation& animation) { | 
| Parth Sane | 7f7b221 | 2023-04-12 17:23:02 +0000 | [diff] [blame] | 1554 | ATRACE_CALL(); | 
| Andriy Naborskyy | 39218ba | 2015-08-16 21:32:50 -0700 | [diff] [blame] | 1555 | const size_t pcount = animation.parts.size(); | 
| Mathias Agopian | a8826d6 | 2009-10-01 03:10:14 -0700 | [diff] [blame] | 1556 | nsecs_t frameDuration = s2ns(1) / animation.fps; | 
| Mathias Agopian | 9f3020d | 2009-11-06 16:30:18 -0800 | [diff] [blame] | 1557 |  | 
| Wei Wang | 159a410 | 2018-10-23 23:15:58 -0700 | [diff] [blame] | 1558 | SLOGD("%sAnimationShownTiming start time: %" PRId64 "ms", mShuttingDown ? "Shutdown" : "Boot", | 
| Keun-young Park | d1794cd | 2017-04-04 12:21:19 -0700 | [diff] [blame] | 1559 | elapsedRealtime()); | 
| Yegor Malyshev | 6925e1b | 2020-09-10 16:42:20 +0200 | [diff] [blame] | 1560 |  | 
|  | 1561 | int fadedFramesCount = 0; | 
| Nicolas Geoffray | 41fffb4 | 2020-12-15 10:50:29 +0000 | [diff] [blame] | 1562 | int lastDisplayedProgress = 0; | 
| Lucas Dupin | bfd1812 | 2022-04-25 11:26:08 -0700 | [diff] [blame] | 1563 | int colorTransitionStart = animation.colorTransitionStart; | 
|  | 1564 | int colorTransitionEnd = animation.colorTransitionEnd; | 
| Narayan Kamath | afd31e0 | 2013-12-03 13:16:03 +0000 | [diff] [blame] | 1565 | for (size_t i=0 ; i<pcount ; i++) { | 
| Mathias Agopian | a8826d6 | 2009-10-01 03:10:14 -0700 | [diff] [blame] | 1566 | const Animation::Part& part(animation.parts[i]); | 
|  | 1567 | const size_t fcount = part.frames.size(); | 
| Mathias Agopian | a8826d6 | 2009-10-01 03:10:14 -0700 | [diff] [blame] | 1568 |  | 
| Andriy Naborskyy | 39218ba | 2015-08-16 21:32:50 -0700 | [diff] [blame] | 1569 | // Handle animation package | 
| Yi Kong | 911ac23 | 2019-03-24 01:49:02 -0700 | [diff] [blame] | 1570 | if (part.animation != nullptr) { | 
| Andriy Naborskyy | 39218ba | 2015-08-16 21:32:50 -0700 | [diff] [blame] | 1571 | playAnimation(*part.animation); | 
|  | 1572 | if (exitPending()) | 
|  | 1573 | break; | 
|  | 1574 | continue; //to next part | 
|  | 1575 | } | 
|  | 1576 |  | 
| Yegor Malyshev | 6925e1b | 2020-09-10 16:42:20 +0200 | [diff] [blame] | 1577 | // process the part not only while the count allows but also if already fading | 
|  | 1578 | for (int r=0 ; !part.count || r<part.count || fadedFramesCount > 0 ; r++) { | 
| Nicolas Geoffray | a129875 | 2021-04-20 15:15:06 +0100 | [diff] [blame] | 1579 | if (shouldStopPlayingPart(part, fadedFramesCount, lastDisplayedProgress)) break; | 
| Kevin Hester | d3782b2 | 2012-04-26 10:38:55 -0700 | [diff] [blame] | 1580 |  | 
| Lucas Dupin | bfd1812 | 2022-04-25 11:26:08 -0700 | [diff] [blame] | 1581 | // It's possible that the sysprops were not loaded yet at this boot phase. | 
|  | 1582 | // If that's the case, then we should keep trying until they are available. | 
|  | 1583 | if (animation.dynamicColoringEnabled && !mDynamicColorsApplied | 
|  | 1584 | && (part.useDynamicColoring || part.postDynamicColoring)) { | 
|  | 1585 | SLOGD("Trying to load dynamic color sysprops."); | 
|  | 1586 | initDynamicColors(); | 
|  | 1587 | if (mDynamicColorsApplied) { | 
|  | 1588 | // Sysprops were loaded. Next step is to adjust the animation if we loaded | 
|  | 1589 | // the colors after the animation should have started. | 
|  | 1590 | const int transitionLength = colorTransitionEnd - colorTransitionStart; | 
|  | 1591 | if (part.postDynamicColoring) { | 
|  | 1592 | colorTransitionStart = 0; | 
|  | 1593 | colorTransitionEnd = fmin(transitionLength, fcount - 1); | 
|  | 1594 | } | 
|  | 1595 | } | 
|  | 1596 | } | 
|  | 1597 |  | 
| Ed Coyne | 7464ac9 | 2017-06-08 12:26:48 -0700 | [diff] [blame] | 1598 | mCallbacks->playPart(i, part, r); | 
| Mike Lockwood | ebf9a0d | 2014-10-02 16:08:47 -0700 | [diff] [blame] | 1599 |  | 
| Jesse Hall | 083b84c | 2014-09-22 10:51:09 -0700 | [diff] [blame] | 1600 | glClearColor( | 
|  | 1601 | part.backgroundColor[0], | 
|  | 1602 | part.backgroundColor[1], | 
|  | 1603 | part.backgroundColor[2], | 
|  | 1604 | 1.0f); | 
|  | 1605 |  | 
| Yoshiaki Maruoka | 9ef19b8 | 2017-10-25 13:55:49 +0900 | [diff] [blame] | 1606 | ALOGD("Playing files = %s/%s, Requested repeat = %d, playUntilComplete = %s", | 
|  | 1607 | animation.fileName.string(), part.path.string(), part.count, | 
|  | 1608 | part.playUntilComplete ? "true" : "false"); | 
|  | 1609 |  | 
| Nicolas Geoffray | 41fffb4 | 2020-12-15 10:50:29 +0000 | [diff] [blame] | 1610 | // For the last animation, if we have progress indicator from | 
|  | 1611 | // the system, display it. | 
|  | 1612 | int currentProgress = android::base::GetIntProperty(PROGRESS_PROP_NAME, 0); | 
|  | 1613 | bool displayProgress = animation.progressEnabled && | 
|  | 1614 | (i == (pcount -1)) && currentProgress != 0; | 
|  | 1615 |  | 
| Yegor Malyshev | 6925e1b | 2020-09-10 16:42:20 +0200 | [diff] [blame] | 1616 | for (size_t j=0 ; j<fcount ; j++) { | 
| Nicolas Geoffray | a129875 | 2021-04-20 15:15:06 +0100 | [diff] [blame] | 1617 | if (shouldStopPlayingPart(part, fadedFramesCount, lastDisplayedProgress)) break; | 
| Yegor Malyshev | 6925e1b | 2020-09-10 16:42:20 +0200 | [diff] [blame] | 1618 |  | 
| Shan Huang | 2b31a36 | 2021-08-16 16:45:39 +0000 | [diff] [blame] | 1619 | // Color progress is | 
| Shan Huang | 488ff74 | 2021-08-25 20:10:46 +0000 | [diff] [blame] | 1620 | // - the animation progress, normalized from | 
|  | 1621 | //   [colorTransitionStart,colorTransitionEnd] to [0, 1] for the dynamic coloring | 
|  | 1622 | //   part. | 
| Shan Huang | 2b31a36 | 2021-08-16 16:45:39 +0000 | [diff] [blame] | 1623 | // - 0 for parts that come before, | 
|  | 1624 | // - 1 for parts that come after. | 
|  | 1625 | float colorProgress = part.useDynamicColoring | 
| Shan Huang | 488ff74 | 2021-08-25 20:10:46 +0000 | [diff] [blame] | 1626 | ? fmin(fmax( | 
| Lucas Dupin | bfd1812 | 2022-04-25 11:26:08 -0700 | [diff] [blame] | 1627 | ((float)j - colorTransitionStart) / | 
|  | 1628 | fmax(colorTransitionEnd - colorTransitionStart, 1.0f), 0.0f), 1.0f) | 
| Shan Huang | 2b31a36 | 2021-08-16 16:45:39 +0000 | [diff] [blame] | 1629 | : (part.postDynamicColoring ? 1 : 0); | 
|  | 1630 |  | 
| Marin Shalamanov | 9070c05 | 2020-05-18 19:05:17 +0200 | [diff] [blame] | 1631 | processDisplayEvents(); | 
|  | 1632 |  | 
| joenchen | 764ea04 | 2022-04-11 23:58:34 +0800 | [diff] [blame] | 1633 | const double ratio_w = static_cast<double>(mWidth) / mInitWidth; | 
|  | 1634 | const double ratio_h = static_cast<double>(mHeight) / mInitHeight; | 
|  | 1635 | const int animationX = (mWidth - animation.width * ratio_w) / 2; | 
|  | 1636 | const int animationY = (mHeight - animation.height * ratio_h) / 2; | 
| Marin Shalamanov | 9070c05 | 2020-05-18 19:05:17 +0200 | [diff] [blame] | 1637 |  | 
| Mathias Agopian | a8826d6 | 2009-10-01 03:10:14 -0700 | [diff] [blame] | 1638 | const Animation::Frame& frame(part.frames[j]); | 
| Mathias Agopian | db7dd2a | 2012-05-12 15:08:21 -0700 | [diff] [blame] | 1639 | nsecs_t lastFrame = systemTime(); | 
| Mathias Agopian | a8826d6 | 2009-10-01 03:10:14 -0700 | [diff] [blame] | 1640 |  | 
|  | 1641 | if (r > 0) { | 
|  | 1642 | glBindTexture(GL_TEXTURE_2D, frame.tid); | 
|  | 1643 | } else { | 
| Lucas Dupin | 8eedc02 | 2021-07-29 21:49:07 +0000 | [diff] [blame] | 1644 | glGenTextures(1, &frame.tid); | 
|  | 1645 | glBindTexture(GL_TEXTURE_2D, frame.tid); | 
| Damien Bargiacchi | 0e3d2ab | 2016-08-29 04:11:19 -0700 | [diff] [blame] | 1646 | int w, h; | 
| Shan Huang | 2b31a36 | 2021-08-16 16:45:39 +0000 | [diff] [blame] | 1647 | // Set decoding option to alpha unpremultiplied so that the R, G, B channels | 
|  | 1648 | // of transparent pixels are preserved. | 
|  | 1649 | initTexture(frame.map, &w, &h, false /* don't premultiply alpha */); | 
| Mathias Agopian | a8826d6 | 2009-10-01 03:10:14 -0700 | [diff] [blame] | 1650 | } | 
|  | 1651 |  | 
| joenchen | 764ea04 | 2022-04-11 23:58:34 +0800 | [diff] [blame] | 1652 | const int trimWidth = frame.trimWidth * ratio_w; | 
|  | 1653 | const int trimHeight = frame.trimHeight * ratio_h; | 
|  | 1654 | const int trimX = frame.trimX * ratio_w; | 
|  | 1655 | const int trimY = frame.trimY * ratio_h; | 
|  | 1656 | const int xc = animationX + trimX; | 
|  | 1657 | const int yc = animationY + trimY; | 
| Shan Huang | 2b31a36 | 2021-08-16 16:45:39 +0000 | [diff] [blame] | 1658 | glClear(GL_COLOR_BUFFER_BIT); | 
| Geoffrey Pitsch | dd214a7 | 2016-06-27 17:14:30 -0400 | [diff] [blame] | 1659 | // specify the y center as ceiling((mHeight - frame.trimHeight) / 2) | 
|  | 1660 | // which is equivalent to mHeight - (yc + frame.trimHeight) | 
| joenchen | 764ea04 | 2022-04-11 23:58:34 +0800 | [diff] [blame] | 1661 | const int frameDrawY = mHeight - (yc + trimHeight); | 
| Yegor Malyshev | 6925e1b | 2020-09-10 16:42:20 +0200 | [diff] [blame] | 1662 |  | 
| Lucas Dupin | 8eedc02 | 2021-07-29 21:49:07 +0000 | [diff] [blame] | 1663 | float fade = 0; | 
| Yegor Malyshev | 6925e1b | 2020-09-10 16:42:20 +0200 | [diff] [blame] | 1664 | // if the part hasn't been stopped yet then continue fading if necessary | 
|  | 1665 | if (exitPending() && part.hasFadingPhase()) { | 
| Lucas Dupin | 8eedc02 | 2021-07-29 21:49:07 +0000 | [diff] [blame] | 1666 | fade = static_cast<float>(++fadedFramesCount) / part.framesToFadeCount; | 
| Yegor Malyshev | 6925e1b | 2020-09-10 16:42:20 +0200 | [diff] [blame] | 1667 | if (fadedFramesCount >= part.framesToFadeCount) { | 
|  | 1668 | fadedFramesCount = MAX_FADED_FRAMES_COUNT; // no more fading | 
|  | 1669 | } | 
|  | 1670 | } | 
| Lucas Dupin | 8eedc02 | 2021-07-29 21:49:07 +0000 | [diff] [blame] | 1671 | glUseProgram(mImageShader); | 
|  | 1672 | glUniform1i(mImageTextureLocation, 0); | 
|  | 1673 | glUniform1f(mImageFadeLocation, fade); | 
| Shan Huang | 2b31a36 | 2021-08-16 16:45:39 +0000 | [diff] [blame] | 1674 | if (animation.dynamicColoringEnabled) { | 
|  | 1675 | glUniform1f(mImageColorProgressLocation, colorProgress); | 
|  | 1676 | } | 
| Lucas Dupin | 8eedc02 | 2021-07-29 21:49:07 +0000 | [diff] [blame] | 1677 | glEnable(GL_BLEND); | 
| joenchen | 764ea04 | 2022-04-11 23:58:34 +0800 | [diff] [blame] | 1678 | drawTexturedQuad(xc, frameDrawY, trimWidth, trimHeight); | 
| Lucas Dupin | 8eedc02 | 2021-07-29 21:49:07 +0000 | [diff] [blame] | 1679 | glDisable(GL_BLEND); | 
| Yegor Malyshev | 6925e1b | 2020-09-10 16:42:20 +0200 | [diff] [blame] | 1680 |  | 
| Damien Bargiacchi | 0e3d2ab | 2016-08-29 04:11:19 -0700 | [diff] [blame] | 1681 | if (mClockEnabled && mTimeIsAccurate && validClock(part)) { | 
|  | 1682 | drawClock(animation.clockFont, part.clockPosX, part.clockPosY); | 
| Damien Bargiacchi | a704b7d | 2016-02-16 16:55:49 -0800 | [diff] [blame] | 1683 | } | 
| Yegor Malyshev | 6925e1b | 2020-09-10 16:42:20 +0200 | [diff] [blame] | 1684 |  | 
| Nicolas Geoffray | 41fffb4 | 2020-12-15 10:50:29 +0000 | [diff] [blame] | 1685 | if (displayProgress) { | 
|  | 1686 | int newProgress = android::base::GetIntProperty(PROGRESS_PROP_NAME, 0); | 
|  | 1687 | // In case the new progress jumped suddenly, still show an | 
|  | 1688 | // increment of 1. | 
|  | 1689 | if (lastDisplayedProgress != 100) { | 
|  | 1690 | // Artificially sleep 1/10th a second to slow down the animation. | 
|  | 1691 | usleep(100000); | 
|  | 1692 | if (lastDisplayedProgress < newProgress) { | 
|  | 1693 | lastDisplayedProgress++; | 
|  | 1694 | } | 
|  | 1695 | } | 
|  | 1696 | // Put the progress percentage right below the animation. | 
|  | 1697 | int posY = animation.height / 3; | 
|  | 1698 | int posX = TEXT_CENTER_VALUE; | 
|  | 1699 | drawProgress(lastDisplayedProgress, animation.progressFont, posX, posY); | 
|  | 1700 | } | 
|  | 1701 |  | 
| Adrian Roos | 9ee5dff | 2018-08-22 20:19:49 +0200 | [diff] [blame] | 1702 | handleViewport(frameDuration); | 
| Damien Bargiacchi | a704b7d | 2016-02-16 16:55:49 -0800 | [diff] [blame] | 1703 |  | 
| Mathias Agopian | a8826d6 | 2009-10-01 03:10:14 -0700 | [diff] [blame] | 1704 | eglSwapBuffers(mDisplay, mSurface); | 
|  | 1705 |  | 
|  | 1706 | nsecs_t now = systemTime(); | 
|  | 1707 | nsecs_t delay = frameDuration - (now - lastFrame); | 
| Wei Wang | 159a410 | 2018-10-23 23:15:58 -0700 | [diff] [blame] | 1708 | //SLOGD("%lld, %lld", ns2ms(now - lastFrame), ns2ms(delay)); | 
| Mathias Agopian | a8826d6 | 2009-10-01 03:10:14 -0700 | [diff] [blame] | 1709 | lastFrame = now; | 
| Mathias Agopian | db7dd2a | 2012-05-12 15:08:21 -0700 | [diff] [blame] | 1710 |  | 
|  | 1711 | if (delay > 0) { | 
|  | 1712 | struct timespec spec; | 
|  | 1713 | spec.tv_sec  = (now + delay) / 1000000000; | 
|  | 1714 | spec.tv_nsec = (now + delay) % 1000000000; | 
|  | 1715 | int err; | 
|  | 1716 | do { | 
| Yi Kong | 911ac23 | 2019-03-24 01:49:02 -0700 | [diff] [blame] | 1717 | err = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &spec, nullptr); | 
| Keith Mok | 0f2e668 | 2021-11-15 05:50:32 +0000 | [diff] [blame] | 1718 | } while (err == EINTR); | 
| Mathias Agopian | db7dd2a | 2012-05-12 15:08:21 -0700 | [diff] [blame] | 1719 | } | 
| Kevin Hester | d3782b2 | 2012-04-26 10:38:55 -0700 | [diff] [blame] | 1720 |  | 
|  | 1721 | checkExit(); | 
| Mathias Agopian | a8826d6 | 2009-10-01 03:10:14 -0700 | [diff] [blame] | 1722 | } | 
| Kevin Hester | d3782b2 | 2012-04-26 10:38:55 -0700 | [diff] [blame] | 1723 |  | 
| niuwenchao | 9cd6ac8 | 2022-12-23 17:34:22 +0800 | [diff] [blame] | 1724 | int pauseDuration = part.pause * ns2us(frameDuration); | 
|  | 1725 | while(pauseDuration > 0 && !exitPending()){ | 
|  | 1726 | if (pauseDuration > MAX_CHECK_EXIT_INTERVAL_US) { | 
|  | 1727 | usleep(MAX_CHECK_EXIT_INTERVAL_US); | 
|  | 1728 | pauseDuration -= MAX_CHECK_EXIT_INTERVAL_US; | 
|  | 1729 | } else { | 
|  | 1730 | usleep(pauseDuration); | 
|  | 1731 | break; | 
|  | 1732 | } | 
|  | 1733 | checkExit(); | 
|  | 1734 | } | 
| Kevin Hester | d3782b2 | 2012-04-26 10:38:55 -0700 | [diff] [blame] | 1735 |  | 
| Yegor Malyshev | 6925e1b | 2020-09-10 16:42:20 +0200 | [diff] [blame] | 1736 | if (exitPending() && !part.count && mCurrentInset >= mTargetInset && | 
|  | 1737 | !part.hasFadingPhase()) { | 
| Nicolas Geoffray | a129875 | 2021-04-20 15:15:06 +0100 | [diff] [blame] | 1738 | if (lastDisplayedProgress != 0 && lastDisplayedProgress != 100) { | 
|  | 1739 | android::base::SetProperty(PROGRESS_PROP_NAME, "100"); | 
|  | 1740 | continue; | 
|  | 1741 | } | 
| Yegor Malyshev | 6925e1b | 2020-09-10 16:42:20 +0200 | [diff] [blame] | 1742 | break; // exit the infinite non-fading part when it has been played at least once | 
|  | 1743 | } | 
| Mathias Agopian | a8826d6 | 2009-10-01 03:10:14 -0700 | [diff] [blame] | 1744 | } | 
| Geoffrey Pitsch | 2fb30fb | 2016-07-06 16:16:20 -0400 | [diff] [blame] | 1745 | } | 
|  | 1746 |  | 
|  | 1747 | // Free textures created for looping parts now that the animation is done. | 
|  | 1748 | for (const Animation::Part& part : animation.parts) { | 
| Mathias Agopian | a8826d6 | 2009-10-01 03:10:14 -0700 | [diff] [blame] | 1749 | if (part.count != 1) { | 
| Geoffrey Pitsch | 2fb30fb | 2016-07-06 16:16:20 -0400 | [diff] [blame] | 1750 | const size_t fcount = part.frames.size(); | 
|  | 1751 | for (size_t j = 0; j < fcount; j++) { | 
| Mathias Agopian | a8826d6 | 2009-10-01 03:10:14 -0700 | [diff] [blame] | 1752 | const Animation::Frame& frame(part.frames[j]); | 
|  | 1753 | glDeleteTextures(1, &frame.tid); | 
|  | 1754 | } | 
|  | 1755 | } | 
|  | 1756 | } | 
| Geoffrey Pitsch | d6d9a1d | 2016-06-08 00:38:58 -0700 | [diff] [blame] | 1757 |  | 
| Yoshiaki Maruoka | 9ef19b8 | 2017-10-25 13:55:49 +0900 | [diff] [blame] | 1758 | ALOGD("%sAnimationShownTiming End time: %" PRId64 "ms", mShuttingDown ? "Shutdown" : "Boot", | 
|  | 1759 | elapsedRealtime()); | 
|  | 1760 |  | 
| Andriy Naborskyy | 39218ba | 2015-08-16 21:32:50 -0700 | [diff] [blame] | 1761 | return true; | 
| Mathias Agopian | a8826d6 | 2009-10-01 03:10:14 -0700 | [diff] [blame] | 1762 | } | 
|  | 1763 |  | 
| Marin Shalamanov | 9070c05 | 2020-05-18 19:05:17 +0200 | [diff] [blame] | 1764 | void BootAnimation::processDisplayEvents() { | 
| Parth Sane | 7f7b221 | 2023-04-12 17:23:02 +0000 | [diff] [blame] | 1765 | ATRACE_CALL(); | 
| Marin Shalamanov | 9070c05 | 2020-05-18 19:05:17 +0200 | [diff] [blame] | 1766 | // This will poll mDisplayEventReceiver and if there are new events it'll call | 
|  | 1767 | // displayEventCallback synchronously. | 
|  | 1768 | mLooper->pollOnce(0); | 
|  | 1769 | } | 
|  | 1770 |  | 
| Adrian Roos | 9ee5dff | 2018-08-22 20:19:49 +0200 | [diff] [blame] | 1771 | void BootAnimation::handleViewport(nsecs_t timestep) { | 
| Parth Sane | 7f7b221 | 2023-04-12 17:23:02 +0000 | [diff] [blame] | 1772 | ATRACE_CALL(); | 
| Adrian Roos | 9ee5dff | 2018-08-22 20:19:49 +0200 | [diff] [blame] | 1773 | if (mShuttingDown || !mFlingerSurfaceControl || mTargetInset == 0) { | 
|  | 1774 | return; | 
|  | 1775 | } | 
|  | 1776 | if (mTargetInset < 0) { | 
|  | 1777 | // Poll the amount for the top display inset. This will return -1 until persistent properties | 
|  | 1778 | // have been loaded. | 
|  | 1779 | mTargetInset = android::base::GetIntProperty("persist.sys.displayinset.top", | 
|  | 1780 | -1 /* default */, -1 /* min */, mHeight / 2 /* max */); | 
|  | 1781 | } | 
|  | 1782 | if (mTargetInset <= 0) { | 
|  | 1783 | return; | 
|  | 1784 | } | 
|  | 1785 |  | 
|  | 1786 | if (mCurrentInset < mTargetInset) { | 
|  | 1787 | // After the device boots, the inset will effectively be cropped away. We animate this here. | 
|  | 1788 | float fraction = static_cast<float>(mCurrentInset) / mTargetInset; | 
|  | 1789 | int interpolatedInset = (cosf((fraction + 1) * M_PI) / 2.0f + 0.5f) * mTargetInset; | 
|  | 1790 |  | 
|  | 1791 | SurfaceComposerClient::Transaction() | 
|  | 1792 | .setCrop(mFlingerSurfaceControl, Rect(0, interpolatedInset, mWidth, mHeight)) | 
|  | 1793 | .apply(); | 
|  | 1794 | } else { | 
|  | 1795 | // At the end of the animation, we switch to the viewport that DisplayManager will apply | 
|  | 1796 | // later. This changes the coordinate system, and means we must move the surface up by | 
|  | 1797 | // the inset amount. | 
| Adrian Roos | 9ee5dff | 2018-08-22 20:19:49 +0200 | [diff] [blame] | 1798 | Rect layerStackRect(0, 0, mWidth, mHeight - mTargetInset); | 
|  | 1799 | Rect displayRect(0, mTargetInset, mWidth, mHeight); | 
|  | 1800 |  | 
|  | 1801 | SurfaceComposerClient::Transaction t; | 
|  | 1802 | t.setPosition(mFlingerSurfaceControl, 0, -mTargetInset) | 
|  | 1803 | .setCrop(mFlingerSurfaceControl, Rect(0, mTargetInset, mWidth, mHeight)); | 
| Dominik Laskowski | 2a3d9aa | 2019-11-18 20:26:39 -0800 | [diff] [blame] | 1804 | t.setDisplayProjection(mDisplayToken, ui::ROTATION_0, layerStackRect, displayRect); | 
| Adrian Roos | 9ee5dff | 2018-08-22 20:19:49 +0200 | [diff] [blame] | 1805 | t.apply(); | 
|  | 1806 |  | 
|  | 1807 | mTargetInset = mCurrentInset = 0; | 
|  | 1808 | } | 
|  | 1809 |  | 
|  | 1810 | int delta = timestep * mTargetInset / ms2ns(200); | 
|  | 1811 | mCurrentInset += delta; | 
|  | 1812 | } | 
|  | 1813 |  | 
| Marin Shalamanov | 9070c05 | 2020-05-18 19:05:17 +0200 | [diff] [blame] | 1814 | void BootAnimation::releaseAnimation(Animation* animation) const { | 
| Parth Sane | 7f7b221 | 2023-04-12 17:23:02 +0000 | [diff] [blame] | 1815 | ATRACE_CALL(); | 
| Andriy Naborskyy | 39218ba | 2015-08-16 21:32:50 -0700 | [diff] [blame] | 1816 | for (Vector<Animation::Part>::iterator it = animation->parts.begin(), | 
|  | 1817 | e = animation->parts.end(); it != e; ++it) { | 
|  | 1818 | if (it->animation) | 
|  | 1819 | releaseAnimation(it->animation); | 
|  | 1820 | } | 
|  | 1821 | if (animation->zip) | 
|  | 1822 | delete animation->zip; | 
|  | 1823 | delete animation; | 
|  | 1824 | } | 
|  | 1825 |  | 
| Marin Shalamanov | 9070c05 | 2020-05-18 19:05:17 +0200 | [diff] [blame] | 1826 | BootAnimation::Animation* BootAnimation::loadAnimation(const String8& fn) { | 
| Parth Sane | 7f7b221 | 2023-04-12 17:23:02 +0000 | [diff] [blame] | 1827 | ATRACE_CALL(); | 
| Andriy Naborskyy | 39218ba | 2015-08-16 21:32:50 -0700 | [diff] [blame] | 1828 | if (mLoadedFiles.indexOf(fn) >= 0) { | 
| Wei Wang | 159a410 | 2018-10-23 23:15:58 -0700 | [diff] [blame] | 1829 | SLOGE("File \"%s\" is already loaded. Cyclic ref is not allowed", | 
| Andriy Naborskyy | 39218ba | 2015-08-16 21:32:50 -0700 | [diff] [blame] | 1830 | fn.string()); | 
| Yi Kong | 911ac23 | 2019-03-24 01:49:02 -0700 | [diff] [blame] | 1831 | return nullptr; | 
| Andriy Naborskyy | 39218ba | 2015-08-16 21:32:50 -0700 | [diff] [blame] | 1832 | } | 
|  | 1833 | ZipFileRO *zip = ZipFileRO::open(fn); | 
| Yi Kong | 911ac23 | 2019-03-24 01:49:02 -0700 | [diff] [blame] | 1834 | if (zip == nullptr) { | 
| Wei Wang | 159a410 | 2018-10-23 23:15:58 -0700 | [diff] [blame] | 1835 | SLOGE("Failed to open animation zip \"%s\": %s", | 
| Andriy Naborskyy | 39218ba | 2015-08-16 21:32:50 -0700 | [diff] [blame] | 1836 | fn.string(), strerror(errno)); | 
| Yi Kong | 911ac23 | 2019-03-24 01:49:02 -0700 | [diff] [blame] | 1837 | return nullptr; | 
| Andriy Naborskyy | 39218ba | 2015-08-16 21:32:50 -0700 | [diff] [blame] | 1838 | } | 
|  | 1839 |  | 
| Yoshiaki Maruoka | 9ef19b8 | 2017-10-25 13:55:49 +0900 | [diff] [blame] | 1840 | ALOGD("%s is loaded successfully", fn.string()); | 
|  | 1841 |  | 
| Andriy Naborskyy | 39218ba | 2015-08-16 21:32:50 -0700 | [diff] [blame] | 1842 | Animation *animation =  new Animation; | 
|  | 1843 | animation->fileName = fn; | 
|  | 1844 | animation->zip = zip; | 
| Damien Bargiacchi | 0e3d2ab | 2016-08-29 04:11:19 -0700 | [diff] [blame] | 1845 | animation->clockFont.map = nullptr; | 
| Andriy Naborskyy | 39218ba | 2015-08-16 21:32:50 -0700 | [diff] [blame] | 1846 | mLoadedFiles.add(animation->fileName); | 
|  | 1847 |  | 
|  | 1848 | parseAnimationDesc(*animation); | 
| Geoffrey Pitsch | a91a2d7 | 2016-07-12 14:46:19 -0400 | [diff] [blame] | 1849 | if (!preloadZip(*animation)) { | 
| Greg Kaiser | 7426ec8 | 2019-09-11 14:34:27 -0700 | [diff] [blame] | 1850 | releaseAnimation(animation); | 
| Yi Kong | 911ac23 | 2019-03-24 01:49:02 -0700 | [diff] [blame] | 1851 | return nullptr; | 
| Geoffrey Pitsch | a91a2d7 | 2016-07-12 14:46:19 -0400 | [diff] [blame] | 1852 | } | 
|  | 1853 |  | 
| Andriy Naborskyy | 39218ba | 2015-08-16 21:32:50 -0700 | [diff] [blame] | 1854 | mLoadedFiles.remove(fn); | 
|  | 1855 | return animation; | 
|  | 1856 | } | 
| Damien Bargiacchi | 9748086 | 2016-03-29 14:55:55 -0700 | [diff] [blame] | 1857 |  | 
|  | 1858 | bool BootAnimation::updateIsTimeAccurate() { | 
| Parth Sane | 7f7b221 | 2023-04-12 17:23:02 +0000 | [diff] [blame] | 1859 | ATRACE_CALL(); | 
| Damien Bargiacchi | 9748086 | 2016-03-29 14:55:55 -0700 | [diff] [blame] | 1860 | static constexpr long long MAX_TIME_IN_PAST =   60000LL * 60LL * 24LL * 30LL;  // 30 days | 
|  | 1861 | static constexpr long long MAX_TIME_IN_FUTURE = 60000LL * 90LL;  // 90 minutes | 
|  | 1862 |  | 
|  | 1863 | if (mTimeIsAccurate) { | 
|  | 1864 | return true; | 
|  | 1865 | } | 
| Keun-young Park | b593842 | 2017-03-23 13:46:24 -0700 | [diff] [blame] | 1866 | if (mShuttingDown) return true; | 
| Damien Bargiacchi | 9748086 | 2016-03-29 14:55:55 -0700 | [diff] [blame] | 1867 | struct stat statResult; | 
| Damien Bargiacchi | 9071db1 | 2016-10-28 17:38:22 -0700 | [diff] [blame] | 1868 |  | 
|  | 1869 | if(stat(TIME_FORMAT_12_HOUR_FLAG_FILE_PATH, &statResult) == 0) { | 
|  | 1870 | mTimeFormat12Hour = true; | 
|  | 1871 | } | 
|  | 1872 |  | 
| Damien Bargiacchi | 9748086 | 2016-03-29 14:55:55 -0700 | [diff] [blame] | 1873 | if(stat(ACCURATE_TIME_FLAG_FILE_PATH, &statResult) == 0) { | 
|  | 1874 | mTimeIsAccurate = true; | 
|  | 1875 | return true; | 
|  | 1876 | } | 
|  | 1877 |  | 
|  | 1878 | FILE* file = fopen(LAST_TIME_CHANGED_FILE_PATH, "r"); | 
| Yi Kong | 911ac23 | 2019-03-24 01:49:02 -0700 | [diff] [blame] | 1879 | if (file != nullptr) { | 
| Damien Bargiacchi | 9748086 | 2016-03-29 14:55:55 -0700 | [diff] [blame] | 1880 | long long lastChangedTime = 0; | 
|  | 1881 | fscanf(file, "%lld", &lastChangedTime); | 
|  | 1882 | fclose(file); | 
|  | 1883 | if (lastChangedTime > 0) { | 
|  | 1884 | struct timespec now; | 
|  | 1885 | clock_gettime(CLOCK_REALTIME, &now); | 
|  | 1886 | // Match the Java timestamp format | 
|  | 1887 | long long rtcNow = (now.tv_sec * 1000LL) + (now.tv_nsec / 1000000LL); | 
| Damien Bargiacchi | 9676281 | 2016-07-12 15:53:40 -0700 | [diff] [blame] | 1888 | if (ACCURATE_TIME_EPOCH < rtcNow | 
|  | 1889 | && lastChangedTime > (rtcNow - MAX_TIME_IN_PAST) | 
|  | 1890 | && lastChangedTime < (rtcNow + MAX_TIME_IN_FUTURE)) { | 
| Damien Bargiacchi | 9748086 | 2016-03-29 14:55:55 -0700 | [diff] [blame] | 1891 | mTimeIsAccurate = true; | 
|  | 1892 | } | 
|  | 1893 | } | 
|  | 1894 | } | 
|  | 1895 |  | 
|  | 1896 | return mTimeIsAccurate; | 
|  | 1897 | } | 
|  | 1898 |  | 
|  | 1899 | BootAnimation::TimeCheckThread::TimeCheckThread(BootAnimation* bootAnimation) : Thread(false), | 
| Josh Yang | f95b200 | 2021-12-23 14:32:28 -0800 | [diff] [blame] | 1900 | mInotifyFd(-1), mBootAnimWd(-1), mTimeWd(-1), mBootAnimation(bootAnimation) {} | 
| Damien Bargiacchi | 9748086 | 2016-03-29 14:55:55 -0700 | [diff] [blame] | 1901 |  | 
|  | 1902 | BootAnimation::TimeCheckThread::~TimeCheckThread() { | 
| Parth Sane | 7f7b221 | 2023-04-12 17:23:02 +0000 | [diff] [blame] | 1903 | ATRACE_CALL(); | 
| Damien Bargiacchi | 9748086 | 2016-03-29 14:55:55 -0700 | [diff] [blame] | 1904 | // mInotifyFd may be -1 but that's ok since we're not at risk of attempting to close a valid FD. | 
|  | 1905 | close(mInotifyFd); | 
|  | 1906 | } | 
|  | 1907 |  | 
|  | 1908 | bool BootAnimation::TimeCheckThread::threadLoop() { | 
| Parth Sane | 7f7b221 | 2023-04-12 17:23:02 +0000 | [diff] [blame] | 1909 | ATRACE_CALL(); | 
| Damien Bargiacchi | 9748086 | 2016-03-29 14:55:55 -0700 | [diff] [blame] | 1910 | bool shouldLoop = doThreadLoop() && !mBootAnimation->mTimeIsAccurate | 
|  | 1911 | && mBootAnimation->mClockEnabled; | 
|  | 1912 | if (!shouldLoop) { | 
|  | 1913 | close(mInotifyFd); | 
|  | 1914 | mInotifyFd = -1; | 
|  | 1915 | } | 
|  | 1916 | return shouldLoop; | 
|  | 1917 | } | 
|  | 1918 |  | 
|  | 1919 | bool BootAnimation::TimeCheckThread::doThreadLoop() { | 
| Parth Sane | 7f7b221 | 2023-04-12 17:23:02 +0000 | [diff] [blame] | 1920 | ATRACE_CALL(); | 
| Damien Bargiacchi | 9748086 | 2016-03-29 14:55:55 -0700 | [diff] [blame] | 1921 | static constexpr int BUFF_LEN (10 * (sizeof(struct inotify_event) + NAME_MAX + 1)); | 
|  | 1922 |  | 
|  | 1923 | // Poll instead of doing a blocking read so the Thread can exit if requested. | 
|  | 1924 | struct pollfd pfd = { mInotifyFd, POLLIN, 0 }; | 
|  | 1925 | ssize_t pollResult = poll(&pfd, 1, 1000); | 
|  | 1926 |  | 
|  | 1927 | if (pollResult == 0) { | 
|  | 1928 | return true; | 
|  | 1929 | } else if (pollResult < 0) { | 
| Wei Wang | 159a410 | 2018-10-23 23:15:58 -0700 | [diff] [blame] | 1930 | SLOGE("Could not poll inotify events"); | 
| Damien Bargiacchi | 9748086 | 2016-03-29 14:55:55 -0700 | [diff] [blame] | 1931 | return false; | 
|  | 1932 | } | 
|  | 1933 |  | 
|  | 1934 | char buff[BUFF_LEN] __attribute__ ((aligned(__alignof__(struct inotify_event))));; | 
|  | 1935 | ssize_t length = read(mInotifyFd, buff, BUFF_LEN); | 
|  | 1936 | if (length == 0) { | 
|  | 1937 | return true; | 
|  | 1938 | } else if (length < 0) { | 
| Wei Wang | 159a410 | 2018-10-23 23:15:58 -0700 | [diff] [blame] | 1939 | SLOGE("Could not read inotify events"); | 
| Damien Bargiacchi | 9748086 | 2016-03-29 14:55:55 -0700 | [diff] [blame] | 1940 | return false; | 
|  | 1941 | } | 
|  | 1942 |  | 
|  | 1943 | const struct inotify_event *event; | 
|  | 1944 | for (char* ptr = buff; ptr < buff + length; ptr += sizeof(struct inotify_event) + event->len) { | 
|  | 1945 | event = (const struct inotify_event *) ptr; | 
| Josh Yang | f95b200 | 2021-12-23 14:32:28 -0800 | [diff] [blame] | 1946 | if (event->wd == mBootAnimWd && strcmp(BOOTANIM_TIME_DIR_NAME, event->name) == 0) { | 
| Damien Bargiacchi | 9748086 | 2016-03-29 14:55:55 -0700 | [diff] [blame] | 1947 | addTimeDirWatch(); | 
|  | 1948 | } else if (event->wd == mTimeWd && (strcmp(LAST_TIME_CHANGED_FILE_NAME, event->name) == 0 | 
|  | 1949 | || strcmp(ACCURATE_TIME_FLAG_FILE_NAME, event->name) == 0)) { | 
|  | 1950 | return !mBootAnimation->updateIsTimeAccurate(); | 
|  | 1951 | } | 
|  | 1952 | } | 
|  | 1953 |  | 
|  | 1954 | return true; | 
|  | 1955 | } | 
|  | 1956 |  | 
|  | 1957 | void BootAnimation::TimeCheckThread::addTimeDirWatch() { | 
| Parth Sane | 7f7b221 | 2023-04-12 17:23:02 +0000 | [diff] [blame] | 1958 | ATRACE_CALL(); | 
| Josh Yang | f95b200 | 2021-12-23 14:32:28 -0800 | [diff] [blame] | 1959 | mTimeWd = inotify_add_watch(mInotifyFd, BOOTANIM_TIME_DIR_PATH, | 
| Damien Bargiacchi | 9748086 | 2016-03-29 14:55:55 -0700 | [diff] [blame] | 1960 | IN_CLOSE_WRITE | IN_MOVED_TO | IN_ATTRIB); | 
|  | 1961 | if (mTimeWd > 0) { | 
|  | 1962 | // No need to watch for the time directory to be created if it already exists | 
| Josh Yang | f95b200 | 2021-12-23 14:32:28 -0800 | [diff] [blame] | 1963 | inotify_rm_watch(mInotifyFd, mBootAnimWd); | 
|  | 1964 | mBootAnimWd = -1; | 
| Damien Bargiacchi | 9748086 | 2016-03-29 14:55:55 -0700 | [diff] [blame] | 1965 | } | 
|  | 1966 | } | 
|  | 1967 |  | 
|  | 1968 | status_t BootAnimation::TimeCheckThread::readyToRun() { | 
| Parth Sane | 7f7b221 | 2023-04-12 17:23:02 +0000 | [diff] [blame] | 1969 | ATRACE_CALL(); | 
| Damien Bargiacchi | 9748086 | 2016-03-29 14:55:55 -0700 | [diff] [blame] | 1970 | mInotifyFd = inotify_init(); | 
|  | 1971 | if (mInotifyFd < 0) { | 
| Wei Wang | 159a410 | 2018-10-23 23:15:58 -0700 | [diff] [blame] | 1972 | SLOGE("Could not initialize inotify fd"); | 
| Damien Bargiacchi | 9748086 | 2016-03-29 14:55:55 -0700 | [diff] [blame] | 1973 | return NO_INIT; | 
|  | 1974 | } | 
|  | 1975 |  | 
| Josh Yang | f95b200 | 2021-12-23 14:32:28 -0800 | [diff] [blame] | 1976 | mBootAnimWd = inotify_add_watch(mInotifyFd, BOOTANIM_DATA_DIR_PATH, IN_CREATE | IN_ATTRIB); | 
|  | 1977 | if (mBootAnimWd < 0) { | 
| Damien Bargiacchi | 9748086 | 2016-03-29 14:55:55 -0700 | [diff] [blame] | 1978 | close(mInotifyFd); | 
|  | 1979 | mInotifyFd = -1; | 
| Josh Yang | f95b200 | 2021-12-23 14:32:28 -0800 | [diff] [blame] | 1980 | SLOGE("Could not add watch for %s: %s", BOOTANIM_DATA_DIR_PATH, strerror(errno)); | 
| Damien Bargiacchi | 9748086 | 2016-03-29 14:55:55 -0700 | [diff] [blame] | 1981 | return NO_INIT; | 
|  | 1982 | } | 
|  | 1983 |  | 
|  | 1984 | addTimeDirWatch(); | 
|  | 1985 |  | 
|  | 1986 | if (mBootAnimation->updateIsTimeAccurate()) { | 
|  | 1987 | close(mInotifyFd); | 
|  | 1988 | mInotifyFd = -1; | 
|  | 1989 | return ALREADY_EXISTS; | 
|  | 1990 | } | 
|  | 1991 |  | 
|  | 1992 | return NO_ERROR; | 
|  | 1993 | } | 
|  | 1994 |  | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1995 | // --------------------------------------------------------------------------- | 
|  | 1996 |  | 
| Marin Shalamanov | 9070c05 | 2020-05-18 19:05:17 +0200 | [diff] [blame] | 1997 | } // namespace android |