void Render() { struct PP_Size* psize = &g_Context.size; PP_ImageDataFormat format = PP_IMAGEDATAFORMAT_BGRA_PREMUL; PP_Resource image = g_pImageData->Create(PSGetInstanceId(), format, psize, PP_FALSE); uint8_t* pixels = g_pImageData->Map(image); struct PP_ImageDataDesc desc; uint8_t* cell_temp; uint32_t x, y; if (!g_Context.cell_in || !g_Context.cell_out) return; g_pImageData->Describe(image, &desc); Stir(desc.size.width, desc.size.height); for (y = 1; y < desc.size.height - 1; ++y) { uint8_t *src0 = (g_Context.cell_in + (y - 1) * desc.size.width) + 1; uint8_t *src1 = src0 + desc.size.width; uint8_t *src2 = src1 + desc.size.width; int count; uint32_t color; uint8_t *dst = (g_Context.cell_out + y * desc.size.width) + 1; uint32_t *pixel_line = (uint32_t*) (pixels + y * desc.stride); for (x = 1; x < (desc.size.width - 1); ++x) { count = src0[-1] + src0[0] + src0[1] + src1[-1] + src1[0] * 9 + src1[1] + src2[-1] + src2[0] + src2[1]; color = kNeighborColors[count]; *pixel_line++ = color; *dst++ = kIsAlive[count]; ++src0; ++src1; ++src2; } } cell_temp = g_Context.cell_in; g_Context.cell_in = g_Context.cell_out; g_Context.cell_out = cell_temp; g_pImageData->Unmap(image); g_pGraphics2D->ReplaceContents(g_Context.ctx, image); g_pGraphics2D->Flush(g_Context.ctx, PP_BlockUntilComplete()); g_pCore->ReleaseResource(image); }
byte RandomPool::GenerateByte() { if (getPos == pool.size()) Stir(); return pool[getPos++]; }
unsigned int RandomPool::Get(byte &outByte) { if (getPos == pool.size) Stir(); outByte = pool[getPos++]; return 1; }
void RandomPool::Put(byte inByte) { if (addPos == pool.size) Stir(); pool[addPos++] ^= inByte; getPos = pool.size; // Force stir on get }
void OldRandomPool::GenerateIntoBufferedTransformation(BufferedTransformation &target, const std::string &channel, lword size) { while (size > 0) { if (getPos == pool.size()) Stir(); size_t t = UnsignedMin(pool.size() - getPos, size); target.ChannelPut(channel, pool+getPos, t); size -= t; getPos += t; }}
void OldRandomPool::IncorporateEntropy(const byte *input, size_t length) { size_t t; while (length > (t = pool.size() - addPos)) { xorbuf(pool+addPos, input, t); input += t; length -= t; Stir(); } if (length) { xorbuf(pool+addPos, input, length); addPos += length; getPos = pool.size(); // Force stir on get } }
void RandomPool::GenerateBlock(byte *outString, unsigned int size) { unsigned t; while (size > (t = pool.size - getPos)) { memcpy(outString, pool+getPos, t); outString += t; size -= t; Stir(); } if (size) { memcpy(outString, pool+getPos, size); getPos += size; } }
void RandomPool::Put(const byte *inString, unsigned int length) { unsigned t; while (length > (t = pool.size - addPos)) { xorbuf(pool+addPos, inString, t); inString += t; length -= t; Stir(); } if (length) { xorbuf(pool+addPos, inString, length); addPos += length; getPos = pool.size; // Force stir on get } }
unsigned int RandomPool::Get(byte *outString, unsigned int getMax) { unsigned t; unsigned int length = getMax; while (length > (t = pool.size - getPos)) { memcpy(outString, pool+getPos, t); outString += t; length -= t; Stir(); } if (length) { memcpy(outString, pool+getPos, length); getPos += length; } return getMax; }
unsigned int RandomPool::Put2(const byte *inString, unsigned int length, int messageEnd, bool blocking) { unsigned t; while (length > (t = pool.size() - addPos)) { xorbuf(pool+addPos, inString, t); inString += t; length -= t; Stir(); } if (length) { xorbuf(pool+addPos, inString, length); addPos += length; getPos = pool.size(); // Force stir on get } return 0; }
unsigned int RandomPool::TransferTo2(BufferedTransformation &target, unsigned long &transferBytes, const std::string &channel, bool blocking) { if (!blocking) throw NotImplemented("RandomPool: nonblocking transfer is not implemented by this object"); unsigned int t; unsigned long size = transferBytes; while (size > (t = pool.size() - getPos)) { target.ChannelPut(channel, pool+getPos, t); size -= t; Stir(); } if (size) { target.ChannelPut(channel, pool+getPos, size); getPos += size; } return 0; }
void Render() { struct PP_Size* psize = &g_Context.size; PP_ImageDataFormat format = PP_IMAGEDATAFORMAT_BGRA_PREMUL; /* * Create a buffer to draw into. Since we are waiting until the next flush * chrome has an opportunity to cache this buffer see ppb_graphics_2d.h. */ PP_Resource image = g_pImageData->Create(PSGetInstanceId(), format, psize, PP_FALSE); uint8_t* pixels = g_pImageData->Map(image); struct PP_ImageDataDesc desc; uint8_t* cell_temp; uint32_t x, y; /* If we somehow have not allocated these pointers yet, skip this frame. */ if (!g_Context.cell_in || !g_Context.cell_out) return; /* Get the stride. */ g_pImageData->Describe(image, &desc); /* Stir up the edges to prevent the simulation from reaching steady state. */ Stir(desc.size.width, desc.size.height); /* Do neighbor summation; apply rules, output pixel color. */ for (y = 1; y < desc.size.height - 1; ++y) { uint8_t *src0 = (g_Context.cell_in + (y - 1) * desc.size.width) + 1; uint8_t *src1 = src0 + desc.size.width; uint8_t *src2 = src1 + desc.size.width; int count; uint32_t color; uint8_t *dst = (g_Context.cell_out + y * desc.size.width) + 1; uint32_t *pixel_line = (uint32_t*) (pixels + y * desc.stride); for (x = 1; x < (desc.size.width - 1); ++x) { /* Jitter and sum neighbors. */ count = src0[-1] + src0[0] + src0[1] + src1[-1] + + src1[1] + src2[-1] + src2[0] + src2[1]; /* Include center cell. */ count = count + count + src1[0]; /* Use table lookup indexed by count to determine pixel & alive state. */ color = kNeighborColors[count]; *pixel_line++ = color; *dst++ = kIsAlive[count]; ++src0; ++src1; ++src2; } } cell_temp = g_Context.cell_in; g_Context.cell_in = g_Context.cell_out; g_Context.cell_out = cell_temp; /* Unmap the range, we no longer need it. */ g_pImageData->Unmap(image); /* Replace the contexts, and block until it's on the screen. */ g_pGraphics2D->ReplaceContents(g_Context.ctx, image); g_pGraphics2D->Flush(g_Context.ctx, PP_BlockUntilComplete()); /* Release the image data, we no longer need it. */ g_pCore->ReleaseResource(image); }