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