Home | History | Annotate | Download | only in android
      1 /*
      2  * Copyright 2010, The Android Open Source Project
      3  *
      4  * Redistribution and use in source and binary forms, with or without
      5  * modification, are permitted provided that the following conditions
      6  * are met:
      7  *  * Redistributions of source code must retain the above copyright
      8  *    notice, this list of conditions and the following disclaimer.
      9  *  * Redistributions in binary form must reproduce the above copyright
     10  *    notice, this list of conditions and the following disclaimer in the
     11  *    documentation and/or other materials provided with the distribution.
     12  *
     13  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
     14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
     17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
     21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     24  */
     25 
     26 #ifndef TilesManager_h
     27 #define TilesManager_h
     28 
     29 #if USE(ACCELERATED_COMPOSITING)
     30 
     31 #include "BaseTile.h"
     32 #include "BaseTileTexture.h"
     33 #include "ImageTexture.h"
     34 #include "LayerAndroid.h"
     35 #include "ShaderProgram.h"
     36 #include "SkBitmapRef.h"
     37 #include "TexturesGenerator.h"
     38 #include "TiledPage.h"
     39 #include "TilesProfiler.h"
     40 #include "TilesTracker.h"
     41 #include "TransferQueue.h"
     42 #include "VideoLayerManager.h"
     43 #include <utils/threads.h>
     44 #include <wtf/HashMap.h>
     45 
     46 namespace WebCore {
     47 
     48 class PaintedSurface;
     49 
     50 class TilesManager {
     51 public:
     52     static TilesManager* instance();
     53     static GLint getMaxTextureSize();
     54     static int getMaxTextureAllocation();
     55 
     56     static bool hardwareAccelerationEnabled()
     57     {
     58         return gInstance != 0;
     59     }
     60 
     61     void removeOperationsForFilter(OperationFilter* filter, bool waitForRunning = false)
     62     {
     63         m_pixmapsGenerationThread->removeOperationsForFilter(filter, waitForRunning);
     64     }
     65 
     66     void removeOperationsForPage(TiledPage* page)
     67     {
     68         m_pixmapsGenerationThread->removeOperationsForPage(page);
     69     }
     70 
     71     void removePaintOperationsForPage(TiledPage* page, bool waitForCompletion)
     72     {
     73         m_pixmapsGenerationThread->removePaintOperationsForPage(page, waitForCompletion);
     74     }
     75 
     76     void scheduleOperation(QueuedOperation* operation)
     77     {
     78         m_pixmapsGenerationThread->scheduleOperation(operation);
     79     }
     80 
     81     void swapLayersTextures(LayerAndroid* newTree, LayerAndroid* oldTree);
     82     void addPaintedSurface(PaintedSurface* surface);
     83 
     84     ShaderProgram* shader() { return &m_shader; }
     85     TransferQueue* transferQueue() { return &m_queue; }
     86     VideoLayerManager* videoLayerManager() { return &m_videoLayerManager; }
     87 
     88     void gatherLayerTextures();
     89     void gatherTextures();
     90     bool layerTexturesRemain() { return m_layerTexturesRemain; }
     91     void gatherTexturesNumbers(int* nbTextures, int* nbAllocatedTextures,
     92                                int* nbLayerTextures, int* nbAllocatedLayerTextures);
     93 
     94     BaseTileTexture* getAvailableTexture(BaseTile* owner);
     95 
     96     void markGeneratorAsReady()
     97     {
     98         {
     99             android::Mutex::Autolock lock(m_generatorLock);
    100             m_generatorReady = true;
    101         }
    102         m_generatorReadyCond.signal();
    103     }
    104 
    105     void printTextures();
    106 
    107     void resetTextureUsage(TiledPage* page);
    108 
    109     int maxTextureCount();
    110     int maxLayerTextureCount();
    111     void setMaxTextureCount(int max);
    112     void setMaxLayerTextureCount(int max);
    113     static float tileWidth();
    114     static float tileHeight();
    115     static float layerTileWidth();
    116     static float layerTileHeight();
    117     void paintedSurfacesCleanup(GLWebViewState* state = 0);
    118     void unregisterGLWebViewState(GLWebViewState* state);
    119 
    120     void allocateTiles();
    121 
    122     // Called when webview is hidden to discard graphics memory
    123     void deallocateTextures(bool allTextures);
    124 
    125     bool getShowVisualIndicator()
    126     {
    127         return m_showVisualIndicator;
    128     }
    129 
    130     void setShowVisualIndicator(bool showVisualIndicator)
    131     {
    132         m_showVisualIndicator = showVisualIndicator;
    133     }
    134 
    135     SharedTextureMode getSharedTextureMode()
    136     {
    137         return SurfaceTextureMode;
    138     }
    139 
    140     TilesProfiler* getProfiler()
    141     {
    142         return &m_profiler;
    143     }
    144 
    145     TilesTracker* getTilesTracker()
    146     {
    147         return &m_tilesTracker;
    148     }
    149 
    150     bool invertedScreen()
    151     {
    152         return m_invertedScreen;
    153     }
    154 
    155     bool invertedScreenSwitch()
    156     {
    157         return m_invertedScreenSwitch;
    158     }
    159 
    160     void setInvertedScreen(bool invert)
    161     {
    162         if (m_invertedScreen != invert)
    163             m_invertedScreenSwitch = true;
    164         m_invertedScreen = invert;
    165     }
    166 
    167     void setInvertedScreenSwitch(bool invertedSwitch)
    168     {
    169         m_invertedScreenSwitch = invertedSwitch;
    170     }
    171 
    172     void setInvertedScreenContrast(float contrast)
    173     {
    174         m_shader.setContrast(contrast);
    175     }
    176 
    177     void setUseMinimalMemory(bool useMinimalMemory)
    178     {
    179         m_useMinimalMemory = useMinimalMemory;
    180     }
    181 
    182     bool useMinimalMemory()
    183     {
    184         return m_useMinimalMemory;
    185     }
    186 
    187     void incDrawGLCount()
    188     {
    189         m_drawGLCount++;
    190     }
    191 
    192     unsigned long long getDrawGLCount()
    193     {
    194         return m_drawGLCount;
    195     }
    196 
    197     int getPaintedSurfaceCount()
    198     {
    199         return m_paintedSurfaces.size();
    200     }
    201 
    202 private:
    203     TilesManager();
    204 
    205     void waitForGenerator()
    206     {
    207         android::Mutex::Autolock lock(m_generatorLock);
    208         while (!m_generatorReady)
    209             m_generatorReadyCond.wait(m_generatorLock);
    210     }
    211 
    212     void deallocateTexturesVector(unsigned long long sparedDrawCount,
    213                                   WTF::Vector<BaseTileTexture*>& textures);
    214 
    215     Vector<BaseTileTexture*> m_textures;
    216     Vector<BaseTileTexture*> m_availableTextures;
    217 
    218     Vector<BaseTileTexture*> m_tilesTextures;
    219     Vector<BaseTileTexture*> m_availableTilesTextures;
    220     bool m_layerTexturesRemain;
    221 
    222     Vector<PaintedSurface*> m_paintedSurfaces;
    223 
    224     int m_maxTextureCount;
    225     int m_maxLayerTextureCount;
    226 
    227     bool m_generatorReady;
    228 
    229     bool m_showVisualIndicator;
    230     bool m_invertedScreen;
    231     bool m_invertedScreenSwitch;
    232 
    233     bool m_useMinimalMemory;
    234 
    235     sp<TexturesGenerator> m_pixmapsGenerationThread;
    236 
    237     android::Mutex m_texturesLock;
    238     android::Mutex m_generatorLock;
    239     android::Condition m_generatorReadyCond;
    240 
    241     static TilesManager* gInstance;
    242 
    243     ShaderProgram m_shader;
    244     TransferQueue m_queue;
    245 
    246     VideoLayerManager m_videoLayerManager;
    247 
    248     TilesProfiler m_profiler;
    249     TilesTracker m_tilesTracker;
    250     unsigned long long m_drawGLCount;
    251     double m_lastTimeLayersUsed;
    252     bool m_hasLayerTextures;
    253 };
    254 
    255 } // namespace WebCore
    256 
    257 #endif // USE(ACCELERATED_COMPOSITING)
    258 #endif // TilesManager_h
    259