Skip to content

Commit

Permalink
Remove the Canvas::flush() method and add a Paint parameter to the Ca…
Browse files Browse the repository at this point in the history
…nvas::drawAtlas() method.
  • Loading branch information
domchen committed Feb 26, 2024
1 parent a810a76 commit 82ad5c7
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 23 deletions.
9 changes: 3 additions & 6 deletions include/tgfx/core/Canvas.h
Original file line number Diff line number Diff line change
Expand Up @@ -272,14 +272,11 @@ class Canvas {
* @param colors one per sprite, may be nullptr.
* @param count number of sprites to draw.
* @param sampling SamplingOptions used to sample the atlas image.
* @param paint blend, alpha, and so on, used to draw.
*/
void drawAtlas(std::shared_ptr<Image> atlas, const Matrix matrix[], const Rect tex[],
const Color colors[], size_t count, SamplingOptions sampling = SamplingOptions());

/**
* Triggers the immediate execution of all pending draw operations.
*/
void flush();
const Color colors[], size_t count, SamplingOptions sampling,
const Paint* paint = nullptr);

private:
Surface* surface = nullptr;
Expand Down
13 changes: 6 additions & 7 deletions src/core/Canvas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,10 +176,6 @@ static Paint CleanPaintForDrawImage(const Paint* paint) {
return cleaned;
}

void Canvas::flush() {
surface->flush();
}

Context* Canvas::getContext() const {
return surface->getContext();
}
Expand Down Expand Up @@ -611,7 +607,8 @@ void Canvas::drawMaskGlyphs(std::shared_ptr<TextBlob> textBlob, const Paint& pai
}

void Canvas::drawAtlas(std::shared_ptr<Image> atlas, const Matrix matrix[], const Rect tex[],
const Color colors[], size_t count, SamplingOptions sampling) {
const Color colors[], size_t count, SamplingOptions sampling,
const Paint* paint) {
// TODO: Support blend mode, atlas as source, colors as destination, colors can be nullptr.
if (atlas == nullptr || count == 0) {
return;
Expand Down Expand Up @@ -646,7 +643,9 @@ void Canvas::drawAtlas(std::shared_ptr<Image> atlas, const Matrix matrix[], cons
if (ops.empty()) {
return;
}
DrawArgs args(getContext(), surface->options()->renderFlags(), Color::White(), drawRect,
auto realPaint = CleanPaintForDrawImage(paint);
auto inputColor = realPaint.getColor().premultiply();
DrawArgs args(getContext(), surface->options()->renderFlags(), inputColor, drawRect,
state->matrix, sampling);
for (auto& rectOp : ops) {
auto processor = FragmentProcessor::Make(atlas, args);
Expand All @@ -657,7 +656,7 @@ void Canvas::drawAtlas(std::shared_ptr<Image> atlas, const Matrix matrix[], cons
return;
}
rectOp->addColorFP(std::move(processor));
addDrawOp(std::move(rectOp), args, {});
addDrawOp(std::move(rectOp), args, realPaint);
}
}

Expand Down
10 changes: 5 additions & 5 deletions test/src/CanvasTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ TGFX_TEST(CanvasTest, merge_draw_call_rect) {
auto task = std::static_pointer_cast<OpsRenderTask>(drawingManager->renderTasks[0]);
EXPECT_TRUE(task->ops.size() == 2);
EXPECT_EQ(static_cast<FillRectOp*>(task->ops[1].get())->rectPaints.size(), drawCallCount);
canvas->flush();
surface->flush();
EXPECT_TRUE(Baseline::Compare(surface, "CanvasTest/merge_draw_call_rect"));
device->unlock();
}
Expand Down Expand Up @@ -160,7 +160,7 @@ TGFX_TEST(CanvasTest, merge_draw_call_rrect) {
auto task = std::static_pointer_cast<OpsRenderTask>(drawingManager->renderTasks[0]);
EXPECT_TRUE(task->ops.size() == 2);
EXPECT_EQ(static_cast<RRectOp*>(task->ops[1].get())->rRectPaints.size(), drawCallCount);
canvas->flush();
surface->flush();
EXPECT_TRUE(Baseline::Compare(surface, "CanvasTest/merge_draw_call_rrect"));
device->unlock();
}
Expand Down Expand Up @@ -202,7 +202,7 @@ TGFX_TEST(CanvasTest, merge_draw_clear_op) {
EXPECT_TRUE(drawingManager->renderTasks.size() == 1);
auto task = std::static_pointer_cast<OpsRenderTask>(drawingManager->renderTasks[0]);
EXPECT_TRUE(task->ops.size() == drawCallCount + 1);
canvas->flush();
surface->flush();
EXPECT_TRUE(Baseline::Compare(surface, "CanvasTest/merge_draw_clear_op"));
device->unlock();
}
Expand Down Expand Up @@ -286,7 +286,7 @@ TGFX_TEST(CanvasTest, textShape) {
canvas->drawGlyphs(textRun.ids.data(), textRun.positions.data(), textRun.ids.size(),
textRun.font, paint);
}
canvas->flush();
surface->flush();
EXPECT_TRUE(Baseline::Compare(surface, "CanvasTest/text_shape"));
device->unlock();
}
Expand Down Expand Up @@ -527,7 +527,7 @@ TGFX_TEST(CanvasTest, image) {
canvas->drawImage(image);
auto decodedImage = image->makeDecoded(context);
EXPECT_FALSE(decodedImage == image);
canvas->flush();
surface->flush();
decodedImage = image->makeDecoded(context);
EXPECT_FALSE(decodedImage == image);
auto textureImage = image->makeTextureImage(context);
Expand Down
8 changes: 4 additions & 4 deletions test/src/ImageReaderTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ TGFX_TEST(ImageReaderTest, updateMask) {
Paint paint = {};
paint.setColor(Color::Black());
canvas->drawImage(maskImage, &paint);
canvas->flush();
surface->flush();

path.reset();
path.addRoundRect(Rect::MakeXYWH(22, 22, 10, 10), 3, 3);
Expand All @@ -58,7 +58,7 @@ TGFX_TEST(ImageReaderTest, updateMask) {
canvas->setMatrix(Matrix::MakeTrans(50, 0));
canvas->drawImage(maskImage, &paint);
EXPECT_FALSE(buffer->expired());
canvas->flush();
surface->flush();
EXPECT_TRUE(buffer->expired());

EXPECT_TRUE(Baseline::Compare(surface, "ImageReaderTest/update_mask"));
Expand Down Expand Up @@ -87,7 +87,7 @@ TGFX_TEST(ImageReaderTest, updateBitmap) {
auto newBuffer = reader->acquireNextBuffer();
EXPECT_FALSE(newBuffer != nullptr);
canvas->drawImage(image);
canvas->flush();
surface->flush();
if (HardwareBufferAvailable()) {
context->submit(true);
}
Expand All @@ -110,7 +110,7 @@ TGFX_TEST(ImageReaderTest, updateBitmap) {
if (!HardwareBufferAvailable()) {
EXPECT_FALSE(buffer->expired());
}
canvas->flush();
surface->flush();
EXPECT_TRUE(buffer->expired());

EXPECT_TRUE(Baseline::Compare(surface, "ImageReaderTest/update_bitmap"));
Expand Down
2 changes: 1 addition & 1 deletion test/src/SurfaceTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ TGFX_TEST(SurfaceTest, ImageSnapshot) {
auto renderTargetProxy = surface->renderTargetProxy;
snapshotImage = nullptr;
canvas->drawImage(image);
canvas->flush();
surface->flush();
EXPECT_TRUE(renderTargetProxy == surface->renderTargetProxy);
snapshotImage = surface->makeImageSnapshot();
snapshotImage2 = surface->makeImageSnapshot();
Expand Down

0 comments on commit 82ad5c7

Please sign in to comment.