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