1/*
2 * Copyright 2011 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 VideoLayerManager_h
27#define VideoLayerManager_h
28
29#include "GLUtils.h"
30#include "IntRect.h"
31#include "RenderSkinMediaButton.h"
32#include <wtf/HashMap.h>
33#include <wtf/Vector.h>
34#include <utils/threads.h>
35
36#if USE(ACCELERATED_COMPOSITING)
37
38namespace WebCore {
39
40enum IconState {
41    Registered,
42    PlayIconShown,
43    PauseIconShown
44};
45
46enum IconType {
47    PlayIcon,
48    PauseIcon
49};
50
51// Every video layer can use its uniqueId to query VideoLayerManager about such
52// info globally.
53struct VideoLayerInfo {
54    GLuint textureId; // GL texture bound with the surface texture.
55    int videoSize; // The size of the video.
56    float aspectRatio; // The aspect ratio of the video.
57    int timeStamp; // Used to decide which VideoLayerInfo is the oldest one.
58    GLfloat surfaceMatrix[16];
59
60    double lastIconShownTime;
61    IconState iconState;
62};
63
64
65class VideoLayerManager {
66
67public:
68    typedef HashMap<int, VideoLayerInfo*>::const_iterator InfoIterator;
69
70    VideoLayerManager();
71
72    // Register the texture when we got setSurfaceTexture call.
73    void registerTexture(const int layerId, const GLuint textureId);
74    // Update the size when the video is prepared.
75    void updateVideoLayerSize(const int layerId, const int size, const float ratio);
76    // At draw time, update the matrix for every video frame update.
77    void updateMatrix(const int layerId, const GLfloat* matrix);
78    // Remove the layer info from the mapping.
79    void removeLayer(const int layerId);
80
81    // Return the texture name corresponding to the layerId
82    GLuint getTextureId(const int layerId);
83    // Return the matrix for surface texture corresponding to the layerId
84    GLfloat* getMatrix(const int layerId);
85    // Return the aspect ratio for the video corresponding to the layerId
86    float getAspectRatio(const int layerId);
87
88    // Delete the GL textures
89    void deleteUnusedTextures();
90
91    double drawIcon(const int layerId, IconType type);
92
93    GLuint getSpinnerInnerTextureId() { return m_spinnerInnerTextureId; }
94    GLuint getSpinnerOuterTextureId() { return m_spinnerOuterTextureId; }
95    GLuint getPosterTextureId() { return m_posterTextureId; }
96    GLuint getPlayTextureId() { return m_playTextureId; }
97    GLuint getPauseTextureId() { return m_pauseTextureId; }
98
99    void initGLResourcesIfNeeded();
100    void cleanupGLResources();
101    void forceNeedsInit() { m_createdTexture = false; }
102
103    static int getButtonSize();
104private:
105    // Get the sum of all the video size stored in m_videoLayerInfoMap.
106    int getTotalMemUsage();
107    // If the memory consumption is out of bound, recycle some textures.
108    bool recycleTextureMem();
109    // The private function to remove layer.
110    void removeLayerInternal(const int layerId);
111    void initGLResources();
112    // Indexed by each layer's uniqueId, this map contains the important info
113    // used for showing the video when playing or the screenshot when paused.
114    HashMap<int, VideoLayerInfo*> m_videoLayerInfoMap;
115    android::Mutex m_videoLayerInfoMapLock;
116
117    // Everytime we add one item to the map, we update the timestamp.
118    int m_currentTimeStamp;
119
120    // The retiredTextures is used to communicate between UI thread and webcore
121    // thread. Basically, GL textures are determined to retire in the webcore
122    // thread, and really get deleted in the UI thread.
123    Vector<GLuint> m_retiredTextures;
124    android::Mutex m_retiredTexturesLock;
125
126    GLuint createTextureFromImage(RenderSkinMediaButton::MediaButton buttonType);
127
128    // Texture for showing the static image will be created at native side.
129    bool m_createdTexture;
130    GLuint m_posterTextureId;
131    GLuint m_spinnerOuterTextureId;
132    GLuint m_spinnerInnerTextureId;
133    GLuint m_playTextureId;
134    GLuint m_pauseTextureId;
135
136    IntRect m_buttonRect;
137};
138
139} // namespace WebCore
140
141#endif // USE(ACCELERATED_COMPOSITING)
142       //
143#endif // VideoLayerManager_h
144