joshualitt | 04d52f3 | 2015-11-13 11:22:19 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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 | |
Greg Daniel | bcf612b | 2017-05-01 13:50:58 +0000 | [diff] [blame] | 9 | #include "GrBackendSurface.h" |
joshualitt | 04d52f3 | 2015-11-13 11:22:19 -0800 | [diff] [blame] | 10 | #include "GrContext.h" |
| 11 | #include "SDL.h" |
| 12 | #include "SkCanvas.h" |
| 13 | #include "SkRandom.h" |
| 14 | #include "SkSurface.h" |
| 15 | |
| 16 | #include "gl/GrGLInterface.h" |
| 17 | #include "gl/GrGLUtil.h" |
| 18 | |
| 19 | #if defined(SK_BUILD_FOR_ANDROID) |
| 20 | #include <GLES/gl.h> |
| 21 | #elif defined(SK_BUILD_FOR_UNIX) |
| 22 | #include <GL/gl.h> |
| 23 | #elif defined(SK_BUILD_FOR_MAC) |
jvanverth | 1ba1d37 | 2016-08-04 12:30:31 -0700 | [diff] [blame] | 24 | #include <OpenGL/gl.h> |
Jim Van Verth | ecfed2b | 2017-08-30 14:02:50 -0400 | [diff] [blame] | 25 | #elif defined(SK_BUILD_FOR_IOS) |
| 26 | #include <OpenGLES/ES2/gl.h> |
joshualitt | 04d52f3 | 2015-11-13 11:22:19 -0800 | [diff] [blame] | 27 | #endif |
| 28 | |
| 29 | /* |
| 30 | * This application is a simple example of how to combine SDL and Skia it demonstrates: |
| 31 | * how to setup gpu rendering to the main window |
| 32 | * how to perform cpu-side rendering and draw the result to the gpu-backed screen |
| 33 | * draw simple primitives (rectangles) |
| 34 | * draw more complex primitives (star) |
| 35 | */ |
| 36 | |
| 37 | struct ApplicationState { |
| 38 | ApplicationState() : fQuit(false) {} |
| 39 | // Storage for the user created rectangles. The last one may still be being edited. |
| 40 | SkTArray<SkRect> fRects; |
| 41 | bool fQuit; |
| 42 | }; |
| 43 | |
| 44 | static void handle_error() { |
| 45 | const char* error = SDL_GetError(); |
| 46 | SkDebugf("SDL Error: %s\n", error); |
| 47 | SDL_ClearError(); |
| 48 | } |
| 49 | |
| 50 | static void handle_events(ApplicationState* state, SkCanvas* canvas) { |
| 51 | SDL_Event event; |
| 52 | while(SDL_PollEvent(&event)) { |
| 53 | switch (event.type) { |
| 54 | case SDL_MOUSEMOTION: |
| 55 | if (event.motion.state == SDL_PRESSED) { |
| 56 | SkRect& rect = state->fRects.back(); |
| 57 | rect.fRight = event.motion.x; |
| 58 | rect.fBottom = event.motion.y; |
| 59 | } |
| 60 | break; |
| 61 | case SDL_MOUSEBUTTONDOWN: |
| 62 | if (event.button.state == SDL_PRESSED) { |
| 63 | state->fRects.push_back() = SkRect::MakeLTRB(SkIntToScalar(event.button.x), |
| 64 | SkIntToScalar(event.button.y), |
| 65 | SkIntToScalar(event.button.x), |
| 66 | SkIntToScalar(event.button.y)); |
| 67 | } |
| 68 | break; |
| 69 | case SDL_KEYDOWN: { |
| 70 | SDL_Keycode key = event.key.keysym.sym; |
| 71 | if (key == SDLK_ESCAPE) { |
| 72 | state->fQuit = true; |
| 73 | } |
| 74 | break; |
| 75 | } |
| 76 | case SDL_QUIT: |
| 77 | state->fQuit = true; |
| 78 | break; |
| 79 | default: |
| 80 | break; |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | // Creates a star type shape using a SkPath |
| 86 | static SkPath create_star() { |
| 87 | static const int kNumPoints = 5; |
| 88 | SkPath concavePath; |
| 89 | SkPoint points[kNumPoints] = {{0, SkIntToScalar(-50)} }; |
| 90 | SkMatrix rot; |
| 91 | rot.setRotate(SkIntToScalar(360) / kNumPoints); |
| 92 | for (int i = 1; i < kNumPoints; ++i) { |
| 93 | rot.mapPoints(points + i, points + i - 1, 1); |
| 94 | } |
| 95 | concavePath.moveTo(points[0]); |
| 96 | for (int i = 0; i < kNumPoints; ++i) { |
| 97 | concavePath.lineTo(points[(2 * i) % kNumPoints]); |
| 98 | } |
| 99 | concavePath.setFillType(SkPath::kEvenOdd_FillType); |
| 100 | SkASSERT(!concavePath.isConvex()); |
| 101 | concavePath.close(); |
| 102 | return concavePath; |
| 103 | } |
| 104 | |
| 105 | #if defined(SK_BUILD_FOR_ANDROID) |
| 106 | int SDL_main(int argc, char** argv) { |
| 107 | #else |
| 108 | int main(int argc, char** argv) { |
| 109 | #endif |
| 110 | uint32_t windowFlags = 0; |
| 111 | |
| 112 | SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); |
| 113 | SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0); |
| 114 | |
| 115 | SDL_GLContext glContext = nullptr; |
Jim Van Verth | 2259bd4 | 2017-09-01 14:49:09 -0400 | [diff] [blame] | 116 | #if defined(SK_BUILD_FOR_ANDROID) || defined(SK_BUILD_FOR_IOS) |
| 117 | // For Android/iOS we need to set up for OpenGL ES and we make the window hi res & full screen |
joshualitt | 04d52f3 | 2015-11-13 11:22:19 -0800 | [diff] [blame] | 118 | SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES); |
| 119 | windowFlags = SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | |
| 120 | SDL_WINDOW_BORDERLESS | SDL_WINDOW_FULLSCREEN_DESKTOP | |
| 121 | SDL_WINDOW_ALLOW_HIGHDPI; |
| 122 | #else |
| 123 | // For all other clients we use the core profile and operate in a window |
| 124 | SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE); |
| 125 | |
| 126 | windowFlags = SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE; |
| 127 | #endif |
| 128 | static const int kStencilBits = 8; // Skia needs 8 stencil bits |
| 129 | SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8); |
| 130 | SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8); |
| 131 | SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8); |
| 132 | SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); |
| 133 | SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 0); |
| 134 | SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, kStencilBits); |
| 135 | |
| 136 | SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL, 1); |
| 137 | |
| 138 | // If you want multisampling, uncomment the below lines and set a sample count |
| 139 | static const int kMsaaSampleCount = 0; //4; |
| 140 | // SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1); |
| 141 | // SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, kMsaaSampleCount); |
| 142 | |
| 143 | /* |
| 144 | * In a real application you might want to initialize more subsystems |
| 145 | */ |
| 146 | if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS) != 0) { |
| 147 | handle_error(); |
| 148 | return 1; |
| 149 | } |
| 150 | |
| 151 | // Setup window |
| 152 | // This code will create a window with the same resolution as the user's desktop. |
| 153 | SDL_DisplayMode dm; |
| 154 | if (SDL_GetDesktopDisplayMode(0, &dm) != 0) { |
| 155 | handle_error(); |
| 156 | return 1; |
| 157 | } |
| 158 | |
| 159 | SDL_Window* window = SDL_CreateWindow("SDL Window", SDL_WINDOWPOS_CENTERED, |
| 160 | SDL_WINDOWPOS_CENTERED, dm.w, dm.h, windowFlags); |
| 161 | |
| 162 | if (!window) { |
| 163 | handle_error(); |
| 164 | return 1; |
| 165 | } |
| 166 | |
| 167 | // To go fullscreen |
| 168 | // SDL_SetWindowFullscreen(window, SDL_WINDOW_FULLSCREEN); |
| 169 | |
| 170 | // try and setup a GL context |
| 171 | glContext = SDL_GL_CreateContext(window); |
| 172 | if (!glContext) { |
| 173 | handle_error(); |
| 174 | return 1; |
| 175 | } |
| 176 | |
| 177 | int success = SDL_GL_MakeCurrent(window, glContext); |
| 178 | if (success != 0) { |
| 179 | handle_error(); |
| 180 | return success; |
| 181 | } |
| 182 | |
Greg Daniel | faa095e | 2017-12-19 13:15:02 -0500 | [diff] [blame] | 183 | uint32_t windowFormat = SDL_GetWindowPixelFormat(window); |
| 184 | int contextType; |
| 185 | SDL_GL_GetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, &contextType); |
| 186 | |
| 187 | |
Jim Van Verth | 2259bd4 | 2017-09-01 14:49:09 -0400 | [diff] [blame] | 188 | int dw, dh; |
| 189 | SDL_GL_GetDrawableSize(window, &dw, &dh); |
| 190 | |
| 191 | glViewport(0, 0, dw, dh); |
joshualitt | 04d52f3 | 2015-11-13 11:22:19 -0800 | [diff] [blame] | 192 | glClearColor(1, 1, 1, 1); |
| 193 | glClearStencil(0); |
| 194 | glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); |
| 195 | |
| 196 | // setup GrContext |
Brian Salomon | 3d6801e | 2017-12-11 10:06:31 -0500 | [diff] [blame] | 197 | auto interface = GrGLMakeNativeInterface(); |
joshualitt | 04d52f3 | 2015-11-13 11:22:19 -0800 | [diff] [blame] | 198 | |
joshualitt | 04d52f3 | 2015-11-13 11:22:19 -0800 | [diff] [blame] | 199 | // setup contexts |
Brian Salomon | 384fab4 | 2017-12-07 12:33:05 -0500 | [diff] [blame] | 200 | sk_sp<GrContext> grContext(GrContext::MakeGL(interface)); |
joshualitt | 04d52f3 | 2015-11-13 11:22:19 -0800 | [diff] [blame] | 201 | SkASSERT(grContext); |
| 202 | |
| 203 | // Wrap the frame buffer object attached to the screen in a Skia render target so Skia can |
| 204 | // render to it |
joshualitt | 04d52f3 | 2015-11-13 11:22:19 -0800 | [diff] [blame] | 205 | GrGLint buffer; |
Jim Van Verth | 4c70c75 | 2017-07-11 12:03:01 -0400 | [diff] [blame] | 206 | GR_GL_GetIntegerv(interface.get(), GR_GL_FRAMEBUFFER_BINDING, &buffer); |
| 207 | GrGLFramebufferInfo info; |
| 208 | info.fFBOID = (GrGLuint) buffer; |
Greg Daniel | faa095e | 2017-12-19 13:15:02 -0500 | [diff] [blame] | 209 | SkColorType colorType; |
| 210 | |
Ben Wagner | f8a131d | 2018-03-13 16:56:43 -0400 | [diff] [blame] | 211 | //SkDebugf("%s", SDL_GetPixelFormatName(windowFormat)); |
| 212 | // TODO: the windowFormat is never any of these? |
Greg Daniel | faa095e | 2017-12-19 13:15:02 -0500 | [diff] [blame] | 213 | if (SDL_PIXELFORMAT_RGBA8888 == windowFormat) { |
| 214 | info.fFormat = GR_GL_RGBA8; |
| 215 | colorType = kRGBA_8888_SkColorType; |
| 216 | } else { |
Greg Daniel | faa095e | 2017-12-19 13:15:02 -0500 | [diff] [blame] | 217 | colorType = kBGRA_8888_SkColorType; |
| 218 | if (SDL_GL_CONTEXT_PROFILE_ES == contextType) { |
| 219 | info.fFormat = GR_GL_BGRA8; |
| 220 | } else { |
| 221 | // We assume the internal format is RGBA8 on desktop GL |
| 222 | info.fFormat = GR_GL_RGBA8; |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | GrBackendRenderTarget target(dw, dh, kMsaaSampleCount, kStencilBits, info); |
joshualitt | 04d52f3 | 2015-11-13 11:22:19 -0800 | [diff] [blame] | 227 | |
| 228 | // setup SkSurface |
| 229 | // To use distance field text, use commented out SkSurfaceProps instead |
| 230 | // SkSurfaceProps props(SkSurfaceProps::kUseDeviceIndependentFonts_Flag, |
| 231 | // SkSurfaceProps::kLegacyFontHost_InitType); |
| 232 | SkSurfaceProps props(SkSurfaceProps::kLegacyFontHost_InitType); |
robertphillips | 12e9662 | 2016-08-01 05:53:23 -0700 | [diff] [blame] | 233 | |
Jim Van Verth | 4c70c75 | 2017-07-11 12:03:01 -0400 | [diff] [blame] | 234 | sk_sp<SkSurface> surface(SkSurface::MakeFromBackendRenderTarget(grContext.get(), target, |
Greg Daniel | bcf612b | 2017-05-01 13:50:58 +0000 | [diff] [blame] | 235 | kBottomLeft_GrSurfaceOrigin, |
Greg Daniel | faa095e | 2017-12-19 13:15:02 -0500 | [diff] [blame] | 236 | colorType, nullptr, &props)); |
joshualitt | 04d52f3 | 2015-11-13 11:22:19 -0800 | [diff] [blame] | 237 | |
| 238 | SkCanvas* canvas = surface->getCanvas(); |
Jim Van Verth | 2259bd4 | 2017-09-01 14:49:09 -0400 | [diff] [blame] | 239 | canvas->scale((float)dw/dm.w, (float)dh/dm.h); |
joshualitt | 04d52f3 | 2015-11-13 11:22:19 -0800 | [diff] [blame] | 240 | |
| 241 | ApplicationState state; |
| 242 | |
| 243 | const char* helpMessage = "Click and drag to create rects. Press esc to quit."; |
| 244 | |
| 245 | SkPaint paint; |
| 246 | |
| 247 | // create a surface for CPU rasterization |
jvanverth | 1ba1d37 | 2016-08-04 12:30:31 -0700 | [diff] [blame] | 248 | sk_sp<SkSurface> cpuSurface(SkSurface::MakeRaster(canvas->imageInfo())); |
joshualitt | 04d52f3 | 2015-11-13 11:22:19 -0800 | [diff] [blame] | 249 | |
| 250 | SkCanvas* offscreen = cpuSurface->getCanvas(); |
| 251 | offscreen->save(); |
| 252 | offscreen->translate(50.0f, 50.0f); |
| 253 | offscreen->drawPath(create_star(), paint); |
| 254 | offscreen->restore(); |
| 255 | |
jvanverth | 1ba1d37 | 2016-08-04 12:30:31 -0700 | [diff] [blame] | 256 | sk_sp<SkImage> image = cpuSurface->makeImageSnapshot(); |
joshualitt | 04d52f3 | 2015-11-13 11:22:19 -0800 | [diff] [blame] | 257 | |
| 258 | int rotation = 0; |
| 259 | while (!state.fQuit) { // Our application loop |
| 260 | SkRandom rand; |
| 261 | canvas->clear(SK_ColorWHITE); |
| 262 | handle_events(&state, canvas); |
| 263 | |
| 264 | paint.setColor(SK_ColorBLACK); |
| 265 | canvas->drawText(helpMessage, strlen(helpMessage), SkIntToScalar(100), |
| 266 | SkIntToScalar(100), paint); |
| 267 | for (int i = 0; i < state.fRects.count(); i++) { |
| 268 | paint.setColor(rand.nextU() | 0x44808080); |
| 269 | canvas->drawRect(state.fRects[i], paint); |
| 270 | } |
| 271 | |
| 272 | // draw offscreen canvas |
| 273 | canvas->save(); |
| 274 | canvas->translate(dm.w / 2.0, dm.h / 2.0); |
| 275 | canvas->rotate(rotation++); |
| 276 | canvas->drawImage(image, -50.0f, -50.0f); |
| 277 | canvas->restore(); |
| 278 | |
| 279 | canvas->flush(); |
| 280 | SDL_GL_SwapWindow(window); |
| 281 | } |
| 282 | |
| 283 | if (glContext) { |
| 284 | SDL_GL_DeleteContext(glContext); |
| 285 | } |
| 286 | |
| 287 | //Destroy window |
| 288 | SDL_DestroyWindow(window); |
| 289 | |
| 290 | //Quit SDL subsystems |
| 291 | SDL_Quit(); |
| 292 | return 0; |
| 293 | } |