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 Derby | 57bfa02 | 2017-02-09 17:25:43 -0500 | [diff] [blame] | 8 | #include "SkArenaAlloc.h" |
Mike Klein | 5cc9da6 | 2017-05-31 14:18:07 -0400 | [diff] [blame] | 9 | #include "SkColorSpace.h" |
| 10 | #include "SkCoreBlitters.h" |
herb | a62038c | 2016-05-26 10:56:17 -0700 | [diff] [blame] | 11 | #include "SkOpts.h" |
Mike Klein | 5cc9da6 | 2017-05-31 14:18:07 -0400 | [diff] [blame] | 12 | #include "SkPM4fPriv.h" |
| 13 | #include "SkRasterPipeline.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 14 | #include "SkSpriteBlitter.h" |
Mike Klein | 45c16fa | 2017-07-18 18:15:13 -0400 | [diff] [blame] | 15 | #include "../jumper/SkJumper.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 16 | |
herb | 7df9e4a | 2016-06-10 13:01:27 -0700 | [diff] [blame] | 17 | SkSpriteBlitter::SkSpriteBlitter(const SkPixmap& source) |
| 18 | : fSource(source) {} |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 19 | |
reed | cb67414 | 2015-06-05 06:58:22 -0700 | [diff] [blame] | 20 | void SkSpriteBlitter::setup(const SkPixmap& dst, int left, int top, const SkPaint& paint) { |
| 21 | fDst = dst; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 22 | fLeft = left; |
| 23 | fTop = top; |
| 24 | fPaint = &paint; |
| 25 | } |
| 26 | |
mike@reedtribe.org | ebe5bcd | 2011-04-20 11:01:37 +0000 | [diff] [blame] | 27 | void SkSpriteBlitter::blitH(int x, int y, int width) { |
tomhudson@google.com | 0c00f21 | 2011-12-28 14:59:50 +0000 | [diff] [blame] | 28 | SkDEBUGFAIL("how did we get here?"); |
herb | 7df9e4a | 2016-06-10 13:01:27 -0700 | [diff] [blame] | 29 | |
| 30 | // Fallback to blitRect. |
| 31 | this->blitRect(x, y, width, 1); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 32 | } |
| 33 | |
herb | 7df9e4a | 2016-06-10 13:01:27 -0700 | [diff] [blame] | 34 | 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] | 35 | SkDEBUGFAIL("how did we get here?"); |
herb | 7df9e4a | 2016-06-10 13:01:27 -0700 | [diff] [blame] | 36 | |
| 37 | // No fallback strategy. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 38 | } |
| 39 | |
mike@reedtribe.org | ebe5bcd | 2011-04-20 11:01:37 +0000 | [diff] [blame] | 40 | void SkSpriteBlitter::blitV(int x, int y, int height, SkAlpha alpha) { |
tomhudson@google.com | 0c00f21 | 2011-12-28 14:59:50 +0000 | [diff] [blame] | 41 | SkDEBUGFAIL("how did we get here?"); |
herb | 7df9e4a | 2016-06-10 13:01:27 -0700 | [diff] [blame] | 42 | |
| 43 | // Fall back to superclass if the code gets here in release mode. |
| 44 | INHERITED::blitV(x, y, height, alpha); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 45 | } |
| 46 | |
herb | 7df9e4a | 2016-06-10 13:01:27 -0700 | [diff] [blame] | 47 | void SkSpriteBlitter::blitMask(const SkMask& mask, const SkIRect& clip) { |
tomhudson@google.com | 0c00f21 | 2011-12-28 14:59:50 +0000 | [diff] [blame] | 48 | SkDEBUGFAIL("how did we get here?"); |
herb | 7df9e4a | 2016-06-10 13:01:27 -0700 | [diff] [blame] | 49 | |
| 50 | // Fall back to superclass if the code gets here in release mode. |
| 51 | INHERITED::blitMask(mask, clip); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 52 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 53 | |
mike@reedtribe.org | ebe5bcd | 2011-04-20 11:01:37 +0000 | [diff] [blame] | 54 | /////////////////////////////////////////////////////////////////////////////// |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 55 | |
Mike Reed | 1919dc4 | 2017-05-31 09:25:18 -0400 | [diff] [blame] | 56 | class SkSpriteBlitter_Memcpy 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 | } |
Mike Klein | 2398033 | 2017-05-31 12:13:42 -0400 | [diff] [blame] | 62 | if (!SkColorSpace::Equals(dst.colorSpace(), src.colorSpace())) { |
| 63 | return false; |
| 64 | } |
reed | 8c3fd4f | 2016-04-15 06:59:38 -0700 | [diff] [blame] | 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(); |
Mike Reed | 1919dc4 | 2017-05-31 09:25:18 -0400 | [diff] [blame] | 72 | return SkBlendMode::kSrc == mode || (SkBlendMode::kSrcOver == mode && src.isOpaque()); |
reed | 8c3fd4f | 2016-04-15 06:59:38 -0700 | [diff] [blame] | 73 | } |
| 74 | |
Mike Reed | 1919dc4 | 2017-05-31 09:25:18 -0400 | [diff] [blame] | 75 | SkSpriteBlitter_Memcpy(const SkPixmap& src) |
herb | 7df9e4a | 2016-06-10 13:01:27 -0700 | [diff] [blame] | 76 | : INHERITED(src) {} |
reed | 8c3fd4f | 2016-04-15 06:59:38 -0700 | [diff] [blame] | 77 | |
reed | 8c3fd4f | 2016-04-15 06:59:38 -0700 | [diff] [blame] | 78 | void blitRect(int x, int y, int width, int height) override { |
| 79 | SkASSERT(fDst.colorType() == fSource.colorType()); |
reed | 8c3fd4f | 2016-04-15 06:59:38 -0700 | [diff] [blame] | 80 | SkASSERT(width > 0 && height > 0); |
| 81 | |
Mike Reed | 1919dc4 | 2017-05-31 09:25:18 -0400 | [diff] [blame] | 82 | char* dst = (char*)fDst.writable_addr(x, y); |
| 83 | const char* src = (const char*)fSource.addr(x - fLeft, y - fTop); |
| 84 | const size_t dstRB = fDst.rowBytes(); |
| 85 | const size_t srcRB = fSource.rowBytes(); |
| 86 | const size_t bytesToCopy = width << fSource.shiftPerPixel(); |
reed | 8c3fd4f | 2016-04-15 06:59:38 -0700 | [diff] [blame] | 87 | |
Mike Reed | 1919dc4 | 2017-05-31 09:25:18 -0400 | [diff] [blame] | 88 | while (height --> 0) { |
| 89 | memcpy(dst, src, bytesToCopy); |
| 90 | dst += dstRB; |
| 91 | src += srcRB; |
reed | 8c3fd4f | 2016-04-15 06:59:38 -0700 | [diff] [blame] | 92 | } |
| 93 | } |
| 94 | |
herb | a62038c | 2016-05-26 10:56:17 -0700 | [diff] [blame] | 95 | private: |
reed | 8c3fd4f | 2016-04-15 06:59:38 -0700 | [diff] [blame] | 96 | typedef SkSpriteBlitter INHERITED; |
herb | a62038c | 2016-05-26 10:56:17 -0700 | [diff] [blame] | 97 | }; |
reed | 8c3fd4f | 2016-04-15 06:59:38 -0700 | [diff] [blame] | 98 | |
Mike Klein | 5cc9da6 | 2017-05-31 14:18:07 -0400 | [diff] [blame] | 99 | class SkRasterPipelineSpriteBlitter : public SkSpriteBlitter { |
| 100 | public: |
| 101 | SkRasterPipelineSpriteBlitter(const SkPixmap& src, SkArenaAlloc* alloc) |
| 102 | : INHERITED(src) |
| 103 | , fAlloc(alloc) |
| 104 | , fBlitter(nullptr) |
Mike Klein | 45c16fa | 2017-07-18 18:15:13 -0400 | [diff] [blame] | 105 | , fSrcPtr{nullptr, 0} |
Mike Klein | 5cc9da6 | 2017-05-31 14:18:07 -0400 | [diff] [blame] | 106 | {} |
| 107 | |
Mike Klein | 5cc9da6 | 2017-05-31 14:18:07 -0400 | [diff] [blame] | 108 | void setup(const SkPixmap& dst, int left, int top, const SkPaint& paint) override { |
| 109 | fDst = dst; |
| 110 | fLeft = left; |
| 111 | fTop = top; |
| 112 | |
| 113 | fPaintColor = SkColor4f_from_SkColor(paint.getColor(), fDst.colorSpace()); |
| 114 | |
| 115 | SkRasterPipeline p(fAlloc); |
Mike Klein | ac568a9 | 2018-01-25 09:09:32 -0500 | [diff] [blame] | 116 | void* ctx = &fSrcPtr; |
Mike Klein | 5cc9da6 | 2017-05-31 14:18:07 -0400 | [diff] [blame] | 117 | switch (fSource.colorType()) { |
Mike Klein | ac568a9 | 2018-01-25 09:09:32 -0500 | [diff] [blame] | 118 | case kAlpha_8_SkColorType: p.append(SkRasterPipeline::load_a8, ctx); break; |
| 119 | case kGray_8_SkColorType: p.append(SkRasterPipeline::load_g8, ctx); break; |
| 120 | case kRGB_565_SkColorType: p.append(SkRasterPipeline::load_565, ctx); break; |
| 121 | case kARGB_4444_SkColorType: p.append(SkRasterPipeline::load_4444, ctx); break; |
| 122 | case kBGRA_8888_SkColorType: p.append(SkRasterPipeline::load_bgra, ctx); break; |
| 123 | case kRGBA_8888_SkColorType: p.append(SkRasterPipeline::load_8888, ctx); break; |
| 124 | case kRGBA_1010102_SkColorType: p.append(SkRasterPipeline::load_1010102, ctx); break; |
| 125 | case kRGBA_F16_SkColorType: p.append(SkRasterPipeline::load_f16, ctx); break; |
| 126 | |
| 127 | case kRGB_888x_SkColorType: p.append(SkRasterPipeline::load_8888, ctx); |
| 128 | p.append(SkRasterPipeline::force_opaque ); break; |
| 129 | case kRGB_101010x_SkColorType: p.append(SkRasterPipeline::load_1010102, ctx); |
| 130 | p.append(SkRasterPipeline::force_opaque ); break; |
Mike Klein | 5cc9da6 | 2017-05-31 14:18:07 -0400 | [diff] [blame] | 131 | default: SkASSERT(false); |
| 132 | } |
| 133 | if (fDst.colorSpace() && |
| 134 | (!fSource.colorSpace() || fSource.colorSpace()->gammaCloseToSRGB())) { |
Mike Klein | f1f1162 | 2017-12-18 14:07:31 -0500 | [diff] [blame] | 135 | p.append(SkRasterPipeline::from_srgb); |
Mike Klein | 5cc9da6 | 2017-05-31 14:18:07 -0400 | [diff] [blame] | 136 | } |
Mike Klein | 5cc9da6 | 2017-05-31 14:18:07 -0400 | [diff] [blame] | 137 | if (fSource.colorType() == kAlpha_8_SkColorType) { |
| 138 | p.append(SkRasterPipeline::set_rgb, &fPaintColor); |
| 139 | p.append(SkRasterPipeline::premul); |
| 140 | } |
| 141 | append_gamut_transform(&p, fAlloc, |
| 142 | fSource.colorSpace(), fDst.colorSpace(), kPremul_SkAlphaType); |
| 143 | if (fPaintColor.fA != 1.0f) { |
| 144 | p.append(SkRasterPipeline::scale_1_float, &fPaintColor.fA); |
| 145 | } |
| 146 | |
Mike Reed | eb7dc79 | 2017-06-12 12:48:45 -0400 | [diff] [blame] | 147 | bool is_opaque = fSource.isOpaque() && fPaintColor.fA == 1.0f; |
| 148 | fBlitter = SkCreateRasterPipelineBlitter(fDst, paint, p, is_opaque, fAlloc); |
Mike Klein | 5cc9da6 | 2017-05-31 14:18:07 -0400 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | void blitRect(int x, int y, int width, int height) override { |
Mike Klein | 45c16fa | 2017-07-18 18:15:13 -0400 | [diff] [blame] | 152 | fSrcPtr.stride = fSource.rowBytesAsPixels(); |
Mike Klein | 548d387 | 2018-01-24 15:36:35 -0500 | [diff] [blame] | 153 | |
| 154 | // We really want fSrcPtr.pixels = fSource.addr(-fLeft, -fTop) here, but that asserts. |
| 155 | // Instead we ask for addr(-fLeft+x, -fTop+y), then back up (x,y) manually. |
| 156 | // Representing bpp as a size_t keeps all this math in size_t instead of int, |
| 157 | // which could wrap around with large enough fSrcPtr.stride and y. |
| 158 | size_t bpp = fSource.info().bytesPerPixel(); |
| 159 | fSrcPtr.pixels = (char*)fSource.addr(-fLeft+x, -fTop+y) - bpp * x |
| 160 | - bpp * y * fSrcPtr.stride; |
Mike Klein | 5cc9da6 | 2017-05-31 14:18:07 -0400 | [diff] [blame] | 161 | |
Mike Klein | 45c16fa | 2017-07-18 18:15:13 -0400 | [diff] [blame] | 162 | fBlitter->blitRect(x,y,width,height); |
Mike Klein | 5cc9da6 | 2017-05-31 14:18:07 -0400 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | private: |
Mike Klein | 45c16fa | 2017-07-18 18:15:13 -0400 | [diff] [blame] | 166 | SkArenaAlloc* fAlloc; |
| 167 | SkBlitter* fBlitter; |
| 168 | SkJumper_MemoryCtx fSrcPtr; |
| 169 | SkColor4f fPaintColor; |
Mike Klein | 5cc9da6 | 2017-05-31 14:18:07 -0400 | [diff] [blame] | 170 | |
| 171 | typedef SkSpriteBlitter INHERITED; |
| 172 | }; |
| 173 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 174 | // returning null means the caller will call SkBlitter::Choose() and |
| 175 | // have wrapped the source bitmap inside a shader |
reed | cb67414 | 2015-06-05 06:58:22 -0700 | [diff] [blame] | 176 | SkBlitter* SkBlitter::ChooseSprite(const SkPixmap& dst, const SkPaint& paint, |
Herb Derby | 57bfa02 | 2017-02-09 17:25:43 -0500 | [diff] [blame] | 177 | const SkPixmap& source, int left, int top, SkArenaAlloc* allocator) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 178 | /* We currently ignore antialiasing and filtertype, meaning we will take our |
| 179 | special blitters regardless of these settings. Ignoring filtertype seems fine |
| 180 | since by definition there is no scale in the matrix. Ignoring antialiasing is |
| 181 | a bit of a hack, since we "could" pass in the fractional left/top for the bitmap, |
| 182 | and respect that by blending the edges of the bitmap against the device. To support |
| 183 | this we could either add more special blitters here, or detect antialiasing in the |
| 184 | paint and return null if it is set, forcing the client to take the slow shader case |
| 185 | (which does respect soft edges). |
| 186 | */ |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 187 | SkASSERT(allocator != nullptr); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 188 | |
herb | 5823846 | 2016-06-03 09:44:53 -0700 | [diff] [blame] | 189 | if (source.alphaType() == kUnpremul_SkAlphaType) { |
| 190 | return nullptr; |
| 191 | } |
| 192 | |
herb | a62038c | 2016-05-26 10:56:17 -0700 | [diff] [blame] | 193 | SkSpriteBlitter* blitter = nullptr; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 194 | |
Mike Klein | 5cc9da6 | 2017-05-31 14:18:07 -0400 | [diff] [blame] | 195 | if (!blitter && SkSpriteBlitter_Memcpy::Supports(dst, source, paint)) { |
Mike Reed | 1919dc4 | 2017-05-31 09:25:18 -0400 | [diff] [blame] | 196 | blitter = allocator->make<SkSpriteBlitter_Memcpy>(source); |
Mike Klein | 5cc9da6 | 2017-05-31 14:18:07 -0400 | [diff] [blame] | 197 | } |
Mike Reed | ef8ce28 | 2017-10-05 16:04:41 -0400 | [diff] [blame] | 198 | if (!blitter && !dst.colorSpace()) { |
| 199 | switch (dst.colorType()) { |
| 200 | case kN32_SkColorType: |
| 201 | blitter = SkSpriteBlitter::ChooseL32(source, paint, allocator); |
| 202 | break; |
| 203 | case kRGB_565_SkColorType: |
| 204 | blitter = SkSpriteBlitter::ChooseL565(source, paint, allocator); |
| 205 | break; |
| 206 | case kAlpha_8_SkColorType: |
| 207 | blitter = SkSpriteBlitter::ChooseLA8(source, paint, allocator); |
| 208 | break; |
| 209 | default: |
| 210 | break; |
| 211 | } |
Mike Klein | 5cc9da6 | 2017-05-31 14:18:07 -0400 | [diff] [blame] | 212 | } |
Mike Reed | 2826627 | 2018-01-17 23:04:04 -0500 | [diff] [blame] | 213 | if (!blitter && !paint.getMaskFilter()) { |
Mike Klein | 5cc9da6 | 2017-05-31 14:18:07 -0400 | [diff] [blame] | 214 | blitter = allocator->make<SkRasterPipelineSpriteBlitter>(source, allocator); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 215 | } |
| 216 | |
mike@reedtribe.org | ebe5bcd | 2011-04-20 11:01:37 +0000 | [diff] [blame] | 217 | if (blitter) { |
reed | cb67414 | 2015-06-05 06:58:22 -0700 | [diff] [blame] | 218 | blitter->setup(dst, left, top, paint); |
mike@reedtribe.org | ebe5bcd | 2011-04-20 11:01:37 +0000 | [diff] [blame] | 219 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 220 | return blitter; |
| 221 | } |