Skip to content

Commit

Permalink
Add the Tracy Profiler library to monitor performance (#335)
Browse files Browse the repository at this point in the history
  • Loading branch information
illidanstormrange authored Nov 25, 2024
1 parent 4b75a70 commit cb9edd7
Show file tree
Hide file tree
Showing 49 changed files with 246 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .github/workflows/autotest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ jobs:
run: |
chmod +x autotest.sh
./autotest.sh
env:
TRACY_NO_INVARIANT_CHECK: 1

- name: Run Codecov
run: |
Expand Down
14 changes: 14 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ option(TGFX_BUILD_SVG "Allow build the svg module" OFF)
option(TGFX_BUILD_DRAWERS "Build the tgfx-drawers library" OFF)
option(EMSCRIPTEN_PTHREADS "Allow the use of threads on the web platform" OFF)

if (CMAKE_BUILD_TYPE MATCHES "Debug")
option(TGFX_ENABLE_PROFILING "Enable Profiling" ON)
endif ()

if (APPLE OR (WEB AND NOT EMSCRIPTEN_PTHREADS))
option(TGFX_USE_FREETYPE "Allow the use of embedded freetype library" OFF)
Expand Down Expand Up @@ -72,6 +75,7 @@ message("TGFX_USE_JPEG_ENCODE: ${TGFX_USE_JPEG_ENCODE}")
message("TGFX_USE_WEBP_DECODE: ${TGFX_USE_WEBP_DECODE}")
message("TGFX_USE_WEBP_ENCODE: ${TGFX_USE_WEBP_ENCODE}")
message("TGFX_BUILD_TESTS: ${TGFX_BUILD_TESTS}")
message("TGFX_ENABLE_PROFILING: ${TGFX_ENABLE_PROFILING}")
message("EMSCRIPTEN_PTHREADS: ${EMSCRIPTEN_PTHREADS}")

if (NOT CMAKE_OSX_DEPLOYMENT_TARGET)
Expand Down Expand Up @@ -260,6 +264,12 @@ else ()
set(TGFX_USE_NATIVE_GL ON)
endif ()

if (TGFX_ENABLE_PROFILING)
list(APPEND TGFX_STATIC_VENDORS tracy)
list(APPEND TGFX_INCLUDES third_party/tracy)
list(APPEND TGFX_DEFINES TGFX_ENABLE_PROFILING)
endif ()

list(APPEND TGFX_STATIC_VENDORS pathkit)
list(APPEND TGFX_INCLUDES third_party/pathkit)
list(APPEND TGFX_STATIC_VENDORS skcms)
Expand Down Expand Up @@ -478,6 +488,10 @@ if (TGFX_BUILD_TESTS)
endif ()
endif ()

if (TGFX_ENABLE_PROFILING)
list(APPEND TGFX_TEST_INCLUDES third_party/tracy)
endif ()

file(GLOB_RECURSE TGFX_TEST_FILES test/src/*.*)
list(APPEND TGFX_TEST_LIBS tgfx-drawers tgfx ${TGFX_SHARED_LIBS})
list(APPEND TGFX_TEST_INCLUDES src test/src third_party/json/include)
Expand Down
5 changes: 5 additions & 0 deletions DEPS
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@
"url": "https://github.com/libexpat/libexpat.git",
"commit": "88b3ed553d8ad335559254863a33360d55b9f1d6",
"dir": "third_party/expat"
},
{
"url": "https://github.com/wolfpld/tracy.git",
"commit": "759b4c3bfe207ab382c01080f0417aca5af4411a",
"dir": "third_party/tracy"
}
]
},
Expand Down
17 changes: 17 additions & 0 deletions src/core/Canvas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "core/LayerUnrollContext.h"
#include "core/Records.h"
#include "core/utils/Log.h"
#include "core/utils/Profiling.h"
#include "tgfx/core/PathEffect.h"
#include "tgfx/core/Surface.h"

Expand Down Expand Up @@ -166,6 +167,7 @@ static FillStyle CreateFillStyle(const Paint* paint) {
}

void Canvas::drawLine(float x0, float y0, float x1, float y1, const Paint& paint) {
TRACE_EVENT;
Path path = {};
path.moveTo(x0, y0);
path.lineTo(x1, y1);
Expand All @@ -175,6 +177,7 @@ void Canvas::drawLine(float x0, float y0, float x1, float y1, const Paint& paint
}

void Canvas::drawRect(const Rect& rect, const Paint& paint) {
TRACE_EVENT;
if (paint.getStroke()) {
Path path = {};
path.addRect(rect);
Expand All @@ -189,24 +192,28 @@ void Canvas::drawRect(const Rect& rect, const Paint& paint) {
}

void Canvas::drawOval(const Rect& oval, const Paint& paint) {
TRACE_EVENT;
RRect rRect = {};
rRect.setOval(oval);
drawRRect(rRect, paint);
}

void Canvas::drawCircle(float centerX, float centerY, float radius, const Paint& paint) {
TRACE_EVENT;
Rect rect =
Rect::MakeLTRB(centerX - radius, centerY - radius, centerX + radius, centerY + radius);
drawOval(rect, paint);
}

void Canvas::drawRoundRect(const Rect& rect, float radiusX, float radiusY, const Paint& paint) {
TRACE_EVENT;
RRect rRect = {};
rRect.setRectXY(rect, radiusX, radiusY);
drawRRect(rRect, paint);
}

void Canvas::drawRRect(const RRect& rRect, const Paint& paint) {
TRACE_EVENT;
if (rRect.radii.isZero()) {
drawRect(rRect.rect, paint);
return;
Expand All @@ -225,11 +232,13 @@ void Canvas::drawRRect(const RRect& rRect, const Paint& paint) {
}

void Canvas::drawPath(const Path& path, const Paint& paint) {
TRACE_EVENT;
auto shape = Shape::MakeFrom(path);
drawShape(std::move(shape), paint);
}

void Canvas::drawShape(std::shared_ptr<Shape> shape, const Paint& paint) {
TRACE_EVENT;
if (shape == nullptr || paint.nothingToDraw()) {
return;
}
Expand Down Expand Up @@ -286,6 +295,7 @@ void Canvas::drawImage(std::shared_ptr<Image> image, const SamplingOptions& samp

void Canvas::drawImage(std::shared_ptr<Image> image, const SamplingOptions& sampling,
const Paint* paint, const Matrix* extraMatrix) {
TRACE_EVENT;
if (image == nullptr || (paint && paint->nothingToDraw())) {
return;
}
Expand All @@ -309,6 +319,7 @@ void Canvas::drawImage(std::shared_ptr<Image> image, const SamplingOptions& samp

void Canvas::drawSimpleText(const std::string& text, float x, float y, const Font& font,
const Paint& paint) {
TRACE_EVENT;
if (text.empty()) {
return;
}
Expand All @@ -318,6 +329,7 @@ void Canvas::drawSimpleText(const std::string& text, float x, float y, const Fon

void Canvas::drawGlyphs(const GlyphID glyphs[], const Point positions[], size_t glyphCount,
const Font& font, const Paint& paint) {
TRACE_EVENT;
if (glyphCount == 0 || paint.nothingToDraw()) {
return;
}
Expand All @@ -329,6 +341,7 @@ void Canvas::drawGlyphs(const GlyphID glyphs[], const Point positions[], size_t

void Canvas::drawTextBlob(std::shared_ptr<TextBlob> textBlob, float x, float y,
const Paint& paint) {
TRACE_EVENT;
if (textBlob == nullptr || paint.nothingToDraw()) {
return;
}
Expand All @@ -341,11 +354,13 @@ void Canvas::drawTextBlob(std::shared_ptr<TextBlob> textBlob, float x, float y,
}

void Canvas::drawPicture(std::shared_ptr<Picture> picture) {
TRACE_EVENT;
drawContext->drawPicture(std::move(picture), *mcState);
}

void Canvas::drawPicture(std::shared_ptr<Picture> picture, const Matrix* matrix,
const Paint* paint) {
TRACE_EVENT;
if (picture == nullptr) {
return;
}
Expand All @@ -363,6 +378,7 @@ void Canvas::drawPicture(std::shared_ptr<Picture> picture, const Matrix* matrix,

void Canvas::drawLayer(std::shared_ptr<Picture> picture, const MCState& state,
const FillStyle& style, std::shared_ptr<ImageFilter> imageFilter) {
TRACE_EVENT;
if (imageFilter == nullptr && picture->records.size() == 1 && style.maskFilter == nullptr) {
LayerUnrollContext layerContext(drawContext, style);
picture->playback(&layerContext, state);
Expand All @@ -376,6 +392,7 @@ void Canvas::drawLayer(std::shared_ptr<Picture> picture, const MCState& state,
void Canvas::drawAtlas(std::shared_ptr<Image> atlas, const Matrix matrix[], const Rect tex[],
const Color colors[], size_t count, const SamplingOptions& sampling,
const Paint* paint) {
TRACE_EVENT;
// TODO: Support blend mode, atlas as source, colors as destination, colors can be nullptr.
if (atlas == nullptr || count == 0 || (paint && paint->nothingToDraw())) {
return;
Expand Down
1 change: 1 addition & 0 deletions src/core/DataProvider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
/////////////////////////////////////////////////////////////////////////////////////////////////

#include "DataProvider.h"
#include "core/utils/Profiling.h"

namespace tgfx {
class DataWrapper : public DataProvider {
Expand Down
2 changes: 2 additions & 0 deletions src/core/GlyphRasterizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
/////////////////////////////////////////////////////////////////////////////////////////////////

#include "GlyphRasterizer.h"
#include "core/utils/Profiling.h"
#include "tgfx/core/Mask.h"

namespace tgfx {
Expand All @@ -34,6 +35,7 @@ GlyphRasterizer::~GlyphRasterizer() {
}

std::shared_ptr<ImageBuffer> GlyphRasterizer::onMakeBuffer(bool tryHardware) const {
TRACE_EVENT;
auto mask = Mask::Make(width(), height(), tryHardware);
if (!mask) {
return nullptr;
Expand Down
3 changes: 3 additions & 0 deletions src/core/ImageCodec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#include "tgfx/core/ImageCodec.h"
#include "core/PixelBuffer.h"
#include "core/utils/Profiling.h"
#include "core/utils/USE.h"
#include "tgfx/core/Buffer.h"
#include "tgfx/core/ImageInfo.h"
Expand All @@ -40,6 +41,7 @@

namespace tgfx {
std::shared_ptr<ImageCodec> ImageCodec::MakeFrom(const std::string& filePath) {
TRACE_EVENT;
std::shared_ptr<ImageCodec> codec = nullptr;
auto stream = Stream::MakeFromFile(filePath);
if (stream == nullptr || stream->size() <= 14) {
Expand Down Expand Up @@ -135,6 +137,7 @@ std::shared_ptr<Data> ImageCodec::Encode(const Pixmap& pixmap, EncodedFormat for
}

std::shared_ptr<ImageBuffer> ImageCodec::onMakeBuffer(bool tryHardware) const {
TRACE_EVENT;
auto pixelBuffer = PixelBuffer::Make(width(), height(), isAlphaOnly(), tryHardware);
if (pixelBuffer == nullptr) {
return nullptr;
Expand Down
1 change: 1 addition & 0 deletions src/core/ImageDecoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#include "ImageDecoder.h"
#include "core/utils/DataTask.h"
#include "core/utils/Profiling.h"

namespace tgfx {

Expand Down
3 changes: 3 additions & 0 deletions src/core/ShapeRasterizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#include "ShapeRasterizer.h"
#include "core/PathTriangulator.h"
#include "core/utils/Profiling.h"
#include "tgfx/core/Mask.h"
#include "utils/Log.h"

Expand All @@ -28,6 +29,7 @@ ShapeRasterizer::ShapeRasterizer(int width, int height, std::shared_ptr<Shape> s
}

std::shared_ptr<ShapeBuffer> ShapeRasterizer::makeRasterized(bool tryHardware) const {
TRACE_EVENT;
auto finalPath = shape->getPath();
if (PathTriangulator::ShouldTriangulatePath(finalPath)) {
return ShapeBuffer::MakeFrom(makeTriangles(finalPath));
Expand All @@ -36,6 +38,7 @@ std::shared_ptr<ShapeBuffer> ShapeRasterizer::makeRasterized(bool tryHardware) c
}

std::shared_ptr<ImageBuffer> ShapeRasterizer::onMakeBuffer(bool tryHardware) const {
TRACE_EVENT;
auto finalPath = shape->getPath();
return makeImageBuffer(finalPath, tryHardware);
}
Expand Down
1 change: 1 addition & 0 deletions src/core/SolidLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

namespace tgfx {
std::shared_ptr<SolidLayer> SolidLayer::Make() {
TRACE_EVENT;
auto layer = std::shared_ptr<SolidLayer>(new SolidLayer());
layer->weakThis = layer;
return layer;
Expand Down
Loading

0 comments on commit cb9edd7

Please sign in to comment.