Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 /* | |
| 2 * Copyright 2016 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 "SkPath.h" | |
| 10 #include "SkRandom.h" | |
| 11 #include "SkRRect.h" | |
| 12 #include "SkSurface.h" | |
| 13 | |
| 14 namespace skiagm { | |
| 15 | |
| 16 constexpr SkRect kSrcImageClip{75, 75, 275, 275}; | |
| 17 | |
| 18 /* | |
| 19 * The purpose of this test is to exercise all three codepaths in GrDrawContext (drawFilledRect, | |
| 20 * fillRectToRect, fillRectWithLocalMatrix) that pre-crop filled rects based on the clip. | |
| 21 * | |
| 22 * The test creates an image of a green square surrounded by red background, the n draws this image | |
| 23 * in various ways with the red clipped out. The test is successful if there is no visible red | |
| 24 * background, scissor is never used, and ideally, all the rectangles draw in on e batch. | |
| 25 */ | |
| 26 class CroppedRectsGM : public GM { | |
| 27 private: | |
| 28 SkString onShortName() override final { return SkString("croppedrects"); } | |
| 29 SkISize onISize() override { return SkISize::Make(500, 500); } | |
| 30 | |
| 31 void onOnceBeforeDraw() override { | |
| 32 sk_sp<SkSurface> srcSurface = SkSurface::MakeRasterN32Premul(500, 500); | |
| 33 SkCanvas* srcCanvas = srcSurface->getCanvas(); | |
| 34 | |
| 35 srcCanvas->clear(SK_ColorRED); | |
| 36 | |
| 37 SkPaint paint; | |
| 38 paint.setColor(0xff00ff00); | |
| 39 srcCanvas->drawRect(kSrcImageClip, paint); | |
| 40 | |
| 41 constexpr SkScalar kStrokeWidth = 10; | |
| 42 SkPaint stroke; | |
| 43 stroke.setStyle(SkPaint::kStroke_Style); | |
| 44 stroke.setStrokeWidth(kStrokeWidth); | |
| 45 stroke.setColor(0xff008800); | |
| 46 srcCanvas->drawRect(kSrcImageClip.makeInset(kStrokeWidth / 2, kStrokeWid th / 2), stroke); | |
| 47 | |
| 48 fSrcImage = srcSurface->makeImageSnapshot(SkBudgeted::kYes, SkSurface::k No_ForceUnique); | |
| 49 fSrcImageShader = fSrcImage->makeShader(SkShader::kClamp_TileMode, | |
| 50 SkShader::kClamp_TileMode); | |
| 51 } | |
| 52 | |
| 53 void onDraw(SkCanvas* canvas) override { | |
| 54 canvas->clear(SK_ColorWHITE); | |
| 55 | |
| 56 { | |
| 57 // GrDrawContext::drawFilledRect. | |
| 58 SkAutoCanvasRestore acr(canvas, true); | |
| 59 SkPaint paint; | |
| 60 paint.setShader(fSrcImageShader); | |
| 61 canvas->clipRect(kSrcImageClip); | |
| 62 canvas->drawPaint(paint); | |
| 63 } | |
| 64 | |
| 65 { | |
| 66 // GrDrawContext::fillRectToRect. | |
| 67 SkAutoCanvasRestore acr(canvas, true); | |
| 68 SkRect drawRect = SkRect::MakeXYWH(350, 100, 100, 300); | |
| 69 canvas->clipRect(drawRect); | |
| 70 canvas->drawImageRect(fSrcImage.get(), | |
| 71 kSrcImageClip.makeOutset(0.5f * kSrcImageClip. width(), | |
| 72 kSrcImageClip.height( )), | |
| 73 drawRect.makeOutset(0.5f * drawRect.width(), d rawRect.height()), | |
| 74 nullptr); | |
| 75 } | |
| 76 | |
| 77 { | |
| 78 // GrDrawContext::fillRectWithLocalMatrix. | |
| 79 SkAutoCanvasRestore acr(canvas, true); | |
| 80 SkPath path; | |
| 81 path.moveTo(kSrcImageClip.fLeft - kSrcImageClip.width(), kSrcImageCl ip.centerY()); | |
| 82 path.lineTo(kSrcImageClip.fRight + 3 * kSrcImageClip.width(), kSrcIm ageClip.centerY()); | |
| 83 SkPaint paint; | |
| 84 paint.setStyle(SkPaint::kStroke_Style); | |
| 85 paint.setStrokeWidth(2 * kSrcImageClip.height()); | |
| 86 paint.setShader(fSrcImageShader); | |
| 87 canvas->translate(-90, 263); | |
| 88 canvas->scale(300 / kSrcImageClip.width(), 100 / kSrcImageClip.heigh t()); | |
| 89 canvas->clipRect(kSrcImageClip); | |
| 90 canvas->drawPath(path, paint); | |
| 91 } | |
| 92 | |
| 93 // TODO: assert the draw target only has one batch? | |
|
csmartdalton
2016/07/11 03:17:56
Doable?
bsalomon
2016/07/11 13:52:23
We could add something to draw context to get the
csmartdalton
2016/07/11 16:37:10
Done.
| |
| 94 } | |
| 95 | |
| 96 sk_sp<SkImage> fSrcImage; | |
| 97 sk_sp<SkShader> fSrcImageShader; | |
| 98 | |
| 99 typedef GM INHERITED; | |
| 100 }; | |
| 101 | |
| 102 DEF_GM( return new CroppedRectsGM(); ) | |
| 103 | |
| 104 } | |
| OLD | NEW |