Line data Source code
1 : /** 2 : Copyright (c) 2023 Stappler LLC <admin@stappler.dev> 3 : 4 : Permission is hereby granted, free of charge, to any person obtaining a copy 5 : of this software and associated documentation files (the "Software"), to deal 6 : in the Software without restriction, including without limitation the rights 7 : to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 : copies of the Software, and to permit persons to whom the Software is 9 : furnished to do so, subject to the following conditions: 10 : 11 : The above copyright notice and this permission notice shall be included in 12 : all copies or substantial portions of the Software. 13 : 14 : THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 : IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 : FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 : AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 : LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 : OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 : THE SOFTWARE. 21 : **/ 22 : 23 : #include "XLFontDeferredRequest.h" 24 : 25 : namespace STAPPLER_VERSIONIZED stappler::xenolith::font { 26 : 27 612 : void DeferredRequest::runFontRenderer(thread::TaskQueue &queue, const Rc<FontExtension> &ext, 28 : const Vector<FontUpdateRequest> &req, Function<void(uint32_t reqIdx, const CharTexture &texData)> &&onTex, Function<void()> &&onComp) { 29 612 : auto data = Rc<DeferredRequest>::alloc(ext, req); 30 612 : data->onTexture = move(onTex); 31 612 : data->onComplete = move(onComp); 32 : 33 3060 : for (uint32_t i = 0; i < queue.getThreadCount(); ++ i) { 34 2448 : queue.perform([data] () { 35 2406 : data->runThread(); 36 2419 : }); 37 : } 38 612 : } 39 : 40 1224 : DeferredRequest::~DeferredRequest() { } 41 : 42 612 : DeferredRequest::DeferredRequest(const Rc<FontExtension> &ext, const Vector<FontUpdateRequest> &req) 43 612 : : ext(ext) { 44 3814 : for (auto &it : req) { 45 3202 : nrequests += it.chars.size(); 46 : } 47 : 48 612 : fontRequests.reserve(nrequests); 49 : 50 3814 : for (uint32_t i = 0; i < req.size(); ++ i) { 51 3202 : faces.emplace_back(req[i].object); 52 104871 : for (auto &it : req[i].chars) { 53 101669 : fontRequests.emplace_back(i, it); 54 : } 55 : } 56 612 : } 57 : 58 2404 : void DeferredRequest::runThread() { 59 2404 : Vector<Rc<FontFaceObjectHandle>> threadFaces; threadFaces.resize(faces.size(), nullptr); 60 2399 : uint32_t target = current.fetch_add(1); 61 2399 : uint32_t c = 0; 62 103386 : while (target < nrequests) { 63 101135 : auto &v = fontRequests[target]; 64 100884 : if (v.second == 0) { 65 18273 : c = complete.fetch_add(1); 66 18273 : target = current.fetch_add(1); 67 18273 : continue; 68 : } 69 : 70 82611 : if (!threadFaces[v.first]) { 71 9465 : threadFaces[v.first] = ext->getLibrary()->makeThreadHandle(faces[v.first]); 72 : } 73 : 74 82507 : threadFaces[v.first]->acquireTexture(v.second, [&, this] (const font::CharTexture &tex) { 75 82403 : onTexture(v.first, tex); 76 82758 : }); 77 82714 : c = complete.fetch_add(1); 78 165428 : target = current.fetch_add(1); 79 : } 80 2251 : threadFaces.clear(); 81 2420 : if (c == nrequests - 1) { 82 612 : onComplete(); 83 : } 84 2420 : } 85 : 86 : }