Window.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. * Copyright 2016 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. #ifndef Window_DEFINED
  8. #define Window_DEFINED
  9. #include "include/core/SkRect.h"
  10. #include "include/core/SkTypes.h"
  11. #include "include/private/SkTDArray.h"
  12. #include "tools/InputState.h"
  13. #include "tools/ModifierKey.h"
  14. #include "tools/sk_app/DisplayParams.h"
  15. class GrContext;
  16. class SkCanvas;
  17. class SkSurface;
  18. class SkSurfaceProps;
  19. namespace sk_app {
  20. class WindowContext;
  21. class Window {
  22. public:
  23. static Window* CreateNativeWindow(void* platformData);
  24. virtual ~Window() { this->detach(); }
  25. virtual void setTitle(const char*) = 0;
  26. virtual void show() = 0;
  27. // JSON-formatted UI state for Android. Do nothing by default
  28. virtual void setUIState(const char*) {}
  29. // Shedules an invalidation event for window if one is not currently pending.
  30. // Make sure that either onPaint or markInvalReceived is called when the client window consumes
  31. // the the inval event. They unset fIsContentInvalided which allow future onInval.
  32. void inval();
  33. virtual bool scaleContentToFit() const { return false; }
  34. enum BackendType {
  35. kNativeGL_BackendType,
  36. #if SK_ANGLE && defined(SK_BUILD_FOR_WIN)
  37. kANGLE_BackendType,
  38. #endif
  39. #ifdef SK_VULKAN
  40. kVulkan_BackendType,
  41. #endif
  42. #if SK_METAL && defined(SK_BUILD_FOR_MAC)
  43. kMetal_BackendType,
  44. #endif
  45. kRaster_BackendType,
  46. kLast_BackendType = kRaster_BackendType
  47. };
  48. enum {
  49. kBackendTypeCount = kLast_BackendType + 1
  50. };
  51. virtual bool attach(BackendType) = 0;
  52. void detach();
  53. // input handling
  54. enum class Key {
  55. kNONE, //corresponds to android's UNKNOWN
  56. kLeftSoftKey,
  57. kRightSoftKey,
  58. kHome, //!< the home key - added to match android
  59. kBack, //!< (CLR)
  60. kSend, //!< the green (talk) key
  61. kEnd, //!< the red key
  62. k0,
  63. k1,
  64. k2,
  65. k3,
  66. k4,
  67. k5,
  68. k6,
  69. k7,
  70. k8,
  71. k9,
  72. kStar, //!< the * key
  73. kHash, //!< the # key
  74. kUp,
  75. kDown,
  76. kLeft,
  77. kRight,
  78. // Keys needed by ImGui
  79. kTab,
  80. kPageUp,
  81. kPageDown,
  82. kDelete,
  83. kEscape,
  84. kShift,
  85. kCtrl,
  86. kOption, // AKA Alt
  87. kA,
  88. kC,
  89. kV,
  90. kX,
  91. kY,
  92. kZ,
  93. kOK, //!< the center key
  94. kVolUp, //!< volume up - match android
  95. kVolDown, //!< volume down - same
  96. kPower, //!< power button - same
  97. kCamera, //!< camera - same
  98. kLast = kCamera
  99. };
  100. static const int kKeyCount = static_cast<int>(Key::kLast) + 1;
  101. class Layer {
  102. public:
  103. Layer() : fActive(true) {}
  104. virtual ~Layer() = default;
  105. bool getActive() { return fActive; }
  106. void setActive(bool active) { fActive = active; }
  107. // return value of 'true' means 'I have handled this event'
  108. virtual void onBackendCreated() {}
  109. virtual void onAttach(Window* window) {}
  110. virtual bool onChar(SkUnichar c, ModifierKey modifiers) { return false; }
  111. virtual bool onKey(Key key, InputState state, ModifierKey modifiers) { return false; }
  112. virtual bool onMouse(int x, int y, InputState state, ModifierKey modifiers) { return false; }
  113. virtual bool onMouseWheel(float delta, ModifierKey modifiers) { return false; }
  114. virtual bool onTouch(intptr_t owner, InputState state, float x, float y) { return false; }
  115. virtual void onUIStateChanged(const SkString& stateName, const SkString& stateValue) {}
  116. virtual void onPrePaint() {}
  117. virtual void onPaint(SkSurface*) {}
  118. virtual void onResize(int width, int height) {}
  119. private:
  120. friend class Window;
  121. bool fActive;
  122. };
  123. void pushLayer(Layer* layer) {
  124. layer->onAttach(this);
  125. fLayers.push_back(layer);
  126. }
  127. void onBackendCreated();
  128. bool onChar(SkUnichar c, ModifierKey modifiers);
  129. bool onKey(Key key, InputState state, ModifierKey modifiers);
  130. bool onMouse(int x, int y, InputState state, ModifierKey modifiers);
  131. bool onMouseWheel(float delta, ModifierKey modifiers);
  132. bool onTouch(intptr_t owner, InputState state, float x, float y); // multi-owner = multi-touch
  133. void onUIStateChanged(const SkString& stateName, const SkString& stateValue);
  134. void onPaint();
  135. void onResize(int width, int height);
  136. int width() const;
  137. int height() const;
  138. virtual const DisplayParams& getRequestedDisplayParams() { return fRequestedDisplayParams; }
  139. virtual void setRequestedDisplayParams(const DisplayParams&, bool allowReattach = true);
  140. // Actual parameters in effect, obtained from the native window.
  141. int sampleCount() const;
  142. int stencilBits() const;
  143. // Returns null if there is not a GPU backend or if the backend is not yet created.
  144. GrContext* getGrContext() const;
  145. protected:
  146. Window();
  147. SkTDArray<Layer*> fLayers;
  148. DisplayParams fRequestedDisplayParams;
  149. WindowContext* fWindowContext = nullptr;
  150. virtual void onInval() = 0;
  151. // Uncheck fIsContentInvalided to allow future inval/onInval.
  152. void markInvalProcessed();
  153. bool fIsContentInvalidated = false; // use this to avoid duplicate invalidate events
  154. void visitLayers(std::function<void(Layer*)> visitor);
  155. bool signalLayers(std::function<bool(Layer*)> visitor);
  156. };
  157. } // namespace sk_app
  158. #endif