OLD | NEW |
1 | |
2 /* | 1 /* |
3 * Copyright 2013 Google Inc. | 2 * Copyright 2013 Google Inc. |
4 * | 3 * |
5 * 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 |
6 * found in the LICENSE file. | 5 * found in the LICENSE file. |
7 */ | 6 */ |
| 7 |
8 #include "gm.h" | 8 #include "gm.h" |
9 #include "SkBitmap.h" | 9 #include "SkBitmap.h" |
10 #include "SkGradientShader.h" | 10 #include "SkGradientShader.h" |
| 11 #include "SkSurface.h" |
11 #include "SkXfermode.h" | 12 #include "SkXfermode.h" |
12 #include "SkColorPriv.h" | 13 #include "SkColorPriv.h" |
13 | 14 |
14 #if SK_SUPPORT_GPU | 15 #if SK_SUPPORT_GPU |
15 #include "GrContext.h" | 16 #include "GrContext.h" |
16 #include "SkGpuDevice.h" | 17 #include "SkGpuDevice.h" |
17 #endif | 18 #endif |
18 | 19 |
19 namespace skiagm { | 20 namespace skiagm { |
20 | 21 |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
52 SK_ColorTRANSPARENT, | 53 SK_ColorTRANSPARENT, |
53 SK_ColorBLUE, | 54 SK_ColorBLUE, |
54 0x80808000 | 55 0x80808000 |
55 }; | 56 }; |
56 | 57 |
57 static const SkColor kBmpAlphas[] = { | 58 static const SkColor kBmpAlphas[] = { |
58 0xff, | 59 0xff, |
59 0x80, | 60 0x80, |
60 }; | 61 }; |
61 | 62 |
62 SkAutoTUnref<SkCanvas> tempCanvas(this->possiblyCreateTempCanvas(canvas,
kSize, kSize)); | 63 SkAutoTUnref<SkSurface> tempSurface(this->possiblyCreateTempSurface(canv
as, kSize, kSize)); |
63 | 64 |
64 int test = 0; | 65 int test = 0; |
65 int x = 0, y = 0; | 66 int x = 0, y = 0; |
66 static const struct { SkPaint::Style fStyle; SkScalar fWidth; } kStrokes
[] = { | 67 static const struct { SkPaint::Style fStyle; SkScalar fWidth; } kStrokes
[] = { |
67 {SkPaint::kFill_Style, 0}, | 68 {SkPaint::kFill_Style, 0}, |
68 {SkPaint::kStroke_Style, SkIntToScalar(kSize) / 2}, | 69 {SkPaint::kStroke_Style, SkIntToScalar(kSize) / 2}, |
69 }; | 70 }; |
70 for (size_t s = 0; s < SK_ARRAY_COUNT(kStrokes); ++s) { | 71 for (size_t s = 0; s < SK_ARRAY_COUNT(kStrokes); ++s) { |
71 for (size_t m = 0; m <= SkXfermode::kLastMode; ++m) { | 72 for (size_t m = 0; m <= SkXfermode::kLastMode; ++m) { |
72 SkXfermode::Mode mode = static_cast<SkXfermode::Mode>(m); | 73 SkXfermode::Mode mode = static_cast<SkXfermode::Mode>(m); |
73 canvas->drawText(SkXfermode::ModeName(mode), | 74 canvas->drawText(SkXfermode::ModeName(mode), |
74 strlen(SkXfermode::ModeName(mode)), | 75 strlen(SkXfermode::ModeName(mode)), |
75 SkIntToScalar(x), | 76 SkIntToScalar(x), |
76 SkIntToScalar(y + kSize + 3) + labelP.getTextSi
ze(), | 77 SkIntToScalar(y + kSize + 3) + labelP.getTextSi
ze(), |
77 labelP); | 78 labelP); |
78 for (size_t c = 0; c < SK_ARRAY_COUNT(kSolidColors); ++c) { | 79 for (size_t c = 0; c < SK_ARRAY_COUNT(kSolidColors); ++c) { |
79 SkPaint modePaint; | 80 SkPaint modePaint; |
80 modePaint.setXfermodeMode(mode); | 81 modePaint.setXfermodeMode(mode); |
81 modePaint.setColor(kSolidColors[c]); | 82 modePaint.setColor(kSolidColors[c]); |
82 modePaint.setStyle(kStrokes[s].fStyle); | 83 modePaint.setStyle(kStrokes[s].fStyle); |
83 modePaint.setStrokeWidth(kStrokes[s].fWidth); | 84 modePaint.setStrokeWidth(kStrokes[s].fWidth); |
84 | 85 |
85 this->drawMode(canvas, x, y, kSize, kSize, modePaint, tempCa
nvas.get()); | 86 this->drawMode(canvas, x, y, kSize, kSize, modePaint, tempSu
rface); |
86 | 87 |
87 ++test; | 88 ++test; |
88 x += kSize + 10; | 89 x += kSize + 10; |
89 if (!(test % kTestsPerRow)) { | 90 if (!(test % kTestsPerRow)) { |
90 x = 0; | 91 x = 0; |
91 y += kSize + 30; | 92 y += kSize + 30; |
92 } | 93 } |
93 } | 94 } |
94 for (size_t a = 0; a < SK_ARRAY_COUNT(kBmpAlphas); ++a) { | 95 for (size_t a = 0; a < SK_ARRAY_COUNT(kBmpAlphas); ++a) { |
95 SkPaint modePaint; | 96 SkPaint modePaint; |
96 modePaint.setXfermodeMode(mode); | 97 modePaint.setXfermodeMode(mode); |
97 modePaint.setAlpha(kBmpAlphas[a]); | 98 modePaint.setAlpha(kBmpAlphas[a]); |
98 modePaint.setShader(fBmpShader); | 99 modePaint.setShader(fBmpShader); |
99 modePaint.setStyle(kStrokes[s].fStyle); | 100 modePaint.setStyle(kStrokes[s].fStyle); |
100 modePaint.setStrokeWidth(kStrokes[s].fWidth); | 101 modePaint.setStrokeWidth(kStrokes[s].fWidth); |
101 | 102 |
102 this->drawMode(canvas, x, y, kSize, kSize, modePaint, tempCa
nvas.get()); | 103 this->drawMode(canvas, x, y, kSize, kSize, modePaint, tempSu
rface); |
103 | 104 |
104 ++test; | 105 ++test; |
105 x += kSize + 10; | 106 x += kSize + 10; |
106 if (!(test % kTestsPerRow)) { | 107 if (!(test % kTestsPerRow)) { |
107 x = 0; | 108 x = 0; |
108 y += kSize + 30; | 109 y += kSize + 30; |
109 } | 110 } |
110 } | 111 } |
111 } | 112 } |
112 } | 113 } |
113 } | 114 } |
114 | 115 |
115 private: | 116 private: |
116 /** | 117 /** |
117 * GrContext has optimizations around full rendertarget draws that can be re
placed with clears. | 118 * GrContext has optimizations around full rendertarget draws that can be re
placed with clears. |
118 * We are trying to test those. We could use saveLayer() to create small SkG
puDevices but | 119 * We are trying to test those. We could use saveLayer() to create small SkG
puDevices but |
119 * saveLayer() uses the texture cache. This means that the actual render tar
get may be larger | 120 * saveLayer() uses the texture cache. This means that the actual render tar
get may be larger |
120 * than the layer. Because the clip will contain the layer's bounds, no draw
s will be full-RT. | 121 * than the layer. Because the clip will contain the layer's bounds, no draw
s will be full-RT. |
121 * So when running on a GPU canvas we explicitly create a temporary canvas u
sing a texture with | 122 * So when running on a GPU canvas we explicitly create a temporary canvas u
sing a texture with |
122 * dimensions exactly matching the layer size. | 123 * dimensions exactly matching the layer size. |
123 */ | 124 */ |
124 SkCanvas* possiblyCreateTempCanvas(SkCanvas* baseCanvas, int w, int h) { | 125 SkSurface* possiblyCreateTempSurface(SkCanvas* baseCanvas, int w, int h) { |
125 SkCanvas* tempCanvas = nullptr; | |
126 #if SK_SUPPORT_GPU | 126 #if SK_SUPPORT_GPU |
127 GrContext* context = baseCanvas->getGrContext(); | 127 GrContext* context = baseCanvas->getGrContext(); |
128 SkImageInfo baseInfo = baseCanvas->imageInfo(); | 128 SkImageInfo baseInfo = baseCanvas->imageInfo(); |
129 SkImageInfo info = SkImageInfo::Make(w, h, baseInfo.colorType(), baseInf
o.alphaType(), | 129 SkImageInfo info = SkImageInfo::Make(w, h, baseInfo.colorType(), baseInf
o.alphaType(), |
130 baseInfo.profileType()); | 130 baseInfo.profileType()); |
131 SkAutoTUnref<SkSurface> surface(SkSurface::NewRenderTarget(context, SkSu
rface::kNo_Budgeted, | 131 return SkSurface::NewRenderTarget(context, SkSurface::kNo_Budgeted, info
, 0, nullptr); |
132 info, 0, nullptr)); | 132 #else |
133 if (surface) { | 133 return nullptr; |
134 tempCanvas = SkRef(surface->getCanvas()); | |
135 } | |
136 #endif | 134 #endif |
137 return tempCanvas; | |
138 } | 135 } |
139 | 136 |
140 void drawMode(SkCanvas* canvas, | 137 void drawMode(SkCanvas* canvas, |
141 int x, int y, int w, int h, | 138 int x, int y, int w, int h, |
142 const SkPaint& modePaint, SkCanvas* layerCanvas) { | 139 const SkPaint& modePaint, SkSurface* surface) { |
143 canvas->save(); | 140 canvas->save(); |
144 | |
145 canvas->translate(SkIntToScalar(x), SkIntToScalar(y)); | 141 canvas->translate(SkIntToScalar(x), SkIntToScalar(y)); |
146 | 142 |
147 SkRect r = SkRect::MakeWH(SkIntToScalar(w), SkIntToScalar(h)); | 143 SkRect r = SkRect::MakeWH(SkIntToScalar(w), SkIntToScalar(h)); |
148 | 144 |
149 SkCanvas* modeCanvas; | 145 SkCanvas* modeCanvas; |
150 if (nullptr == layerCanvas) { | 146 if (nullptr == surface) { |
151 canvas->saveLayer(&r, nullptr); | 147 canvas->saveLayer(&r, nullptr); |
152 modeCanvas = canvas; | 148 modeCanvas = canvas; |
153 } else { | 149 } else { |
154 modeCanvas = layerCanvas; | 150 modeCanvas = surface->getCanvas(); |
155 } | 151 } |
156 | 152 |
157 SkPaint bgPaint; | 153 SkPaint bgPaint; |
158 bgPaint.setAntiAlias(false); | 154 bgPaint.setAntiAlias(false); |
159 bgPaint.setShader(fBGShader); | 155 bgPaint.setShader(fBGShader); |
160 modeCanvas->drawRect(r, bgPaint); | 156 modeCanvas->drawRect(r, bgPaint); |
161 modeCanvas->drawRect(r, modePaint); | 157 modeCanvas->drawRect(r, modePaint); |
162 modeCanvas = nullptr; | 158 modeCanvas = nullptr; |
163 | 159 |
164 if (nullptr == layerCanvas) { | 160 if (nullptr == surface) { |
165 canvas->restore(); | 161 canvas->restore(); |
166 } else { | 162 } else { |
167 SkAutoROCanvasPixels ropixels(layerCanvas); | 163 surface->draw(canvas, 0, 0, nullptr); |
168 SkBitmap bitmap; | |
169 if (ropixels.asROBitmap(&bitmap)) { | |
170 canvas->drawBitmap(bitmap, 0, 0); | |
171 } | |
172 } | 164 } |
173 | 165 |
174 r.inset(-SK_ScalarHalf, -SK_ScalarHalf); | 166 r.inset(-SK_ScalarHalf, -SK_ScalarHalf); |
175 SkPaint borderPaint; | 167 SkPaint borderPaint; |
176 borderPaint.setStyle(SkPaint::kStroke_Style); | 168 borderPaint.setStyle(SkPaint::kStroke_Style); |
177 canvas->drawRect(r, borderPaint); | 169 canvas->drawRect(r, borderPaint); |
178 | 170 |
179 canvas->restore(); | 171 canvas->restore(); |
180 } | 172 } |
181 | 173 |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
233 SkAutoTUnref<SkShader> fBmpShader; | 225 SkAutoTUnref<SkShader> fBmpShader; |
234 | 226 |
235 typedef GM INHERITED; | 227 typedef GM INHERITED; |
236 }; | 228 }; |
237 | 229 |
238 ////////////////////////////////////////////////////////////////////////////// | 230 ////////////////////////////////////////////////////////////////////////////// |
239 | 231 |
240 DEF_GM(return new Xfermodes3GM;) | 232 DEF_GM(return new Xfermodes3GM;) |
241 | 233 |
242 } | 234 } |
OLD | NEW |