skia2/tools/sk_app/Window.cpp
Greg Daniel 0a2464f51f Update internal skia uses to use flushAndSubmit and submit calls.
Bug: skia:10118
Change-Id: Ieb7c0eece56d3d9df56ecb52e00e76c01f038de8
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/289888
Reviewed-by: Brian Salomon <bsalomon@google.com>
Commit-Queue: Greg Daniel <egdaniel@google.com>
2020-05-14 20:26:44 +00:00

162 lines
4.2 KiB
C++

/*
* Copyright 2016 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "tools/sk_app/Window.h"
#include "include/core/SkCanvas.h"
#include "include/core/SkSurface.h"
#include "tools/sk_app/WindowContext.h"
namespace sk_app {
Window::Window() {}
Window::~Window() {}
void Window::detach() { fWindowContext = nullptr; }
void Window::visitLayers(std::function<void(Layer*)> visitor) {
for (int i = 0; i < fLayers.count(); ++i) {
if (fLayers[i]->fActive) {
visitor(fLayers[i]);
}
}
}
bool Window::signalLayers(std::function<bool(Layer*)> visitor) {
for (int i = fLayers.count() - 1; i >= 0; --i) {
if (fLayers[i]->fActive && visitor(fLayers[i])) {
return true;
}
}
return false;
}
void Window::onBackendCreated() {
this->visitLayers([](Layer* layer) { layer->onBackendCreated(); });
}
bool Window::onChar(SkUnichar c, skui::ModifierKey modifiers) {
return this->signalLayers([=](Layer* layer) { return layer->onChar(c, modifiers); });
}
bool Window::onKey(skui::Key key, skui::InputState state, skui::ModifierKey modifiers) {
return this->signalLayers([=](Layer* layer) { return layer->onKey(key, state, modifiers); });
}
bool Window::onMouse(int x, int y, skui::InputState state, skui::ModifierKey modifiers) {
return this->signalLayers([=](Layer* layer) { return layer->onMouse(x, y, state, modifiers); });
}
bool Window::onMouseWheel(float delta, skui::ModifierKey modifiers) {
return this->signalLayers([=](Layer* layer) { return layer->onMouseWheel(delta, modifiers); });
}
bool Window::onTouch(intptr_t owner, skui::InputState state, float x, float y) {
return this->signalLayers([=](Layer* layer) { return layer->onTouch(owner, state, x, y); });
}
bool Window::onFling(skui::InputState state) {
return this->signalLayers([=](Layer* layer) { return layer->onFling(state); });
}
bool Window::onPinch(skui::InputState state, float scale, float x, float y) {
return this->signalLayers([=](Layer* layer) { return layer->onPinch(state, scale, x, y); });
}
void Window::onUIStateChanged(const SkString& stateName, const SkString& stateValue) {
this->visitLayers([=](Layer* layer) { layer->onUIStateChanged(stateName, stateValue); });
}
void Window::onPaint() {
if (!fWindowContext) {
return;
}
sk_sp<SkSurface> backbuffer = fWindowContext->getBackbufferSurface();
if (backbuffer == nullptr) {
printf("no backbuffer!?\n");
// TODO: try recreating testcontext
return;
}
markInvalProcessed();
// draw into the canvas of this surface
this->visitLayers([](Layer* layer) { layer->onPrePaint(); });
this->visitLayers([=](Layer* layer) { layer->onPaint(backbuffer.get()); });
backbuffer->flushAndSubmit();
fWindowContext->swapBuffers();
}
void Window::onResize(int w, int h) {
if (!fWindowContext) {
return;
}
fWindowContext->resize(w, h);
this->visitLayers([=](Layer* layer) { layer->onResize(w, h); });
}
int Window::width() const {
if (!fWindowContext) {
return 0;
}
return fWindowContext->width();
}
int Window::height() const {
if (!fWindowContext) {
return 0;
}
return fWindowContext->height();
}
void Window::setRequestedDisplayParams(const DisplayParams& params, bool /* allowReattach */) {
fRequestedDisplayParams = params;
if (fWindowContext) {
fWindowContext->setDisplayParams(fRequestedDisplayParams);
}
}
int Window::sampleCount() const {
if (!fWindowContext) {
return 0;
}
return fWindowContext->sampleCount();
}
int Window::stencilBits() const {
if (!fWindowContext) {
return -1;
}
return fWindowContext->stencilBits();
}
GrContext* Window::getGrContext() const {
if (!fWindowContext) {
return nullptr;
}
return fWindowContext->getGrContext();
}
void Window::inval() {
if (!fWindowContext) {
return;
}
if (!fIsContentInvalidated) {
fIsContentInvalidated = true;
onInval();
}
}
void Window::markInvalProcessed() {
fIsContentInvalidated = false;
}
} // namespace sk_app