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