Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(633)

Side by Side Diff: gm/aaxfermodes.cpp

Issue 1124373002: Implement Porter Duff XP with a blend table (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Fix issues with clang and msvc Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | gyp/gpu.gypi » ('j') | src/gpu/effects/GrPorterDuffXferProcessor.cpp » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1
2 /*
3 * Copyright 2015 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8 #include "gm.h"
9 #include "SkArithmeticMode.h"
10 #include "SkShader.h"
11 #include "SkXfermode.h"
12
13 enum {
14 kXfermodeCount = SkXfermode::kLastMode + 2, // All xfermodes plus arithmetic mode.
15 kShapeSize = 22,
16 kShapeSpacing = 36,
17 kShapeTypeSpacing = 4 * kShapeSpacing/3,
18 kPaintSpacing = 2 * kShapeSpacing,
19 kPadding = (kPaintSpacing - kShapeSpacing) / 2,
20
21 kPaintWidth = 3*kShapeSpacing + 2*kShapeTypeSpacing + kShapeSize,
22 kPaintPadding = kPaintSpacing - kShapeSize,
23 };
24
25 static const SkColor kBGColor = SkColorSetARGB(200, 210, 184, 135);
26
27 static const SkColor kShapeColors[3] = {
28 SkColorSetARGB(130, 255, 0, 128), // input color unknown
29 SkColorSetARGB(255, 0, 255, 255), // input color opaque
30 SkColorSetARGB(255, 255, 255, 255) // input solid white
31 };
32
33 enum Shape {
34 kSquare_Shape,
35 kDiamond_Shape,
36 kOval_Shape,
37
38 kLast_Shape = kOval_Shape
39 };
40
41 namespace skiagm {
42
43 static void draw_shape(SkCanvas* canvas, Shape shape, const SkColor color, size_ t xfermodeIdx);
44
45 /**
46 * Verifies AA works properly on all Xfermodes, including arithmetic, with vario us color invariants.
47 */
48 class AAXfermodesGM : public GM {
49 public:
50 AAXfermodesGM() {}
51
52 protected:
53 SkString onShortName() override {
54 return SkString("aaxfermodes");
55 }
56
57 SkISize onISize() override {
58 return SkISize::Make(3*kPaintWidth + 2*kPaintPadding + 2*kPadding,
59 (2 + SkXfermode::kLastCoeffMode) * kShapeSpacing + 2*kPadding);
60 }
61
62 void onDraw(SkCanvas* canvas) override {
63 sk_tool_utils::draw_checkerboard(canvas, 0xffffffff, 0xffc0c0c0, 10);
64
65 canvas->saveLayer(NULL, NULL);
66 canvas->drawColor(kBGColor, SkXfermode::kSrc_Mode);
67
68 canvas->translate(kPadding + kShapeSize/2, kPadding + kShapeSpacing + kS hapeSize/2);
69
70 for (size_t colorIdx = 0; colorIdx < SK_ARRAY_COUNT(kShapeColors); color Idx++) {
71 SkColor color = kShapeColors[colorIdx];
72
73 for (size_t shapeIdx = 0; shapeIdx <= kLast_Shape; shapeIdx++) {
74 Shape shape = static_cast<Shape>(shapeIdx);
75 canvas->save();
76
77 for (size_t xfermodeIdx = 0; xfermodeIdx < kXfermodeCount; xferm odeIdx++) {
78 draw_shape(canvas, shape, color, xfermodeIdx);
79
80 if (xfermodeIdx == SkXfermode::kLastCoeffMode) {
81 // New column.
82 canvas->restore();
83 canvas->translate(kShapeSpacing, 0);
84 canvas->save();
85 } else {
86 canvas->translate(0, kShapeSpacing);
87 }
88 }
89
90 canvas->restore();
91
92 if (shape != kLast_Shape) {
93 canvas->translate(kShapeTypeSpacing, 0);
94 } else {
95 canvas->translate(kPaintSpacing, 0);
96 }
97 }
98 }
99
100 canvas->restore();
101
102 SkPaint textPaint;
103 textPaint.setAntiAlias(true);
104 sk_tool_utils::set_portable_typeface(&textPaint);
105 textPaint.setTextAlign(SkPaint::kCenter_Align);
106 textPaint.setFakeBoldText(true);
107 textPaint.setTextSize(21 * kShapeSize/32);
108
109 canvas->translate(kPadding + kPaintWidth/2,
110 kPadding + kShapeSize/2 + textPaint.getTextSize()/4);
111 canvas->drawText("input color unknown", sizeof("input color unknown") - 1, 0, 0, textPaint);
112
113 canvas->translate(kPaintWidth + kPaintPadding, 0);
114 canvas->drawText("input color opaque", sizeof("input color opaque") - 1, 0, 0, textPaint);
115
116 canvas->translate(kPaintWidth + kPaintPadding, 0);
117 canvas->drawText("input solid white", sizeof("input solid white") - 1, 0 , 0, textPaint);
118 }
119
120 private:
121 typedef GM INHERITED;
122 };
123
124 static void draw_shape(SkCanvas* canvas, Shape shape, const SkColor color, size_ t xfermodeIdx) {
125 SkPaint shapePaint;
126 shapePaint.setAntiAlias(kSquare_Shape != shape);
127 shapePaint.setColor(color);
128
129 SkAutoTUnref<SkXfermode> xfermode;
130 if (xfermodeIdx <= SkXfermode::kLastMode) {
131 SkXfermode::Mode mode = static_cast<SkXfermode::Mode>(xfermodeIdx);
132 xfermode.reset(SkXfermode::Create(mode));
133 } else {
134 xfermode.reset(SkArithmeticMode::Create(+1.0f, +0.25f, -0.5f, +0.1f));
135 }
136 shapePaint.setXfermode(xfermode);
137
138 if (xfermodeIdx == SkXfermode::kPlus_Mode) {
139 // Check for overflow and dim the src and dst colors if we need to, othe rwise we might get
140 // confusing AA artifacts.
141 int maxSum = SkTMax(SkTMax(SkColorGetA(kBGColor) + SkColorGetA(color),
142 SkColorGetR(kBGColor) + SkColorGetR(color)),
143 SkTMax(SkColorGetG(kBGColor) + SkColorGetG(color),
144 SkColorGetB(kBGColor) + SkColorGetB(color)));
145
146 if (maxSum > 255) {
147 SkPaint dimPaint;
148 dimPaint.setARGB(255 * 255 / maxSum, 0, 0, 0);
149 dimPaint.setAntiAlias(false);
150 dimPaint.setXfermode(SkXfermode::Create(SkXfermode::kDstIn_Mode));
151 canvas->drawRectCoords(-kShapeSpacing/2, -kShapeSpacing/2,
152 kShapeSpacing/2, kShapeSpacing/2, dimPaint);
153
154 shapePaint.setAlpha(255 * shapePaint.getAlpha() / maxSum);
155 }
156 }
157
158 switch (shape) {
159 case kSquare_Shape:
160 canvas->drawRectCoords(-kShapeSize/2, -kShapeSize/2, kShapeSize/2, k ShapeSize/2,
161 shapePaint);
162 break;
163
164 case kDiamond_Shape:
165 canvas->save();
166 canvas->rotate(45);
167 canvas->drawRectCoords(-kShapeSize/2, -kShapeSize/2, kShapeSize/2, k ShapeSize/2,
168 shapePaint);
169 canvas->restore();
170 break;
171
172 case kOval_Shape:
173 canvas->save();
174 canvas->rotate(static_cast<SkScalar>((511 * xfermodeIdx + 257) % 360 ));
175 canvas->drawArc(SkRect::MakeLTRB(-kShapeSize/2, -1.4f * kShapeSize/2 ,
176 kShapeSize/2, 1.4f * kShapeSize/2),
177 0, 360, true, shapePaint);
178 canvas->restore();
179 break;
180
181 default:
182 SkFAIL("Invalid shape.");
183 }
184 }
185
186 //////////////////////////////////////////////////////////////////////////////
187
188 static GM* MyFactory(void*) { return new AAXfermodesGM; }
189 static GMRegistry reg(MyFactory);
190
191 }
OLDNEW
« no previous file with comments | « no previous file | gyp/gpu.gypi » ('j') | src/gpu/effects/GrPorterDuffXferProcessor.cpp » ('J')

Powered by Google App Engine
This is Rietveld 408576698