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