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