为什么我的位图渲染目标使用这么多内存

问题描述 投票:0回答:1

我通常使用 dc 渲染目标,但我发现有点闪烁,所以我添加了一个位图渲染目标来运行绘图操作,然后在充当双缓冲区的 dc 上绘图 我的内存使用率很高(几乎 60MB)是由于

pDCRenderTarget->CreateCompatibleRenderTarget(&pBitmapRenderTarget);

当我调整窗口大小时,会消耗大量内存。 (请注意第一次发布问题,因为我几乎总是发现已经提出的问题)

我的main.cpp:

#include <windows.h>
#include <d2d1.h>
#pragma comment(lib, "d2d1")

// Global variables
ID2D1Factory* pFactory = nullptr;
ID2D1DCRenderTarget* pDCRenderTarget = nullptr;
ID2D1BitmapRenderTarget* pBitmapRenderTarget = nullptr;

ID2D1Factory* GetFactory() {
    if (pFactory == nullptr) {
        // Create the Direct2D factory
        D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED, &pFactory);
    }
    return pFactory;
}


// Window Procedure
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
    case WM_PAINT:
    {
    

        // Bind the DC render target to the HDC
        RECT rc;
        GetClientRect(hwnd, &rc);
        HDC hdc = GetDC(hwnd);
        pDCRenderTarget->BindDC(hdc, &rc);

        // Create a compatible bitmap render target
        pDCRenderTarget->CreateCompatibleRenderTarget(&pBitmapRenderTarget);

        // Begin drawing on the bitmap render target
        ID2D1Bitmap* pBitmap = nullptr;
        pBitmapRenderTarget->BeginDraw();

        // Clear the background
        pBitmapRenderTarget->Clear(D2D1::ColorF(D2D1::ColorF::White));

        // Draw a rectangle
        ID2D1SolidColorBrush* pBrush = nullptr;
        pBitmapRenderTarget->CreateSolidColorBrush(D2D1::ColorF(D2D1::ColorF::Blue), &pBrush);
        pBitmapRenderTarget->FillRectangle(D2D1::RectF(50, 50, 200, 200), pBrush);

        // End drawing
        pBitmapRenderTarget->EndDraw();

        // Get the bitmap from the render target
        pBitmapRenderTarget->GetBitmap(&pBitmap);

        // Draw the bitmap to the DC render target
        pDCRenderTarget->BeginDraw();
        pDCRenderTarget->DrawBitmap(pBitmap, D2D1::RectF(0, 0, rc.right, rc.bottom)); // Destination rectangle
        pDCRenderTarget->EndDraw();

        // Release resources
        pBrush->Release();
        pBitmap->Release();


        pBitmapRenderTarget->Release();
        pBitmapRenderTarget = nullptr;


        break;
    }
    case WM_DESTROY:
        PostQuitMessage(0);
        pDCRenderTarget->Release();
        pDCRenderTarget = nullptr;
        if (pFactory) pFactory->Release();
        break;

    case WM_CLOSE:

        DestroyWindow(hwnd);
        break;

    default:
        return DefWindowProc(hwnd, message, wParam, lParam);
    }
    return 0;
}

// Main Entry Point
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
    // Register the window class
    const wchar_t CLASS_NAME[] = L"Direct2DDoubleBufferingWindow";

    WNDCLASS wc = {};
    wc.lpfnWndProc = WndProc;
    wc.hInstance = hInstance;
    wc.lpszClassName = CLASS_NAME;

    RegisterClass(&wc);

    // Create the window
    HWND hwnd = CreateWindowEx(
        0,                              // Optional window styles
        CLASS_NAME,                     // Window class
        L"Direct2D Double Buffering",   // Window title
        WS_OVERLAPPEDWINDOW,            // Window style
        CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
        nullptr, nullptr, hInstance, nullptr);

    if (!hwnd)
        return -1;

    // Create a DC render target
    D2D1_RENDER_TARGET_PROPERTIES rtProps = D2D1::RenderTargetProperties(D2D1_RENDER_TARGET_TYPE_DEFAULT, D2D1::PixelFormat(DXGI_FORMAT_B8G8R8A8_UNORM, D2D1_ALPHA_MODE_IGNORE));
    GetFactory()->CreateDCRenderTarget(&rtProps, &pDCRenderTarget);
    ShowWindow(hwnd, nCmdShow);

    // Run the message loop
    MSG msg = {};
    while (GetMessage(&msg, nullptr, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return 0;
}


c++ windows memory-management direct2d
1个回答
0
投票

首先要做的是按照

官方文档
和本 Direct2D 教程使用 Direct2D 绘图中的说明处理 WM_PAINT,即:您需要告诉 Windows 您已处理了
WM_PAINT
消息。

所以你的代码应该像这样:

case WM_PAINT:
{
    // ==> I will handle WM_PAINT
    PAINTSTRUCT ps;
    HDC hdc = BeginPaint(hwnd, &ps);
    
    // Bind the DC render target to the HDC
    RECT rc;
    GetClientRect(hwnd, &rc);

    pDCRenderTarget->BindDC(hdc, &rc);

    // Create a compatible bitmap render target
    pDCRenderTarget->CreateCompatibleRenderTarget(&pBitmapRenderTarget);

    ...

    pBitmapRenderTarget->Release();
    pBitmapRenderTarget = nullptr;

    // ==> now, I have handled WM_PAINT
    EndPaint(hwnd, &ps);
    break;

如果您不这样做,Windows 将继续发送

WM_PAINT
消息,这会消耗您计算机的资源。

然后,根据您的目标,您也可以保留

pBitmapRenderTarget
而不是每次处理 WM_PAINT 时创建它,这实际上取决于您在一般意义上“绘制”时打算做什么(实时更新,很少更改)等)

使用 Direct2D(和 Direct3D 等),另一种方法是在 WM_PAINT 中执行一次 BeginPaint+EndPaint 并在其他地方进行渲染,例如: https://gamedev.stackexchange.com/questions/12901/wm -paint-and-direct3d

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