You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

205 lines
5.7 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #include "test/win/d3d_renderer.h"
  11. #include "common_video/libyuv/include/webrtc_libyuv.h"
  12. #include "rtc_base/checks.h"
  13. namespace webrtc {
  14. namespace test {
  15. #define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ | D3DFVF_TEX1)
  16. struct D3dCustomVertex {
  17. float x, y, z;
  18. float u, v;
  19. };
  20. const char kD3DClassName[] = "d3d_renderer";
  21. VideoRenderer* VideoRenderer::CreatePlatformRenderer(const char* window_title,
  22. size_t width,
  23. size_t height) {
  24. return D3dRenderer::Create(window_title, width, height);
  25. }
  26. D3dRenderer::D3dRenderer(size_t width, size_t height)
  27. : width_(width),
  28. height_(height),
  29. hwnd_(NULL),
  30. d3d_(NULL),
  31. d3d_device_(NULL),
  32. texture_(NULL),
  33. vertex_buffer_(NULL) {
  34. RTC_DCHECK_GT(width, 0);
  35. RTC_DCHECK_GT(height, 0);
  36. }
  37. D3dRenderer::~D3dRenderer() {
  38. Destroy();
  39. }
  40. LRESULT WINAPI D3dRenderer::WindowProc(HWND hwnd,
  41. UINT msg,
  42. WPARAM wparam,
  43. LPARAM lparam) {
  44. if (msg == WM_DESTROY || (msg == WM_CHAR && wparam == VK_RETURN)) {
  45. PostQuitMessage(0);
  46. return 0;
  47. }
  48. return DefWindowProcA(hwnd, msg, wparam, lparam);
  49. }
  50. void D3dRenderer::Destroy() {
  51. texture_ = NULL;
  52. vertex_buffer_ = NULL;
  53. d3d_device_ = NULL;
  54. d3d_ = NULL;
  55. if (hwnd_ != NULL) {
  56. DestroyWindow(hwnd_);
  57. RTC_DCHECK(!IsWindow(hwnd_));
  58. hwnd_ = NULL;
  59. }
  60. }
  61. bool D3dRenderer::Init(const char* window_title) {
  62. hwnd_ = CreateWindowA(kD3DClassName, window_title, WS_OVERLAPPEDWINDOW, 0, 0,
  63. static_cast<int>(width_), static_cast<int>(height_),
  64. NULL, NULL, NULL, NULL);
  65. if (hwnd_ == NULL) {
  66. Destroy();
  67. return false;
  68. }
  69. d3d_ = Direct3DCreate9(D3D_SDK_VERSION);
  70. if (d3d_ == NULL) {
  71. Destroy();
  72. return false;
  73. }
  74. D3DPRESENT_PARAMETERS d3d_params = {};
  75. d3d_params.Windowed = TRUE;
  76. d3d_params.SwapEffect = D3DSWAPEFFECT_COPY;
  77. IDirect3DDevice9* d3d_device;
  78. if (d3d_->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd_,
  79. D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3d_params,
  80. &d3d_device) != D3D_OK) {
  81. Destroy();
  82. return false;
  83. }
  84. d3d_device_ = d3d_device;
  85. d3d_device->Release();
  86. IDirect3DVertexBuffer9* vertex_buffer;
  87. const int kRectVertices = 4;
  88. if (d3d_device_->CreateVertexBuffer(kRectVertices * sizeof(D3dCustomVertex),
  89. 0, D3DFVF_CUSTOMVERTEX, D3DPOOL_MANAGED,
  90. &vertex_buffer, NULL) != D3D_OK) {
  91. Destroy();
  92. return false;
  93. }
  94. vertex_buffer_ = vertex_buffer;
  95. vertex_buffer->Release();
  96. d3d_device_->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
  97. d3d_device_->SetRenderState(D3DRS_LIGHTING, FALSE);
  98. Resize(width_, height_);
  99. ShowWindow(hwnd_, SW_SHOWNOACTIVATE);
  100. d3d_device_->Present(NULL, NULL, NULL, NULL);
  101. return true;
  102. }
  103. D3dRenderer* D3dRenderer::Create(const char* window_title,
  104. size_t width,
  105. size_t height) {
  106. static ATOM wc_atom = 0;
  107. if (wc_atom == 0) {
  108. WNDCLASSA wc = {};
  109. wc.style = CS_HREDRAW | CS_VREDRAW;
  110. wc.lpfnWndProc = WindowProc;
  111. wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  112. wc.hbrBackground = reinterpret_cast<HBRUSH>(COLOR_WINDOW);
  113. wc.lpszClassName = kD3DClassName;
  114. wc_atom = RegisterClassA(&wc);
  115. if (wc_atom == 0)
  116. return nullptr;
  117. }
  118. D3dRenderer* d3d_renderer = new D3dRenderer(width, height);
  119. if (!d3d_renderer->Init(window_title)) {
  120. delete d3d_renderer;
  121. return nullptr;
  122. }
  123. return d3d_renderer;
  124. }
  125. void D3dRenderer::Resize(size_t width, size_t height) {
  126. width_ = width;
  127. height_ = height;
  128. IDirect3DTexture9* texture;
  129. d3d_device_->CreateTexture(static_cast<UINT>(width_),
  130. static_cast<UINT>(height_), 1, 0, D3DFMT_A8R8G8B8,
  131. D3DPOOL_MANAGED, &texture, NULL);
  132. texture_ = texture;
  133. texture->Release();
  134. // Vertices for the video frame to be rendered to.
  135. static const D3dCustomVertex rect[] = {
  136. {-1.0f, -1.0f, 0.0f, 0.0f, 1.0f},
  137. {-1.0f, 1.0f, 0.0f, 0.0f, 0.0f},
  138. {1.0f, -1.0f, 0.0f, 1.0f, 1.0f},
  139. {1.0f, 1.0f, 0.0f, 1.0f, 0.0f},
  140. };
  141. void* buf_data;
  142. if (vertex_buffer_->Lock(0, 0, &buf_data, 0) != D3D_OK)
  143. return;
  144. memcpy(buf_data, &rect, sizeof(rect));
  145. vertex_buffer_->Unlock();
  146. }
  147. void D3dRenderer::OnFrame(const webrtc::VideoFrame& frame) {
  148. if (static_cast<size_t>(frame.width()) != width_ ||
  149. static_cast<size_t>(frame.height()) != height_) {
  150. Resize(static_cast<size_t>(frame.width()),
  151. static_cast<size_t>(frame.height()));
  152. }
  153. D3DLOCKED_RECT lock_rect;
  154. if (texture_->LockRect(0, &lock_rect, NULL, 0) != D3D_OK)
  155. return;
  156. ConvertFromI420(frame, VideoType::kARGB, 0,
  157. static_cast<uint8_t*>(lock_rect.pBits));
  158. texture_->UnlockRect(0);
  159. d3d_device_->BeginScene();
  160. d3d_device_->SetFVF(D3DFVF_CUSTOMVERTEX);
  161. d3d_device_->SetStreamSource(0, vertex_buffer_, 0, sizeof(D3dCustomVertex));
  162. d3d_device_->SetTexture(0, texture_);
  163. d3d_device_->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);
  164. d3d_device_->EndScene();
  165. d3d_device_->Present(NULL, NULL, NULL, NULL);
  166. }
  167. } // namespace test
  168. } // namespace webrtc