epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
| 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.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 9 | |
| 10 | #include "SkBlitter.h" |
| 11 | #include "SkAntiRun.h" |
| 12 | #include "SkColor.h" |
| 13 | #include "SkColorFilter.h" |
commit-bot@chromium.org | a5572e5 | 2014-03-07 03:24:41 +0000 | [diff] [blame] | 14 | #include "SkCoreBlitters.h" |
reed@google.com | 02f65f2 | 2012-08-06 21:20:05 +0000 | [diff] [blame] | 15 | #include "SkFilterShader.h" |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 16 | #include "SkReadBuffer.h" |
| 17 | #include "SkWriteBuffer.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 18 | #include "SkMask.h" |
| 19 | #include "SkMaskFilter.h" |
commit-bot@chromium.org | a5572e5 | 2014-03-07 03:24:41 +0000 | [diff] [blame] | 20 | #include "SkString.h" |
reed@google.com | 4588490 | 2012-05-11 14:38:51 +0000 | [diff] [blame] | 21 | #include "SkTLazy.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 22 | #include "SkUtils.h" |
| 23 | #include "SkXfermode.h" |
| 24 | |
reed@android.com | 845fdac | 2009-06-23 03:01:32 +0000 | [diff] [blame] | 25 | SkBlitter::~SkBlitter() {} |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 26 | |
reed@google.com | ea03360 | 2012-12-14 13:13:55 +0000 | [diff] [blame] | 27 | bool SkBlitter::isNullBlitter() const { return false; } |
| 28 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 29 | const SkBitmap* SkBlitter::justAnOpaqueColor(uint32_t* value) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 30 | return NULL; |
| 31 | } |
| 32 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 33 | void SkBlitter::blitH(int x, int y, int width) { |
tomhudson@google.com | 0c00f21 | 2011-12-28 14:59:50 +0000 | [diff] [blame] | 34 | SkDEBUGFAIL("unimplemented"); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 35 | } |
| 36 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 37 | void SkBlitter::blitAntiH(int x, int y, const SkAlpha antialias[], |
| 38 | const int16_t runs[]) { |
tomhudson@google.com | 0c00f21 | 2011-12-28 14:59:50 +0000 | [diff] [blame] | 39 | SkDEBUGFAIL("unimplemented"); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 40 | } |
| 41 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 42 | void SkBlitter::blitV(int x, int y, int height, SkAlpha alpha) { |
| 43 | if (alpha == 255) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 44 | this->blitRect(x, y, 1, height); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 45 | } else { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 46 | int16_t runs[2]; |
| 47 | runs[0] = 1; |
| 48 | runs[1] = 0; |
| 49 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 50 | while (--height >= 0) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 51 | this->blitAntiH(x, y++, &alpha, runs); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 52 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 53 | } |
| 54 | } |
| 55 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 56 | void SkBlitter::blitRect(int x, int y, int width, int height) { |
tomhudson@google.com | a31ac73 | 2011-12-29 16:09:31 +0000 | [diff] [blame] | 57 | SkASSERT(width > 0); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 58 | while (--height >= 0) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 59 | this->blitH(x, y++, width); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 60 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 61 | } |
| 62 | |
tomhudson@google.com | 49eac19 | 2011-12-27 13:59:20 +0000 | [diff] [blame] | 63 | /// Default implementation doesn't check for any easy optimizations |
| 64 | /// such as alpha == 0 or 255; also uses blitV(), which some subclasses |
| 65 | /// may not support. |
| 66 | void SkBlitter::blitAntiRect(int x, int y, int width, int height, |
| 67 | SkAlpha leftAlpha, SkAlpha rightAlpha) { |
tomhudson@google.com | 4714359 | 2011-12-28 17:58:07 +0000 | [diff] [blame] | 68 | this->blitV(x++, y, height, leftAlpha); |
tomhudson@google.com | a31ac73 | 2011-12-29 16:09:31 +0000 | [diff] [blame] | 69 | if (width > 0) { |
tomhudson@google.com | 4714359 | 2011-12-28 17:58:07 +0000 | [diff] [blame] | 70 | this->blitRect(x, y, width, height); |
| 71 | x += width; |
| 72 | } |
| 73 | this->blitV(x, y, height, rightAlpha); |
tomhudson@google.com | 49eac19 | 2011-12-27 13:59:20 +0000 | [diff] [blame] | 74 | } |
| 75 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 76 | ////////////////////////////////////////////////////////////////////////////// |
| 77 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 78 | static inline void bits_to_runs(SkBlitter* blitter, int x, int y, |
| 79 | const uint8_t bits[], |
| 80 | U8CPU left_mask, int rowBytes, |
| 81 | U8CPU right_mask) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 82 | int inFill = 0; |
| 83 | int pos = 0; |
| 84 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 85 | while (--rowBytes >= 0) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 86 | unsigned b = *bits++ & left_mask; |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 87 | if (rowBytes == 0) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 88 | b &= right_mask; |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 89 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 90 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 91 | for (unsigned test = 0x80; test != 0; test >>= 1) { |
| 92 | if (b & test) { |
| 93 | if (!inFill) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 94 | pos = x; |
| 95 | inFill = true; |
| 96 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 97 | } else { |
| 98 | if (inFill) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 99 | blitter->blitH(pos, y, x - pos); |
| 100 | inFill = false; |
| 101 | } |
| 102 | } |
| 103 | x += 1; |
| 104 | } |
| 105 | left_mask = 0xFF; |
| 106 | } |
| 107 | |
| 108 | // final cleanup |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 109 | if (inFill) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 110 | blitter->blitH(pos, y, x - pos); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 111 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 112 | } |
| 113 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 114 | void SkBlitter::blitMask(const SkMask& mask, const SkIRect& clip) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 115 | SkASSERT(mask.fBounds.contains(clip)); |
| 116 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 117 | if (mask.fFormat == SkMask::kBW_Format) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 118 | int cx = clip.fLeft; |
| 119 | int cy = clip.fTop; |
| 120 | int maskLeft = mask.fBounds.fLeft; |
| 121 | int mask_rowBytes = mask.fRowBytes; |
| 122 | int height = clip.height(); |
| 123 | |
| 124 | const uint8_t* bits = mask.getAddr1(cx, cy); |
| 125 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 126 | if (cx == maskLeft && clip.fRight == mask.fBounds.fRight) { |
| 127 | while (--height >= 0) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 128 | bits_to_runs(this, cx, cy, bits, 0xFF, mask_rowBytes, 0xFF); |
| 129 | bits += mask_rowBytes; |
| 130 | cy += 1; |
| 131 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 132 | } else { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 133 | int left_edge = cx - maskLeft; |
| 134 | SkASSERT(left_edge >= 0); |
| 135 | int rite_edge = clip.fRight - maskLeft; |
| 136 | SkASSERT(rite_edge > left_edge); |
| 137 | |
| 138 | int left_mask = 0xFF >> (left_edge & 7); |
| 139 | int rite_mask = 0xFF << (8 - (rite_edge & 7)); |
| 140 | int full_runs = (rite_edge >> 3) - ((left_edge + 7) >> 3); |
| 141 | |
reed@google.com | a89c77b | 2011-12-01 21:47:26 +0000 | [diff] [blame] | 142 | // check for empty right mask, so we don't read off the end (or go slower than we need to) |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 143 | if (rite_mask == 0) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 144 | SkASSERT(full_runs >= 0); |
| 145 | full_runs -= 1; |
| 146 | rite_mask = 0xFF; |
| 147 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 148 | if (left_mask == 0xFF) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 149 | full_runs -= 1; |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 150 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 151 | |
reed@google.com | a89c77b | 2011-12-01 21:47:26 +0000 | [diff] [blame] | 152 | // back up manually so we can keep in sync with our byte-aligned src |
| 153 | // have cx reflect our actual starting x-coord |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 154 | cx -= left_edge & 7; |
| 155 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 156 | if (full_runs < 0) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 157 | SkASSERT((left_mask & rite_mask) != 0); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 158 | while (--height >= 0) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 159 | bits_to_runs(this, cx, cy, bits, left_mask, 1, rite_mask); |
| 160 | bits += mask_rowBytes; |
| 161 | cy += 1; |
| 162 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 163 | } else { |
| 164 | while (--height >= 0) { |
reed@google.com | a89c77b | 2011-12-01 21:47:26 +0000 | [diff] [blame] | 165 | bits_to_runs(this, cx, cy, bits, left_mask, full_runs + 2, rite_mask); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 166 | bits += mask_rowBytes; |
| 167 | cy += 1; |
| 168 | } |
| 169 | } |
| 170 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 171 | } else { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 172 | int width = clip.width(); |
| 173 | SkAutoSTMalloc<64, int16_t> runStorage(width + 1); |
| 174 | int16_t* runs = runStorage.get(); |
reed@google.com | 7989186 | 2011-10-18 15:44:57 +0000 | [diff] [blame] | 175 | const uint8_t* aa = mask.getAddr8(clip.fLeft, clip.fTop); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 176 | |
| 177 | sk_memset16((uint16_t*)runs, 1, width); |
| 178 | runs[width] = 0; |
| 179 | |
| 180 | int height = clip.height(); |
| 181 | int y = clip.fTop; |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 182 | while (--height >= 0) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 183 | this->blitAntiH(clip.fLeft, y, aa, runs); |
| 184 | aa += mask.fRowBytes; |
| 185 | y += 1; |
| 186 | } |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | /////////////////////// these guys are not virtual, just a helpers |
| 191 | |
| 192 | void SkBlitter::blitMaskRegion(const SkMask& mask, const SkRegion& clip) { |
| 193 | if (clip.quickReject(mask.fBounds)) { |
| 194 | return; |
| 195 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 196 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 197 | SkRegion::Cliperator clipper(clip, mask.fBounds); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 198 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 199 | while (!clipper.done()) { |
| 200 | const SkIRect& cr = clipper.rect(); |
| 201 | this->blitMask(mask, cr); |
| 202 | clipper.next(); |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | void SkBlitter::blitRectRegion(const SkIRect& rect, const SkRegion& clip) { |
| 207 | SkRegion::Cliperator clipper(clip, rect); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 208 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 209 | while (!clipper.done()) { |
| 210 | const SkIRect& cr = clipper.rect(); |
| 211 | this->blitRect(cr.fLeft, cr.fTop, cr.width(), cr.height()); |
| 212 | clipper.next(); |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | void SkBlitter::blitRegion(const SkRegion& clip) { |
| 217 | SkRegion::Iterator iter(clip); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 218 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 219 | while (!iter.done()) { |
| 220 | const SkIRect& cr = iter.rect(); |
| 221 | this->blitRect(cr.fLeft, cr.fTop, cr.width(), cr.height()); |
| 222 | iter.next(); |
| 223 | } |
| 224 | } |
| 225 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 226 | /////////////////////////////////////////////////////////////////////////////// |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 227 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 228 | void SkNullBlitter::blitH(int x, int y, int width) {} |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 229 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 230 | void SkNullBlitter::blitAntiH(int x, int y, const SkAlpha antialias[], |
| 231 | const int16_t runs[]) {} |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 232 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 233 | void SkNullBlitter::blitV(int x, int y, int height, SkAlpha alpha) {} |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 234 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 235 | void SkNullBlitter::blitRect(int x, int y, int width, int height) {} |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 236 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 237 | void SkNullBlitter::blitMask(const SkMask& mask, const SkIRect& clip) {} |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 238 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 239 | const SkBitmap* SkNullBlitter::justAnOpaqueColor(uint32_t* value) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 240 | return NULL; |
| 241 | } |
| 242 | |
reed@google.com | ea03360 | 2012-12-14 13:13:55 +0000 | [diff] [blame] | 243 | bool SkNullBlitter::isNullBlitter() const { return true; } |
| 244 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 245 | /////////////////////////////////////////////////////////////////////////////// |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 246 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 247 | static int compute_anti_width(const int16_t runs[]) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 248 | int width = 0; |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 249 | |
| 250 | for (;;) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 251 | int count = runs[0]; |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 252 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 253 | SkASSERT(count >= 0); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 254 | if (count == 0) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 255 | break; |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 256 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 257 | width += count; |
| 258 | runs += count; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 259 | } |
| 260 | return width; |
| 261 | } |
| 262 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 263 | static inline bool y_in_rect(int y, const SkIRect& rect) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 264 | return (unsigned)(y - rect.fTop) < (unsigned)rect.height(); |
| 265 | } |
| 266 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 267 | static inline bool x_in_rect(int x, const SkIRect& rect) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 268 | return (unsigned)(x - rect.fLeft) < (unsigned)rect.width(); |
| 269 | } |
| 270 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 271 | void SkRectClipBlitter::blitH(int left, int y, int width) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 272 | SkASSERT(width > 0); |
| 273 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 274 | if (!y_in_rect(y, fClipRect)) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 275 | return; |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 276 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 277 | |
| 278 | int right = left + width; |
| 279 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 280 | if (left < fClipRect.fLeft) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 281 | left = fClipRect.fLeft; |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 282 | } |
| 283 | if (right > fClipRect.fRight) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 284 | right = fClipRect.fRight; |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 285 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 286 | |
| 287 | width = right - left; |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 288 | if (width > 0) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 289 | fBlitter->blitH(left, y, width); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 290 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 291 | } |
| 292 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 293 | void SkRectClipBlitter::blitAntiH(int left, int y, const SkAlpha aa[], |
| 294 | const int16_t runs[]) { |
| 295 | if (!y_in_rect(y, fClipRect) || left >= fClipRect.fRight) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 296 | return; |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 297 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 298 | |
| 299 | int x0 = left; |
| 300 | int x1 = left + compute_anti_width(runs); |
| 301 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 302 | if (x1 <= fClipRect.fLeft) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 303 | return; |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 304 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 305 | |
| 306 | SkASSERT(x0 < x1); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 307 | if (x0 < fClipRect.fLeft) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 308 | int dx = fClipRect.fLeft - x0; |
| 309 | SkAlphaRuns::BreakAt((int16_t*)runs, (uint8_t*)aa, dx); |
| 310 | runs += dx; |
| 311 | aa += dx; |
| 312 | x0 = fClipRect.fLeft; |
| 313 | } |
| 314 | |
| 315 | SkASSERT(x0 < x1 && runs[x1 - x0] == 0); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 316 | if (x1 > fClipRect.fRight) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 317 | x1 = fClipRect.fRight; |
| 318 | SkAlphaRuns::BreakAt((int16_t*)runs, (uint8_t*)aa, x1 - x0); |
| 319 | ((int16_t*)runs)[x1 - x0] = 0; |
| 320 | } |
| 321 | |
| 322 | SkASSERT(x0 < x1 && runs[x1 - x0] == 0); |
| 323 | SkASSERT(compute_anti_width(runs) == x1 - x0); |
| 324 | |
| 325 | fBlitter->blitAntiH(x0, y, aa, runs); |
| 326 | } |
| 327 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 328 | void SkRectClipBlitter::blitV(int x, int y, int height, SkAlpha alpha) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 329 | SkASSERT(height > 0); |
| 330 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 331 | if (!x_in_rect(x, fClipRect)) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 332 | return; |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 333 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 334 | |
| 335 | int y0 = y; |
| 336 | int y1 = y + height; |
| 337 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 338 | if (y0 < fClipRect.fTop) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 339 | y0 = fClipRect.fTop; |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 340 | } |
| 341 | if (y1 > fClipRect.fBottom) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 342 | y1 = fClipRect.fBottom; |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 343 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 344 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 345 | if (y0 < y1) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 346 | fBlitter->blitV(x, y0, y1 - y0, alpha); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 347 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 348 | } |
| 349 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 350 | void SkRectClipBlitter::blitRect(int left, int y, int width, int height) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 351 | SkIRect r; |
| 352 | |
| 353 | r.set(left, y, left + width, y + height); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 354 | if (r.intersect(fClipRect)) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 355 | fBlitter->blitRect(r.fLeft, r.fTop, r.width(), r.height()); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 356 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 357 | } |
| 358 | |
tomhudson@google.com | 49eac19 | 2011-12-27 13:59:20 +0000 | [diff] [blame] | 359 | void SkRectClipBlitter::blitAntiRect(int left, int y, int width, int height, |
| 360 | SkAlpha leftAlpha, SkAlpha rightAlpha) { |
| 361 | SkIRect r; |
| 362 | |
| 363 | // The *true* width of the rectangle blitted is width+2: |
| 364 | r.set(left, y, left + width + 2, y + height); |
| 365 | if (r.intersect(fClipRect)) { |
| 366 | if (r.fLeft != left) { |
| 367 | SkASSERT(r.fLeft > left); |
| 368 | leftAlpha = 255; |
| 369 | } |
| 370 | if (r.fRight != left + width + 2) { |
| 371 | SkASSERT(r.fRight < left + width + 2); |
| 372 | rightAlpha = 255; |
| 373 | } |
| 374 | if (255 == leftAlpha && 255 == rightAlpha) { |
| 375 | fBlitter->blitRect(r.fLeft, r.fTop, r.width(), r.height()); |
| 376 | } else if (1 == r.width()) { |
| 377 | if (r.fLeft == left) { |
| 378 | fBlitter->blitV(r.fLeft, r.fTop, r.height(), leftAlpha); |
| 379 | } else { |
| 380 | SkASSERT(r.fLeft == left + width + 1); |
| 381 | fBlitter->blitV(r.fLeft, r.fTop, r.height(), rightAlpha); |
| 382 | } |
| 383 | } else { |
| 384 | fBlitter->blitAntiRect(r.fLeft, r.fTop, r.width() - 2, r.height(), |
| 385 | leftAlpha, rightAlpha); |
| 386 | } |
| 387 | } |
| 388 | } |
| 389 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 390 | void SkRectClipBlitter::blitMask(const SkMask& mask, const SkIRect& clip) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 391 | SkASSERT(mask.fBounds.contains(clip)); |
| 392 | |
| 393 | SkIRect r = clip; |
| 394 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 395 | if (r.intersect(fClipRect)) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 396 | fBlitter->blitMask(mask, r); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 397 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 398 | } |
| 399 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 400 | const SkBitmap* SkRectClipBlitter::justAnOpaqueColor(uint32_t* value) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 401 | return fBlitter->justAnOpaqueColor(value); |
| 402 | } |
| 403 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 404 | /////////////////////////////////////////////////////////////////////////////// |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 405 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 406 | void SkRgnClipBlitter::blitH(int x, int y, int width) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 407 | SkRegion::Spanerator span(*fRgn, y, x, x + width); |
| 408 | int left, right; |
| 409 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 410 | while (span.next(&left, &right)) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 411 | SkASSERT(left < right); |
| 412 | fBlitter->blitH(left, y, right - left); |
| 413 | } |
| 414 | } |
| 415 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 416 | void SkRgnClipBlitter::blitAntiH(int x, int y, const SkAlpha aa[], |
| 417 | const int16_t runs[]) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 418 | int width = compute_anti_width(runs); |
| 419 | SkRegion::Spanerator span(*fRgn, y, x, x + width); |
| 420 | int left, right; |
| 421 | SkDEBUGCODE(const SkIRect& bounds = fRgn->getBounds();) |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 422 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 423 | int prevRite = x; |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 424 | while (span.next(&left, &right)) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 425 | SkASSERT(x <= left); |
| 426 | SkASSERT(left < right); |
| 427 | SkASSERT(left >= bounds.fLeft && right <= bounds.fRight); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 428 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 429 | SkAlphaRuns::Break((int16_t*)runs, (uint8_t*)aa, left - x, right - left); |
| 430 | |
| 431 | // now zero before left |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 432 | if (left > prevRite) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 433 | int index = prevRite - x; |
| 434 | ((uint8_t*)aa)[index] = 0; // skip runs after right |
| 435 | ((int16_t*)runs)[index] = SkToS16(left - prevRite); |
| 436 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 437 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 438 | prevRite = right; |
| 439 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 440 | |
| 441 | if (prevRite > x) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 442 | ((int16_t*)runs)[prevRite - x] = 0; |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 443 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 444 | if (x < 0) { |
| 445 | int skip = runs[0]; |
| 446 | SkASSERT(skip >= -x); |
| 447 | aa += skip; |
| 448 | runs += skip; |
| 449 | x += skip; |
| 450 | } |
| 451 | fBlitter->blitAntiH(x, y, aa, runs); |
| 452 | } |
| 453 | } |
| 454 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 455 | void SkRgnClipBlitter::blitV(int x, int y, int height, SkAlpha alpha) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 456 | SkIRect bounds; |
| 457 | bounds.set(x, y, x + 1, y + height); |
| 458 | |
| 459 | SkRegion::Cliperator iter(*fRgn, bounds); |
| 460 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 461 | while (!iter.done()) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 462 | const SkIRect& r = iter.rect(); |
| 463 | SkASSERT(bounds.contains(r)); |
| 464 | |
| 465 | fBlitter->blitV(x, r.fTop, r.height(), alpha); |
| 466 | iter.next(); |
| 467 | } |
| 468 | } |
| 469 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 470 | void SkRgnClipBlitter::blitRect(int x, int y, int width, int height) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 471 | SkIRect bounds; |
| 472 | bounds.set(x, y, x + width, y + height); |
| 473 | |
| 474 | SkRegion::Cliperator iter(*fRgn, bounds); |
| 475 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 476 | while (!iter.done()) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 477 | const SkIRect& r = iter.rect(); |
| 478 | SkASSERT(bounds.contains(r)); |
| 479 | |
| 480 | fBlitter->blitRect(r.fLeft, r.fTop, r.width(), r.height()); |
| 481 | iter.next(); |
| 482 | } |
| 483 | } |
| 484 | |
tomhudson@google.com | 49eac19 | 2011-12-27 13:59:20 +0000 | [diff] [blame] | 485 | void SkRgnClipBlitter::blitAntiRect(int x, int y, int width, int height, |
| 486 | SkAlpha leftAlpha, SkAlpha rightAlpha) { |
| 487 | // The *true* width of the rectangle to blit is width + 2 |
| 488 | SkIRect bounds; |
| 489 | bounds.set(x, y, x + width + 2, y + height); |
| 490 | |
| 491 | SkRegion::Cliperator iter(*fRgn, bounds); |
| 492 | |
| 493 | while (!iter.done()) { |
| 494 | const SkIRect& r = iter.rect(); |
| 495 | SkASSERT(bounds.contains(r)); |
| 496 | SkASSERT(r.fLeft >= x); |
tomhudson@google.com | 31bab39 | 2012-01-03 20:12:42 +0000 | [diff] [blame] | 497 | SkASSERT(r.fRight <= x + width + 2); |
tomhudson@google.com | 49eac19 | 2011-12-27 13:59:20 +0000 | [diff] [blame] | 498 | |
| 499 | SkAlpha effectiveLeftAlpha = (r.fLeft == x) ? leftAlpha : 255; |
| 500 | SkAlpha effectiveRightAlpha = (r.fRight == x + width + 2) ? |
| 501 | rightAlpha : 255; |
| 502 | |
| 503 | if (255 == effectiveLeftAlpha && 255 == effectiveRightAlpha) { |
| 504 | fBlitter->blitRect(r.fLeft, r.fTop, r.width(), r.height()); |
| 505 | } else if (1 == r.width()) { |
| 506 | if (r.fLeft == x) { |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 507 | fBlitter->blitV(r.fLeft, r.fTop, r.height(), |
tomhudson@google.com | 49eac19 | 2011-12-27 13:59:20 +0000 | [diff] [blame] | 508 | effectiveLeftAlpha); |
| 509 | } else { |
| 510 | SkASSERT(r.fLeft == x + width + 1); |
| 511 | fBlitter->blitV(r.fLeft, r.fTop, r.height(), |
| 512 | effectiveRightAlpha); |
| 513 | } |
| 514 | } else { |
| 515 | fBlitter->blitAntiRect(r.fLeft, r.fTop, r.width() - 2, r.height(), |
| 516 | effectiveLeftAlpha, effectiveRightAlpha); |
| 517 | } |
| 518 | iter.next(); |
| 519 | } |
| 520 | } |
| 521 | |
| 522 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 523 | void SkRgnClipBlitter::blitMask(const SkMask& mask, const SkIRect& clip) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 524 | SkASSERT(mask.fBounds.contains(clip)); |
| 525 | |
| 526 | SkRegion::Cliperator iter(*fRgn, clip); |
| 527 | const SkIRect& r = iter.rect(); |
| 528 | SkBlitter* blitter = fBlitter; |
| 529 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 530 | while (!iter.done()) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 531 | blitter->blitMask(mask, r); |
| 532 | iter.next(); |
| 533 | } |
| 534 | } |
| 535 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 536 | const SkBitmap* SkRgnClipBlitter::justAnOpaqueColor(uint32_t* value) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 537 | return fBlitter->justAnOpaqueColor(value); |
| 538 | } |
| 539 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 540 | /////////////////////////////////////////////////////////////////////////////// |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 541 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 542 | SkBlitter* SkBlitterClipper::apply(SkBlitter* blitter, const SkRegion* clip, |
| 543 | const SkIRect* ir) { |
| 544 | if (clip) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 545 | const SkIRect& clipR = clip->getBounds(); |
| 546 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 547 | if (clip->isEmpty() || (ir && !SkIRect::Intersects(clipR, *ir))) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 548 | blitter = &fNullBlitter; |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 549 | } else if (clip->isRect()) { |
| 550 | if (ir == NULL || !clipR.contains(*ir)) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 551 | fRectBlitter.init(blitter, clipR); |
| 552 | blitter = &fRectBlitter; |
| 553 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 554 | } else { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 555 | fRgnBlitter.init(blitter, clip); |
| 556 | blitter = &fRgnBlitter; |
| 557 | } |
| 558 | } |
| 559 | return blitter; |
| 560 | } |
| 561 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 562 | /////////////////////////////////////////////////////////////////////////////// |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 563 | |
| 564 | #include "SkColorShader.h" |
| 565 | #include "SkColorPriv.h" |
| 566 | |
| 567 | class Sk3DShader : public SkShader { |
| 568 | public: |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 569 | Sk3DShader(SkShader* proxy) : fProxy(proxy) { |
| 570 | SkSafeRef(proxy); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 571 | fMask = NULL; |
| 572 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 573 | |
| 574 | virtual ~Sk3DShader() { |
| 575 | SkSafeUnref(fProxy); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 576 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 577 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 578 | void setMask(const SkMask* mask) { fMask = mask; } |
| 579 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 580 | virtual bool setContext(const SkBitmap& device, const SkPaint& paint, |
reed@google.com | a641f3f | 2012-12-13 22:16:30 +0000 | [diff] [blame] | 581 | const SkMatrix& matrix) SK_OVERRIDE { |
| 582 | if (!this->INHERITED::setContext(device, paint, matrix)) { |
| 583 | return false; |
| 584 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 585 | if (fProxy) { |
reed@google.com | a641f3f | 2012-12-13 22:16:30 +0000 | [diff] [blame] | 586 | if (!fProxy->setContext(device, paint, matrix)) { |
| 587 | // must keep our set/end context calls balanced |
| 588 | this->INHERITED::endContext(); |
| 589 | return false; |
| 590 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 591 | } else { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 592 | fPMColor = SkPreMultiplyColor(paint.getColor()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 593 | } |
reed@google.com | a641f3f | 2012-12-13 22:16:30 +0000 | [diff] [blame] | 594 | return true; |
| 595 | } |
skia.committer@gmail.com | 61b05dc | 2012-12-14 02:02:06 +0000 | [diff] [blame] | 596 | |
reed@google.com | a641f3f | 2012-12-13 22:16:30 +0000 | [diff] [blame] | 597 | virtual void endContext() SK_OVERRIDE { |
| 598 | if (fProxy) { |
| 599 | fProxy->endContext(); |
| 600 | } |
| 601 | this->INHERITED::endContext(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 602 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 603 | |
reed@google.com | a641f3f | 2012-12-13 22:16:30 +0000 | [diff] [blame] | 604 | virtual void shadeSpan(int x, int y, SkPMColor span[], int count) SK_OVERRIDE { |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 605 | if (fProxy) { |
| 606 | fProxy->shadeSpan(x, y, span, count); |
| 607 | } |
| 608 | |
| 609 | if (fMask == NULL) { |
| 610 | if (fProxy == NULL) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 611 | sk_memset32(span, fPMColor, count); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 612 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 613 | return; |
| 614 | } |
| 615 | |
| 616 | SkASSERT(fMask->fBounds.contains(x, y)); |
| 617 | SkASSERT(fMask->fBounds.contains(x + count - 1, y)); |
| 618 | |
| 619 | size_t size = fMask->computeImageSize(); |
reed@google.com | 7989186 | 2011-10-18 15:44:57 +0000 | [diff] [blame] | 620 | const uint8_t* alpha = fMask->getAddr8(x, y); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 621 | const uint8_t* mulp = alpha + size; |
| 622 | const uint8_t* addp = mulp + size; |
| 623 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 624 | if (fProxy) { |
| 625 | for (int i = 0; i < count; i++) { |
| 626 | if (alpha[i]) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 627 | SkPMColor c = span[i]; |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 628 | if (c) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 629 | unsigned a = SkGetPackedA32(c); |
| 630 | unsigned r = SkGetPackedR32(c); |
| 631 | unsigned g = SkGetPackedG32(c); |
| 632 | unsigned b = SkGetPackedB32(c); |
| 633 | |
| 634 | unsigned mul = SkAlpha255To256(mulp[i]); |
| 635 | unsigned add = addp[i]; |
| 636 | |
| 637 | r = SkFastMin32(SkAlphaMul(r, mul) + add, a); |
| 638 | g = SkFastMin32(SkAlphaMul(g, mul) + add, a); |
| 639 | b = SkFastMin32(SkAlphaMul(b, mul) + add, a); |
| 640 | |
| 641 | span[i] = SkPackARGB32(a, r, g, b); |
| 642 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 643 | } else { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 644 | span[i] = 0; |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 645 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 646 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 647 | } else { // color |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 648 | unsigned a = SkGetPackedA32(fPMColor); |
| 649 | unsigned r = SkGetPackedR32(fPMColor); |
| 650 | unsigned g = SkGetPackedG32(fPMColor); |
| 651 | unsigned b = SkGetPackedB32(fPMColor); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 652 | for (int i = 0; i < count; i++) { |
| 653 | if (alpha[i]) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 654 | unsigned mul = SkAlpha255To256(mulp[i]); |
| 655 | unsigned add = addp[i]; |
| 656 | |
| 657 | span[i] = SkPackARGB32( a, |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 658 | SkFastMin32(SkAlphaMul(r, mul) + add, a), |
| 659 | SkFastMin32(SkAlphaMul(g, mul) + add, a), |
| 660 | SkFastMin32(SkAlphaMul(b, mul) + add, a)); |
| 661 | } else { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 662 | span[i] = 0; |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 663 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 664 | } |
| 665 | } |
| 666 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 667 | |
commit-bot@chromium.org | 0f10f7b | 2014-03-13 18:02:17 +0000 | [diff] [blame] | 668 | #ifndef SK_IGNORE_TO_STRING |
robertphillips@google.com | 76f9e93 | 2013-01-15 20:17:47 +0000 | [diff] [blame] | 669 | virtual void toString(SkString* str) const SK_OVERRIDE { |
| 670 | str->append("Sk3DShader: ("); |
| 671 | |
| 672 | if (NULL != fProxy) { |
| 673 | str->append("Proxy: "); |
| 674 | fProxy->toString(str); |
| 675 | } |
| 676 | |
| 677 | this->INHERITED::toString(str); |
| 678 | |
| 679 | str->append(")"); |
| 680 | } |
| 681 | #endif |
| 682 | |
djsollen@google.com | ba28d03 | 2012-03-26 17:57:35 +0000 | [diff] [blame] | 683 | SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(Sk3DShader) |
| 684 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 685 | protected: |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 686 | Sk3DShader(SkReadBuffer& buffer) : INHERITED(buffer) { |
reed@google.com | 3534822 | 2013-10-16 13:05:06 +0000 | [diff] [blame] | 687 | fProxy = buffer.readShader(); |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 688 | fPMColor = buffer.readColor(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 689 | fMask = NULL; |
| 690 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 691 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 692 | virtual void flatten(SkWriteBuffer& buffer) const SK_OVERRIDE { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 693 | this->INHERITED::flatten(buffer); |
| 694 | buffer.writeFlattenable(fProxy); |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 695 | buffer.writeColor(fPMColor); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 696 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 697 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 698 | private: |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 699 | SkShader* fProxy; |
| 700 | SkPMColor fPMColor; |
| 701 | const SkMask* fMask; |
| 702 | |
| 703 | typedef SkShader INHERITED; |
| 704 | }; |
| 705 | |
| 706 | class Sk3DBlitter : public SkBlitter { |
| 707 | public: |
commit-bot@chromium.org | a5572e5 | 2014-03-07 03:24:41 +0000 | [diff] [blame] | 708 | Sk3DBlitter(SkBlitter* proxy, Sk3DShader* shader) |
| 709 | : fProxy(proxy) |
| 710 | , f3DShader(SkRef(shader)) |
| 711 | {} |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 712 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 713 | virtual void blitH(int x, int y, int width) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 714 | fProxy->blitH(x, y, width); |
| 715 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 716 | |
| 717 | virtual void blitAntiH(int x, int y, const SkAlpha antialias[], |
| 718 | const int16_t runs[]) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 719 | fProxy->blitAntiH(x, y, antialias, runs); |
| 720 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 721 | |
| 722 | virtual void blitV(int x, int y, int height, SkAlpha alpha) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 723 | fProxy->blitV(x, y, height, alpha); |
| 724 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 725 | |
| 726 | virtual void blitRect(int x, int y, int width, int height) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 727 | fProxy->blitRect(x, y, width, height); |
| 728 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 729 | |
| 730 | virtual void blitMask(const SkMask& mask, const SkIRect& clip) { |
| 731 | if (mask.fFormat == SkMask::k3D_Format) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 732 | f3DShader->setMask(&mask); |
| 733 | |
| 734 | ((SkMask*)&mask)->fFormat = SkMask::kA8_Format; |
| 735 | fProxy->blitMask(mask, clip); |
| 736 | ((SkMask*)&mask)->fFormat = SkMask::k3D_Format; |
| 737 | |
| 738 | f3DShader->setMask(NULL); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 739 | } else { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 740 | fProxy->blitMask(mask, clip); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 741 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 742 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 743 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 744 | private: |
commit-bot@chromium.org | a5572e5 | 2014-03-07 03:24:41 +0000 | [diff] [blame] | 745 | // fProxy is unowned. It will be deleted by SkSmallAllocator. |
| 746 | SkBlitter* fProxy; |
| 747 | SkAutoTUnref<Sk3DShader> f3DShader; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 748 | }; |
| 749 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 750 | /////////////////////////////////////////////////////////////////////////////// |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 751 | |
| 752 | #include "SkCoreBlitters.h" |
| 753 | |
reed@android.com | d252db0 | 2009-04-01 18:31:44 +0000 | [diff] [blame] | 754 | static bool just_solid_color(const SkPaint& paint) { |
| 755 | if (paint.getAlpha() == 0xFF && paint.getColorFilter() == NULL) { |
| 756 | SkShader* shader = paint.getShader(); |
| 757 | if (NULL == shader || |
| 758 | (shader->getFlags() & SkShader::kOpaqueAlpha_Flag)) { |
| 759 | return true; |
| 760 | } |
| 761 | } |
| 762 | return false; |
| 763 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 764 | |
reed@android.com | d252db0 | 2009-04-01 18:31:44 +0000 | [diff] [blame] | 765 | /** By analyzing the paint (with an xfermode), we may decide we can take |
| 766 | special action. This enum lists our possible actions |
| 767 | */ |
| 768 | enum XferInterp { |
| 769 | kNormal_XferInterp, // no special interpretation, draw normally |
| 770 | kSrcOver_XferInterp, // draw as if in srcover mode |
| 771 | kSkipDrawing_XferInterp // draw nothing |
| 772 | }; |
| 773 | |
| 774 | static XferInterp interpret_xfermode(const SkPaint& paint, SkXfermode* xfer, |
reed@google.com | 900ecf2 | 2014-02-20 20:55:37 +0000 | [diff] [blame] | 775 | SkColorType deviceCT) { |
reed@android.com | 845fdac | 2009-06-23 03:01:32 +0000 | [diff] [blame] | 776 | SkXfermode::Mode mode; |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 777 | |
mike@reedtribe.org | be2aa2a | 2011-11-17 02:32:04 +0000 | [diff] [blame] | 778 | if (SkXfermode::AsMode(xfer, &mode)) { |
reed@android.com | d252db0 | 2009-04-01 18:31:44 +0000 | [diff] [blame] | 779 | switch (mode) { |
reed@android.com | 845fdac | 2009-06-23 03:01:32 +0000 | [diff] [blame] | 780 | case SkXfermode::kSrc_Mode: |
reed@android.com | d252db0 | 2009-04-01 18:31:44 +0000 | [diff] [blame] | 781 | if (just_solid_color(paint)) { |
| 782 | return kSrcOver_XferInterp; |
| 783 | } |
| 784 | break; |
reed@android.com | 845fdac | 2009-06-23 03:01:32 +0000 | [diff] [blame] | 785 | case SkXfermode::kDst_Mode: |
reed@android.com | d252db0 | 2009-04-01 18:31:44 +0000 | [diff] [blame] | 786 | return kSkipDrawing_XferInterp; |
reed@android.com | 845fdac | 2009-06-23 03:01:32 +0000 | [diff] [blame] | 787 | case SkXfermode::kSrcOver_Mode: |
reed@android.com | d252db0 | 2009-04-01 18:31:44 +0000 | [diff] [blame] | 788 | return kSrcOver_XferInterp; |
reed@android.com | 845fdac | 2009-06-23 03:01:32 +0000 | [diff] [blame] | 789 | case SkXfermode::kDstOver_Mode: |
reed@google.com | 900ecf2 | 2014-02-20 20:55:37 +0000 | [diff] [blame] | 790 | if (kRGB_565_SkColorType == deviceCT) { |
reed@android.com | d252db0 | 2009-04-01 18:31:44 +0000 | [diff] [blame] | 791 | return kSkipDrawing_XferInterp; |
| 792 | } |
| 793 | break; |
reed@android.com | 845fdac | 2009-06-23 03:01:32 +0000 | [diff] [blame] | 794 | case SkXfermode::kSrcIn_Mode: |
reed@google.com | 900ecf2 | 2014-02-20 20:55:37 +0000 | [diff] [blame] | 795 | if (kRGB_565_SkColorType == deviceCT && |
reed@android.com | d252db0 | 2009-04-01 18:31:44 +0000 | [diff] [blame] | 796 | just_solid_color(paint)) { |
| 797 | return kSrcOver_XferInterp; |
| 798 | } |
| 799 | break; |
reed@android.com | 845fdac | 2009-06-23 03:01:32 +0000 | [diff] [blame] | 800 | case SkXfermode::kDstIn_Mode: |
reed@android.com | d252db0 | 2009-04-01 18:31:44 +0000 | [diff] [blame] | 801 | if (just_solid_color(paint)) { |
| 802 | return kSkipDrawing_XferInterp; |
| 803 | } |
| 804 | break; |
| 805 | default: |
| 806 | break; |
| 807 | } |
| 808 | } |
| 809 | return kNormal_XferInterp; |
| 810 | } |
| 811 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 812 | SkBlitter* SkBlitter::Choose(const SkBitmap& device, |
| 813 | const SkMatrix& matrix, |
reed@google.com | 6b7aee3 | 2011-04-19 18:36:09 +0000 | [diff] [blame] | 814 | const SkPaint& origPaint, |
commit-bot@chromium.org | a5572e5 | 2014-03-07 03:24:41 +0000 | [diff] [blame] | 815 | SkTBlitterAllocator* allocator, |
reed@google.com | 126f7f5 | 2013-11-07 16:06:53 +0000 | [diff] [blame] | 816 | bool drawCoverage) { |
commit-bot@chromium.org | a5572e5 | 2014-03-07 03:24:41 +0000 | [diff] [blame] | 817 | SkASSERT(allocator != NULL); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 818 | |
| 819 | SkBlitter* blitter = NULL; |
| 820 | |
| 821 | // which check, in case we're being called by a client with a dummy device |
| 822 | // (e.g. they have a bounder that always aborts the draw) |
reed@google.com | 900ecf2 | 2014-02-20 20:55:37 +0000 | [diff] [blame] | 823 | if (kUnknown_SkColorType == device.colorType() || |
| 824 | (drawCoverage && (kAlpha_8_SkColorType != device.colorType()))) { |
commit-bot@chromium.org | a5572e5 | 2014-03-07 03:24:41 +0000 | [diff] [blame] | 825 | blitter = allocator->createT<SkNullBlitter>(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 826 | return blitter; |
| 827 | } |
| 828 | |
reed@google.com | 4588490 | 2012-05-11 14:38:51 +0000 | [diff] [blame] | 829 | SkShader* shader = origPaint.getShader(); |
| 830 | SkColorFilter* cf = origPaint.getColorFilter(); |
| 831 | SkXfermode* mode = origPaint.getXfermode(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 832 | Sk3DShader* shader3D = NULL; |
reed@google.com | 4588490 | 2012-05-11 14:38:51 +0000 | [diff] [blame] | 833 | |
bsalomon@google.com | 5dc26b9 | 2012-10-11 19:32:32 +0000 | [diff] [blame] | 834 | SkTCopyOnFirstWrite<SkPaint> paint(origPaint); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 835 | |
reed@google.com | 4588490 | 2012-05-11 14:38:51 +0000 | [diff] [blame] | 836 | if (origPaint.getMaskFilter() != NULL && |
| 837 | origPaint.getMaskFilter()->getFormat() == SkMask::k3D_Format) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 838 | shader3D = SkNEW_ARGS(Sk3DShader, (shader)); |
reed@google.com | 4588490 | 2012-05-11 14:38:51 +0000 | [diff] [blame] | 839 | // we know we haven't initialized lazyPaint yet, so just do it |
bsalomon@google.com | 5dc26b9 | 2012-10-11 19:32:32 +0000 | [diff] [blame] | 840 | paint.writable()->setShader(shader3D)->unref(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 841 | shader = shader3D; |
| 842 | } |
| 843 | |
reed@android.com | d252db0 | 2009-04-01 18:31:44 +0000 | [diff] [blame] | 844 | if (NULL != mode) { |
reed@google.com | 900ecf2 | 2014-02-20 20:55:37 +0000 | [diff] [blame] | 845 | switch (interpret_xfermode(*paint, mode, device.colorType())) { |
reed@android.com | d252db0 | 2009-04-01 18:31:44 +0000 | [diff] [blame] | 846 | case kSrcOver_XferInterp: |
| 847 | mode = NULL; |
bsalomon@google.com | 5dc26b9 | 2012-10-11 19:32:32 +0000 | [diff] [blame] | 848 | paint.writable()->setXfermode(NULL); |
reed@android.com | d252db0 | 2009-04-01 18:31:44 +0000 | [diff] [blame] | 849 | break; |
commit-bot@chromium.org | a5572e5 | 2014-03-07 03:24:41 +0000 | [diff] [blame] | 850 | case kSkipDrawing_XferInterp:{ |
| 851 | blitter = allocator->createT<SkNullBlitter>(); |
reed@android.com | d252db0 | 2009-04-01 18:31:44 +0000 | [diff] [blame] | 852 | return blitter; |
commit-bot@chromium.org | a5572e5 | 2014-03-07 03:24:41 +0000 | [diff] [blame] | 853 | } |
reed@android.com | d252db0 | 2009-04-01 18:31:44 +0000 | [diff] [blame] | 854 | default: |
| 855 | break; |
| 856 | } |
| 857 | } |
| 858 | |
reed@google.com | 13201e7 | 2012-11-16 13:20:41 +0000 | [diff] [blame] | 859 | /* |
| 860 | * If the xfermode is CLEAR, then we can completely ignore the installed |
| 861 | * color/shader/colorfilter, and just pretend we're SRC + color==0. This |
| 862 | * will fall into our optimizations for SRC mode. |
| 863 | */ |
| 864 | if (SkXfermode::IsMode(mode, SkXfermode::kClear_Mode)) { |
| 865 | SkPaint* p = paint.writable(); |
| 866 | shader = p->setShader(NULL); |
| 867 | cf = p->setColorFilter(NULL); |
| 868 | mode = p->setXfermodeMode(SkXfermode::kSrc_Mode); |
| 869 | p->setColor(0); |
| 870 | } |
reed@google.com | 13201e7 | 2012-11-16 13:20:41 +0000 | [diff] [blame] | 871 | |
reed@google.com | 6b7aee3 | 2011-04-19 18:36:09 +0000 | [diff] [blame] | 872 | if (NULL == shader) { |
reed@google.com | 6b7aee3 | 2011-04-19 18:36:09 +0000 | [diff] [blame] | 873 | if (mode) { |
reed@google.com | 6b7aee3 | 2011-04-19 18:36:09 +0000 | [diff] [blame] | 874 | // xfermodes (and filters) require shaders for our current blitters |
| 875 | shader = SkNEW(SkColorShader); |
bsalomon@google.com | 5dc26b9 | 2012-10-11 19:32:32 +0000 | [diff] [blame] | 876 | paint.writable()->setShader(shader)->unref(); |
reed@google.com | 6b7aee3 | 2011-04-19 18:36:09 +0000 | [diff] [blame] | 877 | } else if (cf) { |
| 878 | // if no shader && no xfermode, we just apply the colorfilter to |
| 879 | // our color and move on. |
bsalomon@google.com | 5dc26b9 | 2012-10-11 19:32:32 +0000 | [diff] [blame] | 880 | SkPaint* writablePaint = paint.writable(); |
| 881 | writablePaint->setColor(cf->filterColor(paint->getColor())); |
| 882 | writablePaint->setColorFilter(NULL); |
reed@google.com | 6b7aee3 | 2011-04-19 18:36:09 +0000 | [diff] [blame] | 883 | cf = NULL; |
| 884 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 885 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 886 | |
reed@google.com | 6b7aee3 | 2011-04-19 18:36:09 +0000 | [diff] [blame] | 887 | if (cf) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 888 | SkASSERT(shader); |
reed@google.com | 6b7aee3 | 2011-04-19 18:36:09 +0000 | [diff] [blame] | 889 | shader = SkNEW_ARGS(SkFilterShader, (shader, cf)); |
bsalomon@google.com | 5dc26b9 | 2012-10-11 19:32:32 +0000 | [diff] [blame] | 890 | paint.writable()->setShader(shader)->unref(); |
reed@android.com | 1fc4c60 | 2009-10-02 16:34:57 +0000 | [diff] [blame] | 891 | // blitters should ignore the presence/absence of a filter, since |
| 892 | // if there is one, the shader will take care of it. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 893 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 894 | |
reed@google.com | a641f3f | 2012-12-13 22:16:30 +0000 | [diff] [blame] | 895 | /* |
| 896 | * We need to have balanced calls to the shader: |
| 897 | * setContext |
| 898 | * endContext |
| 899 | * We make the first call here, in case it fails we can abort the draw. |
| 900 | * The endContext() call is made by the blitter (assuming setContext did |
| 901 | * not fail) in its destructor. |
| 902 | */ |
reed@google.com | 4588490 | 2012-05-11 14:38:51 +0000 | [diff] [blame] | 903 | if (shader && !shader->setContext(device, *paint, matrix)) { |
commit-bot@chromium.org | a5572e5 | 2014-03-07 03:24:41 +0000 | [diff] [blame] | 904 | blitter = allocator->createT<SkNullBlitter>(); |
reed@google.com | a641f3f | 2012-12-13 22:16:30 +0000 | [diff] [blame] | 905 | return blitter; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 906 | } |
| 907 | |
skia.committer@gmail.com | ab7442c | 2013-11-08 07:01:56 +0000 | [diff] [blame] | 908 | |
reed@google.com | 900ecf2 | 2014-02-20 20:55:37 +0000 | [diff] [blame] | 909 | switch (device.colorType()) { |
| 910 | case kAlpha_8_SkColorType: |
reed@google.com | 126f7f5 | 2013-11-07 16:06:53 +0000 | [diff] [blame] | 911 | if (drawCoverage) { |
| 912 | SkASSERT(NULL == shader); |
| 913 | SkASSERT(NULL == paint->getXfermode()); |
commit-bot@chromium.org | a5572e5 | 2014-03-07 03:24:41 +0000 | [diff] [blame] | 914 | blitter = allocator->createT<SkA8_Coverage_Blitter>(device, *paint); |
reed@google.com | 126f7f5 | 2013-11-07 16:06:53 +0000 | [diff] [blame] | 915 | } else if (shader) { |
commit-bot@chromium.org | a5572e5 | 2014-03-07 03:24:41 +0000 | [diff] [blame] | 916 | blitter = allocator->createT<SkA8_Shader_Blitter>(device, *paint); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 917 | } else { |
commit-bot@chromium.org | a5572e5 | 2014-03-07 03:24:41 +0000 | [diff] [blame] | 918 | blitter = allocator->createT<SkA8_Blitter>(device, *paint); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 919 | } |
| 920 | break; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 921 | |
reed@google.com | 900ecf2 | 2014-02-20 20:55:37 +0000 | [diff] [blame] | 922 | case kRGB_565_SkColorType: |
commit-bot@chromium.org | a5572e5 | 2014-03-07 03:24:41 +0000 | [diff] [blame] | 923 | blitter = SkBlitter_ChooseD565(device, *paint, allocator); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 924 | break; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 925 | |
commit-bot@chromium.org | 149e9a1 | 2014-04-09 20:45:29 +0000 | [diff] [blame] | 926 | case kN32_SkColorType: |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 927 | if (shader) { |
commit-bot@chromium.org | a5572e5 | 2014-03-07 03:24:41 +0000 | [diff] [blame] | 928 | blitter = allocator->createT<SkARGB32_Shader_Blitter>(device, *paint); |
reed@google.com | 4588490 | 2012-05-11 14:38:51 +0000 | [diff] [blame] | 929 | } else if (paint->getColor() == SK_ColorBLACK) { |
commit-bot@chromium.org | a5572e5 | 2014-03-07 03:24:41 +0000 | [diff] [blame] | 930 | blitter = allocator->createT<SkARGB32_Black_Blitter>(device, *paint); |
reed@google.com | 4588490 | 2012-05-11 14:38:51 +0000 | [diff] [blame] | 931 | } else if (paint->getAlpha() == 0xFF) { |
commit-bot@chromium.org | a5572e5 | 2014-03-07 03:24:41 +0000 | [diff] [blame] | 932 | blitter = allocator->createT<SkARGB32_Opaque_Blitter>(device, *paint); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 933 | } else { |
commit-bot@chromium.org | a5572e5 | 2014-03-07 03:24:41 +0000 | [diff] [blame] | 934 | blitter = allocator->createT<SkARGB32_Blitter>(device, *paint); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 935 | } |
| 936 | break; |
| 937 | |
| 938 | default: |
tomhudson@google.com | 0c00f21 | 2011-12-28 14:59:50 +0000 | [diff] [blame] | 939 | SkDEBUGFAIL("unsupported device config"); |
commit-bot@chromium.org | a5572e5 | 2014-03-07 03:24:41 +0000 | [diff] [blame] | 940 | blitter = allocator->createT<SkNullBlitter>(); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 941 | break; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 942 | } |
| 943 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 944 | if (shader3D) { |
commit-bot@chromium.org | a5572e5 | 2014-03-07 03:24:41 +0000 | [diff] [blame] | 945 | SkBlitter* innerBlitter = blitter; |
| 946 | // innerBlitter was allocated by allocator, which will delete it. |
| 947 | blitter = allocator->createT<Sk3DBlitter>(innerBlitter, shader3D); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 948 | } |
| 949 | return blitter; |
| 950 | } |
| 951 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 952 | /////////////////////////////////////////////////////////////////////////////// |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 953 | |
| 954 | const uint16_t gMask_0F0F = 0xF0F; |
| 955 | const uint32_t gMask_00FF00FF = 0xFF00FF; |
| 956 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 957 | /////////////////////////////////////////////////////////////////////////////// |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 958 | |
| 959 | SkShaderBlitter::SkShaderBlitter(const SkBitmap& device, const SkPaint& paint) |
reed@android.com | 5119bdb | 2009-06-12 21:27:03 +0000 | [diff] [blame] | 960 | : INHERITED(device) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 961 | fShader = paint.getShader(); |
| 962 | SkASSERT(fShader); |
reed@google.com | a641f3f | 2012-12-13 22:16:30 +0000 | [diff] [blame] | 963 | SkASSERT(fShader->setContextHasBeenCalled()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 964 | |
| 965 | fShader->ref(); |
reed@android.com | 5119bdb | 2009-06-12 21:27:03 +0000 | [diff] [blame] | 966 | fShaderFlags = fShader->getFlags(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 967 | } |
| 968 | |
reed@android.com | 5119bdb | 2009-06-12 21:27:03 +0000 | [diff] [blame] | 969 | SkShaderBlitter::~SkShaderBlitter() { |
reed@google.com | a641f3f | 2012-12-13 22:16:30 +0000 | [diff] [blame] | 970 | SkASSERT(fShader->setContextHasBeenCalled()); |
| 971 | fShader->endContext(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 972 | fShader->unref(); |
| 973 | } |