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 
23 #include <stdint.h>
24 #include <sys/types.h>
25 
26 #include <androidfw/AssetManager.h>
27 #include <gui/DisplayEventReceiver.h>
28 #include <utils/Looper.h>
29 #include <utils/Thread.h>
30 #include <binder/IBinder.h>
31 
32 #include <EGL/egl.h>
33 #include <GLES/gl.h>
34 
35 class SkBitmap;
36 
37 namespace android {
38 
39 class Surface;
40 class SurfaceComposerClient;
41 class SurfaceControl;
42 
43 // ---------------------------------------------------------------------------
44 
45 class BootAnimation : public Thread, public IBinder::DeathRecipient
46 {
47 public:
48     struct Texture {
49         GLint   w;
50         GLint   h;
51         GLuint  name;
52     };
53 
54     struct Font {
55         FileMap* map;
56         Texture texture;
57         int char_width;
58         int char_height;
59     };
60 
61     struct Animation {
62         struct Frame {
63             String8 name;
64             FileMap* map;
65             int trimX;
66             int trimY;
67             int trimWidth;
68             int trimHeight;
69             mutable GLuint tid;
70             bool operator < (const Frame& rhs) const {
71                 return name < rhs.name;
72             }
73         };
74         struct Part {
75             int count;  // The number of times this part should repeat, 0 for infinite
76             int pause;  // The number of frames to pause for at the end of this part
77             int clockPosX;  // The x position of the clock, in pixels. Positive values offset from
78                             // the left of the screen, negative values offset from the right.
79             int clockPosY;  // The y position of the clock, in pixels. Positive values offset from
80                             // the bottom of the screen, negative values offset from the top.
81                             // If either of the above are INT_MIN the clock is disabled, if INT_MAX
82                             // the clock is centred on that axis.
83             String8 path;
84             String8 trimData;
85             SortedVector<Frame> frames;
86             bool playUntilComplete;
87             float backgroundColor[3];
88             uint8_t* audioData;
89             int audioLength;
90             Animation* animation;
91         };
92         int fps;
93         int width;
94         int height;
95         Vector<Part> parts;
96         String8 audioConf;
97         String8 fileName;
98         ZipFileRO* zip;
99         Font clockFont;
100     };
101 
102     // All callbacks will be called from this class's internal thread.
103     class Callbacks : public RefBase {
104     public:
105         // Will be called during initialization after we have loaded
106         // the animation and be provided with all parts in animation.
init(const Vector<Animation::Part> &)107         virtual void init(const Vector<Animation::Part>& /*parts*/) {}
108 
109         // Will be called while animation is playing before each part is
110         // played. It will be provided with the part and play count for it.
111         // It will be provided with the partNumber for the part about to be played,
112         // as well as a reference to the part itself. It will also be provided with
113         // which play of that part is about to start, some parts are repeated
114         // multiple times.
playPart(int,const Animation::Part &,int)115         virtual void playPart(int /*partNumber*/, const Animation::Part& /*part*/,
116                               int /*playNumber*/) {}
117 
118         // Will be called when animation is done and thread is shutting down.
shutdown()119         virtual void shutdown() {}
120     };
121 
122     explicit BootAnimation(sp<Callbacks> callbacks);
123     virtual ~BootAnimation();
124 
125     sp<SurfaceComposerClient> session() const;
126 
127 private:
128     virtual bool        threadLoop();
129     virtual status_t    readyToRun();
130     virtual void        onFirstRef();
131     virtual void        binderDied(const wp<IBinder>& who);
132 
133     bool                updateIsTimeAccurate();
134 
135     class TimeCheckThread : public Thread {
136     public:
137         explicit TimeCheckThread(BootAnimation* bootAnimation);
138         virtual ~TimeCheckThread();
139     private:
140         virtual status_t    readyToRun();
141         virtual bool        threadLoop();
142         bool                doThreadLoop();
143         void                addTimeDirWatch();
144 
145         int mInotifyFd;
146         int mSystemWd;
147         int mTimeWd;
148         BootAnimation* mBootAnimation;
149     };
150 
151     // Display event handling
152     class DisplayEventCallback;
153     int displayEventCallback(int fd, int events, void* data);
154     void processDisplayEvents();
155 
156     status_t initTexture(Texture* texture, AssetManager& asset, const char* name);
157     status_t initTexture(FileMap* map, int* width, int* height);
158     status_t initFont(Font* font, const char* fallback);
159     bool android();
160     bool movie();
161     void drawText(const char* str, const Font& font, bool bold, int* x, int* y);
162     void drawClock(const Font& font, const int xPos, const int yPos);
163     bool validClock(const Animation::Part& part);
164     Animation* loadAnimation(const String8&);
165     bool playAnimation(const Animation&);
166     void releaseAnimation(Animation*) const;
167     bool parseAnimationDesc(Animation&);
168     bool preloadZip(Animation &animation);
169     void findBootAnimationFile();
170     bool findBootAnimationFileInternal(const std::vector<std::string>& files);
171     bool preloadAnimation();
172     EGLConfig getEglConfig(const EGLDisplay&);
173     ui::Size limitSurfaceSize(int width, int height) const;
174     void resizeSurface(int newWidth, int newHeight);
175 
176     void checkExit();
177 
178     void handleViewport(nsecs_t timestep);
179 
180     sp<SurfaceComposerClient>       mSession;
181     AssetManager mAssets;
182     Texture     mAndroid[2];
183     int         mWidth;
184     int         mHeight;
185     int         mMaxWidth = 0;
186     int         mMaxHeight = 0;
187     int         mCurrentInset;
188     int         mTargetInset;
189     bool        mUseNpotTextures = false;
190     EGLDisplay  mDisplay;
191     EGLDisplay  mContext;
192     EGLDisplay  mSurface;
193     sp<IBinder> mDisplayToken;
194     sp<SurfaceControl> mFlingerSurfaceControl;
195     sp<Surface> mFlingerSurface;
196     bool        mClockEnabled;
197     bool        mTimeIsAccurate;
198     bool        mTimeFormat12Hour;
199     bool        mShuttingDown;
200     String8     mZipFileName;
201     SortedVector<String8> mLoadedFiles;
202     sp<TimeCheckThread> mTimeCheckThread = nullptr;
203     sp<Callbacks> mCallbacks;
204     Animation* mAnimation = nullptr;
205     std::unique_ptr<DisplayEventReceiver> mDisplayEventReceiver;
206     sp<Looper> mLooper;
207 };
208 
209 // ---------------------------------------------------------------------------
210 
211 }; // namespace android
212 
213 #endif // ANDROID_BOOTANIMATION_H
214