| 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/gm.h" |
| 9 |
#include "include/core/SkCanvas.h" |
| 10 |
#include "include/core/SkClipOp.h" |
| 11 |
#include "include/core/SkColorSpace.h" |
| 12 |
#include "include/core/SkImageInfo.h" |
| 13 |
#include "include/core/SkMatrix.h" |
| 14 |
#include "include/core/SkPaint.h" |
| 15 |
#include "include/core/SkPoint.h" |
| 16 |
#include "include/core/SkRRect.h" |
| 17 |
#include "include/core/SkRect.h" |
| 18 |
#include "include/core/SkRefCnt.h" |
| 19 |
#include "include/core/SkRegion.h" |
| 20 |
#include "include/core/SkSize.h" |
| 21 |
#include "include/core/SkString.h" |
| 22 |
#include "include/core/SkTypes.h" |
| 23 |
#include "src/core/SkCanvasPriv.h" |
| 24 |
#include "src/core/SkClipStack.h" |
| 25 |
#include "src/core/SkColorData.h" |
| 26 |
#include "tools/ToolUtils.h" |
| 27 |
|
| 28 |
#include <utility> |
| 29 |
|
| 30 |
constexpr static SkIRect kDeviceRect = {0, 0, 600, 600}; |
| 31 |
constexpr static SkIRect kCoverRect = {50, 50, 550, 550}; |
| 32 |
|
| 33 |
namespace skiagm { |
| 34 |
|
| 35 |
//////////////////////////////////////////////////////////////////////////////////////////////////// |
| 36 |
|
| 37 |
/** |
| 38 |
* Draws a clip that will exercise window rectangles if they are supported. |
| 39 |
*/ |
| 40 |
class WindowRectanglesGM : public GM { |
| 41 |
private: |
| 42 |
DrawResult coverClipStack(const SkClipStack&, SkCanvas*, SkString* errorMsg); |
| 43 |
|
| 44 |
SkISize getISize() override { return SkISize::Make(kDeviceRect.width(), kDeviceRect.height()); } |
| 45 |
SkString getName() const override { return SkString("windowrectangles"); } |
| 46 |
DrawResult onDraw(SkCanvas*, SkString* errorMsg) override; |
| 47 |
}; |
| 48 |
|
| 49 |
DrawResult WindowRectanglesGM::coverClipStack(const SkClipStack& stack, SkCanvas* canvas, |
| 50 |
SkString* errorMsg) { |
| 51 |
SkPaint paint; |
| 52 |
paint.setColor(0xff00aa80); |
| 53 |
|
| 54 |
// Set up the canvas's clip to match our SkClipStack. |
| 55 |
SkClipStack::Iter iter(stack, SkClipStack::Iter::kBottom_IterStart); |
| 56 |
for (const SkClipStack::Element* element = iter.next(); element; element = iter.next()) { |
| 57 |
SkASSERT(!element->isReplaceOp()); |
| 58 |
SkClipOp op = element->getOp(); |
| 59 |
bool isAA = element->isAA(); |
| 60 |
switch (element->getDeviceSpaceType()) { |
| 61 |
case SkClipStack::Element::DeviceSpaceType::kShader: |
| 62 |
canvas->clipShader(element->refShader(), op); |
| 63 |
break; |
| 64 |
case SkClipStack::Element::DeviceSpaceType::kPath: |
| 65 |
canvas->clipPath(element->getDeviceSpacePath(), op, isAA); |
| 66 |
break; |
| 67 |
case SkClipStack::Element::DeviceSpaceType::kRRect: |
| 68 |
canvas->clipRRect(element->getDeviceSpaceRRect(), op, isAA); |
| 69 |
break; |
| 70 |
case SkClipStack::Element::DeviceSpaceType::kRect: |
| 71 |
canvas->clipRect(element->getDeviceSpaceRect(), op, isAA); |
| 72 |
break; |
| 73 |
case SkClipStack::Element::DeviceSpaceType::kEmpty: |
| 74 |
canvas->clipRect({ 0, 0, 0, 0 }, SkClipOp::kIntersect, false); |
| 75 |
break; |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
canvas->drawRect(SkRect::Make(kCoverRect), paint); |
| 80 |
return DrawResult::kOk; |
| 81 |
} |
| 82 |
|
| 83 |
DrawResult WindowRectanglesGM::onDraw(SkCanvas* canvas, SkString* errorMsg) { |
| 84 |
ToolUtils::draw_checkerboard(canvas, 0xffffffff, 0xffc6c3c6, 25); |
| 85 |
SkClipStack stack; |
| 86 |
stack.clipRect(SkRect::MakeXYWH(370.75, 80.25, 149, 100), SkMatrix::I(), |
| 87 |
SkClipOp::kDifference, false); |
| 88 |
stack.clipRect(SkRect::MakeXYWH(80.25, 420.75, 150, 100), SkMatrix::I(), |
| 89 |
SkClipOp::kDifference, true); |
| 90 |
stack.clipRRect(SkRRect::MakeRectXY(SkRect::MakeXYWH(200, 200, 200, 200), 60, 45), |
| 91 |
SkMatrix::I(), SkClipOp::kDifference, true); |
| 92 |
|
| 93 |
SkRRect nine; |
| 94 |
nine.setNinePatch(SkRect::MakeXYWH(550 - 30.25 - 100, 370.75, 100, 150), 12, 35, 23, 20); |
| 95 |
stack.clipRRect(nine, SkMatrix::I(), SkClipOp::kDifference, true); |
| 96 |
|
| 97 |
SkRRect complx; |
| 98 |
SkVector complxRadii[4] = {{6, 4}, {8, 12}, {16, 24}, {48, 32}}; |
| 99 |
complx.setRectRadii(SkRect::MakeXYWH(80.25, 80.75, 100, 149), complxRadii); |
| 100 |
stack.clipRRect(complx, SkMatrix::I(), SkClipOp::kDifference, false); |
| 101 |
|
| 102 |
return this->coverClipStack(stack, canvas, errorMsg); |
| 103 |
} |
| 104 |
|
| 105 |
DEF_GM( return new WindowRectanglesGM(); ) |
| 106 |
} // namespace skiagm |