1cb93a386Sopenharmony_ci/*
2cb93a386Sopenharmony_ci* Copyright 2016 Google Inc.
3cb93a386Sopenharmony_ci*
4cb93a386Sopenharmony_ci* Use of this source code is governed by a BSD-style license that can be
5cb93a386Sopenharmony_ci* found in the LICENSE file.
6cb93a386Sopenharmony_ci*/
7cb93a386Sopenharmony_ci
8cb93a386Sopenharmony_ci#ifndef Window_DEFINED
9cb93a386Sopenharmony_ci#define Window_DEFINED
10cb93a386Sopenharmony_ci
11cb93a386Sopenharmony_ci#include "include/core/SkRect.h"
12cb93a386Sopenharmony_ci#include "include/core/SkTypes.h"
13cb93a386Sopenharmony_ci#include "include/private/SkTDArray.h"
14cb93a386Sopenharmony_ci#include "tools/sk_app/DisplayParams.h"
15cb93a386Sopenharmony_ci#include "tools/skui/InputState.h"
16cb93a386Sopenharmony_ci#include "tools/skui/Key.h"
17cb93a386Sopenharmony_ci#include "tools/skui/ModifierKey.h"
18cb93a386Sopenharmony_ci
19cb93a386Sopenharmony_ci#include <functional>
20cb93a386Sopenharmony_ci
21cb93a386Sopenharmony_ciclass GrDirectContext;
22cb93a386Sopenharmony_ciclass SkCanvas;
23cb93a386Sopenharmony_ciclass SkSurface;
24cb93a386Sopenharmony_ciclass SkSurfaceProps;
25cb93a386Sopenharmony_ciclass SkString;
26cb93a386Sopenharmony_ci
27cb93a386Sopenharmony_cinamespace skgpu {
28cb93a386Sopenharmony_ciclass Context;
29cb93a386Sopenharmony_ci}
30cb93a386Sopenharmony_ci
31cb93a386Sopenharmony_cinamespace sk_app {
32cb93a386Sopenharmony_ci
33cb93a386Sopenharmony_ciclass WindowContext;
34cb93a386Sopenharmony_ci
35cb93a386Sopenharmony_ciclass Window {
36cb93a386Sopenharmony_cipublic:
37cb93a386Sopenharmony_ci    static Window* CreateNativeWindow(void* platformData);
38cb93a386Sopenharmony_ci
39cb93a386Sopenharmony_ci    virtual ~Window();
40cb93a386Sopenharmony_ci
41cb93a386Sopenharmony_ci    virtual void setTitle(const char*) = 0;
42cb93a386Sopenharmony_ci    virtual void show() = 0;
43cb93a386Sopenharmony_ci
44cb93a386Sopenharmony_ci    // JSON-formatted UI state for Android. Do nothing by default
45cb93a386Sopenharmony_ci    virtual void setUIState(const char*) {}
46cb93a386Sopenharmony_ci
47cb93a386Sopenharmony_ci    // Interface to the system clipboard. Only implemented on UNIX.
48cb93a386Sopenharmony_ci    virtual const char* getClipboardText() { return nullptr; }
49cb93a386Sopenharmony_ci    virtual void        setClipboardText(const char*) {}
50cb93a386Sopenharmony_ci
51cb93a386Sopenharmony_ci    // Schedules an invalidation event for window if one is not currently pending.
52cb93a386Sopenharmony_ci    // Make sure that either onPaint or markInvalReceived is called when the client window consumes
53cb93a386Sopenharmony_ci    // the the inval event. They unset fIsContentInvalided which allow future onInval.
54cb93a386Sopenharmony_ci    void inval();
55cb93a386Sopenharmony_ci
56cb93a386Sopenharmony_ci    virtual bool scaleContentToFit() const { return false; }
57cb93a386Sopenharmony_ci
58cb93a386Sopenharmony_ci    enum BackendType {
59cb93a386Sopenharmony_ci#ifdef SK_GL
60cb93a386Sopenharmony_ci        kNativeGL_BackendType,
61cb93a386Sopenharmony_ci#endif
62cb93a386Sopenharmony_ci#if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
63cb93a386Sopenharmony_ci        kANGLE_BackendType,
64cb93a386Sopenharmony_ci#endif
65cb93a386Sopenharmony_ci#ifdef SK_DAWN
66cb93a386Sopenharmony_ci        kDawn_BackendType,
67cb93a386Sopenharmony_ci#endif
68cb93a386Sopenharmony_ci#ifdef SK_VULKAN
69cb93a386Sopenharmony_ci        kVulkan_BackendType,
70cb93a386Sopenharmony_ci#endif
71cb93a386Sopenharmony_ci#ifdef SK_METAL
72cb93a386Sopenharmony_ci        kMetal_BackendType,
73cb93a386Sopenharmony_ci#ifdef SK_GRAPHITE_ENABLED
74cb93a386Sopenharmony_ci        kGraphiteMetal_BackendType,
75cb93a386Sopenharmony_ci#endif
76cb93a386Sopenharmony_ci#endif
77cb93a386Sopenharmony_ci#ifdef SK_DIRECT3D
78cb93a386Sopenharmony_ci        kDirect3D_BackendType,
79cb93a386Sopenharmony_ci#endif
80cb93a386Sopenharmony_ci        kRaster_BackendType,
81cb93a386Sopenharmony_ci
82cb93a386Sopenharmony_ci        kLast_BackendType = kRaster_BackendType
83cb93a386Sopenharmony_ci    };
84cb93a386Sopenharmony_ci    enum {
85cb93a386Sopenharmony_ci        kBackendTypeCount = kLast_BackendType + 1
86cb93a386Sopenharmony_ci    };
87cb93a386Sopenharmony_ci
88cb93a386Sopenharmony_ci    virtual bool attach(BackendType) = 0;
89cb93a386Sopenharmony_ci    void detach();
90cb93a386Sopenharmony_ci
91cb93a386Sopenharmony_ci    // input handling
92cb93a386Sopenharmony_ci
93cb93a386Sopenharmony_ci    class Layer {
94cb93a386Sopenharmony_ci    public:
95cb93a386Sopenharmony_ci        Layer() : fActive(true) {}
96cb93a386Sopenharmony_ci        virtual ~Layer() = default;
97cb93a386Sopenharmony_ci
98cb93a386Sopenharmony_ci        bool getActive() { return fActive; }
99cb93a386Sopenharmony_ci        void setActive(bool active) { fActive = active; }
100cb93a386Sopenharmony_ci
101cb93a386Sopenharmony_ci        // return value of 'true' means 'I have handled this event'
102cb93a386Sopenharmony_ci        virtual void onBackendCreated() {}
103cb93a386Sopenharmony_ci        virtual void onAttach(Window* window) {}
104cb93a386Sopenharmony_ci        virtual bool onChar(SkUnichar c, skui::ModifierKey) { return false; }
105cb93a386Sopenharmony_ci        virtual bool onKey(skui::Key, skui::InputState, skui::ModifierKey) { return false; }
106cb93a386Sopenharmony_ci        virtual bool onMouse(int x, int y, skui::InputState, skui::ModifierKey) { return false; }
107cb93a386Sopenharmony_ci        virtual bool onMouseWheel(float delta, skui::ModifierKey) { return false; }
108cb93a386Sopenharmony_ci        virtual bool onTouch(intptr_t owner, skui::InputState, float x, float y) { return false; }
109cb93a386Sopenharmony_ci        // Platform-detected gesture events
110cb93a386Sopenharmony_ci        virtual bool onFling(skui::InputState state) { return false; }
111cb93a386Sopenharmony_ci        virtual bool onPinch(skui::InputState state, float scale, float x, float y) { return false; }
112cb93a386Sopenharmony_ci        virtual void onUIStateChanged(const SkString& stateName, const SkString& stateValue) {}
113cb93a386Sopenharmony_ci        virtual void onPrePaint() {}
114cb93a386Sopenharmony_ci        virtual void onPaint(SkSurface*) {}
115cb93a386Sopenharmony_ci        virtual void onResize(int width, int height) {}
116cb93a386Sopenharmony_ci
117cb93a386Sopenharmony_ci    private:
118cb93a386Sopenharmony_ci        friend class Window;
119cb93a386Sopenharmony_ci        bool fActive;
120cb93a386Sopenharmony_ci    };
121cb93a386Sopenharmony_ci
122cb93a386Sopenharmony_ci    void pushLayer(Layer* layer) {
123cb93a386Sopenharmony_ci        layer->onAttach(this);
124cb93a386Sopenharmony_ci        fLayers.push_back(layer);
125cb93a386Sopenharmony_ci    }
126cb93a386Sopenharmony_ci
127cb93a386Sopenharmony_ci    void onBackendCreated();
128cb93a386Sopenharmony_ci    bool onChar(SkUnichar c, skui::ModifierKey modifiers);
129cb93a386Sopenharmony_ci    bool onKey(skui::Key key, skui::InputState state, skui::ModifierKey modifiers);
130cb93a386Sopenharmony_ci    bool onMouse(int x, int y, skui::InputState state, skui::ModifierKey modifiers);
131cb93a386Sopenharmony_ci    bool onMouseWheel(float delta, skui::ModifierKey modifiers);
132cb93a386Sopenharmony_ci    bool onTouch(intptr_t owner, skui::InputState state, float x, float y);  // multi-owner = multi-touch
133cb93a386Sopenharmony_ci    // Platform-detected gesture events
134cb93a386Sopenharmony_ci    bool onFling(skui::InputState state);
135cb93a386Sopenharmony_ci    bool onPinch(skui::InputState state, float scale, float x, float y);
136cb93a386Sopenharmony_ci    void onUIStateChanged(const SkString& stateName, const SkString& stateValue);
137cb93a386Sopenharmony_ci    void onPaint();
138cb93a386Sopenharmony_ci    void onResize(int width, int height);
139cb93a386Sopenharmony_ci    void onActivate(bool isActive);
140cb93a386Sopenharmony_ci
141cb93a386Sopenharmony_ci    int width() const;
142cb93a386Sopenharmony_ci    int height() const;
143cb93a386Sopenharmony_ci    virtual float scaleFactor() const { return 1.0f; }
144cb93a386Sopenharmony_ci
145cb93a386Sopenharmony_ci    virtual const DisplayParams& getRequestedDisplayParams() { return fRequestedDisplayParams; }
146cb93a386Sopenharmony_ci    virtual void setRequestedDisplayParams(const DisplayParams&, bool allowReattach = true);
147cb93a386Sopenharmony_ci
148cb93a386Sopenharmony_ci    // Actual parameters in effect, obtained from the native window.
149cb93a386Sopenharmony_ci    int sampleCount() const;
150cb93a386Sopenharmony_ci    int stencilBits() const;
151cb93a386Sopenharmony_ci
152cb93a386Sopenharmony_ci    // Returns null if there is not a GPU backend or if the backend is not yet created.
153cb93a386Sopenharmony_ci    GrDirectContext* directContext() const;
154cb93a386Sopenharmony_ci    skgpu::Context* graphiteContext() const;
155cb93a386Sopenharmony_ci
156cb93a386Sopenharmony_ciprotected:
157cb93a386Sopenharmony_ci    Window();
158cb93a386Sopenharmony_ci
159cb93a386Sopenharmony_ci    SkTDArray<Layer*>      fLayers;
160cb93a386Sopenharmony_ci    DisplayParams          fRequestedDisplayParams;
161cb93a386Sopenharmony_ci    bool                   fIsActive = true;
162cb93a386Sopenharmony_ci
163cb93a386Sopenharmony_ci    std::unique_ptr<WindowContext> fWindowContext;
164cb93a386Sopenharmony_ci
165cb93a386Sopenharmony_ci    virtual void onInval() = 0;
166cb93a386Sopenharmony_ci
167cb93a386Sopenharmony_ci    // Uncheck fIsContentInvalided to allow future inval/onInval.
168cb93a386Sopenharmony_ci    void markInvalProcessed();
169cb93a386Sopenharmony_ci
170cb93a386Sopenharmony_ci    bool fIsContentInvalidated = false;  // use this to avoid duplicate invalidate events
171cb93a386Sopenharmony_ci
172cb93a386Sopenharmony_ci    void visitLayers(std::function<void(Layer*)> visitor);
173cb93a386Sopenharmony_ci    bool signalLayers(std::function<bool(Layer*)> visitor);
174cb93a386Sopenharmony_ci};
175cb93a386Sopenharmony_ci
176cb93a386Sopenharmony_ci}   // namespace sk_app
177cb93a386Sopenharmony_ci#endif
178