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 | |
robertphillips
2015/11/13 14:02:52
// This application is a simple example of how to
joshualitt
2015/11/13 18:51:55
Acknowledged.
| |
26 struct ApplicationState { | |
robertphillips
2015/11/13 14:02:51
// Storage for the user created rectangles. The la
joshualitt
2015/11/13 18:51:56
Acknowledged.
| |
27 SkTArray<SkRect> fRects; | |
28 bool fQuit; | |
29 }; | |
30 | |
31 static void handle_error() { | |
32 const char* error = SDL_GetError(); | |
33 SkDebugf("SDL Error: %s\n", error); | |
34 SDL_ClearError(); | |
35 } | |
36 | |
37 static void handle_events(ApplicationState* state, SkCanvas* canvas) { | |
38 SDL_Event event; | |
39 while(SDL_PollEvent(&event)) { | |
40 switch (event.type) { | |
41 case SDL_MOUSEMOTION: | |
42 if (event.motion.state == SDL_PRESSED) { | |
robertphillips
2015/11/13 14:02:51
It might be clearer to make this "SkRect& rect ...
joshualitt
2015/11/13 18:51:55
Acknowledged.
| |
43 const SkRect& rect = state->fRects.back(); | |
44 state->fRects.back() = SkRect::MakeLTRB(rect.fLeft, rect.fTo p, | |
45 event.motion.x, even t.motion.y); | |
46 } | |
47 break; | |
48 case SDL_MOUSEBUTTONDOWN: | |
49 if (event.button.state == SDL_PRESSED) { | |
50 state->fRects.push_back() = SkRect::MakeLTRB(SkIntToScalar(e vent.button.x), | |
51 SkIntToScalar(e vent.button.y), | |
52 SkIntToScalar(e vent.button.x), | |
53 SkIntToScalar(e vent.button.y)); | |
54 } | |
55 break; | |
56 case SDL_KEYDOWN: { | |
57 SDL_Keycode key = event.key.keysym.sym; | |
58 if (key == SDLK_ESCAPE) { | |
59 state->fQuit = true; | |
60 } | |
61 break; | |
62 } | |
63 case SDL_QUIT: | |
64 state->fQuit = true; | |
65 break; | |
66 default: | |
67 break; | |
68 } | |
69 } | |
70 } | |
71 | |
robertphillips
2015/11/13 14:02:51
// Create a pentagram
?
maybe rename the function
joshualitt
2015/11/13 18:51:55
Acknowledged.
| |
72 static SkPath setup_concave_path() { | |
robertphillips
2015/11/13 14:02:52
static const int kNumPoints = 5;
joshualitt
2015/11/13 18:51:55
Acknowledged.
| |
73 SkPath concavePath; | |
74 SkPoint points[5] = {{0, SkIntToScalar(-50)} }; | |
75 SkMatrix rot; | |
76 rot.setRotate(SkIntToScalar(360) / 5); | |
77 for (int i = 1; i < 5; ++i) { | |
78 rot.mapPoints(points + i, points + i - 1, 1); | |
79 } | |
80 concavePath.moveTo(points[0]); | |
robertphillips
2015/11/13 14:02:51
shouldn't this loop start at 1 ?
joshualitt
2015/11/13 18:51:55
dunno, it looks right. Why rock the boat? (Unles
robertphillips
2015/11/13 19:02:59
Don't we do a moveTo followed by a lineTo to the s
| |
81 for (int i = 0; i < 5; ++i) { | |
82 concavePath.lineTo(points[(2 * i) % 5]); | |
83 } | |
robertphillips
2015/11/13 14:02:51
concavePath.close() ?
joshualitt
2015/11/13 18:51:54
Acknowledged.
| |
84 concavePath.setFillType(SkPath::kEvenOdd_FillType); | |
85 SkASSERT(!concavePath.isConvex()); | |
86 return concavePath; | |
87 } | |
88 | |
89 /* | |
90 * In this example, we use Skia with SDL. | |
91 */ | |
92 #if defined(SK_BUILD_FOR_ANDROID) | |
93 int SDL_main(int argc, char** argv) { | |
94 #else | |
95 int main(int argc, char** argv) { | |
96 #endif | |
97 uint32_t windowFlags = 0; | |
98 SDL_Window* window = nullptr; | |
99 #if SK_SUPPORT_GPU | |
100 SDL_GLContext glContext = nullptr; | |
101 #if defined(SK_BUILD_FOR_ANDROID) | |
robertphillips
2015/11/13 14:02:51
Can we pull out the two shared lines ?
// For And
joshualitt
2015/11/13 18:51:55
Acknowledged.
| |
102 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); | |
103 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0); | |
104 SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES); | |
105 windowFlags = SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | | |
106 SDL_WINDOW_BORDERLESS | SDL_WINDOW_FULLSCREEN_DESKTOP | | |
107 SDL_WINDOW_ALLOW_HIGHDPI; | |
108 #else | |
109 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); | |
110 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0); | |
robertphillips
2015/11/13 14:02:52
// For all other clients we use the core profile a
joshualitt
2015/11/13 18:51:55
Acknowledged.
| |
111 SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE ); | |
112 | |
113 windowFlags = SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE; | |
114 #endif | |
robertphillips
2015/11/13 14:02:51
Why not 8 ?
joshualitt
2015/11/13 18:51:55
Acknowledged.
| |
115 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5); | |
116 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5); | |
117 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5); | |
118 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); | |
robertphillips
2015/11/13 14:02:52
Can we set depth to 0 ?
joshualitt
2015/11/13 18:51:55
Acknowledged.
| |
119 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24); | |
robertphillips
2015/11/13 14:02:51
// Skia needs 8 stencil bits
joshualitt
2015/11/13 18:51:55
Acknowledged.
| |
120 SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8); | |
121 | |
122 SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL, 1); | |
123 #endif // SK_SUPPORT_GPU | |
124 | |
robertphillips
2015/11/13 14:02:51
put this in the SK_SUPPORT_GPU block ?
where is m
joshualitt
2015/11/13 18:51:55
I setup the static variable, but got rid of the SK
| |
125 // If you want multisampling, uncomment the below lines | |
126 // SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1); | |
127 // SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, msaaSampleCount); | |
128 | |
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 | |
137 // Setup window | |
138 // This code will create a window with the same resolution as the user's des ktop. | |
139 SDL_DisplayMode dm; | |
140 if (SDL_GetDesktopDisplayMode(0, &dm) != 0) { | |
141 handle_error(); | |
142 return 1; | |
143 } | |
144 | |
robertphillips
2015/11/13 14:02:51
rm this ?
joshualitt
2015/11/13 18:51:55
Acknowledged.
| |
145 SkDebugf("Desktop display mode is %dx%d\n", dm.w, dm.h); | |
146 | |
robertphillips
2015/11/13 14:02:51
Add "SDL_Window*" here and remove the above declar
joshualitt
2015/11/13 18:51:55
Acknowledged.
| |
147 window = SDL_CreateWindow("Skia + SDL Window", SDL_WINDOWPOS_CENTERED, SDL_W INDOWPOS_CENTERED, | |
148 dm.w, dm.h, windowFlags); | |
149 | |
150 if (!window) { | |
151 handle_error(); | |
152 return 1; | |
153 } | |
154 | |
155 // To go fullscreen | |
156 // SDL_SetWindowFullscreen(window, SDL_WINDOW_FULLSCREEN); | |
157 | |
158 // try and setup a GL context | |
159 glContext = SDL_GL_CreateContext(window); | |
160 if (!glContext) { | |
161 handle_error(); | |
162 return 1; | |
163 } | |
164 | |
165 int success = SDL_GL_MakeCurrent(window, glContext); | |
166 if (success != 0) { | |
167 handle_error(); | |
168 return success; | |
169 } | |
170 | |
171 glViewport(0, 0, dm.w, dm.h); | |
172 glClearColor(1, 1, 1, 1); | |
173 glClearStencil(0); | |
174 glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); | |
175 | |
176 // setup GrContext | |
177 SkAutoTUnref<const GrGLInterface> interface(GrGLCreateNativeInterface()); | |
178 | |
robertphillips
2015/11/13 14:02:51
uncomment this -> comment this out ?
joshualitt
2015/11/13 18:51:55
Acknowledged.
| |
179 // To use NVPR, uncomment this | |
180 interface.reset(GrGLInterfaceRemoveNVPR(interface)); | |
181 SkASSERT(interface); | |
182 | |
183 // setup contexts | |
184 SkAutoTUnref<GrContext> grContext(GrContext::Create(kOpenGL_GrBackend, | |
185 (GrBackendContext)interf ace.get())); | |
186 SkASSERT(grContext); | |
187 | |
robertphillips
2015/11/13 14:02:51
// Wrap the frame buffer object attached to the sc
joshualitt
2015/11/13 18:51:55
Then its just a function which takes 10 arguments.
| |
188 // setup rendertarget | |
189 GrBackendRenderTargetDesc desc; | |
190 desc.fWidth = dm.w; | |
191 desc.fHeight = dm.h; | |
192 desc.fConfig = kSkia8888_GrPixelConfig; | |
193 desc.fOrigin = kBottomLeft_GrSurfaceOrigin; | |
robertphillips
2015/11/13 14:02:52
kMSAASampleCount ?
joshualitt
2015/11/13 18:51:55
Acknowledged.
| |
194 desc.fSampleCnt = 0; // This would have to change for MSAA | |
195 desc.fStencilBits = 8; | |
196 GrGLint buffer; | |
197 GR_GL_GetIntegerv(interface, GR_GL_FRAMEBUFFER_BINDING, &buffer); | |
198 desc.fRenderTargetHandle = buffer; | |
199 SkAutoTUnref<GrRenderTarget> | |
200 renderTarget(grContext->textureProvider()->wrapBackendRenderTarget(d esc)); | |
201 | |
202 // setup SkSurface | |
203 // To use distance field text, use commented out SkSurfaceProps instead | |
204 // SkSurfaceProps props(SkSurfaceProps::kUseDeviceIndependentFonts_Flag, | |
205 // SkSurfaceProps::kLegacyFontHost_InitType); | |
206 SkSurfaceProps props(SkSurfaceProps::kLegacyFontHost_InitType); | |
207 SkAutoTUnref<SkSurface> surface(SkSurface::NewRenderTargetDirect(renderTarge t, &props)); | |
208 | |
209 SkCanvas* canvas = surface->getCanvas(); | |
210 | |
211 ApplicationState state; | |
212 sk_bzero(&state, sizeof(ApplicationState)); | |
213 | |
214 const char* helpMessage = "Click and drag to create rects. Press esc to qui t."; | |
215 | |
216 SkPaint paint; | |
217 | |
robertphillips
2015/11/13 14:02:52
Can this be "SkImage* make_a_raster_image()" ?
joshualitt
2015/11/13 18:51:55
see my response to wrap_fbo_for_skia.
| |
218 // create a surface for CPU rasterization | |
robertphillips
2015/11/13 14:02:52
offscreenInfo seem unused
joshualitt
2015/11/13 18:51:55
Acknowledged.
| |
219 SkImageInfo offscreenInfo = canvas->imageInfo(); | |
220 offscreenInfo.makeWH(2000, 2000); | |
robertphillips
2015/11/13 14:02:51
That's a big hunk o' memory
Can it just be the siz
joshualitt
2015/11/13 18:51:54
Acknowledged.
| |
221 SkAutoTUnref<SkSurface> cpuSurface(SkSurface::NewRasterN32Premul(5000, 5000) ); | |
222 | |
223 SkCanvas* offscreen = cpuSurface->getCanvas(); | |
224 offscreen->save(); | |
225 offscreen->translate(50.0f, 50.0f); | |
226 offscreen->drawPath(setup_concave_path(), paint); | |
227 offscreen->restore(); | |
228 | |
229 SkAutoTUnref<SkImage> image(cpuSurface->newImageSnapshot()); | |
230 | |
231 int rotation = 0; | |
232 while (!state.fQuit) { // Our application loop | |
233 SkRandom rand; | |
234 canvas->clear(SK_ColorWHITE); | |
235 handle_events(&state, canvas); | |
236 | |
237 paint.setColor(SK_ColorBLACK); | |
238 canvas->drawText(helpMessage, strlen(helpMessage), SkIntToScalar(100), | |
239 SkIntToScalar(100), paint); | |
240 for (int i = 0; i < state.fRects.count(); i++) { | |
241 paint.setColor(rand.nextU() | 0x44808080); | |
242 canvas->drawRect(state.fRects[i], paint); | |
243 } | |
244 | |
245 // draw offscreen canvas | |
246 canvas->save(); | |
247 canvas->translate(dm.w / 2.0, dm.h / 2.0); | |
248 canvas->rotate(rotation++); | |
249 canvas->drawImage(image, -50.0f, -50.0f); | |
250 canvas->restore(); | |
251 | |
252 canvas->flush(); | |
253 SDL_GL_SwapWindow(window); | |
254 } | |
255 | |
256 if (glContext) { | |
257 SDL_GL_DeleteContext(glContext); | |
258 } | |
259 | |
260 //Destroy window | |
261 SDL_DestroyWindow(window); | |
262 | |
263 //Quit SDL subsystems | |
264 SDL_Quit(); | |
265 return 0; | |
266 } | |
OLD | NEW |