Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / third_party / skia / src / core / SkBitmap.cpp
index e675db2..f0ad029 100644 (file)
@@ -91,32 +91,25 @@ void SkBitmap::reset() {
     sk_bzero(this, sizeof(*this));
 }
 
-#ifdef SK_SUPPORT_LEGACY_BITMAP_CONFIG
-SkBitmap::Config SkBitmap::config() const {
-    return SkColorTypeToBitmapConfig(fInfo.colorType());
-}
-#endif
-
 void SkBitmap::getBounds(SkRect* bounds) const {
     SkASSERT(bounds);
     bounds->set(0, 0,
-                SkIntToScalar(fInfo.fWidth), SkIntToScalar(fInfo.fHeight));
+                SkIntToScalar(fInfo.width()), SkIntToScalar(fInfo.height()));
 }
 
 void SkBitmap::getBounds(SkIRect* bounds) const {
     SkASSERT(bounds);
-    bounds->set(0, 0, fInfo.fWidth, fInfo.fHeight);
+    bounds->set(0, 0, fInfo.width(), fInfo.height());
 }
 
 ///////////////////////////////////////////////////////////////////////////////
 
-bool SkBitmap::setInfo(const SkImageInfo& origInfo, size_t rowBytes) {
-    SkImageInfo info = origInfo;
-
-    if (!SkColorTypeValidateAlphaType(info.fColorType, info.fAlphaType,
-                                      &info.fAlphaType)) {
+bool SkBitmap::setInfo(const SkImageInfo& info, size_t rowBytes) {
+    SkAlphaType newAT = info.alphaType();
+    if (!SkColorTypeValidateAlphaType(info.colorType(), info.alphaType(), &newAT)) {
         return reset_return_false(this);
     }
+    // don't look at info.alphaType(), since newAT is the real value...
 
     // require that rowBytes fit in 31bits
     int64_t mrb = info.minRowBytes64();
@@ -141,31 +134,31 @@ bool SkBitmap::setInfo(const SkImageInfo& origInfo, size_t rowBytes) {
 
     this->freePixels();
 
-    fInfo = info;
+    fInfo = info.makeAlphaType(newAT);
     fRowBytes = SkToU32(rowBytes);
     return true;
 }
 
-bool SkBitmap::setAlphaType(SkAlphaType alphaType) {
-    if (!SkColorTypeValidateAlphaType(fInfo.fColorType, alphaType, &alphaType)) {
+bool SkBitmap::setAlphaType(SkAlphaType newAlphaType) {
+    if (!SkColorTypeValidateAlphaType(fInfo.colorType(), newAlphaType, &newAlphaType)) {
         return false;
     }
-    if (fInfo.fAlphaType != alphaType) {
-        fInfo.fAlphaType = alphaType;
+    if (fInfo.alphaType() != newAlphaType) {
+        fInfo = fInfo.makeAlphaType(newAlphaType);
         if (fPixelRef) {
-            fPixelRef->changeAlphaType(alphaType);
+            fPixelRef->changeAlphaType(newAlphaType);
         }
     }
     return true;
 }
 
 void SkBitmap::updatePixelsFromRef() const {
-    if (NULL != fPixelRef) {
+    if (fPixelRef) {
         if (fPixelLockCount > 0) {
             SkASSERT(fPixelRef->isLocked());
 
             void* p = fPixelRef->pixels();
-            if (NULL != p) {
+            if (p) {
                 p = (char*)p
                     + fPixelRefOrigin.fY * fRowBytes
                     + fPixelRefOrigin.fX * fInfo.bytesPerPixel();
@@ -185,21 +178,21 @@ SkPixelRef* SkBitmap::setPixelRef(SkPixelRef* pr, int dx, int dy) {
     if (pr) {
         if (kUnknown_SkColorType != fInfo.colorType()) {
             const SkImageInfo& prInfo = pr->info();
-            SkASSERT(fInfo.fWidth <= prInfo.fWidth);
-            SkASSERT(fInfo.fHeight <= prInfo.fHeight);
-            SkASSERT(fInfo.fColorType == prInfo.fColorType);
-            switch (prInfo.fAlphaType) {
+            SkASSERT(fInfo.width() <= prInfo.width());
+            SkASSERT(fInfo.height() <= prInfo.height());
+            SkASSERT(fInfo.colorType() == prInfo.colorType());
+            switch (prInfo.alphaType()) {
                 case kIgnore_SkAlphaType:
-                    SkASSERT(fInfo.fAlphaType == kIgnore_SkAlphaType);
+                    SkASSERT(fInfo.alphaType() == kIgnore_SkAlphaType);
                     break;
                 case kOpaque_SkAlphaType:
                 case kPremul_SkAlphaType:
-                    SkASSERT(fInfo.fAlphaType == kOpaque_SkAlphaType ||
-                             fInfo.fAlphaType == kPremul_SkAlphaType);
+                    SkASSERT(fInfo.alphaType() == kOpaque_SkAlphaType ||
+                             fInfo.alphaType() == kPremul_SkAlphaType);
                     break;
                 case kUnpremul_SkAlphaType:
-                    SkASSERT(fInfo.fAlphaType == kOpaque_SkAlphaType ||
-                             fInfo.fAlphaType == kUnpremul_SkAlphaType);
+                    SkASSERT(fInfo.alphaType() == kOpaque_SkAlphaType ||
+                             fInfo.alphaType() == kUnpremul_SkAlphaType);
                     break;
             }
         }
@@ -208,8 +201,7 @@ SkPixelRef* SkBitmap::setPixelRef(SkPixelRef* pr, int dx, int dy) {
 
     if (pr) {
         const SkImageInfo& info = pr->info();
-        fPixelRefOrigin.set(SkPin32(dx, 0, info.fWidth),
-                            SkPin32(dy, 0, info.fHeight));
+        fPixelRefOrigin.set(SkPin32(dx, 0, info.width()), SkPin32(dy, 0, info.height()));
     } else {
         // ignore dx,dy if there is no pixelref
         fPixelRefOrigin.setZero();
@@ -229,7 +221,7 @@ SkPixelRef* SkBitmap::setPixelRef(SkPixelRef* pr, int dx, int dy) {
 }
 
 void SkBitmap::lockPixels() const {
-    if (NULL != fPixelRef && 0 == sk_atomic_inc(&fPixelLockCount)) {
+    if (fPixelRef && 0 == sk_atomic_inc(&fPixelLockCount)) {
         fPixelRef->lockPixels();
         this->updatePixelsFromRef();
     }
@@ -239,7 +231,7 @@ void SkBitmap::lockPixels() const {
 void SkBitmap::unlockPixels() const {
     SkASSERT(NULL == fPixelRef || fPixelLockCount > 0);
 
-    if (NULL != fPixelRef && 1 == sk_atomic_dec(&fPixelLockCount)) {
+    if (fPixelRef && 1 == sk_atomic_dec(&fPixelLockCount)) {
         fPixelRef->unlockPixels();
         this->updatePixelsFromRef();
     }
@@ -274,7 +266,7 @@ void SkBitmap::setPixels(void* p, SkColorTable* ctable) {
     SkDEBUGCODE(this->validate();)
 }
 
-bool SkBitmap::allocPixels(Allocator* allocator, SkColorTable* ctable) {
+bool SkBitmap::tryAllocPixels(Allocator* allocator, SkColorTable* ctable) {
     HeapAllocator stdalloc;
 
     if (NULL == allocator) {
@@ -285,7 +277,7 @@ bool SkBitmap::allocPixels(Allocator* allocator, SkColorTable* ctable) {
 
 ///////////////////////////////////////////////////////////////////////////////
 
-bool SkBitmap::allocPixels(const SkImageInfo& requestedInfo, size_t rowBytes) {
+bool SkBitmap::tryAllocPixels(const SkImageInfo& requestedInfo, size_t rowBytes) {
     if (kIndex_8_SkColorType == requestedInfo.colorType()) {
         return reset_return_false(this);
     }
@@ -314,9 +306,9 @@ bool SkBitmap::allocPixels(const SkImageInfo& requestedInfo, size_t rowBytes) {
     return true;
 }
 
-bool SkBitmap::allocPixels(const SkImageInfo& requestedInfo, SkPixelRefFactory* factory,
-                           SkColorTable* ctable) {
-    if (kIndex_8_SkColorType == requestedInfo.fColorType && NULL == ctable) {
+bool SkBitmap::tryAllocPixels(const SkImageInfo& requestedInfo, SkPixelRefFactory* factory,
+                                SkColorTable* ctable) {
+    if (kIndex_8_SkColorType == requestedInfo.colorType() && NULL == ctable) {
         return reset_return_false(this);
     }
     if (!this->setInfo(requestedInfo)) {
@@ -384,7 +376,7 @@ bool SkBitmap::installMaskPixels(const SkMask& mask) {
 ///////////////////////////////////////////////////////////////////////////////
 
 void SkBitmap::freePixels() {
-    if (NULL != fPixelRef) {
+    if (fPixelRef) {
         if (fPixelLockCount > 0) {
             fPixelRef->unlockPixels();
         }
@@ -473,8 +465,7 @@ bool SkBitmap::copyPixelsTo(void* const dst, size_t dstSize,
             SkAutoLockPixels lock(*this);
             const uint8_t* srcP = reinterpret_cast<const uint8_t*>(getPixels());
             uint8_t* dstP = reinterpret_cast<uint8_t*>(dst);
-            for (int row = 0; row < fInfo.fHeight;
-                 row++, srcP += fRowBytes, dstP += dstRowBytes) {
+            for (int row = 0; row < fInfo.height(); row++, srcP += fRowBytes, dstP += dstRowBytes) {
                 memcpy(dstP, srcP, rowBytes);
             }
 
@@ -863,10 +854,8 @@ bool SkBitmap::readPixels(const SkImageInfo& requestedDstInfo, void* dstPixels,
         return false;
     }
     
-    SkImageInfo dstInfo = requestedDstInfo;
     // the intersect may have shrunk info's logical size
-    dstInfo.fWidth = srcR.width();
-    dstInfo.fHeight = srcR.height();
+    const SkImageInfo dstInfo = requestedDstInfo.makeWH(srcR.width(), srcR.height());
     
     // if x or y are negative, then we have to adjust pixels
     if (x > 0) {
@@ -887,9 +876,7 @@ bool SkBitmap::readPixels(const SkImageInfo& requestedDstInfo, void* dstPixels,
         return false;
     }
     
-    SkImageInfo srcInfo = this->info();
-    srcInfo.fWidth = dstInfo.width();
-    srcInfo.fHeight = dstInfo.height();
+    const SkImageInfo srcInfo = this->info().makeWH(dstInfo.width(), dstInfo.height());
     
     const void* srcPixels = this->getAddr(srcR.x(), srcR.y());
     return SkPixelInfo::CopyPixels(dstInfo, dstPixels, dstRB, srcInfo, srcPixels, this->rowBytes(),
@@ -942,8 +929,7 @@ bool SkBitmap::copyTo(SkBitmap* dst, SkColorType dstColorType, Allocator* alloc)
     // The only way to be readyToDraw is if fPixelRef is non NULL.
     SkASSERT(fPixelRef != NULL);
 
-    SkImageInfo dstInfo = src->info();
-    dstInfo.fColorType = dstColorType;
+    const SkImageInfo dstInfo = src->info().makeColorType(dstColorType);
 
     SkBitmap tmpDst;
     if (!tmpDst.setInfo(dstInfo)) {
@@ -956,7 +942,7 @@ bool SkBitmap::copyTo(SkBitmap* dst, SkColorType dstColorType, Allocator* alloc)
         // TODO: can we just ref() the src colortable? Is it reentrant-safe?
         ctable.reset(SkNEW_ARGS(SkColorTable, (*src->getColorTable())));
     }
-    if (!tmpDst.allocPixels(alloc, ctable)) {
+    if (!tmpDst.tryAllocPixels(alloc, ctable)) {
         return false;
     }
 
@@ -1015,8 +1001,7 @@ bool SkBitmap::deepCopyTo(SkBitmap* dst) const {
                 rowBytes = 0;
             }
 
-            SkImageInfo info = fInfo;
-            info.fColorType = dstCT;
+            const SkImageInfo info = fInfo.makeColorType(dstCT);
             if (!dst->setInfo(info, rowBytes)) {
                 return false;
             }
@@ -1118,7 +1103,7 @@ bool SkBitmap::extractAlpha(SkBitmap* dst, const SkPaint* paint,
     SkMaskFilter* filter = paint ? paint->getMaskFilter() : NULL;
 
     // compute our (larger?) dst bounds if we have a filter
-    if (NULL != filter) {
+    if (filter) {
         identity.reset();
         srcM.fImage = NULL;
         if (!filter->filterMask(&dstM, srcM, identity, NULL)) {
@@ -1128,7 +1113,7 @@ bool SkBitmap::extractAlpha(SkBitmap* dst, const SkPaint* paint,
     } else {
     NO_FILTER_CASE:
         tmpBitmap.setInfo(SkImageInfo::MakeA8(this->width(), this->height()), srcM.fRowBytes);
-        if (!tmpBitmap.allocPixels(allocator, NULL)) {
+        if (!tmpBitmap.tryAllocPixels(allocator, NULL)) {
             // Allocation of pixels for alpha bitmap failed.
             SkDebugf("extractAlpha failed to allocate (%d,%d) alpha bitmap\n",
                     tmpBitmap.width(), tmpBitmap.height());
@@ -1152,7 +1137,7 @@ bool SkBitmap::extractAlpha(SkBitmap* dst, const SkPaint* paint,
 
     tmpBitmap.setInfo(SkImageInfo::MakeA8(dstM.fBounds.width(), dstM.fBounds.height()),
                       dstM.fRowBytes);
-    if (!tmpBitmap.allocPixels(allocator, NULL)) {
+    if (!tmpBitmap.tryAllocPixels(allocator, NULL)) {
         // Allocation of pixels for alpha bitmap failed.
         SkDebugf("extractAlpha failed to allocate (%d,%d) alpha bitmap\n",
                 tmpBitmap.width(), tmpBitmap.height());
@@ -1226,9 +1211,9 @@ bool SkBitmap::ReadRawPixels(SkReadBuffer* buffer, SkBitmap* bitmap) {
         return false;
     }
 
-    char* dst = (char*)sk_malloc_throw(ramSize);
+    SkAutoDataUnref data(SkData::NewUninitialized(ramSize));
+    char* dst = (char*)data->writable_data();
     buffer->readByteArray(dst, snugSize);
-    SkAutoDataUnref data(SkData::NewFromMalloc(dst, ramSize));
 
     if (snugSize != ramSize) {
         const char* srcRow = dst + snugRB * (height - 1);
@@ -1344,7 +1329,7 @@ void SkBitmap::validate() const {
         SkASSERT(fPixelRefOrigin.fX >= 0);
         SkASSERT(fPixelRefOrigin.fY >= 0);
         SkASSERT(fPixelRef->info().width() >= (int)this->width() + fPixelRefOrigin.fX);
-        SkASSERT(fPixelRef->info().fHeight >= (int)this->height() + fPixelRefOrigin.fY);
+        SkASSERT(fPixelRef->info().height() >= (int)this->height() + fPixelRefOrigin.fY);
         SkASSERT(fPixelRef->rowBytes() >= fInfo.minRowBytes());
     } else {
         SkASSERT(NULL == fColorTable);
@@ -1381,7 +1366,7 @@ void SkBitmap::toString(SkString* str) const {
         str->appendf(" pixels:%p", this->getPixels());
     } else {
         const char* uri = pr->getURI();
-        if (NULL != uri) {
+        if (uri) {
             str->appendf(" uri:\"%s\"", uri);
         } else {
             str->appendf(" pixelref:%p", pr);