Skip to content

Commit

Permalink
[core] Improve memory texture handling
Browse files Browse the repository at this point in the history
[core] Delay TTBM cached chars uploading to GPU until draw time (saves
extraneous uploads during initialization)
[html5] disable assertions
  • Loading branch information
hgy29 committed Jan 6, 2020
1 parent 837d77b commit c0f80cb
Show file tree
Hide file tree
Showing 10 changed files with 190 additions and 110 deletions.
69 changes: 44 additions & 25 deletions 2dsg/dib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "giderosexception.h"
#include <gimage.h>
#include <gstdio.h>
#include <string.h>

static unsigned int nextpow2(unsigned int v)
{
Expand All @@ -21,6 +22,20 @@ static unsigned int nextpow2(unsigned int v)
return v;
}

Dib::Dib(const Dib &dib)
{
originalWidth_ = dib.originalWidth_;
originalHeight_ =dib.originalHeight_;
width_ = dib.width_;
height_ = dib.height_;
scale_=dib.scale_;
baseOriginalWidth_ = dib.baseOriginalWidth_;
baseOriginalHeight_ = dib.originalHeight_;

data_ = new unsigned char[width_ * height_ * 4];
memcpy(data_,dib.data_,width_ * height_ * 4);
}

Dib::Dib(Application* application,
int width, int height,
bool pow2, float scale)
Expand All @@ -43,25 +58,29 @@ Dib::Dib(Application* application,
baseOriginalWidth_ = originalWidth_/scale_;
baseOriginalHeight_ = originalHeight_/scale_;

data_.resize(width_ * height_ * 4, 0);
data_ = new unsigned char[width_ * height_ * 4];
}

static void check(int result, const char* file)
static void check(int result, const char* file, unsigned char *buf)
{
switch (result)
{
case GIMAGE_NO_ERROR:
break;
case GIMAGE_CANNOT_OPEN_FILE:
if (buf!=NULL) delete[] buf;
throw GiderosException(GStatus(6000, file));
break;
case GIMAGE_UNRECOGNIZED_FORMAT:
if (buf!=NULL) delete[] buf;
throw GiderosException(GStatus(6005, file));
break;
case GIMAGE_ERROR_WHILE_READING:
if (buf!=NULL) delete[] buf;
throw GiderosException(GStatus(6013, file));
break;
case GIMAGE_UNSUPPORTED_COLOR_SPACE:
if (buf!=NULL) delete[] buf;
throw GiderosException(GStatus(6014, file));
break;
}
Expand Down Expand Up @@ -92,13 +111,13 @@ Dib::Dib(Application* application,

filename = std::string(file, ext - file) + (suffix ? suffix : "") + ext;

check(gimage_parseImage(filename.c_str(), &width2, &height2, &comp), filename.c_str());
check(gimage_parseImage(filename.c_str(), &width2, &height2, &comp), filename.c_str(),NULL);

G_FILE *fis = g_fopen(file, "rb");
if (fis)
{
g_fclose(fis);
check(gimage_parseImage(file, &width1, &height1, NULL), file);
check(gimage_parseImage(file, &width1, &height1, NULL), file,NULL);
}
else
{
Expand All @@ -108,7 +127,7 @@ Dib::Dib(Application* application,
}
else
{
check(gimage_parseImage(file, &width1, &height1, &comp), file);
check(gimage_parseImage(file, &width1, &height1, &comp), file,NULL);
filename = file;
width2 = width1;
height2 = height1;
Expand All @@ -131,19 +150,19 @@ Dib::Dib(Application* application,
height_ = originalHeight_;
}

std::vector<unsigned char> buf(originalWidth_ * originalHeight_ * comp);
unsigned char *buf=new unsigned char[originalWidth_ * originalHeight_ * comp];

check(gimage_loadImage(filename.c_str(), &buf[0]), filename.c_str());
check(gimage_loadImage(filename.c_str(), buf), filename.c_str(),buf);

data_.resize(width_ * height_ * 4);
data_ = new unsigned char[width_ * height_ * 4];

if (comp == 4)
{// optimization for 4 components case.
for (int y = 0; y < originalHeight_; ++y)
{
int srcindex = (y * originalWidth_) * 4;
int dstindex = (y * width_) * 4;
std::copy((const unsigned int*)&buf[srcindex], (const unsigned int*)(buf.data() + srcindex + originalWidth_ * 4),
std::copy((const unsigned int*)&buf[srcindex], (const unsigned int*)(buf + srcindex + originalWidth_ * 4),
(unsigned int*)&data_[dstindex]);
}
}
Expand Down Expand Up @@ -262,20 +281,20 @@ void Dib::intelligentFill()

void Dib::premultiplyAlpha()
{
gimage_premultiplyAlpha(width_, height_, &data_[0]);
gimage_premultiplyAlpha(width_, height_, data_);
}

void Dib::convertGrayscale()
{
unsigned int* iptr = (unsigned int*)&data_[0];
unsigned int* iptr = (unsigned int*)data_;

for (int i = 0; i < width_ * height_; ++i)
iptr[i] = ((iptr[i] ^ 0x00ff0000) << 8) | 0x00ffffff;
}

std::vector<unsigned short> Dib::to565() const
unsigned short *Dib::to565() const
{
std::vector<unsigned short> result(width_ * height_);
unsigned short *result=new unsigned short[width_ * height_];

for (int y = 0; y < height_; ++y)
for (int x = 0; x < width_; ++x)
Expand All @@ -292,9 +311,9 @@ std::vector<unsigned short> Dib::to565() const
return result;
}

std::vector<unsigned char> Dib::to888() const
unsigned char *Dib::to888() const
{
std::vector<unsigned char> result(width_ * height_ * 3);
unsigned char *result=new unsigned char[width_ * height_ * 3];

for (int y = 0; y < height_; ++y)
for (int x = 0; x < width_; ++x)
Expand All @@ -308,9 +327,9 @@ std::vector<unsigned char> Dib::to888() const
return result;
}

std::vector<unsigned char> Dib::toY8() const
unsigned char *Dib::toY8() const
{
std::vector<unsigned char> result(width_ * height_);
unsigned char *result=new unsigned char[width_ * height_];

for (int y = 0; y < height_; ++y)
for (int x = 0; x < width_; ++x)
Expand All @@ -322,9 +341,9 @@ std::vector<unsigned char> Dib::toY8() const
return result;
}

std::vector<unsigned char> Dib::toA8() const
unsigned char *Dib::toA8() const
{
std::vector<unsigned char> result(width_ * height_);
unsigned char *result=new unsigned char[width_ * height_];

for (int y = 0; y < height_; ++y)
for (int x = 0; x < width_; ++x)
Expand All @@ -336,9 +355,9 @@ std::vector<unsigned char> Dib::toA8() const
return result;
}

std::vector<unsigned char> Dib::toYA8() const
unsigned char *Dib::toYA8() const
{
std::vector<unsigned char> result(width_ * height_ * 2);
unsigned char *result=new unsigned char[width_ * height_ * 2];

for (int y = 0; y < height_; ++y)
for (int x = 0; x < width_; ++x)
Expand All @@ -352,9 +371,9 @@ std::vector<unsigned char> Dib::toYA8() const
}


std::vector<unsigned short> Dib::to4444() const
unsigned short *Dib::to4444() const
{
std::vector<unsigned short> result(width_ * height_);
unsigned short *result=new unsigned short[width_ * height_];

for (int y = 0; y < height_; ++y)
for (int x = 0; x < width_; ++x)
Expand All @@ -374,9 +393,9 @@ std::vector<unsigned short> Dib::to4444() const
}


std::vector<unsigned short> Dib::to5551() const
unsigned short *Dib::to5551() const
{
std::vector<unsigned short> result(width_ * height_);
unsigned short *result=new unsigned short[width_ * height_];

for (int y = 0; y < height_; ++y)
for (int x = 0; x < width_; ++x)
Expand Down
28 changes: 15 additions & 13 deletions 2dsg/dib.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#ifndef DIB_H
#define DIB_H

#include <vector>
#include <algorithm>
#include "refptr.h"

class Application;
Expand All @@ -23,6 +21,10 @@ class Dib
bool maketransparent,
unsigned int transparentcolor);

Dib(const Dib &dib);

~Dib() { delete[] data_; };

void fill(unsigned char rgba[4])
{
int n = width_ * height_;
Expand Down Expand Up @@ -50,7 +52,7 @@ class Dib

unsigned char *dataArray()
{
return &data_[0];
return data_;
}

int originalWidth() const
Expand Down Expand Up @@ -80,12 +82,12 @@ class Dib

const unsigned char* data() const
{
return &data_[0];
return data_;
}

unsigned char* data()
{
return &data_[0];
return data_;
}

void getPixel(int x, int y, unsigned char rgba[4])
Expand Down Expand Up @@ -132,16 +134,16 @@ class Dib

void convertGrayscale();

std::vector<unsigned char> to888() const;
std::vector<unsigned short> to565() const;
std::vector<unsigned short> to4444() const;
std::vector<unsigned short> to5551() const;
std::vector<unsigned char> toY8() const;
std::vector<unsigned char> toA8() const;
std::vector<unsigned char> toYA8() const;
unsigned char *to888() const;
unsigned short *to565() const;
unsigned short *to4444() const;
unsigned short *to5551() const;
unsigned char *toY8() const;
unsigned char *toA8() const;
unsigned char *toYA8() const;

private:
std::vector<unsigned char> data_;
unsigned char *data_;

int width_;
int height_;
Expand Down
7 changes: 7 additions & 0 deletions 2dsg/fontbase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,13 @@ CompositeFont::~CompositeFont()
}
}

void CompositeFont::preDraw() {
std::vector<CompositeFontSpec>::iterator bit = fonts_.begin();
for (std::vector<CompositeFontSpec>::iterator it=bit;it != fonts_.end(); it++) {
it->font->preDraw();
}
}

void CompositeFont::drawText(std::vector<GraphicsBase> *graphicsBase, const char *text, float r, float g, float b, float a, TextLayoutParameters *layout, bool hasSample, float minx, float miny,TextLayout &l)
{
std::vector<CompositeFontSpec>::iterator bit = fonts_.begin();
Expand Down
2 changes: 2 additions & 0 deletions 2dsg/fontbase.h
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ class BMFontBase : public FontBase
}

virtual void drawText(std::vector<GraphicsBase> *graphicsBase, const char *text, float r, float g, float b, float a, TextLayoutParameters *layout, bool hasSample, float minx, float miny, TextLayout &l) = 0;
virtual void preDraw() {}; //Called when some text is about to be actually rendered, to flush data to the GPU
};

class CompositeFont : public BMFontBase
Expand All @@ -175,6 +176,7 @@ class CompositeFont : public BMFontBase
virtual float getAscender();
virtual float getDescender();
virtual float getLineHeight();
virtual void preDraw();
protected:
std::vector<CompositeFontSpec> fonts_;
struct FontInfo
Expand Down
2 changes: 2 additions & 0 deletions 2dsg/textfield.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,8 @@ void TextField::doDraw(const CurrentTransform&, float sx, float sy, float ex, fl
G_UNUSED(ex);
G_UNUSED(ey);
if (scaleChanged()) createGraphics();
if (font_ != NULL)
font_->preDraw();
for (std::vector<GraphicsBase>::iterator it=graphicsBase_.begin();it!=graphicsBase_.end();it++)
(*it).draw(shader_);
}
Expand Down
Loading

0 comments on commit c0f80cb

Please sign in to comment.