blob: 8e93ef1303eda6d339bc4944f875593008e917b8 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
2/*
3 * Copyright 2006 The Android Open Source Project
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
reed@android.com8a1c16f2008-12-17 15:59:43 +00009
10#include "SkBlitter.h"
11#include "SkAntiRun.h"
12#include "SkColor.h"
13#include "SkColorFilter.h"
14#include "SkMask.h"
15#include "SkMaskFilter.h"
16#include "SkTemplatesPriv.h"
17#include "SkUtils.h"
18#include "SkXfermode.h"
19
reed@android.com845fdac2009-06-23 03:01:32 +000020SkBlitter::~SkBlitter() {}
reed@android.com8a1c16f2008-12-17 15:59:43 +000021
reed@google.com82065d62011-02-07 15:30:46 +000022const SkBitmap* SkBlitter::justAnOpaqueColor(uint32_t* value) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000023 return NULL;
24}
25
reed@google.com82065d62011-02-07 15:30:46 +000026void SkBlitter::blitH(int x, int y, int width) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000027 SkASSERT(!"unimplemented");
28}
29
reed@google.com82065d62011-02-07 15:30:46 +000030void SkBlitter::blitAntiH(int x, int y, const SkAlpha antialias[],
31 const int16_t runs[]) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000032 SkASSERT(!"unimplemented");
33}
34
reed@google.com82065d62011-02-07 15:30:46 +000035void SkBlitter::blitV(int x, int y, int height, SkAlpha alpha) {
36 if (alpha == 255) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000037 this->blitRect(x, y, 1, height);
reed@google.com82065d62011-02-07 15:30:46 +000038 } else {
reed@android.com8a1c16f2008-12-17 15:59:43 +000039 int16_t runs[2];
40 runs[0] = 1;
41 runs[1] = 0;
42
reed@google.com82065d62011-02-07 15:30:46 +000043 while (--height >= 0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000044 this->blitAntiH(x, y++, &alpha, runs);
reed@google.com82065d62011-02-07 15:30:46 +000045 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000046 }
47}
48
reed@google.com82065d62011-02-07 15:30:46 +000049void SkBlitter::blitRect(int x, int y, int width, int height) {
50 while (--height >= 0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000051 this->blitH(x, y++, width);
reed@google.com82065d62011-02-07 15:30:46 +000052 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000053}
54
tomhudson@google.com49eac192011-12-27 13:59:20 +000055/// Default implementation doesn't check for any easy optimizations
56/// such as alpha == 0 or 255; also uses blitV(), which some subclasses
57/// may not support.
58void SkBlitter::blitAntiRect(int x, int y, int width, int height,
59 SkAlpha leftAlpha, SkAlpha rightAlpha) {
60 this->blitV(x, y, height, leftAlpha);
61 this->blitRect(x + 1, y, width, height);
62 this->blitV(x + width + 1, y, height, rightAlpha);
63}
64
reed@android.com8a1c16f2008-12-17 15:59:43 +000065//////////////////////////////////////////////////////////////////////////////
66
reed@google.com82065d62011-02-07 15:30:46 +000067static inline void bits_to_runs(SkBlitter* blitter, int x, int y,
68 const uint8_t bits[],
69 U8CPU left_mask, int rowBytes,
70 U8CPU right_mask) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000071 int inFill = 0;
72 int pos = 0;
73
reed@google.com82065d62011-02-07 15:30:46 +000074 while (--rowBytes >= 0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000075 unsigned b = *bits++ & left_mask;
reed@google.com82065d62011-02-07 15:30:46 +000076 if (rowBytes == 0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000077 b &= right_mask;
reed@google.com82065d62011-02-07 15:30:46 +000078 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000079
reed@google.com82065d62011-02-07 15:30:46 +000080 for (unsigned test = 0x80; test != 0; test >>= 1) {
81 if (b & test) {
82 if (!inFill) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000083 pos = x;
84 inFill = true;
85 }
reed@google.com82065d62011-02-07 15:30:46 +000086 } else {
87 if (inFill) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000088 blitter->blitH(pos, y, x - pos);
89 inFill = false;
90 }
91 }
92 x += 1;
93 }
94 left_mask = 0xFF;
95 }
96
97 // final cleanup
reed@google.com82065d62011-02-07 15:30:46 +000098 if (inFill) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000099 blitter->blitH(pos, y, x - pos);
reed@google.com82065d62011-02-07 15:30:46 +0000100 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000101}
102
reed@google.com82065d62011-02-07 15:30:46 +0000103void SkBlitter::blitMask(const SkMask& mask, const SkIRect& clip) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000104 SkASSERT(mask.fBounds.contains(clip));
105
reed@google.com82065d62011-02-07 15:30:46 +0000106 if (mask.fFormat == SkMask::kBW_Format) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000107 int cx = clip.fLeft;
108 int cy = clip.fTop;
109 int maskLeft = mask.fBounds.fLeft;
110 int mask_rowBytes = mask.fRowBytes;
111 int height = clip.height();
112
113 const uint8_t* bits = mask.getAddr1(cx, cy);
114
reed@google.com82065d62011-02-07 15:30:46 +0000115 if (cx == maskLeft && clip.fRight == mask.fBounds.fRight) {
116 while (--height >= 0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000117 bits_to_runs(this, cx, cy, bits, 0xFF, mask_rowBytes, 0xFF);
118 bits += mask_rowBytes;
119 cy += 1;
120 }
reed@google.com82065d62011-02-07 15:30:46 +0000121 } else {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000122 int left_edge = cx - maskLeft;
123 SkASSERT(left_edge >= 0);
124 int rite_edge = clip.fRight - maskLeft;
125 SkASSERT(rite_edge > left_edge);
126
127 int left_mask = 0xFF >> (left_edge & 7);
128 int rite_mask = 0xFF << (8 - (rite_edge & 7));
129 int full_runs = (rite_edge >> 3) - ((left_edge + 7) >> 3);
130
reed@google.coma89c77b2011-12-01 21:47:26 +0000131 // check for empty right mask, so we don't read off the end (or go slower than we need to)
reed@google.com82065d62011-02-07 15:30:46 +0000132 if (rite_mask == 0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000133 SkASSERT(full_runs >= 0);
134 full_runs -= 1;
135 rite_mask = 0xFF;
136 }
reed@google.com82065d62011-02-07 15:30:46 +0000137 if (left_mask == 0xFF) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000138 full_runs -= 1;
reed@google.com82065d62011-02-07 15:30:46 +0000139 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000140
reed@google.coma89c77b2011-12-01 21:47:26 +0000141 // back up manually so we can keep in sync with our byte-aligned src
142 // have cx reflect our actual starting x-coord
reed@android.com8a1c16f2008-12-17 15:59:43 +0000143 cx -= left_edge & 7;
144
reed@google.com82065d62011-02-07 15:30:46 +0000145 if (full_runs < 0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000146 SkASSERT((left_mask & rite_mask) != 0);
reed@google.com82065d62011-02-07 15:30:46 +0000147 while (--height >= 0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000148 bits_to_runs(this, cx, cy, bits, left_mask, 1, rite_mask);
149 bits += mask_rowBytes;
150 cy += 1;
151 }
reed@google.com82065d62011-02-07 15:30:46 +0000152 } else {
153 while (--height >= 0) {
reed@google.coma89c77b2011-12-01 21:47:26 +0000154 bits_to_runs(this, cx, cy, bits, left_mask, full_runs + 2, rite_mask);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000155 bits += mask_rowBytes;
156 cy += 1;
157 }
158 }
159 }
reed@google.com82065d62011-02-07 15:30:46 +0000160 } else {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000161 int width = clip.width();
162 SkAutoSTMalloc<64, int16_t> runStorage(width + 1);
163 int16_t* runs = runStorage.get();
reed@google.com79891862011-10-18 15:44:57 +0000164 const uint8_t* aa = mask.getAddr8(clip.fLeft, clip.fTop);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000165
166 sk_memset16((uint16_t*)runs, 1, width);
167 runs[width] = 0;
168
169 int height = clip.height();
170 int y = clip.fTop;
reed@google.com82065d62011-02-07 15:30:46 +0000171 while (--height >= 0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000172 this->blitAntiH(clip.fLeft, y, aa, runs);
173 aa += mask.fRowBytes;
174 y += 1;
175 }
176 }
177}
178
179/////////////////////// these guys are not virtual, just a helpers
180
181void SkBlitter::blitMaskRegion(const SkMask& mask, const SkRegion& clip) {
182 if (clip.quickReject(mask.fBounds)) {
183 return;
184 }
reed@google.com82065d62011-02-07 15:30:46 +0000185
reed@android.com8a1c16f2008-12-17 15:59:43 +0000186 SkRegion::Cliperator clipper(clip, mask.fBounds);
reed@google.com82065d62011-02-07 15:30:46 +0000187
reed@android.com8a1c16f2008-12-17 15:59:43 +0000188 while (!clipper.done()) {
189 const SkIRect& cr = clipper.rect();
190 this->blitMask(mask, cr);
191 clipper.next();
192 }
193}
194
195void SkBlitter::blitRectRegion(const SkIRect& rect, const SkRegion& clip) {
196 SkRegion::Cliperator clipper(clip, rect);
reed@google.com82065d62011-02-07 15:30:46 +0000197
reed@android.com8a1c16f2008-12-17 15:59:43 +0000198 while (!clipper.done()) {
199 const SkIRect& cr = clipper.rect();
200 this->blitRect(cr.fLeft, cr.fTop, cr.width(), cr.height());
201 clipper.next();
202 }
203}
204
205void SkBlitter::blitRegion(const SkRegion& clip) {
206 SkRegion::Iterator iter(clip);
reed@google.com82065d62011-02-07 15:30:46 +0000207
reed@android.com8a1c16f2008-12-17 15:59:43 +0000208 while (!iter.done()) {
209 const SkIRect& cr = iter.rect();
210 this->blitRect(cr.fLeft, cr.fTop, cr.width(), cr.height());
211 iter.next();
212 }
213}
214
reed@google.com82065d62011-02-07 15:30:46 +0000215///////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +0000216
reed@google.com82065d62011-02-07 15:30:46 +0000217void SkNullBlitter::blitH(int x, int y, int width) {}
reed@android.com8a1c16f2008-12-17 15:59:43 +0000218
reed@google.com82065d62011-02-07 15:30:46 +0000219void SkNullBlitter::blitAntiH(int x, int y, const SkAlpha antialias[],
220 const int16_t runs[]) {}
reed@android.com8a1c16f2008-12-17 15:59:43 +0000221
reed@google.com82065d62011-02-07 15:30:46 +0000222void SkNullBlitter::blitV(int x, int y, int height, SkAlpha alpha) {}
reed@android.com8a1c16f2008-12-17 15:59:43 +0000223
reed@google.com82065d62011-02-07 15:30:46 +0000224void SkNullBlitter::blitRect(int x, int y, int width, int height) {}
reed@android.com8a1c16f2008-12-17 15:59:43 +0000225
reed@google.com82065d62011-02-07 15:30:46 +0000226void SkNullBlitter::blitMask(const SkMask& mask, const SkIRect& clip) {}
reed@android.com8a1c16f2008-12-17 15:59:43 +0000227
reed@google.com82065d62011-02-07 15:30:46 +0000228const SkBitmap* SkNullBlitter::justAnOpaqueColor(uint32_t* value) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000229 return NULL;
230}
231
reed@google.com82065d62011-02-07 15:30:46 +0000232///////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +0000233
reed@google.com82065d62011-02-07 15:30:46 +0000234static int compute_anti_width(const int16_t runs[]) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000235 int width = 0;
reed@google.com82065d62011-02-07 15:30:46 +0000236
237 for (;;) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000238 int count = runs[0];
reed@google.com82065d62011-02-07 15:30:46 +0000239
reed@android.com8a1c16f2008-12-17 15:59:43 +0000240 SkASSERT(count >= 0);
reed@google.com82065d62011-02-07 15:30:46 +0000241 if (count == 0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000242 break;
reed@google.com82065d62011-02-07 15:30:46 +0000243 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000244 width += count;
245 runs += count;
reed@google.com82065d62011-02-07 15:30:46 +0000246
reed@android.com8a1c16f2008-12-17 15:59:43 +0000247 SkASSERT(width < 20000);
248 }
249 return width;
250}
251
reed@google.com82065d62011-02-07 15:30:46 +0000252static inline bool y_in_rect(int y, const SkIRect& rect) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000253 return (unsigned)(y - rect.fTop) < (unsigned)rect.height();
254}
255
reed@google.com82065d62011-02-07 15:30:46 +0000256static inline bool x_in_rect(int x, const SkIRect& rect) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000257 return (unsigned)(x - rect.fLeft) < (unsigned)rect.width();
258}
259
reed@google.com82065d62011-02-07 15:30:46 +0000260void SkRectClipBlitter::blitH(int left, int y, int width) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000261 SkASSERT(width > 0);
262
reed@google.com82065d62011-02-07 15:30:46 +0000263 if (!y_in_rect(y, fClipRect)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000264 return;
reed@google.com82065d62011-02-07 15:30:46 +0000265 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000266
267 int right = left + width;
268
reed@google.com82065d62011-02-07 15:30:46 +0000269 if (left < fClipRect.fLeft) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000270 left = fClipRect.fLeft;
reed@google.com82065d62011-02-07 15:30:46 +0000271 }
272 if (right > fClipRect.fRight) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000273 right = fClipRect.fRight;
reed@google.com82065d62011-02-07 15:30:46 +0000274 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000275
276 width = right - left;
reed@google.com82065d62011-02-07 15:30:46 +0000277 if (width > 0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000278 fBlitter->blitH(left, y, width);
reed@google.com82065d62011-02-07 15:30:46 +0000279 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000280}
281
reed@google.com82065d62011-02-07 15:30:46 +0000282void SkRectClipBlitter::blitAntiH(int left, int y, const SkAlpha aa[],
283 const int16_t runs[]) {
284 if (!y_in_rect(y, fClipRect) || left >= fClipRect.fRight) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000285 return;
reed@google.com82065d62011-02-07 15:30:46 +0000286 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000287
288 int x0 = left;
289 int x1 = left + compute_anti_width(runs);
290
reed@google.com82065d62011-02-07 15:30:46 +0000291 if (x1 <= fClipRect.fLeft) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000292 return;
reed@google.com82065d62011-02-07 15:30:46 +0000293 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000294
295 SkASSERT(x0 < x1);
reed@google.com82065d62011-02-07 15:30:46 +0000296 if (x0 < fClipRect.fLeft) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000297 int dx = fClipRect.fLeft - x0;
298 SkAlphaRuns::BreakAt((int16_t*)runs, (uint8_t*)aa, dx);
299 runs += dx;
300 aa += dx;
301 x0 = fClipRect.fLeft;
302 }
303
304 SkASSERT(x0 < x1 && runs[x1 - x0] == 0);
reed@google.com82065d62011-02-07 15:30:46 +0000305 if (x1 > fClipRect.fRight) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000306 x1 = fClipRect.fRight;
307 SkAlphaRuns::BreakAt((int16_t*)runs, (uint8_t*)aa, x1 - x0);
308 ((int16_t*)runs)[x1 - x0] = 0;
309 }
310
311 SkASSERT(x0 < x1 && runs[x1 - x0] == 0);
312 SkASSERT(compute_anti_width(runs) == x1 - x0);
313
314 fBlitter->blitAntiH(x0, y, aa, runs);
315}
316
reed@google.com82065d62011-02-07 15:30:46 +0000317void SkRectClipBlitter::blitV(int x, int y, int height, SkAlpha alpha) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000318 SkASSERT(height > 0);
319
reed@google.com82065d62011-02-07 15:30:46 +0000320 if (!x_in_rect(x, fClipRect)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000321 return;
reed@google.com82065d62011-02-07 15:30:46 +0000322 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000323
324 int y0 = y;
325 int y1 = y + height;
326
reed@google.com82065d62011-02-07 15:30:46 +0000327 if (y0 < fClipRect.fTop) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000328 y0 = fClipRect.fTop;
reed@google.com82065d62011-02-07 15:30:46 +0000329 }
330 if (y1 > fClipRect.fBottom) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000331 y1 = fClipRect.fBottom;
reed@google.com82065d62011-02-07 15:30:46 +0000332 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000333
reed@google.com82065d62011-02-07 15:30:46 +0000334 if (y0 < y1) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000335 fBlitter->blitV(x, y0, y1 - y0, alpha);
reed@google.com82065d62011-02-07 15:30:46 +0000336 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000337}
338
reed@google.com82065d62011-02-07 15:30:46 +0000339void SkRectClipBlitter::blitRect(int left, int y, int width, int height) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000340 SkIRect r;
341
342 r.set(left, y, left + width, y + height);
reed@google.com82065d62011-02-07 15:30:46 +0000343 if (r.intersect(fClipRect)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000344 fBlitter->blitRect(r.fLeft, r.fTop, r.width(), r.height());
reed@google.com82065d62011-02-07 15:30:46 +0000345 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000346}
347
tomhudson@google.com49eac192011-12-27 13:59:20 +0000348void SkRectClipBlitter::blitAntiRect(int left, int y, int width, int height,
349 SkAlpha leftAlpha, SkAlpha rightAlpha) {
350 SkIRect r;
351
352 // The *true* width of the rectangle blitted is width+2:
353 r.set(left, y, left + width + 2, y + height);
354 if (r.intersect(fClipRect)) {
355 if (r.fLeft != left) {
356 SkASSERT(r.fLeft > left);
357 leftAlpha = 255;
358 }
359 if (r.fRight != left + width + 2) {
360 SkASSERT(r.fRight < left + width + 2);
361 rightAlpha = 255;
362 }
363 if (255 == leftAlpha && 255 == rightAlpha) {
364 fBlitter->blitRect(r.fLeft, r.fTop, r.width(), r.height());
365 } else if (1 == r.width()) {
366 if (r.fLeft == left) {
367 fBlitter->blitV(r.fLeft, r.fTop, r.height(), leftAlpha);
368 } else {
369 SkASSERT(r.fLeft == left + width + 1);
370 fBlitter->blitV(r.fLeft, r.fTop, r.height(), rightAlpha);
371 }
372 } else {
373 fBlitter->blitAntiRect(r.fLeft, r.fTop, r.width() - 2, r.height(),
374 leftAlpha, rightAlpha);
375 }
376 }
377}
378
reed@google.com82065d62011-02-07 15:30:46 +0000379void SkRectClipBlitter::blitMask(const SkMask& mask, const SkIRect& clip) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000380 SkASSERT(mask.fBounds.contains(clip));
381
382 SkIRect r = clip;
383
reed@google.com82065d62011-02-07 15:30:46 +0000384 if (r.intersect(fClipRect)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000385 fBlitter->blitMask(mask, r);
reed@google.com82065d62011-02-07 15:30:46 +0000386 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000387}
388
reed@google.com82065d62011-02-07 15:30:46 +0000389const SkBitmap* SkRectClipBlitter::justAnOpaqueColor(uint32_t* value) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000390 return fBlitter->justAnOpaqueColor(value);
391}
392
reed@google.com82065d62011-02-07 15:30:46 +0000393///////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +0000394
reed@google.com82065d62011-02-07 15:30:46 +0000395void SkRgnClipBlitter::blitH(int x, int y, int width) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000396 SkRegion::Spanerator span(*fRgn, y, x, x + width);
397 int left, right;
398
reed@google.com82065d62011-02-07 15:30:46 +0000399 while (span.next(&left, &right)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000400 SkASSERT(left < right);
401 fBlitter->blitH(left, y, right - left);
402 }
403}
404
reed@google.com82065d62011-02-07 15:30:46 +0000405void SkRgnClipBlitter::blitAntiH(int x, int y, const SkAlpha aa[],
406 const int16_t runs[]) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000407 int width = compute_anti_width(runs);
408 SkRegion::Spanerator span(*fRgn, y, x, x + width);
409 int left, right;
410 SkDEBUGCODE(const SkIRect& bounds = fRgn->getBounds();)
reed@google.com82065d62011-02-07 15:30:46 +0000411
reed@android.com8a1c16f2008-12-17 15:59:43 +0000412 int prevRite = x;
reed@google.com82065d62011-02-07 15:30:46 +0000413 while (span.next(&left, &right)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000414 SkASSERT(x <= left);
415 SkASSERT(left < right);
416 SkASSERT(left >= bounds.fLeft && right <= bounds.fRight);
reed@google.com82065d62011-02-07 15:30:46 +0000417
reed@android.com8a1c16f2008-12-17 15:59:43 +0000418 SkAlphaRuns::Break((int16_t*)runs, (uint8_t*)aa, left - x, right - left);
419
420 // now zero before left
reed@google.com82065d62011-02-07 15:30:46 +0000421 if (left > prevRite) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000422 int index = prevRite - x;
423 ((uint8_t*)aa)[index] = 0; // skip runs after right
424 ((int16_t*)runs)[index] = SkToS16(left - prevRite);
425 }
reed@google.com82065d62011-02-07 15:30:46 +0000426
reed@android.com8a1c16f2008-12-17 15:59:43 +0000427 prevRite = right;
428 }
reed@google.com82065d62011-02-07 15:30:46 +0000429
430 if (prevRite > x) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000431 ((int16_t*)runs)[prevRite - x] = 0;
reed@google.com82065d62011-02-07 15:30:46 +0000432
reed@android.com8a1c16f2008-12-17 15:59:43 +0000433 if (x < 0) {
434 int skip = runs[0];
435 SkASSERT(skip >= -x);
436 aa += skip;
437 runs += skip;
438 x += skip;
439 }
440 fBlitter->blitAntiH(x, y, aa, runs);
441 }
442}
443
reed@google.com82065d62011-02-07 15:30:46 +0000444void SkRgnClipBlitter::blitV(int x, int y, int height, SkAlpha alpha) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000445 SkIRect bounds;
446 bounds.set(x, y, x + 1, y + height);
447
448 SkRegion::Cliperator iter(*fRgn, bounds);
449
reed@google.com82065d62011-02-07 15:30:46 +0000450 while (!iter.done()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000451 const SkIRect& r = iter.rect();
452 SkASSERT(bounds.contains(r));
453
454 fBlitter->blitV(x, r.fTop, r.height(), alpha);
455 iter.next();
456 }
457}
458
reed@google.com82065d62011-02-07 15:30:46 +0000459void SkRgnClipBlitter::blitRect(int x, int y, int width, int height) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000460 SkIRect bounds;
461 bounds.set(x, y, x + width, y + height);
462
463 SkRegion::Cliperator iter(*fRgn, bounds);
464
reed@google.com82065d62011-02-07 15:30:46 +0000465 while (!iter.done()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000466 const SkIRect& r = iter.rect();
467 SkASSERT(bounds.contains(r));
468
469 fBlitter->blitRect(r.fLeft, r.fTop, r.width(), r.height());
470 iter.next();
471 }
472}
473
tomhudson@google.com49eac192011-12-27 13:59:20 +0000474void SkRgnClipBlitter::blitAntiRect(int x, int y, int width, int height,
475 SkAlpha leftAlpha, SkAlpha rightAlpha) {
476 // The *true* width of the rectangle to blit is width + 2
477 SkIRect bounds;
478 bounds.set(x, y, x + width + 2, y + height);
479
480 SkRegion::Cliperator iter(*fRgn, bounds);
481
482 while (!iter.done()) {
483 const SkIRect& r = iter.rect();
484 SkASSERT(bounds.contains(r));
485 SkASSERT(r.fLeft >= x);
486 SkASSERT(r.fRight < x + width + 2);
487
488 SkAlpha effectiveLeftAlpha = (r.fLeft == x) ? leftAlpha : 255;
489 SkAlpha effectiveRightAlpha = (r.fRight == x + width + 2) ?
490 rightAlpha : 255;
491
492 if (255 == effectiveLeftAlpha && 255 == effectiveRightAlpha) {
493 fBlitter->blitRect(r.fLeft, r.fTop, r.width(), r.height());
494 } else if (1 == r.width()) {
495 if (r.fLeft == x) {
496 fBlitter->blitV(r.fLeft, r.fTop, r.height(),
497 effectiveLeftAlpha);
498 } else {
499 SkASSERT(r.fLeft == x + width + 1);
500 fBlitter->blitV(r.fLeft, r.fTop, r.height(),
501 effectiveRightAlpha);
502 }
503 } else {
504 fBlitter->blitAntiRect(r.fLeft, r.fTop, r.width() - 2, r.height(),
505 effectiveLeftAlpha, effectiveRightAlpha);
506 }
507 iter.next();
508 }
509}
510
511
reed@google.com82065d62011-02-07 15:30:46 +0000512void SkRgnClipBlitter::blitMask(const SkMask& mask, const SkIRect& clip) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000513 SkASSERT(mask.fBounds.contains(clip));
514
515 SkRegion::Cliperator iter(*fRgn, clip);
516 const SkIRect& r = iter.rect();
517 SkBlitter* blitter = fBlitter;
518
reed@google.com82065d62011-02-07 15:30:46 +0000519 while (!iter.done()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000520 blitter->blitMask(mask, r);
521 iter.next();
522 }
523}
524
reed@google.com82065d62011-02-07 15:30:46 +0000525const SkBitmap* SkRgnClipBlitter::justAnOpaqueColor(uint32_t* value) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000526 return fBlitter->justAnOpaqueColor(value);
527}
528
reed@google.com82065d62011-02-07 15:30:46 +0000529///////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +0000530
reed@google.com82065d62011-02-07 15:30:46 +0000531SkBlitter* SkBlitterClipper::apply(SkBlitter* blitter, const SkRegion* clip,
532 const SkIRect* ir) {
533 if (clip) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000534 const SkIRect& clipR = clip->getBounds();
535
reed@google.com82065d62011-02-07 15:30:46 +0000536 if (clip->isEmpty() || (ir && !SkIRect::Intersects(clipR, *ir))) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000537 blitter = &fNullBlitter;
reed@google.com82065d62011-02-07 15:30:46 +0000538 } else if (clip->isRect()) {
539 if (ir == NULL || !clipR.contains(*ir)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000540 fRectBlitter.init(blitter, clipR);
541 blitter = &fRectBlitter;
542 }
reed@google.com82065d62011-02-07 15:30:46 +0000543 } else {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000544 fRgnBlitter.init(blitter, clip);
545 blitter = &fRgnBlitter;
546 }
547 }
548 return blitter;
549}
550
reed@google.com82065d62011-02-07 15:30:46 +0000551///////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +0000552
553#include "SkColorShader.h"
554#include "SkColorPriv.h"
555
556class Sk3DShader : public SkShader {
557public:
reed@google.com82065d62011-02-07 15:30:46 +0000558 Sk3DShader(SkShader* proxy) : fProxy(proxy) {
559 SkSafeRef(proxy);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000560 fMask = NULL;
561 }
reed@google.com82065d62011-02-07 15:30:46 +0000562
563 virtual ~Sk3DShader() {
564 SkSafeUnref(fProxy);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000565 }
reed@google.com82065d62011-02-07 15:30:46 +0000566
reed@android.com8a1c16f2008-12-17 15:59:43 +0000567 void setMask(const SkMask* mask) { fMask = mask; }
568
reed@google.com82065d62011-02-07 15:30:46 +0000569 virtual bool setContext(const SkBitmap& device, const SkPaint& paint,
570 const SkMatrix& matrix) {
571 if (fProxy) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000572 return fProxy->setContext(device, paint, matrix);
reed@google.com82065d62011-02-07 15:30:46 +0000573 } else {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000574 fPMColor = SkPreMultiplyColor(paint.getColor());
575 return this->INHERITED::setContext(device, paint, matrix);
576 }
577 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000578
reed@google.com82065d62011-02-07 15:30:46 +0000579 virtual void shadeSpan(int x, int y, SkPMColor span[], int count) {
580 if (fProxy) {
581 fProxy->shadeSpan(x, y, span, count);
582 }
583
584 if (fMask == NULL) {
585 if (fProxy == NULL) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000586 sk_memset32(span, fPMColor, count);
reed@google.com82065d62011-02-07 15:30:46 +0000587 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000588 return;
589 }
590
591 SkASSERT(fMask->fBounds.contains(x, y));
592 SkASSERT(fMask->fBounds.contains(x + count - 1, y));
593
594 size_t size = fMask->computeImageSize();
reed@google.com79891862011-10-18 15:44:57 +0000595 const uint8_t* alpha = fMask->getAddr8(x, y);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000596 const uint8_t* mulp = alpha + size;
597 const uint8_t* addp = mulp + size;
598
reed@google.com82065d62011-02-07 15:30:46 +0000599 if (fProxy) {
600 for (int i = 0; i < count; i++) {
601 if (alpha[i]) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000602 SkPMColor c = span[i];
reed@google.com82065d62011-02-07 15:30:46 +0000603 if (c) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000604 unsigned a = SkGetPackedA32(c);
605 unsigned r = SkGetPackedR32(c);
606 unsigned g = SkGetPackedG32(c);
607 unsigned b = SkGetPackedB32(c);
608
609 unsigned mul = SkAlpha255To256(mulp[i]);
610 unsigned add = addp[i];
611
612 r = SkFastMin32(SkAlphaMul(r, mul) + add, a);
613 g = SkFastMin32(SkAlphaMul(g, mul) + add, a);
614 b = SkFastMin32(SkAlphaMul(b, mul) + add, a);
615
616 span[i] = SkPackARGB32(a, r, g, b);
617 }
reed@google.com82065d62011-02-07 15:30:46 +0000618 } else {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000619 span[i] = 0;
reed@google.com82065d62011-02-07 15:30:46 +0000620 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000621 }
reed@google.com82065d62011-02-07 15:30:46 +0000622 } else { // color
reed@android.com8a1c16f2008-12-17 15:59:43 +0000623 unsigned a = SkGetPackedA32(fPMColor);
624 unsigned r = SkGetPackedR32(fPMColor);
625 unsigned g = SkGetPackedG32(fPMColor);
626 unsigned b = SkGetPackedB32(fPMColor);
reed@google.com82065d62011-02-07 15:30:46 +0000627 for (int i = 0; i < count; i++) {
628 if (alpha[i]) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000629 unsigned mul = SkAlpha255To256(mulp[i]);
630 unsigned add = addp[i];
631
632 span[i] = SkPackARGB32( a,
reed@google.com82065d62011-02-07 15:30:46 +0000633 SkFastMin32(SkAlphaMul(r, mul) + add, a),
634 SkFastMin32(SkAlphaMul(g, mul) + add, a),
635 SkFastMin32(SkAlphaMul(b, mul) + add, a));
636 } else {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000637 span[i] = 0;
reed@google.com82065d62011-02-07 15:30:46 +0000638 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000639 }
640 }
641 }
reed@google.com82065d62011-02-07 15:30:46 +0000642
643 virtual void beginSession() {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000644 this->INHERITED::beginSession();
reed@google.com82065d62011-02-07 15:30:46 +0000645 if (fProxy) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000646 fProxy->beginSession();
reed@google.com82065d62011-02-07 15:30:46 +0000647 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000648 }
reed@google.com82065d62011-02-07 15:30:46 +0000649
650 virtual void endSession() {
651 if (fProxy) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000652 fProxy->endSession();
reed@google.com82065d62011-02-07 15:30:46 +0000653 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000654 this->INHERITED::endSession();
655 }
656
657protected:
658 Sk3DShader(SkFlattenableReadBuffer& buffer) :
reed@google.com82065d62011-02-07 15:30:46 +0000659 INHERITED(buffer) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000660 fProxy = static_cast<SkShader*>(buffer.readFlattenable());
661 fPMColor = buffer.readU32();
662 fMask = NULL;
663 }
reed@google.com82065d62011-02-07 15:30:46 +0000664
665 virtual void flatten(SkFlattenableWriteBuffer& buffer) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000666 this->INHERITED::flatten(buffer);
667 buffer.writeFlattenable(fProxy);
668 buffer.write32(fPMColor);
669 }
reed@google.com82065d62011-02-07 15:30:46 +0000670
671 virtual Factory getFactory() {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000672 return CreateProc;
673 }
674
675private:
reed@google.com82065d62011-02-07 15:30:46 +0000676 static SkFlattenable* CreateProc(SkFlattenableReadBuffer& buffer) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000677 return SkNEW_ARGS(Sk3DShader, (buffer));
678 }
679
680 SkShader* fProxy;
681 SkPMColor fPMColor;
682 const SkMask* fMask;
683
684 typedef SkShader INHERITED;
685};
686
687class Sk3DBlitter : public SkBlitter {
688public:
689 Sk3DBlitter(SkBlitter* proxy, Sk3DShader* shader, void (*killProc)(void*))
reed@google.com82065d62011-02-07 15:30:46 +0000690 : fProxy(proxy), f3DShader(shader), fKillProc(killProc) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000691 shader->ref();
692 }
reed@google.com82065d62011-02-07 15:30:46 +0000693
694 virtual ~Sk3DBlitter() {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000695 f3DShader->unref();
696 fKillProc(fProxy);
697 }
698
reed@google.com82065d62011-02-07 15:30:46 +0000699 virtual void blitH(int x, int y, int width) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000700 fProxy->blitH(x, y, width);
701 }
reed@google.com82065d62011-02-07 15:30:46 +0000702
703 virtual void blitAntiH(int x, int y, const SkAlpha antialias[],
704 const int16_t runs[]) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000705 fProxy->blitAntiH(x, y, antialias, runs);
706 }
reed@google.com82065d62011-02-07 15:30:46 +0000707
708 virtual void blitV(int x, int y, int height, SkAlpha alpha) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000709 fProxy->blitV(x, y, height, alpha);
710 }
reed@google.com82065d62011-02-07 15:30:46 +0000711
712 virtual void blitRect(int x, int y, int width, int height) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000713 fProxy->blitRect(x, y, width, height);
714 }
reed@google.com82065d62011-02-07 15:30:46 +0000715
716 virtual void blitMask(const SkMask& mask, const SkIRect& clip) {
717 if (mask.fFormat == SkMask::k3D_Format) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000718 f3DShader->setMask(&mask);
719
720 ((SkMask*)&mask)->fFormat = SkMask::kA8_Format;
721 fProxy->blitMask(mask, clip);
722 ((SkMask*)&mask)->fFormat = SkMask::k3D_Format;
723
724 f3DShader->setMask(NULL);
reed@google.com82065d62011-02-07 15:30:46 +0000725 } else {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000726 fProxy->blitMask(mask, clip);
reed@google.com82065d62011-02-07 15:30:46 +0000727 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000728 }
reed@google.com82065d62011-02-07 15:30:46 +0000729
reed@android.com8a1c16f2008-12-17 15:59:43 +0000730private:
731 SkBlitter* fProxy;
732 Sk3DShader* f3DShader;
733 void (*fKillProc)(void*);
734};
735
reed@google.com82065d62011-02-07 15:30:46 +0000736///////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +0000737
738#include "SkCoreBlitters.h"
739
reed@android.com8a1c16f2008-12-17 15:59:43 +0000740class SkAutoCallProc {
741public:
742 typedef void (*Proc)(void*);
reed@google.com82065d62011-02-07 15:30:46 +0000743
reed@android.com8a1c16f2008-12-17 15:59:43 +0000744 SkAutoCallProc(void* obj, Proc proc)
reed@google.com82065d62011-02-07 15:30:46 +0000745 : fObj(obj), fProc(proc) {}
746
747 ~SkAutoCallProc() {
748 if (fObj && fProc) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000749 fProc(fObj);
reed@google.com82065d62011-02-07 15:30:46 +0000750 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000751 }
reed@google.com82065d62011-02-07 15:30:46 +0000752
reed@android.com8a1c16f2008-12-17 15:59:43 +0000753 void* get() const { return fObj; }
reed@google.com82065d62011-02-07 15:30:46 +0000754
755 void* detach() {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000756 void* obj = fObj;
757 fObj = NULL;
758 return obj;
759 }
reed@google.com82065d62011-02-07 15:30:46 +0000760
reed@android.com8a1c16f2008-12-17 15:59:43 +0000761private:
762 void* fObj;
763 Proc fProc;
764};
765
reed@google.com82065d62011-02-07 15:30:46 +0000766static void destroy_blitter(void* blitter) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000767 ((SkBlitter*)blitter)->~SkBlitter();
768}
769
reed@google.com82065d62011-02-07 15:30:46 +0000770static void delete_blitter(void* blitter) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000771 SkDELETE((SkBlitter*)blitter);
772}
773
reed@android.comd252db02009-04-01 18:31:44 +0000774static bool just_solid_color(const SkPaint& paint) {
775 if (paint.getAlpha() == 0xFF && paint.getColorFilter() == NULL) {
776 SkShader* shader = paint.getShader();
777 if (NULL == shader ||
778 (shader->getFlags() & SkShader::kOpaqueAlpha_Flag)) {
779 return true;
780 }
781 }
782 return false;
783}
reed@google.com82065d62011-02-07 15:30:46 +0000784
reed@android.comd252db02009-04-01 18:31:44 +0000785/** By analyzing the paint (with an xfermode), we may decide we can take
786 special action. This enum lists our possible actions
787 */
788enum XferInterp {
789 kNormal_XferInterp, // no special interpretation, draw normally
790 kSrcOver_XferInterp, // draw as if in srcover mode
791 kSkipDrawing_XferInterp // draw nothing
792};
793
794static XferInterp interpret_xfermode(const SkPaint& paint, SkXfermode* xfer,
795 SkBitmap::Config deviceConfig) {
reed@android.com845fdac2009-06-23 03:01:32 +0000796 SkXfermode::Mode mode;
reed@google.com82065d62011-02-07 15:30:46 +0000797
mike@reedtribe.orgbe2aa2a2011-11-17 02:32:04 +0000798 if (SkXfermode::AsMode(xfer, &mode)) {
reed@android.comd252db02009-04-01 18:31:44 +0000799 switch (mode) {
reed@android.com845fdac2009-06-23 03:01:32 +0000800 case SkXfermode::kSrc_Mode:
reed@android.comd252db02009-04-01 18:31:44 +0000801 if (just_solid_color(paint)) {
802 return kSrcOver_XferInterp;
803 }
804 break;
reed@android.com845fdac2009-06-23 03:01:32 +0000805 case SkXfermode::kDst_Mode:
reed@android.comd252db02009-04-01 18:31:44 +0000806 return kSkipDrawing_XferInterp;
reed@android.com845fdac2009-06-23 03:01:32 +0000807 case SkXfermode::kSrcOver_Mode:
reed@android.comd252db02009-04-01 18:31:44 +0000808 return kSrcOver_XferInterp;
reed@android.com845fdac2009-06-23 03:01:32 +0000809 case SkXfermode::kDstOver_Mode:
reed@android.comd252db02009-04-01 18:31:44 +0000810 if (SkBitmap::kRGB_565_Config == deviceConfig) {
811 return kSkipDrawing_XferInterp;
812 }
813 break;
reed@android.com845fdac2009-06-23 03:01:32 +0000814 case SkXfermode::kSrcIn_Mode:
reed@android.comd252db02009-04-01 18:31:44 +0000815 if (SkBitmap::kRGB_565_Config == deviceConfig &&
816 just_solid_color(paint)) {
817 return kSrcOver_XferInterp;
818 }
819 break;
reed@android.com845fdac2009-06-23 03:01:32 +0000820 case SkXfermode::kDstIn_Mode:
reed@android.comd252db02009-04-01 18:31:44 +0000821 if (just_solid_color(paint)) {
822 return kSkipDrawing_XferInterp;
823 }
824 break;
825 default:
826 break;
827 }
828 }
829 return kNormal_XferInterp;
830}
831
reed@android.com8a1c16f2008-12-17 15:59:43 +0000832SkBlitter* SkBlitter::Choose(const SkBitmap& device,
833 const SkMatrix& matrix,
reed@google.com6b7aee32011-04-19 18:36:09 +0000834 const SkPaint& origPaint,
reed@google.com82065d62011-02-07 15:30:46 +0000835 void* storage, size_t storageSize) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000836 SkASSERT(storageSize == 0 || storage != NULL);
837
838 SkBlitter* blitter = NULL;
839
840 // which check, in case we're being called by a client with a dummy device
841 // (e.g. they have a bounder that always aborts the draw)
reed@google.com82065d62011-02-07 15:30:46 +0000842 if (SkBitmap::kNo_Config == device.getConfig()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000843 SK_PLACEMENT_NEW(blitter, SkNullBlitter, storage, storageSize);
844 return blitter;
845 }
846
reed@google.com6b7aee32011-04-19 18:36:09 +0000847 SkPaint paint(origPaint);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000848 SkShader* shader = paint.getShader();
reed@google.com6b7aee32011-04-19 18:36:09 +0000849 SkColorFilter* cf = paint.getColorFilter();
850 SkXfermode* mode = paint.getXfermode();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000851
852 Sk3DShader* shader3D = NULL;
reed@google.com82065d62011-02-07 15:30:46 +0000853 if (paint.getMaskFilter() != NULL &&
854 paint.getMaskFilter()->getFormat() == SkMask::k3D_Format) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000855 shader3D = SkNEW_ARGS(Sk3DShader, (shader));
reed@google.com6b7aee32011-04-19 18:36:09 +0000856 paint.setShader(shader3D)->unref();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000857 shader = shader3D;
858 }
859
reed@android.comd252db02009-04-01 18:31:44 +0000860 if (NULL != mode) {
861 switch (interpret_xfermode(paint, mode, device.config())) {
862 case kSrcOver_XferInterp:
863 mode = NULL;
reed@google.com6b7aee32011-04-19 18:36:09 +0000864 paint.setXfermode(NULL);
reed@android.comd252db02009-04-01 18:31:44 +0000865 break;
866 case kSkipDrawing_XferInterp:
867 SK_PLACEMENT_NEW(blitter, SkNullBlitter, storage, storageSize);
868 return blitter;
869 default:
870 break;
871 }
872 }
873
reed@google.com6b7aee32011-04-19 18:36:09 +0000874 if (NULL == shader) {
875#ifdef SK_IGNORE_CF_OPTIMIZATION
876 if (mode || cf) {
877#else
878 if (mode) {
879#endif
880 // xfermodes (and filters) require shaders for our current blitters
881 shader = SkNEW(SkColorShader);
882 paint.setShader(shader)->unref();
883 } else if (cf) {
884 // if no shader && no xfermode, we just apply the colorfilter to
885 // our color and move on.
886 paint.setColor(cf->filterColor(paint.getColor()));
887 paint.setColorFilter(NULL);
888 cf = NULL;
889 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000890 }
reed@google.com82065d62011-02-07 15:30:46 +0000891
reed@google.com6b7aee32011-04-19 18:36:09 +0000892 if (cf) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000893 SkASSERT(shader);
reed@google.com6b7aee32011-04-19 18:36:09 +0000894 shader = SkNEW_ARGS(SkFilterShader, (shader, cf));
895 paint.setShader(shader)->unref();
reed@android.com1fc4c602009-10-02 16:34:57 +0000896 // blitters should ignore the presence/absence of a filter, since
897 // if there is one, the shader will take care of it.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000898 }
reed@google.com82065d62011-02-07 15:30:46 +0000899
reed@android.comcafc9f92009-08-22 03:44:57 +0000900 if (shader && !shader->setContext(device, paint, matrix)) {
901 return SkNEW(SkNullBlitter);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000902 }
903
904 switch (device.getConfig()) {
reed@google.com82065d62011-02-07 15:30:46 +0000905 case SkBitmap::kA1_Config:
906 SK_PLACEMENT_NEW_ARGS(blitter, SkA1_Blitter,
907 storage, storageSize, (device, paint));
908 break;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000909
reed@google.com82065d62011-02-07 15:30:46 +0000910 case SkBitmap::kA8_Config:
911 if (shader) {
912 SK_PLACEMENT_NEW_ARGS(blitter, SkA8_Shader_Blitter,
913 storage, storageSize, (device, paint));
914 } else {
915 SK_PLACEMENT_NEW_ARGS(blitter, SkA8_Blitter,
916 storage, storageSize, (device, paint));
917 }
918 break;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000919
reed@google.com82065d62011-02-07 15:30:46 +0000920 case SkBitmap::kARGB_4444_Config:
921 blitter = SkBlitter_ChooseD4444(device, paint, storage, storageSize);
922 break;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000923
reed@google.com82065d62011-02-07 15:30:46 +0000924 case SkBitmap::kRGB_565_Config:
925 blitter = SkBlitter_ChooseD565(device, paint, storage, storageSize);
926 break;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000927
reed@google.com82065d62011-02-07 15:30:46 +0000928 case SkBitmap::kARGB_8888_Config:
929 if (shader) {
930 SK_PLACEMENT_NEW_ARGS(blitter, SkARGB32_Shader_Blitter,
931 storage, storageSize, (device, paint));
932 } else if (paint.getColor() == SK_ColorBLACK) {
933 SK_PLACEMENT_NEW_ARGS(blitter, SkARGB32_Black_Blitter,
934 storage, storageSize, (device, paint));
935 } else if (paint.getAlpha() == 0xFF) {
936 SK_PLACEMENT_NEW_ARGS(blitter, SkARGB32_Opaque_Blitter,
937 storage, storageSize, (device, paint));
938 } else {
939 SK_PLACEMENT_NEW_ARGS(blitter, SkARGB32_Blitter,
940 storage, storageSize, (device, paint));
941 }
942 break;
943
944 default:
945 SkASSERT(!"unsupported device config");
946 SK_PLACEMENT_NEW(blitter, SkNullBlitter, storage, storageSize);
947 break;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000948 }
949
reed@google.com82065d62011-02-07 15:30:46 +0000950 if (shader3D) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000951 void (*proc)(void*) = ((void*)storage == (void*)blitter) ? destroy_blitter : delete_blitter;
952 SkAutoCallProc tmp(blitter, proc);
953
954 blitter = SkNEW_ARGS(Sk3DBlitter, (blitter, shader3D, proc));
955 (void)tmp.detach();
956 }
957 return blitter;
958}
959
reed@google.com82065d62011-02-07 15:30:46 +0000960///////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +0000961
962const uint16_t gMask_0F0F = 0xF0F;
963const uint32_t gMask_00FF00FF = 0xFF00FF;
964
reed@google.com82065d62011-02-07 15:30:46 +0000965///////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +0000966
967SkShaderBlitter::SkShaderBlitter(const SkBitmap& device, const SkPaint& paint)
reed@android.com5119bdb2009-06-12 21:27:03 +0000968 : INHERITED(device) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000969 fShader = paint.getShader();
970 SkASSERT(fShader);
971
972 fShader->ref();
973 fShader->beginSession();
reed@android.com5119bdb2009-06-12 21:27:03 +0000974 fShaderFlags = fShader->getFlags();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000975}
976
reed@android.com5119bdb2009-06-12 21:27:03 +0000977SkShaderBlitter::~SkShaderBlitter() {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000978 fShader->endSession();
979 fShader->unref();
980}
981