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