Repository: google/filament
File: filament/backend/src/webgpu/WebGPUSwapChain.cpp (C++)

Code before:
WebGPUSwapChain::WebGPUSwapChain(wgpu::Surface&& surface, wgpu::Extent2D const& surfaceSize,
        wgpu::Adapter& adapter, wgpu::Device& device, uint64_t flags)
    : mSurface(surface) {
    wgpu::SurfaceCapabilities capabilities = {};
    if (!mSurface.GetCapabilities(adapter, &capabilities)) {
        FWGPU_LOGW << "Failed to get WebGPU surface capabilities" << utils::io::endl;
    } else {
#if FWGPU_ENABLED(FWGPU_PRINT_SYSTEM)
        printSurfaceCapabilitiesDetails(capabilities);
#endif
    }
    const bool useSRGBColorSpace = (flags & SWAP_CHAIN_CONFIG_SRGB_COLORSPACE) != 0;
    initConfig(mConfig, device, capabilities, surfaceSize, useSRGBColorSpace);
    mSurface.Configure(&mConfig);
}

WebGPUSwapChain::~WebGPUSwapChain() {
    mSurface.Unconfigure();
}

void WebGPUSwapChain::setExtent(wgpu::Extent2D const& currentSurfaceSize) {
    if (currentSurfaceSize.width < 1 || currentSurfaceSize.height < 1) {
        PANIC_LOG("WebGPUSwapChain::getNextSurfaceTextureView: Invalid width and/or height "
                  "requested.");
        return;
    }
    if (mConfig.width != currentSurfaceSize.width || mConfig.height != currentSurfaceSize.height) {
        mConfig.width = currentSurfaceSize.width;
        mConfig.height = currentSurfaceSize.height;
#if FWGPU_ENABLED(FWGPU_PRINT_SYSTEM)
        printSurfaceConfiguration(mConfig);
#endif
        FWGPU_LOGD << "Resizing to width " << mConfig.width << " height " << mConfig.height
                   << utils::io::endl;
        mSurface.Configure(&mConfig);
    }
}

wgpu::TextureView WebGPUSwapChain::getCurrentSurfaceTextureView(
        wgpu::Extent2D const& currentSurfaceSize) {
    setExtent(currentSurfaceSize);
    wgpu::SurfaceTexture surfaceTexture;
    mSurface.GetCurrentTexture(&surfaceTexture);
    if (surfaceTexture.status != wgpu::SurfaceGetCurrentTextureStatus::SuccessOptimal) {
        return nullptr;
    }
    // Create a view for this surface texture
    // TODO: review these initiliazations as webgpu pipeline gets mature
    wgpu::TextureViewDescriptor textureViewDescriptor = {
        .label = "texture_view",
        .format = surfaceTexture.texture.GetFormat(),

Review comment (suggestion):
should we change this to something like `FILAMENT_CHECK_POSTCONDITION` 

```
    FILAMENT_CHECK_POSTCONDITION(currentSurfaceSize.width < 1 || currentSurfaceSize.height < 1)
            << "WebGPUSwapChain::getNextSurfaceTextureView: Invalid width and/or height requested. ";
```

Code after:
WebGPUSwapChain::WebGPUSwapChain(wgpu::Surface&& surface, wgpu::Extent2D const& surfaceSize,
        wgpu::Adapter& adapter, wgpu::Device& device, uint64_t flags)
    : mSurface(surface) {
    wgpu::SurfaceCapabilities capabilities = {};
    if (!mSurface.GetCapabilities(adapter, &capabilities)) {
        FWGPU_LOGW << "Failed to get WebGPU surface capabilities" << utils::io::endl;
    } else {
#if FWGPU_ENABLED(FWGPU_PRINT_SYSTEM)
        printSurfaceCapabilitiesDetails(capabilities);
#endif
    }
    const bool useSRGBColorSpace = (flags & SWAP_CHAIN_CONFIG_SRGB_COLORSPACE) != 0;
    initConfig(mConfig, device, capabilities, surfaceSize, useSRGBColorSpace);
    mSurface.Configure(&mConfig);
}

WebGPUSwapChain::~WebGPUSwapChain() {
    mSurface.Unconfigure();
}

void WebGPUSwapChain::setExtent(wgpu::Extent2D const& currentSurfaceSize) {
    FILAMENT_CHECK_POSTCONDITION(currentSurfaceSize.width > 0 || currentSurfaceSize.height > 0)
            << "WebGPUSwapChain::setExtent: Invalid width " << currentSurfaceSize.width
            << " and/or height " << currentSurfaceSize.height << " requested.";
    if (mConfig.width != currentSurfaceSize.width || mConfig.height != currentSurfaceSize.height) {
        mConfig.width = currentSurfaceSize.width;
        mConfig.height = currentSurfaceSize.height;
#if FWGPU_ENABLED(FWGPU_PRINT_SYSTEM)
        printSurfaceConfiguration(mConfig);
#endif
        FWGPU_LOGD << "Resizing to width " << mConfig.width << " height " << mConfig.height
                   << utils::io::endl;
        // TODO we may need to ensure no surface texture is flight when we do this. some
        //      synchronization may be necessary
        mSurface.Configure(&mConfig);
    }
}

wgpu::TextureView WebGPUSwapChain::getCurrentSurfaceTextureView(
        wgpu::Extent2D const& currentSurfaceSize) {
    setExtent(currentSurfaceSize);
    wgpu::SurfaceTexture surfaceTexture;
    mSurface.GetCurrentTexture(&surfaceTexture);
    if (surfaceTexture.status != wgpu::SurfaceGetCurrentTextureStatus::SuccessOptimal) {
        return nullptr;
    }
    // Create a view for this surface texture
    // TODO: review these initiliazations as webgpu pipeline gets mature
    wgpu::TextureViewDescriptor textureViewDescriptor = {
        .label = "texture_view",
        .format = surfaceTexture.texture.GetFormat(),