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



我使用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;
}

有没有另一种方法可以绘制插件? 或者我想解决这个问题。

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

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

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

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

最新更新