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