blob: 38cd21013e3749292653f6fde2fabff7095b2281 [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 */
7#include "SkCanvasStack.h"
8
9SkCanvasStack::SkCanvasStack(int width, int height)
10 : INHERITED(width, height) {}
11
12SkCanvasStack::~SkCanvasStack() {
13 this->removeAll();
14}
15
Mike Reed584ca892016-11-15 11:52:55 -050016void SkCanvasStack::pushCanvas(std::unique_ptr<SkCanvas> canvas, const SkIPoint& origin) {
djsollen@google.com339e79f2013-09-04 17:16:00 +000017 if (canvas) {
18 // compute the bounds of this canvas
Mike Reed3661bc92017-02-22 13:21:42 -050019 const SkIRect canvasBounds = SkIRect::MakeSize(canvas->getBaseLayerSize());
djsollen@google.com339e79f2013-09-04 17:16:00 +000020
21 // push the canvas onto the stack
Mike Reed584ca892016-11-15 11:52:55 -050022 this->INHERITED::addCanvas(canvas.get());
djsollen@google.com339e79f2013-09-04 17:16:00 +000023
24 // push the canvas data onto the stack
25 CanvasData* data = &fCanvasData.push_back();
26 data->origin = origin;
27 data->requiredClip.setRect(canvasBounds);
Mike Reed584ca892016-11-15 11:52:55 -050028 data->ownedCanvas = std::move(canvas);
djsollen@google.com339e79f2013-09-04 17:16:00 +000029
30 // subtract this region from the canvas objects already on the stack.
31 // This ensures they do not draw into the space occupied by the layers
32 // above them.
33 for (int i = fList.count() - 1; i > 0; --i) {
34 SkIRect localBounds = canvasBounds;
35 localBounds.offset(origin - fCanvasData[i-1].origin);
36
37 fCanvasData[i-1].requiredClip.op(localBounds, SkRegion::kDifference_Op);
bungeman@google.coma089f032013-12-02 21:34:36 +000038 fList[i-1]->clipRegion(fCanvasData[i-1].requiredClip);
djsollen@google.com339e79f2013-09-04 17:16:00 +000039 }
40 }
41 SkASSERT(fList.count() == fCanvasData.count());
42}
43
44void SkCanvasStack::removeAll() {
Mike Reed584ca892016-11-15 11:52:55 -050045 this->INHERITED::removeAll(); // call the baseclass *before* we actually delete the canvases
djsollen@google.com339e79f2013-09-04 17:16:00 +000046 fCanvasData.reset();
djsollen@google.com339e79f2013-09-04 17:16:00 +000047}
48
49/**
50 * Traverse all canvases (e.g. layers) the stack and ensure that they are clipped
51 * to their bounds and that the area covered by any canvas higher in the stack is
52 * also clipped out.
53 */
54void SkCanvasStack::clipToZOrderedBounds() {
55 SkASSERT(fList.count() == fCanvasData.count());
56 for (int i = 0; i < fList.count(); ++i) {
reed73603f32016-09-20 08:42:38 -070057 fList[i]->clipRegion(fCanvasData[i].requiredClip);
djsollen@google.com339e79f2013-09-04 17:16:00 +000058 }
59}
60
61////////////////////////////////////////////////////////////////////////////////
62
63/**
64 * We need to handle setMatrix specially as it overwrites the matrix in each
65 * canvas unlike all other matrix operations (i.e. translate, scale, etc) which
66 * just pre-concatenate with the existing matrix.
67 */
commit-bot@chromium.org44c48d02014-03-13 20:03:58 +000068void SkCanvasStack::didSetMatrix(const SkMatrix& matrix) {
djsollen@google.com339e79f2013-09-04 17:16:00 +000069 SkASSERT(fList.count() == fCanvasData.count());
70 for (int i = 0; i < fList.count(); ++i) {
71
72 SkMatrix tempMatrix = matrix;
73 tempMatrix.postTranslate(SkIntToScalar(-fCanvasData[i].origin.x()),
74 SkIntToScalar(-fCanvasData[i].origin.y()));
75 fList[i]->setMatrix(tempMatrix);
76 }
commit-bot@chromium.org44c48d02014-03-13 20:03:58 +000077 this->SkCanvas::didSetMatrix(matrix);
djsollen@google.com339e79f2013-09-04 17:16:00 +000078}
79
Mike Reedc1f77742016-12-09 09:00:50 -050080void SkCanvasStack::onClipRect(const SkRect& r, SkClipOp op, ClipEdgeStyle edgeStyle) {
robertphillips@google.com8f90a892014-02-28 18:19:39 +000081 this->INHERITED::onClipRect(r, op, edgeStyle);
djsollen@google.com339e79f2013-09-04 17:16:00 +000082 this->clipToZOrderedBounds();
djsollen@google.com339e79f2013-09-04 17:16:00 +000083}
84
Mike Reedc1f77742016-12-09 09:00:50 -050085void SkCanvasStack::onClipRRect(const SkRRect& rr, SkClipOp op, ClipEdgeStyle edgeStyle) {
robertphillips@google.com8f90a892014-02-28 18:19:39 +000086 this->INHERITED::onClipRRect(rr, op, edgeStyle);
djsollen@google.com339e79f2013-09-04 17:16:00 +000087 this->clipToZOrderedBounds();
djsollen@google.com339e79f2013-09-04 17:16:00 +000088}
89
Mike Reedc1f77742016-12-09 09:00:50 -050090void SkCanvasStack::onClipPath(const SkPath& p, SkClipOp op, ClipEdgeStyle edgeStyle) {
robertphillips@google.com8f90a892014-02-28 18:19:39 +000091 this->INHERITED::onClipPath(p, op, edgeStyle);
djsollen@google.com339e79f2013-09-04 17:16:00 +000092 this->clipToZOrderedBounds();
djsollen@google.com339e79f2013-09-04 17:16:00 +000093}
94
Mike Reedc1f77742016-12-09 09:00:50 -050095void SkCanvasStack::onClipRegion(const SkRegion& deviceRgn, SkClipOp op) {
djsollen@google.com339e79f2013-09-04 17:16:00 +000096 SkASSERT(fList.count() == fCanvasData.count());
97 for (int i = 0; i < fList.count(); ++i) {
98 SkRegion tempRegion;
99 deviceRgn.translate(-fCanvasData[i].origin.x(),
100 -fCanvasData[i].origin.y(), &tempRegion);
101 tempRegion.op(fCanvasData[i].requiredClip, SkRegion::kIntersect_Op);
102 fList[i]->clipRegion(tempRegion, op);
103 }
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000104 this->SkCanvas::onClipRegion(deviceRgn, op);
djsollen@google.com339e79f2013-09-04 17:16:00 +0000105}