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