1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
|
/*
* Copyright 2011 The Android Open Source Project
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "VideoLayerManager.h"
#include "RenderSkinMediaButton.h"
#include "SkCanvas.h"
#include <cutils/log.h>
#include <wtf/CurrentTime.h>
#include <wtf/text/CString.h>
#if USE(ACCELERATED_COMPOSITING)
#undef XLOGC
#define XLOGC(...) android_printLog(ANDROID_LOG_DEBUG, "VideoLayerManager", __VA_ARGS__)
#ifdef DEBUG
#undef XLOG
#define XLOG(...) android_printLog(ANDROID_LOG_DEBUG, "VideoLayerManager", __VA_ARGS__)
#else
#undef XLOG
#define XLOG(...)
#endif // DEBUG
// The animation of the play/pause icon will last for PLAY_PAUSE_ICON_SHOW_TIME
// seconds.
#define PLAY_PAUSE_ICON_SHOW_TIME 1
// Define the max sum of all the video's sizes.
// Note that video_size = width * height. If there is no compression, then the
// maximum memory consumption could be 4 * video_size.
// Setting this to 2M, means that maximum memory consumption of all the
// screenshots would not be above 8M.
#define MAX_VIDEOSIZE_SUM 2097152
// We don't preload the video data, so we don't have the exact size yet.
// Assuming 16:9 by default, this will be corrected after video prepared.
#define DEFAULT_VIDEO_ASPECT_RATIO 1.78
#define VIDEO_TEXTURE_NUMBER 5
#define VIDEO_BUTTON_SIZE 64
namespace WebCore {
VideoLayerManager::VideoLayerManager()
: m_currentTimeStamp(0)
, m_createdTexture(false)
, m_posterTextureId(0)
, m_spinnerOuterTextureId(0)
, m_spinnerInnerTextureId(0)
, m_playTextureId(0)
, m_pauseTextureId(0)
, m_buttonRect(0, 0, VIDEO_BUTTON_SIZE, VIDEO_BUTTON_SIZE)
{
}
int VideoLayerManager::getButtonSize()
{
return VIDEO_BUTTON_SIZE;
}
GLuint VideoLayerManager::createTextureFromImage(int buttonType)
{
SkRect rect = SkRect(m_buttonRect);
SkBitmap bitmap;
bitmap.setConfig(SkBitmap::kARGB_8888_Config, rect.width(), rect.height());
bitmap.allocPixels();
bitmap.eraseColor(0);
SkCanvas canvas(bitmap);
canvas.drawARGB(0, 0, 0, 0, SkXfermode::kClear_Mode);
RenderSkinMediaButton::Draw(&canvas, m_buttonRect, buttonType, true, 0,
false);
GLuint texture;
glGenTextures(1, &texture);
GLUtils::createTextureWithBitmap(texture, bitmap);
bitmap.reset();
return texture;
}
// Should be called at the VideoLayerAndroid::drawGL to make sure we allocate
// the GL resources lazily.
void VideoLayerManager::initGLResourcesIfNeeded()
{
if (!m_createdTexture) {
XLOGC("Reinit GLResource for VideoLayer");
initGLResources();
}
}
void VideoLayerManager::initGLResources()
{
GLUtils::checkGlError("before initGLResources()");
if (!m_createdTexture) {
m_spinnerOuterTextureId =
createTextureFromImage(RenderSkinMediaButton::SPINNER_OUTER);
m_spinnerInnerTextureId =
createTextureFromImage(RenderSkinMediaButton::SPINNER_INNER);
m_posterTextureId =
createTextureFromImage(RenderSkinMediaButton::VIDEO);
m_playTextureId = createTextureFromImage(RenderSkinMediaButton::PLAY);
m_pauseTextureId = createTextureFromImage(RenderSkinMediaButton::PAUSE);
}
m_createdTexture = !GLUtils::checkGlError("initGLResources()");
return;
}
void VideoLayerManager::cleanupGLResources()
{
if (m_createdTexture) {
GLuint videoTextures[VIDEO_TEXTURE_NUMBER] = { m_spinnerOuterTextureId,
m_spinnerInnerTextureId, m_posterTextureId, m_playTextureId,
m_pauseTextureId };
glDeleteTextures(VIDEO_TEXTURE_NUMBER, videoTextures);
m_createdTexture = false;
}
// Delete the texture in retired mode, but have not hit draw call to be
// removed.
deleteUnusedTextures();
// Go over the registered GL textures (screen shot textures) and delete them.
android::Mutex::Autolock lock(m_videoLayerInfoMapLock);
InfoIterator end = m_videoLayerInfoMap.end();
for (InfoIterator it = m_videoLayerInfoMap.begin(); it != end; ++it) {
// The map include every video has been played, so their textureId can
// be deleted already, like hitting onTrimMemory multiple times.
if (it->second->textureId) {
XLOG("delete texture from the map %d", it->second->textureId);
glDeleteTextures(1, &it->second->textureId);
// Set the textureID to 0 to show the video icon.
it->second->textureId = 0;
}
}
GLUtils::checkGlError("cleanupGLResources()");
return;
}
// Getting TextureId for GL draw call, in the UI thread.
GLuint VideoLayerManager::getTextureId(const int layerId)
{
android::Mutex::Autolock lock(m_videoLayerInfoMapLock);
GLuint result = 0;
if (m_videoLayerInfoMap.contains(layerId))
result = m_videoLayerInfoMap.get(layerId)->textureId;
return result;
}
// Getting the aspect ratio for GL draw call, in the UI thread.
float VideoLayerManager::getAspectRatio(const int layerId)
{
android::Mutex::Autolock lock(m_videoLayerInfoMapLock);
float result = 0;
if (m_videoLayerInfoMap.contains(layerId))
result = m_videoLayerInfoMap.get(layerId)->aspectRatio;
return result;
}
// Getting matrix for GL draw call, in the UI thread.
GLfloat* VideoLayerManager::getMatrix(const int layerId)
{
android::Mutex::Autolock lock(m_videoLayerInfoMapLock);
GLfloat* result = 0;
if (m_videoLayerInfoMap.contains(layerId))
result = m_videoLayerInfoMap.get(layerId)->surfaceMatrix;
return result;
}
int VideoLayerManager::getTotalMemUsage()
{
int sum = 0;
InfoIterator end = m_videoLayerInfoMap.end();
for (InfoIterator it = m_videoLayerInfoMap.begin(); it != end; ++it)
sum += it->second->videoSize;
return sum;
}
// When the video start, we know its texture info, so we register when we
// recieve the setSurfaceTexture call, this happens on UI thread.
void VideoLayerManager::registerTexture(const int layerId, const GLuint textureId)
{
android::Mutex::Autolock lock(m_videoLayerInfoMapLock);
// If the texture has been registered, then early return.
if (m_videoLayerInfoMap.get(layerId)) {
GLuint oldTextureId = m_videoLayerInfoMap.get(layerId)->textureId;
if (oldTextureId != textureId)
removeLayerInternal(layerId);
else
return;
}
// The old info is deleted and now complete the new info and store it.
VideoLayerInfo* pInfo = new VideoLayerInfo();
pInfo->textureId = textureId;
memset(pInfo->surfaceMatrix, 0, sizeof(pInfo->surfaceMatrix));
pInfo->videoSize = 0;
pInfo->aspectRatio = DEFAULT_VIDEO_ASPECT_RATIO;
m_currentTimeStamp++;
pInfo->timeStamp = m_currentTimeStamp;
pInfo->lastIconShownTime = 0;
pInfo->iconState = Registered;
m_videoLayerInfoMap.add(layerId, pInfo);
XLOG("GL texture %d regisered for layerId %d", textureId, layerId);
return;
}
// Only when the video is prepared, we got the video size. So we should update
// the size for the video accordingly.
// This is called from webcore thread, from MediaPlayerPrivateAndroid.
void VideoLayerManager::updateVideoLayerSize(const int layerId, const int size,
const float ratio)
{
android::Mutex::Autolock lock(m_videoLayerInfoMapLock);
if (m_videoLayerInfoMap.contains(layerId)) {
VideoLayerInfo* pInfo = m_videoLayerInfoMap.get(layerId);
if (pInfo) {
pInfo->videoSize = size;
pInfo->aspectRatio = ratio;
}
}
// If the memory usage is out of bound, then just delete the oldest ones.
// Because we only recycle the texture before the current timestamp, the
// current video's texture will not be deleted.
while (getTotalMemUsage() > MAX_VIDEOSIZE_SUM)
if (!recycleTextureMem())
break;
return;
}
// This is called only from UI thread, at drawGL time.
void VideoLayerManager::updateMatrix(const int layerId, const GLfloat* matrix)
{
android::Mutex::Autolock lock(m_videoLayerInfoMapLock);
if (m_videoLayerInfoMap.contains(layerId)) {
// If the existing video layer's matrix is matching the incoming one,
// then skip the update.
VideoLayerInfo* pInfo = m_videoLayerInfoMap.get(layerId);
ASSERT(matrix);
if (pInfo && !memcmp(matrix, pInfo->surfaceMatrix, sizeof(pInfo->surfaceMatrix)))
return;
memcpy(pInfo->surfaceMatrix, matrix, sizeof(pInfo->surfaceMatrix));
} else {
XLOG("Error: should not reach here, the layerId %d should exist!", layerId);
ASSERT(false);
}
return;
}
// This is called on the webcore thread, save the GL texture for recycle in
// the retired queue. They will be deleted in deleteUnusedTextures() in the UI
// thread.
// Return true when we found one texture to retire.
bool VideoLayerManager::recycleTextureMem()
{
// Find the oldest texture int the m_videoLayerInfoMap, put it in m_retiredTextures
int oldestTimeStamp = m_currentTimeStamp;
int oldestLayerId = -1;
InfoIterator end = m_videoLayerInfoMap.end();
#ifdef DEBUG
XLOG("VideoLayerManager::recycleTextureMem m_videoLayerInfoMap contains");
for (InfoIterator it = m_videoLayerInfoMap.begin(); it != end; ++it)
XLOG(" layerId %d, textureId %d, videoSize %d, timeStamp %d ",
it->first, it->second->textureId, it->second->videoSize, it->second->timeStamp);
#endif
for (InfoIterator it = m_videoLayerInfoMap.begin(); it != end; ++it) {
if (it->second->timeStamp < oldestTimeStamp) {
oldestTimeStamp = it->second->timeStamp;
oldestLayerId = it->first;
}
}
bool foundTextureToRetire = (oldestLayerId != -1);
if (foundTextureToRetire)
removeLayerInternal(oldestLayerId);
return foundTextureToRetire;
}
// This is only called in the UI thread, b/c glDeleteTextures need to be called
// on the right context.
void VideoLayerManager::deleteUnusedTextures()
{
m_retiredTexturesLock.lock();
int size = m_retiredTextures.size();
if (size > 0) {
GLuint* textureNames = new GLuint[size];
int index = 0;
Vector<GLuint>::const_iterator end = m_retiredTextures.end();
for (Vector<GLuint>::const_iterator it = m_retiredTextures.begin();
it != end; ++it) {
GLuint textureName = *it;
if (textureName) {
textureNames[index] = textureName;
index++;
XLOG("GL texture %d will be deleted", textureName);
}
}
glDeleteTextures(size, textureNames);
delete textureNames;
m_retiredTextures.clear();
}
m_retiredTexturesLock.unlock();
GLUtils::checkGlError("deleteUnusedTextures");
return;
}
// This can be called in the webcore thread in the media player's dtor.
void VideoLayerManager::removeLayer(const int layerId)
{
android::Mutex::Autolock lock(m_videoLayerInfoMapLock);
removeLayerInternal(layerId);
}
// This can be called on both UI and webcore thread. Since this is a private
// function, it is up to the public function to handle the lock for
// m_videoLayerInfoMap.
void VideoLayerManager::removeLayerInternal(const int layerId)
{
// Delete the layerInfo corresponding to this layerId and remove from the map.
if (m_videoLayerInfoMap.contains(layerId)) {
GLuint textureId = m_videoLayerInfoMap.get(layerId)->textureId;
if (textureId) {
// Buffer up the retired textures in either UI or webcore thread,
// will be purged at deleteUnusedTextures in the UI thread.
m_retiredTexturesLock.lock();
m_retiredTextures.append(textureId);
m_retiredTexturesLock.unlock();
}
delete m_videoLayerInfoMap.get(layerId);
m_videoLayerInfoMap.remove(layerId);
}
return;
}
double VideoLayerManager::drawIcon(const int layerId, IconType type)
{
// When ratio 0 is returned, the Icon should not be drawn.
double ratio = 0;
android::Mutex::Autolock lock(m_videoLayerInfoMapLock);
if (m_videoLayerInfoMap.contains(layerId)) {
VideoLayerInfo* pInfo = m_videoLayerInfoMap.get(layerId);
// If this is state switching moment, reset the time and state
if ((type == PlayIcon && pInfo->iconState != PlayIconShown)
|| (type == PauseIcon && pInfo->iconState != PauseIconShown)) {
pInfo->lastIconShownTime = WTF::currentTime();
pInfo->iconState = (type == PlayIcon) ? PlayIconShown : PauseIconShown;
}
// After switching the state, we calculate the ratio depending on the
// time interval.
if ((type == PlayIcon && pInfo->iconState == PlayIconShown)
|| (type == PauseIcon && pInfo->iconState == PauseIconShown)) {
double delta = WTF::currentTime() - pInfo->lastIconShownTime;
ratio = 1.0 - (delta / PLAY_PAUSE_ICON_SHOW_TIME);
}
}
if (ratio > 1 || ratio < 0)
ratio = 0;
return ratio;
}
}
#endif // USE(ACCELERATED_COMPOSITING)
|