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

Side by Side Diff: gm/bitmapalphathreshold.cpp

Issue 23707019: alpha threshold bitmap shader (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: cleanup Created 7 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 | Annotate | Revision Log
« no previous file with comments | « no previous file | gyp/effects.gypi » ('j') | include/effects/SkBitmapAlphaThresholdShader.h » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /*
2 * Copyright 2013 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"
9 #include "SkGradientShader.h"
10 #include "SkBitmapAlphaThresholdShader.h"
11 #include "SkTArray.h"
12 #include "SkParsePath.h"
13
14 class BitmapAlphaThresholdGM : public skiagm::GM {
15 public:
16 BitmapAlphaThresholdGM() {
17 this->setBGColor(0xFF000000);
18 }
19
20 protected:
21 virtual uint32_t onGetFlags() const {
22 // narrow this flags when the shader has a CPU implementation and
23 // when it serializes.
24 return
25 kSkipPDF_Flag |
26 kSkipPicture_Flag |
27 kSkipPipe_Flag |
28 kSkipPipeCrossProcess_Flag |
29 kSkipTiled_Flag |
30 kSkip565_Flag |
31 kSkipScaledReplay_Flag |
32 kSkipPDFRasterization_Flag |
33
34 kGPUOnly_Flag;
35 }
36
37 private:
38 virtual void onOnceBeforeDraw() SK_OVERRIDE {
39 fBM.setConfig(SkBitmap::kARGB_8888_Config, 100, 100);
40 if (!fBM.allocPixels()) {
41 return;
42 }
43 SkCanvas canvas(fBM);
44 SkPoint pts[] = { {0, 0}, {SkIntToScalar(fBM.width()), SkIntToScalar(fBM .height())} };
45 SkColor colors[] = {0x00000000, 0xffffffff};
46 SkShader* grad = SkGradientShader::CreateLinear(pts, colors, NULL, 2,
47 SkShader::kClamp_TileMod e);
48 SkPaint gradPaint;
49 gradPaint.setShader(grad)->unref();
50 gradPaint.setXfermodeMode(SkXfermode::kSrc_Mode);
51 canvas.drawPaint(gradPaint);
52
53 // Construct the region used as a mask.
54 SkRegion bmpBoundsClip;
55 bmpBoundsClip.setRect(0, 0, fBM.width(), fBM.height());
56 SkPath circlePath;
57 SkScalar radius = SkScalarSqrt(SkIntToScalar(fBM.width() * fBM.height()) ) / 2;
58 circlePath.addCircle(SkIntToScalar(fBM.width() / 2),
59 SkIntToScalar(fBM.height() / 2),
60 radius);
61 fMask.setPath(circlePath, bmpBoundsClip);
62
63 SkPath batPath;
64 SkParsePath::FromSVGString(
65 "M305.214,374.779c2.463,0,3.45,0.493,3.45,0.493l1.478-6.241c0,0,1.15,4.7 63,1.643,9.034"
66 "c0.493,4.271,8.048,1.479,14.454,0.164c6.405-1.314,7.72-11.662,7 .72-11.662h59.294c0,0-35.807,10.841-26.772,34.656"
67 "c0,0-52.889-8.048-61.101,24.967h-0.001c-8.212-33.015-61.101-24. 967-61.101-24.967c9.034-23.815-26.772-34.656-26.772-34.656"
68 "h59.294c0,0,1.314,10.348,7.719,11.662c6.406,1.314,13.962,4.106, 14.454-0.164c0.493-4.271,1.643-9.034,1.643-9.034l1.479,6.241"
69 "c0,0,0.985-0.493,3.449-0.493H305.214L305.214,374.779z",
70 &batPath);
71
72 SkMatrix matrix;
73 matrix.setTranslate(-208, -280);
74 matrix.postScale(radius / 100, radius / 100);
75 batPath.transform(matrix, &batPath);
76 SkRegion batRegion;
77 batRegion.setPath(batPath, bmpBoundsClip);
78
79 fMask.op(batRegion, SkRegion::kDifference_Op);
80 }
81
82 virtual SkString onShortName() SK_OVERRIDE {
83 return SkString("bat");
84 }
85
86 virtual SkISize onISize() SK_OVERRIDE {
87 return SkISize::Make(920, 480);
88 }
89
90 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
91
92 SkTArray<SkMatrix> lms;
93 lms.push_back().reset();
94 lms.push_back().setScale(SK_Scalar1 / 2, SK_Scalar1);
95 lms.push_back().setScale(SK_Scalar1, 2 * SK_Scalar1);
96 lms.push_back().setSkew(0, SK_Scalar1 / 200);
97 lms.push_back().setRotate(SK_Scalar1 * 10);
98
99 static const SkScalar kMargin = 5 * SK_Scalar1;
100 canvas->translate(kMargin, kMargin);
101 canvas->save();
102
103 static const U8CPU kThresholds[] = { 0x0, 0x08, 0x40, 0x80, 0xC0, 0xF0, 0xFF };
104
105 for (size_t i = 0; i < SK_ARRAY_COUNT(kThresholds); ++i) {
106 for (int j = 0; j < lms.count(); ++j) {
107 SkRect rect;
108 rect.fLeft = 0;
109 rect.fTop = 0;
110 rect.fRight = SkIntToScalar(fBM.width());
111 rect.fBottom = SkIntToScalar(fBM.height());
112
113 SkShader* thresh;
114 // This SkShader currently only has a GPU implementation.
115 if (canvas->getDevice()->accessRenderTarget()) {
116 thresh = SkBitmapAlphaThresholdShader::Create(fBM, fMask, kT hresholds[i]);
117 } else {
118 thresh = SkShader::CreateBitmapShader(fBM, SkShader::kClamp_ TileMode, SkShader::kClamp_TileMode);
119 }
120
121 thresh->setLocalMatrix(lms[j]);
122
123 SkPaint paint;
124 paint.setShader(thresh)->unref();
125
126 canvas->drawRect(rect, paint);
127 canvas->translate(SkIntToScalar(fBM.width() + kMargin), 0);
128 }
129 canvas->restore();
130 canvas->translate(0, SkIntToScalar(fBM.height() + kMargin));
131 canvas->save();
132 }
133
134 }
135
136 SkBitmap fBM;
137 SkRegion fMask;
138
139 typedef skiagm::GM INHERITED;
140 };
141
142 //////////////////////////////////////////////////////////////////////////////
143
144 DEF_GM( return new BitmapAlphaThresholdGM(); )
OLDNEW
« no previous file with comments | « no previous file | gyp/effects.gypi » ('j') | include/effects/SkBitmapAlphaThresholdShader.h » ('J')

Powered by Google App Engine
This is Rietveld 408576698