Remove extra line from unit_tests.isolate
[chromium-blink-merge.git] / cc / CCResourceProvider.cpp
blob29ca7d4373d9579af4f36ab5aa3d1681703c54d7
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.
5 #include "config.h"
7 #include "CCResourceProvider.h"
8 #ifdef LOG
9 #undef LOG
10 #endif
12 #include <limits.h>
14 #include "base/debug/alias.h"
15 #include "base/hash_tables.h"
16 #include "base/stl_util.h"
17 #include "base/string_split.h"
18 #include "base/string_util.h"
19 #include "CCProxy.h"
20 #include "CCRendererGL.h" // For the GLC() macro.
21 #include "Extensions3DChromium.h"
22 #include "IntRect.h"
23 #include "LayerTextureSubImage.h"
24 #include "ThrottledTextureUploader.h"
25 #include "UnthrottledTextureUploader.h"
26 #include <public/WebGraphicsContext3D.h>
28 using WebKit::WebGraphicsContext3D;
30 namespace cc {
32 static GC3Denum textureToStorageFormat(GC3Denum textureFormat)
34 GC3Denum storageFormat = Extensions3D::RGBA8_OES;
35 switch (textureFormat) {
36 case GraphicsContext3D::RGBA:
37 break;
38 case Extensions3D::BGRA_EXT:
39 storageFormat = Extensions3DChromium::BGRA8_EXT;
40 break;
41 default:
42 ASSERT_NOT_REACHED();
43 break;
46 return storageFormat;
49 static bool isTextureFormatSupportedForStorage(GC3Denum format)
51 return (format == GraphicsContext3D::RGBA || format == Extensions3D::BGRA_EXT);
54 CCResourceProvider::TransferableResourceList::TransferableResourceList()
58 CCResourceProvider::TransferableResourceList::~TransferableResourceList()
62 CCResourceProvider::Resource::Resource()
63 : glId(0)
64 , pixels(0)
65 , pool(0)
66 , lockForReadCount(0)
67 , lockedForWrite(false)
68 , external(false)
69 , exported(false)
70 , size()
71 , format(0)
72 , type(static_cast<ResourceType>(0))
76 CCResourceProvider::Resource::Resource(unsigned textureId, int pool, const IntSize& size, GC3Denum format)
77 : glId(textureId)
78 , pixels(0)
79 , pool(pool)
80 , lockForReadCount(0)
81 , lockedForWrite(false)
82 , external(false)
83 , exported(false)
84 , size(size)
85 , format(format)
86 , type(GLTexture)
90 CCResourceProvider::Resource::Resource(uint8_t* pixels, int pool, const IntSize& size, GC3Denum format)
91 : glId(0)
92 , pixels(pixels)
93 , pool(pool)
94 , lockForReadCount(0)
95 , lockedForWrite(false)
96 , external(false)
97 , exported(false)
98 , size(size)
99 , format(format)
100 , type(Bitmap)
104 CCResourceProvider::Child::Child()
108 CCResourceProvider::Child::~Child()
112 PassOwnPtr<CCResourceProvider> CCResourceProvider::create(CCGraphicsContext* context)
114 OwnPtr<CCResourceProvider> resourceProvider(adoptPtr(new CCResourceProvider(context)));
115 if (!resourceProvider->initialize())
116 return nullptr;
117 return resourceProvider.release();
120 CCResourceProvider::~CCResourceProvider()
122 WebGraphicsContext3D* context3d = m_context->context3D();
123 if (!context3d || !context3d->makeContextCurrent())
124 return;
125 m_textureUploader.clear();
126 m_textureCopier.clear();
129 WebGraphicsContext3D* CCResourceProvider::graphicsContext3D()
131 ASSERT(CCProxy::isImplThread());
132 return m_context->context3D();
135 bool CCResourceProvider::inUseByConsumer(ResourceId id)
137 ASSERT(CCProxy::isImplThread());
138 ResourceMap::iterator it = m_resources.find(id);
139 CHECK(it != m_resources.end());
140 #if WTF_NEW_HASHMAP_ITERATORS_INTERFACE
141 Resource* resource = &it->value;
142 #else
143 Resource* resource = &it->second;
144 #endif
145 return !!resource->lockForReadCount || resource->exported;
148 CCResourceProvider::ResourceId CCResourceProvider::createResource(int pool, const IntSize& size, GC3Denum format, TextureUsageHint hint)
150 switch (m_defaultResourceType) {
151 case GLTexture:
152 return createGLTexture(pool, size, format, hint);
153 case Bitmap:
154 ASSERT(format == GraphicsContext3D::RGBA);
155 return createBitmap(pool, size);
158 CRASH();
159 return 0;
162 CCResourceProvider::ResourceId CCResourceProvider::createGLTexture(int pool, const IntSize& size, GC3Denum format, TextureUsageHint hint)
164 ASSERT(CCProxy::isImplThread());
165 unsigned textureId = 0;
166 WebGraphicsContext3D* context3d = m_context->context3D();
167 ASSERT(context3d);
168 GLC(context3d, textureId = context3d->createTexture());
169 GLC(context3d, context3d->bindTexture(GraphicsContext3D::TEXTURE_2D, textureId));
170 GLC(context3d, context3d->texParameteri(GraphicsContext3D::TEXTURE_2D, GraphicsContext3D::TEXTURE_MIN_FILTER, GraphicsContext3D::LINEAR));
171 GLC(context3d, context3d->texParameteri(GraphicsContext3D::TEXTURE_2D, GraphicsContext3D::TEXTURE_MAG_FILTER, GraphicsContext3D::LINEAR));
172 GLC(context3d, context3d->texParameteri(GraphicsContext3D::TEXTURE_2D, GraphicsContext3D::TEXTURE_WRAP_S, GraphicsContext3D::CLAMP_TO_EDGE));
173 GLC(context3d, context3d->texParameteri(GraphicsContext3D::TEXTURE_2D, GraphicsContext3D::TEXTURE_WRAP_T, GraphicsContext3D::CLAMP_TO_EDGE));
175 if (m_useTextureUsageHint && hint == TextureUsageFramebuffer)
176 GLC(context3d, context3d->texParameteri(GraphicsContext3D::TEXTURE_2D, Extensions3DChromium::GL_TEXTURE_USAGE_ANGLE, Extensions3DChromium::GL_FRAMEBUFFER_ATTACHMENT_ANGLE));
177 if (m_useTextureStorageExt && isTextureFormatSupportedForStorage(format)) {
178 GC3Denum storageFormat = textureToStorageFormat(format);
179 GLC(context3d, context3d->texStorage2DEXT(GraphicsContext3D::TEXTURE_2D, 1, storageFormat, size.width(), size.height()));
180 } else
181 GLC(context3d, context3d->texImage2D(GraphicsContext3D::TEXTURE_2D, 0, format, size.width(), size.height(), 0, format, GraphicsContext3D::UNSIGNED_BYTE, 0));
182 ResourceId id = m_nextId++;
183 Resource resource(textureId, pool, size, format);
184 m_resources.add(id, resource);
185 return id;
188 CCResourceProvider::ResourceId CCResourceProvider::createBitmap(int pool, const IntSize& size)
190 ASSERT(CCProxy::isImplThread());
192 uint8_t* pixels = new uint8_t[size.width() * size.height() * 4];
194 ResourceId id = m_nextId++;
195 Resource resource(pixels, pool, size, GraphicsContext3D::RGBA);
196 m_resources.add(id, resource);
197 return id;
200 CCResourceProvider::ResourceId CCResourceProvider::createResourceFromExternalTexture(unsigned textureId)
202 ASSERT(CCProxy::isImplThread());
203 ASSERT(m_context->context3D());
204 ResourceId id = m_nextId++;
205 Resource resource(textureId, 0, IntSize(), 0);
206 resource.external = true;
207 m_resources.add(id, resource);
208 return id;
211 void CCResourceProvider::deleteResource(ResourceId id)
213 ASSERT(CCProxy::isImplThread());
214 ResourceMap::iterator it = m_resources.find(id);
215 CHECK(it != m_resources.end());
216 #if WTF_NEW_HASHMAP_ITERATORS_INTERFACE
217 Resource* resource = &it->value;
218 #else
219 Resource* resource = &it->second;
220 #endif
221 ASSERT(!resource->lockedForWrite);
222 ASSERT(!resource->lockForReadCount);
224 if (resource->glId && !resource->external) {
225 WebGraphicsContext3D* context3d = m_context->context3D();
226 ASSERT(context3d);
227 GLC(context3d, context3d->deleteTexture(resource->glId));
229 if (resource->pixels)
230 delete resource->pixels;
232 m_resources.remove(it);
235 void CCResourceProvider::deleteOwnedResources(int pool)
237 ASSERT(CCProxy::isImplThread());
238 ResourceIdArray toDelete;
239 for (ResourceMap::iterator it = m_resources.begin(); it != m_resources.end(); ++it) {
240 #if WTF_NEW_HASHMAP_ITERATORS_INTERFACE
241 if (it->value.pool == pool && !it->value.external)
242 toDelete.append(it->key);
243 #else
244 if (it->second.pool == pool && !it->second.external)
245 toDelete.append(it->first);
246 #endif
248 for (ResourceIdArray::iterator it = toDelete.begin(); it != toDelete.end(); ++it)
249 deleteResource(*it);
252 CCResourceProvider::ResourceType CCResourceProvider::resourceType(ResourceId id)
254 ResourceMap::iterator it = m_resources.find(id);
255 CHECK(it != m_resources.end());
256 #if WTF_NEW_HASHMAP_ITERATORS_INTERFACE
257 Resource* resource = &it->value;
258 #else
259 Resource* resource = &it->second;
260 #endif
261 return resource->type;
264 void CCResourceProvider::upload(ResourceId id, const uint8_t* image, const IntRect& imageRect, const IntRect& sourceRect, const IntSize& destOffset)
266 ASSERT(CCProxy::isImplThread());
267 ResourceMap::iterator it = m_resources.find(id);
268 CHECK(it != m_resources.end());
269 #if WTF_NEW_HASHMAP_ITERATORS_INTERFACE
270 Resource* resource = &it->value;
271 #else
272 Resource* resource = &it->second;
273 #endif
274 ASSERT(!resource->lockedForWrite);
275 ASSERT(!resource->lockForReadCount);
276 ASSERT(!resource->external);
278 if (resource->glId) {
279 WebGraphicsContext3D* context3d = m_context->context3D();
280 ASSERT(context3d);
281 ASSERT(m_texSubImage.get());
282 context3d->bindTexture(GraphicsContext3D::TEXTURE_2D, resource->glId);
283 m_texSubImage->upload(image, imageRect, sourceRect, destOffset, resource->format, context3d);
286 if (resource->pixels) {
287 SkBitmap srcFull;
288 srcFull.setConfig(SkBitmap::kARGB_8888_Config, imageRect.width(), imageRect.height());
289 srcFull.setPixels(const_cast<uint8_t*>(image));
290 SkBitmap srcSubset;
291 SkIRect skSourceRect = SkIRect::MakeXYWH(sourceRect.x(), sourceRect.y(), sourceRect.width(), sourceRect.height());
292 skSourceRect.offset(-imageRect.x(), -imageRect.y());
293 srcFull.extractSubset(&srcSubset, skSourceRect);
295 ScopedWriteLockSoftware lock(this, id);
296 SkCanvas* dest = lock.skCanvas();
297 dest->writePixels(srcSubset, destOffset.width(), destOffset.height());
301 void CCResourceProvider::flush()
303 ASSERT(CCProxy::isImplThread());
304 WebGraphicsContext3D* context3d = m_context->context3D();
305 if (context3d)
306 context3d->flush();
309 bool CCResourceProvider::shallowFlushIfSupported()
311 ASSERT(CCProxy::isImplThread());
312 WebGraphicsContext3D* context3d = m_context->context3D();
313 if (!context3d || !m_useShallowFlush)
314 return false;
316 context3d->shallowFlushCHROMIUM();
317 return true;
320 const CCResourceProvider::Resource* CCResourceProvider::lockForRead(ResourceId id)
322 ASSERT(CCProxy::isImplThread());
323 ResourceMap::iterator it = m_resources.find(id);
324 CHECK(it != m_resources.end());
326 #if WTF_NEW_HASHMAP_ITERATORS_INTERFACE
327 Resource* resource = &it->value;
328 #else
329 Resource* resource = &it->second;
330 #endif
331 ASSERT(!resource->lockedForWrite);
332 resource->lockForReadCount++;
333 return resource;
336 void CCResourceProvider::unlockForRead(ResourceId id)
338 ASSERT(CCProxy::isImplThread());
339 ResourceMap::iterator it = m_resources.find(id);
340 CHECK(it != m_resources.end());
341 #if WTF_NEW_HASHMAP_ITERATORS_INTERFACE
342 Resource* resource = &it->value;
343 #else
344 Resource* resource = &it->second;
345 #endif
346 ASSERT(resource->lockForReadCount > 0);
347 resource->lockForReadCount--;
350 const CCResourceProvider::Resource* CCResourceProvider::lockForWrite(ResourceId id)
352 ASSERT(CCProxy::isImplThread());
353 ResourceMap::iterator it = m_resources.find(id);
354 CHECK(it != m_resources.end());
355 #if WTF_NEW_HASHMAP_ITERATORS_INTERFACE
356 Resource* resource = &it->value;
357 #else
358 Resource* resource = &it->second;
359 #endif
360 ASSERT(!resource->lockedForWrite);
361 ASSERT(!resource->lockForReadCount);
362 ASSERT(!resource->external);
363 resource->lockedForWrite = true;
364 return resource;
367 void CCResourceProvider::unlockForWrite(ResourceId id)
369 ASSERT(CCProxy::isImplThread());
370 ResourceMap::iterator it = m_resources.find(id);
371 CHECK(it != m_resources.end());
372 #if WTF_NEW_HASHMAP_ITERATORS_INTERFACE
373 Resource* resource = &it->value;
374 #else
375 Resource* resource = &it->second;
376 #endif
377 ASSERT(resource->lockedForWrite);
378 ASSERT(!resource->external);
379 resource->lockedForWrite = false;
382 CCResourceProvider::ScopedReadLockGL::ScopedReadLockGL(CCResourceProvider* resourceProvider, CCResourceProvider::ResourceId resourceId)
383 : m_resourceProvider(resourceProvider)
384 , m_resourceId(resourceId)
385 , m_textureId(resourceProvider->lockForRead(resourceId)->glId)
387 ASSERT(m_textureId);
390 CCResourceProvider::ScopedReadLockGL::~ScopedReadLockGL()
392 m_resourceProvider->unlockForRead(m_resourceId);
395 CCResourceProvider::ScopedWriteLockGL::ScopedWriteLockGL(CCResourceProvider* resourceProvider, CCResourceProvider::ResourceId resourceId)
396 : m_resourceProvider(resourceProvider)
397 , m_resourceId(resourceId)
398 , m_textureId(resourceProvider->lockForWrite(resourceId)->glId)
400 ASSERT(m_textureId);
403 CCResourceProvider::ScopedWriteLockGL::~ScopedWriteLockGL()
405 m_resourceProvider->unlockForWrite(m_resourceId);
408 void CCResourceProvider::populateSkBitmapWithResource(SkBitmap* skBitmap, const Resource* resource)
410 ASSERT(resource->pixels);
411 ASSERT(resource->format == GraphicsContext3D::RGBA);
412 skBitmap->setConfig(SkBitmap::kARGB_8888_Config, resource->size.width(), resource->size.height());
413 skBitmap->setPixels(resource->pixels);
416 CCResourceProvider::ScopedReadLockSoftware::ScopedReadLockSoftware(CCResourceProvider* resourceProvider, CCResourceProvider::ResourceId resourceId)
417 : m_resourceProvider(resourceProvider)
418 , m_resourceId(resourceId)
420 CCResourceProvider::populateSkBitmapWithResource(&m_skBitmap, resourceProvider->lockForRead(resourceId));
423 CCResourceProvider::ScopedReadLockSoftware::~ScopedReadLockSoftware()
425 m_resourceProvider->unlockForRead(m_resourceId);
428 CCResourceProvider::ScopedWriteLockSoftware::ScopedWriteLockSoftware(CCResourceProvider* resourceProvider, CCResourceProvider::ResourceId resourceId)
429 : m_resourceProvider(resourceProvider)
430 , m_resourceId(resourceId)
432 CCResourceProvider::populateSkBitmapWithResource(&m_skBitmap, resourceProvider->lockForWrite(resourceId));
433 m_skCanvas = adoptPtr(new SkCanvas(m_skBitmap));
436 CCResourceProvider::ScopedWriteLockSoftware::~ScopedWriteLockSoftware()
438 m_resourceProvider->unlockForWrite(m_resourceId);
441 CCResourceProvider::CCResourceProvider(CCGraphicsContext* context)
442 : m_context(context)
443 , m_nextId(1)
444 , m_nextChild(1)
445 , m_defaultResourceType(GLTexture)
446 , m_useTextureStorageExt(false)
447 , m_useTextureUsageHint(false)
448 , m_useShallowFlush(false)
449 , m_maxTextureSize(0)
453 bool CCResourceProvider::initialize()
455 ASSERT(CCProxy::isImplThread());
456 WebGraphicsContext3D* context3d = m_context->context3D();
457 if (!context3d) {
458 m_maxTextureSize = INT_MAX / 2;
459 m_textureUploader = UnthrottledTextureUploader::create();
460 return true;
462 if (!context3d->makeContextCurrent())
463 return false;
465 std::string extensionsString = UTF16ToASCII(context3d->getString(GraphicsContext3D::EXTENSIONS));
466 std::vector<std::string> extensions;
467 base::SplitString(extensionsString, ' ', &extensions);
468 bool useMapSub = false;
469 bool useBindUniform = false;
470 for (size_t i = 0; i < extensions.size(); ++i) {
471 if (extensions[i] == "GL_EXT_texture_storage")
472 m_useTextureStorageExt = true;
473 else if (extensions[i] == "GL_ANGLE_texture_usage")
474 m_useTextureUsageHint = true;
475 else if (extensions[i] == "GL_CHROMIUM_map_sub")
476 useMapSub = true;
477 else if (extensions[i] == "GL_CHROMIUM_shallow_flush")
478 m_useShallowFlush = true;
479 else if (extensions[i] == "GL_CHROMIUM_bind_uniform_location")
480 useBindUniform = true;
483 m_texSubImage = adoptPtr(new LayerTextureSubImage(useMapSub));
484 m_textureCopier = AcceleratedTextureCopier::create(context3d, useBindUniform);
486 m_textureUploader = ThrottledTextureUploader::create(context3d);
487 GLC(context3d, context3d->getIntegerv(GraphicsContext3D::MAX_TEXTURE_SIZE, &m_maxTextureSize));
488 return true;
491 int CCResourceProvider::createChild(int pool)
493 ASSERT(CCProxy::isImplThread());
494 Child childInfo;
495 childInfo.pool = pool;
496 int child = m_nextChild++;
497 m_children.add(child, childInfo);
498 return child;
501 void CCResourceProvider::destroyChild(int child)
503 ASSERT(CCProxy::isImplThread());
504 ChildMap::iterator it = m_children.find(child);
505 ASSERT(it != m_children.end());
506 #if WTF_NEW_HASHMAP_ITERATORS_INTERFACE
507 deleteOwnedResources(it->value.pool);
508 #else
509 deleteOwnedResources(it->second.pool);
510 #endif
511 m_children.remove(it);
512 trimMailboxDeque();
515 const CCResourceProvider::ResourceIdMap& CCResourceProvider::getChildToParentMap(int child) const
517 ASSERT(CCProxy::isImplThread());
518 ChildMap::const_iterator it = m_children.find(child);
519 ASSERT(it != m_children.end());
520 #if WTF_NEW_HASHMAP_ITERATORS_INTERFACE
521 return it->value.childToParentMap;
522 #else
523 return it->second.childToParentMap;
524 #endif
527 CCResourceProvider::TransferableResourceList CCResourceProvider::prepareSendToParent(const ResourceIdArray& resources)
529 ASSERT(CCProxy::isImplThread());
530 TransferableResourceList list;
531 list.syncPoint = 0;
532 WebGraphicsContext3D* context3d = m_context->context3D();
533 if (!context3d || !context3d->makeContextCurrent()) {
534 // FIXME: Implement this path for software compositing.
535 return list;
537 for (ResourceIdArray::const_iterator it = resources.begin(); it != resources.end(); ++it) {
538 TransferableResource resource;
539 if (transferResource(context3d, *it, &resource)) {
540 #if WTF_NEW_HASHMAP_ITERATORS_INTERFACE
541 m_resources.find(*it)->value.exported = true;
542 #else
543 m_resources.find(*it)->second.exported = true;
544 #endif
545 list.resources.append(resource);
548 if (list.resources.size())
549 list.syncPoint = context3d->insertSyncPoint();
550 return list;
553 CCResourceProvider::TransferableResourceList CCResourceProvider::prepareSendToChild(int child, const ResourceIdArray& resources)
555 ASSERT(CCProxy::isImplThread());
556 TransferableResourceList list;
557 list.syncPoint = 0;
558 WebGraphicsContext3D* context3d = m_context->context3D();
559 if (!context3d || !context3d->makeContextCurrent()) {
560 // FIXME: Implement this path for software compositing.
561 return list;
563 #if WTF_NEW_HASHMAP_ITERATORS_INTERFACE
564 Child& childInfo = m_children.find(child)->value;
565 #else
566 Child& childInfo = m_children.find(child)->second;
567 #endif
568 for (ResourceIdArray::const_iterator it = resources.begin(); it != resources.end(); ++it) {
569 TransferableResource resource;
570 if (!transferResource(context3d, *it, &resource))
571 ASSERT_NOT_REACHED();
572 resource.id = childInfo.parentToChildMap.get(*it);
573 childInfo.parentToChildMap.remove(*it);
574 childInfo.childToParentMap.remove(resource.id);
575 list.resources.append(resource);
576 deleteResource(*it);
578 if (list.resources.size())
579 list.syncPoint = context3d->insertSyncPoint();
580 return list;
583 void CCResourceProvider::receiveFromChild(int child, const TransferableResourceList& resources)
585 ASSERT(CCProxy::isImplThread());
586 WebGraphicsContext3D* context3d = m_context->context3D();
587 if (!context3d || !context3d->makeContextCurrent()) {
588 // FIXME: Implement this path for software compositing.
589 return;
591 if (resources.syncPoint) {
592 // NOTE: If the parent is a browser and the child a renderer, the parent
593 // is not supposed to have its context wait, because that could induce
594 // deadlocks and/or security issues. The caller is responsible for
595 // waiting asynchronously, and resetting syncPoint before calling this.
596 // However if the parent is a renderer (e.g. browser tag), it may be ok
597 // (and is simpler) to wait.
598 GLC(context3d, context3d->waitSyncPoint(resources.syncPoint));
600 #if WTF_NEW_HASHMAP_ITERATORS_INTERFACE
601 Child& childInfo = m_children.find(child)->value;
602 #else
603 Child& childInfo = m_children.find(child)->second;
604 #endif
605 for (Vector<TransferableResource>::const_iterator it = resources.resources.begin(); it != resources.resources.end(); ++it) {
606 unsigned textureId;
607 GLC(context3d, textureId = context3d->createTexture());
608 GLC(context3d, context3d->bindTexture(GraphicsContext3D::TEXTURE_2D, textureId));
609 GLC(context3d, context3d->consumeTextureCHROMIUM(GraphicsContext3D::TEXTURE_2D, it->mailbox.name));
610 ResourceId id = m_nextId++;
611 Resource resource(textureId, childInfo.pool, it->size, it->format);
612 m_resources.add(id, resource);
613 m_mailboxes.append(it->mailbox);
614 childInfo.parentToChildMap.add(id, it->id);
615 childInfo.childToParentMap.add(it->id, id);
619 void CCResourceProvider::receiveFromParent(const TransferableResourceList& resources)
621 ASSERT(CCProxy::isImplThread());
622 WebGraphicsContext3D* context3d = m_context->context3D();
623 if (!context3d || !context3d->makeContextCurrent()) {
624 // FIXME: Implement this path for software compositing.
625 return;
627 if (resources.syncPoint)
628 GLC(context3d, context3d->waitSyncPoint(resources.syncPoint));
629 for (Vector<TransferableResource>::const_iterator it = resources.resources.begin(); it != resources.resources.end(); ++it) {
630 #if WTF_NEW_HASHMAP_ITERATORS_INTERFACE
631 Resource* resource = &m_resources.find(it->id)->value;
632 #else
633 Resource* resource = &m_resources.find(it->id)->second;
634 #endif
635 ASSERT(resource->exported);
636 resource->exported = false;
637 GLC(context3d, context3d->bindTexture(GraphicsContext3D::TEXTURE_2D, resource->glId));
638 GLC(context3d, context3d->consumeTextureCHROMIUM(GraphicsContext3D::TEXTURE_2D, it->mailbox.name));
639 m_mailboxes.append(it->mailbox);
643 bool CCResourceProvider::transferResource(WebGraphicsContext3D* context, ResourceId id, TransferableResource* resource)
645 ASSERT(CCProxy::isImplThread());
646 ResourceMap::const_iterator it = m_resources.find(id);
647 CHECK(it != m_resources.end());
648 #if WTF_NEW_HASHMAP_ITERATORS_INTERFACE
649 const Resource* source = &it->value;
650 #else
651 const Resource* source = &it->second;
652 #endif
653 ASSERT(!source->lockedForWrite);
654 ASSERT(!source->lockForReadCount);
655 ASSERT(!source->external);
656 if (source->exported)
657 return false;
658 resource->id = id;
659 resource->format = source->format;
660 resource->size = source->size;
661 if (!m_mailboxes.isEmpty())
662 resource->mailbox = m_mailboxes.takeFirst();
663 else
664 GLC(context, context->genMailboxCHROMIUM(resource->mailbox.name));
665 GLC(context, context->bindTexture(GraphicsContext3D::TEXTURE_2D, source->glId));
666 GLC(context, context->produceTextureCHROMIUM(GraphicsContext3D::TEXTURE_2D, resource->mailbox.name));
667 return true;
670 void CCResourceProvider::trimMailboxDeque()
672 // Trim the mailbox deque to the maximum number of resources we may need to
673 // send.
674 // If we have a parent, any non-external resource not already transfered is
675 // eligible to be sent to the parent. Otherwise, all resources belonging to
676 // a child might need to be sent back to the child.
677 size_t maxMailboxCount = 0;
678 if (m_context->capabilities().hasParentCompositor) {
679 for (ResourceMap::iterator it = m_resources.begin(); it != m_resources.end(); ++it) {
680 #if WTF_NEW_HASHMAP_ITERATORS_INTERFACE
681 if (!it->value.exported && !it->value.external)
682 #else
683 if (!it->second.exported && !it->second.external)
684 #endif
685 ++maxMailboxCount;
687 } else {
688 base::hash_set<int> childPoolSet;
689 for (ChildMap::iterator it = m_children.begin(); it != m_children.end(); ++it)
690 #if WTF_NEW_HASHMAP_ITERATORS_INTERFACE
691 childPoolSet.insert(it->value.pool);
692 #else
693 childPoolSet.insert(it->second.pool);
694 #endif
695 for (ResourceMap::iterator it = m_resources.begin(); it != m_resources.end(); ++it) {
696 #if WTF_NEW_HASHMAP_ITERATORS_INTERFACE
697 if (ContainsKey(childPoolSet, it->value.pool))
698 #else
699 if (ContainsKey(childPoolSet, it->second.pool))
700 #endif
701 ++maxMailboxCount;
704 while (m_mailboxes.size() > maxMailboxCount)
705 m_mailboxes.removeFirst();