michael@0: michael@0: /* michael@0: * Copyright 2013 Google Inc. michael@0: * michael@0: * Use of this source code is governed by a BSD-style license that can be michael@0: * found in the LICENSE file. michael@0: */ michael@0: #include "SkCanvasStack.h" michael@0: michael@0: SkCanvasStack::SkCanvasStack(int width, int height) michael@0: : INHERITED(width, height) {} michael@0: michael@0: SkCanvasStack::~SkCanvasStack() { michael@0: this->removeAll(); michael@0: } michael@0: michael@0: void SkCanvasStack::pushCanvas(SkCanvas* canvas, const SkIPoint& origin) { michael@0: if (canvas) { michael@0: // compute the bounds of this canvas michael@0: const SkIRect canvasBounds = SkIRect::MakeSize(canvas->getDeviceSize()); michael@0: michael@0: // push the canvas onto the stack michael@0: this->INHERITED::addCanvas(canvas); michael@0: michael@0: // push the canvas data onto the stack michael@0: CanvasData* data = &fCanvasData.push_back(); michael@0: data->origin = origin; michael@0: data->requiredClip.setRect(canvasBounds); michael@0: michael@0: // subtract this region from the canvas objects already on the stack. michael@0: // This ensures they do not draw into the space occupied by the layers michael@0: // above them. michael@0: for (int i = fList.count() - 1; i > 0; --i) { michael@0: SkIRect localBounds = canvasBounds; michael@0: localBounds.offset(origin - fCanvasData[i-1].origin); michael@0: michael@0: fCanvasData[i-1].requiredClip.op(localBounds, SkRegion::kDifference_Op); michael@0: fList[i-1]->clipRegion(fCanvasData[i-1].requiredClip); michael@0: } michael@0: } michael@0: SkASSERT(fList.count() == fCanvasData.count()); michael@0: } michael@0: michael@0: void SkCanvasStack::removeAll() { michael@0: fCanvasData.reset(); michael@0: this->INHERITED::removeAll(); michael@0: } michael@0: michael@0: /** michael@0: * Traverse all canvases (e.g. layers) the stack and ensure that they are clipped michael@0: * to their bounds and that the area covered by any canvas higher in the stack is michael@0: * also clipped out. michael@0: */ michael@0: void SkCanvasStack::clipToZOrderedBounds() { michael@0: SkASSERT(fList.count() == fCanvasData.count()); michael@0: for (int i = 0; i < fList.count(); ++i) { michael@0: fList[i]->clipRegion(fCanvasData[i].requiredClip, SkRegion::kIntersect_Op); michael@0: } michael@0: } michael@0: michael@0: //////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: /** michael@0: * We need to handle setMatrix specially as it overwrites the matrix in each michael@0: * canvas unlike all other matrix operations (i.e. translate, scale, etc) which michael@0: * just pre-concatenate with the existing matrix. michael@0: */ michael@0: void SkCanvasStack::didSetMatrix(const SkMatrix& matrix) { michael@0: SkASSERT(fList.count() == fCanvasData.count()); michael@0: for (int i = 0; i < fList.count(); ++i) { michael@0: michael@0: SkMatrix tempMatrix = matrix; michael@0: tempMatrix.postTranslate(SkIntToScalar(-fCanvasData[i].origin.x()), michael@0: SkIntToScalar(-fCanvasData[i].origin.y())); michael@0: fList[i]->setMatrix(tempMatrix); michael@0: } michael@0: this->SkCanvas::didSetMatrix(matrix); michael@0: } michael@0: michael@0: void SkCanvasStack::onClipRect(const SkRect& r, SkRegion::Op op, ClipEdgeStyle edgeStyle) { michael@0: this->INHERITED::onClipRect(r, op, edgeStyle); michael@0: this->clipToZOrderedBounds(); michael@0: } michael@0: michael@0: void SkCanvasStack::onClipRRect(const SkRRect& rr, SkRegion::Op op, ClipEdgeStyle edgeStyle) { michael@0: this->INHERITED::onClipRRect(rr, op, edgeStyle); michael@0: this->clipToZOrderedBounds(); michael@0: } michael@0: michael@0: void SkCanvasStack::onClipPath(const SkPath& p, SkRegion::Op op, ClipEdgeStyle edgeStyle) { michael@0: this->INHERITED::onClipPath(p, op, edgeStyle); michael@0: this->clipToZOrderedBounds(); michael@0: } michael@0: michael@0: void SkCanvasStack::onClipRegion(const SkRegion& deviceRgn, SkRegion::Op op) { michael@0: SkASSERT(fList.count() == fCanvasData.count()); michael@0: for (int i = 0; i < fList.count(); ++i) { michael@0: SkRegion tempRegion; michael@0: deviceRgn.translate(-fCanvasData[i].origin.x(), michael@0: -fCanvasData[i].origin.y(), &tempRegion); michael@0: tempRegion.op(fCanvasData[i].requiredClip, SkRegion::kIntersect_Op); michael@0: fList[i]->clipRegion(tempRegion, op); michael@0: } michael@0: this->SkCanvas::onClipRegion(deviceRgn, op); michael@0: }