blob: 307d51b340904c5b495c2403f873c80cbb1a8775 [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
55//////////////////////////////////////////////////////////////////////////////
56
reed@google.com82065d62011-02-07 15:30:46 +000057static inline void bits_to_runs(SkBlitter* blitter, int x, int y,
58 const uint8_t bits[],
59 U8CPU left_mask, int rowBytes,
60 U8CPU right_mask) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000061 int inFill = 0;
62 int pos = 0;
63
reed@google.com82065d62011-02-07 15:30:46 +000064 while (--rowBytes >= 0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000065 unsigned b = *bits++ & left_mask;
reed@google.com82065d62011-02-07 15:30:46 +000066 if (rowBytes == 0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000067 b &= right_mask;
reed@google.com82065d62011-02-07 15:30:46 +000068 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000069
reed@google.com82065d62011-02-07 15:30:46 +000070 for (unsigned test = 0x80; test != 0; test >>= 1) {
71 if (b & test) {
72 if (!inFill) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000073 pos = x;
74 inFill = true;
75 }
reed@google.com82065d62011-02-07 15:30:46 +000076 } else {
77 if (inFill) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000078 blitter->blitH(pos, y, x - pos);
79 inFill = false;
80 }
81 }
82 x += 1;
83 }
84 left_mask = 0xFF;
85 }
86
87 // final cleanup
reed@google.com82065d62011-02-07 15:30:46 +000088 if (inFill) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000089 blitter->blitH(pos, y, x - pos);
reed@google.com82065d62011-02-07 15:30:46 +000090 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000091}
92
reed@google.com82065d62011-02-07 15:30:46 +000093void SkBlitter::blitMask(const SkMask& mask, const SkIRect& clip) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000094 SkASSERT(mask.fBounds.contains(clip));
95
reed@google.com82065d62011-02-07 15:30:46 +000096 if (mask.fFormat == SkMask::kBW_Format) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000097 int cx = clip.fLeft;
98 int cy = clip.fTop;
99 int maskLeft = mask.fBounds.fLeft;
100 int mask_rowBytes = mask.fRowBytes;
101 int height = clip.height();
102
103 const uint8_t* bits = mask.getAddr1(cx, cy);
104
reed@google.com82065d62011-02-07 15:30:46 +0000105 if (cx == maskLeft && clip.fRight == mask.fBounds.fRight) {
106 while (--height >= 0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000107 bits_to_runs(this, cx, cy, bits, 0xFF, mask_rowBytes, 0xFF);
108 bits += mask_rowBytes;
109 cy += 1;
110 }
reed@google.com82065d62011-02-07 15:30:46 +0000111 } else {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000112 int left_edge = cx - maskLeft;
113 SkASSERT(left_edge >= 0);
114 int rite_edge = clip.fRight - maskLeft;
115 SkASSERT(rite_edge > left_edge);
116
117 int left_mask = 0xFF >> (left_edge & 7);
118 int rite_mask = 0xFF << (8 - (rite_edge & 7));
119 int full_runs = (rite_edge >> 3) - ((left_edge + 7) >> 3);
120
reed@google.coma89c77b2011-12-01 21:47:26 +0000121 // 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 +0000122 if (rite_mask == 0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000123 SkASSERT(full_runs >= 0);
124 full_runs -= 1;
125 rite_mask = 0xFF;
126 }
reed@google.com82065d62011-02-07 15:30:46 +0000127 if (left_mask == 0xFF) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000128 full_runs -= 1;
reed@google.com82065d62011-02-07 15:30:46 +0000129 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000130
reed@google.coma89c77b2011-12-01 21:47:26 +0000131 // back up manually so we can keep in sync with our byte-aligned src
132 // have cx reflect our actual starting x-coord
reed@android.com8a1c16f2008-12-17 15:59:43 +0000133 cx -= left_edge & 7;
134
reed@google.com82065d62011-02-07 15:30:46 +0000135 if (full_runs < 0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000136 SkASSERT((left_mask & rite_mask) != 0);
reed@google.com82065d62011-02-07 15:30:46 +0000137 while (--height >= 0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000138 bits_to_runs(this, cx, cy, bits, left_mask, 1, rite_mask);
139 bits += mask_rowBytes;
140 cy += 1;
141 }
reed@google.com82065d62011-02-07 15:30:46 +0000142 } else {
143 while (--height >= 0) {
reed@google.coma89c77b2011-12-01 21:47:26 +0000144 bits_to_runs(this, cx, cy, bits, left_mask, full_runs + 2, rite_mask);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000145 bits += mask_rowBytes;
146 cy += 1;
147 }
148 }
149 }
reed@google.com82065d62011-02-07 15:30:46 +0000150 } else {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000151 int width = clip.width();
152 SkAutoSTMalloc<64, int16_t> runStorage(width + 1);
153 int16_t* runs = runStorage.get();
reed@google.com79891862011-10-18 15:44:57 +0000154 const uint8_t* aa = mask.getAddr8(clip.fLeft, clip.fTop);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000155
156 sk_memset16((uint16_t*)runs, 1, width);
157 runs[width] = 0;
158
159 int height = clip.height();
160 int y = clip.fTop;
reed@google.com82065d62011-02-07 15:30:46 +0000161 while (--height >= 0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000162 this->blitAntiH(clip.fLeft, y, aa, runs);
163 aa += mask.fRowBytes;
164 y += 1;
165 }
166 }
167}
168
169/////////////////////// these guys are not virtual, just a helpers
170
171void SkBlitter::blitMaskRegion(const SkMask& mask, const SkRegion& clip) {
172 if (clip.quickReject(mask.fBounds)) {
173 return;
174 }
reed@google.com82065d62011-02-07 15:30:46 +0000175
reed@android.com8a1c16f2008-12-17 15:59:43 +0000176 SkRegion::Cliperator clipper(clip, mask.fBounds);
reed@google.com82065d62011-02-07 15:30:46 +0000177
reed@android.com8a1c16f2008-12-17 15:59:43 +0000178 while (!clipper.done()) {
179 const SkIRect& cr = clipper.rect();
180 this->blitMask(mask, cr);
181 clipper.next();
182 }
183}
184
185void SkBlitter::blitRectRegion(const SkIRect& rect, const SkRegion& clip) {
186 SkRegion::Cliperator clipper(clip, rect);
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->blitRect(cr.fLeft, cr.fTop, cr.width(), cr.height());
191 clipper.next();
192 }
193}
194
195void SkBlitter::blitRegion(const SkRegion& clip) {
196 SkRegion::Iterator iter(clip);
reed@google.com82065d62011-02-07 15:30:46 +0000197
reed@android.com8a1c16f2008-12-17 15:59:43 +0000198 while (!iter.done()) {
199 const SkIRect& cr = iter.rect();
200 this->blitRect(cr.fLeft, cr.fTop, cr.width(), cr.height());
201 iter.next();
202 }
203}
204
reed@google.com82065d62011-02-07 15:30:46 +0000205///////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +0000206
reed@google.com82065d62011-02-07 15:30:46 +0000207void SkNullBlitter::blitH(int x, int y, int width) {}
reed@android.com8a1c16f2008-12-17 15:59:43 +0000208
reed@google.com82065d62011-02-07 15:30:46 +0000209void SkNullBlitter::blitAntiH(int x, int y, const SkAlpha antialias[],
210 const int16_t runs[]) {}
reed@android.com8a1c16f2008-12-17 15:59:43 +0000211
reed@google.com82065d62011-02-07 15:30:46 +0000212void SkNullBlitter::blitV(int x, int y, int height, SkAlpha alpha) {}
reed@android.com8a1c16f2008-12-17 15:59:43 +0000213
reed@google.com82065d62011-02-07 15:30:46 +0000214void SkNullBlitter::blitRect(int x, int y, int width, int height) {}
reed@android.com8a1c16f2008-12-17 15:59:43 +0000215
reed@google.com82065d62011-02-07 15:30:46 +0000216void SkNullBlitter::blitMask(const SkMask& mask, const SkIRect& clip) {}
reed@android.com8a1c16f2008-12-17 15:59:43 +0000217
reed@google.com82065d62011-02-07 15:30:46 +0000218const SkBitmap* SkNullBlitter::justAnOpaqueColor(uint32_t* value) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000219 return NULL;
220}
221
reed@google.com82065d62011-02-07 15:30:46 +0000222///////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +0000223
reed@google.com82065d62011-02-07 15:30:46 +0000224static int compute_anti_width(const int16_t runs[]) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000225 int width = 0;
reed@google.com82065d62011-02-07 15:30:46 +0000226
227 for (;;) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000228 int count = runs[0];
reed@google.com82065d62011-02-07 15:30:46 +0000229
reed@android.com8a1c16f2008-12-17 15:59:43 +0000230 SkASSERT(count >= 0);
reed@google.com82065d62011-02-07 15:30:46 +0000231 if (count == 0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000232 break;
reed@google.com82065d62011-02-07 15:30:46 +0000233 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000234 width += count;
235 runs += count;
reed@google.com82065d62011-02-07 15:30:46 +0000236
reed@android.com8a1c16f2008-12-17 15:59:43 +0000237 SkASSERT(width < 20000);
238 }
239 return width;
240}
241
reed@google.com82065d62011-02-07 15:30:46 +0000242static inline bool y_in_rect(int y, const SkIRect& rect) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000243 return (unsigned)(y - rect.fTop) < (unsigned)rect.height();
244}
245
reed@google.com82065d62011-02-07 15:30:46 +0000246static inline bool x_in_rect(int x, const SkIRect& rect) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000247 return (unsigned)(x - rect.fLeft) < (unsigned)rect.width();
248}
249
reed@google.com82065d62011-02-07 15:30:46 +0000250void SkRectClipBlitter::blitH(int left, int y, int width) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000251 SkASSERT(width > 0);
252
reed@google.com82065d62011-02-07 15:30:46 +0000253 if (!y_in_rect(y, fClipRect)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000254 return;
reed@google.com82065d62011-02-07 15:30:46 +0000255 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000256
257 int right = left + width;
258
reed@google.com82065d62011-02-07 15:30:46 +0000259 if (left < fClipRect.fLeft) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000260 left = fClipRect.fLeft;
reed@google.com82065d62011-02-07 15:30:46 +0000261 }
262 if (right > fClipRect.fRight) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000263 right = fClipRect.fRight;
reed@google.com82065d62011-02-07 15:30:46 +0000264 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000265
266 width = right - left;
reed@google.com82065d62011-02-07 15:30:46 +0000267 if (width > 0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000268 fBlitter->blitH(left, y, width);
reed@google.com82065d62011-02-07 15:30:46 +0000269 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000270}
271
reed@google.com82065d62011-02-07 15:30:46 +0000272void SkRectClipBlitter::blitAntiH(int left, int y, const SkAlpha aa[],
273 const int16_t runs[]) {
274 if (!y_in_rect(y, fClipRect) || left >= fClipRect.fRight) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000275 return;
reed@google.com82065d62011-02-07 15:30:46 +0000276 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000277
278 int x0 = left;
279 int x1 = left + compute_anti_width(runs);
280
reed@google.com82065d62011-02-07 15:30:46 +0000281 if (x1 <= fClipRect.fLeft) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000282 return;
reed@google.com82065d62011-02-07 15:30:46 +0000283 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000284
285 SkASSERT(x0 < x1);
reed@google.com82065d62011-02-07 15:30:46 +0000286 if (x0 < fClipRect.fLeft) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000287 int dx = fClipRect.fLeft - x0;
288 SkAlphaRuns::BreakAt((int16_t*)runs, (uint8_t*)aa, dx);
289 runs += dx;
290 aa += dx;
291 x0 = fClipRect.fLeft;
292 }
293
294 SkASSERT(x0 < x1 && runs[x1 - x0] == 0);
reed@google.com82065d62011-02-07 15:30:46 +0000295 if (x1 > fClipRect.fRight) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000296 x1 = fClipRect.fRight;
297 SkAlphaRuns::BreakAt((int16_t*)runs, (uint8_t*)aa, x1 - x0);
298 ((int16_t*)runs)[x1 - x0] = 0;
299 }
300
301 SkASSERT(x0 < x1 && runs[x1 - x0] == 0);
302 SkASSERT(compute_anti_width(runs) == x1 - x0);
303
304 fBlitter->blitAntiH(x0, y, aa, runs);
305}
306
reed@google.com82065d62011-02-07 15:30:46 +0000307void SkRectClipBlitter::blitV(int x, int y, int height, SkAlpha alpha) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000308 SkASSERT(height > 0);
309
reed@google.com82065d62011-02-07 15:30:46 +0000310 if (!x_in_rect(x, fClipRect)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000311 return;
reed@google.com82065d62011-02-07 15:30:46 +0000312 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000313
314 int y0 = y;
315 int y1 = y + height;
316
reed@google.com82065d62011-02-07 15:30:46 +0000317 if (y0 < fClipRect.fTop) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000318 y0 = fClipRect.fTop;
reed@google.com82065d62011-02-07 15:30:46 +0000319 }
320 if (y1 > fClipRect.fBottom) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000321 y1 = fClipRect.fBottom;
reed@google.com82065d62011-02-07 15:30:46 +0000322 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000323
reed@google.com82065d62011-02-07 15:30:46 +0000324 if (y0 < y1) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000325 fBlitter->blitV(x, y0, y1 - y0, alpha);
reed@google.com82065d62011-02-07 15:30:46 +0000326 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000327}
328
reed@google.com82065d62011-02-07 15:30:46 +0000329void SkRectClipBlitter::blitRect(int left, int y, int width, int height) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000330 SkIRect r;
331
332 r.set(left, y, left + width, y + height);
reed@google.com82065d62011-02-07 15:30:46 +0000333 if (r.intersect(fClipRect)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000334 fBlitter->blitRect(r.fLeft, r.fTop, r.width(), r.height());
reed@google.com82065d62011-02-07 15:30:46 +0000335 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000336}
337
reed@google.com82065d62011-02-07 15:30:46 +0000338void SkRectClipBlitter::blitMask(const SkMask& mask, const SkIRect& clip) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000339 SkASSERT(mask.fBounds.contains(clip));
340
341 SkIRect r = clip;
342
reed@google.com82065d62011-02-07 15:30:46 +0000343 if (r.intersect(fClipRect)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000344 fBlitter->blitMask(mask, r);
reed@google.com82065d62011-02-07 15:30:46 +0000345 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000346}
347
reed@google.com82065d62011-02-07 15:30:46 +0000348const SkBitmap* SkRectClipBlitter::justAnOpaqueColor(uint32_t* value) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000349 return fBlitter->justAnOpaqueColor(value);
350}
351
reed@google.com82065d62011-02-07 15:30:46 +0000352///////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +0000353
reed@google.com82065d62011-02-07 15:30:46 +0000354void SkRgnClipBlitter::blitH(int x, int y, int width) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000355 SkRegion::Spanerator span(*fRgn, y, x, x + width);
356 int left, right;
357
reed@google.com82065d62011-02-07 15:30:46 +0000358 while (span.next(&left, &right)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000359 SkASSERT(left < right);
360 fBlitter->blitH(left, y, right - left);
361 }
362}
363
reed@google.com82065d62011-02-07 15:30:46 +0000364void SkRgnClipBlitter::blitAntiH(int x, int y, const SkAlpha aa[],
365 const int16_t runs[]) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000366 int width = compute_anti_width(runs);
367 SkRegion::Spanerator span(*fRgn, y, x, x + width);
368 int left, right;
369 SkDEBUGCODE(const SkIRect& bounds = fRgn->getBounds();)
reed@google.com82065d62011-02-07 15:30:46 +0000370
reed@android.com8a1c16f2008-12-17 15:59:43 +0000371 int prevRite = x;
reed@google.com82065d62011-02-07 15:30:46 +0000372 while (span.next(&left, &right)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000373 SkASSERT(x <= left);
374 SkASSERT(left < right);
375 SkASSERT(left >= bounds.fLeft && right <= bounds.fRight);
reed@google.com82065d62011-02-07 15:30:46 +0000376
reed@android.com8a1c16f2008-12-17 15:59:43 +0000377 SkAlphaRuns::Break((int16_t*)runs, (uint8_t*)aa, left - x, right - left);
378
379 // now zero before left
reed@google.com82065d62011-02-07 15:30:46 +0000380 if (left > prevRite) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000381 int index = prevRite - x;
382 ((uint8_t*)aa)[index] = 0; // skip runs after right
383 ((int16_t*)runs)[index] = SkToS16(left - prevRite);
384 }
reed@google.com82065d62011-02-07 15:30:46 +0000385
reed@android.com8a1c16f2008-12-17 15:59:43 +0000386 prevRite = right;
387 }
reed@google.com82065d62011-02-07 15:30:46 +0000388
389 if (prevRite > x) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000390 ((int16_t*)runs)[prevRite - x] = 0;
reed@google.com82065d62011-02-07 15:30:46 +0000391
reed@android.com8a1c16f2008-12-17 15:59:43 +0000392 if (x < 0) {
393 int skip = runs[0];
394 SkASSERT(skip >= -x);
395 aa += skip;
396 runs += skip;
397 x += skip;
398 }
399 fBlitter->blitAntiH(x, y, aa, runs);
400 }
401}
402
reed@google.com82065d62011-02-07 15:30:46 +0000403void SkRgnClipBlitter::blitV(int x, int y, int height, SkAlpha alpha) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000404 SkIRect bounds;
405 bounds.set(x, y, x + 1, y + height);
406
407 SkRegion::Cliperator iter(*fRgn, bounds);
408
reed@google.com82065d62011-02-07 15:30:46 +0000409 while (!iter.done()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000410 const SkIRect& r = iter.rect();
411 SkASSERT(bounds.contains(r));
412
413 fBlitter->blitV(x, r.fTop, r.height(), alpha);
414 iter.next();
415 }
416}
417
reed@google.com82065d62011-02-07 15:30:46 +0000418void SkRgnClipBlitter::blitRect(int x, int y, int width, int height) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000419 SkIRect bounds;
420 bounds.set(x, y, x + width, y + height);
421
422 SkRegion::Cliperator iter(*fRgn, bounds);
423
reed@google.com82065d62011-02-07 15:30:46 +0000424 while (!iter.done()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000425 const SkIRect& r = iter.rect();
426 SkASSERT(bounds.contains(r));
427
428 fBlitter->blitRect(r.fLeft, r.fTop, r.width(), r.height());
429 iter.next();
430 }
431}
432
reed@google.com82065d62011-02-07 15:30:46 +0000433void SkRgnClipBlitter::blitMask(const SkMask& mask, const SkIRect& clip) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000434 SkASSERT(mask.fBounds.contains(clip));
435
436 SkRegion::Cliperator iter(*fRgn, clip);
437 const SkIRect& r = iter.rect();
438 SkBlitter* blitter = fBlitter;
439
reed@google.com82065d62011-02-07 15:30:46 +0000440 while (!iter.done()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000441 blitter->blitMask(mask, r);
442 iter.next();
443 }
444}
445
reed@google.com82065d62011-02-07 15:30:46 +0000446const SkBitmap* SkRgnClipBlitter::justAnOpaqueColor(uint32_t* value) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000447 return fBlitter->justAnOpaqueColor(value);
448}
449
reed@google.com82065d62011-02-07 15:30:46 +0000450///////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +0000451
reed@google.com82065d62011-02-07 15:30:46 +0000452SkBlitter* SkBlitterClipper::apply(SkBlitter* blitter, const SkRegion* clip,
453 const SkIRect* ir) {
454 if (clip) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000455 const SkIRect& clipR = clip->getBounds();
456
reed@google.com82065d62011-02-07 15:30:46 +0000457 if (clip->isEmpty() || (ir && !SkIRect::Intersects(clipR, *ir))) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000458 blitter = &fNullBlitter;
reed@google.com82065d62011-02-07 15:30:46 +0000459 } else if (clip->isRect()) {
460 if (ir == NULL || !clipR.contains(*ir)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000461 fRectBlitter.init(blitter, clipR);
462 blitter = &fRectBlitter;
463 }
reed@google.com82065d62011-02-07 15:30:46 +0000464 } else {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000465 fRgnBlitter.init(blitter, clip);
466 blitter = &fRgnBlitter;
467 }
468 }
469 return blitter;
470}
471
reed@google.com82065d62011-02-07 15:30:46 +0000472///////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +0000473
474#include "SkColorShader.h"
475#include "SkColorPriv.h"
476
477class Sk3DShader : public SkShader {
478public:
reed@google.com82065d62011-02-07 15:30:46 +0000479 Sk3DShader(SkShader* proxy) : fProxy(proxy) {
480 SkSafeRef(proxy);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000481 fMask = NULL;
482 }
reed@google.com82065d62011-02-07 15:30:46 +0000483
484 virtual ~Sk3DShader() {
485 SkSafeUnref(fProxy);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000486 }
reed@google.com82065d62011-02-07 15:30:46 +0000487
reed@android.com8a1c16f2008-12-17 15:59:43 +0000488 void setMask(const SkMask* mask) { fMask = mask; }
489
reed@google.com82065d62011-02-07 15:30:46 +0000490 virtual bool setContext(const SkBitmap& device, const SkPaint& paint,
491 const SkMatrix& matrix) {
492 if (fProxy) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000493 return fProxy->setContext(device, paint, matrix);
reed@google.com82065d62011-02-07 15:30:46 +0000494 } else {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000495 fPMColor = SkPreMultiplyColor(paint.getColor());
496 return this->INHERITED::setContext(device, paint, matrix);
497 }
498 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000499
reed@google.com82065d62011-02-07 15:30:46 +0000500 virtual void shadeSpan(int x, int y, SkPMColor span[], int count) {
501 if (fProxy) {
502 fProxy->shadeSpan(x, y, span, count);
503 }
504
505 if (fMask == NULL) {
506 if (fProxy == NULL) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000507 sk_memset32(span, fPMColor, count);
reed@google.com82065d62011-02-07 15:30:46 +0000508 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000509 return;
510 }
511
512 SkASSERT(fMask->fBounds.contains(x, y));
513 SkASSERT(fMask->fBounds.contains(x + count - 1, y));
514
515 size_t size = fMask->computeImageSize();
reed@google.com79891862011-10-18 15:44:57 +0000516 const uint8_t* alpha = fMask->getAddr8(x, y);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000517 const uint8_t* mulp = alpha + size;
518 const uint8_t* addp = mulp + size;
519
reed@google.com82065d62011-02-07 15:30:46 +0000520 if (fProxy) {
521 for (int i = 0; i < count; i++) {
522 if (alpha[i]) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000523 SkPMColor c = span[i];
reed@google.com82065d62011-02-07 15:30:46 +0000524 if (c) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000525 unsigned a = SkGetPackedA32(c);
526 unsigned r = SkGetPackedR32(c);
527 unsigned g = SkGetPackedG32(c);
528 unsigned b = SkGetPackedB32(c);
529
530 unsigned mul = SkAlpha255To256(mulp[i]);
531 unsigned add = addp[i];
532
533 r = SkFastMin32(SkAlphaMul(r, mul) + add, a);
534 g = SkFastMin32(SkAlphaMul(g, mul) + add, a);
535 b = SkFastMin32(SkAlphaMul(b, mul) + add, a);
536
537 span[i] = SkPackARGB32(a, r, g, b);
538 }
reed@google.com82065d62011-02-07 15:30:46 +0000539 } else {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000540 span[i] = 0;
reed@google.com82065d62011-02-07 15:30:46 +0000541 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000542 }
reed@google.com82065d62011-02-07 15:30:46 +0000543 } else { // color
reed@android.com8a1c16f2008-12-17 15:59:43 +0000544 unsigned a = SkGetPackedA32(fPMColor);
545 unsigned r = SkGetPackedR32(fPMColor);
546 unsigned g = SkGetPackedG32(fPMColor);
547 unsigned b = SkGetPackedB32(fPMColor);
reed@google.com82065d62011-02-07 15:30:46 +0000548 for (int i = 0; i < count; i++) {
549 if (alpha[i]) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000550 unsigned mul = SkAlpha255To256(mulp[i]);
551 unsigned add = addp[i];
552
553 span[i] = SkPackARGB32( a,
reed@google.com82065d62011-02-07 15:30:46 +0000554 SkFastMin32(SkAlphaMul(r, mul) + add, a),
555 SkFastMin32(SkAlphaMul(g, mul) + add, a),
556 SkFastMin32(SkAlphaMul(b, mul) + add, a));
557 } else {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000558 span[i] = 0;
reed@google.com82065d62011-02-07 15:30:46 +0000559 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000560 }
561 }
562 }
reed@google.com82065d62011-02-07 15:30:46 +0000563
564 virtual void beginSession() {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000565 this->INHERITED::beginSession();
reed@google.com82065d62011-02-07 15:30:46 +0000566 if (fProxy) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000567 fProxy->beginSession();
reed@google.com82065d62011-02-07 15:30:46 +0000568 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000569 }
reed@google.com82065d62011-02-07 15:30:46 +0000570
571 virtual void endSession() {
572 if (fProxy) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000573 fProxy->endSession();
reed@google.com82065d62011-02-07 15:30:46 +0000574 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000575 this->INHERITED::endSession();
576 }
577
578protected:
579 Sk3DShader(SkFlattenableReadBuffer& buffer) :
reed@google.com82065d62011-02-07 15:30:46 +0000580 INHERITED(buffer) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000581 fProxy = static_cast<SkShader*>(buffer.readFlattenable());
582 fPMColor = buffer.readU32();
583 fMask = NULL;
584 }
reed@google.com82065d62011-02-07 15:30:46 +0000585
586 virtual void flatten(SkFlattenableWriteBuffer& buffer) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000587 this->INHERITED::flatten(buffer);
588 buffer.writeFlattenable(fProxy);
589 buffer.write32(fPMColor);
590 }
reed@google.com82065d62011-02-07 15:30:46 +0000591
592 virtual Factory getFactory() {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000593 return CreateProc;
594 }
595
596private:
reed@google.com82065d62011-02-07 15:30:46 +0000597 static SkFlattenable* CreateProc(SkFlattenableReadBuffer& buffer) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000598 return SkNEW_ARGS(Sk3DShader, (buffer));
599 }
600
601 SkShader* fProxy;
602 SkPMColor fPMColor;
603 const SkMask* fMask;
604
605 typedef SkShader INHERITED;
606};
607
608class Sk3DBlitter : public SkBlitter {
609public:
610 Sk3DBlitter(SkBlitter* proxy, Sk3DShader* shader, void (*killProc)(void*))
reed@google.com82065d62011-02-07 15:30:46 +0000611 : fProxy(proxy), f3DShader(shader), fKillProc(killProc) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000612 shader->ref();
613 }
reed@google.com82065d62011-02-07 15:30:46 +0000614
615 virtual ~Sk3DBlitter() {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000616 f3DShader->unref();
617 fKillProc(fProxy);
618 }
619
reed@google.com82065d62011-02-07 15:30:46 +0000620 virtual void blitH(int x, int y, int width) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000621 fProxy->blitH(x, y, width);
622 }
reed@google.com82065d62011-02-07 15:30:46 +0000623
624 virtual void blitAntiH(int x, int y, const SkAlpha antialias[],
625 const int16_t runs[]) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000626 fProxy->blitAntiH(x, y, antialias, runs);
627 }
reed@google.com82065d62011-02-07 15:30:46 +0000628
629 virtual void blitV(int x, int y, int height, SkAlpha alpha) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000630 fProxy->blitV(x, y, height, alpha);
631 }
reed@google.com82065d62011-02-07 15:30:46 +0000632
633 virtual void blitRect(int x, int y, int width, int height) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000634 fProxy->blitRect(x, y, width, height);
635 }
reed@google.com82065d62011-02-07 15:30:46 +0000636
637 virtual void blitMask(const SkMask& mask, const SkIRect& clip) {
638 if (mask.fFormat == SkMask::k3D_Format) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000639 f3DShader->setMask(&mask);
640
641 ((SkMask*)&mask)->fFormat = SkMask::kA8_Format;
642 fProxy->blitMask(mask, clip);
643 ((SkMask*)&mask)->fFormat = SkMask::k3D_Format;
644
645 f3DShader->setMask(NULL);
reed@google.com82065d62011-02-07 15:30:46 +0000646 } else {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000647 fProxy->blitMask(mask, clip);
reed@google.com82065d62011-02-07 15:30:46 +0000648 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000649 }
reed@google.com82065d62011-02-07 15:30:46 +0000650
reed@android.com8a1c16f2008-12-17 15:59:43 +0000651private:
652 SkBlitter* fProxy;
653 Sk3DShader* f3DShader;
654 void (*fKillProc)(void*);
655};
656
reed@google.com82065d62011-02-07 15:30:46 +0000657///////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +0000658
659#include "SkCoreBlitters.h"
660
reed@android.com8a1c16f2008-12-17 15:59:43 +0000661class SkAutoCallProc {
662public:
663 typedef void (*Proc)(void*);
reed@google.com82065d62011-02-07 15:30:46 +0000664
reed@android.com8a1c16f2008-12-17 15:59:43 +0000665 SkAutoCallProc(void* obj, Proc proc)
reed@google.com82065d62011-02-07 15:30:46 +0000666 : fObj(obj), fProc(proc) {}
667
668 ~SkAutoCallProc() {
669 if (fObj && fProc) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000670 fProc(fObj);
reed@google.com82065d62011-02-07 15:30:46 +0000671 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000672 }
reed@google.com82065d62011-02-07 15:30:46 +0000673
reed@android.com8a1c16f2008-12-17 15:59:43 +0000674 void* get() const { return fObj; }
reed@google.com82065d62011-02-07 15:30:46 +0000675
676 void* detach() {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000677 void* obj = fObj;
678 fObj = NULL;
679 return obj;
680 }
reed@google.com82065d62011-02-07 15:30:46 +0000681
reed@android.com8a1c16f2008-12-17 15:59:43 +0000682private:
683 void* fObj;
684 Proc fProc;
685};
686
reed@google.com82065d62011-02-07 15:30:46 +0000687static void destroy_blitter(void* blitter) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000688 ((SkBlitter*)blitter)->~SkBlitter();
689}
690
reed@google.com82065d62011-02-07 15:30:46 +0000691static void delete_blitter(void* blitter) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000692 SkDELETE((SkBlitter*)blitter);
693}
694
reed@android.comd252db02009-04-01 18:31:44 +0000695static bool just_solid_color(const SkPaint& paint) {
696 if (paint.getAlpha() == 0xFF && paint.getColorFilter() == NULL) {
697 SkShader* shader = paint.getShader();
698 if (NULL == shader ||
699 (shader->getFlags() & SkShader::kOpaqueAlpha_Flag)) {
700 return true;
701 }
702 }
703 return false;
704}
reed@google.com82065d62011-02-07 15:30:46 +0000705
reed@android.comd252db02009-04-01 18:31:44 +0000706/** By analyzing the paint (with an xfermode), we may decide we can take
707 special action. This enum lists our possible actions
708 */
709enum XferInterp {
710 kNormal_XferInterp, // no special interpretation, draw normally
711 kSrcOver_XferInterp, // draw as if in srcover mode
712 kSkipDrawing_XferInterp // draw nothing
713};
714
715static XferInterp interpret_xfermode(const SkPaint& paint, SkXfermode* xfer,
716 SkBitmap::Config deviceConfig) {
reed@android.com845fdac2009-06-23 03:01:32 +0000717 SkXfermode::Mode mode;
reed@google.com82065d62011-02-07 15:30:46 +0000718
mike@reedtribe.orgbe2aa2a2011-11-17 02:32:04 +0000719 if (SkXfermode::AsMode(xfer, &mode)) {
reed@android.comd252db02009-04-01 18:31:44 +0000720 switch (mode) {
reed@android.com845fdac2009-06-23 03:01:32 +0000721 case SkXfermode::kSrc_Mode:
reed@android.comd252db02009-04-01 18:31:44 +0000722 if (just_solid_color(paint)) {
723 return kSrcOver_XferInterp;
724 }
725 break;
reed@android.com845fdac2009-06-23 03:01:32 +0000726 case SkXfermode::kDst_Mode:
reed@android.comd252db02009-04-01 18:31:44 +0000727 return kSkipDrawing_XferInterp;
reed@android.com845fdac2009-06-23 03:01:32 +0000728 case SkXfermode::kSrcOver_Mode:
reed@android.comd252db02009-04-01 18:31:44 +0000729 return kSrcOver_XferInterp;
reed@android.com845fdac2009-06-23 03:01:32 +0000730 case SkXfermode::kDstOver_Mode:
reed@android.comd252db02009-04-01 18:31:44 +0000731 if (SkBitmap::kRGB_565_Config == deviceConfig) {
732 return kSkipDrawing_XferInterp;
733 }
734 break;
reed@android.com845fdac2009-06-23 03:01:32 +0000735 case SkXfermode::kSrcIn_Mode:
reed@android.comd252db02009-04-01 18:31:44 +0000736 if (SkBitmap::kRGB_565_Config == deviceConfig &&
737 just_solid_color(paint)) {
738 return kSrcOver_XferInterp;
739 }
740 break;
reed@android.com845fdac2009-06-23 03:01:32 +0000741 case SkXfermode::kDstIn_Mode:
reed@android.comd252db02009-04-01 18:31:44 +0000742 if (just_solid_color(paint)) {
743 return kSkipDrawing_XferInterp;
744 }
745 break;
746 default:
747 break;
748 }
749 }
750 return kNormal_XferInterp;
751}
752
reed@android.com8a1c16f2008-12-17 15:59:43 +0000753SkBlitter* SkBlitter::Choose(const SkBitmap& device,
754 const SkMatrix& matrix,
reed@google.com6b7aee32011-04-19 18:36:09 +0000755 const SkPaint& origPaint,
reed@google.com82065d62011-02-07 15:30:46 +0000756 void* storage, size_t storageSize) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000757 SkASSERT(storageSize == 0 || storage != NULL);
758
759 SkBlitter* blitter = NULL;
760
761 // which check, in case we're being called by a client with a dummy device
762 // (e.g. they have a bounder that always aborts the draw)
reed@google.com82065d62011-02-07 15:30:46 +0000763 if (SkBitmap::kNo_Config == device.getConfig()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000764 SK_PLACEMENT_NEW(blitter, SkNullBlitter, storage, storageSize);
765 return blitter;
766 }
767
reed@google.com6b7aee32011-04-19 18:36:09 +0000768 SkPaint paint(origPaint);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000769 SkShader* shader = paint.getShader();
reed@google.com6b7aee32011-04-19 18:36:09 +0000770 SkColorFilter* cf = paint.getColorFilter();
771 SkXfermode* mode = paint.getXfermode();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000772
773 Sk3DShader* shader3D = NULL;
reed@google.com82065d62011-02-07 15:30:46 +0000774 if (paint.getMaskFilter() != NULL &&
775 paint.getMaskFilter()->getFormat() == SkMask::k3D_Format) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000776 shader3D = SkNEW_ARGS(Sk3DShader, (shader));
reed@google.com6b7aee32011-04-19 18:36:09 +0000777 paint.setShader(shader3D)->unref();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000778 shader = shader3D;
779 }
780
reed@android.comd252db02009-04-01 18:31:44 +0000781 if (NULL != mode) {
782 switch (interpret_xfermode(paint, mode, device.config())) {
783 case kSrcOver_XferInterp:
784 mode = NULL;
reed@google.com6b7aee32011-04-19 18:36:09 +0000785 paint.setXfermode(NULL);
reed@android.comd252db02009-04-01 18:31:44 +0000786 break;
787 case kSkipDrawing_XferInterp:
788 SK_PLACEMENT_NEW(blitter, SkNullBlitter, storage, storageSize);
789 return blitter;
790 default:
791 break;
792 }
793 }
794
reed@google.com6b7aee32011-04-19 18:36:09 +0000795 if (NULL == shader) {
796#ifdef SK_IGNORE_CF_OPTIMIZATION
797 if (mode || cf) {
798#else
799 if (mode) {
800#endif
801 // xfermodes (and filters) require shaders for our current blitters
802 shader = SkNEW(SkColorShader);
803 paint.setShader(shader)->unref();
804 } else if (cf) {
805 // if no shader && no xfermode, we just apply the colorfilter to
806 // our color and move on.
807 paint.setColor(cf->filterColor(paint.getColor()));
808 paint.setColorFilter(NULL);
809 cf = NULL;
810 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000811 }
reed@google.com82065d62011-02-07 15:30:46 +0000812
reed@google.com6b7aee32011-04-19 18:36:09 +0000813 if (cf) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000814 SkASSERT(shader);
reed@google.com6b7aee32011-04-19 18:36:09 +0000815 shader = SkNEW_ARGS(SkFilterShader, (shader, cf));
816 paint.setShader(shader)->unref();
reed@android.com1fc4c602009-10-02 16:34:57 +0000817 // blitters should ignore the presence/absence of a filter, since
818 // if there is one, the shader will take care of it.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000819 }
reed@google.com82065d62011-02-07 15:30:46 +0000820
reed@android.comcafc9f92009-08-22 03:44:57 +0000821 if (shader && !shader->setContext(device, paint, matrix)) {
822 return SkNEW(SkNullBlitter);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000823 }
824
825 switch (device.getConfig()) {
reed@google.com82065d62011-02-07 15:30:46 +0000826 case SkBitmap::kA1_Config:
827 SK_PLACEMENT_NEW_ARGS(blitter, SkA1_Blitter,
828 storage, storageSize, (device, paint));
829 break;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000830
reed@google.com82065d62011-02-07 15:30:46 +0000831 case SkBitmap::kA8_Config:
832 if (shader) {
833 SK_PLACEMENT_NEW_ARGS(blitter, SkA8_Shader_Blitter,
834 storage, storageSize, (device, paint));
835 } else {
836 SK_PLACEMENT_NEW_ARGS(blitter, SkA8_Blitter,
837 storage, storageSize, (device, paint));
838 }
839 break;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000840
reed@google.com82065d62011-02-07 15:30:46 +0000841 case SkBitmap::kARGB_4444_Config:
842 blitter = SkBlitter_ChooseD4444(device, paint, storage, storageSize);
843 break;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000844
reed@google.com82065d62011-02-07 15:30:46 +0000845 case SkBitmap::kRGB_565_Config:
846 blitter = SkBlitter_ChooseD565(device, paint, storage, storageSize);
847 break;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000848
reed@google.com82065d62011-02-07 15:30:46 +0000849 case SkBitmap::kARGB_8888_Config:
850 if (shader) {
851 SK_PLACEMENT_NEW_ARGS(blitter, SkARGB32_Shader_Blitter,
852 storage, storageSize, (device, paint));
853 } else if (paint.getColor() == SK_ColorBLACK) {
854 SK_PLACEMENT_NEW_ARGS(blitter, SkARGB32_Black_Blitter,
855 storage, storageSize, (device, paint));
856 } else if (paint.getAlpha() == 0xFF) {
857 SK_PLACEMENT_NEW_ARGS(blitter, SkARGB32_Opaque_Blitter,
858 storage, storageSize, (device, paint));
859 } else {
860 SK_PLACEMENT_NEW_ARGS(blitter, SkARGB32_Blitter,
861 storage, storageSize, (device, paint));
862 }
863 break;
864
865 default:
866 SkASSERT(!"unsupported device config");
867 SK_PLACEMENT_NEW(blitter, SkNullBlitter, storage, storageSize);
868 break;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000869 }
870
reed@google.com82065d62011-02-07 15:30:46 +0000871 if (shader3D) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000872 void (*proc)(void*) = ((void*)storage == (void*)blitter) ? destroy_blitter : delete_blitter;
873 SkAutoCallProc tmp(blitter, proc);
874
875 blitter = SkNEW_ARGS(Sk3DBlitter, (blitter, shader3D, proc));
876 (void)tmp.detach();
877 }
878 return blitter;
879}
880
reed@google.com82065d62011-02-07 15:30:46 +0000881///////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +0000882
883const uint16_t gMask_0F0F = 0xF0F;
884const uint32_t gMask_00FF00FF = 0xFF00FF;
885
reed@google.com82065d62011-02-07 15:30:46 +0000886///////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +0000887
888SkShaderBlitter::SkShaderBlitter(const SkBitmap& device, const SkPaint& paint)
reed@android.com5119bdb2009-06-12 21:27:03 +0000889 : INHERITED(device) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000890 fShader = paint.getShader();
891 SkASSERT(fShader);
892
893 fShader->ref();
894 fShader->beginSession();
reed@android.com5119bdb2009-06-12 21:27:03 +0000895 fShaderFlags = fShader->getFlags();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000896}
897
reed@android.com5119bdb2009-06-12 21:27:03 +0000898SkShaderBlitter::~SkShaderBlitter() {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000899 fShader->endSession();
900 fShader->unref();
901}
902