From ffa4a7248baf1ce68763f8243a7f25be9feba5f3 Mon Sep 17 00:00:00 2001 From: Hermet Park Date: Wed, 19 Aug 2020 14:50:49 +0900 Subject: [PATCH] code refactoring Now, stabilizing is pretty enough. Remove assert code so that we never abort process in any cases. This also reduce the binary size. Change-Id: Ia7d2d5c5a0757b12481eaebad7a86aade6a89c1e --- src/lib/sw_engine/tvgSwFill.cpp | 11 +-------- src/lib/sw_engine/tvgSwMath.cpp | 2 -- src/lib/sw_engine/tvgSwRenderer.cpp | 2 +- src/lib/sw_engine/tvgSwRle.cpp | 13 ----------- src/lib/sw_engine/tvgSwShape.cpp | 21 ----------------- src/lib/sw_engine/tvgSwStroke.cpp | 36 ++--------------------------- src/lib/tvgCommon.h | 1 - src/lib/tvgGlCanvas.cpp | 5 +--- src/lib/tvgLinearGradient.cpp | 5 +--- src/lib/tvgRadialGradient.cpp | 5 +--- src/lib/tvgRender.cpp | 2 -- src/lib/tvgRender.h | 3 --- src/lib/tvgShapeImpl.h | 3 --- src/lib/tvgShapePath.h | 2 -- src/lib/tvgSwCanvas.cpp | 5 +--- 15 files changed, 8 insertions(+), 108 deletions(-) diff --git a/src/lib/sw_engine/tvgSwFill.cpp b/src/lib/sw_engine/tvgSwFill.cpp index 1539c172..ccc05745 100644 --- a/src/lib/sw_engine/tvgSwFill.cpp +++ b/src/lib/sw_engine/tvgSwFill.cpp @@ -36,11 +36,9 @@ static bool _updateColorTable(SwFill* fill, const Fill* fdata, SwSurface* surface) { - assert(fill && fdata); - if (!fill->ctable) { fill->ctable = static_cast(malloc(GRADIENT_STOP_SIZE * sizeof(uint32_t))); - assert(fill->ctable); + if (!fill->ctable) return false; } const Fill::ColorStop* colors; @@ -71,7 +69,6 @@ static bool _updateColorTable(SwFill* fill, const Fill* fdata, SwSurface* surfac for (uint32_t j = 0; j < cnt - 1; ++j) { auto curr = colors + j; auto next = curr + 1; - assert(curr && next); auto delta = 1.0f / (next->offset - curr->offset); if (next->a < 255) fill->translucent = true; @@ -104,8 +101,6 @@ static bool _updateColorTable(SwFill* fill, const Fill* fdata, SwSurface* surfac bool _prepareLinear(SwFill* fill, const LinearGradient* linear, const Matrix* transform) { - assert(fill && linear); - float x1, x2, y1, y2; if (linear->linear(&x1, &y1, &x2, &y2) != Result::Success) return false; @@ -140,8 +135,6 @@ bool _prepareLinear(SwFill* fill, const LinearGradient* linear, const Matrix* tr bool _prepareRadial(SwFill* fill, const RadialGradient* radial, const Matrix* transform) { - assert(fill && radial); - float radius; if (radial->radial(&fill->radial.cx, &fill->radial.cy, &radius) != Result::Success) return false; if (radius < FLT_EPSILON) return true; @@ -286,8 +279,6 @@ bool fillGenColorTable(SwFill* fill, const Fill* fdata, const Matrix* transform, { if (!fill) return false; - assert(fdata); - fill->spread = fdata->spread(); if (ctable) { diff --git a/src/lib/sw_engine/tvgSwMath.cpp b/src/lib/sw_engine/tvgSwMath.cpp index 5ac3912a..5227d794 100644 --- a/src/lib/sw_engine/tvgSwMath.cpp +++ b/src/lib/sw_engine/tvgSwMath.cpp @@ -384,8 +384,6 @@ SwFixed mathLength(SwPoint& pt) void mathSplitCubic(SwPoint* base) { - assert(base); - SwCoord a, b, c, d; base[6].x = base[3].x; diff --git a/src/lib/sw_engine/tvgSwRenderer.cpp b/src/lib/sw_engine/tvgSwRenderer.cpp index 8d02b404..b173ce47 100644 --- a/src/lib/sw_engine/tvgSwRenderer.cpp +++ b/src/lib/sw_engine/tvgSwRenderer.cpp @@ -101,7 +101,7 @@ void* SwRenderer::prepare(const Shape& sdata, void* data, const RenderTransform* auto shape = static_cast(data); if (!shape) { shape = static_cast(calloc(1, sizeof(SwShape))); - assert(shape); + if (!shape) return nullptr; } if (flags == RenderUpdateFlag::None) return shape; diff --git a/src/lib/sw_engine/tvgSwRle.cpp b/src/lib/sw_engine/tvgSwRle.cpp index fb4b87bd..c9aa6aec 100644 --- a/src/lib/sw_engine/tvgSwRle.cpp +++ b/src/lib/sw_engine/tvgSwRle.cpp @@ -145,8 +145,6 @@ static inline SwCoord HYPOT(SwPoint pt) static void _genSpan(SwRleData* rle, SwSpan* spans, uint32_t count) { - assert(rle && spans); - auto newSize = rle->size + count; /* allocate enough memory for new spans */ @@ -155,12 +153,10 @@ static void _genSpan(SwRleData* rle, SwSpan* spans, uint32_t count) if (rle->alloc < newSize) { rle->alloc = (newSize * 2); rle->spans = static_cast(realloc(rle->spans, rle->alloc * sizeof(SwSpan))); - assert(rle->spans); } //copy the new spans to the allocated memory SwSpan* lastSpan = rle->spans + rle->size; - assert(lastSpan); memcpy(lastSpan, spans, count * sizeof(SwSpan)); rle->size = newSize; @@ -205,7 +201,6 @@ static void _horizLine(RleWorker& rw, SwCoord x, SwCoord y, SwCoord area, SwCoor if (!rw.antiAlias) coverage = 255; auto count = rw.spansCnt; auto span = rw.spans + count - 1; - assert(span); //see whether we can add this span to the current list if ((count > 0) && (rw.ySpan == y) && @@ -226,10 +221,8 @@ static void _horizLine(RleWorker& rw, SwCoord x, SwCoord y, SwCoord area, SwCoor rw.spansCnt = 0; rw.ySpan = 0; span = rw.spans; - assert(span); } else { ++span; - assert(span); } //Clip x range @@ -290,7 +283,6 @@ static Cell* _findCell(RleWorker& rw) if (x > rw.cellXCnt) x = rw.cellXCnt; auto pcell = &rw.yCells[rw.cellPos.y]; - assert(pcell); while(true) { Cell* cell = *pcell; @@ -302,7 +294,6 @@ static Cell* _findCell(RleWorker& rw) if (rw.cellsCnt >= rw.maxCells) longjmp(rw.jmpBuf, 1); auto cell = rw.cells + rw.cellsCnt++; - assert(cell); cell->x = x; cell->area = 0; cell->cover = 0; @@ -317,7 +308,6 @@ static void _recordCell(RleWorker& rw) { if (rw.area | rw.cover) { auto cell = _findCell(rw); - assert(cell); cell->area += rw.area; cell->cover += rw.cover; } @@ -498,8 +488,6 @@ static void _lineTo(RleWorker& rw, const SwPoint& to) static void _cubicTo(RleWorker& rw, const SwPoint& ctrl1, const SwPoint& ctrl2, const SwPoint& to) { auto arc = rw.bezStack; - assert(arc); - arc[0] = to; arc[1] = ctrl2; arc[2] = ctrl1; @@ -668,7 +656,6 @@ SwRleData* rleRender(const SwOutline* outline, const SwBBox& bbox, const SwSize& rw.clip = clip; rw.antiAlias = antiAlias; rw.rle = reinterpret_cast(calloc(1, sizeof(SwRleData))); - assert(rw.rle); //Generate RLE Band bands[BAND_SIZE]; diff --git a/src/lib/sw_engine/tvgSwShape.cpp b/src/lib/sw_engine/tvgSwShape.cpp index c94ef260..f14d3b65 100644 --- a/src/lib/sw_engine/tvgSwShape.cpp +++ b/src/lib/sw_engine/tvgSwShape.cpp @@ -76,7 +76,6 @@ static void _growOutlineContour(SwOutline& outline, uint32_t n) if (outline.reservedCntrsCnt >= outline.cntrsCnt + n) return; outline.reservedCntrsCnt = outline.cntrsCnt + n; outline.cntrs = static_cast(realloc(outline.cntrs, outline.reservedCntrsCnt * sizeof(uint32_t))); - assert(outline.cntrs); } @@ -85,9 +84,7 @@ static void _growOutlinePoint(SwOutline& outline, uint32_t n) if (outline.reservedPtsCnt >= outline.ptsCnt + n) return; outline.reservedPtsCnt = outline.ptsCnt + n; outline.pts = static_cast(realloc(outline.pts, outline.reservedPtsCnt * sizeof(SwPoint))); - assert(outline.pts); outline.types = static_cast(realloc(outline.types, outline.reservedPtsCnt * sizeof(uint8_t))); - assert(outline.types); } @@ -114,8 +111,6 @@ static void _outlineEnd(SwOutline& outline) static void _outlineMoveTo(SwOutline& outline, const Point* to, const Matrix* transform) { - assert(to); - _growOutlinePoint(outline, 1); outline.pts[outline.ptsCnt] = _transform(to, transform); @@ -134,8 +129,6 @@ static void _outlineMoveTo(SwOutline& outline, const Point* to, const Matrix* tr static void _outlineLineTo(SwOutline& outline, const Point* to, const Matrix* transform) { - assert(to); - _growOutlinePoint(outline, 1); outline.pts[outline.ptsCnt] = _transform(to, transform); @@ -146,8 +139,6 @@ static void _outlineLineTo(SwOutline& outline, const Point* to, const Matrix* tr static void _outlineCubicTo(SwOutline& outline, const Point* ctrl1, const Point* ctrl2, const Point* to, const Matrix* transform) { - assert(ctrl1 && ctrl2 && to); - _growOutlinePoint(outline, 3); outline.pts[outline.ptsCnt] = _transform(ctrl1, transform); @@ -203,7 +194,6 @@ static bool _updateBBox(SwOutline* outline, SwBBox& bbox) if (!outline) return false; auto pt = outline->pts; - assert(pt); if (outline->ptsCnt <= 0) { _initBBox(bbox); @@ -236,8 +226,6 @@ static bool _updateBBox(SwOutline* outline, SwBBox& bbox) static bool _checkValid(const SwOutline* outline, const SwBBox& bbox, const SwSize& clip) { - assert(outline); - if (outline->ptsCnt == 0 || outline->cntrsCnt <= 0) return false; //Check boundary @@ -341,8 +329,6 @@ static void _dashCubicTo(SwDashStroke& dash, const Point* ctrl1, const Point* ct SwOutline* _genDashOutline(const Shape* sdata, const Matrix* transform) { - assert(sdata); - const PathCommand* cmds = nullptr; auto cmdCnt = sdata->pathCommands(&cmds); @@ -361,12 +347,10 @@ SwOutline* _genDashOutline(const Shape* sdata, const Matrix* transform) const float* pattern; dash.cnt = sdata->strokeDash(&pattern); - assert(dash.cnt > 0 && pattern); //Is it safe to mutual exclusive? dash.pattern = const_cast(pattern); dash.outline = static_cast(calloc(1, sizeof(SwOutline))); - assert(dash.outline); dash.outline->opened = true; //smart reservation @@ -510,8 +494,6 @@ void shapeReset(SwShape* shape) bool shapeGenOutline(SwShape* shape, const Shape* sdata, const Matrix* transform) { - assert(sdata); - const PathCommand* cmds = nullptr; auto cmdCnt = sdata->pathCommands(&cmds); @@ -552,7 +534,6 @@ bool shapeGenOutline(SwShape* shape, const Shape* sdata, const Matrix* transform auto outline = shape->outline; if (!outline) outline = static_cast(calloc(1, sizeof(SwOutline))); - assert(outline); outline->opened = true; _growOutlinePoint(*outline, outlinePtsCnt); @@ -638,8 +619,6 @@ void shapeResetStroke(SwShape* shape, const Shape* sdata, const Matrix* transfor bool shapeGenStrokeRle(SwShape* shape, const Shape* sdata, const Matrix* transform, const SwSize& clip) { - assert(sdata); - SwOutline* shapeOutline = nullptr; //Dash Style Stroke diff --git a/src/lib/sw_engine/tvgSwStroke.cpp b/src/lib/sw_engine/tvgSwStroke.cpp index 94c738c9..8400237a 100644 --- a/src/lib/sw_engine/tvgSwStroke.cpp +++ b/src/lib/sw_engine/tvgSwStroke.cpp @@ -48,8 +48,6 @@ static inline void SCALE(SwStroke& stroke, SwPoint& pt) static void _growBorder(SwStrokeBorder* border, uint32_t newPts) { - assert(border); - auto maxOld = border->maxPts; auto maxNew = border->ptsCnt + newPts; @@ -61,21 +59,15 @@ static void _growBorder(SwStrokeBorder* border, uint32_t newPts) maxCur += (maxCur >> 1) + 16; border->pts = static_cast(realloc(border->pts, maxCur * sizeof(SwPoint))); - assert(border->pts); - border->tags = static_cast(realloc(border->tags, maxCur * sizeof(uint8_t))); - assert(border->tags); - border->maxPts = maxCur; } static void _borderClose(SwStrokeBorder* border, bool reverse) { - assert(border && border->start >= 0); - - uint32_t start = border->start; - uint32_t count = border->ptsCnt; + auto start = border->start; + auto count = border->ptsCnt; //Don't record empty paths! if (count <= start + 1U) { @@ -123,8 +115,6 @@ static void _borderClose(SwStrokeBorder* border, bool reverse) static void _borderCubicTo(SwStrokeBorder* border, SwPoint& ctrl1, SwPoint& ctrl2, SwPoint& to) { - assert(border && border->start >= 0); - _growBorder(border, 3); auto pt = border->pts + border->ptsCnt; @@ -199,8 +189,6 @@ static void _borderArcTo(SwStrokeBorder* border, SwPoint& center, SwFixed radius static void _borderLineTo(SwStrokeBorder* border, SwPoint& to, bool movable) { - assert(border && border->start >= 0); - if (border->movable) { //move last point border->pts[border->ptsCnt - 1] = to; @@ -221,8 +209,6 @@ static void _borderLineTo(SwStrokeBorder* border, SwPoint& to, bool movable) static void _borderMoveTo(SwStrokeBorder* border, SwPoint& to) { - assert(border); - //close current open path if any? if (border->start >= 0) _borderClose(border, false); @@ -249,10 +235,7 @@ static void _outside(SwStroke& stroke, int32_t side, SwFixed lineLength) { constexpr SwFixed MITER_LIMIT = 4 * (1 << 16); - assert(MITER_LIMIT >= 0x10000); - auto border = stroke.borders + side; - assert(border); if (stroke.join == StrokeJoin::Round) { _arcTo(stroke, side); @@ -640,8 +623,6 @@ static void _addReverseLeft(SwStroke& stroke, bool opened) { auto right = stroke.borders + 0; auto left = stroke.borders + 1; - assert(left->start >= 0); - auto newPts = left->ptsCnt - left->start; if (newPts <= 0) return; @@ -708,7 +689,6 @@ static void _endSubPath(SwStroke& stroke) { if (stroke.openSubPath) { auto right = stroke.borders; - assert(right); /* all right, this is an opened path, we need to add a cap between right & left, add the reverse of left, then add a final cap @@ -756,8 +736,6 @@ static void _endSubPath(SwStroke& stroke) static void _getCounts(SwStrokeBorder* border, uint32_t& ptsCnt, uint32_t& cntrsCnt) { - assert(border); - auto count = border->ptsCnt; auto tags = border->tags; uint32_t _ptsCnt = 0; @@ -796,7 +774,6 @@ fail: static void _exportBorderOutline(SwStroke& stroke, SwOutline* outline, uint32_t side) { auto border = stroke.borders + side; - assert(border); if (!border->valid) return; @@ -850,8 +827,6 @@ void strokeFree(SwStroke* stroke) void strokeReset(SwStroke* stroke, const Shape* sdata, const Matrix* transform) { - assert(sdata); - if (transform) { stroke->sx = sqrt(pow(transform->e11, 2) + pow(transform->e21, 2)); stroke->sy = sqrt(pow(transform->e12, 2) + pow(transform->e22, 2)); @@ -941,16 +916,9 @@ SwOutline* strokeExportOutline(SwStroke* stroke) auto cntrsCnt = count2 + count4; auto outline = static_cast(calloc(1, sizeof(SwOutline))); - assert(outline); - outline->pts = static_cast(malloc(sizeof(SwPoint) * ptsCnt)); - assert(outline->pts); - outline->types = static_cast(malloc(sizeof(uint8_t) * ptsCnt)); - assert(outline->types); - outline->cntrs = static_cast(malloc(sizeof(uint32_t) * cntrsCnt)); - assert(outline->cntrs); _exportBorderOutline(*stroke, outline, 0); //left _exportBorderOutline(*stroke, outline, 1); //right diff --git a/src/lib/tvgCommon.h b/src/lib/tvgCommon.h index 0c73b914..66e08bf5 100644 --- a/src/lib/tvgCommon.h +++ b/src/lib/tvgCommon.h @@ -25,7 +25,6 @@ #include "config.h" #include -#include #include #include #include diff --git a/src/lib/tvgGlCanvas.cpp b/src/lib/tvgGlCanvas.cpp index 1ac57329..e1b5c3c8 100644 --- a/src/lib/tvgGlCanvas.cpp +++ b/src/lib/tvgGlCanvas.cpp @@ -81,10 +81,7 @@ Result GlCanvas::target(uint32_t* buffer, uint32_t stride, uint32_t w, uint32_t unique_ptr GlCanvas::gen() noexcept { #ifdef THORVG_GL_RASTER_SUPPORT - auto canvas = unique_ptr(new GlCanvas); - assert(canvas); - - return canvas; + return unique_ptr(new GlCanvas); #endif return nullptr; } diff --git a/src/lib/tvgLinearGradient.cpp b/src/lib/tvgLinearGradient.cpp index 68a0d8aa..bd2dbc17 100644 --- a/src/lib/tvgLinearGradient.cpp +++ b/src/lib/tvgLinearGradient.cpp @@ -76,10 +76,7 @@ Result LinearGradient::linear(float* x1, float* y1, float* x2, float* y2) const unique_ptr LinearGradient::gen() noexcept { - auto fill = unique_ptr(new LinearGradient); - assert(fill); - - return fill; + return unique_ptr(new LinearGradient); } #endif /* _TVG_LINEAR_GRADIENT_CPP_ */ \ No newline at end of file diff --git a/src/lib/tvgRadialGradient.cpp b/src/lib/tvgRadialGradient.cpp index 179c51e3..9f123bf9 100644 --- a/src/lib/tvgRadialGradient.cpp +++ b/src/lib/tvgRadialGradient.cpp @@ -73,10 +73,7 @@ Result RadialGradient::radial(float* cx, float* cy, float* radius) const noexcep unique_ptr RadialGradient::gen() noexcept { - auto fill = unique_ptr(new RadialGradient); - assert(fill); - - return fill; + return unique_ptr(new RadialGradient); } #endif /* _TVG_RADIAL_GRADIENT_CPP_ */ \ No newline at end of file diff --git a/src/lib/tvgRender.cpp b/src/lib/tvgRender.cpp index b1e8cf0e..30bc906d 100644 --- a/src/lib/tvgRender.cpp +++ b/src/lib/tvgRender.cpp @@ -107,8 +107,6 @@ RenderTransform::RenderTransform() RenderTransform::RenderTransform(const RenderTransform* lhs, const RenderTransform* rhs) { - assert(lhs && rhs); - m.e11 = lhs->m.e11 * rhs->m.e11 + lhs->m.e12 * rhs->m.e21 + lhs->m.e13 * rhs->m.e31; m.e12 = lhs->m.e11 * rhs->m.e12 + lhs->m.e12 * rhs->m.e22 + lhs->m.e13 * rhs->m.e32; m.e13 = lhs->m.e11 * rhs->m.e13 + lhs->m.e12 * rhs->m.e23 + lhs->m.e13 * rhs->m.e33; diff --git a/src/lib/tvgRender.h b/src/lib/tvgRender.h index 3535bf43..c379d0e4 100644 --- a/src/lib/tvgRender.h +++ b/src/lib/tvgRender.h @@ -76,7 +76,6 @@ struct RenderInitializer static bool init(RenderInitializer& renderInit, RenderMethod* engine) { - assert(engine); if (renderInit.pInst || renderInit.refCnt > 0) return false; renderInit.pInst = engine; renderInit.refCnt = 0; @@ -100,7 +99,6 @@ struct RenderInitializer static uint32_t unref(RenderInitializer& renderInit) { - assert(renderInit.refCnt > 0); --renderInit.refCnt; //engine has been requested to termination @@ -115,7 +113,6 @@ struct RenderInitializer static RenderMethod* inst(RenderInitializer& renderInit) { - assert(renderInit.pInst); return renderInit.pInst; } diff --git a/src/lib/tvgShapeImpl.h b/src/lib/tvgShapeImpl.h index 1b85c07b..612bfe34 100644 --- a/src/lib/tvgShapeImpl.h +++ b/src/lib/tvgShapeImpl.h @@ -144,8 +144,6 @@ struct Shape::Impl bool strokeDash(const float* pattern, uint32_t cnt) { - assert(pattern); - if (!stroke) stroke = new ShapeStroke(); if (!stroke) return false; @@ -155,7 +153,6 @@ struct Shape::Impl } if (!stroke->dashPattern) stroke->dashPattern = static_cast(malloc(sizeof(float) * cnt)); - assert(stroke->dashPattern); for (uint32_t i = 0; i < cnt; ++i) stroke->dashPattern[i] = pattern[i]; diff --git a/src/lib/tvgShapePath.h b/src/lib/tvgShapePath.h index 75c86ad6..04934dca 100644 --- a/src/lib/tvgShapePath.h +++ b/src/lib/tvgShapePath.h @@ -50,7 +50,6 @@ struct ShapePath if (cmdCnt <= reservedCmdCnt) return; reservedCmdCnt = cmdCnt; cmds = static_cast(realloc(cmds, sizeof(PathCommand) * reservedCmdCnt)); - assert(cmds); } void reservePts(uint32_t ptsCnt) @@ -58,7 +57,6 @@ struct ShapePath if (ptsCnt <= reservedPtsCnt) return; reservedPtsCnt = ptsCnt; pts = static_cast(realloc(pts, sizeof(Point) * reservedPtsCnt)); - assert(pts); } void grow(uint32_t cmdCnt, uint32_t ptsCnt) diff --git a/src/lib/tvgSwCanvas.cpp b/src/lib/tvgSwCanvas.cpp index 755d422d..5d280fd3 100644 --- a/src/lib/tvgSwCanvas.cpp +++ b/src/lib/tvgSwCanvas.cpp @@ -80,10 +80,7 @@ Result SwCanvas::target(uint32_t* buffer, uint32_t stride, uint32_t w, uint32_t unique_ptr SwCanvas::gen() noexcept { #ifdef THORVG_SW_RASTER_SUPPORT - auto canvas = unique_ptr(new SwCanvas); - assert(canvas); - - return canvas; + return unique_ptr(new SwCanvas); #endif return nullptr; }