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