void* SkDeque::push_front() { fCount += 1; if (NULL == fFront) { fFront = (Head*)sk_malloc_throw(sizeof(Head) + INIT_ELEM_COUNT * fElemSize); fFront->init(sizeof(Head) + INIT_ELEM_COUNT * fElemSize); fBack = fFront; // update our linklist } Head* first = fFront; char* begin; if (NULL == first->fBegin) { INIT_CHUNK: first->fEnd = first->fStop; begin = first->fStop - fElemSize; } else { begin = first->fBegin - fElemSize; if (begin < first->start()) { // no more room in this chunk // should we alloc more as we accumulate more elements? size_t size = sizeof(Head) + INIT_ELEM_COUNT * fElemSize; first = (Head*)sk_malloc_throw(size); first->init(size); first->fNext = fFront; fFront->fPrev = first; fFront = first; goto INIT_CHUNK; } } first->fBegin = begin; return begin; }
void* SkDeque::push_back() { fCount += 1; if (NULL == fBack) { fBack = (Head*)sk_malloc_throw(sizeof(Head) + INIT_ELEM_COUNT * fElemSize); fBack->init(sizeof(Head) + INIT_ELEM_COUNT * fElemSize); fFront = fBack; // update our linklist } Head* last = fBack; char* end; if (NULL == last->fBegin) { INIT_CHUNK: last->fBegin = last->start(); end = last->fBegin + fElemSize; } else { end = last->fEnd + fElemSize; if (end > last->fStop) { // no more room in this chunk // should we alloc more as we accumulate more elements? size_t size = sizeof(Head) + INIT_ELEM_COUNT * fElemSize; last = (Head*)sk_malloc_throw(size); last->init(size); last->fPrev = fBack; fBack->fNext = last; fBack = last; goto INIT_CHUNK; } } last->fEnd = end; return end - fElemSize; }
static void write_png(const png_bytep rgba, png_uint_32 width, png_uint_32 height, SkWStream& out) { png_structp png = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); SkASSERT(png != nullptr); png_infop info_ptr = png_create_info_struct(png); SkASSERT(info_ptr != nullptr); if (setjmp(png_jmpbuf(png))) { SkFAIL("png encode error"); } png_set_IHDR(png, info_ptr, width, height, 8, PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT); png_set_compression_level(png, 1); png_bytepp rows = (png_bytepp) sk_malloc_throw(height * sizeof(png_byte*)); png_bytep pixels = (png_bytep) sk_malloc_throw(width * height * 3); for (png_size_t y = 0; y < height; ++y) { const png_bytep src = rgba + y * width * 4; rows[y] = pixels + y * width * 3; // convert from RGBA to RGB for (png_size_t x = 0; x < width; ++x) { rows[y][x * 3] = src[x * 4]; rows[y][x * 3 + 1] = src[x * 4 + 1]; rows[y][x * 3 + 2] = src[x * 4 + 2]; } } png_set_filter(png, 0, PNG_NO_FILTERS); png_set_rows(png, info_ptr, &rows[0]); png_set_write_fn(png, &out, write_png_callback, NULL); png_write_png(png, info_ptr, PNG_TRANSFORM_IDENTITY, NULL); png_destroy_write_struct(&png, NULL); sk_free(rows); }
SkScalerContext_Ascender::SkScalerContext_Ascender(const SkDescriptor* desc) : SkScalerContext(desc) { int size = aca_Get_FontHandleRec_Size(); fHandle = (aca_FontHandle)sk_malloc_throw(size); // get the pointer to the font fFontStream = new SkMMAPStream("/UcsGB2312-Hei-H.FDL"); fHintStream = new SkMMAPStream("/genv6-23.bin"); void* hints = sk_malloc_throw(fHintStream->getLength()); memcpy(hints, fHintStream->getMemoryBase(), fHintStream->getLength()); aca_Create_Font_Handle(fHandle, (void*)fFontStream->getMemoryBase(), fFontStream->getLength(), "fred", hints, fHintStream->getLength()); // compute our factors from the record SkMatrix m; fRec.getSingleMatrix(&m); // now compute our scale factors SkScalar sx = m.getScaleX(); SkScalar sy = m.getScaleY(); int ppemX = SkScalarRound(sx); int ppemY = SkScalarRound(sy); size = aca_Find_Font_Memory_Required(fHandle, ppemX, ppemY); size *= 8; // Jeff suggests this :) fWorkspace = sk_malloc_throw(size); aca_Set_Font_Memory(fHandle, (uint8_t*)fWorkspace, size); aca_GlyphAttribsRec rec; memset(&rec, 0, sizeof(rec)); rec.xSize = ppemX; rec.ySize = ppemY; rec.doAdjust = true; rec.doExceptions = true; rec.doGlyphHints = true; rec.doInterpolate = true; rec.grayMode = 2; aca_Set_Font_Attributes(fHandle, &rec, &size); fGlyphWorkspace = sk_malloc_throw(size); aca_Set_Glyph_Memory(fHandle, fGlyphWorkspace); }
char* SkOrderedReadBuffer::readString() { const char* string = fReader.readString(); const int32_t length = strlen(string); char* value = (char*)sk_malloc_throw(length + 1); strcpy(value, string); return value; }
SkDashPathEffect::SkDashPathEffect(SkReadBuffer& buffer) : INHERITED(buffer) { bool useOldPic = buffer.isVersionLT(SkReadBuffer::kDashWritesPhaseIntervals_Version); if (useOldPic) { fInitialDashIndex = buffer.readInt(); fInitialDashLength = buffer.readScalar(); fIntervalLength = buffer.readScalar(); buffer.readBool(); // Dummy for old ScalarToFit field } else { fPhase = buffer.readScalar(); } fCount = buffer.getArrayCount(); size_t allocSize = sizeof(SkScalar) * fCount; if (buffer.validateAvailable(allocSize)) { fIntervals = (SkScalar*)sk_malloc_throw(allocSize); buffer.readScalarArray(fIntervals, fCount); } else { fIntervals = NULL; } if (useOldPic) { fPhase = 0; if (fInitialDashLength != -1) { // Signal for bad dash interval for (int i = 0; i < fInitialDashIndex; ++i) { fPhase += fIntervals[i]; } fPhase += fIntervals[fInitialDashIndex] - fInitialDashLength; } } else { this->setInternalMembers(fPhase); } }
const GrIndexBuffer* GrGpu::getQuadIndexBuffer() const { if (NULL == fQuadIndexBuffer || fQuadIndexBuffer->wasDestroyed()) { SkSafeUnref(fQuadIndexBuffer); static const int SIZE = sizeof(uint16_t) * 6 * MAX_QUADS; GrGpu* me = const_cast<GrGpu*>(this); fQuadIndexBuffer = me->createIndexBuffer(SIZE, false); if (fQuadIndexBuffer) { uint16_t* indices = (uint16_t*)fQuadIndexBuffer->map(); if (indices) { fill_indices(indices, MAX_QUADS); fQuadIndexBuffer->unmap(); } else { indices = (uint16_t*)sk_malloc_throw(SIZE); fill_indices(indices, MAX_QUADS); if (!fQuadIndexBuffer->updateData(indices, SIZE)) { fQuadIndexBuffer->unref(); fQuadIndexBuffer = NULL; SkFAIL("Can't get indices into buffer!"); } sk_free(indices); } } } return fQuadIndexBuffer; }
SkPDFShader::State::State(const SkShader& shader, const SkMatrix& canvasTransform, const SkIRect& bbox) : fCanvasTransform(canvasTransform), fBBox(bbox), fPixelGeneration(0) { fInfo.fColorCount = 0; fInfo.fColors = NULL; fInfo.fColorOffsets = NULL; shader.getLocalMatrix(&fShaderTransform); fImageTileModes[0] = fImageTileModes[1] = SkShader::kClamp_TileMode; fType = shader.asAGradient(&fInfo); if (fType == SkShader::kNone_GradientType) { SkShader::BitmapType bitmapType; SkMatrix matrix; bitmapType = shader.asABitmap(&fImage, &matrix, fImageTileModes, NULL); if (bitmapType != SkShader::kDefault_BitmapType) { fImage.reset(); return; } SkASSERT(matrix.isIdentity()); fPixelGeneration = fImage.getGenerationID(); } else { fColorData.set(sk_malloc_throw( fInfo.fColorCount * (sizeof(SkColor) + sizeof(SkScalar)))); fInfo.fColors = reinterpret_cast<SkColor*>(fColorData.get()); fInfo.fColorOffsets = reinterpret_cast<SkScalar*>(fInfo.fColors + fInfo.fColorCount); shader.asAGradient(&fInfo); } }
int FindOrAdd(IDWriteFontFileLoader* fontFileLoader, const void* refKey, UINT32 refKeySize) const { SkTScopedComPtr<IUnknown> fontFileLoaderId; HR_GENERAL(fontFileLoader->QueryInterface(&fontFileLoaderId), "Failed to re-convert to IDWriteFontFileLoader.", SkFontIdentity::kInvalidDataId); SkAutoMutexAcquire ama(fDataIdCacheMutex); int count = fDataIdCache.count(); int i; for (i = 0; i < count; ++i) { const DataId& current = fDataIdCache[i]; if (fontFileLoaderId.get() == current.fLoader && refKeySize == current.fKeySize && 0 == memcmp(refKey, current.fKey, refKeySize)) { return i; } } DataId& added = fDataIdCache.push_back(); added.fLoader = fontFileLoaderId.release(); // Ref is passed. added.fKey = sk_malloc_throw(refKeySize); memcpy(added.fKey, refKey, refKeySize); added.fKeySize = refKeySize; return i; }
void SkPDFShader::State::AllocateGradientInfoStorage() { fColorData.set(sk_malloc_throw( fInfo.fColorCount * (sizeof(SkColor) + sizeof(SkScalar)))); fInfo.fColors = reinterpret_cast<SkColor*>(fColorData.get()); fInfo.fColorOffsets = reinterpret_cast<SkScalar*>(fInfo.fColors + fInfo.fColorCount); }
SkGradientShaderBase::SkGradientShaderBase(SkFlattenableReadBuffer& buffer) : INHERITED(buffer) { fCacheAlpha = 256; fMapper = buffer.readFlattenableT<SkUnitMapper>(); fCache16 = fCache16Storage = NULL; fCache32 = NULL; fCache32PixelRef = NULL; int colorCount = fColorCount = buffer.getArrayCount(); if (colorCount > kColorStorageCount) { size_t size = sizeof(SkColor) + sizeof(SkPMColor) + sizeof(Rec); fOrigColors = (SkColor*)sk_malloc_throw(size * colorCount); } else { fOrigColors = fStorage; } buffer.readColorArray(fOrigColors); fTileMode = (TileMode)buffer.readUInt(); fTileProc = gTileProcs[fTileMode]; fRecs = (Rec*)(fOrigColors + colorCount); if (colorCount > 2) { Rec* recs = fRecs; recs[0].fPos = 0; for (int i = 1; i < colorCount; i++) { recs[i].fPos = buffer.readInt(); recs[i].fScale = buffer.readUInt(); } } buffer.readMatrix(&fPtsToUnit); this->initCommon(); }
SkAutoAsciiToLC::SkAutoAsciiToLC(const char str[], size_t len) { // see if we need to compute the length if ((long)len < 0) { len = strlen(str); } fLength = len; // assign lc to our preallocated storage if len is small enough, or allocate // it on the heap char* lc; if (len <= STORAGE) { lc = fStorage; } else { lc = (char*)sk_malloc_throw(len + 1); } fLC = lc; // convert any asii to lower-case. we let non-ascii (utf8) chars pass // through unchanged for (int i = (int)(len - 1); i >= 0; --i) { int c = str[i]; if ((c & 0x80) == 0) { // is just ascii c = tolower(c); } lc[i] = c; } lc[len] = 0; }
static bool SK_WARN_UNUSED_RESULT flatten(const SkImage& image, Json::Value* target, bool sendBinaries) { if (sendBinaries) { SkData* encoded = image.encode(SkImageEncoder::kPNG_Type, 100); if (encoded == nullptr) { // PNG encode doesn't necessarily support all color formats, convert to a different // format size_t rowBytes = 4 * image.width(); void* buffer = sk_malloc_throw(rowBytes * image.height()); SkImageInfo dstInfo = SkImageInfo::Make(image.width(), image.height(), kN32_SkColorType, kPremul_SkAlphaType); if (!image.readPixels(dstInfo, buffer, rowBytes, 0, 0)) { SkDebugf("readPixels failed\n"); return false; } SkImage* converted = SkImage::NewRasterCopy(dstInfo, buffer, rowBytes); encoded = converted->encode(SkImageEncoder::kPNG_Type, 100); if (encoded == nullptr) { SkDebugf("image encode failed\n"); return false; } free(converted); free(buffer); } Json::Value bytes; encode_data(encoded->data(), encoded->size(), &bytes); (*target)[SKJSONCANVAS_ATTRIBUTE_BYTES] = bytes; encoded->unref(); } else { SkString description = SkStringPrintf("%dx%d pixel image", image.width(), image.height()); (*target)[SKJSONCANVAS_ATTRIBUTE_DESCRIPTION] = Json::Value(description.c_str()); } return true; }
SkARGB32_Shader_Blitter::SkARGB32_Shader_Blitter(const SkBitmap& device, const SkPaint& paint) : INHERITED(device, paint) { fBuffer = (SkPMColor*)sk_malloc_throw(device.width() * (sizeof(SkPMColor))); (fXfermode = paint.getXfermode())->safeRef(); }
const GrIndexBuffer* GrGpu::getQuadIndexBuffer() const { if (NULL == fQuadIndexBuffer) { static const int SIZE = sizeof(uint16_t) * 6 * MAX_QUADS; GrGpu* me = const_cast<GrGpu*>(this); fQuadIndexBuffer = me->createIndexBuffer(SIZE, false); if (NULL != fQuadIndexBuffer) { uint16_t* indices = (uint16_t*)fQuadIndexBuffer->lock(); if (NULL != indices) { fill_indices(indices, MAX_QUADS); fQuadIndexBuffer->unlock(); } else { indices = (uint16_t*)sk_malloc_throw(SIZE); fill_indices(indices, MAX_QUADS); if (!fQuadIndexBuffer->updateData(indices, SIZE)) { fQuadIndexBuffer->unref(); fQuadIndexBuffer = NULL; GrCrash("Can't get indices into buffer!"); } sk_free(indices); } } } return fQuadIndexBuffer; }
sk_sp<SkDataTable> SkDataTable::MakeCopyArrays(const void * const * ptrs, const size_t sizes[], int count) { if (count <= 0) { return SkDataTable::MakeEmpty(); } size_t dataSize = 0; for (int i = 0; i < count; ++i) { dataSize += sizes[i]; } size_t bufferSize = count * sizeof(Dir) + dataSize; void* buffer = sk_malloc_throw(bufferSize); Dir* dir = (Dir*)buffer; char* elem = (char*)(dir + count); for (int i = 0; i < count; ++i) { dir[i].fPtr = elem; dir[i].fSize = sizes[i]; memcpy(elem, ptrs[i], sizes[i]); elem += sizes[i]; } return sk_sp<SkDataTable>(new SkDataTable(dir, count, malloc_freeproc, buffer)); }
SkData* SkWriter32::snapshotAsData() const { // get a non const version of this, we are only conceptually const SkWriter32& mutable_this = *const_cast<SkWriter32*>(this); // we use size change detection to invalidate the cached data if ((fSnapshot.get() != NULL) && (fSnapshot->size() != fUsed)) { mutable_this.fSnapshot.reset(NULL); } if (fSnapshot.get() == NULL) { uint8_t* buffer = NULL; if ((fExternal != NULL) && (fData == fExternal)) { // We need to copy to an allocated buffer before returning. buffer = (uint8_t*)sk_malloc_throw(fUsed); memcpy(buffer, fData, fUsed); } else { buffer = mutable_this.fInternal.detach(); // prepare us to do copy on write, by pretending the data buffer // is external and size limited mutable_this.fData = buffer; mutable_this.fCapacity = fUsed; mutable_this.fExternal = buffer; } mutable_this.fSnapshot.reset(SkData::NewFromMalloc(buffer, fUsed)); } return SkRef(fSnapshot.get()); // Take an extra ref for the caller. }
static void setup_MC_state(SkMCState* state, const SkMatrix& matrix, const SkRegion& clip) { // initialize the struct state->clipRectCount = 0; // capture the matrix for (int i = 0; i < 9; i++) { state->matrix[i] = matrix.get(i); } /* * capture the clip * * storage is allocated on the stack for the first 4 rects. This value was * chosen somewhat arbitrarily, but does allow us to represent simple clips * and some more common complex clips (e.g. a clipRect with a sub-rect * clipped out of its interior) without needing to malloc any additional memory. */ SkSWriter32<4*sizeof(ClipRect)> clipWriter; if (!clip.isEmpty()) { // only returns the b/w clip so aa clips fail SkRegion::Iterator clip_iterator(clip); for (; !clip_iterator.done(); clip_iterator.next()) { // this assumes the SkIRect is stored in l,t,r,b ordering which // matches the ordering of our ClipRect struct clipWriter.writeIRect(clip_iterator.rect()); state->clipRectCount++; } } // allocate memory for the clip then and copy them to the struct state->clipRects = (ClipRect*) sk_malloc_throw(clipWriter.bytesWritten()); clipWriter.flatten(state->clipRects); }
static void apply_paint_patheffect(const SkPaint& paint, Json::Value* target, bool sendBinaries) { SkPathEffect* pathEffect = paint.getPathEffect(); if (pathEffect != nullptr) { SkPathEffect::DashInfo dashInfo; SkPathEffect::DashType dashType = pathEffect->asADash(&dashInfo); if (dashType == SkPathEffect::kDash_DashType) { dashInfo.fIntervals = (SkScalar*) sk_malloc_throw(dashInfo.fCount * sizeof(SkScalar)); pathEffect->asADash(&dashInfo); Json::Value dashing(Json::objectValue); Json::Value intervals(Json::arrayValue); for (int32_t i = 0; i < dashInfo.fCount; i++) { intervals.append(Json::Value(dashInfo.fIntervals[i])); } free(dashInfo.fIntervals); dashing[SKJSONCANVAS_ATTRIBUTE_INTERVALS] = intervals; dashing[SKJSONCANVAS_ATTRIBUTE_PHASE] = dashInfo.fPhase; (*target)[SKJSONCANVAS_ATTRIBUTE_DASHING] = dashing; } else { Json::Value jsonPathEffect; flatten(pathEffect, &jsonPathEffect, sendBinaries); (*target)[SKJSONCANVAS_ATTRIBUTE_PATHEFFECT] = jsonPathEffect; } } }
static CGDataProviderRef SkStreamToDataProvider(SkStream* stream) { // TODO: use callbacks, so we don't have to load all the data into RAM size_t len = stream->getLength(); void* data = sk_malloc_throw(len); stream->read(data, len); return CGDataProviderCreateWithData(data, data, len, malloc_release_proc); }
static Block* Create(size_t size) { SkASSERT(SkAlign4(size) == size); Block* block = (Block*)sk_malloc_throw(sizeof(Block) + size); block->fNext = NULL; block->fSizeOfBlock = size; block->fAllocatedSoFar = 0; return block; }
void* SkOrderedReadBuffer::readEncodedString(size_t* length, SkPaint::TextEncoding encoding) { int32_t encodingType = fReader.readInt(); SkASSERT(encodingType == encoding); *length = fReader.readInt(); void* data = sk_malloc_throw(*length); memcpy(data, fReader.skip(SkAlign4(*length)), *length); return data; }
SkFlipPixelRef::SkFlipPixelRef(SkFlattenableReadBuffer& buffer) : INHERITED(buffer, NULL) { fSize = buffer.readU32(); fStorage = sk_malloc_throw(fSize << 1); fPage0 = fStorage; fPage1 = (char*)fStorage + fSize; buffer.read(fPage0, fSize); }
SkData* SkValidatingSerializeFlattenable(SkFlattenable* flattenable) { SkWriteBuffer writer(SkWriteBuffer::kValidation_Flag); writer.writeFlattenable(flattenable); size_t size = writer.bytesWritten(); void* data = sk_malloc_throw(size); writer.writeToMemory(data); return SkData::NewFromMalloc(data, size); }
static SkBufferBlock* Alloc(size_t length) { size_t capacity = LengthToCapacity(length); SkBufferBlock* block = (SkBufferBlock*)sk_malloc_throw(sizeof(SkBufferBlock) + capacity); block->fNext = NULL; block->fUsed = 0; block->fCapacity = capacity; return block; }
void SkColorTable::init(const SkPMColor colors[], int count) { SkASSERT((unsigned)count <= 256); fCount = count; fColors = reinterpret_cast<SkPMColor*>(sk_malloc_throw(count * sizeof(SkPMColor))); memcpy(fColors, colors, count * sizeof(SkPMColor)); }
Entry(const void* buffer, size_t size, const SkBitmap& bm) : fPrev(nullptr), fNext(nullptr), fBitmap(bm) { fBuffer = sk_malloc_throw(size); fSize = size; memcpy(fBuffer, buffer, size); }
SkFlipPixelRef::SkFlipPixelRef(SkBitmap::Config config, int width, int height) : fFlipper(width, height) { fConfig = config; fSize = SkBitmap::ComputeSize(config, width, height); fStorage = sk_malloc_throw(fSize << 1); fPage0 = fStorage; fPage1 = (char*)fStorage + fSize; }
void* PipeController::requestBlock(size_t minRequest, size_t *actual) { sk_free(fBlock); fBlockSize = minRequest; fBlock = sk_malloc_throw(fBlockSize); fBytesWritten = 0; *actual = fBlockSize; return fBlock; }
SkPictureShader::PictureShaderContext::PictureShaderContext( const SkPictureShader& shader, const ContextRec& rec, SkShader* bitmapShader) : INHERITED(shader, rec) , fBitmapShader(SkRef(bitmapShader)) { fBitmapShaderContextStorage = sk_malloc_throw(bitmapShader->contextSize()); fBitmapShaderContext = bitmapShader->createContext(rec, fBitmapShaderContextStorage); //if fBitmapShaderContext is null, we are invalid }