1 /*
2  *  Copyright (C) 2010-2018 Team Kodi
3  *  This file is part of Kodi - https://kodi.tv
4  *
5  *  SPDX-License-Identifier: GPL-2.0-or-later
6  *  See LICENSES/README.md for more information.
7  */
8 
9 #pragma once
10 
11 #include <vector>
12 
13 #include "system_gl.h"
14 
15 #include "BaseRenderer.h"
16 #include "cores/VideoPlayer/DVDCodecs/Video/DVDVideoCodec.h"
17 #include "cores/VideoSettings.h"
18 #include "FrameBufferObject.h"
19 #include "guilib/Shader.h"
20 #include "RenderFlags.h"
21 #include "RenderInfo.h"
22 #include "windowing/GraphicContext.h"
23 
24 extern "C" {
25 #include <libavutil/mastering_display_metadata.h>
26 }
27 
28 class CRenderCapture;
29 class CRenderSystemGLES;
30 
31 class CTexture;
32 namespace Shaders { class BaseYUV2RGBGLSLShader; }
33 namespace Shaders { class BaseVideoFilterShader; }
34 
35 struct DRAWRECT
36 {
37   float left;
38   float top;
39   float right;
40   float bottom;
41 };
42 
43 enum RenderMethod
44 {
45   RENDER_GLSL = 0x01,
46   RENDER_CUSTOM = 0x02,
47 };
48 
49 enum RenderQuality
50 {
51   RQ_LOW = 1,
52   RQ_SINGLEPASS,
53   RQ_MULTIPASS,
54   RQ_SOFTWARE
55 };
56 
57 class CEvent;
58 
59 class CLinuxRendererGLES : public CBaseRenderer
60 {
61 public:
62   CLinuxRendererGLES();
63   ~CLinuxRendererGLES() override;
64 
65   // Registration
66   static CBaseRenderer* Create(CVideoBuffer *buffer);
67   static bool Register();
68 
69   // Player functions
70   bool Configure(const VideoPicture& picture, float fps, unsigned int orientation) override;
IsConfigured()71   bool IsConfigured() override { return m_bConfigured; }
72   void AddVideoPicture(const VideoPicture& picture, int index) override;
73   void UnInit() override;
74   bool Flush(bool saveBuffers) override;
SetBufferSize(int numBuffers)75   void SetBufferSize(int numBuffers) override { m_NumYV12Buffers = numBuffers; }
76   bool IsGuiLayer() override;
77   void ReleaseBuffer(int idx) override;
78   void RenderUpdate(int index, int index2, bool clear, unsigned int flags, unsigned int alpha) override;
79   void Update() override;
80   bool RenderCapture(CRenderCapture* capture) override;
81   CRenderInfo GetRenderInfo() override;
82   bool ConfigChanged(const VideoPicture& picture) override;
83 
84   // Feature support
85   bool SupportsMultiPassRendering() override;
86   bool Supports(ERENDERFEATURE feature) override;
87   bool Supports(ESCALINGMETHOD method) override;
88 
89 protected:
90   static const int FIELD_FULL{0};
91   static const int FIELD_TOP{1};
92   static const int FIELD_BOT{2};
93 
94   virtual void Render(unsigned int flags, int index);
95   virtual void RenderUpdateVideo(bool clear, unsigned int flags = 0, unsigned int alpha = 255);
96 
97   int NextYV12Texture();
98   virtual bool ValidateRenderTarget();
99   virtual void LoadShaders(int field=FIELD_FULL);
100   virtual void ReleaseShaders();
101   void SetTextureFilter(GLenum method);
102   void UpdateVideoFilter();
103   AVColorPrimaries GetSrcPrimaries(AVColorPrimaries srcPrimaries, unsigned int width, unsigned int height);
104 
105   // textures
106   virtual bool UploadTexture(int index);
107   virtual void DeleteTexture(int index);
108   virtual bool CreateTexture(int index);
109 
110   bool UploadYV12Texture(int index);
111   void DeleteYV12Texture(int index);
112   bool CreateYV12Texture(int index);
SkipUploadYV12(int index)113   virtual bool SkipUploadYV12(int index) { return false; }
114 
115   bool UploadNV12Texture(int index);
116   void DeleteNV12Texture(int index);
117   bool CreateNV12Texture(int index);
118 
119   void CalculateTextureSourceRects(int source, int num_planes);
120 
121   // renderers
122   void RenderToFBO(int index, int field);
123   void RenderFromFBO();
124   void RenderSinglePass(int index, int field); // single pass glsl renderer
125 
126   // hooks for HwDec Renderered
LoadShadersHook()127   virtual bool LoadShadersHook() { return false; };
RenderHook(int idx)128   virtual bool RenderHook(int idx) { return false; };
AfterRenderHook(int idx)129   virtual void AfterRenderHook(int idx) {};
130 
131   struct
132   {
133     CFrameBufferObject fbo;
134     float width{0.0};
135     float height{0.0};
136   } m_fbo;
137 
138   int m_iYV12RenderBuffer{0};
139   int m_NumYV12Buffers{0};
140 
141   bool m_bConfigured{false};
142   bool m_bValidated{false};
143   GLenum m_textureTarget;
144   int m_renderMethod{RENDER_GLSL};
145   RenderQuality m_renderQuality{RQ_SINGLEPASS};
146 
147   // Raw data used by renderer
148   int m_currentField{FIELD_FULL};
149   int m_reloadShaders{0};
150   CRenderSystemGLES *m_renderSystem{nullptr};
151   GLenum m_pixelStoreKey{0};
152 
153   struct CYuvPlane
154   {
155     GLuint id{0};
156     CRect rect{0, 0, 0, 0};
157 
158     float width{0.0};
159     float height{0.0};
160 
161     unsigned texwidth{0};
162     unsigned texheight{0};
163 
164     //pixels per texel
165     unsigned pixpertex_x{0};
166     unsigned pixpertex_y{0};
167   };
168 
169   struct CPictureBuffer
170   {
171     CYuvPlane fields[MAX_FIELDS][YuvImage::MAX_PLANES];
172     YuvImage image;
173 
174     CVideoBuffer *videoBuffer{nullptr};
175     bool loaded{false};
176 
177     AVColorPrimaries m_srcPrimaries;
178     AVColorSpace m_srcColSpace;
179     int m_srcBits{8};
180     int m_srcTextureBits{8};
181     bool m_srcFullRange;
182 
183     bool hasDisplayMetadata{false};
184     AVMasteringDisplayMetadata displayMetadata;
185     bool hasLightMetadata{false};
186     AVContentLightMetadata lightMetadata;
187   };
188 
189   // YV12 decoder textures
190   // field index 0 is full image, 1 is odd scanlines, 2 is even scanlines
191   CPictureBuffer m_buffers[NUM_BUFFERS];
192 
193   void LoadPlane(CYuvPlane& plane, int type,
194                  unsigned width,  unsigned height,
195                  int stride, int bpp, void* data);
196 
197   Shaders::BaseYUV2RGBGLSLShader *m_pYUVProgShader{nullptr};
198   Shaders::BaseYUV2RGBGLSLShader *m_pYUVBobShader{nullptr};
199   Shaders::BaseVideoFilterShader *m_pVideoFilterShader{nullptr};
200   ESCALINGMETHOD m_scalingMethod{VS_SCALINGMETHOD_LINEAR};
201   ESCALINGMETHOD m_scalingMethodGui{VS_SCALINGMETHOD_MAX};
202   bool m_fullRange;
203   AVColorPrimaries m_srcPrimaries;
204   bool m_toneMap = false;
205   bool m_passthroughHDR = false;
206   unsigned char* m_planeBuffer = nullptr;
207   size_t m_planeBufferSize = 0;
208 
209   // clear colour for "black" bars
210   float m_clearColour{0.0f};
211   CRect m_viewRect;
212 
213 private:
214   void DrawBlackBars();
215 };
216