This source file includes following definitions.
- allocPixelRef
- m_rowBytes
- allocAndLockDiscardableMemory
- onNewLockPixels
- onUnlockPixels
- isDiscardable
#include "config.h"
#include "platform/graphics/DiscardablePixelRef.h"
#include "public/platform/Platform.h"
#include "wtf/StdLibExtras.h"
namespace WebCore {
namespace {
const char labelDiscardable[] = "discardable";
}
bool DiscardablePixelRefAllocator::allocPixelRef(SkBitmap* dst, SkColorTable* ctable)
{
ASSERT(!ctable);
int64_t size = dst->computeSize64();
if (size < 0 || !sk_64_isS32(size))
return false;
SkImageInfo info;
if (!dst->asImageInfo(&info))
return false;
SkAutoTUnref<DiscardablePixelRef> pixelRef(new DiscardablePixelRef(info, dst->rowBytes(), adoptPtr(new SkMutex())));
if (pixelRef->allocAndLockDiscardableMemory(sk_64_asS32(size))) {
pixelRef->setURI(labelDiscardable);
dst->setPixelRef(pixelRef.get());
dst->lockPixels();
return true;
}
return dst->allocPixels();
}
DiscardablePixelRef::DiscardablePixelRef(const SkImageInfo& info, size_t rowBytes, PassOwnPtr<SkMutex> mutex)
: SkPixelRef(info, mutex.get())
, m_lockedMemory(0)
, m_mutex(mutex)
, m_rowBytes(rowBytes)
{
}
DiscardablePixelRef::~DiscardablePixelRef()
{
}
bool DiscardablePixelRef::allocAndLockDiscardableMemory(size_t bytes)
{
m_discardable = adoptPtr(blink::Platform::current()->allocateAndLockDiscardableMemory(bytes));
if (m_discardable) {
m_lockedMemory = m_discardable->data();
return true;
}
return false;
}
bool DiscardablePixelRef::onNewLockPixels(LockRec* rec)
{
if (!m_lockedMemory && m_discardable->lock())
m_lockedMemory = m_discardable->data();
if (m_lockedMemory) {
rec->fPixels = m_lockedMemory;
rec->fColorTable = 0;
rec->fRowBytes = m_rowBytes;
return true;
}
return false;
}
void DiscardablePixelRef::onUnlockPixels()
{
if (m_lockedMemory)
m_discardable->unlock();
m_lockedMemory = 0;
}
bool DiscardablePixelRef::isDiscardable(SkPixelRef* pixelRef)
{
DEFINE_STATIC_LOCAL(const SkString, discardable, (labelDiscardable));
return pixelRef && pixelRef->getURI() && discardable.equals(pixelRef->getURI());
}
}