Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 /* Copyright 2013 The Native Client SDK Authors. All rights reserved. | |
| 2 * Use of this source code is governed by a BSD-style license that can | |
| 3 * be found in the LICENSE file. */ | |
| 4 | |
| 5 #include <assert.h> | |
| 6 #include <stdio.h> | |
| 7 #include <stdlib.h> | |
| 8 #include <string.h> | |
| 9 | |
| 10 #include "ppapi/c/pp_resource.h" | |
| 11 #include "ppapi/c/ppb_core.h" | |
| 12 #include "ppapi/c/ppb_fullscreen.h" | |
| 13 #include "ppapi/c/ppb_graphics_2d.h" | |
| 14 #include "ppapi/c/ppb_image_data.h" | |
| 15 #include "ppapi/c/ppb_input_event.h" | |
| 16 #include "ppapi/c/ppb_instance.h" | |
| 17 #include "ppapi/c/ppb_view.h" | |
| 18 | |
| 19 #include "ppapi_simple/ps_event.h" | |
| 20 #include "ppapi_simple/ps_main.h" | |
| 21 | |
| 22 PPB_Core* g_pCore; | |
| 23 PPB_Fullscreen* g_pFullscreen; | |
| 24 PPB_Graphics2D* g_pGraphics2D; | |
| 25 PPB_ImageData* g_pImageData; | |
| 26 PPB_Instance* g_pInstance; | |
| 27 PPB_View* g_pView; | |
| 28 PPB_InputEvent* g_pInputEvent; | |
| 29 PPB_KeyboardInputEvent* g_pKeyboardInput; | |
| 30 PPB_MouseInputEvent* g_pMouseInput; | |
| 31 PPB_TouchInputEvent* g_pTouchInput; | |
| 32 | |
| 33 struct { | |
| 34 PP_Resource ctx; | |
| 35 struct PP_Size size; | |
| 36 int bound; | |
| 37 uint8_t* cell_in; | |
| 38 uint8_t* cell_out; | |
| 39 } g_Context; | |
| 40 | |
| 41 | |
| 42 const unsigned int kInitialRandSeed = 0xC0DE533D; | |
| 43 | |
| 44 #define MakeRGBA(r, g, b, a) \ | |
| 45 (((a) << 24) | ((r) << 16) | ((g) << 8) | (b)) | |
| 46 | |
| 47 | |
| 48 /* | |
| 49 * Given a count of cells in a 3x3 grid where cells are worth 1 except for | |
| 50 * the center which is worth 9, this is a color representation of how | |
| 51 * "alive" that cell is making for a more interesting representation than | |
| 52 * a binary alive or dead. | |
| 53 */ | |
| 54 const uint32_t kNeighborColors[] = { | |
| 55 MakeRGBA(0x00, 0x00, 0x00, 0xff), | |
| 56 MakeRGBA(0x00, 0x40, 0x00, 0xff), | |
| 57 MakeRGBA(0x00, 0x60, 0x00, 0xff), | |
| 58 MakeRGBA(0x00, 0x80, 0x00, 0xff), | |
| 59 MakeRGBA(0x00, 0xA0, 0x00, 0xff), | |
| 60 MakeRGBA(0x00, 0xC0, 0x00, 0xff), | |
| 61 MakeRGBA(0x00, 0xE0, 0x00, 0xff), | |
| 62 MakeRGBA(0x00, 0x00, 0x00, 0xff), | |
| 63 MakeRGBA(0x00, 0x40, 0x00, 0xff), | |
| 64 MakeRGBA(0x00, 0x60, 0x00, 0xff), | |
| 65 MakeRGBA(0x00, 0x80, 0x00, 0xff), | |
| 66 MakeRGBA(0x00, 0xA0, 0x00, 0xff), | |
| 67 MakeRGBA(0x00, 0xC0, 0x00, 0xff), | |
| 68 MakeRGBA(0x00, 0xE0, 0x00, 0xff), | |
| 69 MakeRGBA(0x00, 0xFF, 0x00, 0xff), | |
| 70 MakeRGBA(0x00, 0xFF, 0x00, 0xff), | |
| 71 MakeRGBA(0x00, 0xFF, 0x00, 0xff), | |
| 72 MakeRGBA(0x00, 0xFF, 0x00, 0xff), | |
| 73 }; | |
| 74 | |
| 75 /* | |
| 76 * These represent the new health value of a cell based on its neighboring | |
| 77 * values. The health is binary: either alive or dead. | |
| 78 */ | |
| 79 const uint8_t kIsAlive[] = { | |
| 80 0, 0, 0, 1, 0, 0, 0, 0, 0, /* Values if the center cell is dead. */ | |
| 81 0, 0, 1, 1, 0, 0, 0, 0, 0 /* Values if the center cell is alive. */ | |
| 82 }; | |
| 83 | |
| 84 void UpdateContext(uint32_t width, uint32_t height) { | |
| 85 if (width != g_Context.size.width || height != g_Context.size.height) { | |
| 86 size_t size = width * height; | |
| 87 size_t index; | |
| 88 | |
| 89 free(g_Context.cell_in); | |
| 90 free(g_Context.cell_out); | |
| 91 | |
| 92 /* Create a new context */ | |
| 93 g_Context.cell_in = (uint8_t*) malloc(size); | |
| 94 g_Context.cell_out = (uint8_t*) malloc(size); | |
| 95 | |
| 96 memset(g_Context.cell_out, 0, size); | |
| 97 for (index = 0; index < size; index++) { | |
| 98 g_Context.cell_in[index] = rand() & 1; | |
| 99 } | |
| 100 } | |
| 101 | |
| 102 /* Recreate the graphics context on a view change */ | |
| 103 g_pCore->ReleaseResource(g_Context.ctx); | |
| 104 g_Context.size.width = width; | |
| 105 g_Context.size.height = height; | |
| 106 g_Context.ctx = | |
| 107 g_pGraphics2D->Create(PSGetInstanceId(), &g_Context.size, PP_TRUE); | |
| 108 g_Context.bound = | |
| 109 g_pInstance->BindGraphics(PSGetInstanceId(), g_Context.ctx); | |
| 110 } | |
| 111 | |
| 112 void DrawCell(int32_t x, int32_t y) { | |
| 113 int32_t width = g_Context.size.width; | |
| 114 int32_t height = g_Context.size.height; | |
| 115 | |
| 116 if (!g_Context.cell_in) return; | |
| 117 | |
| 118 if (x > 0 && x < width - 1 && y > 0 && y < height - 1) { | |
| 119 g_Context.cell_in[x - 1 + y * width] = 1; | |
| 120 g_Context.cell_in[x + 1 + y * width] = 1; | |
| 121 g_Context.cell_in[x + (y - 1) * width] = 1; | |
| 122 g_Context.cell_in[x + (y + 1) * width] = 1; | |
| 123 } | |
| 124 } | |
| 125 | |
| 126 | |
| 127 void ProcessEvent(PSEvent* event) { | |
| 128 switch(event->type) { | |
| 129 /* If the view updates, build a new Graphics 2D Context */ | |
| 130 case PSE_INSTANCE_DIDCHANGEVIEW: { | |
| 131 struct PP_Rect rect; | |
| 132 | |
| 133 g_pView->GetRect(event->as_resource, &rect); | |
| 134 UpdateContext(rect.size.width, rect.size.height); | |
| 135 break; | |
| 136 } | |
| 137 | |
| 138 case PSE_INSTANCE_HANDLEINPUT: { | |
| 139 PP_InputEvent_Type type = g_pInputEvent->GetType(event->as_resource); | |
| 140 PP_InputEvent_Modifier modifiers = | |
| 141 g_pInputEvent->GetModifiers(event->as_resource); | |
| 142 | |
| 143 switch(type) { | |
| 144 case PP_INPUTEVENT_TYPE_MOUSEDOWN: { | |
| 145 struct PP_Point location = | |
| 146 g_pMouseInput->GetPosition(event->as_resource); | |
| 147 DrawCell(location.x, location.y); | |
| 148 break; | |
| 149 } | |
| 150 | |
| 151 case PP_INPUTEVENT_TYPE_MOUSEMOVE: { | |
| 152 struct PP_Point location = | |
| 153 g_pMouseInput->GetPosition(event->as_resource); | |
| 154 | |
| 155 /* If the button is down, draw */ | |
| 156 if (modifiers & PP_INPUTEVENT_MODIFIER_LEFTBUTTONDOWN) { | |
| 157 DrawCell(location.x, location.y); | |
| 158 } | |
| 159 break; | |
| 160 } | |
| 161 | |
| 162 case PP_INPUTEVENT_TYPE_KEYDOWN: { | |
| 163 PP_Bool fullscreen = g_pFullscreen->IsFullscreen(PSGetInstanceId()); | |
| 164 g_pFullscreen->SetFullscreen(PSGetInstanceId(), | |
| 165 fullscreen ? PP_FALSE : PP_TRUE); | |
| 166 break; | |
| 167 } | |
| 168 default: | |
| 169 break; | |
| 170 } | |
| 171 /* case PSE_INSTANCE_HANDLEINPUT */ | |
| 172 break; | |
| 173 } | |
| 174 | |
| 175 default: | |
| 176 break; | |
| 177 } | |
| 178 } | |
| 179 | |
| 180 | |
| 181 void Stir(uint32_t width, uint32_t height) { | |
| 182 int i; | |
| 183 if (g_Context.cell_in == NULL || g_Context.cell_out == NULL) | |
| 184 return; | |
| 185 | |
| 186 for (i = 0; i < width; ++i) { | |
| 187 g_Context.cell_in[i] = rand() & 1; | |
| 188 g_Context.cell_in[i + (height - 1) * width] = rand() & 1; | |
| 189 } | |
| 190 for (i = 0; i < height; ++i) { | |
| 191 g_Context.cell_in[i * width] = rand() & 1; | |
| 192 g_Context.cell_in[i * width + (width - 1)] = rand() & 1; | |
| 193 } | |
| 194 } | |
| 195 | |
| 196 void Render() { | |
| 197 struct PP_Size* psize = &g_Context.size; | |
| 198 PP_ImageDataFormat format = g_pImageData->GetNativeImageDataFormat(); | |
| 199 | |
| 200 /* | |
| 201 * Create a buffer to draw into. Since we are waiting until the next flush | |
| 202 * chrome has an opportunity to cache this buffer see ppb_graphics_2d.h. | |
| 203 */ | |
| 204 PP_Resource image = | |
| 205 g_pImageData->Create(PSGetInstanceId(), format, psize, PP_FALSE); | |
| 206 uint8_t* pixels = g_pImageData->Map(image); | |
| 207 | |
| 208 struct PP_ImageDataDesc desc; | |
| 209 uint8_t* cell_temp; | |
| 210 uint32_t x, y; | |
| 211 | |
| 212 // If we somehow have not allocated these pointers yet, skip this frame. | |
|
nfullagar1
2013/05/30 21:07:31
/* */ line above
noelallen1
2013/05/30 21:58:03
Done.
| |
| 213 if (!g_Context.cell_in || !g_Context.cell_out) return; | |
| 214 | |
| 215 /* Get the stride. */ | |
| 216 g_pImageData->Describe(image, &desc); | |
| 217 | |
| 218 /* Stir up the edges to prevent the simulation from reaching steady state. */ | |
| 219 Stir(desc.size.width, desc.size.height); | |
| 220 | |
| 221 /* Do neighbor summation; apply rules, output pixel color. */ | |
| 222 for (y = 1; y < desc.size.height - 1; ++y) { | |
| 223 uint8_t *src0 = (g_Context.cell_in + (y - 1) * desc.size.width) + 1; | |
| 224 uint8_t *src1 = src0 + desc.size.width; | |
| 225 uint8_t *src2 = src1 + desc.size.width; | |
| 226 int count; | |
| 227 uint32_t color; | |
| 228 uint8_t *dst = (g_Context.cell_out + y * desc.size.width) + 1; | |
| 229 uint32_t *pixel_line = (uint32_t*) (pixels + y * desc.stride); | |
| 230 | |
| 231 for (x = 1; x < (desc.size.width - 1); ++x) { | |
| 232 /* Build sum, weight center by 9x. */ | |
| 233 count = src0[-1] + src0[0] + src0[1] + | |
| 234 src1[-1] + src1[0] * 9 + src1[1] + | |
| 235 src2[-1] + src2[0] + src2[1]; | |
| 236 color = kNeighborColors[count]; | |
| 237 | |
| 238 *pixel_line++ = color; | |
| 239 *dst++ = kIsAlive[count]; | |
| 240 ++src0; | |
| 241 ++src1; | |
| 242 ++src2; | |
| 243 } | |
| 244 } | |
| 245 | |
| 246 cell_temp = g_Context.cell_in; | |
| 247 g_Context.cell_in = g_Context.cell_out; | |
| 248 g_Context.cell_out = cell_temp; | |
| 249 | |
| 250 /* Unmap the range, we no longer need it. */ | |
| 251 g_pImageData->Unmap(image); | |
| 252 | |
| 253 /* Replace the contexts, and block until it's on the screen. */ | |
| 254 g_pGraphics2D->ReplaceContents(g_Context.ctx, image); | |
| 255 g_pGraphics2D->Flush(g_Context.ctx, PP_BlockUntilComplete()); | |
| 256 | |
| 257 /* Release the image data, we no longer need it. */ | |
| 258 g_pCore->ReleaseResource(image); | |
| 259 } | |
| 260 | |
| 261 /* | |
| 262 * Starting point for the module. We do not use main since it would | |
| 263 * collide with main in libppapi_cpp. | |
| 264 */ | |
| 265 int example_main(int argc, const char *argv[]) { | |
| 266 fprintf(stdout,"Started main.\n"); | |
| 267 g_pCore = (PPB_Core*)PSGetInterface(PPB_CORE_INTERFACE); | |
| 268 g_pFullscreen = (PPB_Fullscreen*)PSGetInterface(PPB_FULLSCREEN_INTERFACE); | |
| 269 g_pGraphics2D = (PPB_Graphics2D*)PSGetInterface(PPB_GRAPHICS_2D_INTERFACE); | |
| 270 g_pInstance = (PPB_Instance*)PSGetInterface(PPB_INSTANCE_INTERFACE); | |
| 271 g_pImageData = (PPB_ImageData*)PSGetInterface(PPB_IMAGEDATA_INTERFACE); | |
| 272 g_pView = (PPB_View*)PSGetInterface(PPB_VIEW_INTERFACE); | |
| 273 | |
| 274 g_pInputEvent = | |
| 275 (PPB_InputEvent*) PSGetInterface(PPB_INPUT_EVENT_INTERFACE); | |
| 276 g_pKeyboardInput = (PPB_KeyboardInputEvent*) | |
| 277 PSGetInterface(PPB_KEYBOARD_INPUT_EVENT_INTERFACE); | |
| 278 g_pMouseInput = | |
| 279 (PPB_MouseInputEvent*) PSGetInterface(PPB_MOUSE_INPUT_EVENT_INTERFACE); | |
| 280 g_pTouchInput = | |
| 281 (PPB_TouchInputEvent*) PSGetInterface(PPB_TOUCH_INPUT_EVENT_INTERFACE); | |
| 282 | |
| 283 PSEventSetFilter(PSE_ALL); | |
| 284 while (1) { | |
| 285 /* Process all waiting events without blocking */ | |
| 286 PSEvent* event; | |
| 287 while ((event = PSEventTryAcquire()) != NULL) { | |
| 288 ProcessEvent(event); | |
| 289 PSEventRelease(event); | |
| 290 } | |
| 291 | |
| 292 /* Render a frame, blocking until complete. */ | |
| 293 if (g_Context.bound) { | |
| 294 Render(); | |
| 295 } | |
| 296 } | |
| 297 return 0; | |
| 298 } | |
| 299 | |
| 300 /* | |
| 301 * Register the function to call once the Instance Object is initialized. | |
| 302 * see: pappi_simple/ps_main.h | |
| 303 */ | |
| 304 PPAPI_SIMPLE_REGISTER_MAIN(example_main); | |
| OLD | NEW |