From 0651051a98b37b1573692e9eb5d87f387ababc64 Mon Sep 17 00:00:00 2001 From: subhransu mohanty Date: Fri, 4 Jan 2019 15:23:56 +0900 Subject: [PATCH] lottie/render: added drawBitmap api to painter class. Change-Id: I07fc5f90c9988e02182f69672eeef9608aa51a2a --- src/vector/vpainter.cpp | 90 ++++++++++++++++++++++++++++++++++++++++- src/vector/vpainter.h | 12 +++++- 2 files changed, 100 insertions(+), 2 deletions(-) diff --git a/src/vector/vpainter.cpp b/src/vector/vpainter.cpp index e8f54b2..39b19f6 100644 --- a/src/vector/vpainter.cpp +++ b/src/vector/vpainter.cpp @@ -25,7 +25,8 @@ class VPainterImpl { public: void drawRle(const VPoint &pos, const VRle &rle); void drawRle(const VRle &rle, const VRle &clip); - + void setCompositionMode(VPainter::CompositionMode mode) { mSpanData.mCompositionMode = mode;} + void drawBitmapUntransform(const VRect &target, const VBitmap &bitmap, const VRect &source); public: VRasterBuffer mBuffer; VSpanData mSpanData; @@ -55,6 +56,54 @@ void VPainterImpl::drawRle(const VRle &rle, const VRle &clip) &mSpanData); } +static void fillRect(const VRect &r, VSpanData *data) +{ + int x1, x2, y1, y2; + + x1 = std::max(r.x(), 0); + x2 = std::min(r.x() + r.width(), data->mRasterBuffer->width()); + y1 = std::max(r.y(), 0); + y2 = std::min(r.y() + r.height(), data->mRasterBuffer->height()); + + if (x2 <= x1 || y2 <= y1) + return; + + const int nspans = 256; + VRle::Span spans[nspans]; + + int y = y1; + while (y < y2) { + int n = std::min(nspans, y2 - y); + int i = 0; + while (i < n) { + spans[i].x = x1; + spans[i].len = x2 - x1; + spans[i].y = y + i; + spans[i].coverage = 255; + ++i; + } + + data->mUnclippedBlendFunc(n, spans, data); + y += n; + } +} + +void VPainterImpl::drawBitmapUntransform(const VRect &target, + const VBitmap &bitmap, + const VRect &source) +{ + mSpanData.initTexture(&bitmap, 255, VBitmapData::Plain, source); + if (!mSpanData.mUnclippedBlendFunc) + return; + mSpanData.dx = -target.x(); + mSpanData.dy = -target.y(); + + VRect rr = source.translated(target.x(), target.y()); + + fillRect(rr, &mSpanData); +} + + VPainter::~VPainter() { @@ -86,6 +135,11 @@ void VPainter::setBrush(const VBrush &brush) mImpl->mSpanData.setup(brush); } +void VPainter::setCompositionMode(CompositionMode mode) +{ + mImpl->setCompositionMode(mode); +} + void VPainter::drawRle(const VPoint &pos, const VRle &rle) { mImpl->drawRle(pos, rle); @@ -102,4 +156,38 @@ VRect VPainter::clipBoundingRect() const return mImpl->mSpanData.mSystemClip; } +void VPainter::drawBitmap(const VPoint &point, const VBitmap &bitmap, const VRect &source) +{ + if (!bitmap.valid()) return; + + drawBitmap(VRect(point.x(), point.y(), bitmap.width(), bitmap.height()), + bitmap, source); +} + +void VPainter::drawBitmap(const VRect &target, const VBitmap &bitmap, const VRect &source) +{ + if (!bitmap.valid()) return; + + if (target.size() == source.size()) { + mImpl->drawBitmapUntransform(target, bitmap, source); + } else { + // @TODO scaling + } +} + +void VPainter::drawBitmap(const VPoint &point, const VBitmap &bitmap) +{ + if (!bitmap.valid()) return; + + drawBitmap(VRect(point.x(), point.y(), bitmap.width(), bitmap.height()), + bitmap, VRect(0, 0, bitmap.width(), bitmap.height())); +} + +void VPainter::drawBitmap(const VRect &rect, const VBitmap &bitmap) +{ + if (!bitmap.valid()) return; + + drawBitmap(rect, bitmap, VRect(0, 0, bitmap.width(), bitmap.height())); +} + V_END_NAMESPACE diff --git a/src/vector/vpainter.h b/src/vector/vpainter.h index d4fbe9f..389c94d 100644 --- a/src/vector/vpainter.h +++ b/src/vector/vpainter.h @@ -29,17 +29,27 @@ class VBitmap; class VPainterImpl; class VPainter { public: - enum CompositionMode { CompModeSrc, CompModeSrcOver }; + enum CompositionMode { + CompModeSrc, + CompModeSrcOver, + CompModeDestIn, + CompModeDestOut + }; ~VPainter(); VPainter(); VPainter(VBitmap *buffer); bool begin(VBitmap *buffer); void end(); void setBrush(const VBrush &brush); + void setCompositionMode(CompositionMode mode); void drawRle(const VPoint &pos, const VRle &rle); void drawRle(const VRle &rle, const VRle &clip); VRect clipBoundingRect() const; + void drawBitmap(const VPoint &point, const VBitmap &bitmap, const VRect &source); + void drawBitmap(const VRect &target, const VBitmap &bitmap, const VRect &source); + void drawBitmap(const VPoint &point, const VBitmap &bitmap); + void drawBitmap(const VRect &rect, const VBitmap &bitmap); private: VPainterImpl *mImpl; }; -- 2.34.1