2012-04-19 14:35:52 +08:00
/****************************************************************************
2014-01-07 11:25:07 +08:00
Copyright ( c ) 2010 - 2012 cocos2d - x . org
2018-01-29 16:25:32 +08:00
Copyright ( c ) 2013 - 2016 Chukong Technologies Inc .
Copyright ( c ) 2017 - 2018 Xiamen Yaji Software Co . , Ltd .
2020-09-01 13:24:45 +08:00
Copyright ( c ) 2020 c4games . com
2012-04-19 14:35:52 +08:00
http : //www.cocos2d-x.org
Permission is hereby granted , free of charge , to any person obtaining a copy
of this software and associated documentation files ( the " Software " ) , to deal
in the Software without restriction , including without limitation the rights
to use , copy , modify , merge , publish , distribute , sublicense , and / or sell
copies of the Software , and to permit persons to whom the Software is
furnished to do so , subject to the following conditions :
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software .
THE SOFTWARE IS PROVIDED " AS IS " , WITHOUT WARRANTY OF ANY KIND , EXPRESS OR
IMPLIED , INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY ,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT . IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM , DAMAGES OR OTHER
LIABILITY , WHETHER IN AN ACTION OF CONTRACT , TORT OR OTHERWISE , ARISING FROM ,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2013-07-25 21:35:00 +08:00
2014-05-17 05:36:00 +08:00
# include "platform/CCImage.h"
2020-09-25 11:07:56 +08:00
# include "renderer/backend/TextureUtils.h"
2013-06-18 17:10:54 +08:00
2012-04-19 14:35:52 +08:00
# include <string>
# include <ctype.h>
2019-08-20 15:15:40 +08:00
# include "base/ccConfig.h" // CC_USE_JPEG, CC_USE_WEBP
2019-11-25 02:28:38 +08:00
# define STBI_NO_JPEG
# define STBI_NO_PNG
# define STBI_NO_GIF
# define STBI_NO_PSD
# define STBI_NO_PIC
# define STBI_NO_PNM
# define STBI_NO_HDR
# define STBI_NO_TGA
# define STB_IMAGE_IMPLEMENTATION
2020-06-14 15:52:15 +08:00
# if CC_TARGET_PLATFORM == CC_PLATFORM_IOS
# define STBI_NO_THREAD_LOCALS
# endif
2019-11-25 02:28:38 +08:00
# include "stb/stb_image.h"
2014-05-17 05:36:00 +08:00
2013-08-06 11:34:48 +08:00
extern " C "
{
2014-06-06 12:09:39 +08:00
// To resolve link error when building 32bits with Xcode 6.
// More information please refer to the discussion in https://github.com/cocos2d/cocos2d-x/pull/6986
2014-07-21 18:12:46 +08:00
# if defined (__unix) || (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
2014-06-05 21:54:00 +08:00
# ifndef __ENABLE_COMPATIBILITY_WITH_UNIX_2003__
# define __ENABLE_COMPATIBILITY_WITH_UNIX_2003__
# include <stdio.h>
2017-01-19 13:55:14 +08:00
# include <dirent.h>
2014-06-05 21:54:00 +08:00
FILE * fopen $ UNIX2003 ( const char * filename , const char * mode )
{
return fopen ( filename , mode ) ;
}
size_t fwrite $ UNIX2003 ( const void * a , size_t b , size_t c , FILE * d )
{
return fwrite ( a , b , c , d ) ;
}
2017-01-19 13:55:14 +08:00
int fputs $ UNIX2003 ( const char * res1 , FILE * res2 ) {
return fputs ( res1 , res2 ) ;
}
2014-06-05 21:54:00 +08:00
char * strerror $ UNIX2003 ( int errnum )
{
return strerror ( errnum ) ;
}
2017-01-19 13:55:14 +08:00
DIR * opendir $ INODE64 $ UNIX2003 ( char * dirName )
{
return opendir ( dirName ) ;
}
DIR * opendir $ INODE64 ( char * dirName )
{
return opendir ( dirName ) ;
}
int closedir $ UNIX2003 ( DIR * dir )
{
return closedir ( dir ) ;
}
struct dirent * readdir $ INODE64 ( DIR * dir )
{
return readdir ( dir ) ;
}
2014-06-05 21:54:00 +08:00
# endif
2014-07-22 17:03:27 +08:00
# endif
2015-04-16 00:19:38 +08:00
# if CC_USE_PNG
2013-07-25 21:35:00 +08:00
# include "png.h"
2015-04-16 00:19:38 +08:00
# endif //CC_USE_PNG
2014-05-01 10:09:13 +08:00
# include "base/etc1.h"
2020-08-30 17:58:39 +08:00
# include "base/etc2.h"
2020-02-11 23:54:17 +08:00
# include "base/astc.h"
2014-08-21 15:19:26 +08:00
# if CC_USE_JPEG
# include "jpeglib.h"
2018-02-08 09:24:33 +08:00
# include <setjmp.h>
2014-08-21 15:19:26 +08:00
# endif // CC_USE_JPEG
2013-08-06 11:34:48 +08:00
}
2014-05-01 10:09:13 +08:00
# include "base/s3tc.h"
# include "base/atitc.h"
2014-03-18 02:28:24 +08:00
# include "base/pvr.h"
2014-05-17 05:36:00 +08:00
# include "base/TGAlib.h"
2013-10-14 14:01:00 +08:00
2014-08-21 15:19:26 +08:00
# if CC_USE_WEBP
# include "decode.h"
# endif // CC_USE_WEBP
2014-04-30 08:37:36 +08:00
# include "base/ccMacros.h"
2016-03-20 21:53:44 +08:00
# include "platform/CCCommon.h"
# include "platform/CCStdC.h"
# include "platform/CCFileUtils.h"
2014-05-01 10:09:13 +08:00
# include "base/CCConfiguration.h"
2014-05-17 05:36:00 +08:00
# include "base/ccUtils.h"
2014-04-30 08:37:36 +08:00
# include "base/ZipUtils.h"
2013-07-25 21:35:00 +08:00
# if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
2016-03-20 21:53:44 +08:00
# include "platform/android/CCFileUtils-android.h"
2019-12-02 23:01:06 +08:00
# include "platform/CCGL.h"
2013-07-25 21:35:00 +08:00
# endif
2013-08-16 14:03:30 +08:00
# define CC_GL_ATC_RGB_AMD 0x8C92
# define CC_GL_ATC_RGBA_EXPLICIT_ALPHA_AMD 0x8C93
# define CC_GL_ATC_RGBA_INTERPOLATED_ALPHA_AMD 0x87EE
2020-02-05 22:45:19 +08:00
# define ASTC_MAGIC_FILE_CONSTANT 0x5CA1AB13
2012-05-25 16:52:47 +08:00
NS_CC_BEGIN
2013-07-25 21:35:00 +08:00
//////////////////////////////////////////////////////////////////////////
//struct and data for pvr structure
2013-08-08 14:11:22 +08:00
2013-08-01 15:53:52 +08:00
namespace
2013-07-25 21:35:00 +08:00
{
2013-08-01 15:53:52 +08:00
static const int PVR_TEXTURE_FLAG_TYPE_MASK = 0xff ;
2014-08-21 15:19:26 +08:00
2013-08-01 15:53:52 +08:00
// Values taken from PVRTexture.h from http://www.imgtec.com
2013-08-13 10:11:28 +08:00
enum class PVR2TextureFlag
{
2013-08-06 16:14:36 +08:00
Mipmap = ( 1 < < 8 ) , // has mip map levels
Twiddle = ( 1 < < 9 ) , // is twiddled
Bumpmap = ( 1 < < 10 ) , // has normals encoded for a bump map
Tiling = ( 1 < < 11 ) , // is bordered for tiled pvr
Cubemap = ( 1 < < 12 ) , // is a cubemap/skybox
FalseMipCol = ( 1 < < 13 ) , // are there false colored MIP levels
Volume = ( 1 < < 14 ) , // is this a volume texture
Alpha = ( 1 < < 15 ) , // v2.1 is there transparency info in the texture
VerticalFlip = ( 1 < < 16 ) , // v2.1 is the texture vertically flipped
} ;
2013-08-01 15:53:52 +08:00
2013-08-06 16:14:36 +08:00
enum class PVR3TextureFlag
{
2015-08-14 21:17:51 +08:00
PremultipliedAlpha = ( 1 < < 1 ) // has premultiplied alpha
2013-08-01 15:53:52 +08:00
} ;
2013-08-06 16:14:36 +08:00
static const char gPVRTexIdentifier [ 5 ] = " PVR! " ;
// v2
2020-09-01 14:57:18 +08:00
enum class PVR2TexturePixelFormat : uint8_t
2013-08-06 16:14:36 +08:00
{
RGBA4444 = 0x10 ,
RGBA5551 ,
RGBA8888 ,
RGB565 ,
RGB555 , // unsupported
RGB888 ,
I8 ,
AI88 ,
PVRTC2BPP_RGBA ,
PVRTC4BPP_RGBA ,
BGRA8888 ,
A8 ,
2013-08-13 10:11:28 +08:00
} ;
2013-08-06 16:14:36 +08:00
2013-08-13 10:11:28 +08:00
// v3
enum class PVR3TexturePixelFormat : uint64_t
{
PVRTC2BPP_RGB = 0ULL ,
PVRTC2BPP_RGBA = 1ULL ,
PVRTC4BPP_RGB = 2ULL ,
PVRTC4BPP_RGBA = 3ULL ,
2014-03-18 02:28:24 +08:00
PVRTC2_2BPP_RGBA = 4ULL ,
PVRTC2_4BPP_RGBA = 5ULL ,
ETC1 = 6ULL ,
DXT1 = 7ULL ,
DXT2 = 8ULL ,
DXT3 = 9ULL ,
DXT4 = 10ULL ,
DXT5 = 11ULL ,
BC1 = 7ULL ,
BC2 = 9ULL ,
BC3 = 11ULL ,
BC4 = 12ULL ,
BC5 = 13ULL ,
BC6 = 14ULL ,
BC7 = 15ULL ,
UYVY = 16ULL ,
YUY2 = 17ULL ,
BW1bpp = 18ULL ,
R9G9B9E5 = 19ULL ,
RGBG8888 = 20ULL ,
GRGB8888 = 21ULL ,
ETC2_RGB = 22ULL ,
ETC2_RGBA = 23ULL ,
ETC2_RGBA1 = 24ULL ,
EAC_R11_Unsigned = 25ULL ,
EAC_R11_Signed = 26ULL ,
EAC_RG11_Unsigned = 27ULL ,
EAC_RG11_Signed = 28ULL ,
2013-08-06 16:14:36 +08:00
2013-08-13 10:11:28 +08:00
BGRA8888 = 0x0808080861726762ULL ,
RGBA8888 = 0x0808080861626772ULL ,
RGBA4444 = 0x0404040461626772ULL ,
RGBA5551 = 0x0105050561626772ULL ,
RGB565 = 0x0005060500626772ULL ,
RGB888 = 0x0008080800626772ULL ,
A8 = 0x0000000800000061ULL ,
L8 = 0x000000080000006cULL ,
LA88 = 0x000008080000616cULL ,
} ;
2013-08-06 16:14:36 +08:00
2013-08-13 10:11:28 +08:00
// v2
2019-06-03 09:39:51 +08:00
typedef const std : : map < PVR2TexturePixelFormat , backend : : PixelFormat > _pixel2_formathash ;
2013-08-13 10:11:28 +08:00
static const _pixel2_formathash : : value_type v2_pixel_formathash_value [ ] =
{
2020-09-25 11:07:56 +08:00
_pixel2_formathash : : value_type ( PVR2TexturePixelFormat : : BGRA8888 , backend : : PixelFormat : : BGRA8 ) ,
_pixel2_formathash : : value_type ( PVR2TexturePixelFormat : : RGBA8888 , backend : : PixelFormat : : RGBA8 ) ,
_pixel2_formathash : : value_type ( PVR2TexturePixelFormat : : RGBA4444 , backend : : PixelFormat : : RGBA4 ) ,
2019-06-03 09:39:51 +08:00
_pixel2_formathash : : value_type ( PVR2TexturePixelFormat : : RGBA5551 , backend : : PixelFormat : : RGB5A1 ) ,
_pixel2_formathash : : value_type ( PVR2TexturePixelFormat : : RGB565 , backend : : PixelFormat : : RGB565 ) ,
2020-09-25 11:07:56 +08:00
_pixel2_formathash : : value_type ( PVR2TexturePixelFormat : : RGB888 , backend : : PixelFormat : : RGB8 ) ,
2019-06-03 09:39:51 +08:00
_pixel2_formathash : : value_type ( PVR2TexturePixelFormat : : A8 , backend : : PixelFormat : : A8 ) ,
2020-09-25 11:07:56 +08:00
_pixel2_formathash : : value_type ( PVR2TexturePixelFormat : : I8 , backend : : PixelFormat : : L8 ) ,
_pixel2_formathash : : value_type ( PVR2TexturePixelFormat : : AI88 , backend : : PixelFormat : : LA8 ) ,
2013-08-06 16:14:36 +08:00
2019-06-03 09:39:51 +08:00
_pixel2_formathash : : value_type ( PVR2TexturePixelFormat : : PVRTC2BPP_RGBA , backend : : PixelFormat : : PVRTC2A ) ,
_pixel2_formathash : : value_type ( PVR2TexturePixelFormat : : PVRTC4BPP_RGBA , backend : : PixelFormat : : PVRTC4A ) ,
2013-08-13 10:11:28 +08:00
} ;
2013-08-06 16:14:36 +08:00
2013-08-13 10:11:28 +08:00
static const int PVR2_MAX_TABLE_ELEMENTS = sizeof ( v2_pixel_formathash_value ) / sizeof ( v2_pixel_formathash_value [ 0 ] ) ;
static const _pixel2_formathash v2_pixel_formathash ( v2_pixel_formathash_value , v2_pixel_formathash_value + PVR2_MAX_TABLE_ELEMENTS ) ;
2013-08-06 16:14:36 +08:00
2013-08-13 10:11:28 +08:00
// v3
2019-06-03 09:39:51 +08:00
typedef const std : : map < PVR3TexturePixelFormat , backend : : PixelFormat > _pixel3_formathash ;
2013-08-13 10:11:28 +08:00
static _pixel3_formathash : : value_type v3_pixel_formathash_value [ ] =
{
2020-09-25 11:07:56 +08:00
_pixel3_formathash : : value_type ( PVR3TexturePixelFormat : : BGRA8888 , backend : : PixelFormat : : BGRA8 ) ,
_pixel3_formathash : : value_type ( PVR3TexturePixelFormat : : RGBA8888 , backend : : PixelFormat : : RGBA8 ) ,
_pixel3_formathash : : value_type ( PVR3TexturePixelFormat : : RGBA4444 , backend : : PixelFormat : : RGBA4 ) ,
2019-06-03 09:39:51 +08:00
_pixel3_formathash : : value_type ( PVR3TexturePixelFormat : : RGBA5551 , backend : : PixelFormat : : RGB5A1 ) ,
_pixel3_formathash : : value_type ( PVR3TexturePixelFormat : : RGB565 , backend : : PixelFormat : : RGB565 ) ,
2020-09-25 11:07:56 +08:00
_pixel3_formathash : : value_type ( PVR3TexturePixelFormat : : RGB888 , backend : : PixelFormat : : RGB8 ) ,
2019-06-03 09:39:51 +08:00
_pixel3_formathash : : value_type ( PVR3TexturePixelFormat : : A8 , backend : : PixelFormat : : A8 ) ,
2020-09-25 11:07:56 +08:00
_pixel3_formathash : : value_type ( PVR3TexturePixelFormat : : L8 , backend : : PixelFormat : : L8 ) ,
_pixel3_formathash : : value_type ( PVR3TexturePixelFormat : : LA88 , backend : : PixelFormat : : LA8 ) ,
2013-08-06 16:14:36 +08:00
2019-06-03 09:39:51 +08:00
_pixel3_formathash : : value_type ( PVR3TexturePixelFormat : : PVRTC2BPP_RGB , backend : : PixelFormat : : PVRTC2 ) ,
_pixel3_formathash : : value_type ( PVR3TexturePixelFormat : : PVRTC2BPP_RGBA , backend : : PixelFormat : : PVRTC2A ) ,
_pixel3_formathash : : value_type ( PVR3TexturePixelFormat : : PVRTC4BPP_RGB , backend : : PixelFormat : : PVRTC4 ) ,
_pixel3_formathash : : value_type ( PVR3TexturePixelFormat : : PVRTC4BPP_RGBA , backend : : PixelFormat : : PVRTC4A ) ,
2014-03-18 02:28:24 +08:00
2020-09-01 11:43:03 +08:00
_pixel3_formathash : : value_type ( PVR3TexturePixelFormat : : ETC1 , backend : : PixelFormat : : ETC1 ) ,
2013-08-13 10:11:28 +08:00
} ;
2013-08-06 16:14:36 +08:00
2013-08-13 10:11:28 +08:00
static const int PVR3_MAX_TABLE_ELEMENTS = sizeof ( v3_pixel_formathash_value ) / sizeof ( v3_pixel_formathash_value [ 0 ] ) ;
2013-08-06 16:14:36 +08:00
2013-08-13 10:11:28 +08:00
static const _pixel3_formathash v3_pixel_formathash ( v3_pixel_formathash_value , v3_pixel_formathash_value + PVR3_MAX_TABLE_ELEMENTS ) ;
2013-08-06 16:14:36 +08:00
2013-08-13 10:11:28 +08:00
typedef struct _PVRTexHeader
{
unsigned int headerLength ;
unsigned int height ;
unsigned int width ;
unsigned int numMipmaps ;
unsigned int flags ;
unsigned int dataLength ;
unsigned int bpp ;
unsigned int bitmaskRed ;
unsigned int bitmaskGreen ;
unsigned int bitmaskBlue ;
unsigned int bitmaskAlpha ;
unsigned int pvrTag ;
unsigned int numSurfs ;
} PVRv2TexHeader ;
2013-08-06 16:14:36 +08:00
2013-07-25 21:35:00 +08:00
# ifdef _MSC_VER
# pragma pack(push,1)
# endif
2013-08-13 10:11:28 +08:00
typedef struct
{
uint32_t version ;
uint32_t flags ;
uint64_t pixelFormat ;
uint32_t colorSpace ;
uint32_t channelType ;
uint32_t height ;
uint32_t width ;
uint32_t depth ;
uint32_t numberOfSurfaces ;
uint32_t numberOfFaces ;
uint32_t numberOfMipmaps ;
uint32_t metadataLength ;
2013-07-25 21:35:00 +08:00
# ifdef _MSC_VER
2013-08-13 10:11:28 +08:00
} PVRv3TexHeader ;
2013-07-25 21:35:00 +08:00
# pragma pack(pop)
# else
2013-08-06 16:14:36 +08:00
} __attribute__ ( ( packed ) ) PVRv3TexHeader ;
2013-07-25 21:35:00 +08:00
# endif
2013-08-01 15:53:52 +08:00
}
2013-07-25 21:35:00 +08:00
//pvr structure end
2013-08-06 11:19:45 +08:00
2013-07-25 21:35:00 +08:00
//////////////////////////////////////////////////////////////////////////
2013-08-06 11:19:45 +08:00
//struct and data for s3tc(dds) struct
2013-08-08 14:59:46 +08:00
namespace
2013-08-06 11:19:45 +08:00
{
2013-08-09 12:54:05 +08:00
struct DDColorKey
2013-08-06 11:19:45 +08:00
{
2013-08-09 12:54:05 +08:00
uint32_t colorSpaceLowValue ;
uint32_t colorSpaceHighValue ;
} ;
2013-08-06 11:19:45 +08:00
2013-08-09 12:54:05 +08:00
struct DDSCaps
2013-08-06 11:19:45 +08:00
{
2013-08-09 12:54:05 +08:00
uint32_t caps ;
uint32_t caps2 ;
uint32_t caps3 ;
uint32_t caps4 ;
} ;
2013-08-06 11:19:45 +08:00
2013-08-09 12:54:05 +08:00
struct DDPixelFormat
{
uint32_t size ;
uint32_t flags ;
uint32_t fourCC ;
uint32_t RGBBitCount ;
uint32_t RBitMask ;
uint32_t GBitMask ;
uint32_t BBitMask ;
uint32_t ABitMask ;
} ;
2013-08-06 11:19:45 +08:00
2013-08-09 12:54:05 +08:00
struct DDSURFACEDESC2
2013-08-06 11:19:45 +08:00
{
2013-08-09 12:54:05 +08:00
uint32_t size ;
uint32_t flags ;
uint32_t height ;
uint32_t width ;
2013-08-08 14:59:46 +08:00
union
{
2013-08-09 12:54:05 +08:00
uint32_t pitch ;
uint32_t linearSize ;
2013-08-08 14:59:46 +08:00
} DUMMYUNIONNAMEN1 ;
union
{
2013-08-09 12:54:05 +08:00
uint32_t backBufferCount ;
uint32_t depth ;
2013-08-08 14:59:46 +08:00
} DUMMYUNIONNAMEN5 ;
union
{
2013-08-09 12:54:05 +08:00
uint32_t mipMapCount ;
uint32_t refreshRate ;
uint32_t srcVBHandle ;
2013-08-08 14:59:46 +08:00
} DUMMYUNIONNAMEN2 ;
2013-08-09 12:54:05 +08:00
uint32_t alphaBitDepth ;
uint32_t reserved ;
uint32_t surface ;
2013-08-08 14:59:46 +08:00
union
{
2013-08-09 12:54:05 +08:00
DDColorKey ddckCKDestOverlay ;
uint32_t emptyFaceColor ;
2013-08-08 14:59:46 +08:00
} DUMMYUNIONNAMEN3 ;
2013-08-09 12:54:05 +08:00
DDColorKey ddckCKDestBlt ;
DDColorKey ddckCKSrcOverlay ;
DDColorKey ddckCKSrcBlt ;
2013-08-08 14:59:46 +08:00
union
{
2013-08-09 12:54:05 +08:00
DDPixelFormat ddpfPixelFormat ;
uint32_t FVF ;
2013-08-08 14:59:46 +08:00
} DUMMYUNIONNAMEN4 ;
2013-08-09 12:54:05 +08:00
DDSCaps ddsCaps ;
uint32_t textureStage ;
} ;
2013-08-06 11:19:45 +08:00
2013-08-08 14:59:46 +08:00
# pragma pack(push,1)
2013-08-06 11:19:45 +08:00
2013-08-09 12:54:05 +08:00
struct S3TCTexHeader
2013-08-06 11:19:45 +08:00
{
2013-08-08 14:59:46 +08:00
char fileCode [ 4 ] ;
DDSURFACEDESC2 ddsd ;
2013-08-09 12:54:05 +08:00
} ;
2013-08-06 11:19:45 +08:00
# pragma pack(pop)
2013-08-08 14:59:46 +08:00
}
2013-08-06 11:19:45 +08:00
//s3tc struct end
2013-08-16 11:02:44 +08:00
//////////////////////////////////////////////////////////////////////////
//struct and data for atitc(ktx) struct
namespace
{
struct ATITCTexHeader
{
//HEADER
char identifier [ 12 ] ;
uint32_t endianness ;
uint32_t glType ;
uint32_t glTypeSize ;
uint32_t glFormat ;
uint32_t glInternalFormat ;
uint32_t glBaseInternalFormat ;
uint32_t pixelWidth ;
uint32_t pixelHeight ;
uint32_t pixelDepth ;
uint32_t numberOfArrayElements ;
uint32_t numberOfFaces ;
uint32_t numberOfMipmapLevels ;
uint32_t bytesOfKeyValueData ;
} ;
}
2016-03-29 09:52:17 +08:00
//atitc struct end
2013-08-16 11:02:44 +08:00
//////////////////////////////////////////////////////////////////////////
2013-08-06 11:19:45 +08:00
2020-02-05 22:45:19 +08:00
//struct and data for ASTC struct
namespace
{
struct ASTCTexHeader
{
uint8_t magic [ 4 ] ;
uint8_t blockdim_x ;
uint8_t blockdim_y ;
uint8_t blockdim_z ;
uint8_t xsize [ 3 ] ; // x-size = xsize[0] + xsize[1] + xsize[2]
uint8_t ysize [ 3 ] ; // x-size, y-size and z-size are given in texels;
uint8_t zsize [ 3 ] ; // block count is inferred
} ;
}
//atitc struct end
//////////////////////////////////////////////////////////////////////////
2013-08-08 14:59:46 +08:00
namespace
{
2013-08-06 16:14:36 +08:00
typedef struct
2012-04-19 14:35:52 +08:00
{
2020-09-01 14:57:18 +08:00
const uint8_t * data ;
2013-12-06 16:32:06 +08:00
ssize_t size ;
2013-08-06 16:14:36 +08:00
int offset ;
} tImageSource ;
2015-04-16 00:19:38 +08:00
2017-07-04 14:17:17 +08:00
# if CC_USE_PNG
2021-04-22 01:15:49 +08:00
void pngWriteCallback ( png_structp png_ptr , png_bytep data , size_t length )
{
if ( png_ptr = = NULL )
return ;
FileStream * fileStream = ( FileStream * ) png_get_io_ptr ( png_ptr ) ;
const auto check = fileStream - > write ( data , length ) ;
if ( check ! = length )
png_error ( png_ptr , " Write Error " ) ;
}
2013-08-06 16:14:36 +08:00
static void pngReadCallback ( png_structp png_ptr , png_bytep data , png_size_t length )
2012-04-19 14:35:52 +08:00
{
2013-08-06 16:14:36 +08:00
tImageSource * isource = ( tImageSource * ) png_get_io_ptr ( png_ptr ) ;
if ( ( int ) ( isource - > offset + length ) < = isource - > size )
{
memcpy ( data , isource - > data + isource - > offset , length ) ;
isource - > offset + = length ;
}
else
{
png_error ( png_ptr , " pngReaderCallback failed " ) ;
}
2012-04-19 14:35:52 +08:00
}
2015-04-16 00:19:38 +08:00
# endif //CC_USE_PNG
2012-04-19 14:35:52 +08:00
}
2020-09-01 11:43:03 +08:00
/*
2020-10-24 21:57:36 +08:00
* Notes : PVR file Specification have many pixel formats , cocos2d - x - v2 ~ v4 and engine - x only support pvrtc and etc1
2020-09-01 11:43:03 +08:00
* see : https : //cdn.imgtec.com/sdk-documentation/PVR+File+Format.Specification.pdf
*/
static backend : : PixelFormat getDevicePVRPixelFormat ( backend : : PixelFormat format )
2014-03-20 17:07:28 +08:00
{
switch ( format ) {
2019-06-03 09:39:51 +08:00
case backend : : PixelFormat : : PVRTC4 :
case backend : : PixelFormat : : PVRTC4A :
case backend : : PixelFormat : : PVRTC2 :
case backend : : PixelFormat : : PVRTC2A :
2014-03-20 17:07:28 +08:00
if ( Configuration : : getInstance ( ) - > supportsPVRTC ( ) )
return format ;
else
2020-09-25 11:07:56 +08:00
return backend : : PixelFormat : : RGBA8 ;
2020-09-01 11:43:03 +08:00
case backend : : PixelFormat : : ETC1 :
if ( Configuration : : getInstance ( ) - > supportsETC1 ( ) )
2020-08-31 23:09:42 +08:00
return format ;
2020-09-01 11:43:03 +08:00
else if ( Configuration : : getInstance ( ) - > supportsETC2 ( ) )
return backend : : PixelFormat : : ETC2_RGB ;
2020-08-31 23:09:42 +08:00
else
2020-09-25 11:07:56 +08:00
return backend : : PixelFormat : : RGBA8 ;
2014-03-20 17:07:28 +08:00
default :
return format ;
}
}
2020-08-31 23:09:42 +08:00
namespace
{
bool testFormatForPvr2TCSupport ( PVR2TexturePixelFormat /*format*/ )
{
return true ;
}
bool testFormatForPvr3TCSupport ( PVR3TexturePixelFormat format )
{
switch ( format ) {
case PVR3TexturePixelFormat : : DXT1 :
case PVR3TexturePixelFormat : : DXT3 :
case PVR3TexturePixelFormat : : DXT5 :
return Configuration : : getInstance ( ) - > supportsS3TC ( ) ;
case PVR3TexturePixelFormat : : BGRA8888 :
return Configuration : : getInstance ( ) - > supportsBGRA8888 ( ) ;
case PVR3TexturePixelFormat : : PVRTC2BPP_RGB :
case PVR3TexturePixelFormat : : PVRTC2BPP_RGBA :
case PVR3TexturePixelFormat : : PVRTC4BPP_RGB :
case PVR3TexturePixelFormat : : PVRTC4BPP_RGBA :
case PVR3TexturePixelFormat : : ETC1 :
case PVR3TexturePixelFormat : : RGBA8888 :
case PVR3TexturePixelFormat : : RGBA4444 :
case PVR3TexturePixelFormat : : RGBA5551 :
case PVR3TexturePixelFormat : : RGB565 :
case PVR3TexturePixelFormat : : RGB888 :
case PVR3TexturePixelFormat : : A8 :
case PVR3TexturePixelFormat : : L8 :
case PVR3TexturePixelFormat : : LA88 :
return true ;
default :
return false ;
}
}
}
namespace
{
static uint32_t makeFourCC ( char ch0 , char ch1 , char ch2 , char ch3 )
{
const uint32_t fourCC = ( ( uint32_t ) ( char ) ( ch0 ) | ( ( uint32_t ) ( char ) ( ch1 ) < < 8 ) | ( ( uint32_t ) ( char ) ( ch2 ) < < 16 ) | ( ( uint32_t ) ( char ) ( ch3 ) < < 24 ) ) ;
return fourCC ;
}
}
2012-04-19 14:35:52 +08:00
//////////////////////////////////////////////////////////////////////////
2013-06-20 14:13:12 +08:00
// Implement Image
2012-04-19 14:35:52 +08:00
//////////////////////////////////////////////////////////////////////////
2016-05-11 16:35:36 +08:00
bool Image : : PNG_PREMULTIPLIED_ALPHA_ENABLED = true ;
2020-11-16 12:21:27 +08:00
uint32_t Image : : COMPRESSED_IMAGE_PMA_FLAGS = Image : : CompressedImagePMAFlag : : ETC1 ;
void Image : : setCompressedImagesHavePMA ( uint32_t targets , bool havePMA )
{
if ( havePMA )
COMPRESSED_IMAGE_PMA_FLAGS | = targets ;
else
COMPRESSED_IMAGE_PMA_FLAGS & = ~ targets ;
}
bool Image : : isCompressedImageHavePMA ( uint32_t target )
{
return target & COMPRESSED_IMAGE_PMA_FLAGS ;
}
2012-04-19 14:35:52 +08:00
2013-06-20 14:13:12 +08:00
Image : : Image ( )
2014-01-09 16:33:31 +08:00
: _data ( nullptr )
2013-07-25 21:35:00 +08:00
, _dataLen ( 0 )
2019-11-20 18:19:24 +08:00
, _offset ( 0 )
2013-07-25 21:35:00 +08:00
, _width ( 0 )
2013-06-15 14:03:30 +08:00
, _height ( 0 )
2014-03-20 17:07:28 +08:00
, _unpack ( false )
2015-02-24 01:36:20 +08:00
, _fileType ( Format : : UNKNOWN )
2019-06-03 09:39:51 +08:00
, _pixelFormat ( backend : : PixelFormat : : NONE )
2013-07-25 21:35:00 +08:00
, _numberOfMipmaps ( 0 )
2016-07-26 18:17:10 +08:00
, _hasPremultipliedAlpha ( false )
2012-04-19 14:35:52 +08:00
{
}
2013-06-20 14:13:12 +08:00
Image : : ~ Image ( )
2012-04-19 14:35:52 +08:00
{
2020-08-31 21:04:47 +08:00
if ( ! _unpack )
2014-03-18 02:28:24 +08:00
{
2020-08-31 21:04:47 +08:00
CC_SAFE_FREE ( _data ) ;
}
else {
2014-11-22 08:23:54 +08:00
for ( int i = 0 ; i < _numberOfMipmaps ; + + i )
2020-08-31 21:04:47 +08:00
CC_SAFE_FREE ( _mipmaps [ i ] . address ) ;
2014-03-18 02:28:24 +08:00
}
2012-04-19 14:35:52 +08:00
}
2013-12-24 10:51:47 +08:00
bool Image : : initWithImageFile ( const std : : string & path )
2012-04-19 14:35:52 +08:00
{
2013-12-24 10:51:47 +08:00
bool ret = false ;
_filePath = FileUtils : : getInstance ( ) - > fullPathForFilename ( path ) ;
2013-05-18 08:11:52 +08:00
2013-12-18 14:58:17 +08:00
Data data = FileUtils : : getInstance ( ) - > getDataFromFile ( _filePath ) ;
2013-07-25 21:35:00 +08:00
2013-12-18 14:58:17 +08:00
if ( ! data . isNull ( ) )
2013-07-25 21:35:00 +08:00
{
2019-11-20 18:19:24 +08:00
ssize_t n = 0 ;
auto buf = data . takeBuffer ( & n ) ;
ret = initWithImageData ( buf , n , true ) ;
2013-07-25 21:35:00 +08:00
}
2013-05-18 08:11:52 +08:00
2013-12-24 10:51:47 +08:00
return ret ;
2012-04-19 14:35:52 +08:00
}
2013-12-24 15:49:58 +08:00
bool Image : : initWithImageFileThreadSafe ( const std : : string & fullpath )
2012-04-19 14:35:52 +08:00
{
2013-11-06 09:36:44 +08:00
bool ret = false ;
2013-11-29 14:31:42 +08:00
_filePath = fullpath ;
2013-12-18 14:58:17 +08:00
2020-08-18 14:29:09 +08:00
Data data = FileUtils : : getInstance ( ) - > getDataFromFile ( _filePath ) ;
2013-12-18 14:58:17 +08:00
if ( ! data . isNull ( ) )
2012-06-19 16:31:26 +08:00
{
2019-11-20 18:19:24 +08:00
ssize_t n = 0 ;
auto buf = data . takeBuffer ( & n ) ;
ret = initWithImageData ( buf , n , true ) ;
2012-06-19 16:31:26 +08:00
}
2013-12-18 14:58:17 +08:00
2013-11-06 09:36:44 +08:00
return ret ;
2012-04-19 14:35:52 +08:00
}
2020-09-01 14:57:18 +08:00
bool Image : : initWithImageData ( const uint8_t * data , ssize_t dataLen , bool ownData )
2012-04-19 14:35:52 +08:00
{
2013-08-12 17:05:19 +08:00
bool ret = false ;
2013-08-12 11:54:54 +08:00
do
2012-04-19 14:35:52 +08:00
{
2019-11-20 18:19:24 +08:00
CC_BREAK_IF ( ! data | | dataLen = = 0 ) ;
2013-08-12 11:54:54 +08:00
2020-09-01 14:57:18 +08:00
uint8_t * unpackedData = nullptr ;
2013-12-05 17:19:01 +08:00
ssize_t unpackedLen = 0 ;
2013-08-12 11:54:54 +08:00
2015-09-22 16:08:23 +08:00
//detect and unzip the compress file
2013-11-12 10:09:47 +08:00
if ( ZipUtils : : isCCZBuffer ( data , dataLen ) )
2013-08-12 11:54:54 +08:00
{
2013-11-12 10:09:47 +08:00
unpackedLen = ZipUtils : : inflateCCZBuffer ( data , dataLen , & unpackedData ) ;
2013-08-16 16:10:39 +08:00
}
2013-11-12 10:09:47 +08:00
else if ( ZipUtils : : isGZipBuffer ( data , dataLen ) )
2013-08-12 11:54:54 +08:00
{
2020-09-01 14:57:18 +08:00
unpackedLen = ZipUtils : : inflateMemory ( const_cast < uint8_t * > ( data ) , dataLen , & unpackedData ) ;
2013-08-16 16:10:39 +08:00
}
else
2013-08-12 11:54:54 +08:00
{
2020-09-01 14:57:18 +08:00
unpackedData = const_cast < uint8_t * > ( data ) ;
2013-08-12 11:54:54 +08:00
unpackedLen = dataLen ;
}
2012-04-19 14:35:52 +08:00
2020-08-31 19:24:23 +08:00
if ( unpackedData ! = data )
{ // free old data and own the unpackedData
if ( ownData ) free ( ( void * ) data ) ;
ownData = true ;
}
2013-08-12 11:54:54 +08:00
_fileType = detectFormat ( unpackedData , unpackedLen ) ;
2013-07-17 17:12:04 +08:00
2013-07-25 21:35:00 +08:00
switch ( _fileType )
2012-04-19 14:35:52 +08:00
{
2013-07-27 22:06:30 +08:00
case Format : : PNG :
2013-08-12 17:05:19 +08:00
ret = initWithPngData ( unpackedData , unpackedLen ) ;
2013-08-12 11:54:54 +08:00
break ;
2013-07-27 22:06:30 +08:00
case Format : : JPG :
2013-08-12 17:05:19 +08:00
ret = initWithJpgData ( unpackedData , unpackedLen ) ;
2013-08-12 11:54:54 +08:00
break ;
2013-07-27 22:06:30 +08:00
case Format : : WEBP :
2013-08-12 17:05:19 +08:00
ret = initWithWebpData ( unpackedData , unpackedLen ) ;
2013-08-12 11:54:54 +08:00
break ;
2013-07-27 22:06:30 +08:00
case Format : : PVR :
2020-08-31 23:09:42 +08:00
ret = initWithPVRData ( unpackedData , unpackedLen , ownData ) ;
2013-08-12 11:54:54 +08:00
break ;
2020-09-01 11:43:03 +08:00
case Format : : ETC1 :
2020-08-31 23:09:42 +08:00
ret = initWithETCData ( unpackedData , unpackedLen , ownData ) ;
2013-08-12 11:54:54 +08:00
break ;
2020-08-30 17:58:39 +08:00
case Format : : ETC2 :
2020-08-31 23:09:42 +08:00
ret = initWithETC2Data ( unpackedData , unpackedLen , ownData ) ;
2020-08-30 17:58:39 +08:00
break ;
2013-08-06 11:19:45 +08:00
case Format : : S3TC :
2020-08-31 23:09:42 +08:00
ret = initWithS3TCData ( unpackedData , unpackedLen , ownData ) ;
2013-08-12 11:54:54 +08:00
break ;
2013-08-16 11:02:44 +08:00
case Format : : ATITC :
2020-08-31 23:09:42 +08:00
ret = initWithATITCData ( unpackedData , unpackedLen , ownData ) ;
2013-08-16 14:27:13 +08:00
break ;
2020-02-05 22:45:19 +08:00
case Format : : ASTC :
2020-08-31 23:09:42 +08:00
ret = initWithASTCData ( unpackedData , unpackedLen , ownData ) ;
2020-02-05 22:45:19 +08:00
break ;
2019-11-25 02:28:38 +08:00
case Format : : BMP :
ret = initWithBmpData ( unpackedData , unpackedLen ) ;
break ;
2013-07-25 21:35:00 +08:00
default :
2013-11-29 14:31:42 +08:00
{
// load and detect image format
tImageTGA * tgaData = tgaLoadBuffer ( unpackedData , unpackedLen ) ;
if ( tgaData ! = nullptr & & tgaData - > status = = TGA_OK )
{
ret = initWithTGAData ( tgaData ) ;
}
else
{
2015-08-30 15:40:15 +08:00
CCLOG ( " cocos2d: unsupported image format! " ) ;
2013-11-29 14:31:42 +08:00
}
free ( tgaData ) ;
break ;
}
2013-08-12 11:54:54 +08:00
}
2020-08-31 19:24:23 +08:00
if ( _data ! = unpackedData & & ownData ) free ( unpackedData ) ;
// else, the hardware texture decoder used, the compressed data was stored directly
2012-04-19 14:35:52 +08:00
} while ( 0 ) ;
2013-07-25 21:35:00 +08:00
2013-08-12 17:05:19 +08:00
return ret ;
2012-04-19 14:35:52 +08:00
}
2020-09-01 14:57:18 +08:00
bool Image : : initWithRawData ( const uint8_t * data , ssize_t /*dataLen*/ , int width , int height , int /*bitsPerComponent*/ , bool preMulti )
2020-09-01 11:43:03 +08:00
{
bool ret = false ;
do
{
CC_BREAK_IF ( 0 = = width | | 0 = = height ) ;
_height = height ;
_width = width ;
_hasPremultipliedAlpha = preMulti ;
2020-09-25 11:07:56 +08:00
_pixelFormat = backend : : PixelFormat : : RGBA8 ;
2020-09-01 11:43:03 +08:00
// only RGBA8888 supported
int bytesPerComponent = 4 ;
_dataLen = height * width * bytesPerComponent ;
2020-09-01 14:57:18 +08:00
_data = static_cast < uint8_t * > ( malloc ( _dataLen ) ) ;
2020-09-01 11:43:03 +08:00
CC_BREAK_IF ( ! _data ) ;
memcpy ( _data , data , _dataLen ) ;
ret = true ;
} while ( 0 ) ;
return ret ;
}
2020-09-01 14:57:18 +08:00
bool Image : : isPng ( const uint8_t * data , ssize_t dataLen )
2013-07-17 17:12:04 +08:00
{
2013-07-25 21:35:00 +08:00
if ( dataLen < = 8 )
2013-07-19 15:37:54 +08:00
{
return false ;
}
2013-07-17 17:12:04 +08:00
2020-09-01 14:57:18 +08:00
static const uint8_t PNG_SIGNATURE [ ] = { 0x89 , 0x50 , 0x4e , 0x47 , 0x0d , 0x0a , 0x1a , 0x0a } ;
2013-07-17 17:12:04 +08:00
2013-07-25 21:35:00 +08:00
return memcmp ( PNG_SIGNATURE , data , sizeof ( PNG_SIGNATURE ) ) = = 0 ;
}
2020-09-01 14:57:18 +08:00
bool Image : : isBmp ( const uint8_t * data , ssize_t dataLen )
2019-11-25 02:28:38 +08:00
{
return dataLen > 54 & & data [ 0 ] = = ' B ' & & data [ 1 ] = = ' M ' ;
}
2013-07-25 21:35:00 +08:00
2020-09-01 14:57:18 +08:00
bool Image : : isEtc1 ( const uint8_t * data , ssize_t /*dataLen*/ )
2013-07-25 21:35:00 +08:00
{
2020-08-30 17:58:39 +08:00
return ! ! etc1_pkm_is_valid ( ( etc1_byte * ) data ) ;
2013-07-17 17:12:04 +08:00
}
2020-09-01 14:57:18 +08:00
bool Image : : isEtc2 ( const uint8_t * data , ssize_t dataLen )
2020-08-30 17:58:39 +08:00
{
return ! ! etc2_pkm_is_valid ( ( etc2_byte * ) data ) ;
}
2013-08-08 14:11:22 +08:00
2020-09-01 14:57:18 +08:00
bool Image : : isS3TC ( const uint8_t * data , ssize_t /*dataLen*/ )
2013-08-06 11:19:45 +08:00
{
2013-08-09 12:54:05 +08:00
S3TCTexHeader * header = ( S3TCTexHeader * ) data ;
2013-08-06 11:19:45 +08:00
2020-08-30 17:58:39 +08:00
return ( strncmp ( header - > fileCode , " DDS " , 3 ) = = 0 ) ;
2013-08-06 11:19:45 +08:00
}
2020-09-01 14:57:18 +08:00
bool Image : : isATITC ( const uint8_t * data , ssize_t /*dataLen*/ )
2013-08-16 11:02:44 +08:00
{
ATITCTexHeader * header = ( ATITCTexHeader * ) data ;
2020-08-30 17:58:39 +08:00
return ( strncmp ( & header - > identifier [ 1 ] , " KTX " , 3 ) = = 0 ) ;
2013-08-16 11:02:44 +08:00
}
2020-09-01 14:57:18 +08:00
bool Image : : isASTC ( const uint8_t * data , ssize_t /*dataLen*/ )
2020-02-05 22:45:19 +08:00
{
ASTCTexHeader * hdr = ( ASTCTexHeader * ) data ;
uint32_t magicval = hdr - > magic [ 0 ] + 256 * ( uint32_t ) ( hdr - > magic [ 1 ] ) + 65536 * ( uint32_t ) ( hdr - > magic [ 2 ] ) + 16777216 * ( uint32_t ) ( hdr - > magic [ 3 ] ) ;
2020-08-30 17:58:39 +08:00
return ( magicval = = ASTC_MAGIC_FILE_CONSTANT ) ;
2020-02-05 22:45:19 +08:00
}
2020-09-01 14:57:18 +08:00
bool Image : : isJpg ( const uint8_t * data , ssize_t dataLen )
2013-07-17 17:12:04 +08:00
{
2013-07-25 21:35:00 +08:00
if ( dataLen < = 4 )
2013-07-19 15:37:54 +08:00
{
return false ;
}
2013-07-17 17:12:04 +08:00
2020-09-01 14:57:18 +08:00
static const uint8_t JPG_SOI [ ] = { 0xFF , 0xD8 } ;
2013-07-17 17:12:04 +08:00
2013-07-25 21:35:00 +08:00
return memcmp ( data , JPG_SOI , 2 ) = = 0 ;
2013-07-17 17:12:04 +08:00
}
2020-09-01 14:57:18 +08:00
bool Image : : isWebp ( const uint8_t * data , ssize_t dataLen )
2013-07-17 17:12:04 +08:00
{
2013-07-25 21:35:00 +08:00
if ( dataLen < = 12 )
2013-07-19 15:37:54 +08:00
{
return false ;
}
2013-07-17 17:12:04 +08:00
2013-07-19 15:37:54 +08:00
static const char * WEBP_RIFF = " RIFF " ;
static const char * WEBP_WEBP = " WEBP " ;
2013-07-17 17:12:04 +08:00
2013-07-25 21:35:00 +08:00
return memcmp ( data , WEBP_RIFF , 4 ) = = 0
2020-09-01 14:57:18 +08:00
& & memcmp ( static_cast < const uint8_t * > ( data ) + 8 , WEBP_WEBP , 4 ) = = 0 ;
2013-07-25 21:35:00 +08:00
}
2020-09-01 14:57:18 +08:00
bool Image : : isPvr ( const uint8_t * data , ssize_t dataLen )
2013-07-25 21:35:00 +08:00
{
2013-11-13 11:22:34 +08:00
if ( static_cast < size_t > ( dataLen ) < sizeof ( PVRv2TexHeader ) | | static_cast < size_t > ( dataLen ) < sizeof ( PVRv3TexHeader ) )
2013-07-25 21:35:00 +08:00
{
return false ;
}
2013-08-12 17:05:19 +08:00
const PVRv2TexHeader * headerv2 = static_cast < const PVRv2TexHeader * > ( static_cast < const void * > ( data ) ) ;
const PVRv3TexHeader * headerv3 = static_cast < const PVRv3TexHeader * > ( static_cast < const void * > ( data ) ) ;
2013-07-25 21:35:00 +08:00
return memcmp ( & headerv2 - > pvrTag , gPVRTexIdentifier , strlen ( gPVRTexIdentifier ) ) = = 0 | | CC_SWAP_INT32_BIG_TO_HOST ( headerv3 - > version ) = = 0x50565203 ;
2013-07-17 17:12:04 +08:00
}
2020-09-01 14:57:18 +08:00
Image : : Format Image : : detectFormat ( const uint8_t * data , ssize_t dataLen )
2013-07-17 17:12:04 +08:00
{
2013-07-25 21:35:00 +08:00
if ( isPng ( data , dataLen ) )
2013-07-19 15:37:54 +08:00
{
2013-07-27 22:06:30 +08:00
return Format : : PNG ;
2013-08-16 16:10:39 +08:00
}
else if ( isJpg ( data , dataLen ) )
2013-07-19 15:37:54 +08:00
{
2013-07-27 22:06:30 +08:00
return Format : : JPG ;
2013-08-16 16:10:39 +08:00
}
2019-11-25 02:28:38 +08:00
else if ( isBmp ( data , dataLen ) )
{
return Format : : BMP ;
}
2013-08-16 16:10:39 +08:00
else if ( isWebp ( data , dataLen ) )
2013-07-19 15:37:54 +08:00
{
2013-07-27 22:06:30 +08:00
return Format : : WEBP ;
2013-08-16 16:10:39 +08:00
}
else if ( isPvr ( data , dataLen ) )
2013-07-19 15:37:54 +08:00
{
2013-07-27 22:06:30 +08:00
return Format : : PVR ;
2013-08-16 16:10:39 +08:00
}
2020-09-01 11:43:03 +08:00
else if ( isEtc1 ( data , dataLen ) )
2013-07-25 21:35:00 +08:00
{
2020-09-01 11:43:03 +08:00
return Format : : ETC1 ;
2013-08-16 16:10:39 +08:00
}
2020-08-30 17:58:39 +08:00
else if ( isEtc2 ( data , dataLen ) )
{
return Format : : ETC2 ;
}
2013-08-16 16:10:39 +08:00
else if ( isS3TC ( data , dataLen ) )
2013-08-06 11:19:45 +08:00
{
return Format : : S3TC ;
2013-08-16 16:10:39 +08:00
}
else if ( isATITC ( data , dataLen ) )
2013-08-16 11:02:44 +08:00
{
return Format : : ATITC ;
2013-07-25 21:35:00 +08:00
}
2020-02-05 22:45:19 +08:00
else if ( isASTC ( data , dataLen ) )
{
return Format : : ASTC ;
}
2013-07-25 21:35:00 +08:00
else
{
2015-02-24 01:36:20 +08:00
return Format : : UNKNOWN ;
2013-07-19 15:37:54 +08:00
}
2013-07-17 17:12:04 +08:00
}
2013-07-25 21:35:00 +08:00
int Image : : getBitPerPixel ( )
{
2020-11-02 16:47:07 +08:00
return backend : : PixelFormatUtils : : getFormatDescriptor ( _pixelFormat ) . bpp ;
2013-07-25 21:35:00 +08:00
}
bool Image : : hasAlpha ( )
{
2020-09-25 11:07:56 +08:00
return backend : : PixelFormatUtils : : getFormatDescriptor ( _pixelFormat ) . alpha ;
2013-07-25 21:35:00 +08:00
}
bool Image : : isCompressed ( )
{
2020-09-25 11:07:56 +08:00
return backend : : PixelFormatUtils : : isCompressed ( _pixelFormat ) ;
2013-07-25 21:35:00 +08:00
}
2013-08-06 16:14:36 +08:00
2014-08-21 15:19:26 +08:00
namespace
2013-08-06 16:14:36 +08:00
{
2014-08-21 15:19:26 +08:00
/*
* ERROR HANDLING :
*
* The JPEG library ' s standard error handler ( jerror . c ) is divided into
* several " methods " which you can override individually . This lets you
* adjust the behavior without duplicating a lot of code , which you might
* have to update with each future release .
*
* We override the " error_exit " method so that control is returned to the
* library ' s caller when a fatal error occurs , rather than calling exit ( )
* as the standard error_exit method does .
*
* We use C ' s setjmp / longjmp facility to return control . This means that the
* routine which calls the JPEG library must first execute a setjmp ( ) call to
* establish the return point . We want the replacement error_exit to do a
* longjmp ( ) . But we need to make the setjmp buffer accessible to the
* error_exit routine . To do this , we make a private extension of the
* standard JPEG error handler object . ( If we were using C + + , we ' d say we
* were making a subclass of the regular error handler . )
*
* Here ' s the extended error handler struct :
*/
# if CC_USE_JPEG
struct MyErrorMgr
{
2015-08-14 21:17:51 +08:00
struct jpeg_error_mgr pub ; /* "public" fields */
jmp_buf setjmp_buffer ; /* for return to caller */
2014-08-21 15:19:26 +08:00
} ;
typedef struct MyErrorMgr * MyErrorPtr ;
/*
* Here ' s the routine that will replace the standard error_exit method :
*/
METHODDEF ( void )
myErrorExit ( j_common_ptr cinfo )
{
/* cinfo->err really points to a MyErrorMgr struct, so coerce pointer */
MyErrorPtr myerr = ( MyErrorPtr ) cinfo - > err ;
/* Always display the message. */
/* We could postpone this until after returning, if we chose. */
2015-09-22 16:08:23 +08:00
/* internal message function can't show error message in some platforms, so we rewrite it here.
* edit it if has version conflict .
2014-08-21 15:19:26 +08:00
*/
//(*cinfo->err->output_message) (cinfo);
char buffer [ JMSG_LENGTH_MAX ] ;
( * cinfo - > err - > format_message ) ( cinfo , buffer ) ;
CCLOG ( " jpeg error: %s " , buffer ) ;
2013-08-06 16:14:36 +08:00
2014-08-21 15:19:26 +08:00
/* Return control to the setjmp point */
longjmp ( myerr - > setjmp_buffer , 1 ) ;
2013-08-06 16:14:36 +08:00
}
2014-08-21 15:19:26 +08:00
# endif // CC_USE_JPEG
}
2020-09-01 14:57:18 +08:00
bool Image : : initWithJpgData ( uint8_t * data , ssize_t dataLen )
2014-08-21 15:19:26 +08:00
{
metal support for cocos2d-x (#19305)
* remove deprecated files
* remove some deprecated codes
* remove more deprecated codes
* remove ui deprecated codes
* remove more deprecated codes
* remove deprecated codes in ccmenuitem
* remove more deprecated codes in ui
* remove more deprecated codes in ui
* remove more deprecated codes in ui
* remove more deprecated codes
* remove more deprecated codes
* remove more deprecated codes
* remove vr related codes and ignore some modules
* remove allocator
* remove some config
* 【Feature】add back-end project file
* [Feature] add back-end file
* add pipeline descriptor and shader cache
* [Feature] support sprite for backend
* [Feature] remove unneeded code
* [Feature] according to es2.0 spec, you must use clamp-to-edge as texture wrap mode, and no mipmapping for non-power-of-two texture
* [Feature] set texture wrap mode to clamp-to-edge, and no mipmapping for non-power-of-two texture
* [Feature] remove macro define to .cpp file
* [Feature] add log info
* [Feature] add PipelineDescriptor for TriangleCommand
* [Feature] add PipelineDescriptor object as member of TriangleCommand
* [Feature] add getPipelineDescriptor method
* add renderbackend
* complete pipeline descriptor
* [Feature] add viewport in RenderCommand
* set viewport when rendrering
* [Feature] occur error when using RendererBackend, to be fixed.
* a workaround to fix black screen on macOS 10.14 (#19090)
* add rendererbackend init function
* fix typo
* [Feature] modify testFile
* [BugFix] modify shader path
* [Feature] set default viewport
* fix projection
* [Feature] modify log info
* [BugFix] change viewport data type to int
* [BugFix] add BindGroup to PipelienDescriptor
* [BugFix] change a_position to vec3 in sprite.vert
* [BugFix] set vertexLayout according to V3F_C4B_T2F structure
* [Feature] revert a_position to vec4
* [Feature] renderer should not use gl codes directly
* [Feature] it's better not use default value parameter
* fix depth test setting
* rendererbackend -> renderer
* clear color and depth at begin
* add metal backend
* metal support normalized attribute
* simplify codes
* update external
* add render pass desctriptor in pipeline descriptor
* fix warnings
* fix crash and memeory leak
* refactor Texture2D
* put pipeline descriptor into render command
* simplify codes
* [Feature] update Sprite
* fix crash when closing app
* [Feature] update SpriteBatchNode and TextureAtlas
* support render texture(not finish)
* [Feature] remove unused code
* make tests work on mac
* fix download-deps path error
* make tests work on iOS
* [Feature] support ttf under normal label effect
* refactor triangle command processing
* let renderer handle more common commands
* refactor backend
* make render texture work
* [Feature] refactor backend for GL
* [Feature]Renaming to make it easy to understand
* [Feature] change warp mode to CLAMP_TO_EDGE
* fix ghost
* simplify visit render queue logic
* support progress timer without rial mode
* support partcile system
* Feature/update label (#149)
* [BugFix] fix compile error
* [Feature] support outline effect in ios
* [Feature] add shader file
* [BugFix] fix begin and end RenderPass
* [Feature] update CustomCommand
* [Feature] revert project.pbxproj
* [Feature] simplify codes
* [BugFix] pack AI88 to RGBA8888 only when outline enable
* [Feature] support shadow effect in Label
* [Feature] support BMFont
* [Feature] support glow effect
* [Feature] simplify shader files
* LabelAtlas work
* handle blend function correctly
* support tile map
* don't share buffer in metal
* alloc buffer size as needed
* support more tilemap
* Merge branch 'minggo/metal-support' into feature/updateLabel
* minggo/metal-support:
support tile map
handle blend function correctly
LabelAtlas work
Feature/update label (#149)
support partcile system
# Conflicts:
# cocos/2d/CCLabel.cpp
# cocos/2d/CCSprite.cpp
# cocos/2d/CCSpriteBatchNode.cpp
# cocos/renderer/CCQuadCommand.cpp
# cocos/renderer/CCQuadCommand.h
* render texture work without saving file
* use global viewport
* grid3d works
* remove grabber
* tiled3d works
* [BugFix] fix label bug
* [Feature] add updateSubData for buffer
* [Feature] remove setVertexCount
* support depth test
* add callback command
* [Feature] add UITest
* [Feature] update UITest
* [Feature] remove unneeded codes
* fix custom command issue
* fix layer color blend issue
* [BugFix] fix iOS compile error
* [Feature] remove unneeded codes
* [Feature] fix updateVertexBuffer
* layerradial works
* add draw test back
* fix batch issue
* fix compiling error
* [BugFix] support ETC1
* [BugFix] get the correct pipelineDescriptor
* [BugFix] skip draw when backendTexture nullptr
* clipping node support
* [Feature] add shader files
* fix stencil issue in metal
* [Feature] update UILayoutTest
* [BugFix] skip drawing when vertexCount is zero
* refactor renderer
* add set global z order for stencil manager commands
* fix warnings caused by type
* remove viewport in render command
* [Feature] fix warnings caused by type
* [BugFix] clear vertexCount and indexCount for CustomComand when needed
* [Feature] update clear for CustomCommand
* ios use metal
* fix viewport issue
* fix LayerColorGradient crash
* [cmake] transport to android and windows (#160)
* save point 1
* compile on windows
* run on android
* revert useless change
* android set CC_ENABLE_CACHE_TEXTURE_DATA to 1
* add initGlew
* fix android crash
* add TODO new-renderer
* review update
* revert onGLFWWindowPosCallback
* fix android compiling error
* Impl progress radial (#162)
* progresstimer add radial impl
* default drawType to element
* dec invoke times of createVertexBuffer (#163)
* support depth/stencil format for gl backend
* simplify progress timer codes
* support motionstreak, effect is wrong
* fix motionstreak issue
* [Feature] update Scissor Test (#161)
* [Feature] update Scissor Test
* [Feature] update ScissorTest
* [Feature] rename function
* [Feature] get constant reference if needed
* [Feature] show render status (#164)
* improve performance
* fix depth state
* fill error that triangle vertex/index number bigger than buffer
* fix compiline error in release mode
* fix buffer conflict between CPU and GPU on iOS/macOS
* Renderer refactor (#165)
* use one vertes/index buffer with opengl
* fix error on windows
* custom command support index format config
* CCLayer: compact vertex data structure
* update comment
* fix doc
* support fast tilemap
* pass index format instead
* fix some wrong effect
* fix render texture error
* fix texture per-element size
* fix texture format error
* BlendFunc type refactor, GLenum -> backend::BlendFactor (#167)
* BlendFunc use backend::BlendFactor as inner field
* update comments
* use int to replace GLenum
* update xcode project fiel
* rename to GLBlendConst
* add ccConstants.h
* update xcode project file
* update copyright
* remove primitive command
* remove CCPrimitive.cpp/.h
* remove deprecated files
* remove unneeded files
* remove multiple view support
* remove multiple view support
* remove the usage of frame buffer in camera
* director don't use frame buffer
* remove FrameBuffer
* remove BatchCommand
* add some api reference
* add physics2d back
* fix crash when close app on mac
* improve render texture
* fix rendertexture issue
* fix rendertexture issue
* simplify codes
* CMake support for mac & ios (#169)
* update cmake
* fix compile error
* update 3rd libs version
* remove CCThread.h/.cpp
* remove ccthread
* use audio engine to implement simple audio engine
* remove unneeded codes
* remove deprecated codes
* remove winrt macro
* remove CC_USE_WIC
* set partcile blend function in more elegant way
* remove unneeded codes
* remove unneeded codes
* cmake works on windows
* update project setting
* improve performance
* GLFloat -> float
* sync v3 cmake improvements into metal-support (#172)
* pick: modern cmake, compile definitions improvement (#19139)
* modern cmake, use target_compile_definitions partly
* simplify macro define, remove USE_*
* modern cmake, macro define
* add physics 2d macro define into ccConfig.h
* remove USE_CHIPMUNK macro in build.gradle
* remove CocosSelectModule.cmake
* shrink useless define
* simplify compile options config, re-add if necessary
* update external for tmp CI test
* un-quote target_compile_options value
* add "-g" parameter only when debug mode
* keep single build type when generator Xcode & VS projecy
* update external for tmp CI tes
* add static_cast<char>(-1), fix -Wc++11-narrowing
* simplify win32 compile define
* not modify code, only improve compile options
# Conflicts:
# .gitignore
# cmake/Modules/CocosConfigDepend.cmake
# cocos/CMakeLists.txt
# external/config.json
# tests/cpp-tests/CMakeLists.txt
* modern cmake, improve cmake_compiler_flags (#19145)
* cmake_compiler_flags
* Fix typo
* Fix typo2
* Remove chanages from Android.mk
* correct lua template cmake build (#19149)
* don't add -Wno-deprecated into jsb target
* correct lua template cmake build
* fix win32 lua template compile error
* prevent cmake in-source-build friendly (#19151)
* pick: Copy resources to "Resources/" on win32 like in linux configuration
* add "/Z7" for cpp-tests on windows
* [cmake] fix iOS xcode property setting failed (#19208)
* fix iOS xcode property setting failed
* use search_depend_libs_recursive at dlls collect
* fix typo
* [cmake] add find_host_library into iOS toolchain file (#19230)
* pick: [lua android] use luajit & template cmake update (#19239)
* increase cmake stability , remove tests/CMakeLists.txt (#19261)
* cmake win32 Precompiled header (#19273)
* Precompiled header
* Fix
* Precompiled header for cocos
* Precompiled header jscocos2d
* Fix for COCOS2D_DEBUG is always 1 on Android (#19291)
Related #19289
* little build fix, tests cpp-tests works on mac
* sync v3 build related codes into metal-support (#173)
* strict initialization for std::array
* remove proj.win32 project configs
* modern cmake, cmake_cleanup_remove_unused_variables (#19146)
* Switch travis CI to xenial (#19207)
* Switch travis CI to xenial
* Remove language: android
* Set language: cpp
* Fix java problem
* Update sdkmanager
* Fix sdkmanger
* next sdkmanager fix
* Remove xenial from android
* revert to sdk-tools-{system}-3859397
* Remove linux cmake install
* Update before-install.sh
* Update .travis.yml
* Simplify install-deps-linux.sh, tested on Ubuntu 16.04 (#19212)
* Simplify install-deps-linux.sh
* Cleanup
* pick: install ninja
* update cocos2d-console submodule
* for metal-support alpha release, we only test cpp
* add HelloCpp into project(Cocos2d-x) for tmp test
* update extenal metal-support-4
* update uniform setting
* [Feature] update BindGroup
* [Feature] empty-test
* [Feature] cpp-test
* [Feature] fix GL compiler error
* [Feature] fix GL crash
* [Feature] empty-test
* [Feature] cpp-tests
* [feature] improve frameRate
* [feature] fix opengl compile error
* [feature] fix opengl compile error
* [BugFix] fix compute maxLocation error
* [Feature] update setting unifrom
* [Feature] fix namespace
* [Feature] remove unneeded code
* [Bugfix] fix project file
* [Feature] update review
* [texture2d] impl texture format support (#175)
* texture update
* update
* update texture
* commit
* compile on windows
* ddd
* rename
* rename methods
* no crash
* save gl
* save
* save
* rename
* move out pixel format convert functions
* metal crash
* update
* update android
* support gles compressed texture format
* support more compress format
* add more conversion methods
* ss
* save
* update conversion methods
* add PVRTC format support
* reformat
* add marco linux
* fix GL marcro
* pvrtc supported only by ios 8.0+
* remove unused cmake
* revert change
* refactor Texture2D::initWithData
* fix conversion log
* refactor Texture2D::initWithData
* remove some OpenGL constants for PVRTC
* add todo
* fix typo
* AutoTest works on mac/iOS by disable part cases, sync v3 bug fix (#174)
* review cpp-tests, and fix part issues on start auto test
* sync png format fix: Node:Particle3D abnormal texture effects #19204
* fix cpp-tests SpritePolygon crash, wrong png format (#19170)
* fix wrong png convert format from sRGB to Gray
* erase plist index if all frames was erased
* test_A8.png have I8 format, fix it
* [CCSpriteCache] allow re-add plist & add testcase (#19175)
* allow re-add plist & add testcase
* remove comments/rename method/update testcase
* fix isSpriteFramesWithFileLoaded & add testcase
* remove used variable
* remove unused variable
* fix double free issues when js/lua-tests exit on iOS (#19236)
* disable part cases, AutoTest works without crash on mac
* update cocos2dx files json, to test cocos new next
* fix spritecache plist parsing issue (#19269)
* [linux] Fix FileUtils::getContents with folder (#19157)
* fix FileUtils::getContents on linux/mac
* use stat.st_mode
* simplify
* [CCFileUtils] win32 getFileSize (#19176)
* win32 getFileSize
* fix stat
* [cpp test-Android]20:FileUtils/2 change title (#19197)
* sync #19200
* sync #19231
* [android lua] improve performance of lua loader (#19234)
* [lua] improve performance of lua loader
* remove cache fix
* Revert "fix spritecache plist parsing issue (#19269)"
This reverts commit f3a85ece4307a7b90816c34489d1ed2c8fd11baf.
* remove win32 project files ref in template.json
* add metal framework lnk ref into cpp template
* test on iOS, and disable part cases
* alBufferData instead of alBufferDataStatic for small audio file on Apple (#19227)
* changes AudioCache to use alBufferData instead of alBufferDataStatic
(also makes test 19 faster to trigger openal bugs faster)
The original problem: CrashIfClientProvidedBogusAudioBufferList
https://github.com/cocos2d/cocos2d-x/issues/18948
is not happening anymore, but there's still a not very frequent issue
that makes OpenAL crash with a call stack like this.
AudioCache::readDataTask > alBufferData > CleanUpDeadBufferList
It happes more frequently when the device is "cold", which means after
half an hour of not using the device (locked).
I could not find the actual source code for iOS OpenAL, so I used the
macOS versions:
https://opensource.apple.com/source/OpenAL/OpenAL-48.7/Source/OpenAL/oalImp.cpp.auto.html
They seem to use CAGuard.h to make sure the dead buffer list
has no threading issues. I'm worried because the CAGuard code I found
has macos and win32 define but no iOS, so I'm not sure. I guess the
iOS version is different and has the guard.
I could not find a place in the code that's unprotected by the locks
except the InitializeBufferMap() which should not be called more than
once from cocos, and there's a workaround in AudioEngine-impl for it.
I reduced the occurence of the CleanUpDeadBufferList crash by moving
the guard in ~AudioCache to cover the alDeleteBuffers call.
* remove hack method "setTimeout" on audio
* AutoTest works on iOS
* support set ios deployment target for root project
* enable all texture2d cases, since Jiang have fixed
* add CCTextureUtils to xcode project file (#176)
* add leak cases for SpriteFrameCache (#177)
* re-add SpriteFrameCache cases
* update template file json
* Update SpriteFrameCacheTest.cpp
* fix compiling error
2019-01-18 15:08:25 +08:00
# if CC_USE_JPEG
2014-08-21 15:19:26 +08:00
/* these are standard libjpeg structures for reading(decompression) */
struct jpeg_decompress_struct cinfo ;
/* We use our private extension JPEG error handler.
2015-08-14 21:17:51 +08:00
* Note that this struct must live as long as the main JPEG parameter
* struct , to avoid dangling - pointer problems .
*/
struct MyErrorMgr jerr ;
2014-08-21 15:19:26 +08:00
/* libjpeg data structure for storing one row, that is, scanline of an image */
JSAMPROW row_pointer [ 1 ] = { 0 } ;
unsigned long location = 0 ;
2014-08-21 15:41:05 +08:00
bool ret = false ;
2014-08-21 15:19:26 +08:00
do
2013-11-29 15:13:16 +08:00
{
2014-08-21 15:19:26 +08:00
/* We set up the normal JPEG error routines, then override error_exit. */
2015-08-14 21:17:51 +08:00
cinfo . err = jpeg_std_error ( & jerr . pub ) ;
jerr . pub . error_exit = myErrorExit ;
/* Establish the setjmp return context for MyErrorExit to use. */
if ( setjmp ( jerr . setjmp_buffer ) )
2014-08-21 15:41:05 +08:00
{
2015-08-14 21:17:51 +08:00
/* If we get here, the JPEG code has signaled an error.
* We need to clean up the JPEG object , close the input file , and return .
*/
jpeg_destroy_decompress ( & cinfo ) ;
break ;
}
2014-08-21 15:19:26 +08:00
/* setup decompression process and source, then read JPEG header */
jpeg_create_decompress ( & cinfo ) ;
# ifndef CC_TARGET_QT5
2020-09-01 14:57:18 +08:00
jpeg_mem_src ( & cinfo , const_cast < uint8_t * > ( data ) , dataLen ) ;
2014-08-21 15:19:26 +08:00
# endif /* CC_TARGET_QT5 */
/* reading the image header which contains image information */
# if (JPEG_LIB_VERSION >= 90)
// libjpeg 0.9 adds stricter types.
2014-08-21 15:41:05 +08:00
jpeg_read_header ( & cinfo , TRUE ) ;
2014-08-21 15:19:26 +08:00
# else
2014-08-21 15:41:05 +08:00
jpeg_read_header ( & cinfo , TRUE ) ;
metal support for cocos2d-x (#19305)
* remove deprecated files
* remove some deprecated codes
* remove more deprecated codes
* remove ui deprecated codes
* remove more deprecated codes
* remove deprecated codes in ccmenuitem
* remove more deprecated codes in ui
* remove more deprecated codes in ui
* remove more deprecated codes in ui
* remove more deprecated codes
* remove more deprecated codes
* remove more deprecated codes
* remove vr related codes and ignore some modules
* remove allocator
* remove some config
* 【Feature】add back-end project file
* [Feature] add back-end file
* add pipeline descriptor and shader cache
* [Feature] support sprite for backend
* [Feature] remove unneeded code
* [Feature] according to es2.0 spec, you must use clamp-to-edge as texture wrap mode, and no mipmapping for non-power-of-two texture
* [Feature] set texture wrap mode to clamp-to-edge, and no mipmapping for non-power-of-two texture
* [Feature] remove macro define to .cpp file
* [Feature] add log info
* [Feature] add PipelineDescriptor for TriangleCommand
* [Feature] add PipelineDescriptor object as member of TriangleCommand
* [Feature] add getPipelineDescriptor method
* add renderbackend
* complete pipeline descriptor
* [Feature] add viewport in RenderCommand
* set viewport when rendrering
* [Feature] occur error when using RendererBackend, to be fixed.
* a workaround to fix black screen on macOS 10.14 (#19090)
* add rendererbackend init function
* fix typo
* [Feature] modify testFile
* [BugFix] modify shader path
* [Feature] set default viewport
* fix projection
* [Feature] modify log info
* [BugFix] change viewport data type to int
* [BugFix] add BindGroup to PipelienDescriptor
* [BugFix] change a_position to vec3 in sprite.vert
* [BugFix] set vertexLayout according to V3F_C4B_T2F structure
* [Feature] revert a_position to vec4
* [Feature] renderer should not use gl codes directly
* [Feature] it's better not use default value parameter
* fix depth test setting
* rendererbackend -> renderer
* clear color and depth at begin
* add metal backend
* metal support normalized attribute
* simplify codes
* update external
* add render pass desctriptor in pipeline descriptor
* fix warnings
* fix crash and memeory leak
* refactor Texture2D
* put pipeline descriptor into render command
* simplify codes
* [Feature] update Sprite
* fix crash when closing app
* [Feature] update SpriteBatchNode and TextureAtlas
* support render texture(not finish)
* [Feature] remove unused code
* make tests work on mac
* fix download-deps path error
* make tests work on iOS
* [Feature] support ttf under normal label effect
* refactor triangle command processing
* let renderer handle more common commands
* refactor backend
* make render texture work
* [Feature] refactor backend for GL
* [Feature]Renaming to make it easy to understand
* [Feature] change warp mode to CLAMP_TO_EDGE
* fix ghost
* simplify visit render queue logic
* support progress timer without rial mode
* support partcile system
* Feature/update label (#149)
* [BugFix] fix compile error
* [Feature] support outline effect in ios
* [Feature] add shader file
* [BugFix] fix begin and end RenderPass
* [Feature] update CustomCommand
* [Feature] revert project.pbxproj
* [Feature] simplify codes
* [BugFix] pack AI88 to RGBA8888 only when outline enable
* [Feature] support shadow effect in Label
* [Feature] support BMFont
* [Feature] support glow effect
* [Feature] simplify shader files
* LabelAtlas work
* handle blend function correctly
* support tile map
* don't share buffer in metal
* alloc buffer size as needed
* support more tilemap
* Merge branch 'minggo/metal-support' into feature/updateLabel
* minggo/metal-support:
support tile map
handle blend function correctly
LabelAtlas work
Feature/update label (#149)
support partcile system
# Conflicts:
# cocos/2d/CCLabel.cpp
# cocos/2d/CCSprite.cpp
# cocos/2d/CCSpriteBatchNode.cpp
# cocos/renderer/CCQuadCommand.cpp
# cocos/renderer/CCQuadCommand.h
* render texture work without saving file
* use global viewport
* grid3d works
* remove grabber
* tiled3d works
* [BugFix] fix label bug
* [Feature] add updateSubData for buffer
* [Feature] remove setVertexCount
* support depth test
* add callback command
* [Feature] add UITest
* [Feature] update UITest
* [Feature] remove unneeded codes
* fix custom command issue
* fix layer color blend issue
* [BugFix] fix iOS compile error
* [Feature] remove unneeded codes
* [Feature] fix updateVertexBuffer
* layerradial works
* add draw test back
* fix batch issue
* fix compiling error
* [BugFix] support ETC1
* [BugFix] get the correct pipelineDescriptor
* [BugFix] skip draw when backendTexture nullptr
* clipping node support
* [Feature] add shader files
* fix stencil issue in metal
* [Feature] update UILayoutTest
* [BugFix] skip drawing when vertexCount is zero
* refactor renderer
* add set global z order for stencil manager commands
* fix warnings caused by type
* remove viewport in render command
* [Feature] fix warnings caused by type
* [BugFix] clear vertexCount and indexCount for CustomComand when needed
* [Feature] update clear for CustomCommand
* ios use metal
* fix viewport issue
* fix LayerColorGradient crash
* [cmake] transport to android and windows (#160)
* save point 1
* compile on windows
* run on android
* revert useless change
* android set CC_ENABLE_CACHE_TEXTURE_DATA to 1
* add initGlew
* fix android crash
* add TODO new-renderer
* review update
* revert onGLFWWindowPosCallback
* fix android compiling error
* Impl progress radial (#162)
* progresstimer add radial impl
* default drawType to element
* dec invoke times of createVertexBuffer (#163)
* support depth/stencil format for gl backend
* simplify progress timer codes
* support motionstreak, effect is wrong
* fix motionstreak issue
* [Feature] update Scissor Test (#161)
* [Feature] update Scissor Test
* [Feature] update ScissorTest
* [Feature] rename function
* [Feature] get constant reference if needed
* [Feature] show render status (#164)
* improve performance
* fix depth state
* fill error that triangle vertex/index number bigger than buffer
* fix compiline error in release mode
* fix buffer conflict between CPU and GPU on iOS/macOS
* Renderer refactor (#165)
* use one vertes/index buffer with opengl
* fix error on windows
* custom command support index format config
* CCLayer: compact vertex data structure
* update comment
* fix doc
* support fast tilemap
* pass index format instead
* fix some wrong effect
* fix render texture error
* fix texture per-element size
* fix texture format error
* BlendFunc type refactor, GLenum -> backend::BlendFactor (#167)
* BlendFunc use backend::BlendFactor as inner field
* update comments
* use int to replace GLenum
* update xcode project fiel
* rename to GLBlendConst
* add ccConstants.h
* update xcode project file
* update copyright
* remove primitive command
* remove CCPrimitive.cpp/.h
* remove deprecated files
* remove unneeded files
* remove multiple view support
* remove multiple view support
* remove the usage of frame buffer in camera
* director don't use frame buffer
* remove FrameBuffer
* remove BatchCommand
* add some api reference
* add physics2d back
* fix crash when close app on mac
* improve render texture
* fix rendertexture issue
* fix rendertexture issue
* simplify codes
* CMake support for mac & ios (#169)
* update cmake
* fix compile error
* update 3rd libs version
* remove CCThread.h/.cpp
* remove ccthread
* use audio engine to implement simple audio engine
* remove unneeded codes
* remove deprecated codes
* remove winrt macro
* remove CC_USE_WIC
* set partcile blend function in more elegant way
* remove unneeded codes
* remove unneeded codes
* cmake works on windows
* update project setting
* improve performance
* GLFloat -> float
* sync v3 cmake improvements into metal-support (#172)
* pick: modern cmake, compile definitions improvement (#19139)
* modern cmake, use target_compile_definitions partly
* simplify macro define, remove USE_*
* modern cmake, macro define
* add physics 2d macro define into ccConfig.h
* remove USE_CHIPMUNK macro in build.gradle
* remove CocosSelectModule.cmake
* shrink useless define
* simplify compile options config, re-add if necessary
* update external for tmp CI test
* un-quote target_compile_options value
* add "-g" parameter only when debug mode
* keep single build type when generator Xcode & VS projecy
* update external for tmp CI tes
* add static_cast<char>(-1), fix -Wc++11-narrowing
* simplify win32 compile define
* not modify code, only improve compile options
# Conflicts:
# .gitignore
# cmake/Modules/CocosConfigDepend.cmake
# cocos/CMakeLists.txt
# external/config.json
# tests/cpp-tests/CMakeLists.txt
* modern cmake, improve cmake_compiler_flags (#19145)
* cmake_compiler_flags
* Fix typo
* Fix typo2
* Remove chanages from Android.mk
* correct lua template cmake build (#19149)
* don't add -Wno-deprecated into jsb target
* correct lua template cmake build
* fix win32 lua template compile error
* prevent cmake in-source-build friendly (#19151)
* pick: Copy resources to "Resources/" on win32 like in linux configuration
* add "/Z7" for cpp-tests on windows
* [cmake] fix iOS xcode property setting failed (#19208)
* fix iOS xcode property setting failed
* use search_depend_libs_recursive at dlls collect
* fix typo
* [cmake] add find_host_library into iOS toolchain file (#19230)
* pick: [lua android] use luajit & template cmake update (#19239)
* increase cmake stability , remove tests/CMakeLists.txt (#19261)
* cmake win32 Precompiled header (#19273)
* Precompiled header
* Fix
* Precompiled header for cocos
* Precompiled header jscocos2d
* Fix for COCOS2D_DEBUG is always 1 on Android (#19291)
Related #19289
* little build fix, tests cpp-tests works on mac
* sync v3 build related codes into metal-support (#173)
* strict initialization for std::array
* remove proj.win32 project configs
* modern cmake, cmake_cleanup_remove_unused_variables (#19146)
* Switch travis CI to xenial (#19207)
* Switch travis CI to xenial
* Remove language: android
* Set language: cpp
* Fix java problem
* Update sdkmanager
* Fix sdkmanger
* next sdkmanager fix
* Remove xenial from android
* revert to sdk-tools-{system}-3859397
* Remove linux cmake install
* Update before-install.sh
* Update .travis.yml
* Simplify install-deps-linux.sh, tested on Ubuntu 16.04 (#19212)
* Simplify install-deps-linux.sh
* Cleanup
* pick: install ninja
* update cocos2d-console submodule
* for metal-support alpha release, we only test cpp
* add HelloCpp into project(Cocos2d-x) for tmp test
* update extenal metal-support-4
* update uniform setting
* [Feature] update BindGroup
* [Feature] empty-test
* [Feature] cpp-test
* [Feature] fix GL compiler error
* [Feature] fix GL crash
* [Feature] empty-test
* [Feature] cpp-tests
* [feature] improve frameRate
* [feature] fix opengl compile error
* [feature] fix opengl compile error
* [BugFix] fix compute maxLocation error
* [Feature] update setting unifrom
* [Feature] fix namespace
* [Feature] remove unneeded code
* [Bugfix] fix project file
* [Feature] update review
* [texture2d] impl texture format support (#175)
* texture update
* update
* update texture
* commit
* compile on windows
* ddd
* rename
* rename methods
* no crash
* save gl
* save
* save
* rename
* move out pixel format convert functions
* metal crash
* update
* update android
* support gles compressed texture format
* support more compress format
* add more conversion methods
* ss
* save
* update conversion methods
* add PVRTC format support
* reformat
* add marco linux
* fix GL marcro
* pvrtc supported only by ios 8.0+
* remove unused cmake
* revert change
* refactor Texture2D::initWithData
* fix conversion log
* refactor Texture2D::initWithData
* remove some OpenGL constants for PVRTC
* add todo
* fix typo
* AutoTest works on mac/iOS by disable part cases, sync v3 bug fix (#174)
* review cpp-tests, and fix part issues on start auto test
* sync png format fix: Node:Particle3D abnormal texture effects #19204
* fix cpp-tests SpritePolygon crash, wrong png format (#19170)
* fix wrong png convert format from sRGB to Gray
* erase plist index if all frames was erased
* test_A8.png have I8 format, fix it
* [CCSpriteCache] allow re-add plist & add testcase (#19175)
* allow re-add plist & add testcase
* remove comments/rename method/update testcase
* fix isSpriteFramesWithFileLoaded & add testcase
* remove used variable
* remove unused variable
* fix double free issues when js/lua-tests exit on iOS (#19236)
* disable part cases, AutoTest works without crash on mac
* update cocos2dx files json, to test cocos new next
* fix spritecache plist parsing issue (#19269)
* [linux] Fix FileUtils::getContents with folder (#19157)
* fix FileUtils::getContents on linux/mac
* use stat.st_mode
* simplify
* [CCFileUtils] win32 getFileSize (#19176)
* win32 getFileSize
* fix stat
* [cpp test-Android]20:FileUtils/2 change title (#19197)
* sync #19200
* sync #19231
* [android lua] improve performance of lua loader (#19234)
* [lua] improve performance of lua loader
* remove cache fix
* Revert "fix spritecache plist parsing issue (#19269)"
This reverts commit f3a85ece4307a7b90816c34489d1ed2c8fd11baf.
* remove win32 project files ref in template.json
* add metal framework lnk ref into cpp template
* test on iOS, and disable part cases
* alBufferData instead of alBufferDataStatic for small audio file on Apple (#19227)
* changes AudioCache to use alBufferData instead of alBufferDataStatic
(also makes test 19 faster to trigger openal bugs faster)
The original problem: CrashIfClientProvidedBogusAudioBufferList
https://github.com/cocos2d/cocos2d-x/issues/18948
is not happening anymore, but there's still a not very frequent issue
that makes OpenAL crash with a call stack like this.
AudioCache::readDataTask > alBufferData > CleanUpDeadBufferList
It happes more frequently when the device is "cold", which means after
half an hour of not using the device (locked).
I could not find the actual source code for iOS OpenAL, so I used the
macOS versions:
https://opensource.apple.com/source/OpenAL/OpenAL-48.7/Source/OpenAL/oalImp.cpp.auto.html
They seem to use CAGuard.h to make sure the dead buffer list
has no threading issues. I'm worried because the CAGuard code I found
has macos and win32 define but no iOS, so I'm not sure. I guess the
iOS version is different and has the guard.
I could not find a place in the code that's unprotected by the locks
except the InitializeBufferMap() which should not be called more than
once from cocos, and there's a workaround in AudioEngine-impl for it.
I reduced the occurence of the CleanUpDeadBufferList crash by moving
the guard in ~AudioCache to cover the alDeleteBuffers call.
* remove hack method "setTimeout" on audio
* AutoTest works on iOS
* support set ios deployment target for root project
* enable all texture2d cases, since Jiang have fixed
* add CCTextureUtils to xcode project file (#176)
* add leak cases for SpriteFrameCache (#177)
* re-add SpriteFrameCache cases
* update template file json
* Update SpriteFrameCacheTest.cpp
* fix compiling error
2019-01-18 15:08:25 +08:00
# endif //(JPEG_LIB_VERSION >= 90)
2014-08-21 15:19:26 +08:00
// we only support RGB or grayscale
if ( cinfo . jpeg_color_space = = JCS_GRAYSCALE )
{
2020-09-25 11:07:56 +08:00
_pixelFormat = backend : : PixelFormat : : L8 ;
2014-08-21 15:19:26 +08:00
} else
{
cinfo . out_color_space = JCS_RGB ;
2020-09-25 11:07:56 +08:00
_pixelFormat = backend : : PixelFormat : : RGB8 ;
2014-08-21 15:19:26 +08:00
}
/* Start decompression jpeg here */
jpeg_start_decompress ( & cinfo ) ;
/* init image info */
_width = cinfo . output_width ;
_height = cinfo . output_height ;
_dataLen = cinfo . output_width * cinfo . output_height * cinfo . output_components ;
2020-09-01 14:57:18 +08:00
_data = static_cast < uint8_t * > ( malloc ( _dataLen ) ) ;
2014-08-21 15:19:26 +08:00
CC_BREAK_IF ( ! _data ) ;
/* now actually read the jpeg into the raw buffer */
/* read one scan line at a time */
2014-08-21 15:41:05 +08:00
while ( cinfo . output_scanline < cinfo . output_height )
2014-08-21 15:19:26 +08:00
{
2014-09-15 23:15:01 +08:00
row_pointer [ 0 ] = _data + location ;
location + = cinfo . output_width * cinfo . output_components ;
jpeg_read_scanlines ( & cinfo , row_pointer , 1 ) ;
2014-08-21 15:19:26 +08:00
}
2014-09-16 21:55:05 +08:00
2015-08-14 21:17:51 +08:00
/* When read image file with broken data, jpeg_finish_decompress() may cause error.
* Besides , jpeg_destroy_decompress ( ) shall deallocate and release all memory associated
* with the decompression object .
* So it doesn ' t need to call jpeg_finish_decompress ( ) .
*/
//jpeg_finish_decompress( &cinfo );
2014-08-21 15:19:26 +08:00
jpeg_destroy_decompress ( & cinfo ) ;
/* wrap up decompression, destroy objects, free pointers and close open files */
2014-08-21 15:41:05 +08:00
ret = true ;
2014-08-21 15:19:26 +08:00
} while ( 0 ) ;
2014-08-21 15:41:05 +08:00
return ret ;
2014-08-21 15:19:26 +08:00
# else
2015-04-16 00:19:38 +08:00
CCLOG ( " jpeg is not enabled, please enable it in ccConfig.h " ) ;
2014-08-21 15:19:26 +08:00
return false ;
# endif // CC_USE_JPEG
2012-04-19 14:35:52 +08:00
}
2020-09-01 14:57:18 +08:00
bool Image : : initWithPngData ( uint8_t * data , ssize_t dataLen )
2012-04-19 14:35:52 +08:00
{
metal support for cocos2d-x (#19305)
* remove deprecated files
* remove some deprecated codes
* remove more deprecated codes
* remove ui deprecated codes
* remove more deprecated codes
* remove deprecated codes in ccmenuitem
* remove more deprecated codes in ui
* remove more deprecated codes in ui
* remove more deprecated codes in ui
* remove more deprecated codes
* remove more deprecated codes
* remove more deprecated codes
* remove vr related codes and ignore some modules
* remove allocator
* remove some config
* 【Feature】add back-end project file
* [Feature] add back-end file
* add pipeline descriptor and shader cache
* [Feature] support sprite for backend
* [Feature] remove unneeded code
* [Feature] according to es2.0 spec, you must use clamp-to-edge as texture wrap mode, and no mipmapping for non-power-of-two texture
* [Feature] set texture wrap mode to clamp-to-edge, and no mipmapping for non-power-of-two texture
* [Feature] remove macro define to .cpp file
* [Feature] add log info
* [Feature] add PipelineDescriptor for TriangleCommand
* [Feature] add PipelineDescriptor object as member of TriangleCommand
* [Feature] add getPipelineDescriptor method
* add renderbackend
* complete pipeline descriptor
* [Feature] add viewport in RenderCommand
* set viewport when rendrering
* [Feature] occur error when using RendererBackend, to be fixed.
* a workaround to fix black screen on macOS 10.14 (#19090)
* add rendererbackend init function
* fix typo
* [Feature] modify testFile
* [BugFix] modify shader path
* [Feature] set default viewport
* fix projection
* [Feature] modify log info
* [BugFix] change viewport data type to int
* [BugFix] add BindGroup to PipelienDescriptor
* [BugFix] change a_position to vec3 in sprite.vert
* [BugFix] set vertexLayout according to V3F_C4B_T2F structure
* [Feature] revert a_position to vec4
* [Feature] renderer should not use gl codes directly
* [Feature] it's better not use default value parameter
* fix depth test setting
* rendererbackend -> renderer
* clear color and depth at begin
* add metal backend
* metal support normalized attribute
* simplify codes
* update external
* add render pass desctriptor in pipeline descriptor
* fix warnings
* fix crash and memeory leak
* refactor Texture2D
* put pipeline descriptor into render command
* simplify codes
* [Feature] update Sprite
* fix crash when closing app
* [Feature] update SpriteBatchNode and TextureAtlas
* support render texture(not finish)
* [Feature] remove unused code
* make tests work on mac
* fix download-deps path error
* make tests work on iOS
* [Feature] support ttf under normal label effect
* refactor triangle command processing
* let renderer handle more common commands
* refactor backend
* make render texture work
* [Feature] refactor backend for GL
* [Feature]Renaming to make it easy to understand
* [Feature] change warp mode to CLAMP_TO_EDGE
* fix ghost
* simplify visit render queue logic
* support progress timer without rial mode
* support partcile system
* Feature/update label (#149)
* [BugFix] fix compile error
* [Feature] support outline effect in ios
* [Feature] add shader file
* [BugFix] fix begin and end RenderPass
* [Feature] update CustomCommand
* [Feature] revert project.pbxproj
* [Feature] simplify codes
* [BugFix] pack AI88 to RGBA8888 only when outline enable
* [Feature] support shadow effect in Label
* [Feature] support BMFont
* [Feature] support glow effect
* [Feature] simplify shader files
* LabelAtlas work
* handle blend function correctly
* support tile map
* don't share buffer in metal
* alloc buffer size as needed
* support more tilemap
* Merge branch 'minggo/metal-support' into feature/updateLabel
* minggo/metal-support:
support tile map
handle blend function correctly
LabelAtlas work
Feature/update label (#149)
support partcile system
# Conflicts:
# cocos/2d/CCLabel.cpp
# cocos/2d/CCSprite.cpp
# cocos/2d/CCSpriteBatchNode.cpp
# cocos/renderer/CCQuadCommand.cpp
# cocos/renderer/CCQuadCommand.h
* render texture work without saving file
* use global viewport
* grid3d works
* remove grabber
* tiled3d works
* [BugFix] fix label bug
* [Feature] add updateSubData for buffer
* [Feature] remove setVertexCount
* support depth test
* add callback command
* [Feature] add UITest
* [Feature] update UITest
* [Feature] remove unneeded codes
* fix custom command issue
* fix layer color blend issue
* [BugFix] fix iOS compile error
* [Feature] remove unneeded codes
* [Feature] fix updateVertexBuffer
* layerradial works
* add draw test back
* fix batch issue
* fix compiling error
* [BugFix] support ETC1
* [BugFix] get the correct pipelineDescriptor
* [BugFix] skip draw when backendTexture nullptr
* clipping node support
* [Feature] add shader files
* fix stencil issue in metal
* [Feature] update UILayoutTest
* [BugFix] skip drawing when vertexCount is zero
* refactor renderer
* add set global z order for stencil manager commands
* fix warnings caused by type
* remove viewport in render command
* [Feature] fix warnings caused by type
* [BugFix] clear vertexCount and indexCount for CustomComand when needed
* [Feature] update clear for CustomCommand
* ios use metal
* fix viewport issue
* fix LayerColorGradient crash
* [cmake] transport to android and windows (#160)
* save point 1
* compile on windows
* run on android
* revert useless change
* android set CC_ENABLE_CACHE_TEXTURE_DATA to 1
* add initGlew
* fix android crash
* add TODO new-renderer
* review update
* revert onGLFWWindowPosCallback
* fix android compiling error
* Impl progress radial (#162)
* progresstimer add radial impl
* default drawType to element
* dec invoke times of createVertexBuffer (#163)
* support depth/stencil format for gl backend
* simplify progress timer codes
* support motionstreak, effect is wrong
* fix motionstreak issue
* [Feature] update Scissor Test (#161)
* [Feature] update Scissor Test
* [Feature] update ScissorTest
* [Feature] rename function
* [Feature] get constant reference if needed
* [Feature] show render status (#164)
* improve performance
* fix depth state
* fill error that triangle vertex/index number bigger than buffer
* fix compiline error in release mode
* fix buffer conflict between CPU and GPU on iOS/macOS
* Renderer refactor (#165)
* use one vertes/index buffer with opengl
* fix error on windows
* custom command support index format config
* CCLayer: compact vertex data structure
* update comment
* fix doc
* support fast tilemap
* pass index format instead
* fix some wrong effect
* fix render texture error
* fix texture per-element size
* fix texture format error
* BlendFunc type refactor, GLenum -> backend::BlendFactor (#167)
* BlendFunc use backend::BlendFactor as inner field
* update comments
* use int to replace GLenum
* update xcode project fiel
* rename to GLBlendConst
* add ccConstants.h
* update xcode project file
* update copyright
* remove primitive command
* remove CCPrimitive.cpp/.h
* remove deprecated files
* remove unneeded files
* remove multiple view support
* remove multiple view support
* remove the usage of frame buffer in camera
* director don't use frame buffer
* remove FrameBuffer
* remove BatchCommand
* add some api reference
* add physics2d back
* fix crash when close app on mac
* improve render texture
* fix rendertexture issue
* fix rendertexture issue
* simplify codes
* CMake support for mac & ios (#169)
* update cmake
* fix compile error
* update 3rd libs version
* remove CCThread.h/.cpp
* remove ccthread
* use audio engine to implement simple audio engine
* remove unneeded codes
* remove deprecated codes
* remove winrt macro
* remove CC_USE_WIC
* set partcile blend function in more elegant way
* remove unneeded codes
* remove unneeded codes
* cmake works on windows
* update project setting
* improve performance
* GLFloat -> float
* sync v3 cmake improvements into metal-support (#172)
* pick: modern cmake, compile definitions improvement (#19139)
* modern cmake, use target_compile_definitions partly
* simplify macro define, remove USE_*
* modern cmake, macro define
* add physics 2d macro define into ccConfig.h
* remove USE_CHIPMUNK macro in build.gradle
* remove CocosSelectModule.cmake
* shrink useless define
* simplify compile options config, re-add if necessary
* update external for tmp CI test
* un-quote target_compile_options value
* add "-g" parameter only when debug mode
* keep single build type when generator Xcode & VS projecy
* update external for tmp CI tes
* add static_cast<char>(-1), fix -Wc++11-narrowing
* simplify win32 compile define
* not modify code, only improve compile options
# Conflicts:
# .gitignore
# cmake/Modules/CocosConfigDepend.cmake
# cocos/CMakeLists.txt
# external/config.json
# tests/cpp-tests/CMakeLists.txt
* modern cmake, improve cmake_compiler_flags (#19145)
* cmake_compiler_flags
* Fix typo
* Fix typo2
* Remove chanages from Android.mk
* correct lua template cmake build (#19149)
* don't add -Wno-deprecated into jsb target
* correct lua template cmake build
* fix win32 lua template compile error
* prevent cmake in-source-build friendly (#19151)
* pick: Copy resources to "Resources/" on win32 like in linux configuration
* add "/Z7" for cpp-tests on windows
* [cmake] fix iOS xcode property setting failed (#19208)
* fix iOS xcode property setting failed
* use search_depend_libs_recursive at dlls collect
* fix typo
* [cmake] add find_host_library into iOS toolchain file (#19230)
* pick: [lua android] use luajit & template cmake update (#19239)
* increase cmake stability , remove tests/CMakeLists.txt (#19261)
* cmake win32 Precompiled header (#19273)
* Precompiled header
* Fix
* Precompiled header for cocos
* Precompiled header jscocos2d
* Fix for COCOS2D_DEBUG is always 1 on Android (#19291)
Related #19289
* little build fix, tests cpp-tests works on mac
* sync v3 build related codes into metal-support (#173)
* strict initialization for std::array
* remove proj.win32 project configs
* modern cmake, cmake_cleanup_remove_unused_variables (#19146)
* Switch travis CI to xenial (#19207)
* Switch travis CI to xenial
* Remove language: android
* Set language: cpp
* Fix java problem
* Update sdkmanager
* Fix sdkmanger
* next sdkmanager fix
* Remove xenial from android
* revert to sdk-tools-{system}-3859397
* Remove linux cmake install
* Update before-install.sh
* Update .travis.yml
* Simplify install-deps-linux.sh, tested on Ubuntu 16.04 (#19212)
* Simplify install-deps-linux.sh
* Cleanup
* pick: install ninja
* update cocos2d-console submodule
* for metal-support alpha release, we only test cpp
* add HelloCpp into project(Cocos2d-x) for tmp test
* update extenal metal-support-4
* update uniform setting
* [Feature] update BindGroup
* [Feature] empty-test
* [Feature] cpp-test
* [Feature] fix GL compiler error
* [Feature] fix GL crash
* [Feature] empty-test
* [Feature] cpp-tests
* [feature] improve frameRate
* [feature] fix opengl compile error
* [feature] fix opengl compile error
* [BugFix] fix compute maxLocation error
* [Feature] update setting unifrom
* [Feature] fix namespace
* [Feature] remove unneeded code
* [Bugfix] fix project file
* [Feature] update review
* [texture2d] impl texture format support (#175)
* texture update
* update
* update texture
* commit
* compile on windows
* ddd
* rename
* rename methods
* no crash
* save gl
* save
* save
* rename
* move out pixel format convert functions
* metal crash
* update
* update android
* support gles compressed texture format
* support more compress format
* add more conversion methods
* ss
* save
* update conversion methods
* add PVRTC format support
* reformat
* add marco linux
* fix GL marcro
* pvrtc supported only by ios 8.0+
* remove unused cmake
* revert change
* refactor Texture2D::initWithData
* fix conversion log
* refactor Texture2D::initWithData
* remove some OpenGL constants for PVRTC
* add todo
* fix typo
* AutoTest works on mac/iOS by disable part cases, sync v3 bug fix (#174)
* review cpp-tests, and fix part issues on start auto test
* sync png format fix: Node:Particle3D abnormal texture effects #19204
* fix cpp-tests SpritePolygon crash, wrong png format (#19170)
* fix wrong png convert format from sRGB to Gray
* erase plist index if all frames was erased
* test_A8.png have I8 format, fix it
* [CCSpriteCache] allow re-add plist & add testcase (#19175)
* allow re-add plist & add testcase
* remove comments/rename method/update testcase
* fix isSpriteFramesWithFileLoaded & add testcase
* remove used variable
* remove unused variable
* fix double free issues when js/lua-tests exit on iOS (#19236)
* disable part cases, AutoTest works without crash on mac
* update cocos2dx files json, to test cocos new next
* fix spritecache plist parsing issue (#19269)
* [linux] Fix FileUtils::getContents with folder (#19157)
* fix FileUtils::getContents on linux/mac
* use stat.st_mode
* simplify
* [CCFileUtils] win32 getFileSize (#19176)
* win32 getFileSize
* fix stat
* [cpp test-Android]20:FileUtils/2 change title (#19197)
* sync #19200
* sync #19231
* [android lua] improve performance of lua loader (#19234)
* [lua] improve performance of lua loader
* remove cache fix
* Revert "fix spritecache plist parsing issue (#19269)"
This reverts commit f3a85ece4307a7b90816c34489d1ed2c8fd11baf.
* remove win32 project files ref in template.json
* add metal framework lnk ref into cpp template
* test on iOS, and disable part cases
* alBufferData instead of alBufferDataStatic for small audio file on Apple (#19227)
* changes AudioCache to use alBufferData instead of alBufferDataStatic
(also makes test 19 faster to trigger openal bugs faster)
The original problem: CrashIfClientProvidedBogusAudioBufferList
https://github.com/cocos2d/cocos2d-x/issues/18948
is not happening anymore, but there's still a not very frequent issue
that makes OpenAL crash with a call stack like this.
AudioCache::readDataTask > alBufferData > CleanUpDeadBufferList
It happes more frequently when the device is "cold", which means after
half an hour of not using the device (locked).
I could not find the actual source code for iOS OpenAL, so I used the
macOS versions:
https://opensource.apple.com/source/OpenAL/OpenAL-48.7/Source/OpenAL/oalImp.cpp.auto.html
They seem to use CAGuard.h to make sure the dead buffer list
has no threading issues. I'm worried because the CAGuard code I found
has macos and win32 define but no iOS, so I'm not sure. I guess the
iOS version is different and has the guard.
I could not find a place in the code that's unprotected by the locks
except the InitializeBufferMap() which should not be called more than
once from cocos, and there's a workaround in AudioEngine-impl for it.
I reduced the occurence of the CleanUpDeadBufferList crash by moving
the guard in ~AudioCache to cover the alDeleteBuffers call.
* remove hack method "setTimeout" on audio
* AutoTest works on iOS
* support set ios deployment target for root project
* enable all texture2d cases, since Jiang have fixed
* add CCTextureUtils to xcode project file (#176)
* add leak cases for SpriteFrameCache (#177)
* re-add SpriteFrameCache cases
* update template file json
* Update SpriteFrameCacheTest.cpp
* fix compiling error
2019-01-18 15:08:25 +08:00
# if CC_USE_PNG
2013-07-19 15:37:54 +08:00
// length of bytes to check if it is a valid png file
2012-04-26 17:36:32 +08:00
# define PNGSIGSIZE 8
2014-08-21 15:41:05 +08:00
bool ret = false ;
2012-04-26 17:36:32 +08:00
png_byte header [ PNGSIGSIZE ] = { 0 } ;
2012-04-19 14:35:52 +08:00
png_structp png_ptr = 0 ;
png_infop info_ptr = 0 ;
do
{
// png header len is 8 bytes
2013-07-25 21:35:00 +08:00
CC_BREAK_IF ( dataLen < PNGSIGSIZE ) ;
2012-04-19 14:35:52 +08:00
// check the data is png or not
2013-07-25 21:35:00 +08:00
memcpy ( header , data , PNGSIGSIZE ) ;
2012-04-26 17:36:32 +08:00
CC_BREAK_IF ( png_sig_cmp ( header , 0 , PNGSIGSIZE ) ) ;
2012-04-19 14:35:52 +08:00
// init png_struct
png_ptr = png_create_read_struct ( PNG_LIBPNG_VER_STRING , 0 , 0 , 0 ) ;
CC_BREAK_IF ( ! png_ptr ) ;
// init png_info
info_ptr = png_create_info_struct ( png_ptr ) ;
CC_BREAK_IF ( ! info_ptr ) ;
2012-04-26 17:36:32 +08:00
2012-04-19 14:35:52 +08:00
CC_BREAK_IF ( setjmp ( png_jmpbuf ( png_ptr ) ) ) ;
2012-04-26 17:36:32 +08:00
2012-04-19 14:35:52 +08:00
// set the read call back function
tImageSource imageSource ;
2020-09-01 14:57:18 +08:00
imageSource . data = ( uint8_t * ) data ;
2013-07-25 21:35:00 +08:00
imageSource . size = dataLen ;
2012-04-19 14:35:52 +08:00
imageSource . offset = 0 ;
png_set_read_fn ( png_ptr , & imageSource , pngReadCallback ) ;
2012-04-26 17:36:32 +08:00
// read png header info
2013-07-19 15:37:54 +08:00
2012-04-26 17:36:32 +08:00
// read png file info
png_read_info ( png_ptr , info_ptr ) ;
2013-07-19 15:37:54 +08:00
2013-06-15 14:03:30 +08:00
_width = png_get_image_width ( png_ptr , info_ptr ) ;
_height = png_get_image_height ( png_ptr , info_ptr ) ;
2013-07-25 21:35:00 +08:00
png_byte bit_depth = png_get_bit_depth ( png_ptr , info_ptr ) ;
2012-04-26 17:36:32 +08:00
png_uint_32 color_type = png_get_color_type ( png_ptr , info_ptr ) ;
2012-08-29 21:50:09 +08:00
//CCLOG("color type %u", color_type);
2013-07-19 15:37:54 +08:00
2012-07-24 16:43:48 +08:00
// force palette images to be expanded to 24-bit RGB
// it may include alpha channel
if ( color_type = = PNG_COLOR_TYPE_PALETTE )
{
png_set_palette_to_rgb ( png_ptr ) ;
}
2012-09-16 05:19:14 +08:00
// low-bit-depth grayscale images are to be expanded to 8 bits
2013-07-25 21:35:00 +08:00
if ( color_type = = PNG_COLOR_TYPE_GRAY & & bit_depth < 8 )
2012-07-24 16:43:48 +08:00
{
2013-07-25 21:35:00 +08:00
bit_depth = 8 ;
2012-07-26 11:42:31 +08:00
png_set_expand_gray_1_2_4_to_8 ( png_ptr ) ;
2012-04-26 17:36:32 +08:00
}
2012-07-24 16:43:48 +08:00
// expand any tRNS chunk data into a full alpha channel
if ( png_get_valid ( png_ptr , info_ptr , PNG_INFO_tRNS ) )
{
png_set_tRNS_to_alpha ( png_ptr ) ;
}
// reduce images with 16-bit samples to 8 bits
2013-07-25 21:35:00 +08:00
if ( bit_depth = = 16 )
2012-04-26 17:36:32 +08:00
{
2012-07-24 16:43:48 +08:00
png_set_strip_16 ( png_ptr ) ;
2012-04-26 17:36:32 +08:00
}
2013-07-17 17:12:04 +08:00
// Expanded earlier for grayscale, now take care of palette and rgb
2014-08-21 15:41:05 +08:00
if ( bit_depth < 8 )
{
2013-07-19 15:37:54 +08:00
png_set_packing ( png_ptr ) ;
}
// update info
png_read_update_info ( png_ptr , info_ptr ) ;
color_type = png_get_color_type ( png_ptr , info_ptr ) ;
switch ( color_type )
{
case PNG_COLOR_TYPE_GRAY :
2020-09-25 11:07:56 +08:00
_pixelFormat = backend : : PixelFormat : : L8 ;
2013-07-25 21:35:00 +08:00
break ;
2013-07-19 15:37:54 +08:00
case PNG_COLOR_TYPE_GRAY_ALPHA :
2020-09-25 11:07:56 +08:00
_pixelFormat = backend : : PixelFormat : : LA8 ;
2013-07-19 15:37:54 +08:00
break ;
case PNG_COLOR_TYPE_RGB :
2020-09-25 11:07:56 +08:00
_pixelFormat = backend : : PixelFormat : : RGB8 ;
2013-07-25 21:35:00 +08:00
break ;
2013-07-19 15:37:54 +08:00
case PNG_COLOR_TYPE_RGB_ALPHA :
2020-09-25 11:07:56 +08:00
_pixelFormat = backend : : PixelFormat : : RGBA8 ;
2013-07-19 15:37:54 +08:00
break ;
default :
break ;
}
2012-04-19 14:35:52 +08:00
2012-04-26 17:36:32 +08:00
// read png data
2013-12-06 16:32:06 +08:00
png_size_t rowbytes ;
2013-06-15 14:03:30 +08:00
png_bytep * row_pointers = ( png_bytep * ) malloc ( sizeof ( png_bytep ) * _height ) ;
2013-07-19 15:37:54 +08:00
2012-07-24 16:43:48 +08:00
rowbytes = png_get_rowbytes ( png_ptr , info_ptr ) ;
2013-07-19 15:37:54 +08:00
2013-07-25 21:35:00 +08:00
_dataLen = rowbytes * _height ;
2020-09-01 14:57:18 +08:00
_data = static_cast < uint8_t * > ( malloc ( _dataLen ) ) ;
2014-08-21 15:41:05 +08:00
if ( ! _data )
2014-04-13 06:13:58 +08:00
{
if ( row_pointers ! = nullptr )
{
free ( row_pointers ) ;
}
break ;
}
2013-07-19 15:37:54 +08:00
2013-06-15 14:03:30 +08:00
for ( unsigned short i = 0 ; i < _height ; + + i )
2012-04-19 14:35:52 +08:00
{
2013-06-15 14:03:30 +08:00
row_pointers [ i ] = _data + i * rowbytes ;
2012-07-24 16:43:48 +08:00
}
png_read_image ( png_ptr , row_pointers ) ;
2013-07-19 15:37:54 +08:00
2014-02-13 16:22:41 +08:00
png_read_end ( png_ptr , nullptr ) ;
2013-07-19 15:37:54 +08:00
2014-06-03 15:06:15 +08:00
// premultiplied alpha for RGBA8888
2019-04-11 09:33:26 +08:00
if ( color_type = = PNG_COLOR_TYPE_RGB_ALPHA )
2014-06-03 15:06:15 +08:00
{
2019-04-11 09:33:26 +08:00
if ( PNG_PREMULTIPLIED_ALPHA_ENABLED )
{
2019-07-26 17:09:00 +08:00
premultiplyAlpha ( ) ;
2019-04-11 09:33:26 +08:00
}
else
2020-11-16 12:21:27 +08:00
{
// if PNG_PREMULTIPLIED_ALPHA_ENABLED == false && CC_ENABLE_PREMULTIPLIED_ALPHA != 0,
// you must do PMA at shader, such as modify positionTextureColor.frag
_hasPremultipliedAlpha = ! ! CC_ENABLE_PREMULTIPLIED_ALPHA ;
2019-04-11 09:33:26 +08:00
}
2014-06-03 15:06:15 +08:00
}
2012-07-24 16:43:48 +08:00
2013-11-29 15:13:16 +08:00
if ( row_pointers ! = nullptr )
{
free ( row_pointers ) ;
2014-04-13 06:13:58 +08:00
}
2012-07-26 11:42:31 +08:00
2014-08-21 15:41:05 +08:00
ret = true ;
2012-04-19 14:35:52 +08:00
} while ( 0 ) ;
if ( png_ptr )
{
png_destroy_read_struct ( & png_ptr , ( info_ptr ) ? & info_ptr : 0 , 0 ) ;
}
2014-08-21 15:41:05 +08:00
return ret ;
2015-04-16 00:19:38 +08:00
# else
CCLOG ( " png is not enabled, please enable it in ccConfig.h " ) ;
return false ;
# endif //CC_USE_PNG
2012-04-19 14:35:52 +08:00
}
2020-09-01 14:57:18 +08:00
bool Image : : initWithBmpData ( uint8_t * data , ssize_t dataLen )
2019-11-25 02:28:38 +08:00
{
2020-09-01 12:06:21 +08:00
const int nrChannels = 4 ;
2019-11-25 02:28:38 +08:00
_data = stbi_load_from_memory ( data , dataLen , & _width , & _height , nullptr , nrChannels ) ;
if ( _data ) {
_dataLen = _width * _height * nrChannels ;
_fileType = Format : : BMP ;
2020-09-25 11:07:56 +08:00
_pixelFormat = backend : : PixelFormat : : RGBA8 ;
2020-09-01 12:06:21 +08:00
return true ;
2019-11-25 02:28:38 +08:00
}
2020-09-01 12:06:21 +08:00
return false ;
2019-11-25 02:28:38 +08:00
}
2020-09-01 14:57:18 +08:00
bool Image : : initWithWebpData ( uint8_t * data , ssize_t dataLen )
2020-09-01 11:43:03 +08:00
{
# if CC_USE_WEBP
bool ret = false ;
do
{
WebPDecoderConfig config ;
if ( WebPInitDecoderConfig ( & config ) = = 0 ) break ;
if ( WebPGetFeatures ( static_cast < const uint8_t * > ( data ) , dataLen , & config . input ) ! = VP8_STATUS_OK ) break ;
if ( config . input . width = = 0 | | config . input . height = = 0 ) break ;
config . output . colorspace = config . input . has_alpha ? MODE_rgbA : MODE_RGB ;
2020-09-25 11:07:56 +08:00
_pixelFormat = config . input . has_alpha ? backend : : PixelFormat : : RGBA8 : backend : : PixelFormat : : RGB8 ;
2020-09-01 11:43:03 +08:00
_width = config . input . width ;
_height = config . input . height ;
//we ask webp to give data with premultiplied alpha
_hasPremultipliedAlpha = ( config . input . has_alpha ! = 0 ) ;
_dataLen = _width * _height * ( config . input . has_alpha ? 4 : 3 ) ;
2020-09-01 14:57:18 +08:00
_data = static_cast < uint8_t * > ( malloc ( _dataLen ) ) ;
2020-09-01 11:43:03 +08:00
config . output . u . RGBA . rgba = static_cast < uint8_t * > ( _data ) ;
config . output . u . RGBA . stride = _width * ( config . input . has_alpha ? 4 : 3 ) ;
config . output . u . RGBA . size = _dataLen ;
config . output . is_external_memory = 1 ;
if ( WebPDecode ( static_cast < const uint8_t * > ( data ) , dataLen , & config ) ! = VP8_STATUS_OK )
{
free ( _data ) ;
_data = nullptr ;
break ;
}
ret = true ;
} while ( 0 ) ;
return ret ;
# else
CCLOG ( " webp is not enabled, please enable it in ccConfig.h " ) ;
return false ;
# endif // CC_USE_WEBP
}
2020-08-31 23:09:42 +08:00
bool Image : : initWithTGAData ( tImageTGA * tgaData )
2013-07-25 21:35:00 +08:00
{
2020-08-31 23:09:42 +08:00
bool ret = false ;
do
2013-07-25 21:35:00 +08:00
{
2020-08-31 23:09:42 +08:00
CC_BREAK_IF ( tgaData = = nullptr ) ;
// tgaLoadBuffer only support type 2, 3, 10
if ( 2 = = tgaData - > type | | 10 = = tgaData - > type )
{
// true color
// unsupported RGB555
if ( tgaData - > pixelDepth = = 16 )
{
_pixelFormat = backend : : PixelFormat : : RGB5A1 ;
}
else if ( tgaData - > pixelDepth = = 24 )
{
2020-09-25 11:07:56 +08:00
_pixelFormat = backend : : PixelFormat : : RGB8 ;
2020-08-31 23:09:42 +08:00
}
else if ( tgaData - > pixelDepth = = 32 )
{
2020-09-25 11:07:56 +08:00
_pixelFormat = backend : : PixelFormat : : RGBA8 ;
2020-08-31 23:09:42 +08:00
}
else
{
CCLOG ( " Image WARNING: unsupported true color tga data pixel format. FILE: %s " , _filePath . c_str ( ) ) ;
break ;
}
}
else if ( 3 = = tgaData - > type )
{
// gray
if ( 8 = = tgaData - > pixelDepth )
{
2020-09-25 11:07:56 +08:00
_pixelFormat = backend : : PixelFormat : : L8 ;
2020-08-31 23:09:42 +08:00
}
else
{
// actually this won't happen, if it happens, maybe the image file is not a tga
CCLOG ( " Image WARNING: unsupported gray tga data pixel format. FILE: %s " , _filePath . c_str ( ) ) ;
break ;
}
}
_width = tgaData - > width ;
_height = tgaData - > height ;
_data = tgaData - > imageData ;
_dataLen = _width * _height * tgaData - > pixelDepth / 8 ;
_fileType = Format : : TGA ;
ret = true ;
} while ( false ) ;
2013-07-25 21:35:00 +08:00
2020-08-31 23:09:42 +08:00
if ( ret )
2013-08-06 16:14:36 +08:00
{
2020-08-31 23:09:42 +08:00
if ( FileUtils : : getInstance ( ) - > getFileExtension ( _filePath ) ! = " .tga " )
{
CCLOG ( " Image WARNING: the image file suffix is not tga, but parsed as a tga image file. FILE: %s " , _filePath . c_str ( ) ) ;
2013-08-06 16:14:36 +08:00
}
2013-07-25 21:35:00 +08:00
}
2020-08-31 23:09:42 +08:00
else
{
if ( tgaData & & tgaData - > imageData ! = nullptr )
{
free ( tgaData - > imageData ) ;
_data = nullptr ;
}
}
return ret ;
2013-07-25 21:35:00 +08:00
}
2020-09-01 14:57:18 +08:00
bool Image : : initWithPVRv2Data ( uint8_t * data , ssize_t dataLen , bool ownData )
2013-07-25 21:35:00 +08:00
{
2013-07-29 11:40:18 +08:00
int blockSize = 0 , widthBlocks = 0 , heightBlocks = 0 ;
int width = 0 , height = 0 ;
2013-07-25 21:35:00 +08:00
//Cast first sizeof(PVRTexHeader) bytes of data stream as PVRTexHeader
2013-08-12 17:05:19 +08:00
const PVRv2TexHeader * header = static_cast < const PVRv2TexHeader * > ( static_cast < const void * > ( data ) ) ;
2013-07-25 21:35:00 +08:00
//Make sure that tag is in correct formatting
2013-08-06 16:14:36 +08:00
if ( memcmp ( & header - > pvrTag , gPVRTexIdentifier , strlen ( gPVRTexIdentifier ) ) ! = 0 )
2013-07-25 21:35:00 +08:00
{
return false ;
}
Configuration * configuration = Configuration : : getInstance ( ) ;
2014-07-25 18:19:04 +08:00
//can not detect the premultiplied alpha from pvr file, use _PVRHaveAlphaPremultiplied instead.
2020-11-16 12:21:27 +08:00
_hasPremultipliedAlpha = isCompressedImageHavePMA ( CompressedImagePMAFlag : : PVR ) ;
2014-07-25 18:01:32 +08:00
2013-08-06 16:14:36 +08:00
unsigned int flags = CC_SWAP_INT32_LITTLE_TO_HOST ( header - > flags ) ;
PVR2TexturePixelFormat formatFlags = static_cast < PVR2TexturePixelFormat > ( flags & PVR_TEXTURE_FLAG_TYPE_MASK ) ;
2013-08-01 15:53:52 +08:00
bool flipped = ( flags & ( unsigned int ) PVR2TextureFlag : : VerticalFlip ) ? true : false ;
2013-07-25 21:35:00 +08:00
if ( flipped )
{
CCLOG ( " cocos2d: WARNING: Image is flipped. Regenerate it using PVRTexTool " ) ;
}
if ( ! configuration - > supportsNPOT ( ) & &
2013-12-27 15:06:16 +08:00
( static_cast < int > ( header - > width ) ! = ccNextPOT ( header - > width )
| | static_cast < int > ( header - > height ) ! = ccNextPOT ( header - > height ) ) )
2013-07-25 21:35:00 +08:00
{
CCLOG ( " cocos2d: ERROR: Loading an NPOT texture (%dx%d) but is not supported on this device " , header - > width , header - > height ) ;
return false ;
}
2013-08-06 16:14:36 +08:00
if ( ! testFormatForPvr2TCSupport ( formatFlags ) )
2013-07-25 21:35:00 +08:00
{
2013-11-06 09:36:44 +08:00
CCLOG ( " cocos2d: WARNING: Unsupported PVR Pixel Format: 0x%02X. Re-encode it with a OpenGL pixel format variant " , ( int ) formatFlags ) ;
2013-07-25 21:35:00 +08:00
return false ;
}
if ( v2_pixel_formathash . find ( formatFlags ) = = v2_pixel_formathash . end ( ) )
{
2013-11-06 09:36:44 +08:00
CCLOG ( " cocos2d: WARNING: Unsupported PVR Pixel Format: 0x%02X. Re-encode it with a OpenGL pixel format variant " , ( int ) formatFlags ) ;
2013-07-25 21:35:00 +08:00
return false ;
}
2020-09-25 11:07:56 +08:00
auto pixelFormat = getDevicePVRPixelFormat ( v2_pixel_formathash . at ( formatFlags ) ) ;
auto & info = backend : : PixelFormatUtils : : getFormatDescriptor ( pixelFormat ) ;
int bpp = info . bpp ;
if ( ! bpp )
2013-07-25 21:35:00 +08:00
{
2013-11-06 09:36:44 +08:00
CCLOG ( " cocos2d: WARNING: Unsupported PVR Pixel Format: 0x%02X. Re-encode it with a OpenGL pixel format variant " , ( int ) formatFlags ) ;
2013-07-25 21:35:00 +08:00
return false ;
}
2020-09-25 11:07:56 +08:00
_pixelFormat = pixelFormat ;
2013-07-25 21:35:00 +08:00
//Reset num of mipmaps
_numberOfMipmaps = 0 ;
//Get size of mipmap
_width = width = CC_SWAP_INT32_LITTLE_TO_HOST ( header - > width ) ;
_height = height = CC_SWAP_INT32_LITTLE_TO_HOST ( header - > height ) ;
//Move by size of header
2020-08-31 21:04:47 +08:00
const int pixelOffset = sizeof ( PVRv2TexHeader ) ;
2020-09-01 14:57:18 +08:00
uint8_t * pixelData = data + pixelOffset ;
2020-08-31 21:04:47 +08:00
int dataOffset = 0 , dataSize = 0 ;
//Get ptr to where data starts..
int dataLength = CC_SWAP_INT32_LITTLE_TO_HOST ( header - > dataLength ) ;
2013-07-25 21:35:00 +08:00
// Calculate the data size for each texture level and respect the minimum number of blocks
while ( dataOffset < dataLength )
{
switch ( formatFlags ) {
2014-06-26 02:44:20 +08:00
case PVR2TexturePixelFormat : : PVRTC2BPP_RGBA :
2014-08-21 15:41:05 +08:00
if ( ! Configuration : : getInstance ( ) - > supportsPVRTC ( ) )
2014-06-26 02:44:20 +08:00
{
CCLOG ( " cocos2d: Hardware PVR decoder not present. Using software decoder " ) ;
_unpack = true ;
_mipmaps [ _numberOfMipmaps ] . len = width * height * 4 ;
2020-09-01 14:57:18 +08:00
_mipmaps [ _numberOfMipmaps ] . address = ( uint8_t * ) malloc ( width * height * 4 ) ;
2020-08-31 21:04:47 +08:00
PVRTDecompressPVRTC ( pixelData + dataOffset , width , height , _mipmaps [ _numberOfMipmaps ] . address , true ) ;
2014-06-26 02:44:20 +08:00
bpp = 2 ;
}
blockSize = 8 * 4 ; // Pixel by pixel block size for 2bpp
widthBlocks = width / 8 ;
heightBlocks = height / 4 ;
break ;
case PVR2TexturePixelFormat : : PVRTC4BPP_RGBA :
2014-08-21 15:41:05 +08:00
if ( ! Configuration : : getInstance ( ) - > supportsPVRTC ( ) )
2014-06-26 02:44:20 +08:00
{
CCLOG ( " cocos2d: Hardware PVR decoder not present. Using software decoder " ) ;
_unpack = true ;
_mipmaps [ _numberOfMipmaps ] . len = width * height * 4 ;
2020-09-01 14:57:18 +08:00
_mipmaps [ _numberOfMipmaps ] . address = ( uint8_t * ) malloc ( width * height * 4 ) ;
2020-08-31 21:04:47 +08:00
PVRTDecompressPVRTC ( pixelData + dataOffset , width , height , _mipmaps [ _numberOfMipmaps ] . address , false ) ;
2014-06-26 02:44:20 +08:00
bpp = 4 ;
}
blockSize = 4 * 4 ; // Pixel by pixel block size for 4bpp
widthBlocks = width / 4 ;
heightBlocks = height / 4 ;
break ;
case PVR2TexturePixelFormat : : BGRA8888 :
2020-08-31 19:24:23 +08:00
if ( ! Configuration : : getInstance ( ) - > supportsBGRA8888 ( ) )
2014-06-26 02:44:20 +08:00
{
CCLOG ( " cocos2d: Image. BGRA8888 not supported on this device " ) ;
return false ;
}
default :
blockSize = 1 ;
widthBlocks = width ;
heightBlocks = height ;
break ;
2013-07-25 21:35:00 +08:00
}
2014-06-26 02:44:20 +08:00
2013-07-25 21:35:00 +08:00
// Clamp to minimum number of blocks
if ( widthBlocks < 2 )
{
widthBlocks = 2 ;
}
if ( heightBlocks < 2 )
{
heightBlocks = 2 ;
}
2014-06-26 02:44:20 +08:00
2014-03-25 16:16:56 +08:00
dataSize = widthBlocks * heightBlocks * ( ( blockSize * bpp ) / 8 ) ;
2013-07-29 11:40:18 +08:00
int packetLength = ( dataLength - dataOffset ) ;
2013-07-25 21:35:00 +08:00
packetLength = packetLength > dataSize ? dataSize : packetLength ;
2014-06-26 02:44:20 +08:00
2013-07-25 21:35:00 +08:00
//Make record to the mipmaps array and increment counter
2014-03-20 17:07:28 +08:00
if ( ! _unpack )
2014-03-18 02:28:24 +08:00
{
2020-08-31 21:04:47 +08:00
_mipmaps [ _numberOfMipmaps ] . address = pixelData + dataOffset ;
2014-03-18 02:28:24 +08:00
_mipmaps [ _numberOfMipmaps ] . len = packetLength ;
}
2013-07-25 21:35:00 +08:00
_numberOfMipmaps + + ;
2014-06-26 02:44:20 +08:00
2013-07-25 21:35:00 +08:00
dataOffset + = packetLength ;
2014-06-26 02:44:20 +08:00
2013-07-25 21:35:00 +08:00
//Update width and height to the next lower power of two
width = MAX ( width > > 1 , 1 ) ;
height = MAX ( height > > 1 , 1 ) ;
}
2014-03-20 17:07:28 +08:00
2020-08-31 21:04:47 +08:00
if ( ! _unpack ) { // hardware decoder, hold data directly
2020-08-31 23:09:42 +08:00
forwardPixels ( data , dataLen , pixelOffset , ownData ) ;
2020-08-31 21:04:47 +08:00
}
else {
2014-03-20 17:07:28 +08:00
_data = _mipmaps [ 0 ] . address ;
_dataLen = _mipmaps [ 0 ] . len ;
}
2013-07-25 21:35:00 +08:00
return true ;
}
2020-09-01 14:57:18 +08:00
bool Image : : initWithPVRv3Data ( uint8_t * data , ssize_t dataLen , bool ownData )
2013-07-25 21:35:00 +08:00
{
2013-11-13 11:22:34 +08:00
if ( static_cast < size_t > ( dataLen ) < sizeof ( PVRv3TexHeader ) )
2013-07-25 21:35:00 +08:00
{
2015-08-14 21:17:51 +08:00
return false ;
}
const PVRv3TexHeader * header = static_cast < const PVRv3TexHeader * > ( static_cast < const void * > ( data ) ) ;
// validate version
if ( CC_SWAP_INT32_BIG_TO_HOST ( header - > version ) ! = 0x50565203 )
{
CCLOG ( " cocos2d: WARNING: pvr file version mismatch " ) ;
return false ;
}
// parse pixel format
PVR3TexturePixelFormat pixelFormat = static_cast < PVR3TexturePixelFormat > ( header - > pixelFormat ) ;
2013-07-25 21:35:00 +08:00
2013-08-06 16:14:36 +08:00
if ( ! testFormatForPvr3TCSupport ( pixelFormat ) )
2013-07-25 21:35:00 +08:00
{
2013-08-06 16:14:36 +08:00
CCLOG ( " cocos2d: WARNING: Unsupported PVR Pixel Format: 0x%016llX. Re-encode it with a OpenGL pixel format variant " ,
static_cast < unsigned long long > ( pixelFormat ) ) ;
2013-07-25 21:35:00 +08:00
return false ;
}
if ( v3_pixel_formathash . find ( pixelFormat ) = = v3_pixel_formathash . end ( ) )
{
2013-08-06 16:14:36 +08:00
CCLOG ( " cocos2d: WARNING: Unsupported PVR Pixel Format: 0x%016llX. Re-encode it with a OpenGL pixel format variant " ,
static_cast < unsigned long long > ( pixelFormat ) ) ;
2013-07-25 21:35:00 +08:00
return false ;
}
2020-09-25 11:07:56 +08:00
auto finalPixelFormat = getDevicePVRPixelFormat ( v3_pixel_formathash . at ( pixelFormat ) ) ;
auto & info = backend : : PixelFormatUtils : : getFormatDescriptor ( finalPixelFormat ) ;
int bpp = info . bpp ;
if ( ! info . bpp )
2013-07-25 21:35:00 +08:00
{
2013-08-06 16:14:36 +08:00
CCLOG ( " cocos2d: WARNING: Unsupported PVR Pixel Format: 0x%016llX. Re-encode it with a OpenGL pixel format variant " ,
static_cast < unsigned long long > ( pixelFormat ) ) ;
2013-07-25 21:35:00 +08:00
return false ;
}
2020-09-25 11:07:56 +08:00
_pixelFormat = finalPixelFormat ;
2013-07-25 21:35:00 +08:00
// flags
2015-08-14 21:17:51 +08:00
int flags = CC_SWAP_INT32_LITTLE_TO_HOST ( header - > flags ) ;
2013-07-26 17:34:44 +08:00
// PVRv3 specifies premultiply alpha in a flag -- should always respect this in PVRv3 files
2013-08-01 15:53:52 +08:00
if ( flags & ( unsigned int ) PVR3TextureFlag : : PremultipliedAlpha )
2013-07-26 17:34:44 +08:00
{
2014-07-25 18:01:32 +08:00
_hasPremultipliedAlpha = true ;
2013-07-26 17:34:44 +08:00
}
2013-07-25 21:35:00 +08:00
2015-08-14 21:17:51 +08:00
// sizing
int width = CC_SWAP_INT32_LITTLE_TO_HOST ( header - > width ) ;
int height = CC_SWAP_INT32_LITTLE_TO_HOST ( header - > height ) ;
_width = width ;
_height = height ;
2020-08-31 21:04:47 +08:00
const int pixelOffset = ( sizeof ( PVRv3TexHeader ) + header - > metadataLength ) ;
2020-09-01 14:57:18 +08:00
uint8_t * pixelData = data + pixelOffset ;
2020-08-31 21:04:47 +08:00
int pixelLen = dataLen - pixelOffset ;
2015-08-14 21:17:51 +08:00
int dataOffset = 0 , dataSize = 0 ;
int blockSize = 0 , widthBlocks = 0 , heightBlocks = 0 ;
2020-08-31 21:04:47 +08:00
2015-08-14 21:17:51 +08:00
_numberOfMipmaps = header - > numberOfMipmaps ;
2016-05-18 00:49:35 +08:00
CCASSERT ( _numberOfMipmaps < MIPMAP_MAX , " Image: Maximum number of mimpaps reached. Increase the CC_MIPMAP_MAX value " ) ;
2015-08-14 21:17:51 +08:00
for ( int i = 0 ; i < _numberOfMipmaps ; i + + )
2013-07-25 21:35:00 +08:00
{
2015-08-14 21:17:51 +08:00
switch ( ( PVR3TexturePixelFormat ) pixelFormat )
2013-07-25 21:35:00 +08:00
{
2020-09-25 11:07:56 +08:00
case PVR3TexturePixelFormat : : PVRTC2BPP_RGB :
case PVR3TexturePixelFormat : : PVRTC2BPP_RGBA :
2014-08-21 15:41:05 +08:00
if ( ! Configuration : : getInstance ( ) - > supportsPVRTC ( ) )
2014-06-26 02:44:20 +08:00
{
CCLOG ( " cocos2d: Hardware PVR decoder not present. Using software decoder " ) ;
_unpack = true ;
_mipmaps [ i ] . len = width * height * 4 ;
2020-09-01 14:57:18 +08:00
_mipmaps [ i ] . address = ( uint8_t * ) malloc ( width * height * 4 ) ;
2020-08-31 21:04:47 +08:00
PVRTDecompressPVRTC ( pixelData + dataOffset , width , height , _mipmaps [ i ] . address , true ) ;
2014-03-25 16:16:56 +08:00
bpp = 2 ;
2014-06-26 02:44:20 +08:00
}
blockSize = 8 * 4 ; // Pixel by pixel block size for 2bpp
widthBlocks = width / 8 ;
heightBlocks = height / 4 ;
break ;
2020-09-25 11:07:56 +08:00
case PVR3TexturePixelFormat : : PVRTC4BPP_RGB :
case PVR3TexturePixelFormat : : PVRTC4BPP_RGBA :
2014-08-21 15:41:05 +08:00
if ( ! Configuration : : getInstance ( ) - > supportsPVRTC ( ) )
2014-06-26 02:44:20 +08:00
{
CCLOG ( " cocos2d: Hardware PVR decoder not present. Using software decoder " ) ;
_unpack = true ;
_mipmaps [ i ] . len = width * height * 4 ;
2020-09-01 14:57:18 +08:00
_mipmaps [ i ] . address = ( uint8_t * ) malloc ( width * height * 4 ) ;
2020-08-31 21:04:47 +08:00
PVRTDecompressPVRTC ( pixelData + dataOffset , width , height , _mipmaps [ i ] . address , false ) ;
2014-03-25 16:16:56 +08:00
bpp = 4 ;
2014-06-26 02:44:20 +08:00
}
blockSize = 4 * 4 ; // Pixel by pixel block size for 4bpp
widthBlocks = width / 4 ;
heightBlocks = height / 4 ;
break ;
case PVR3TexturePixelFormat : : ETC1 :
2020-09-01 11:43:03 +08:00
if ( ! Configuration : : getInstance ( ) - > supportsETC1 ( ) )
2014-06-26 02:44:20 +08:00
{
CCLOG ( " cocos2d: Hardware ETC1 decoder not present. Using software decoder " ) ;
2020-09-01 11:43:03 +08:00
const int bytePerPixel = 4 ;
2014-03-20 17:07:28 +08:00
_unpack = true ;
2014-06-26 02:44:20 +08:00
_mipmaps [ i ] . len = width * height * bytePerPixel ;
2020-09-01 14:57:18 +08:00
_mipmaps [ i ] . address = ( uint8_t * ) malloc ( width * height * bytePerPixel ) ;
2020-09-01 11:43:03 +08:00
if ( etc2_decode_image ( ETC2_RGB_NO_MIPMAPS , pixelData + dataOffset , static_cast < etc1_byte * > ( _mipmaps [ i ] . address ) , width , height ) ! = 0 )
2014-06-26 02:44:20 +08:00
{
return false ;
}
}
blockSize = 4 * 4 ; // Pixel by pixel block size for 4bpp
widthBlocks = width / 4 ;
heightBlocks = height / 4 ;
break ;
case PVR3TexturePixelFormat : : BGRA8888 :
2014-08-21 15:41:05 +08:00
if ( ! Configuration : : getInstance ( ) - > supportsBGRA8888 ( ) )
2013-07-25 21:35:00 +08:00
{
2014-06-26 02:44:20 +08:00
CCLOG ( " cocos2d: Image. BGRA8888 not supported on this device " ) ;
return false ;
}
default :
blockSize = 1 ;
widthBlocks = width ;
heightBlocks = height ;
break ;
2015-08-14 21:17:51 +08:00
}
2013-07-25 21:35:00 +08:00
2015-08-14 21:17:51 +08:00
// Clamp to minimum number of blocks
if ( widthBlocks < 2 )
2013-07-25 21:35:00 +08:00
{
2015-08-14 21:17:51 +08:00
widthBlocks = 2 ;
2013-07-25 21:35:00 +08:00
}
2015-08-14 21:17:51 +08:00
if ( heightBlocks < 2 )
{
heightBlocks = 2 ;
}
dataSize = widthBlocks * heightBlocks * ( ( blockSize * bpp ) / 8 ) ;
2020-08-31 21:04:47 +08:00
auto packetLength = pixelLen - dataOffset ;
2015-08-14 21:17:51 +08:00
packetLength = packetLength > dataSize ? dataSize : packetLength ;
if ( ! _unpack )
2013-07-25 21:35:00 +08:00
{
2020-08-31 21:04:47 +08:00
_mipmaps [ i ] . address = pixelData + dataOffset ;
2015-08-14 21:17:51 +08:00
_mipmaps [ i ] . len = static_cast < int > ( packetLength ) ;
2013-07-25 21:35:00 +08:00
}
2015-08-14 21:17:51 +08:00
dataOffset + = packetLength ;
2020-08-31 21:04:47 +08:00
CCASSERT ( dataOffset < = pixelLen , " Image: Invalid length " ) ;
2015-08-14 21:17:51 +08:00
width = MAX ( width > > 1 , 1 ) ;
height = MAX ( height > > 1 , 1 ) ;
}
2014-03-20 17:07:28 +08:00
2020-08-31 21:04:47 +08:00
if ( ! _unpack ) {
2020-08-31 23:09:42 +08:00
forwardPixels ( data , dataLen , pixelOffset , ownData ) ;
2020-08-31 21:04:47 +08:00
}
else {
2014-03-20 17:07:28 +08:00
_data = _mipmaps [ 0 ] . address ;
_dataLen = _mipmaps [ 0 ] . len ;
}
2015-08-14 21:17:51 +08:00
return true ;
2013-07-25 21:35:00 +08:00
}
2020-09-01 14:57:18 +08:00
bool Image : : initWithETCData ( uint8_t * data , ssize_t dataLen , bool ownData )
2013-07-25 21:35:00 +08:00
{
2013-08-06 16:14:36 +08:00
const etc1_byte * header = static_cast < const etc1_byte * > ( data ) ;
2013-07-26 17:34:44 +08:00
//check the data
2014-08-21 15:41:05 +08:00
if ( ! etc1_pkm_is_valid ( header ) )
2013-07-26 17:34:44 +08:00
{
return false ;
}
_width = etc1_pkm_get_width ( header ) ;
_height = etc1_pkm_get_height ( header ) ;
2014-08-21 15:41:05 +08:00
if ( 0 = = _width | | 0 = = _height )
2013-07-26 17:34:44 +08:00
{
return false ;
}
2013-07-25 21:35:00 +08:00
2020-08-31 14:11:48 +08:00
// GL_ETC1_RGB8_OES is not available in any desktop GL extension but the compression
// format is forwards compatible so just use the ETC2 format.
backend : : PixelFormat compressedFormat ;
2020-09-01 11:43:03 +08:00
if ( Configuration : : getInstance ( ) - > supportsETC1 ( ) )
compressedFormat = backend : : PixelFormat : : ETC1 ;
2020-08-31 14:11:48 +08:00
else if ( Configuration : : getInstance ( ) - > supportsETC2 ( ) )
compressedFormat = backend : : PixelFormat : : ETC2_RGB ;
else
compressedFormat = backend : : PixelFormat : : NONE ;
2020-08-31 16:31:37 +08:00
if ( compressedFormat ! = backend : : PixelFormat : : NONE )
2013-07-26 17:34:44 +08:00
{
2020-08-31 14:11:48 +08:00
_pixelFormat = compressedFormat ;
2020-08-31 23:09:42 +08:00
forwardPixels ( data , dataLen , ETC_PKM_HEADER_SIZE , ownData ) ;
2013-07-26 17:34:44 +08:00
return true ;
}
else
{
2013-09-24 08:03:28 +08:00
CCLOG ( " cocos2d: Hardware ETC1 decoder not present. Using software decoder " ) ;
2020-08-31 15:32:59 +08:00
_dataLen = _width * _height * 4 ;
2020-09-01 14:57:18 +08:00
_data = static_cast < uint8_t * > ( malloc ( _dataLen ) ) ;
if ( etc2_decode_image ( ETC2_RGB_NO_MIPMAPS , static_cast < const uint8_t * > ( data ) + ETC2_PKM_HEADER_SIZE , static_cast < etc2_byte * > ( _data ) , _width , _height ) = = 0 )
2020-09-01 11:43:03 +08:00
{ // if it is not gles or device do not support ETC1, decode texture by software
// directly decode ETC1_RGB to RGBA8888
2020-09-25 11:07:56 +08:00
_pixelFormat = backend : : PixelFormat : : RGBA8 ;
2020-09-01 11:43:03 +08:00
return true ;
2013-07-26 17:34:44 +08:00
}
2019-11-20 18:19:24 +08:00
2020-09-01 11:43:03 +08:00
// software decode fail, release pixels data
CC_SAFE_FREE ( _data ) ;
_dataLen = 0 ;
return false ;
2013-07-26 17:34:44 +08:00
}
2013-07-25 21:35:00 +08:00
}
2020-09-01 14:57:18 +08:00
bool Image : : initWithETC2Data ( uint8_t * data , ssize_t dataLen , bool ownData )
2020-08-30 17:58:39 +08:00
{
const etc2_byte * header = static_cast < const etc2_byte * > ( data ) ;
2020-11-16 12:21:27 +08:00
do {
//check the data
if ( ! etc2_pkm_is_valid ( header ) )
break ;
2020-08-30 17:58:39 +08:00
2020-11-16 12:21:27 +08:00
_width = etc2_pkm_get_width ( header ) ;
_height = etc2_pkm_get_height ( header ) ;
2020-08-30 17:58:39 +08:00
2020-11-16 12:21:27 +08:00
if ( 0 = = _width | | 0 = = _height )
break ;
2020-08-30 17:58:39 +08:00
2020-11-16 12:21:27 +08:00
etc2_uint32 format = etc2_pkm_get_format ( header ) ;
2020-08-31 14:11:48 +08:00
2020-11-16 12:21:27 +08:00
// We only support ETC2_RGBA_NO_MIPMAPS and ETC2_RGB_NO_MIPMAPS
assert ( format = = ETC2_RGBA_NO_MIPMAPS | | format = = ETC2_RGB_NO_MIPMAPS ) ;
2020-08-30 17:58:39 +08:00
2020-11-16 12:21:27 +08:00
if ( Configuration : : getInstance ( ) - > supportsETC2 ( ) ) {
_pixelFormat = format = = ETC2_RGBA_NO_MIPMAPS ? backend : : PixelFormat : : ETC2_RGBA : backend : : PixelFormat : : ETC2_RGB ;
2020-08-30 17:58:39 +08:00
2020-11-16 12:21:27 +08:00
forwardPixels ( data , dataLen , ETC2_PKM_HEADER_SIZE , ownData ) ;
}
else {
CCLOG ( " cocos2d: Hardware ETC2 decoder not present. Using software decoder " ) ;
// if device do not support ETC2, decode texture by software
// etc2_decode_image always decode to RGBA8888
_dataLen = _width * _height * 4 ;
_data = static_cast < uint8_t * > ( malloc ( _dataLen ) ) ;
if ( UTILS_UNLIKELY ( etc2_decode_image ( format , static_cast < const uint8_t * > ( data ) + ETC2_PKM_HEADER_SIZE , static_cast < etc2_byte * > ( _data ) , _width , _height ) ! = 0 ) )
{
// software decode fail, release pixels data
CC_SAFE_FREE ( _data ) ;
_dataLen = 0 ;
break ;
}
2020-09-25 11:07:56 +08:00
_pixelFormat = backend : : PixelFormat : : RGBA8 ;
2020-08-31 14:11:48 +08:00
}
2020-11-16 12:21:27 +08:00
_hasPremultipliedAlpha = isCompressedImageHavePMA ( CompressedImagePMAFlag : : ETC2 ) ;
return true ;
} while ( false ) ;
return false ;
2020-08-30 17:58:39 +08:00
}
2020-02-05 22:45:19 +08:00
2020-09-01 14:57:18 +08:00
bool Image : : initWithASTCData ( uint8_t * data , ssize_t dataLen , bool ownData )
2020-02-05 22:45:19 +08:00
{
ASTCTexHeader * header = ( ASTCTexHeader * ) data ;
2020-11-16 12:21:27 +08:00
do {
_width = header - > xsize [ 0 ] + 256 * header - > xsize [ 1 ] + 65536 * header - > xsize [ 2 ] ;
_height = header - > ysize [ 0 ] + 256 * header - > ysize [ 1 ] + 65536 * header - > ysize [ 2 ] ;
2020-02-05 22:45:19 +08:00
2020-11-16 12:21:27 +08:00
if ( 0 = = _width | | 0 = = _height )
break ;
2020-02-05 22:45:19 +08:00
2020-11-16 12:21:27 +08:00
uint8_t xdim = header - > blockdim_x ;
uint8_t ydim = header - > blockdim_y ;
2020-02-05 22:45:19 +08:00
2020-11-16 12:21:27 +08:00
if ( xdim < 4 | | ydim < 4 )
2020-02-06 21:54:45 +08:00
{
2020-11-16 12:21:27 +08:00
CCLOG ( " cocos2d: The ASTC block with and height should be >= 4 " ) ;
break ;
2020-02-06 21:54:45 +08:00
}
2020-11-16 12:21:27 +08:00
if ( Configuration : : getInstance ( ) - > supportsASTC ( ) )
{
if ( xdim = = 4 & & ydim = = 4 ) {
_pixelFormat = backend : : PixelFormat : : ASTC4x4 ;
}
else if ( xdim = = 5 & & ydim = = 5 ) {
_pixelFormat = backend : : PixelFormat : : ASTC5x5 ;
}
else if ( xdim = = 6 & & ydim = = 6 ) {
_pixelFormat = backend : : PixelFormat : : ASTC6x6 ;
}
else if ( xdim = = 8 & & ydim = = 5 ) {
_pixelFormat = backend : : PixelFormat : : ASTC8x5 ;
}
else if ( xdim = = 8 & & ydim = = 6 ) {
_pixelFormat = backend : : PixelFormat : : ASTC8x6 ;
}
else if ( xdim = = 8 & & ydim = = 8 )
{
_pixelFormat = backend : : PixelFormat : : ASTC8x8 ;
}
else if ( xdim = = 10 & & ydim = = 5 ) {
_pixelFormat = backend : : PixelFormat : : ASTC10x5 ;
}
forwardPixels ( data , dataLen , ASTC_HEAD_SIZE , ownData ) ;
2020-09-25 11:07:56 +08:00
}
2020-11-16 12:21:27 +08:00
else
{
CCLOG ( " cocos2d: Hardware ASTC decoder not present. Using software decoder " ) ;
2020-08-31 21:04:47 +08:00
2020-11-16 12:21:27 +08:00
_dataLen = _width * _height * 4 ;
_data = static_cast < uint8_t * > ( malloc ( _dataLen ) ) ;
if ( UTILS_UNLIKELY ( decompress_astc ( static_cast < const uint8_t * > ( data ) + ASTC_HEAD_SIZE , _data , _width , _height , xdim , ydim , _dataLen ) ! = 0 ) ) {
CC_SAFE_FREE ( _data ) ;
_dataLen = 0 ;
break ;
}
2020-02-11 23:54:17 +08:00
2020-09-25 11:07:56 +08:00
_pixelFormat = backend : : PixelFormat : : RGBA8 ;
2020-02-11 23:54:17 +08:00
}
2020-02-05 22:45:19 +08:00
2020-11-16 12:21:27 +08:00
_hasPremultipliedAlpha = isCompressedImageHavePMA ( CompressedImagePMAFlag : : ASTC ) ;
return true ;
} while ( false ) ;
return false ;
2020-02-05 22:45:19 +08:00
}
2020-09-01 14:57:18 +08:00
bool Image : : initWithS3TCData ( uint8_t * data , ssize_t dataLen , bool ownData )
2013-08-06 11:19:45 +08:00
{
2013-08-07 16:39:05 +08:00
const uint32_t FOURCC_DXT1 = makeFourCC ( ' D ' , ' X ' , ' T ' , ' 1 ' ) ;
const uint32_t FOURCC_DXT3 = makeFourCC ( ' D ' , ' X ' , ' T ' , ' 3 ' ) ;
const uint32_t FOURCC_DXT5 = makeFourCC ( ' D ' , ' X ' , ' T ' , ' 5 ' ) ;
2013-08-06 11:19:45 +08:00
/* load the .dds file */
2013-08-09 12:54:05 +08:00
S3TCTexHeader * header = ( S3TCTexHeader * ) data ;
_width = header - > ddsd . width ;
_height = header - > ddsd . height ;
2014-02-20 13:48:09 +08:00
_numberOfMipmaps = MAX ( 1 , header - > ddsd . DUMMYUNIONNAMEN2 . mipMapCount ) ; //if dds header reports 0 mipmaps, set to 1 to force correct software decoding (if needed).
2013-08-16 11:02:44 +08:00
_dataLen = 0 ;
2013-08-09 12:54:05 +08:00
int blockSize = ( FOURCC_DXT1 = = header - > ddsd . DUMMYUNIONNAMEN4 . ddpfPixelFormat . fourCC ) ? 8 : 16 ;
2013-08-07 16:39:05 +08:00
2014-02-20 13:48:09 +08:00
/* calculate the dataLen */
2013-08-06 11:19:45 +08:00
int width = _width ;
int height = _height ;
2020-08-31 21:04:47 +08:00
const int pixelOffset = sizeof ( S3TCTexHeader ) ;
2020-09-01 14:57:18 +08:00
uint8_t * pixelData = data + pixelOffset ;
2013-08-06 11:19:45 +08:00
2020-08-31 21:04:47 +08:00
bool hardware = Configuration : : getInstance ( ) - > supportsS3TC ( ) ;
2014-02-19 06:42:41 +08:00
/* if hardware supports s3tc, set pixelformat before loading mipmaps, to support non-mipmapped textures */
2020-08-31 21:04:47 +08:00
if ( hardware )
2015-09-22 16:08:23 +08:00
{ //decode texture through hardware
2014-02-19 06:42:41 +08:00
if ( FOURCC_DXT1 = = header - > ddsd . DUMMYUNIONNAMEN4 . ddpfPixelFormat . fourCC )
{
2019-06-03 09:39:51 +08:00
_pixelFormat = backend : : PixelFormat : : S3TC_DXT1 ;
2014-02-19 06:42:41 +08:00
}
else if ( FOURCC_DXT3 = = header - > ddsd . DUMMYUNIONNAMEN4 . ddpfPixelFormat . fourCC )
{
2019-06-03 09:39:51 +08:00
_pixelFormat = backend : : PixelFormat : : S3TC_DXT3 ;
2014-02-19 06:42:41 +08:00
}
else if ( FOURCC_DXT5 = = header - > ddsd . DUMMYUNIONNAMEN4 . ddpfPixelFormat . fourCC )
{
2019-06-03 09:39:51 +08:00
_pixelFormat = backend : : PixelFormat : : S3TC_DXT5 ;
2014-02-19 06:42:41 +08:00
}
2014-02-20 13:48:09 +08:00
} else { //will software decode
2020-09-25 11:07:56 +08:00
_pixelFormat = backend : : PixelFormat : : RGBA8 ;
2020-08-31 21:04:47 +08:00
// prepare data for software decompress
for ( int i = 0 ; i < _numberOfMipmaps & & ( width | | height ) ; + + i )
{
if ( width = = 0 ) width = 1 ;
if ( height = = 0 ) height = 1 ;
_dataLen + = ( height * width * 4 ) ;
width > > = 1 ;
height > > = 1 ;
}
2020-09-01 14:57:18 +08:00
_data = static_cast < uint8_t * > ( malloc ( _dataLen ) ) ;
2014-02-19 06:42:41 +08:00
}
2013-08-06 11:19:45 +08:00
/* load the mipmaps */
2013-08-09 14:37:47 +08:00
int encodeOffset = 0 ;
int decodeOffset = 0 ;
2013-08-09 12:54:05 +08:00
width = _width ; height = _height ;
2013-08-06 11:19:45 +08:00
2013-09-08 11:26:38 +08:00
for ( int i = 0 ; i < _numberOfMipmaps & & ( width | | height ) ; + + i )
2013-08-06 11:19:45 +08:00
{
if ( width = = 0 ) width = 1 ;
if ( height = = 0 ) height = 1 ;
int size = ( ( width + 3 ) / 4 ) * ( ( height + 3 ) / 4 ) * blockSize ;
if ( Configuration : : getInstance ( ) - > supportsS3TC ( ) )
2015-09-22 16:08:23 +08:00
{ //decode texture through hardware
2020-09-01 14:57:18 +08:00
_mipmaps [ i ] . address = ( uint8_t * ) pixelData + encodeOffset ;
2013-08-06 11:19:45 +08:00
_mipmaps [ i ] . len = size ;
}
else
2013-08-07 16:39:05 +08:00
{ //if it is not gles or device do not support S3TC, decode texture by software
2013-09-24 08:03:28 +08:00
CCLOG ( " cocos2d: Hardware S3TC decoder not present. Using software decoder " ) ;
2013-08-06 11:19:45 +08:00
int bytePerPixel = 4 ;
unsigned int stride = width * bytePerPixel ;
2020-09-01 14:57:18 +08:00
std : : vector < uint8_t > decodeImageData ( stride * height ) ;
2013-08-09 12:54:05 +08:00
if ( FOURCC_DXT1 = = header - > ddsd . DUMMYUNIONNAMEN4 . ddpfPixelFormat . fourCC )
2013-08-06 11:19:45 +08:00
{
2013-08-09 14:37:47 +08:00
s3tc_decode ( pixelData + encodeOffset , & decodeImageData [ 0 ] , width , height , S3TCDecodeFlag : : DXT1 ) ;
2013-08-06 11:19:45 +08:00
}
2013-08-09 12:54:05 +08:00
else if ( FOURCC_DXT3 = = header - > ddsd . DUMMYUNIONNAMEN4 . ddpfPixelFormat . fourCC )
2013-08-06 11:19:45 +08:00
{
2013-08-09 14:37:47 +08:00
s3tc_decode ( pixelData + encodeOffset , & decodeImageData [ 0 ] , width , height , S3TCDecodeFlag : : DXT3 ) ;
2013-08-06 11:19:45 +08:00
}
2013-08-09 12:54:05 +08:00
else if ( FOURCC_DXT5 = = header - > ddsd . DUMMYUNIONNAMEN4 . ddpfPixelFormat . fourCC )
2013-08-06 11:19:45 +08:00
{
2013-08-09 14:37:47 +08:00
s3tc_decode ( pixelData + encodeOffset , & decodeImageData [ 0 ] , width , height , S3TCDecodeFlag : : DXT5 ) ;
2013-08-06 11:19:45 +08:00
}
2020-09-01 14:57:18 +08:00
_mipmaps [ i ] . address = ( uint8_t * ) _data + decodeOffset ;
2013-08-06 11:19:45 +08:00
_mipmaps [ i ] . len = ( stride * height ) ;
memcpy ( ( void * ) _mipmaps [ i ] . address , ( void * ) & decodeImageData [ 0 ] , _mipmaps [ i ] . len ) ;
2013-08-09 14:37:47 +08:00
decodeOffset + = stride * height ;
2013-08-06 11:19:45 +08:00
}
2020-08-31 21:04:47 +08:00
2013-08-09 15:00:26 +08:00
encodeOffset + = size ;
2013-08-06 11:19:45 +08:00
width > > = 1 ;
height > > = 1 ;
}
2013-08-07 16:39:05 +08:00
/* end load the mipmaps */
2020-08-31 21:04:47 +08:00
if ( hardware ) {
2020-08-31 23:09:42 +08:00
forwardPixels ( data , dataLen , pixelOffset , ownData ) ;
2020-08-31 21:04:47 +08:00
}
2013-08-06 11:19:45 +08:00
return true ;
}
2013-08-16 14:27:13 +08:00
2020-09-01 14:57:18 +08:00
bool Image : : initWithATITCData ( uint8_t * data , ssize_t dataLen , bool ownData )
2013-08-16 11:02:44 +08:00
{
/* load the .ktx file */
ATITCTexHeader * header = ( ATITCTexHeader * ) data ;
_width = header - > pixelWidth ;
_height = header - > pixelHeight ;
_numberOfMipmaps = header - > numberOfMipmapLevels ;
int blockSize = 0 ;
switch ( header - > glInternalFormat )
{
2013-08-16 14:03:30 +08:00
case CC_GL_ATC_RGB_AMD :
2013-08-16 11:02:44 +08:00
blockSize = 8 ;
break ;
2013-08-16 14:03:30 +08:00
case CC_GL_ATC_RGBA_EXPLICIT_ALPHA_AMD :
2013-08-16 11:02:44 +08:00
blockSize = 16 ;
break ;
2013-08-16 14:03:30 +08:00
case CC_GL_ATC_RGBA_INTERPOLATED_ALPHA_AMD :
2013-08-16 11:02:44 +08:00
blockSize = 16 ;
break ;
default :
break ;
}
/* pixelData point to the compressed data address */
2020-08-31 21:04:47 +08:00
int pixelOffset = sizeof ( ATITCTexHeader ) + header - > bytesOfKeyValueData + 4 ;
2020-09-01 14:57:18 +08:00
uint8_t * pixelData = ( uint8_t * ) data + pixelOffset ;
2013-08-16 11:02:44 +08:00
2015-09-22 16:08:23 +08:00
/* calculate the dataLen */
2013-08-16 11:02:44 +08:00
int width = _width ;
int height = _height ;
2020-08-31 21:04:47 +08:00
bool hardware = Configuration : : getInstance ( ) - > supportsATITC ( ) ;
if ( hardware ) //compressed data length
2013-08-16 11:02:44 +08:00
{
2020-08-31 21:04:47 +08:00
CCLOG ( " this is atitc H decode " ) ;
switch ( header - > glInternalFormat )
{
case CC_GL_ATC_RGB_AMD :
_pixelFormat = backend : : PixelFormat : : ATC_RGB ;
break ;
case CC_GL_ATC_RGBA_EXPLICIT_ALPHA_AMD :
_pixelFormat = backend : : PixelFormat : : ATC_EXPLICIT_ALPHA ;
break ;
case CC_GL_ATC_RGBA_INTERPOLATED_ALPHA_AMD :
_pixelFormat = backend : : PixelFormat : : ATC_INTERPOLATED_ALPHA ;
break ;
default :
break ;
}
2013-08-16 11:02:44 +08:00
}
2020-08-31 21:04:47 +08:00
else // decompressed data length
{ /* if it is not gles or device do not support ATITC, decode texture by software */
CCLOG ( " cocos2d: Hardware ATITC decoder not present. Using software decoder " ) ;
2020-09-25 11:07:56 +08:00
_pixelFormat = backend : : PixelFormat : : RGBA8 ;
2020-08-31 21:04:47 +08:00
2013-09-08 11:26:38 +08:00
for ( int i = 0 ; i < _numberOfMipmaps & & ( width | | height ) ; + + i )
2013-08-16 11:02:44 +08:00
{
if ( width = = 0 ) width = 1 ;
if ( height = = 0 ) height = 1 ;
_dataLen + = ( height * width * 4 ) ;
width > > = 1 ;
height > > = 1 ;
}
2020-09-01 14:57:18 +08:00
_data = static_cast < uint8_t * > ( malloc ( _dataLen ) ) ;
2013-08-16 11:02:44 +08:00
}
/* load the mipmaps */
int encodeOffset = 0 ;
int decodeOffset = 0 ;
width = _width ; height = _height ;
2013-09-08 11:26:38 +08:00
for ( int i = 0 ; i < _numberOfMipmaps & & ( width | | height ) ; + + i )
2013-08-16 11:02:44 +08:00
{
if ( width = = 0 ) width = 1 ;
if ( height = = 0 ) height = 1 ;
int size = ( ( width + 3 ) / 4 ) * ( ( height + 3 ) / 4 ) * blockSize ;
2020-08-31 21:04:47 +08:00
if ( hardware )
2013-08-16 11:02:44 +08:00
{
2015-09-22 16:08:23 +08:00
/* decode texture through hardware */
2020-09-01 14:57:18 +08:00
_mipmaps [ i ] . address = ( uint8_t * ) pixelData + encodeOffset ;
2013-08-16 11:02:44 +08:00
_mipmaps [ i ] . len = size ;
}
else
{
int bytePerPixel = 4 ;
unsigned int stride = width * bytePerPixel ;
2020-08-31 21:04:47 +08:00
2020-09-01 14:57:18 +08:00
std : : vector < uint8_t > decodeImageData ( stride * height ) ;
2013-08-16 11:02:44 +08:00
switch ( header - > glInternalFormat )
{
2013-08-16 14:03:30 +08:00
case CC_GL_ATC_RGB_AMD :
2013-08-16 11:02:44 +08:00
atitc_decode ( pixelData + encodeOffset , & decodeImageData [ 0 ] , width , height , ATITCDecodeFlag : : ATC_RGB ) ;
break ;
2013-08-16 14:03:30 +08:00
case CC_GL_ATC_RGBA_EXPLICIT_ALPHA_AMD :
2013-08-16 11:02:44 +08:00
atitc_decode ( pixelData + encodeOffset , & decodeImageData [ 0 ] , width , height , ATITCDecodeFlag : : ATC_EXPLICIT_ALPHA ) ;
break ;
2013-08-16 14:03:30 +08:00
case CC_GL_ATC_RGBA_INTERPOLATED_ALPHA_AMD :
2013-08-16 11:02:44 +08:00
atitc_decode ( pixelData + encodeOffset , & decodeImageData [ 0 ] , width , height , ATITCDecodeFlag : : ATC_INTERPOLATED_ALPHA ) ;
break ;
default :
break ;
}
2020-09-01 14:57:18 +08:00
_mipmaps [ i ] . address = ( uint8_t * ) _data + decodeOffset ;
2013-08-16 11:02:44 +08:00
_mipmaps [ i ] . len = ( stride * height ) ;
memcpy ( ( void * ) _mipmaps [ i ] . address , ( void * ) & decodeImageData [ 0 ] , _mipmaps [ i ] . len ) ;
decodeOffset + = stride * height ;
}
encodeOffset + = ( size + 4 ) ;
width > > = 1 ;
height > > = 1 ;
}
/* end load the mipmaps */
2020-08-31 21:04:47 +08:00
if ( hardware ) {
2020-08-31 23:09:42 +08:00
forwardPixels ( data , dataLen , pixelOffset , ownData ) ;
2020-08-31 21:04:47 +08:00
}
2013-08-16 11:02:44 +08:00
return true ;
}
2020-09-01 14:57:18 +08:00
bool Image : : initWithPVRData ( uint8_t * data , ssize_t dataLen , bool ownData )
2013-07-25 21:35:00 +08:00
{
2020-08-31 23:09:42 +08:00
return initWithPVRv2Data ( data , dataLen , ownData ) | | initWithPVRv3Data ( data , dataLen , ownData ) ;
}
2020-09-01 14:57:18 +08:00
void Image : : forwardPixels ( uint8_t * data , ssize_t dataLen , int offset , bool ownData )
2020-08-31 23:09:42 +08:00
{
if ( ownData ) {
_data = data ;
_dataLen = dataLen ;
_offset = offset ;
}
else {
_dataLen = dataLen - offset ;
2020-09-01 14:57:18 +08:00
_data = ( uint8_t * ) malloc ( _dataLen ) ;
2020-08-31 23:09:42 +08:00
memcpy ( _data , data + offset , _dataLen ) ;
}
2013-07-25 21:35:00 +08:00
}
2013-08-01 15:53:52 +08:00
# if (CC_TARGET_PLATFORM != CC_PLATFORM_IOS)
2014-08-21 15:41:05 +08:00
bool Image : : saveToFile ( const std : : string & filename , bool isToRGB )
2012-04-19 14:35:52 +08:00
{
2020-09-25 11:07:56 +08:00
//only support for backend::PixelFormat::RGB8 or backend::PixelFormat::RGBA8 uncompressed data
if ( isCompressed ( ) | | ( _pixelFormat ! = backend : : PixelFormat : : RGB8 & & _pixelFormat ! = backend : : PixelFormat : : RGBA8 ) )
2013-07-26 17:34:44 +08:00
{
2020-09-25 11:07:56 +08:00
CCLOG ( " cocos2d: Image: saveToFile is only support for backend::PixelFormat::RGB8 or backend::PixelFormat::RGBA8 uncompressed data for now " ) ;
2013-07-26 17:34:44 +08:00
return false ;
}
2015-08-06 03:21:16 +08:00
std : : string fileExtension = FileUtils : : getInstance ( ) - > getFileExtension ( filename ) ;
2012-04-19 14:35:52 +08:00
2015-08-06 03:21:16 +08:00
if ( fileExtension = = " .png " )
2012-04-19 14:35:52 +08:00
{
2015-08-06 03:21:16 +08:00
return saveImageToPNG ( filename , isToRGB ) ;
}
else if ( fileExtension = = " .jpg " )
{
return saveImageToJPG ( filename ) ;
}
else
{
CCLOG ( " cocos2d: Image: saveToFile no support file extension(only .png or .jpg) for file: %s " , filename . c_str ( ) ) ;
return false ;
}
2012-04-19 14:35:52 +08:00
}
2013-08-01 15:53:52 +08:00
# endif
2012-04-19 14:35:52 +08:00
2013-11-15 09:19:16 +08:00
bool Image : : saveImageToPNG ( const std : : string & filePath , bool isToRGB )
2012-04-19 14:35:52 +08:00
{
metal support for cocos2d-x (#19305)
* remove deprecated files
* remove some deprecated codes
* remove more deprecated codes
* remove ui deprecated codes
* remove more deprecated codes
* remove deprecated codes in ccmenuitem
* remove more deprecated codes in ui
* remove more deprecated codes in ui
* remove more deprecated codes in ui
* remove more deprecated codes
* remove more deprecated codes
* remove more deprecated codes
* remove vr related codes and ignore some modules
* remove allocator
* remove some config
* 【Feature】add back-end project file
* [Feature] add back-end file
* add pipeline descriptor and shader cache
* [Feature] support sprite for backend
* [Feature] remove unneeded code
* [Feature] according to es2.0 spec, you must use clamp-to-edge as texture wrap mode, and no mipmapping for non-power-of-two texture
* [Feature] set texture wrap mode to clamp-to-edge, and no mipmapping for non-power-of-two texture
* [Feature] remove macro define to .cpp file
* [Feature] add log info
* [Feature] add PipelineDescriptor for TriangleCommand
* [Feature] add PipelineDescriptor object as member of TriangleCommand
* [Feature] add getPipelineDescriptor method
* add renderbackend
* complete pipeline descriptor
* [Feature] add viewport in RenderCommand
* set viewport when rendrering
* [Feature] occur error when using RendererBackend, to be fixed.
* a workaround to fix black screen on macOS 10.14 (#19090)
* add rendererbackend init function
* fix typo
* [Feature] modify testFile
* [BugFix] modify shader path
* [Feature] set default viewport
* fix projection
* [Feature] modify log info
* [BugFix] change viewport data type to int
* [BugFix] add BindGroup to PipelienDescriptor
* [BugFix] change a_position to vec3 in sprite.vert
* [BugFix] set vertexLayout according to V3F_C4B_T2F structure
* [Feature] revert a_position to vec4
* [Feature] renderer should not use gl codes directly
* [Feature] it's better not use default value parameter
* fix depth test setting
* rendererbackend -> renderer
* clear color and depth at begin
* add metal backend
* metal support normalized attribute
* simplify codes
* update external
* add render pass desctriptor in pipeline descriptor
* fix warnings
* fix crash and memeory leak
* refactor Texture2D
* put pipeline descriptor into render command
* simplify codes
* [Feature] update Sprite
* fix crash when closing app
* [Feature] update SpriteBatchNode and TextureAtlas
* support render texture(not finish)
* [Feature] remove unused code
* make tests work on mac
* fix download-deps path error
* make tests work on iOS
* [Feature] support ttf under normal label effect
* refactor triangle command processing
* let renderer handle more common commands
* refactor backend
* make render texture work
* [Feature] refactor backend for GL
* [Feature]Renaming to make it easy to understand
* [Feature] change warp mode to CLAMP_TO_EDGE
* fix ghost
* simplify visit render queue logic
* support progress timer without rial mode
* support partcile system
* Feature/update label (#149)
* [BugFix] fix compile error
* [Feature] support outline effect in ios
* [Feature] add shader file
* [BugFix] fix begin and end RenderPass
* [Feature] update CustomCommand
* [Feature] revert project.pbxproj
* [Feature] simplify codes
* [BugFix] pack AI88 to RGBA8888 only when outline enable
* [Feature] support shadow effect in Label
* [Feature] support BMFont
* [Feature] support glow effect
* [Feature] simplify shader files
* LabelAtlas work
* handle blend function correctly
* support tile map
* don't share buffer in metal
* alloc buffer size as needed
* support more tilemap
* Merge branch 'minggo/metal-support' into feature/updateLabel
* minggo/metal-support:
support tile map
handle blend function correctly
LabelAtlas work
Feature/update label (#149)
support partcile system
# Conflicts:
# cocos/2d/CCLabel.cpp
# cocos/2d/CCSprite.cpp
# cocos/2d/CCSpriteBatchNode.cpp
# cocos/renderer/CCQuadCommand.cpp
# cocos/renderer/CCQuadCommand.h
* render texture work without saving file
* use global viewport
* grid3d works
* remove grabber
* tiled3d works
* [BugFix] fix label bug
* [Feature] add updateSubData for buffer
* [Feature] remove setVertexCount
* support depth test
* add callback command
* [Feature] add UITest
* [Feature] update UITest
* [Feature] remove unneeded codes
* fix custom command issue
* fix layer color blend issue
* [BugFix] fix iOS compile error
* [Feature] remove unneeded codes
* [Feature] fix updateVertexBuffer
* layerradial works
* add draw test back
* fix batch issue
* fix compiling error
* [BugFix] support ETC1
* [BugFix] get the correct pipelineDescriptor
* [BugFix] skip draw when backendTexture nullptr
* clipping node support
* [Feature] add shader files
* fix stencil issue in metal
* [Feature] update UILayoutTest
* [BugFix] skip drawing when vertexCount is zero
* refactor renderer
* add set global z order for stencil manager commands
* fix warnings caused by type
* remove viewport in render command
* [Feature] fix warnings caused by type
* [BugFix] clear vertexCount and indexCount for CustomComand when needed
* [Feature] update clear for CustomCommand
* ios use metal
* fix viewport issue
* fix LayerColorGradient crash
* [cmake] transport to android and windows (#160)
* save point 1
* compile on windows
* run on android
* revert useless change
* android set CC_ENABLE_CACHE_TEXTURE_DATA to 1
* add initGlew
* fix android crash
* add TODO new-renderer
* review update
* revert onGLFWWindowPosCallback
* fix android compiling error
* Impl progress radial (#162)
* progresstimer add radial impl
* default drawType to element
* dec invoke times of createVertexBuffer (#163)
* support depth/stencil format for gl backend
* simplify progress timer codes
* support motionstreak, effect is wrong
* fix motionstreak issue
* [Feature] update Scissor Test (#161)
* [Feature] update Scissor Test
* [Feature] update ScissorTest
* [Feature] rename function
* [Feature] get constant reference if needed
* [Feature] show render status (#164)
* improve performance
* fix depth state
* fill error that triangle vertex/index number bigger than buffer
* fix compiline error in release mode
* fix buffer conflict between CPU and GPU on iOS/macOS
* Renderer refactor (#165)
* use one vertes/index buffer with opengl
* fix error on windows
* custom command support index format config
* CCLayer: compact vertex data structure
* update comment
* fix doc
* support fast tilemap
* pass index format instead
* fix some wrong effect
* fix render texture error
* fix texture per-element size
* fix texture format error
* BlendFunc type refactor, GLenum -> backend::BlendFactor (#167)
* BlendFunc use backend::BlendFactor as inner field
* update comments
* use int to replace GLenum
* update xcode project fiel
* rename to GLBlendConst
* add ccConstants.h
* update xcode project file
* update copyright
* remove primitive command
* remove CCPrimitive.cpp/.h
* remove deprecated files
* remove unneeded files
* remove multiple view support
* remove multiple view support
* remove the usage of frame buffer in camera
* director don't use frame buffer
* remove FrameBuffer
* remove BatchCommand
* add some api reference
* add physics2d back
* fix crash when close app on mac
* improve render texture
* fix rendertexture issue
* fix rendertexture issue
* simplify codes
* CMake support for mac & ios (#169)
* update cmake
* fix compile error
* update 3rd libs version
* remove CCThread.h/.cpp
* remove ccthread
* use audio engine to implement simple audio engine
* remove unneeded codes
* remove deprecated codes
* remove winrt macro
* remove CC_USE_WIC
* set partcile blend function in more elegant way
* remove unneeded codes
* remove unneeded codes
* cmake works on windows
* update project setting
* improve performance
* GLFloat -> float
* sync v3 cmake improvements into metal-support (#172)
* pick: modern cmake, compile definitions improvement (#19139)
* modern cmake, use target_compile_definitions partly
* simplify macro define, remove USE_*
* modern cmake, macro define
* add physics 2d macro define into ccConfig.h
* remove USE_CHIPMUNK macro in build.gradle
* remove CocosSelectModule.cmake
* shrink useless define
* simplify compile options config, re-add if necessary
* update external for tmp CI test
* un-quote target_compile_options value
* add "-g" parameter only when debug mode
* keep single build type when generator Xcode & VS projecy
* update external for tmp CI tes
* add static_cast<char>(-1), fix -Wc++11-narrowing
* simplify win32 compile define
* not modify code, only improve compile options
# Conflicts:
# .gitignore
# cmake/Modules/CocosConfigDepend.cmake
# cocos/CMakeLists.txt
# external/config.json
# tests/cpp-tests/CMakeLists.txt
* modern cmake, improve cmake_compiler_flags (#19145)
* cmake_compiler_flags
* Fix typo
* Fix typo2
* Remove chanages from Android.mk
* correct lua template cmake build (#19149)
* don't add -Wno-deprecated into jsb target
* correct lua template cmake build
* fix win32 lua template compile error
* prevent cmake in-source-build friendly (#19151)
* pick: Copy resources to "Resources/" on win32 like in linux configuration
* add "/Z7" for cpp-tests on windows
* [cmake] fix iOS xcode property setting failed (#19208)
* fix iOS xcode property setting failed
* use search_depend_libs_recursive at dlls collect
* fix typo
* [cmake] add find_host_library into iOS toolchain file (#19230)
* pick: [lua android] use luajit & template cmake update (#19239)
* increase cmake stability , remove tests/CMakeLists.txt (#19261)
* cmake win32 Precompiled header (#19273)
* Precompiled header
* Fix
* Precompiled header for cocos
* Precompiled header jscocos2d
* Fix for COCOS2D_DEBUG is always 1 on Android (#19291)
Related #19289
* little build fix, tests cpp-tests works on mac
* sync v3 build related codes into metal-support (#173)
* strict initialization for std::array
* remove proj.win32 project configs
* modern cmake, cmake_cleanup_remove_unused_variables (#19146)
* Switch travis CI to xenial (#19207)
* Switch travis CI to xenial
* Remove language: android
* Set language: cpp
* Fix java problem
* Update sdkmanager
* Fix sdkmanger
* next sdkmanager fix
* Remove xenial from android
* revert to sdk-tools-{system}-3859397
* Remove linux cmake install
* Update before-install.sh
* Update .travis.yml
* Simplify install-deps-linux.sh, tested on Ubuntu 16.04 (#19212)
* Simplify install-deps-linux.sh
* Cleanup
* pick: install ninja
* update cocos2d-console submodule
* for metal-support alpha release, we only test cpp
* add HelloCpp into project(Cocos2d-x) for tmp test
* update extenal metal-support-4
* update uniform setting
* [Feature] update BindGroup
* [Feature] empty-test
* [Feature] cpp-test
* [Feature] fix GL compiler error
* [Feature] fix GL crash
* [Feature] empty-test
* [Feature] cpp-tests
* [feature] improve frameRate
* [feature] fix opengl compile error
* [feature] fix opengl compile error
* [BugFix] fix compute maxLocation error
* [Feature] update setting unifrom
* [Feature] fix namespace
* [Feature] remove unneeded code
* [Bugfix] fix project file
* [Feature] update review
* [texture2d] impl texture format support (#175)
* texture update
* update
* update texture
* commit
* compile on windows
* ddd
* rename
* rename methods
* no crash
* save gl
* save
* save
* rename
* move out pixel format convert functions
* metal crash
* update
* update android
* support gles compressed texture format
* support more compress format
* add more conversion methods
* ss
* save
* update conversion methods
* add PVRTC format support
* reformat
* add marco linux
* fix GL marcro
* pvrtc supported only by ios 8.0+
* remove unused cmake
* revert change
* refactor Texture2D::initWithData
* fix conversion log
* refactor Texture2D::initWithData
* remove some OpenGL constants for PVRTC
* add todo
* fix typo
* AutoTest works on mac/iOS by disable part cases, sync v3 bug fix (#174)
* review cpp-tests, and fix part issues on start auto test
* sync png format fix: Node:Particle3D abnormal texture effects #19204
* fix cpp-tests SpritePolygon crash, wrong png format (#19170)
* fix wrong png convert format from sRGB to Gray
* erase plist index if all frames was erased
* test_A8.png have I8 format, fix it
* [CCSpriteCache] allow re-add plist & add testcase (#19175)
* allow re-add plist & add testcase
* remove comments/rename method/update testcase
* fix isSpriteFramesWithFileLoaded & add testcase
* remove used variable
* remove unused variable
* fix double free issues when js/lua-tests exit on iOS (#19236)
* disable part cases, AutoTest works without crash on mac
* update cocos2dx files json, to test cocos new next
* fix spritecache plist parsing issue (#19269)
* [linux] Fix FileUtils::getContents with folder (#19157)
* fix FileUtils::getContents on linux/mac
* use stat.st_mode
* simplify
* [CCFileUtils] win32 getFileSize (#19176)
* win32 getFileSize
* fix stat
* [cpp test-Android]20:FileUtils/2 change title (#19197)
* sync #19200
* sync #19231
* [android lua] improve performance of lua loader (#19234)
* [lua] improve performance of lua loader
* remove cache fix
* Revert "fix spritecache plist parsing issue (#19269)"
This reverts commit f3a85ece4307a7b90816c34489d1ed2c8fd11baf.
* remove win32 project files ref in template.json
* add metal framework lnk ref into cpp template
* test on iOS, and disable part cases
* alBufferData instead of alBufferDataStatic for small audio file on Apple (#19227)
* changes AudioCache to use alBufferData instead of alBufferDataStatic
(also makes test 19 faster to trigger openal bugs faster)
The original problem: CrashIfClientProvidedBogusAudioBufferList
https://github.com/cocos2d/cocos2d-x/issues/18948
is not happening anymore, but there's still a not very frequent issue
that makes OpenAL crash with a call stack like this.
AudioCache::readDataTask > alBufferData > CleanUpDeadBufferList
It happes more frequently when the device is "cold", which means after
half an hour of not using the device (locked).
I could not find the actual source code for iOS OpenAL, so I used the
macOS versions:
https://opensource.apple.com/source/OpenAL/OpenAL-48.7/Source/OpenAL/oalImp.cpp.auto.html
They seem to use CAGuard.h to make sure the dead buffer list
has no threading issues. I'm worried because the CAGuard code I found
has macos and win32 define but no iOS, so I'm not sure. I guess the
iOS version is different and has the guard.
I could not find a place in the code that's unprotected by the locks
except the InitializeBufferMap() which should not be called more than
once from cocos, and there's a workaround in AudioEngine-impl for it.
I reduced the occurence of the CleanUpDeadBufferList crash by moving
the guard in ~AudioCache to cover the alDeleteBuffers call.
* remove hack method "setTimeout" on audio
* AutoTest works on iOS
* support set ios deployment target for root project
* enable all texture2d cases, since Jiang have fixed
* add CCTextureUtils to xcode project file (#176)
* add leak cases for SpriteFrameCache (#177)
* re-add SpriteFrameCache cases
* update template file json
* Update SpriteFrameCacheTest.cpp
* fix compiling error
2019-01-18 15:08:25 +08:00
# if CC_USE_PNG
2014-08-21 15:41:05 +08:00
bool ret = false ;
2015-04-16 00:19:38 +08:00
do
2012-04-19 14:35:52 +08:00
{
png_structp png_ptr ;
png_infop info_ptr ;
png_bytep * row_pointers ;
2021-04-22 01:15:49 +08:00
auto * outStream = FileUtils : : getInstance ( ) - > openFileStream ( filePath , FileStream : : Mode : : WRITE ) ;
2012-04-19 14:35:52 +08:00
2021-04-24 07:22:27 +08:00
CC_BREAK_IF ( nullptr = = outStream ) ;
2012-04-19 14:35:52 +08:00
2014-02-13 16:22:41 +08:00
png_ptr = png_create_write_struct ( PNG_LIBPNG_VER_STRING , nullptr , nullptr , nullptr ) ;
2012-04-19 14:35:52 +08:00
2014-02-13 16:22:41 +08:00
if ( nullptr = = png_ptr )
2012-04-19 14:35:52 +08:00
{
2021-04-22 01:15:49 +08:00
delete outStream ;
2012-04-19 14:35:52 +08:00
break ;
}
info_ptr = png_create_info_struct ( png_ptr ) ;
2014-02-13 16:22:41 +08:00
if ( nullptr = = info_ptr )
2012-04-19 14:35:52 +08:00
{
2021-04-22 01:15:49 +08:00
delete outStream ;
2014-02-13 16:22:41 +08:00
png_destroy_write_struct ( & png_ptr , nullptr ) ;
2012-04-19 14:35:52 +08:00
break ;
}
if ( setjmp ( png_jmpbuf ( png_ptr ) ) )
{
2021-04-22 01:15:49 +08:00
delete outStream ;
2012-04-19 14:35:52 +08:00
png_destroy_write_struct ( & png_ptr , & info_ptr ) ;
break ;
}
2021-04-22 01:15:49 +08:00
//png_init_io(png_ptr, outStream);
png_set_write_fn ( png_ptr , outStream , pngWriteCallback , nullptr ) ;
2012-04-19 14:35:52 +08:00
2013-08-06 16:14:36 +08:00
if ( ! isToRGB & & hasAlpha ( ) )
2012-04-19 14:35:52 +08:00
{
2013-06-15 14:03:30 +08:00
png_set_IHDR ( png_ptr , info_ptr , _width , _height , 8 , PNG_COLOR_TYPE_RGB_ALPHA ,
2012-04-19 14:35:52 +08:00
PNG_INTERLACE_NONE , PNG_COMPRESSION_TYPE_BASE , PNG_FILTER_TYPE_BASE ) ;
}
else
{
2013-06-15 14:03:30 +08:00
png_set_IHDR ( png_ptr , info_ptr , _width , _height , 8 , PNG_COLOR_TYPE_RGB ,
2012-04-19 14:35:52 +08:00
PNG_INTERLACE_NONE , PNG_COMPRESSION_TYPE_BASE , PNG_FILTER_TYPE_BASE ) ;
}
2017-10-26 11:39:50 +08:00
2012-04-19 14:35:52 +08:00
png_write_info ( png_ptr , info_ptr ) ;
png_set_packing ( png_ptr ) ;
2013-06-15 14:03:30 +08:00
row_pointers = ( png_bytep * ) malloc ( _height * sizeof ( png_bytep ) ) ;
2014-02-13 16:22:41 +08:00
if ( row_pointers = = nullptr )
2012-04-19 14:35:52 +08:00
{
2021-04-22 01:15:49 +08:00
delete outStream ;
2012-04-19 14:35:52 +08:00
png_destroy_write_struct ( & png_ptr , & info_ptr ) ;
break ;
}
2014-02-13 16:22:41 +08:00
if ( ! hasAlpha ( ) )
2012-04-19 14:35:52 +08:00
{
2013-06-15 14:03:30 +08:00
for ( int i = 0 ; i < ( int ) _height ; i + + )
2012-04-19 14:35:52 +08:00
{
2013-06-15 14:03:30 +08:00
row_pointers [ i ] = ( png_bytep ) _data + i * _width * 3 ;
2012-04-19 14:35:52 +08:00
}
png_write_image ( png_ptr , row_pointers ) ;
free ( row_pointers ) ;
2014-02-13 16:22:41 +08:00
row_pointers = nullptr ;
2012-04-19 14:35:52 +08:00
}
else
{
2013-08-06 16:14:36 +08:00
if ( isToRGB )
2012-04-19 14:35:52 +08:00
{
2020-09-01 14:57:18 +08:00
uint8_t * tempData = static_cast < uint8_t * > ( malloc ( _width * _height * 3 ) ) ;
2014-08-21 15:41:05 +08:00
if ( nullptr = = tempData )
2012-04-19 14:35:52 +08:00
{
2021-04-22 01:15:49 +08:00
delete outStream ;
2012-04-19 14:35:52 +08:00
png_destroy_write_struct ( & png_ptr , & info_ptr ) ;
2014-04-13 06:13:58 +08:00
free ( row_pointers ) ;
row_pointers = nullptr ;
2012-04-19 14:35:52 +08:00
break ;
}
2013-06-15 14:03:30 +08:00
for ( int i = 0 ; i < _height ; + + i )
2012-04-19 14:35:52 +08:00
{
2013-06-15 14:03:30 +08:00
for ( int j = 0 ; j < _width ; + + j )
2012-04-19 14:35:52 +08:00
{
2014-08-21 15:41:05 +08:00
tempData [ ( i * _width + j ) * 3 ] = _data [ ( i * _width + j ) * 4 ] ;
tempData [ ( i * _width + j ) * 3 + 1 ] = _data [ ( i * _width + j ) * 4 + 1 ] ;
tempData [ ( i * _width + j ) * 3 + 2 ] = _data [ ( i * _width + j ) * 4 + 2 ] ;
2012-04-19 14:35:52 +08:00
}
}
2013-06-15 14:03:30 +08:00
for ( int i = 0 ; i < ( int ) _height ; i + + )
2012-04-19 14:35:52 +08:00
{
2014-08-21 15:41:05 +08:00
row_pointers [ i ] = ( png_bytep ) tempData + i * _width * 3 ;
2012-04-19 14:35:52 +08:00
}
png_write_image ( png_ptr , row_pointers ) ;
free ( row_pointers ) ;
2014-02-13 16:22:41 +08:00
row_pointers = nullptr ;
2012-04-19 14:35:52 +08:00
2014-08-21 15:41:05 +08:00
if ( tempData ! = nullptr )
2013-11-29 15:13:16 +08:00
{
2014-08-21 15:41:05 +08:00
free ( tempData ) ;
2013-11-29 15:13:16 +08:00
}
2012-04-19 14:35:52 +08:00
}
else
{
2013-06-15 14:03:30 +08:00
for ( int i = 0 ; i < ( int ) _height ; i + + )
2012-04-19 14:35:52 +08:00
{
2013-06-15 14:03:30 +08:00
row_pointers [ i ] = ( png_bytep ) _data + i * _width * 4 ;
2012-04-19 14:35:52 +08:00
}
png_write_image ( png_ptr , row_pointers ) ;
free ( row_pointers ) ;
2014-02-13 16:22:41 +08:00
row_pointers = nullptr ;
2012-04-19 14:35:52 +08:00
}
}
png_write_end ( png_ptr , info_ptr ) ;
png_destroy_write_struct ( & png_ptr , & info_ptr ) ;
2021-04-22 01:15:49 +08:00
delete outStream ;
2012-04-19 14:35:52 +08:00
2014-08-21 15:41:05 +08:00
ret = true ;
2012-04-19 14:35:52 +08:00
} while ( 0 ) ;
2014-08-21 15:41:05 +08:00
return ret ;
2015-04-16 00:19:38 +08:00
# else
CCLOG ( " png is not enabled, please enable it in ccConfig.h " ) ;
return false ;
# endif // CC_USE_PNG
2012-04-19 14:35:52 +08:00
}
2015-04-16 00:19:38 +08:00
2013-11-15 09:19:16 +08:00
bool Image : : saveImageToJPG ( const std : : string & filePath )
2012-04-19 14:35:52 +08:00
{
metal support for cocos2d-x (#19305)
* remove deprecated files
* remove some deprecated codes
* remove more deprecated codes
* remove ui deprecated codes
* remove more deprecated codes
* remove deprecated codes in ccmenuitem
* remove more deprecated codes in ui
* remove more deprecated codes in ui
* remove more deprecated codes in ui
* remove more deprecated codes
* remove more deprecated codes
* remove more deprecated codes
* remove vr related codes and ignore some modules
* remove allocator
* remove some config
* 【Feature】add back-end project file
* [Feature] add back-end file
* add pipeline descriptor and shader cache
* [Feature] support sprite for backend
* [Feature] remove unneeded code
* [Feature] according to es2.0 spec, you must use clamp-to-edge as texture wrap mode, and no mipmapping for non-power-of-two texture
* [Feature] set texture wrap mode to clamp-to-edge, and no mipmapping for non-power-of-two texture
* [Feature] remove macro define to .cpp file
* [Feature] add log info
* [Feature] add PipelineDescriptor for TriangleCommand
* [Feature] add PipelineDescriptor object as member of TriangleCommand
* [Feature] add getPipelineDescriptor method
* add renderbackend
* complete pipeline descriptor
* [Feature] add viewport in RenderCommand
* set viewport when rendrering
* [Feature] occur error when using RendererBackend, to be fixed.
* a workaround to fix black screen on macOS 10.14 (#19090)
* add rendererbackend init function
* fix typo
* [Feature] modify testFile
* [BugFix] modify shader path
* [Feature] set default viewport
* fix projection
* [Feature] modify log info
* [BugFix] change viewport data type to int
* [BugFix] add BindGroup to PipelienDescriptor
* [BugFix] change a_position to vec3 in sprite.vert
* [BugFix] set vertexLayout according to V3F_C4B_T2F structure
* [Feature] revert a_position to vec4
* [Feature] renderer should not use gl codes directly
* [Feature] it's better not use default value parameter
* fix depth test setting
* rendererbackend -> renderer
* clear color and depth at begin
* add metal backend
* metal support normalized attribute
* simplify codes
* update external
* add render pass desctriptor in pipeline descriptor
* fix warnings
* fix crash and memeory leak
* refactor Texture2D
* put pipeline descriptor into render command
* simplify codes
* [Feature] update Sprite
* fix crash when closing app
* [Feature] update SpriteBatchNode and TextureAtlas
* support render texture(not finish)
* [Feature] remove unused code
* make tests work on mac
* fix download-deps path error
* make tests work on iOS
* [Feature] support ttf under normal label effect
* refactor triangle command processing
* let renderer handle more common commands
* refactor backend
* make render texture work
* [Feature] refactor backend for GL
* [Feature]Renaming to make it easy to understand
* [Feature] change warp mode to CLAMP_TO_EDGE
* fix ghost
* simplify visit render queue logic
* support progress timer without rial mode
* support partcile system
* Feature/update label (#149)
* [BugFix] fix compile error
* [Feature] support outline effect in ios
* [Feature] add shader file
* [BugFix] fix begin and end RenderPass
* [Feature] update CustomCommand
* [Feature] revert project.pbxproj
* [Feature] simplify codes
* [BugFix] pack AI88 to RGBA8888 only when outline enable
* [Feature] support shadow effect in Label
* [Feature] support BMFont
* [Feature] support glow effect
* [Feature] simplify shader files
* LabelAtlas work
* handle blend function correctly
* support tile map
* don't share buffer in metal
* alloc buffer size as needed
* support more tilemap
* Merge branch 'minggo/metal-support' into feature/updateLabel
* minggo/metal-support:
support tile map
handle blend function correctly
LabelAtlas work
Feature/update label (#149)
support partcile system
# Conflicts:
# cocos/2d/CCLabel.cpp
# cocos/2d/CCSprite.cpp
# cocos/2d/CCSpriteBatchNode.cpp
# cocos/renderer/CCQuadCommand.cpp
# cocos/renderer/CCQuadCommand.h
* render texture work without saving file
* use global viewport
* grid3d works
* remove grabber
* tiled3d works
* [BugFix] fix label bug
* [Feature] add updateSubData for buffer
* [Feature] remove setVertexCount
* support depth test
* add callback command
* [Feature] add UITest
* [Feature] update UITest
* [Feature] remove unneeded codes
* fix custom command issue
* fix layer color blend issue
* [BugFix] fix iOS compile error
* [Feature] remove unneeded codes
* [Feature] fix updateVertexBuffer
* layerradial works
* add draw test back
* fix batch issue
* fix compiling error
* [BugFix] support ETC1
* [BugFix] get the correct pipelineDescriptor
* [BugFix] skip draw when backendTexture nullptr
* clipping node support
* [Feature] add shader files
* fix stencil issue in metal
* [Feature] update UILayoutTest
* [BugFix] skip drawing when vertexCount is zero
* refactor renderer
* add set global z order for stencil manager commands
* fix warnings caused by type
* remove viewport in render command
* [Feature] fix warnings caused by type
* [BugFix] clear vertexCount and indexCount for CustomComand when needed
* [Feature] update clear for CustomCommand
* ios use metal
* fix viewport issue
* fix LayerColorGradient crash
* [cmake] transport to android and windows (#160)
* save point 1
* compile on windows
* run on android
* revert useless change
* android set CC_ENABLE_CACHE_TEXTURE_DATA to 1
* add initGlew
* fix android crash
* add TODO new-renderer
* review update
* revert onGLFWWindowPosCallback
* fix android compiling error
* Impl progress radial (#162)
* progresstimer add radial impl
* default drawType to element
* dec invoke times of createVertexBuffer (#163)
* support depth/stencil format for gl backend
* simplify progress timer codes
* support motionstreak, effect is wrong
* fix motionstreak issue
* [Feature] update Scissor Test (#161)
* [Feature] update Scissor Test
* [Feature] update ScissorTest
* [Feature] rename function
* [Feature] get constant reference if needed
* [Feature] show render status (#164)
* improve performance
* fix depth state
* fill error that triangle vertex/index number bigger than buffer
* fix compiline error in release mode
* fix buffer conflict between CPU and GPU on iOS/macOS
* Renderer refactor (#165)
* use one vertes/index buffer with opengl
* fix error on windows
* custom command support index format config
* CCLayer: compact vertex data structure
* update comment
* fix doc
* support fast tilemap
* pass index format instead
* fix some wrong effect
* fix render texture error
* fix texture per-element size
* fix texture format error
* BlendFunc type refactor, GLenum -> backend::BlendFactor (#167)
* BlendFunc use backend::BlendFactor as inner field
* update comments
* use int to replace GLenum
* update xcode project fiel
* rename to GLBlendConst
* add ccConstants.h
* update xcode project file
* update copyright
* remove primitive command
* remove CCPrimitive.cpp/.h
* remove deprecated files
* remove unneeded files
* remove multiple view support
* remove multiple view support
* remove the usage of frame buffer in camera
* director don't use frame buffer
* remove FrameBuffer
* remove BatchCommand
* add some api reference
* add physics2d back
* fix crash when close app on mac
* improve render texture
* fix rendertexture issue
* fix rendertexture issue
* simplify codes
* CMake support for mac & ios (#169)
* update cmake
* fix compile error
* update 3rd libs version
* remove CCThread.h/.cpp
* remove ccthread
* use audio engine to implement simple audio engine
* remove unneeded codes
* remove deprecated codes
* remove winrt macro
* remove CC_USE_WIC
* set partcile blend function in more elegant way
* remove unneeded codes
* remove unneeded codes
* cmake works on windows
* update project setting
* improve performance
* GLFloat -> float
* sync v3 cmake improvements into metal-support (#172)
* pick: modern cmake, compile definitions improvement (#19139)
* modern cmake, use target_compile_definitions partly
* simplify macro define, remove USE_*
* modern cmake, macro define
* add physics 2d macro define into ccConfig.h
* remove USE_CHIPMUNK macro in build.gradle
* remove CocosSelectModule.cmake
* shrink useless define
* simplify compile options config, re-add if necessary
* update external for tmp CI test
* un-quote target_compile_options value
* add "-g" parameter only when debug mode
* keep single build type when generator Xcode & VS projecy
* update external for tmp CI tes
* add static_cast<char>(-1), fix -Wc++11-narrowing
* simplify win32 compile define
* not modify code, only improve compile options
# Conflicts:
# .gitignore
# cmake/Modules/CocosConfigDepend.cmake
# cocos/CMakeLists.txt
# external/config.json
# tests/cpp-tests/CMakeLists.txt
* modern cmake, improve cmake_compiler_flags (#19145)
* cmake_compiler_flags
* Fix typo
* Fix typo2
* Remove chanages from Android.mk
* correct lua template cmake build (#19149)
* don't add -Wno-deprecated into jsb target
* correct lua template cmake build
* fix win32 lua template compile error
* prevent cmake in-source-build friendly (#19151)
* pick: Copy resources to "Resources/" on win32 like in linux configuration
* add "/Z7" for cpp-tests on windows
* [cmake] fix iOS xcode property setting failed (#19208)
* fix iOS xcode property setting failed
* use search_depend_libs_recursive at dlls collect
* fix typo
* [cmake] add find_host_library into iOS toolchain file (#19230)
* pick: [lua android] use luajit & template cmake update (#19239)
* increase cmake stability , remove tests/CMakeLists.txt (#19261)
* cmake win32 Precompiled header (#19273)
* Precompiled header
* Fix
* Precompiled header for cocos
* Precompiled header jscocos2d
* Fix for COCOS2D_DEBUG is always 1 on Android (#19291)
Related #19289
* little build fix, tests cpp-tests works on mac
* sync v3 build related codes into metal-support (#173)
* strict initialization for std::array
* remove proj.win32 project configs
* modern cmake, cmake_cleanup_remove_unused_variables (#19146)
* Switch travis CI to xenial (#19207)
* Switch travis CI to xenial
* Remove language: android
* Set language: cpp
* Fix java problem
* Update sdkmanager
* Fix sdkmanger
* next sdkmanager fix
* Remove xenial from android
* revert to sdk-tools-{system}-3859397
* Remove linux cmake install
* Update before-install.sh
* Update .travis.yml
* Simplify install-deps-linux.sh, tested on Ubuntu 16.04 (#19212)
* Simplify install-deps-linux.sh
* Cleanup
* pick: install ninja
* update cocos2d-console submodule
* for metal-support alpha release, we only test cpp
* add HelloCpp into project(Cocos2d-x) for tmp test
* update extenal metal-support-4
* update uniform setting
* [Feature] update BindGroup
* [Feature] empty-test
* [Feature] cpp-test
* [Feature] fix GL compiler error
* [Feature] fix GL crash
* [Feature] empty-test
* [Feature] cpp-tests
* [feature] improve frameRate
* [feature] fix opengl compile error
* [feature] fix opengl compile error
* [BugFix] fix compute maxLocation error
* [Feature] update setting unifrom
* [Feature] fix namespace
* [Feature] remove unneeded code
* [Bugfix] fix project file
* [Feature] update review
* [texture2d] impl texture format support (#175)
* texture update
* update
* update texture
* commit
* compile on windows
* ddd
* rename
* rename methods
* no crash
* save gl
* save
* save
* rename
* move out pixel format convert functions
* metal crash
* update
* update android
* support gles compressed texture format
* support more compress format
* add more conversion methods
* ss
* save
* update conversion methods
* add PVRTC format support
* reformat
* add marco linux
* fix GL marcro
* pvrtc supported only by ios 8.0+
* remove unused cmake
* revert change
* refactor Texture2D::initWithData
* fix conversion log
* refactor Texture2D::initWithData
* remove some OpenGL constants for PVRTC
* add todo
* fix typo
* AutoTest works on mac/iOS by disable part cases, sync v3 bug fix (#174)
* review cpp-tests, and fix part issues on start auto test
* sync png format fix: Node:Particle3D abnormal texture effects #19204
* fix cpp-tests SpritePolygon crash, wrong png format (#19170)
* fix wrong png convert format from sRGB to Gray
* erase plist index if all frames was erased
* test_A8.png have I8 format, fix it
* [CCSpriteCache] allow re-add plist & add testcase (#19175)
* allow re-add plist & add testcase
* remove comments/rename method/update testcase
* fix isSpriteFramesWithFileLoaded & add testcase
* remove used variable
* remove unused variable
* fix double free issues when js/lua-tests exit on iOS (#19236)
* disable part cases, AutoTest works without crash on mac
* update cocos2dx files json, to test cocos new next
* fix spritecache plist parsing issue (#19269)
* [linux] Fix FileUtils::getContents with folder (#19157)
* fix FileUtils::getContents on linux/mac
* use stat.st_mode
* simplify
* [CCFileUtils] win32 getFileSize (#19176)
* win32 getFileSize
* fix stat
* [cpp test-Android]20:FileUtils/2 change title (#19197)
* sync #19200
* sync #19231
* [android lua] improve performance of lua loader (#19234)
* [lua] improve performance of lua loader
* remove cache fix
* Revert "fix spritecache plist parsing issue (#19269)"
This reverts commit f3a85ece4307a7b90816c34489d1ed2c8fd11baf.
* remove win32 project files ref in template.json
* add metal framework lnk ref into cpp template
* test on iOS, and disable part cases
* alBufferData instead of alBufferDataStatic for small audio file on Apple (#19227)
* changes AudioCache to use alBufferData instead of alBufferDataStatic
(also makes test 19 faster to trigger openal bugs faster)
The original problem: CrashIfClientProvidedBogusAudioBufferList
https://github.com/cocos2d/cocos2d-x/issues/18948
is not happening anymore, but there's still a not very frequent issue
that makes OpenAL crash with a call stack like this.
AudioCache::readDataTask > alBufferData > CleanUpDeadBufferList
It happes more frequently when the device is "cold", which means after
half an hour of not using the device (locked).
I could not find the actual source code for iOS OpenAL, so I used the
macOS versions:
https://opensource.apple.com/source/OpenAL/OpenAL-48.7/Source/OpenAL/oalImp.cpp.auto.html
They seem to use CAGuard.h to make sure the dead buffer list
has no threading issues. I'm worried because the CAGuard code I found
has macos and win32 define but no iOS, so I'm not sure. I guess the
iOS version is different and has the guard.
I could not find a place in the code that's unprotected by the locks
except the InitializeBufferMap() which should not be called more than
once from cocos, and there's a workaround in AudioEngine-impl for it.
I reduced the occurence of the CleanUpDeadBufferList crash by moving
the guard in ~AudioCache to cover the alDeleteBuffers call.
* remove hack method "setTimeout" on audio
* AutoTest works on iOS
* support set ios deployment target for root project
* enable all texture2d cases, since Jiang have fixed
* add CCTextureUtils to xcode project file (#176)
* add leak cases for SpriteFrameCache (#177)
* re-add SpriteFrameCache cases
* update template file json
* Update SpriteFrameCacheTest.cpp
* fix compiling error
2019-01-18 15:08:25 +08:00
# if CC_USE_JPEG
2014-08-21 15:41:05 +08:00
bool ret = false ;
2014-08-21 15:19:26 +08:00
do
2014-08-15 14:29:10 +08:00
{
2014-08-21 15:19:26 +08:00
struct jpeg_compress_struct cinfo ;
struct jpeg_error_mgr jerr ;
2021-04-22 19:36:26 +08:00
FileStream * outfile ; /* target file */
2014-08-21 15:19:26 +08:00
JSAMPROW row_pointer [ 1 ] ; /* pointer to JSAMPLE row[s] */
int row_stride ; /* physical row width in image buffer */
cinfo . err = jpeg_std_error ( & jerr ) ;
/* Now we can initialize the JPEG compression object. */
jpeg_create_compress ( & cinfo ) ;
2021-04-22 19:36:26 +08:00
outfile = FileUtils : : getInstance ( ) - > openFileStream ( filePath , FileStream : : Mode : : WRITE ) ;
2021-04-24 07:22:27 +08:00
CC_BREAK_IF ( nullptr = = outfile ) ;
2021-04-22 19:36:26 +08:00
unsigned char * outputBuffer = nullptr ;
unsigned long outputSize = 0 ;
jpeg_mem_dest ( & cinfo , & outputBuffer , & outputSize ) ;
2014-08-21 15:19:26 +08:00
cinfo . image_width = _width ; /* image width and height, in pixels */
cinfo . image_height = _height ;
cinfo . input_components = 3 ; /* # of color components per pixel */
cinfo . in_color_space = JCS_RGB ; /* colorspace of input image */
jpeg_set_defaults ( & cinfo ) ;
jpeg_set_quality ( & cinfo , 90 , TRUE ) ;
jpeg_start_compress ( & cinfo , TRUE ) ;
row_stride = _width * 3 ; /* JSAMPLEs per row in image_buffer */
if ( hasAlpha ( ) )
{
2020-09-01 14:57:18 +08:00
uint8_t * tempData = static_cast < uint8_t * > ( malloc ( _width * _height * 3 ) ) ;
2014-08-21 15:41:05 +08:00
if ( nullptr = = tempData )
2014-08-21 15:19:26 +08:00
{
jpeg_finish_compress ( & cinfo ) ;
jpeg_destroy_compress ( & cinfo ) ;
2021-04-22 19:36:26 +08:00
delete outfile ;
outfile = nullptr ;
if ( outputBuffer )
{
free ( outputBuffer ) ;
outputBuffer = nullptr ;
}
2014-08-21 15:19:26 +08:00
break ;
}
for ( int i = 0 ; i < _height ; + + i )
{
for ( int j = 0 ; j < _width ; + + j )
{
2014-08-21 15:41:05 +08:00
tempData [ ( i * _width + j ) * 3 ] = _data [ ( i * _width + j ) * 4 ] ;
tempData [ ( i * _width + j ) * 3 + 1 ] = _data [ ( i * _width + j ) * 4 + 1 ] ;
tempData [ ( i * _width + j ) * 3 + 2 ] = _data [ ( i * _width + j ) * 4 + 2 ] ;
2014-08-21 15:19:26 +08:00
}
}
2014-08-21 15:41:05 +08:00
while ( cinfo . next_scanline < cinfo . image_height )
{
row_pointer [ 0 ] = & tempData [ cinfo . next_scanline * row_stride ] ;
2014-08-21 15:19:26 +08:00
( void ) jpeg_write_scanlines ( & cinfo , row_pointer , 1 ) ;
}
2014-08-21 15:41:05 +08:00
if ( tempData ! = nullptr )
2014-08-21 15:19:26 +08:00
{
2014-08-21 15:41:05 +08:00
free ( tempData ) ;
2014-08-21 15:19:26 +08:00
}
}
else
{
while ( cinfo . next_scanline < cinfo . image_height ) {
row_pointer [ 0 ] = & _data [ cinfo . next_scanline * row_stride ] ;
( void ) jpeg_write_scanlines ( & cinfo , row_pointer , 1 ) ;
}
}
jpeg_finish_compress ( & cinfo ) ;
2021-04-22 19:36:26 +08:00
outfile - > write ( outputBuffer , outputSize ) ;
delete outfile ; // closes FileStream automatically
outfile = nullptr ;
if ( outputBuffer )
{
free ( outputBuffer ) ;
outputBuffer = nullptr ;
}
2014-08-21 15:19:26 +08:00
jpeg_destroy_compress ( & cinfo ) ;
2014-08-21 15:41:05 +08:00
ret = true ;
2014-08-21 15:19:26 +08:00
} while ( 0 ) ;
2014-08-21 15:41:05 +08:00
return ret ;
2014-08-21 15:19:26 +08:00
# else
CCLOG ( " jpeg is not enabled, please enable it in ccConfig.h " ) ;
return false ;
# endif // CC_USE_JPEG
2012-04-19 14:35:52 +08:00
}
2019-07-26 17:09:00 +08:00
void Image : : premultiplyAlpha ( )
2014-06-03 15:06:15 +08:00
{
2020-11-16 12:21:27 +08:00
# if CC_ENABLE_PREMULTIPLIED_ALPHA
2020-09-25 11:07:56 +08:00
CCASSERT ( _pixelFormat = = backend : : PixelFormat : : RGBA8 , " The pixel format should be RGBA8888! " ) ;
2020-11-16 12:21:27 +08:00
2014-06-03 15:19:01 +08:00
unsigned int * fourBytes = ( unsigned int * ) _data ;
2020-11-16 12:21:27 +08:00
for ( int i = 0 ; i < _width * _height ; i + + )
2014-06-03 15:06:15 +08:00
{
2020-09-01 14:57:18 +08:00
uint8_t * p = _data + i * 4 ;
2014-06-03 15:06:15 +08:00
fourBytes [ i ] = CC_RGB_PREMULTIPLY_ALPHA ( p [ 0 ] , p [ 1 ] , p [ 2 ] , p [ 3 ] ) ;
}
2020-11-16 12:21:27 +08:00
2014-07-25 18:01:32 +08:00
_hasPremultipliedAlpha = true ;
2020-11-16 12:21:27 +08:00
# else
_hasPremultipliedAlpha = false ;
2016-06-20 23:25:31 +08:00
# endif
2014-06-03 15:06:15 +08:00
}
2020-09-01 14:57:18 +08:00
static inline uint8_t clamp ( int x ) {
return ( uint8_t ) ( x > = 0 ? ( x < 255 ? x : 255 ) : 0 ) ;
2019-07-26 17:09:00 +08:00
}
void Image : : reversePremultipliedAlpha ( )
{
2020-09-25 11:07:56 +08:00
CCASSERT ( _pixelFormat = = backend : : PixelFormat : : RGBA8 , " The pixel format should be RGBA8888! " ) ;
2019-07-26 17:09:00 +08:00
unsigned int * fourBytes = ( unsigned int * ) _data ;
for ( int i = 0 ; i < _width * _height ; i + + )
{
2020-09-01 14:57:18 +08:00
uint8_t * p = _data + i * 4 ;
2019-07-26 17:09:00 +08:00
if ( p [ 3 ] > 0 )
{
fourBytes [ i ] = clamp ( int ( std : : ceil ( ( p [ 0 ] * 255.0f ) / p [ 3 ] ) ) ) |
clamp ( int ( std : : ceil ( ( p [ 1 ] * 255.0f ) / p [ 3 ] ) ) ) < < 8 |
clamp ( int ( std : : ceil ( ( p [ 2 ] * 255.0f ) / p [ 3 ] ) ) ) < < 16 |
p [ 3 ] < < 24 ;
}
}
_hasPremultipliedAlpha = false ;
}
2014-08-21 15:19:26 +08:00
2014-07-28 10:38:22 +08:00
void Image : : setPVRImagesHavePremultipliedAlpha ( bool haveAlphaPremultiplied )
2014-07-25 18:19:04 +08:00
{
2020-11-16 12:21:27 +08:00
setCompressedImagesHavePMA ( CompressedImagePMAFlag : : PVR , haveAlphaPremultiplied ) ;
2014-07-25 18:19:04 +08:00
}
2012-04-19 14:35:52 +08:00
NS_CC_END