blob: 08b21dcfb03abb6b54202f78510848758391f7e5 [file] [log] [blame]
reed@android.com8a1c16f2008-12-17 15:59:43 +00001/* libs/graphics/sgl/SkBlitter.cpp
2**
3** Copyright 2006, The Android Open Source Project
4**
reed@google.com82065d62011-02-07 15:30:46 +00005** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
reed@android.com8a1c16f2008-12-17 15:59:43 +00008**
reed@google.com82065d62011-02-07 15:30:46 +00009** http://www.apache.org/licenses/LICENSE-2.0
reed@android.com8a1c16f2008-12-17 15:59:43 +000010**
reed@google.com82065d62011-02-07 15:30:46 +000011** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
reed@android.com8a1c16f2008-12-17 15:59:43 +000015** limitations under the License.
16*/
17
18#include "SkBlitter.h"
19#include "SkAntiRun.h"
20#include "SkColor.h"
21#include "SkColorFilter.h"
22#include "SkMask.h"
23#include "SkMaskFilter.h"
24#include "SkTemplatesPriv.h"
25#include "SkUtils.h"
26#include "SkXfermode.h"
27
reed@android.com845fdac2009-06-23 03:01:32 +000028SkBlitter::~SkBlitter() {}
reed@android.com8a1c16f2008-12-17 15:59:43 +000029
reed@google.com82065d62011-02-07 15:30:46 +000030const SkBitmap* SkBlitter::justAnOpaqueColor(uint32_t* value) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000031 return NULL;
32}
33
reed@google.com82065d62011-02-07 15:30:46 +000034void SkBlitter::blitH(int x, int y, int width) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000035 SkASSERT(!"unimplemented");
36}
37
reed@google.com82065d62011-02-07 15:30:46 +000038void SkBlitter::blitAntiH(int x, int y, const SkAlpha antialias[],
39 const int16_t runs[]) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000040 SkASSERT(!"unimplemented");
41}
42
reed@google.com82065d62011-02-07 15:30:46 +000043void SkBlitter::blitV(int x, int y, int height, SkAlpha alpha) {
44 if (alpha == 255) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000045 this->blitRect(x, y, 1, height);
reed@google.com82065d62011-02-07 15:30:46 +000046 } else {
reed@android.com8a1c16f2008-12-17 15:59:43 +000047 int16_t runs[2];
48 runs[0] = 1;
49 runs[1] = 0;
50
reed@google.com82065d62011-02-07 15:30:46 +000051 while (--height >= 0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000052 this->blitAntiH(x, y++, &alpha, runs);
reed@google.com82065d62011-02-07 15:30:46 +000053 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000054 }
55}
56
reed@google.com82065d62011-02-07 15:30:46 +000057void SkBlitter::blitRect(int x, int y, int width, int height) {
58 while (--height >= 0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000059 this->blitH(x, y++, width);
reed@google.com82065d62011-02-07 15:30:46 +000060 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000061}
62
63//////////////////////////////////////////////////////////////////////////////
64
reed@google.com82065d62011-02-07 15:30:46 +000065static inline void bits_to_runs(SkBlitter* blitter, int x, int y,
66 const uint8_t bits[],
67 U8CPU left_mask, int rowBytes,
68 U8CPU right_mask) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000069 int inFill = 0;
70 int pos = 0;
71
reed@google.com82065d62011-02-07 15:30:46 +000072 while (--rowBytes >= 0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000073 unsigned b = *bits++ & left_mask;
reed@google.com82065d62011-02-07 15:30:46 +000074 if (rowBytes == 0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000075 b &= right_mask;
reed@google.com82065d62011-02-07 15:30:46 +000076 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000077
reed@google.com82065d62011-02-07 15:30:46 +000078 for (unsigned test = 0x80; test != 0; test >>= 1) {
79 if (b & test) {
80 if (!inFill) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000081 pos = x;
82 inFill = true;
83 }
reed@google.com82065d62011-02-07 15:30:46 +000084 } else {
85 if (inFill) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000086 blitter->blitH(pos, y, x - pos);
87 inFill = false;
88 }
89 }
90 x += 1;
91 }
92 left_mask = 0xFF;
93 }
94
95 // final cleanup
reed@google.com82065d62011-02-07 15:30:46 +000096 if (inFill) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000097 blitter->blitH(pos, y, x - pos);
reed@google.com82065d62011-02-07 15:30:46 +000098 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000099}
100
reed@google.com82065d62011-02-07 15:30:46 +0000101void SkBlitter::blitMask(const SkMask& mask, const SkIRect& clip) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000102 SkASSERT(mask.fBounds.contains(clip));
103
reed@google.com82065d62011-02-07 15:30:46 +0000104 if (mask.fFormat == SkMask::kBW_Format) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000105 int cx = clip.fLeft;
106 int cy = clip.fTop;
107 int maskLeft = mask.fBounds.fLeft;
108 int mask_rowBytes = mask.fRowBytes;
109 int height = clip.height();
110
111 const uint8_t* bits = mask.getAddr1(cx, cy);
112
reed@google.com82065d62011-02-07 15:30:46 +0000113 if (cx == maskLeft && clip.fRight == mask.fBounds.fRight) {
114 while (--height >= 0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000115 bits_to_runs(this, cx, cy, bits, 0xFF, mask_rowBytes, 0xFF);
116 bits += mask_rowBytes;
117 cy += 1;
118 }
reed@google.com82065d62011-02-07 15:30:46 +0000119 } else {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000120 int left_edge = cx - maskLeft;
121 SkASSERT(left_edge >= 0);
122 int rite_edge = clip.fRight - maskLeft;
123 SkASSERT(rite_edge > left_edge);
124
125 int left_mask = 0xFF >> (left_edge & 7);
126 int rite_mask = 0xFF << (8 - (rite_edge & 7));
127 int full_runs = (rite_edge >> 3) - ((left_edge + 7) >> 3);
128
129 // 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 +0000130 if (rite_mask == 0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000131 SkASSERT(full_runs >= 0);
132 full_runs -= 1;
133 rite_mask = 0xFF;
134 }
reed@google.com82065d62011-02-07 15:30:46 +0000135 if (left_mask == 0xFF) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000136 full_runs -= 1;
reed@google.com82065d62011-02-07 15:30:46 +0000137 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000138
139 // back up manually so we can keep in sync with our byte-aligned src
140 // have cx reflect our actual starting x-coord
141 cx -= left_edge & 7;
142
reed@google.com82065d62011-02-07 15:30:46 +0000143 if (full_runs < 0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000144 SkASSERT((left_mask & rite_mask) != 0);
reed@google.com82065d62011-02-07 15:30:46 +0000145 while (--height >= 0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000146 bits_to_runs(this, cx, cy, bits, left_mask, 1, rite_mask);
147 bits += mask_rowBytes;
148 cy += 1;
149 }
reed@google.com82065d62011-02-07 15:30:46 +0000150 } else {
151 while (--height >= 0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000152 bits_to_runs(this, cx, cy, bits, left_mask, full_runs + 2, rite_mask);
153 bits += mask_rowBytes;
154 cy += 1;
155 }
156 }
157 }
reed@google.com82065d62011-02-07 15:30:46 +0000158 } else {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000159 int width = clip.width();
160 SkAutoSTMalloc<64, int16_t> runStorage(width + 1);
161 int16_t* runs = runStorage.get();
162 const uint8_t* aa = mask.getAddr(clip.fLeft, clip.fTop);
163
164 sk_memset16((uint16_t*)runs, 1, width);
165 runs[width] = 0;
166
167 int height = clip.height();
168 int y = clip.fTop;
reed@google.com82065d62011-02-07 15:30:46 +0000169 while (--height >= 0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000170 this->blitAntiH(clip.fLeft, y, aa, runs);
171 aa += mask.fRowBytes;
172 y += 1;
173 }
174 }
175}
176
177/////////////////////// these guys are not virtual, just a helpers
178
179void SkBlitter::blitMaskRegion(const SkMask& mask, const SkRegion& clip) {
180 if (clip.quickReject(mask.fBounds)) {
181 return;
182 }
reed@google.com82065d62011-02-07 15:30:46 +0000183
reed@android.com8a1c16f2008-12-17 15:59:43 +0000184 SkRegion::Cliperator clipper(clip, mask.fBounds);
reed@google.com82065d62011-02-07 15:30:46 +0000185
reed@android.com8a1c16f2008-12-17 15:59:43 +0000186 while (!clipper.done()) {
187 const SkIRect& cr = clipper.rect();
188 this->blitMask(mask, cr);
189 clipper.next();
190 }
191}
192
193void SkBlitter::blitRectRegion(const SkIRect& rect, const SkRegion& clip) {
194 SkRegion::Cliperator clipper(clip, rect);
reed@google.com82065d62011-02-07 15:30:46 +0000195
reed@android.com8a1c16f2008-12-17 15:59:43 +0000196 while (!clipper.done()) {
197 const SkIRect& cr = clipper.rect();
198 this->blitRect(cr.fLeft, cr.fTop, cr.width(), cr.height());
199 clipper.next();
200 }
201}
202
203void SkBlitter::blitRegion(const SkRegion& clip) {
204 SkRegion::Iterator iter(clip);
reed@google.com82065d62011-02-07 15:30:46 +0000205
reed@android.com8a1c16f2008-12-17 15:59:43 +0000206 while (!iter.done()) {
207 const SkIRect& cr = iter.rect();
208 this->blitRect(cr.fLeft, cr.fTop, cr.width(), cr.height());
209 iter.next();
210 }
211}
212
reed@google.com82065d62011-02-07 15:30:46 +0000213///////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +0000214
reed@google.com82065d62011-02-07 15:30:46 +0000215void SkNullBlitter::blitH(int x, int y, int width) {}
reed@android.com8a1c16f2008-12-17 15:59:43 +0000216
reed@google.com82065d62011-02-07 15:30:46 +0000217void SkNullBlitter::blitAntiH(int x, int y, const SkAlpha antialias[],
218 const int16_t runs[]) {}
reed@android.com8a1c16f2008-12-17 15:59:43 +0000219
reed@google.com82065d62011-02-07 15:30:46 +0000220void SkNullBlitter::blitV(int x, int y, int height, SkAlpha alpha) {}
reed@android.com8a1c16f2008-12-17 15:59:43 +0000221
reed@google.com82065d62011-02-07 15:30:46 +0000222void SkNullBlitter::blitRect(int x, int y, int width, int height) {}
reed@android.com8a1c16f2008-12-17 15:59:43 +0000223
reed@google.com82065d62011-02-07 15:30:46 +0000224void SkNullBlitter::blitMask(const SkMask& mask, const SkIRect& clip) {}
reed@android.com8a1c16f2008-12-17 15:59:43 +0000225
reed@google.com82065d62011-02-07 15:30:46 +0000226const SkBitmap* SkNullBlitter::justAnOpaqueColor(uint32_t* value) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000227 return NULL;
228}
229
reed@google.com82065d62011-02-07 15:30:46 +0000230///////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +0000231
reed@google.com82065d62011-02-07 15:30:46 +0000232static int compute_anti_width(const int16_t runs[]) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000233 int width = 0;
reed@google.com82065d62011-02-07 15:30:46 +0000234
235 for (;;) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000236 int count = runs[0];
reed@google.com82065d62011-02-07 15:30:46 +0000237
reed@android.com8a1c16f2008-12-17 15:59:43 +0000238 SkASSERT(count >= 0);
reed@google.com82065d62011-02-07 15:30:46 +0000239 if (count == 0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000240 break;
reed@google.com82065d62011-02-07 15:30:46 +0000241 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000242 width += count;
243 runs += count;
reed@google.com82065d62011-02-07 15:30:46 +0000244
reed@android.com8a1c16f2008-12-17 15:59:43 +0000245 SkASSERT(width < 20000);
246 }
247 return width;
248}
249
reed@google.com82065d62011-02-07 15:30:46 +0000250static inline bool y_in_rect(int y, const SkIRect& rect) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000251 return (unsigned)(y - rect.fTop) < (unsigned)rect.height();
252}
253
reed@google.com82065d62011-02-07 15:30:46 +0000254static inline bool x_in_rect(int x, const SkIRect& rect) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000255 return (unsigned)(x - rect.fLeft) < (unsigned)rect.width();
256}
257
reed@google.com82065d62011-02-07 15:30:46 +0000258void SkRectClipBlitter::blitH(int left, int y, int width) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000259 SkASSERT(width > 0);
260
reed@google.com82065d62011-02-07 15:30:46 +0000261 if (!y_in_rect(y, fClipRect)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000262 return;
reed@google.com82065d62011-02-07 15:30:46 +0000263 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000264
265 int right = left + width;
266
reed@google.com82065d62011-02-07 15:30:46 +0000267 if (left < fClipRect.fLeft) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000268 left = fClipRect.fLeft;
reed@google.com82065d62011-02-07 15:30:46 +0000269 }
270 if (right > fClipRect.fRight) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000271 right = fClipRect.fRight;
reed@google.com82065d62011-02-07 15:30:46 +0000272 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000273
274 width = right - left;
reed@google.com82065d62011-02-07 15:30:46 +0000275 if (width > 0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000276 fBlitter->blitH(left, y, width);
reed@google.com82065d62011-02-07 15:30:46 +0000277 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000278}
279
reed@google.com82065d62011-02-07 15:30:46 +0000280void SkRectClipBlitter::blitAntiH(int left, int y, const SkAlpha aa[],
281 const int16_t runs[]) {
282 if (!y_in_rect(y, fClipRect) || left >= fClipRect.fRight) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000283 return;
reed@google.com82065d62011-02-07 15:30:46 +0000284 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000285
286 int x0 = left;
287 int x1 = left + compute_anti_width(runs);
288
reed@google.com82065d62011-02-07 15:30:46 +0000289 if (x1 <= fClipRect.fLeft) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000290 return;
reed@google.com82065d62011-02-07 15:30:46 +0000291 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000292
293 SkASSERT(x0 < x1);
reed@google.com82065d62011-02-07 15:30:46 +0000294 if (x0 < fClipRect.fLeft) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000295 int dx = fClipRect.fLeft - x0;
296 SkAlphaRuns::BreakAt((int16_t*)runs, (uint8_t*)aa, dx);
297 runs += dx;
298 aa += dx;
299 x0 = fClipRect.fLeft;
300 }
301
302 SkASSERT(x0 < x1 && runs[x1 - x0] == 0);
reed@google.com82065d62011-02-07 15:30:46 +0000303 if (x1 > fClipRect.fRight) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000304 x1 = fClipRect.fRight;
305 SkAlphaRuns::BreakAt((int16_t*)runs, (uint8_t*)aa, x1 - x0);
306 ((int16_t*)runs)[x1 - x0] = 0;
307 }
308
309 SkASSERT(x0 < x1 && runs[x1 - x0] == 0);
310 SkASSERT(compute_anti_width(runs) == x1 - x0);
311
312 fBlitter->blitAntiH(x0, y, aa, runs);
313}
314
reed@google.com82065d62011-02-07 15:30:46 +0000315void SkRectClipBlitter::blitV(int x, int y, int height, SkAlpha alpha) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000316 SkASSERT(height > 0);
317
reed@google.com82065d62011-02-07 15:30:46 +0000318 if (!x_in_rect(x, fClipRect)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000319 return;
reed@google.com82065d62011-02-07 15:30:46 +0000320 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000321
322 int y0 = y;
323 int y1 = y + height;
324
reed@google.com82065d62011-02-07 15:30:46 +0000325 if (y0 < fClipRect.fTop) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000326 y0 = fClipRect.fTop;
reed@google.com82065d62011-02-07 15:30:46 +0000327 }
328 if (y1 > fClipRect.fBottom) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000329 y1 = fClipRect.fBottom;
reed@google.com82065d62011-02-07 15:30:46 +0000330 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000331
reed@google.com82065d62011-02-07 15:30:46 +0000332 if (y0 < y1) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000333 fBlitter->blitV(x, y0, y1 - y0, alpha);
reed@google.com82065d62011-02-07 15:30:46 +0000334 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000335}
336
reed@google.com82065d62011-02-07 15:30:46 +0000337void SkRectClipBlitter::blitRect(int left, int y, int width, int height) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000338 SkIRect r;
339
340 r.set(left, y, left + width, y + height);
reed@google.com82065d62011-02-07 15:30:46 +0000341 if (r.intersect(fClipRect)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000342 fBlitter->blitRect(r.fLeft, r.fTop, r.width(), r.height());
reed@google.com82065d62011-02-07 15:30:46 +0000343 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000344}
345
reed@google.com82065d62011-02-07 15:30:46 +0000346void SkRectClipBlitter::blitMask(const SkMask& mask, const SkIRect& clip) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000347 SkASSERT(mask.fBounds.contains(clip));
348
349 SkIRect r = clip;
350
reed@google.com82065d62011-02-07 15:30:46 +0000351 if (r.intersect(fClipRect)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000352 fBlitter->blitMask(mask, r);
reed@google.com82065d62011-02-07 15:30:46 +0000353 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000354}
355
reed@google.com82065d62011-02-07 15:30:46 +0000356const SkBitmap* SkRectClipBlitter::justAnOpaqueColor(uint32_t* value) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000357 return fBlitter->justAnOpaqueColor(value);
358}
359
reed@google.com82065d62011-02-07 15:30:46 +0000360///////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +0000361
reed@google.com82065d62011-02-07 15:30:46 +0000362void SkRgnClipBlitter::blitH(int x, int y, int width) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000363 SkRegion::Spanerator span(*fRgn, y, x, x + width);
364 int left, right;
365
reed@google.com82065d62011-02-07 15:30:46 +0000366 while (span.next(&left, &right)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000367 SkASSERT(left < right);
368 fBlitter->blitH(left, y, right - left);
369 }
370}
371
reed@google.com82065d62011-02-07 15:30:46 +0000372void SkRgnClipBlitter::blitAntiH(int x, int y, const SkAlpha aa[],
373 const int16_t runs[]) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000374 int width = compute_anti_width(runs);
375 SkRegion::Spanerator span(*fRgn, y, x, x + width);
376 int left, right;
377 SkDEBUGCODE(const SkIRect& bounds = fRgn->getBounds();)
reed@google.com82065d62011-02-07 15:30:46 +0000378
reed@android.com8a1c16f2008-12-17 15:59:43 +0000379 int prevRite = x;
reed@google.com82065d62011-02-07 15:30:46 +0000380 while (span.next(&left, &right)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000381 SkASSERT(x <= left);
382 SkASSERT(left < right);
383 SkASSERT(left >= bounds.fLeft && right <= bounds.fRight);
reed@google.com82065d62011-02-07 15:30:46 +0000384
reed@android.com8a1c16f2008-12-17 15:59:43 +0000385 SkAlphaRuns::Break((int16_t*)runs, (uint8_t*)aa, left - x, right - left);
386
387 // now zero before left
reed@google.com82065d62011-02-07 15:30:46 +0000388 if (left > prevRite) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000389 int index = prevRite - x;
390 ((uint8_t*)aa)[index] = 0; // skip runs after right
391 ((int16_t*)runs)[index] = SkToS16(left - prevRite);
392 }
reed@google.com82065d62011-02-07 15:30:46 +0000393
reed@android.com8a1c16f2008-12-17 15:59:43 +0000394 prevRite = right;
395 }
reed@google.com82065d62011-02-07 15:30:46 +0000396
397 if (prevRite > x) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000398 ((int16_t*)runs)[prevRite - x] = 0;
reed@google.com82065d62011-02-07 15:30:46 +0000399
reed@android.com8a1c16f2008-12-17 15:59:43 +0000400 if (x < 0) {
401 int skip = runs[0];
402 SkASSERT(skip >= -x);
403 aa += skip;
404 runs += skip;
405 x += skip;
406 }
407 fBlitter->blitAntiH(x, y, aa, runs);
408 }
409}
410
reed@google.com82065d62011-02-07 15:30:46 +0000411void SkRgnClipBlitter::blitV(int x, int y, int height, SkAlpha alpha) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000412 SkIRect bounds;
413 bounds.set(x, y, x + 1, y + height);
414
415 SkRegion::Cliperator iter(*fRgn, bounds);
416
reed@google.com82065d62011-02-07 15:30:46 +0000417 while (!iter.done()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000418 const SkIRect& r = iter.rect();
419 SkASSERT(bounds.contains(r));
420
421 fBlitter->blitV(x, r.fTop, r.height(), alpha);
422 iter.next();
423 }
424}
425
reed@google.com82065d62011-02-07 15:30:46 +0000426void SkRgnClipBlitter::blitRect(int x, int y, int width, int height) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000427 SkIRect bounds;
428 bounds.set(x, y, x + width, y + height);
429
430 SkRegion::Cliperator iter(*fRgn, bounds);
431
reed@google.com82065d62011-02-07 15:30:46 +0000432 while (!iter.done()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000433 const SkIRect& r = iter.rect();
434 SkASSERT(bounds.contains(r));
435
436 fBlitter->blitRect(r.fLeft, r.fTop, r.width(), r.height());
437 iter.next();
438 }
439}
440
reed@google.com82065d62011-02-07 15:30:46 +0000441void SkRgnClipBlitter::blitMask(const SkMask& mask, const SkIRect& clip) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000442 SkASSERT(mask.fBounds.contains(clip));
443
444 SkRegion::Cliperator iter(*fRgn, clip);
445 const SkIRect& r = iter.rect();
446 SkBlitter* blitter = fBlitter;
447
reed@google.com82065d62011-02-07 15:30:46 +0000448 while (!iter.done()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000449 blitter->blitMask(mask, r);
450 iter.next();
451 }
452}
453
reed@google.com82065d62011-02-07 15:30:46 +0000454const SkBitmap* SkRgnClipBlitter::justAnOpaqueColor(uint32_t* value) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000455 return fBlitter->justAnOpaqueColor(value);
456}
457
reed@google.com82065d62011-02-07 15:30:46 +0000458///////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +0000459
reed@google.com82065d62011-02-07 15:30:46 +0000460SkBlitter* SkBlitterClipper::apply(SkBlitter* blitter, const SkRegion* clip,
461 const SkIRect* ir) {
462 if (clip) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000463 const SkIRect& clipR = clip->getBounds();
464
reed@google.com82065d62011-02-07 15:30:46 +0000465 if (clip->isEmpty() || (ir && !SkIRect::Intersects(clipR, *ir))) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000466 blitter = &fNullBlitter;
reed@google.com82065d62011-02-07 15:30:46 +0000467 } else if (clip->isRect()) {
468 if (ir == NULL || !clipR.contains(*ir)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000469 fRectBlitter.init(blitter, clipR);
470 blitter = &fRectBlitter;
471 }
reed@google.com82065d62011-02-07 15:30:46 +0000472 } else {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000473 fRgnBlitter.init(blitter, clip);
474 blitter = &fRgnBlitter;
475 }
476 }
477 return blitter;
478}
479
reed@google.com82065d62011-02-07 15:30:46 +0000480///////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +0000481
482#include "SkColorShader.h"
483#include "SkColorPriv.h"
484
485class Sk3DShader : public SkShader {
486public:
reed@google.com82065d62011-02-07 15:30:46 +0000487 Sk3DShader(SkShader* proxy) : fProxy(proxy) {
488 SkSafeRef(proxy);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000489 fMask = NULL;
490 }
reed@google.com82065d62011-02-07 15:30:46 +0000491
492 virtual ~Sk3DShader() {
493 SkSafeUnref(fProxy);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000494 }
reed@google.com82065d62011-02-07 15:30:46 +0000495
reed@android.com8a1c16f2008-12-17 15:59:43 +0000496 void setMask(const SkMask* mask) { fMask = mask; }
497
reed@google.com82065d62011-02-07 15:30:46 +0000498 virtual bool setContext(const SkBitmap& device, const SkPaint& paint,
499 const SkMatrix& matrix) {
500 if (fProxy) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000501 return fProxy->setContext(device, paint, matrix);
reed@google.com82065d62011-02-07 15:30:46 +0000502 } else {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000503 fPMColor = SkPreMultiplyColor(paint.getColor());
504 return this->INHERITED::setContext(device, paint, matrix);
505 }
506 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000507
reed@google.com82065d62011-02-07 15:30:46 +0000508 virtual void shadeSpan(int x, int y, SkPMColor span[], int count) {
509 if (fProxy) {
510 fProxy->shadeSpan(x, y, span, count);
511 }
512
513 if (fMask == NULL) {
514 if (fProxy == NULL) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000515 sk_memset32(span, fPMColor, count);
reed@google.com82065d62011-02-07 15:30:46 +0000516 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000517 return;
518 }
519
520 SkASSERT(fMask->fBounds.contains(x, y));
521 SkASSERT(fMask->fBounds.contains(x + count - 1, y));
522
523 size_t size = fMask->computeImageSize();
524 const uint8_t* alpha = fMask->getAddr(x, y);
525 const uint8_t* mulp = alpha + size;
526 const uint8_t* addp = mulp + size;
527
reed@google.com82065d62011-02-07 15:30:46 +0000528 if (fProxy) {
529 for (int i = 0; i < count; i++) {
530 if (alpha[i]) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000531 SkPMColor c = span[i];
reed@google.com82065d62011-02-07 15:30:46 +0000532 if (c) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000533 unsigned a = SkGetPackedA32(c);
534 unsigned r = SkGetPackedR32(c);
535 unsigned g = SkGetPackedG32(c);
536 unsigned b = SkGetPackedB32(c);
537
538 unsigned mul = SkAlpha255To256(mulp[i]);
539 unsigned add = addp[i];
540
541 r = SkFastMin32(SkAlphaMul(r, mul) + add, a);
542 g = SkFastMin32(SkAlphaMul(g, mul) + add, a);
543 b = SkFastMin32(SkAlphaMul(b, mul) + add, a);
544
545 span[i] = SkPackARGB32(a, r, g, b);
546 }
reed@google.com82065d62011-02-07 15:30:46 +0000547 } else {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000548 span[i] = 0;
reed@google.com82065d62011-02-07 15:30:46 +0000549 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000550 }
reed@google.com82065d62011-02-07 15:30:46 +0000551 } else { // color
reed@android.com8a1c16f2008-12-17 15:59:43 +0000552 unsigned a = SkGetPackedA32(fPMColor);
553 unsigned r = SkGetPackedR32(fPMColor);
554 unsigned g = SkGetPackedG32(fPMColor);
555 unsigned b = SkGetPackedB32(fPMColor);
reed@google.com82065d62011-02-07 15:30:46 +0000556 for (int i = 0; i < count; i++) {
557 if (alpha[i]) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000558 unsigned mul = SkAlpha255To256(mulp[i]);
559 unsigned add = addp[i];
560
561 span[i] = SkPackARGB32( a,
reed@google.com82065d62011-02-07 15:30:46 +0000562 SkFastMin32(SkAlphaMul(r, mul) + add, a),
563 SkFastMin32(SkAlphaMul(g, mul) + add, a),
564 SkFastMin32(SkAlphaMul(b, mul) + add, a));
565 } else {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000566 span[i] = 0;
reed@google.com82065d62011-02-07 15:30:46 +0000567 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000568 }
569 }
570 }
reed@google.com82065d62011-02-07 15:30:46 +0000571
572 virtual void beginSession() {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000573 this->INHERITED::beginSession();
reed@google.com82065d62011-02-07 15:30:46 +0000574 if (fProxy) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000575 fProxy->beginSession();
reed@google.com82065d62011-02-07 15:30:46 +0000576 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000577 }
reed@google.com82065d62011-02-07 15:30:46 +0000578
579 virtual void endSession() {
580 if (fProxy) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000581 fProxy->endSession();
reed@google.com82065d62011-02-07 15:30:46 +0000582 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000583 this->INHERITED::endSession();
584 }
585
586protected:
587 Sk3DShader(SkFlattenableReadBuffer& buffer) :
reed@google.com82065d62011-02-07 15:30:46 +0000588 INHERITED(buffer) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000589 fProxy = static_cast<SkShader*>(buffer.readFlattenable());
590 fPMColor = buffer.readU32();
591 fMask = NULL;
592 }
reed@google.com82065d62011-02-07 15:30:46 +0000593
594 virtual void flatten(SkFlattenableWriteBuffer& buffer) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000595 this->INHERITED::flatten(buffer);
596 buffer.writeFlattenable(fProxy);
597 buffer.write32(fPMColor);
598 }
reed@google.com82065d62011-02-07 15:30:46 +0000599
600 virtual Factory getFactory() {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000601 return CreateProc;
602 }
603
604private:
reed@google.com82065d62011-02-07 15:30:46 +0000605 static SkFlattenable* CreateProc(SkFlattenableReadBuffer& buffer) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000606 return SkNEW_ARGS(Sk3DShader, (buffer));
607 }
608
609 SkShader* fProxy;
610 SkPMColor fPMColor;
611 const SkMask* fMask;
612
613 typedef SkShader INHERITED;
614};
615
616class Sk3DBlitter : public SkBlitter {
617public:
618 Sk3DBlitter(SkBlitter* proxy, Sk3DShader* shader, void (*killProc)(void*))
reed@google.com82065d62011-02-07 15:30:46 +0000619 : fProxy(proxy), f3DShader(shader), fKillProc(killProc) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000620 shader->ref();
621 }
reed@google.com82065d62011-02-07 15:30:46 +0000622
623 virtual ~Sk3DBlitter() {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000624 f3DShader->unref();
625 fKillProc(fProxy);
626 }
627
reed@google.com82065d62011-02-07 15:30:46 +0000628 virtual void blitH(int x, int y, int width) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000629 fProxy->blitH(x, y, width);
630 }
reed@google.com82065d62011-02-07 15:30:46 +0000631
632 virtual void blitAntiH(int x, int y, const SkAlpha antialias[],
633 const int16_t runs[]) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000634 fProxy->blitAntiH(x, y, antialias, runs);
635 }
reed@google.com82065d62011-02-07 15:30:46 +0000636
637 virtual void blitV(int x, int y, int height, SkAlpha alpha) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000638 fProxy->blitV(x, y, height, alpha);
639 }
reed@google.com82065d62011-02-07 15:30:46 +0000640
641 virtual void blitRect(int x, int y, int width, int height) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000642 fProxy->blitRect(x, y, width, height);
643 }
reed@google.com82065d62011-02-07 15:30:46 +0000644
645 virtual void blitMask(const SkMask& mask, const SkIRect& clip) {
646 if (mask.fFormat == SkMask::k3D_Format) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000647 f3DShader->setMask(&mask);
648
649 ((SkMask*)&mask)->fFormat = SkMask::kA8_Format;
650 fProxy->blitMask(mask, clip);
651 ((SkMask*)&mask)->fFormat = SkMask::k3D_Format;
652
653 f3DShader->setMask(NULL);
reed@google.com82065d62011-02-07 15:30:46 +0000654 } else {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000655 fProxy->blitMask(mask, clip);
reed@google.com82065d62011-02-07 15:30:46 +0000656 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000657 }
reed@google.com82065d62011-02-07 15:30:46 +0000658
reed@android.com8a1c16f2008-12-17 15:59:43 +0000659private:
660 SkBlitter* fProxy;
661 Sk3DShader* f3DShader;
662 void (*fKillProc)(void*);
663};
664
reed@google.com82065d62011-02-07 15:30:46 +0000665///////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +0000666
667#include "SkCoreBlitters.h"
668
reed@android.com8a1c16f2008-12-17 15:59:43 +0000669class SkAutoCallProc {
670public:
671 typedef void (*Proc)(void*);
reed@google.com82065d62011-02-07 15:30:46 +0000672
reed@android.com8a1c16f2008-12-17 15:59:43 +0000673 SkAutoCallProc(void* obj, Proc proc)
reed@google.com82065d62011-02-07 15:30:46 +0000674 : fObj(obj), fProc(proc) {}
675
676 ~SkAutoCallProc() {
677 if (fObj && fProc) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000678 fProc(fObj);
reed@google.com82065d62011-02-07 15:30:46 +0000679 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000680 }
reed@google.com82065d62011-02-07 15:30:46 +0000681
reed@android.com8a1c16f2008-12-17 15:59:43 +0000682 void* get() const { return fObj; }
reed@google.com82065d62011-02-07 15:30:46 +0000683
684 void* detach() {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000685 void* obj = fObj;
686 fObj = NULL;
687 return obj;
688 }
reed@google.com82065d62011-02-07 15:30:46 +0000689
reed@android.com8a1c16f2008-12-17 15:59:43 +0000690private:
691 void* fObj;
692 Proc fProc;
693};
694
reed@google.com82065d62011-02-07 15:30:46 +0000695static void destroy_blitter(void* blitter) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000696 ((SkBlitter*)blitter)->~SkBlitter();
697}
698
reed@google.com82065d62011-02-07 15:30:46 +0000699static void delete_blitter(void* blitter) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000700 SkDELETE((SkBlitter*)blitter);
701}
702
reed@android.comd252db02009-04-01 18:31:44 +0000703static bool just_solid_color(const SkPaint& paint) {
704 if (paint.getAlpha() == 0xFF && paint.getColorFilter() == NULL) {
705 SkShader* shader = paint.getShader();
706 if (NULL == shader ||
707 (shader->getFlags() & SkShader::kOpaqueAlpha_Flag)) {
708 return true;
709 }
710 }
711 return false;
712}
reed@google.com82065d62011-02-07 15:30:46 +0000713
reed@android.comd252db02009-04-01 18:31:44 +0000714/** By analyzing the paint (with an xfermode), we may decide we can take
715 special action. This enum lists our possible actions
716 */
717enum XferInterp {
718 kNormal_XferInterp, // no special interpretation, draw normally
719 kSrcOver_XferInterp, // draw as if in srcover mode
720 kSkipDrawing_XferInterp // draw nothing
721};
722
723static XferInterp interpret_xfermode(const SkPaint& paint, SkXfermode* xfer,
724 SkBitmap::Config deviceConfig) {
reed@android.com845fdac2009-06-23 03:01:32 +0000725 SkXfermode::Mode mode;
reed@google.com82065d62011-02-07 15:30:46 +0000726
reed@android.com845fdac2009-06-23 03:01:32 +0000727 if (SkXfermode::IsMode(xfer, &mode)) {
reed@android.comd252db02009-04-01 18:31:44 +0000728 switch (mode) {
reed@android.com845fdac2009-06-23 03:01:32 +0000729 case SkXfermode::kSrc_Mode:
reed@android.comd252db02009-04-01 18:31:44 +0000730 if (just_solid_color(paint)) {
731 return kSrcOver_XferInterp;
732 }
733 break;
reed@android.com845fdac2009-06-23 03:01:32 +0000734 case SkXfermode::kDst_Mode:
reed@android.comd252db02009-04-01 18:31:44 +0000735 return kSkipDrawing_XferInterp;
reed@android.com845fdac2009-06-23 03:01:32 +0000736 case SkXfermode::kSrcOver_Mode:
reed@android.comd252db02009-04-01 18:31:44 +0000737 return kSrcOver_XferInterp;
reed@android.com845fdac2009-06-23 03:01:32 +0000738 case SkXfermode::kDstOver_Mode:
reed@android.comd252db02009-04-01 18:31:44 +0000739 if (SkBitmap::kRGB_565_Config == deviceConfig) {
740 return kSkipDrawing_XferInterp;
741 }
742 break;
reed@android.com845fdac2009-06-23 03:01:32 +0000743 case SkXfermode::kSrcIn_Mode:
reed@android.comd252db02009-04-01 18:31:44 +0000744 if (SkBitmap::kRGB_565_Config == deviceConfig &&
745 just_solid_color(paint)) {
746 return kSrcOver_XferInterp;
747 }
748 break;
reed@android.com845fdac2009-06-23 03:01:32 +0000749 case SkXfermode::kDstIn_Mode:
reed@android.comd252db02009-04-01 18:31:44 +0000750 if (just_solid_color(paint)) {
751 return kSkipDrawing_XferInterp;
752 }
753 break;
754 default:
755 break;
756 }
757 }
758 return kNormal_XferInterp;
759}
760
reed@android.com8a1c16f2008-12-17 15:59:43 +0000761SkBlitter* SkBlitter::Choose(const SkBitmap& device,
762 const SkMatrix& matrix,
reed@google.com6b7aee32011-04-19 18:36:09 +0000763 const SkPaint& origPaint,
reed@google.com82065d62011-02-07 15:30:46 +0000764 void* storage, size_t storageSize) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000765 SkASSERT(storageSize == 0 || storage != NULL);
766
767 SkBlitter* blitter = NULL;
768
769 // which check, in case we're being called by a client with a dummy device
770 // (e.g. they have a bounder that always aborts the draw)
reed@google.com82065d62011-02-07 15:30:46 +0000771 if (SkBitmap::kNo_Config == device.getConfig()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000772 SK_PLACEMENT_NEW(blitter, SkNullBlitter, storage, storageSize);
773 return blitter;
774 }
775
reed@google.com6b7aee32011-04-19 18:36:09 +0000776 SkPaint paint(origPaint);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000777 SkShader* shader = paint.getShader();
reed@google.com6b7aee32011-04-19 18:36:09 +0000778 SkColorFilter* cf = paint.getColorFilter();
779 SkXfermode* mode = paint.getXfermode();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000780
781 Sk3DShader* shader3D = NULL;
reed@google.com82065d62011-02-07 15:30:46 +0000782 if (paint.getMaskFilter() != NULL &&
783 paint.getMaskFilter()->getFormat() == SkMask::k3D_Format) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000784 shader3D = SkNEW_ARGS(Sk3DShader, (shader));
reed@google.com6b7aee32011-04-19 18:36:09 +0000785 paint.setShader(shader3D)->unref();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000786 shader = shader3D;
787 }
788
reed@android.comd252db02009-04-01 18:31:44 +0000789 if (NULL != mode) {
790 switch (interpret_xfermode(paint, mode, device.config())) {
791 case kSrcOver_XferInterp:
792 mode = NULL;
reed@google.com6b7aee32011-04-19 18:36:09 +0000793 paint.setXfermode(NULL);
reed@android.comd252db02009-04-01 18:31:44 +0000794 break;
795 case kSkipDrawing_XferInterp:
796 SK_PLACEMENT_NEW(blitter, SkNullBlitter, storage, storageSize);
797 return blitter;
798 default:
799 break;
800 }
801 }
802
reed@google.com6b7aee32011-04-19 18:36:09 +0000803 if (NULL == shader) {
804#ifdef SK_IGNORE_CF_OPTIMIZATION
805 if (mode || cf) {
806#else
807 if (mode) {
808#endif
809 // xfermodes (and filters) require shaders for our current blitters
810 shader = SkNEW(SkColorShader);
811 paint.setShader(shader)->unref();
812 } else if (cf) {
813 // if no shader && no xfermode, we just apply the colorfilter to
814 // our color and move on.
815 paint.setColor(cf->filterColor(paint.getColor()));
816 paint.setColorFilter(NULL);
817 cf = NULL;
818 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000819 }
reed@google.com82065d62011-02-07 15:30:46 +0000820
reed@google.com6b7aee32011-04-19 18:36:09 +0000821 if (cf) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000822 SkASSERT(shader);
reed@google.com6b7aee32011-04-19 18:36:09 +0000823 shader = SkNEW_ARGS(SkFilterShader, (shader, cf));
824 paint.setShader(shader)->unref();
reed@android.com1fc4c602009-10-02 16:34:57 +0000825 // blitters should ignore the presence/absence of a filter, since
826 // if there is one, the shader will take care of it.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000827 }
reed@google.com82065d62011-02-07 15:30:46 +0000828
reed@android.comcafc9f92009-08-22 03:44:57 +0000829 if (shader && !shader->setContext(device, paint, matrix)) {
830 return SkNEW(SkNullBlitter);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000831 }
832
833 switch (device.getConfig()) {
reed@google.com82065d62011-02-07 15:30:46 +0000834 case SkBitmap::kA1_Config:
835 SK_PLACEMENT_NEW_ARGS(blitter, SkA1_Blitter,
836 storage, storageSize, (device, paint));
837 break;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000838
reed@google.com82065d62011-02-07 15:30:46 +0000839 case SkBitmap::kA8_Config:
840 if (shader) {
841 SK_PLACEMENT_NEW_ARGS(blitter, SkA8_Shader_Blitter,
842 storage, storageSize, (device, paint));
843 } else {
844 SK_PLACEMENT_NEW_ARGS(blitter, SkA8_Blitter,
845 storage, storageSize, (device, paint));
846 }
847 break;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000848
reed@google.com82065d62011-02-07 15:30:46 +0000849 case SkBitmap::kARGB_4444_Config:
850 blitter = SkBlitter_ChooseD4444(device, paint, storage, storageSize);
851 break;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000852
reed@google.com82065d62011-02-07 15:30:46 +0000853 case SkBitmap::kRGB_565_Config:
854 blitter = SkBlitter_ChooseD565(device, paint, storage, storageSize);
855 break;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000856
reed@google.com82065d62011-02-07 15:30:46 +0000857 case SkBitmap::kARGB_8888_Config:
858 if (shader) {
859 SK_PLACEMENT_NEW_ARGS(blitter, SkARGB32_Shader_Blitter,
860 storage, storageSize, (device, paint));
861 } else if (paint.getColor() == SK_ColorBLACK) {
862 SK_PLACEMENT_NEW_ARGS(blitter, SkARGB32_Black_Blitter,
863 storage, storageSize, (device, paint));
864 } else if (paint.getAlpha() == 0xFF) {
865 SK_PLACEMENT_NEW_ARGS(blitter, SkARGB32_Opaque_Blitter,
866 storage, storageSize, (device, paint));
867 } else {
868 SK_PLACEMENT_NEW_ARGS(blitter, SkARGB32_Blitter,
869 storage, storageSize, (device, paint));
870 }
871 break;
872
873 default:
874 SkASSERT(!"unsupported device config");
875 SK_PLACEMENT_NEW(blitter, SkNullBlitter, storage, storageSize);
876 break;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000877 }
878
reed@google.com82065d62011-02-07 15:30:46 +0000879 if (shader3D) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000880 void (*proc)(void*) = ((void*)storage == (void*)blitter) ? destroy_blitter : delete_blitter;
881 SkAutoCallProc tmp(blitter, proc);
882
883 blitter = SkNEW_ARGS(Sk3DBlitter, (blitter, shader3D, proc));
884 (void)tmp.detach();
885 }
886 return blitter;
887}
888
reed@google.com82065d62011-02-07 15:30:46 +0000889///////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +0000890
891const uint16_t gMask_0F0F = 0xF0F;
892const uint32_t gMask_00FF00FF = 0xFF00FF;
893
reed@google.com82065d62011-02-07 15:30:46 +0000894///////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +0000895
896SkShaderBlitter::SkShaderBlitter(const SkBitmap& device, const SkPaint& paint)
reed@android.com5119bdb2009-06-12 21:27:03 +0000897 : INHERITED(device) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000898 fShader = paint.getShader();
899 SkASSERT(fShader);
900
901 fShader->ref();
902 fShader->beginSession();
reed@android.com5119bdb2009-06-12 21:27:03 +0000903 fShaderFlags = fShader->getFlags();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000904}
905
reed@android.com5119bdb2009-06-12 21:27:03 +0000906SkShaderBlitter::~SkShaderBlitter() {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000907 fShader->endSession();
908 fShader->unref();
909}
910