blob: b52222c799b0ef2dd8dedc5aa7999651fb0f3658 [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 Shalamanov1a6e36892020-05-18 19:05:17 +020021#include <queue>
Yegor Malysheveafcb992020-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 Shalamanov1a6e36892020-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 -080036class SkBitmap;
37
38namespace 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 Malysheveafcb992020-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 {
58 FileMap* map;
59 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;
67 FileMap* map;
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 Malysheveafcb992020-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;
Yegor Malysheveafcb992020-09-10 16:42:20 +020095
96 bool hasFadingPhase() const {
97 return !playUntilComplete && framesToFadeCount > 0;
98 }
Mathias Agopiana8826d62009-10-01 03:10:14 -070099 };
100 int fps;
101 int width;
102 int height;
Nicolas Geoffraybbb00e72020-12-15 10:50:29 +0000103 bool progressEnabled;
Mathias Agopiana8826d62009-10-01 03:10:14 -0700104 Vector<Part> parts;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700105 String8 audioConf;
106 String8 fileName;
107 ZipFileRO* zip;
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700108 Font clockFont;
Nicolas Geoffraybbb00e72020-12-15 10:50:29 +0000109 Font progressFont;
Mathias Agopiana8826d62009-10-01 03:10:14 -0700110 };
111
Ed Coyne7464ac92017-06-08 12:26:48 -0700112 // All callbacks will be called from this class's internal thread.
113 class Callbacks : public RefBase {
114 public:
115 // Will be called during initialization after we have loaded
116 // the animation and be provided with all parts in animation.
117 virtual void init(const Vector<Animation::Part>& /*parts*/) {}
Ed Coyne2c9e94a2017-05-31 10:08:28 -0700118
Ed Coyne7464ac92017-06-08 12:26:48 -0700119 // Will be called while animation is playing before each part is
120 // played. It will be provided with the part and play count for it.
121 // It will be provided with the partNumber for the part about to be played,
122 // as well as a reference to the part itself. It will also be provided with
123 // which play of that part is about to start, some parts are repeated
124 // multiple times.
125 virtual void playPart(int /*partNumber*/, const Animation::Part& /*part*/,
126 int /*playNumber*/) {}
Ed Coyne2c9e94a2017-05-31 10:08:28 -0700127
Ed Coyne7464ac92017-06-08 12:26:48 -0700128 // Will be called when animation is done and thread is shutting down.
129 virtual void shutdown() {}
130 };
131
Chih-Hung Hsieha08d2c22018-12-20 13:39:40 -0800132 explicit BootAnimation(sp<Callbacks> callbacks);
Huihong Luo50a2f362018-08-11 09:27:57 -0700133 virtual ~BootAnimation();
Ed Coyne2c9e94a2017-05-31 10:08:28 -0700134
135 sp<SurfaceComposerClient> session() const;
136
137private:
138 virtual bool threadLoop();
139 virtual status_t readyToRun();
140 virtual void onFirstRef();
141 virtual void binderDied(const wp<IBinder>& who);
142
143 bool updateIsTimeAccurate();
144
145 class TimeCheckThread : public Thread {
146 public:
Chih-Hung Hsieha08d2c22018-12-20 13:39:40 -0800147 explicit TimeCheckThread(BootAnimation* bootAnimation);
Ed Coyne2c9e94a2017-05-31 10:08:28 -0700148 virtual ~TimeCheckThread();
149 private:
150 virtual status_t readyToRun();
151 virtual bool threadLoop();
152 bool doThreadLoop();
153 void addTimeDirWatch();
154
155 int mInotifyFd;
156 int mSystemWd;
157 int mTimeWd;
158 BootAnimation* mBootAnimation;
159 };
160
Marin Shalamanov1a6e36892020-05-18 19:05:17 +0200161 // Display event handling
162 class DisplayEventCallback;
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);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800169 bool android();
Mathias Agopiana8826d62009-10-01 03:10:14 -0700170 bool movie();
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700171 void drawText(const char* str, const Font& font, bool bold, int* x, int* y);
172 void drawClock(const Font& font, const int xPos, const int yPos);
Nicolas Geoffraybbb00e72020-12-15 10:50:29 +0000173 void drawProgress(int percent, const Font& font, const int xPos, const int yPos);
Yegor Malysheveafcb992020-09-10 16:42:20 +0200174 void fadeFrame(int frameLeft, int frameBottom, int frameWidth, int frameHeight,
175 const Animation::Part& part, int fadedFramesCount);
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700176 bool validClock(const Animation::Part& part);
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700177 Animation* loadAnimation(const String8&);
178 bool playAnimation(const Animation&);
179 void releaseAnimation(Animation*) const;
180 bool parseAnimationDesc(Animation&);
181 bool preloadZip(Animation &animation);
Huihong Luo50a2f362018-08-11 09:27:57 -0700182 void findBootAnimationFile();
Nikita Ioffe06c986e2020-01-27 17:16:03 +0000183 bool findBootAnimationFileInternal(const std::vector<std::string>& files);
Huihong Luo50a2f362018-08-11 09:27:57 -0700184 bool preloadAnimation();
Marin Shalamanov1a6e36892020-05-18 19:05:17 +0200185 EGLConfig getEglConfig(const EGLDisplay&);
Marin Shalamanov047802d2020-05-19 23:55:12 +0200186 ui::Size limitSurfaceSize(int width, int height) const;
Marin Shalamanov1a6e36892020-05-18 19:05:17 +0200187 void resizeSurface(int newWidth, int newHeight);
Yegor Malysheveafcb992020-09-10 16:42:20 +0200188 void projectSceneToWindow();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800189
Yegor Malysheveafcb992020-09-10 16:42:20 +0200190 bool shouldStopPlayingPart(const Animation::Part& part, int fadedFramesCount);
Kevin Hesterd3782b22012-04-26 10:38:55 -0700191 void checkExit();
192
Adrian Roos9ee5dff2018-08-22 20:19:49 +0200193 void handleViewport(nsecs_t timestep);
194
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800195 sp<SurfaceComposerClient> mSession;
196 AssetManager mAssets;
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700197 Texture mAndroid[2];
198 int mWidth;
199 int mHeight;
Marin Shalamanov047802d2020-05-19 23:55:12 +0200200 int mMaxWidth = 0;
201 int mMaxHeight = 0;
Adrian Roos9ee5dff2018-08-22 20:19:49 +0200202 int mCurrentInset;
203 int mTargetInset;
Sai Kiran Korwar27167492015-07-07 20:00:06 +0530204 bool mUseNpotTextures = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800205 EGLDisplay mDisplay;
206 EGLDisplay mContext;
207 EGLDisplay mSurface;
Dominik Laskowski3316a0a2019-01-25 02:56:41 -0800208 sp<IBinder> mDisplayToken;
Mathias Agopian17f638b2009-04-16 20:04:08 -0700209 sp<SurfaceControl> mFlingerSurfaceControl;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800210 sp<Surface> mFlingerSurface;
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800211 bool mClockEnabled;
Damien Bargiacchi97480862016-03-29 14:55:55 -0700212 bool mTimeIsAccurate;
Damien Bargiacchi9071db12016-10-28 17:38:22 -0700213 bool mTimeFormat12Hour;
Keun-young Parkb5938422017-03-23 13:46:24 -0700214 bool mShuttingDown;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700215 String8 mZipFileName;
216 SortedVector<String8> mLoadedFiles;
Geoffrey Pitscha9173532016-07-19 14:56:39 -0400217 sp<TimeCheckThread> mTimeCheckThread = nullptr;
Ed Coyne7464ac92017-06-08 12:26:48 -0700218 sp<Callbacks> mCallbacks;
Huihong Luo50a2f362018-08-11 09:27:57 -0700219 Animation* mAnimation = nullptr;
Marin Shalamanov1a6e36892020-05-18 19:05:17 +0200220 std::unique_ptr<DisplayEventReceiver> mDisplayEventReceiver;
221 sp<Looper> mLooper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800222};
223
224// ---------------------------------------------------------------------------
225
226}; // namespace android
227
228#endif // ANDROID_BOOTANIMATION_H