2 * Copyright (C) 2013 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include "platform/graphics/DiscardablePixelRef.h"
29 #include "public/platform/Platform.h"
30 #include "wtf/StdLibExtras.h"
36 // URI label for a discardable SkPixelRef.
37 const char labelDiscardable[] = "discardable";
42 bool DiscardablePixelRefAllocator::allocPixelRef(SkBitmap* dst, SkColorTable* ctable)
44 // It should not be possible to have a non-null color table in Blink.
47 int64_t size = dst->computeSize64();
48 if (size < 0 || !sk_64_isS32(size))
52 if (!dst->asImageInfo(&info))
55 SkAutoTUnref<DiscardablePixelRef> pixelRef(new DiscardablePixelRef(info, dst->rowBytes(), adoptPtr(new SkMutex())));
56 if (pixelRef->allocAndLockDiscardableMemory(sk_64_asS32(size))) {
57 pixelRef->setURI(labelDiscardable);
58 dst->setPixelRef(pixelRef.get());
59 // This method is only called when a DiscardablePixelRef is created to back a SkBitmap.
60 // It is necessary to lock this SkBitmap to have a valid pointer to pixels. Otherwise,
61 // this SkBitmap could be assigned to another SkBitmap and locking/unlocking the other
62 // SkBitmap will make this one losing its pixels.
67 // Fallback to heap allocator if discardable memory is not available.
68 return dst->allocPixels();
71 DiscardablePixelRef::DiscardablePixelRef(const SkImageInfo& info, size_t rowBytes, PassOwnPtr<SkMutex> mutex)
72 : SkPixelRef(info, mutex.get())
75 , m_rowBytes(rowBytes)
79 DiscardablePixelRef::~DiscardablePixelRef()
83 bool DiscardablePixelRef::allocAndLockDiscardableMemory(size_t bytes)
85 m_discardable = adoptPtr(blink::Platform::current()->allocateAndLockDiscardableMemory(bytes));
87 m_lockedMemory = m_discardable->data();
93 bool DiscardablePixelRef::onNewLockPixels(LockRec* rec)
95 if (!m_lockedMemory && m_discardable->lock())
96 m_lockedMemory = m_discardable->data();
99 rec->fPixels = m_lockedMemory;
100 rec->fColorTable = 0;
101 rec->fRowBytes = m_rowBytes;
107 void DiscardablePixelRef::onUnlockPixels()
110 m_discardable->unlock();
114 bool DiscardablePixelRef::isDiscardable(SkPixelRef* pixelRef)
116 // FIXME: DEFINE_STATIC_LOCAL is not thread safe.
117 // ImageDecodingStore provides the synchronization for this.
118 DEFINE_STATIC_LOCAL(const SkString, discardable, (labelDiscardable));
119 return pixelRef && pixelRef->getURI() && discardable.equals(pixelRef->getURI());
122 } // namespace WebCore