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