remove SkRefCnt safeRef() and safeUnref(), and replace the call-sites with
SkSafeRef() and SkSafeUnref().
This is basically a bug waiting to happen. An optimizing compiler can remove
checks for null on "this" if it chooses. However, SkRefCnt::safeRef() relies on
precisely this check...
void SkRefCnt::safeRef() {
if (this) {
this->ref();
}
}
Since a compiler might skip the if-clause, it breaks the intention of this
method, hence its removal.
static inline void SkSafeRef(SkRefCnt* obj) {
if (obj) {
obj->ref();
}
}
This form is not ignored by an optimizing compile, so we use it instead.
git-svn-id: http://skia.googlecode.com/svn/trunk@762 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/samplecode/SamplePatch.cpp b/samplecode/SamplePatch.cpp
index fb70dba..4203391 100644
--- a/samplecode/SamplePatch.cpp
+++ b/samplecode/SamplePatch.cpp
@@ -23,17 +23,17 @@
static void drawtriangle(SkCanvas* canvas, const SkPaint& paint,
const SkPoint pts[3]) {
SkPath path;
-
+
path.moveTo(pts[0]);
path.lineTo(pts[1]);
path.lineTo(pts[2]);
-
+
canvas->drawPath(path, paint);
}
static SkShader* make_shader0(SkIPoint* size) {
SkBitmap bm;
-
+
// SkImageDecoder::DecodeFile("/skimages/progressivejpg.jpg", &bm);
SkImageDecoder::DecodeFile("/skimages/logo.png", &bm);
size->set(bm.width(), bm.height());
@@ -54,7 +54,7 @@
public:
Patch() { sk_bzero(fPts, sizeof(fPts)); }
~Patch() {}
-
+
void setPatch(const SkPoint pts[12]) {
memcpy(fPts, pts, 12 * sizeof(SkPoint));
fPts[12] = pts[0]; // the last shall be first
@@ -63,7 +63,7 @@
void draw(SkCanvas*, const SkPaint&, int segsU, int segsV,
bool doTextures, bool doColors);
-
+
private:
SkPoint fPts[13];
int fW, fH;
@@ -89,12 +89,12 @@
SkScalar u = SkIntToScalar(iu) / nu;
SkScalar v = SkIntToScalar(iv) / nv;
-
+
SkScalar uv = SkScalarMul(u, v);
SkScalar Uv = SkScalarMul(SK_Scalar1 - u, v);
SkScalar uV = SkScalarMul(u, SK_Scalar1 - v);
SkScalar UV = SkScalarMul(SK_Scalar1 - u, SK_Scalar1 - v);
-
+
SkScalar x0 = SkScalarMul(UV, edge[TL].fX) + SkScalarMul(uV, edge[TR].fX) +
SkScalarMul(Uv, edge[BL].fX) + SkScalarMul(uv, edge[BR].fX);
SkScalar y0 = SkScalarMul(UV, edge[TL].fY) + SkScalarMul(uV, edge[TR].fY) +
@@ -123,7 +123,7 @@
static SkColor make_color(SkScalar s, SkScalar t) {
int cs = ScalarTo255(s);
- int ct = ScalarTo255(t);
+ int ct = ScalarTo255(t);
return SkColorSetARGB(0xFF, cs, 0, 0) + SkColorSetARGB(0, 0, ct, 0);
}
@@ -139,22 +139,22 @@
SkPoint* edge1 = edge0 + nu;
SkPoint* edge2 = edge1 + nv;
SkPoint* edge3 = edge2 + nu;
-
+
// evaluate the edge points
eval_patch_edge(fPts + 0, edge0, nu);
eval_patch_edge(fPts + 3, edge1, nv);
eval_patch_edge(fPts + 6, edge2, nu);
eval_patch_edge(fPts + 9, edge3, nv);
edge3[nv] = edge0[0]; // the last shall be first
-
+
for (i = 0; i < npts; i++) {
// canvas->drawLine(edge0[i].fX, edge0[i].fY, edge0[i+1].fX, edge0[i+1].fY, paint);
}
-
+
int row, vertCount = (nu + 1) * (nv + 1);
SkAutoTMalloc<SkPoint> vertStorage(vertCount);
SkPoint* verts = vertStorage.get();
-
+
// first row
memcpy(verts, edge0, (nu + 1) * sizeof(SkPoint));
// rows
@@ -172,9 +172,9 @@
for (i = 0; i <= nu; i++) {
last[i] = edge2[nu - i];
}
-
+
// canvas->drawPoints(verts, vertCount, paint);
-
+
int stripCount = (nu + 1) * 2;
SkAutoTMalloc<SkPoint> stripStorage(stripCount * 2);
SkAutoTMalloc<SkColor> colorStorage(stripCount);
@@ -226,8 +226,8 @@
SkShader* fShader1;
SkIPoint fSize0, fSize1;
SkPoint fPts[12];
-
-public:
+
+public:
PatchView() {
fShader0 = make_shader0(&fSize0);
fSize1 = fSize0;
@@ -251,12 +251,12 @@
fPts[10].set(S*0, T*3);
fPts[11].set(S*0, T*2);
}
-
+
virtual ~PatchView() {
- fShader0->safeUnref();
- fShader1->safeUnref();
+ SkSafeUnref(fShader0);
+ SkSafeUnref(fShader1);
}
-
+
protected:
// overrides from SkEventSink
virtual bool onQuery(SkEvent* evt) {
@@ -268,22 +268,22 @@
}
return this->INHERITED::onQuery(evt);
}
-
+
void drawBG(SkCanvas* canvas) {
canvas->drawColor(SK_ColorGRAY);
}
-
+
virtual void onDraw(SkCanvas* canvas) {
this->drawBG(canvas);
-
+
SkPaint paint;
paint.setDither(true);
paint.setFilterBitmap(true);
-
+
canvas->translate(SkIntToScalar(20), 0);
Patch patch;
-
+
paint.setShader(fShader0);
if (fSize0.fX == 0) {
fSize0.fX = 1;
@@ -292,33 +292,33 @@
fSize0.fY = 1;
}
patch.setBounds(fSize0.fX, fSize0.fY);
-
- patch.setPatch(fPts);
+
+ patch.setPatch(fPts);
drawpatches(canvas, paint, 10, 10, &patch);
-
+
paint.setShader(NULL);
paint.setAntiAlias(true);
paint.setStrokeWidth(SkIntToScalar(5));
canvas->drawPoints(SkCanvas::kPoints_PointMode, SK_ARRAY_COUNT(fPts), fPts, paint);
-
+
canvas->translate(0, SkIntToScalar(300));
-
+
paint.setAntiAlias(false);
paint.setShader(fShader1);
patch.setBounds(fSize1.fX, fSize1.fY);
drawpatches(canvas, paint, 10, 10, &patch);
}
-
+
class PtClick : public Click {
public:
int fIndex;
PtClick(SkView* view, int index) : Click(view), fIndex(index) {}
};
-
+
static bool hittest(const SkPoint& pt, SkScalar x, SkScalar y) {
return SkPoint::Length(pt.fX - x, pt.fY - y) < SkIntToScalar(5);
}
-
+
virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y) {
for (int i = 0; i < SK_ARRAY_COUNT(fPts); i++) {
if (hittest(fPts[i], x, y)) {
@@ -327,13 +327,13 @@
}
return this->INHERITED::onFindClickHandler(x, y);
}
-
+
virtual bool onClick(Click* click) {
fPts[((PtClick*)click)->fIndex].set(click->fCurr.fX, click->fCurr.fY);
this->inval(NULL);
return true;
}
-
+
private:
typedef SkView INHERITED;
};