blob: 0a66b9c4df357809c3e6b5f5051642e4f96f120f [file] [log] [blame]
herbfeec8782016-02-17 10:00:07 -08001/*
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"
fmalitad0c4e092016-02-22 17:19:04 -08009#include "SkBlitter.h"
herbfeec8782016-02-17 10:00:07 -080010#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"
herb42da45d2016-04-08 14:24:37 -070018#include "SkBitmapProcShader.h"
herbfeec8782016-02-17 10:00:07 -080019
20static 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
44static 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
reed9ce9d672016-03-17 10:51:11 -070061 sk_sp<SkImage> image(SkImage::MakeRasterCopy(SkPixmap(info, pmsrc.addr32(), pmsrc.rowBytes())));
herbfeec8782016-02-17 10:00:07 -080062 SkPaint paint;
herb42da45d2016-04-08 14:24:37 -070063 int32_t storage[kSkBlitterContextSize];
herb6eff52a2016-03-23 09:00:33 -070064
65 sk_sp<SkShader> shader = image->makeShader(SkShader::kRepeat_TileMode,
66 SkShader::kRepeat_TileMode);
67
herbfeec8782016-02-17 10:00:07 -080068 if (useBilerp) {
69 paint.setFilterQuality(SkFilterQuality::kLow_SkFilterQuality);
70 } else {
71 paint.setFilterQuality(SkFilterQuality::kNone_SkFilterQuality);
72 }
herb6eff52a2016-03-23 09:00:33 -070073 paint.setShader(std::move(shader));
fmalitad0c4e092016-02-22 17:19:04 -080074 const SkShader::ContextRec rec(paint, *mat, nullptr,
75 SkBlitter::PreferredShaderDest(pmsrc.info()));
fmalita8d9f2e42016-02-22 10:39:41 -080076 SkASSERT(paint.getShader()->contextSize(rec) <= sizeof(storage));
herbfeec8782016-02-17 10:00:07 -080077
fmalita8d9f2e42016-02-22 10:39:41 -080078 SkShader::Context* ctx = paint.getShader()->createContext(rec, storage);
herbfeec8782016-02-17 10:00:07 -080079
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();
herbfeec8782016-02-17 10:00:07 -080087}
88
89static 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()];
herbfeec8782016-02-17 10:00:07 -0800104
105 SkMatrix inv;
106 bool trash = mat->invert(&inv);
107 sk_ignore_unused_variable(trash);
108
herbc5eddd72016-02-17 19:50:05 -0800109 SkFilterQuality filterQuality;
110 if (useBilerp) {
111 filterQuality = SkFilterQuality::kLow_SkFilterQuality;
112 } else {
113 filterQuality = SkFilterQuality::kNone_SkFilterQuality;
114 }
herbfeec8782016-02-17 10:00:07 -0800115
116 uint32_t flags = 0;
117 //if (kSRGB_SkColorProfileType == profile) {
118 //flags |= SkXfermode::kDstIsSRGB_PM4fFlag;
119 //}
reed8f7b0b22016-02-24 06:21:36 -0800120 auto procN = SkXfermode::GetD32Proc(nullptr, flags);
herbfeec8782016-02-17 10:00:07 -0800121
122 SkLinearBitmapPipeline pipeline{
herbc5eddd72016-02-17 19:50:05 -0800123 inv, filterQuality,
herb670f01f2016-05-13 10:04:46 -0700124 SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode, SK_ColorBLACK, pmsrc};
herbfeec8782016-02-17 10:00:07 -0800125
126 for (int y = 0; y < ir.height(); y++) {
127 pipeline.shadeSpan4f(0, y, dstBits, ir.width());
reed8f7b0b22016-02-24 06:21:36 -0800128 procN(nullptr, pmdst.writable_addr32(0, y), dstBits, ir.width(), nullptr);
herbfeec8782016-02-17 10:00:07 -0800129 }
130
131 delete [] dstBits;
132
133 canvas->drawBitmap(bmdst, r.left(), r.top(), nullptr);
134}
135
136static 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 */
herb94665712016-02-29 07:53:22 -0800148DEF_SIMPLE_GM(linear_pipeline, canvas, 580, 2200) {
herbfeec8782016-02-17 10:00:07 -0800149 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();
herb94665712016-02-29 07:53:22 -0800161 SkMatrix mlr;
162 mlr.setScale(-1.0f, 1.0f, 20, 0.0f);
herbfeec8782016-02-17 10:00:07 -0800163 SkMatrix mt;
164 mt.setTranslate(8, 8);
herb94665712016-02-29 07:53:22 -0800165 SkMatrix mt2;
166 mt2.setTranslate(-18, -18);
herbfeec8782016-02-17 10:00:07 -0800167 SkMatrix ms;
herb6eff52a2016-03-23 09:00:33 -0700168 ms.setScale(2.7f, 2.7f, -1.5f, 0);
herb94665712016-02-29 07:53:22 -0800169 SkMatrix ms2;
herb6eff52a2016-03-23 09:00:33 -0700170 ms2.setScale(-0.4f, 0.4f);
herbfeec8782016-02-17 10:00:07 -0800171 SkMatrix mr;
172 mr.setRotate(10);
173
herb94665712016-02-29 07:53:22 -0800174 const SkMatrix* mats[] = {nullptr, &mi, &mlr, &mt, &mt2, &ms, &ms2, &mr};
herbfeec8782016-02-17 10:00:07 -0800175
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}