DSDisplayOverlay.VMR9.cxx 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /* Copyright (C) 2011-2013 Doubango Telecom <http://www.doubango.org>
  2. *
  3. * This file is part of Open Source Doubango Framework.
  4. *
  5. * DOUBANGO 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 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * DOUBANGO 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 DOUBANGO.
  17. */
  18. #if defined(VMR9) || defined(VMR9_WINDOWLESS)
  19. #define DIRECT3D_VERSION 0x0900
  20. #include <internals/DSDisplayOverlay.h>
  21. #include <internals/DSDisplayGraph.h>
  22. #include <internals/DSUtils.h>
  23. using namespace std;
  24. #define FILENAME _T("Overlay.png")
  25. #define ALPHA_VALUE_START 0.8f
  26. #define ALPHA_VALUE_STOP 0.0f
  27. DSDisplayOverlay::DSDisplayOverlay()
  28. {
  29. this->window = NULL;
  30. this->direct3DDevice = NULL;
  31. this->direct3DSurface = NULL;
  32. this->direct3D = Direct3DCreate9(D3D_SDK_VERSION);
  33. if (!this->direct3D) {
  34. cout << "Cannot create Direct3D environment" << endl;
  35. return;
  36. }
  37. }
  38. DSDisplayOverlay::~DSDisplayOverlay()
  39. {
  40. SAFE_RELEASE(this->direct3D);
  41. }
  42. void DSDisplayOverlay::attach(HWND parent, DSDisplayGraph *graph)
  43. {
  44. HRESULT hr;
  45. // Gets the handle of the parent and the graph
  46. this->window = parent;
  47. this->displayGraph = graph;
  48. if (this->window) {
  49. D3DPRESENT_PARAMETERS d3dpp;
  50. ZeroMemory(&d3dpp, sizeof(D3DPRESENT_PARAMETERS));
  51. d3dpp.Windowed = TRUE;
  52. d3dpp.SwapEffect = D3DSWAPEFFECT_COPY;
  53. hr = this->direct3D->CreateDevice(
  54. D3DADAPTER_DEFAULT,
  55. D3DDEVTYPE_HAL,
  56. this->window,
  57. D3DCREATE_SOFTWARE_VERTEXPROCESSING,
  58. &d3dpp,
  59. &this->direct3DDevice);
  60. if (FAILED(hr)) {
  61. cout << "Cannot create Direct3D device" << endl;
  62. return;
  63. }
  64. ZeroMemory(&this->overlayInfo, sizeof(D3DXIMAGE_INFO));
  65. hr = D3DXGetImageInfoFromFile(FILENAME, &this->overlayInfo);
  66. if (FAILED(hr)) {
  67. cout << "Cannot stat overlay file" << endl;
  68. return;
  69. }
  70. hr = this->direct3DDevice->CreateOffscreenPlainSurface(
  71. this->overlayInfo.Width,
  72. this->overlayInfo.Height,
  73. D3DFMT_A8R8G8B8,
  74. D3DPOOL_SYSTEMMEM,
  75. &this->direct3DSurface,
  76. NULL);
  77. if (FAILED(hr)) {
  78. cout << "Cannot create Direct3D surface" << endl;
  79. return;
  80. }
  81. D3DCOLOR alphaKey = 0xFF000000;
  82. hr = D3DXLoadSurfaceFromFile(this->direct3DSurface,
  83. NULL,
  84. NULL,
  85. FILENAME,
  86. NULL,
  87. D3DX_FILTER_NONE,
  88. alphaKey,
  89. &this->overlayInfo);
  90. if (FAILED(hr)) {
  91. cout << "Cannot load overlay file" << endl;
  92. return;
  93. }
  94. D3DVIEWPORT9 viewport;
  95. ZeroMemory(&viewport, sizeof(D3DVIEWPORT9));
  96. hr= this->direct3DDevice->GetViewport(&viewport);
  97. if (FAILED(hr)) {
  98. cout << "Cannot get view port" << endl;
  99. return;
  100. }
  101. ZeroMemory(&this->alphaBitmap, sizeof(VMR9AlphaBitmap));
  102. this->alphaBitmap.dwFlags = VMR9AlphaBitmap_EntireDDS;
  103. this->alphaBitmap.hdc = NULL;
  104. this->alphaBitmap.pDDS = this->direct3DSurface;
  105. // Source rectangle
  106. this->alphaBitmap.rSrc.left = 0;
  107. this->alphaBitmap.rSrc.top = 0;
  108. this->alphaBitmap.rSrc.right = this->overlayInfo.Width;
  109. this->alphaBitmap.rSrc.bottom = this->overlayInfo.Height;
  110. // Destination rectangle
  111. this->alphaBitmap.rDest.left = (viewport.Width - this->overlayInfo.Width) / 2.0;
  112. this->alphaBitmap.rDest.top = (viewport.Height - this->overlayInfo.Height) / 2.0;
  113. this->alphaBitmap.rDest.right = this->alphaBitmap.rDest.left + this->overlayInfo.Width;
  114. this->alphaBitmap.rDest.bottom = this->alphaBitmap.rDest.top + this->overlayInfo.Height;
  115. this->alphaBitmap.rDest.left /= viewport.Width;
  116. this->alphaBitmap.rDest.top /= viewport.Height;
  117. this->alphaBitmap.rDest.right /= viewport.Width;
  118. this->alphaBitmap.rDest.bottom /= viewport.Height;
  119. // Alpha value for start
  120. this->alphaBitmap.fAlpha = ALPHA_VALUE_START;
  121. }
  122. }
  123. void DSDisplayOverlay::detach()
  124. {
  125. SAFE_RELEASE(this->direct3DSurface);
  126. SAFE_RELEASE(this->direct3DDevice);
  127. this->displayGraph = NULL;
  128. this->window = NULL;
  129. }
  130. void DSDisplayOverlay::show(int value)
  131. {
  132. // Store the ticks to count down
  133. this->ticks = value;
  134. // Compute alpha value decrement
  135. this->alphaStep = (this->ticks > 0) ? ((ALPHA_VALUE_START - ALPHA_VALUE_STOP) / this->ticks) : 0;
  136. this->alphaBitmap.fAlpha = ALPHA_VALUE_START;
  137. this->internalUpdate();
  138. }
  139. void DSDisplayOverlay::update()
  140. {
  141. if (this->displayGraph && (this->ticks > 0)) {
  142. this->ticks--;
  143. // Be sure alpha is in 0.0 .. 1.0 range.
  144. float value = this->alphaBitmap.fAlpha;
  145. value -= this->alphaStep;
  146. this->alphaBitmap.fAlpha = (value >= 0.0f) ? value : 0.0f;
  147. this->internalUpdate();
  148. }
  149. }
  150. void DSDisplayOverlay::internalUpdate()
  151. {
  152. HRESULT hr;
  153. if (this->ticks > 0) {
  154. this->alphaBitmap.dwFlags = VMR9AlphaBitmap_EntireDDS;
  155. }
  156. else {
  157. this->alphaBitmap.dwFlags = VMR9AlphaBitmap_Disable;
  158. }
  159. hr = this->displayGraph->getMixerBitmap()->SetAlphaBitmap(&this->alphaBitmap);
  160. if (FAILED(hr)) {
  161. cout << "Failed to mix overylay (" << hr << ")" << endl;
  162. return;
  163. }
  164. }
  165. #endif