aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/views/sdl/SkOSWindow_SDL.cpp
blob: 80802a1dd09f8d3f6084397f12f19fbfd41e28b4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/*
 * Copyright 2011 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */
#include "SkOSWindow_SDL.h"
#include "SkCanvas.h"

#if defined(SK_BUILD_FOR_ANDROID)
#include <GLES/gl.h>
#else
#include <GL/gl.h>
#endif

const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;

static void handle_error() {
    const char* error = SDL_GetError();
    SkDebugf("SDL Error: %s\n", error);
    SDL_ClearError();
}

SkOSWindow::SkOSWindow(void* screen) : fQuit(false) , fGLContext(nullptr) {
#if defined(SK_BUILD_FOR_ANDROID)
    // TODO we should try and get a 3.0 context first
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);
    fWindowFlags = SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE |
                   SDL_WINDOW_BORDERLESS | SDL_WINDOW_FULLSCREEN_DESKTOP |
                   SDL_WINDOW_ALLOW_HIGHDPI;
#else
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
    SDL_StartTextInput();

    fWindowFlags = SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE;
#endif
    SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
    SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
    SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
    SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
    SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);

    SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL, 1);

    if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS) != 0) {
        handle_error();
        return;
    }

    SDL_DisplayMode dm;
    if (SDL_GetDesktopDisplayMode(0, &dm) != 0) {
        handle_error();
        return;
    }

    fWindow = SDL_CreateWindow("SDL Window", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
                               dm.w, dm.h, fWindowFlags);

    if (!fWindow) {
        handle_error();
        return;
    }
    this->resize(SkIntToScalar(dm.w), SkIntToScalar(dm.h));
}

SkOSWindow::~SkOSWindow() {
    if (fGLContext) {
        SDL_GL_DeleteContext(fGLContext);
    }

    //Destroy window
    SDL_DestroyWindow(fWindow);

    //Quit SDL subsystems
    SDL_Quit();
}

void SkOSWindow::detach() {
    if (fGLContext) {
        SDL_GL_DeleteContext(fGLContext);
        fGLContext = nullptr;
    }

#if defined(SK_BUILD_FOR_ANDROID)
    if (fWindow) {
        // Destroy window
        // Not totally sure why, but we have to do this or swapbuffers will hang
        SDL_DestroyWindow(fWindow);
        fWindow = nullptr;
    }
#endif
}

bool SkOSWindow::attach(SkBackEndTypes attachType, int msaaSampleCount, AttachmentInfo* info) {
    if (!fWindow) {
        fWindow = SDL_CreateWindow("SDL Window", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
                                   SCREEN_WIDTH, SCREEN_HEIGHT,
                                   fWindowFlags);
    }

    if (msaaSampleCount > 0) {
        SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
        SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, msaaSampleCount);
    }

    info->fSampleCount = msaaSampleCount;
    info->fStencilBits = 8;

    fGLContext = SDL_GL_CreateContext(fWindow);
    if (!fGLContext) {
        handle_error();
        return false;
    }

    int success =  SDL_GL_MakeCurrent(fWindow, fGLContext);
    if (success != 0) {
        handle_error();
        return false;
    }

    glViewport(0, 0, SkScalarFloorToInt(this->width()), SkScalarFloorToInt(this->height()));
    glClearColor(1, 1, 1, 1);
    glClearStencil(0);
    glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);

    return true;
}

void SkOSWindow::present() {
    SDL_GL_SwapWindow(fWindow);
}

bool SkOSWindow::makeFullscreen() {
    SDL_SetWindowFullscreen(fWindow, SDL_WINDOW_FULLSCREEN);
    return true;
}

void SkOSWindow::setVsync(bool vsync) {
    SDL_GL_SetSwapInterval(vsync ? 1 : 0);
}

void SkOSWindow::closeWindow() {
    fQuit = true;
}

static SkKey convert_sdlkey_to_skkey(SDL_Keycode src) {
    switch (src) {
        case SDLK_UP:
            return kUp_SkKey;
        case SDLK_DOWN:
            return kDown_SkKey;
        case SDLK_LEFT:
            return kLeft_SkKey;
        case SDLK_RIGHT:
            return kRight_SkKey;
        case SDLK_HOME:
            return kHome_SkKey;
        case SDLK_END:
            return kEnd_SkKey;
        case SDLK_ASTERISK:
            return kStar_SkKey;
        case SDLK_HASH:
            return kHash_SkKey;
        case SDLK_0:
            return k0_SkKey;
        case SDLK_1:
            return k1_SkKey;
        case SDLK_2:
            return k2_SkKey;
        case SDLK_3:
            return k3_SkKey;
        case SDLK_4:
            return k4_SkKey;
        case SDLK_5:
            return k5_SkKey;
        case SDLK_6:
            return k6_SkKey;
        case SDLK_7:
            return k7_SkKey;
        case SDLK_8:
            return k8_SkKey;
        case SDLK_9:
            return k9_SkKey;
        default:
            return kNONE_SkKey;
    }
}

void SkOSWindow::handleEvents() {
    SkEvent::ServiceQueueTimer();
    SkEvent::ProcessEvent();

    SDL_Event event;
    while(SDL_PollEvent(&event)) {
        switch (event.type) {
            case SDL_MOUSEMOTION:
                if (event.motion.state == SDL_PRESSED) {
                    this->handleClick(event.motion.x, event.motion.y,
                                     SkView::Click::kMoved_State, nullptr);
                }
                break;
            case SDL_MOUSEBUTTONDOWN:
            case SDL_MOUSEBUTTONUP:
                this->handleClick(event.button.x, event.button.y,
                                  event.button.state == SDL_PRESSED ?
                                  SkView::Click::kDown_State :
                                  SkView::Click::kUp_State, nullptr);
                break;
            case SDL_KEYDOWN: {
                SDL_Keycode key = event.key.keysym.sym;
                SkKey sk = convert_sdlkey_to_skkey(key);
                if (kNONE_SkKey != sk) {
                    if (event.key.state == SDL_PRESSED) {
                        this->handleKey(sk);
                    } else {
                        this->handleKeyUp(sk);
                    }
                } else if (key == SDLK_ESCAPE) {
                    fQuit = true;
                }
                break;
            }
            case SDL_TEXTINPUT: {
                size_t len = strlen(event.text.text);
                for (size_t i = 0; i < len; i++) {
                    this->handleChar((SkUnichar)event.text.text[i]);
                }
                break;
            }
            case SDL_QUIT:
                fQuit = true;
                break;
            default:
                break;
        }
    }
}


void SkOSWindow::onSetTitle(const char title[]) {
    SDL_SetWindowTitle(fWindow, title);
}
///////////////////////////////////////////////////////////////////////////////////////

void SkEvent::SignalNonEmptyQueue() {
    // nothing to do, since we spin on our event-queue
}

void SkEvent::SignalQueueTimer(SkMSec delay) {
    // just need to record the delay time. We handle waking up for it in
}

void SkOSWindow::onHandleInval(const SkIRect& rect) {
}

void SkOSWindow::onPDFSaved(const char title[], const char desc[], const char path[]) {
}

//////////////////////////////////////////////////////////////////////////////////////////////

#include "SkApplication.h"
#include "SkEvent.h"
#include "SkWindow.h"

#if defined(SK_BUILD_FOR_ANDROID)
int SDL_main(int argc, char** argv) {
#else
int main(int argc, char** argv) {
#endif
    SkOSWindow* window = create_sk_window(nullptr, argc, argv);

    // drain any events that occurred before |window| was assigned.
    while (SkEvent::ProcessEvent());

    // Start normal Skia sequence
    application_init();

    window->loop();

    delete window;
    application_term();
    return 0;
}