NPAPI插件在Mac上有很高的CPU使用率safari

NPAPI插件在Mac上有很高的CPU使用率safari

问题描述:

我使用NPAPI进行视频流传输。NPAPI插件在Mac上有很高的CPU使用率safari

但在Mac Safari(Mt.Lion,v6.0.2)中,加载时CPU占用率高(7〜80%)。 Chrome或FF是正常的。

我想在调用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; 
} 

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

您正在告诉它重绘的速度尽可能快!

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

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

而不是从你的绘图处理程序(你永远不应该这么做)中调用invalidateRect和forceRedraw来设置一个定时器。请记住,如果您每秒绘制超过60帧,则可能会浪费CPU周期,因为大多数显示器只会更新得快。我通常推荐30fps作为大多数事情的最大值,但是这与你和视频卡之间的差距很大。