blob: 4699cfe2ac2d506cd2b7cd86675052b7c607ebbf [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>
34#include <GLES/gl.h>
35
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;
101 Vector<Part> parts;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700102 String8 audioConf;
103 String8 fileName;
104 ZipFileRO* zip;
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700105 Font clockFont;
Mathias Agopiana8826d62009-10-01 03:10:14 -0700106 };
107
Ed Coyne7464ac92017-06-08 12:26:48 -0700108 // All callbacks will be called from this class's internal thread.
109 class Callbacks : public RefBase {
110 public:
111 // Will be called during initialization after we have loaded
112 // the animation and be provided with all parts in animation.
113 virtual void init(const Vector<Animation::Part>& /*parts*/) {}
Ed Coyne2c9e94a2017-05-31 10:08:28 -0700114
Ed Coyne7464ac92017-06-08 12:26:48 -0700115 // Will be called while animation is playing before each part is
116 // played. It will be provided with the part and play count for it.
117 // It will be provided with the partNumber for the part about to be played,
118 // as well as a reference to the part itself. It will also be provided with
119 // which play of that part is about to start, some parts are repeated
120 // multiple times.
121 virtual void playPart(int /*partNumber*/, const Animation::Part& /*part*/,
122 int /*playNumber*/) {}
Ed Coyne2c9e94a2017-05-31 10:08:28 -0700123
Ed Coyne7464ac92017-06-08 12:26:48 -0700124 // Will be called when animation is done and thread is shutting down.
125 virtual void shutdown() {}
126 };
127
Chih-Hung Hsieha08d2c22018-12-20 13:39:40 -0800128 explicit BootAnimation(sp<Callbacks> callbacks);
Huihong Luo50a2f362018-08-11 09:27:57 -0700129 virtual ~BootAnimation();
Ed Coyne2c9e94a2017-05-31 10:08:28 -0700130
131 sp<SurfaceComposerClient> session() const;
132
133private:
134 virtual bool threadLoop();
135 virtual status_t readyToRun();
136 virtual void onFirstRef();
137 virtual void binderDied(const wp<IBinder>& who);
138
139 bool updateIsTimeAccurate();
140
141 class TimeCheckThread : public Thread {
142 public:
Chih-Hung Hsieha08d2c22018-12-20 13:39:40 -0800143 explicit TimeCheckThread(BootAnimation* bootAnimation);
Ed Coyne2c9e94a2017-05-31 10:08:28 -0700144 virtual ~TimeCheckThread();
145 private:
146 virtual status_t readyToRun();
147 virtual bool threadLoop();
148 bool doThreadLoop();
149 void addTimeDirWatch();
150
151 int mInotifyFd;
152 int mSystemWd;
153 int mTimeWd;
154 BootAnimation* mBootAnimation;
155 };
156
Marin Shalamanov9070c052020-05-18 19:05:17 +0200157 // Display event handling
158 class DisplayEventCallback;
159 std::unique_ptr<DisplayEventReceiver> mDisplayEventReceiver;
160 sp<Looper> mLooper;
161 int displayEventCallback(int fd, int events, void* data);
162 void processDisplayEvents();
163
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800164 status_t initTexture(Texture* texture, AssetManager& asset, const char* name);
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700165 status_t initTexture(FileMap* map, int* width, int* height);
166 status_t initFont(Font* font, const char* fallback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800167 bool android();
Mathias Agopiana8826d62009-10-01 03:10:14 -0700168 bool movie();
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700169 void drawText(const char* str, const Font& font, bool bold, int* x, int* y);
170 void drawClock(const Font& font, const int xPos, const int yPos);
Yegor Malyshev6925e1b2020-09-10 16:42:20 +0200171 void fadeFrame(int frameLeft, int frameBottom, int frameWidth, int frameHeight,
172 const Animation::Part& part, int fadedFramesCount);
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700173 bool validClock(const Animation::Part& part);
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700174 Animation* loadAnimation(const String8&);
175 bool playAnimation(const Animation&);
176 void releaseAnimation(Animation*) const;
177 bool parseAnimationDesc(Animation&);
178 bool preloadZip(Animation &animation);
Huihong Luo50a2f362018-08-11 09:27:57 -0700179 void findBootAnimationFile();
Nikita Ioffe06c986e2020-01-27 17:16:03 +0000180 bool findBootAnimationFileInternal(const std::vector<std::string>& files);
Huihong Luo50a2f362018-08-11 09:27:57 -0700181 bool preloadAnimation();
Marin Shalamanov9070c052020-05-18 19:05:17 +0200182 EGLConfig getEglConfig(const EGLDisplay&);
Marin Shalamanov047802d2020-05-19 23:55:12 +0200183 ui::Size limitSurfaceSize(int width, int height) const;
Marin Shalamanov9070c052020-05-18 19:05:17 +0200184 void resizeSurface(int newWidth, int newHeight);
Yegor Malyshev6925e1b2020-09-10 16:42:20 +0200185 void projectSceneToWindow();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800186
Yegor Malyshev6925e1b2020-09-10 16:42:20 +0200187 bool shouldStopPlayingPart(const Animation::Part& part, int fadedFramesCount);
Kevin Hesterd3782b22012-04-26 10:38:55 -0700188 void checkExit();
189
Adrian Roos9ee5dff2018-08-22 20:19:49 +0200190 void handleViewport(nsecs_t timestep);
191
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800192 sp<SurfaceComposerClient> mSession;
193 AssetManager mAssets;
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700194 Texture mAndroid[2];
195 int mWidth;
196 int mHeight;
Marin Shalamanov047802d2020-05-19 23:55:12 +0200197 int mMaxWidth = 0;
198 int mMaxHeight = 0;
Adrian Roos9ee5dff2018-08-22 20:19:49 +0200199 int mCurrentInset;
200 int mTargetInset;
Sai Kiran Korwar27167492015-07-07 20:00:06 +0530201 bool mUseNpotTextures = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800202 EGLDisplay mDisplay;
203 EGLDisplay mContext;
204 EGLDisplay mSurface;
Dominik Laskowski3316a0a2019-01-25 02:56:41 -0800205 sp<IBinder> mDisplayToken;
Mathias Agopian17f638b2009-04-16 20:04:08 -0700206 sp<SurfaceControl> mFlingerSurfaceControl;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800207 sp<Surface> mFlingerSurface;
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800208 bool mClockEnabled;
Damien Bargiacchi97480862016-03-29 14:55:55 -0700209 bool mTimeIsAccurate;
Damien Bargiacchi9071db12016-10-28 17:38:22 -0700210 bool mTimeFormat12Hour;
Keun-young Parkb5938422017-03-23 13:46:24 -0700211 bool mShuttingDown;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700212 String8 mZipFileName;
213 SortedVector<String8> mLoadedFiles;
Geoffrey Pitscha9173532016-07-19 14:56:39 -0400214 sp<TimeCheckThread> mTimeCheckThread = nullptr;
Ed Coyne7464ac92017-06-08 12:26:48 -0700215 sp<Callbacks> mCallbacks;
Huihong Luo50a2f362018-08-11 09:27:57 -0700216 Animation* mAnimation = nullptr;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800217};
218
219// ---------------------------------------------------------------------------
220
221}; // namespace android
222
223#endif // ANDROID_BOOTANIMATION_H