mf: Rework node collection implementation.

Signed-off-by: Nikolay Sivov <nsivov@codeweavers.com>
Signed-off-by: Alexandre Julliard <julliard@winehq.org>
This commit is contained in:
Nikolay Sivov 2019-05-08 14:46:41 +03:00 committed by Alexandre Julliard
parent 6b1a33ce93
commit 184f999459
1 changed files with 99 additions and 94 deletions

View File

@ -45,15 +45,6 @@ WINE_DEFAULT_DEBUG_CHANNEL(mfplat);
static LONG next_node_id; static LONG next_node_id;
static TOPOID next_topology_id; static TOPOID next_topology_id;
struct topology
{
IMFTopology IMFTopology_iface;
LONG refcount;
IMFAttributes *attributes;
IMFCollection *nodes;
TOPOID id;
};
struct node_stream struct node_stream
{ {
IMFMediaType *preferred_type; IMFMediaType *preferred_type;
@ -81,6 +72,20 @@ struct topology_node
CRITICAL_SECTION cs; CRITICAL_SECTION cs;
}; };
struct topology
{
IMFTopology IMFTopology_iface;
LONG refcount;
IMFAttributes *attributes;
struct
{
struct topology_node **nodes;
size_t size;
size_t count;
} nodes;
TOPOID id;
};
struct topology_loader struct topology_loader
{ {
IMFTopoLoader IMFTopoLoader_iface; IMFTopoLoader IMFTopoLoader_iface;
@ -169,19 +174,32 @@ static ULONG WINAPI topology_AddRef(IMFTopology *iface)
return refcount; return refcount;
} }
static void topology_clear(struct topology *topology)
{
size_t i;
for (i = 0; i < topology->nodes.count; ++i)
{
IMFTopologyNode_Release(&topology->nodes.nodes[i]->IMFTopologyNode_iface);
}
heap_free(topology->nodes.nodes);
topology->nodes.nodes = NULL;
topology->nodes.count = 0;
topology->nodes.size = 0;
}
static ULONG WINAPI topology_Release(IMFTopology *iface) static ULONG WINAPI topology_Release(IMFTopology *iface)
{ {
struct topology *topology = impl_from_IMFTopology(iface); struct topology *topology = impl_from_IMFTopology(iface);
ULONG refcount = InterlockedDecrement(&topology->refcount); ULONG refcount = InterlockedDecrement(&topology->refcount);
TRACE("(%p) refcount=%u\n", iface, refcount); TRACE("%p, refcount %u.\n", iface, refcount);
if (!refcount) if (!refcount)
{ {
if (topology->attributes) if (topology->attributes)
IMFAttributes_Release(topology->attributes); IMFAttributes_Release(topology->attributes);
if (topology->nodes) topology_clear(topology);
IMFCollection_Release(topology->nodes);
heap_free(topology); heap_free(topology);
} }
@ -476,80 +494,72 @@ static HRESULT WINAPI topology_GetTopologyID(IMFTopology *iface, TOPOID *id)
return S_OK; return S_OK;
} }
static HRESULT topology_get_node_by_id(const struct topology *topology, TOPOID id, IMFTopologyNode **node) static HRESULT topology_get_node_by_id(const struct topology *topology, TOPOID id, struct topology_node **node)
{ {
IMFTopologyNode *iter; size_t i = 0;
unsigned int i = 0;
while (IMFCollection_GetElement(topology->nodes, i++, (IUnknown **)&iter) == S_OK) for (i = 0; i < topology->nodes.count; ++i)
{ {
TOPOID node_id; if (topology->nodes.nodes[i]->id == id)
HRESULT hr;
hr = IMFTopologyNode_GetTopoNodeID(iter, &node_id);
if (FAILED(hr))
{ {
IMFTopologyNode_Release(iter); *node = topology->nodes.nodes[i];
return hr; IMFTopologyNode_AddRef(&(*node)->IMFTopologyNode_iface);
}
if (node_id == id)
{
*node = iter;
return S_OK; return S_OK;
} }
IMFTopologyNode_Release(iter);
} }
return MF_E_NOT_FOUND; return MF_E_NOT_FOUND;
} }
static HRESULT WINAPI topology_AddNode(IMFTopology *iface, IMFTopologyNode *node) static HRESULT WINAPI topology_AddNode(IMFTopology *iface, IMFTopologyNode *node_iface)
{ {
struct topology *topology = impl_from_IMFTopology(iface); struct topology *topology = impl_from_IMFTopology(iface);
IMFTopologyNode *match; struct topology_node *node = unsafe_impl_from_IMFTopologyNode(node_iface);
HRESULT hr; struct topology_node *match;
TOPOID id;
TRACE("(%p)->(%p)\n", iface, node); TRACE("%p, %p.\n", iface, node_iface);
if (!node) if (!node)
return E_POINTER; return E_POINTER;
if (FAILED(hr = IMFTopologyNode_GetTopoNodeID(node, &id))) if (SUCCEEDED(topology_get_node_by_id(topology, node->id, &match)))
return hr; {
IMFTopologyNode_Release(&match->IMFTopologyNode_iface);
return E_INVALIDARG;
}
if (FAILED(topology_get_node_by_id(topology, id, &match))) if (!mf_array_reserve((void **)&topology->nodes.nodes, &topology->nodes.size, topology->nodes.count + 1,
return IMFCollection_AddElement(topology->nodes, (IUnknown *)node); sizeof(*topology->nodes.nodes)))
{
return E_OUTOFMEMORY;
}
IMFTopologyNode_Release(match); topology->nodes.nodes[topology->nodes.count++] = node;
IMFTopologyNode_AddRef(&node->IMFTopologyNode_iface);
return E_INVALIDARG; return S_OK;
} }
static HRESULT WINAPI topology_RemoveNode(IMFTopology *iface, IMFTopologyNode *node) static HRESULT WINAPI topology_RemoveNode(IMFTopology *iface, IMFTopologyNode *node)
{ {
struct topology *topology = impl_from_IMFTopology(iface); struct topology *topology = impl_from_IMFTopology(iface);
unsigned int i = 0; size_t i, count;
IUnknown *element;
TRACE("(%p)->(%p)\n", iface, node); TRACE("%p, %p.\n", iface, node);
while (IMFCollection_GetElement(topology->nodes, i, &element) == S_OK) for (i = 0; i < topology->nodes.count; ++i)
{ {
BOOL match = element == (IUnknown *)node; if (&topology->nodes.nodes[i]->IMFTopologyNode_iface == node)
IUnknown_Release(element);
if (match)
{ {
IMFCollection_RemoveElement(topology->nodes, i, &element); count = topology->nodes.count - i - 1;
IUnknown_Release(element); if (count)
{
memmove(&topology->nodes.nodes[i], &topology->nodes.nodes[i + 1],
count * sizeof(*topology->nodes.nodes));
}
topology->nodes.count--;
return S_OK; return S_OK;
} }
++i;
} }
return E_INVALIDARG; return E_INVALIDARG;
@ -558,38 +568,43 @@ static HRESULT WINAPI topology_RemoveNode(IMFTopology *iface, IMFTopologyNode *n
static HRESULT WINAPI topology_GetNodeCount(IMFTopology *iface, WORD *count) static HRESULT WINAPI topology_GetNodeCount(IMFTopology *iface, WORD *count)
{ {
struct topology *topology = impl_from_IMFTopology(iface); struct topology *topology = impl_from_IMFTopology(iface);
DWORD nodecount;
HRESULT hr;
TRACE("(%p)->(%p)\n", iface, count); TRACE("%p, %p.\n", iface, count);
hr = IMFCollection_GetElementCount(topology->nodes, count ? &nodecount : NULL); if (!count)
if (count) return E_POINTER;
*count = nodecount;
return hr; *count = topology->nodes.count;
return S_OK;
} }
static HRESULT WINAPI topology_GetNode(IMFTopology *iface, WORD index, IMFTopologyNode **node) static HRESULT WINAPI topology_GetNode(IMFTopology *iface, WORD index, IMFTopologyNode **node)
{ {
struct topology *topology = impl_from_IMFTopology(iface); struct topology *topology = impl_from_IMFTopology(iface);
TRACE("(%p)->(%u, %p)\n", iface, index, node); TRACE("%p, %u, %p.\n", iface, index, node);
if (!node) if (!node)
return E_POINTER; return E_POINTER;
return SUCCEEDED(IMFCollection_GetElement(topology->nodes, index, (IUnknown **)node)) ? if (index >= topology->nodes.count)
S_OK : MF_E_INVALIDINDEX; return MF_E_INVALIDINDEX;
*node = &topology->nodes.nodes[index]->IMFTopologyNode_iface;
IMFTopologyNode_AddRef(*node);
return S_OK;
} }
static HRESULT WINAPI topology_Clear(IMFTopology *iface) static HRESULT WINAPI topology_Clear(IMFTopology *iface)
{ {
struct topology *topology = impl_from_IMFTopology(iface); struct topology *topology = impl_from_IMFTopology(iface);
TRACE("(%p)\n", iface); TRACE("%p.\n", iface);
return IMFCollection_RemoveAllElements(topology->nodes); topology_clear(topology);
return S_OK;
} }
static HRESULT WINAPI topology_CloneFrom(IMFTopology *iface, IMFTopology *src_topology) static HRESULT WINAPI topology_CloneFrom(IMFTopology *iface, IMFTopology *src_topology)
@ -599,36 +614,27 @@ static HRESULT WINAPI topology_CloneFrom(IMFTopology *iface, IMFTopology *src_to
return E_NOTIMPL; return E_NOTIMPL;
} }
static HRESULT WINAPI topology_GetNodeByID(IMFTopology *iface, TOPOID id, IMFTopologyNode **node) static HRESULT WINAPI topology_GetNodeByID(IMFTopology *iface, TOPOID id, IMFTopologyNode **ret)
{ {
struct topology *topology = impl_from_IMFTopology(iface); struct topology *topology = impl_from_IMFTopology(iface);
struct topology_node *node;
TRACE("(%p)->(%p)\n", iface, node);
return topology_get_node_by_id(topology, id, node);
}
static HRESULT topology_add_node_of_type(const struct topology *topology, IMFTopologyNode *node,
MF_TOPOLOGY_TYPE filter, IMFCollection *collection)
{
MF_TOPOLOGY_TYPE node_type;
HRESULT hr; HRESULT hr;
if (FAILED(hr = IMFTopologyNode_GetNodeType(node, &node_type))) TRACE("%p, %p.\n", iface, ret);
return hr;
if (node_type != filter) if (SUCCEEDED(hr = topology_get_node_by_id(topology, id, &node)))
return S_OK; *ret = &node->IMFTopologyNode_iface;
else
*ret = NULL;
return IMFCollection_AddElement(collection, (IUnknown *)node); return hr;
} }
static HRESULT topology_get_node_collection(const struct topology *topology, MF_TOPOLOGY_TYPE node_type, static HRESULT topology_get_node_collection(const struct topology *topology, MF_TOPOLOGY_TYPE node_type,
IMFCollection **collection) IMFCollection **collection)
{ {
IMFTopologyNode *node;
unsigned int i = 0;
HRESULT hr; HRESULT hr;
size_t i;
if (!collection) if (!collection)
return E_POINTER; return E_POINTER;
@ -636,15 +642,17 @@ static HRESULT topology_get_node_collection(const struct topology *topology, MF_
if (FAILED(hr = MFCreateCollection(collection))) if (FAILED(hr = MFCreateCollection(collection)))
return hr; return hr;
while (IMFCollection_GetElement(topology->nodes, i++, (IUnknown **)&node) == S_OK) for (i = 0; i < topology->nodes.count; ++i)
{ {
hr = topology_add_node_of_type(topology, node, node_type, *collection); if (topology->nodes.nodes[i]->node_type == node_type)
IMFTopologyNode_Release(node);
if (FAILED(hr))
{ {
IMFCollection_Release(*collection); if (FAILED(hr = IMFCollection_AddElement(*collection,
*collection = NULL; (IUnknown *)&topology->nodes.nodes[i]->IMFTopologyNode_iface)))
break; {
IMFCollection_Release(*collection);
*collection = NULL;
break;
}
} }
} }
@ -750,9 +758,6 @@ HRESULT WINAPI MFCreateTopology(IMFTopology **topology)
object->refcount = 1; object->refcount = 1;
hr = MFCreateAttributes(&object->attributes, 0); hr = MFCreateAttributes(&object->attributes, 0);
if (SUCCEEDED(hr))
hr = MFCreateCollection(&object->nodes);
if (FAILED(hr)) if (FAILED(hr))
{ {
IMFTopology_Release(&object->IMFTopology_iface); IMFTopology_Release(&object->IMFTopology_iface);