[XAudio2] avoid leak + fix voice creation for closest match
[xbmc.git] / xbmc / pictures / SlideShowPictureDX.cpp
blobb1ea44b00d85fb67e6a4d48c8049119edcdb40cc
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 "SlideShowPictureDX.h"
11 #include "guilib/TextureDX.h"
12 #include "rendering/dx/DeviceResources.h"
13 #include "rendering/dx/RenderContext.h"
15 #include <DirectXMath.h>
16 using namespace DirectX;
17 using namespace Microsoft::WRL;
19 std::unique_ptr<CSlideShowPic> CSlideShowPic::CreateSlideShowPicture()
21 return std::make_unique<CSlideShowPicDX>();
24 bool CSlideShowPicDX::UpdateVertexBuffer(Vertex* vertices)
26 if (!m_vb) // create new
28 CD3D11_BUFFER_DESC desc(sizeof(Vertex) * 5, D3D11_BIND_VERTEX_BUFFER, D3D11_USAGE_DYNAMIC,
29 D3D11_CPU_ACCESS_WRITE);
30 D3D11_SUBRESOURCE_DATA initData = {};
31 initData.pSysMem = vertices;
32 initData.SysMemPitch = sizeof(Vertex) * 5;
33 if (SUCCEEDED(DX::DeviceResources::Get()->GetD3DDevice()->CreateBuffer(
34 &desc, &initData, m_vb.ReleaseAndGetAddressOf())))
35 return true;
37 else // update
39 ID3D11DeviceContext* pContext = DX::DeviceResources::Get()->GetD3DContext();
40 D3D11_MAPPED_SUBRESOURCE res;
41 if (SUCCEEDED(pContext->Map(m_vb.Get(), 0, D3D11_MAP_WRITE_DISCARD, 0, &res)))
43 memcpy(res.pData, vertices, sizeof(Vertex) * 5);
44 pContext->Unmap(m_vb.Get(), 0);
45 return true;
49 return false;
52 void CSlideShowPicDX::Render(float* x,
53 float* y,
54 CTexture* pTexture,
55 KODI::UTILS::COLOR::Color color)
57 Vertex vertex[5];
58 for (int i = 0; i < 4; i++)
60 vertex[i].pos = XMFLOAT3(x[i], y[i], 0);
61 CD3DHelper::XMStoreColor(&vertex[i].color, color);
62 vertex[i].texCoord = XMFLOAT2(0.0f, 0.0f);
63 vertex[i].texCoord2 = XMFLOAT2(0.0f, 0.0f);
66 if (pTexture)
68 vertex[1].texCoord.x = vertex[2].texCoord.x =
69 (float)pTexture->GetWidth() / pTexture->GetTextureWidth();
70 vertex[2].texCoord.y = vertex[3].texCoord.y =
71 (float)pTexture->GetHeight() / pTexture->GetTextureHeight();
73 else
75 vertex[1].texCoord.x = vertex[2].texCoord.x = 1.0f;
76 vertex[2].texCoord.y = vertex[3].texCoord.y = 1.0f;
78 vertex[4] = vertex[0]; // Not used when pTexture != NULL
80 CGUIShaderDX* pGUIShader = DX::Windowing()->GetGUIShader();
81 pGUIShader->Begin(SHADER_METHOD_RENDER_TEXTURE_BLEND);
83 // Set state to render the image
84 if (pTexture)
86 pTexture->LoadToGPU();
87 CDXTexture* dxTexture = reinterpret_cast<CDXTexture*>(pTexture);
88 ID3D11ShaderResourceView* shaderRes = dxTexture->GetShaderResource();
89 pGUIShader->SetShaderViews(1, &shaderRes);
90 pGUIShader->DrawQuad(vertex[0], vertex[1], vertex[2], vertex[3]);
92 else
94 if (!UpdateVertexBuffer(vertex))
95 return;
97 ComPtr<ID3D11DeviceContext> pContext = DX::DeviceResources::Get()->GetD3DContext();
99 unsigned stride = sizeof(Vertex);
100 unsigned offset = 0;
101 pContext->IASetVertexBuffers(0, 1, m_vb.GetAddressOf(), &stride, &offset);
102 pContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_LINESTRIP);
104 pGUIShader->Draw(5, 0);
105 pGUIShader->RestoreBuffers();