Merge pull request #4687 from ruuk/textboxgettext
[vuplus_xbmc] / xbmc / guilib / TexturePi.cpp
1 /*
2  *      Copyright (C) 2013 Team XBMC
3  *      http://xbmc.org
4  *
5  *  This Program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2, or (at your option)
8  *  any later version.
9  *
10  *  This Program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with XBMC; see the file COPYING.  If not, see
17  *  <http://www.gnu.org/licenses/>.
18  *
19  */
20
21 #include "system.h"
22 #include "Texture.h"
23 #include "windowing/WindowingFactory.h"
24 #include "utils/log.h"
25 #include "utils/GLUtils.h"
26 #include "guilib/TextureManager.h"
27 #include "utils/URIUtils.h"
28
29 #if defined(HAS_OMXPLAYER)
30 #include "cores/omxplayer/OMXImage.h"
31
32 using namespace std;
33
34 /************************************************************************/
35 /*    CPiTexture                                                       */
36 /************************************************************************/
37
38 CPiTexture::CPiTexture(unsigned int width, unsigned int height, unsigned int format)
39 : CGLTexture(width, height, format)
40 {
41   m_egl_image = NULL;
42 }
43
44 CPiTexture::~CPiTexture()
45 {
46   if (m_egl_image)
47   {
48     g_OMXImage.DestroyTexture(m_egl_image);
49     m_egl_image = NULL;
50   }
51 }
52
53 void CPiTexture::Allocate(unsigned int width, unsigned int height, unsigned int format)
54 {
55   if (m_egl_image)
56   {
57     m_imageWidth = m_originalWidth = width;
58     m_imageHeight = m_originalHeight = height;
59     m_format = format;
60     m_orientation = 0;
61
62     m_textureWidth = m_imageWidth;
63     m_textureHeight = m_imageHeight;
64     return;
65   }
66   return CGLTexture::Allocate(width, height, format);
67 }
68
69 void CPiTexture::CreateTextureObject()
70 {
71   if (m_egl_image && !m_texture)
72   {
73     g_OMXImage.GetTexture(m_egl_image, &m_texture);
74     return;
75   }
76   CGLTexture::CreateTextureObject();
77 }
78
79 void CPiTexture::LoadToGPU()
80 {
81   if (m_egl_image)
82   {
83     if (m_loadedToGPU)
84     {
85       // nothing to load - probably same image (no change)
86       return;
87     }
88     if (m_texture == 0)
89     {
90       // Have OpenGL generate a texture object handle for us
91       // this happens only one time - the first time the texture is loaded
92       CreateTextureObject();
93     }
94
95     // Bind the texture object
96     glBindTexture(GL_TEXTURE_2D, m_texture);
97
98     m_loadedToGPU = true;
99     return;
100   }
101   CGLTexture::LoadToGPU();
102 }
103
104 void CPiTexture::Update(unsigned int width, unsigned int height, unsigned int pitch, unsigned int format, const unsigned char *pixels, bool loadToGPU)
105 {
106   if (m_egl_image)
107   {
108     if (loadToGPU)
109       LoadToGPU();
110     return;
111   }
112   CGLTexture::Update(width, height, pitch, format, pixels, loadToGPU);
113 }
114
115 bool CPiTexture::LoadFromFileInternal(const CStdString& texturePath, unsigned int maxWidth, unsigned int maxHeight, bool autoRotate, bool requirePixels, const std::string& strMimeType)
116 {
117   if (URIUtils::HasExtension(texturePath, ".jpg|.tbn"))
118   {
119     COMXImageFile *file = g_OMXImage.LoadJpeg(texturePath);
120     if (file)
121     {
122       bool okay = false;
123       int orientation = file->GetOrientation();
124       // limit the sizes of jpegs (even if we fail to decode)
125       g_OMXImage.ClampLimits(maxWidth, maxHeight, file->GetWidth(), file->GetHeight(), orientation & 4);
126
127       if (requirePixels)
128       {
129         Allocate(maxWidth, maxHeight, XB_FMT_A8R8G8B8);
130         if (m_pixels && COMXImage::DecodeJpeg(file, maxWidth, GetRows(), GetPitch(), (void *)m_pixels))
131           okay = true;
132       }
133       else
134       {
135         if (g_OMXImage.DecodeJpegToTexture(file, maxWidth, maxHeight, &m_egl_image) && m_egl_image)
136         {
137           Allocate(maxWidth, maxHeight, XB_FMT_A8R8G8B8);
138           okay = true;
139         }
140       }
141       g_OMXImage.CloseJpeg(file);
142       if (okay)
143       {
144         m_hasAlpha = false;
145         if (autoRotate && orientation)
146           m_orientation = orientation - 1;
147         return true;
148       }
149     }
150   }
151   return CGLTexture::LoadFromFileInternal(texturePath, maxWidth, maxHeight, autoRotate, requirePixels);
152 }
153
154 #endif