Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 | 1 |
| 2 /* | 2 /* |
| 3 * Copyright 2014 Google Inc. | 3 * Copyright 2014 Google Inc. |
| 4 * | 4 * |
| 5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
| 6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
| 7 */ | 7 */ |
| 8 | 8 |
| 9 #include "gm.h" | 9 #include "gm.h" |
| 10 | 10 |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 76 ConvexPolyClip() { | 76 ConvexPolyClip() { |
| 77 this->setBGColor(0xFFFFFFFF); | 77 this->setBGColor(0xFFFFFFFF); |
| 78 } | 78 } |
| 79 | 79 |
| 80 protected: | 80 protected: |
| 81 virtual SkString onShortName() SK_OVERRIDE { | 81 virtual SkString onShortName() SK_OVERRIDE { |
| 82 return SkString("convex_poly_clip"); | 82 return SkString("convex_poly_clip"); |
| 83 } | 83 } |
| 84 | 84 |
| 85 virtual SkISize onISize() SK_OVERRIDE { | 85 virtual SkISize onISize() SK_OVERRIDE { |
| 86 return make_isize(435, 440); | 86 return make_isize(435, 540); |
| 87 } | 87 } |
| 88 | 88 |
| 89 virtual void onOnceBeforeDraw() SK_OVERRIDE { | 89 virtual void onOnceBeforeDraw() SK_OVERRIDE { |
| 90 SkPath tri; | 90 SkPath tri; |
| 91 tri.moveTo(5.f, 5.f); | 91 tri.moveTo(5.f, 5.f); |
| 92 tri.lineTo(100.f, 20.f); | 92 tri.lineTo(100.f, 20.f); |
| 93 tri.lineTo(15.f, 100.f); | 93 tri.lineTo(15.f, 100.f); |
| 94 | 94 |
| 95 fPaths.addToTail(tri); | 95 fClips.addToTail()->setPath(tri); |
| 96 | 96 |
| 97 SkPath hexagon; | 97 SkPath hexagon; |
| 98 static const SkScalar kRadius = 45.f; | 98 static const SkScalar kRadius = 45.f; |
| 99 const SkPoint center = { kRadius, kRadius }; | 99 const SkPoint center = { kRadius, kRadius }; |
| 100 for (int i = 0; i < 6; ++i) { | 100 for (int i = 0; i < 6; ++i) { |
| 101 SkScalar angle = 2 * SK_ScalarPI * i / 6; | 101 SkScalar angle = 2 * SK_ScalarPI * i / 6; |
| 102 SkPoint point; | 102 SkPoint point; |
| 103 point.fY = SkScalarSinCos(angle, &point.fX); | 103 point.fY = SkScalarSinCos(angle, &point.fX); |
| 104 point.scale(kRadius); | 104 point.scale(kRadius); |
| 105 point = center + point; | 105 point = center + point; |
| 106 if (0 == i) { | 106 if (0 == i) { |
| 107 hexagon.moveTo(point); | 107 hexagon.moveTo(point); |
| 108 } else { | 108 } else { |
| 109 hexagon.lineTo(point); | 109 hexagon.lineTo(point); |
| 110 } | 110 } |
| 111 } | 111 } |
| 112 fPaths.addToTail(hexagon); | 112 fClips.addToTail()->setPath(hexagon); |
| 113 | 113 |
| 114 SkMatrix scaleM; | 114 SkMatrix scaleM; |
| 115 scaleM.setScale(1.1f, 0.4f, kRadius, kRadius); | 115 scaleM.setScale(1.1f, 0.4f, kRadius, kRadius); |
| 116 hexagon.transform(scaleM); | 116 hexagon.transform(scaleM); |
| 117 fPaths.addToTail(hexagon); | 117 fClips.addToTail()->setPath(hexagon); |
| 118 | |
| 119 fClips.addToTail()->setRect(SkRect::MakeXYWH(8.3f, 11.6f, 78.2f, 72.6f)) ; | |
| 118 | 120 |
| 119 SkPath rotRect; | 121 SkPath rotRect; |
| 120 SkRect rect = SkRect::MakeLTRB(10.f, 12.f, 80.f, 86.f); | 122 SkRect rect = SkRect::MakeLTRB(10.f, 12.f, 80.f, 86.f); |
| 121 rotRect.addRect(rect); | 123 rotRect.addRect(rect); |
| 122 SkMatrix rotM; | 124 SkMatrix rotM; |
| 123 rotM.setRotate(23.f, rect.centerX(), rect.centerY()); | 125 rotM.setRotate(23.f, rect.centerX(), rect.centerY()); |
| 124 rotRect.transform(rotM); | 126 rotRect.transform(rotM); |
| 125 fPaths.addToTail(rotRect); | 127 fClips.addToTail()->setPath(rotRect); |
| 126 | 128 |
| 127 fBmp = make_bmp(100, 100); | 129 fBmp = make_bmp(100, 100); |
| 128 } | 130 } |
| 129 | 131 |
| 130 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE { | 132 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE { |
| 131 const SkPath* path; | |
| 132 SkScalar y = 0; | 133 SkScalar y = 0; |
| 133 static const SkScalar kMargin = 10.f; | 134 static const SkScalar kMargin = 10.f; |
| 134 | 135 |
| 135 SkPaint bgPaint; | 136 SkPaint bgPaint; |
| 136 bgPaint.setAlpha(0x15); | 137 bgPaint.setAlpha(0x15); |
| 137 SkISize size = canvas->getDeviceSize(); | 138 SkISize size = canvas->getDeviceSize(); |
| 138 SkRect dstRect = SkRect::MakeWH(SkIntToScalar(size.fWidth), | 139 SkRect dstRect = SkRect::MakeWH(SkIntToScalar(size.fWidth), |
| 139 SkIntToScalar(size.fHeight)); | 140 SkIntToScalar(size.fHeight)); |
| 140 canvas->drawBitmapRectToRect(fBmp, NULL, dstRect, &bgPaint); | 141 canvas->drawBitmapRectToRect(fBmp, NULL, dstRect, &bgPaint); |
| 141 | 142 |
| 142 for (SkTLList<SkPath>::Iter iter(fPaths, SkTLList<SkPath>::Iter::kHead_I terStart); | 143 for (SkTLList<Clip>::Iter iter(fClips, SkTLList<Clip>::Iter::kHead_IterS tart); |
| 143 NULL != (path = iter.get()); | 144 NULL != iter.get(); |
| 144 iter.next()) { | 145 iter.next()) { |
| 146 const Clip* clip = iter.get(); | |
| 145 SkScalar x = 0; | 147 SkScalar x = 0; |
| 146 for (int aa = 0; aa < 2; ++aa) { | 148 for (int aa = 0; aa < 2; ++aa) { |
| 147 canvas->save(); | 149 canvas->save(); |
| 148 canvas->translate(x, y); | 150 canvas->translate(x, y); |
| 149 canvas->clipPath(*path, SkRegion::kIntersect_Op, SkToBool(aa)); | 151 clip->setOnCanvas(canvas, SkRegion::kIntersect_Op, SkToBool(aa)) ; |
| 150 canvas->drawBitmap(fBmp, 0, 0); | 152 canvas->drawBitmap(fBmp, 0, 0); |
| 151 canvas->restore(); | 153 canvas->restore(); |
| 152 x += fBmp.width() + kMargin; | 154 x += fBmp.width() + kMargin; |
| 153 } | 155 } |
| 154 for (int aa = 0; aa < 2; ++aa) { | 156 for (int aa = 0; aa < 2; ++aa) { |
| 155 static const char kTxt[] = "Clip Me!"; | 157 static const char kTxt[] = "Clip Me!"; |
| 156 SkPaint txtPaint; | 158 SkPaint txtPaint; |
| 157 txtPaint.setTextSize(23.f); | 159 txtPaint.setTextSize(23.f); |
| 158 txtPaint.setAntiAlias(true); | 160 txtPaint.setAntiAlias(true); |
| 159 txtPaint.setColor(SK_ColorDKGRAY); | 161 txtPaint.setColor(SK_ColorDKGRAY); |
| 160 | 162 |
| 161 SkPaint clipOutlinePaint; | 163 SkPaint clipOutlinePaint; |
| 162 clipOutlinePaint.setAntiAlias(true); | 164 clipOutlinePaint.setAntiAlias(true); |
| 163 clipOutlinePaint.setColor(0x50505050); | 165 clipOutlinePaint.setColor(0x50505050); |
| 164 clipOutlinePaint.setStyle(SkPaint::kStroke_Style); | 166 clipOutlinePaint.setStyle(SkPaint::kStroke_Style); |
| 165 clipOutlinePaint.setStrokeWidth(0); | 167 clipOutlinePaint.setStrokeWidth(0); |
| 166 | 168 |
| 167 canvas->save(); | 169 canvas->save(); |
| 168 canvas->translate(x, y); | 170 canvas->translate(x, y); |
| 169 SkPath closedClipPath = *path; | 171 SkPath closedClipPath; |
| 170 closedClipPath.close(); | 172 clip->asClosedPath(&closedClipPath); |
| 171 canvas->drawPath(closedClipPath, clipOutlinePaint); | 173 canvas->drawPath(closedClipPath, clipOutlinePaint); |
| 172 canvas->clipPath(*path, SkRegion::kIntersect_Op, SkToBool(aa)); | 174 clip->setOnCanvas(canvas, SkRegion::kIntersect_Op, SkToBool(aa)) ; |
| 173 canvas->scale(1.f, 1.8f); | 175 canvas->scale(1.f, 1.8f); |
| 174 canvas->drawText(kTxt, SK_ARRAY_COUNT(kTxt)-1, | 176 canvas->drawText(kTxt, SK_ARRAY_COUNT(kTxt)-1, |
| 175 0, 1.5f * txtPaint.getTextSize(), | 177 0, 1.5f * txtPaint.getTextSize(), |
| 176 txtPaint); | 178 txtPaint); |
| 177 canvas->restore(); | 179 canvas->restore(); |
| 178 x += fBmp.width() + kMargin; | 180 x += fBmp.width() + kMargin; |
| 179 } | 181 } |
| 180 | 182 |
| 181 y += fBmp.height() + kMargin; | 183 y += fBmp.height() + kMargin; |
| 182 } | 184 } |
| 183 } | 185 } |
| 184 | 186 |
| 185 virtual uint32_t onGetFlags() const { | 187 virtual uint32_t onGetFlags() const { |
| 186 return kAsBench_Flag; | 188 return kAsBench_Flag; |
| 187 } | 189 } |
| 188 | 190 |
| 189 private: | 191 private: |
| 190 SkTLList<SkPath> fPaths; | 192 class Clip { |
| 193 public: | |
| 194 enum ClipType { | |
| 195 kNone_ClipType, | |
| 196 kPath_ClipType, | |
| 197 kRect_ClipType | |
| 198 }; | |
| 199 | |
| 200 Clip () : fClipType(kNone_ClipType) {} | |
| 201 | |
| 202 void setOnCanvas(SkCanvas* canvas, SkRegion::Op op, bool aa) const { | |
| 203 switch (fClipType) { | |
| 204 case kPath_ClipType: | |
| 205 canvas->clipPath(fPath, op, aa); | |
| 206 break; | |
| 207 case kRect_ClipType: | |
| 208 canvas->clipRect(fRect, op, aa); | |
| 209 break; | |
| 210 case kNone_ClipType: | |
| 211 SkDEBUGFAIL("Uninitialized Clip."); | |
| 212 break; | |
| 213 } | |
| 214 } | |
| 215 | |
| 216 void asClosedPath(SkPath* path) const { | |
| 217 switch (fClipType) { | |
| 218 case kPath_ClipType: | |
| 219 *path = fPath; | |
| 220 path->close(); | |
| 221 break; | |
| 222 case kRect_ClipType: | |
| 223 path->reset(); | |
| 224 path->addRect(fRect); | |
| 225 break; | |
| 226 case kNone_ClipType: | |
| 227 SkDEBUGFAIL("Uninitialized Clip."); | |
| 228 break; | |
| 229 } | |
| 230 } | |
| 231 | |
| 232 void setPath(const SkPath& path) { | |
| 233 fClipType = kPath_ClipType; | |
| 234 fPath = path; | |
| 235 } | |
| 236 | |
| 237 void setRect(const SkRect& rect) { | |
| 238 fClipType = kRect_ClipType; | |
| 239 fRect = rect; | |
|
robertphillips
2014/02/03 22:32:45
if (kPath_ClipType == fClipType) {
fPath.reset
bsalomon
2014/02/04 14:24:34
The path object is always valid (no union).
| |
| 240 fPath.reset(); | |
| 241 } | |
| 242 | |
| 243 ClipType getType() const { return fClipType; } | |
| 244 | |
| 245 private: | |
| 246 ClipType fClipType; | |
|
robertphillips
2014/02/03 22:32:45
union?
bsalomon
2014/02/04 14:24:34
SkPath can't be in a union, it'd have to be SkPath
| |
| 247 SkPath fPath; | |
| 248 SkRect fRect; | |
| 249 }; | |
| 250 | |
| 251 SkTLList<Clip> fClips; | |
| 191 SkBitmap fBmp; | 252 SkBitmap fBmp; |
| 192 | 253 |
| 193 typedef GM INHERITED; | 254 typedef GM INHERITED; |
| 194 }; | 255 }; |
| 195 | 256 |
| 196 DEF_GM( return SkNEW(ConvexPolyClip); ) | 257 DEF_GM( return SkNEW(ConvexPolyClip); ) |
| 197 | 258 |
| 198 } | 259 } |
| OLD | NEW |