xref: /aosp_15_r20/frameworks/base/cmds/bootanimation/BootAnimation.h (revision d57664e9bc4670b3ecf6748a746a57c557b6bc9e)
1 /*
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 
20 #include <vector>
21 #include <queue>
22 #include <climits>
23 
24 #include <stdint.h>
25 #include <sys/types.h>
26 
27 #include <androidfw/AssetManager.h>
28 #include <gui/DisplayEventReceiver.h>
29 #include <utils/Looper.h>
30 #include <utils/Thread.h>
31 #include <binder/IBinder.h>
32 
33 #include <ui/Rotation.h>
34 #include <ui/LayerStack.h>
35 
36 #include <EGL/egl.h>
37 #include <GLES2/gl2.h>
38 
39 namespace android {
40 
41 class Surface;
42 class SurfaceComposerClient;
43 class SurfaceControl;
44 
45 // ---------------------------------------------------------------------------
46 
47 class BootAnimation : public Thread, public IBinder::DeathRecipient
48 {
49 public:
50     static constexpr int MAX_FADED_FRAMES_COUNT = std::numeric_limits<int>::max();
51 
52     struct Texture {
53         GLint   w;
54         GLint   h;
55         GLuint  name;
56     };
57 
58     struct Font {
59         FileMap* map = nullptr;
60         Texture texture;
61         int char_width;
62         int char_height;
63     };
64 
65     struct Animation {
66         struct Frame {
67             String8 name;
68             FileMap* map = nullptr;
69             int trimX;
70             int trimY;
71             int trimWidth;
72             int trimHeight;
73             mutable GLuint tid;
74             bool operator < (const Frame& rhs) const {
75                 return name < rhs.name;
76             }
77         };
78         struct Part {
79             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
81             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.
87             String8 path;
88             String8 trimData;
89             SortedVector<Frame> frames;
90             bool playUntilComplete;
91             int framesToFadeCount;
92             float backgroundColor[3];
93             uint8_t* audioData;
94             int audioLength;
95             Animation* animation;
96             // 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;
100 
hasFadingPhaseAnimation::Part101             bool hasFadingPhase() const {
102                 return !playUntilComplete && framesToFadeCount > 0;
103             }
104         };
105         int fps;
106         int width;
107         int height;
108         bool progressEnabled;
109         Vector<Part> parts;
110         String8 audioConf;
111         String8 fileName;
112         ZipFileRO* zip;
113         Font clockFont;
114         Font progressFont;
115          // Controls if dynamic coloring is enabled for the whole animation.
116         bool dynamicColoringEnabled = false;
117         int colorTransitionStart = 0; // Start frame of dynamic color transition.
118         int colorTransitionEnd = 0; // End frame of dynamic color transition.
119         float startColors[4][3]; // Start colors of dynamic color transition.
120         float endColors[4][3];   // End colors of dynamic color transition.
121     };
122 
123     // 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 
135     // 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.
init(const Vector<Animation::Part> &)140         virtual void init(const Vector<Animation::Part>& /*parts*/) {}
141 
142         // 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.
playPart(int,const Animation::Part &,int)148         virtual void playPart(int /*partNumber*/, const Animation::Part& /*part*/,
149                               int /*playNumber*/) {}
150 
151         // Will be called when animation is done and thread is shutting down.
shutdown()152         virtual void shutdown() {}
153     };
154 
155     explicit BootAnimation(sp<Callbacks> callbacks);
156     virtual ~BootAnimation();
157 
158     sp<SurfaceComposerClient> session() const;
159 
160 private:
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:
170         explicit TimeCheckThread(BootAnimation* bootAnimation);
171         virtual ~TimeCheckThread();
172     private:
173         virtual status_t    readyToRun();
174         virtual bool        threadLoop();
175         bool                doThreadLoop();
176         void                addTimeDirWatch();
177 
178         int mInotifyFd;
179         int mBootAnimWd;
180         int mTimeWd;
181         BootAnimation* mBootAnimation;
182     };
183 
184     // 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 
191     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);
195     status_t initFont(Font* font, const char* fallback);
196     void initShaders();
197     bool android(const Display& display);
198     status_t initDisplaysAndSurfaces();
199     bool movie();
200     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);
205     void fadeFrame(int frameLeft, int frameBottom, int frameWidth, int frameHeight,
206                    const Animation::Part& part, int fadedFramesCount);
207     void drawTexturedQuad(float xStart, float yStart,
208                           float width, float height, const Display& display);
209     bool validClock(const Animation::Part& part);
210     Animation* loadAnimation(const String8&);
211     bool playAnimation(const Animation&);
212     void releaseAnimation(Animation*) const;
213     bool parseAnimationDesc(Animation&);
214     bool preloadZip(Animation &animation);
215     void findBootAnimationFile();
216     bool findBootAnimationFileInternal(const std::vector<std::string>& files);
217     bool preloadAnimation();
218     EGLConfig getEglConfig(const EGLDisplay&);
219     ui::Size limitSurfaceSize(int width, int height) const;
220     void resizeSurface(int newWidth, int newHeight, Display& display);
221     void projectSceneToWindow(const Display& display);
222     void rotateAwayFromNaturalOrientationIfNeeded(Display& display);
223     ui::Rotation parseOrientationProperty();
224     void configureDisplayAndLayerStack(const Display& display, ui::LayerStack layerStack);
225 
226     bool shouldStopPlayingPart(const Animation::Part& part, int fadedFramesCount,
227                                int lastDisplayedProgress);
228     void checkExit();
229 
230     void handleViewport(nsecs_t timestep, const Display& display);
231     void initDynamicColors();
232 
233     sp<SurfaceComposerClient>       mSession;
234     AssetManager mAssets;
235     Texture     mAndroid[2];
236     int         mMaxWidth = 0;
237     int         mMaxHeight = 0;
238     int         mCurrentInset;
239     int         mTargetInset;
240     bool        mUseNpotTextures = false;
241     EGLDisplay  mEgl;
242     EGLDisplay  mEglContext;
243     // Per-Display Attributes (to support multi-display)
244     std::vector<Display> mDisplays;
245     bool        mClockEnabled;
246     bool        mTimeIsAccurate;
247     bool        mTimeFormat12Hour;
248     bool        mShuttingDown;
249     bool        mDynamicColorsApplied = false;
250     String8     mZipFileName;
251     SortedVector<String8> mLoadedFiles;
252     sp<TimeCheckThread> mTimeCheckThread = nullptr;
253     sp<Callbacks> mCallbacks;
254     Animation* mAnimation = nullptr;
255     GLuint mImageShader;
256     GLuint mTextShader;
257     GLuint mImageFadeLocation;
258     GLuint mImageTextureLocation;
259     GLuint mTextCropAreaLocation;
260     GLuint mTextTextureLocation;
261     GLuint mImageColorProgressLocation;
262 };
263 
264 // ---------------------------------------------------------------------------
265 
266 }; // namespace android
267 
268 #endif // ANDROID_BOOTANIMATION_H
269