OLD | NEW |
---|---|
(Empty) | |
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 | |
9 #include "GrContext.h" | |
10 #include "SDL.h" | |
11 #include "SkCanvas.h" | |
12 #include "SkRandom.h" | |
13 #include "SkSurface.h" | |
14 | |
15 #include "gl/GrGLInterface.h" | |
16 #include "gl/GrGLUtil.h" | |
17 | |
18 #if defined(SK_BUILD_FOR_ANDROID) | |
19 #include <GLES/gl.h> | |
20 #elif defined(SK_BUILD_FOR_UNIX) | |
21 #include <GL/gl.h> | |
22 #elif defined(SK_BUILD_FOR_MAC) | |
23 #include <gl.h> | |
24 #endif | |
25 | |
26 /* | |
27 * This application is a simple example of how to combine SDL and Skia it demons trates: | |
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 scr een | |
30 * draw simple primitives (rectangles) | |
31 * draw more complex primitives (star) | |
32 */ | |
33 | |
34 struct ApplicationState { | |
35 // Storage for the user created rectangles. The last one may still be being edited. | |
36 SkTArray<SkRect> fRects; | |
37 bool fQuit; | |
38 }; | |
39 | |
40 static void handle_error() { | |
41 const char* error = SDL_GetError(); | |
42 SkDebugf("SDL Error: %s\n", error); | |
43 SDL_ClearError(); | |
44 } | |
45 | |
46 static void handle_events(ApplicationState* state, SkCanvas* canvas) { | |
47 SDL_Event event; | |
48 while(SDL_PollEvent(&event)) { | |
49 switch (event.type) { | |
50 case SDL_MOUSEMOTION: | |
51 if (event.motion.state == SDL_PRESSED) { | |
52 SkRect& rect = state->fRects.back(); | |
53 rect.fRight = event.motion.x; | |
54 rect.fBottom = event.motion.y; | |
55 } | |
56 break; | |
57 case SDL_MOUSEBUTTONDOWN: | |
58 if (event.button.state == SDL_PRESSED) { | |
59 state->fRects.push_back() = SkRect::MakeLTRB(SkIntToScalar(e vent.button.x), | |
60 SkIntToScalar(e vent.button.y), | |
61 SkIntToScalar(e vent.button.x), | |
62 SkIntToScalar(e vent.button.y)); | |
63 } | |
64 break; | |
65 case SDL_KEYDOWN: { | |
66 SDL_Keycode key = event.key.keysym.sym; | |
67 if (key == SDLK_ESCAPE) { | |
68 state->fQuit = true; | |
69 } | |
70 break; | |
71 } | |
72 case SDL_QUIT: | |
73 state->fQuit = true; | |
74 break; | |
75 default: | |
76 break; | |
77 } | |
78 } | |
79 } | |
80 | |
81 // Creates a star type shape using a SkPath | |
82 static SkPath create_star() { | |
83 static const int kNumPoints = 5; | |
84 SkPath concavePath; | |
85 SkPoint points[kNumPoints] = {{0, SkIntToScalar(-50)} }; | |
86 SkMatrix rot; | |
87 rot.setRotate(SkIntToScalar(360) / kNumPoints); | |
88 for (int i = 1; i < kNumPoints; ++i) { | |
89 rot.mapPoints(points + i, points + i - 1, 1); | |
90 } | |
91 concavePath.moveTo(points[0]); | |
92 for (int i = 0; i < kNumPoints; ++i) { | |
93 concavePath.lineTo(points[(2 * i) % kNumPoints]); | |
94 } | |
95 concavePath.setFillType(SkPath::kEvenOdd_FillType); | |
96 SkASSERT(!concavePath.isConvex()); | |
97 concavePath.close(); | |
98 return concavePath; | |
99 } | |
100 | |
101 #if defined(SK_BUILD_FOR_ANDROID) | |
102 int SDL_main(int argc, char** argv) { | |
103 #else | |
104 int main(int argc, char** argv) { | |
105 #endif | |
106 uint32_t windowFlags = 0; | |
107 | |
108 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); | |
109 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0); | |
110 | |
111 SDL_GLContext glContext = nullptr; | |
112 #if defined(SK_BUILD_FOR_ANDROID) | |
113 // For Android we need to set up for OpenGL ES and we make the window hi res & full screen | |
114 SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES); | |
115 windowFlags = SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | | |
116 SDL_WINDOW_BORDERLESS | SDL_WINDOW_FULLSCREEN_DESKTOP | | |
117 SDL_WINDOW_ALLOW_HIGHDPI; | |
118 #else | |
119 // For all other clients we use the core profile and operate in a window | |
120 SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE ); | |
121 | |
122 windowFlags = SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE; | |
123 #endif | |
124 static const int kStencilBits = 8; // Skia needs 8 stencil bits | |
125 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8); | |
126 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8); | |
127 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8); | |
128 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); | |
129 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 0); | |
130 SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, kStencilBits); | |
131 | |
132 SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL, 1); | |
133 | |
134 // If you want multisampling, uncomment the below lines and set a sample cou nt | |
135 static const int kMsaaSampleCount = 0; //4; | |
136 // SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1); | |
137 // SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, kMsaaSampleCount); | |
138 | |
139 /* | |
140 * In a real application you might want to initialize more subsystems | |
141 */ | |
142 if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS) != 0) { | |
143 handle_error(); | |
144 return 1; | |
145 } | |
146 | |
147 // Setup window | |
148 // This code will create a window with the same resolution as the user's des ktop. | |
149 SDL_DisplayMode dm; | |
150 if (SDL_GetDesktopDisplayMode(0, &dm) != 0) { | |
151 handle_error(); | |
152 return 1; | |
153 } | |
154 | |
155 SDL_Window* window = SDL_CreateWindow("SDL Window", SDL_WINDOWPOS_CENTERED, | |
156 SDL_WINDOWPOS_CENTERED, dm.w, dm.h, wi ndowFlags); | |
157 | |
158 if (!window) { | |
159 handle_error(); | |
160 return 1; | |
161 } | |
162 | |
163 // To go fullscreen | |
164 // SDL_SetWindowFullscreen(window, SDL_WINDOW_FULLSCREEN); | |
165 | |
166 // try and setup a GL context | |
167 glContext = SDL_GL_CreateContext(window); | |
168 if (!glContext) { | |
169 handle_error(); | |
170 return 1; | |
171 } | |
172 | |
173 int success = SDL_GL_MakeCurrent(window, glContext); | |
174 if (success != 0) { | |
175 handle_error(); | |
176 return success; | |
177 } | |
178 | |
179 glViewport(0, 0, dm.w, dm.h); | |
180 glClearColor(1, 1, 1, 1); | |
181 glClearStencil(0); | |
182 glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); | |
183 | |
184 // setup GrContext | |
185 SkAutoTUnref<const GrGLInterface> interface(GrGLCreateNativeInterface()); | |
186 | |
187 // To use NVPR, comment this out | |
188 interface.reset(GrGLInterfaceRemoveNVPR(interface)); | |
189 SkASSERT(interface); | |
190 | |
191 // setup contexts | |
192 SkAutoTUnref<GrContext> grContext(GrContext::Create(kOpenGL_GrBackend, | |
193 (GrBackendContext)interf ace.get())); | |
194 SkASSERT(grContext); | |
195 | |
196 // Wrap the frame buffer object attached to the screen in a Skia render targ et so Skia can | |
197 // render to it | |
198 GrBackendRenderTargetDesc desc; | |
199 desc.fWidth = dm.w; | |
200 desc.fHeight = dm.h; | |
201 desc.fConfig = kSkia8888_GrPixelConfig; | |
202 desc.fOrigin = kBottomLeft_GrSurfaceOrigin; | |
robertphillips
2015/11/13 19:02:59
Is the comment still right ?
| |
203 desc.fSampleCnt = kMsaaSampleCount; // This would have to change for MSAA | |
204 desc.fStencilBits = kStencilBits; | |
205 GrGLint buffer; | |
206 GR_GL_GetIntegerv(interface, GR_GL_FRAMEBUFFER_BINDING, &buffer); | |
207 desc.fRenderTargetHandle = buffer; | |
208 SkAutoTUnref<GrRenderTarget> | |
209 renderTarget(grContext->textureProvider()->wrapBackendRenderTarget(d esc)); | |
210 | |
211 // setup SkSurface | |
212 // To use distance field text, use commented out SkSurfaceProps instead | |
213 // SkSurfaceProps props(SkSurfaceProps::kUseDeviceIndependentFonts_Flag, | |
214 // SkSurfaceProps::kLegacyFontHost_InitType); | |
215 SkSurfaceProps props(SkSurfaceProps::kLegacyFontHost_InitType); | |
216 SkAutoTUnref<SkSurface> surface(SkSurface::NewRenderTargetDirect(renderTarge t, &props)); | |
217 | |
218 SkCanvas* canvas = surface->getCanvas(); | |
219 | |
220 ApplicationState state; | |
robertphillips
2015/11/13 19:02:59
I'm surprised SkTArray doesn't gripe about being b
| |
221 sk_bzero(&state, sizeof(ApplicationState)); | |
222 | |
223 const char* helpMessage = "Click and drag to create rects. Press esc to qui t."; | |
224 | |
225 SkPaint paint; | |
226 | |
227 // create a surface for CPU rasterization | |
228 SkAutoTUnref<SkSurface> cpuSurface(SkSurface::NewRaster(canvas->imageInfo()) ); | |
229 | |
230 SkCanvas* offscreen = cpuSurface->getCanvas(); | |
231 offscreen->save(); | |
232 offscreen->translate(50.0f, 50.0f); | |
233 offscreen->drawPath(create_star(), paint); | |
234 offscreen->restore(); | |
235 | |
236 SkAutoTUnref<SkImage> image(cpuSurface->newImageSnapshot()); | |
237 | |
238 int rotation = 0; | |
239 while (!state.fQuit) { // Our application loop | |
240 SkRandom rand; | |
241 canvas->clear(SK_ColorWHITE); | |
242 handle_events(&state, canvas); | |
243 | |
244 paint.setColor(SK_ColorBLACK); | |
245 canvas->drawText(helpMessage, strlen(helpMessage), SkIntToScalar(100), | |
246 SkIntToScalar(100), paint); | |
247 for (int i = 0; i < state.fRects.count(); i++) { | |
248 paint.setColor(rand.nextU() | 0x44808080); | |
249 canvas->drawRect(state.fRects[i], paint); | |
250 } | |
251 | |
252 // draw offscreen canvas | |
253 canvas->save(); | |
254 canvas->translate(dm.w / 2.0, dm.h / 2.0); | |
255 canvas->rotate(rotation++); | |
256 canvas->drawImage(image, -50.0f, -50.0f); | |
257 canvas->restore(); | |
258 | |
259 canvas->flush(); | |
260 SDL_GL_SwapWindow(window); | |
261 } | |
262 | |
263 if (glContext) { | |
264 SDL_GL_DeleteContext(glContext); | |
265 } | |
266 | |
267 //Destroy window | |
268 SDL_DestroyWindow(window); | |
269 | |
270 //Quit SDL subsystems | |
271 SDL_Quit(); | |
272 return 0; | |
273 } | |
OLD | NEW |