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