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