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