1

我使用 NPAPI 进行视频流。

但是在 Mac Safari (Mt.Lion, v6.0.2) 中,加载时 CPU 使用率很高 (7~80%)。Chrome 或 FireFox 是常态。

我猜当调用 NPNFuncs.invalidaterect 函数时。

int16_t PLUGINAPI::handleEvent(void* event)
{
    NPCocoaEvent* cocoaEvent = (NPCocoaEvent*)event;
    ScriptablePluginObject* pObject = (ScriptablePluginObject*)m_pScriptableObject;


    if(cocoaEvent->type == NPCocoaEventDrawRect) {
        CGContextRef cgContext = cocoaEvent->data.draw.context;

        if(!cgContext)
            return true;

        //Add rect and translate the video
        CGContextAddRect(cgContext, CGRectMake (0, 0, m_Window->width, m_Window->height));
        CGContextTranslateCTM(cgContext, 0, m_Window->height);
        CGContextScaleCTM(cgContext, 1.0, -1.0);

        //Display the video here
        if(pObject && pObject->m_pNpapiPlugin) 
            pObject->m_pNpapiPlugin->WEBVIEWER_DisplayFrame(cgContext, m_Window->width, m_Window->height);

        //Fulsh cgcontextref
        CGContextFlush(cgContext);

        //Generate DrawRect event
        NPRect rect = {0, 0, m_Window->height, m_Window->width};
        NPNFuncs.invalidaterect(m_pNPInstance, &rect);
        NPNFuncs.forceredraw(m_pNPInstance);

    } else {

        if(pObject && pObject->m_pNpapiPlugin)
            pObject->m_pNpapiPlugin->WEBVIEWER_SendEvent(cocoaEvent);
    }

    return true;
}

插件绘图还有其他方法吗?或者我想要解决这个问题。

4

1 回答 1

1

您是在告诉它尽快重绘!

NPNFuncs.invalidaterect(m_pNPInstance, &rect);
NPNFuncs.forceredraw(m_pNPInstance);

当您调用它时,它将触发另一个绘图事件。Safari 的重绘速度可能比其他浏览器快,这可能就是您使用如此多 CPU 的原因。基本上你说的是“每次你画,马上再画!”。

而不是从你的绘图处理程序调用 invalidateRect 和 forceRedraw (你不应该这样做!)设置一个计时器。请记住,如果您每秒绘制超过 60 帧,您可能会浪费 CPU 周期,因为大多数显示器的刷新速度都那么快。对于大多数情况,我通常建议将 30fps 作为最大值,但那是在你和显卡之间。

于 2012-11-24T15:48:30.640 回答