OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright (C) 2013 Google Inc. All rights reserved. | |
3 * | |
4 * Redistribution and use in source and binary forms, with or without | |
5 * modification, are permitted provided that the following conditions | |
6 * are met: | |
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. | |
12 * | |
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. | |
24 */ | |
25 | |
26 #include "config.h" | |
27 #include "platform/graphics/DiscardablePixelRef.h" | |
28 | |
29 #include "public/platform/Platform.h" | |
30 #include <string.h> | |
31 | |
32 namespace blink { | |
33 | |
34 namespace { | |
35 | |
36 // URI label for a discardable SkPixelRef. | |
37 const char labelDiscardable[] = "discardable"; | |
38 | |
39 } // namespace | |
40 | |
41 | |
42 bool DiscardablePixelRefAllocator::allocPixelRef(SkBitmap* dst, SkColorTable* ct
able) | |
43 { | |
44 // It should not be possible to have a non-null color table in Blink. | |
45 ASSERT(!ctable); | |
46 | |
47 int64_t size = dst->computeSize64(); | |
48 if (size < 0 || !sk_64_isS32(size)) | |
49 return false; | |
50 | |
51 const SkImageInfo& info = dst->info(); | |
52 if (kUnknown_SkColorType == info.colorType()) | |
53 return false; | |
54 | |
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 b
ack a SkBitmap. | |
60 // It is necessary to lock this SkBitmap to have a valid pointer to pixe
ls. Otherwise, | |
61 // this SkBitmap could be assigned to another SkBitmap and locking/unloc
king the other | |
62 // SkBitmap will make this one losing its pixels. | |
63 dst->lockPixels(); | |
64 return true; | |
65 } | |
66 | |
67 // Fallback to heap allocator if discardable memory is not available. | |
68 return dst->allocPixels(); | |
69 } | |
70 | |
71 DiscardablePixelRef::DiscardablePixelRef(const SkImageInfo& info, size_t rowByte
s, PassOwnPtr<SkMutex> mutex) | |
72 : SkPixelRef(info, mutex.get()) | |
73 , m_lockedMemory(0) | |
74 , m_mutex(mutex) | |
75 , m_rowBytes(rowBytes) | |
76 { | |
77 } | |
78 | |
79 DiscardablePixelRef::~DiscardablePixelRef() | |
80 { | |
81 } | |
82 | |
83 bool DiscardablePixelRef::allocAndLockDiscardableMemory(size_t bytes) | |
84 { | |
85 m_discardable = adoptPtr(Platform::current()->allocateAndLockDiscardableMemo
ry(bytes)); | |
86 if (m_discardable) { | |
87 m_lockedMemory = m_discardable->data(); | |
88 return true; | |
89 } | |
90 return false; | |
91 } | |
92 | |
93 bool DiscardablePixelRef::onNewLockPixels(LockRec* rec) | |
94 { | |
95 if (!m_lockedMemory && m_discardable->lock()) | |
96 m_lockedMemory = m_discardable->data(); | |
97 | |
98 if (m_lockedMemory) { | |
99 rec->fPixels = m_lockedMemory; | |
100 rec->fColorTable = 0; | |
101 rec->fRowBytes = m_rowBytes; | |
102 return true; | |
103 } | |
104 return false; | |
105 } | |
106 | |
107 void DiscardablePixelRef::onUnlockPixels() | |
108 { | |
109 if (m_lockedMemory) | |
110 m_discardable->unlock(); | |
111 m_lockedMemory = 0; | |
112 } | |
113 | |
114 bool DiscardablePixelRef::isDiscardable(SkPixelRef* pixelRef) | |
115 { | |
116 return pixelRef && pixelRef->getURI() && !strcmp(pixelRef->getURI(), labelDi
scardable); | |
117 } | |
118 | |
119 } // namespace blink | |
OLD | NEW |