blob: 0a057461228c7f1d717ce753ba6f67afed791259 [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>
Rahul Banerjee0c27488a2024-07-03 18:20:18 -070034#include <ui/LayerStack.h>
Mariia Sandrikovaa166f012022-12-10 02:32:14 +000035
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036#include <EGL/egl.h>
Lucas Dupindd7ed602021-07-29 21:49:07 +000037#include <GLES2/gl2.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039namespace android {
40
Mathias Agopian8335f1c2012-02-25 18:48:35 -080041class Surface;
42class SurfaceComposerClient;
43class SurfaceControl;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044
45// ---------------------------------------------------------------------------
46
Mathias Agopianbc726112009-09-23 15:44:05 -070047class BootAnimation : public Thread, public IBinder::DeathRecipient
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048{
49public:
Yegor Malyshev6925e1b2020-09-10 16:42:20 +020050 static constexpr int MAX_FADED_FRAMES_COUNT = std::numeric_limits<int>::max();
51
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052 struct Texture {
53 GLint w;
54 GLint h;
55 GLuint name;
56 };
57
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -070058 struct Font {
Shan Huang89965002021-08-16 16:45:39 +000059 FileMap* map = nullptr;
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -070060 Texture texture;
61 int char_width;
62 int char_height;
63 };
64
Mathias Agopiana8826d62009-10-01 03:10:14 -070065 struct Animation {
66 struct Frame {
67 String8 name;
Shan Huang89965002021-08-16 16:45:39 +000068 FileMap* map = nullptr;
Geoffrey Pitschdd214a72016-06-27 17:14:30 -040069 int trimX;
70 int trimY;
71 int trimWidth;
72 int trimHeight;
Mathias Agopiana8826d62009-10-01 03:10:14 -070073 mutable GLuint tid;
74 bool operator < (const Frame& rhs) const {
75 return name < rhs.name;
76 }
77 };
78 struct Part {
Damien Bargiacchia704b7d2016-02-16 16:55:49 -080079 int count; // The number of times this part should repeat, 0 for infinite
80 int pause; // The number of frames to pause for at the end of this part
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -070081 int clockPosX; // The x position of the clock, in pixels. Positive values offset from
82 // the left of the screen, negative values offset from the right.
83 int clockPosY; // The y position of the clock, in pixels. Positive values offset from
84 // the bottom of the screen, negative values offset from the top.
85 // If either of the above are INT_MIN the clock is disabled, if INT_MAX
86 // the clock is centred on that axis.
Mathias Agopiana8826d62009-10-01 03:10:14 -070087 String8 path;
Geoffrey Pitschdd214a72016-06-27 17:14:30 -040088 String8 trimData;
Mathias Agopiana8826d62009-10-01 03:10:14 -070089 SortedVector<Frame> frames;
Kevin Hesterd3782b22012-04-26 10:38:55 -070090 bool playUntilComplete;
Yegor Malyshev6925e1b2020-09-10 16:42:20 +020091 int framesToFadeCount;
Jesse Hall083b84c2014-09-22 10:51:09 -070092 float backgroundColor[3];
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -070093 uint8_t* audioData;
94 int audioLength;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -070095 Animation* animation;
Shan Huang89965002021-08-16 16:45:39 +000096 // Controls if dynamic coloring is enabled for this part.
97 bool useDynamicColoring = false;
98 // Defines if this part is played after the dynamic coloring part.
99 bool postDynamicColoring = false;
Yegor Malyshev6925e1b2020-09-10 16:42:20 +0200100
101 bool hasFadingPhase() const {
102 return !playUntilComplete && framesToFadeCount > 0;
103 }
Mathias Agopiana8826d62009-10-01 03:10:14 -0700104 };
105 int fps;
106 int width;
107 int height;
Nicolas Geoffray41fffb42020-12-15 10:50:29 +0000108 bool progressEnabled;
Mathias Agopiana8826d62009-10-01 03:10:14 -0700109 Vector<Part> parts;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700110 String8 audioConf;
111 String8 fileName;
112 ZipFileRO* zip;
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700113 Font clockFont;
Nicolas Geoffray41fffb42020-12-15 10:50:29 +0000114 Font progressFont;
Shan Huang89965002021-08-16 16:45:39 +0000115 // Controls if dynamic coloring is enabled for the whole animation.
116 bool dynamicColoringEnabled = false;
Shan Huang488ff742021-08-25 20:10:46 +0000117 int colorTransitionStart = 0; // Start frame of dynamic color transition.
118 int colorTransitionEnd = 0; // End frame of dynamic color transition.
Shan Huang89965002021-08-16 16:45:39 +0000119 float startColors[4][3]; // Start colors of dynamic color transition.
120 float endColors[4][3]; // End colors of dynamic color transition.
Mathias Agopiana8826d62009-10-01 03:10:14 -0700121 };
122
Rahul Banerjee0c27488a2024-07-03 18:20:18 -0700123 // Collects all attributes that must be tracked per physical display.
124 struct Display {
125 int width;
126 int height;
127 int initWidth;
128 int initHeight;
129 EGLDisplay eglSurface;
130 sp<IBinder> displayToken;
131 sp<SurfaceControl> surfaceControl;
132 sp<Surface> surface;
133 };
134
Ed Coyne7464ac92017-06-08 12:26:48 -0700135 // All callbacks will be called from this class's internal thread.
136 class Callbacks : public RefBase {
137 public:
138 // Will be called during initialization after we have loaded
139 // the animation and be provided with all parts in animation.
140 virtual void init(const Vector<Animation::Part>& /*parts*/) {}
Ed Coyne2c9e94a2017-05-31 10:08:28 -0700141
Ed Coyne7464ac92017-06-08 12:26:48 -0700142 // Will be called while animation is playing before each part is
143 // played. It will be provided with the part and play count for it.
144 // It will be provided with the partNumber for the part about to be played,
145 // as well as a reference to the part itself. It will also be provided with
146 // which play of that part is about to start, some parts are repeated
147 // multiple times.
148 virtual void playPart(int /*partNumber*/, const Animation::Part& /*part*/,
149 int /*playNumber*/) {}
Ed Coyne2c9e94a2017-05-31 10:08:28 -0700150
Ed Coyne7464ac92017-06-08 12:26:48 -0700151 // Will be called when animation is done and thread is shutting down.
152 virtual void shutdown() {}
153 };
154
Chih-Hung Hsieha08d2c22018-12-20 13:39:40 -0800155 explicit BootAnimation(sp<Callbacks> callbacks);
Huihong Luo50a2f362018-08-11 09:27:57 -0700156 virtual ~BootAnimation();
Ed Coyne2c9e94a2017-05-31 10:08:28 -0700157
158 sp<SurfaceComposerClient> session() const;
159
160private:
161 virtual bool threadLoop();
162 virtual status_t readyToRun();
163 virtual void onFirstRef();
164 virtual void binderDied(const wp<IBinder>& who);
165
166 bool updateIsTimeAccurate();
167
168 class TimeCheckThread : public Thread {
169 public:
Chih-Hung Hsieha08d2c22018-12-20 13:39:40 -0800170 explicit TimeCheckThread(BootAnimation* bootAnimation);
Ed Coyne2c9e94a2017-05-31 10:08:28 -0700171 virtual ~TimeCheckThread();
172 private:
173 virtual status_t readyToRun();
174 virtual bool threadLoop();
175 bool doThreadLoop();
176 void addTimeDirWatch();
177
178 int mInotifyFd;
Josh Yangf95b2002021-12-23 14:32:28 -0800179 int mBootAnimWd;
Ed Coyne2c9e94a2017-05-31 10:08:28 -0700180 int mTimeWd;
181 BootAnimation* mBootAnimation;
182 };
183
Marin Shalamanov9070c052020-05-18 19:05:17 +0200184 // Display event handling
185 class DisplayEventCallback;
186 std::unique_ptr<DisplayEventReceiver> mDisplayEventReceiver;
187 sp<Looper> mLooper;
188 int displayEventCallback(int fd, int events, void* data);
189 void processDisplayEvents();
190
Shan Huang89965002021-08-16 16:45:39 +0000191 status_t initTexture(Texture* texture, AssetManager& asset, const char* name,
192 bool premultiplyAlpha = true);
193 status_t initTexture(FileMap* map, int* width, int* height,
194 bool premultiplyAlpha = true);
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700195 status_t initFont(Font* font, const char* fallback);
Lucas Dupindd7ed602021-07-29 21:49:07 +0000196 void initShaders();
Rahul Banerjee0c27488a2024-07-03 18:20:18 -0700197 bool android(const Display& display);
198 status_t initDisplaysAndSurfaces();
Mathias Agopiana8826d62009-10-01 03:10:14 -0700199 bool movie();
Rahul Banerjee0c27488a2024-07-03 18:20:18 -0700200 void drawText(const char* str, const Font& font, bool bold,
201 int* x, int* y, const Display& display);
202 void drawClock(const Font& font, const int xPos, const int yPos, const Display& display);
203 void drawProgress(int percent, const Font& font,
204 const int xPos, const int yPos, const Display& display);
Yegor Malyshev6925e1b2020-09-10 16:42:20 +0200205 void fadeFrame(int frameLeft, int frameBottom, int frameWidth, int frameHeight,
206 const Animation::Part& part, int fadedFramesCount);
Rahul Banerjee0c27488a2024-07-03 18:20:18 -0700207 void drawTexturedQuad(float xStart, float yStart,
208 float width, float height, const Display& display);
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700209 bool validClock(const Animation::Part& part);
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700210 Animation* loadAnimation(const String8&);
211 bool playAnimation(const Animation&);
212 void releaseAnimation(Animation*) const;
213 bool parseAnimationDesc(Animation&);
214 bool preloadZip(Animation &animation);
Huihong Luo50a2f362018-08-11 09:27:57 -0700215 void findBootAnimationFile();
Nikita Ioffe06c986e2020-01-27 17:16:03 +0000216 bool findBootAnimationFileInternal(const std::vector<std::string>& files);
Huihong Luo50a2f362018-08-11 09:27:57 -0700217 bool preloadAnimation();
Marin Shalamanov9070c052020-05-18 19:05:17 +0200218 EGLConfig getEglConfig(const EGLDisplay&);
Marin Shalamanov047802d2020-05-19 23:55:12 +0200219 ui::Size limitSurfaceSize(int width, int height) const;
Rahul Banerjee0c27488a2024-07-03 18:20:18 -0700220 void resizeSurface(int newWidth, int newHeight, Display& display);
221 void projectSceneToWindow(const Display& display);
222 void rotateAwayFromNaturalOrientationIfNeeded(Display& display);
Mariia Sandrikovaa166f012022-12-10 02:32:14 +0000223 ui::Rotation parseOrientationProperty();
Rahul Banerjee0c27488a2024-07-03 18:20:18 -0700224 void configureDisplayAndLayerStack(const Display& display, ui::LayerStack layerStack);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800225
Nicolas Geoffraya1298752021-04-20 15:15:06 +0100226 bool shouldStopPlayingPart(const Animation::Part& part, int fadedFramesCount,
227 int lastDisplayedProgress);
Kevin Hesterd3782b22012-04-26 10:38:55 -0700228 void checkExit();
229
Rahul Banerjee0c27488a2024-07-03 18:20:18 -0700230 void handleViewport(nsecs_t timestep, const Display& display);
Shan Huanga1f7a71c22021-08-18 00:56:08 +0000231 void initDynamicColors();
Adrian Roos9ee5dff2018-08-22 20:19:49 +0200232
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800233 sp<SurfaceComposerClient> mSession;
234 AssetManager mAssets;
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700235 Texture mAndroid[2];
Marin Shalamanov047802d2020-05-19 23:55:12 +0200236 int mMaxWidth = 0;
237 int mMaxHeight = 0;
Adrian Roos9ee5dff2018-08-22 20:19:49 +0200238 int mCurrentInset;
239 int mTargetInset;
Sai Kiran Korwar27167492015-07-07 20:00:06 +0530240 bool mUseNpotTextures = false;
Rahul Banerjee0c27488a2024-07-03 18:20:18 -0700241 EGLDisplay mEgl;
242 EGLDisplay mEglContext;
243 // Per-Display Attributes (to support multi-display)
244 std::vector<Display> mDisplays;
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800245 bool mClockEnabled;
Damien Bargiacchi97480862016-03-29 14:55:55 -0700246 bool mTimeIsAccurate;
Damien Bargiacchi9071db12016-10-28 17:38:22 -0700247 bool mTimeFormat12Hour;
Keun-young Parkb5938422017-03-23 13:46:24 -0700248 bool mShuttingDown;
Lucas Dupincd256842022-03-29 15:46:36 -0700249 bool mDynamicColorsApplied = false;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700250 String8 mZipFileName;
251 SortedVector<String8> mLoadedFiles;
Geoffrey Pitscha9173532016-07-19 14:56:39 -0400252 sp<TimeCheckThread> mTimeCheckThread = nullptr;
Ed Coyne7464ac92017-06-08 12:26:48 -0700253 sp<Callbacks> mCallbacks;
Huihong Luo50a2f362018-08-11 09:27:57 -0700254 Animation* mAnimation = nullptr;
Lucas Dupindd7ed602021-07-29 21:49:07 +0000255 GLuint mImageShader;
256 GLuint mTextShader;
257 GLuint mImageFadeLocation;
258 GLuint mImageTextureLocation;
259 GLuint mTextCropAreaLocation;
260 GLuint mTextTextureLocation;
Shan Huang89965002021-08-16 16:45:39 +0000261 GLuint mImageColorProgressLocation;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800262};
263
264// ---------------------------------------------------------------------------
265
266}; // namespace android
267
268#endif // ANDROID_BOOTANIMATION_H