xref: /aosp_15_r20/external/skia/tools/window/win/ANGLEWindowContext_win.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2015 Google Inc.
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #include "tools/window/ANGLEWindowContext.h"
9 #include "tools/window/win/WindowContextFactory_win.h"
10 
11 using skwindow::DisplayParams;
12 using skwindow::internal::ANGLEWindowContext;
13 
14 namespace {
15 
16 class ANGLEWindowContext_win : public ANGLEWindowContext {
17 public:
18     ANGLEWindowContext_win(HWND, std::unique_ptr<const DisplayParams>);
19 
20 protected:
21     EGLDisplay onGetEGLDisplay(
22             PFNEGLGETPLATFORMDISPLAYEXTPROC eglGetPlatformDisplayEXT) const override;
23     NativeWindowType onGetNativeWindow() const override;
24     SkISize onGetSize() const override;
25     int onGetStencilBits() const override;
26 
27 private:
28     HWND fHWND;
29     HDC fHDC;
30 };
31 
ANGLEWindowContext_win(HWND wnd,std::unique_ptr<const DisplayParams> params)32 ANGLEWindowContext_win::ANGLEWindowContext_win(HWND wnd,
33                                                std::unique_ptr<const DisplayParams> params)
34         : ANGLEWindowContext(std::move(params)), fHWND(wnd), fHDC(GetDC(fHWND)) {
35     this->initializeContext();
36 }
37 
onGetEGLDisplay(PFNEGLGETPLATFORMDISPLAYEXTPROC eglGetPlatformDisplayEXT) const38 EGLDisplay ANGLEWindowContext_win::onGetEGLDisplay(
39         PFNEGLGETPLATFORMDISPLAYEXTPROC eglGetPlatformDisplayEXT) const {
40     // We currently only support D3D11 ANGLE.
41     static constexpr EGLint kType = EGL_PLATFORM_ANGLE_TYPE_D3D11_ANGLE;
42     static constexpr EGLint attribs[] = {EGL_PLATFORM_ANGLE_TYPE_ANGLE, kType, EGL_NONE};
43     return eglGetPlatformDisplayEXT(EGL_PLATFORM_ANGLE_ANGLE, fHDC, attribs);
44 }
45 
onGetNativeWindow() const46 NativeWindowType ANGLEWindowContext_win::onGetNativeWindow() const { return fHWND; }
47 
onGetStencilBits() const48 int ANGLEWindowContext_win::onGetStencilBits() const {
49     // use DescribePixelFormat to get the stencil depth.
50     int pixelFormat = GetPixelFormat(fHDC);
51     PIXELFORMATDESCRIPTOR pfd;
52     DescribePixelFormat(fHDC, pixelFormat, sizeof(pfd), &pfd);
53     return pfd.cStencilBits;
54 }
55 
onGetSize() const56 SkISize ANGLEWindowContext_win::onGetSize() const {
57     RECT rect;
58     GetClientRect(fHWND, &rect);
59     return SkISize::Make(rect.right - rect.left, rect.bottom - rect.top);
60 }
61 
62 }  // anonymous namespace
63 
64 namespace skwindow {
65 
MakeANGLEForWin(HWND wnd,std::unique_ptr<const DisplayParams> params)66 std::unique_ptr<WindowContext> MakeANGLEForWin(HWND wnd,
67                                                std::unique_ptr<const DisplayParams> params) {
68     std::unique_ptr<WindowContext> ctx(new ANGLEWindowContext_win(wnd, std::move(params)));
69     if (!ctx->isValid()) {
70         return nullptr;
71     }
72     return ctx;
73 }
74 
75 }  // namespace skwindow
76