1 /******************************************************************************
2     Copyright (C) 2014 by Hugh Bailey <obs.jim@gmail.com>
3 
4     This program is free software: you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation, either version 2 of the License, or
7     (at your option) any later version.
8 
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13 
14     You should have received a copy of the GNU General Public License
15     along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 ******************************************************************************/
17 
18 #pragma once
19 
20 #include <graphics/vec4.h>
21 #include <graphics/matrix4.h>
22 
GetScaleAndCenterPos(int baseCX,int baseCY,int windowCX,int windowCY,int & x,int & y,float & scale)23 static inline void GetScaleAndCenterPos(int baseCX, int baseCY, int windowCX,
24 					int windowCY, int &x, int &y,
25 					float &scale)
26 {
27 	double windowAspect, baseAspect;
28 	int newCX, newCY;
29 
30 	windowAspect = double(windowCX) / double(windowCY);
31 	baseAspect = double(baseCX) / double(baseCY);
32 
33 	if (windowAspect > baseAspect) {
34 		scale = float(windowCY) / float(baseCY);
35 		newCX = int(double(windowCY) * baseAspect);
36 		newCY = windowCY;
37 	} else {
38 		scale = float(windowCX) / float(baseCX);
39 		newCX = windowCX;
40 		newCY = int(float(windowCX) / baseAspect);
41 	}
42 
43 	x = windowCX / 2 - newCX / 2;
44 	y = windowCY / 2 - newCY / 2;
45 }
46 
GetCenterPosFromFixedScale(int baseCX,int baseCY,int windowCX,int windowCY,int & x,int & y,float scale)47 static inline void GetCenterPosFromFixedScale(int baseCX, int baseCY,
48 					      int windowCX, int windowCY,
49 					      int &x, int &y, float scale)
50 {
51 	x = (float(windowCX) - float(baseCX) * scale) / 2.0f;
52 	y = (float(windowCY) - float(baseCY) * scale) / 2.0f;
53 }
54 
GetPixelSize(QWidget * widget)55 static inline QSize GetPixelSize(QWidget *widget)
56 {
57 	return widget->size() * widget->devicePixelRatioF();
58 }
59 
60 #define OUTLINE_COLOR 0xFFD0D0D0
61 #define LINE_LENGTH 0.1f
62 
63 // Rec. ITU-R BT.1848-1 / EBU R 95
64 #define ACTION_SAFE_PERCENT 0.035f       // 3.5%
65 #define GRAPHICS_SAFE_PERCENT 0.05f      // 5.0%
66 #define FOURBYTHREE_SAFE_PERCENT 0.1625f // 16.25%
67 
InitSafeAreas(gs_vertbuffer_t ** actionSafeMargin,gs_vertbuffer_t ** graphicsSafeMargin,gs_vertbuffer_t ** fourByThreeSafeMargin,gs_vertbuffer_t ** leftLine,gs_vertbuffer_t ** topLine,gs_vertbuffer_t ** rightLine)68 static inline void InitSafeAreas(gs_vertbuffer_t **actionSafeMargin,
69 				 gs_vertbuffer_t **graphicsSafeMargin,
70 				 gs_vertbuffer_t **fourByThreeSafeMargin,
71 				 gs_vertbuffer_t **leftLine,
72 				 gs_vertbuffer_t **topLine,
73 				 gs_vertbuffer_t **rightLine)
74 {
75 	obs_enter_graphics();
76 
77 	// All essential action should be placed inside this area
78 	gs_render_start(true);
79 	gs_vertex2f(ACTION_SAFE_PERCENT, ACTION_SAFE_PERCENT);
80 	gs_vertex2f(ACTION_SAFE_PERCENT, 1 - ACTION_SAFE_PERCENT);
81 	gs_vertex2f(1 - ACTION_SAFE_PERCENT, 1 - ACTION_SAFE_PERCENT);
82 	gs_vertex2f(1 - ACTION_SAFE_PERCENT, ACTION_SAFE_PERCENT);
83 	gs_vertex2f(ACTION_SAFE_PERCENT, ACTION_SAFE_PERCENT);
84 	*actionSafeMargin = gs_render_save();
85 
86 	// All graphics should be placed inside this area
87 	gs_render_start(true);
88 	gs_vertex2f(GRAPHICS_SAFE_PERCENT, GRAPHICS_SAFE_PERCENT);
89 	gs_vertex2f(GRAPHICS_SAFE_PERCENT, 1 - GRAPHICS_SAFE_PERCENT);
90 	gs_vertex2f(1 - GRAPHICS_SAFE_PERCENT, 1 - GRAPHICS_SAFE_PERCENT);
91 	gs_vertex2f(1 - GRAPHICS_SAFE_PERCENT, GRAPHICS_SAFE_PERCENT);
92 	gs_vertex2f(GRAPHICS_SAFE_PERCENT, GRAPHICS_SAFE_PERCENT);
93 	*graphicsSafeMargin = gs_render_save();
94 
95 	// 4:3 safe area for widescreen
96 	gs_render_start(true);
97 	gs_vertex2f(FOURBYTHREE_SAFE_PERCENT, GRAPHICS_SAFE_PERCENT);
98 	gs_vertex2f(1 - FOURBYTHREE_SAFE_PERCENT, GRAPHICS_SAFE_PERCENT);
99 	gs_vertex2f(1 - FOURBYTHREE_SAFE_PERCENT, 1 - GRAPHICS_SAFE_PERCENT);
100 	gs_vertex2f(FOURBYTHREE_SAFE_PERCENT, 1 - GRAPHICS_SAFE_PERCENT);
101 	gs_vertex2f(FOURBYTHREE_SAFE_PERCENT, GRAPHICS_SAFE_PERCENT);
102 	*fourByThreeSafeMargin = gs_render_save();
103 
104 	gs_render_start(true);
105 	gs_vertex2f(0.0f, 0.5f);
106 	gs_vertex2f(LINE_LENGTH, 0.5f);
107 	*leftLine = gs_render_save();
108 
109 	gs_render_start(true);
110 	gs_vertex2f(0.5f, 0.0f);
111 	gs_vertex2f(0.5f, LINE_LENGTH);
112 	*topLine = gs_render_save();
113 
114 	gs_render_start(true);
115 	gs_vertex2f(1.0f, 0.5f);
116 	gs_vertex2f(1 - LINE_LENGTH, 0.5f);
117 	*rightLine = gs_render_save();
118 
119 	obs_leave_graphics();
120 }
121 
RenderSafeAreas(gs_vertbuffer_t * vb,int cx,int cy)122 static inline void RenderSafeAreas(gs_vertbuffer_t *vb, int cx, int cy)
123 {
124 	if (!vb)
125 		return;
126 
127 	matrix4 transform;
128 	matrix4_identity(&transform);
129 	transform.x.x = cx;
130 	transform.y.y = cy;
131 
132 	gs_load_vertexbuffer(vb);
133 
134 	gs_matrix_push();
135 	gs_matrix_mul(&transform);
136 
137 	gs_effect_t *solid = obs_get_base_effect(OBS_EFFECT_SOLID);
138 	gs_eparam_t *color = gs_effect_get_param_by_name(solid, "color");
139 
140 	gs_effect_set_color(color, OUTLINE_COLOR);
141 	while (gs_effect_loop(solid, "Solid"))
142 		gs_draw(GS_LINESTRIP, 0, 0);
143 
144 	gs_matrix_pop();
145 }
146