blob: 8683b71a3762b16755e9d3896bd0863b0a3e441c [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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
17#ifndef ANDROID_BOOTANIMATION_H
18#define ANDROID_BOOTANIMATION_H
19
Nikita Ioffe06c986e2020-01-27 17:16:03 +000020#include <vector>
Marin Shalamanov9070c052020-05-18 19:05:17 +020021#include <queue>
Yegor Malyshev6925e1b2020-09-10 16:42:20 +020022#include <climits>
Nikita Ioffe06c986e2020-01-27 17:16:03 +000023
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024#include <stdint.h>
25#include <sys/types.h>
26
Mathias Agopianb13b9bd2012-02-17 18:27:36 -080027#include <androidfw/AssetManager.h>
Marin Shalamanov9070c052020-05-18 19:05:17 +020028#include <gui/DisplayEventReceiver.h>
29#include <utils/Looper.h>
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -070030#include <utils/Thread.h>
Ed Coyne33f4b7d2018-04-10 13:39:09 -070031#include <binder/IBinder.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032
Mariia Sandrikovaa166f012022-12-10 02:32:14 +000033#include <ui/Rotation.h>
34
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035#include <EGL/egl.h>
Lucas Dupindd7ed602021-07-29 21:49:07 +000036#include <GLES2/gl2.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038namespace android {
39
Mathias Agopian8335f1c2012-02-25 18:48:35 -080040class Surface;
41class SurfaceComposerClient;
42class SurfaceControl;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043
44// ---------------------------------------------------------------------------
45
Mathias Agopianbc726112009-09-23 15:44:05 -070046class BootAnimation : public Thread, public IBinder::DeathRecipient
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047{
48public:
Yegor Malyshev6925e1b2020-09-10 16:42:20 +020049 static constexpr int MAX_FADED_FRAMES_COUNT = std::numeric_limits<int>::max();
50
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051 struct Texture {
52 GLint w;
53 GLint h;
54 GLuint name;
55 };
56
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -070057 struct Font {
Shan Huang89965002021-08-16 16:45:39 +000058 FileMap* map = nullptr;
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -070059 Texture texture;
60 int char_width;
61 int char_height;
62 };
63
Mathias Agopiana8826d62009-10-01 03:10:14 -070064 struct Animation {
65 struct Frame {
66 String8 name;
Shan Huang89965002021-08-16 16:45:39 +000067 FileMap* map = nullptr;
Geoffrey Pitschdd214a72016-06-27 17:14:30 -040068 int trimX;
69 int trimY;
70 int trimWidth;
71 int trimHeight;
Mathias Agopiana8826d62009-10-01 03:10:14 -070072 mutable GLuint tid;
73 bool operator < (const Frame& rhs) const {
74 return name < rhs.name;
75 }
76 };
77 struct Part {
Damien Bargiacchia704b7d2016-02-16 16:55:49 -080078 int count; // The number of times this part should repeat, 0 for infinite
79 int pause; // The number of frames to pause for at the end of this part
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -070080 int clockPosX; // The x position of the clock, in pixels. Positive values offset from
81 // the left of the screen, negative values offset from the right.
82 int clockPosY; // The y position of the clock, in pixels. Positive values offset from
83 // the bottom of the screen, negative values offset from the top.
84 // If either of the above are INT_MIN the clock is disabled, if INT_MAX
85 // the clock is centred on that axis.
Mathias Agopiana8826d62009-10-01 03:10:14 -070086 String8 path;
Geoffrey Pitschdd214a72016-06-27 17:14:30 -040087 String8 trimData;
Mathias Agopiana8826d62009-10-01 03:10:14 -070088 SortedVector<Frame> frames;
Kevin Hesterd3782b22012-04-26 10:38:55 -070089 bool playUntilComplete;
Yegor Malyshev6925e1b2020-09-10 16:42:20 +020090 int framesToFadeCount;
Jesse Hall083b84c2014-09-22 10:51:09 -070091 float backgroundColor[3];
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -070092 uint8_t* audioData;
93 int audioLength;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -070094 Animation* animation;
Shan Huang89965002021-08-16 16:45:39 +000095 // Controls if dynamic coloring is enabled for this part.
96 bool useDynamicColoring = false;
97 // Defines if this part is played after the dynamic coloring part.
98 bool postDynamicColoring = false;
Yegor Malyshev6925e1b2020-09-10 16:42:20 +020099
100 bool hasFadingPhase() const {
101 return !playUntilComplete && framesToFadeCount > 0;
102 }
Mathias Agopiana8826d62009-10-01 03:10:14 -0700103 };
104 int fps;
105 int width;
106 int height;
Nicolas Geoffray41fffb42020-12-15 10:50:29 +0000107 bool progressEnabled;
Mathias Agopiana8826d62009-10-01 03:10:14 -0700108 Vector<Part> parts;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700109 String8 audioConf;
110 String8 fileName;
111 ZipFileRO* zip;
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700112 Font clockFont;
Nicolas Geoffray41fffb42020-12-15 10:50:29 +0000113 Font progressFont;
Shan Huang89965002021-08-16 16:45:39 +0000114 // Controls if dynamic coloring is enabled for the whole animation.
115 bool dynamicColoringEnabled = false;
Shan Huang488ff742021-08-25 20:10:46 +0000116 int colorTransitionStart = 0; // Start frame of dynamic color transition.
117 int colorTransitionEnd = 0; // End frame of dynamic color transition.
Shan Huang89965002021-08-16 16:45:39 +0000118 float startColors[4][3]; // Start colors of dynamic color transition.
119 float endColors[4][3]; // End colors of dynamic color transition.
Mathias Agopiana8826d62009-10-01 03:10:14 -0700120 };
121
Ed Coyne7464ac92017-06-08 12:26:48 -0700122 // All callbacks will be called from this class's internal thread.
123 class Callbacks : public RefBase {
124 public:
125 // Will be called during initialization after we have loaded
126 // the animation and be provided with all parts in animation.
127 virtual void init(const Vector<Animation::Part>& /*parts*/) {}
Ed Coyne2c9e94a2017-05-31 10:08:28 -0700128
Ed Coyne7464ac92017-06-08 12:26:48 -0700129 // Will be called while animation is playing before each part is
130 // played. It will be provided with the part and play count for it.
131 // It will be provided with the partNumber for the part about to be played,
132 // as well as a reference to the part itself. It will also be provided with
133 // which play of that part is about to start, some parts are repeated
134 // multiple times.
135 virtual void playPart(int /*partNumber*/, const Animation::Part& /*part*/,
136 int /*playNumber*/) {}
Ed Coyne2c9e94a2017-05-31 10:08:28 -0700137
Ed Coyne7464ac92017-06-08 12:26:48 -0700138 // Will be called when animation is done and thread is shutting down.
139 virtual void shutdown() {}
140 };
141
Chih-Hung Hsieha08d2c22018-12-20 13:39:40 -0800142 explicit BootAnimation(sp<Callbacks> callbacks);
Huihong Luo50a2f362018-08-11 09:27:57 -0700143 virtual ~BootAnimation();
Ed Coyne2c9e94a2017-05-31 10:08:28 -0700144
145 sp<SurfaceComposerClient> session() const;
146
147private:
148 virtual bool threadLoop();
149 virtual status_t readyToRun();
150 virtual void onFirstRef();
151 virtual void binderDied(const wp<IBinder>& who);
152
153 bool updateIsTimeAccurate();
154
155 class TimeCheckThread : public Thread {
156 public:
Chih-Hung Hsieha08d2c22018-12-20 13:39:40 -0800157 explicit TimeCheckThread(BootAnimation* bootAnimation);
Ed Coyne2c9e94a2017-05-31 10:08:28 -0700158 virtual ~TimeCheckThread();
159 private:
160 virtual status_t readyToRun();
161 virtual bool threadLoop();
162 bool doThreadLoop();
163 void addTimeDirWatch();
164
165 int mInotifyFd;
Josh Yangf95b2002021-12-23 14:32:28 -0800166 int mBootAnimWd;
Ed Coyne2c9e94a2017-05-31 10:08:28 -0700167 int mTimeWd;
168 BootAnimation* mBootAnimation;
169 };
170
Marin Shalamanov9070c052020-05-18 19:05:17 +0200171 // Display event handling
172 class DisplayEventCallback;
173 std::unique_ptr<DisplayEventReceiver> mDisplayEventReceiver;
174 sp<Looper> mLooper;
175 int displayEventCallback(int fd, int events, void* data);
176 void processDisplayEvents();
177
Shan Huang89965002021-08-16 16:45:39 +0000178 status_t initTexture(Texture* texture, AssetManager& asset, const char* name,
179 bool premultiplyAlpha = true);
180 status_t initTexture(FileMap* map, int* width, int* height,
181 bool premultiplyAlpha = true);
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700182 status_t initFont(Font* font, const char* fallback);
Lucas Dupindd7ed602021-07-29 21:49:07 +0000183 void initShaders();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800184 bool android();
Mathias Agopiana8826d62009-10-01 03:10:14 -0700185 bool movie();
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700186 void drawText(const char* str, const Font& font, bool bold, int* x, int* y);
187 void drawClock(const Font& font, const int xPos, const int yPos);
Nicolas Geoffray41fffb42020-12-15 10:50:29 +0000188 void drawProgress(int percent, const Font& font, const int xPos, const int yPos);
Yegor Malyshev6925e1b2020-09-10 16:42:20 +0200189 void fadeFrame(int frameLeft, int frameBottom, int frameWidth, int frameHeight,
190 const Animation::Part& part, int fadedFramesCount);
Lucas Dupindd7ed602021-07-29 21:49:07 +0000191 void drawTexturedQuad(float xStart, float yStart, float width, float height);
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700192 bool validClock(const Animation::Part& part);
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700193 Animation* loadAnimation(const String8&);
194 bool playAnimation(const Animation&);
195 void releaseAnimation(Animation*) const;
196 bool parseAnimationDesc(Animation&);
197 bool preloadZip(Animation &animation);
Huihong Luo50a2f362018-08-11 09:27:57 -0700198 void findBootAnimationFile();
Nikita Ioffe06c986e2020-01-27 17:16:03 +0000199 bool findBootAnimationFileInternal(const std::vector<std::string>& files);
Huihong Luo50a2f362018-08-11 09:27:57 -0700200 bool preloadAnimation();
Marin Shalamanov9070c052020-05-18 19:05:17 +0200201 EGLConfig getEglConfig(const EGLDisplay&);
Marin Shalamanov047802d2020-05-19 23:55:12 +0200202 ui::Size limitSurfaceSize(int width, int height) const;
Marin Shalamanov9070c052020-05-18 19:05:17 +0200203 void resizeSurface(int newWidth, int newHeight);
Yegor Malyshev6925e1b2020-09-10 16:42:20 +0200204 void projectSceneToWindow();
Mariia Sandrikovaa166f012022-12-10 02:32:14 +0000205 void rotateAwayFromNaturalOrientationIfNeeded();
206 ui::Rotation parseOrientationProperty();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800207
Nicolas Geoffraya1298752021-04-20 15:15:06 +0100208 bool shouldStopPlayingPart(const Animation::Part& part, int fadedFramesCount,
209 int lastDisplayedProgress);
Kevin Hesterd3782b22012-04-26 10:38:55 -0700210 void checkExit();
211
Adrian Roos9ee5dff2018-08-22 20:19:49 +0200212 void handleViewport(nsecs_t timestep);
Shan Huanga1f7a71c22021-08-18 00:56:08 +0000213 void initDynamicColors();
Adrian Roos9ee5dff2018-08-22 20:19:49 +0200214
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800215 sp<SurfaceComposerClient> mSession;
216 AssetManager mAssets;
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700217 Texture mAndroid[2];
218 int mWidth;
219 int mHeight;
joenchen764ea042022-04-11 23:58:34 +0800220 int mInitWidth;
221 int mInitHeight;
Marin Shalamanov047802d2020-05-19 23:55:12 +0200222 int mMaxWidth = 0;
223 int mMaxHeight = 0;
Adrian Roos9ee5dff2018-08-22 20:19:49 +0200224 int mCurrentInset;
225 int mTargetInset;
Sai Kiran Korwar27167492015-07-07 20:00:06 +0530226 bool mUseNpotTextures = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800227 EGLDisplay mDisplay;
228 EGLDisplay mContext;
229 EGLDisplay mSurface;
Dominik Laskowski3316a0a2019-01-25 02:56:41 -0800230 sp<IBinder> mDisplayToken;
Mathias Agopian17f638b2009-04-16 20:04:08 -0700231 sp<SurfaceControl> mFlingerSurfaceControl;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800232 sp<Surface> mFlingerSurface;
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800233 bool mClockEnabled;
Damien Bargiacchi97480862016-03-29 14:55:55 -0700234 bool mTimeIsAccurate;
Damien Bargiacchi9071db12016-10-28 17:38:22 -0700235 bool mTimeFormat12Hour;
Keun-young Parkb5938422017-03-23 13:46:24 -0700236 bool mShuttingDown;
Lucas Dupincd256842022-03-29 15:46:36 -0700237 bool mDynamicColorsApplied = false;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700238 String8 mZipFileName;
239 SortedVector<String8> mLoadedFiles;
Geoffrey Pitscha9173532016-07-19 14:56:39 -0400240 sp<TimeCheckThread> mTimeCheckThread = nullptr;
Ed Coyne7464ac92017-06-08 12:26:48 -0700241 sp<Callbacks> mCallbacks;
Huihong Luo50a2f362018-08-11 09:27:57 -0700242 Animation* mAnimation = nullptr;
Lucas Dupindd7ed602021-07-29 21:49:07 +0000243 GLuint mImageShader;
244 GLuint mTextShader;
245 GLuint mImageFadeLocation;
246 GLuint mImageTextureLocation;
247 GLuint mTextCropAreaLocation;
248 GLuint mTextTextureLocation;
Shan Huang89965002021-08-16 16:45:39 +0000249 GLuint mImageColorProgressLocation;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800250};
251
252// ---------------------------------------------------------------------------
253
254}; // namespace android
255
256#endif // ANDROID_BOOTANIMATION_H