2 * Copyright 2016 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
10 #include "SkArenaAlloc.h"
11 #include "SkBlitter.h"
15 #include "SkImageInfo.h"
16 #include "SkLinearBitmapPipeline.h"
17 #include "SkXfermodePriv.h"
18 #include "SkPM4fPriv.h"
21 static void fill_in_bits(SkBitmap& bm, SkIRect ir, SkColor c, bool premul) {
22 bm.allocN32Pixels(ir.width(), ir.height());
26 SkPMColor b = SkColorSetARGBMacro(255, 0, 0, 0);
29 w = SkPreMultiplyColor(c);
31 w = SkPackARGB32NoCheck(SkColorGetA(c), SkColorGetR(c), SkColorGetG(c), SkColorGetB(c));
34 for (int y = 0; y < ir.height(); y++) {
35 for (int x = 0; x < ir.width(); x++) {
37 *pm.writable_addr32(x, y) = b;
39 *pm.writable_addr32(x, y) = w;
45 static void draw_rect_orig(SkCanvas* canvas, const SkRect& r, SkColor c, const SkMatrix* mat, bool useBilerp) {
46 const SkIRect ir = r.round();
49 fill_in_bits(bmsrc, ir, c, true);
52 bmsrc.peekPixels(&pmsrc);
55 bmdst.allocN32Pixels(ir.width(), ir.height());
56 bmdst.eraseColor(0xFFFFFFFF);
58 bmdst.peekPixels(&pmdst);
60 SkImageInfo info = SkImageInfo::MakeN32Premul(ir.width(), ir.height());
62 sk_sp<SkImage> image(SkImage::MakeRasterCopy(SkPixmap(info, pmsrc.addr32(), pmsrc.rowBytes())));
64 SkArenaAlloc alloc{0};
66 sk_sp<SkShader> shader = image->makeShader(SkShader::kRepeat_TileMode,
67 SkShader::kRepeat_TileMode);
70 paint.setFilterQuality(SkFilterQuality::kLow_SkFilterQuality);
72 paint.setFilterQuality(SkFilterQuality::kNone_SkFilterQuality);
74 paint.setShader(std::move(shader));
75 const SkShader::ContextRec rec(paint, *mat, nullptr,
76 SkBlitter::PreferredShaderDest(pmsrc.info()),
77 canvas->imageInfo().colorSpace());
79 SkShader::Context* ctx = paint.getShader()->makeContext(rec, &alloc);
81 for (int y = 0; y < ir.height(); y++) {
82 ctx->shadeSpan(0, y, pmdst.writable_addr32(0, y), ir.width());
85 canvas->drawBitmap(bmdst, r.left(), r.top(), nullptr);
88 static void draw_rect_fp(SkCanvas* canvas, const SkRect& r, SkColor c, const SkMatrix* mat, bool useBilerp) {
89 const SkIRect ir = r.round();
92 fill_in_bits(bmsrc, ir, c, true);
94 bmsrc.peekPixels(&pmsrc);
97 bmdst.allocN32Pixels(ir.width(), ir.height());
98 bmdst.eraseColor(0xFFFFFFFF);
100 bmdst.peekPixels(&pmdst);
102 SkPM4f* dstBits = new SkPM4f[ir.width()];
105 bool trash = mat->invert(&inv);
106 sk_ignore_unused_variable(trash);
108 SkFilterQuality filterQuality;
110 filterQuality = SkFilterQuality::kLow_SkFilterQuality;
112 filterQuality = SkFilterQuality::kNone_SkFilterQuality;
116 auto procN = SkXfermode::GetD32Proc(SkBlendMode::kSrcOver, flags);
119 SkArenaAlloc allocator{storage, sizeof(storage)};
120 SkLinearBitmapPipeline pipeline{
122 SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode,
123 SK_ColorBLACK, pmsrc, &allocator};
125 for (int y = 0; y < ir.height(); y++) {
126 pipeline.shadeSpan4f(0, y, dstBits, ir.width());
127 procN(SkBlendMode::kSrcOver, pmdst.writable_addr32(0, y), dstBits, ir.width(), nullptr);
132 canvas->drawBitmap(bmdst, r.left(), r.top(), nullptr);
135 static void draw_rect_none(SkCanvas* canvas, const SkRect& r, SkColor c) {
136 const SkIRect ir = r.round();
139 fill_in_bits(bm, ir, c, true);
141 canvas->drawBitmap(bm, r.left(), r.top(), nullptr);
145 * Test SkXfer4fProcs directly for src-over, comparing them to current SkColor blits.
147 DEF_SIMPLE_GM(linear_pipeline, canvas, 580, 2200) {
149 const SkScalar W = IW;
150 const SkScalar H = 100;
152 const SkColor colors[] = {
153 0x880000FF, 0x8800FF00, 0x88FF0000, 0x88000000,
154 SK_ColorBLUE, SK_ColorGREEN, SK_ColorRED, SK_ColorBLACK,
157 canvas->translate(20, 20);
159 SkMatrix mi = SkMatrix::I();
161 mlr.setScale(-1.0f, 1.0f, 20, 0.0f);
163 mt.setTranslate(8, 8);
165 mt2.setTranslate(-18, -18);
167 ms.setScale(2.7f, 2.7f, -1.5f, 0);
169 ms2.setScale(-0.4f, 0.4f);
173 const SkMatrix* mats[] = {nullptr, &mi, &mlr, &mt, &mt2, &ms, &ms2, &mr};
175 const SkRect r = SkRect::MakeWH(W, H);
176 bool useBilerp = false;
179 for (auto mat : mats) {
181 for (SkColor c : colors) {
182 if (mat == nullptr) {
185 draw_rect_none(canvas, r, c);
186 canvas->translate(W + 20, 0);
187 draw_rect_none(canvas, r, c);
190 draw_rect_orig(canvas, r, c, mat, useBilerp);
191 canvas->translate(W + 20, 0);
192 draw_rect_fp(canvas, r, c, mat, useBilerp);
194 canvas->translate(W + 20, 0);
197 canvas->translate(0, H + 20);
200 canvas->translate(0, (H + 20) * SK_ARRAY_COUNT(mats));
201 if (useBilerp) break;