1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
|
/*
* Copyright 2006 The Android Open Source Project
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "SkCoreBlitters.h"
#include "SkColorPriv.h"
#include "SkShader.h"
#include "SkUtils.h"
#include "SkXfermode.h"
#include "SkBlitMask.h"
///////////////////////////////////////////////////////////////////////////////
static void SkARGB32_Blit32(const SkBitmap& device, const SkMask& mask,
const SkIRect& clip, SkPMColor srcColor) {
U8CPU alpha = SkGetPackedA32(srcColor);
unsigned flags = SkBlitRow::kSrcPixelAlpha_Flag32;
if (alpha != 255) {
flags |= SkBlitRow::kGlobalAlpha_Flag32;
}
SkBlitRow::Proc32 proc = SkBlitRow::Factory32(flags);
int x = clip.fLeft;
int y = clip.fTop;
int width = clip.width();
int height = clip.height();
SkPMColor* dstRow = device.getAddr32(x, y);
const SkPMColor* srcRow = reinterpret_cast<const SkPMColor*>(mask.getAddr8(x, y));
do {
proc(dstRow, srcRow, width, alpha);
dstRow = (SkPMColor*)((char*)dstRow + device.rowBytes());
srcRow = (const SkPMColor*)((const char*)srcRow + mask.fRowBytes);
} while (--height != 0);
}
//////////////////////////////////////////////////////////////////////////////////////
SkARGB32_Blitter::SkARGB32_Blitter(const SkBitmap& device, const SkPaint& paint)
: INHERITED(device) {
SkColor color = paint.getColor();
fColor = color;
fSrcA = SkColorGetA(color);
unsigned scale = SkAlpha255To256(fSrcA);
fSrcR = SkAlphaMul(SkColorGetR(color), scale);
fSrcG = SkAlphaMul(SkColorGetG(color), scale);
fSrcB = SkAlphaMul(SkColorGetB(color), scale);
fPMColor = SkPackARGB32(fSrcA, fSrcR, fSrcG, fSrcB);
fColor32Proc = SkBlitRow::ColorProcFactory();
fColorRect32Proc = SkBlitRow::ColorRectProcFactory();
}
const SkBitmap* SkARGB32_Blitter::justAnOpaqueColor(uint32_t* value) {
if (255 == fSrcA) {
*value = fPMColor;
return &fDevice;
}
return NULL;
}
#if defined _WIN32 && _MSC_VER >= 1300 // disable warning : local variable used without having been initialized
#pragma warning ( push )
#pragma warning ( disable : 4701 )
#endif
void SkARGB32_Blitter::blitH(int x, int y, int width) {
SkASSERT(x >= 0 && y >= 0 && x + width <= fDevice.width());
uint32_t* device = fDevice.getAddr32(x, y);
fColor32Proc(device, device, width, fPMColor);
}
void SkARGB32_Blitter::blitAntiH(int x, int y, const SkAlpha antialias[],
const int16_t runs[]) {
if (fSrcA == 0) {
return;
}
uint32_t color = fPMColor;
uint32_t* device = fDevice.getAddr32(x, y);
unsigned opaqueMask = fSrcA; // if fSrcA is 0xFF, then we will catch the fast opaque case
for (;;) {
int count = runs[0];
SkASSERT(count >= 0);
if (count <= 0) {
return;
}
unsigned aa = antialias[0];
if (aa) {
if ((opaqueMask & aa) == 255) {
sk_memset32(device, color, count);
} else {
uint32_t sc = SkAlphaMulQ(color, SkAlpha255To256(aa));
fColor32Proc(device, device, count, sc);
}
}
runs += count;
antialias += count;
device += count;
}
}
//////////////////////////////////////////////////////////////////////////////////////
#define solid_8_pixels(mask, dst, color) \
do { \
if (mask & 0x80) dst[0] = color; \
if (mask & 0x40) dst[1] = color; \
if (mask & 0x20) dst[2] = color; \
if (mask & 0x10) dst[3] = color; \
if (mask & 0x08) dst[4] = color; \
if (mask & 0x04) dst[5] = color; \
if (mask & 0x02) dst[6] = color; \
if (mask & 0x01) dst[7] = color; \
} while (0)
#define SK_BLITBWMASK_NAME SkARGB32_BlitBW
#define SK_BLITBWMASK_ARGS , SkPMColor color
#define SK_BLITBWMASK_BLIT8(mask, dst) solid_8_pixels(mask, dst, color)
#define SK_BLITBWMASK_GETADDR getAddr32
#define SK_BLITBWMASK_DEVTYPE uint32_t
#include "SkBlitBWMaskTemplate.h"
#define blend_8_pixels(mask, dst, sc, dst_scale) \
do { \
if (mask & 0x80) { dst[0] = sc + SkAlphaMulQ(dst[0], dst_scale); } \
if (mask & 0x40) { dst[1] = sc + SkAlphaMulQ(dst[1], dst_scale); } \
if (mask & 0x20) { dst[2] = sc + SkAlphaMulQ(dst[2], dst_scale); } \
if (mask & 0x10) { dst[3] = sc + SkAlphaMulQ(dst[3], dst_scale); } \
if (mask & 0x08) { dst[4] = sc + SkAlphaMulQ(dst[4], dst_scale); } \
if (mask & 0x04) { dst[5] = sc + SkAlphaMulQ(dst[5], dst_scale); } \
if (mask & 0x02) { dst[6] = sc + SkAlphaMulQ(dst[6], dst_scale); } \
if (mask & 0x01) { dst[7] = sc + SkAlphaMulQ(dst[7], dst_scale); } \
} while (0)
#define SK_BLITBWMASK_NAME SkARGB32_BlendBW
#define SK_BLITBWMASK_ARGS , uint32_t sc, unsigned dst_scale
#define SK_BLITBWMASK_BLIT8(mask, dst) blend_8_pixels(mask, dst, sc, dst_scale)
#define SK_BLITBWMASK_GETADDR getAddr32
#define SK_BLITBWMASK_DEVTYPE uint32_t
#include "SkBlitBWMaskTemplate.h"
void SkARGB32_Blitter::blitMask(const SkMask& mask, const SkIRect& clip) {
SkASSERT(mask.fBounds.contains(clip));
SkASSERT(fSrcA != 0xFF);
if (fSrcA == 0) {
return;
}
if (SkBlitMask::BlitColor(fDevice, mask, clip, fColor)) {
return;
}
if (mask.fFormat == SkMask::kBW_Format) {
SkARGB32_BlendBW(fDevice, mask, clip, fPMColor, SkAlpha255To256(255 - fSrcA));
} else if (SkMask::kARGB32_Format == mask.fFormat) {
SkARGB32_Blit32(fDevice, mask, clip, fPMColor);
}
}
void SkARGB32_Opaque_Blitter::blitMask(const SkMask& mask,
const SkIRect& clip) {
SkASSERT(mask.fBounds.contains(clip));
if (SkBlitMask::BlitColor(fDevice, mask, clip, fColor)) {
return;
}
if (mask.fFormat == SkMask::kBW_Format) {
SkARGB32_BlitBW(fDevice, mask, clip, fPMColor);
} else if (SkMask::kARGB32_Format == mask.fFormat) {
SkARGB32_Blit32(fDevice, mask, clip, fPMColor);
}
}
///////////////////////////////////////////////////////////////////////////////
void SkARGB32_Blitter::blitV(int x, int y, int height, SkAlpha alpha) {
if (alpha == 0 || fSrcA == 0) {
return;
}
uint32_t* device = fDevice.getAddr32(x, y);
uint32_t color = fPMColor;
if (alpha != 255) {
color = SkAlphaMulQ(color, SkAlpha255To256(alpha));
}
unsigned dst_scale = 255 - SkGetPackedA32(color);
size_t rowBytes = fDevice.rowBytes();
while (--height >= 0) {
device[0] = color + SkAlphaMulQ(device[0], dst_scale);
device = (uint32_t*)((char*)device + rowBytes);
}
}
void SkARGB32_Blitter::blitRect(int x, int y, int width, int height) {
SkASSERT(x >= 0 && y >= 0 && x + width <= fDevice.width() && y + height <= fDevice.height());
if (fSrcA == 0) {
return;
}
uint32_t* device = fDevice.getAddr32(x, y);
uint32_t color = fPMColor;
size_t rowBytes = fDevice.rowBytes();
if (255 == SkGetPackedA32(color)) {
fColorRect32Proc(device, width, height, rowBytes, color);
} else {
while (--height >= 0) {
fColor32Proc(device, device, width, color);
device = (uint32_t*)((char*)device + rowBytes);
}
}
}
#if defined _WIN32 && _MSC_VER >= 1300
#pragma warning ( pop )
#endif
///////////////////////////////////////////////////////////////////////
void SkARGB32_Black_Blitter::blitAntiH(int x, int y, const SkAlpha antialias[],
const int16_t runs[]) {
uint32_t* device = fDevice.getAddr32(x, y);
SkPMColor black = (SkPMColor)(SK_A32_MASK << SK_A32_SHIFT);
for (;;) {
int count = runs[0];
SkASSERT(count >= 0);
if (count <= 0) {
return;
}
unsigned aa = antialias[0];
if (aa) {
if (aa == 255) {
sk_memset32(device, black, count);
} else {
SkPMColor src = aa << SK_A32_SHIFT;
unsigned dst_scale = 256 - aa;
int n = count;
do {
--n;
device[n] = src + SkAlphaMulQ(device[n], dst_scale);
} while (n > 0);
}
}
runs += count;
antialias += count;
device += count;
}
}
///////////////////////////////////////////////////////////////////////////////
// Special version of SkBlitRow::Factory32 that knows we're in kSrc_Mode,
// instead of kSrcOver_Mode
static void blend_srcmode(SkPMColor* SK_RESTRICT device,
const SkPMColor* SK_RESTRICT span,
int count, U8CPU aa) {
int aa256 = SkAlpha255To256(aa);
for (int i = 0; i < count; ++i) {
device[i] = SkFourByteInterp256(span[i], device[i], aa256);
}
}
SkARGB32_Shader_Blitter::SkARGB32_Shader_Blitter(const SkBitmap& device,
const SkPaint& paint) : INHERITED(device, paint) {
fBuffer = (SkPMColor*)sk_malloc_throw(device.width() * (sizeof(SkPMColor)));
fXfermode = paint.getXfermode();
SkSafeRef(fXfermode);
int flags = 0;
if (!(fShader->getFlags() & SkShader::kOpaqueAlpha_Flag)) {
flags |= SkBlitRow::kSrcPixelAlpha_Flag32;
}
// we call this on the output from the shader
fProc32 = SkBlitRow::Factory32(flags);
// we call this on the output from the shader + alpha from the aa buffer
fProc32Blend = SkBlitRow::Factory32(flags | SkBlitRow::kGlobalAlpha_Flag32);
fShadeDirectlyIntoDevice = false;
if (fXfermode == NULL) {
if (fShader->getFlags() & SkShader::kOpaqueAlpha_Flag) {
fShadeDirectlyIntoDevice = true;
}
} else {
SkXfermode::Mode mode;
if (fXfermode->asMode(&mode)) {
if (SkXfermode::kSrc_Mode == mode) {
fShadeDirectlyIntoDevice = true;
fProc32Blend = blend_srcmode;
}
}
}
fConstInY = SkToBool(fShader->getFlags() & SkShader::kConstInY32_Flag);
}
SkARGB32_Shader_Blitter::~SkARGB32_Shader_Blitter() {
SkSafeUnref(fXfermode);
sk_free(fBuffer);
}
void SkARGB32_Shader_Blitter::blitH(int x, int y, int width) {
SkASSERT(x >= 0 && y >= 0 && x + width <= fDevice.width());
uint32_t* device = fDevice.getAddr32(x, y);
if (fShadeDirectlyIntoDevice) {
fShader->shadeSpan(x, y, device, width);
} else {
SkPMColor* span = fBuffer;
fShader->shadeSpan(x, y, span, width);
if (fXfermode) {
fXfermode->xfer32(device, span, width, NULL);
} else {
fProc32(device, span, width, 255);
}
}
}
void SkARGB32_Shader_Blitter::blitRect(int x, int y, int width, int height) {
SkASSERT(x >= 0 && y >= 0 &&
x + width <= fDevice.width() && y + height <= fDevice.height());
uint32_t* device = fDevice.getAddr32(x, y);
size_t deviceRB = fDevice.rowBytes();
SkShader* shader = fShader;
SkPMColor* span = fBuffer;
if (fConstInY) {
if (fShadeDirectlyIntoDevice) {
// shade the first row directly into the device
fShader->shadeSpan(x, y, device, width);
span = device;
while (--height > 0) {
device = (uint32_t*)((char*)device + deviceRB);
memcpy(device, span, width << 2);
}
} else {
fShader->shadeSpan(x, y, span, width);
SkXfermode* xfer = fXfermode;
if (xfer) {
do {
xfer->xfer32(device, span, width, NULL);
y += 1;
device = (uint32_t*)((char*)device + deviceRB);
} while (--height > 0);
} else {
SkBlitRow::Proc32 proc = fProc32;
do {
proc(device, span, width, 255);
y += 1;
device = (uint32_t*)((char*)device + deviceRB);
} while (--height > 0);
}
}
return;
}
if (fShadeDirectlyIntoDevice) {
void* ctx;
SkShader::ShadeProc shadeProc = fShader->asAShadeProc(&ctx);
if (shadeProc) {
do {
shadeProc(ctx, x, y, device, width);
y += 1;
device = (uint32_t*)((char*)device + deviceRB);
} while (--height > 0);
} else {
do {
shader->shadeSpan(x, y, device, width);
y += 1;
device = (uint32_t*)((char*)device + deviceRB);
} while (--height > 0);
}
} else {
SkXfermode* xfer = fXfermode;
if (xfer) {
do {
shader->shadeSpan(x, y, span, width);
xfer->xfer32(device, span, width, NULL);
y += 1;
device = (uint32_t*)((char*)device + deviceRB);
} while (--height > 0);
} else {
SkBlitRow::Proc32 proc = fProc32;
do {
shader->shadeSpan(x, y, span, width);
proc(device, span, width, 255);
y += 1;
device = (uint32_t*)((char*)device + deviceRB);
} while (--height > 0);
}
}
}
void SkARGB32_Shader_Blitter::blitAntiH(int x, int y, const SkAlpha antialias[],
const int16_t runs[]) {
SkPMColor* span = fBuffer;
uint32_t* device = fDevice.getAddr32(x, y);
SkShader* shader = fShader;
if (fXfermode && !fShadeDirectlyIntoDevice) {
for (;;) {
SkXfermode* xfer = fXfermode;
int count = *runs;
if (count <= 0)
break;
int aa = *antialias;
if (aa) {
shader->shadeSpan(x, y, span, count);
if (aa == 255) {
xfer->xfer32(device, span, count, NULL);
} else {
// count is almost always 1
for (int i = count - 1; i >= 0; --i) {
xfer->xfer32(&device[i], &span[i], 1, antialias);
}
}
}
device += count;
runs += count;
antialias += count;
x += count;
}
} else if (fShadeDirectlyIntoDevice ||
(fShader->getFlags() & SkShader::kOpaqueAlpha_Flag)) {
for (;;) {
int count = *runs;
if (count <= 0) {
break;
}
int aa = *antialias;
if (aa) {
if (aa == 255) {
// cool, have the shader draw right into the device
shader->shadeSpan(x, y, device, count);
} else {
shader->shadeSpan(x, y, span, count);
fProc32Blend(device, span, count, aa);
}
}
device += count;
runs += count;
antialias += count;
x += count;
}
} else {
for (;;) {
int count = *runs;
if (count <= 0) {
break;
}
int aa = *antialias;
if (aa) {
fShader->shadeSpan(x, y, span, count);
if (aa == 255) {
fProc32(device, span, count, 255);
} else {
fProc32Blend(device, span, count, aa);
}
}
device += count;
runs += count;
antialias += count;
x += count;
}
}
}
void SkARGB32_Shader_Blitter::blitMask(const SkMask& mask, const SkIRect& clip) {
// we only handle kA8 with an xfermode
if (fXfermode && (SkMask::kA8_Format != mask.fFormat)) {
this->INHERITED::blitMask(mask, clip);
return;
}
SkASSERT(mask.fBounds.contains(clip));
SkBlitMask::RowProc proc = NULL;
if (!fXfermode) {
unsigned flags = 0;
if (fShader->getFlags() & SkShader::kOpaqueAlpha_Flag) {
flags |= SkBlitMask::kSrcIsOpaque_RowFlag;
}
proc = SkBlitMask::RowFactory(SkBitmap::kARGB_8888_Config, mask.fFormat,
(SkBlitMask::RowFlags)flags);
if (NULL == proc) {
this->INHERITED::blitMask(mask, clip);
return;
}
}
const int x = clip.fLeft;
const int width = clip.width();
int y = clip.fTop;
int height = clip.height();
char* dstRow = (char*)fDevice.getAddr32(x, y);
const size_t dstRB = fDevice.rowBytes();
const uint8_t* maskRow = (const uint8_t*)mask.getAddr(x, y);
const size_t maskRB = mask.fRowBytes;
SkShader* shader = fShader;
SkPMColor* span = fBuffer;
if (fXfermode) {
SkASSERT(SkMask::kA8_Format == mask.fFormat);
SkXfermode* xfer = fXfermode;
do {
shader->shadeSpan(x, y, span, width);
xfer->xfer32((SkPMColor*)dstRow, span, width, maskRow);
dstRow += dstRB;
maskRow += maskRB;
y += 1;
} while (--height > 0);
} else {
do {
shader->shadeSpan(x, y, span, width);
proc(dstRow, maskRow, span, width);
dstRow += dstRB;
maskRow += maskRB;
y += 1;
} while (--height > 0);
}
}
void SkARGB32_Shader_Blitter::blitV(int x, int y, int height, SkAlpha alpha) {
SkASSERT(x >= 0 && y >= 0 && y + height <= fDevice.height());
uint32_t* device = fDevice.getAddr32(x, y);
size_t deviceRB = fDevice.rowBytes();
SkShader* shader = fShader;
if (fConstInY) {
SkPMColor c;
fShader->shadeSpan(x, y, &c, 1);
if (fShadeDirectlyIntoDevice) {
if (255 == alpha) {
do {
*device = c;
device = (uint32_t*)((char*)device + deviceRB);
} while (--height > 0);
} else {
do {
*device = SkFourByteInterp(c, *device, alpha);
device = (uint32_t*)((char*)device + deviceRB);
} while (--height > 0);
}
} else {
SkXfermode* xfer = fXfermode;
if (xfer) {
do {
xfer->xfer32(device, &c, 1, &alpha);
device = (uint32_t*)((char*)device + deviceRB);
} while (--height > 0);
} else {
SkBlitRow::Proc32 proc = (255 == alpha) ? fProc32 : fProc32Blend;
do {
proc(device, &c, 1, alpha);
device = (uint32_t*)((char*)device + deviceRB);
} while (--height > 0);
}
}
return;
}
if (fShadeDirectlyIntoDevice) {
void* ctx;
SkShader::ShadeProc shadeProc = fShader->asAShadeProc(&ctx);
if (255 == alpha) {
if (shadeProc) {
do {
shadeProc(ctx, x, y, device, 1);
y += 1;
device = (uint32_t*)((char*)device + deviceRB);
} while (--height > 0);
} else {
do {
shader->shadeSpan(x, y, device, 1);
y += 1;
device = (uint32_t*)((char*)device + deviceRB);
} while (--height > 0);
}
} else { // alpha < 255
SkPMColor c;
if (shadeProc) {
do {
shadeProc(ctx, x, y, &c, 1);
*device = SkFourByteInterp(c, *device, alpha);
y += 1;
device = (uint32_t*)((char*)device + deviceRB);
} while (--height > 0);
} else {
do {
shader->shadeSpan(x, y, &c, 1);
*device = SkFourByteInterp(c, *device, alpha);
y += 1;
device = (uint32_t*)((char*)device + deviceRB);
} while (--height > 0);
}
}
} else {
SkPMColor* span = fBuffer;
SkXfermode* xfer = fXfermode;
if (xfer) {
do {
shader->shadeSpan(x, y, span, 1);
xfer->xfer32(device, span, 1, &alpha);
y += 1;
device = (uint32_t*)((char*)device + deviceRB);
} while (--height > 0);
} else {
SkBlitRow::Proc32 proc = (255 == alpha) ? fProc32 : fProc32Blend;
do {
shader->shadeSpan(x, y, span, 1);
proc(device, span, 1, alpha);
y += 1;
device = (uint32_t*)((char*)device + deviceRB);
} while (--height > 0);
}
}
}
|