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

Side by Side Diff: gm/composeshader.cpp

Issue 1311043008: Added ComposeShaderBitmapGM that tests composing bitmaps with gradients (Closed) Base URL: https://skia.googlesource.com/skia@cs3_programstest
Patch Set: Created 5 years, 3 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 | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2012 Google Inc. 2 * Copyright 2012 Google Inc.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 7
8 #include "gm.h" 8 #include "gm.h"
9 9
10 #include "SkCanvas.h" 10 #include "SkCanvas.h"
11 #include "SkComposeShader.h" 11 #include "SkComposeShader.h"
12 #include "SkGradientShader.h" 12 #include "SkGradientShader.h"
13 #include "SkGraphics.h" 13 #include "SkGraphics.h"
14 #include "SkShader.h" 14 #include "SkShader.h"
15 #include "SkString.h" 15 #include "SkString.h"
16 #include "SkXfermode.h" 16 #include "SkXfermode.h"
17 #include "SkBitmapProcShader.h"
egdaniel 2015/09/02 20:15:36 Alphabetize this include
wangyix 2015/09/02 20:49:35 Done.
17 18
18 static SkShader* make_shader(SkXfermode::Mode mode) { 19 static SkShader* make_shader(SkXfermode::Mode mode) {
19 SkPoint pts[2]; 20 SkPoint pts[2];
20 SkColor colors[2]; 21 SkColor colors[2];
21 22
22 pts[0].set(0, 0); 23 pts[0].set(0, 0);
23 pts[1].set(SkIntToScalar(100), 0); 24 pts[1].set(SkIntToScalar(100), 0);
24 colors[0] = SK_ColorRED; 25 colors[0] = SK_ColorRED;
25 colors[1] = SK_ColorBLUE; 26 colors[1] = SK_ColorBLUE;
26 SkAutoTUnref<SkShader> shaderA(SkGradientShader::CreateLinear(pts, colors, n ullptr, 2, 27 SkAutoTUnref<SkShader> shaderA(SkGradientShader::CreateLinear(pts, colors, n ullptr, 2,
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
77 class ComposeShaderAlphaGM : public skiagm::GM { 78 class ComposeShaderAlphaGM : public skiagm::GM {
78 public: 79 public:
79 ComposeShaderAlphaGM() {} 80 ComposeShaderAlphaGM() {}
80 81
81 protected: 82 protected:
82 SkString onShortName() override { 83 SkString onShortName() override {
83 return SkString("composeshader_alpha"); 84 return SkString("composeshader_alpha");
84 } 85 }
85 86
86 SkISize onISize() override { 87 SkISize onISize() override {
87 return SkISize::Make(220, 750); 88 return SkISize::Make(750, 220);
88 } 89 }
89 90
90 void onDraw(SkCanvas* canvas) override { 91 void onDraw(SkCanvas* canvas) override {
91 SkAutoTUnref<SkShader> shader0(make_shader(SkXfermode::kDstIn_Mode)); 92 SkAutoTUnref<SkShader> shader0(make_shader(SkXfermode::kDstIn_Mode));
92 SkAutoTUnref<SkShader> shader1(make_shader(SkXfermode::kSrcOver_Mode)); 93 SkAutoTUnref<SkShader> shader1(make_shader(SkXfermode::kSrcOver_Mode));
93 SkShader* shaders[] = { shader0.get(), shader1.get() }; 94 SkShader* shaders[] = { shader0.get(), shader1.get() };
94 95
95 SkPaint paint; 96 SkPaint paint;
96 paint.setColor(SK_ColorGREEN); 97 paint.setColor(SK_ColorGREEN);
97 98
(...skipping 10 matching lines...) Expand all
108 paint.setAlpha(alpha); 109 paint.setAlpha(alpha);
109 paint.setShader(shader); 110 paint.setShader(shader);
110 canvas->drawRect(r, paint); 111 canvas->drawRect(r, paint);
111 112
112 canvas->translate(r.width() + 5, 0); 113 canvas->translate(r.width() + 5, 0);
113 } 114 }
114 canvas->restore(); 115 canvas->restore();
115 canvas->translate(0, r.height() + 5); 116 canvas->translate(0, r.height() + 5);
116 } 117 }
117 } 118 }
119
120 private:
121 typedef GM INHERITED ;
122 };
123
124
125 // creates a square bitmap with red background and a green circle in the center
126 static void draw_color_bm(SkBitmap* bm, int length) {
127 SkPaint paint;
128 paint.setColor(SK_ColorGREEN);
129
130 bm->allocN32Pixels(length, length);
131 bm->eraseColor(SK_ColorRED);
132
133 SkCanvas canvas(*bm);
134 canvas.drawCircle(length/2, length/2, length/2, paint);
135 }
136
137 // creates a square alpha8 bitmap with transparent background and an opaque circ le in the center
138 static void draw_alpha8_bm(SkBitmap* bm, int length) {
139 SkPaint circlePaint;
140 circlePaint.setColor(SK_ColorBLACK);
141
142 bm->allocPixels(SkImageInfo::MakeA8(length, length));
143 bm->eraseColor(SK_ColorTRANSPARENT);
144
145 SkCanvas canvas(*bm);
146 canvas.drawCircle(length/2, length/2, length/4, circlePaint);
147 }
148
149 // creates a linear gradient shader
150 static SkShader* make_linear_gradient_shader(int length) {
151 SkPoint pts[2];
152 SkColor colors[2];
153 pts[0].set(0, 0);
154 pts[1].set(SkIntToScalar(length), 0);
155 colors[0] = SK_ColorBLUE;
156 colors[1] = SkColorSetARGB(0, 0, 0, 0xFF);
157 return SkGradientShader::CreateLinear(pts, colors, nullptr, 2, SkShader::kCl amp_TileMode);
158 }
159
160
161 class ComposeShaderBitmapGM : public skiagm::GM {
162 public:
163 /** Can set the dimension of the bitmap that will be used. This way, larger numbers can be
egdaniel 2015/09/02 20:15:36 Are you saying this will draw differently in relea
wangyix 2015/09/02 20:27:43 No, I was trying to say you can easily change this
164 * used for release mode since debug mode will fail an assertion in SkSmallA llocator unless the
egdaniel 2015/09/02 20:15:36 align comment
165 * size of the bitmap is sufficiently small, making the result hard to see.
166 */
167 ComposeShaderBitmapGM(int squareSize)
168 : squareLength(squareSize) {
169 draw_color_bm(&fColorBitmap, squareLength);
170 draw_alpha8_bm(&fAlpha8Bitmap, squareLength);
171 SkMatrix s;
172 s.reset();
173 fColorBitmapShader = new SkBitmapProcShader(fColorBitmap, SkShader::kRep eat_TileMode,
174 SkShader::kRepeat_TileMode, &s);
175 fAlpha8BitmapShader = new SkBitmapProcShader(fAlpha8Bitmap, SkShader::kR epeat_TileMode,
176 SkShader::kRepeat_TileMode, &s);
177 fLinearGradientShader = make_linear_gradient_shader(squareLength);
178 }
179 ~ComposeShaderBitmapGM() {
180 SkSafeUnref(fColorBitmapShader);
181 SkSafeUnref(fAlpha8BitmapShader);
182 SkSafeUnref(fLinearGradientShader);
183 }
184
185 protected:
186 SkString onShortName() override {
187 return SkString("composeshader_bitmap");
188 }
189
190 SkISize onISize() override {
191 return SkISize::Make(7 * (squareLength + 5), 2 * (squareLength + 5));
192 }
193
194 void onDraw(SkCanvas* canvas) override {
195 SkAutoTUnref<SkXfermode> xfer(SkXfermode::Create(SkXfermode::kDstOver_Mo de));
egdaniel 2015/09/02 20:15:36 I wonder if it would be interesting to loop over x
196
197 // gradient should appear over color bitmap
198 SkAutoTUnref<SkShader> shader0(new SkComposeShader(fLinearGradientShader ,
199 fColorBitmapShader,
200 xfer));
201 // gradient should appear over alpha8 bitmap colorized by the paint colo r
202 SkAutoTUnref<SkShader> shader1(new SkComposeShader(fLinearGradientShader ,
203 fAlpha8BitmapShader,
204 xfer));
205
206 SkShader* shaders[] = { shader0.get(), shader1.get() };
207
208 SkPaint paint;
209 paint.setColor(SK_ColorYELLOW);
210
211 const SkRect r = SkRect::MakeXYWH(0, 0, squareLength, squareLength);
212
213 for (size_t y = 0; y < SK_ARRAY_COUNT(shaders); ++y) {
214 SkShader* shader = shaders[y];
215 canvas->save();
216 for (int alpha = 0xFF; alpha > 0; alpha -= 0x28) {
217 paint.setAlpha(alpha);
218 paint.setShader(shader);
219 canvas->drawRect(r, paint);
220
221 canvas->translate(r.width() + 5, 0);
222 }
223 canvas->restore();
224 canvas->translate(0, r.height() + 5);
225 }
226 }
227 private:
228 const int squareLength;
229 SkBitmap fColorBitmap;
230 SkBitmap fAlpha8Bitmap;
231 SkShader* fColorBitmapShader;
232 SkShader* fAlpha8BitmapShader;
233 SkShader* fLinearGradientShader;
234
235 typedef GM INHERITED;
118 }; 236 };
119 237
120 ////////////////////////////////////////////////////////////////////////////// 238 //////////////////////////////////////////////////////////////////////////////
121 239
122 DEF_GM( return new ComposeShaderGM; ) 240 DEF_GM( return new ComposeShaderGM; )
123 DEF_GM( return new ComposeShaderAlphaGM; ) 241 DEF_GM( return new ComposeShaderAlphaGM; )
242 DEF_GM( return new ComposeShaderBitmapGM(20); )
egdaniel 2015/09/02 20:15:36 This is a general question to anyone, but do we ha
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698