axmol/core/renderer/Texture2D.cpp

817 lines
27 KiB
C++
Raw Normal View History

2019-11-23 20:27:39 +08:00
/****************************************************************************
Copyright (c) 2008 Apple Inc. All Rights Reserved.
Copyright (c) 2010-2012 cocos2d-x.org
Copyright (c) 2013-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
Copyright (c) 2020 C4games Ltd.
Copyright (c) 2021-2022 Bytedance Inc.
2019-11-23 20:27:39 +08:00
2022-10-01 16:24:52 +08:00
https://axmolengine.github.io/
2019-11-23 20:27:39 +08:00
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.
****************************************************************************/
/*
2021-12-25 10:04:45 +08:00
* Support for RGBA_4_4_4_4 and RGBA_5_5_5_1 was copied from:
* https://devforums.apple.com/message/37855#37855 by a1studmuffin
*/
2019-11-23 20:27:39 +08:00
#include "renderer/Texture2D.h"
#include "platform/Image.h"
#include "platform/GL.h"
#include "base/Utils.h"
#include "platform/Device.h"
#include "base/Config.h"
#include "base/Macros.h"
#include "base/UTF8.h"
#include "base/Configuration.h"
#include "platform/PlatformMacros.h"
#include "base/Director.h"
#include "base/NinePatchImageParser.h"
2019-11-23 20:27:39 +08:00
#include "renderer/backend/Device.h"
#include "renderer/backend/ProgramState.h"
#include "renderer/Shaders.h"
#include "renderer/backend/PixelFormatUtils.h"
#include "renderer/Renderer.h"
2019-11-23 20:27:39 +08:00
2022-07-16 10:43:05 +08:00
#if AX_ENABLE_CACHE_TEXTURE_DATA
# include "renderer/TextureCache.h"
2019-11-23 20:27:39 +08:00
#endif
NS_AX_BEGIN
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
// CLASS IMPLEMENTATIONS:
2019-11-23 20:27:39 +08:00
// If the image has alpha, you can create RGBA8 (32-bit) or RGBA4 (16-bit) or RGB5A1 (16-bit)
// Default is: RGBA8888 (32-bit textures)
static backend::PixelFormat g_defaultAlphaPixelFormat = backend::PixelFormat::RGBA8;
2019-11-23 20:27:39 +08:00
Texture2D::Texture2D()
2021-12-25 10:04:45 +08:00
: _pixelFormat(backend::PixelFormat::NONE)
, _pixelsWide(0)
, _pixelsHigh(0)
, _maxS(0.0)
, _maxT(0.0)
, _flags(TextureFlag::ANTIALIAS_ENABLED)
, _samplerFlags(TextureSamplerFlag::DEFAULT)
, _ninePatchInfo(nullptr)
, _valid(true)
2019-11-23 20:27:39 +08:00
{
backend::TextureDescriptor textureDescriptor;
textureDescriptor.textureFormat = PixelFormat::NONE;
_texture = static_cast<backend::Texture2DBackend*>(backend::Device::getInstance()->newTexture(textureDescriptor));
}
Texture2D::~Texture2D()
{
2022-07-16 10:43:05 +08:00
#if AX_ENABLE_CACHE_TEXTURE_DATA
2019-11-23 20:27:39 +08:00
VolatileTextureMgr::removeTexture(this);
#endif
2022-07-16 10:43:05 +08:00
AXLOGINFO("deallocing Texture2D: %p - id=%u", this, _name);
2019-11-23 20:27:39 +08:00
2022-07-16 10:43:05 +08:00
AX_SAFE_DELETE(_ninePatchInfo);
2019-11-23 20:27:39 +08:00
2022-07-16 10:43:05 +08:00
AX_SAFE_RELEASE(_texture);
AX_SAFE_RELEASE(_programState);
2019-11-23 20:27:39 +08:00
}
backend::PixelFormat Texture2D::getPixelFormat() const
{
return _pixelFormat;
}
int Texture2D::getPixelsWide() const
{
return _pixelsWide;
}
int Texture2D::getPixelsHigh() const
{
return _pixelsHigh;
}
backend::TextureBackend* Texture2D::getBackendTexture() const
{
return _texture;
}
2021-10-23 23:27:14 +08:00
Vec2 Texture2D::getContentSize() const
2019-11-23 20:27:39 +08:00
{
2021-10-23 23:27:14 +08:00
Vec2 ret;
2022-07-16 10:43:05 +08:00
ret.width = _contentSize.width / AX_CONTENT_SCALE_FACTOR();
ret.height = _contentSize.height / AX_CONTENT_SCALE_FACTOR();
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
return ret;
}
2021-10-23 23:27:14 +08:00
const Vec2& Texture2D::getContentSizeInPixels()
2019-11-23 20:27:39 +08:00
{
return _contentSize;
}
float Texture2D::getMaxS() const
{
return _maxS;
}
void Texture2D::setMaxS(float maxS)
{
_maxS = maxS;
}
float Texture2D::getMaxT() const
{
return _maxT;
}
void Texture2D::setMaxT(float maxT)
{
_maxT = maxT;
}
bool Texture2D::hasPremultipliedAlpha() const
{
return _flags & TextureFlag::PREMULTIPLIEDALPHA;
2019-11-23 20:27:39 +08:00
}
void Texture2D::setPremultipliedAlpha(bool premultipliedAlpha)
{
2021-12-25 10:04:45 +08:00
if (premultipliedAlpha)
_flags |= TextureFlag::PREMULTIPLIEDALPHA;
else
_flags &= ~TextureFlag::PREMULTIPLIEDALPHA;
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
bool Texture2D::initWithData(const void* data,
ssize_t dataLen,
backend::PixelFormat pixelFormat,
backend::PixelFormat renderFormat,
int pixelsWide,
int pixelsHigh,
bool preMultipliedAlpha)
2019-11-23 20:27:39 +08:00
{
2022-07-16 10:43:05 +08:00
AXASSERT(dataLen > 0 && pixelsWide > 0 && pixelsHigh > 0, "Invalid size");
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
// if data has no mipmaps, we will consider it has only one mipmap
2019-11-23 20:27:39 +08:00
MipmapInfo mipmap;
mipmap.address = (unsigned char*)data;
2021-12-25 10:04:45 +08:00
mipmap.len = static_cast<int>(dataLen);
2019-11-23 20:27:39 +08:00
return initWithMipmaps(&mipmap, 1, pixelFormat, renderFormat, pixelsWide, pixelsHigh, preMultipliedAlpha);
}
2021-12-25 10:04:45 +08:00
bool Texture2D::initWithMipmaps(MipmapInfo* mipmaps,
int mipmapsNum,
backend::PixelFormat pixelFormat,
backend::PixelFormat renderFormat,
int pixelsWide,
int pixelsHigh,
bool preMultipliedAlpha)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
// the pixelFormat must be a certain value
2019-11-23 20:27:39 +08:00
updateWithMipmaps(mipmaps, mipmapsNum, pixelFormat, renderFormat, pixelsWide, pixelsHigh, preMultipliedAlpha);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
return true;
}
bool Texture2D::updateWithImage(Image* image, backend::PixelFormat format, int index)
2019-11-23 20:27:39 +08:00
{
if (image == nullptr)
{
2022-10-01 16:24:52 +08:00
AXLOG("axmol: Texture2D. Can't create Texture. UIImage is nil");
2019-11-23 20:27:39 +08:00
return false;
}
2021-12-25 10:04:45 +08:00
if (this->_filePath.empty())
this->_filePath = image->getFilePath();
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
int imageWidth = image->getWidth();
2019-11-23 20:27:39 +08:00
int imageHeight = image->getHeight();
Configuration* conf = Configuration::getInstance();
int maxTextureSize = conf->getMaxTextureSize();
if (imageWidth > maxTextureSize || imageHeight > maxTextureSize)
{
2022-10-01 16:24:52 +08:00
AXLOG("axmol: WARNING: Image (%u x %u) is bigger than the supported %u x %u", imageWidth, imageHeight,
2021-12-25 10:04:45 +08:00
maxTextureSize, maxTextureSize);
2019-11-23 20:27:39 +08:00
return false;
}
2021-12-25 10:04:45 +08:00
unsigned char* tempData = image->getData();
2022-02-06 13:06:49 +08:00
// Vec2 imageSize = Vec2((float)imageWidth, (float)imageHeight);
2021-12-25 10:04:45 +08:00
backend::PixelFormat renderFormat = (PixelFormat::NONE == format) ? image->getPixelFormat() : format;
backend::PixelFormat imagePixelFormat = image->getPixelFormat();
size_t tempDataLen = image->getDataLen();
2019-11-23 20:27:39 +08:00
2022-07-16 10:43:05 +08:00
#ifdef AX_USE_METAL
2021-12-25 10:04:45 +08:00
//! override renderFormat, since some render format is not supported by metal
2019-11-23 20:27:39 +08:00
switch (renderFormat)
{
2022-07-16 10:43:05 +08:00
# if (AX_TARGET_PLATFORM != AX_PLATFORM_IOS || TARGET_OS_SIMULATOR)
2021-12-25 10:04:45 +08:00
// packed 16 bits pixels only available on iOS
2019-11-23 20:27:39 +08:00
case PixelFormat::RGB565:
case PixelFormat::RGB5A1:
case PixelFormat::RGBA4:
2021-12-25 10:04:45 +08:00
# endif
case PixelFormat::L8:
case PixelFormat::LA8:
case PixelFormat::RGB8:
2021-12-25 10:04:45 +08:00
// Note: conversion to RGBA8 will happends
renderFormat = PixelFormat::RGBA8;
2019-11-23 20:27:39 +08:00
break;
default:
break;
}
2023-08-09 14:37:43 +08:00
#elif defined(AX_USE_GL_CORE_PROFILE)
// Note: axmol-1.1 deprecated A8, L8, LA8 as renderFormat, preferred R8, RG8
switch (renderFormat)
{
case PixelFormat::A8:
case PixelFormat::L8:
case PixelFormat::LA8:
// Note: conversion to RGBA8 will happends
renderFormat = PixelFormat::RGBA8;
}
2019-11-23 20:27:39 +08:00
#endif
if (image->getNumberOfMipmaps() > 1)
{
if (renderFormat != image->getPixelFormat())
{
2022-10-01 16:24:52 +08:00
AXLOG("axmol: WARNING: This image has more than 1 mipmaps and we will not convert the data format");
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
// pixel format of data is not converted, renderFormat can be different from pixelFormat
// it will be done later
2022-02-08 12:41:18 +08:00
updateWithMipmaps(image->getMipmaps(), image->getNumberOfMipmaps(), image->getPixelFormat(), renderFormat, imageHeight, imageWidth, image->hasPremultipliedAlpha(), index);
2019-11-23 20:27:39 +08:00
}
else if (image->isCompressed())
2021-12-25 10:04:45 +08:00
{ // !Only hardware support texture will be compression PixelFormat, otherwise, will convert to RGBA8 duraing image
// load
renderFormat = imagePixelFormat;
2022-02-06 13:06:49 +08:00
updateWithData(tempData, tempDataLen, image->getPixelFormat(), image->getPixelFormat(), imageWidth, imageHeight, image->hasPremultipliedAlpha(), index);
2019-11-23 20:27:39 +08:00
}
else
{
2021-12-25 10:04:45 +08:00
// after conversion, renderFormat == pixelFormat of data
2022-02-06 13:06:49 +08:00
updateWithData(tempData, tempDataLen, imagePixelFormat, renderFormat, imageWidth, imageHeight,
2021-12-25 10:04:45 +08:00
image->hasPremultipliedAlpha(), index);
2019-11-23 20:27:39 +08:00
}
return true;
}
2021-12-25 10:04:45 +08:00
bool Texture2D::updateWithData(const void* data,
ssize_t dataLen,
backend::PixelFormat pixelFormat,
backend::PixelFormat renderFormat,
int pixelsWide,
int pixelsHigh,
bool preMultipliedAlpha,
int index)
2019-11-23 20:27:39 +08:00
{
2022-07-16 10:43:05 +08:00
AXASSERT(dataLen > 0 && pixelsWide > 0 && pixelsHigh > 0, "Invalid size");
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
// if data has no mipmaps, we will consider it has only one mipmap
2019-11-23 20:27:39 +08:00
MipmapInfo mipmap;
mipmap.address = (unsigned char*)data;
2021-12-25 10:04:45 +08:00
mipmap.len = static_cast<int>(dataLen);
2019-11-23 20:27:39 +08:00
return updateWithMipmaps(&mipmap, 1, pixelFormat, renderFormat, pixelsWide, pixelsHigh, preMultipliedAlpha, index);
}
2021-12-25 10:04:45 +08:00
bool Texture2D::updateWithMipmaps(MipmapInfo* mipmaps,
int mipmapsNum,
backend::PixelFormat pixelFormat,
backend::PixelFormat renderFormat,
int pixelsWide,
int pixelsHigh,
bool preMultipliedAlpha,
int index)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
// the pixelFormat must be a certain value
2022-07-16 10:43:05 +08:00
AXASSERT(pixelFormat != PixelFormat::NONE, "the \"pixelFormat\" param must be a certain value!");
AXASSERT(pixelsWide > 0 && pixelsHigh > 0, "Invalid size");
2019-11-23 20:27:39 +08:00
if (mipmapsNum <= 0)
{
2022-10-01 16:24:52 +08:00
AXLOG("axmol: WARNING: mipmap number is less than 1");
2019-11-23 20:27:39 +08:00
return false;
}
auto& pfd = backend::PixelFormatUtils::getFormatDescriptor(pixelFormat);
if (!pfd.bpp)
2019-11-23 20:27:39 +08:00
{
2022-10-01 16:24:52 +08:00
AXLOG("axmol: WARNING: unsupported pixelformat: %x", (uint32_t)pixelFormat);
2022-07-16 10:43:05 +08:00
#ifdef AX_USE_METAL
AXASSERT(false, "pixeformat not found in _pixelFormatInfoTables, register required!");
2019-11-23 20:27:39 +08:00
#endif
return false;
}
bool compressed = backend::PixelFormatUtils::isCompressed(pixelFormat);
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
if (compressed && !Configuration::getInstance()->supportsPVRTC() && !Configuration::getInstance()->supportsETC1() &&
!Configuration::getInstance()->supportsETC2() && !Configuration::getInstance()->supportsS3TC() &&
!Configuration::getInstance()->supportsASTC() && !Configuration::getInstance()->supportsATITC())
2019-11-23 20:27:39 +08:00
{
2022-10-01 16:24:52 +08:00
AXLOG("axmol: WARNING: PVRTC/ETC images are not supported");
2019-11-23 20:27:39 +08:00
return false;
}
2022-07-16 10:43:05 +08:00
#if AX_ENABLE_CACHE_TEXTURE_DATA
2019-11-23 20:27:39 +08:00
VolatileTextureMgr::findVolotileTexture(this);
#endif
backend::TextureDescriptor textureDescriptor;
2021-12-25 10:04:45 +08:00
textureDescriptor.width = pixelsWide;
2019-11-23 20:27:39 +08:00
textureDescriptor.height = pixelsHigh;
2021-12-25 10:04:45 +08:00
textureDescriptor.samplerDescriptor.magFilter =
(_flags & TextureFlag::ANTIALIAS_ENABLED) ? backend::SamplerFilter::LINEAR : backend::SamplerFilter::NEAREST;
2019-11-23 20:27:39 +08:00
if (mipmapsNum == 1)
{
textureDescriptor.samplerDescriptor.minFilter = (_flags & TextureFlag::ANTIALIAS_ENABLED)
2021-12-25 10:04:45 +08:00
? backend::SamplerFilter::LINEAR
: backend::SamplerFilter::NEAREST;
2019-11-23 20:27:39 +08:00
}
else
{
textureDescriptor.samplerDescriptor.minFilter = (_flags & TextureFlag::ANTIALIAS_ENABLED)
2021-12-25 10:04:45 +08:00
? backend::SamplerFilter::LINEAR_MIPMAP_NEAREST
: backend::SamplerFilter::NEAREST_MIPMAP_NEAREST;
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
int width = pixelsWide;
int height = pixelsHigh;
2019-11-23 20:27:39 +08:00
backend::PixelFormat oriPixelFormat = pixelFormat;
for (int i = 0; i < mipmapsNum; ++i)
{
2021-12-25 10:04:45 +08:00
unsigned char* data = mipmaps[i].address;
size_t dataLen = mipmaps[i].len;
2019-11-23 20:27:39 +08:00
unsigned char* outData = data;
2021-12-25 10:04:45 +08:00
size_t outDataLen = dataLen;
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
if (renderFormat != oriPixelFormat && !compressed) // need conversion
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
auto convertedFormat = backend::PixelFormatUtils::convertDataToFormat(data, dataLen, oriPixelFormat,
renderFormat, &outData, &outDataLen);
2022-07-16 10:43:05 +08:00
#ifdef AX_USE_METAL
AXASSERT(convertedFormat == renderFormat, "PixelFormat convert failed!");
2019-11-23 20:27:39 +08:00
#endif
2021-12-25 10:04:45 +08:00
if (convertedFormat == renderFormat)
pixelFormat = renderFormat;
2019-11-23 20:27:39 +08:00
}
textureDescriptor.textureFormat = pixelFormat;
2022-07-16 10:43:05 +08:00
AXASSERT(textureDescriptor.textureFormat != backend::PixelFormat::NONE, "PixelFormat should not be NONE");
2019-11-23 20:27:39 +08:00
if (_texture->getTextureFormat() != textureDescriptor.textureFormat)
_texture->updateTextureDescriptor(textureDescriptor, index);
if (compressed)
2019-11-23 20:27:39 +08:00
{
_texture->updateCompressedData(data, width, height, dataLen, i, index);
2019-11-23 20:27:39 +08:00
}
else
{
_texture->updateData(outData, width, height, i, index);
}
if (outData && outData != data && outDataLen > 0)
{
free(outData);
2021-12-25 10:04:45 +08:00
outData = nullptr;
2019-11-23 20:27:39 +08:00
outDataLen = 0;
}
if (i > 0 && (width != height || ccNextPOT(width) != width))
{
2022-07-16 10:43:05 +08:00
AXLOG(
2023-08-09 14:37:43 +08:00
"axmol: Texture2D. WARNING. Mipmap level %u is not squared. Texture won't render correctly. width=%d "
2021-12-25 10:04:45 +08:00
"!= height=%d",
i, width, height);
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
width = MAX(width >> 1, 1);
2019-11-23 20:27:39 +08:00
height = MAX(height >> 1, 1);
}
2021-12-25 10:04:45 +08:00
if (index == 0)
{
2021-10-23 23:27:14 +08:00
_contentSize = Vec2((float)pixelsWide, (float)pixelsHigh);
2021-12-25 10:04:45 +08:00
_pixelsWide = pixelsWide;
_pixelsHigh = pixelsHigh;
2019-11-23 20:27:39 +08:00
_pixelFormat = pixelFormat;
2021-12-25 10:04:45 +08:00
_maxS = 1;
_maxT = 1;
2019-11-23 20:27:39 +08:00
setPremultipliedAlpha(preMultipliedAlpha);
}
2022-02-06 13:06:49 +08:00
// pitfall: we do merge RGB+A at at dual sampler shader, so must mark as _hasPremultipliedAlpha = true to makesure
// alpha blend works well.
if (index > 0)
{
setPremultipliedAlpha(Image::isCompressedImageHavePMA(Image::CompressedImagePMAFlag::DUAL_SAMPLER));
_samplerFlags |= TextureSamplerFlag::DUAL_SAMPLER;
}
2019-11-23 20:27:39 +08:00
return true;
}
2021-12-25 10:04:45 +08:00
bool Texture2D::updateWithSubData(void* data, int offsetX, int offsetY, int width, int height, int index)
2019-11-23 20:27:39 +08:00
{
if (_texture && width > 0 && height > 0)
{
uint8_t* textureData = static_cast<uint8_t*>(data);
_texture->updateSubData(offsetX, offsetY, width, height, 0, textureData, index);
return true;
}
return false;
}
// implementation Texture2D (Image)
2021-12-25 10:04:45 +08:00
bool Texture2D::initWithImage(Image* image)
2019-11-23 20:27:39 +08:00
{
return initWithImage(image, g_defaultAlphaPixelFormat);
}
2021-12-25 10:04:45 +08:00
bool Texture2D::initWithImage(Image* image, backend::PixelFormat format)
2019-11-23 20:27:39 +08:00
{
if (image == nullptr)
{
2022-10-01 16:24:52 +08:00
AXLOG("axmol: Texture2D. Can't create Texture. UIImage is nil");
2019-11-23 20:27:39 +08:00
return false;
}
this->_filePath = image->getFilePath();
return updateWithImage(image, format);
}
// implementation Texture2D (Text)
bool Texture2D::initWithString(std::string_view text,
std::string_view fontName,
2021-12-25 10:04:45 +08:00
float fontSize,
const Vec2& dimensions /* = Vec2(0, 0)*/,
TextHAlignment hAlignment /* = TextHAlignment::CENTER */,
TextVAlignment vAlignment /* = TextVAlignment::TOP */,
bool enableWrap /* = false */,
int overflow /* = 0 */)
2019-11-23 20:27:39 +08:00
{
FontDefinition tempDef;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
tempDef._shadow._shadowEnabled = false;
tempDef._stroke._strokeEnabled = false;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
tempDef._fontName = fontName;
tempDef._fontSize = fontSize;
tempDef._dimensions = dimensions;
tempDef._alignment = hAlignment;
tempDef._vertAlignment = vAlignment;
tempDef._fontFillColor = Color3B::WHITE;
tempDef._enableWrap = enableWrap;
tempDef._overflow = overflow;
return initWithString(text, tempDef);
}
bool Texture2D::initWithString(std::string_view text, const FontDefinition& textDefinition)
2019-11-23 20:27:39 +08:00
{
if (text.empty())
2019-11-23 20:27:39 +08:00
return false;
2022-07-16 10:43:05 +08:00
#if AX_ENABLE_CACHE_TEXTURE_DATA
2019-11-23 20:27:39 +08:00
// cache the texture data
VolatileTextureMgr::addStringTexture(this, text, textDefinition);
#endif
bool ret = false;
Device::TextAlign align;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
if (TextVAlignment::TOP == textDefinition._vertAlignment)
{
align = (TextHAlignment::CENTER == textDefinition._alignment) ? Device::TextAlign::TOP
2021-12-25 10:04:45 +08:00
: (TextHAlignment::LEFT == textDefinition._alignment) ? Device::TextAlign::TOP_LEFT
: Device::TextAlign::TOP_RIGHT;
2019-11-23 20:27:39 +08:00
}
else if (TextVAlignment::CENTER == textDefinition._vertAlignment)
{
align = (TextHAlignment::CENTER == textDefinition._alignment) ? Device::TextAlign::CENTER
2021-12-25 10:04:45 +08:00
: (TextHAlignment::LEFT == textDefinition._alignment) ? Device::TextAlign::LEFT
: Device::TextAlign::RIGHT;
2019-11-23 20:27:39 +08:00
}
else if (TextVAlignment::BOTTOM == textDefinition._vertAlignment)
{
align = (TextHAlignment::CENTER == textDefinition._alignment) ? Device::TextAlign::BOTTOM
2021-12-25 10:04:45 +08:00
: (TextHAlignment::LEFT == textDefinition._alignment) ? Device::TextAlign::BOTTOM_LEFT
: Device::TextAlign::BOTTOM_RIGHT;
2019-11-23 20:27:39 +08:00
}
else
{
2022-07-16 10:43:05 +08:00
AXASSERT(false, "Not supported alignment format!");
2019-11-23 20:27:39 +08:00
return false;
}
2021-12-25 10:04:45 +08:00
2022-07-16 10:43:05 +08:00
#if (AX_TARGET_PLATFORM != AX_PLATFORM_ANDROID) && (AX_TARGET_PLATFORM != AX_PLATFORM_IOS)
AXASSERT(textDefinition._stroke._strokeEnabled == false, "Currently stroke only supported on iOS and Android!");
2019-11-23 20:27:39 +08:00
#endif
2021-12-25 10:04:45 +08:00
PixelFormat pixelFormat = g_defaultAlphaPixelFormat;
2019-11-23 20:27:39 +08:00
unsigned char* outTempData = nullptr;
2021-12-25 10:04:45 +08:00
size_t outTempDataLen = 0;
2019-11-23 20:27:39 +08:00
int imageWidth;
int imageHeight;
2021-12-25 10:04:45 +08:00
auto textDef = textDefinition;
2022-07-16 10:43:05 +08:00
auto contentScaleFactor = AX_CONTENT_SCALE_FACTOR();
2019-11-23 20:27:39 +08:00
textDef._fontSize *= contentScaleFactor;
textDef._dimensions.width *= contentScaleFactor;
textDef._dimensions.height *= contentScaleFactor;
textDef._stroke._strokeSize *= contentScaleFactor;
textDef._shadow._shadowEnabled = false;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
bool hasPremultipliedAlpha;
Data outData = Device::getTextureDataForText(text, textDef, align, imageWidth, imageHeight, hasPremultipliedAlpha);
2021-12-25 10:04:45 +08:00
if (outData.isNull())
2019-11-23 20:27:39 +08:00
{
return false;
}
2021-12-25 10:04:45 +08:00
Vec2 imageSize = Vec2((float)imageWidth, (float)imageHeight);
pixelFormat =
backend::PixelFormatUtils::convertDataToFormat(outData.getBytes(), imageWidth * imageHeight * 4,
PixelFormat::RGBA8, pixelFormat, &outTempData, &outTempDataLen);
2019-11-23 20:27:39 +08:00
2022-02-06 13:06:49 +08:00
ret = initWithData(outTempData, outTempDataLen, pixelFormat, imageWidth, imageHeight);
2019-11-23 20:27:39 +08:00
if (outTempData != nullptr && outTempData != outData.getBytes())
{
free(outTempData);
}
setPremultipliedAlpha(hasPremultipliedAlpha);
return ret;
}
bool Texture2D::updateTextureDescriptor(const backend::TextureDescriptor& descriptor, bool preMultipliedAlpha)
2019-11-23 20:27:39 +08:00
{
2022-07-16 10:43:05 +08:00
AX_ASSERT(_texture);
_texture->updateTextureDescriptor(descriptor);
2019-11-23 20:27:39 +08:00
_pixelsWide = _contentSize.width = _texture->getWidth();
_pixelsHigh = _contentSize.height = _texture->getHeight();
setPremultipliedAlpha(preMultipliedAlpha);
setRenderTarget(descriptor.textureUsage == TextureUsage::RENDER_TARGET);
2022-06-24 14:18:48 +08:00
if (_pixelFormat == PixelFormat::NONE)
_pixelFormat = descriptor.textureFormat;
2019-11-23 20:27:39 +08:00
return true;
}
void Texture2D::setRenderTarget(bool renderTarget)
{
if (renderTarget)
_flags |= TextureFlag::RENDERTARGET;
else
_flags &= TextureFlag::RENDERTARGET;
2019-11-23 20:27:39 +08:00
}
bool Texture2D::hasMipmaps() const
{
return _texture->hasMipmaps();
}
void Texture2D::setAliasTexParameters()
{
if ((_flags & TextureFlag::ANTIALIAS_ENABLED) == 0)
2019-11-23 20:27:39 +08:00
{
return;
}
_flags &= ~TextureFlag::ANTIALIAS_ENABLED;
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
backend::SamplerDescriptor descriptor(backend::SamplerFilter::NEAREST, // magFilter
(_texture->hasMipmaps()) ? backend::SamplerFilter::NEAREST_MIPMAP_NEAREST
: backend::SamplerFilter::NEAREST, // minFilter
backend::SamplerAddressMode::DONT_CARE, // sAddressMode
backend::SamplerAddressMode::DONT_CARE // tAddressMode
2019-11-23 20:27:39 +08:00
);
_texture->updateSamplerDescriptor(descriptor);
}
void Texture2D::setAntiAliasTexParameters()
{
if (_flags & TextureFlag::ANTIALIAS_ENABLED)
2019-11-23 20:27:39 +08:00
{
return;
}
_flags |= TextureFlag::ANTIALIAS_ENABLED;
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
backend::SamplerDescriptor descriptor(backend::SamplerFilter::LINEAR, // magFilter
(_texture->hasMipmaps()) ? backend::SamplerFilter::LINEAR_MIPMAP_NEAREST
: backend::SamplerFilter::LINEAR, // minFilter
backend::SamplerAddressMode::DONT_CARE, // sAddressMode
backend::SamplerAddressMode::DONT_CARE // tAddressMode
2019-11-23 20:27:39 +08:00
);
_texture->updateSamplerDescriptor(descriptor);
}
const char* Texture2D::getStringForFormat() const
{
return backend::PixelFormatUtils::getFormatDescriptor(_pixelFormat).name;
2019-11-23 20:27:39 +08:00
}
//
// Texture options for images that contains alpha
//
// implementation Texture2D (PixelFormat)
void Texture2D::setDefaultAlphaPixelFormat(backend::PixelFormat format)
{
g_defaultAlphaPixelFormat = format;
}
backend::PixelFormat Texture2D::getDefaultAlphaPixelFormat()
{
return g_defaultAlphaPixelFormat;
}
2020-11-02 16:35:31 +08:00
unsigned int Texture2D::getBitsPerPixelForFormat(backend::PixelFormat format) const
2019-11-23 20:27:39 +08:00
{
return backend::PixelFormatUtils::getFormatDescriptor(format).bpp;
2019-11-23 20:27:39 +08:00
}
unsigned int Texture2D::getBitsPerPixelForFormat() const
{
return getBitsPerPixelForFormat(_pixelFormat);
2019-11-23 20:27:39 +08:00
}
void Texture2D::addSpriteFrameCapInset(SpriteFrame* spritframe, const Rect& capInsets)
{
2021-12-25 10:04:45 +08:00
if (nullptr == _ninePatchInfo)
2019-11-23 20:27:39 +08:00
{
2021-12-08 00:11:53 +08:00
_ninePatchInfo = new NinePatchInfo;
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
if (nullptr == spritframe)
2019-11-23 20:27:39 +08:00
{
_ninePatchInfo->capInsetSize = capInsets;
}
else
{
_ninePatchInfo->capInsetMap[spritframe] = capInsets;
}
}
2021-12-25 10:04:45 +08:00
bool Texture2D::isContain9PatchInfo() const
2019-11-23 20:27:39 +08:00
{
return nullptr != _ninePatchInfo;
}
2022-08-08 18:02:17 +08:00
const Rect& Texture2D::getSpriteFrameCapInset(ax::SpriteFrame* spriteFrame) const
2019-11-23 20:27:39 +08:00
{
2022-07-16 10:43:05 +08:00
AXASSERT(_ninePatchInfo != nullptr,
2019-11-23 20:27:39 +08:00
"Can't get the sprite frame capInset when the texture contains no 9-patch info.");
2021-12-25 10:04:45 +08:00
if (nullptr == spriteFrame)
2019-11-23 20:27:39 +08:00
{
return this->_ninePatchInfo->capInsetSize;
}
else
{
2021-12-25 10:04:45 +08:00
auto& capInsetMap = this->_ninePatchInfo->capInsetMap;
if (capInsetMap.find(spriteFrame) != capInsetMap.end())
2019-11-23 20:27:39 +08:00
{
return capInsetMap.at(spriteFrame);
}
else
{
return this->_ninePatchInfo->capInsetSize;
}
}
}
void Texture2D::removeSpriteFrameCapInset(SpriteFrame* spriteFrame)
{
2021-12-25 10:04:45 +08:00
if (nullptr != this->_ninePatchInfo)
2019-11-23 20:27:39 +08:00
{
auto capInsetMap = this->_ninePatchInfo->capInsetMap;
2021-12-25 10:04:45 +08:00
if (capInsetMap.find(spriteFrame) != capInsetMap.end())
2019-11-23 20:27:39 +08:00
{
capInsetMap.erase(spriteFrame);
}
}
}
2021-12-25 10:04:45 +08:00
void Texture2D::setTexParameters(const Texture2D::TexParams& desc)
2019-11-23 20:27:39 +08:00
{
_texture->updateSamplerDescriptor(desc);
}
void Texture2D::generateMipmap()
{
2022-07-16 10:43:05 +08:00
AXASSERT(_pixelsWide == ccNextPOT(_pixelsWide) && _pixelsHigh == ccNextPOT(_pixelsHigh),
2021-12-25 10:04:45 +08:00
"Mipmap texture only works in POT textures");
2019-11-23 20:27:39 +08:00
_texture->generateMipmaps();
}
void Texture2D::initProgram()
{
2021-12-25 10:04:45 +08:00
if (_programState != nullptr)
2019-11-23 20:27:39 +08:00
return;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
auto& pipelineDescriptor = _customCommand.getPipelineDescriptor();
2021-12-25 10:04:45 +08:00
// create program state
auto* program = backend::Program::getBuiltinProgram(backend::ProgramType::POSITION_TEXTURE);
2022-08-08 18:02:17 +08:00
_programState = new ax::backend::ProgramState(program);
2019-11-23 20:27:39 +08:00
_mvpMatrixLocation = _programState->getUniformLocation("u_MVPMatrix");
_textureLocation = _programState->getUniformLocation("u_tex0");
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
pipelineDescriptor.programState = _programState;
2021-12-25 10:04:45 +08:00
// create vertex buffer
2019-11-23 20:27:39 +08:00
_customCommand.setDrawType(CustomCommand::DrawType::ARRAY);
_customCommand.setPrimitiveType(CustomCommand::PrimitiveType::TRIANGLE_STRIP);
_customCommand.createVertexBuffer(4 * sizeof(float), 4, CustomCommand::BufferUsage::DYNAMIC);
2021-12-25 10:04:45 +08:00
// setup blend state
2019-11-23 20:27:39 +08:00
BlendFunc blendFunc;
2021-12-25 10:04:45 +08:00
if (hasPremultipliedAlpha())
2019-11-23 20:27:39 +08:00
{
blendFunc = BlendFunc::ALPHA_PREMULTIPLIED;
}
else
{
blendFunc = BlendFunc::ALPHA_NON_PREMULTIPLIED;
}
2021-12-25 10:04:45 +08:00
auto& blendDescriptor = pipelineDescriptor.blendDescriptor;
blendDescriptor.blendEnabled = true;
2019-11-23 20:27:39 +08:00
blendDescriptor.sourceRGBBlendFactor = blendDescriptor.sourceAlphaBlendFactor = blendFunc.src;
blendDescriptor.destinationRGBBlendFactor = blendDescriptor.destinationAlphaBlendFactor = blendFunc.dst;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
_programState->setTexture(_textureLocation, 0, _texture);
}
2021-12-25 10:04:45 +08:00
void Texture2D::drawAtPoint(const Vec2& point, float globalZOrder)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
float width = (float)_pixelsWide * _maxS;
2019-11-23 20:27:39 +08:00
float height = (float)_pixelsHigh * _maxT;
2021-12-25 10:04:45 +08:00
Rect rect = {point.x, point.y, width, height};
2019-11-23 20:27:39 +08:00
drawInRect(rect, globalZOrder);
}
void Texture2D::drawInRect(const Rect& rect, float globalZOrder)
{
initProgram();
_customCommand.init(globalZOrder);
2021-12-25 10:04:45 +08:00
auto director = Director::getInstance();
const auto& modelView = director->getMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
2019-11-23 20:27:39 +08:00
const auto& projection = director->getMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
Mat4 matrixMVP = projection * modelView;
2021-12-25 10:04:45 +08:00
float vertexData[] = {rect.origin.x,
rect.origin.y,
0.0f,
_maxT,
rect.size.width + rect.origin.x,
rect.origin.y,
_maxS,
_maxT,
rect.origin.x,
rect.size.height + rect.origin.y,
0.0f,
0.0f,
rect.size.width + rect.origin.x,
rect.size.height + rect.origin.y,
_maxS,
0.0f};
2019-11-23 20:27:39 +08:00
_programState->setUniform(_mvpMatrixLocation, matrixMVP.m, sizeof(matrixMVP.m));
_customCommand.updateVertexBuffer(vertexData, sizeof(vertexData));
Director::getInstance()->getRenderer()->addCommand(&_customCommand);
}
NS_AX_END