High CPU load if offscreen-rendering is enabled

Having problems with building or using CEF's C/C++ APIs? This forum is here to help. Please do not post bug reports or feature requests here.

High CPU load if offscreen-rendering is enabled

Postby Jusonex » Sun Apr 27, 2014 2:35 pm

Hi,
I'm trying around with offscreen-rendering and noticed that one CPU core/thread is almost working to capacity for some sites.

You can reproduce it via:
1.)
Code: Select all
cefclient.exe --off-screen-rendering-enabled

2.) URL: http://nyan.cat

Could it be that the frames per second are not limited? If so, is there a direct way to limit it somehow?

Regards
Jusonex
Techie
 
Posts: 13
Joined: Sun Apr 27, 2014 2:17 pm

Re: High CPU load if offscreen-rendering is enabled

Postby magreenblatt » Sun Apr 27, 2014 5:17 pm

Software rendering does not use the GPU so higher CPU usage is expected. Which process is maxing out the core? You can also add your own code (or use a profiler) to test the frequency at which OnPaint is being called. What OS and CEF version are you testing with?
magreenblatt
Site Admin
 
Posts: 12409
Joined: Fri May 29, 2009 6:57 pm

Re: High CPU load if offscreen-rendering is enabled

Postby Jusonex » Mon Apr 28, 2014 5:09 am

EDIT: Nervermind, the release build works perfectly. I was just confused by the cefclient which caused such high CPU loads. Thanks once more for your great support!


Which process is maxing out the core?

The process that calls the message loop.
I attached a debugger an paused the test application to get a stack trace:
Image

You can also add your own code

A piece of hacky code:
Code: Select all
#include <Windows.h>
#include <iostream>
#include <d3d9.h>
#include <D3dx9tex.h>
#include "include/cef_app.h"
#include "include/cef_client.h"
#include "include/cef_render_handler.h"

LPDIRECT3DTEXTURE9 pTexture = NULL;
LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    if (message == WM_DESTROY)
    {
        PostQuitMessage(0);
        return 0;
    }
    return DefWindowProc(hWnd, message, wParam, lParam);
}

unsigned int lastTick = 0;
class Client : public CefClient, public CefRenderHandler
{
public:
    virtual CefRefPtr<CefRenderHandler> GetRenderHandler() override
    {
        return this;
    }
    virtual bool GetViewRect(CefRefPtr<CefBrowser> browser, CefRect& rect) override
    {
        rect.x = 0;
        rect.y = 0;
        rect.width = 700;
        rect.height = 450;
        return true;
    }
    virtual void OnPaint(CefRefPtr<CefBrowser> browser, CefRenderHandler::PaintElementType paintType, const CefRenderHandler::RectList& dirtyRects, const void* buffer, int width, int height) override
    {
        D3DLOCKED_RECT LockedRect;
        D3DSURFACE_DESC SurfaceDesc;
        IDirect3DSurface9* pSurface;
        pTexture->GetSurfaceLevel(0, &pSurface);

        pSurface->GetDesc(&SurfaceDesc);
        pSurface->LockRect(&LockedRect, NULL, 0);
        memcpy(LockedRect.pBits, buffer, width * height * 4);
        pSurface->UnlockRect();

        unsigned int diffTime = GetTickCount() - lastTick;
        lastTick = GetTickCount();
    }

    IMPLEMENT_REFCOUNTING(Client);
};

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
    WNDCLASSEX wc;
    ZeroMemory(&wc, sizeof(WNDCLASSEX));
    wc.cbSize = sizeof(WNDCLASSEX);
    wc.style = CS_HREDRAW | CS_VREDRAW;
    wc.lpfnWndProc = WindowProc;
    wc.hInstance = hInstance;
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
    wc.lpszClassName = "WindowClass";
    RegisterClassEx(&wc);
    HWND hWnd = CreateWindowEx(NULL, "WindowClass", "Tests", WS_OVERLAPPEDWINDOW, 300, 300, 800, 600, NULL, NULL, hInstance, NULL);
    ShowWindow(hWnd, nCmdShow);

    LPDIRECT3D9 d3d = Direct3DCreate9(D3D_SDK_VERSION);
    D3DPRESENT_PARAMETERS d3dpp;
    ZeroMemory(&d3dpp, sizeof(d3dpp));
    d3dpp.Windowed = TRUE;
    d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
    d3dpp.hDeviceWindow = hWnd;
    LPDIRECT3DDEVICE9 d3ddev;
    d3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &d3ddev);

    LPD3DXSPRITE pSprite;
    D3DXCreateTexture(d3ddev, 700, 450, 0, 0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, &pTexture);
    D3DXCreateSprite(d3ddev, &pSprite);

    CefMainArgs args;
    CefSettings settings;
    Client client;
    settings.no_sandbox = true;
    CefExecuteProcess(args, NULL, NULL);
    CefInitialize(args, settings, NULL, NULL);

    CefBrowserSettings browserSettings;
    CefWindowInfo windowInfo;
    windowInfo.SetAsOffScreen(nullptr);
    CefRefPtr<CefBrowser> pBrowser = CefBrowserHost::CreateBrowserSync(windowInfo, &client, "http://nyan.cat", browserSettings, NULL);
   
    MSG msg;
    for (;;)
    {
        while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }

        if (msg.message == WM_QUIT)
            break;
       
        // Update CEF
        CefDoMessageLoopWork();

        // Render and display the frame
        d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 40, 100), 1.0f, 0);
        d3ddev->BeginScene();
        pSprite->Begin(D3DXSPRITE_ALPHABLEND);
        D3DXVECTOR3 vPosition(50.0f, 50.0f, 0.0f);
        D3DCOLOR Color = 0xFFFFFFFF;
        HRESULT hr = pSprite->Draw(pTexture, NULL, NULL, &vPosition, Color);
        pSprite->End();
        d3ddev->EndScene();
        d3ddev->Present(NULL, NULL, NULL, NULL);
       
        // Wait a short moment
        Sleep(100);
    }
   
    CefShutdown();
    d3ddev->Release();
    d3d->Release();

    return true;
}

"diffTime" was always around 100ms for me.
I also increased the time to sleep between the frames, but that didn't change anything since the load is caused by the CEF process.

What OS and CEF version are you testing with?

Windows 8.1 x64 and CEF 3.1750.1658 (x86)
Jusonex
Techie
 
Posts: 13
Joined: Sun Apr 27, 2014 2:17 pm

Re: High CPU load if offscreen-rendering is enabled

Postby amitkanfer » Wed Aug 27, 2014 3:13 am

Jusonex,
Did you find a reason for the high CPU usage?
i'm facing the same issue now...

thanks,
Amit
amitkanfer
Techie
 
Posts: 31
Joined: Mon Jul 21, 2014 4:10 am

Re: High CPU load if offscreen-rendering is enabled

Postby Jusonex » Wed Aug 27, 2014 10:06 am

EDIT: Nervermind, the release build works perfectly. I was just confused by the cefclient which caused such high CPU loads.
Jusonex
Techie
 
Posts: 13
Joined: Sun Apr 27, 2014 2:17 pm


Return to Support Forum

Who is online

Users browsing this forum: Devyre, Google [Bot] and 93 guests