From: Eunki, Hong Date: Wed, 16 Mar 2022 18:58:16 +0000 (+0900) Subject: Minor optimization on image loading X-Git-Tag: dali_2.1.16~2 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=3ff3d757a67b27cd14340e6fbd1cc4035d2cde8c;hp=f5c00ac01b401482c5034280f057b7ee429b79eb;p=platform%2Fcore%2Fuifw%2Fdali-adaptor.git Minor optimization on image loading 1. Wrap DALI_UNLIKELY if there was some system / file error. e.g. fread, fseek faild. malloc faild. image size or format not valid. 2. Reduce Dali::Vector operation more times. When we use fixed size of Dali::Vector, use void * or std::uint8_t* instead of &vector[0]. It's for reducing function call of operator[] so we don't check index range, so we don't call Dali::Vector::Count(). 3. Don't setup initial value of buffer for some cases. Note. gausian-blur.cpp defect will be fixed in Heeyong's stride patch. 4. Reduce useless operations on loader-ico.cpp Previous code use 4 times memory + buffer overflow error exist when bitcount is 1 + read mask buffer only for each line. not whole buffer. Change-Id: I28cdd7468ec4d573e53f4f6f7ad2345bca60b273 Signed-off-by: Eunki, Hong --- diff --git a/dali/internal/imaging/common/file-download.cpp b/dali/internal/imaging/common/file-download.cpp index 3866e36..f566757 100644 --- a/dali/internal/imaging/common/file-download.cpp +++ b/dali/internal/imaging/common/file-download.cpp @@ -145,11 +145,14 @@ CURLcode DownloadFileDataByChunk(CURL* curlHandle, Dali::Vector& dataBu } dataBuffer.ResizeUninitialized(dataSize); - size_t offset = 0; - for(size_t i = 0; i < chunks.size(); ++i) + if(DALI_LIKELY(dataSize > 0)) { - memcpy(&dataBuffer[offset], &chunks[i].data[0], chunks[i].data.capacity()); - offset += chunks[i].data.capacity(); + std::uint8_t* dataBufferPtr = dataBuffer.Begin(); + for(size_t i = 0; i < chunks.size(); ++i) + { + memcpy(dataBufferPtr, &chunks[i].data[0], chunks[i].data.capacity()); + dataBufferPtr += chunks[i].data.capacity(); + } } return result; diff --git a/dali/internal/imaging/common/gif-loading.cpp b/dali/internal/imaging/common/gif-loading.cpp index cfb055e..a3f34f0 100644 --- a/dali/internal/imaging/common/gif-loading.cpp +++ b/dali/internal/imaging/common/gif-loading.cpp @@ -260,7 +260,7 @@ struct GifAccessor { LoaderInfo::FileInfo* fi = reinterpret_cast(gifFileType->UserData); - if(fi->position >= fi->length) + if(DALI_UNLIKELY(fi->position >= fi->length)) { return 0; // if at or past end - no } @@ -294,23 +294,23 @@ bool LoaderInfo::FileData::LoadLocalFile() { Internal::Platform::FileReader fileReader(fileName); FILE* fp = fileReader.GetFile(); - if(fp == NULL) + if(DALI_UNLIKELY(fp == NULL)) { return false; } - if(fseek(fp, 0, SEEK_END) <= -1) + if(DALI_UNLIKELY(fseek(fp, 0, SEEK_END) <= -1)) { return false; } length = ftell(fp); - if(length <= -1) + if(DALI_UNLIKELY(length <= -1)) { return false; } - if((!fseek(fp, 0, SEEK_SET))) + if(DALI_LIKELY(!fseek(fp, 0, SEEK_SET))) { globalMap = reinterpret_cast(malloc(sizeof(GifByteType) * length)); length = fread(globalMap, sizeof(GifByteType), length, fp); @@ -330,17 +330,17 @@ bool LoaderInfo::FileData::LoadRemoteFile() size_t dataSize; succeeded = TizenPlatform::Network::DownloadRemoteFileIntoMemory(fileName, dataBuffer, dataSize, MAXIMUM_DOWNLOAD_IMAGE_SIZE); - if(succeeded) + if(DALI_LIKELY(succeeded)) { size_t blobSize = dataBuffer.Size(); - if(blobSize > 0U) + if(DALI_LIKELY(blobSize > 0U)) { // Open a file handle on the memory buffer: Dali::Internal::Platform::FileReader fileReader(dataBuffer, blobSize); FILE* const fp = fileReader.GetFile(); - if(NULL != fp) + if(DALI_LIKELY(NULL != fp)) { - if((!fseek(fp, 0, SEEK_SET))) + if(DALI_LIKELY(!fseek(fp, 0, SEEK_SET))) { globalMap = reinterpret_cast(malloc(sizeof(GifByteType) * blobSize)); length = fread(globalMap, sizeof(GifByteType), blobSize, fp); @@ -368,12 +368,12 @@ bool LoaderInfo::FileData::LoadRemoteFile() * @param[in] index Frame index to be searched in GIF * @return A pointer to the ImageFrame. */ -inline int CombinePixelABGR(int a, int r, int g, int b) +inline std::uint32_t CombinePixelABGR(const std::uint32_t& a, const std::uint32_t& r, const std::uint32_t& g, const std::uint32_t& b) { return (((a) << 24) + ((b) << 16) + ((g) << 8) + (r)); } -inline int PixelLookup(ColorMapObject* colorMap, int index) +inline std::uint32_t PixelLookup(const ColorMapObject* const& colorMap, int index) { return CombinePixelABGR(0xFF, colorMap->Colors[index].Red, colorMap->Colors[index].Green, colorMap->Colors[index].Blue); } @@ -401,25 +401,47 @@ ImageFrame* FindFrame(const GifAnimationData& animated, int index) * @brief Fill in an image with a specific rgba color value. * * @param[in] data A pointer pointing to an image data - * @param[in] row A int containing the number of rows in an image + * @param[in] stride A int containing the number of stride in an image * @param[in] val A uint32_t containing rgba color value * @param[in] x X-coordinate used an offset to calculate pixel position * @param[in] y Y-coordinate used an offset to calculate pixel position * @param[in] width Width of the image * @param[in] height Height of the image */ -void FillImage(uint32_t* data, int row, uint32_t val, int x, int y, int width, int height) +void FillImage(uint32_t* data, int stride, uint32_t val, int x, int y, int width, int height) { - int xAxis, yAxis; uint32_t* pixelPosition; - for(yAxis = 0; yAxis < height; yAxis++) + // Boost time if stride == width and x == 0. We can assume that all pointer is continuous. + if(x == 0 && stride == width) { - pixelPosition = data + ((y + yAxis) * row) + x; - for(xAxis = 0; xAxis < width; xAxis++) + pixelPosition = data + (y * stride); + // Clear as white or transparent + // Special case. we can use memset. + if(val == 0x00 || val == 0xffffffffu) { - *pixelPosition = val; - pixelPosition++; + const std::int8_t setupVal = val & 0xff; + memset(pixelPosition, setupVal, width * height * sizeof(std::uint32_t)); + } + else + { + for(int byteCount = 0; byteCount < width * height; ++byteCount) + { + *pixelPosition = val; + ++pixelPosition; + } + } + } + else + { + for(int yAxis = 0; yAxis < height; ++yAxis) + { + pixelPosition = data + ((y + yAxis) * stride) + x; + for(int xAxis = 0; xAxis < width; ++xAxis) + { + *pixelPosition = val; + ++pixelPosition; + } } } } @@ -428,7 +450,7 @@ void FillImage(uint32_t* data, int row, uint32_t val, int x, int y, int width, i * @brief Fill a rgba data pixle blob with a frame color (bg or trans) * * @param[in] data A pointer pointing to an image data - * @param[in] row A int containing the number of rows in an image + * @param[in] stride A int containing the number of stride in an image * @param[in] gif A pointer pointing to GIF File Type * @param[in] frameInfo A pointer pointing to Frame Information data * @param[in] x X-coordinate used an offset to calculate pixel position @@ -436,7 +458,7 @@ void FillImage(uint32_t* data, int row, uint32_t val, int x, int y, int width, i * @param[in] width Width of the image * @param[in] height Height of the image */ -void FillFrame(uint32_t* data, int row, GifFileType* gif, FrameInfo* frameInfo, int x, int y, int w, int h) +void FillFrame(uint32_t* data, int stride, GifFileType* gif, FrameInfo* frameInfo, int x, int y, int w, int h) { // solid color fill for pre frame region if(frameInfo->transparent < 0) @@ -455,12 +477,12 @@ void FillFrame(uint32_t* data, int row, GifFileType* gif, FrameInfo* frameInfo, } backGroundColor = gif->SBackGroundColor; // and do the fill - FillImage(data, row, CombinePixelABGR(0xff, colorMap->Colors[backGroundColor].Red, colorMap->Colors[backGroundColor].Green, colorMap->Colors[backGroundColor].Blue), x, y, w, h); + FillImage(data, stride, CombinePixelABGR(0xff, colorMap->Colors[backGroundColor].Red, colorMap->Colors[backGroundColor].Green, colorMap->Colors[backGroundColor].Blue), x, y, w, h); } // fill in region with 0 (transparent) else { - FillImage(data, row, 0, x, y, w, h); + FillImage(data, stride, 0, x, y, w, h); } } @@ -650,7 +672,7 @@ bool DecodeImage(GifFileType* gif, GifCachedColorData& gifCachedColor, uint32_t* gifW = sp->ImageDesc.Width; gifH = sp->ImageDesc.Height; - if((gifW < w) || (gifH < h)) + if(DALI_UNLIKELY((gifW < w) || (gifH < h))) { DALI_LOG_ERROR("gifW : %d, w : %d, gifH : %d, h : %d\n", gifW, w, gifH, h); DALI_ASSERT_DEBUG(false && "Dimensions are bigger than the Gif image size"); @@ -660,7 +682,7 @@ bool DecodeImage(GifFileType* gif, GifCachedColorData& gifCachedColor, uint32_t* // build a blob of memory to have pointers to rows of pixels // AND store the decoded gif pixels (1 byte per pixel) as welll rows = static_cast(malloc((gifH * sizeof(GifRowType)) + (gifW * gifH * sizeof(GifPixelType)))); - if(!rows) + if(DALI_UNLIKELY(!rows)) { goto on_error; } @@ -678,7 +700,7 @@ bool DecodeImage(GifFileType* gif, GifCachedColorData& gifCachedColor, uint32_t* { for(yy = intoffset[i]; yy < gifH; yy += intjump[i]) { - if(DGifGetLine(gif, rows[yy], gifW) != GIF_OK) + if(DALI_UNLIKELY(DGifGetLine(gif, rows[yy], gifW) != GIF_OK)) { goto on_error; } @@ -690,7 +712,7 @@ bool DecodeImage(GifFileType* gif, GifCachedColorData& gifCachedColor, uint32_t* { for(yy = 0; yy < gifH; yy++) { - if(DGifGetLine(gif, rows[yy], gifW) != GIF_OK) + if(DALI_UNLIKELY(DGifGetLine(gif, rows[yy], gifW) != GIF_OK)) { goto on_error; } @@ -884,7 +906,7 @@ bool ReadHeader(LoaderInfo& loaderInfo, bool full = true; success = fileData.LoadFile(); - if(!success || !fileData.globalMap) + if(DALI_UNLIKELY(!success || !fileData.globalMap)) { success = false; DALI_LOG_ERROR("LOAD_ERROR_CORRUPT_FILE\n"); @@ -903,7 +925,7 @@ bool ReadHeader(LoaderInfo& loaderInfo, prop.h = gifAccessor.gif->SHeight; // if size is invalid - abort here - if((prop.w < 1) || (prop.h < 1) || (prop.w > IMG_MAX_SIZE) || (prop.h > IMG_MAX_SIZE) || IMG_TOO_BIG(prop.w, prop.h)) + if(DALI_UNLIKELY((prop.w < 1) || (prop.h < 1) || (prop.w > IMG_MAX_SIZE) || (prop.h > IMG_MAX_SIZE) || IMG_TOO_BIG(prop.w, prop.h))) { if(IMG_TOO_BIG(prop.w, prop.h)) { @@ -945,14 +967,14 @@ bool ReadHeader(LoaderInfo& loaderInfo, GifByteType* img; // get image desc - if(DGifGetImageDesc(gifAccessor.gif) == GIF_ERROR) + if(DALI_UNLIKELY(DGifGetImageDesc(gifAccessor.gif) == GIF_ERROR)) { success = false; DALI_LOG_ERROR("LOAD_ERROR_UNKNOWN_FORMAT\n"); break; } // skip decoding and just walk image to next - if(DGifGetCode(gifAccessor.gif, &img_code, &img) == GIF_ERROR) + if(DALI_UNLIKELY(DGifGetCode(gifAccessor.gif, &img_code, &img) == GIF_ERROR)) { success = false; DALI_LOG_ERROR("LOAD_ERROR_UNKNOWN_FORMAT\n"); @@ -975,7 +997,7 @@ bool ReadHeader(LoaderInfo& loaderInfo, { // allocate and save frame with field data frameInfo = NewFrame(animated, -1, 0, 0, imageNumber + 1); - if(!frameInfo) + if(DALI_UNLIKELY(!frameInfo)) { success = false; DALI_LOG_ERROR("LOAD_ERROR_RESOURCE_ALLOCATION_FAILED"); @@ -1007,7 +1029,7 @@ bool ReadHeader(LoaderInfo& loaderInfo, int disposeMode = (ext[1] >> 2) & 0x7; int delay = (int(ext[3]) << 8) | int(ext[2]); frameInfo = NewFrame(animated, transparencyIndex, disposeMode, delay, imageNumber + 1); - if(!frameInfo) + if(DALI_UNLIKELY(!frameInfo)) { success = false; DALI_LOG_ERROR("LOAD_ERROR_RESOURCE_ALLOCATION_FAILED"); @@ -1104,7 +1126,7 @@ bool ReadNextFrame(LoaderInfo& loaderInfo, ImageProperties& prop, // use for w index = animated.currentFrame; // if index is invalid for animated image - error out - if((animated.animated) && ((index <= 0) || (index > animated.frameCount))) + if(DALI_UNLIKELY((animated.animated) && ((index <= 0) || (index > animated.frameCount)))) { DALI_LOG_ERROR("LOAD_ERROR_GENERIC"); return false; @@ -1112,7 +1134,7 @@ bool ReadNextFrame(LoaderInfo& loaderInfo, ImageProperties& prop, // use for w // find the given frame index frame = FindFrame(animated, index); - if(!frame) + if(DALI_UNLIKELY(!frame)) { DALI_LOG_ERROR("LOAD_ERROR_CORRUPT_FILE\n"); return false; @@ -1137,13 +1159,13 @@ bool ReadNextFrame(LoaderInfo& loaderInfo, ImageProperties& prop, // use for w loaderInfo.fileInfo.map = fileData.globalMap; loaderInfo.fileInfo.length = fileData.length; loaderInfo.fileInfo.position = 0; - if(!loaderInfo.fileInfo.map) + if(DALI_UNLIKELY(!loaderInfo.fileInfo.map)) { DALI_LOG_ERROR("LOAD_ERROR_CORRUPT_FILE\n"); return false; } std::unique_ptr gifAccessor = std::make_unique(loaderInfo.fileInfo); - if(!gifAccessor->gif) + if(DALI_UNLIKELY(!gifAccessor->gif)) { DALI_LOG_ERROR("LOAD_ERROR_UNKNOWN_FORMAT\n"); return false; @@ -1158,7 +1180,7 @@ bool ReadNextFrame(LoaderInfo& loaderInfo, ImageProperties& prop, // use for w // walk through gif records in file to figure out info do { - if(DGifGetRecordType(loaderInfo.gifAccessor->gif, &rec) == GIF_ERROR) + if(DALI_UNLIKELY(DGifGetRecordType(loaderInfo.gifAccessor->gif, &rec) == GIF_ERROR)) { DALI_LOG_ERROR("LOAD_ERROR_UNKNOWN_FORMAT\n"); return false; @@ -1186,7 +1208,7 @@ bool ReadNextFrame(LoaderInfo& loaderInfo, ImageProperties& prop, // use for w ImageFrame* thisFrame = NULL; // get image desc - if(DGifGetImageDesc(loaderInfo.gifAccessor->gif) == GIF_ERROR) + if(DALI_UNLIKELY(DGifGetImageDesc(loaderInfo.gifAccessor->gif) == GIF_ERROR)) { DALI_LOG_ERROR("LOAD_ERROR_UNKNOWN_FORMAT\n"); return false; @@ -1204,7 +1226,7 @@ bool ReadNextFrame(LoaderInfo& loaderInfo, ImageProperties& prop, // use for w // allocate it thisFrame->data = new uint32_t[prop.w * prop.h]; - if(!thisFrame->data) + if(DALI_UNLIKELY(!thisFrame->data)) { DALI_LOG_ERROR("LOAD_ERROR_RESOURCE_ALLOCATION_FAILED"); return false; @@ -1243,7 +1265,7 @@ bool ReadNextFrame(LoaderInfo& loaderInfo, ImageProperties& prop, // use for w { // Find last preserved frame. lastPreservedFrame = FindFrame(animated, imageNumber - prevIndex); - if(!lastPreservedFrame) + if(DALI_UNLIKELY(!lastPreservedFrame)) { DALI_LOG_ERROR("LOAD_ERROR_LAST_PRESERVED_FRAME_NOT_FOUND"); return false; @@ -1260,7 +1282,7 @@ bool ReadNextFrame(LoaderInfo& loaderInfo, ImageProperties& prop, // use for w // now draw this frame on top frameInfo = &(thisFrame->info); ClipCoordinates(prop.w, prop.h, &xin, &yin, frameInfo->x, frameInfo->y, frameInfo->w, frameInfo->h, &x, &y, &w, &h); - if(!DecodeImage(loaderInfo.gifAccessor->gif, loaderInfo.cachedColor, thisFrame->data, prop.w, xin, yin, frameInfo->transparent, x, y, w, h, first)) + if(DALI_UNLIKELY(!DecodeImage(loaderInfo.gifAccessor->gif, loaderInfo.cachedColor, thisFrame->data, prop.w, xin, yin, frameInfo->transparent, x, y, w, h, first))) { DALI_LOG_ERROR("LOAD_ERROR_CORRUPT_FILE\n"); return false; @@ -1286,7 +1308,7 @@ bool ReadNextFrame(LoaderInfo& loaderInfo, ImageProperties& prop, // use for w FillFrame(reinterpret_cast(pixels), prop.w, loaderInfo.gifAccessor->gif, frameInfo, 0, 0, prop.w, prop.h); // and decode the gif with overwriting - if(!DecodeImage(loaderInfo.gifAccessor->gif, loaderInfo.cachedColor, reinterpret_cast(pixels), prop.w, xin, yin, frameInfo->transparent, x, y, w, h, true)) + if(DALI_UNLIKELY(!DecodeImage(loaderInfo.gifAccessor->gif, loaderInfo.cachedColor, reinterpret_cast(pixels), prop.w, xin, yin, frameInfo->transparent, x, y, w, h, true))) { DALI_LOG_ERROR("LOAD_ERROR_CORRUPT_FILE\n"); return false; @@ -1300,7 +1322,7 @@ bool ReadNextFrame(LoaderInfo& loaderInfo, ImageProperties& prop, // use for w else { // skip decoding and just walk image to next - if(DGifGetCode(loaderInfo.gifAccessor->gif, &img_code, &img) == GIF_ERROR) + if(DALI_UNLIKELY(DGifGetCode(loaderInfo.gifAccessor->gif, &img_code, &img) == GIF_ERROR)) { DALI_LOG_ERROR("LOAD_ERROR_UNKNOWN_FORMAT\n"); return false; @@ -1397,7 +1419,7 @@ bool GifLoading::LoadNextNFrames(uint32_t frameStartIndex, int count, std::vecto bool ret = false; Mutex::ScopedLock lock(mImpl->mMutex); - if(!mImpl->mLoadSucceeded) + if(DALI_UNLIKELY(!mImpl->mLoadSucceeded)) { return false; } diff --git a/dali/internal/imaging/common/loader-astc.cpp b/dali/internal/imaging/common/loader-astc.cpp index 4ab6d67..7cd2271 100644 --- a/dali/internal/imaging/common/loader-astc.cpp +++ b/dali/internal/imaging/common/loader-astc.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Samsung Electronics Co., Ltd. + * Copyright (c) 2022 Samsung Electronics Co., Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -115,14 +115,14 @@ bool LoadAstcHeader(FILE* const filePointer, unsigned int& width, unsigned int& { // Pull the bytes of the file header in as a block: const unsigned int readLength = sizeof(AstcFileHeader); - if(fread(&fileHeader, 1, readLength, filePointer) != readLength) + if(DALI_UNLIKELY(fread(&fileHeader, 1, readLength, filePointer) != readLength)) { return false; } // Check the header contains the ASTC native file identifier. bool headerIsValid = memcmp(fileHeader.magic, FileIdentifier, sizeof(fileHeader.magic)) == 0; - if(!headerIsValid) + if(DALI_UNLIKELY(!headerIsValid)) { DALI_LOG_ERROR("File is not a valid ASTC native file\n"); // Return here as otherwise, if not a valid ASTC file, we are likely to pick up other header errors spuriously. @@ -136,14 +136,14 @@ bool LoadAstcHeader(FILE* const filePointer, unsigned int& width, unsigned int& const unsigned int zDepth = static_cast(fileHeader.zsize[0]) + (static_cast(fileHeader.zsize[1]) << 8) + (static_cast(fileHeader.zsize[2]) << 16); // Check image dimensions are within limits. - if((width > MAX_TEXTURE_DIMENSION) || (height > MAX_TEXTURE_DIMENSION)) + if(DALI_UNLIKELY((width > MAX_TEXTURE_DIMENSION) || (height > MAX_TEXTURE_DIMENSION))) { DALI_LOG_ERROR("ASTC file has larger than supported dimensions: %d,%d\n", width, height); headerIsValid = false; } // Confirm the ASTC block does not have any Z depth. - if(zDepth != 1) + if(DALI_UNLIKELY(zDepth != 1)) { DALI_LOG_ERROR("ASTC files with z size other than 1 are not supported. Z size is: %d\n", zDepth); headerIsValid = false; @@ -165,7 +165,7 @@ bool LoadAstcHeader(const Dali::ImageLoader::Input& input, unsigned int& width, bool LoadBitmapFromAstc(const Dali::ImageLoader::Input& input, Dali::Devel::PixelBuffer& bitmap) { FILE* const filePointer = input.file; - if(!filePointer) + if(DALI_UNLIKELY(!filePointer)) { DALI_LOG_ERROR("Null file handle passed to ASTC compressed bitmap file loader.\n"); return false; @@ -175,7 +175,7 @@ bool LoadBitmapFromAstc(const Dali::ImageLoader::Input& input, Dali::Devel::Pixe AstcFileHeader fileHeader; unsigned int width, height; - if(!LoadAstcHeader(filePointer, width, height, fileHeader)) + if(DALI_UNLIKELY(!LoadAstcHeader(filePointer, width, height, fileHeader))) { DALI_LOG_ERROR("Could not load ASTC Header from file.\n"); return false; @@ -183,27 +183,27 @@ bool LoadBitmapFromAstc(const Dali::ImageLoader::Input& input, Dali::Devel::Pixe // Retrieve the pixel format from the ASTC block size. Pixel::Format pixelFormat = GetAstcPixelFormat(fileHeader); - if(pixelFormat == Pixel::INVALID) + if(DALI_UNLIKELY(pixelFormat == Pixel::INVALID)) { DALI_LOG_ERROR("No internal pixel format supported for ASTC file pixel format.\n"); return false; } // Retrieve the file size. - if(fseek(filePointer, 0L, SEEK_END)) + if(DALI_UNLIKELY(fseek(filePointer, 0L, SEEK_END))) { DALI_LOG_ERROR("Could not seek through file.\n"); return false; } off_t fileSize = ftell(filePointer); - if(fileSize == -1L) + if(DALI_UNLIKELY(fileSize == -1L)) { DALI_LOG_ERROR("Could not determine ASTC file size.\n"); return false; } - if(fseek(filePointer, sizeof(AstcFileHeader), SEEK_SET)) + if(DALI_UNLIKELY(fseek(filePointer, sizeof(AstcFileHeader), SEEK_SET))) { DALI_LOG_ERROR("Could not seek through file.\n"); return false; @@ -213,7 +213,7 @@ bool LoadBitmapFromAstc(const Dali::ImageLoader::Input& input, Dali::Devel::Pixe size_t imageByteCount = fileSize - sizeof(AstcFileHeader); // Sanity-check the image data is not too large and that it is at less than 2 bytes per texel: - if((imageByteCount > MAX_IMAGE_DATA_SIZE) || (imageByteCount > ((static_cast(width) * height) << 1))) + if(DALI_UNLIKELY((imageByteCount > MAX_IMAGE_DATA_SIZE) || (imageByteCount > ((static_cast(width) * height) << 1)))) { DALI_LOG_ERROR("ASTC file has too large image-data field.\n"); return false; @@ -236,7 +236,7 @@ bool LoadBitmapFromAstc(const Dali::ImageLoader::Input& input, Dali::Devel::Pixe const size_t bytesRead = fread(pixels, 1, imageByteCount, filePointer); // Check the size of loaded data is what we expected. - if(bytesRead != imageByteCount) + if(DALI_UNLIKELY(bytesRead != imageByteCount)) { DALI_LOG_ERROR("Read of image pixel data failed.\n"); return false; diff --git a/dali/internal/imaging/common/loader-bmp.cpp b/dali/internal/imaging/common/loader-bmp.cpp index 80c455d..05d0518 100644 --- a/dali/internal/imaging/common/loader-bmp.cpp +++ b/dali/internal/imaging/common/loader-bmp.cpp @@ -82,7 +82,7 @@ inline bool ReadHeader(FILE* fp, T& header) const unsigned int readLength = sizeof(T); // Load the information directly into our structure - if(fread(&header, 1, readLength, fp) != readLength) + if(DALI_UNLIKELY(fread(&header, 1, readLength, fp) != readLength)) { return false; } @@ -92,13 +92,13 @@ inline bool ReadHeader(FILE* fp, T& header) bool LoadBmpHeader(FILE* fp, unsigned int& width, unsigned int& height, BmpFileHeader& fileHeader, BmpInfoHeader& infoHeader) { - if(!ReadHeader(fp, fileHeader)) + if(DALI_UNLIKELY(!ReadHeader(fp, fileHeader))) { DALI_LOG_ERROR("File header read failed\n"); return false; } - if(!ReadHeader(fp, infoHeader)) + if(DALI_UNLIKELY(!ReadHeader(fp, infoHeader))) { DALI_LOG_ERROR("Info header read failed\n"); return false; @@ -107,7 +107,7 @@ bool LoadBmpHeader(FILE* fp, unsigned int& width, unsigned int& height, BmpFileH width = infoHeader.width; height = abs(infoHeader.height); - if(infoHeader.width == 0) + if(DALI_UNLIKELY(infoHeader.width == 0)) { DALI_LOG_ERROR("Invalid header size\n"); return false; @@ -136,12 +136,12 @@ bool DecodeRGB24V5(FILE* fp, unsigned int rowStride, unsigned int padding) { - if(fp == NULL || pixels == NULL) + if(DALI_UNLIKELY(fp == NULL || pixels == NULL)) { DALI_LOG_ERROR("Error decoding BMP_RGB24V5 format\n"); return false; } - if(fseek(fp, offset, SEEK_SET)) + if(DALI_UNLIKELY(fseek(fp, offset, SEEK_SET))) { DALI_LOG_ERROR("Error seeking BMP_RGB24V5 data\n"); return false; @@ -158,7 +158,7 @@ bool DecodeRGB24V5(FILE* fp, { pixelsPtr = pixels + (((height - 1) - yPos) * rowStride); } - if(fread(pixelsPtr, 1, rowStride, fp) != rowStride) + if(DALI_UNLIKELY(fread(pixelsPtr, 1, rowStride, fp) != rowStride)) { DALI_LOG_ERROR("Error reading the BMP image\n"); return false; @@ -173,7 +173,7 @@ bool DecodeRGB24V5(FILE* fp, if(padding) { // move past the padding. - if(fseek(fp, padding, SEEK_CUR)) + if(DALI_UNLIKELY(fseek(fp, padding, SEEK_CUR))) { DALI_LOG_ERROR("Error moving past BMP_RGB24V5 padding\n"); } @@ -203,12 +203,12 @@ bool DecodeBF32V4(FILE* fp, unsigned int rowStride, unsigned int padding) { - if(fp == NULL || pixels == NULL) + if(DALI_UNLIKELY(fp == NULL || pixels == NULL)) { DALI_LOG_ERROR("Error decoding BMP_BITFIELDS32V4 format\n"); return false; } - if(fseek(fp, offset, SEEK_SET)) + if(DALI_UNLIKELY(fseek(fp, offset, SEEK_SET))) { DALI_LOG_ERROR("Error seeking BMP_BITFIELDS32V4 data\n"); return false; @@ -225,7 +225,7 @@ bool DecodeBF32V4(FILE* fp, { pixelsPtr = pixels + (((height - 1) - yPos) * rowStride); } - if(fread(pixelsPtr, 1, rowStride, fp) != rowStride) + if(DALI_UNLIKELY(fread(pixelsPtr, 1, rowStride, fp) != rowStride)) { DALI_LOG_ERROR("Error reading the BMP image\n"); return false; @@ -239,7 +239,7 @@ bool DecodeBF32V4(FILE* fp, if(padding) { // move past the padding. - if(fseek(fp, padding, SEEK_CUR)) + if(DALI_UNLIKELY(fseek(fp, padding, SEEK_CUR))) { DALI_LOG_ERROR("Error moving past BMP_BITFIELDS32V4 padding\n"); } @@ -269,12 +269,12 @@ bool DecodeBF32(FILE* fp, unsigned int rowStride, unsigned int padding) { - if(fp == NULL || pixels == NULL) + if(DALI_UNLIKELY(fp == NULL || pixels == NULL)) { DALI_LOG_ERROR("Error decoding BMP_BITFIELDS32 format\n"); return false; } - if(fseek(fp, offset, SEEK_SET)) + if(DALI_UNLIKELY(fseek(fp, offset, SEEK_SET))) { DALI_LOG_ERROR("Error seeking BMP_BITFIELDS32 data\n"); return false; @@ -294,7 +294,7 @@ bool DecodeBF32(FILE* fp, pixelsPtr = pixels + (((height - 1) - yPos) * rowStride); } - if(fread(pixelsPtr, 1, rowStride, fp) != rowStride) + if(DALI_UNLIKELY(fread(pixelsPtr, 1, rowStride, fp) != rowStride)) { DALI_LOG_ERROR("Error reading the BMP image\n"); return false; @@ -309,7 +309,7 @@ bool DecodeBF32(FILE* fp, if(padding) { // move past the padding. - if(fseek(fp, padding, SEEK_CUR)) + if(DALI_UNLIKELY(fseek(fp, padding, SEEK_CUR))) { DALI_LOG_ERROR("Error moving past BMP_BITFIELDS32 padding\n"); } @@ -335,12 +335,12 @@ bool DecodeBF565(FILE* fp, unsigned int offset, bool topDown) { - if(fp == NULL || pixels == NULL) + if(DALI_UNLIKELY(fp == NULL || pixels == NULL)) { DALI_LOG_ERROR("Error decoding RGB565 format\n"); return false; } - if(fseek(fp, offset, SEEK_SET)) + if(DALI_UNLIKELY(fseek(fp, offset, SEEK_SET))) { DALI_LOG_ERROR("Error seeking RGB565 data\n"); return false; @@ -362,7 +362,7 @@ bool DecodeBF565(FILE* fp, // the data in the file is bottom up, and we store the data top down pixelsPtr = pixels + (((height - 1) - i) * rowStride); } - if(fread(pixelsPtr, 1, rowStride, fp) != rowStride) + if(DALI_UNLIKELY(fread(pixelsPtr, 1, rowStride, fp) != rowStride)) { return false; } @@ -388,13 +388,13 @@ bool DecodeBF555(FILE* fp, unsigned int offset, bool topDown) { - if(fp == NULL || pixels == NULL) + if(DALI_UNLIKELY(fp == NULL || pixels == NULL)) { DALI_LOG_ERROR("Error decoding BMP_BITFIELDS555 format\n"); return false; } - if(fseek(fp, offset, SEEK_SET)) + if(DALI_UNLIKELY(fseek(fp, offset, SEEK_SET))) { DALI_LOG_ERROR("Error seeking BMP_BITFIELDS555 data\n"); return false; @@ -410,7 +410,7 @@ bool DecodeBF555(FILE* fp, for(std::uint32_t j = 0; j < height; ++j) { rawPtr = &raw[0] + (j * rawStride); - if(fread(rawPtr, 1, rawStride, fp) != rawStride) + if(DALI_UNLIKELY(fread(rawPtr, 1, rawStride, fp) != rawStride)) { return false; } @@ -458,12 +458,12 @@ bool DecodeRGB555(FILE* fp, unsigned int offset, bool topDown) { - if(fp == NULL || pixels == NULL) + if(DALI_UNLIKELY(fp == NULL || pixels == NULL)) { DALI_LOG_ERROR("Error decoding BMP_RGB555 format\n"); return false; } - if(fseek(fp, offset, SEEK_SET)) + if(DALI_UNLIKELY(fseek(fp, offset, SEEK_SET))) { DALI_LOG_ERROR("Error seeking BMP_RGB555 data\n"); return false; @@ -478,7 +478,7 @@ bool DecodeRGB555(FILE* fp, for(std::uint32_t j = 0; j < height; ++j) { rawPtr = &raw[0] + (j * rawStride); - if(fread(rawPtr, 1, rawStride, fp) != rawStride) + if(DALI_UNLIKELY(fread(rawPtr, 1, rawStride, fp) != rawStride)) { return false; } @@ -524,12 +524,12 @@ bool DecodeRGB1(FILE* fp, unsigned int offset, bool topDown) { - if(fp == NULL || pixels == NULL) + if(DALI_UNLIKELY(fp == NULL || pixels == NULL)) { DALI_LOG_ERROR("Error decoding BMP_RGB1 format\n"); return false; } - if(fseek(fp, offset, SEEK_SET)) + if(DALI_UNLIKELY(fseek(fp, offset, SEEK_SET))) { DALI_LOG_ERROR("Error seeking BMP_RGB1 data\n"); return false; @@ -541,14 +541,14 @@ bool DecodeRGB1(FILE* fp, std::vector colorIndex(fillw * height); std::uint32_t rowStride = fillw * 3; // RGB - if(fread(colorTable, 1, 8, fp) != 8) + if(DALI_UNLIKELY(fread(colorTable, 1, 8, fp) != 8)) { return false; } for(std::uint32_t i = 0; i < fillw * height; i += 8) { - if(fread(&cmd, 1, 1, fp) != 1) + if(DALI_UNLIKELY(fread(&cmd, 1, 1, fp) != 1)) { return false; } @@ -616,12 +616,12 @@ bool DecodeRGB4(FILE* fp, unsigned int offset, bool topDown) { - if(fp == NULL || pixels == NULL) + if(DALI_UNLIKELY(fp == NULL || pixels == NULL)) { DALI_LOG_ERROR("Error decoding BMP_RGB4 format\n"); return false; } - if(fseek(fp, offset, SEEK_SET)) + if(DALI_UNLIKELY(fseek(fp, offset, SEEK_SET))) { DALI_LOG_ERROR("Error seeking BMP_RGB4 data\n"); return false; @@ -633,14 +633,14 @@ bool DecodeRGB4(FILE* fp, std::vector colorIndex(fillw * height); std::uint32_t rowStride = fillw * 3; - if(fread(colorTable, 1, 64, fp) != 64) + if(DALI_UNLIKELY(fread(colorTable, 1, 64, fp) != 64)) { return false; } for(std::uint32_t i = 0; i < fillw * height; i += 2) { - if(fread(&cmd, 1, 1, fp) != 1) + if(DALI_UNLIKELY(fread(&cmd, 1, 1, fp) != 1)) { return false; } @@ -692,12 +692,12 @@ bool DecodeRGB8(FILE* fp, unsigned int offset, bool topDown) { - if(fp == NULL || pixels == NULL) + if(DALI_UNLIKELY(fp == NULL || pixels == NULL)) { DALI_LOG_ERROR("Error decoding BMP_RGB8 format\n"); return false; } - if(fseek(fp, offset, SEEK_SET)) + if(DALI_UNLIKELY(fseek(fp, offset, SEEK_SET))) { DALI_LOG_ERROR("Error seeking BMP_RGB8 data\n"); return false; @@ -708,11 +708,11 @@ bool DecodeRGB8(FILE* fp, std::vector colorIndex(width * height); std::uint32_t rowStride = width * 3; //RGB8->RGB24 - if(fread(&colorTable[0], 1, 1024, fp) != 1024) + if(DALI_UNLIKELY(fread(&colorTable[0], 1, 1024, fp) != 1024)) { return false; } - if(fread(&colorIndex[0], 1, width * height, fp) != width * height) + if(DALI_UNLIKELY(fread(&colorIndex[0], 1, width * height, fp) != width * height)) { return false; } @@ -758,7 +758,7 @@ bool DecodeRLE4(FILE* fp, unsigned int offset, bool topDown) { - if(fp == NULL || pixels == NULL) + if(DALI_UNLIKELY(fp == NULL || pixels == NULL)) { DALI_LOG_ERROR("Error decoding BMP_RLE4 format\n"); return false; @@ -779,13 +779,13 @@ bool DecodeRLE4(FILE* fp, bool finish = false; - if(fseek(fp, offset, SEEK_SET)) + if(DALI_UNLIKELY(fseek(fp, offset, SEEK_SET))) { DALI_LOG_ERROR("Error seeking BMP_RLE4 data\n"); return false; } - if(fread(colorTable, 1, 64, fp) != 64) + if(DALI_UNLIKELY(fread(colorTable, 1, 64, fp) != 64)) { return false; } @@ -796,7 +796,7 @@ bool DecodeRLE4(FILE* fp, { break; } - if(fread(cmd, 1, cmdStride, fp) != cmdStride) + if(DALI_UNLIKELY(fread(cmd, 1, cmdStride, fp) != cmdStride)) { return false; } @@ -812,7 +812,7 @@ bool DecodeRLE4(FILE* fp, y++; break; case 2: // delta - if(fread(cmd, 1, cmdStride, fp) != cmdStride) + if(DALI_UNLIKELY(fread(cmd, 1, cmdStride, fp) != cmdStride)) { DALI_LOG_ERROR("Error reading the BMP image\n"); return false; @@ -831,7 +831,7 @@ bool DecodeRLE4(FILE* fp, bytesize >>= 1; bytesize += (bytesize & 1); run.resize(bytesize); - if(fread(&run[0], 1, bytesize, fp) != bytesize) + if(DALI_UNLIKELY(fread(&run[0], 1, bytesize, fp) != bytesize)) { DALI_LOG_ERROR("Error reading the BMP image\n"); return false; @@ -926,7 +926,7 @@ bool DecodeRLE8(FILE* fp, unsigned int offset, bool topDown) { - if(fp == NULL || pixels == NULL) + if(DALI_UNLIKELY(fp == NULL || pixels == NULL)) { DALI_LOG_ERROR("Error decoding BMP_RLE8 format\n"); return false; @@ -941,13 +941,13 @@ bool DecodeRLE8(FILE* fp, std::uint8_t cmd[2]; std::vector colorIndex(width * height); - if(fseek(fp, offset, SEEK_SET)) + if(DALI_UNLIKELY(fseek(fp, offset, SEEK_SET))) { DALI_LOG_ERROR("Error seeking BMP_RLE8 data\n"); return false; } - if(fread(&colorTable[0], 1, 1024, fp) != 1024) + if(DALI_UNLIKELY(fread(&colorTable[0], 1, 1024, fp) != 1024)) { return false; } @@ -960,11 +960,11 @@ bool DecodeRLE8(FILE* fp, std::vector run; while((x + y * width) < width * height) { - if(finish) + if(DALI_UNLIKELY(finish)) { break; } - if(fread(cmd, 1, cmdStride, fp) != cmdStride) + if(DALI_UNLIKELY(fread(cmd, 1, cmdStride, fp) != cmdStride)) { return false; } @@ -981,7 +981,7 @@ bool DecodeRLE8(FILE* fp, y++; break; case 2: // delta - if(fread(cmd, 1, cmdStride, fp) != cmdStride) + if(DALI_UNLIKELY(fread(cmd, 1, cmdStride, fp) != cmdStride)) { DALI_LOG_ERROR("Error reading the BMP image\n"); return false; @@ -998,7 +998,7 @@ bool DecodeRLE8(FILE* fp, //absolute mode must be word-aligned length += (length & 1); run.resize(length); - if(fread(&run[0], 1, length, fp) != length) + if(DALI_UNLIKELY(fread(&run[0], 1, length, fp) != length)) { DALI_LOG_ERROR("Error reading the BMP image\n"); return false; @@ -1049,7 +1049,7 @@ bool LoadBitmapFromBmp(const Dali::ImageLoader::Input& input, Dali::Devel::Pixel { //DALI_ASSERT_DEBUG( bitmap.GetPackedPixelsProfile() != 0 && "Need a packed pixel bitmap to load into." ); FILE* const fp = input.file; - if(fp == NULL) + if(DALI_UNLIKELY(fp == NULL)) { DALI_LOG_ERROR("Error loading bitmap\n"); return false; @@ -1061,7 +1061,7 @@ bool LoadBitmapFromBmp(const Dali::ImageLoader::Input& input, Dali::Devel::Pixel // Load the header info unsigned int width, height; - if(!LoadBmpHeader(fp, width, height, fileHeader, infoHeader)) + if(DALI_UNLIKELY(!LoadBmpHeader(fp, width, height, fileHeader, infoHeader))) { return false; } @@ -1127,13 +1127,13 @@ bool LoadBitmapFromBmp(const Dali::ImageLoader::Input& input, Dali::Devel::Pixel { if(infoHeader.bitsPerPixel == 16) { - if(fseek(fp, 14 + infoHeader.infoHeaderSize + 1, SEEK_SET)) + if(DALI_UNLIKELY(fseek(fp, 14 + infoHeader.infoHeaderSize + 1, SEEK_SET))) { return false; } char mask; - if(fread(&mask, 1, 1, fp) != 1) + if(DALI_UNLIKELY(fread(&mask, 1, 1, fp) != 1)) { return false; } @@ -1322,7 +1322,7 @@ bool LoadBitmapFromBmp(const Dali::ImageLoader::Input& input, Dali::Devel::Pixel pixelsIterator = pixels + (((height - 1) - yPos) * rowStride); } - if(fread(pixelsIterator, 1, rowStride, fp) != rowStride) + if(DALI_UNLIKELY(fread(pixelsIterator, 1, rowStride, fp) != rowStride)) { DALI_LOG_ERROR("Error reading the BMP image\n"); break; @@ -1342,7 +1342,7 @@ bool LoadBitmapFromBmp(const Dali::ImageLoader::Input& input, Dali::Devel::Pixel if(padding) { - if(fseek(fp, padding, SEEK_CUR)) // move past the padding. + if(DALI_UNLIKELY(fseek(fp, padding, SEEK_CUR))) // move past the padding. { DALI_LOG_ERROR("Error moving past BMP padding\n"); } @@ -1354,7 +1354,7 @@ bool LoadBitmapFromBmp(const Dali::ImageLoader::Input& input, Dali::Devel::Pixel } } // switch - if(!decodeResult) + if(DALI_UNLIKELY(!decodeResult)) { DALI_LOG_ERROR("Decoding failed\n"); return false; diff --git a/dali/internal/imaging/common/loader-gif.cpp b/dali/internal/imaging/common/loader-gif.cpp index 57da14f..c8e9091 100644 --- a/dali/internal/imaging/common/loader-gif.cpp +++ b/dali/internal/imaging/common/loader-gif.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Samsung Electronics Co., Ltd. + * Copyright (c) 2022 Samsung Electronics Co., Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -100,7 +100,7 @@ bool LoadGifHeader(FILE* fp, unsigned int& width, unsigned int& height, GifFileT *gifInfo = DGifOpen(reinterpret_cast(fp), ReadDataFromGif); #endif - if(!(*gifInfo) || errorCode) + if(DALI_UNLIKELY(!(*gifInfo) || errorCode)) { DALI_LOG_ERROR("GIF Loader: DGifOpen Error. Code: %d\n", errorCode); return false; @@ -110,7 +110,7 @@ bool LoadGifHeader(FILE* fp, unsigned int& width, unsigned int& height, GifFileT height = (*gifInfo)->SHeight; // No proper size in GIF. - if(width <= 0 || height <= 0) + if(DALI_UNLIKELY(width <= 0 || height <= 0)) { return false; } @@ -131,7 +131,7 @@ bool DecodeImage(GifFileType* gifInfo, unsigned char* decodedData, const unsigne for(unsigned int currentByte = interlacePairPtr->startingByte; currentByte < height; currentByte += interlacePairPtr->incrementalByte) { unsigned char* row = decodedData + currentByte * bytesPerRow; - if(DGifGetLine(gifInfo, row, width) == GIF_ERROR) + if(DALI_UNLIKELY(DGifGetLine(gifInfo, row, width) == GIF_ERROR)) { DALI_LOG_ERROR("GIF Loader: Error reading Interlaced GIF\n"); return false; @@ -146,7 +146,7 @@ bool DecodeImage(GifFileType* gifInfo, unsigned char* decodedData, const unsigne for(unsigned int row = 0; row < height; ++row) { - if(DGifGetLine(gifInfo, decodedDataPtr, width) == GIF_ERROR) + if(DALI_UNLIKELY(DGifGetLine(gifInfo, decodedDataPtr, width) == GIF_ERROR)) { DALI_LOG_ERROR("GIF Loader: Error reading non-interlaced GIF\n"); return false; @@ -176,14 +176,14 @@ GifColorType* GetImageColors(SavedImage* image, GifFileType* gifInfo) /// Called when we want to handle IMAGE_DESC_RECORD_TYPE bool HandleImageDescriptionRecordType(Dali::Devel::PixelBuffer& bitmap, GifFileType* gifInfo, unsigned int width, unsigned int height, bool& finished) { - if(DGifGetImageDesc(gifInfo) == GIF_ERROR) + if(DALI_UNLIKELY(DGifGetImageDesc(gifInfo) == GIF_ERROR)) { DALI_LOG_ERROR("GIF Loader: Error getting Image Description\n"); return false; } // Ensure there is at least 1 image in the GIF. - if(gifInfo->ImageCount < 1) + if(DALI_UNLIKELY(gifInfo->ImageCount < 1)) { DALI_LOG_ERROR("GIF Loader: No Images\n"); return false; @@ -206,7 +206,7 @@ bool HandleImageDescriptionRecordType(Dali::Devel::PixelBuffer& bitmap, GifFileT bitmap = Dali::Devel::PixelBuffer::New(actualWidth, actualHeight, pixelFormat); // Decode the GIF Image - if(!DecodeImage(gifInfo, decodedData, actualWidth, actualHeight, bytesPerRow)) + if(DALI_UNLIKELY(!DecodeImage(gifInfo, decodedData, actualWidth, actualHeight, bytesPerRow))) { return false; } @@ -256,7 +256,7 @@ bool HandleExtensionRecordType(GifFileType* gifInfo) extensionByte != NULL; extRetCode = DGifGetExtensionNext(gifInfo, &extensionByte)) { - if(extRetCode == GIF_ERROR) + if(DALI_UNLIKELY(extRetCode == GIF_ERROR)) { DALI_LOG_ERROR("GIF Loader: Error reading GIF Extension record.\n"); return false; @@ -285,7 +285,7 @@ bool LoadBitmapFromGif(const Dali::ImageLoader::Input& input, Dali::Devel::Pixel GifFileType* gifInfo(NULL); unsigned int width(0); unsigned int height(0); - if(!LoadGifHeader(fp, width, height, &gifInfo)) + if(DALI_UNLIKELY(!LoadGifHeader(fp, width, height, &gifInfo))) { return false; } @@ -299,7 +299,7 @@ bool LoadBitmapFromGif(const Dali::ImageLoader::Input& input, Dali::Devel::Pixel !finished && recordType != TERMINATE_RECORD_TYPE; returnCode = DGifGetRecordType(gifInfo, &recordType)) { - if(returnCode == GIF_ERROR) + if(DALI_UNLIKELY(returnCode == GIF_ERROR)) { DALI_LOG_ERROR("GIF Loader: Error getting Record Type\n"); return false; @@ -307,14 +307,14 @@ bool LoadBitmapFromGif(const Dali::ImageLoader::Input& input, Dali::Devel::Pixel if(IMAGE_DESC_RECORD_TYPE == recordType) { - if(!HandleImageDescriptionRecordType(bitmap, gifInfo, width, height, finished)) + if(DALI_UNLIKELY(!HandleImageDescriptionRecordType(bitmap, gifInfo, width, height, finished))) { return false; } } else if(EXTENSION_RECORD_TYPE == recordType) { - if(!HandleExtensionRecordType(gifInfo)) + if(DALI_UNLIKELY(!HandleExtensionRecordType(gifInfo))) { return false; } diff --git a/dali/internal/imaging/common/loader-ico.cpp b/dali/internal/imaging/common/loader-ico.cpp index ac68291..b23eaee 100644 --- a/dali/internal/imaging/common/loader-ico.cpp +++ b/dali/internal/imaging/common/loader-ico.cpp @@ -80,11 +80,11 @@ typedef unsigned char DATA8; #define ARGB_JOIN(a, r, g, b) \ (((a) << 24) + ((r) << 16) + ((g) << 8) + (b)) -bool read_ushort(unsigned char* map, size_t length, size_t* position, unsigned short* ret) +bool read_ushort(const unsigned char* const& map, size_t length, size_t* position, unsigned short* ret) { unsigned char b[2]; - if(*position + 2 > length) + if(DALI_UNLIKELY(*position + 2 > length)) { return false; } @@ -94,12 +94,12 @@ bool read_ushort(unsigned char* map, size_t length, size_t* position, unsigned s return true; } -bool read_uint(unsigned char* map, size_t length, size_t* position, unsigned int* ret) +bool read_uint(const unsigned char* const& map, size_t length, size_t* position, unsigned int* ret) { unsigned char b[4]; unsigned int i; - if(*position + 4 > length) + if(DALI_UNLIKELY(*position + 4 > length)) { return false; } @@ -111,9 +111,9 @@ bool read_uint(unsigned char* map, size_t length, size_t* position, unsigned int return true; } -bool read_uchar(unsigned char* map, size_t length, size_t* position, unsigned char* ret) +bool read_uchar(const unsigned char* const& map, size_t length, size_t* position, unsigned char* ret) { - if(*position + 1 > length) + if(DALI_UNLIKELY(*position + 1 > length)) { return false; } @@ -121,9 +121,9 @@ bool read_uchar(unsigned char* map, size_t length, size_t* position, unsigned ch return true; } -bool read_mem(unsigned char* map, size_t length, size_t* position, void* buffer, int size) +bool read_mem(const unsigned char* const& map, size_t length, size_t* position, void* buffer, int size) { - if(*position + size > length) + if(DALI_UNLIKELY(*position + size > length)) { return false; } @@ -163,7 +163,7 @@ bool LoadIcoHeaderHelper(FILE* fp, { memset(&chosen, 0, sizeof(chosen)); - if(fp == NULL) + if(DALI_UNLIKELY(fp == NULL)) { DALI_LOG_ERROR("Error loading bitmap\n"); return false; @@ -172,7 +172,7 @@ bool LoadIcoHeaderHelper(FILE* fp, unsigned short word; unsigned char byte; - if(fseek(fp, 0, SEEK_END)) + if(DALI_UNLIKELY(fseek(fp, 0, SEEK_END))) { DALI_LOG_ERROR("Error seeking ICO data\n"); return false; @@ -186,45 +186,46 @@ bool LoadIcoHeaderHelper(FILE* fp, fsize = static_cast(positionIndicator); } - if(0u == fsize) + if(DALI_UNLIKELY(0u == fsize)) { return false; } - if(fseek(fp, 0, SEEK_SET)) + if(DALI_UNLIKELY(fseek(fp, 0, SEEK_SET))) { DALI_LOG_ERROR("Error seeking ICO data\n"); return false; } - if(fsize < (ICO_FILE_HEADER + ICO_IMAGE_INFO_HEADER)) //6 + 16 + 40 + if(DALI_UNLIKELY(fsize < (ICO_FILE_HEADER + ICO_IMAGE_INFO_HEADER))) //6 + 16 + 40 { return false; } map.ResizeUninitialized(fsize); - - if(fread(&map[0], 1, fsize, fp) != fsize) + if(DALI_UNLIKELY(fread(&map[0], 1, fsize, fp) != fsize)) { DALI_LOG_WARNING("image file read opeation error!\n"); return false; } + const std::uint8_t* const inputBufferPtr = &map[0]; + int search = BIGGEST; unsigned short reserved, type, count; - if(!read_ushort(&map[0], fsize, &position, &reserved)) + if(DALI_UNLIKELY(!read_ushort(inputBufferPtr, fsize, &position, &reserved))) { return false; } - if(!read_ushort(&map[0], fsize, &position, &type)) + if(DALI_UNLIKELY(!read_ushort(inputBufferPtr, fsize, &position, &type))) { return false; } - if(!read_ushort(&map[0], fsize, &position, &count)) + if(DALI_UNLIKELY(!read_ushort(inputBufferPtr, fsize, &position, &count))) { return false; } - if(!((reserved == 0) && - ((type == ICON) || (type == CURSOR)) && (count != 0))) + if(DALI_UNLIKELY(!((reserved == 0) && + ((type == ICON) || (type == CURSOR)) && (count != 0)))) { return false; } @@ -235,7 +236,7 @@ bool LoadIcoHeaderHelper(FILE* fp, for(unsigned short i = 0; i < count; i++) { unsigned char tw = 0, th = 0, tcols = 0; - if(!read_uchar(&map[0], fsize, &position, &tw)) + if(DALI_UNLIKELY(!read_uchar(inputBufferPtr, fsize, &position, &tw))) { return false; } @@ -244,7 +245,7 @@ bool LoadIcoHeaderHelper(FILE* fp, { w = 256; } - if(!read_uchar(&map[0], fsize, &position, &th)) + if(DALI_UNLIKELY(!read_uchar(inputBufferPtr, fsize, &position, &th))) { return false; } @@ -253,16 +254,16 @@ bool LoadIcoHeaderHelper(FILE* fp, { h = 256; } - if(!read_uchar(&map[0], fsize, &position, &tcols)) + if(DALI_UNLIKELY(!read_uchar(inputBufferPtr, fsize, &position, &tcols))) { return false; } int cols = tcols; - if(!read_uchar(&map[0], fsize, &position, &byte)) + if(DALI_UNLIKELY(!read_uchar(inputBufferPtr, fsize, &position, &byte))) { return false; } - if(!read_ushort(&map[0], fsize, &position, &word)) + if(DALI_UNLIKELY(!read_ushort(inputBufferPtr, fsize, &position, &word))) { return false; } @@ -272,7 +273,7 @@ bool LoadIcoHeaderHelper(FILE* fp, planes = word; } //else hot_x = word; - if(!read_ushort(&map[0], fsize, &position, &word)) + if(DALI_UNLIKELY(!read_ushort(inputBufferPtr, fsize, &position, &word))) { return false; } @@ -291,15 +292,15 @@ bool LoadIcoHeaderHelper(FILE* fp, //else hot_y = word; unsigned int bmoffset, bmsize; - if(!read_uint(&map[0], fsize, &position, &bmsize)) + if(DALI_UNLIKELY(!read_uint(inputBufferPtr, fsize, &position, &bmsize))) { return false; } - if(!read_uint(&map[0], fsize, &position, &bmoffset)) + if(DALI_UNLIKELY(!read_uint(inputBufferPtr, fsize, &position, &bmoffset))) { return false; } - if((bmsize <= 0) || (bmoffset <= 0) || (bmoffset >= fsize)) + if(DALI_UNLIKELY((bmsize <= 0) || (bmoffset <= 0) || (bmoffset >= fsize))) { return false; } @@ -324,7 +325,7 @@ bool LoadIcoHeaderHelper(FILE* fp, } } - if(chosen.bmoffset == 0) + if(DALI_UNLIKELY(chosen.bmoffset == 0)) { return false; } @@ -335,9 +336,9 @@ bool LoadIcoHeaderHelper(FILE* fp, /** * @brief Handle the different bits per pixel * @param[in] bitcount The bit count - * @param[in/out] map The map to use/set + * @param[in] inputBufferPtr The map to use * @param[in/out] pix A reference to the pointer to the pix buffer - * @param[in/out] surface A reference to the surface buffer + * @param[in/out] outputBufferPtr A reference to the surface buffer * @param[in] width The width * @param[in] height The height * @param[in] fsize The file size @@ -348,24 +349,28 @@ bool LoadIcoHeaderHelper(FILE* fp, */ bool HandleBitsPerPixel( const unsigned int bitcount, - Dali::Vector& map, + const std::uint8_t* const& inputBufferPtr, unsigned int*& pix, - Dali::Vector& surface, + std::uint32_t* const& outputBufferPtr, const unsigned int width, const unsigned int height, const unsigned int fsize, size_t& position, - Dali::Vector& pixbuf, const unsigned int stride, const Dali::Vector& palette) { + // Pixbuf only ever contains one scanline worth of data. + Dali::Vector pixbuf; + pixbuf.ResizeUninitialized(stride); + std::uint8_t* lineBufferPtr = &pixbuf[0]; + // Note: Switch is in order of most common format first. switch(bitcount) { case 32: { - unsigned char* p = &map[position]; - pix = &surface[0] + ((height - 1) * width); + const std::uint8_t* p = inputBufferPtr + position; + pix = outputBufferPtr + ((height - 1) * width); for(unsigned int i = 0; i < height; i++) { @@ -384,12 +389,12 @@ bool HandleBitsPerPixel( { for(unsigned int i = 0; i < height; i++) { - pix = &surface[0] + ((height - 1 - i) * width); - if(!read_mem(&map[0], fsize, &position, &pixbuf[0], stride)) + pix = outputBufferPtr + ((height - 1 - i) * width); + if(DALI_UNLIKELY(!read_mem(inputBufferPtr, fsize, &position, lineBufferPtr, stride))) { return false; } - unsigned char* p = &pixbuf[0]; + const std::uint8_t* p = lineBufferPtr; for(unsigned int j = 0; j < width; j++) { *pix++ = ARGB_JOIN(0xff, p[0], p[1], p[2]); @@ -403,12 +408,12 @@ bool HandleBitsPerPixel( { for(unsigned int i = 0; i < height; i++) { - pix = &surface[0] + ((height - 1 - i) * width); - if(!read_mem(&map[0], fsize, &position, &pixbuf[0], stride)) + pix = outputBufferPtr + ((height - 1 - i) * width); + if(DALI_UNLIKELY(!read_mem(inputBufferPtr, fsize, &position, lineBufferPtr, stride))) { return false; } - unsigned char* p = &pixbuf[0]; + const std::uint8_t* p = lineBufferPtr; for(unsigned int j = 0; j < width; j++) { *pix++ = palette[*p++]; @@ -421,12 +426,12 @@ bool HandleBitsPerPixel( { for(unsigned int i = 0; i < height; i++) { - pix = &surface[0] + ((height - 1 - i) * width); - if(!read_mem(&map[0], fsize, &position, &pixbuf[0], stride)) + pix = outputBufferPtr + ((height - 1 - i) * width); + if(DALI_UNLIKELY(!read_mem(inputBufferPtr, fsize, &position, lineBufferPtr, stride))) { return false; } - unsigned char* p = &pixbuf[0]; + const std::uint8_t* p = lineBufferPtr; for(unsigned int j = 0; j < width; j++) { if(j & 0x1) @@ -446,16 +451,18 @@ bool HandleBitsPerPixel( case 1: { + const std::uint32_t bytesPerWidth = width / 8; + const std::uint32_t bytesRemainingPerWidth = width & 7; for(unsigned int i = 0; i < height; i++) { - pix = &surface[0] + ((height - 1 - i) * width); - if(!read_mem(&map[0], fsize, &position, &pixbuf[0], stride)) + pix = outputBufferPtr + ((height - 1 - i) * width); + if(DALI_UNLIKELY(!read_mem(inputBufferPtr, fsize, &position, lineBufferPtr, stride))) { return false; } - unsigned char* p = &pixbuf[0]; - for(unsigned int j = 0; j < width; j += 8) + const std::uint8_t* p = lineBufferPtr; + for(unsigned int j = 0; j < bytesPerWidth; ++j) { *pix++ = palette[*p >> 7]; *pix++ = palette[*p >> 6 & 0x01]; @@ -466,7 +473,15 @@ bool HandleBitsPerPixel( *pix++ = palette[*p >> 1 & 0x01]; *pix++ = palette[*p >> 0 & 0x01]; - p++; + ++p; + } + if(bytesRemainingPerWidth > 0) + { + for(std::uint32_t j = 0; j < bytesRemainingPerWidth; ++j) + { + *pix++ = palette[(*p >> (7 - j)) & 0x01]; + } + ++p; } } break; @@ -484,31 +499,28 @@ bool HandleBitsPerPixel( /** * @brief Apply the mask if required - * @param[in/out] map The map to use/set + * @param[in] inputBufferPtr The map to use * @param[in] fsize The file size * @param[in/out] position The position in the file - * @param[in//out] maskbuf The mask buffer * @param[in] bitStride The stride * @param[in] width The width * @param[in] height The height * @param[in/out] pix A reference to the pointer to the pix buffer - * @param[in/out] surface A reference to the surface buffer + * @param[in/out] outputBufferPtr A reference to the surface buffer */ bool ApplyMask( - Dali::Vector& map, - const unsigned int fsize, - size_t& position, - Dali::Vector& maskbuf, - const unsigned int bitStride, - const unsigned int width, - const unsigned int height, - unsigned int*& pix, - Dali::Vector& surface) + const std::uint8_t* const& inputBufferPtr, + const unsigned int fsize, + size_t& position, + const unsigned int bitStride, + const unsigned int width, + const unsigned int height, + unsigned int*& pix, + std::uint32_t* const& outputBufferPtr) { - if(!read_mem(&map[0], fsize, &position, &maskbuf[0], bitStride * height)) - { - return false; - } + Dali::Vector maskbuf; + maskbuf.ResizeUninitialized(bitStride); + std::uint8_t* lineBufferPtr = &maskbuf[0]; // Apply mask. // Precalc to save time in the loops. @@ -518,8 +530,12 @@ bool ApplyMask( // Loop for each line of the image. for(unsigned int i = 0; i < height; ++i) { - unsigned char* m = &maskbuf[0] + (bitStride * i); - pix = &surface[0] + ((height - 1 - i) * width); + pix = outputBufferPtr + ((height - 1 - i) * width); + if(DALI_UNLIKELY(!read_mem(inputBufferPtr, fsize, &position, lineBufferPtr, bitStride))) + { + return false; + } + const std::uint8_t* m = lineBufferPtr; // Do chunks of 8 pixels first so mask operations can be unrolled. for(unsigned int j = 0; j < bytesPerWidth; ++j) @@ -561,7 +577,7 @@ bool LoadIcoHeader(const Dali::ImageLoader::Input& input, unsigned int& width, u unsigned int fsize; FILE* const fp = input.file; - if(false == LoadIcoHeaderHelper(fp, chosen, map, fsize)) + if(DALI_UNLIKELY(false == LoadIcoHeaderHelper(fp, chosen, map, fsize))) { return false; } @@ -579,17 +595,11 @@ bool LoadBitmapFromIco(const Dali::ImageLoader::Input& input, Dali::Devel::Pixel unsigned int fsize; FILE* const fp = input.file; - if(false == LoadIcoHeaderHelper(fp, chosen, map, fsize)) + if(DALI_UNLIKELY(false == LoadIcoHeaderHelper(fp, chosen, map, fsize))) { return false; } - Dali::Vector pal; - Dali::Vector surface; - Dali::Vector maskbuf; - Dali::Vector pixbuf; - pal.Resize(256 * 4); - unsigned int dword; unsigned short word; @@ -602,12 +612,14 @@ bool LoadBitmapFromIco(const Dali::ImageLoader::Input& input, Dali::Devel::Pixel unsigned int h = chosen.h; unsigned int cols = chosen.cols; + const std::uint8_t* const inputBufferPtr = &map[0]; + // read bmp header time... let's do some checking - if(!read_uint(&map[0], fsize, &position, &dword)) + if(DALI_UNLIKELY(!read_uint(inputBufferPtr, fsize, &position, &dword))) { return false; // headersize - dont care } - if(!read_uint(&map[0], fsize, &position, &dword)) + if(DALI_UNLIKELY(!read_uint(inputBufferPtr, fsize, &position, &dword))) { return false; // width } @@ -619,7 +631,7 @@ bool LoadBitmapFromIco(const Dali::ImageLoader::Input& input, Dali::Devel::Pixel diff_size = 1; } } - if(!read_uint(&map[0], fsize, &position, &dword)) + if(DALI_UNLIKELY(!read_uint(inputBufferPtr, fsize, &position, &dword))) { return false; // height } @@ -636,95 +648,98 @@ bool LoadBitmapFromIco(const Dali::ImageLoader::Input& input, Dali::Devel::Pixel DALI_LOG_WARNING("Broken ICO file!\n"); } - // Set up the surface as soon as we have the width and height, so we have a black image if there are any further errors. - surface.ResizeUninitialized(w * h * 4); - memset(&surface[0], 0, w * h * 4); - - if(!read_ushort(&map[0], fsize, &position, &word)) + if(DALI_UNLIKELY(!read_ushort(inputBufferPtr, fsize, &position, &word))) { return false; // planes } //planes2 = word; - if(!read_ushort(&map[0], fsize, &position, &word)) + if(DALI_UNLIKELY(!read_ushort(inputBufferPtr, fsize, &position, &word))) { return false; // bitcount } unsigned int bitcount = word; - if(!read_uint(&map[0], fsize, &position, &dword)) + if(DALI_UNLIKELY(!read_uint(inputBufferPtr, fsize, &position, &dword))) { return false; // compression } //compression = dword; - if(!read_uint(&map[0], fsize, &position, &dword)) + if(DALI_UNLIKELY(!read_uint(inputBufferPtr, fsize, &position, &dword))) { return false; // imagesize } //imagesize = dword; - if(!read_uint(&map[0], fsize, &position, &dword)) + if(DALI_UNLIKELY(!read_uint(inputBufferPtr, fsize, &position, &dword))) { return false; // z pixels per m } - if(!read_uint(&map[0], fsize, &position, &dword)) + if(DALI_UNLIKELY(!read_uint(inputBufferPtr, fsize, &position, &dword))) { return false; // y pizels per m } - if(!read_uint(&map[0], fsize, &position, &dword)) + if(DALI_UNLIKELY(!read_uint(inputBufferPtr, fsize, &position, &dword))) { return false; // colors used } //colorsused = dword; - if(!read_uint(&map[0], fsize, &position, &dword)) + if(DALI_UNLIKELY(!read_uint(inputBufferPtr, fsize, &position, &dword))) { return false; // colors important } + Dali::Vector pal; + pal.Resize(256 * 4); for(unsigned int i = 0; i < cols; i++) { unsigned char a, r, g, b; - if(!read_uchar(&map[0], fsize, &position, &b)) + if(DALI_UNLIKELY(!read_uchar(inputBufferPtr, fsize, &position, &b))) { return false; } - if(!read_uchar(&map[0], fsize, &position, &g)) + if(DALI_UNLIKELY(!read_uchar(inputBufferPtr, fsize, &position, &g))) { return false; } - if(!read_uchar(&map[0], fsize, &position, &r)) + if(DALI_UNLIKELY(!read_uchar(inputBufferPtr, fsize, &position, &r))) { return false; } - if(!read_uchar(&map[0], fsize, &position, &a)) + if(DALI_UNLIKELY(!read_uchar(inputBufferPtr, fsize, &position, &a))) { return false; } pal[i] = ARGB_JOIN(0xff, b, g, r); } + Dali::Vector surface; + // This is the reference way of calculating the total number of bytes necessary to store one row of pixels. unsigned int stride = (((bitcount * w) + 31) / 32) * 4; unsigned int bitStride = ((w + 31) / 32) * 4; + // Set up the surface as soon as we have the width and height. + surface.ResizeUninitialized(w * h); - // Pixbuf only ever contains one scanline worth of data. - pixbuf.ResizeUninitialized(stride); - maskbuf.ResizeUninitialized(bitStride * h); + std::uint32_t* const outputBufferPtr = &surface[0]; // Handle different bits-per-pixel. - if(!HandleBitsPerPixel(bitcount, map, pix, surface, w, h, fsize, position, pixbuf, stride, pal)) + if(DALI_UNLIKELY(!HandleBitsPerPixel(bitcount, inputBufferPtr, pix, outputBufferPtr, w, h, fsize, position, stride, pal))) { return false; } // From the spec: If bpp is less than 32, there will be a 1bpp mask bitmap also. - if((bitcount < 32) && !ApplyMask(map, fsize, position, maskbuf, bitStride, w, h, pix, surface)) + if(bitcount < 32) { - // Return false if not able to apply mask when the bpp is less than 32 - return false; + if(DALI_UNLIKELY(!ApplyMask(inputBufferPtr, fsize, position, bitStride, w, h, pix, outputBufferPtr))) + { + // Return false if not able to apply mask when the bpp is less than 32 + return false; + } } bitmap = Dali::Devel::PixelBuffer::New(w, h, Pixel::Format::RGBA8888); auto pixels = bitmap.GetBuffer(); - memcpy(pixels, &surface[0], w * h * 4); + memcpy(pixels, outputBufferPtr, w * h * 4); return true; } diff --git a/dali/internal/imaging/common/loader-jpeg-turbo.cpp b/dali/internal/imaging/common/loader-jpeg-turbo.cpp index 83ce109..0571582 100644 --- a/dali/internal/imaging/common/loader-jpeg-turbo.cpp +++ b/dali/internal/imaging/common/loader-jpeg-turbo.cpp @@ -497,7 +497,7 @@ bool LoadJpegHeader(FILE* fp, unsigned int& width, unsigned int& height) // On error exit from the JPEG lib, control will pass via JpegErrorHandler // into this branch body for cleanup and error return: - if(setjmp(jerr.jumpBuffer)) + if(DALI_UNLIKELY(setjmp(jerr.jumpBuffer))) { DALI_LOG_ERROR("setjmp failed\n"); jpeg_destroy_decompress(&cinfo); @@ -513,7 +513,7 @@ bool LoadJpegHeader(FILE* fp, unsigned int& width, unsigned int& height) jpeg_stdio_src(&cinfo, fp); // Check header to see if it is JPEG file - if(jpeg_read_header(&cinfo, TRUE) != JPEG_HEADER_OK) + if(DALI_UNLIKELY(jpeg_read_header(&cinfo, TRUE) != JPEG_HEADER_OK)) { DALI_LOG_ERROR("jpeg_read_header failed\n"); width = height = 0; @@ -533,7 +533,7 @@ bool LoadBitmapFromJpeg(const Dali::ImageLoader::Input& input, Dali::Devel::Pixe const int flags = 0; FILE* const fp = input.file; - if(fseek(fp, 0, SEEK_END)) + if(DALI_UNLIKELY(fseek(fp, 0, SEEK_END))) { DALI_LOG_ERROR("Error seeking to end of file\n"); return false; @@ -546,13 +546,13 @@ bool LoadBitmapFromJpeg(const Dali::ImageLoader::Input& input, Dali::Devel::Pixe jpegBufferSize = static_cast(positionIndicator); } - if(0u == jpegBufferSize) + if(DALI_UNLIKELY(0u == jpegBufferSize)) { DALI_LOG_ERROR("Jpeg buffer size error\n"); return false; } - if(fseek(fp, 0, SEEK_SET)) + if(DALI_UNLIKELY(fseek(fp, 0, SEEK_SET))) { DALI_LOG_ERROR("Error seeking to start of file\n"); return false; @@ -571,20 +571,21 @@ bool LoadBitmapFromJpeg(const Dali::ImageLoader::Input& input, Dali::Devel::Pixe unsigned char* const jpegBufferPtr = jpegBuffer.Begin(); // Pull the compressed JPEG image bytes out of a file and into memory: - if(fread(jpegBufferPtr, 1, jpegBufferSize, fp) != jpegBufferSize) + if(DALI_UNLIKELY(fread(jpegBufferPtr, 1, jpegBufferSize, fp) != jpegBufferSize)) { DALI_LOG_ERROR("Error on image file read.\n"); return false; } - if(fseek(fp, 0, SEEK_SET)) + if(DALI_UNLIKELY(fseek(fp, 0, SEEK_SET))) { DALI_LOG_ERROR("Error seeking to start of file\n"); + return false; } auto jpeg = MakeJpegDecompressor(); - if(!jpeg) + if(DALI_UNLIKELY(!jpeg)) { DALI_LOG_ERROR("%s\n", tjGetErrorStr()); return false; @@ -641,7 +642,7 @@ bool LoadBitmapFromJpeg(const Dali::ImageLoader::Input& input, Dali::Devel::Pixe } #endif - if(preXformImageWidth == 0 || preXformImageHeight == 0) + if(DALI_UNLIKELY(preXformImageWidth == 0 || preXformImageHeight == 0)) { DALI_LOG_ERROR("Invalid Image!\n"); return false; @@ -711,7 +712,7 @@ bool LoadBitmapFromJpeg(const Dali::ImageLoader::Input& input, Dali::Devel::Pixe { std::string errorString = tjGetErrorStr(); - if(IsJpegErrorFatal(errorString)) + if(DALI_UNLIKELY(IsJpegErrorFatal(errorString))) { DALI_LOG_ERROR("%s\n", errorString.c_str()); return false; @@ -867,7 +868,7 @@ bool EncodeToJpeg(const unsigned char* const pixelBuffer, Vector& // Initialise a JPEG codec: { auto jpeg = MakeJpegCompressor(); - if(!jpeg) + if(DALI_UNLIKELY(!jpeg)) { DALI_LOG_ERROR("JPEG Compressor init failed: %s\n", tjGetErrorStr()); return false; @@ -882,17 +883,17 @@ bool EncodeToJpeg(const unsigned char* const pixelBuffer, Vector& unsigned long dstBufferSize = 0; const int flags = 0; - if(tjCompress2(jpeg.get(), - const_cast(pixelBuffer), - width, - 0, - height, - jpegPixelFormat, - SetPointer(dstBuffer), - &dstBufferSize, - TJSAMP_444, - quality, - flags)) + if(DALI_UNLIKELY(tjCompress2(jpeg.get(), + const_cast(pixelBuffer), + width, + 0, + height, + jpegPixelFormat, + SetPointer(dstBuffer), + &dstBufferSize, + TJSAMP_444, + quality, + flags))) { DALI_LOG_ERROR("JPEG Compression failed: %s\n", tjGetErrorStr()); return false; @@ -985,7 +986,7 @@ bool TransformSize(int requiredWidth, int requiredHeight, FittingMode::Type fitt int numFactors = 0; tjscalingfactor* factors = tjGetScalingFactors(&numFactors); - if(factors == NULL) + if(DALI_UNLIKELY(factors == NULL)) { DALI_LOG_WARNING("TurboJpeg tjGetScalingFactors error!\n"); success = false; @@ -1080,7 +1081,7 @@ ExifHandle LoadExifData(FILE* fp) auto exifData = MakeNullExifData(); unsigned char dataBuffer[1024]; - if(fseek(fp, 0, SEEK_SET)) + if(DALI_UNLIKELY(fseek(fp, 0, SEEK_SET))) { DALI_LOG_ERROR("Error seeking to start of file\n"); } @@ -1120,7 +1121,7 @@ bool LoadJpegHeader(const Dali::ImageLoader::Input& input, unsigned int& width, unsigned int headerHeight; success = LoadJpegHeader(fp, headerWidth, headerHeight); - if(success) + if(DALI_LIKELY(success)) { auto transform = JpegTransform::NONE; diff --git a/dali/internal/imaging/common/loader-ktx.cpp b/dali/internal/imaging/common/loader-ktx.cpp index 7600732..f4ab1e2 100644 --- a/dali/internal/imaging/common/loader-ktx.cpp +++ b/dali/internal/imaging/common/loader-ktx.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Samsung Electronics Co., Ltd. + * Copyright (c) 2022 Samsung Electronics Co., Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -187,7 +187,7 @@ inline bool ReadHeader(FILE* filePointer, KtxFileHeader& header) const unsigned int readLength = sizeof(KtxFileHeader); // Load the information directly into our structure - if(fread(&header, 1, readLength, filePointer) != readLength) + if(DALI_UNLIKELY(fread(&header, 1, readLength, filePointer) != readLength)) { return false; } @@ -457,14 +457,14 @@ bool ConvertPixelFormat(const uint32_t ktxPixelFormat, Dali::Pixel::Format& form bool LoadKtxHeader(FILE* const fp, unsigned int& width, unsigned int& height, KtxFileHeader& fileHeader) { // Pull the bytes of the file header in as a block: - if(!ReadHeader(fp, fileHeader)) + if(DALI_UNLIKELY(!ReadHeader(fp, fileHeader))) { return false; } width = fileHeader.pixelWidth; height = fileHeader.pixelHeight; - if(width > MAX_TEXTURE_DIMENSION || height > MAX_TEXTURE_DIMENSION) + if(DALI_UNLIKELY(width > MAX_TEXTURE_DIMENSION || height > MAX_TEXTURE_DIMENSION)) { return false; } @@ -531,7 +531,7 @@ bool LoadBitmapFromKtx(const Dali::ImageLoader::Input& input, Dali::Devel::Pixel static_assert(sizeof(uint32_t) == 4); FILE* const fp = input.file; - if(fp == NULL) + if(DALI_UNLIKELY(fp == NULL)) { DALI_LOG_ERROR("Null file handle passed to KTX compressed bitmap file loader.\n"); return false; @@ -541,14 +541,14 @@ bool LoadBitmapFromKtx(const Dali::ImageLoader::Input& input, Dali::Devel::Pixel // Load the header info unsigned int width, height; - if(!LoadKtxHeader(fp, width, height, fileHeader)) + if(DALI_UNLIKELY(!LoadKtxHeader(fp, width, height, fileHeader))) { return false; } // Skip the key-values: const long int imageSizeOffset = sizeof(KtxFileHeader) + fileHeader.bytesOfKeyValueData; - if(fseek(fp, imageSizeOffset, SEEK_SET)) + if(DALI_UNLIKELY(fseek(fp, imageSizeOffset, SEEK_SET))) { DALI_LOG_ERROR("Seek past key/vals in KTX compressed bitmap file failed.\n"); return false; @@ -556,15 +556,15 @@ bool LoadBitmapFromKtx(const Dali::ImageLoader::Input& input, Dali::Devel::Pixel // Load the size of the image data: uint32_t imageByteCount = 0; - if(fread(&imageByteCount, 1, 4, fp) != 4) + if(DALI_UNLIKELY(fread(&imageByteCount, 1, 4, fp) != 4)) { DALI_LOG_ERROR("Read of image size failed.\n"); return false; } // Sanity-check the image size: - if(imageByteCount > MAX_IMAGE_DATA_SIZE || - // A compressed texture should certainly be less than 2 bytes per texel: - imageByteCount > width * height * 2) + if(DALI_UNLIKELY(imageByteCount > MAX_IMAGE_DATA_SIZE || + // A compressed texture should certainly be less than 2 bytes per texel: + imageByteCount > width * height * 2)) { DALI_LOG_ERROR("KTX file with too-large image-data field.\n"); return false; @@ -572,7 +572,7 @@ bool LoadBitmapFromKtx(const Dali::ImageLoader::Input& input, Dali::Devel::Pixel Pixel::Format pixelFormat; const bool pixelFormatKnown = ConvertPixelFormat(fileHeader.glInternalFormat, pixelFormat); - if(!pixelFormatKnown) + if(DALI_UNLIKELY(!pixelFormatKnown)) { DALI_LOG_ERROR("No internal pixel format supported for KTX file pixel format.\n"); return false; @@ -591,14 +591,14 @@ bool LoadBitmapFromKtx(const Dali::ImageLoader::Input& input, Dali::Devel::Pixel pixels = bitmap.GetBuffer(); } - if(!pixels) + if(DALI_UNLIKELY(!pixels)) { DALI_LOG_ERROR("Unable to reserve a pixel buffer to load the requested bitmap into.\n"); return false; } const size_t bytesRead = fread(pixels, 1, imageByteCount, fp); - if(bytesRead != imageByteCount) + if(DALI_UNLIKELY(bytesRead != imageByteCount)) { DALI_LOG_ERROR("Read of image pixel data failed.\n"); return false; diff --git a/dali/internal/imaging/common/loader-png.cpp b/dali/internal/imaging/common/loader-png.cpp index 397b822..ae63822 100644 --- a/dali/internal/imaging/common/loader-png.cpp +++ b/dali/internal/imaging/common/loader-png.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Samsung Electronics Co., Ltd. + * Copyright (c) 2022 Samsung Electronics Co., Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -59,13 +59,13 @@ bool LoadPngHeader(FILE* fp, unsigned int& width, unsigned int& height, png_stru // Check header to see if it is a PNG file size_t size = fread(header, 1, 8, fp); - if(size != 8) + if(DALI_UNLIKELY(size != 8)) { DALI_LOG_ERROR("fread failed\n"); return false; } - if(png_sig_cmp(header, 0, 8)) + if(DALI_UNLIKELY(png_sig_cmp(header, 0, 8))) { DALI_LOG_ERROR("png_sig_cmp failed\n"); return false; @@ -73,14 +73,14 @@ bool LoadPngHeader(FILE* fp, unsigned int& width, unsigned int& height, png_stru png = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); - if(!png) + if(DALI_UNLIKELY(!png)) { DALI_LOG_ERROR("Can't create PNG read structure\n"); return false; } info = png_create_info_struct(png); - if(!info) + if(DALI_UNLIKELY(!info)) { DALI_LOG_ERROR("png_create_info_struct failed\n"); return false; @@ -88,7 +88,7 @@ bool LoadPngHeader(FILE* fp, unsigned int& width, unsigned int& height, png_stru png_set_expand(png); - if(setjmp(png_jmpbuf(png))) + if(DALI_UNLIKELY(setjmp(png_jmpbuf(png)))) { DALI_LOG_ERROR("error during png_init_io\n"); return false; @@ -134,7 +134,7 @@ bool LoadBitmapFromPng(const Dali::ImageLoader::Input& input, Dali::Devel::Pixel bool valid = false; // Load info from the header - if(!LoadPngHeader(input.file, width, height, png, info)) + if(DALI_UNLIKELY(!LoadPngHeader(input.file, width, height, png, info))) { return false; } @@ -259,7 +259,7 @@ bool LoadBitmapFromPng(const Dali::ImageLoader::Input& input, Dali::Devel::Pixel } } - if(!valid) + if(DALI_UNLIKELY(!valid)) { DALI_LOG_ERROR("Unsupported png format\n"); return false; @@ -270,7 +270,7 @@ bool LoadBitmapFromPng(const Dali::ImageLoader::Input& input, Dali::Devel::Pixel png_read_update_info(png, info); - if(setjmp(png_jmpbuf(png))) + if(DALI_UNLIKELY(setjmp(png_jmpbuf(png)))) { DALI_LOG_ERROR("error during png_read_image\n"); return false; @@ -300,18 +300,18 @@ bool LoadBitmapFromPng(const Dali::ImageLoader::Input& input, Dali::Devel::Pixel break; } } - - // decode the whole image into bitmap buffer - auto pixels = (bitmap = Dali::Devel::PixelBuffer::New(bufferWidth, bufferHeight, pixelFormat)).GetBuffer(); - - DALI_ASSERT_DEBUG(pixels); rows = reinterpret_cast(malloc(sizeof(png_bytep) * height)); - if(!rows) + if(DALI_UNLIKELY(!rows)) { DALI_LOG_ERROR("malloc is failed\n"); return false; } + // decode the whole image into bitmap buffer + auto pixels = (bitmap = Dali::Devel::PixelBuffer::New(bufferWidth, bufferHeight, pixelFormat)).GetBuffer(); + + DALI_ASSERT_DEBUG(pixels); + for(y = 0; y < height; y++) { rows[y] = pixels + y * stride; diff --git a/dali/internal/imaging/common/loader-wbmp.cpp b/dali/internal/imaging/common/loader-wbmp.cpp index aee8621..0fd3773 100644 --- a/dali/internal/imaging/common/loader-wbmp.cpp +++ b/dali/internal/imaging/common/loader-wbmp.cpp @@ -47,7 +47,7 @@ Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_LOA ((1ULL << (29)) - 2048)) //extract multiple bytes integer , and saved in *data -int extractMultiByteInteger(unsigned int* data, void* map, size_t length, size_t* position) +int extractMultiByteInteger(unsigned int* data, const std::uint8_t* const& map, size_t length, size_t* position) { // the header field contains an image type indentifier of multi-byte length(TypeField), an octet of general header info(FixHeaderField) //, a multi-byte width field(Width) and a multi-byte height field(Height) and so on. @@ -66,15 +66,15 @@ int extractMultiByteInteger(unsigned int* data, void* map, size_t length, size_t // for general width and height, if(buf & 0x80) == 0, then the next byte does not need to fetch again // first step, readBufCount = 1 , read int(4 bytes) to buf, if buf & 0x80 !=0, the buf need to continue to fetch // second step, readBufCount = 2, read next( 4 bytes) to buf, if buf & 0x80 == 0, then assigned the buf to target - if((readBufCount++) == 4) + if(DALI_UNLIKELY((readBufCount++) == 4)) { return -1; } - if(*position > length) + if(DALI_UNLIKELY(*position > length)) { return -1; } - buf = reinterpret_cast(map)[(*position)++]; + buf = map[(*position)++]; targetMultiByteInteger = (targetMultiByteInteger << 7) | (buf & 0x7f); if((buf & 0x80) == 0) @@ -121,7 +121,7 @@ constexpr std::uint32_t cachedCalculation4BitTo4ByteTable[16] = { bool LoadBitmapFromWbmp(const Dali::ImageLoader::Input& input, Dali::Devel::PixelBuffer& bitmap) { FILE* const fp = input.file; - if(fp == NULL) + if(DALI_UNLIKELY(fp == NULL)) { DALI_LOG_ERROR("Error loading bitmap\n"); return false; @@ -133,7 +133,7 @@ bool LoadBitmapFromWbmp(const Dali::ImageLoader::Input& input, Dali::Devel::Pixe std::uint32_t type; std::uint32_t lineByteLength; - if(fseek(fp, 0, SEEK_END)) + if(DALI_UNLIKELY(fseek(fp, 0, SEEK_END))) { DALI_LOG_ERROR("Error seeking WBMP data\n"); return false; @@ -146,57 +146,59 @@ bool LoadBitmapFromWbmp(const Dali::ImageLoader::Input& input, Dali::Devel::Pixe fsize = static_cast(positionIndicator); } - if(0u == fsize) + if(DALI_UNLIKELY(0u == fsize)) { DALI_LOG_ERROR("Error: filesize is 0!\n"); return false; } - if(fseek(fp, 0, SEEK_SET)) + if(DALI_UNLIKELY(fseek(fp, 0, SEEK_SET))) { DALI_LOG_ERROR("Error seeking WBMP data\n"); return false; } - if(fsize <= 4) + if(DALI_UNLIKELY(fsize <= 4)) { DALI_LOG_ERROR("Error: WBMP Raw Data Not Found!\n"); return false; } - if(fsize > 4096 * 4096 * 4) + if(DALI_UNLIKELY(fsize > 4096 * 4096 * 4)) { DALI_LOG_ERROR("Error: WBMP size is too large!\n"); return false; } map.ResizeUninitialized(fsize); - if(fread(&map[0], 1, fsize, fp) != fsize) + if(DALI_UNLIKELY(fread(&map[0], 1, fsize, fp) != fsize)) { DALI_LOG_WARNING("image file read opeation error!\n"); return false; } - if(extractMultiByteInteger(&type, &map[0], fsize, &position) < 0) + const std::uint8_t* const inputBufferPtr = &map[0]; + + if(DALI_UNLIKELY(extractMultiByteInteger(&type, inputBufferPtr, fsize, &position) < 0)) { return false; } position++; /* skipping one byte */ - if(extractMultiByteInteger(&w, &map[0], fsize, &position) < 0) + if(DALI_UNLIKELY(extractMultiByteInteger(&w, inputBufferPtr, fsize, &position) < 0)) { return false; } - if(extractMultiByteInteger(&h, &map[0], fsize, &position) < 0) + if(DALI_UNLIKELY(extractMultiByteInteger(&h, inputBufferPtr, fsize, &position) < 0)) { return false; } - if(type != 0) + if(DALI_UNLIKELY(type != 0)) { DALI_LOG_ERROR("Unknown Format!\n"); return false; } - if((w < 1) || (h < 1) || (w > IMG_MAX_SIZE) || (h > IMG_MAX_SIZE)) + if(DALI_UNLIKELY((w < 1) || (h < 1) || (w > IMG_MAX_SIZE) || (h > IMG_MAX_SIZE))) { return false; } @@ -211,6 +213,7 @@ bool LoadBitmapFromWbmp(const Dali::ImageLoader::Input& input, Dali::Devel::Pixe // w >= 1 and h >= 1. So we can assume that outputPixels is not null. auto outputPixels = (bitmap = Dali::Devel::PixelBuffer::New(w, h, Pixel::L8)).GetBuffer(); + /** * @code * std::uint8_t* line = NULL; @@ -237,13 +240,13 @@ bool LoadBitmapFromWbmp(const Dali::ImageLoader::Input& input, Dali::Devel::Pixe * @endcode */ - const std::uint8_t* inputPixels = &map[0] + position; + const std::uint8_t* inputPixels = inputBufferPtr + position; const std::uint32_t lineByteLengthWithoutPadding = w >> 3; const std::uint8_t linePadding = w & 0x07; - for(std::uint32_t y = 0; y < h; y++) + for(std::uint32_t y = 0; y < h; ++y) { - for(std::uint32_t x = 0; x < lineByteLengthWithoutPadding; x++) + for(std::uint32_t x = 0; x < lineByteLengthWithoutPadding; ++x) { // memset whole 8 bits // outputPixels filled 4 bytes in one operation. @@ -272,7 +275,7 @@ bool LoadBitmapFromWbmp(const Dali::ImageLoader::Input& input, Dali::Devel::Pixe bool LoadWbmpHeader(const Dali::ImageLoader::Input& input, unsigned int& width, unsigned int& height) { FILE* const fp = input.file; - if(fp == NULL) + if(DALI_UNLIKELY(fp == NULL)) { DALI_LOG_ERROR("Error loading bitmap\n"); return false; @@ -282,7 +285,7 @@ bool LoadWbmpHeader(const Dali::ImageLoader::Input& input, unsigned int& width, unsigned int w, h; unsigned int type; - if(fseek(fp, 0, SEEK_END)) + if(DALI_UNLIKELY(fseek(fp, 0, SEEK_END))) { DALI_LOG_ERROR("Error seeking WBMP data\n"); return false; @@ -295,17 +298,17 @@ bool LoadWbmpHeader(const Dali::ImageLoader::Input& input, unsigned int& width, fsize = static_cast(positionIndicator); } - if(0u == fsize) + if(DALI_UNLIKELY(0u == fsize)) { return false; } - if(fseek(fp, 0, SEEK_SET)) + if(DALI_UNLIKELY(fseek(fp, 0, SEEK_SET))) { DALI_LOG_ERROR("Error seeking WBMP data\n"); return false; } - if(fsize <= 4) + if(DALI_UNLIKELY(fsize <= 4)) { DALI_LOG_ERROR("Error: WBMP Raw Data Not Found!\n"); return false; @@ -316,35 +319,37 @@ bool LoadWbmpHeader(const Dali::ImageLoader::Input& input, unsigned int& width, headerSize = std::min(headerSize, fsize); map.ResizeUninitialized(headerSize); - if(fread(&map[0], 1, headerSize, fp) != headerSize) + if(DALI_UNLIKELY(fread(&map[0], 1, headerSize, fp) != headerSize)) { DALI_LOG_WARNING("image file read opeation error!\n"); return false; } - if(extractMultiByteInteger(&type, &map[0], headerSize, &position) < 0) + const std::uint8_t* const inputBufferPtr = &map[0]; + + if(DALI_UNLIKELY(extractMultiByteInteger(&type, inputBufferPtr, headerSize, &position) < 0)) { DALI_LOG_ERROR("Error: unable to read type!\n"); return false; } position++; /* skipping one byte */ - if(type != 0) + if(DALI_UNLIKELY(type != 0)) { DALI_LOG_ERROR("Error: unknown format!\n"); return false; } - if(extractMultiByteInteger(&w, &map[0], headerSize, &position) < 0) + if(DALI_UNLIKELY(extractMultiByteInteger(&w, inputBufferPtr, headerSize, &position) < 0)) { DALI_LOG_ERROR("Error: can not read width!\n"); return false; } - if(extractMultiByteInteger(&h, &map[0], headerSize, &position) < 0) + if(DALI_UNLIKELY(extractMultiByteInteger(&h, inputBufferPtr, headerSize, &position) < 0)) { DALI_LOG_ERROR("Error: can not read height!\n"); return false; } - if((w < 1) || (h < 1) || (w > IMG_MAX_SIZE) || (h > IMG_MAX_SIZE)) + if(DALI_UNLIKELY((w < 1) || (h < 1) || (w > IMG_MAX_SIZE) || (h > IMG_MAX_SIZE))) { DALI_LOG_ERROR("Error: file size is not supported!\n"); return false; diff --git a/dali/internal/imaging/common/loader-webp.cpp b/dali/internal/imaging/common/loader-webp.cpp index da82a89..00abbe5 100644 --- a/dali/internal/imaging/common/loader-webp.cpp +++ b/dali/internal/imaging/common/loader-webp.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Samsung Electronics Co., Ltd. + * Copyright (c) 2022 Samsung Electronics Co., Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -40,19 +40,19 @@ namespace TizenPlatform #ifdef DALI_ANIMATED_WEBP_ENABLED bool ReadWebPInformation(FILE* const fp, WebPData& webPData) { - if(fp == NULL) + if(DALI_UNLIKELY(fp == NULL)) { return false; } - if(fseek(fp, 0, SEEK_END) <= -1) + if(DALI_UNLIKELY(fseek(fp, 0, SEEK_END) <= -1)) { return false; } WebPDataInit(&webPData); webPData.size = ftell(fp); - if((!fseek(fp, 0, SEEK_SET))) + if(DALI_LIKELY(!fseek(fp, 0, SEEK_SET))) { unsigned char* WebPDataBuffer; WebPDataBuffer = reinterpret_cast(malloc(sizeof(WebPByteType) * webPData.size)); @@ -82,12 +82,12 @@ void ReleaseResource(WebPData& webPData, WebPAnimDecoder* webPAnimDecoder) bool LoadWebpHeader(const Dali::ImageLoader::Input& input, unsigned int& width, unsigned int& height) { FILE* const fp = input.file; - if(fp == NULL) + if(DALI_UNLIKELY(fp == NULL)) { return false; } - if(fseek(fp, 0, SEEK_END) <= -1) + if(DALI_UNLIKELY(fseek(fp, 0, SEEK_END) <= -1)) { return false; } @@ -95,12 +95,12 @@ bool LoadWebpHeader(const Dali::ImageLoader::Input& input, unsigned int& width, // If the image is non-animated webp #ifdef DALI_WEBP_AVAILABLE size_t webPSize = ftell(fp); - if((!fseek(fp, 0, SEEK_SET))) + if(DALI_LIKELY(!fseek(fp, 0, SEEK_SET))) { std::vector encodedImage; - encodedImage.resize(webPSize, 0); + encodedImage.resize(webPSize); size_t readCount = fread(&encodedImage[0], sizeof(uint8_t), encodedImage.size(), fp); - if(readCount != encodedImage.size()) + if(DALI_UNLIKELY(readCount != encodedImage.size())) { return false; } @@ -143,12 +143,12 @@ bool LoadWebpHeader(const Dali::ImageLoader::Input& input, unsigned int& width, bool LoadBitmapFromWebp(const Dali::ImageLoader::Input& input, Dali::Devel::PixelBuffer& bitmap) { FILE* const fp = input.file; - if(fp == NULL) + if(DALI_UNLIKELY(fp == NULL)) { return false; } - if(fseek(fp, 0, SEEK_END) <= -1) + if(DALI_UNLIKELY(fseek(fp, 0, SEEK_END) <= -1)) { return false; } @@ -156,26 +156,26 @@ bool LoadBitmapFromWebp(const Dali::ImageLoader::Input& input, Dali::Devel::Pixe // If the image is non-animated webp #ifdef DALI_WEBP_AVAILABLE size_t webPSize = ftell(fp); - if((!fseek(fp, 0, SEEK_SET))) + if(DALI_LIKELY(!fseek(fp, 0, SEEK_SET))) { std::vector encodedImage; - encodedImage.resize(webPSize, 0); + encodedImage.resize(webPSize); size_t readCount = fread(&encodedImage[0], sizeof(uint8_t), encodedImage.size(), fp); - if(readCount != encodedImage.size()) + if(DALI_UNLIKELY(readCount != encodedImage.size())) { DALI_LOG_ERROR("WebP image loading failed.\n"); return false; } int32_t width, height; - if(!WebPGetInfo(&encodedImage[0], encodedImage.size(), &width, &height)) + if(DALI_UNLIKELY(!WebPGetInfo(&encodedImage[0], encodedImage.size(), &width, &height))) { DALI_LOG_ERROR("Cannot retrieve WebP image size information.\n"); return false; } WebPBitstreamFeatures features; - if(VP8_STATUS_NOT_ENOUGH_DATA == WebPGetFeatures(&encodedImage[0], encodedImage.size(), &features)) + if(DALI_UNLIKELY(VP8_STATUS_NOT_ENOUGH_DATA == WebPGetFeatures(&encodedImage[0], encodedImage.size(), &features))) { DALI_LOG_ERROR("Cannot retrieve WebP image features.\n"); return false; diff --git a/dali/internal/imaging/common/webp-loading.cpp b/dali/internal/imaging/common/webp-loading.cpp index 707821b..cd829d7 100644 --- a/dali/internal/imaging/common/webp-loading.cpp +++ b/dali/internal/imaging/common/webp-loading.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Samsung Electronics Co., Ltd. + * Copyright (c) 2022 Samsung Electronics Co., Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -74,7 +74,7 @@ public: mLoadSucceeded(true) { // mFrameCount will be 1 if the input image is non-animated image or animated image with single frame. - if(ReadWebPInformation(isLocalResource)) + if(DALI_LIKELY(ReadWebPInformation(isLocalResource))) { #ifdef DALI_ANIMATED_WEBP_ENABLED WebPDataInit(&mWebPData); @@ -116,18 +116,18 @@ public: { Internal::Platform::FileReader fileReader(mUrl); fp = fileReader.GetFile(); - if(fp == nullptr) + if(DALI_UNLIKELY(fp == nullptr)) { return false; } - if(fseek(fp, 0, SEEK_END) <= -1) + if(DALI_UNLIKELY(fseek(fp, 0, SEEK_END) <= -1)) { return false; } mBufferSize = ftell(fp); - if(!fseek(fp, 0, SEEK_SET)) + if(DALI_LIKELY(!fseek(fp, 0, SEEK_SET))) { mBuffer = reinterpret_cast(malloc(sizeof(WebPByteType) * mBufferSize)); mBufferSize = fread(mBuffer, sizeof(WebPByteType), mBufferSize, fp); @@ -142,17 +142,17 @@ public: size_t dataSize; succeeded = TizenPlatform::Network::DownloadRemoteFileIntoMemory(mUrl, dataBuffer, dataSize, MAXIMUM_DOWNLOAD_IMAGE_SIZE); - if(succeeded) + if(DALI_LIKELY(succeeded)) { mBufferSize = dataBuffer.Size(); - if(mBufferSize > 0U) + if(DALI_LIKELY(mBufferSize > 0U)) { // Open a file handle on the memory buffer: Internal::Platform::FileReader fileReader(dataBuffer, mBufferSize); fp = fileReader.GetFile(); - if(fp != nullptr) + if(DALI_LIKELY(fp != nullptr)) { - if(!fseek(fp, 0, SEEK_SET)) + if(DALI_LIKELY(!fseek(fp, 0, SEEK_SET))) { mBuffer = reinterpret_cast(malloc(sizeof(WebPByteType) * mBufferSize)); mBufferSize = fread(mBuffer, sizeof(WebPByteType), mBufferSize, fp); @@ -242,7 +242,7 @@ bool WebPLoading::LoadNextNFrames(uint32_t frameStartIndex, int count, std::vect Dali::PixelData imageData = Devel::PixelBuffer::Convert(pixelBuffer); pixelData.push_back(imageData); } - if(pixelData.size() != static_cast(count)) + if(DALI_UNLIKELY(pixelData.size() != static_cast(count))) { return false; } @@ -259,13 +259,13 @@ Dali::Devel::PixelBuffer WebPLoading::LoadFrame(uint32_t frameIndex) if(mImpl->mFrameCount == 1) { int32_t width, height; - if(!WebPGetInfo(mImpl->mBuffer, mImpl->mBufferSize, &width, &height)) + if(DALI_UNLIKELY(!WebPGetInfo(mImpl->mBuffer, mImpl->mBufferSize, &width, &height))) { return pixelBuffer; } WebPBitstreamFeatures features; - if(VP8_STATUS_NOT_ENOUGH_DATA == WebPGetFeatures(mImpl->mBuffer, mImpl->mBufferSize, &features)) + if(DALI_UNLIKELY(VP8_STATUS_NOT_ENOUGH_DATA == WebPGetFeatures(mImpl->mBuffer, mImpl->mBufferSize, &features))) { return pixelBuffer; } @@ -296,7 +296,7 @@ Dali::Devel::PixelBuffer WebPLoading::LoadFrame(uint32_t frameIndex) #ifdef DALI_ANIMATED_WEBP_ENABLED Mutex::ScopedLock lock(mImpl->mMutex); - if(frameIndex >= mImpl->mWebPAnimInfo.frame_count || !mImpl->mLoadSucceeded) + if(DALI_UNLIKELY(frameIndex >= mImpl->mWebPAnimInfo.frame_count || !mImpl->mLoadSucceeded)) { return pixelBuffer; }