NPAPI插件在Mac Safari中具有很高的CPU使用率

问题描述 投票:1回答: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;
}

是否有另一种插件绘制方式?或者我想解决这个问题。

npapi
1个回答
1
投票

你告诉它尽可能快地重绘!

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

当你调用它时,它将触发另一个绘制事件。 Safari可能比其他浏览器重绘得更快,这可能就是你使用这么多CPU的原因。基本上你所说的是“每次画画,立即画画!”。

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

© www.soinside.com 2019 - 2024. All rights reserved.