Esempio n. 1
0
bool Renderer::requestRendering() {
    auto s = getSource();
    if (!_enabled || _renderingInProgress || _surfaceSize.equals(cocos2d::Size::ZERO) || !s) {
        return false;
    }
    font::Source *fontSet = nullptr;
    Document *document = nullptr;
    if (s->isReady()) {
        fontSet = s->getSource();
        document = s->getDocument();
    }

    if (fontSet && document) {
        auto media = _media;
        media.fontScale = s->getFontScale();
        media.pageMargin = _pageMargin;
        if (_isPageSplitted) {
            media.flags |= RenderFlag::SplitPages;
        }

        Builder * impl = new Builder(document, media, fontSet, _ids);
        impl->setHyphens(_hyphens);
        _renderingInProgress = true;
        if (_renderingCallback) {
            _renderingCallback(nullptr, true);
        }

        retain();
        auto &thread = resource::thread();
        thread.perform([impl] (cocos2d::Ref *) -> bool {
            // auto now = Time::now();
            impl->render();
            // TimeInterval all = (Time::now() - now);
            // log::format("Profiling", "Result rendering: %lu (%lu reader %lu other)", all.toMicroseconds(),
            //		impl->getReaderTime().toMicroseconds(), (all - impl->getReaderTime()).toMicroseconds());
            return true;
        }, [this, impl] (cocos2d::Ref *, bool) {
            auto result = impl->getResult();
            //log::format("Result size:", "%lu %d %d %d", result->getSizeInMemory(), sizeof(font::CharSpec), sizeof(font::LineSpec), sizeof(font::RangeSpec));
            if (result) {
                onResult(result);
            }
            release();
            delete impl;
        }, this);

        _renderingDirty = false;
    }
    return false;
}