gfx/skia/trunk/src/utils/SkCanvasStack.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/gfx/skia/trunk/src/utils/SkCanvasStack.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,105 @@
     1.4 +
     1.5 +/*
     1.6 + * Copyright 2013 Google Inc.
     1.7 + *
     1.8 + * Use of this source code is governed by a BSD-style license that can be
     1.9 + * found in the LICENSE file.
    1.10 + */
    1.11 +#include "SkCanvasStack.h"
    1.12 +
    1.13 +SkCanvasStack::SkCanvasStack(int width, int height)
    1.14 +        : INHERITED(width, height) {}
    1.15 +
    1.16 +SkCanvasStack::~SkCanvasStack() {
    1.17 +    this->removeAll();
    1.18 +}
    1.19 +
    1.20 +void SkCanvasStack::pushCanvas(SkCanvas* canvas, const SkIPoint& origin) {
    1.21 +    if (canvas) {
    1.22 +        // compute the bounds of this canvas
    1.23 +        const SkIRect canvasBounds = SkIRect::MakeSize(canvas->getDeviceSize());
    1.24 +
    1.25 +        // push the canvas onto the stack
    1.26 +        this->INHERITED::addCanvas(canvas);
    1.27 +
    1.28 +        // push the canvas data onto the stack
    1.29 +        CanvasData* data = &fCanvasData.push_back();
    1.30 +        data->origin = origin;
    1.31 +        data->requiredClip.setRect(canvasBounds);
    1.32 +
    1.33 +        // subtract this region from the canvas objects already on the stack.
    1.34 +        // This ensures they do not draw into the space occupied by the layers
    1.35 +        // above them.
    1.36 +        for (int i = fList.count() - 1; i > 0; --i) {
    1.37 +            SkIRect localBounds = canvasBounds;
    1.38 +            localBounds.offset(origin - fCanvasData[i-1].origin);
    1.39 +
    1.40 +            fCanvasData[i-1].requiredClip.op(localBounds, SkRegion::kDifference_Op);
    1.41 +            fList[i-1]->clipRegion(fCanvasData[i-1].requiredClip);
    1.42 +        }
    1.43 +    }
    1.44 +    SkASSERT(fList.count() == fCanvasData.count());
    1.45 +}
    1.46 +
    1.47 +void SkCanvasStack::removeAll() {
    1.48 +    fCanvasData.reset();
    1.49 +    this->INHERITED::removeAll();
    1.50 +}
    1.51 +
    1.52 +/**
    1.53 + * Traverse all canvases (e.g. layers) the stack and ensure that they are clipped
    1.54 + * to their bounds and that the area covered by any canvas higher in the stack is
    1.55 + * also clipped out.
    1.56 + */
    1.57 +void SkCanvasStack::clipToZOrderedBounds() {
    1.58 +    SkASSERT(fList.count() == fCanvasData.count());
    1.59 +    for (int i = 0; i < fList.count(); ++i) {
    1.60 +        fList[i]->clipRegion(fCanvasData[i].requiredClip, SkRegion::kIntersect_Op);
    1.61 +    }
    1.62 +}
    1.63 +
    1.64 +////////////////////////////////////////////////////////////////////////////////
    1.65 +
    1.66 +/**
    1.67 + * We need to handle setMatrix specially as it overwrites the matrix in each
    1.68 + * canvas unlike all other matrix operations (i.e. translate, scale, etc) which
    1.69 + * just pre-concatenate with the existing matrix.
    1.70 + */
    1.71 +void SkCanvasStack::didSetMatrix(const SkMatrix& matrix) {
    1.72 +    SkASSERT(fList.count() == fCanvasData.count());
    1.73 +    for (int i = 0; i < fList.count(); ++i) {
    1.74 +
    1.75 +        SkMatrix tempMatrix = matrix;
    1.76 +        tempMatrix.postTranslate(SkIntToScalar(-fCanvasData[i].origin.x()),
    1.77 +                                 SkIntToScalar(-fCanvasData[i].origin.y()));
    1.78 +        fList[i]->setMatrix(tempMatrix);
    1.79 +    }
    1.80 +    this->SkCanvas::didSetMatrix(matrix);
    1.81 +}
    1.82 +
    1.83 +void SkCanvasStack::onClipRect(const SkRect& r, SkRegion::Op op, ClipEdgeStyle edgeStyle) {
    1.84 +    this->INHERITED::onClipRect(r, op, edgeStyle);
    1.85 +    this->clipToZOrderedBounds();
    1.86 +}
    1.87 +
    1.88 +void SkCanvasStack::onClipRRect(const SkRRect& rr, SkRegion::Op op, ClipEdgeStyle edgeStyle) {
    1.89 +    this->INHERITED::onClipRRect(rr, op, edgeStyle);
    1.90 +    this->clipToZOrderedBounds();
    1.91 +}
    1.92 +
    1.93 +void SkCanvasStack::onClipPath(const SkPath& p, SkRegion::Op op, ClipEdgeStyle edgeStyle) {
    1.94 +    this->INHERITED::onClipPath(p, op, edgeStyle);
    1.95 +    this->clipToZOrderedBounds();
    1.96 +}
    1.97 +
    1.98 +void SkCanvasStack::onClipRegion(const SkRegion& deviceRgn, SkRegion::Op op) {
    1.99 +    SkASSERT(fList.count() == fCanvasData.count());
   1.100 +    for (int i = 0; i < fList.count(); ++i) {
   1.101 +        SkRegion tempRegion;
   1.102 +        deviceRgn.translate(-fCanvasData[i].origin.x(),
   1.103 +                            -fCanvasData[i].origin.y(), &tempRegion);
   1.104 +        tempRegion.op(fCanvasData[i].requiredClip, SkRegion::kIntersect_Op);
   1.105 +        fList[i]->clipRegion(tempRegion, op);
   1.106 +    }
   1.107 +    this->SkCanvas::onClipRegion(deviceRgn, op);
   1.108 +}

mercurial