windows text is not sharp in less font size

43 views
Skip to first unread message

Hemanth

unread,
Nov 11, 2024, 7:16:06 AMNov 11
to skia-discuss
hi team,
we render text in windows app using paragraph builder , text style and font collection
where the text looks blurred after rendering
will share the sample code:
Device resources creation: 
        DXGI_SWAP_CHAIN_DESC1 swapChainDesc = {};
        swapChainDesc.BufferCount = 2;
        swapChainDesc.Width = width;
        swapChainDesc.Height = height;
        swapChainDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
        swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
        swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_FLIP_DISCARD;
        swapChainDesc.SampleDesc.Count = 1;

        UINT dxgiFactoryFlags = 0;

#if defined(_DEBUG)
        // Enable the debug layer (requires the Graphics Tools "optional feature").
        // NOTE: Enabling the debug layer after device creation will invalidate the active device.
        {
            ComPtr<ID3D12Debug> debugController;
            if (SUCCEEDED(D3D12GetDebugInterface(IID_PPV_ARGS(&debugController))))
            {
                debugController->EnableDebugLayer();
                dxgiFactoryFlags |= DXGI_CREATE_FACTORY_DEBUG;
            }
        }
#endif

        gr_cp<IDXGIFactory4> factory;
        HRESULT hr = CreateDXGIFactory2(dxgiFactoryFlags, IID_PPV_ARGS(&factory));
        if (FAILED(hr))
        {
            throw winrt::hresult_error(hr);
        }

        //get_hardware_adapter(factory.get(), &hardwareAdapter);
        hr = factory->EnumWarpAdapter(IID_PPV_ARGS(&hardwareAdapter));
        //SkDebugf("hardware in constructor - %p\n", hardwareAdapter);
        // Create the Direct3D 12 API device object
        hr = D3D12CreateDevice(
            hardwareAdapter.get(),
            D3D_FEATURE_LEVEL_11_0,
            IID_PPV_ARGS(&d3dDevice)
        );
        //d3dDevice->AddRef();

        if (FAILED(hr))
        {
            throw winrt::hresult_error(hr);
        }

        // Describe and create the command queue
        D3D12_COMMAND_QUEUE_DESC queueDesc = {};
        queueDesc.Flags = D3D12_COMMAND_QUEUE_FLAG_NONE;
        queueDesc.Type = D3D12_COMMAND_LIST_TYPE_DIRECT;

        hr = d3dDevice->CreateCommandQueue(&queueDesc, IID_PPV_ARGS(&commandQueue));
        if (FAILED(hr))
        {
            throw winrt::hresult_error(hr);
        }

        // Create command allocator
        hr = d3dDevice->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, IID_PPV_ARGS(&commandAllocator));
        if (FAILED(hr))
        {
            throw winrt::hresult_error(hr, L"Failed to create command allocator");
        }

        // Create the command list
        hr = d3dDevice->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, commandAllocator.get(), nullptr, IID_PPV_ARGS(&commandList));
        if (FAILED(hr))
        {
            throw winrt::hresult_error(hr, L"Failed to create command list");
        }

        // Command lists are created in the recording state. Since there is nothing to record right now, close the command list.
        hr = commandList->Close();
        if (FAILED(hr))
        {
            throw winrt::hresult_error(hr, L"Failed to close command list");
        }

        gr_cp<IDXGISwapChain1> swapChain1;
        hr = factory->CreateSwapChainForComposition(
            commandQueue.get(),        // Swap chain needs the queue so that it can force a flush on it
            &swapChainDesc,
            nullptr,
            &swapChain1
        );

        if (FAILED(hr))
        {
            throw winrt::hresult_error(hr);
        }

        hr = swapChain1->QueryInterface(IID_PPV_ARGS(&swapChain));
        if (FAILED(hr))
        {
            throw winrt::hresult_error(hr);
        }
        swapChain1.release();
        //swapChain1 = nullptr;

        // Create descriptor heaps.
        {
            D3D12_DESCRIPTOR_HEAP_DESC rtvHeapDesc = {};
            rtvHeapDesc.NumDescriptors = 2;
            rtvHeapDesc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_RTV;
            rtvHeapDesc.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_NONE;
            hr = d3dDevice->CreateDescriptorHeap(&rtvHeapDesc, IID_PPV_ARGS(&rtvHeap));
            if (FAILED(hr))
            {
                throw winrt::hresult_error(hr);
            }

            rtvDescriptorSize = d3dDevice->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_RTV);
        }

        // Create frame resources.
        {
            CD3DX12_CPU_DESCRIPTOR_HANDLE rtvHandle(rtvHeap->GetCPUDescriptorHandleForHeapStart());

            // Create a RTV for each frame.
            for (UINT n = 0; n < 2; n++)
            {
                hr = swapChain->GetBuffer(n, IID_PPV_ARGS(&renderTargets[n]));
                if (FAILED(hr))
                {
                    throw winrt::hresult_error(hr);
                }
                d3dDevice->CreateRenderTargetView(renderTargets[n].get(), nullptr, rtvHandle);
                rtvHandle.Offset(1, rtvDescriptorSize);
            }

            frameIndex = swapChain->GetCurrentBackBufferIndex();
        }


        panelNative = swapchainPanel.as<ISwapChainPanelNative>();
        check_hresult(panelNative->SetSwapChain(swapChain.get()));
        swapChain->GetBuffer(frameIndex, IID_PPV_ARGS(&renderTargets[frameIndex]));

        backendContext.fAdapter = hardwareAdapter;
        backendContext.fDevice = d3dDevice;
        backendContext.fQueue = commandQueue;
        backendContext.fProtectedContext = GrProtected::kNo;

        fContext = GrDirectContext::MakeDirect3D(backendContext);
  D3D12_RESOURCE_DESC textureDesc = {};
  textureDesc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D;
  textureDesc.Width = width;
  textureDesc.Height = height;
  textureDesc.DepthOrArraySize = 1;
  textureDesc.MipLevels = 1;
  textureDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
  textureDesc.SampleDesc.Count = 1;
  textureDesc.Flags = D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET;

  D3D12_HEAP_PROPERTIES heapProps = CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_DEFAULT);
  d3dDevice->CreateCommittedResource(&heapProps, D3D12_HEAP_FLAG_NONE, &textureDesc,
      D3D12_RESOURCE_STATE_COMMON, nullptr, IID_PPV_ARGS(&renderTargets[frameIndex]));
  // Create Skia surface
  info.fResource = renderTargets[frameIndex];
  info.fResourceState = D3D12_RESOURCE_STATE_COMMON;
  info.fFormat = DXGI_FORMAT_R8G8B8A8_UNORM;


and our rendering code as follows:

  frameIndex = swapChain->GetCurrentBackBufferIndex();
  commandAllocator->Reset();
  commandList->Reset(commandAllocator.get(), nullptr);

  D3D12_RESOURCE_BARRIER barrier = CD3DX12_RESOURCE_BARRIER::Transition(renderTargets[frameIndex].get(), D3D12_RESOURCE_STATE_PRESENT, D3D12_RESOURCE_STATE_RENDER_TARGET);
  commandList->ResourceBarrier(1, &barrier);

  // Get the Skia canvas and draw
  swapChain->GetBuffer(frameIndex, IID_PPV_ARGS(&renderTargets[frameIndex]));
  info.fResource = renderTargets[frameIndex];
  GrBackendTexture backendTexture(width, height, info);
  sk_sp<SkColorSpace> space = SkColorSpace::MakeSRGB();

  SkSurfaceProps surfaceProps(
      SkSurfaceProps::kUseDeviceIndependentFonts_Flag,
      SkPixelGeometry::kRGB_H_SkPixelGeometry
  );
  GrBackendRenderTarget backendRT(width, height, info);
  skSurface = SkSurface::MakeFromBackendRenderTarget(
      fContext.get(),
      backendRT,
      kTopLeft_GrSurfaceOrigin,
      kRGBA_8888_SkColorType,nullptr,
      &surfaceProps
  );

            SkCanvas* canvas = skSurface->getCanvas();
            canvas->drawColor(SK_ColorWHITE);
            auto fontSize = 30 * 1.33;// for converting points to pixel
            SkPaint paint;
            paint.setAntiAlias(false);
            paint.setColor(SK_ColorBLACK);
            //SkFontStyle fontStyle;
            skia::textlayout::TextStyle textStyle;
            SkFontStyle fontStyle = SkFontStyle::Normal();
            textStyle.setFontSize(fontSize);
            textStyle.setForegroundColor(paint);
            textStyle.setFontStyle(fontStyle);

            textStyle.setFontFamilies({ SkString("Segoe UI") });
            skia::textlayout::ParagraphStyle paraStyle;
            paraStyle.setMaxLines(1);
            paraStyle.setTextAlign(skia::textlayout::TextAlign::kLeft);
            auto builder = skia::textlayout::ParagraphBuilderImpl::make(paraStyle, ZSKSkiaRendererUtilities::fontCollection);
            builder->pushStyle(textStyle);
            std::string text = "cellText";
            builder->addText(text.c_str(), text.length());
            auto para = builder->Build();
            para.get()->layout(400);
            canvas->save();
            para.get()->paint(canvas, 100 + 1, 100);
            canvas->restore();



            auto fontSize1 = 10 * 1.33;// for converting points to pixel
           
            skia::textlayout::TextStyle textStyle1;
            textStyle1.setFontSize(fontSize1);
            textStyle1.setForegroundColor(paint);
            textStyle1.setFontStyle(fontStyle);

            textStyle1.setFontFamilies({ SkString("Segoe UI") });
            skia::textlayout::ParagraphStyle paraStyle1;
            paraStyle1.setMaxLines(1);
            paraStyle1.setTextAlign(skia::textlayout::TextAlign::kLeft);
            auto builder1 = skia::textlayout::ParagraphBuilderImpl::make(paraStyle1, ZSKSkiaRendererUtilities::fontCollection);
            builder1->pushStyle(textStyle1);
            std::string text1 = "cellText";
            builder1->addText(text1.c_str(), text1.length());
            auto para1 = builder1->Build();
            para1.get()->layout(400);
            canvas->save();
            para1.get()->paint(canvas, 500 + 1,500);
            canvas->restore();


            // Transition back to PRESENT state
            barrier = CD3DX12_RESOURCE_BARRIER::Transition(renderTargets[frameIndex].get(), D3D12_RESOURCE_STATE_RENDER_TARGET, D3D12_RESOURCE_STATE_PRESENT);
            commandList->ResourceBarrier(1, &barrier);

            // Execute command list and present
            commandList->Close();
            ID3D12CommandList* ppCommandLists[] = { commandList.get() };
            commandQueue->ExecuteCommandLists(_countof(ppCommandLists), ppCommandLists);

            swapChain->Present(1, 0);

for this the rendered view is 

Screenshot 2024-11-11 161658.png

small text is not sharp comparing to large size text
why that so can you explain this behaviour

Sowmyyy Reanz

unread,
Nov 19, 2024, 9:35:52 AMNov 19
to skia-discuss
Team, can anyone help with this ?
Reply all
Reply to author
Forward
0 new messages