epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2006 The Android Open Source Project |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
herb | a62038c | 2016-05-26 10:56:17 -0700 | [diff] [blame] | 8 | #include "SkOpts.h" |
commit-bot@chromium.org | a5572e5 | 2014-03-07 03:24:41 +0000 | [diff] [blame] | 9 | #include "SkSmallAllocator.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 10 | #include "SkSpriteBlitter.h" |
| 11 | |
herb | 7df9e4a | 2016-06-10 13:01:27 -0700 | [diff] [blame] | 12 | SkSpriteBlitter::SkSpriteBlitter(const SkPixmap& source) |
| 13 | : fSource(source) {} |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 14 | |
reed | cb67414 | 2015-06-05 06:58:22 -0700 | [diff] [blame] | 15 | void SkSpriteBlitter::setup(const SkPixmap& dst, int left, int top, const SkPaint& paint) { |
| 16 | fDst = dst; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 17 | fLeft = left; |
| 18 | fTop = top; |
| 19 | fPaint = &paint; |
| 20 | } |
| 21 | |
mike@reedtribe.org | ebe5bcd | 2011-04-20 11:01:37 +0000 | [diff] [blame] | 22 | void SkSpriteBlitter::blitH(int x, int y, int width) { |
tomhudson@google.com | 0c00f21 | 2011-12-28 14:59:50 +0000 | [diff] [blame] | 23 | SkDEBUGFAIL("how did we get here?"); |
herb | 7df9e4a | 2016-06-10 13:01:27 -0700 | [diff] [blame] | 24 | |
| 25 | // Fallback to blitRect. |
| 26 | this->blitRect(x, y, width, 1); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 27 | } |
| 28 | |
herb | 7df9e4a | 2016-06-10 13:01:27 -0700 | [diff] [blame] | 29 | void SkSpriteBlitter::blitAntiH(int x, int y, const SkAlpha antialias[], const int16_t runs[]) { |
tomhudson@google.com | 0c00f21 | 2011-12-28 14:59:50 +0000 | [diff] [blame] | 30 | SkDEBUGFAIL("how did we get here?"); |
herb | 7df9e4a | 2016-06-10 13:01:27 -0700 | [diff] [blame] | 31 | |
| 32 | // No fallback strategy. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 33 | } |
| 34 | |
mike@reedtribe.org | ebe5bcd | 2011-04-20 11:01:37 +0000 | [diff] [blame] | 35 | void SkSpriteBlitter::blitV(int x, int y, int height, SkAlpha alpha) { |
tomhudson@google.com | 0c00f21 | 2011-12-28 14:59:50 +0000 | [diff] [blame] | 36 | SkDEBUGFAIL("how did we get here?"); |
herb | 7df9e4a | 2016-06-10 13:01:27 -0700 | [diff] [blame] | 37 | |
| 38 | // Fall back to superclass if the code gets here in release mode. |
| 39 | INHERITED::blitV(x, y, height, alpha); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 40 | } |
| 41 | |
herb | 7df9e4a | 2016-06-10 13:01:27 -0700 | [diff] [blame] | 42 | void SkSpriteBlitter::blitMask(const SkMask& mask, const SkIRect& clip) { |
tomhudson@google.com | 0c00f21 | 2011-12-28 14:59:50 +0000 | [diff] [blame] | 43 | SkDEBUGFAIL("how did we get here?"); |
herb | 7df9e4a | 2016-06-10 13:01:27 -0700 | [diff] [blame] | 44 | |
| 45 | // Fall back to superclass if the code gets here in release mode. |
| 46 | INHERITED::blitMask(mask, clip); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 47 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 48 | |
mike@reedtribe.org | ebe5bcd | 2011-04-20 11:01:37 +0000 | [diff] [blame] | 49 | /////////////////////////////////////////////////////////////////////////////// |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 50 | |
reed | 8c3fd4f | 2016-04-15 06:59:38 -0700 | [diff] [blame] | 51 | // Only valid if... |
| 52 | // 1. src == dst format |
| 53 | // 2. paint has no modifiers (i.e. alpha, colorfilter, etc.) |
| 54 | // 3. xfermode needs no blending: e.g. kSrc_Mode or kSrcOver_Mode + opaque src |
| 55 | // |
herb | 7df9e4a | 2016-06-10 13:01:27 -0700 | [diff] [blame] | 56 | class SkSpriteBlitter_Src_SrcOver final : public SkSpriteBlitter { |
reed | 8c3fd4f | 2016-04-15 06:59:38 -0700 | [diff] [blame] | 57 | public: |
| 58 | static bool Supports(const SkPixmap& dst, const SkPixmap& src, const SkPaint& paint) { |
| 59 | if (dst.colorType() != src.colorType()) { |
| 60 | return false; |
| 61 | } |
reed | dabe5d3 | 2016-06-21 10:28:14 -0700 | [diff] [blame] | 62 | if (dst.info().gammaCloseToSRGB() != src.info().gammaCloseToSRGB()) { |
reed | 8c3fd4f | 2016-04-15 06:59:38 -0700 | [diff] [blame] | 63 | return false; |
| 64 | } |
| 65 | if (paint.getMaskFilter() || paint.getColorFilter() || paint.getImageFilter()) { |
| 66 | return false; |
| 67 | } |
| 68 | if (0xFF != paint.getAlpha()) { |
| 69 | return false; |
| 70 | } |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 71 | SkBlendMode mode = paint.getBlendMode(); |
| 72 | if (SkBlendMode::kSrc == mode) { |
reed | 8c3fd4f | 2016-04-15 06:59:38 -0700 | [diff] [blame] | 73 | return true; |
| 74 | } |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 75 | if (SkBlendMode::kSrcOver == mode && src.isOpaque()) { |
reed | 8c3fd4f | 2016-04-15 06:59:38 -0700 | [diff] [blame] | 76 | return true; |
| 77 | } |
herb | a62038c | 2016-05-26 10:56:17 -0700 | [diff] [blame] | 78 | |
| 79 | // At this point memcpy can't be used. The following check for using SrcOver. |
| 80 | |
reed | dabe5d3 | 2016-06-21 10:28:14 -0700 | [diff] [blame] | 81 | if (dst.colorType() != kN32_SkColorType || !dst.info().gammaCloseToSRGB()) { |
herb | a62038c | 2016-05-26 10:56:17 -0700 | [diff] [blame] | 82 | return false; |
| 83 | } |
| 84 | |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 85 | return SkBlendMode::kSrcOver == mode; |
reed | 8c3fd4f | 2016-04-15 06:59:38 -0700 | [diff] [blame] | 86 | } |
| 87 | |
herb | 7df9e4a | 2016-06-10 13:01:27 -0700 | [diff] [blame] | 88 | SkSpriteBlitter_Src_SrcOver(const SkPixmap& src) |
| 89 | : INHERITED(src) {} |
reed | 8c3fd4f | 2016-04-15 06:59:38 -0700 | [diff] [blame] | 90 | |
| 91 | void setup(const SkPixmap& dst, int left, int top, const SkPaint& paint) override { |
| 92 | SkASSERT(Supports(dst, fSource, paint)); |
| 93 | this->INHERITED::setup(dst, left, top, paint); |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 94 | SkBlendMode mode = paint.getBlendMode(); |
herb | a62038c | 2016-05-26 10:56:17 -0700 | [diff] [blame] | 95 | |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 96 | SkASSERT(mode == SkBlendMode::kSrcOver || mode == SkBlendMode::kSrc); |
herb | a62038c | 2016-05-26 10:56:17 -0700 | [diff] [blame] | 97 | |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 98 | if (mode == SkBlendMode::kSrcOver && !fSource.isOpaque()) { |
herb | a62038c | 2016-05-26 10:56:17 -0700 | [diff] [blame] | 99 | fUseMemcpy = false; |
| 100 | } |
reed | 8c3fd4f | 2016-04-15 06:59:38 -0700 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | void blitRect(int x, int y, int width, int height) override { |
| 104 | SkASSERT(fDst.colorType() == fSource.colorType()); |
reed | dabe5d3 | 2016-06-21 10:28:14 -0700 | [diff] [blame] | 105 | SkASSERT(fDst.info().gammaCloseToSRGB() == fSource.info().gammaCloseToSRGB()); |
reed | 8c3fd4f | 2016-04-15 06:59:38 -0700 | [diff] [blame] | 106 | SkASSERT(width > 0 && height > 0); |
| 107 | |
herb | a62038c | 2016-05-26 10:56:17 -0700 | [diff] [blame] | 108 | if (fUseMemcpy) { |
| 109 | char* dst = (char*)fDst.writable_addr(x, y); |
| 110 | const char* src = (const char*)fSource.addr(x - fLeft, y - fTop); |
| 111 | const size_t dstRB = fDst.rowBytes(); |
| 112 | const size_t srcRB = fSource.rowBytes(); |
| 113 | const size_t bytesToCopy = width << fSource.shiftPerPixel(); |
reed | 8c3fd4f | 2016-04-15 06:59:38 -0700 | [diff] [blame] | 114 | |
herb | a62038c | 2016-05-26 10:56:17 -0700 | [diff] [blame] | 115 | while (height --> 0) { |
| 116 | memcpy(dst, src, bytesToCopy); |
| 117 | dst += dstRB; |
| 118 | src += srcRB; |
| 119 | } |
| 120 | } else { |
| 121 | uint32_t* dst = fDst.writable_addr32(x, y); |
| 122 | const uint32_t* src = fSource.addr32(x - fLeft, y - fTop); |
| 123 | const int dstStride = fDst.rowBytesAsPixels(); |
| 124 | const int srcStride = fSource.rowBytesAsPixels(); |
| 125 | |
| 126 | while (height --> 0) { |
| 127 | SkOpts::srcover_srgb_srgb(dst, src, width, width); |
| 128 | dst += dstStride; |
| 129 | src += srcStride; |
| 130 | } |
reed | 8c3fd4f | 2016-04-15 06:59:38 -0700 | [diff] [blame] | 131 | } |
| 132 | } |
| 133 | |
herb | a62038c | 2016-05-26 10:56:17 -0700 | [diff] [blame] | 134 | private: |
reed | 8c3fd4f | 2016-04-15 06:59:38 -0700 | [diff] [blame] | 135 | typedef SkSpriteBlitter INHERITED; |
reed | 8c3fd4f | 2016-04-15 06:59:38 -0700 | [diff] [blame] | 136 | |
herb | a62038c | 2016-05-26 10:56:17 -0700 | [diff] [blame] | 137 | bool fUseMemcpy {true}; |
| 138 | }; |
reed | 8c3fd4f | 2016-04-15 06:59:38 -0700 | [diff] [blame] | 139 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 140 | // returning null means the caller will call SkBlitter::Choose() and |
| 141 | // have wrapped the source bitmap inside a shader |
reed | cb67414 | 2015-06-05 06:58:22 -0700 | [diff] [blame] | 142 | SkBlitter* SkBlitter::ChooseSprite(const SkPixmap& dst, const SkPaint& paint, |
reed | c240e71 | 2015-05-23 12:26:41 -0700 | [diff] [blame] | 143 | const SkPixmap& source, int left, int top, SkTBlitterAllocator* allocator) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 144 | /* We currently ignore antialiasing and filtertype, meaning we will take our |
| 145 | special blitters regardless of these settings. Ignoring filtertype seems fine |
| 146 | since by definition there is no scale in the matrix. Ignoring antialiasing is |
| 147 | a bit of a hack, since we "could" pass in the fractional left/top for the bitmap, |
| 148 | and respect that by blending the edges of the bitmap against the device. To support |
| 149 | this we could either add more special blitters here, or detect antialiasing in the |
| 150 | paint and return null if it is set, forcing the client to take the slow shader case |
| 151 | (which does respect soft edges). |
| 152 | */ |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 153 | SkASSERT(allocator != nullptr); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 154 | |
herb | 5823846 | 2016-06-03 09:44:53 -0700 | [diff] [blame] | 155 | // Defer to the general code if the pixels are unpremultipled. This case is not common, |
| 156 | // and this simplifies the code. |
| 157 | if (source.alphaType() == kUnpremul_SkAlphaType) { |
| 158 | return nullptr; |
| 159 | } |
| 160 | |
herb | a62038c | 2016-05-26 10:56:17 -0700 | [diff] [blame] | 161 | SkSpriteBlitter* blitter = nullptr; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 162 | |
herb | a62038c | 2016-05-26 10:56:17 -0700 | [diff] [blame] | 163 | if (SkSpriteBlitter_Src_SrcOver::Supports(dst, source, paint)) { |
| 164 | blitter = allocator->createT<SkSpriteBlitter_Src_SrcOver>(source); |
reed | 8c3fd4f | 2016-04-15 06:59:38 -0700 | [diff] [blame] | 165 | } else { |
| 166 | switch (dst.colorType()) { |
| 167 | case kRGB_565_SkColorType: |
| 168 | blitter = SkSpriteBlitter::ChooseD16(source, paint, allocator); |
| 169 | break; |
| 170 | case kN32_SkColorType: |
reed | dabe5d3 | 2016-06-21 10:28:14 -0700 | [diff] [blame] | 171 | if (dst.info().gammaCloseToSRGB()) { |
reed | 8c3fd4f | 2016-04-15 06:59:38 -0700 | [diff] [blame] | 172 | blitter = SkSpriteBlitter::ChooseS32(source, paint, allocator); |
| 173 | } else { |
| 174 | blitter = SkSpriteBlitter::ChooseL32(source, paint, allocator); |
| 175 | } |
| 176 | break; |
| 177 | case kRGBA_F16_SkColorType: |
| 178 | blitter = SkSpriteBlitter::ChooseF16(source, paint, allocator); |
| 179 | break; |
| 180 | default: |
reed | 8c3fd4f | 2016-04-15 06:59:38 -0700 | [diff] [blame] | 181 | break; |
| 182 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 183 | } |
| 184 | |
mike@reedtribe.org | ebe5bcd | 2011-04-20 11:01:37 +0000 | [diff] [blame] | 185 | if (blitter) { |
reed | cb67414 | 2015-06-05 06:58:22 -0700 | [diff] [blame] | 186 | blitter->setup(dst, left, top, paint); |
mike@reedtribe.org | ebe5bcd | 2011-04-20 11:01:37 +0000 | [diff] [blame] | 187 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 188 | return blitter; |
| 189 | } |