00001 /* 00002 * Copyright (C) 2008 The Android Open Source Project 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 00017 #ifndef SkPixelRef_DEFINED 00018 #define SkPixelRef_DEFINED 00019 00020 #include "SkRefCnt.h" 00021 #include "SkString.h" 00022 00023 class SkColorTable; 00024 class SkMutex; 00025 class SkFlattenableReadBuffer; 00026 class SkFlattenableWriteBuffer; 00027 00036 class SkPixelRef : public SkRefCnt { 00037 public: 00038 explicit SkPixelRef(SkMutex* mutex = NULL); 00039 00043 void* pixels() const { return fPixels; } 00044 00047 SkColorTable* colorTable() const { return fColorTable; } 00048 00051 int getLockCount() const { return fLockCount; } 00052 00056 void lockPixels(); 00062 void unlockPixels(); 00063 00068 uint32_t getGenerationID() const; 00069 00074 void notifyPixelsChanged(); 00075 00079 bool isImmutable() const { return fIsImmutable; } 00080 00085 void setImmutable(); 00086 00090 const char* getURI() const { return fURI.size() ? fURI.c_str() : NULL; } 00091 00094 void setURI(const char uri[]) { 00095 fURI.set(uri); 00096 } 00097 00100 void setURI(const char uri[], size_t len) { 00101 fURI.set(uri, len); 00102 } 00103 00106 void setURI(const SkString& uri) { fURI = uri; } 00107 00108 // serialization 00109 00110 typedef SkPixelRef* (*Factory)(SkFlattenableReadBuffer&); 00111 00112 virtual Factory getFactory() const { return NULL; } 00113 virtual void flatten(SkFlattenableWriteBuffer&) const; 00114 00115 static Factory NameToFactory(const char name[]); 00116 static const char* FactoryToName(Factory); 00117 static void Register(const char name[], Factory); 00118 00119 class Registrar { 00120 public: 00121 Registrar(const char name[], Factory factory) { 00122 SkPixelRef::Register(name, factory); 00123 } 00124 }; 00125 00126 protected: 00130 virtual void* onLockPixels(SkColorTable**) = 0; 00135 virtual void onUnlockPixels() = 0; 00136 00140 SkMutex* mutex() const { return fMutex; } 00141 00142 SkPixelRef(SkFlattenableReadBuffer&, SkMutex*); 00143 00144 private: 00145 SkMutex* fMutex; // must remain in scope for the life of this object 00146 void* fPixels; 00147 SkColorTable* fColorTable; // we do not track ownership, subclass does 00148 int fLockCount; 00149 00150 mutable uint32_t fGenerationID; 00151 00152 SkString fURI; 00153 00154 // can go from false to true, but never from true to false 00155 bool fIsImmutable; 00156 }; 00157 00158 #endif