PageRenderTime 25ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 1ms

/manaplus/src/resources/image/image.cpp

https://gitlab.com/jesusalva/TMW-BR_NecHercules
C++ | 500 lines | 420 code | 51 blank | 29 comment | 66 complexity | e7d1be39c841fa8b4801191982dadc02 MD5 | raw file
  1. /*
  2. * The ManaPlus Client
  3. * Copyright (C) 2004-2009 The Mana World Development Team
  4. * Copyright (C) 2009-2010 The Mana Developers
  5. * Copyright (C) 2011-2016 The ManaPlus Developers
  6. *
  7. * This file is part of The ManaPlus Client.
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. */
  22. #include "resources/image/image.h"
  23. #include "logger.h"
  24. #ifdef USE_OPENGL
  25. #include "resources/openglimagehelper.h"
  26. #endif // USE_OPENGL
  27. #include "resources/memorymanager.h"
  28. #include "resources/sdlimagehelper.h"
  29. #include "resources/image/subimage.h"
  30. #include "resources/resourcemanager/resourcemanager.h"
  31. #include "utils/sdlcheckutils.h"
  32. #ifdef USE_SDL2
  33. #include <SDL2_rotozoom.h>
  34. #else // USE_SDL2
  35. #include <SDL_rotozoom.h>
  36. #endif // USE_SDL2
  37. #include "debug.h"
  38. #ifdef USE_SDL2
  39. Image::Image(SDL_Texture *restrict const image,
  40. const int width, const int height) :
  41. Resource(),
  42. #ifdef USE_OPENGL
  43. mGLImage(0),
  44. mTexWidth(0),
  45. mTexHeight(0),
  46. #endif
  47. mBounds(),
  48. mAlpha(1.0F),
  49. mSDLSurface(nullptr),
  50. mTexture(image),
  51. mAlphaChannel(nullptr),
  52. mAlphaCache(),
  53. mLoaded(false),
  54. mHasAlphaChannel(false),
  55. mUseAlphaCache(false),
  56. mIsAlphaVisible(true),
  57. mIsAlphaCalculated(false)
  58. {
  59. #ifdef DEBUG_IMAGES
  60. logger->log("created image: %p", this);
  61. #endif
  62. mBounds.x = 0;
  63. mBounds.y = 0;
  64. if (mTexture)
  65. {
  66. mBounds.w = CAST_U16(width);
  67. mBounds.h = CAST_U16(height);
  68. mLoaded = true;
  69. }
  70. else
  71. {
  72. logger->log("Image::Image(SDL_Texture *const image):"
  73. " Couldn't load invalid Surface!");
  74. mBounds.w = 0;
  75. mBounds.h = 0;
  76. }
  77. }
  78. #endif
  79. Image::Image(SDL_Surface *restrict const image, const bool hasAlphaChannel0,
  80. uint8_t *restrict const alphaChannel) :
  81. Resource(),
  82. #ifdef USE_OPENGL
  83. mGLImage(0),
  84. mTexWidth(0),
  85. mTexHeight(0),
  86. #endif
  87. mBounds(),
  88. mAlpha(1.0F),
  89. mSDLSurface(image),
  90. #ifdef USE_SDL2
  91. mTexture(nullptr),
  92. #endif
  93. mAlphaChannel(alphaChannel),
  94. mAlphaCache(),
  95. mLoaded(false),
  96. mHasAlphaChannel(hasAlphaChannel0),
  97. mUseAlphaCache(SDLImageHelper::mEnableAlphaCache),
  98. mIsAlphaVisible(hasAlphaChannel0),
  99. mIsAlphaCalculated(false)
  100. {
  101. #ifdef DEBUG_IMAGES
  102. logger->log("created image: %p", static_cast<void*>(this));
  103. #endif
  104. mBounds.x = 0;
  105. mBounds.y = 0;
  106. if (mSDLSurface)
  107. {
  108. mBounds.w = CAST_U16(mSDLSurface->w);
  109. mBounds.h = CAST_U16(mSDLSurface->h);
  110. mLoaded = true;
  111. }
  112. else
  113. {
  114. logger->log(
  115. "Image::Image(SDL_Surface*): Couldn't load invalid Surface!");
  116. mBounds.w = 0;
  117. mBounds.h = 0;
  118. }
  119. }
  120. #ifdef USE_OPENGL
  121. Image::Image(const GLuint glimage, const int width, const int height,
  122. const int texWidth, const int texHeight) :
  123. Resource(),
  124. mGLImage(glimage),
  125. mTexWidth(texWidth),
  126. mTexHeight(texHeight),
  127. mBounds(),
  128. mAlpha(1.0F),
  129. mSDLSurface(nullptr),
  130. #ifdef USE_SDL2
  131. mTexture(nullptr),
  132. #endif
  133. mAlphaChannel(nullptr),
  134. mAlphaCache(),
  135. mLoaded(false),
  136. mHasAlphaChannel(true),
  137. mUseAlphaCache(false),
  138. mIsAlphaVisible(true),
  139. mIsAlphaCalculated(false)
  140. {
  141. #ifdef DEBUG_IMAGES
  142. logger->log("created image: %p", static_cast<void*>(this));
  143. #endif
  144. mBounds.x = 0;
  145. mBounds.y = 0;
  146. mBounds.w = CAST_U16(width);
  147. mBounds.h = CAST_U16(height);
  148. if (mGLImage)
  149. {
  150. mLoaded = true;
  151. }
  152. else
  153. {
  154. logger->log("Image::Image(GLuint*, ...):"
  155. " Couldn't load invalid Surface!");
  156. }
  157. }
  158. #endif
  159. Image::~Image()
  160. {
  161. #ifdef DEBUG_IMAGES
  162. logger->log("delete image: %p", static_cast<void*>(this));
  163. logger->log(" %s, %s", mIdPath.c_str(), mSource.c_str());
  164. #endif
  165. unload();
  166. }
  167. void Image::SDLCleanCache()
  168. {
  169. for (std::map<float, SDL_Surface*>::iterator
  170. i = mAlphaCache.begin(), i_end = mAlphaCache.end();
  171. i != i_end; ++i)
  172. {
  173. if (mSDLSurface != i->second)
  174. resourceManager->scheduleDelete(i->second);
  175. i->second = nullptr;
  176. }
  177. mAlphaCache.clear();
  178. }
  179. void Image::unload()
  180. {
  181. mLoaded = false;
  182. if (mSDLSurface)
  183. {
  184. SDLCleanCache();
  185. // Free the image surface.
  186. MSDL_FreeSurface(mSDLSurface);
  187. mSDLSurface = nullptr;
  188. delete [] mAlphaChannel;
  189. mAlphaChannel = nullptr;
  190. }
  191. #ifdef USE_SDL2
  192. if (mTexture)
  193. {
  194. SDL_DestroyTexture(mTexture);
  195. mTexture = nullptr;
  196. }
  197. #endif
  198. #ifdef USE_OPENGL
  199. if (mGLImage)
  200. {
  201. glDeleteTextures(1, &mGLImage);
  202. mGLImage = 0;
  203. #ifdef DEBUG_OPENGL_LEAKS
  204. if (textures_count > 0)
  205. textures_count --;
  206. #endif
  207. }
  208. #endif
  209. }
  210. bool Image::hasAlphaChannel() const
  211. {
  212. if (mLoaded)
  213. return mHasAlphaChannel;
  214. #ifdef USE_OPENGL
  215. if (OpenGLImageHelper::mUseOpenGL != RENDER_SOFTWARE)
  216. return true;
  217. #endif
  218. return false;
  219. }
  220. SDL_Surface *Image::getByAlpha(const float alpha)
  221. {
  222. const std::map<float, SDL_Surface*>::const_iterator
  223. it = mAlphaCache.find(alpha);
  224. if (it != mAlphaCache.end())
  225. return (*it).second;
  226. return nullptr;
  227. }
  228. void Image::setAlpha(const float alpha)
  229. {
  230. if (mAlpha == alpha || !ImageHelper::mEnableAlpha)
  231. return;
  232. if (alpha < 0.0F || alpha > 1.0F)
  233. return;
  234. if (mSDLSurface)
  235. {
  236. if (mUseAlphaCache)
  237. {
  238. SDL_Surface *surface = getByAlpha(mAlpha);
  239. if (!surface)
  240. {
  241. if (mAlphaCache.size() > 100)
  242. {
  243. #ifdef DEBUG_ALPHA_CACHE
  244. logger->log("cleanCache");
  245. for (std::map<float, SDL_Surface*>::const_iterator
  246. i = mAlphaCache.begin(), i_end = mAlphaCache.end();
  247. i != i_end; ++i)
  248. {
  249. logger->log("alpha: " + toString(i->first));
  250. }
  251. #endif
  252. SDLCleanCache();
  253. }
  254. surface = mSDLSurface;
  255. if (surface)
  256. mAlphaCache[mAlpha] = surface;
  257. }
  258. else
  259. {
  260. logger->log("cache bug");
  261. }
  262. surface = getByAlpha(alpha);
  263. if (surface)
  264. {
  265. if (mSDLSurface == surface)
  266. logger->log("bug");
  267. mAlphaCache.erase(alpha);
  268. mSDLSurface = surface;
  269. mAlpha = alpha;
  270. return;
  271. }
  272. else
  273. {
  274. mSDLSurface = SDLImageHelper::SDLDuplicateSurface(mSDLSurface);
  275. if (!mSDLSurface)
  276. return;
  277. }
  278. }
  279. mAlpha = alpha;
  280. if (!mHasAlphaChannel)
  281. {
  282. #ifdef USE_SDL2
  283. SDL_SetSurfaceAlphaMod(mSDLSurface,
  284. CAST_U8(255 * mAlpha));
  285. #else
  286. // Set the alpha value this image is drawn at
  287. SDL_SetAlpha(mSDLSurface, SDL_SRCALPHA,
  288. CAST_U8(255 * mAlpha));
  289. #endif
  290. }
  291. else
  292. {
  293. if (SDL_MUSTLOCK(mSDLSurface))
  294. SDL_LockSurface(mSDLSurface);
  295. const int bx = mBounds.x;
  296. const int by = mBounds.y;
  297. const int bw = mBounds.w;
  298. const int bh = mBounds.h;
  299. const int bxw = bx + bw;
  300. const int sw = mSDLSurface->w;
  301. const int maxHeight = std::min(by + bh, mSDLSurface->h);
  302. const int maxWidth = std::min(bxw, sw);
  303. const int i1 = by * sw + bx;
  304. const SDL_PixelFormat * const fmt = mSDLSurface->format;
  305. const uint32_t amask = fmt->Amask;
  306. const uint32_t invMask = ~fmt->Amask;
  307. const uint8_t aloss = fmt->Aloss;
  308. const uint8_t ashift = fmt->Ashift;
  309. if (!bx && bxw == sw)
  310. {
  311. const int i2 = (maxHeight - 1) * sw + maxWidth - 1;
  312. for (int i = i1; i <= i2; i++)
  313. {
  314. const uint8_t sourceAlpha = mAlphaChannel[i];
  315. if (sourceAlpha > 0)
  316. {
  317. const uint8_t a = CAST_U8(
  318. static_cast<float>(sourceAlpha) * mAlpha);
  319. uint32_t c = (static_cast<uint32_t*>(
  320. mSDLSurface->pixels))[i];
  321. c &= invMask;
  322. c |= ((a >> aloss) << ashift & amask);
  323. (static_cast<uint32_t*>(mSDLSurface->pixels))[i] = c;
  324. }
  325. }
  326. }
  327. else
  328. {
  329. for (int y = by; y < maxHeight; y ++)
  330. {
  331. const int idx = y * sw;
  332. const int x1 = idx + bx;
  333. const int x2 = idx + maxWidth;
  334. for (int i = x1; i < x2; i ++)
  335. {
  336. const uint8_t sourceAlpha = mAlphaChannel[i];
  337. if (sourceAlpha > 0)
  338. {
  339. const uint8_t a = CAST_U8(
  340. static_cast<float>(sourceAlpha) * mAlpha);
  341. uint32_t c = (static_cast<uint32_t*>(
  342. mSDLSurface->pixels))[i];
  343. c &= invMask;
  344. c |= ((a >> aloss) << ashift & amask);
  345. (static_cast<uint32_t*>(
  346. mSDLSurface->pixels))[i] = c;
  347. }
  348. }
  349. }
  350. }
  351. if (SDL_MUSTLOCK(mSDLSurface))
  352. SDL_UnlockSurface(mSDLSurface);
  353. }
  354. }
  355. #ifdef USE_SDL2
  356. else if (mTexture)
  357. {
  358. mAlpha = alpha;
  359. SDL_SetTextureAlphaMod(mTexture,
  360. CAST_U8(255 * mAlpha));
  361. }
  362. #endif
  363. else
  364. {
  365. mAlpha = alpha;
  366. }
  367. }
  368. Image* Image::SDLgetScaledImage(const int width, const int height) const
  369. {
  370. // No scaling on incorrect new values.
  371. if (width == 0 || height == 0)
  372. return nullptr;
  373. // No scaling when there is ... no different given size ...
  374. if (width == mBounds.w && height == mBounds.h)
  375. return nullptr;
  376. Image* scaledImage = nullptr;
  377. if (mSDLSurface)
  378. {
  379. SDL_Surface *const scaledSurface = zoomSurface(mSDLSurface,
  380. static_cast<double>(width) / mBounds.w,
  381. static_cast<double>(height) / mBounds.h,
  382. 1);
  383. // The load function takes care of the SDL<->OpenGL implementation
  384. // and about freeing the given SDL_surface*.
  385. if (scaledSurface)
  386. {
  387. scaledImage = imageHelper->loadSurface(scaledSurface);
  388. MSDL_FreeSurface(scaledSurface);
  389. }
  390. }
  391. return scaledImage;
  392. }
  393. Image *Image::getSubImage(const int x, const int y,
  394. const int width, const int height)
  395. {
  396. // Create a new clipped sub-image
  397. #ifdef USE_OPENGL
  398. const RenderType mode = OpenGLImageHelper::mUseOpenGL;
  399. if (mode == RENDER_NORMAL_OPENGL ||
  400. mode == RENDER_SAFE_OPENGL ||
  401. mode == RENDER_GLES_OPENGL ||
  402. mode == RENDER_GLES2_OPENGL ||
  403. mode == RENDER_MODERN_OPENGL)
  404. {
  405. return new SubImage(this,
  406. mGLImage,
  407. x, y,
  408. width, height,
  409. mTexWidth, mTexHeight);
  410. }
  411. #endif
  412. #ifdef USE_SDL2
  413. #ifndef USE_OPENGL
  414. const RenderType mode = ImageHelper::mUseOpenGL;
  415. #endif
  416. if (mode == RENDER_SOFTWARE)
  417. return new SubImage(this, mSDLSurface, x, y, width, height);
  418. else
  419. return new SubImage(this, mTexture, x, y, width, height);
  420. #else
  421. return new SubImage(this, mSDLSurface, x, y, width, height);
  422. #endif
  423. }
  424. void Image::SDLTerminateAlphaCache()
  425. {
  426. SDLCleanCache();
  427. mUseAlphaCache = false;
  428. }
  429. int Image::calcMemoryLocal() const
  430. {
  431. // +++ this calculation can be wrong for SDL2
  432. int sz = static_cast<int>(sizeof(Image) +
  433. sizeof(std::map<float, SDL_Surface*>)) +
  434. Resource::calcMemoryLocal();
  435. if (mSDLSurface)
  436. {
  437. sz += CAST_S32(mAlphaCache.size()) *
  438. memoryManager.getSurfaceSize(mSDLSurface);
  439. }
  440. return sz;
  441. }
  442. #ifdef USE_OPENGL
  443. void Image::decRef()
  444. {
  445. if (mGLImage && getRefCount() <= 1)
  446. OpenGLImageHelper::invalidate(mGLImage);
  447. Resource::decRef();
  448. }
  449. #endif