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