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