herb | feec878 | 2016-02-17 10:00:07 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 Google Inc. |
| 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 | |
| 8 | #include "gm.h" |
fmalita | d0c4e09 | 2016-02-22 17:19:04 -0800 | [diff] [blame] | 9 | #include "SkBlitter.h" |
herb | feec878 | 2016-02-17 10:00:07 -0800 | [diff] [blame] | 10 | #include "SkCanvas.h" |
| 11 | #include "SkColor.h" |
| 12 | #include "SkImage.h" |
| 13 | #include "SkImageInfo.h" |
| 14 | #include "SkLinearBitmapPipeline.h" |
| 15 | #include "SkXfermode.h" |
| 16 | #include "SkPM4fPriv.h" |
| 17 | #include "SkShader.h" |
| 18 | |
| 19 | static void fill_in_bits(SkBitmap& bm, SkIRect ir, SkColor c, bool premul) { |
| 20 | bm.allocN32Pixels(ir.width(), ir.height()); |
| 21 | SkPixmap pm; |
| 22 | bm.peekPixels(&pm); |
| 23 | |
| 24 | SkPMColor b = SkColorSetARGBMacro(255, 0, 0, 0); |
| 25 | SkPMColor w; |
| 26 | if (premul) { |
| 27 | w = SkPreMultiplyColor(c); |
| 28 | } else { |
| 29 | w = SkPackARGB32NoCheck(SkColorGetA(c), SkColorGetR(c), SkColorGetG(c), SkColorGetB(c)); |
| 30 | } |
| 31 | |
| 32 | for (int y = 0; y < ir.height(); y++) { |
| 33 | for (int x = 0; x < ir.width(); x++) { |
| 34 | if ((x ^ y) & 16) { |
| 35 | *pm.writable_addr32(x, y) = b; |
| 36 | } else { |
| 37 | *pm.writable_addr32(x, y) = w; |
| 38 | } |
| 39 | } |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | static void draw_rect_orig(SkCanvas* canvas, const SkRect& r, SkColor c, const SkMatrix* mat, bool useBilerp) { |
| 44 | const SkIRect ir = r.round(); |
| 45 | |
| 46 | SkBitmap bmsrc; |
| 47 | fill_in_bits(bmsrc, ir, c, true); |
| 48 | |
| 49 | SkPixmap pmsrc; |
| 50 | bmsrc.peekPixels(&pmsrc); |
| 51 | |
| 52 | SkBitmap bmdst; |
| 53 | bmdst.allocN32Pixels(ir.width(), ir.height()); |
| 54 | bmdst.eraseColor(0xFFFFFFFF); |
| 55 | SkPixmap pmdst; |
| 56 | bmdst.peekPixels(&pmdst); |
| 57 | |
brianosman | 52ede1d | 2016-06-20 08:25:02 -0700 | [diff] [blame] | 58 | SkImageInfo info = SkImageInfo::MakeN32Premul(ir.width(), ir.height()); |
herb | feec878 | 2016-02-17 10:00:07 -0800 | [diff] [blame] | 59 | |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 60 | sk_sp<SkImage> image(SkImage::MakeRasterCopy(SkPixmap(info, pmsrc.addr32(), pmsrc.rowBytes()))); |
herb | feec878 | 2016-02-17 10:00:07 -0800 | [diff] [blame] | 61 | SkPaint paint; |
herb | 42da45d | 2016-04-08 14:24:37 -0700 | [diff] [blame] | 62 | int32_t storage[kSkBlitterContextSize]; |
herb | 6eff52a | 2016-03-23 09:00:33 -0700 | [diff] [blame] | 63 | |
| 64 | sk_sp<SkShader> shader = image->makeShader(SkShader::kRepeat_TileMode, |
| 65 | SkShader::kRepeat_TileMode); |
| 66 | |
herb | feec878 | 2016-02-17 10:00:07 -0800 | [diff] [blame] | 67 | if (useBilerp) { |
| 68 | paint.setFilterQuality(SkFilterQuality::kLow_SkFilterQuality); |
| 69 | } else { |
| 70 | paint.setFilterQuality(SkFilterQuality::kNone_SkFilterQuality); |
| 71 | } |
herb | 6eff52a | 2016-03-23 09:00:33 -0700 | [diff] [blame] | 72 | paint.setShader(std::move(shader)); |
fmalita | d0c4e09 | 2016-02-22 17:19:04 -0800 | [diff] [blame] | 73 | const SkShader::ContextRec rec(paint, *mat, nullptr, |
| 74 | SkBlitter::PreferredShaderDest(pmsrc.info())); |
fmalita | 8d9f2e4 | 2016-02-22 10:39:41 -0800 | [diff] [blame] | 75 | SkASSERT(paint.getShader()->contextSize(rec) <= sizeof(storage)); |
herb | feec878 | 2016-02-17 10:00:07 -0800 | [diff] [blame] | 76 | |
fmalita | 8d9f2e4 | 2016-02-22 10:39:41 -0800 | [diff] [blame] | 77 | SkShader::Context* ctx = paint.getShader()->createContext(rec, storage); |
herb | feec878 | 2016-02-17 10:00:07 -0800 | [diff] [blame] | 78 | |
| 79 | for (int y = 0; y < ir.height(); y++) { |
| 80 | ctx->shadeSpan(0, y, pmdst.writable_addr32(0, y), ir.width()); |
| 81 | } |
| 82 | |
| 83 | canvas->drawBitmap(bmdst, r.left(), r.top(), nullptr); |
| 84 | |
| 85 | ctx->~Context(); |
herb | feec878 | 2016-02-17 10:00:07 -0800 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | static void draw_rect_fp(SkCanvas* canvas, const SkRect& r, SkColor c, const SkMatrix* mat, bool useBilerp) { |
| 89 | const SkIRect ir = r.round(); |
| 90 | |
| 91 | SkBitmap bmsrc; |
| 92 | fill_in_bits(bmsrc, ir, c, true); |
| 93 | SkPixmap pmsrc; |
| 94 | bmsrc.peekPixels(&pmsrc); |
| 95 | |
| 96 | SkBitmap bmdst; |
| 97 | bmdst.allocN32Pixels(ir.width(), ir.height()); |
| 98 | bmdst.eraseColor(0xFFFFFFFF); |
| 99 | SkPixmap pmdst; |
| 100 | bmdst.peekPixels(&pmdst); |
| 101 | |
| 102 | SkPM4f* dstBits = new SkPM4f[ir.width()]; |
herb | feec878 | 2016-02-17 10:00:07 -0800 | [diff] [blame] | 103 | |
| 104 | SkMatrix inv; |
| 105 | bool trash = mat->invert(&inv); |
| 106 | sk_ignore_unused_variable(trash); |
| 107 | |
herb | c5eddd7 | 2016-02-17 19:50:05 -0800 | [diff] [blame] | 108 | SkFilterQuality filterQuality; |
| 109 | if (useBilerp) { |
| 110 | filterQuality = SkFilterQuality::kLow_SkFilterQuality; |
| 111 | } else { |
| 112 | filterQuality = SkFilterQuality::kNone_SkFilterQuality; |
| 113 | } |
herb | feec878 | 2016-02-17 10:00:07 -0800 | [diff] [blame] | 114 | |
| 115 | uint32_t flags = 0; |
reed | 8f7b0b2 | 2016-02-24 06:21:36 -0800 | [diff] [blame] | 116 | auto procN = SkXfermode::GetD32Proc(nullptr, flags); |
herb | feec878 | 2016-02-17 10:00:07 -0800 | [diff] [blame] | 117 | |
| 118 | SkLinearBitmapPipeline pipeline{ |
herb | c5eddd7 | 2016-02-17 19:50:05 -0800 | [diff] [blame] | 119 | inv, filterQuality, |
herb | 670f01f | 2016-05-13 10:04:46 -0700 | [diff] [blame] | 120 | SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode, SK_ColorBLACK, pmsrc}; |
herb | feec878 | 2016-02-17 10:00:07 -0800 | [diff] [blame] | 121 | |
| 122 | for (int y = 0; y < ir.height(); y++) { |
| 123 | pipeline.shadeSpan4f(0, y, dstBits, ir.width()); |
reed | 8f7b0b2 | 2016-02-24 06:21:36 -0800 | [diff] [blame] | 124 | procN(nullptr, pmdst.writable_addr32(0, y), dstBits, ir.width(), nullptr); |
herb | feec878 | 2016-02-17 10:00:07 -0800 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | delete [] dstBits; |
| 128 | |
| 129 | canvas->drawBitmap(bmdst, r.left(), r.top(), nullptr); |
| 130 | } |
| 131 | |
| 132 | static void draw_rect_none(SkCanvas* canvas, const SkRect& r, SkColor c) { |
| 133 | const SkIRect ir = r.round(); |
| 134 | |
| 135 | SkBitmap bm; |
| 136 | fill_in_bits(bm, ir, c, true); |
| 137 | |
| 138 | canvas->drawBitmap(bm, r.left(), r.top(), nullptr); |
| 139 | } |
| 140 | |
| 141 | /* |
| 142 | * Test SkXfer4fProcs directly for src-over, comparing them to current SkColor blits. |
| 143 | */ |
herb | 9466571 | 2016-02-29 07:53:22 -0800 | [diff] [blame] | 144 | DEF_SIMPLE_GM(linear_pipeline, canvas, 580, 2200) { |
herb | feec878 | 2016-02-17 10:00:07 -0800 | [diff] [blame] | 145 | const int IW = 50; |
| 146 | const SkScalar W = IW; |
| 147 | const SkScalar H = 100; |
| 148 | |
| 149 | const SkColor colors[] = { |
| 150 | 0x880000FF, 0x8800FF00, 0x88FF0000, 0x88000000, |
| 151 | SK_ColorBLUE, SK_ColorGREEN, SK_ColorRED, SK_ColorBLACK, |
| 152 | }; |
| 153 | |
| 154 | canvas->translate(20, 20); |
| 155 | |
| 156 | SkMatrix mi = SkMatrix::I(); |
herb | 9466571 | 2016-02-29 07:53:22 -0800 | [diff] [blame] | 157 | SkMatrix mlr; |
| 158 | mlr.setScale(-1.0f, 1.0f, 20, 0.0f); |
herb | feec878 | 2016-02-17 10:00:07 -0800 | [diff] [blame] | 159 | SkMatrix mt; |
| 160 | mt.setTranslate(8, 8); |
herb | 9466571 | 2016-02-29 07:53:22 -0800 | [diff] [blame] | 161 | SkMatrix mt2; |
| 162 | mt2.setTranslate(-18, -18); |
herb | feec878 | 2016-02-17 10:00:07 -0800 | [diff] [blame] | 163 | SkMatrix ms; |
herb | 6eff52a | 2016-03-23 09:00:33 -0700 | [diff] [blame] | 164 | ms.setScale(2.7f, 2.7f, -1.5f, 0); |
herb | 9466571 | 2016-02-29 07:53:22 -0800 | [diff] [blame] | 165 | SkMatrix ms2; |
herb | 6eff52a | 2016-03-23 09:00:33 -0700 | [diff] [blame] | 166 | ms2.setScale(-0.4f, 0.4f); |
herb | feec878 | 2016-02-17 10:00:07 -0800 | [diff] [blame] | 167 | SkMatrix mr; |
| 168 | mr.setRotate(10); |
| 169 | |
herb | 9466571 | 2016-02-29 07:53:22 -0800 | [diff] [blame] | 170 | const SkMatrix* mats[] = {nullptr, &mi, &mlr, &mt, &mt2, &ms, &ms2, &mr}; |
herb | feec878 | 2016-02-17 10:00:07 -0800 | [diff] [blame] | 171 | |
| 172 | const SkRect r = SkRect::MakeWH(W, H); |
| 173 | bool useBilerp = false; |
| 174 | while (true) { |
| 175 | canvas->save(); |
| 176 | for (auto mat : mats) { |
| 177 | canvas->save(); |
| 178 | for (SkColor c : colors) { |
| 179 | if (mat == nullptr) { |
| 180 | SkPaint p; |
| 181 | p.setColor(c); |
| 182 | draw_rect_none(canvas, r, c); |
| 183 | canvas->translate(W + 20, 0); |
| 184 | draw_rect_none(canvas, r, c); |
| 185 | |
| 186 | } else { |
| 187 | draw_rect_orig(canvas, r, c, mat, useBilerp); |
| 188 | canvas->translate(W + 20, 0); |
| 189 | draw_rect_fp(canvas, r, c, mat, useBilerp); |
| 190 | } |
| 191 | canvas->translate(W + 20, 0); |
| 192 | } |
| 193 | canvas->restore(); |
| 194 | canvas->translate(0, H + 20); |
| 195 | } |
| 196 | canvas->restore(); |
| 197 | canvas->translate(0, (H + 20) * SK_ARRAY_COUNT(mats)); |
| 198 | if (useBilerp) break; |
| 199 | useBilerp = true; |
| 200 | } |
| 201 | } |