Flutter Engine
The Flutter Engine
SkCanvasStack.cpp
Go to the documentation of this file.
1/*
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
9
10#include "include/core/SkRect.h"
14#include <utility>
15
16class SkPath;
17class SkRRect;
18
21
23 this->removeAll();
24}
25
26void SkCanvasStack::pushCanvas(std::unique_ptr<SkCanvas> canvas, const SkIPoint& origin) {
27 if (canvas) {
28 // compute the bounds of this canvas
29 const SkIRect canvasBounds = SkIRect::MakeSize(canvas->getBaseLayerSize());
30
31 // push the canvas onto the stack
32 this->INHERITED::addCanvas(canvas.get());
33
34 // push the canvas data onto the stack
35 CanvasData* data = &fCanvasData.push_back();
36 data->origin = origin;
37 data->requiredClip.setRect(canvasBounds);
38 data->ownedCanvas = std::move(canvas);
39
40 // subtract this region from the canvas objects already on the stack.
41 // This ensures they do not draw into the space occupied by the layers
42 // above them.
43 for (int i = fList.size() - 1; i > 0; --i) {
44 SkIRect localBounds = canvasBounds;
45 localBounds.offset(origin - fCanvasData[i-1].origin);
46
47 fCanvasData[i-1].requiredClip.op(localBounds, SkRegion::kDifference_Op);
48 fList[i-1]->clipRegion(fCanvasData[i-1].requiredClip);
49 }
50 }
51 SkASSERT(fList.size() == fCanvasData.size());
52}
53
55 this->INHERITED::removeAll(); // call the baseclass *before* we actually delete the canvases
56 fCanvasData.clear();
57}
58
59/**
60 * Traverse all canvases (e.g. layers) the stack and ensure that they are clipped
61 * to their bounds and that the area covered by any canvas higher in the stack is
62 * also clipped out.
63 */
64void SkCanvasStack::clipToZOrderedBounds() {
65 SkASSERT(fList.size() == fCanvasData.size());
66 for (int i = 0; i < fList.size(); ++i) {
67 fList[i]->clipRegion(fCanvasData[i].requiredClip);
68 }
69}
70
71////////////////////////////////////////////////////////////////////////////////
72
73/**
74 * We need to handle setMatrix specially as it overwrites the matrix in each
75 * canvas unlike all other matrix operations (i.e. translate, scale, etc) which
76 * just pre-concatenate with the existing matrix.
77 */
79 SkASSERT(fList.size() == fCanvasData.size());
80 for (int i = 0; i < fList.size(); ++i) {
81 fList[i]->setMatrix(SkM44::Translate(SkIntToScalar(-fCanvasData[i].origin.x()),
82 SkIntToScalar(-fCanvasData[i].origin.y())) * mx);
83 }
84 this->SkCanvas::didSetM44(mx);
85}
86
88 this->INHERITED::onClipRect(r, op, edgeStyle);
89 this->clipToZOrderedBounds();
90}
91
93 this->INHERITED::onClipRRect(rr, op, edgeStyle);
94 this->clipToZOrderedBounds();
95}
96
98 this->INHERITED::onClipPath(p, op, edgeStyle);
99 this->clipToZOrderedBounds();
100}
101
103 this->INHERITED::onClipShader(std::move(cs), op);
104 // we don't change the "bounds" of the clip, so we don't need to update zorder
105}
106
108 SkASSERT(fList.size() == fCanvasData.size());
109 for (int i = 0; i < fList.size(); ++i) {
110 SkRegion tempRegion;
111 deviceRgn.translate(-fCanvasData[i].origin.x(),
112 -fCanvasData[i].origin.y(), &tempRegion);
113 tempRegion.op(fCanvasData[i].requiredClip, SkRegion::kIntersect_Op);
114 fList[i]->clipRegion(tempRegion, op);
115 }
116 this->SkCanvas::onClipRegion(deviceRgn, op);
117}
#define SkASSERT(cond)
Definition: SkAssert.h:116
SkClipOp
Definition: SkClipOp.h:13
#define SkIntToScalar(x)
Definition: SkScalar.h:57
~SkCanvasStack() override
void pushCanvas(std::unique_ptr< SkCanvas >, const SkIPoint &origin)
void didSetM44(const SkM44 &) override
void removeAll() override
void onClipRegion(const SkRegion &, SkClipOp) override
void onClipRRect(const SkRRect &, SkClipOp, ClipEdgeStyle) override
void onClipShader(sk_sp< SkShader >, SkClipOp) override
void onClipRect(const SkRect &, SkClipOp, ClipEdgeStyle) override
void onClipPath(const SkPath &, SkClipOp, ClipEdgeStyle) override
SkCanvasStack(int width, int height)
virtual void didSetM44(const SkM44 &)
Definition: SkCanvas.h:2280
virtual void onClipRegion(const SkRegion &deviceRgn, SkClipOp op)
Definition: SkCanvas.cpp:1515
Definition: SkM44.h:150
static SkM44 Translate(SkScalar x, SkScalar y, SkScalar z=0)
Definition: SkM44.h:225
virtual void addCanvas(SkCanvas *)
void onClipRect(const SkRect &, SkClipOp, ClipEdgeStyle) override
virtual void removeAll()
void onClipRRect(const SkRRect &, SkClipOp, ClipEdgeStyle) override
SkTDArray< SkCanvas * > fList
Definition: SkNWayCanvas.h:60
void onClipPath(const SkPath &, SkClipOp, ClipEdgeStyle) override
void onClipShader(sk_sp< SkShader >, SkClipOp) override
Definition: SkPath.h:59
void translate(int dx, int dy)
Definition: SkRegion.h:349
@ kIntersect_Op
target intersected with operand
Definition: SkRegion.h:368
@ kDifference_Op
target minus operand
Definition: SkRegion.h:367
bool op(const SkIRect &rect, Op op)
Definition: SkRegion.h:384
int size() const
Definition: SkTDArray.h:138
int size() const
Definition: SkTArray.h:421
int32_t height
int32_t width
Definition: SkRect.h:32
static constexpr SkIRect MakeSize(const SkISize &size)
Definition: SkRect.h:66
void offset(int32_t dx, int32_t dy)
Definition: SkRect.h:367
std::shared_ptr< const fml::Mapping > data
Definition: texture_gles.cc:63