mirror of
https://github.com/thorvg/thorvg.git
synced 2025-07-23 22:58:44 +00:00
renderer, loader: minor code refactoring.
- sync with its file name - remove unnecessary section comments - compact binary size (-300) - private Task::run() methods from the loaders
This commit is contained in:
parent
4c95ed3e51
commit
d25d7f348f
15 changed files with 130 additions and 129 deletions
|
@ -22,7 +22,6 @@
|
|||
|
||||
#include <memory.h>
|
||||
#include <turbojpeg.h>
|
||||
#include "tvgLoader.h"
|
||||
#include "tvgJpgLoader.h"
|
||||
|
||||
/************************************************************************/
|
||||
|
|
|
@ -23,6 +23,8 @@
|
|||
#ifndef _TVG_JPG_LOADER_H_
|
||||
#define _TVG_JPG_LOADER_H_
|
||||
|
||||
#include "tvgLoader.h"
|
||||
|
||||
using tjhandle = void*;
|
||||
|
||||
//TODO: Use Task?
|
||||
|
|
|
@ -20,7 +20,6 @@
|
|||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "tvgLoader.h"
|
||||
#include "tvgPngLoader.h"
|
||||
|
||||
/************************************************************************/
|
||||
|
|
|
@ -24,6 +24,7 @@
|
|||
#define _TVG_PNG_LOADER_H_
|
||||
|
||||
#include <png.h>
|
||||
#include "tvgLoader.h"
|
||||
|
||||
class PngLoader : public LoadModule
|
||||
{
|
||||
|
|
|
@ -23,7 +23,6 @@
|
|||
#include <memory.h>
|
||||
#include <webp/decode.h>
|
||||
|
||||
#include "tvgLoader.h"
|
||||
#include "tvgWebpLoader.h"
|
||||
|
||||
|
||||
|
@ -40,6 +39,17 @@ void WebpLoader::clear()
|
|||
}
|
||||
|
||||
|
||||
void WebpLoader::run(unsigned tid)
|
||||
{
|
||||
if (image) {
|
||||
WebPFree(image);
|
||||
image = nullptr;
|
||||
}
|
||||
|
||||
image = WebPDecodeBGRA(data, size, nullptr, nullptr);
|
||||
}
|
||||
|
||||
|
||||
/************************************************************************/
|
||||
/* External Class Implementation */
|
||||
/************************************************************************/
|
||||
|
@ -156,15 +166,4 @@ unique_ptr<Surface> WebpLoader::bitmap()
|
|||
surface->premultiplied = false;
|
||||
surface->owner = true;
|
||||
return unique_ptr<Surface>(surface);
|
||||
}
|
||||
|
||||
|
||||
void WebpLoader::run(unsigned tid)
|
||||
{
|
||||
if (image) {
|
||||
WebPFree(image);
|
||||
image = nullptr;
|
||||
}
|
||||
|
||||
image = WebPDecodeBGRA(data, size, nullptr, nullptr);
|
||||
}
|
||||
}
|
|
@ -23,6 +23,7 @@
|
|||
#ifndef _TVG_WEBP_LOADER_H_
|
||||
#define _TVG_WEBP_LOADER_H_
|
||||
|
||||
#include "tvgLoader.h"
|
||||
#include "tvgTaskScheduler.h"
|
||||
|
||||
class WebpLoader : public LoadModule, public Task
|
||||
|
@ -38,10 +39,10 @@ public:
|
|||
bool close() override;
|
||||
|
||||
unique_ptr<Surface> bitmap() override;
|
||||
void run(unsigned tid) override;
|
||||
|
||||
private:
|
||||
void clear();
|
||||
void run(unsigned tid) override;
|
||||
|
||||
unsigned char* data = nullptr;
|
||||
unsigned char *image = nullptr;
|
||||
|
|
|
@ -21,7 +21,6 @@
|
|||
*/
|
||||
|
||||
#include <memory.h>
|
||||
#include "tvgLoader.h"
|
||||
#include "tvgJpgLoader.h"
|
||||
|
||||
/************************************************************************/
|
||||
|
@ -38,6 +37,16 @@ void JpgLoader::clear()
|
|||
}
|
||||
|
||||
|
||||
void JpgLoader::run(unsigned tid)
|
||||
{
|
||||
if (image) {
|
||||
free(image);
|
||||
image = nullptr;
|
||||
}
|
||||
image = jpgdDecompress(decoder);
|
||||
}
|
||||
|
||||
|
||||
/************************************************************************/
|
||||
/* External Class Implementation */
|
||||
/************************************************************************/
|
||||
|
@ -130,14 +139,4 @@ unique_ptr<Surface> JpgLoader::bitmap()
|
|||
surface->owner = true;
|
||||
|
||||
return unique_ptr<Surface>(surface);
|
||||
}
|
||||
|
||||
|
||||
void JpgLoader::run(unsigned tid)
|
||||
{
|
||||
if (image) {
|
||||
free(image);
|
||||
image = nullptr;
|
||||
}
|
||||
image = jpgdDecompress(decoder);
|
||||
}
|
||||
}
|
|
@ -23,6 +23,7 @@
|
|||
#ifndef _TVG_JPG_LOADER_H_
|
||||
#define _TVG_JPG_LOADER_H_
|
||||
|
||||
#include "tvgLoader.h"
|
||||
#include "tvgTaskScheduler.h"
|
||||
#include "tvgJpgd.h"
|
||||
|
||||
|
@ -35,6 +36,7 @@ private:
|
|||
bool freeData = false;
|
||||
|
||||
void clear();
|
||||
void run(unsigned tid) override;
|
||||
|
||||
public:
|
||||
~JpgLoader();
|
||||
|
@ -46,7 +48,6 @@ public:
|
|||
bool close() override;
|
||||
|
||||
unique_ptr<Surface> bitmap() override;
|
||||
void run(unsigned tid) override;
|
||||
};
|
||||
|
||||
#endif //_TVG_JPG_LOADER_H_
|
||||
|
|
|
@ -40,6 +40,21 @@ void PngLoader::clear()
|
|||
}
|
||||
|
||||
|
||||
void PngLoader::run(unsigned tid)
|
||||
{
|
||||
if (image) {
|
||||
free(image);
|
||||
image = nullptr;
|
||||
}
|
||||
auto width = static_cast<unsigned>(w);
|
||||
auto height = static_cast<unsigned>(h);
|
||||
|
||||
if (lodepng_decode(&image, &width, &height, &state, data, size)) {
|
||||
TVGERR("PNG", "Failed to decode image");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/************************************************************************/
|
||||
/* External Class Implementation */
|
||||
/************************************************************************/
|
||||
|
@ -167,19 +182,4 @@ unique_ptr<Surface> PngLoader::bitmap()
|
|||
surface->owner = true;
|
||||
|
||||
return unique_ptr<Surface>(surface);
|
||||
}
|
||||
|
||||
|
||||
void PngLoader::run(unsigned tid)
|
||||
{
|
||||
if (image) {
|
||||
free(image);
|
||||
image = nullptr;
|
||||
}
|
||||
auto width = static_cast<unsigned>(w);
|
||||
auto height = static_cast<unsigned>(h);
|
||||
|
||||
if (lodepng_decode(&image, &width, &height, &state, data, size)) {
|
||||
TVGERR("PNG", "Failed to decode image");
|
||||
}
|
||||
}
|
||||
}
|
|
@ -37,6 +37,7 @@ private:
|
|||
bool freeData = false;
|
||||
|
||||
void clear();
|
||||
void run(unsigned tid) override;
|
||||
|
||||
public:
|
||||
PngLoader();
|
||||
|
@ -49,7 +50,6 @@ public:
|
|||
bool close() override;
|
||||
|
||||
unique_ptr<Surface> bitmap() override;
|
||||
void run(unsigned tid) override;
|
||||
};
|
||||
|
||||
#endif //_TVG_PNG_LOADER_H_
|
||||
|
|
|
@ -20,14 +20,11 @@
|
|||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#ifndef _TVG_CANVAS_IMPL_H_
|
||||
#define _TVG_CANVAS_IMPL_H_
|
||||
#ifndef _TVG_CANVAS_H_
|
||||
#define _TVG_CANVAS_H_
|
||||
|
||||
#include "tvgPaint.h"
|
||||
|
||||
/************************************************************************/
|
||||
/* Internal Class Implementation */
|
||||
/************************************************************************/
|
||||
|
||||
struct Canvas::Impl
|
||||
{
|
||||
|
@ -151,4 +148,4 @@ struct Canvas::Impl
|
|||
}
|
||||
};
|
||||
|
||||
#endif /* _TVG_CANVAS_IMPL_H_ */
|
||||
#endif /* _TVG_CANVAS_H_ */
|
||||
|
|
|
@ -55,6 +55,71 @@ RenderUpdateFlag Picture::Impl::load()
|
|||
return RenderUpdateFlag::None;
|
||||
}
|
||||
|
||||
|
||||
bool Picture::Impl::needComposition(uint8_t opacity)
|
||||
{
|
||||
//In this case, paint(scene) would try composition itself.
|
||||
if (opacity < 255) return false;
|
||||
|
||||
//Composition test
|
||||
const Paint* target;
|
||||
auto method = picture->composite(&target);
|
||||
if (!target || method == tvg::CompositeMethod::ClipPath) return false;
|
||||
if (target->pImpl->opacity == 255 || target->pImpl->opacity == 0) return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
bool Picture::Impl::render(RenderMethod &renderer)
|
||||
{
|
||||
bool ret = false;
|
||||
if (surface) return renderer.renderImage(rd);
|
||||
else if (paint) {
|
||||
Compositor* cmp = nullptr;
|
||||
if (needComp) {
|
||||
cmp = renderer.target(bounds(renderer), renderer.colorSpace());
|
||||
renderer.beginComposite(cmp, CompositeMethod::None, 255);
|
||||
}
|
||||
ret = paint->pImpl->render(renderer);
|
||||
if (cmp) renderer.endComposite(cmp);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
bool Picture::Impl::size(float w, float h)
|
||||
{
|
||||
this->w = w;
|
||||
this->h = h;
|
||||
resizing = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
RenderRegion Picture::Impl::bounds(RenderMethod& renderer)
|
||||
{
|
||||
if (rd) return renderer.region(rd);
|
||||
if (paint) return paint->pImpl->bounds(renderer);
|
||||
return {0, 0, 0, 0};
|
||||
}
|
||||
|
||||
|
||||
RenderTransform Picture::Impl::resizeTransform(const RenderTransform* pTransform)
|
||||
{
|
||||
//Overriding Transformation by the desired image size
|
||||
auto sx = w / loader->w;
|
||||
auto sy = h / loader->h;
|
||||
auto scale = sx < sy ? sx : sy;
|
||||
|
||||
RenderTransform tmp;
|
||||
tmp.m = {scale, 0, 0, 0, scale, 0, 0, 0, 1};
|
||||
|
||||
if (!pTransform) return tmp;
|
||||
else return RenderTransform(pTransform, &tmp);
|
||||
}
|
||||
|
||||
|
||||
/************************************************************************/
|
||||
/* External Class Implementation */
|
||||
/************************************************************************/
|
||||
|
|
|
@ -20,16 +20,13 @@
|
|||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#ifndef _TVG_PICTURE_IMPL_H_
|
||||
#define _TVG_PICTURE_IMPL_H_
|
||||
#ifndef _TVG_PICTURE_H_
|
||||
#define _TVG_PICTURE_H_
|
||||
|
||||
#include <string>
|
||||
#include "tvgPaint.h"
|
||||
#include "tvgLoader.h"
|
||||
|
||||
/************************************************************************/
|
||||
/* Internal Class Implementation */
|
||||
/************************************************************************/
|
||||
|
||||
struct PictureIterator : Iterator
|
||||
{
|
||||
|
@ -71,6 +68,12 @@ struct Picture::Impl
|
|||
bool resizing = false;
|
||||
bool needComp = false; //need composition
|
||||
|
||||
RenderTransform resizeTransform(const RenderTransform* pTransform);
|
||||
bool needComposition(uint8_t opacity);
|
||||
bool render(RenderMethod &renderer);
|
||||
bool size(float w, float h);
|
||||
RenderRegion bounds(RenderMethod& renderer);
|
||||
|
||||
Impl(Picture* p) : picture(p)
|
||||
{
|
||||
}
|
||||
|
@ -89,34 +92,6 @@ struct Picture::Impl
|
|||
return true;
|
||||
}
|
||||
|
||||
RenderTransform resizeTransform(const RenderTransform* pTransform)
|
||||
{
|
||||
//Overriding Transformation by the desired image size
|
||||
auto sx = w / loader->w;
|
||||
auto sy = h / loader->h;
|
||||
auto scale = sx < sy ? sx : sy;
|
||||
|
||||
RenderTransform tmp;
|
||||
tmp.m = {scale, 0, 0, 0, scale, 0, 0, 0, 1};
|
||||
|
||||
if (!pTransform) return tmp;
|
||||
else return RenderTransform(pTransform, &tmp);
|
||||
}
|
||||
|
||||
bool needComposition(uint8_t opacity)
|
||||
{
|
||||
//In this case, paint(scene) would try composition itself.
|
||||
if (opacity < 255) return false;
|
||||
|
||||
//Composition test
|
||||
const Paint* target;
|
||||
auto method = picture->composite(&target);
|
||||
if (!target || method == tvg::CompositeMethod::ClipPath) return false;
|
||||
if (target->pImpl->opacity == 255 || target->pImpl->opacity == 0) return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
RenderData update(RenderMethod &renderer, const RenderTransform* pTransform, Array<RenderData>& clips, uint8_t opacity, RenderUpdateFlag pFlag, bool clipper)
|
||||
{
|
||||
auto flag = load();
|
||||
|
@ -135,30 +110,6 @@ struct Picture::Impl
|
|||
return rd;
|
||||
}
|
||||
|
||||
bool render(RenderMethod &renderer)
|
||||
{
|
||||
bool ret = false;
|
||||
if (surface) return renderer.renderImage(rd);
|
||||
else if (paint) {
|
||||
Compositor* cmp = nullptr;
|
||||
if (needComp) {
|
||||
cmp = renderer.target(bounds(renderer), renderer.colorSpace());
|
||||
renderer.beginComposite(cmp, CompositeMethod::None, 255);
|
||||
}
|
||||
ret = paint->pImpl->render(renderer);
|
||||
if (cmp) renderer.endComposite(cmp);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool size(float w, float h)
|
||||
{
|
||||
this->w = w;
|
||||
this->h = h;
|
||||
resizing = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool bounds(float* x, float* y, float* w, float* h, bool stroking)
|
||||
{
|
||||
if (rm.triangleCnt > 0) {
|
||||
|
@ -195,13 +146,6 @@ struct Picture::Impl
|
|||
return true;
|
||||
}
|
||||
|
||||
RenderRegion bounds(RenderMethod& renderer)
|
||||
{
|
||||
if (rd) return renderer.region(rd);
|
||||
if (paint) return paint->pImpl->bounds(renderer);
|
||||
return {0, 0, 0, 0};
|
||||
}
|
||||
|
||||
Result load(const string& path)
|
||||
{
|
||||
if (paint || surface) return Result::InsufficientCondition;
|
||||
|
@ -308,4 +252,4 @@ struct Picture::Impl
|
|||
RenderUpdateFlag load();
|
||||
};
|
||||
|
||||
#endif //_TVG_PICTURE_IMPL_H_
|
||||
#endif //_TVG_PICTURE_H_
|
||||
|
|
|
@ -20,15 +20,12 @@
|
|||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#ifndef _TVG_SCENE_IMPL_H_
|
||||
#define _TVG_SCENE_IMPL_H_
|
||||
#ifndef _TVG_SCENE_H_
|
||||
#define _TVG_SCENE_H_
|
||||
|
||||
#include <float.h>
|
||||
#include "tvgPaint.h"
|
||||
|
||||
/************************************************************************/
|
||||
/* Internal Class Implementation */
|
||||
/************************************************************************/
|
||||
|
||||
struct SceneIterator : Iterator
|
||||
{
|
||||
|
@ -247,4 +244,4 @@ struct Scene::Impl
|
|||
}
|
||||
};
|
||||
|
||||
#endif //_TVG_SCENE_IMPL_H_
|
||||
#endif //_TVG_SCENE_H_
|
||||
|
|
|
@ -20,16 +20,13 @@
|
|||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#ifndef _TVG_SHAPE_IMPL_H_
|
||||
#define _TVG_SHAPE_IMPL_H_
|
||||
#ifndef _TVG_SHAPE_H_
|
||||
#define _TVG_SHAPE_H_
|
||||
|
||||
#include <memory.h>
|
||||
#include "tvgMath.h"
|
||||
#include "tvgPaint.h"
|
||||
|
||||
/************************************************************************/
|
||||
/* Internal Class Implementation */
|
||||
/************************************************************************/
|
||||
|
||||
struct Shape::Impl
|
||||
{
|
||||
|
@ -388,4 +385,4 @@ struct Shape::Impl
|
|||
}
|
||||
};
|
||||
|
||||
#endif //_TVG_SHAPE_IMPL_H_
|
||||
#endif //_TVG_SHAPE_H_
|
||||
|
|
Loading…
Add table
Reference in a new issue