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