| OLD | NEW |
| (Empty) |
| 1 // Copyright 2012 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "cc/prioritized_resource.h" | |
| 6 | |
| 7 #include <algorithm> | |
| 8 | |
| 9 #include "cc/platform_color.h" | |
| 10 #include "cc/prioritized_resource_manager.h" | |
| 11 #include "cc/priority_calculator.h" | |
| 12 #include "cc/trees/proxy.h" | |
| 13 | |
| 14 namespace cc { | |
| 15 | |
| 16 PrioritizedResource::PrioritizedResource(PrioritizedResourceManager* manager, gf
x::Size size, GLenum format) | |
| 17 : m_size(size) | |
| 18 , m_format(format) | |
| 19 , m_bytes(0) | |
| 20 , m_contentsSwizzled(false) | |
| 21 , m_priority(PriorityCalculator::LowestPriority()) | |
| 22 , m_isAbovePriorityCutoff(false) | |
| 23 , m_isSelfManaged(false) | |
| 24 , m_backing(0) | |
| 25 , m_manager(0) | |
| 26 { | |
| 27 // m_manager is set in registerTexture() so validity can be checked. | |
| 28 DCHECK(format || size.IsEmpty()); | |
| 29 if (format) | |
| 30 m_bytes = Resource::MemorySizeBytes(size, format); | |
| 31 if (manager) | |
| 32 manager->registerTexture(this); | |
| 33 } | |
| 34 | |
| 35 PrioritizedResource::~PrioritizedResource() | |
| 36 { | |
| 37 if (m_manager) | |
| 38 m_manager->unregisterTexture(this); | |
| 39 } | |
| 40 | |
| 41 void PrioritizedResource::setTextureManager(PrioritizedResourceManager* manager) | |
| 42 { | |
| 43 if (m_manager == manager) | |
| 44 return; | |
| 45 if (m_manager) | |
| 46 m_manager->unregisterTexture(this); | |
| 47 if (manager) | |
| 48 manager->registerTexture(this); | |
| 49 } | |
| 50 | |
| 51 void PrioritizedResource::setDimensions(gfx::Size size, GLenum format) | |
| 52 { | |
| 53 if (m_format != format || m_size != size) { | |
| 54 m_isAbovePriorityCutoff = false; | |
| 55 m_format = format; | |
| 56 m_size = size; | |
| 57 m_bytes = Resource::MemorySizeBytes(size, format); | |
| 58 DCHECK(m_manager || !m_backing); | |
| 59 if (m_manager) | |
| 60 m_manager->returnBackingTexture(this); | |
| 61 } | |
| 62 } | |
| 63 | |
| 64 bool PrioritizedResource::requestLate() | |
| 65 { | |
| 66 if (!m_manager) | |
| 67 return false; | |
| 68 return m_manager->requestLate(this); | |
| 69 } | |
| 70 | |
| 71 bool PrioritizedResource::backingResourceWasEvicted() const | |
| 72 { | |
| 73 return m_backing ? m_backing->resourceHasBeenDeleted() : false; | |
| 74 } | |
| 75 | |
| 76 void PrioritizedResource::acquireBackingTexture(ResourceProvider* resourceProvid
er) | |
| 77 { | |
| 78 DCHECK(m_isAbovePriorityCutoff); | |
| 79 if (m_isAbovePriorityCutoff) | |
| 80 m_manager->acquireBackingTextureIfNeeded(this, resourceProvider); | |
| 81 } | |
| 82 | |
| 83 ResourceProvider::ResourceId PrioritizedResource::resourceId() const | |
| 84 { | |
| 85 if (m_backing) | |
| 86 return m_backing->id(); | |
| 87 return 0; | |
| 88 } | |
| 89 | |
| 90 void PrioritizedResource::setPixels(ResourceProvider* resourceProvider, | |
| 91 const uint8_t* image, const gfx::Rect& imageR
ect, | |
| 92 const gfx::Rect& sourceRect, const gfx::Vecto
r2d& destOffset) | |
| 93 { | |
| 94 DCHECK(m_isAbovePriorityCutoff); | |
| 95 if (m_isAbovePriorityCutoff) | |
| 96 acquireBackingTexture(resourceProvider); | |
| 97 DCHECK(m_backing); | |
| 98 resourceProvider->SetPixels(resourceId(), image, imageRect, sourceRect, dest
Offset); | |
| 99 | |
| 100 // The component order may be bgra if we uploaded bgra pixels to rgba | |
| 101 // texture. Mark contents as swizzled if image component order is | |
| 102 // different than texture format. | |
| 103 m_contentsSwizzled = !PlatformColor::SameComponentOrder(m_format); | |
| 104 } | |
| 105 | |
| 106 void PrioritizedResource::link(Backing* backing) | |
| 107 { | |
| 108 DCHECK(backing); | |
| 109 DCHECK(!backing->m_owner); | |
| 110 DCHECK(!m_backing); | |
| 111 | |
| 112 m_backing = backing; | |
| 113 m_backing->m_owner = this; | |
| 114 } | |
| 115 | |
| 116 void PrioritizedResource::unlink() | |
| 117 { | |
| 118 DCHECK(m_backing); | |
| 119 DCHECK(m_backing->m_owner == this); | |
| 120 | |
| 121 m_backing->m_owner = 0; | |
| 122 m_backing = 0; | |
| 123 } | |
| 124 | |
| 125 void PrioritizedResource::setToSelfManagedMemoryPlaceholder(size_t bytes) | |
| 126 { | |
| 127 setDimensions(gfx::Size(), GL_RGBA); | |
| 128 setIsSelfManaged(true); | |
| 129 m_bytes = bytes; | |
| 130 } | |
| 131 | |
| 132 PrioritizedResource::Backing::Backing(unsigned id, ResourceProvider* resourcePro
vider, gfx::Size size, GLenum format) | |
| 133 : Resource(id, size, format) | |
| 134 , m_owner(0) | |
| 135 , m_priorityAtLastPriorityUpdate(PriorityCalculator::LowestPriority()) | |
| 136 , m_wasAbovePriorityCutoffAtLastPriorityUpdate(false) | |
| 137 , m_inDrawingImplTree(false) | |
| 138 , m_resourceHasBeenDeleted(false) | |
| 139 #ifndef NDEBUG | |
| 140 , m_resourceProvider(resourceProvider) | |
| 141 #endif | |
| 142 { | |
| 143 } | |
| 144 | |
| 145 PrioritizedResource::Backing::~Backing() | |
| 146 { | |
| 147 DCHECK(!m_owner); | |
| 148 DCHECK(m_resourceHasBeenDeleted); | |
| 149 } | |
| 150 | |
| 151 void PrioritizedResource::Backing::deleteResource(ResourceProvider* resourceProv
ider) | |
| 152 { | |
| 153 DCHECK(!proxy() || proxy()->IsImplThread()); | |
| 154 DCHECK(!m_resourceHasBeenDeleted); | |
| 155 #ifndef NDEBUG | |
| 156 DCHECK(resourceProvider == m_resourceProvider); | |
| 157 #endif | |
| 158 | |
| 159 resourceProvider->DeleteResource(id()); | |
| 160 set_id(0); | |
| 161 m_resourceHasBeenDeleted = true; | |
| 162 } | |
| 163 | |
| 164 bool PrioritizedResource::Backing::resourceHasBeenDeleted() const | |
| 165 { | |
| 166 DCHECK(!proxy() || proxy()->IsImplThread()); | |
| 167 return m_resourceHasBeenDeleted; | |
| 168 } | |
| 169 | |
| 170 bool PrioritizedResource::Backing::canBeRecycled() const | |
| 171 { | |
| 172 DCHECK(!proxy() || proxy()->IsImplThread()); | |
| 173 return !m_wasAbovePriorityCutoffAtLastPriorityUpdate && !m_inDrawingImplTree
; | |
| 174 } | |
| 175 | |
| 176 void PrioritizedResource::Backing::updatePriority() | |
| 177 { | |
| 178 DCHECK(!proxy() || proxy()->IsImplThread() && proxy()->IsMainThreadBlocked()
); | |
| 179 if (m_owner) { | |
| 180 m_priorityAtLastPriorityUpdate = m_owner->requestPriority(); | |
| 181 m_wasAbovePriorityCutoffAtLastPriorityUpdate = m_owner->isAbovePriorityC
utoff(); | |
| 182 } else { | |
| 183 m_priorityAtLastPriorityUpdate = PriorityCalculator::LowestPriority(); | |
| 184 m_wasAbovePriorityCutoffAtLastPriorityUpdate = false; | |
| 185 } | |
| 186 } | |
| 187 | |
| 188 void PrioritizedResource::Backing::updateInDrawingImplTree() | |
| 189 { | |
| 190 DCHECK(!proxy() || proxy()->IsImplThread() && proxy()->IsMainThreadBlocked()
); | |
| 191 m_inDrawingImplTree = !!owner(); | |
| 192 if (!m_inDrawingImplTree) | |
| 193 DCHECK_EQ(m_priorityAtLastPriorityUpdate, PriorityCalculator::LowestPrio
rity()); | |
| 194 } | |
| 195 | |
| 196 void PrioritizedResource::returnBackingTexture() | |
| 197 { | |
| 198 DCHECK(m_manager || !m_backing); | |
| 199 if (m_manager) | |
| 200 m_manager->returnBackingTexture(this); | |
| 201 } | |
| 202 | |
| 203 const Proxy* PrioritizedResource::Backing::proxy() const | |
| 204 { | |
| 205 if (!m_owner || !m_owner->resourceManager()) | |
| 206 return 0; | |
| 207 return m_owner->resourceManager()->proxyForDebug(); | |
| 208 } | |
| 209 | |
| 210 } // namespace cc | |
| OLD | NEW |