Home | History | Annotate | Download | only in sk_app
      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 
      8 #include "Window.h"
      9 
     10 #include "SkSurface.h"
     11 #include "SkCanvas.h"
     12 #include "WindowContext.h"
     13 
     14 namespace sk_app {
     15 
     16 static void default_backend_created_func(void* userData) {}
     17 
     18 static bool default_char_func(SkUnichar c, uint32_t modifiers, void* userData) {
     19     return false;
     20 }
     21 
     22 static bool default_key_func(Window::Key key, Window::InputState state, uint32_t modifiers,
     23                              void* userData) {
     24     return false;
     25 }
     26 
     27 static bool default_mouse_func(int x, int y, Window::InputState state, uint32_t modifiers,
     28                                void* userData) {
     29     return false;
     30 }
     31 
     32 static bool default_mouse_wheel_func(float delta, uint32_t modifiers, void* userData) {
     33     return false;
     34 }
     35 
     36 static bool default_touch_func(intptr_t owner, Window::InputState state, float x, float y,
     37                                void* userData) {
     38     return false;
     39 }
     40 
     41 static void default_ui_state_changed_func(
     42         const SkString& stateName, const SkString& stateValue, void* userData) {}
     43 
     44 static void default_paint_func(SkCanvas*, void* userData) {}
     45 
     46 Window::Window() : fBackendCreatedFunc(default_backend_created_func)
     47                  , fCharFunc(default_char_func)
     48                  , fKeyFunc(default_key_func)
     49                  , fMouseFunc(default_mouse_func)
     50                  , fMouseWheelFunc(default_mouse_wheel_func)
     51                  , fTouchFunc(default_touch_func)
     52                  , fUIStateChangedFunc(default_ui_state_changed_func)
     53                  , fPaintFunc(default_paint_func) {
     54 }
     55 
     56 void Window::detach() {
     57     delete fWindowContext;
     58     fWindowContext = nullptr;
     59 }
     60 
     61 void Window::onBackendCreated() {
     62     fBackendCreatedFunc(fBackendCreatedUserData);
     63 }
     64 
     65 bool Window::onChar(SkUnichar c, uint32_t modifiers) {
     66     return fCharFunc(c, modifiers, fCharUserData);
     67 }
     68 
     69 bool Window::onKey(Key key, InputState state, uint32_t modifiers) {
     70     return fKeyFunc(key, state, modifiers, fKeyUserData);
     71 }
     72 
     73 bool Window::onMouse(int x, int y, InputState state, uint32_t modifiers) {
     74     return fMouseFunc(x, y, state, modifiers, fMouseUserData);
     75 }
     76 
     77 bool Window::onMouseWheel(float delta, uint32_t modifiers) {
     78     return fMouseWheelFunc(delta, modifiers, fMouseWheelUserData);
     79 }
     80 
     81 bool Window::onTouch(intptr_t owner, InputState state, float x, float y) {
     82     return fTouchFunc(owner, state, x, y, fTouchUserData);
     83 }
     84 
     85 void Window::onUIStateChanged(const SkString& stateName, const SkString& stateValue) {
     86     return fUIStateChangedFunc(stateName, stateValue, fUIStateChangedUserData);
     87 }
     88 
     89 void Window::onPaint() {
     90     if (!fWindowContext) {
     91         return;
     92     }
     93     markInvalProcessed();
     94     sk_sp<SkSurface> backbuffer = fWindowContext->getBackbufferSurface();
     95     if (backbuffer) {
     96         // draw into the canvas of this surface
     97         SkCanvas* canvas = backbuffer->getCanvas();
     98 
     99         fPaintFunc(canvas, fPaintUserData);
    100 
    101         canvas->flush();
    102 
    103         fWindowContext->swapBuffers();
    104     } else {
    105         printf("no backbuffer!?\n");
    106         // try recreating testcontext
    107     }
    108 }
    109 
    110 void Window::onResize(int w, int h) {
    111     if (!fWindowContext) {
    112         return;
    113     }
    114     fWindowContext->resize(w, h);
    115 }
    116 
    117 int Window::width() {
    118     if (!fWindowContext) {
    119         return 0;
    120     }
    121     return fWindowContext->width();
    122 }
    123 
    124 int Window::height() {
    125     if (!fWindowContext) {
    126         return 0;
    127     }
    128     return fWindowContext->height();
    129 }
    130 
    131 void Window::setRequestedDisplayParams(const DisplayParams& params, bool /* allowReattach */) {
    132     fRequestedDisplayParams = params;
    133     if (fWindowContext) {
    134         fWindowContext->setDisplayParams(fRequestedDisplayParams);
    135     }
    136 }
    137 
    138 int Window::sampleCount() const {
    139     if (!fWindowContext) {
    140         return -1;
    141     }
    142     return fWindowContext->sampleCount();
    143 }
    144 
    145 int Window::stencilBits() const {
    146     if (!fWindowContext) {
    147         return -1;
    148     }
    149     return fWindowContext->stencilBits();
    150 }
    151 
    152 const GrContext* Window::getGrContext() const {
    153     if (!fWindowContext) {
    154         return nullptr;
    155     }
    156     return fWindowContext->getGrContext();
    157 }
    158 
    159 void Window::inval() {
    160     if (!fWindowContext) {
    161         return;
    162     }
    163     if (!fIsContentInvalidated) {
    164         fIsContentInvalidated = true;
    165         onInval();
    166     }
    167 }
    168 
    169 void Window::markInvalProcessed() {
    170     fIsContentInvalidated = false;
    171 }
    172 
    173 }   // namespace sk_app
    174