This commit is contained in:
halx99 2020-09-11 14:45:45 +08:00
parent a6b3fa91fd
commit c79da1ca05
7 changed files with 34 additions and 11 deletions

View File

@ -127,6 +127,16 @@ ssize_t Data::copy(const unsigned char* bytes, const ssize_t size)
return _size; return _size;
} }
void Data::resize(ssize_t size)
{
if (_size < size) {
auto newmb = (uint8_t*)realloc(_bytes, size);
if (!newmb) return;
_bytes = newmb;
}
_size = size;
}
void Data::fastSet(unsigned char* bytes, const ssize_t size) void Data::fastSet(unsigned char* bytes, const ssize_t size)
{ {
CCASSERT(size >= 0, "fastSet size should be non-negative"); CCASSERT(size >= 0, "fastSet size should be non-negative");

View File

@ -101,6 +101,8 @@ public:
*/ */
ssize_t copy(const unsigned char* bytes, const ssize_t size); ssize_t copy(const unsigned char* bytes, const ssize_t size);
void resize(ssize_t size);
/** Fast set the buffer pointer and its size. Please use it carefully. /** Fast set the buffer pointer and its size. Please use it carefully.
* @param bytes The buffer pointer, note that it have to be allocated by 'malloc' or 'calloc', * @param bytes The buffer pointer, note that it have to be allocated by 'malloc' or 'calloc',
* since in the destructor of Data, the buffer will be deleted by 'free'. * since in the destructor of Data, the buffer will be deleted by 'free'.

View File

@ -105,13 +105,7 @@ class ResizableBufferAdapter<Data> : public ResizableBuffer {
public: public:
explicit ResizableBufferAdapter(BufferType* buffer) : _buffer(buffer) {} explicit ResizableBufferAdapter(BufferType* buffer) : _buffer(buffer) {}
virtual void resize(size_t size) override { virtual void resize(size_t size) override {
size_t oldSize = static_cast<size_t>(_buffer->getSize()); _buffer->resize(size);
if (oldSize != size) {
auto old = _buffer->getBytes();
void* buffer = realloc(old, size);
if (buffer)
_buffer->fastSet((unsigned char*)buffer, size);
}
} }
virtual void* buffer() const override { virtual void* buffer() const override {
return _buffer->getBytes(); return _buffer->getBytes();

View File

@ -30,3 +30,19 @@
#define MAX_COLOR_ATTCHMENT 1 #define MAX_COLOR_ATTCHMENT 1
#define MAX_INFLIGHT_BUFFER 3 #define MAX_INFLIGHT_BUFFER 3
/*
* helps the compiler's optimizer predicting branches
*/
#if __has_builtin(__builtin_expect)
# ifdef __cplusplus
# define UTILS_LIKELY( exp ) (__builtin_expect( !!(exp), true ))
# define UTILS_UNLIKELY( exp ) (__builtin_expect( !!(exp), false ))
# else
# define UTILS_LIKELY( exp ) (__builtin_expect( !!(exp), 1 ))
# define UTILS_UNLIKELY( exp ) (__builtin_expect( !!(exp), 0 ))
# endif
#else
# define UTILS_LIKELY( exp ) (!!(exp))
# define UTILS_UNLIKELY( exp ) (!!(exp))
#endif

View File

@ -17,8 +17,8 @@ struct PixelBufferDescriptor
explicit operator bool() const { return !_data.isNull(); } explicit operator bool() const { return !_data.isNull(); }
Data _data; Data _data;
int _width; int _width = 0;
int _height; int _height = 0;
}; };
CC_BACKEND_END CC_BACKEND_END

View File

@ -51,14 +51,14 @@ struct TextureDescriptor
SamplerDescriptor samplerDescriptor; SamplerDescriptor samplerDescriptor;
}; };
class UtilsGL; struct UtilsGL;
/** /**
* A base texture * A base texture
*/ */
class TextureBackend : public Ref class TextureBackend : public Ref
{ {
friend class UtilsGL; friend struct UtilsGL;
public: public:
/** /**
* Update sampler * Update sampler

View File

@ -27,6 +27,7 @@
#include "base/ccMacros.h" #include "base/ccMacros.h"
#include "platform/CCGL.h" #include "platform/CCGL.h"
#include "renderer/backend/Types.h" #include "renderer/backend/Types.h"
#include "renderer/backend/PixelBufferDescriptor.h"
CC_BACKEND_BEGIN CC_BACKEND_BEGIN
/** /**