1 //
2 // Copyright 2024 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5 //
6 // WindowSurfaceVkWin32.cpp:
7 // Defines the class interface for WindowSurfaceWgpuWin32, implementing WindowSurfaceWgpu.
8 //
9
10 #include "libANGLE/renderer/wgpu/win32/WindowSurfaceWgpuWin32.h"
11
12 #include "libANGLE/Display.h"
13 #include "libANGLE/renderer/wgpu/DisplayWgpu.h"
14 #include "libANGLE/renderer/wgpu/wgpu_utils.h"
15
16 namespace rx
17 {
WindowSurfaceWgpuWin32(const egl::SurfaceState & surfaceState,EGLNativeWindowType window)18 WindowSurfaceWgpuWin32::WindowSurfaceWgpuWin32(const egl::SurfaceState &surfaceState,
19 EGLNativeWindowType window)
20 : WindowSurfaceWgpu(surfaceState, window)
21 {}
22
createWgpuSurface(const egl::Display * display,wgpu::Surface * outSurface)23 angle::Result WindowSurfaceWgpuWin32::createWgpuSurface(const egl::Display *display,
24 wgpu::Surface *outSurface)
25 {
26 DisplayWgpu *displayWgpu = webgpu::GetImpl(display);
27 auto &surfaceCache = displayWgpu->getSurfaceCache();
28
29 EGLNativeWindowType window = getNativeWindow();
30 auto cachedSurfaceIter = surfaceCache.find(window);
31 if (cachedSurfaceIter != surfaceCache.end())
32 {
33 *outSurface = cachedSurfaceIter->second;
34 return angle::Result::Continue;
35 }
36
37 wgpu::Instance instance = displayWgpu->getInstance();
38
39 wgpu::SurfaceDescriptorFromWindowsHWND hwndDesc;
40 hwndDesc.hinstance = GetModuleHandle(nullptr);
41 hwndDesc.hwnd = window;
42
43 wgpu::SurfaceDescriptor surfaceDesc;
44 surfaceDesc.nextInChain = &hwndDesc;
45
46 wgpu::Surface surface = instance.CreateSurface(&surfaceDesc);
47 *outSurface = surface;
48
49 surfaceCache.insert_or_assign(window, surface);
50 return angle::Result::Continue;
51 }
52
getCurrentWindowSize(const egl::Display * display,gl::Extents * outSize)53 angle::Result WindowSurfaceWgpuWin32::getCurrentWindowSize(const egl::Display *display,
54 gl::Extents *outSize)
55 {
56 RECT rect;
57 if (!GetClientRect(getNativeWindow(), &rect))
58 {
59 // TODO: generate a proper error + msg
60 return angle::Result::Stop;
61 }
62
63 *outSize = gl::Extents(rect.right - rect.left, rect.bottom - rect.top, 1);
64 return angle::Result::Continue;
65 }
66
CreateWgpuWindowSurface(const egl::SurfaceState & surfaceState,EGLNativeWindowType window)67 WindowSurfaceWgpu *CreateWgpuWindowSurface(const egl::SurfaceState &surfaceState,
68 EGLNativeWindowType window)
69 {
70 return new WindowSurfaceWgpuWin32(surfaceState, window);
71 }
72 } // namespace rx
73