SkiaSDLExample.cpp 9.4 KB

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