mtklein | 9a5c47f | 2016-07-22 11:05:04 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2016 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #include "SkBlitter.h" |
| 9 | #include "SkColor.h" |
| 10 | #include "SkColorFilter.h" |
| 11 | #include "SkPM4f.h" |
| 12 | #include "SkPM4fPriv.h" |
| 13 | #include "SkRasterPipeline.h" |
| 14 | #include "SkShader.h" |
| 15 | #include "SkSRGB.h" |
| 16 | #include "SkXfermode.h" |
| 17 | |
| 18 | |
| 19 | class SkRasterPipelineBlitter : public SkBlitter { |
| 20 | public: |
| 21 | static SkBlitter* Create(const SkPixmap&, const SkPaint&, SkTBlitterAllocator*); |
| 22 | |
| 23 | SkRasterPipelineBlitter(SkPixmap dst, |
| 24 | SkRasterPipeline shader, |
| 25 | SkRasterPipeline colorFilter, |
| 26 | SkRasterPipeline xfermode, |
| 27 | SkPM4f paintColor) |
| 28 | : fDst(dst) |
| 29 | , fShader(shader) |
| 30 | , fColorFilter(colorFilter) |
| 31 | , fXfermode(xfermode) |
| 32 | , fPaintColor(paintColor) |
| 33 | {} |
| 34 | |
| 35 | void blitH (int x, int y, int w) override; |
| 36 | void blitAntiH(int x, int y, const SkAlpha[], const int16_t[]) override; |
| 37 | void blitMask (const SkMask&, const SkIRect& clip) override; |
| 38 | |
| 39 | // TODO: The default implementations of the other blits look fine, |
| 40 | // but some of them like blitV could probably benefit from custom |
| 41 | // blits using something like a SkRasterPipeline::runFew() method. |
| 42 | |
| 43 | private: |
| 44 | SkPixmap fDst; |
| 45 | SkRasterPipeline fShader, fColorFilter, fXfermode; |
| 46 | SkPM4f fPaintColor; |
| 47 | |
| 48 | typedef SkBlitter INHERITED; |
| 49 | }; |
| 50 | |
| 51 | SkBlitter* SkCreateRasterPipelineBlitter(const SkPixmap& dst, |
| 52 | const SkPaint& paint, |
| 53 | SkTBlitterAllocator* alloc) { |
| 54 | return SkRasterPipelineBlitter::Create(dst, paint, alloc); |
| 55 | } |
| 56 | |
| 57 | |
| 58 | // The default shader produces a constant color (from the SkPaint). |
| 59 | static void SK_VECTORCALL constant_color(SkRasterPipeline::Stage* st, size_t x, |
| 60 | Sk4f r, Sk4f g, Sk4f b, Sk4f a, |
| 61 | Sk4f dr, Sk4f dg, Sk4f db, Sk4f da) { |
| 62 | auto color = st->ctx<const SkPM4f*>(); |
| 63 | r = color->r(); |
| 64 | g = color->g(); |
| 65 | b = color->b(); |
| 66 | a = color->a(); |
| 67 | st->next(x, r,g,b,a, dr,dg,db,da); |
| 68 | } |
| 69 | |
| 70 | // The default transfer mode is srcover, s' = s + d*(1-sa). |
| 71 | static void SK_VECTORCALL srcover(SkRasterPipeline::Stage* st, size_t x, |
| 72 | Sk4f r, Sk4f g, Sk4f b, Sk4f a, |
| 73 | Sk4f dr, Sk4f dg, Sk4f db, Sk4f da) { |
| 74 | auto A = 1.0f - a; |
| 75 | r += dr*A; |
| 76 | g += dg*A; |
| 77 | b += db*A; |
| 78 | a += da*A; |
| 79 | st->next(x, r,g,b,a, dr,dg,db,da); |
| 80 | } |
| 81 | |
| 82 | static Sk4f lerp(const Sk4f& from, const Sk4f& to, const Sk4f& cov) { |
| 83 | return from + (to-from)*cov; |
| 84 | } |
| 85 | |
| 86 | // s' = d(1-c) + sc, for a constant c. |
| 87 | static void SK_VECTORCALL lerp_constant_float(SkRasterPipeline::Stage* st, size_t x, |
| 88 | Sk4f r, Sk4f g, Sk4f b, Sk4f a, |
| 89 | Sk4f dr, Sk4f dg, Sk4f db, Sk4f da) { |
| 90 | Sk4f c = *st->ctx<const float*>(); |
| 91 | |
| 92 | r = lerp(dr, r, c); |
| 93 | g = lerp(dg, g, c); |
| 94 | b = lerp(db, b, c); |
| 95 | a = lerp(da, a, c); |
| 96 | st->next(x, r,g,b,a, dr,dg,db,da); |
| 97 | } |
| 98 | |
| 99 | // s' = d(1-c) + sc, 4 pixels at a time for 8-bit coverage. |
| 100 | static void SK_VECTORCALL lerp_a8(SkRasterPipeline::Stage* st, size_t x, |
| 101 | Sk4f r, Sk4f g, Sk4f b, Sk4f a, |
| 102 | Sk4f dr, Sk4f dg, Sk4f db, Sk4f da) { |
| 103 | auto ptr = st->ctx<const uint8_t*>() + x; |
| 104 | Sk4f c = SkNx_cast<float>(Sk4b::Load(ptr)) * (1/255.0f); |
| 105 | |
| 106 | r = lerp(dr, r, c); |
| 107 | g = lerp(dg, g, c); |
| 108 | b = lerp(db, b, c); |
| 109 | a = lerp(da, a, c); |
| 110 | st->next(x, r,g,b,a, dr,dg,db,da); |
| 111 | } |
| 112 | |
| 113 | // Tail variant of lerp_a8() handling 1 pixel at a time. |
| 114 | static void SK_VECTORCALL lerp_a8_1(SkRasterPipeline::Stage* st, size_t x, |
| 115 | Sk4f r, Sk4f g, Sk4f b, Sk4f a, |
| 116 | Sk4f dr, Sk4f dg, Sk4f db, Sk4f da) { |
| 117 | auto ptr = st->ctx<const uint8_t*>() + x; |
| 118 | Sk4f c = *ptr * (1/255.0f); |
| 119 | |
| 120 | r = lerp(dr, r, c); |
| 121 | g = lerp(dg, g, c); |
| 122 | b = lerp(db, b, c); |
| 123 | a = lerp(da, a, c); |
| 124 | st->next(x, r,g,b,a, dr,dg,db,da); |
| 125 | } |
| 126 | |
| 127 | static void upscale_lcd16(const Sk4h& lcd16, Sk4f* r, Sk4f* g, Sk4f* b) { |
| 128 | Sk4i _32_bit = SkNx_cast<int>(lcd16); |
| 129 | |
| 130 | *r = SkNx_cast<float>(_32_bit & SK_R16_MASK_IN_PLACE) * (1.0f / SK_R16_MASK_IN_PLACE); |
| 131 | *g = SkNx_cast<float>(_32_bit & SK_G16_MASK_IN_PLACE) * (1.0f / SK_G16_MASK_IN_PLACE); |
| 132 | *b = SkNx_cast<float>(_32_bit & SK_B16_MASK_IN_PLACE) * (1.0f / SK_B16_MASK_IN_PLACE); |
| 133 | } |
| 134 | |
| 135 | // s' = d(1-c) + sc, 4 pixels at a time for 565 coverage. |
| 136 | static void SK_VECTORCALL lerp_lcd16(SkRasterPipeline::Stage* st, size_t x, |
| 137 | Sk4f r, Sk4f g, Sk4f b, Sk4f a, |
| 138 | Sk4f dr, Sk4f dg, Sk4f db, Sk4f da) { |
| 139 | auto ptr = st->ctx<const uint16_t*>() + x; |
| 140 | Sk4f cr, cg, cb; |
| 141 | upscale_lcd16(Sk4h::Load(ptr), &cr, &cg, &cb); |
| 142 | |
| 143 | r = lerp(dr, r, cr); |
| 144 | g = lerp(dg, g, cg); |
| 145 | b = lerp(db, b, cb); |
| 146 | a = 1.0f; |
| 147 | st->next(x, r,g,b,a, dr,dg,db,da); |
| 148 | } |
| 149 | |
| 150 | // Tail variant of lerp_lcd16() handling 1 pixel at a time. |
| 151 | static void SK_VECTORCALL lerp_lcd16_1(SkRasterPipeline::Stage* st, size_t x, |
| 152 | Sk4f r, Sk4f g, Sk4f b, Sk4f a, |
| 153 | Sk4f dr, Sk4f dg, Sk4f db, Sk4f da) { |
| 154 | auto ptr = st->ctx<const uint16_t*>() + x; |
| 155 | Sk4f cr, cg, cb; |
| 156 | upscale_lcd16({*ptr,0,0,0}, &cr, &cg, &cb); |
| 157 | |
| 158 | r = lerp(dr, r, cr); |
| 159 | g = lerp(dg, g, cg); |
| 160 | b = lerp(db, b, cb); |
| 161 | a = 1.0f; |
| 162 | st->next(x, r,g,b,a, dr,dg,db,da); |
| 163 | } |
| 164 | |
| 165 | // Load 4 8-bit sRGB pixels from SkPMColor order to RGBA. |
| 166 | static void SK_VECTORCALL load_d_srgb(SkRasterPipeline::Stage* st, size_t x, |
| 167 | Sk4f r, Sk4f g, Sk4f b, Sk4f a, |
| 168 | Sk4f dr, Sk4f dg, Sk4f db, Sk4f da) { |
| 169 | auto ptr = st->ctx<const uint32_t*>() + x; |
| 170 | |
| 171 | dr = { sk_linear_from_srgb[(ptr[0] >> SK_R32_SHIFT) & 0xff], |
| 172 | sk_linear_from_srgb[(ptr[1] >> SK_R32_SHIFT) & 0xff], |
| 173 | sk_linear_from_srgb[(ptr[2] >> SK_R32_SHIFT) & 0xff], |
| 174 | sk_linear_from_srgb[(ptr[3] >> SK_R32_SHIFT) & 0xff] }; |
| 175 | |
| 176 | dg = { sk_linear_from_srgb[(ptr[0] >> SK_G32_SHIFT) & 0xff], |
| 177 | sk_linear_from_srgb[(ptr[1] >> SK_G32_SHIFT) & 0xff], |
| 178 | sk_linear_from_srgb[(ptr[2] >> SK_G32_SHIFT) & 0xff], |
| 179 | sk_linear_from_srgb[(ptr[3] >> SK_G32_SHIFT) & 0xff] }; |
| 180 | |
| 181 | db = { sk_linear_from_srgb[(ptr[0] >> SK_B32_SHIFT) & 0xff], |
| 182 | sk_linear_from_srgb[(ptr[1] >> SK_B32_SHIFT) & 0xff], |
| 183 | sk_linear_from_srgb[(ptr[2] >> SK_B32_SHIFT) & 0xff], |
| 184 | sk_linear_from_srgb[(ptr[3] >> SK_B32_SHIFT) & 0xff] }; |
| 185 | |
| 186 | // TODO: this >> doesn't really need mask if we make it logical instead of arithmetic. |
| 187 | da = SkNx_cast<float>((Sk4i::Load(ptr) >> SK_A32_SHIFT) & 0xff) * (1/255.0f); |
| 188 | |
| 189 | st->next(x, r,g,b,a, dr,dg,db,da); |
| 190 | } |
| 191 | |
| 192 | // Tail variant of load_d_srgb() handling 1 pixel at a time. |
| 193 | static void SK_VECTORCALL load_d_srgb_1(SkRasterPipeline::Stage* st, size_t x, |
| 194 | Sk4f r, Sk4f g, Sk4f b, Sk4f a, |
| 195 | Sk4f dr, Sk4f dg, Sk4f db, Sk4f da) { |
| 196 | auto ptr = st->ctx<const uint32_t*>() + x; |
| 197 | |
| 198 | dr = { sk_linear_from_srgb[(*ptr >> SK_R32_SHIFT) & 0xff], 0,0,0 }; |
| 199 | dg = { sk_linear_from_srgb[(*ptr >> SK_G32_SHIFT) & 0xff], 0,0,0 }; |
| 200 | db = { sk_linear_from_srgb[(*ptr >> SK_B32_SHIFT) & 0xff], 0,0,0 }; |
| 201 | da = { (1/255.0f) * (*ptr >> SK_A32_SHIFT) , 0,0,0 }; |
| 202 | |
| 203 | st->next(x, r,g,b,a, dr,dg,db,da); |
| 204 | } |
| 205 | |
| 206 | // Write out 4 pixels as 8-bit SkPMColor-order sRGB. |
| 207 | static void SK_VECTORCALL store_srgb(SkRasterPipeline::Stage* st, size_t x, |
| 208 | Sk4f r, Sk4f g, Sk4f b, Sk4f a, |
| 209 | Sk4f dr, Sk4f dg, Sk4f db, Sk4f da) { |
| 210 | auto dst = st->ctx<uint32_t*>() + x; |
| 211 | ( sk_linear_to_srgb(r) << SK_R32_SHIFT |
| 212 | | sk_linear_to_srgb(g) << SK_G32_SHIFT |
| 213 | | sk_linear_to_srgb(b) << SK_B32_SHIFT |
| 214 | | Sk4f_round(255.0f*a) << SK_A32_SHIFT).store(dst); |
| 215 | } |
| 216 | |
| 217 | // Tail variant of store_srgb() handling 1 pixel at a time. |
| 218 | static void SK_VECTORCALL store_srgb_1(SkRasterPipeline::Stage* st, size_t x, |
| 219 | Sk4f r, Sk4f g, Sk4f b, Sk4f a, |
| 220 | Sk4f dr, Sk4f dg, Sk4f db, Sk4f da) { |
| 221 | auto dst = st->ctx<uint32_t*>() + x; |
| 222 | *dst = Sk4f_toS32(swizzle_rb_if_bgra({ r[0], g[0], b[0], a[0] })); |
| 223 | } |
| 224 | |
| 225 | |
| 226 | template <typename Effect> |
| 227 | static bool append_effect_stages(const Effect* effect, SkRasterPipeline* pipeline) { |
| 228 | return !effect || effect->appendStages(pipeline); |
| 229 | } |
| 230 | |
| 231 | |
| 232 | SkBlitter* SkRasterPipelineBlitter::Create(const SkPixmap& dst, |
| 233 | const SkPaint& paint, |
| 234 | SkTBlitterAllocator* alloc) { |
| 235 | if (!dst.info().gammaCloseToSRGB()) { |
| 236 | return nullptr; // TODO: f16, etc. |
| 237 | } |
| 238 | if (paint.getShader()) { |
| 239 | return nullptr; // TODO: need to work out how shaders and their contexts work |
| 240 | } |
| 241 | |
| 242 | SkRasterPipeline shader, colorFilter, xfermode; |
| 243 | if (!append_effect_stages(paint.getColorFilter(), &colorFilter) || |
| 244 | !append_effect_stages(paint.getXfermode(), &xfermode )) { |
| 245 | return nullptr; |
| 246 | } |
| 247 | |
| 248 | auto blitter = alloc->createT<SkRasterPipelineBlitter>( |
| 249 | dst, |
| 250 | shader, colorFilter, xfermode, |
| 251 | SkColor4f::FromColor(paint.getColor()).premul()); |
| 252 | |
| 253 | if (!paint.getShader()) { |
| 254 | blitter->fShader.append(constant_color, &blitter->fPaintColor); |
| 255 | } |
| 256 | if (!paint.getXfermode()) { |
| 257 | blitter->fXfermode.append(srcover); |
| 258 | } |
| 259 | |
| 260 | return blitter; |
| 261 | } |
| 262 | |
| 263 | void SkRasterPipelineBlitter::blitH(int x, int y, int w) { |
| 264 | auto dst = fDst.writable_addr(0,y); |
| 265 | |
| 266 | SkRasterPipeline p; |
| 267 | p.extend(fShader); |
| 268 | p.extend(fColorFilter); |
| 269 | p.append(load_d_srgb, load_d_srgb_1, dst); |
| 270 | p.extend(fXfermode); |
| 271 | p.append(store_srgb, store_srgb_1, dst); |
| 272 | |
| 273 | p.run(x, w); |
| 274 | } |
| 275 | |
| 276 | void SkRasterPipelineBlitter::blitAntiH(int x, int y, const SkAlpha aa[], const int16_t runs[]) { |
| 277 | auto dst = fDst.writable_addr(0,y); |
| 278 | float coverage; |
| 279 | |
| 280 | SkRasterPipeline p; |
| 281 | p.extend(fShader); |
| 282 | p.extend(fColorFilter); |
| 283 | p.append(load_d_srgb, load_d_srgb_1, dst); |
| 284 | p.extend(fXfermode); |
| 285 | p.append(lerp_constant_float, &coverage); |
| 286 | p.append(store_srgb, store_srgb_1, dst); |
| 287 | |
| 288 | for (int16_t run = *runs; run > 0; run = *runs) { |
| 289 | coverage = *aa * (1/255.0f); |
| 290 | p.run(x, run); |
| 291 | |
| 292 | x += run; |
| 293 | runs += run; |
| 294 | aa += run; |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | void SkRasterPipelineBlitter::blitMask(const SkMask& mask, const SkIRect& clip) { |
| 299 | if (mask.fFormat == SkMask::kBW_Format) { |
| 300 | // TODO: native BW masks? |
| 301 | return INHERITED::blitMask(mask, clip); |
| 302 | } |
| 303 | |
| 304 | int x = clip.left(); |
| 305 | for (int y = clip.top(); y < clip.bottom(); y++) { |
| 306 | auto dst = fDst.writable_addr(0,y); |
| 307 | |
| 308 | SkRasterPipeline p; |
| 309 | p.extend(fShader); |
| 310 | p.extend(fColorFilter); |
| 311 | p.append(load_d_srgb, load_d_srgb_1, dst); |
| 312 | p.extend(fXfermode); |
| 313 | switch (mask.fFormat) { |
| 314 | case SkMask::kA8_Format: |
| 315 | p.append(lerp_a8, lerp_a8_1, mask.getAddr8(x,y)-x); |
| 316 | break; |
| 317 | case SkMask::kLCD16_Format: |
| 318 | p.append(lerp_lcd16, lerp_lcd16_1, mask.getAddrLCD16(x,y)-x); |
| 319 | break; |
| 320 | default: break; |
| 321 | } |
| 322 | p.append(store_srgb, store_srgb_1, dst); |
| 323 | |
| 324 | p.run(x, clip.width()); |
| 325 | } |
| 326 | } |