summaryrefslogtreecommitdiffstats
path: root/skia/sgl/SkPixelRef.cpp
diff options
context:
space:
mode:
authorinitial.commit <initial.commit@0039d316-1c4b-4281-b951-d872f2087c98>2008-07-27 00:09:42 +0000
committerinitial.commit <initial.commit@0039d316-1c4b-4281-b951-d872f2087c98>2008-07-27 00:09:42 +0000
commitae2c20f398933a9e86c387dcc465ec0f71065ffc (patch)
treede668b1411e2ee0b4e49b6d8f8b68183134ac990 /skia/sgl/SkPixelRef.cpp
parent09911bf300f1a419907a9412154760efd0b7abc3 (diff)
downloadchromium_src-ae2c20f398933a9e86c387dcc465ec0f71065ffc.zip
chromium_src-ae2c20f398933a9e86c387dcc465ec0f71065ffc.tar.gz
chromium_src-ae2c20f398933a9e86c387dcc465ec0f71065ffc.tar.bz2
Add skia to the repository.
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@16 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'skia/sgl/SkPixelRef.cpp')
-rw-r--r--skia/sgl/SkPixelRef.cpp129
1 files changed, 129 insertions, 0 deletions
diff --git a/skia/sgl/SkPixelRef.cpp b/skia/sgl/SkPixelRef.cpp
new file mode 100644
index 0000000..adfc3c0
--- /dev/null
+++ b/skia/sgl/SkPixelRef.cpp
@@ -0,0 +1,129 @@
+#include "SkPixelRef.h"
+#include "SkFlattenable.h"
+#include "SkThread.h"
+
+static SkMutex gPixelRefMutex;
+static int32_t gPixelRefGenerationID;
+
+SkPixelRef::SkPixelRef(SkMutex* mutex) {
+ if (NULL == mutex) {
+ mutex = &gPixelRefMutex;
+ }
+ fMutex = mutex;
+ fPixels = NULL;
+ fColorTable = NULL; // we do not track ownership of this
+ fLockCount = 0;
+ fGenerationID = 0; // signal to rebuild
+ fIsImmutable = false;
+}
+
+SkPixelRef::SkPixelRef(SkFlattenableReadBuffer& buffer, SkMutex* mutex) {
+ if (NULL == mutex) {
+ mutex = &gPixelRefMutex;
+ }
+ fMutex = mutex;
+ fPixels = NULL;
+ fColorTable = NULL; // we do not track ownership of this
+ fLockCount = 0;
+ fGenerationID = 0; // signal to rebuild
+ fIsImmutable = buffer.readBool();
+}
+
+void SkPixelRef::flatten(SkFlattenableWriteBuffer& buffer) const {
+ buffer.writeBool(fIsImmutable);
+}
+
+void SkPixelRef::lockPixels() {
+ SkAutoMutexAcquire ac(*fMutex);
+
+ if (1 == ++fLockCount) {
+ fPixels = this->onLockPixels(&fColorTable);
+ }
+}
+
+void SkPixelRef::unlockPixels() {
+ SkAutoMutexAcquire ac(*fMutex);
+
+ SkASSERT(fLockCount > 0);
+ if (0 == --fLockCount) {
+ this->onUnlockPixels();
+ fPixels = NULL;
+ fColorTable = NULL;
+ }
+}
+
+uint32_t SkPixelRef::getGenerationID() const {
+ uint32_t genID = fGenerationID;
+ if (0 == genID) {
+ // do a loop in case our global wraps around, as we never want to
+ // return a 0
+ do {
+ genID = sk_atomic_inc(&gPixelRefGenerationID) + 1;
+ } while (0 == genID);
+ fGenerationID = genID;
+ }
+ return genID;
+}
+
+void SkPixelRef::notifyPixelsChanged() {
+ if (fIsImmutable) {
+ SkDebugf("========== notifyPixelsChanged called on immutable pixelref");
+ sk_throw();
+ }
+ // this signals us to recompute this next time around
+ fGenerationID = 0;
+}
+
+void SkPixelRef::setImmutable() {
+ fIsImmutable = true;
+}
+
+///////////////////////////////////////////////////////////////////////////////
+
+#define MAX_PAIR_COUNT 16
+
+struct Pair {
+ const char* fName;
+ SkPixelRef::Factory fFactory;
+};
+
+static int gCount;
+static Pair gPairs[MAX_PAIR_COUNT];
+
+void SkPixelRef::Register(const char name[], Factory factory) {
+ SkASSERT(name);
+ SkASSERT(factory);
+
+ static bool gOnce;
+ if (!gOnce) {
+ gCount = 0;
+ gOnce = true;
+ }
+
+ SkASSERT(gCount < MAX_PAIR_COUNT);
+
+ gPairs[gCount].fName = name;
+ gPairs[gCount].fFactory = factory;
+ gCount += 1;
+}
+
+SkPixelRef::Factory SkPixelRef::NameToFactory(const char name[]) {
+ const Pair* pairs = gPairs;
+ for (int i = gCount - 1; i >= 0; --i) {
+ if (strcmp(pairs[i].fName, name) == 0) {
+ return pairs[i].fFactory;
+ }
+ }
+ return NULL;
+}
+
+const char* SkPixelRef::FactoryToName(Factory fact) {
+ const Pair* pairs = gPairs;
+ for (int i = gCount - 1; i >= 0; --i) {
+ if (pairs[i].fFactory == fact) {
+ return pairs[i].fName;
+ }
+ }
+ return NULL;
+}
+