blob: 2d4daae781168bf3ef6595446e3a68f146d77172 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
2/*
3 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
reed@android.com8a1c16f2008-12-17 15:59:43 +00008#include "SkPixelRef.h"
9#include "SkFlattenable.h"
10#include "SkThread.h"
11
digit@google.com1771cbf2012-01-26 21:26:40 +000012SK_DECLARE_STATIC_MUTEX(gPixelRefMutex);
bsalomon@google.com586f48c2011-04-14 15:07:22 +000013
reed@google.com9c49bc32011-07-07 13:42:37 +000014extern int32_t SkNextPixelRefGenerationID();
15int32_t SkNextPixelRefGenerationID() {
bsalomon@google.com586f48c2011-04-14 15:07:22 +000016 static int32_t gPixelRefGenerationID;
17 // do a loop in case our global wraps around, as we never want to
18 // return a 0
19 int32_t genID;
20 do {
21 genID = sk_atomic_inc(&gPixelRefGenerationID) + 1;
22 } while (0 == genID);
23 return genID;
24}
25
reed@android.com8a1c16f2008-12-17 15:59:43 +000026
digit@google.com1771cbf2012-01-26 21:26:40 +000027SkPixelRef::SkPixelRef(SkBaseMutex* mutex) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000028 if (NULL == mutex) {
29 mutex = &gPixelRefMutex;
30 }
31 fMutex = mutex;
32 fPixels = NULL;
33 fColorTable = NULL; // we do not track ownership of this
34 fLockCount = 0;
35 fGenerationID = 0; // signal to rebuild
36 fIsImmutable = false;
37}
38
djsollen@google.com5370cd92012-03-28 20:47:01 +000039SkPixelRef::SkPixelRef(SkFlattenableReadBuffer& buffer, SkBaseMutex* mutex)
40 : INHERITED(buffer) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000041 if (NULL == mutex) {
42 mutex = &gPixelRefMutex;
43 }
44 fMutex = mutex;
45 fPixels = NULL;
46 fColorTable = NULL; // we do not track ownership of this
47 fLockCount = 0;
48 fGenerationID = 0; // signal to rebuild
49 fIsImmutable = buffer.readBool();
50}
51
djsollen@google.com5370cd92012-03-28 20:47:01 +000052void SkPixelRef::flatten(SkFlattenableWriteBuffer& buffer) {
53 this->INHERITED::flatten(buffer);
reed@android.com8a1c16f2008-12-17 15:59:43 +000054 buffer.writeBool(fIsImmutable);
55}
56
57void SkPixelRef::lockPixels() {
58 SkAutoMutexAcquire ac(*fMutex);
reed@google.com93c5f9e2011-02-24 18:09:46 +000059
reed@android.com8a1c16f2008-12-17 15:59:43 +000060 if (1 == ++fLockCount) {
61 fPixels = this->onLockPixels(&fColorTable);
62 }
63}
64
65void SkPixelRef::unlockPixels() {
66 SkAutoMutexAcquire ac(*fMutex);
reed@google.com93c5f9e2011-02-24 18:09:46 +000067
reed@android.com8a1c16f2008-12-17 15:59:43 +000068 SkASSERT(fLockCount > 0);
69 if (0 == --fLockCount) {
70 this->onUnlockPixels();
71 fPixels = NULL;
72 fColorTable = NULL;
73 }
74}
75
reed@google.com9c49bc32011-07-07 13:42:37 +000076bool SkPixelRef::lockPixelsAreWritable() const {
77 return this->onLockPixelsAreWritable();
78}
79
80bool SkPixelRef::onLockPixelsAreWritable() const {
81 return true;
82}
83
reed@android.com8a1c16f2008-12-17 15:59:43 +000084uint32_t SkPixelRef::getGenerationID() const {
bsalomon@google.com586f48c2011-04-14 15:07:22 +000085 if (0 == fGenerationID) {
86 fGenerationID = SkNextPixelRefGenerationID();
reed@android.com8a1c16f2008-12-17 15:59:43 +000087 }
bsalomon@google.com586f48c2011-04-14 15:07:22 +000088 return fGenerationID;
reed@android.com8a1c16f2008-12-17 15:59:43 +000089}
90
91void SkPixelRef::notifyPixelsChanged() {
reed@android.com3eab80c2009-03-24 18:47:35 +000092#ifdef SK_DEBUG
reed@android.com8a1c16f2008-12-17 15:59:43 +000093 if (fIsImmutable) {
94 SkDebugf("========== notifyPixelsChanged called on immutable pixelref");
reed@android.com8a1c16f2008-12-17 15:59:43 +000095 }
reed@android.com3eab80c2009-03-24 18:47:35 +000096#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +000097 // this signals us to recompute this next time around
98 fGenerationID = 0;
99}
100
101void SkPixelRef::setImmutable() {
102 fIsImmutable = true;
103}
104
reed@google.com50dfa012011-04-01 19:05:36 +0000105bool SkPixelRef::readPixels(SkBitmap* dst, const SkIRect* subset) {
106 return this->onReadPixels(dst, subset);
107}
108
109bool SkPixelRef::onReadPixels(SkBitmap* dst, const SkIRect* subset) {
110 return false;
111}
112
reed@android.com8a1c16f2008-12-17 15:59:43 +0000113///////////////////////////////////////////////////////////////////////////////
114
djsollen@google.com56c69772011-11-08 19:00:26 +0000115#ifdef SK_BUILD_FOR_ANDROID
djsollen@google.com57f49692011-02-23 20:46:31 +0000116void SkPixelRef::globalRef(void* data) {
117 this->ref();
118}
119
120void SkPixelRef::globalUnref() {
121 this->unref();
122}
reed@google.com93c5f9e2011-02-24 18:09:46 +0000123#endif