[release] version bump to 13.0 beta1
[vuplus_xbmc] / xbmc / guilib / Texture.h
1 /*
2  *      Copyright (C) 2005-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 #pragma once
22
23 #include "system.h"
24 #include "gui3d.h"
25 #include "utils/StdString.h"
26 #include "XBTF.h"
27 #include "guilib/imagefactory.h"
28
29 #pragma pack(1)
30 struct COLOR {unsigned char b,g,r,x;};  // Windows GDI expects 4bytes per color
31 #pragma pack()
32
33 class CTexture;
34 class CGLTexture;
35 class CPiTexture;
36 class CDXTexture;
37
38 /*!
39 \ingroup textures
40 \brief Base texture class, subclasses of which depend on the render spec (DX, GL etc.)
41 */
42 class CBaseTexture
43 {
44
45 public:
46   CBaseTexture(unsigned int width = 0, unsigned int height = 0, unsigned int format = XB_FMT_A8R8G8B8);
47
48   virtual ~CBaseTexture();
49
50   /*! \brief Load a texture from a file
51    Loads a texture from a file, restricting in size if needed based on maxHeight and maxWidth.
52    Note that these are the ideal size to load at - the returned texture may be smaller or larger than these.
53    \param texturePath the path of the texture to load.
54    \param idealWidth the ideal width of the texture (defaults to 0, no ideal width).
55    \param idealHeight the ideal height of the texture (defaults to 0, no ideal height).
56    \param autoRotate whether the textures should be autorotated based on EXIF information (defaults to false).
57    \return a CBaseTexture pointer to the created texture - NULL if the texture failed to load.
58    */
59   static CBaseTexture *LoadFromFile(const CStdString& texturePath, unsigned int idealWidth = 0, unsigned int idealHeight = 0,
60                                     bool autoRotate = false, bool requirePixels = false);
61
62   /*! \brief Load a texture from a file in memory
63    Loads a texture from a file in memory, restricting in size if needed based on maxHeight and maxWidth.
64    Note that these are the ideal size to load at - the returned texture may be smaller or larger than these.
65    \param buffer the memory buffer holding the file.
66    \param bufferSize the size of buffer.
67    \param mimeType the mime type of the file in buffer.
68    \param idealWidth the ideal width of the texture (defaults to 0, no ideal width).
69    \param idealHeight the ideal height of the texture (defaults to 0, no ideal height).
70    \return a CBaseTexture pointer to the created texture - NULL if the texture failed to load.
71    */
72   static CBaseTexture *LoadFromFileInMemory(unsigned char* buffer, size_t bufferSize, const std::string& mimeType,
73                                             unsigned int idealWidth = 0, unsigned int idealHeight = 0);
74
75   bool LoadFromMemory(unsigned int width, unsigned int height, unsigned int pitch, unsigned int format, bool hasAlpha, unsigned char* pixels);
76   bool LoadPaletted(unsigned int width, unsigned int height, unsigned int pitch, unsigned int format, const unsigned char *pixels, const COLOR *palette);
77
78   bool HasAlpha() const;
79
80   virtual void CreateTextureObject() = 0;
81   virtual void DestroyTextureObject() = 0;
82   virtual void LoadToGPU() = 0;
83   virtual void BindToUnit(unsigned int unit) = 0;
84
85   unsigned char* GetPixels() const { return m_pixels; }
86   unsigned int GetPitch() const { return GetPitch(m_textureWidth); }
87   unsigned int GetRows() const { return GetRows(m_textureHeight); }
88   unsigned int GetTextureWidth() const { return m_textureWidth; }
89   unsigned int GetTextureHeight() const { return m_textureHeight; }
90   unsigned int GetWidth() const { return m_imageWidth; }
91   unsigned int GetHeight() const { return m_imageHeight; }
92   /*! \brief return the original width of the image, before scaling/cropping */
93   unsigned int GetOriginalWidth() const { return m_originalWidth; }
94   /*! \brief return the original height of the image, before scaling/cropping */
95   unsigned int GetOriginalHeight() const { return m_originalHeight; }
96
97   int GetOrientation() const { return m_orientation; }
98   void SetOrientation(int orientation) { m_orientation = orientation; }
99
100   void Update(unsigned int width, unsigned int height, unsigned int pitch, unsigned int format, const unsigned char *pixels, bool loadToGPU);
101   void Allocate(unsigned int width, unsigned int height, unsigned int format);
102   void ClampToEdge();
103
104   static unsigned int PadPow2(unsigned int x);
105   static bool SwapBlueRed(unsigned char *pixels, unsigned int height, unsigned int pitch, unsigned int elements = 4, unsigned int offset=0);
106
107 private:
108   // no copy constructor
109   CBaseTexture(const CBaseTexture &copy);
110
111 protected:
112   bool LoadFromFileInMem(unsigned char* buffer, size_t size, const std::string& mimeType,
113                          unsigned int maxWidth, unsigned int maxHeight);
114   bool LoadFromFileInternal(const CStdString& texturePath, unsigned int maxWidth, unsigned int maxHeight, bool autoRotate, bool requirePixels);
115   bool LoadIImage(IImage* pImage, unsigned char* buffer, unsigned int bufSize, unsigned int width, unsigned int height, bool autoRotate=false);
116   // helpers for computation of texture parameters for compressed textures
117   unsigned int GetPitch(unsigned int width) const;
118   unsigned int GetRows(unsigned int height) const;
119   unsigned int GetBlockSize() const;
120
121   unsigned int m_imageWidth;
122   unsigned int m_imageHeight;
123   unsigned int m_textureWidth;
124   unsigned int m_textureHeight;
125   unsigned int m_originalWidth;   ///< original image width before scaling or cropping
126   unsigned int m_originalHeight;  ///< original image height before scaling or cropping
127
128   unsigned char* m_pixels;
129   bool m_loadedToGPU;
130   unsigned int m_format;
131   int m_orientation;
132   bool m_hasAlpha;
133 };
134
135 #if defined(HAS_OMXPLAYER)
136 #include "TexturePi.h"
137 #define CTexture CPiTexture
138 #elif defined(HAS_GL) || defined(HAS_GLES)
139 #include "TextureGL.h"
140 #define CTexture CGLTexture
141 #elif defined(HAS_DX)
142 #include "TextureDX.h"
143 #define CTexture CDXTexture
144 #endif