xref: /aosp_15_r20/external/skia/tools/window/mac/GaneshANGLEWindowContext_mac.mm (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1/*
2 * Copyright 2023 Google LLC
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/mac/GaneshANGLEWindowContext_mac.h"
9
10#include "tools/window/ANGLEWindowContext.h"
11#include "tools/window/mac/MacWindowGLUtils.h"
12#include "tools/window/mac/MacWindowInfo.h"
13
14using skwindow::DisplayParams;
15using skwindow::MacWindowInfo;
16using skwindow::internal::ANGLEWindowContext;
17
18namespace {
19
20class ANGLEWindowContext_mac : public ANGLEWindowContext {
21public:
22    ANGLEWindowContext_mac(const MacWindowInfo&, std::unique_ptr<const DisplayParams>);
23
24protected:
25    EGLDisplay onGetEGLDisplay(
26            PFNEGLGETPLATFORMDISPLAYEXTPROC eglGetPlatformDisplayEXT) const override;
27    NativeWindowType onGetNativeWindow() const override;
28    SkISize onGetSize() const override;
29    int onGetStencilBits() const override;
30
31private:
32    NSView* fMainView;
33};
34
35ANGLEWindowContext_mac::ANGLEWindowContext_mac(const MacWindowInfo& info,
36                                               std::unique_ptr<const DisplayParams> params)
37        : ANGLEWindowContext(std::move(params)), fMainView(info.fMainView) {
38    this->initializeContext();
39}
40
41EGLDisplay ANGLEWindowContext_mac::onGetEGLDisplay(
42        PFNEGLGETPLATFORMDISPLAYEXTPROC eglGetPlatformDisplayEXT) const {
43    static constexpr EGLint kType = EGL_PLATFORM_ANGLE_TYPE_METAL_ANGLE;
44    static constexpr EGLint attribs[] = {EGL_PLATFORM_ANGLE_TYPE_ANGLE, kType, EGL_NONE};
45    return eglGetPlatformDisplayEXT(
46            EGL_PLATFORM_ANGLE_ANGLE, reinterpret_cast<void*>(EGL_DEFAULT_DISPLAY), attribs);
47}
48
49NativeWindowType ANGLEWindowContext_mac::onGetNativeWindow() const {
50    [fMainView setWantsLayer:YES];
51    return fMainView.layer;
52}
53
54int ANGLEWindowContext_mac::onGetStencilBits() const {
55    GLint stencilBits;
56    NSOpenGLPixelFormat* pixelFormat =
57            skwindow::GetGLPixelFormat(fDisplayParams->msaaSampleCount());
58    [pixelFormat getValues:&stencilBits forAttribute:NSOpenGLPFAStencilSize forVirtualScreen:0];
59    return stencilBits;
60}
61
62SkISize ANGLEWindowContext_mac::onGetSize() const {
63    CGFloat backingScaleFactor = skwindow::GetBackingScaleFactor(fMainView);
64    return SkISize::Make(fMainView.bounds.size.width * backingScaleFactor,
65                         fMainView.bounds.size.height * backingScaleFactor);
66}
67
68}  // anonymous namespace
69
70namespace skwindow {
71
72std::unique_ptr<WindowContext> MakeGaneshANGLEForMac(const MacWindowInfo& info,
73                                                     std::unique_ptr<const DisplayParams> params) {
74    std::unique_ptr<WindowContext> ctx(new ANGLEWindowContext_mac(info, std::move(params)));
75    if (!ctx->isValid()) {
76        return nullptr;
77    }
78    return ctx;
79}
80
81}  // namespace skwindow
82