OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright (C) 2012 Google Inc. All rights reserved. | 2 * Copyright (C) 2012 Google Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions | 5 * modification, are permitted provided that the following conditions |
6 * are met: | 6 * are met: |
7 * | 7 * |
8 * 1. Redistributions of source code must retain the above copyright | 8 * 1. Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
10 * 2. Redistributions in binary form must reproduce the above copyright | 10 * 2. Redistributions in binary form must reproduce the above copyright |
(...skipping 406 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
417 if (image->uniqueID() == m_lastImageId) | 417 if (image->uniqueID() == m_lastImageId) |
418 return false; | 418 return false; |
419 m_lastImageId = image->uniqueID(); | 419 m_lastImageId = image->uniqueID(); |
420 | 420 |
421 MailboxInfo* mailboxInfo = createMailboxInfo(); | 421 MailboxInfo* mailboxInfo = createMailboxInfo(); |
422 mailboxInfo->m_status = MailboxInUse; | 422 mailboxInfo->m_status = MailboxInUse; |
423 mailboxInfo->m_image = image; | 423 mailboxInfo->m_image = image; |
424 | 424 |
425 ASSERT(mailboxInfo->m_mailbox.syncPoint == 0); | 425 ASSERT(mailboxInfo->m_mailbox.syncPoint == 0); |
426 ASSERT(mailboxInfo->m_image.get()); | 426 ASSERT(mailboxInfo->m_image.get()); |
427 | |
428 // set m_parentLayerBridge to make sure 'this' stays alive as long as it has | |
429 // live mailboxes | |
430 ASSERT(!mailboxInfo->m_parentLayerBridge); | |
431 mailboxInfo->m_parentLayerBridge = this; | |
432 *outMailbox = mailboxInfo->m_mailbox; | |
433 | |
434 GrContext* grContext = m_contextProvider->grContext(); | |
435 if (!grContext) | |
436 return true; // for testing: skip gl stuff when using a mock graphics co ntext. | |
437 | |
427 ASSERT(mailboxInfo->m_image->getTexture()); | 438 ASSERT(mailboxInfo->m_image->getTexture()); |
428 | 439 |
429 // Because of texture sharing with the compositor, we must invalidate | 440 // Because of texture sharing with the compositor, we must invalidate |
430 // the state cached in skia so that the deferred copy on write | 441 // the state cached in skia so that the deferred copy on write |
431 // in SkSurface_Gpu does not make any false assumptions. | 442 // in SkSurface_Gpu does not make any false assumptions. |
432 mailboxInfo->m_image->getTexture()->textureParamsModified(); | 443 mailboxInfo->m_image->getTexture()->textureParamsModified(); |
433 | 444 |
434 webContext->bindTexture(GL_TEXTURE_2D, mailboxInfo->m_image->getTexture()->g etTextureHandle()); | 445 webContext->bindTexture(GL_TEXTURE_2D, mailboxInfo->m_image->getTexture()->g etTextureHandle()); |
435 webContext->texParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); | 446 webContext->texParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
436 webContext->texParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); | 447 webContext->texParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
437 webContext->texParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE ); | 448 webContext->texParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE ); |
438 webContext->texParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE ); | 449 webContext->texParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE ); |
439 webContext->produceTextureCHROMIUM(GL_TEXTURE_2D, mailboxInfo->m_mailbox.nam e); | 450 webContext->produceTextureCHROMIUM(GL_TEXTURE_2D, mailboxInfo->m_mailbox.nam e); |
440 if (isHidden()) { | 451 if (isHidden()) { |
441 // With hidden canvases, we release the SkImage immediately because | 452 // With hidden canvases, we release the SkImage immediately because |
442 // there is no need for animations to be double buffered. | 453 // there is no need for animations to be double buffered. |
443 mailboxInfo->m_image.clear(); | 454 mailboxInfo->m_image.clear(); |
444 } else { | 455 } else { |
445 webContext->flush(); | 456 webContext->flush(); |
446 mailboxInfo->m_mailbox.syncPoint = webContext->insertSyncPoint(); | 457 mailboxInfo->m_mailbox.syncPoint = webContext->insertSyncPoint(); |
447 } | 458 } |
448 webContext->bindTexture(GL_TEXTURE_2D, 0); | 459 webContext->bindTexture(GL_TEXTURE_2D, 0); |
449 // Because we are changing the texture binding without going through skia, | 460 // Because we are changing the texture binding without going through skia, |
450 // we must dirty the context. | 461 // we must dirty the context. |
451 m_contextProvider->grContext()->resetContext(kTextureBinding_GrGLBackendStat e); | 462 grContext->resetContext(kTextureBinding_GrGLBackendState); |
452 | |
453 // set m_parentLayerBridge to make sure 'this' stays alive as long as it has | |
454 // live mailboxes | |
455 ASSERT(!mailboxInfo->m_parentLayerBridge); | |
456 mailboxInfo->m_parentLayerBridge = this; | |
457 *outMailbox = mailboxInfo->m_mailbox; | |
458 | 463 |
459 return true; | 464 return true; |
460 } | 465 } |
461 | 466 |
462 Canvas2DLayerBridge::MailboxInfo* Canvas2DLayerBridge::createMailboxInfo() { | 467 Canvas2DLayerBridge::MailboxInfo* Canvas2DLayerBridge::createMailboxInfo() { |
463 ASSERT(!m_destructionInProgress); | 468 ASSERT(!m_destructionInProgress); |
464 MailboxInfo* mailboxInfo; | 469 MailboxInfo* mailboxInfo; |
465 for (mailboxInfo = m_mailboxes.begin(); mailboxInfo < m_mailboxes.end(); mai lboxInfo++) { | 470 for (mailboxInfo = m_mailboxes.begin(); mailboxInfo < m_mailboxes.end(); mai lboxInfo++) { |
466 if (mailboxInfo->m_status == MailboxAvailable) { | 471 if (mailboxInfo->m_status == MailboxAvailable) { |
467 return mailboxInfo; | 472 return mailboxInfo; |
(...skipping 30 matching lines...) Expand all Loading... | |
498 // No need to clean up the mailbox resource, but make sure the | 503 // No need to clean up the mailbox resource, but make sure the |
499 // mailbox can also be reusable once the context is restored. | 504 // mailbox can also be reusable once the context is restored. |
500 mailboxInfo->m_status = MailboxAvailable; | 505 mailboxInfo->m_status = MailboxAvailable; |
501 m_releasedMailboxInfoIndex = InvalidMailboxIndex; | 506 m_releasedMailboxInfoIndex = InvalidMailboxIndex; |
502 Canvas2DLayerManager::get().layerTransientResourceAllocationChan ged(this); | 507 Canvas2DLayerManager::get().layerTransientResourceAllocationChan ged(this); |
503 } else if (lostResource) { | 508 } else if (lostResource) { |
504 // In case of the resource is lost, we need to delete the backin g | 509 // In case of the resource is lost, we need to delete the backin g |
505 // texture and remove the mailbox from list to avoid reusing it | 510 // texture and remove the mailbox from list to avoid reusing it |
506 // in future. | 511 // in future. |
507 if (mailboxInfo->m_image) { | 512 if (mailboxInfo->m_image) { |
508 mailboxInfo->m_image->getTexture()->resetFlag( | 513 GrTexture* texture = mailboxInfo->m_image->getTexture(); |
509 static_cast<GrTextureFlags>(GrTexture::kReturnToCache_Fl agBit)); | 514 if (texture) { |
510 mailboxInfo->m_image->getTexture()->textureParamsModified(); | 515 texture->resetFlag(static_cast<GrTextureFlags>(GrTexture ::kReturnToCache_FlagBit)); |
516 texture->textureParamsModified(); | |
517 } | |
511 mailboxInfo->m_image.clear(); | 518 mailboxInfo->m_image.clear(); |
512 } | 519 } |
513 size_t i = mailboxInfo - m_mailboxes.begin(); | 520 if (m_destructionInProgress) { |
514 m_mailboxes.remove(i); | 521 mailboxInfo->m_status = MailboxAvailable; // To satisfy asse rt in destructor |
515 Canvas2DLayerManager::get().layerTransientResourceAllocationChan ged(this); | 522 |
516 // Here we need to return early since mailboxInfo removal would | 523 // The following line may trigger self destruction. We do no t care about |
517 // also clear m_parentLayerBridge reference. | 524 // not cleaning up m_mailboxes during destruction sequence b ecause |
525 // mailboxes will not be recycled after this point. Calling remove() | |
Hongbo Min
2014/09/10 04:51:08
It seems that the issue of memory-use-after-free i
| |
526 // could trigger a memory use after free, so we just clear t he self | |
527 // reference to be safe, and we let the Canvas2DLayerBridge destructor | |
528 // take care of freeing m_mailboxes. | |
529 mailboxInfo->m_parentLayerBridge.clear(); | |
530 } else { | |
531 size_t i = mailboxInfo - m_mailboxes.begin(); | |
532 m_mailboxes.remove(i); // indirectly clears mailboxInfo->m_p arentLayerBridge | |
533 Canvas2DLayerManager::get().layerTransientResourceAllocation Changed(this); | |
534 } | |
535 // mailboxInfo is not valid from this point, so we return immedi ately. | |
518 return; | 536 return; |
519 } else { | 537 } else { |
520 mailboxInfo->m_status = MailboxReleased; | 538 mailboxInfo->m_status = MailboxReleased; |
521 m_releasedMailboxInfoIndex = mailboxInfo - m_mailboxes.begin(); | 539 m_releasedMailboxInfoIndex = mailboxInfo - m_mailboxes.begin(); |
522 m_framesSinceMailboxRelease = 0; | 540 m_framesSinceMailboxRelease = 0; |
523 if (isHidden()) { | 541 if (isHidden()) { |
524 freeReleasedMailbox(); | 542 freeReleasedMailbox(); |
525 } else { | 543 } else { |
526 ASSERT(!m_destructionInProgress); | 544 ASSERT(!m_destructionInProgress); |
527 Canvas2DLayerManager::get().layerTransientResourceAllocation Changed(this); | 545 Canvas2DLayerManager::get().layerTransientResourceAllocation Changed(this); |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
569 // This copy constructor should only be used for Vector reallocation | 587 // This copy constructor should only be used for Vector reallocation |
570 // Assuming 'other' is to be destroyed, we transfer m_image and | 588 // Assuming 'other' is to be destroyed, we transfer m_image and |
571 // m_parentLayerBridge ownership rather than do a refcount dance. | 589 // m_parentLayerBridge ownership rather than do a refcount dance. |
572 memcpy(&m_mailbox, &other.m_mailbox, sizeof(m_mailbox)); | 590 memcpy(&m_mailbox, &other.m_mailbox, sizeof(m_mailbox)); |
573 m_image = const_cast<MailboxInfo*>(&other)->m_image.release(); | 591 m_image = const_cast<MailboxInfo*>(&other)->m_image.release(); |
574 m_parentLayerBridge = const_cast<MailboxInfo*>(&other)->m_parentLayerBridge. release(); | 592 m_parentLayerBridge = const_cast<MailboxInfo*>(&other)->m_parentLayerBridge. release(); |
575 m_status = other.m_status; | 593 m_status = other.m_status; |
576 } | 594 } |
577 | 595 |
578 } // namespace blink | 596 } // namespace blink |
OLD | NEW |