| 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 | #ifndef VDRAWHELPER_H |
| 20 | #define VDRAWHELPER_H |
| 21 |
| 22 | #include <cstring> |
| 23 | #include <memory> |
| 24 | #include "assert.h" |
| 25 | #include "vbitmap.h" |
| 26 | #include "vbrush.h" |
| 27 | #include "vpainter.h" |
| 28 | #include "vrect.h" |
| 29 | #include "vrle.h" |
| 30 |
| 31 | V_USE_NAMESPACE |
| 32 |
| 33 | struct VSpanData; |
| 34 | struct Operator; |
| 35 |
| 36 | typedef void (*CompositionFunctionSolid)(uint32_t *dest, int length, |
| 37 | uint32_t color, uint32_t const_alpha); |
| 38 | typedef void (*CompositionFunction)(uint32_t *dest, const uint32_t *src, |
| 39 | int length, uint32_t const_alpha); |
| 40 | typedef void (*SourceFetchProc)(uint32_t *buffer, const Operator *o, |
| 41 | const VSpanData *data, int y, int x, |
| 42 | int length); |
| 43 | typedef void (*ProcessRleSpan)(size_t count, const VRle::Span *spans, |
| 44 | void *userData); |
| 45 |
| 46 | extern void memfill32(uint32_t *dest, uint32_t value, int count); |
| 47 |
| 48 | struct LinearGradientValues { |
| 49 | float dx; |
| 50 | float dy; |
| 51 | float l; |
| 52 | float off; |
| 53 | }; |
| 54 |
| 55 | struct RadialGradientValues { |
| 56 | float dx; |
| 57 | float dy; |
| 58 | float dr; |
| 59 | float sqrfr; |
| 60 | float a; |
| 61 | float inv2a; |
| 62 | bool extended; |
| 63 | }; |
| 64 |
| 65 | struct Operator { |
| 66 | VPainter::CompositionMode mode; |
| 67 | SourceFetchProc srcFetch; |
| 68 | CompositionFunctionSolid funcSolid; |
| 69 | CompositionFunction func; |
| 70 | union { |
| 71 | LinearGradientValues linear; |
| 72 | RadialGradientValues radial; |
| 73 | }; |
| 74 | }; |
| 75 |
| 76 | class VRasterBuffer { |
| 77 | public: |
| 78 | VBitmap::Format prepare(VBitmap *image); |
| 79 | void clear(); |
| 80 |
| 81 | void resetBuffer(int val = 0); |
| 82 |
| 83 | inline uchar *scanLine(int y) |
| 84 | { |
| 85 | assert(y >= 0); |
| 86 | assert(y < mHeight); |
| 87 | return mBuffer + y * mBytesPerLine; |
| 88 | } |
| 89 |
| 90 | int width() const { return mWidth; } |
| 91 | int height() const { return mHeight; } |
| 92 | int bytesPerLine() const { return mBytesPerLine; } |
| 93 | int bytesPerPixel() const { return mBytesPerPixel; } |
| 94 |
| 95 | VBitmap::Format mFormat{VBitmap::Format::ARGB32}; |
| 96 | private: |
| 97 | int mWidth{0}; |
| 98 | int mHeight{0}; |
| 99 | int mBytesPerLine{0}; |
| 100 | int mBytesPerPixel{0}; |
| 101 | uchar *mBuffer{nullptr}; |
| 102 | }; |
| 103 |
| 104 | struct VGradientData { |
| 105 | VGradient::Spread mSpread; |
| 106 | union { |
| 107 | struct { |
| 108 | float x1, y1, x2, y2; |
| 109 | } linear; |
| 110 | struct { |
| 111 | float cx, cy, fx, fy, cradius, fradius; |
| 112 | } radial; |
| 113 | }; |
| 114 | const uint32_t *mColorTable; |
| 115 | bool mColorTableAlpha; |
| 116 | }; |
| 117 |
| 118 | struct VBitmapData |
| 119 | { |
| 120 | const uchar *imageData; |
| 121 | const uchar *scanLine(int y) const { return imageData + y*bytesPerLine; } |
| 122 |
| 123 | int width; |
| 124 | int height; |
| 125 | // clip rect |
| 126 | int x1; |
| 127 | int y1; |
| 128 | int x2; |
| 129 | int y2; |
| 130 | uint bytesPerLine; |
| 131 | VBitmap::Format format; |
| 132 | bool hasAlpha; |
| 133 | enum Type { |
| 134 | Plain, |
| 135 | Tiled |
| 136 | }; |
| 137 | Type type; |
| 138 | int const_alpha; |
| 139 | }; |
| 140 |
| 141 | struct VColorTable |
| 142 | { |
| 143 | uint32_t buffer32[VGradient::colorTableSize]; |
| 144 | bool alpha{true}; |
| 145 | }; |
| 146 |
| 147 | struct VSpanData { |
| 148 | enum class Type { None, Solid, LinearGradient, RadialGradient, Texture }; |
| 149 |
| 150 | void updateSpanFunc(); |
| 151 | void init(VRasterBuffer *image); |
| 152 | void setup(const VBrush & brush, |
| 153 | VPainter::CompositionMode mode = VPainter::CompModeSrcOver, |
| 154 | int alpha = 255); |
| 155 | void setupMatrix(const VMatrix &matrix); |
| 156 |
| 157 | VRect clipRect() const |
| 158 | { |
| 159 | return VRect(0, 0, mDrawableSize.width(), mDrawableSize.height()); |
| 160 | } |
| 161 |
| 162 | void setDrawRegion(const VRect ®ion) |
| 163 | { |
| 164 | mOffset = VPoint(region.left(), region.top()); |
| 165 | mDrawableSize = VSize(region.width(), region.height()); |
| 166 | } |
| 167 |
| 168 | uint *buffer(int x, int y) const |
| 169 | { |
| 170 | return (uint *)(mRasterBuffer->scanLine(y + mOffset.y())) + x + mOffset.x(); |
| 171 | } |
| 172 | void initTexture(const VBitmap *image, int alpha, VBitmapData::Type type, const VRect &sourceRect); |
| 173 |
| 174 | VPainter::CompositionMode mCompositionMode{VPainter::CompositionMode::CompModeSrcOver}; |
| 175 | VRasterBuffer * mRasterBuffer; |
| 176 | ProcessRleSpan mBlendFunc; |
| 177 | ProcessRleSpan mUnclippedBlendFunc; |
| 178 | VSpanData::Type mType; |
| 179 | std::shared_ptr<const VColorTable> mColorTable{nullptr}; |
| 180 | VPoint mOffset; // offset to the subsurface |
| 181 | VSize mDrawableSize;// suburface size |
| 182 | union { |
| 183 | uint32_t mSolid; |
| 184 | VGradientData mGradient; |
| 185 | VBitmapData mBitmap; |
| 186 | }; |
| 187 | float m11, m12, m13, m21, m22, m23, m33, dx, dy; // inverse xform matrix |
| 188 | bool fast_matrix{true}; |
| 189 | VMatrix::MatrixType transformType{VMatrix::MatrixType::None}; |
| 190 | }; |
| 191 |
| 192 | void vInitDrawhelperFunctions(); |
| 193 | extern void vInitBlendFunctions(); |
| 194 |
| 195 | #define BYTE_MUL(c, a) \ |
| 196 | ((((((c) >> 8) & 0x00ff00ff) * (a)) & 0xff00ff00) + \ |
| 197 | (((((c)&0x00ff00ff) * (a)) >> 8) & 0x00ff00ff)) |
| 198 |
| 199 | inline constexpr int vRed(uint32_t c) |
| 200 | { |
| 201 | return ((c >> 16) & 0xff); |
| 202 | } |
| 203 |
| 204 | inline constexpr int vGreen(uint32_t c) |
| 205 | { |
| 206 | return ((c >> 8) & 0xff); |
| 207 | } |
| 208 |
| 209 | inline constexpr int vBlue(uint32_t c) |
| 210 | { |
| 211 | return (c & 0xff); |
| 212 | } |
| 213 |
| 214 | inline constexpr int vAlpha(uint32_t c) |
| 215 | { |
| 216 | return c >> 24; |
| 217 | } |
| 218 |
| 219 | static inline uint INTERPOLATE_PIXEL_255(uint x, uint a, uint y, uint b) |
| 220 | { |
| 221 | uint t = (x & 0xff00ff) * a + (y & 0xff00ff) * b; |
| 222 | t >>= 8; |
| 223 | t &= 0xff00ff; |
| 224 | x = ((x >> 8) & 0xff00ff) * a + ((y >> 8) & 0xff00ff) * b; |
| 225 | x &= 0xff00ff00; |
| 226 | x |= t; |
| 227 | return x; |
| 228 | } |
| 229 |
| 230 | #define LOOP_ALIGNED_U1_A4(DEST, LENGTH, UOP, A4OP) \ |
| 231 | { \ |
| 232 | while ((uintptr_t)DEST & 0xF && LENGTH) \ |
| 233 | UOP \ |
| 234 | \ |
| 235 | while (LENGTH) \ |
| 236 | { \ |
| 237 | switch (LENGTH) { \ |
| 238 | case 3: \ |
| 239 | case 2: \ |
| 240 | case 1: \ |
| 241 | UOP break; \ |
| 242 | default: \ |
| 243 | A4OP break; \ |
| 244 | } \ |
| 245 | } \ |
| 246 | } |
| 247 |
| 248 | #endif // QDRAWHELPER_P_H |
| 249 |