Skip to content

Commit d62d406

Browse files
reed-at-googleSkia Commit-Bot
authored andcommitted
Revert "Revert "try resolver pattern""
This reverts commit 2bafb64. Change-Id: I46f29284546a8978fd0005a0937e28410e5ac0da Reviewed-on: https://skia-review.googlesource.com/c/skia/+/220518 Commit-Queue: Mike Reed <reed@google.com> Reviewed-by: Florin Malita <fmalita@chromium.org>
1 parent 698fa78 commit d62d406

File tree

7 files changed

+656
-2
lines changed

7 files changed

+656
-2
lines changed

BUILD.gn

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ declare_args() {
2121
skia_use_angle = false
2222
skia_use_egl = false
2323
skia_use_expat = true
24+
skia_use_experimental_xform = false
2425
skia_use_ffmpeg = false
2526
skia_use_fontconfig = is_linux
2627
skia_use_fonthost_mac = is_mac
@@ -1724,8 +1725,8 @@ if (skia_enable_tools) {
17241725
":skia",
17251726
":skvm_builders",
17261727
":tool_utils",
1727-
"modules/skshaper",
17281728
"modules/skparagraph:bench",
1729+
"modules/skshaper",
17291730
]
17301731
}
17311732

@@ -1763,6 +1764,17 @@ if (skia_enable_tools) {
17631764
}
17641765
}
17651766

1767+
test_lib("experimental_xform") {
1768+
sources = [
1769+
"experimental/xform/SkShape.cpp",
1770+
"experimental/xform/SkXform.cpp",
1771+
"experimental/xform/XContext.cpp",
1772+
]
1773+
deps = [
1774+
":skia",
1775+
]
1776+
}
1777+
17661778
if (skia_use_lua) {
17671779
test_lib("lua") {
17681780
sources = [
@@ -1937,8 +1949,8 @@ if (skia_enable_tools) {
19371949
":skia",
19381950
":tool_utils",
19391951
":trace",
1940-
"modules/sksg",
19411952
"modules/skparagraph:bench",
1953+
"modules/sksg",
19421954
"modules/skshaper",
19431955
]
19441956
}
@@ -2394,6 +2406,10 @@ if (skia_enable_tools) {
23942406
"modules/sksg:samples",
23952407
"//third_party/imgui",
23962408
]
2409+
if (skia_use_experimental_xform) {
2410+
deps += [ ":experimental_xform" ]
2411+
sources += [ "gm/xform.cpp" ]
2412+
}
23972413
}
23982414

23992415
if (!skia_use_angle && (is_linux || is_win || is_mac)) {

experimental/xform/SkShape.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright 2019 Google LLC.
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+
8+
#include "experimental/xform/SkShape.h"
9+
#include "experimental/xform/SkXform.h"
10+
#include "include/core/SkCanvas.h"
11+
12+
void GeoShape::draw(XContext* ctx) {
13+
ctx->drawRect(fRect, fPaint, this->xform());
14+
}
15+
16+
void GroupShape::draw(XContext* ctx) {
17+
if (fArray.count() == 0) {
18+
return;
19+
}
20+
21+
ctx->push(this->xform());
22+
for (auto s : fArray) {
23+
s->draw(ctx);
24+
}
25+
ctx->pop();
26+
}

experimental/xform/SkShape.h

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/*
2+
* Copyright 2019 Google LLC.
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+
8+
#ifndef SkShape_DEFINED
9+
#define SkShape_DEFINED
10+
11+
#include "experimental/xform/SkXform.h"
12+
#include "include/core/SkPaint.h"
13+
14+
class SkCanvas;
15+
16+
class XContext {
17+
public:
18+
virtual ~XContext() {}
19+
20+
void push(Xform* parentXform) { this->onPush(parentXform); }
21+
void pop() { this->onPop(); }
22+
23+
void drawRect(const SkRect&, const SkPaint&, Xform* localXform);
24+
25+
static std::unique_ptr<XContext> Make(SkCanvas*);
26+
27+
protected:
28+
virtual void onPush(Xform*) = 0;
29+
virtual void onPop() = 0;
30+
31+
virtual void onDrawRect(const SkRect&, const SkPaint&, Xform*) = 0;
32+
};
33+
34+
class Shape : public SkRefCnt {
35+
sk_sp<Xform> fXform;
36+
37+
public:
38+
Shape(sk_sp<Xform> x = nullptr) : fXform(std::move(x)) {}
39+
40+
Xform* xform() const { return fXform.get(); }
41+
void setXform(sk_sp<Xform> x) {
42+
fXform = std::move(x);
43+
}
44+
45+
virtual void draw(XContext*) {}
46+
};
47+
48+
class GeoShape : public Shape {
49+
SkRect fRect;
50+
SkPaint fPaint;
51+
52+
GeoShape(sk_sp<Xform> x, const SkRect& r, SkColor c) : Shape(std::move(x)), fRect(r) {
53+
fPaint.setColor(c);
54+
}
55+
56+
public:
57+
static sk_sp<Shape> Make(sk_sp<Xform> x, const SkRect& r, SkColor c) {
58+
return sk_sp<Shape>(new GeoShape(std::move(x), r, c));
59+
}
60+
61+
void draw(XContext*) override;
62+
};
63+
64+
class GroupShape : public Shape {
65+
SkTDArray<Shape*> fArray;
66+
67+
GroupShape(sk_sp<Xform> x) : Shape(std::move(x)) {}
68+
69+
public:
70+
static sk_sp<GroupShape> Make(sk_sp<Xform> x = nullptr) {
71+
return sk_sp<GroupShape>(new GroupShape(std::move(x)));
72+
}
73+
74+
static sk_sp<GroupShape> Make(sk_sp<Xform> x, sk_sp<Shape> s) {
75+
auto g = sk_sp<GroupShape>(new GroupShape(std::move(x)));
76+
g->append(std::move(s));
77+
return g;
78+
}
79+
80+
~GroupShape() override {
81+
fArray.unrefAll();
82+
}
83+
84+
int count() const { return fArray.count(); }
85+
Shape* get(int index) const { return fArray[index]; }
86+
void set(int index, sk_sp<Shape> s) {
87+
fArray[index] = s.release();
88+
}
89+
90+
void append(sk_sp<Shape> s) {
91+
*fArray.append() = s.release();
92+
}
93+
void insert(int index, sk_sp<Shape> s) {
94+
*fArray.insert(index) = s.release();
95+
}
96+
void remove(int index) {
97+
SkSafeUnref(fArray[index]);
98+
fArray.remove(index);
99+
}
100+
101+
void draw(XContext*) override ;
102+
};
103+
104+
#endif

experimental/xform/SkXform.cpp

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
* Copyright 2019 Google LLC.
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+
8+
#include "experimental/xform/SkXform.h"
9+
10+
static std::atomic<uint32_t> gGenID{1};
11+
Xform::GenID Xform::NextGenID() {
12+
return gGenID++;
13+
}
14+
15+
#ifdef SK_DEBUG
16+
void Xform::debugValidate() const {
17+
if (this->isCached() && fParent) {
18+
SkASSERT(fParent->isCached());
19+
}
20+
for (auto c : fChildren) {
21+
SkASSERT(c->parent() == this);
22+
c->debugValidate();
23+
}
24+
}
25+
#endif
26+
27+
void Xform::setParent(sk_sp<Xform> parent) {
28+
if (parent == fParent) {
29+
return;
30+
}
31+
32+
if (fParent) {
33+
fParent->internalRemoveChild(this);
34+
}
35+
if (parent) {
36+
parent->internalAddChild(this);
37+
}
38+
fParent = std::move(parent);
39+
40+
// Potentially we could skip this if knew that our old and new parents
41+
// were both cached, and they started us in the same state...
42+
// For now, we conservatively always inval
43+
this->invalidateCaches();
44+
45+
this->debugValidate();
46+
}
47+
48+
void Xform::internalAddChild(Xform* child) {
49+
SkASSERT(fChildren.find(child) < 0);
50+
fChildren.push_back(child);
51+
}
52+
53+
void Xform::internalRemoveChild(Xform* child) {
54+
int index = fChildren.find(child);
55+
SkASSERT(index >= 0);
56+
fChildren.removeShuffle(index);
57+
}
58+
59+
void Xform::invalidateCaches() {
60+
fGenID = 0;
61+
if (this->isCached()) {
62+
this->internalInvalidateCaches();
63+
for (auto c : fChildren) {
64+
c->invalidateCaches();
65+
}
66+
}
67+
}
68+
69+
void Xform::visit(XformResolver* resolver) {
70+
this->onVisit(resolver);
71+
}
72+
73+
void Xform::setCache(const SkMatrix& ctm, sk_sp<ClipCache> clip) {
74+
fCTM = ctm;
75+
fClip = std::move(clip);
76+
fGenID = NextGenID();
77+
}
78+
79+
//////////////////////////////////////////////////////////////////////////////////////////////////
80+
81+
void MatrixXF::onVisit(XformResolver* resolver) {
82+
resolver->concat(fLocalMatrix);
83+
}
84+
85+
void ClipXF::onVisit(XformResolver* resolver) {
86+
resolver->clipRect(fRect, fOp);
87+
}

0 commit comments

Comments
 (0)