OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2011 Google Inc. | 2 * Copyright 2011 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 #include "SkOSWindow_SDL.h" | 7 #include "SkOSWindow_SDL.h" |
8 #include "SkCanvas.h" | 8 #include "SkCanvas.h" |
9 | 9 |
| 10 #if defined(SK_BUILD_FOR_ANDROID) |
| 11 #include <GLES/gl.h> |
| 12 #else |
10 #include <GL/gl.h> | 13 #include <GL/gl.h> |
| 14 #endif |
11 | 15 |
12 const int SCREEN_WIDTH = 640; | 16 const int SCREEN_WIDTH = 640; |
13 const int SCREEN_HEIGHT = 480; | 17 const int SCREEN_HEIGHT = 480; |
14 | 18 |
15 static void handle_error() { | 19 static void handle_error() { |
16 const char* error = SDL_GetError(); | 20 const char* error = SDL_GetError(); |
17 SkDebugf("SDL Error: %s\n", error); | 21 SkDebugf("SDL Error: %s\n", error); |
18 SDL_ClearError(); | 22 SDL_ClearError(); |
19 } | 23 } |
20 | 24 |
21 SkOSWindow::SkOSWindow(void* screen) : fQuit(false) , fGLContext(nullptr) { | 25 SkOSWindow::SkOSWindow(void* screen) : fQuit(false) , fGLContext(nullptr) { |
22 //Create window | 26 #if defined(SK_BUILD_FOR_ANDROID) |
23 SDL_Init(SDL_INIT_VIDEO|SDL_INIT_GAMECONTROLLER|SDL_INIT_EVENTS); | 27 // TODO we should try and get a 3.0 context first |
24 fWindow = SDL_CreateWindow("SDL Window", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOW
POS_UNDEFINED, | 28 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2); |
25 SCREEN_WIDTH, SCREEN_HEIGHT, | 29 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0); |
26 SDL_WINDOW_OPENGL|SDL_WINDOW_SHOWN ); | 30 SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES); |
| 31 fWindowFlags = SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | |
| 32 SDL_WINDOW_BORDERLESS | SDL_WINDOW_FULLSCREEN_DESKTOP | |
| 33 SDL_WINDOW_ALLOW_HIGHDPI; |
| 34 #else |
| 35 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); |
| 36 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1); |
| 37 SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE
); |
| 38 SDL_StartTextInput(); |
| 39 |
| 40 fWindowFlags = SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE; |
| 41 #endif |
| 42 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5); |
| 43 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5); |
| 44 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5); |
| 45 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); |
| 46 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24); |
| 47 SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8); |
| 48 |
| 49 SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL, 1); |
| 50 |
| 51 if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS) != 0) { |
| 52 handle_error(); |
| 53 return; |
| 54 } |
| 55 |
| 56 SDL_DisplayMode dm; |
| 57 if (SDL_GetDesktopDisplayMode(0, &dm) != 0) { |
| 58 handle_error(); |
| 59 return; |
| 60 } |
| 61 |
| 62 fWindow = SDL_CreateWindow("SDL Window", SDL_WINDOWPOS_CENTERED, SDL_WINDOWP
OS_CENTERED, |
| 63 dm.w, dm.h, fWindowFlags); |
| 64 |
27 if (!fWindow) { | 65 if (!fWindow) { |
28 handle_error(); | 66 handle_error(); |
29 return; | 67 return; |
30 } | 68 } |
31 SDL_StartTextInput(); | 69 this->resize(SkIntToScalar(dm.w), SkIntToScalar(dm.h)); |
32 this->resize(SCREEN_WIDTH, SCREEN_HEIGHT); | |
33 } | 70 } |
34 | 71 |
35 SkOSWindow::~SkOSWindow() { | 72 SkOSWindow::~SkOSWindow() { |
36 if (fGLContext) { | 73 if (fGLContext) { |
37 SDL_GL_DeleteContext(fGLContext); | 74 SDL_GL_DeleteContext(fGLContext); |
38 } | 75 } |
39 | 76 |
40 //Destroy window | 77 //Destroy window |
41 SDL_DestroyWindow(fWindow); | 78 SDL_DestroyWindow(fWindow); |
42 | 79 |
43 //Quit SDL subsystems | 80 //Quit SDL subsystems |
44 SDL_Quit(); | 81 SDL_Quit(); |
45 } | 82 } |
46 | 83 |
47 void SkOSWindow::detach() { | 84 void SkOSWindow::detach() { |
48 if (fGLContext) { | 85 if (fGLContext) { |
49 SDL_GL_DeleteContext(fGLContext); | 86 SDL_GL_DeleteContext(fGLContext); |
50 fGLContext = nullptr; | 87 fGLContext = nullptr; |
51 } | 88 } |
| 89 |
| 90 #if defined(SK_BUILD_FOR_ANDROID) |
| 91 if (fWindow) { |
| 92 // Destroy window |
| 93 // Not totally sure why, but we have to do this or swapbuffers will hang |
| 94 SDL_DestroyWindow(fWindow); |
| 95 fWindow = nullptr; |
| 96 } |
| 97 #endif |
52 } | 98 } |
53 | 99 |
54 bool SkOSWindow::attach(SkBackEndTypes attachType, int msaaSampleCount, Attachme
ntInfo*) { | 100 bool SkOSWindow::attach(SkBackEndTypes attachType, int msaaSampleCount, Attachme
ntInfo* info) { |
55 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); | 101 if (!fWindow) { |
56 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1); | 102 fWindow = SDL_CreateWindow("SDL Window", SDL_WINDOWPOS_CENTERED, SDL_WIN
DOWPOS_CENTERED, |
57 SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE
); | 103 SCREEN_WIDTH, SCREEN_HEIGHT, |
58 | 104 fWindowFlags); |
59 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); | 105 } |
60 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24); | |
61 SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8); | |
62 | 106 |
63 if (msaaSampleCount > 0) { | 107 if (msaaSampleCount > 0) { |
64 SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1); | 108 SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1); |
65 SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, msaaSampleCount); | 109 SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, msaaSampleCount); |
66 } | 110 } |
67 | 111 |
| 112 info->fSampleCount = msaaSampleCount; |
| 113 info->fStencilBits = 8; |
| 114 |
68 fGLContext = SDL_GL_CreateContext(fWindow); | 115 fGLContext = SDL_GL_CreateContext(fWindow); |
69 if (!fGLContext) { | 116 if (!fGLContext) { |
70 handle_error(); | 117 handle_error(); |
71 return false; | 118 return false; |
72 } | 119 } |
73 | 120 |
74 int success = SDL_GL_MakeCurrent(fWindow, fGLContext); | 121 int success = SDL_GL_MakeCurrent(fWindow, fGLContext); |
75 if (success != 0) { | 122 if (success != 0) { |
76 handle_error(); | 123 handle_error(); |
77 return false; | 124 return false; |
78 } | 125 } |
79 | 126 |
80 glViewport(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT); | 127 glViewport(0, 0, SkScalarFloorToInt(this->width()), SkScalarFloorToInt(this-
>height())); |
81 glClearColor(1, 1, 1, 1); | 128 glClearColor(1, 1, 1, 1); |
82 glClearStencil(0); | 129 glClearStencil(0); |
83 glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); | 130 glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); |
84 | 131 |
85 return true; | 132 return true; |
86 } | 133 } |
87 | 134 |
88 void SkOSWindow::present() { | 135 void SkOSWindow::present() { |
89 SDL_GL_SwapWindow(fWindow); | 136 SDL_GL_SwapWindow(fWindow); |
90 } | 137 } |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
202 ////////////////////////////////////////////////////////////////////////////////
/////// | 249 ////////////////////////////////////////////////////////////////////////////////
/////// |
203 | 250 |
204 void SkEvent::SignalNonEmptyQueue() { | 251 void SkEvent::SignalNonEmptyQueue() { |
205 // nothing to do, since we spin on our event-queue | 252 // nothing to do, since we spin on our event-queue |
206 } | 253 } |
207 | 254 |
208 void SkEvent::SignalQueueTimer(SkMSec delay) { | 255 void SkEvent::SignalQueueTimer(SkMSec delay) { |
209 // just need to record the delay time. We handle waking up for it in | 256 // just need to record the delay time. We handle waking up for it in |
210 } | 257 } |
211 | 258 |
| 259 void SkOSWindow::onHandleInval(const SkIRect& rect) { |
| 260 } |
| 261 |
| 262 void SkOSWindow::onPDFSaved(const char title[], const char desc[], const char pa
th[]) { |
| 263 } |
| 264 |
212 ////////////////////////////////////////////////////////////////////////////////
////////////// | 265 ////////////////////////////////////////////////////////////////////////////////
////////////// |
213 | 266 |
214 | |
215 #include "SkApplication.h" | 267 #include "SkApplication.h" |
216 #include "SkEvent.h" | 268 #include "SkEvent.h" |
217 #include "SkWindow.h" | 269 #include "SkWindow.h" |
218 | 270 |
219 int main(int argc, char** argv){ | 271 #if defined(SK_BUILD_FOR_ANDROID) |
| 272 int SDL_main(int argc, char** argv) { |
| 273 #else |
| 274 int main(int argc, char** argv) { |
| 275 #endif |
220 SkOSWindow* window = create_sk_window(nullptr, argc, argv); | 276 SkOSWindow* window = create_sk_window(nullptr, argc, argv); |
221 | 277 |
222 // drain any events that occurred before |window| was assigned. | 278 // drain any events that occurred before |window| was assigned. |
223 while (SkEvent::ProcessEvent()); | 279 while (SkEvent::ProcessEvent()); |
224 | 280 |
225 // Start normal Skia sequence | 281 // Start normal Skia sequence |
226 application_init(); | 282 application_init(); |
227 | 283 |
228 window->loop(); | 284 window->loop(); |
229 | 285 |
230 delete window; | 286 delete window; |
231 application_term(); | 287 application_term(); |
232 return 0; | 288 return 0; |
233 } | 289 } |
OLD | NEW |