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