-#include "vraster.h"
+/*
+ * Copyright (c) 2018 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * Licensed under the Flora License, Version 1.1 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://floralicense.org/license/
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "vraster.h"
#include <cstring>
#include <thread>
#include "v_ft_raster.h"
#include "vmatrix.h"
#include "vpath.h"
#include "vtaskqueue.h"
+#include "vrle.h"
V_BEGIN_NAMESPACE
{
if (mPointSize) delete[] ft.points;
if (mTagSize) delete[] ft.tags;
- if (mSegmentSize) delete[] ft.contours;
+ if (mSegmentSize) {
+ delete[] ft.contours;
+ delete[] ft.contours_flag;
+ }
}
void reset();
void grow(int, int);
SW_FT_Stroker_LineJoin ftJoin;
SW_FT_Fixed ftWidth;
SW_FT_Fixed ftMeterLimit;
- SW_FT_Bool ftClosed;
};
void FTOutline::reset()
}
if (segment_size > mSegmentSize) {
- if (mSegmentSize) delete [] ft.contours;
+ if (mSegmentSize) {
+ delete [] ft.contours;
+ delete [] ft.contours_flag;
+ }
ft.contours = new short[segment_size];
+ ft.contours_flag = new char[segment_size];
mSegmentSize = segment_size;
}
void FTOutline::convert(CapStyle cap, JoinStyle join, float width,
float meterLimit)
{
- ftClosed = (SW_FT_Bool)closed;
-
// map strokeWidth to freetype. It uses as the radius of the pen not the
// diameter
width = width / 2.0;
ft.contours[ft.n_contours] = ft.n_points - 1;
ft.n_contours++;
}
+ // mark the current contour as open
+ // will be updated if ther is a close tag at the end.
+ ft.contours_flag[ft.n_contours] = 1;
+
ft.n_points++;
- closed = false;
}
void FTOutline::lineTo(const VPointF &pt)
ft.points[ft.n_points].y = TO_FT_COORD(pt.y());
ft.tags[ft.n_points] = SW_FT_CURVE_TAG_ON;
ft.n_points++;
- closed = false;
}
void FTOutline::cubicTo(const VPointF &cp1, const VPointF &cp2,
ft.points[ft.n_points].y = TO_FT_COORD(ep.y());
ft.tags[ft.n_points] = SW_FT_CURVE_TAG_ON;
ft.n_points++;
- closed = false;
}
void FTOutline::close()
{
+ // mark the contour as a close path.
+ ft.contours_flag[ft.n_contours] = 0;
+
int index;
if (ft.n_contours) {
index = ft.contours[ft.n_contours - 1] + 1;
ft.points[ft.n_points].y = ft.points[index].y;
ft.tags[ft.n_points] = SW_FT_CURVE_TAG_ON;
ft.n_points++;
- closed = true;
}
void FTOutline::end()
rle->addSpan(rleSpan, count);
}
+static void bboxCb(int x, int y, int w, int h, void *user)
+{
+ VRle * rle = (VRle *)user;
+ rle->setBoundingRect({x, y, w, h});
+}
+
struct RleTask {
- RleTask() { receiver = sender.get_future(); }
- std::promise<VRle> sender;
- std::future<VRle> receiver;
- bool stroke;
+ RleShare mRlePromise;
VPath path;
VRle rle;
+ float width;
+ float meterLimit;
+ VRect clip;
FillRule fillRule;
CapStyle cap;
JoinStyle join;
- float width;
- float meterLimit;
+ bool stroke;
VRle operator()(FTOutline &outRef, SW_FT_Stroker &stroker);
+ void render(FTOutline &outRef);
};
+void RleTask::render(FTOutline &outRef)
+{
+ SW_FT_Raster_Params params;
+
+ params.flags = SW_FT_RASTER_FLAG_DIRECT | SW_FT_RASTER_FLAG_AA;
+ params.gray_spans = &rleGenerationCb;
+ params.bbox_cb = &bboxCb;
+ params.user = &rle;
+ params.source = &outRef.ft;
+
+ if (!clip.empty()) {
+ params.flags |= SW_FT_RASTER_FLAG_CLIP;
+
+ params.clip_box.xMin = clip.left();
+ params.clip_box.yMin = clip.top();
+ params.clip_box.xMax = clip.right();
+ params.clip_box.yMax = clip.bottom();
+ }
+ // compute rle
+ sw_ft_grays_raster.raster_render(nullptr, ¶ms);
+}
+
VRle RleTask::operator()(FTOutline &outRef, SW_FT_Stroker &stroker)
{
rle.reset();
SW_FT_Stroker_Set(stroker, outRef.ftWidth, outRef.ftCap, outRef.ftJoin,
outRef.ftMeterLimit);
- SW_FT_Stroker_ParseOutline(stroker, &outRef.ft, !outRef.ftClosed);
+ SW_FT_Stroker_ParseOutline(stroker, &outRef.ft);
SW_FT_Stroker_GetCounts(stroker, &points, &contors);
outRef.grow(points, contors);
SW_FT_Stroker_Export(stroker, &outRef.ft);
- SW_FT_Raster_Params params;
-
- params.flags = SW_FT_RASTER_FLAG_DIRECT | SW_FT_RASTER_FLAG_AA;
- params.gray_spans = &rleGenerationCb;
- params.user = &rle;
- params.source = &outRef;
-
- sw_ft_grays_raster.raster_render(nullptr, ¶ms);
-
} else { // Fill Task
outRef.convert(path);
int fillRuleFlag = SW_FT_OUTLINE_NONE;
break;
}
outRef.ft.flags = fillRuleFlag;
- SW_FT_Raster_Params params;
+ }
- params.flags = SW_FT_RASTER_FLAG_DIRECT | SW_FT_RASTER_FLAG_AA;
- params.gray_spans = &rleGenerationCb;
- params.user = &rle;
- params.source = &outRef.ft;
+ render(outRef);
+
+ path = VPath();
- sw_ft_grays_raster.raster_render(nullptr, ¶ms);
- }
return std::move(rle);
}
SW_FT_Stroker_New(&stroker);
// Task Loop
+ RleTask task;
while (true) {
- RleTask *task = nullptr;
+ bool success = false;
for (unsigned n = 0; n != _count * 32; ++n) {
- if (_q[(i + n) % _count].try_pop(task)) break;
+ if (_q[(i + n) % _count].try_pop(task)) {
+ success = true;
+ break;
+ }
}
- if (!task && !_q[i].pop(task)) break;
- task->sender.set_value((*task)(outlineRef, stroker));
- delete task;
+ if (!success && !_q[i].pop(task)) break;
+
+ task.mRlePromise->set_value((task)(outlineRef, stroker));
}
// cleanup
SW_FT_Stroker_Done(stroker);
}
-public:
RleTaskScheduler()
{
for (unsigned n = 0; n != _count; ++n) {
_threads.emplace_back([&, n] { run(n); });
}
}
+public:
+ static RleTaskScheduler& instance()
+ {
+ static RleTaskScheduler singleton;
+ return singleton;
+ }
~RleTaskScheduler()
{
for (auto &e : _threads) e.join();
}
- std::future<VRle> async(RleTask *task)
+ void async(RleTask &&task)
{
- auto receiver = std::move(task->receiver);
auto i = _index++;
for (unsigned n = 0; n != _count; ++n) {
- if (_q[(i + n) % _count].try_push(task)) return receiver;
+ if (_q[(i + n) % _count].try_push(std::move(task))) return;
}
- _q[i % _count].push(task);
-
- return receiver;
+ _q[i % _count].push(std::move(task));
}
- std::future<VRle> strokeRle(const VPath &path, VRle &&rle, CapStyle cap, JoinStyle join,
- float width, float meterLimit)
+ void strokeRle(RleShare &promise, VPath &&path, VRle &&rle, CapStyle cap, JoinStyle join,
+ float width, float meterLimit, const VRect &clip)
{
- RleTask *task = new RleTask();
- task->stroke = true;
- task->path = path;
- task->rle = std::move(rle);
- task->cap = cap;
- task->join = join;
- task->width = width;
- task->meterLimit = meterLimit;
- return async(task);
+ RleTask task;
+ task.stroke = true;
+ task.path = std::move(path);
+ task.rle = std::move(rle);
+ task.cap = cap;
+ task.join = join;
+ task.width = width;
+ task.meterLimit = meterLimit;
+ task.clip = clip;
+ task.mRlePromise = promise;
+
+ async(std::move(task));
}
- std::future<VRle> fillRle(const VPath &path, VRle &&rle, FillRule fillRule)
+ void fillRle(RleShare &promise, VPath &&path, VRle &&rle, FillRule fillRule, const VRect &clip)
{
- RleTask *task = new RleTask();
- task->path = path;
- task->rle = std::move(rle);
- task->fillRule = fillRule;
- task->stroke = false;
- return async(task);
+ RleTask task;
+ task.path = std::move(path);
+ task.rle = std::move(rle);
+ task.fillRule = fillRule;
+ task.clip = clip;
+ task.stroke = false;
+ task.mRlePromise = promise;
+
+ async(std::move(task));
}
};
-static RleTaskScheduler raster_scheduler;
-
-VRaster::VRaster() {}
-
-VRaster::~VRaster() {}
-
-std::future<VRle> VRaster::generateFillInfo(const VPath &path, VRle &&rle,
- FillRule fillRule)
+void VRaster::generateFillInfo(RleShare &promise, VPath &&path, VRle &&rle,
+ FillRule fillRule, const VRect &clip)
{
- if (path.isEmpty()) {
- std::promise<VRle> promise;
- promise.set_value(VRle());
- return promise.get_future();
+ if (path.empty()) {
+ promise->set_value(VRle());
+ return;
}
- return raster_scheduler.fillRle(path, std::move(rle), fillRule);
+ return RleTaskScheduler::instance().fillRle(promise, std::move(path), std::move(rle), fillRule, clip);
}
-std::future<VRle> VRaster::generateStrokeInfo(const VPath &path, VRle &&rle, CapStyle cap,
- JoinStyle join, float width,
- float meterLimit)
+void VRaster::generateStrokeInfo(RleShare &promise, VPath &&path, VRle &&rle, CapStyle cap,
+ JoinStyle join, float width,
+ float meterLimit, const VRect &clip)
{
- if (path.isEmpty()) {
- std::promise<VRle> promise;
- promise.set_value(VRle());
- return promise.get_future();
+ if (path.empty()) {
+ promise->set_value(VRle());
+ return;
}
- return raster_scheduler.strokeRle(path, std::move(rle), cap, join, width, meterLimit);
+ return RleTaskScheduler::instance().strokeRle(promise, std::move(path), std::move(rle), cap, join, width, meterLimit, clip);
}
V_END_NAMESPACE