Window.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. #include "tools/sk_app/Window.h"
  8. #include "include/core/SkCanvas.h"
  9. #include "include/core/SkSurface.h"
  10. #include "tools/sk_app/WindowContext.h"
  11. namespace sk_app {
  12. Window::Window() {}
  13. void Window::detach() {
  14. delete fWindowContext;
  15. fWindowContext = nullptr;
  16. }
  17. void Window::visitLayers(std::function<void(Layer*)> visitor) {
  18. for (int i = 0; i < fLayers.count(); ++i) {
  19. if (fLayers[i]->fActive) {
  20. visitor(fLayers[i]);
  21. }
  22. }
  23. }
  24. bool Window::signalLayers(std::function<bool(Layer*)> visitor) {
  25. for (int i = fLayers.count() - 1; i >= 0; --i) {
  26. if (fLayers[i]->fActive && visitor(fLayers[i])) {
  27. return true;
  28. }
  29. }
  30. return false;
  31. }
  32. void Window::onBackendCreated() {
  33. this->visitLayers([](Layer* layer) { layer->onBackendCreated(); });
  34. }
  35. bool Window::onChar(SkUnichar c, ModifierKey modifiers) {
  36. return this->signalLayers([=](Layer* layer) { return layer->onChar(c, modifiers); });
  37. }
  38. bool Window::onKey(Key key, InputState state, ModifierKey modifiers) {
  39. return this->signalLayers([=](Layer* layer) { return layer->onKey(key, state, modifiers); });
  40. }
  41. bool Window::onMouse(int x, int y, InputState state, ModifierKey modifiers) {
  42. return this->signalLayers([=](Layer* layer) { return layer->onMouse(x, y, state, modifiers); });
  43. }
  44. bool Window::onMouseWheel(float delta, ModifierKey modifiers) {
  45. return this->signalLayers([=](Layer* layer) { return layer->onMouseWheel(delta, modifiers); });
  46. }
  47. bool Window::onTouch(intptr_t owner, InputState state, float x, float y) {
  48. return this->signalLayers([=](Layer* layer) { return layer->onTouch(owner, state, x, y); });
  49. }
  50. void Window::onUIStateChanged(const SkString& stateName, const SkString& stateValue) {
  51. this->visitLayers([=](Layer* layer) { layer->onUIStateChanged(stateName, stateValue); });
  52. }
  53. void Window::onPaint() {
  54. if (!fWindowContext) {
  55. return;
  56. }
  57. markInvalProcessed();
  58. this->visitLayers([](Layer* layer) { layer->onPrePaint(); });
  59. sk_sp<SkSurface> backbuffer = fWindowContext->getBackbufferSurface();
  60. if (backbuffer) {
  61. // draw into the canvas of this surface
  62. this->visitLayers([=](Layer* layer) { layer->onPaint(backbuffer.get()); });
  63. backbuffer->flush();
  64. fWindowContext->swapBuffers();
  65. } else {
  66. printf("no backbuffer!?\n");
  67. // try recreating testcontext
  68. }
  69. }
  70. void Window::onResize(int w, int h) {
  71. if (!fWindowContext) {
  72. return;
  73. }
  74. fWindowContext->resize(w, h);
  75. this->visitLayers([=](Layer* layer) { layer->onResize(w, h); });
  76. }
  77. int Window::width() const {
  78. if (!fWindowContext) {
  79. return 0;
  80. }
  81. return fWindowContext->width();
  82. }
  83. int Window::height() const {
  84. if (!fWindowContext) {
  85. return 0;
  86. }
  87. return fWindowContext->height();
  88. }
  89. void Window::setRequestedDisplayParams(const DisplayParams& params, bool /* allowReattach */) {
  90. fRequestedDisplayParams = params;
  91. if (fWindowContext) {
  92. fWindowContext->setDisplayParams(fRequestedDisplayParams);
  93. }
  94. }
  95. int Window::sampleCount() const {
  96. if (!fWindowContext) {
  97. return 0;
  98. }
  99. return fWindowContext->sampleCount();
  100. }
  101. int Window::stencilBits() const {
  102. if (!fWindowContext) {
  103. return -1;
  104. }
  105. return fWindowContext->stencilBits();
  106. }
  107. GrContext* Window::getGrContext() const {
  108. if (!fWindowContext) {
  109. return nullptr;
  110. }
  111. return fWindowContext->getGrContext();
  112. }
  113. void Window::inval() {
  114. if (!fWindowContext) {
  115. return;
  116. }
  117. if (!fIsContentInvalidated) {
  118. fIsContentInvalidated = true;
  119. onInval();
  120. }
  121. }
  122. void Window::markInvalProcessed() {
  123. fIsContentInvalidated = false;
  124. }
  125. } // namespace sk_app