[Windows] Fix driver version detection of AMD RDNA+ GPU on Windows 10
[xbmc.git] / xbmc / guilib / DDSImage.cpp
blob76b61e5f08e2ebc4ef3c87c645433da9caa5f0d1
1 /*
2 * Copyright (C) 2005-2018 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
7 */
9 #include "DDSImage.h"
11 #include "XBTF.h"
12 #include "filesystem/File.h"
13 #include "utils/log.h"
15 #include <algorithm>
16 #include <string.h>
17 using namespace XFILE;
19 CDDSImage::CDDSImage()
21 m_data = NULL;
22 memset(&m_desc, 0, sizeof(m_desc));
25 CDDSImage::CDDSImage(unsigned int width, unsigned int height, XB_FMT format)
27 m_data = NULL;
28 Allocate(width, height, format);
31 CDDSImage::~CDDSImage()
33 delete[] m_data;
36 unsigned int CDDSImage::GetWidth() const
38 return m_desc.width;
41 unsigned int CDDSImage::GetHeight() const
43 return m_desc.height;
46 XB_FMT CDDSImage::GetFormat() const
48 if (m_desc.pixelFormat.flags & DDPF_RGB)
49 return XB_FMT_UNKNOWN; // Not supported
50 if (m_desc.pixelFormat.flags & DDPF_FOURCC)
52 if (strncmp((const char *)&m_desc.pixelFormat.fourcc, "DXT1", 4) == 0)
53 return XB_FMT_DXT1;
54 if (strncmp((const char *)&m_desc.pixelFormat.fourcc, "DXT3", 4) == 0)
55 return XB_FMT_DXT3;
56 if (strncmp((const char *)&m_desc.pixelFormat.fourcc, "DXT5", 4) == 0)
57 return XB_FMT_DXT5;
58 if (strncmp((const char *)&m_desc.pixelFormat.fourcc, "ARGB", 4) == 0)
59 return XB_FMT_A8R8G8B8;
61 return XB_FMT_UNKNOWN;
64 unsigned int CDDSImage::GetSize() const
66 return m_desc.linearSize;
69 unsigned char *CDDSImage::GetData() const
71 return m_data;
74 bool CDDSImage::ReadFile(const std::string &inputFile)
76 // open the file
77 CFile file;
78 if (!file.Open(inputFile))
79 return false;
81 // read the header
82 uint32_t magic;
83 if (file.Read(&magic, 4) != 4)
84 return false;
85 if (file.Read(&m_desc, sizeof(m_desc)) != sizeof(m_desc))
86 return false;
87 if (!GetFormat())
88 return false; // not supported
90 // allocate our data
91 m_data = new unsigned char[m_desc.linearSize];
92 if (!m_data)
93 return false;
95 // and read it in
96 if (file.Read(m_data, m_desc.linearSize) != static_cast<ssize_t>(m_desc.linearSize))
97 return false;
99 file.Close();
100 return true;
103 unsigned int CDDSImage::GetStorageRequirements(unsigned int width,
104 unsigned int height,
105 XB_FMT format)
107 switch (format)
109 case XB_FMT_DXT1:
110 return ((width + 3) / 4) * ((height + 3) / 4) * 8;
111 case XB_FMT_DXT3:
112 case XB_FMT_DXT5:
113 return ((width + 3) / 4) * ((height + 3) / 4) * 16;
114 case XB_FMT_A8R8G8B8:
115 default:
116 return width * height * 4;
120 void CDDSImage::Allocate(unsigned int width, unsigned int height, XB_FMT format)
122 memset(&m_desc, 0, sizeof(m_desc));
123 m_desc.size = sizeof(m_desc);
124 m_desc.flags = ddsd_caps | ddsd_pixelformat | ddsd_width | ddsd_height | ddsd_linearsize;
125 m_desc.height = height;
126 m_desc.width = width;
127 m_desc.linearSize = GetStorageRequirements(width, height, format);
128 m_desc.pixelFormat.size = sizeof(m_desc.pixelFormat);
129 m_desc.pixelFormat.flags = ddpf_fourcc;
130 memcpy(&m_desc.pixelFormat.fourcc, GetFourCC(format), 4);
131 m_desc.caps.flags1 = ddscaps_texture;
132 delete[] m_data;
133 m_data = new unsigned char[m_desc.linearSize];
136 const char* CDDSImage::GetFourCC(XB_FMT format)
138 switch (format)
140 case XB_FMT_DXT1:
141 return "DXT1";
142 case XB_FMT_DXT3:
143 return "DXT3";
144 case XB_FMT_DXT5:
145 return "DXT5";
146 case XB_FMT_A8R8G8B8:
147 default:
148 return "ARGB";