我阅读了Android Oreo源代码中我不太了解的一些代码。

首先,类IOMXNode具有一个功能:

class IOMXNode : public IInterface {
public:
+-- 46 lines: DECLARE_HYBRID_META_INTERFACE(OMXNode, IOmxNode);-------------------------------------------------------------------------------------------------------------------------------

    // Instructs the component to use the buffer passed in via |omxBuf| on the
    // specified port. Returns in |*buffer| the buffer id that the component
    // assigns to this buffer. |omxBuf| must be one of:
    // 1) OMXBuffer::sPreset for meta-mode,
    // 2) type kBufferTypeANWBuffer for non-meta-graphic buffer mode,
    // 3) type kBufferTypeSharedMem for bytebuffer mode.
    virtual status_t useBuffer(
            OMX_U32 port_index, const OMXBuffer &omxBuf, buffer_id *buffer) = 0;


useBuffer()具有三个参数,其中之一是const OMXBuffer&,它的定义如下:

class OMXBuffer {
public:
    // sPreset is used in places where we are referring to a pre-registered
    // buffer on a port. It has type kBufferTypePreset and mRangeLength of 0.
    static OMXBuffer sPreset;

    // Default constructor, constructs a buffer of type kBufferTypeInvalid.
    OMXBuffer();

    // Constructs a buffer of type kBufferTypePreset with mRangeOffset set to
    // |codecBuffer|'s offset and mRangeLength set to |codecBuffer|'s size (or 0
    // if |codecBuffer| is NULL).
    OMXBuffer(const sp<MediaCodecBuffer> &codecBuffer);

    // Constructs a buffer of type kBufferTypePreset with specified mRangeOffset
    // and mRangeLength.
    OMXBuffer(OMX_U32 rangeOffset, OMX_U32 rangeLength);

    // Constructs a buffer of type kBufferTypeSharedMem.
    OMXBuffer(const sp<IMemory> &mem);


然后在某处调用此函数,例如:

sp<IOMXNode> mOMXNode;
...
sp<IMemory> mem;
...
err = mOMXNode->useBuffer(portIndex, mem, &info.mBufferID);


那么,为什么sp<IMemory>可以直接匹配函数useBuffer()的第二个参数类型const OMXBuffer&?

我能以某种方式将它们放在一起,编译器使用sp<IMemory>并构造一个OMXBuffer,然后匹配useBuffer()第二个参数。

这是属于类型的推论还是其他我不知道的?

最佳答案

重要的行是这些(来自OMXBuffer类):

// Constructs a buffer of type kBufferTypeSharedMem.
OMXBuffer(const sp<IMemory> &mem);


通过converting constructor,可以从OMXBuffer对象创建sp<IMemory>对象。

这意味着真正发生的是

err = mOMXNode->useBuffer(portIndex, OMXBuffer(mem), &info.mBufferID);


即,从OMXBuffer对象创建一个临时mem对象。然后,对该临时OMXBuffer对象的引用将传递给useBuffer函数。一旦useBuffer函数返回,临时的OMXBuffer对象就会被破坏。

关于android - C++参数匹配/推论,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50674282/

10-12 01:36