blob: 26e5b6611c8132e1e74b686eaab033b636b159e1 [file] [log] [blame]
djsollen@google.com339e79f2013-09-04 17:16:00 +00001/*
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 */
Mike Reed121c2af2020-03-10 14:02:56 -04007
8#include "include/core/SkShader.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -05009#include "src/utils/SkCanvasStack.h"
djsollen@google.com339e79f2013-09-04 17:16:00 +000010
11SkCanvasStack::SkCanvasStack(int width, int height)
12 : INHERITED(width, height) {}
13
14SkCanvasStack::~SkCanvasStack() {
15 this->removeAll();
16}
17
Mike Reed584ca892016-11-15 11:52:55 -050018void SkCanvasStack::pushCanvas(std::unique_ptr<SkCanvas> canvas, const SkIPoint& origin) {
djsollen@google.com339e79f2013-09-04 17:16:00 +000019 if (canvas) {
20 // compute the bounds of this canvas
Mike Reed3661bc92017-02-22 13:21:42 -050021 const SkIRect canvasBounds = SkIRect::MakeSize(canvas->getBaseLayerSize());
djsollen@google.com339e79f2013-09-04 17:16:00 +000022
23 // push the canvas onto the stack
Mike Reed584ca892016-11-15 11:52:55 -050024 this->INHERITED::addCanvas(canvas.get());
djsollen@google.com339e79f2013-09-04 17:16:00 +000025
26 // push the canvas data onto the stack
27 CanvasData* data = &fCanvasData.push_back();
28 data->origin = origin;
29 data->requiredClip.setRect(canvasBounds);
Mike Reed584ca892016-11-15 11:52:55 -050030 data->ownedCanvas = std::move(canvas);
djsollen@google.com339e79f2013-09-04 17:16:00 +000031
32 // subtract this region from the canvas objects already on the stack.
33 // This ensures they do not draw into the space occupied by the layers
34 // above them.
35 for (int i = fList.count() - 1; i > 0; --i) {
36 SkIRect localBounds = canvasBounds;
37 localBounds.offset(origin - fCanvasData[i-1].origin);
38
39 fCanvasData[i-1].requiredClip.op(localBounds, SkRegion::kDifference_Op);
bungeman@google.coma089f032013-12-02 21:34:36 +000040 fList[i-1]->clipRegion(fCanvasData[i-1].requiredClip);
djsollen@google.com339e79f2013-09-04 17:16:00 +000041 }
42 }
43 SkASSERT(fList.count() == fCanvasData.count());
44}
45
46void SkCanvasStack::removeAll() {
Mike Reed584ca892016-11-15 11:52:55 -050047 this->INHERITED::removeAll(); // call the baseclass *before* we actually delete the canvases
djsollen@google.com339e79f2013-09-04 17:16:00 +000048 fCanvasData.reset();
djsollen@google.com339e79f2013-09-04 17:16:00 +000049}
50
51/**
52 * Traverse all canvases (e.g. layers) the stack and ensure that they are clipped
53 * to their bounds and that the area covered by any canvas higher in the stack is
54 * also clipped out.
55 */
56void SkCanvasStack::clipToZOrderedBounds() {
57 SkASSERT(fList.count() == fCanvasData.count());
58 for (int i = 0; i < fList.count(); ++i) {
reed73603f32016-09-20 08:42:38 -070059 fList[i]->clipRegion(fCanvasData[i].requiredClip);
djsollen@google.com339e79f2013-09-04 17:16:00 +000060 }
61}
62
63////////////////////////////////////////////////////////////////////////////////
64
65/**
66 * We need to handle setMatrix specially as it overwrites the matrix in each
67 * canvas unlike all other matrix operations (i.e. translate, scale, etc) which
68 * just pre-concatenate with the existing matrix.
69 */
commit-bot@chromium.org44c48d02014-03-13 20:03:58 +000070void SkCanvasStack::didSetMatrix(const SkMatrix& matrix) {
djsollen@google.com339e79f2013-09-04 17:16:00 +000071 SkASSERT(fList.count() == fCanvasData.count());
72 for (int i = 0; i < fList.count(); ++i) {
73
74 SkMatrix tempMatrix = matrix;
75 tempMatrix.postTranslate(SkIntToScalar(-fCanvasData[i].origin.x()),
76 SkIntToScalar(-fCanvasData[i].origin.y()));
77 fList[i]->setMatrix(tempMatrix);
78 }
commit-bot@chromium.org44c48d02014-03-13 20:03:58 +000079 this->SkCanvas::didSetMatrix(matrix);
djsollen@google.com339e79f2013-09-04 17:16:00 +000080}
81
Mike Reedc1f77742016-12-09 09:00:50 -050082void SkCanvasStack::onClipRect(const SkRect& r, SkClipOp op, ClipEdgeStyle edgeStyle) {
robertphillips@google.com8f90a892014-02-28 18:19:39 +000083 this->INHERITED::onClipRect(r, op, edgeStyle);
djsollen@google.com339e79f2013-09-04 17:16:00 +000084 this->clipToZOrderedBounds();
djsollen@google.com339e79f2013-09-04 17:16:00 +000085}
86
Mike Reedc1f77742016-12-09 09:00:50 -050087void SkCanvasStack::onClipRRect(const SkRRect& rr, SkClipOp op, ClipEdgeStyle edgeStyle) {
robertphillips@google.com8f90a892014-02-28 18:19:39 +000088 this->INHERITED::onClipRRect(rr, op, edgeStyle);
djsollen@google.com339e79f2013-09-04 17:16:00 +000089 this->clipToZOrderedBounds();
djsollen@google.com339e79f2013-09-04 17:16:00 +000090}
91
Mike Reedc1f77742016-12-09 09:00:50 -050092void SkCanvasStack::onClipPath(const SkPath& p, SkClipOp op, ClipEdgeStyle edgeStyle) {
robertphillips@google.com8f90a892014-02-28 18:19:39 +000093 this->INHERITED::onClipPath(p, op, edgeStyle);
djsollen@google.com339e79f2013-09-04 17:16:00 +000094 this->clipToZOrderedBounds();
djsollen@google.com339e79f2013-09-04 17:16:00 +000095}
96
Mike Reed121c2af2020-03-10 14:02:56 -040097void SkCanvasStack::onClipShader(sk_sp<SkShader> cs, SkClipOp op) {
98 this->INHERITED::onClipShader(std::move(cs), op);
99 // we don't change the "bounds" of the clip, so we don't need to update zorder
100}
101
Mike Reedc1f77742016-12-09 09:00:50 -0500102void SkCanvasStack::onClipRegion(const SkRegion& deviceRgn, SkClipOp op) {
djsollen@google.com339e79f2013-09-04 17:16:00 +0000103 SkASSERT(fList.count() == fCanvasData.count());
104 for (int i = 0; i < fList.count(); ++i) {
105 SkRegion tempRegion;
106 deviceRgn.translate(-fCanvasData[i].origin.x(),
107 -fCanvasData[i].origin.y(), &tempRegion);
108 tempRegion.op(fCanvasData[i].requiredClip, SkRegion::kIntersect_Op);
109 fList[i]->clipRegion(tempRegion, op);
110 }
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000111 this->SkCanvas::onClipRegion(deviceRgn, op);
djsollen@google.com339e79f2013-09-04 17:16:00 +0000112}