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