axmol/cocos/renderer/CCTexture2D.h

467 lines
15 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.
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.
****************************************************************************/
#pragma once
#include <string>
#include <map>
#include <unordered_map>
#include "base/CCRef.h"
2021-10-23 23:27:14 +08:00
#include "math/CCMath.h"
2019-11-23 20:27:39 +08:00
#include "base/ccTypes.h"
#include "renderer/CCCustomCommand.h"
NS_CC_BEGIN
class Image;
class NinePatchInfo;
class SpriteFrame;
typedef struct _MipmapInfo MipmapInfo;
namespace ui
{
2021-12-25 10:04:45 +08:00
class Scale9Sprite;
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
namespace backend
{
class Texture2DBackend;
class TextureBackend;
class ProgramState;
} // namespace backend
2019-11-23 20:27:39 +08:00
/**
* @addtogroup _2d
* @{
*/
2021-12-25 10:04:45 +08:00
// CONSTANTS:
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
// CLASS INTERFACES:
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
/**
* @brief Texture2D class. This class allows to easily create OpenGL 2D textures from images, text or raw data.
* The created Texture2D object will always have power-of-two dimensions.
* Depending on how you create the Texture2D object, the actual image area of the texture might be smaller than the
* texture dimensions i.e. "contentSize" != (pixelsWide, pixelsHigh) and (maxS, maxT) != (1.0, 1.0). Be aware that the
* content of the generated textures will be upside-down!
*/
2019-11-23 20:27:39 +08:00
class CC_DLL Texture2D : public Ref
{
public:
2021-12-25 10:04:45 +08:00
struct PixelFormatInfo
{
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
PixelFormatInfo(int aBpp, bool aCompressed, bool anAlpha) : bpp(aBpp), compressed(aCompressed), alpha(anAlpha)
2019-11-23 20:27:39 +08:00
{}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
int bpp;
bool compressed;
bool alpha;
};
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* Extension to set the Min / Mag filter
*/
using TexParams = backend::SamplerDescriptor;
public:
/** sets the default pixel format for UIImagescontains alpha channel.
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
@param format
If the UIImage contains alpha channel, then the options are:
- generate 32-bit textures: backend::PixelFormat::RGBA8 (default one)
- generate 24-bit textures: backend::PixelFormat::RGB8
- generate 16-bit textures: backend::PixelFormat::RGBA4
2019-11-23 20:27:39 +08:00
- generate 16-bit textures: backend::PixelFormat::RGB5A1
- generate 16-bit textures: backend::PixelFormat::RGB565
- generate 8-bit textures: backend::PixelFormat::A8 (only use it if you use just 1 color)
How does it work ?
2021-12-25 10:04:45 +08:00
- If the image is an RGBA (with Alpha) then the default pixel format will be used (it can be a 8-bit, 16-bit or
32-bit texture)
- If the image is an RGB (without Alpha) then: If the default pixel format is RGBA8888 then a RGBA8888 (32-bit)
will be used. Otherwise a RGB565 (16-bit texture) will be used.
2019-11-23 20:27:39 +08:00
This parameter is not valid for PVR / PVR.CCZ images.
@since v0.8
*/
static void setDefaultAlphaPixelFormat(backend::PixelFormat format);
/** Returns the alpha pixel format.
@since v0.8
*/
static backend::PixelFormat getDefaultAlphaPixelFormat();
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
public:
/**
* @js ctor
*/
Texture2D();
/**
* @js NA
* @lua NA
*/
virtual ~Texture2D();
/** Initializes with a texture2d with data.
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
@param data Specifies a pointer to the image data in memory.
@param dataLen The image data length.
@param pixelFormat The image pixelFormat.
@param pixelsWide The image width.
@param pixelsHigh The image height.
@param contentSize The image content size.
@param preMultipliedAlpha The texture has premultiplied alpha
* @js NA
* @lua NA
*/
2021-12-25 10:04:45 +08:00
bool initWithData(const void* data,
ssize_t dataLen,
backend::PixelFormat pixelFormat,
int pixelsWide,
int pixelsHigh,
const Vec2& contentSize,
bool preMultipliedAlpha = false)
{
return initWithData(data, dataLen, pixelFormat, pixelFormat, pixelsWide, pixelsHigh, contentSize,
preMultipliedAlpha);
}
2019-11-23 20:27:39 +08:00
/** Initializes with a texture2d with data.
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
@param data Specifies a pointer to the image data in memory.
@param dataLen The image data length.
@param pixelFormat The image pixelFormat.
@param renderFormat The format converted to.
@param pixelsWide The image width.
@param pixelsHigh The image height.
@param contentSize The image content size.
@param preMultipliedAlpha The texture has premultiplied alpha
* @js NA
* @lua NA
*/
2021-12-25 10:04:45 +08:00
bool initWithData(const void* data,
ssize_t dataLen,
backend::PixelFormat pixelFormat,
backend::PixelFormat renderFormat,
int pixelsWide,
int pixelsHigh,
const Vec2& contentSize,
bool preMultipliedAlpha = false);
/** Initializes with mipmaps.
2019-11-23 20:27:39 +08:00
@param mipmaps Specifies a pointer to the image data in memory.
@param mipmapsNum The mipmaps number.
@param pixelFormat The image pixelFormat.
@param pixelsWide The image width.
@param pixelsHigh The image height.
@param preMultipliedAlpha The texture has premultiplied alpha
*/
2021-12-25 10:04:45 +08:00
bool initWithMipmaps(MipmapInfo* mipmaps,
int mipmapsNum,
backend::PixelFormat pixelFormat,
backend::PixelFormat renderFormat,
int pixelsWide,
int pixelsHigh,
bool preMultipliedAlpha = false);
2019-11-23 20:27:39 +08:00
/** Update with image.
@param data Specifies a pointer to the image data in memory.
@param offsetX Specifies a texel offset in the x direction within the texture array.
@param offsetY Specifies a texel offset in the y direction within the texture array.
@param width Specifies the width of the texture subimage.
@param height Specifies the height of the texture subimage.
*/
bool updateWithImage(Image* image, backend::PixelFormat format, int index = 0);
2021-12-25 10:04:45 +08:00
bool updateWithData(const void* data,
ssize_t dataLen,
backend::PixelFormat pixelFormat,
backend::PixelFormat renderFormat,
int pixelsWide,
int pixelsHigh,
const Vec2& /*contentSize*/,
bool preMultipliedAlpha,
int index = 0);
bool updateWithMipmaps(MipmapInfo* mipmaps,
int mipmapsNum,
backend::PixelFormat pixelFormat,
backend::PixelFormat renderFormat,
int pixelsWide,
int pixelsHigh,
bool preMultipliedAlpha = false,
int index = 0);
2019-11-23 20:27:39 +08:00
/** Update with texture data.
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
@param data Specifies a pointer to the image data in memory.
@param offsetX Specifies a texel offset in the x direction within the texture array.
@param offsetY Specifies a texel offset in the y direction within the texture array.
@param width Specifies the width of the texture subimage.
@param height Specifies the height of the texture subimage.
*/
2021-12-25 10:04:45 +08:00
bool updateWithSubData(void* data, int offsetX, int offsetY, int width, int height, int index = 0);
2019-11-23 20:27:39 +08:00
/**
Drawing extensions to make it easy to draw basic quads using a Texture2D object.
2021-12-25 10:04:45 +08:00
These functions require GL_TEXTURE_2D and both GL_VERTEX_ARRAY and GL_TEXTURE_COORD_ARRAY client states to be
enabled.
2019-11-23 20:27:39 +08:00
*/
/** Draws a texture at a given point. */
void drawAtPoint(const Vec2& point, float globalZOrder);
/** Draws a texture inside a rect.*/
void drawInRect(const Rect& rect, float globalZOrder);
/**
Extensions to make it easy to create a Texture2D object from an image file.
*/
2021-12-25 10:04:45 +08:00
/**
2019-11-23 20:27:39 +08:00
Initializes a texture from a UIImage object.
We will use the format you specified with setDefaultAlphaPixelFormat to convert the image for texture.
NOTE: It will not convert the pvr image file.
@param image An UIImage object.
*/
2021-12-25 10:04:45 +08:00
bool initWithImage(Image* image);
/**
2019-11-23 20:27:39 +08:00
Initializes a texture from a UIImage object.
We will use the format you passed to the function to convert the image format to the texture format.
If you pass PixelFormat::NONE, we will auto detect the image render type and use that type for texture to render.
2019-11-23 20:27:39 +08:00
@param image An UIImage object.
@param format Texture pixel formats.
**/
2021-12-25 10:04:45 +08:00
bool initWithImage(Image* image, backend::PixelFormat format);
/** Initializes a texture from a string with dimensions, alignment, font name and font size.
2019-11-23 20:27:39 +08:00
@param text A null terminated string.
@param fontName The font name.
@param fontSize The font size.
@param dimensions The font dimension.
@param hAlignment The font horizontal text alignment type.
@param vAlignment The font vertical text alignment type.
@param enableWrap Whether enable text wrap or not.
@param overflow Whether shrink font size when content larger than the dimensions.
*/
2021-12-25 10:04:45 +08:00
bool initWithString(const char* 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 = true,
int overflow = 0);
2019-11-23 20:27:39 +08:00
/** Initializes a texture from a string using a text definition.
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
@param text A null terminated string.
@param textDefinition A FontDefinition object contains font attributes.
*/
2021-12-25 10:04:45 +08:00
bool initWithString(const char* text, const FontDefinition& textDefinition);
//!!Used for render buffer, such depth stencil attachment
bool updateTextureDescriptor(const backend::TextureDescriptor& descriptor, bool preMultipliedAlpha = false);
2019-11-23 20:27:39 +08:00
void setRenderTarget(bool renderTarget);
inline bool isRenderTarget() const { return _flags & TextureFlag::RENDERTARGET; }
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
void setTexParameters(const TexParams& params);
2019-11-23 20:27:39 +08:00
/** Generates mipmap images for the texture.
It only works if the texture size is POT (power of 2).
@since v0.99.0
*/
void generateMipmap();
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/** Sets antialias texture parameters:
- GL_TEXTURE_MIN_FILTER = GL_LINEAR
- GL_TEXTURE_MAG_FILTER = GL_LINEAR
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
@warning Calling this method could allocate additional texture memory.
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
@since v0.8
*/
void setAntiAliasTexParameters();
/** Sets alias texture parameters:
- GL_TEXTURE_MIN_FILTER = GL_NEAREST
- GL_TEXTURE_MAG_FILTER = GL_NEAREST
@warning Calling this method could allocate additional texture memory.
@since v0.8
*/
void setAliasTexParameters();
/** Returns the pixel format.
@since v2.0
*/
const char* getStringForFormat() const;
/** Returns the bits-per-pixel of the in-memory OpenGL texture
@since v1.0
*/
unsigned int getBitsPerPixelForFormat() const;
/** Helper functions that returns bits per pixels for a given format.
@since v2.0
*/
2020-11-02 16:35:31 +08:00
unsigned int getBitsPerPixelForFormat(backend::PixelFormat format) const;
2019-11-23 20:27:39 +08:00
/** Get content size. */
2021-10-23 23:27:14 +08:00
const Vec2& getContentSizeInPixels();
2019-11-23 20:27:39 +08:00
/** Whether or not the texture has their Alpha premultiplied. */
bool hasPremultipliedAlpha() const;
void setPremultipliedAlpha(bool premultipliedAlpha);
/** Whether or not the texture has mip maps.*/
bool hasMipmaps() const;
/** Gets the pixel format of the texture. */
backend::PixelFormat getPixelFormat() const;
2021-12-25 10:04:45 +08:00
int getSamplerFlags() const { return _samplerFlags; }
2019-11-23 20:27:39 +08:00
/** Gets the width of the texture in pixels. */
int getPixelsWide() const;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/** Gets the height of the texture in pixels. */
int getPixelsHigh() const;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
backend::TextureBackend* getBackendTexture() const;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/** Gets max S. */
float getMaxS() const;
/** Sets max S. */
void setMaxS(float maxS);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/** Gets max T. */
float getMaxT() const;
/** Sets max T. */
void setMaxT(float maxT);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/** Get the texture content size.*/
2021-10-23 23:27:14 +08:00
Vec2 getContentSize() const;
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
std::string getPath() const { return _filePath; }
2019-11-23 20:27:39 +08:00
private:
/**
2021-12-25 10:04:45 +08:00
* A struct for storing 9-patch image capInsets.
*/
2019-11-23 20:27:39 +08:00
class NinePatchInfo
{
public:
Rect capInsetSize;
std::unordered_map<SpriteFrame*, Rect> capInsetMap;
};
/**
* Whether the texture contains a 9-patch capInset info or not.
*
* @return True is Texture contains a 9-patch info, false otherwise.
*/
2021-12-25 10:04:45 +08:00
bool isContain9PatchInfo() const;
2019-11-23 20:27:39 +08:00
/**
* Get spriteFrame capInset, If spriteFrame can't be found in 9-patch info map,
* then single 9-patch texture capInset will be returned.
* If the arg is nullptr, the capInset of single 9-patch texture will be returned.
*
* @param spriteFrame A SpriteFrame object pointer.
*
* @return The capInset of the SpriteFrame object.
*/
2021-12-25 10:04:45 +08:00
const Rect& getSpriteFrameCapInset(SpriteFrame* spriteFrame) const;
2019-11-23 20:27:39 +08:00
/**
* Remove the spriteFrame capInset info when the spriteFrame is removed.
*
* @param spriteFrame A SpriteFrame object pointer.
*/
void removeSpriteFrameCapInset(SpriteFrame* spriteFrame);
/**
* Add capInset for sprite atlas.
* When handling single texture, pass nullptr in the first arg.
*
* @param spritframe The sprite frame object.
* @param capInsets The parsed capInset from a .9 patch image.
*/
void addSpriteFrameCapInset(SpriteFrame* spritframe, const Rect& capInsets);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
void initProgram();
protected:
/** pixel format of the texture */
backend::PixelFormat _pixelFormat;
/** width in pixels */
int _pixelsWide;
/** height in pixels */
int _pixelsHigh;
/** texture name */
backend::Texture2DBackend* _texture;
/** texture max S */
float _maxS;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/** texture max T */
float _maxT;
/** content size */
2021-10-23 23:27:14 +08:00
Vec2 _contentSize;
2019-11-23 20:27:39 +08:00
uint16_t _flags : 16;
uint16_t _samplerFlags : 16;
2019-11-23 20:27:39 +08:00
NinePatchInfo* _ninePatchInfo;
friend class SpriteFrameCache;
friend class TextureCache;
friend class ui::Scale9Sprite;
bool _valid;
std::string _filePath;
backend::ProgramState* _programState = nullptr;
backend::UniformLocation _mvpMatrixLocation;
backend::UniformLocation _textureLocation;
CustomCommand _customCommand;
};
// end of textures group
/// @}
NS_CC_END