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