xref: /aosp_15_r20/external/skia/tools/window/mac/GaneshANGLEWindowContext_mac.mm (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1*c8dee2aaSAndroid Build Coastguard Worker/*
2*c8dee2aaSAndroid Build Coastguard Worker * Copyright 2023 Google LLC
3*c8dee2aaSAndroid Build Coastguard Worker *
4*c8dee2aaSAndroid Build Coastguard Worker * Use of this source code is governed by a BSD-style license that can be
5*c8dee2aaSAndroid Build Coastguard Worker * found in the LICENSE file.
6*c8dee2aaSAndroid Build Coastguard Worker */
7*c8dee2aaSAndroid Build Coastguard Worker
8*c8dee2aaSAndroid Build Coastguard Worker#include "tools/window/mac/GaneshANGLEWindowContext_mac.h"
9*c8dee2aaSAndroid Build Coastguard Worker
10*c8dee2aaSAndroid Build Coastguard Worker#include "tools/window/ANGLEWindowContext.h"
11*c8dee2aaSAndroid Build Coastguard Worker#include "tools/window/mac/MacWindowGLUtils.h"
12*c8dee2aaSAndroid Build Coastguard Worker#include "tools/window/mac/MacWindowInfo.h"
13*c8dee2aaSAndroid Build Coastguard Worker
14*c8dee2aaSAndroid Build Coastguard Workerusing skwindow::DisplayParams;
15*c8dee2aaSAndroid Build Coastguard Workerusing skwindow::MacWindowInfo;
16*c8dee2aaSAndroid Build Coastguard Workerusing skwindow::internal::ANGLEWindowContext;
17*c8dee2aaSAndroid Build Coastguard Worker
18*c8dee2aaSAndroid Build Coastguard Workernamespace {
19*c8dee2aaSAndroid Build Coastguard Worker
20*c8dee2aaSAndroid Build Coastguard Workerclass ANGLEWindowContext_mac : public ANGLEWindowContext {
21*c8dee2aaSAndroid Build Coastguard Workerpublic:
22*c8dee2aaSAndroid Build Coastguard Worker    ANGLEWindowContext_mac(const MacWindowInfo&, std::unique_ptr<const DisplayParams>);
23*c8dee2aaSAndroid Build Coastguard Worker
24*c8dee2aaSAndroid Build Coastguard Workerprotected:
25*c8dee2aaSAndroid Build Coastguard Worker    EGLDisplay onGetEGLDisplay(
26*c8dee2aaSAndroid Build Coastguard Worker            PFNEGLGETPLATFORMDISPLAYEXTPROC eglGetPlatformDisplayEXT) const override;
27*c8dee2aaSAndroid Build Coastguard Worker    NativeWindowType onGetNativeWindow() const override;
28*c8dee2aaSAndroid Build Coastguard Worker    SkISize onGetSize() const override;
29*c8dee2aaSAndroid Build Coastguard Worker    int onGetStencilBits() const override;
30*c8dee2aaSAndroid Build Coastguard Worker
31*c8dee2aaSAndroid Build Coastguard Workerprivate:
32*c8dee2aaSAndroid Build Coastguard Worker    NSView* fMainView;
33*c8dee2aaSAndroid Build Coastguard Worker};
34*c8dee2aaSAndroid Build Coastguard Worker
35*c8dee2aaSAndroid Build Coastguard WorkerANGLEWindowContext_mac::ANGLEWindowContext_mac(const MacWindowInfo& info,
36*c8dee2aaSAndroid Build Coastguard Worker                                               std::unique_ptr<const DisplayParams> params)
37*c8dee2aaSAndroid Build Coastguard Worker        : ANGLEWindowContext(std::move(params)), fMainView(info.fMainView) {
38*c8dee2aaSAndroid Build Coastguard Worker    this->initializeContext();
39*c8dee2aaSAndroid Build Coastguard Worker}
40*c8dee2aaSAndroid Build Coastguard Worker
41*c8dee2aaSAndroid Build Coastguard WorkerEGLDisplay ANGLEWindowContext_mac::onGetEGLDisplay(
42*c8dee2aaSAndroid Build Coastguard Worker        PFNEGLGETPLATFORMDISPLAYEXTPROC eglGetPlatformDisplayEXT) const {
43*c8dee2aaSAndroid Build Coastguard Worker    static constexpr EGLint kType = EGL_PLATFORM_ANGLE_TYPE_METAL_ANGLE;
44*c8dee2aaSAndroid Build Coastguard Worker    static constexpr EGLint attribs[] = {EGL_PLATFORM_ANGLE_TYPE_ANGLE, kType, EGL_NONE};
45*c8dee2aaSAndroid Build Coastguard Worker    return eglGetPlatformDisplayEXT(
46*c8dee2aaSAndroid Build Coastguard Worker            EGL_PLATFORM_ANGLE_ANGLE, reinterpret_cast<void*>(EGL_DEFAULT_DISPLAY), attribs);
47*c8dee2aaSAndroid Build Coastguard Worker}
48*c8dee2aaSAndroid Build Coastguard Worker
49*c8dee2aaSAndroid Build Coastguard WorkerNativeWindowType ANGLEWindowContext_mac::onGetNativeWindow() const {
50*c8dee2aaSAndroid Build Coastguard Worker    [fMainView setWantsLayer:YES];
51*c8dee2aaSAndroid Build Coastguard Worker    return fMainView.layer;
52*c8dee2aaSAndroid Build Coastguard Worker}
53*c8dee2aaSAndroid Build Coastguard Worker
54*c8dee2aaSAndroid Build Coastguard Workerint ANGLEWindowContext_mac::onGetStencilBits() const {
55*c8dee2aaSAndroid Build Coastguard Worker    GLint stencilBits;
56*c8dee2aaSAndroid Build Coastguard Worker    NSOpenGLPixelFormat* pixelFormat =
57*c8dee2aaSAndroid Build Coastguard Worker            skwindow::GetGLPixelFormat(fDisplayParams->msaaSampleCount());
58*c8dee2aaSAndroid Build Coastguard Worker    [pixelFormat getValues:&stencilBits forAttribute:NSOpenGLPFAStencilSize forVirtualScreen:0];
59*c8dee2aaSAndroid Build Coastguard Worker    return stencilBits;
60*c8dee2aaSAndroid Build Coastguard Worker}
61*c8dee2aaSAndroid Build Coastguard Worker
62*c8dee2aaSAndroid Build Coastguard WorkerSkISize ANGLEWindowContext_mac::onGetSize() const {
63*c8dee2aaSAndroid Build Coastguard Worker    CGFloat backingScaleFactor = skwindow::GetBackingScaleFactor(fMainView);
64*c8dee2aaSAndroid Build Coastguard Worker    return SkISize::Make(fMainView.bounds.size.width * backingScaleFactor,
65*c8dee2aaSAndroid Build Coastguard Worker                         fMainView.bounds.size.height * backingScaleFactor);
66*c8dee2aaSAndroid Build Coastguard Worker}
67*c8dee2aaSAndroid Build Coastguard Worker
68*c8dee2aaSAndroid Build Coastguard Worker}  // anonymous namespace
69*c8dee2aaSAndroid Build Coastguard Worker
70*c8dee2aaSAndroid Build Coastguard Workernamespace skwindow {
71*c8dee2aaSAndroid Build Coastguard Worker
72*c8dee2aaSAndroid Build Coastguard Workerstd::unique_ptr<WindowContext> MakeGaneshANGLEForMac(const MacWindowInfo& info,
73*c8dee2aaSAndroid Build Coastguard Worker                                                     std::unique_ptr<const DisplayParams> params) {
74*c8dee2aaSAndroid Build Coastguard Worker    std::unique_ptr<WindowContext> ctx(new ANGLEWindowContext_mac(info, std::move(params)));
75*c8dee2aaSAndroid Build Coastguard Worker    if (!ctx->isValid()) {
76*c8dee2aaSAndroid Build Coastguard Worker        return nullptr;
77*c8dee2aaSAndroid Build Coastguard Worker    }
78*c8dee2aaSAndroid Build Coastguard Worker    return ctx;
79*c8dee2aaSAndroid Build Coastguard Worker}
80*c8dee2aaSAndroid Build Coastguard Worker
81*c8dee2aaSAndroid Build Coastguard Worker}  // namespace skwindow
82