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