forked from xbmc/xbmc
/
OverlayRenderer.cpp
411 lines (352 loc) · 10.3 KB
/
OverlayRenderer.cpp
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
399
400
401
402
403
404
405
406
407
408
409
410
/*
* Initial code sponsored by: Voddler Inc (voddler.com)
* Copyright (C) 2005-2013 Team XBMC
* http://xbmc.org
*
* This Program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This Program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with XBMC; see the file COPYING. If not, see
* <http://www.gnu.org/licenses/>.
*
*/
#include "system.h"
#include "OverlayRenderer.h"
#include "cores/VideoPlayer/DVDCodecs/Overlay/DVDOverlay.h"
#include "cores/VideoPlayer/DVDCodecs/Overlay/DVDOverlayImage.h"
#include "cores/VideoPlayer/DVDCodecs/Overlay/DVDOverlaySpu.h"
#include "cores/VideoPlayer/DVDCodecs/Overlay/DVDOverlaySSA.h"
#include "cores/VideoPlayer/DVDCodecs/Overlay/DVDOverlayText.h"
#include "cores/VideoPlayer/VideoRenderers/RenderManager.h"
#include "guilib/GraphicContext.h"
#include "Application.h"
#include "settings/Settings.h"
#include "settings/AdvancedSettings.h"
#include "threads/SingleLock.h"
#include "utils/MathUtils.h"
#include "OverlayRendererUtil.h"
#include "OverlayRendererGUI.h"
#if defined(HAS_GL) || defined(HAS_GLES)
#include "OverlayRendererGL.h"
#elif defined(HAS_DX)
#include "OverlayRendererDX.h"
#endif
using namespace OVERLAY;
COverlay::COverlay()
{
m_x = 0.0f;
m_y = 0.0f;
m_width = 0.0;
m_height = 0.0;
m_type = TYPE_NONE;
m_align = ALIGN_SCREEN;
m_pos = POSITION_RELATIVE;
}
COverlay::~COverlay()
{
}
unsigned int CRenderer::m_textureid = 1;
CRenderer::CRenderer(CRenderManager *renderManager)
{
m_pRenderManager = renderManager;
}
CRenderer::~CRenderer()
{
for(int i = 0; i < NUM_BUFFERS; i++)
Release(m_buffers[i]);
}
void CRenderer::AddOverlay(CDVDOverlay* o, double pts, int index)
{
CSingleLock lock(m_section);
SElement e;
e.pts = pts;
e.overlay_dvd = o->Acquire();
m_buffers[index].push_back(e);
}
void CRenderer::Release(std::vector<SElement>& list)
{
std::vector<SElement> l = list;
list.clear();
for(std::vector<SElement>::iterator it = l.begin(); it != l.end(); ++it)
{
if (it->overlay_dvd)
it->overlay_dvd->Release();
}
}
void CRenderer::Flush()
{
CSingleLock lock(m_section);
for(int i = 0; i < NUM_BUFFERS; i++)
Release(m_buffers[i]);
ReleaseCache();
}
void CRenderer::Release(int idx)
{
CSingleLock lock(m_section);
Release(m_buffers[idx]);
}
void CRenderer::ReleaseCache()
{
for (auto& overlay : m_textureCache)
{
delete overlay.second;
}
m_textureCache.clear();
m_textureid++;
}
void CRenderer::ReleaseUnused()
{
for (auto it = m_textureCache.begin(); it != m_textureCache.end(); )
{
bool found = false;
for (auto& buffer : m_buffers)
{
for (auto& dvdoverlay : buffer)
{
if (dvdoverlay.overlay_dvd && dvdoverlay.overlay_dvd->m_textureid == it->first)
{
found = true;
break;
}
}
if (found)
break;
}
if (!found)
{
delete it->second;
it = m_textureCache.erase(it);
}
else
++it;
}
}
void CRenderer::Render(int idx)
{
CSingleLock lock(m_section);
std::vector<COverlay*> render;
std::vector<SElement>& list = m_buffers[idx];
for(std::vector<SElement>::iterator it = list.begin(); it != list.end(); ++it)
{
COverlay* o = NULL;
if(it->overlay_dvd)
o = Convert(it->overlay_dvd, it->pts);
if(!o)
continue;
render.push_back(o);
}
float total_height = 0.0f;
float cur_height = 0.0f;
int subalign = CSettings::GetInstance().GetInt(CSettings::SETTING_SUBTITLES_ALIGN);
for (std::vector<COverlay*>::iterator it = render.begin(); it != render.end(); ++it)
{
COverlay* o = *it;
o->PrepareRender();
total_height += o->m_height;
}
for (std::vector<COverlay*>::iterator it = render.begin(); it != render.end(); ++it)
{
COverlay* o = *it;
float adjust_height = 0.0f;
if (o->m_type == COverlay::TYPE_GUITEXT)
{
if(subalign == SUBTITLE_ALIGN_TOP_INSIDE ||
subalign == SUBTITLE_ALIGN_TOP_OUTSIDE)
{
adjust_height = cur_height;
cur_height += o->m_height;
}
else
{
total_height -= o->m_height;
adjust_height = -total_height;
}
}
Render(o, adjust_height);
}
ReleaseUnused();
}
void CRenderer::Render(COverlay* o, float adjust_height)
{
CRect rs, rd, rv;
m_pRenderManager->GetVideoRect(rs, rd, rv);
SRenderState state;
state.x = o->m_x;
state.y = o->m_y;
state.width = o->m_width;
state.height = o->m_height;
COverlay::EPosition pos = o->m_pos;
COverlay::EAlign align = o->m_align;
if(pos == COverlay::POSITION_RELATIVE)
{
float scale_x = 1.0;
float scale_y = 1.0;
if(align == COverlay::ALIGN_SCREEN
|| align == COverlay::ALIGN_SUBTITLE)
{
scale_x = (float)rv.Width();
scale_y = (float)rv.Height();
}
if(align == COverlay::ALIGN_VIDEO)
{
scale_x = rs.Width();
scale_y = rs.Height();
}
state.x *= scale_x;
state.y *= scale_y;
state.width *= scale_x;
state.height *= scale_y;
pos = COverlay::POSITION_ABSOLUTE;
}
if(pos == COverlay::POSITION_ABSOLUTE)
{
if(align == COverlay::ALIGN_SCREEN
|| align == COverlay::ALIGN_SUBTITLE)
{
if(align == COverlay::ALIGN_SUBTITLE)
{
RESOLUTION_INFO res = g_graphicsContext.GetResInfo(g_graphicsContext.GetVideoResolution());
state.x += rv.x1 + rv.Width() * 0.5f;
state.y += rv.y1 + (res.iSubtitles - res.Overscan.top);
}
else
{
state.x += rv.x1;
state.y += rv.y1;
}
}
if(align == COverlay::ALIGN_VIDEO)
{
float scale_x = rd.Width() / rs.Width();
float scale_y = rd.Height() / rs.Height();
state.x *= scale_x;
state.y *= scale_y;
state.width *= scale_x;
state.height *= scale_y;
state.x += rd.x1;
state.y += rd.y1;
}
}
state.x += GetStereoscopicDepth();
state.y += adjust_height;
o->Render(state);
}
bool CRenderer::HasOverlay(int idx)
{
bool hasOverlay = false;
CSingleLock lock(m_section);
std::vector<SElement>& list = m_buffers[idx];
for(std::vector<SElement>::iterator it = list.begin(); it != list.end(); ++it)
{
if (it->overlay_dvd)
{
hasOverlay = true;
break;
}
}
return hasOverlay;
}
COverlay* CRenderer::Convert(CDVDOverlaySSA* o, double pts)
{
// libass render in a target area which named as frame. the frame size may bigger than video size,
// and including margins between video to frame edge. libass allow to render subtitles into the margins.
// this has been used to show subtitles in the top or bottom "black bar" between video to frame border.
CRect src, dst, target;
m_pRenderManager->GetVideoRect(src, dst, target);
int videoWidth = MathUtils::round_int(dst.Width());
int videoHeight = MathUtils::round_int(dst.Height());
int targetWidth = MathUtils::round_int(target.Width());
int targetHeight = MathUtils::round_int(target.Height());
int useMargin;
int subalign = CSettings::GetInstance().GetInt(CSettings::SETTING_SUBTITLES_ALIGN);
if(subalign == SUBTITLE_ALIGN_BOTTOM_OUTSIDE
|| subalign == SUBTITLE_ALIGN_TOP_OUTSIDE
||(subalign == SUBTITLE_ALIGN_MANUAL && g_advancedSettings.m_videoAssFixedWorks))
useMargin = 1;
else
useMargin = 0;
double position;
// position used to call ass_set_line_position, it's vertical line position of subtitles in percent.
// value is 0-100: 0 = on the bottom (default), 100 = on top.
if(subalign == SUBTITLE_ALIGN_TOP_INSIDE
|| subalign == SUBTITLE_ALIGN_TOP_OUTSIDE)
position = 100.0;
else if (subalign == SUBTITLE_ALIGN_MANUAL && g_advancedSettings.m_videoAssFixedWorks)
{
RESOLUTION_INFO res;
res = g_graphicsContext.GetResInfo(g_graphicsContext.GetVideoResolution());
position = 100.0 - (res.iSubtitles - res.Overscan.top) * 100 / res.iHeight;
}
else
position = 0.0;
int changes = 0;
ASS_Image* images = o->m_libass->RenderImage(targetWidth, targetHeight, videoWidth, videoHeight, pts, useMargin, position, &changes);
if(o->m_textureid)
{
if(changes == 0)
{
std::map<unsigned int, COverlay*>::iterator it = m_textureCache.find(o->m_textureid);
if (it != m_textureCache.end())
return it->second;
}
}
COverlay *overlay = NULL;
#if defined(HAS_GL) || defined(HAS_GLES)
overlay = new COverlayGlyphGL(images, targetWidth, targetHeight);
#elif defined(HAS_DX)
overlay = new COverlayQuadsDX(images, targetWidth, targetHeight);
#endif
// scale to video dimensions
if (overlay)
{
overlay->m_width = (float)targetWidth / videoWidth;
overlay->m_height = (float)targetHeight / videoHeight;
overlay->m_x = ((float)videoWidth - targetWidth) / 2 / videoWidth;
overlay->m_y = ((float)videoHeight - targetHeight) / 2 / videoHeight;
}
m_textureCache[m_textureid] = overlay;
o->m_textureid = m_textureid;
m_textureid++;
return overlay;
}
COverlay* CRenderer::Convert(CDVDOverlay* o, double pts)
{
COverlay* r = NULL;
if(o->IsOverlayType(DVDOVERLAY_TYPE_SSA))
r = Convert((CDVDOverlaySSA*)o, pts);
else if(o->m_textureid)
{
std::map<unsigned int, COverlay*>::iterator it = m_textureCache.find(o->m_textureid);
if (it != m_textureCache.end())
r = it->second;
}
if (r)
{
return r;
}
#if defined(HAS_GL) || defined(HAS_GLES)
if (o->IsOverlayType(DVDOVERLAY_TYPE_IMAGE))
r = new COverlayTextureGL((CDVDOverlayImage*)o);
else if(o->IsOverlayType(DVDOVERLAY_TYPE_SPU))
r = new COverlayTextureGL((CDVDOverlaySpu*)o);
#elif defined(HAS_DX)
if (o->IsOverlayType(DVDOVERLAY_TYPE_IMAGE))
r = new COverlayImageDX((CDVDOverlayImage*)o);
else if(o->IsOverlayType(DVDOVERLAY_TYPE_SPU))
r = new COverlayImageDX((CDVDOverlaySpu*)o);
#endif
if(!r && o->IsOverlayType(DVDOVERLAY_TYPE_TEXT))
r = new COverlayText((CDVDOverlayText*)o);
m_textureCache[m_textureid] = r;
o->m_textureid = m_textureid;
m_textureid++;
return r;
}