aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/core
diff options
context:
space:
mode:
authorGravatar reed <reed@google.com>2014-11-11 04:56:05 -0800
committerGravatar Commit bot <commit-bot@chromium.org>2014-11-11 04:56:05 -0800
commitc536942dc93d81aeb5df8a62f81582b0bdaa4f54 (patch)
treee2232627470c89b82f5f44b301353b9f7652b1dd /src/core
parent81f71b6630a9b7398bf983689436cccdd8dd3ff7 (diff)
initial checkin for experimenting
Diffstat (limited to 'src/core')
-rw-r--r--src/core/SkCanvasDrawable.cpp50
-rw-r--r--src/core/SkCanvasDrawable.h59
2 files changed, 109 insertions, 0 deletions
diff --git a/src/core/SkCanvasDrawable.cpp b/src/core/SkCanvasDrawable.cpp
new file mode 100644
index 0000000000..7c01738dd6
--- /dev/null
+++ b/src/core/SkCanvasDrawable.cpp
@@ -0,0 +1,50 @@
+/*
+ * Copyright 2014 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#include "SkCanvas.h"
+#include "SkCanvasDrawable.h"
+#include "SkThread.h"
+
+static int32_t next_generation_id() {
+ static int32_t gCanvasDrawableGenerationID;
+
+ // do a loop in case our global wraps around, as we never want to
+ // return a 0
+ int32_t genID;
+ do {
+ genID = sk_atomic_inc(&gCanvasDrawableGenerationID) + 1;
+ } while (0 == genID);
+ return genID;
+}
+
+SkCanvasDrawable::SkCanvasDrawable() : fGenerationID(0) {}
+
+void SkCanvasDrawable::draw(SkCanvas* canvas) {
+ SkAutoCanvasRestore acr(canvas, true);
+ this->onDraw(canvas);
+}
+
+uint32_t SkCanvasDrawable::getGenerationID() {
+ if (0 == fGenerationID) {
+ fGenerationID = next_generation_id();
+ }
+ return fGenerationID;
+}
+
+bool SkCanvasDrawable::getBounds(SkRect* boundsPtr) {
+ SkRect bounds;
+ if (!boundsPtr) {
+ boundsPtr = &bounds;
+ }
+ return this->onGetBounds(boundsPtr);
+}
+
+void SkCanvasDrawable::notifyDrawingChanged() {
+ fGenerationID = 0;
+}
+
+
diff --git a/src/core/SkCanvasDrawable.h b/src/core/SkCanvasDrawable.h
new file mode 100644
index 0000000000..8092ea0b7a
--- /dev/null
+++ b/src/core/SkCanvasDrawable.h
@@ -0,0 +1,59 @@
+/*
+ * Copyright 2014 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#ifndef SkCanvasDrawable_DEFINED
+#define SkCanvasDrawable_DEFINED
+
+#include "SkRefCnt.h"
+
+class SkCanvas;
+struct SkRect;
+
+/**
+ * Base-class to capture a set of drawing commands (sent to SkCanvas). Instances of this class
+ * need not be thread-safe, but they must be able to be used in a thread different from where
+ * they were created.
+ */
+class SkCanvasDrawable : public SkRefCnt {
+public:
+ SkCanvasDrawable();
+
+ /**
+ * Draws into the specified content. The drawing sequence will be balanced upon return
+ * (i.e. the saveLevel() on the canvas will match what it was when draw() was called,
+ * and the current matrix and clip settings will not be changed.
+ */
+ void draw(SkCanvas*);
+
+ /**
+ * Return a unique value for this instance. If two calls to this return the same value,
+ * it is presumed that calling the draw() method will render the same thing as well.
+ *
+ * Subclasses that change their state should call notifyDrawingChanged() to ensure that
+ * a new value will be returned the next time it is called.
+ */
+ uint32_t getGenerationID();
+
+ /**
+ * If the drawable knows a bounds that will contains all of its drawing, return true and
+ * set the parameter to that rectangle. If one is not known, ignore the parameter and
+ * return false.
+ */
+ bool getBounds(SkRect*);
+
+ void notifyDrawingChanged();
+
+protected:
+ virtual void onDraw(SkCanvas*) = 0;
+
+ virtual bool onGetBounds(SkRect*) { return false; }
+
+private:
+ int32_t fGenerationID;
+};
+
+#endif