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 "LayerAndroid.h"
32#include "ShaderProgram.h"
33#include "TexturesGenerator.h"
34#include "TilesProfiler.h"
35#include "VideoLayerManager.h"
36#include <utils/threads.h>
37#include <wtf/HashMap.h>
38
39namespace WebCore {
40
41class OperationFilter;
42class Tile;
43class TileTexture;
44class TransferQueue;
45
46class TilesManager {
47public:
48    // May only be called from the UI thread
49    static TilesManager* instance();
50
51    static bool hardwareAccelerationEnabled()
52    {
53        return gInstance != 0;
54    }
55
56    ShaderProgram* shader() { return &m_shader; }
57    TransferQueue* transferQueue();
58
59    VideoLayerManager* videoLayerManager() { return &m_videoLayerManager; }
60
61    void updateTilesIfContextVerified();
62    void cleanupGLResources();
63
64    void gatherTextures();
65    bool layerTexturesRemain() { return m_layerTexturesRemain; }
66    void gatherTexturesNumbers(int* nbTextures, int* nbAllocatedTextures,
67                               int* nbLayerTextures, int* nbAllocatedLayerTextures);
68
69    TileTexture* getAvailableTexture(Tile* owner);
70
71    void dirtyAllTiles();
72
73    void printTextures();
74
75    // m_highEndGfx is written/read only on UI thread, no need for a lock.
76    void setHighEndGfx(bool highEnd);
77    bool highEndGfx();
78
79    int currentTextureCount();
80    int currentLayerTextureCount();
81    void setCurrentTextureCount(int newTextureCount);
82    void setCurrentLayerTextureCount(int newTextureCount);
83    static int tileWidth();
84    static int tileHeight();
85
86    void allocateTextures();
87
88    // remove all tiles from textures (and optionally deallocate gl memory)
89    void discardTextures(bool allTextures, bool glTextures);
90
91    bool getShowVisualIndicator()
92    {
93        return m_showVisualIndicator;
94    }
95
96    void setShowVisualIndicator(bool showVisualIndicator)
97    {
98        m_showVisualIndicator = showVisualIndicator;
99    }
100
101    TilesProfiler* getProfiler()
102    {
103        return &m_profiler;
104    }
105
106    bool invertedScreen()
107    {
108        return m_invertedScreen;
109    }
110
111    void setInvertedScreen(bool invert)
112    {
113        m_invertedScreen = invert;
114    }
115
116    void setInvertedScreenContrast(float contrast)
117    {
118        m_shader.setContrast(contrast);
119    }
120
121    void setUseMinimalMemory(bool useMinimalMemory)
122    {
123        m_useMinimalMemory = useMinimalMemory;
124    }
125
126    bool useMinimalMemory()
127    {
128        return m_useMinimalMemory;
129    }
130
131    void setUseDoubleBuffering(bool useDoubleBuffering)
132    {
133        m_useDoubleBuffering = useDoubleBuffering;
134    }
135    bool useDoubleBuffering() { return m_useDoubleBuffering; }
136
137
138    unsigned int incWebkitContentUpdates() { return m_webkitContentUpdates++; }
139
140    void incContentUpdates() { m_contentUpdates++; }
141    unsigned int getContentUpdates() { return m_contentUpdates; }
142    void clearContentUpdates() { m_contentUpdates = 0; }
143
144    void incDrawGLCount()
145    {
146        m_drawGLCount++;
147    }
148
149    unsigned long long getDrawGLCount()
150    {
151        return m_drawGLCount;
152    }
153
154    // operations on/for texture generator threads
155    void removeOperationsForFilter(OperationFilter* filter);
156    bool tryUpdateOperationWithPainter(Tile* tile, TilePainter* painter);
157    void scheduleOperation(QueuedOperation* operation);
158
159private:
160    TilesManager();
161    ~TilesManager();
162    int m_scheduleThread;
163
164    void discardTexturesVector(unsigned long long sparedDrawCount,
165                               WTF::Vector<TileTexture*>& textures,
166                               bool deallocateGLTextures);
167    void dirtyTexturesVector(WTF::Vector<TileTexture*>& textures);
168    void markAllGLTexturesZero();
169    int getMaxTextureAllocation();
170
171    WTF::Vector<TileTexture*> m_textures;
172    WTF::Vector<TileTexture*> m_availableTextures;
173
174    WTF::Vector<TileTexture*> m_tilesTextures;
175    WTF::Vector<TileTexture*> m_availableTilesTextures;
176    bool m_layerTexturesRemain;
177
178    bool m_highEndGfx;
179    int m_currentTextureCount;
180    int m_currentLayerTextureCount;
181    int m_maxTextureAllocation;
182
183    bool m_generatorReady;
184
185    bool m_showVisualIndicator;
186    bool m_invertedScreen;
187
188    bool m_useMinimalMemory;
189
190    bool m_useDoubleBuffering;
191    unsigned int m_contentUpdates; // nr of successful tiled paints
192    unsigned int m_webkitContentUpdates; // nr of paints from webkit
193
194    sp<TexturesGenerator>* m_textureGenerators;
195
196    android::Mutex m_texturesLock;
197
198    static TilesManager* gInstance;
199
200    ShaderProgram m_shader;
201    TransferQueue* m_queue;
202
203    VideoLayerManager m_videoLayerManager;
204
205    TilesProfiler m_profiler;
206    unsigned long long m_drawGLCount;
207    double m_lastTimeLayersUsed;
208    bool m_hasLayerTextures;
209
210    EGLContext m_eglContext;
211};
212
213} // namespace WebCore
214
215#endif // USE(ACCELERATED_COMPOSITING)
216#endif // TilesManager_h
217