| 6 | * License as published by the Free Software Foundation; either |
| 7 | * version 2.1 of the License, or (at your option) any later version. |
| 8 | * |
| 9 | * This library is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | * Lesser General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU Lesser General Public |
| 15 | * License along with this library; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 17 | */ |
| 18 |
| 19 | /**************************************************************************** |
| 20 | ** |
| 21 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). |
| 22 | ** All rights reserved. |
| 23 | ** Contact: Nokia Corporation (qt-info@nokia.com) |
| 24 | ** |
| 25 | ** This file is part of the QtGui module of the Qt Toolkit. |
| 26 | ** |
| 27 | ** $QT_BEGIN_LICENSE:LGPL$ |
| 28 | ** No Commercial Usage |
| 29 | ** This file contains pre-release code and may not be distributed. |
| 30 | ** You may use this file in accordance with the terms and conditions |
| 31 | ** contained in the Technology Preview License Agreement accompanying |
| 32 | ** this package. |
| 33 | ** |
| 34 | ** GNU Lesser General Public License Usage |
| 35 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
| 36 | ** General Public License version 2.1 as published by the Free Software |
| 37 | ** Foundation and appearing in the file LICENSE.LGPL included in the |
| 38 | ** packaging of this file. Please review the following information to |
| 39 | ** ensure the GNU Lesser General Public License version 2.1 requirements |
| 40 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
| 41 | ** |
| 42 | ** In addition, as a special exception, Nokia gives you certain additional |
| 43 | ** rights. These rights are described in the Nokia Qt LGPL Exception |
| 44 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
| 45 | ** |
| 46 | ** If you have questions regarding the use of this file, please contact |
| 47 | ** Nokia at qt-info@nokia.com. |
| 48 | ** |
| 49 | ** $QT_END_LICENSE$ |
| 50 | ** |
| 51 | ****************************************************************************/ |
| 52 |
| 53 | #include "vdrawhelper.h" |
| 54 | #include <algorithm> |
| 55 | #include <climits> |
| 56 | #include <cstring> |
| 57 | #include <mutex> |
| 58 | #include <unordered_map> |
| 59 |
| 60 | class VGradientCache { |
| 61 | public: |
| 62 | struct CacheInfo : public VColorTable { |
| 63 | inline CacheInfo(VGradientStops s) : stops(std::move(s)) {} |
| 64 | VGradientStops stops; |
| 65 | }; |
| 66 | using VCacheData = std::shared_ptr<const CacheInfo>; |
| 67 | using VCacheKey = int64_t; |
| 68 | using VGradientColorTableHash = |
| 69 | std::unordered_multimap<VCacheKey, VCacheData>; |
| 70 |
| 71 | bool generateGradientColorTable(const VGradientStops &stops, float alpha, |
| 72 | uint32_t *colorTable, int size); |
| 73 | VCacheData getBuffer(const VGradient &gradient) |
| 74 | { |
| 75 | VCacheKey hash_val = 0; |
| 76 | VCacheData info; |
| 77 | const VGradientStops &stops = gradient.mStops; |
| 78 | for (uint i = 0; i < stops.size() && i <= 2; i++) |
| 79 | hash_val += (stops[i].second.premulARGB() * gradient.alpha()); |
| 80 |
| 81 | { |
| 82 | std::lock_guard<std::mutex> guard(mMutex); |
| 83 |
| 84 | size_t count = mCache.count(hash_val); |
| 85 | if (!count) { |
| 86 | // key is not present in the hash |
| 87 | info = addCacheElement(hash_val, gradient); |
| 88 | } else if (count == 1) { |
| 89 | auto search = mCache.find(hash_val); |
| 90 | if (search->second->stops == stops) { |
| 91 | info = search->second; |
| 92 | } else { |
| 93 | // didn't find an exact match |
| 94 | info = addCacheElement(hash_val, gradient); |
| 95 | } |
| 96 | } else { |
| 97 | // we have a multiple data with same key |
| 98 | auto range = mCache.equal_range(hash_val); |
| 99 | for (auto it = range.first; it != range.second; ++it) { |
| 100 | if (it->second->stops == stops) { |
| 101 | info = it->second; |
| 102 | break; |
| 103 | } |
| 104 | } |
| 105 | if (!info) { |
| 106 | // didn't find an exact match |
| 107 | info = addCacheElement(hash_val, gradient); |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | return info; |
| 112 | } |
| 113 |
| 114 | protected: |
| 115 | uint maxCacheSize() const { return 60; } |
| 116 | VCacheData addCacheElement(VCacheKey hash_val, const VGradient &gradient) |
| 117 | { |
| 118 | if (mCache.size() == maxCacheSize()) { |
| 119 | uint count = maxCacheSize() / 10; |
| 120 | while (count--) { |
| 121 | mCache.erase(mCache.begin()); |
| 122 | } |
| 123 | } |
| 124 | auto cache_entry = std::make_shared<CacheInfo>(gradient.mStops); |
| 125 | cache_entry->alpha = generateGradientColorTable( |
| 126 | gradient.mStops, gradient.alpha(), cache_entry->buffer32, |
| 127 | VGradient::colorTableSize); |
| 128 | mCache.insert(std::make_pair(hash_val, cache_entry)); |
| 129 | return cache_entry; |
| 130 | } |
| 131 |
| 132 | VGradientColorTableHash mCache; |
| 133 | std::mutex mMutex; |
| 134 | }; |
| 135 |
| 136 | bool VGradientCache::generateGradientColorTable(const VGradientStops &stops, |
| 137 | float opacity, |
| 138 | uint32_t *colorTable, int size) |
| 139 | { |
| 140 | if (stops.empty()) { |
| 141 | return false; |
| 142 | } |
| 143 | int dist, idist, pos = 0, i; |
| 144 | bool alpha = false; |
| 145 | int stopCount = stops.size(); |
| 146 | const VGradientStop *curr, *next, *start; |
| 147 | uint32_t curColor, nextColor; |
| 148 | float delta, t, incr, fpos; |
| 149 |
| 150 | if (!vCompare(opacity, 1.0f)) alpha = true; |
| 151 |
| 152 | start = stops.data(); |
| 153 | curr = start; |
| 154 | if (!curr->second.isOpaque()) alpha = true; |
| 155 | curColor = curr->second.premulARGB(opacity); |
| 156 | incr = 1.0 / (float)size; |
| 157 | fpos = 1.5 * incr; |
| 158 |
| 159 | colorTable[pos++] = curColor; |
| 160 |
| 161 | while (fpos <= curr->first && pos < size) { |
| 162 | colorTable[pos] = colorTable[pos - 1]; |
| 163 | pos++; |
| 164 | fpos += incr; |
| 165 | } |
| 166 |
| 167 | for (i = 0; i < stopCount - 1; ++i) { |
| 168 | curr = (start + i); |
| 169 | next = (start + i + 1); |
| 170 | delta = 1 / (next->first - curr->first); |
| 171 | if (!next->second.isOpaque()) alpha = true; |
| 172 | nextColor = next->second.premulARGB(opacity); |
| 173 | while (fpos < next->first && pos < size) { |
| 174 | t = (fpos - curr->first) * delta; |
| 175 | dist = (int)(255 * t); |
| 176 | idist = 255 - dist; |
| 177 | colorTable[pos] = |
| 178 | INTERPOLATE_PIXEL_255(curColor, idist, nextColor, dist); |
| 179 | ++pos; |
| 180 | fpos += incr; |
| 181 | } |
| 182 | curColor = nextColor; |
| 183 | } |
| 184 |
| 185 | for (; pos < size; ++pos) colorTable[pos] = curColor; |
| 186 |
| 187 | // Make sure the last color stop is represented at the end of the table |
| 188 | colorTable[size - 1] = curColor; |
| 189 | return alpha; |
| 190 | } |
| 191 |
| 192 | static VGradientCache VGradientCacheInstance; |
| 193 |
| 194 | void VRasterBuffer::clear() |
| 195 | { |
| 196 | memset(mBuffer, 0, mHeight * mBytesPerLine); |
| 197 | } |
| 198 |
| 199 | VBitmap::Format VRasterBuffer::prepare(VBitmap *image) |
| 200 | { |
| 201 | mBuffer = image->data(); |
| 202 | mWidth = image->width(); |
| 203 | mHeight = image->height(); |
| 204 | mBytesPerPixel = 4; |
| 205 | mBytesPerLine = image->stride(); |
| 206 |
| 207 | mFormat = image->format(); |
| 208 | return mFormat; |
| 209 | } |
| 210 |
| 211 | void VSpanData::init(VRasterBuffer *image) |
| 212 | { |
| 213 | mRasterBuffer = image; |
| 214 | setDrawRegion(VRect(0, 0, image->width(), image->height())); |
| 215 | mType = VSpanData::Type::None; |
| 216 | mBlendFunc = nullptr; |
| 217 | mUnclippedBlendFunc = nullptr; |
| 218 | } |
| 219 |
| 220 | extern CompositionFunction COMP_functionForMode_C[]; |
| 221 | extern CompositionFunctionSolid COMP_functionForModeSolid_C[]; |
| 222 | static const CompositionFunction * functionForMode = COMP_functionForMode_C; |
| 223 | static const CompositionFunctionSolid *functionForModeSolid = |
| 224 | COMP_functionForModeSolid_C; |
| 225 |
| 226 | /* |
| 227 | * Gradient Draw routines |
| 228 | * |
| 229 | */ |
| 230 |
| 231 | #define FIXPT_BITS 8 |
| 232 | #define FIXPT_SIZE (1 << FIXPT_BITS) |
| 233 | static inline void getLinearGradientValues(LinearGradientValues *v, |
| 234 | const VSpanData * data) |
| 235 | { |
| 236 | const VGradientData *grad = &data->mGradient; |
| 237 | v->dx = grad->linear.x2 - grad->linear.x1; |
| 238 | v->dy = grad->linear.y2 - grad->linear.y1; |
| 239 | v->l = v->dx * v->dx + v->dy * v->dy; |
| 240 | v->off = 0; |
| 241 | if (v->l != 0) { |
| 242 | v->dx /= v->l; |
| 243 | v->dy /= v->l; |
| 244 | v->off = -v->dx * grad->linear.x1 - v->dy * grad->linear.y1; |
| 245 | } |
| 246 | } |
| 247 |
| 248 | static inline void getRadialGradientValues(RadialGradientValues *v, |
| 249 | const VSpanData * data) |
| 250 | { |
| 251 | const VGradientData &gradient = data->mGradient; |
| 252 | v->dx = gradient.radial.cx - gradient.radial.fx; |
| 253 | v->dy = gradient.radial.cy - gradient.radial.fy; |
| 254 |
| 255 | v->dr = gradient.radial.cradius - gradient.radial.fradius; |
| 256 | v->sqrfr = gradient.radial.fradius * gradient.radial.fradius; |
| 257 |
| 258 | v->a = v->dr * v->dr - v->dx * v->dx - v->dy * v->dy; |
| 259 | v->inv2a = 1 / (2 * v->a); |
| 260 |
| 261 | v->extended = !vIsZero(gradient.radial.fradius) || v->a <= 0; |
| 262 | } |
| 263 |
| 264 | static inline int gradientClamp(const VGradientData *grad, int ipos) |
| 265 | { |
| 266 | int limit; |
| 267 |
| 268 | if (grad->mSpread == VGradient::Spread::Repeat) { |
| 269 | ipos = ipos % VGradient::colorTableSize; |
| 270 | ipos = ipos < 0 ? VGradient::colorTableSize + ipos : ipos; |
| 271 | } else if (grad->mSpread == VGradient::Spread::Reflect) { |
| 272 | limit = VGradient::colorTableSize * 2; |
| 273 | ipos = ipos % limit; |
| 274 | ipos = ipos < 0 ? limit + ipos : ipos; |
| 275 | ipos = ipos >= VGradient::colorTableSize ? limit - 1 - ipos : ipos; |
| 276 | } else { |
| 277 | if (ipos < 0) |
| 278 | ipos = 0; |
| 279 | else if (ipos >= VGradient::colorTableSize) |
| 280 | ipos = VGradient::colorTableSize - 1; |
| 281 | } |
| 282 | return ipos; |
| 283 | } |
| 284 |
| 285 | static uint32_t gradientPixelFixed(const VGradientData *grad, int fixed_pos) |
| 286 | { |
| 287 | int ipos = (fixed_pos + (FIXPT_SIZE / 2)) >> FIXPT_BITS; |
| 288 |
| 289 | return grad->mColorTable[gradientClamp(grad, ipos)]; |
| 290 | } |
| 291 |
| 292 | static inline uint32_t gradientPixel(const VGradientData *grad, float pos) |
| 293 | { |
| 294 | int ipos = (int)(pos * (VGradient::colorTableSize - 1) + (float)(0.5)); |
| 295 |
| 296 | return grad->mColorTable[gradientClamp(grad, ipos)]; |
| 297 | } |
| 298 |
| 299 | void fetch_linear_gradient(uint32_t *buffer, const Operator *op, |
| 300 | const VSpanData *data, int y, int x, int length) |
| 301 | { |
| 302 | float t, inc; |
| 303 | const VGradientData *gradient = &data->mGradient; |
| 304 |
| 305 | bool affine = true; |
| 306 | float rx = 0, ry = 0; |
| 307 | if (op->linear.l == 0) { |
| 308 | t = inc = 0; |
| 309 | } else { |
| 310 | rx = data->m21 * (y + float(0.5)) + data->m11 * (x + float(0.5)) + |
| 311 | data->dx; |
| 312 | ry = data->m22 * (y + float(0.5)) + data->m12 * (x + float(0.5)) + |
| 313 | data->dy; |
| 314 | t = op->linear.dx * rx + op->linear.dy * ry + op->linear.off; |
| 315 | inc = op->linear.dx * data->m11 + op->linear.dy * data->m12; |
| 316 | affine = !data->m13 && !data->m23; |
| 317 |
| 318 | if (affine) { |
| 319 | t *= (VGradient::colorTableSize - 1); |
| 320 | inc *= (VGradient::colorTableSize - 1); |
| 321 | } |
| 322 | } |
| 323 |
| 324 | const uint32_t *end = buffer + length; |
| 325 | if (affine) { |
| 326 | if (inc > float(-1e-5) && inc < float(1e-5)) { |
| 327 | memfill32(buffer, gradientPixelFixed(gradient, int(t * FIXPT_SIZE)), |
| 328 | length); |
| 329 | } else { |
| 330 | if (t + inc * length < float(INT_MAX >> (FIXPT_BITS + 1)) && |
| 331 | t + inc * length > float(INT_MIN >> (FIXPT_BITS + 1))) { |
| 332 | // we can use fixed point math |
| 333 | int t_fixed = int(t * FIXPT_SIZE); |
| 334 | int inc_fixed = int(inc * FIXPT_SIZE); |
| 335 | while (buffer < end) { |
| 336 | *buffer = gradientPixelFixed(gradient, t_fixed); |
| 337 | t_fixed += inc_fixed; |
| 338 | ++buffer; |
| 339 | } |
| 340 | } else { |
| 341 | // we have to fall back to float math |
| 342 | while (buffer < end) { |
| 343 | *buffer = |
| 344 | gradientPixel(gradient, t / VGradient::colorTableSize); |
| 345 | t += inc; |
| 346 | ++buffer; |
| 347 | } |
| 348 | } |
| 349 | } |
| 350 | } else { // fall back to float math here as well |
| 351 | float rw = data->m23 * (y + float(0.5)) + data->m13 * (x + float(0.5)) + |
| 352 | data->m33; |
| 353 | while (buffer < end) { |
| 354 | float x = rx / rw; |
| 355 | float y = ry / rw; |
| 356 | t = (op->linear.dx * x + op->linear.dy * y) + op->linear.off; |
| 357 |
| 358 | *buffer = gradientPixel(gradient, t); |
| 359 | rx += data->m11; |
| 360 | ry += data->m12; |
| 361 | rw += data->m13; |
| 362 | if (!rw) { |
| 363 | rw += data->m13; |
| 364 | } |
| 365 | ++buffer; |
| 366 | } |
| 367 | } |
| 368 | } |
| 369 |
| 370 | static inline float radialDeterminant(float a, float b, float c) |
| 371 | { |
| 372 | return (b * b) - (4 * a * c); |
| 373 | } |
| 374 |
| 375 | static void fetch(uint32_t *buffer, uint32_t *end, const Operator *op, |
| 376 | const VSpanData *data, float det, float delta_det, |
| 377 | float delta_delta_det, float b, float delta_b) |
| 378 | { |
| 379 | if (op->radial.extended) { |
| 380 | while (buffer < end) { |
| 381 | uint32_t result = 0; |
| 382 | if (det >= 0) { |
| 383 | float w = std::sqrt(det) - b; |
| 384 | if (data->mGradient.radial.fradius + op->radial.dr * w >= 0) |
| 385 | result = gradientPixel(&data->mGradient, w); |
| 386 | } |
| 387 |
| 388 | *buffer = result; |
| 389 |
| 390 | det += delta_det; |
| 391 | delta_det += delta_delta_det; |
| 392 | b += delta_b; |
| 393 |
| 394 | ++buffer; |
| 395 | } |
| 396 | } else { |
| 397 | while (buffer < end) { |
| 398 | *buffer++ = gradientPixel(&data->mGradient, std::sqrt(det) - b); |
| 399 |
| 400 | det += delta_det; |
| 401 | delta_det += delta_delta_det; |
| 402 | b += delta_b; |
| 403 | } |
| 404 | } |
| 405 | } |
| 406 |
| 407 | void fetch_radial_gradient(uint32_t *buffer, const Operator *op, |
| 408 | const VSpanData *data, int y, int x, int length) |
| 409 | { |
| 410 | // avoid division by zero |
| 411 | if (vIsZero(op->radial.a)) { |
| 412 | memfill32(buffer, 0, length); |
| 413 | return; |
| 414 | } |
| 415 |
| 416 | float rx = |
| 417 | data->m21 * (y + float(0.5)) + data->dx + data->m11 * (x + float(0.5)); |
| 418 | float ry = |
| 419 | data->m22 * (y + float(0.5)) + data->dy + data->m12 * (x + float(0.5)); |
| 420 | bool affine = !data->m13 && !data->m23; |
| 421 |
| 422 | uint32_t *end = buffer + length; |
| 423 | if (affine) { |
| 424 | rx -= data->mGradient.radial.fx; |
| 425 | ry -= data->mGradient.radial.fy; |
| 426 |
| 427 | float inv_a = 1 / float(2 * op->radial.a); |
| 428 |
| 429 | const float delta_rx = data->m11; |
| 430 | const float delta_ry = data->m12; |
| 431 |
| 432 | float b = 2 * (op->radial.dr * data->mGradient.radial.fradius + |
| 433 | rx * op->radial.dx + ry * op->radial.dy); |
| 434 | float delta_b = |
| 435 | 2 * (delta_rx * op->radial.dx + delta_ry * op->radial.dy); |
| 436 | const float b_delta_b = 2 * b * delta_b; |
| 437 | const float delta_b_delta_b = 2 * delta_b * delta_b; |
| 438 |
| 439 | const float bb = b * b; |
| 440 | const float delta_bb = delta_b * delta_b; |
| 441 |
| 442 | b *= inv_a; |
| 443 | delta_b *= inv_a; |
| 444 |
| 445 | const float rxrxryry = rx * rx + ry * ry; |
| 446 | const float delta_rxrxryry = delta_rx * delta_rx + delta_ry * delta_ry; |
| 447 | const float rx_plus_ry = 2 * (rx * delta_rx + ry * delta_ry); |
| 448 | const float delta_rx_plus_ry = 2 * delta_rxrxryry; |
| 449 |
| 450 | inv_a *= inv_a; |
| 451 |
| 452 | float det = |
| 453 | (bb - 4 * op->radial.a * (op->radial.sqrfr - rxrxryry)) * inv_a; |
| 454 | float delta_det = (b_delta_b + delta_bb + |
| 455 | 4 * op->radial.a * (rx_plus_ry + delta_rxrxryry)) * |
| 456 | inv_a; |
| 457 | const float delta_delta_det = |
| 458 | (delta_b_delta_b + 4 * op->radial.a * delta_rx_plus_ry) * inv_a; |
| 459 |
| 460 | fetch(buffer, end, op, data, det, delta_det, delta_delta_det, b, |
| 461 | delta_b); |
| 462 | } else { |
| 463 | float rw = data->m23 * (y + float(0.5)) + data->m33 + |
| 464 | data->m13 * (x + float(0.5)); |
| 465 |
| 466 | while (buffer < end) { |
| 467 | if (rw == 0) { |
| 468 | *buffer = 0; |
| 469 | } else { |
| 470 | float invRw = 1 / rw; |
| 471 | float gx = rx * invRw - data->mGradient.radial.fx; |
| 472 | float gy = ry * invRw - data->mGradient.radial.fy; |
| 473 | float b = 2 * (op->radial.dr * data->mGradient.radial.fradius + |
| 474 | gx * op->radial.dx + gy * op->radial.dy); |
| 475 | float det = radialDeterminant( |
| 476 | op->radial.a, b, op->radial.sqrfr - (gx * gx + gy * gy)); |
| 477 |
| 478 | uint32_t result = 0; |
| 479 | if (det >= 0) { |
| 480 | float detSqrt = std::sqrt(det); |
| 481 |
| 482 | float s0 = (-b - detSqrt) * op->radial.inv2a; |
| 483 | float s1 = (-b + detSqrt) * op->radial.inv2a; |
| 484 |
| 485 | float s = vMax(s0, s1); |
| 486 |
| 487 | if (data->mGradient.radial.fradius + op->radial.dr * s >= 0) |
| 488 | result = gradientPixel(&data->mGradient, s); |
| 489 | } |
| 490 |
| 491 | *buffer = result; |
| 492 | } |
| 493 |
| 494 | rx += data->m11; |
| 495 | ry += data->m12; |
| 496 | rw += data->m13; |
| 497 |
| 498 | ++buffer; |
| 499 | } |
| 500 | } |
| 501 | } |
| 502 |
| 503 | static inline Operator getOperator(const VSpanData *data, const VRle::Span *, |
| 504 | size_t) |
| 505 | { |
| 506 | Operator op; |
| 507 | bool solidSource = false; |
| 508 |
| 509 | switch (data->mType) { |
| 510 | case VSpanData::Type::Solid: |
| 511 | solidSource = (vAlpha(data->mSolid) == 255); |
| 512 | op.srcFetch = nullptr; |
| 513 | break; |
| 514 | case VSpanData::Type::LinearGradient: |
| 515 | solidSource = false; |
| 516 | getLinearGradientValues(&op.linear, data); |
| 517 | op.srcFetch = &fetch_linear_gradient; |
| 518 | break; |
| 519 | case VSpanData::Type::RadialGradient: |
| 520 | solidSource = false; |
| 521 | getRadialGradientValues(&op.radial, data); |
| 522 | op.srcFetch = &fetch_radial_gradient; |
| 523 | break; |
| 524 | default: |
| 525 | op.srcFetch = nullptr; |
| 526 | break; |
| 527 | } |
| 528 |
| 529 | op.mode = data->mCompositionMode; |
| 530 | if (op.mode == VPainter::CompModeSrcOver && solidSource) |
| 531 | op.mode = VPainter::CompModeSrc; |
| 532 |
| 533 | op.funcSolid = functionForModeSolid[op.mode]; |
| 534 | op.func = functionForMode[op.mode]; |
| 535 |
| 536 | return op; |
| 537 | } |
| 538 |
| 539 | static void blendColorARGB(size_t count, const VRle::Span *spans, |
| 540 | void *userData) |
| 541 | { |
| 542 | VSpanData *data = (VSpanData *)(userData); |
| 543 | Operator op = getOperator(data, spans, count); |
| 544 | const uint color = data->mSolid; |
| 545 |
| 546 | if (op.mode == VPainter::CompModeSrc) { |
| 547 | // inline for performance |
| 548 | while (count--) { |
| 549 | uint *target = data->buffer(spans->x, spans->y); |
| 550 | if (spans->coverage == 255) { |
| 551 | memfill32(target, color, spans->len); |
| 552 | } else { |
| 553 | uint c = BYTE_MUL(color, spans->coverage); |
| 554 | int ialpha = 255 - spans->coverage; |
| 555 | for (int i = 0; i < spans->len; ++i) |
| 556 | target[i] = c + BYTE_MUL(target[i], ialpha); |
| 557 | } |
| 558 | ++spans; |
| 559 | } |
| 560 | return; |
| 561 | } |
| 562 |
| 563 | while (count--) { |
| 564 | uint *target = data->buffer(spans->x, spans->y); |
| 565 | op.funcSolid(target, spans->len, color, spans->coverage); |
| 566 | ++spans; |
| 567 | } |
| 568 | } |
| 569 |
| 570 | #define BLEND_GRADIENT_BUFFER_SIZE 2048 |
| 571 | static void blendGradientARGB(size_t count, const VRle::Span *spans, |
| 572 | void *userData) |
| 573 | { |
| 574 | VSpanData *data = (VSpanData *)(userData); |
| 575 | Operator op = getOperator(data, spans, count); |
| 576 |
| 577 | unsigned int buffer[BLEND_GRADIENT_BUFFER_SIZE]; |
| 578 |
| 579 | if (!op.srcFetch) return; |
| 580 |
| 581 | while (count--) { |
| 582 | uint *target = data->buffer(spans->x, spans->y); |
| 583 | int length = spans->len; |
| 584 | while (length) { |
| 585 | int l = std::min(length, BLEND_GRADIENT_BUFFER_SIZE); |
| 586 | op.srcFetch(buffer, &op, data, spans->y, spans->x, l); |
| 587 | op.func(target, buffer, l, spans->coverage); |
| 588 | target += l; |
| 589 | length -= l; |
| 590 | } |
| 591 | ++spans; |
| 592 | } |
| 593 | } |
| 594 |
| 595 | template <class T> |
| 596 | constexpr const T &clamp(const T &v, const T &lo, const T &hi) |
| 597 | { |
| 598 | return v < lo ? lo : hi < v ? hi : v; |
| 599 | } |
| 600 |
| 601 | static const int buffer_size = 1024; |
| 602 | static const int fixed_scale = 1 << 16; |
| 603 | static void blend_transformed_argb(size_t count, const VRle::Span *spans, |
| 604 | void *userData) |
| 605 | { |
| 606 | VSpanData *data = reinterpret_cast<VSpanData *>(userData); |
| 607 | if (data->mBitmap.format != VBitmap::Format::ARGB32_Premultiplied && |
| 608 | data->mBitmap.format != VBitmap::Format::ARGB32) { |
| 609 | //@TODO other formats not yet handled. |
| 610 | return; |
| 611 | } |
| 612 |
| 613 | Operator op = getOperator(data, spans, count); |
| 614 | uint buffer[buffer_size]; |
| 615 |
| 616 | const int image_x1 = data->mBitmap.x1; |
| 617 | const int image_y1 = data->mBitmap.y1; |
| 618 | const int image_x2 = data->mBitmap.x2 - 1; |
| 619 | const int image_y2 = data->mBitmap.y2 - 1; |
| 620 |
| 621 | if (data->fast_matrix) { |
| 622 | // The increment pr x in the scanline |
| 623 | int fdx = (int)(data->m11 * fixed_scale); |
| 624 | int fdy = (int)(data->m12 * fixed_scale); |
| 625 |
| 626 | while (count--) { |
| 627 | uint *target = data->buffer(spans->x, spans->y); |
| 628 |
| 629 | const float cx = spans->x + float(0.5); |
| 630 | const float cy = spans->y + float(0.5); |
| 631 |
| 632 | int x = |
| 633 | int((data->m21 * cy + data->m11 * cx + data->dx) * fixed_scale); |
| 634 | int y = |
| 635 | int((data->m22 * cy + data->m12 * cx + data->dy) * fixed_scale); |
| 636 |
| 637 | int length = spans->len; |
| 638 | const int coverage = |
| 639 | (spans->coverage * data->mBitmap.const_alpha) >> 8; |
| 640 | while (length) { |
| 641 | int l = std::min(length, buffer_size); |
| 642 | const uint *end = buffer + l; |
| 643 | uint * b = buffer; |
| 644 | while (b < end) { |
| 645 | int px = clamp(x >> 16, image_x1, image_x2); |
| 646 | int py = clamp(y >> 16, image_y1, image_y2); |
| 647 | *b = reinterpret_cast<const uint *>( |
| 648 | data->mBitmap.scanLine(py))[px]; |
| 649 |
| 650 | x += fdx; |
| 651 | y += fdy; |
| 652 | ++b; |
| 653 | } |
| 654 | op.func(target, buffer, l, coverage); |
| 655 | target += l; |
| 656 | length -= l; |
| 657 | } |
| 658 | ++spans; |
| 659 | } |
| 660 | } else { |
| 661 | const float fdx = data->m11; |
| 662 | const float fdy = data->m12; |
| 663 | const float fdw = data->m13; |
| 664 | while (count--) { |
| 665 | uint *target = data->buffer(spans->x, spans->y); |
| 666 |
| 667 | const float cx = spans->x + float(0.5); |
| 668 | const float cy = spans->y + float(0.5); |
| 669 |
| 670 | float x = data->m21 * cy + data->m11 * cx + data->dx; |
| 671 | float y = data->m22 * cy + data->m12 * cx + data->dy; |
| 672 | float w = data->m23 * cy + data->m13 * cx + data->m33; |
| 673 |
| 674 | int length = spans->len; |
| 675 | const int coverage = |
| 676 | (spans->coverage * data->mBitmap.const_alpha) >> 8; |
| 677 | while (length) { |
| 678 | int l = std::min(length, buffer_size); |
| 679 | const uint *end = buffer + l; |
| 680 | uint * b = buffer; |
| 681 | while (b < end) { |
| 682 | const float iw = w == 0 ? 1 : 1 / w; |
| 683 | const float tx = x * iw; |
| 684 | const float ty = y * iw; |
| 685 | const int px = |
| 686 | clamp(int(tx) - (tx < 0), image_x1, image_x2); |
| 687 | const int py = |
| 688 | clamp(int(ty) - (ty < 0), image_y1, image_y2); |
| 689 |
| 690 | *b = reinterpret_cast<const uint *>( |
| 691 | data->mBitmap.scanLine(py))[px]; |
| 692 | x += fdx; |
| 693 | y += fdy; |
| 694 | w += fdw; |
| 695 |
| 696 | ++b; |
| 697 | } |
| 698 | op.func(target, buffer, l, coverage); |
| 699 | target += l; |
| 700 | length -= l; |
| 701 | } |
| 702 | ++spans; |
| 703 | } |
| 704 | } |
| 705 | } |
| 706 |
| 707 | static void blend_untransformed_argb(size_t count, const VRle::Span *spans, |
| 708 | void *userData) |
| 709 | { |
| 710 | VSpanData *data = reinterpret_cast<VSpanData *>(userData); |
| 711 | if (data->mBitmap.format != VBitmap::Format::ARGB32_Premultiplied && |
| 712 | data->mBitmap.format != VBitmap::Format::ARGB32) { |
| 713 | //@TODO other formats not yet handled. |
| 714 | return; |
| 715 | } |
| 716 |
| 717 | Operator op = getOperator(data, spans, count); |
| 718 |
| 719 | const int image_width = data->mBitmap.width; |
| 720 | const int image_height = data->mBitmap.height; |
| 721 |
| 722 | int xoff = data->dx; |
| 723 | int yoff = data->dy; |
| 724 |
| 725 | while (count--) { |
| 726 | int x = spans->x; |
| 727 | int length = spans->len; |
| 728 | int sx = xoff + x; |
| 729 | int sy = yoff + spans->y; |
| 730 | if (sy >= 0 && sy < image_height && sx < image_width) { |
| 731 | if (sx < 0) { |
| 732 | x -= sx; |
| 733 | length += sx; |
| 734 | sx = 0; |
| 735 | } |
| 736 | if (sx + length > image_width) length = image_width - sx; |
| 737 | if (length > 0) { |
| 738 | const int coverage = |
| 739 | (spans->coverage * data->mBitmap.const_alpha) >> 8; |
| 740 | const uint *src = (const uint *)data->mBitmap.scanLine(sy) + sx; |
| 741 | uint * dest = data->buffer(x, spans->y); |
| 742 | op.func(dest, src, length, coverage); |
| 743 | } |
| 744 | } |
| 745 | ++spans; |
| 746 | } |
| 747 | } |
| 748 |
| 749 | void VSpanData::setup(const VBrush &brush, VPainter::CompositionMode /*mode*/, |
| 750 | int /*alpha*/) |
| 751 | { |
| 752 | transformType = VMatrix::MatrixType::None; |
| 753 |
| 754 | switch (brush.type()) { |
| 755 | case VBrush::Type::NoBrush: |
| 756 | mType = VSpanData::Type::None; |
| 757 | break; |
| 758 | case VBrush::Type::Solid: |
| 759 | mType = VSpanData::Type::Solid; |
| 760 | mSolid = brush.mColor.premulARGB(); |
| 761 | break; |
| 762 | case VBrush::Type::LinearGradient: { |
| 763 | mType = VSpanData::Type::LinearGradient; |
| 764 | mColorTable = VGradientCacheInstance.getBuffer(*brush.mGradient); |
| 765 | mGradient.mColorTable = mColorTable->buffer32; |
| 766 | mGradient.mColorTableAlpha = mColorTable->alpha; |
| 767 | mGradient.linear.x1 = brush.mGradient->linear.x1; |
| 768 | mGradient.linear.y1 = brush.mGradient->linear.y1; |
| 769 | mGradient.linear.x2 = brush.mGradient->linear.x2; |
| 770 | mGradient.linear.y2 = brush.mGradient->linear.y2; |
| 771 | mGradient.mSpread = brush.mGradient->mSpread; |
| 772 | setupMatrix(brush.mGradient->mMatrix); |
| 773 | break; |
| 774 | } |
| 775 | case VBrush::Type::RadialGradient: { |
| 776 | mType = VSpanData::Type::RadialGradient; |
| 777 | mColorTable = VGradientCacheInstance.getBuffer(*brush.mGradient); |
| 778 | mGradient.mColorTable = mColorTable->buffer32; |
| 779 | mGradient.mColorTableAlpha = mColorTable->alpha; |
| 780 | mGradient.radial.cx = brush.mGradient->radial.cx; |
| 781 | mGradient.radial.cy = brush.mGradient->radial.cy; |
| 782 | mGradient.radial.fx = brush.mGradient->radial.fx; |
| 783 | mGradient.radial.fy = brush.mGradient->radial.fy; |
| 784 | mGradient.radial.cradius = brush.mGradient->radial.cradius; |
| 785 | mGradient.radial.fradius = brush.mGradient->radial.fradius; |
| 786 | mGradient.mSpread = brush.mGradient->mSpread; |
| 787 | setupMatrix(brush.mGradient->mMatrix); |
| 788 | break; |
| 789 | } |
| 790 | case VBrush::Type::Texture: { |
| 791 | mType = VSpanData::Type::Texture; |
| 792 | initTexture( |
| 793 | &brush.mTexture, 255, VBitmapData::Plain, |
| 794 | VRect(0, 0, brush.mTexture.width(), brush.mTexture.height())); |
| 795 | setupMatrix(brush.mMatrix); |
| 796 | break; |
| 797 | } |
| 798 | default: |
| 799 | break; |
| 800 | } |
| 801 | updateSpanFunc(); |
| 802 | } |
| 803 |
| 804 | void VSpanData::setupMatrix(const VMatrix &matrix) |
| 805 | { |
| 806 | VMatrix inv = matrix.inverted(); |
| 807 | m11 = inv.m11; |
| 808 | m12 = inv.m12; |
| 809 | m13 = inv.m13; |
| 810 | m21 = inv.m21; |
| 811 | m22 = inv.m22; |
| 812 | m23 = inv.m23; |
| 813 | m33 = inv.m33; |
| 814 | dx = inv.mtx; |
| 815 | dy = inv.mty; |
| 816 | transformType = inv.type(); |
| 817 |
| 818 | const bool affine = inv.isAffine(); |
| 819 | const float f1 = m11 * m11 + m21 * m21; |
| 820 | const float f2 = m12 * m12 + m22 * m22; |
| 821 | fast_matrix = affine && f1 < 1e4 && f2 < 1e4 && f1 > (1.0 / 65536) && |
| 822 | f2 > (1.0 / 65536) && fabs(dx) < 1e4 && fabs(dy) < 1e4; |
| 823 | } |
| 824 |
| 825 | void VSpanData::initTexture(const VBitmap *bitmap, int alpha, |
| 826 | VBitmapData::Type type, const VRect &sourceRect) |
| 827 | { |
| 828 | mType = VSpanData::Type::Texture; |
| 829 |
| 830 | mBitmap.imageData = bitmap->data(); |
| 831 | mBitmap.width = bitmap->width(); |
| 832 | mBitmap.height = bitmap->height(); |
| 833 | mBitmap.bytesPerLine = bitmap->stride(); |
| 834 | mBitmap.format = bitmap->format(); |
| 835 | mBitmap.x1 = sourceRect.x(); |
| 836 | mBitmap.y1 = sourceRect.y(); |
| 837 | mBitmap.x2 = std::min(mBitmap.x1 + sourceRect.width(), mBitmap.width); |
| 838 | mBitmap.y2 = std::min(mBitmap.y1 + sourceRect.height(), mBitmap.height); |
| 839 |
| 840 | mBitmap.const_alpha = alpha; |
| 841 | mBitmap.type = type; |
| 842 |
| 843 | updateSpanFunc(); |
| 844 | } |
| 845 |
| 846 | void VSpanData::updateSpanFunc() |
| 847 | { |
| 848 | switch (mType) { |
| 849 | case VSpanData::Type::None: |
| 850 | mUnclippedBlendFunc = nullptr; |
| 851 | break; |
| 852 | case VSpanData::Type::Solid: |
| 853 | mUnclippedBlendFunc = &blendColorARGB; |
| 854 | break; |
| 855 | case VSpanData::Type::LinearGradient: |
| 856 | case VSpanData::Type::RadialGradient: { |
| 857 | mUnclippedBlendFunc = &blendGradientARGB; |
| 858 | break; |
| 859 | } |
| 860 | case VSpanData::Type::Texture: { |
| 861 | //@TODO update proper image function. |
| 862 | if (transformType <= VMatrix::MatrixType::Translate) { |
| 863 | mUnclippedBlendFunc = &blend_untransformed_argb; |
| 864 | } else { |
| 865 | mUnclippedBlendFunc = &blend_transformed_argb; |
| 866 | } |
| 867 | break; |
| 868 | } |
| 869 | } |
| 870 | } |
| 871 |
| 872 | #if !defined(__ARM_NEON__) && !defined(__ARM64_NEON__) |
| 873 |
| 874 | void memfill32(uint32_t *dest, uint32_t value, int length) |
| 875 | { |
| 876 | int n; |
| 877 |
| 878 | if (length <= 0) return; |
| 879 |
| 880 | // Cute hack to align future memcopy operation |
| 881 | // and do unroll the loop a bit. Not sure it is |
| 882 | // the most efficient, but will do for now. |
| 883 | n = (length + 7) / 8; |
| 884 | switch (length & 0x07) { |
| 885 | case 0: |
| 886 | do { |
| 887 | *dest++ = value; |
| 888 | VECTOR_FALLTHROUGH; |
| 889 | case 7: |
| 890 | *dest++ = value; |
| 891 | VECTOR_FALLTHROUGH; |
| 892 | case 6: |
| 893 | *dest++ = value; |
| 894 | VECTOR_FALLTHROUGH; |
| 895 | case 5: |
| 896 | *dest++ = value; |
| 897 | VECTOR_FALLTHROUGH; |
| 898 | case 4: |
| 899 | *dest++ = value; |
| 900 | VECTOR_FALLTHROUGH; |
| 901 | case 3: |
| 902 | *dest++ = value; |
| 903 | VECTOR_FALLTHROUGH; |
| 904 | case 2: |
| 905 | *dest++ = value; |
| 906 | VECTOR_FALLTHROUGH; |
| 907 | case 1: |
| 908 | *dest++ = value; |
| 909 | } while (--n > 0); |
| 910 | } |
| 911 | } |
| 912 | #endif |
| 913 |
| 914 | void vInitDrawhelperFunctions() |
| 915 | { |
| 916 | vInitBlendFunctions(); |
| 917 |
| 918 | #if defined(__ARM_NEON__) || defined(__ARM64_NEON__) |
| 919 | // update fast path for NEON |
| 920 | extern void comp_func_solid_SourceOver_neon( |
| 921 | uint32_t * dest, int length, uint32_t color, uint32_t const_alpha); |
| 922 |
| 923 | COMP_functionForModeSolid_C[VPainter::CompModeSrcOver] = |
| 924 | comp_func_solid_SourceOver_neon; |
| 925 | #endif |
| 926 | } |
| 927 |
| 928 | V_CONSTRUCTOR_FUNCTION(vInitDrawhelperFunctions) |
| 929 |