blob: 735ad0431fb0603ed9d60159f337a7d160432de8 [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) {
tomhudson@google.com0c00f212011-12-28 14:59:50 +000027 SkDEBUGFAIL("unimplemented");
reed@android.com8a1c16f2008-12-17 15:59:43 +000028}
29
reed@google.com82065d62011-02-07 15:30:46 +000030void SkBlitter::blitAntiH(int x, int y, const SkAlpha antialias[],
31 const int16_t runs[]) {
tomhudson@google.com0c00f212011-12-28 14:59:50 +000032 SkDEBUGFAIL("unimplemented");
reed@android.com8a1c16f2008-12-17 15:59:43 +000033}
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) {
tomhudson@google.coma31ac732011-12-29 16:09:31 +000050 SkASSERT(width > 0);
reed@google.com82065d62011-02-07 15:30:46 +000051 while (--height >= 0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000052 this->blitH(x, y++, width);
reed@google.com82065d62011-02-07 15:30:46 +000053 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000054}
55
tomhudson@google.com49eac192011-12-27 13:59:20 +000056/// Default implementation doesn't check for any easy optimizations
57/// such as alpha == 0 or 255; also uses blitV(), which some subclasses
58/// may not support.
59void SkBlitter::blitAntiRect(int x, int y, int width, int height,
60 SkAlpha leftAlpha, SkAlpha rightAlpha) {
tomhudson@google.com47143592011-12-28 17:58:07 +000061 this->blitV(x++, y, height, leftAlpha);
tomhudson@google.coma31ac732011-12-29 16:09:31 +000062 if (width > 0) {
tomhudson@google.com47143592011-12-28 17:58:07 +000063 this->blitRect(x, y, width, height);
64 x += width;
65 }
66 this->blitV(x, y, height, rightAlpha);
tomhudson@google.com49eac192011-12-27 13:59:20 +000067}
68
reed@android.com8a1c16f2008-12-17 15:59:43 +000069//////////////////////////////////////////////////////////////////////////////
70
reed@google.com82065d62011-02-07 15:30:46 +000071static inline void bits_to_runs(SkBlitter* blitter, int x, int y,
72 const uint8_t bits[],
73 U8CPU left_mask, int rowBytes,
74 U8CPU right_mask) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000075 int inFill = 0;
76 int pos = 0;
77
reed@google.com82065d62011-02-07 15:30:46 +000078 while (--rowBytes >= 0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000079 unsigned b = *bits++ & left_mask;
reed@google.com82065d62011-02-07 15:30:46 +000080 if (rowBytes == 0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000081 b &= right_mask;
reed@google.com82065d62011-02-07 15:30:46 +000082 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000083
reed@google.com82065d62011-02-07 15:30:46 +000084 for (unsigned test = 0x80; test != 0; test >>= 1) {
85 if (b & test) {
86 if (!inFill) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000087 pos = x;
88 inFill = true;
89 }
reed@google.com82065d62011-02-07 15:30:46 +000090 } else {
91 if (inFill) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000092 blitter->blitH(pos, y, x - pos);
93 inFill = false;
94 }
95 }
96 x += 1;
97 }
98 left_mask = 0xFF;
99 }
100
101 // final cleanup
reed@google.com82065d62011-02-07 15:30:46 +0000102 if (inFill) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000103 blitter->blitH(pos, y, x - pos);
reed@google.com82065d62011-02-07 15:30:46 +0000104 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000105}
106
reed@google.com82065d62011-02-07 15:30:46 +0000107void SkBlitter::blitMask(const SkMask& mask, const SkIRect& clip) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000108 SkASSERT(mask.fBounds.contains(clip));
109
reed@google.com82065d62011-02-07 15:30:46 +0000110 if (mask.fFormat == SkMask::kBW_Format) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000111 int cx = clip.fLeft;
112 int cy = clip.fTop;
113 int maskLeft = mask.fBounds.fLeft;
114 int mask_rowBytes = mask.fRowBytes;
115 int height = clip.height();
116
117 const uint8_t* bits = mask.getAddr1(cx, cy);
118
reed@google.com82065d62011-02-07 15:30:46 +0000119 if (cx == maskLeft && clip.fRight == mask.fBounds.fRight) {
120 while (--height >= 0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000121 bits_to_runs(this, cx, cy, bits, 0xFF, mask_rowBytes, 0xFF);
122 bits += mask_rowBytes;
123 cy += 1;
124 }
reed@google.com82065d62011-02-07 15:30:46 +0000125 } else {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000126 int left_edge = cx - maskLeft;
127 SkASSERT(left_edge >= 0);
128 int rite_edge = clip.fRight - maskLeft;
129 SkASSERT(rite_edge > left_edge);
130
131 int left_mask = 0xFF >> (left_edge & 7);
132 int rite_mask = 0xFF << (8 - (rite_edge & 7));
133 int full_runs = (rite_edge >> 3) - ((left_edge + 7) >> 3);
134
reed@google.coma89c77b2011-12-01 21:47:26 +0000135 // 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 +0000136 if (rite_mask == 0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000137 SkASSERT(full_runs >= 0);
138 full_runs -= 1;
139 rite_mask = 0xFF;
140 }
reed@google.com82065d62011-02-07 15:30:46 +0000141 if (left_mask == 0xFF) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000142 full_runs -= 1;
reed@google.com82065d62011-02-07 15:30:46 +0000143 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000144
reed@google.coma89c77b2011-12-01 21:47:26 +0000145 // back up manually so we can keep in sync with our byte-aligned src
146 // have cx reflect our actual starting x-coord
reed@android.com8a1c16f2008-12-17 15:59:43 +0000147 cx -= left_edge & 7;
148
reed@google.com82065d62011-02-07 15:30:46 +0000149 if (full_runs < 0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000150 SkASSERT((left_mask & rite_mask) != 0);
reed@google.com82065d62011-02-07 15:30:46 +0000151 while (--height >= 0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000152 bits_to_runs(this, cx, cy, bits, left_mask, 1, rite_mask);
153 bits += mask_rowBytes;
154 cy += 1;
155 }
reed@google.com82065d62011-02-07 15:30:46 +0000156 } else {
157 while (--height >= 0) {
reed@google.coma89c77b2011-12-01 21:47:26 +0000158 bits_to_runs(this, cx, cy, bits, left_mask, full_runs + 2, rite_mask);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000159 bits += mask_rowBytes;
160 cy += 1;
161 }
162 }
163 }
reed@google.com82065d62011-02-07 15:30:46 +0000164 } else {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000165 int width = clip.width();
166 SkAutoSTMalloc<64, int16_t> runStorage(width + 1);
167 int16_t* runs = runStorage.get();
reed@google.com79891862011-10-18 15:44:57 +0000168 const uint8_t* aa = mask.getAddr8(clip.fLeft, clip.fTop);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000169
170 sk_memset16((uint16_t*)runs, 1, width);
171 runs[width] = 0;
172
173 int height = clip.height();
174 int y = clip.fTop;
reed@google.com82065d62011-02-07 15:30:46 +0000175 while (--height >= 0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000176 this->blitAntiH(clip.fLeft, y, aa, runs);
177 aa += mask.fRowBytes;
178 y += 1;
179 }
180 }
181}
182
183/////////////////////// these guys are not virtual, just a helpers
184
185void SkBlitter::blitMaskRegion(const SkMask& mask, const SkRegion& clip) {
186 if (clip.quickReject(mask.fBounds)) {
187 return;
188 }
reed@google.com82065d62011-02-07 15:30:46 +0000189
reed@android.com8a1c16f2008-12-17 15:59:43 +0000190 SkRegion::Cliperator clipper(clip, mask.fBounds);
reed@google.com82065d62011-02-07 15:30:46 +0000191
reed@android.com8a1c16f2008-12-17 15:59:43 +0000192 while (!clipper.done()) {
193 const SkIRect& cr = clipper.rect();
194 this->blitMask(mask, cr);
195 clipper.next();
196 }
197}
198
199void SkBlitter::blitRectRegion(const SkIRect& rect, const SkRegion& clip) {
200 SkRegion::Cliperator clipper(clip, rect);
reed@google.com82065d62011-02-07 15:30:46 +0000201
reed@android.com8a1c16f2008-12-17 15:59:43 +0000202 while (!clipper.done()) {
203 const SkIRect& cr = clipper.rect();
204 this->blitRect(cr.fLeft, cr.fTop, cr.width(), cr.height());
205 clipper.next();
206 }
207}
208
209void SkBlitter::blitRegion(const SkRegion& clip) {
210 SkRegion::Iterator iter(clip);
reed@google.com82065d62011-02-07 15:30:46 +0000211
reed@android.com8a1c16f2008-12-17 15:59:43 +0000212 while (!iter.done()) {
213 const SkIRect& cr = iter.rect();
214 this->blitRect(cr.fLeft, cr.fTop, cr.width(), cr.height());
215 iter.next();
216 }
217}
218
reed@google.com82065d62011-02-07 15:30:46 +0000219///////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +0000220
reed@google.com82065d62011-02-07 15:30:46 +0000221void SkNullBlitter::blitH(int x, int y, int width) {}
reed@android.com8a1c16f2008-12-17 15:59:43 +0000222
reed@google.com82065d62011-02-07 15:30:46 +0000223void SkNullBlitter::blitAntiH(int x, int y, const SkAlpha antialias[],
224 const int16_t runs[]) {}
reed@android.com8a1c16f2008-12-17 15:59:43 +0000225
reed@google.com82065d62011-02-07 15:30:46 +0000226void SkNullBlitter::blitV(int x, int y, int height, SkAlpha alpha) {}
reed@android.com8a1c16f2008-12-17 15:59:43 +0000227
reed@google.com82065d62011-02-07 15:30:46 +0000228void SkNullBlitter::blitRect(int x, int y, int width, int height) {}
reed@android.com8a1c16f2008-12-17 15:59:43 +0000229
reed@google.com82065d62011-02-07 15:30:46 +0000230void SkNullBlitter::blitMask(const SkMask& mask, const SkIRect& clip) {}
reed@android.com8a1c16f2008-12-17 15:59:43 +0000231
reed@google.com82065d62011-02-07 15:30:46 +0000232const SkBitmap* SkNullBlitter::justAnOpaqueColor(uint32_t* value) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000233 return NULL;
234}
235
reed@google.com82065d62011-02-07 15:30:46 +0000236///////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +0000237
reed@google.com82065d62011-02-07 15:30:46 +0000238static int compute_anti_width(const int16_t runs[]) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000239 int width = 0;
reed@google.com82065d62011-02-07 15:30:46 +0000240
241 for (;;) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000242 int count = runs[0];
reed@google.com82065d62011-02-07 15:30:46 +0000243
reed@android.com8a1c16f2008-12-17 15:59:43 +0000244 SkASSERT(count >= 0);
reed@google.com82065d62011-02-07 15:30:46 +0000245 if (count == 0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000246 break;
reed@google.com82065d62011-02-07 15:30:46 +0000247 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000248 width += count;
249 runs += count;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000250 }
251 return width;
252}
253
reed@google.com82065d62011-02-07 15:30:46 +0000254static inline bool y_in_rect(int y, const SkIRect& rect) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000255 return (unsigned)(y - rect.fTop) < (unsigned)rect.height();
256}
257
reed@google.com82065d62011-02-07 15:30:46 +0000258static inline bool x_in_rect(int x, const SkIRect& rect) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000259 return (unsigned)(x - rect.fLeft) < (unsigned)rect.width();
260}
261
reed@google.com82065d62011-02-07 15:30:46 +0000262void SkRectClipBlitter::blitH(int left, int y, int width) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000263 SkASSERT(width > 0);
264
reed@google.com82065d62011-02-07 15:30:46 +0000265 if (!y_in_rect(y, fClipRect)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000266 return;
reed@google.com82065d62011-02-07 15:30:46 +0000267 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000268
269 int right = left + width;
270
reed@google.com82065d62011-02-07 15:30:46 +0000271 if (left < fClipRect.fLeft) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000272 left = fClipRect.fLeft;
reed@google.com82065d62011-02-07 15:30:46 +0000273 }
274 if (right > fClipRect.fRight) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000275 right = fClipRect.fRight;
reed@google.com82065d62011-02-07 15:30:46 +0000276 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000277
278 width = right - left;
reed@google.com82065d62011-02-07 15:30:46 +0000279 if (width > 0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000280 fBlitter->blitH(left, y, width);
reed@google.com82065d62011-02-07 15:30:46 +0000281 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000282}
283
reed@google.com82065d62011-02-07 15:30:46 +0000284void SkRectClipBlitter::blitAntiH(int left, int y, const SkAlpha aa[],
285 const int16_t runs[]) {
286 if (!y_in_rect(y, fClipRect) || left >= fClipRect.fRight) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000287 return;
reed@google.com82065d62011-02-07 15:30:46 +0000288 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000289
290 int x0 = left;
291 int x1 = left + compute_anti_width(runs);
292
reed@google.com82065d62011-02-07 15:30:46 +0000293 if (x1 <= fClipRect.fLeft) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000294 return;
reed@google.com82065d62011-02-07 15:30:46 +0000295 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000296
297 SkASSERT(x0 < x1);
reed@google.com82065d62011-02-07 15:30:46 +0000298 if (x0 < fClipRect.fLeft) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000299 int dx = fClipRect.fLeft - x0;
300 SkAlphaRuns::BreakAt((int16_t*)runs, (uint8_t*)aa, dx);
301 runs += dx;
302 aa += dx;
303 x0 = fClipRect.fLeft;
304 }
305
306 SkASSERT(x0 < x1 && runs[x1 - x0] == 0);
reed@google.com82065d62011-02-07 15:30:46 +0000307 if (x1 > fClipRect.fRight) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000308 x1 = fClipRect.fRight;
309 SkAlphaRuns::BreakAt((int16_t*)runs, (uint8_t*)aa, x1 - x0);
310 ((int16_t*)runs)[x1 - x0] = 0;
311 }
312
313 SkASSERT(x0 < x1 && runs[x1 - x0] == 0);
314 SkASSERT(compute_anti_width(runs) == x1 - x0);
315
316 fBlitter->blitAntiH(x0, y, aa, runs);
317}
318
reed@google.com82065d62011-02-07 15:30:46 +0000319void SkRectClipBlitter::blitV(int x, int y, int height, SkAlpha alpha) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000320 SkASSERT(height > 0);
321
reed@google.com82065d62011-02-07 15:30:46 +0000322 if (!x_in_rect(x, fClipRect)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000323 return;
reed@google.com82065d62011-02-07 15:30:46 +0000324 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000325
326 int y0 = y;
327 int y1 = y + height;
328
reed@google.com82065d62011-02-07 15:30:46 +0000329 if (y0 < fClipRect.fTop) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000330 y0 = fClipRect.fTop;
reed@google.com82065d62011-02-07 15:30:46 +0000331 }
332 if (y1 > fClipRect.fBottom) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000333 y1 = fClipRect.fBottom;
reed@google.com82065d62011-02-07 15:30:46 +0000334 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000335
reed@google.com82065d62011-02-07 15:30:46 +0000336 if (y0 < y1) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000337 fBlitter->blitV(x, y0, y1 - y0, alpha);
reed@google.com82065d62011-02-07 15:30:46 +0000338 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000339}
340
reed@google.com82065d62011-02-07 15:30:46 +0000341void SkRectClipBlitter::blitRect(int left, int y, int width, int height) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000342 SkIRect r;
343
344 r.set(left, y, left + width, y + height);
reed@google.com82065d62011-02-07 15:30:46 +0000345 if (r.intersect(fClipRect)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000346 fBlitter->blitRect(r.fLeft, r.fTop, r.width(), r.height());
reed@google.com82065d62011-02-07 15:30:46 +0000347 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000348}
349
tomhudson@google.com49eac192011-12-27 13:59:20 +0000350void SkRectClipBlitter::blitAntiRect(int left, int y, int width, int height,
351 SkAlpha leftAlpha, SkAlpha rightAlpha) {
352 SkIRect r;
353
354 // The *true* width of the rectangle blitted is width+2:
355 r.set(left, y, left + width + 2, y + height);
356 if (r.intersect(fClipRect)) {
357 if (r.fLeft != left) {
358 SkASSERT(r.fLeft > left);
359 leftAlpha = 255;
360 }
361 if (r.fRight != left + width + 2) {
362 SkASSERT(r.fRight < left + width + 2);
363 rightAlpha = 255;
364 }
365 if (255 == leftAlpha && 255 == rightAlpha) {
366 fBlitter->blitRect(r.fLeft, r.fTop, r.width(), r.height());
367 } else if (1 == r.width()) {
368 if (r.fLeft == left) {
369 fBlitter->blitV(r.fLeft, r.fTop, r.height(), leftAlpha);
370 } else {
371 SkASSERT(r.fLeft == left + width + 1);
372 fBlitter->blitV(r.fLeft, r.fTop, r.height(), rightAlpha);
373 }
374 } else {
375 fBlitter->blitAntiRect(r.fLeft, r.fTop, r.width() - 2, r.height(),
376 leftAlpha, rightAlpha);
377 }
378 }
379}
380
reed@google.com82065d62011-02-07 15:30:46 +0000381void SkRectClipBlitter::blitMask(const SkMask& mask, const SkIRect& clip) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000382 SkASSERT(mask.fBounds.contains(clip));
383
384 SkIRect r = clip;
385
reed@google.com82065d62011-02-07 15:30:46 +0000386 if (r.intersect(fClipRect)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000387 fBlitter->blitMask(mask, r);
reed@google.com82065d62011-02-07 15:30:46 +0000388 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000389}
390
reed@google.com82065d62011-02-07 15:30:46 +0000391const SkBitmap* SkRectClipBlitter::justAnOpaqueColor(uint32_t* value) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000392 return fBlitter->justAnOpaqueColor(value);
393}
394
reed@google.com82065d62011-02-07 15:30:46 +0000395///////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +0000396
reed@google.com82065d62011-02-07 15:30:46 +0000397void SkRgnClipBlitter::blitH(int x, int y, int width) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000398 SkRegion::Spanerator span(*fRgn, y, x, x + width);
399 int left, right;
400
reed@google.com82065d62011-02-07 15:30:46 +0000401 while (span.next(&left, &right)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000402 SkASSERT(left < right);
403 fBlitter->blitH(left, y, right - left);
404 }
405}
406
reed@google.com82065d62011-02-07 15:30:46 +0000407void SkRgnClipBlitter::blitAntiH(int x, int y, const SkAlpha aa[],
408 const int16_t runs[]) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000409 int width = compute_anti_width(runs);
410 SkRegion::Spanerator span(*fRgn, y, x, x + width);
411 int left, right;
412 SkDEBUGCODE(const SkIRect& bounds = fRgn->getBounds();)
reed@google.com82065d62011-02-07 15:30:46 +0000413
reed@android.com8a1c16f2008-12-17 15:59:43 +0000414 int prevRite = x;
reed@google.com82065d62011-02-07 15:30:46 +0000415 while (span.next(&left, &right)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000416 SkASSERT(x <= left);
417 SkASSERT(left < right);
418 SkASSERT(left >= bounds.fLeft && right <= bounds.fRight);
reed@google.com82065d62011-02-07 15:30:46 +0000419
reed@android.com8a1c16f2008-12-17 15:59:43 +0000420 SkAlphaRuns::Break((int16_t*)runs, (uint8_t*)aa, left - x, right - left);
421
422 // now zero before left
reed@google.com82065d62011-02-07 15:30:46 +0000423 if (left > prevRite) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000424 int index = prevRite - x;
425 ((uint8_t*)aa)[index] = 0; // skip runs after right
426 ((int16_t*)runs)[index] = SkToS16(left - prevRite);
427 }
reed@google.com82065d62011-02-07 15:30:46 +0000428
reed@android.com8a1c16f2008-12-17 15:59:43 +0000429 prevRite = right;
430 }
reed@google.com82065d62011-02-07 15:30:46 +0000431
432 if (prevRite > x) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000433 ((int16_t*)runs)[prevRite - x] = 0;
reed@google.com82065d62011-02-07 15:30:46 +0000434
reed@android.com8a1c16f2008-12-17 15:59:43 +0000435 if (x < 0) {
436 int skip = runs[0];
437 SkASSERT(skip >= -x);
438 aa += skip;
439 runs += skip;
440 x += skip;
441 }
442 fBlitter->blitAntiH(x, y, aa, runs);
443 }
444}
445
reed@google.com82065d62011-02-07 15:30:46 +0000446void SkRgnClipBlitter::blitV(int x, int y, int height, SkAlpha alpha) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000447 SkIRect bounds;
448 bounds.set(x, y, x + 1, y + height);
449
450 SkRegion::Cliperator iter(*fRgn, bounds);
451
reed@google.com82065d62011-02-07 15:30:46 +0000452 while (!iter.done()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000453 const SkIRect& r = iter.rect();
454 SkASSERT(bounds.contains(r));
455
456 fBlitter->blitV(x, r.fTop, r.height(), alpha);
457 iter.next();
458 }
459}
460
reed@google.com82065d62011-02-07 15:30:46 +0000461void SkRgnClipBlitter::blitRect(int x, int y, int width, int height) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000462 SkIRect bounds;
463 bounds.set(x, y, x + width, y + height);
464
465 SkRegion::Cliperator iter(*fRgn, bounds);
466
reed@google.com82065d62011-02-07 15:30:46 +0000467 while (!iter.done()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000468 const SkIRect& r = iter.rect();
469 SkASSERT(bounds.contains(r));
470
471 fBlitter->blitRect(r.fLeft, r.fTop, r.width(), r.height());
472 iter.next();
473 }
474}
475
tomhudson@google.com49eac192011-12-27 13:59:20 +0000476void SkRgnClipBlitter::blitAntiRect(int x, int y, int width, int height,
477 SkAlpha leftAlpha, SkAlpha rightAlpha) {
478 // The *true* width of the rectangle to blit is width + 2
479 SkIRect bounds;
480 bounds.set(x, y, x + width + 2, y + height);
481
482 SkRegion::Cliperator iter(*fRgn, bounds);
483
484 while (!iter.done()) {
485 const SkIRect& r = iter.rect();
486 SkASSERT(bounds.contains(r));
487 SkASSERT(r.fLeft >= x);
tomhudson@google.com31bab392012-01-03 20:12:42 +0000488 SkASSERT(r.fRight <= x + width + 2);
tomhudson@google.com49eac192011-12-27 13:59:20 +0000489
490 SkAlpha effectiveLeftAlpha = (r.fLeft == x) ? leftAlpha : 255;
491 SkAlpha effectiveRightAlpha = (r.fRight == x + width + 2) ?
492 rightAlpha : 255;
493
494 if (255 == effectiveLeftAlpha && 255 == effectiveRightAlpha) {
495 fBlitter->blitRect(r.fLeft, r.fTop, r.width(), r.height());
496 } else if (1 == r.width()) {
497 if (r.fLeft == x) {
498 fBlitter->blitV(r.fLeft, r.fTop, r.height(),
499 effectiveLeftAlpha);
500 } else {
501 SkASSERT(r.fLeft == x + width + 1);
502 fBlitter->blitV(r.fLeft, r.fTop, r.height(),
503 effectiveRightAlpha);
504 }
505 } else {
506 fBlitter->blitAntiRect(r.fLeft, r.fTop, r.width() - 2, r.height(),
507 effectiveLeftAlpha, effectiveRightAlpha);
508 }
509 iter.next();
510 }
511}
512
513
reed@google.com82065d62011-02-07 15:30:46 +0000514void SkRgnClipBlitter::blitMask(const SkMask& mask, const SkIRect& clip) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000515 SkASSERT(mask.fBounds.contains(clip));
516
517 SkRegion::Cliperator iter(*fRgn, clip);
518 const SkIRect& r = iter.rect();
519 SkBlitter* blitter = fBlitter;
520
reed@google.com82065d62011-02-07 15:30:46 +0000521 while (!iter.done()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000522 blitter->blitMask(mask, r);
523 iter.next();
524 }
525}
526
reed@google.com82065d62011-02-07 15:30:46 +0000527const SkBitmap* SkRgnClipBlitter::justAnOpaqueColor(uint32_t* value) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000528 return fBlitter->justAnOpaqueColor(value);
529}
530
reed@google.com82065d62011-02-07 15:30:46 +0000531///////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +0000532
reed@google.com82065d62011-02-07 15:30:46 +0000533SkBlitter* SkBlitterClipper::apply(SkBlitter* blitter, const SkRegion* clip,
534 const SkIRect* ir) {
535 if (clip) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000536 const SkIRect& clipR = clip->getBounds();
537
reed@google.com82065d62011-02-07 15:30:46 +0000538 if (clip->isEmpty() || (ir && !SkIRect::Intersects(clipR, *ir))) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000539 blitter = &fNullBlitter;
reed@google.com82065d62011-02-07 15:30:46 +0000540 } else if (clip->isRect()) {
541 if (ir == NULL || !clipR.contains(*ir)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000542 fRectBlitter.init(blitter, clipR);
543 blitter = &fRectBlitter;
544 }
reed@google.com82065d62011-02-07 15:30:46 +0000545 } else {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000546 fRgnBlitter.init(blitter, clip);
547 blitter = &fRgnBlitter;
548 }
549 }
550 return blitter;
551}
552
reed@google.com82065d62011-02-07 15:30:46 +0000553///////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +0000554
555#include "SkColorShader.h"
556#include "SkColorPriv.h"
557
558class Sk3DShader : public SkShader {
559public:
reed@google.com82065d62011-02-07 15:30:46 +0000560 Sk3DShader(SkShader* proxy) : fProxy(proxy) {
561 SkSafeRef(proxy);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000562 fMask = NULL;
563 }
reed@google.com82065d62011-02-07 15:30:46 +0000564
565 virtual ~Sk3DShader() {
566 SkSafeUnref(fProxy);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000567 }
reed@google.com82065d62011-02-07 15:30:46 +0000568
reed@android.com8a1c16f2008-12-17 15:59:43 +0000569 void setMask(const SkMask* mask) { fMask = mask; }
570
reed@google.com82065d62011-02-07 15:30:46 +0000571 virtual bool setContext(const SkBitmap& device, const SkPaint& paint,
572 const SkMatrix& matrix) {
573 if (fProxy) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000574 return fProxy->setContext(device, paint, matrix);
reed@google.com82065d62011-02-07 15:30:46 +0000575 } else {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000576 fPMColor = SkPreMultiplyColor(paint.getColor());
577 return this->INHERITED::setContext(device, paint, matrix);
578 }
579 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000580
reed@google.com82065d62011-02-07 15:30:46 +0000581 virtual void shadeSpan(int x, int y, SkPMColor span[], int count) {
582 if (fProxy) {
583 fProxy->shadeSpan(x, y, span, count);
584 }
585
586 if (fMask == NULL) {
587 if (fProxy == NULL) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000588 sk_memset32(span, fPMColor, count);
reed@google.com82065d62011-02-07 15:30:46 +0000589 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000590 return;
591 }
592
593 SkASSERT(fMask->fBounds.contains(x, y));
594 SkASSERT(fMask->fBounds.contains(x + count - 1, y));
595
596 size_t size = fMask->computeImageSize();
reed@google.com79891862011-10-18 15:44:57 +0000597 const uint8_t* alpha = fMask->getAddr8(x, y);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000598 const uint8_t* mulp = alpha + size;
599 const uint8_t* addp = mulp + size;
600
reed@google.com82065d62011-02-07 15:30:46 +0000601 if (fProxy) {
602 for (int i = 0; i < count; i++) {
603 if (alpha[i]) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000604 SkPMColor c = span[i];
reed@google.com82065d62011-02-07 15:30:46 +0000605 if (c) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000606 unsigned a = SkGetPackedA32(c);
607 unsigned r = SkGetPackedR32(c);
608 unsigned g = SkGetPackedG32(c);
609 unsigned b = SkGetPackedB32(c);
610
611 unsigned mul = SkAlpha255To256(mulp[i]);
612 unsigned add = addp[i];
613
614 r = SkFastMin32(SkAlphaMul(r, mul) + add, a);
615 g = SkFastMin32(SkAlphaMul(g, mul) + add, a);
616 b = SkFastMin32(SkAlphaMul(b, mul) + add, a);
617
618 span[i] = SkPackARGB32(a, r, g, b);
619 }
reed@google.com82065d62011-02-07 15:30:46 +0000620 } else {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000621 span[i] = 0;
reed@google.com82065d62011-02-07 15:30:46 +0000622 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000623 }
reed@google.com82065d62011-02-07 15:30:46 +0000624 } else { // color
reed@android.com8a1c16f2008-12-17 15:59:43 +0000625 unsigned a = SkGetPackedA32(fPMColor);
626 unsigned r = SkGetPackedR32(fPMColor);
627 unsigned g = SkGetPackedG32(fPMColor);
628 unsigned b = SkGetPackedB32(fPMColor);
reed@google.com82065d62011-02-07 15:30:46 +0000629 for (int i = 0; i < count; i++) {
630 if (alpha[i]) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000631 unsigned mul = SkAlpha255To256(mulp[i]);
632 unsigned add = addp[i];
633
634 span[i] = SkPackARGB32( a,
reed@google.com82065d62011-02-07 15:30:46 +0000635 SkFastMin32(SkAlphaMul(r, mul) + add, a),
636 SkFastMin32(SkAlphaMul(g, mul) + add, a),
637 SkFastMin32(SkAlphaMul(b, mul) + add, a));
638 } else {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000639 span[i] = 0;
reed@google.com82065d62011-02-07 15:30:46 +0000640 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000641 }
642 }
643 }
reed@google.com82065d62011-02-07 15:30:46 +0000644
645 virtual void beginSession() {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000646 this->INHERITED::beginSession();
reed@google.com82065d62011-02-07 15:30:46 +0000647 if (fProxy) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000648 fProxy->beginSession();
reed@google.com82065d62011-02-07 15:30:46 +0000649 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000650 }
reed@google.com82065d62011-02-07 15:30:46 +0000651
652 virtual void endSession() {
653 if (fProxy) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000654 fProxy->endSession();
reed@google.com82065d62011-02-07 15:30:46 +0000655 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000656 this->INHERITED::endSession();
657 }
658
djsollen@google.comba28d032012-03-26 17:57:35 +0000659 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(Sk3DShader)
660
reed@android.com8a1c16f2008-12-17 15:59:43 +0000661protected:
djsollen@google.com54924242012-03-29 15:18:04 +0000662 Sk3DShader(SkFlattenableReadBuffer& buffer) : INHERITED(buffer) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000663 fProxy = static_cast<SkShader*>(buffer.readFlattenable());
664 fPMColor = buffer.readU32();
665 fMask = NULL;
666 }
reed@google.com82065d62011-02-07 15:30:46 +0000667
djsollen@google.com54924242012-03-29 15:18:04 +0000668 virtual void flatten(SkFlattenableWriteBuffer& buffer) const SK_OVERRIDE {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000669 this->INHERITED::flatten(buffer);
670 buffer.writeFlattenable(fProxy);
671 buffer.write32(fPMColor);
672 }
reed@google.com82065d62011-02-07 15:30:46 +0000673
reed@android.com8a1c16f2008-12-17 15:59:43 +0000674private:
reed@android.com8a1c16f2008-12-17 15:59:43 +0000675 SkShader* fProxy;
676 SkPMColor fPMColor;
677 const SkMask* fMask;
678
679 typedef SkShader INHERITED;
680};
681
682class Sk3DBlitter : public SkBlitter {
683public:
684 Sk3DBlitter(SkBlitter* proxy, Sk3DShader* shader, void (*killProc)(void*))
reed@google.com82065d62011-02-07 15:30:46 +0000685 : fProxy(proxy), f3DShader(shader), fKillProc(killProc) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000686 shader->ref();
687 }
reed@google.com82065d62011-02-07 15:30:46 +0000688
689 virtual ~Sk3DBlitter() {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000690 f3DShader->unref();
691 fKillProc(fProxy);
692 }
693
reed@google.com82065d62011-02-07 15:30:46 +0000694 virtual void blitH(int x, int y, int width) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000695 fProxy->blitH(x, y, width);
696 }
reed@google.com82065d62011-02-07 15:30:46 +0000697
698 virtual void blitAntiH(int x, int y, const SkAlpha antialias[],
699 const int16_t runs[]) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000700 fProxy->blitAntiH(x, y, antialias, runs);
701 }
reed@google.com82065d62011-02-07 15:30:46 +0000702
703 virtual void blitV(int x, int y, int height, SkAlpha alpha) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000704 fProxy->blitV(x, y, height, alpha);
705 }
reed@google.com82065d62011-02-07 15:30:46 +0000706
707 virtual void blitRect(int x, int y, int width, int height) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000708 fProxy->blitRect(x, y, width, height);
709 }
reed@google.com82065d62011-02-07 15:30:46 +0000710
711 virtual void blitMask(const SkMask& mask, const SkIRect& clip) {
712 if (mask.fFormat == SkMask::k3D_Format) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000713 f3DShader->setMask(&mask);
714
715 ((SkMask*)&mask)->fFormat = SkMask::kA8_Format;
716 fProxy->blitMask(mask, clip);
717 ((SkMask*)&mask)->fFormat = SkMask::k3D_Format;
718
719 f3DShader->setMask(NULL);
reed@google.com82065d62011-02-07 15:30:46 +0000720 } else {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000721 fProxy->blitMask(mask, clip);
reed@google.com82065d62011-02-07 15:30:46 +0000722 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000723 }
reed@google.com82065d62011-02-07 15:30:46 +0000724
reed@android.com8a1c16f2008-12-17 15:59:43 +0000725private:
726 SkBlitter* fProxy;
727 Sk3DShader* f3DShader;
728 void (*fKillProc)(void*);
729};
730
reed@google.com82065d62011-02-07 15:30:46 +0000731///////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +0000732
733#include "SkCoreBlitters.h"
734
reed@android.com8a1c16f2008-12-17 15:59:43 +0000735class SkAutoCallProc {
736public:
737 typedef void (*Proc)(void*);
reed@google.com82065d62011-02-07 15:30:46 +0000738
reed@android.com8a1c16f2008-12-17 15:59:43 +0000739 SkAutoCallProc(void* obj, Proc proc)
reed@google.com82065d62011-02-07 15:30:46 +0000740 : fObj(obj), fProc(proc) {}
741
742 ~SkAutoCallProc() {
743 if (fObj && fProc) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000744 fProc(fObj);
reed@google.com82065d62011-02-07 15:30:46 +0000745 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000746 }
reed@google.com82065d62011-02-07 15:30:46 +0000747
reed@android.com8a1c16f2008-12-17 15:59:43 +0000748 void* get() const { return fObj; }
reed@google.com82065d62011-02-07 15:30:46 +0000749
750 void* detach() {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000751 void* obj = fObj;
752 fObj = NULL;
753 return obj;
754 }
reed@google.com82065d62011-02-07 15:30:46 +0000755
reed@android.com8a1c16f2008-12-17 15:59:43 +0000756private:
757 void* fObj;
758 Proc fProc;
759};
760
reed@google.com82065d62011-02-07 15:30:46 +0000761static void destroy_blitter(void* blitter) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000762 ((SkBlitter*)blitter)->~SkBlitter();
763}
764
reed@google.com82065d62011-02-07 15:30:46 +0000765static void delete_blitter(void* blitter) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000766 SkDELETE((SkBlitter*)blitter);
767}
768
reed@android.comd252db02009-04-01 18:31:44 +0000769static bool just_solid_color(const SkPaint& paint) {
770 if (paint.getAlpha() == 0xFF && paint.getColorFilter() == NULL) {
771 SkShader* shader = paint.getShader();
772 if (NULL == shader ||
773 (shader->getFlags() & SkShader::kOpaqueAlpha_Flag)) {
774 return true;
775 }
776 }
777 return false;
778}
reed@google.com82065d62011-02-07 15:30:46 +0000779
reed@android.comd252db02009-04-01 18:31:44 +0000780/** By analyzing the paint (with an xfermode), we may decide we can take
781 special action. This enum lists our possible actions
782 */
783enum XferInterp {
784 kNormal_XferInterp, // no special interpretation, draw normally
785 kSrcOver_XferInterp, // draw as if in srcover mode
786 kSkipDrawing_XferInterp // draw nothing
787};
788
789static XferInterp interpret_xfermode(const SkPaint& paint, SkXfermode* xfer,
790 SkBitmap::Config deviceConfig) {
reed@android.com845fdac2009-06-23 03:01:32 +0000791 SkXfermode::Mode mode;
reed@google.com82065d62011-02-07 15:30:46 +0000792
mike@reedtribe.orgbe2aa2a2011-11-17 02:32:04 +0000793 if (SkXfermode::AsMode(xfer, &mode)) {
reed@android.comd252db02009-04-01 18:31:44 +0000794 switch (mode) {
reed@android.com845fdac2009-06-23 03:01:32 +0000795 case SkXfermode::kSrc_Mode:
reed@android.comd252db02009-04-01 18:31:44 +0000796 if (just_solid_color(paint)) {
797 return kSrcOver_XferInterp;
798 }
799 break;
reed@android.com845fdac2009-06-23 03:01:32 +0000800 case SkXfermode::kDst_Mode:
reed@android.comd252db02009-04-01 18:31:44 +0000801 return kSkipDrawing_XferInterp;
reed@android.com845fdac2009-06-23 03:01:32 +0000802 case SkXfermode::kSrcOver_Mode:
reed@android.comd252db02009-04-01 18:31:44 +0000803 return kSrcOver_XferInterp;
reed@android.com845fdac2009-06-23 03:01:32 +0000804 case SkXfermode::kDstOver_Mode:
reed@android.comd252db02009-04-01 18:31:44 +0000805 if (SkBitmap::kRGB_565_Config == deviceConfig) {
806 return kSkipDrawing_XferInterp;
807 }
808 break;
reed@android.com845fdac2009-06-23 03:01:32 +0000809 case SkXfermode::kSrcIn_Mode:
reed@android.comd252db02009-04-01 18:31:44 +0000810 if (SkBitmap::kRGB_565_Config == deviceConfig &&
811 just_solid_color(paint)) {
812 return kSrcOver_XferInterp;
813 }
814 break;
reed@android.com845fdac2009-06-23 03:01:32 +0000815 case SkXfermode::kDstIn_Mode:
reed@android.comd252db02009-04-01 18:31:44 +0000816 if (just_solid_color(paint)) {
817 return kSkipDrawing_XferInterp;
818 }
819 break;
820 default:
821 break;
822 }
823 }
824 return kNormal_XferInterp;
825}
826
reed@android.com8a1c16f2008-12-17 15:59:43 +0000827SkBlitter* SkBlitter::Choose(const SkBitmap& device,
828 const SkMatrix& matrix,
reed@google.com6b7aee32011-04-19 18:36:09 +0000829 const SkPaint& origPaint,
reed@google.com82065d62011-02-07 15:30:46 +0000830 void* storage, size_t storageSize) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000831 SkASSERT(storageSize == 0 || storage != NULL);
832
833 SkBlitter* blitter = NULL;
834
835 // which check, in case we're being called by a client with a dummy device
836 // (e.g. they have a bounder that always aborts the draw)
reed@google.com82065d62011-02-07 15:30:46 +0000837 if (SkBitmap::kNo_Config == device.getConfig()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000838 SK_PLACEMENT_NEW(blitter, SkNullBlitter, storage, storageSize);
839 return blitter;
840 }
841
reed@google.com6b7aee32011-04-19 18:36:09 +0000842 SkPaint paint(origPaint);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000843 SkShader* shader = paint.getShader();
reed@google.com6b7aee32011-04-19 18:36:09 +0000844 SkColorFilter* cf = paint.getColorFilter();
845 SkXfermode* mode = paint.getXfermode();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000846
847 Sk3DShader* shader3D = NULL;
reed@google.com82065d62011-02-07 15:30:46 +0000848 if (paint.getMaskFilter() != NULL &&
849 paint.getMaskFilter()->getFormat() == SkMask::k3D_Format) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000850 shader3D = SkNEW_ARGS(Sk3DShader, (shader));
reed@google.com6b7aee32011-04-19 18:36:09 +0000851 paint.setShader(shader3D)->unref();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000852 shader = shader3D;
853 }
854
reed@android.comd252db02009-04-01 18:31:44 +0000855 if (NULL != mode) {
856 switch (interpret_xfermode(paint, mode, device.config())) {
857 case kSrcOver_XferInterp:
858 mode = NULL;
reed@google.com6b7aee32011-04-19 18:36:09 +0000859 paint.setXfermode(NULL);
reed@android.comd252db02009-04-01 18:31:44 +0000860 break;
861 case kSkipDrawing_XferInterp:
862 SK_PLACEMENT_NEW(blitter, SkNullBlitter, storage, storageSize);
863 return blitter;
864 default:
865 break;
866 }
867 }
868
reed@google.com6b7aee32011-04-19 18:36:09 +0000869 if (NULL == shader) {
870#ifdef SK_IGNORE_CF_OPTIMIZATION
871 if (mode || cf) {
872#else
873 if (mode) {
874#endif
875 // xfermodes (and filters) require shaders for our current blitters
876 shader = SkNEW(SkColorShader);
877 paint.setShader(shader)->unref();
878 } else if (cf) {
879 // if no shader && no xfermode, we just apply the colorfilter to
880 // our color and move on.
881 paint.setColor(cf->filterColor(paint.getColor()));
882 paint.setColorFilter(NULL);
883 cf = NULL;
884 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000885 }
reed@google.com82065d62011-02-07 15:30:46 +0000886
reed@google.com6b7aee32011-04-19 18:36:09 +0000887 if (cf) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000888 SkASSERT(shader);
reed@google.com6b7aee32011-04-19 18:36:09 +0000889 shader = SkNEW_ARGS(SkFilterShader, (shader, cf));
890 paint.setShader(shader)->unref();
reed@android.com1fc4c602009-10-02 16:34:57 +0000891 // blitters should ignore the presence/absence of a filter, since
892 // if there is one, the shader will take care of it.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000893 }
reed@google.com82065d62011-02-07 15:30:46 +0000894
reed@android.comcafc9f92009-08-22 03:44:57 +0000895 if (shader && !shader->setContext(device, paint, matrix)) {
896 return SkNEW(SkNullBlitter);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000897 }
898
899 switch (device.getConfig()) {
reed@google.com82065d62011-02-07 15:30:46 +0000900 case SkBitmap::kA1_Config:
901 SK_PLACEMENT_NEW_ARGS(blitter, SkA1_Blitter,
902 storage, storageSize, (device, paint));
903 break;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000904
reed@google.com82065d62011-02-07 15:30:46 +0000905 case SkBitmap::kA8_Config:
906 if (shader) {
907 SK_PLACEMENT_NEW_ARGS(blitter, SkA8_Shader_Blitter,
908 storage, storageSize, (device, paint));
909 } else {
910 SK_PLACEMENT_NEW_ARGS(blitter, SkA8_Blitter,
911 storage, storageSize, (device, paint));
912 }
913 break;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000914
reed@google.com82065d62011-02-07 15:30:46 +0000915 case SkBitmap::kARGB_4444_Config:
916 blitter = SkBlitter_ChooseD4444(device, paint, storage, storageSize);
917 break;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000918
reed@google.com82065d62011-02-07 15:30:46 +0000919 case SkBitmap::kRGB_565_Config:
920 blitter = SkBlitter_ChooseD565(device, paint, storage, storageSize);
921 break;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000922
reed@google.com82065d62011-02-07 15:30:46 +0000923 case SkBitmap::kARGB_8888_Config:
924 if (shader) {
925 SK_PLACEMENT_NEW_ARGS(blitter, SkARGB32_Shader_Blitter,
926 storage, storageSize, (device, paint));
927 } else if (paint.getColor() == SK_ColorBLACK) {
928 SK_PLACEMENT_NEW_ARGS(blitter, SkARGB32_Black_Blitter,
929 storage, storageSize, (device, paint));
930 } else if (paint.getAlpha() == 0xFF) {
931 SK_PLACEMENT_NEW_ARGS(blitter, SkARGB32_Opaque_Blitter,
932 storage, storageSize, (device, paint));
933 } else {
934 SK_PLACEMENT_NEW_ARGS(blitter, SkARGB32_Blitter,
935 storage, storageSize, (device, paint));
936 }
937 break;
938
939 default:
tomhudson@google.com0c00f212011-12-28 14:59:50 +0000940 SkDEBUGFAIL("unsupported device config");
reed@google.com82065d62011-02-07 15:30:46 +0000941 SK_PLACEMENT_NEW(blitter, SkNullBlitter, storage, storageSize);
942 break;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000943 }
944
reed@google.com82065d62011-02-07 15:30:46 +0000945 if (shader3D) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000946 void (*proc)(void*) = ((void*)storage == (void*)blitter) ? destroy_blitter : delete_blitter;
947 SkAutoCallProc tmp(blitter, proc);
948
949 blitter = SkNEW_ARGS(Sk3DBlitter, (blitter, shader3D, proc));
950 (void)tmp.detach();
951 }
952 return blitter;
953}
954
reed@google.com82065d62011-02-07 15:30:46 +0000955///////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +0000956
957const uint16_t gMask_0F0F = 0xF0F;
958const uint32_t gMask_00FF00FF = 0xFF00FF;
959
reed@google.com82065d62011-02-07 15:30:46 +0000960///////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +0000961
962SkShaderBlitter::SkShaderBlitter(const SkBitmap& device, const SkPaint& paint)
reed@android.com5119bdb2009-06-12 21:27:03 +0000963 : INHERITED(device) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000964 fShader = paint.getShader();
965 SkASSERT(fShader);
966
967 fShader->ref();
968 fShader->beginSession();
reed@android.com5119bdb2009-06-12 21:27:03 +0000969 fShaderFlags = fShader->getFlags();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000970}
971
reed@android.com5119bdb2009-06-12 21:27:03 +0000972SkShaderBlitter::~SkShaderBlitter() {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000973 fShader->endSession();
974 fShader->unref();
975}
976