blob: 7b616d91c58b7eb8b81eaddb3276a8bb2043a0b5 [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
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033#include <EGL/egl.h>
Lucas Dupindd7ed602021-07-29 21:49:07 +000034#include <GLES2/gl2.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036namespace android {
37
Mathias Agopian8335f1c2012-02-25 18:48:35 -080038class Surface;
39class SurfaceComposerClient;
40class SurfaceControl;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041
42// ---------------------------------------------------------------------------
43
Mathias Agopianbc726112009-09-23 15:44:05 -070044class BootAnimation : public Thread, public IBinder::DeathRecipient
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045{
46public:
Yegor Malyshev6925e1b2020-09-10 16:42:20 +020047 static constexpr int MAX_FADED_FRAMES_COUNT = std::numeric_limits<int>::max();
48
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049 struct Texture {
50 GLint w;
51 GLint h;
52 GLuint name;
53 };
54
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -070055 struct Font {
56 FileMap* map;
57 Texture texture;
58 int char_width;
59 int char_height;
60 };
61
Mathias Agopiana8826d62009-10-01 03:10:14 -070062 struct Animation {
63 struct Frame {
64 String8 name;
65 FileMap* map;
Geoffrey Pitschdd214a72016-06-27 17:14:30 -040066 int trimX;
67 int trimY;
68 int trimWidth;
69 int trimHeight;
Mathias Agopiana8826d62009-10-01 03:10:14 -070070 mutable GLuint tid;
71 bool operator < (const Frame& rhs) const {
72 return name < rhs.name;
73 }
74 };
75 struct Part {
Damien Bargiacchia704b7d2016-02-16 16:55:49 -080076 int count; // The number of times this part should repeat, 0 for infinite
77 int pause; // The number of frames to pause for at the end of this part
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -070078 int clockPosX; // The x position of the clock, in pixels. Positive values offset from
79 // the left of the screen, negative values offset from the right.
80 int clockPosY; // The y position of the clock, in pixels. Positive values offset from
81 // the bottom of the screen, negative values offset from the top.
82 // If either of the above are INT_MIN the clock is disabled, if INT_MAX
83 // the clock is centred on that axis.
Mathias Agopiana8826d62009-10-01 03:10:14 -070084 String8 path;
Geoffrey Pitschdd214a72016-06-27 17:14:30 -040085 String8 trimData;
Mathias Agopiana8826d62009-10-01 03:10:14 -070086 SortedVector<Frame> frames;
Kevin Hesterd3782b22012-04-26 10:38:55 -070087 bool playUntilComplete;
Yegor Malyshev6925e1b2020-09-10 16:42:20 +020088 int framesToFadeCount;
Jesse Hall083b84c2014-09-22 10:51:09 -070089 float backgroundColor[3];
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -070090 uint8_t* audioData;
91 int audioLength;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -070092 Animation* animation;
Yegor Malyshev6925e1b2020-09-10 16:42:20 +020093
94 bool hasFadingPhase() const {
95 return !playUntilComplete && framesToFadeCount > 0;
96 }
Mathias Agopiana8826d62009-10-01 03:10:14 -070097 };
98 int fps;
99 int width;
100 int height;
Nicolas Geoffray41fffb42020-12-15 10:50:29 +0000101 bool progressEnabled;
Mathias Agopiana8826d62009-10-01 03:10:14 -0700102 Vector<Part> parts;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700103 String8 audioConf;
104 String8 fileName;
105 ZipFileRO* zip;
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700106 Font clockFont;
Nicolas Geoffray41fffb42020-12-15 10:50:29 +0000107 Font progressFont;
Mathias Agopiana8826d62009-10-01 03:10:14 -0700108 };
109
Ed Coyne7464ac92017-06-08 12:26:48 -0700110 // All callbacks will be called from this class's internal thread.
111 class Callbacks : public RefBase {
112 public:
113 // Will be called during initialization after we have loaded
114 // the animation and be provided with all parts in animation.
115 virtual void init(const Vector<Animation::Part>& /*parts*/) {}
Ed Coyne2c9e94a2017-05-31 10:08:28 -0700116
Ed Coyne7464ac92017-06-08 12:26:48 -0700117 // Will be called while animation is playing before each part is
118 // played. It will be provided with the part and play count for it.
119 // It will be provided with the partNumber for the part about to be played,
120 // as well as a reference to the part itself. It will also be provided with
121 // which play of that part is about to start, some parts are repeated
122 // multiple times.
123 virtual void playPart(int /*partNumber*/, const Animation::Part& /*part*/,
124 int /*playNumber*/) {}
Ed Coyne2c9e94a2017-05-31 10:08:28 -0700125
Ed Coyne7464ac92017-06-08 12:26:48 -0700126 // Will be called when animation is done and thread is shutting down.
127 virtual void shutdown() {}
128 };
129
Chih-Hung Hsieha08d2c22018-12-20 13:39:40 -0800130 explicit BootAnimation(sp<Callbacks> callbacks);
Huihong Luo50a2f362018-08-11 09:27:57 -0700131 virtual ~BootAnimation();
Ed Coyne2c9e94a2017-05-31 10:08:28 -0700132
133 sp<SurfaceComposerClient> session() const;
134
135private:
136 virtual bool threadLoop();
137 virtual status_t readyToRun();
138 virtual void onFirstRef();
139 virtual void binderDied(const wp<IBinder>& who);
140
141 bool updateIsTimeAccurate();
142
143 class TimeCheckThread : public Thread {
144 public:
Chih-Hung Hsieha08d2c22018-12-20 13:39:40 -0800145 explicit TimeCheckThread(BootAnimation* bootAnimation);
Ed Coyne2c9e94a2017-05-31 10:08:28 -0700146 virtual ~TimeCheckThread();
147 private:
148 virtual status_t readyToRun();
149 virtual bool threadLoop();
150 bool doThreadLoop();
151 void addTimeDirWatch();
152
153 int mInotifyFd;
154 int mSystemWd;
155 int mTimeWd;
156 BootAnimation* mBootAnimation;
157 };
158
Marin Shalamanov9070c052020-05-18 19:05:17 +0200159 // Display event handling
160 class DisplayEventCallback;
161 std::unique_ptr<DisplayEventReceiver> mDisplayEventReceiver;
162 sp<Looper> mLooper;
163 int displayEventCallback(int fd, int events, void* data);
164 void processDisplayEvents();
165
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800166 status_t initTexture(Texture* texture, AssetManager& asset, const char* name);
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700167 status_t initTexture(FileMap* map, int* width, int* height);
168 status_t initFont(Font* font, const char* fallback);
Lucas Dupindd7ed602021-07-29 21:49:07 +0000169 void initShaders();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800170 bool android();
Mathias Agopiana8826d62009-10-01 03:10:14 -0700171 bool movie();
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700172 void drawText(const char* str, const Font& font, bool bold, int* x, int* y);
173 void drawClock(const Font& font, const int xPos, const int yPos);
Nicolas Geoffray41fffb42020-12-15 10:50:29 +0000174 void drawProgress(int percent, const Font& font, const int xPos, const int yPos);
Yegor Malyshev6925e1b2020-09-10 16:42:20 +0200175 void fadeFrame(int frameLeft, int frameBottom, int frameWidth, int frameHeight,
176 const Animation::Part& part, int fadedFramesCount);
Lucas Dupindd7ed602021-07-29 21:49:07 +0000177 void drawTexturedQuad(float xStart, float yStart, float width, float height);
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700178 bool validClock(const Animation::Part& part);
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700179 Animation* loadAnimation(const String8&);
180 bool playAnimation(const Animation&);
181 void releaseAnimation(Animation*) const;
182 bool parseAnimationDesc(Animation&);
183 bool preloadZip(Animation &animation);
Huihong Luo50a2f362018-08-11 09:27:57 -0700184 void findBootAnimationFile();
Nikita Ioffe06c986e2020-01-27 17:16:03 +0000185 bool findBootAnimationFileInternal(const std::vector<std::string>& files);
Huihong Luo50a2f362018-08-11 09:27:57 -0700186 bool preloadAnimation();
Marin Shalamanov9070c052020-05-18 19:05:17 +0200187 EGLConfig getEglConfig(const EGLDisplay&);
Marin Shalamanov047802d2020-05-19 23:55:12 +0200188 ui::Size limitSurfaceSize(int width, int height) const;
Marin Shalamanov9070c052020-05-18 19:05:17 +0200189 void resizeSurface(int newWidth, int newHeight);
Yegor Malyshev6925e1b2020-09-10 16:42:20 +0200190 void projectSceneToWindow();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800191
Nicolas Geoffraya1298752021-04-20 15:15:06 +0100192 bool shouldStopPlayingPart(const Animation::Part& part, int fadedFramesCount,
193 int lastDisplayedProgress);
Kevin Hesterd3782b22012-04-26 10:38:55 -0700194 void checkExit();
195
Adrian Roos9ee5dff2018-08-22 20:19:49 +0200196 void handleViewport(nsecs_t timestep);
197
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800198 sp<SurfaceComposerClient> mSession;
199 AssetManager mAssets;
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700200 Texture mAndroid[2];
201 int mWidth;
202 int mHeight;
Marin Shalamanov047802d2020-05-19 23:55:12 +0200203 int mMaxWidth = 0;
204 int mMaxHeight = 0;
Adrian Roos9ee5dff2018-08-22 20:19:49 +0200205 int mCurrentInset;
206 int mTargetInset;
Sai Kiran Korwar27167492015-07-07 20:00:06 +0530207 bool mUseNpotTextures = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800208 EGLDisplay mDisplay;
209 EGLDisplay mContext;
210 EGLDisplay mSurface;
Dominik Laskowski3316a0a2019-01-25 02:56:41 -0800211 sp<IBinder> mDisplayToken;
Mathias Agopian17f638b2009-04-16 20:04:08 -0700212 sp<SurfaceControl> mFlingerSurfaceControl;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800213 sp<Surface> mFlingerSurface;
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800214 bool mClockEnabled;
Damien Bargiacchi97480862016-03-29 14:55:55 -0700215 bool mTimeIsAccurate;
Damien Bargiacchi9071db12016-10-28 17:38:22 -0700216 bool mTimeFormat12Hour;
Keun-young Parkb5938422017-03-23 13:46:24 -0700217 bool mShuttingDown;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700218 String8 mZipFileName;
219 SortedVector<String8> mLoadedFiles;
Geoffrey Pitscha9173532016-07-19 14:56:39 -0400220 sp<TimeCheckThread> mTimeCheckThread = nullptr;
Ed Coyne7464ac92017-06-08 12:26:48 -0700221 sp<Callbacks> mCallbacks;
Huihong Luo50a2f362018-08-11 09:27:57 -0700222 Animation* mAnimation = nullptr;
Lucas Dupindd7ed602021-07-29 21:49:07 +0000223 GLuint mImageShader;
224 GLuint mTextShader;
225 GLuint mImageFadeLocation;
226 GLuint mImageTextureLocation;
227 GLuint mTextCropAreaLocation;
228 GLuint mTextTextureLocation;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800229};
230
231// ---------------------------------------------------------------------------
232
233}; // namespace android
234
235#endif // ANDROID_BOOTANIMATION_H