Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(62)

Side by Side Diff: Source/core/html/HTMLCanvasElement.cpp

Issue 211503006: Implementation of 2D canvas context lost/restored events (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: rebase Created 6 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2004, 2006, 2007 Apple Inc. All rights reserved. 2 * Copyright (C) 2004, 2006, 2007 Apple Inc. All rights reserved.
3 * Copyright (C) 2007 Alp Toker <alp@atoker.com> 3 * Copyright (C) 2007 Alp Toker <alp@atoker.com>
4 * Copyright (C) 2010 Torch Mobile (Beijing) Co. Ltd. All rights reserved. 4 * Copyright (C) 2010 Torch Mobile (Beijing) Co. Ltd. All rights reserved.
5 * 5 *
6 * Redistribution and use in source and binary forms, with or without 6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions 7 * modification, are permitted provided that the following conditions
8 * are met: 8 * are met:
9 * 1. Redistributions of source code must retain the above copyright 9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer. 10 * notice, this list of conditions and the following disclaimer.
(...skipping 340 matching lines...) Expand 10 before | Expand all | Expand 10 after
351 m_presentedImage.clear(); 351 m_presentedImage.clear();
352 updateExternallyAllocatedMemory(); 352 updateExternallyAllocatedMemory();
353 } 353 }
354 354
355 void HTMLCanvasElement::setSurfaceSize(const IntSize& size) 355 void HTMLCanvasElement::setSurfaceSize(const IntSize& size)
356 { 356 {
357 m_size = size; 357 m_size = size;
358 m_didFailToCreateImageBuffer = false; 358 m_didFailToCreateImageBuffer = false;
359 discardImageBuffer(); 359 discardImageBuffer();
360 clearCopiedImage(); 360 clearCopiedImage();
361 if (m_context && m_context->is2d()) {
362 CanvasRenderingContext2D* context2d = toCanvasRenderingContext2D(m_conte xt.get());
363 if (context2d->isContextLost()) {
364 context2d->restoreContext();
365 }
366 }
361 } 367 }
362 368
363 String HTMLCanvasElement::toEncodingMimeType(const String& mimeType) 369 String HTMLCanvasElement::toEncodingMimeType(const String& mimeType)
364 { 370 {
365 String lowercaseMimeType = mimeType.lower(); 371 String lowercaseMimeType = mimeType.lower();
366 372
367 // FIXME: Make isSupportedImageMIMETypeForEncoding threadsafe (to allow this method to be used on a worker thread). 373 // FIXME: Make isSupportedImageMIMETypeForEncoding threadsafe (to allow this method to be used on a worker thread).
368 if (mimeType.isNull() || !MIMETypeRegistry::isSupportedImageMIMETypeForEncod ing(lowercaseMimeType)) 374 if (mimeType.isNull() || !MIMETypeRegistry::isSupportedImageMIMETypeForEncod ing(lowercaseMimeType))
369 lowercaseMimeType = "image/png"; 375 lowercaseMimeType = "image/png";
370 376
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
443 OwnPtr<ImageBufferSurface> surface = adoptPtr(new Canvas2DImageBufferSur face(size(), opacityMode, *msaaSampleCount)); 449 OwnPtr<ImageBufferSurface> surface = adoptPtr(new Canvas2DImageBufferSur face(size(), opacityMode, *msaaSampleCount));
444 if (surface->isValid()) 450 if (surface->isValid())
445 return surface.release(); 451 return surface.release();
446 } 452 }
447 453
448 return adoptPtr(new UnacceleratedImageBufferSurface(size(), opacityMode)); 454 return adoptPtr(new UnacceleratedImageBufferSurface(size(), opacityMode));
449 } 455 }
450 456
451 void HTMLCanvasElement::createImageBuffer() 457 void HTMLCanvasElement::createImageBuffer()
452 { 458 {
459 createImageBufferInternal();
460 if (m_didFailToCreateImageBuffer && m_context && m_context->is2d())
461 toCanvasRenderingContext2D(m_context.get())->loseContext();
462 }
463
464 void HTMLCanvasElement::createImageBufferInternal()
465 {
453 ASSERT(!m_imageBuffer); 466 ASSERT(!m_imageBuffer);
454 ASSERT(!m_contextStateSaver); 467 ASSERT(!m_contextStateSaver);
455 468
456 m_didFailToCreateImageBuffer = true; 469 m_didFailToCreateImageBuffer = true;
457 m_didClearImageBuffer = true; 470 m_didClearImageBuffer = true;
458 471
459 IntSize deviceSize = size(); 472 IntSize deviceSize = size();
460 if (deviceSize.width() * deviceSize.height() > MaxCanvasArea) 473 if (deviceSize.width() * deviceSize.height() > MaxCanvasArea)
461 return; 474 return;
462 475
463 if (deviceSize.width() > MaxSkiaDim || deviceSize.height() > MaxSkiaDim) 476 if (deviceSize.width() > MaxSkiaDim || deviceSize.height() > MaxSkiaDim)
464 return; 477 return;
465 478
466 if (!deviceSize.width() || !deviceSize.height()) 479 if (!deviceSize.width() || !deviceSize.height())
467 return; 480 return;
468 481
469 int msaaSampleCount; 482 int msaaSampleCount;
470 OwnPtr<ImageBufferSurface> surface = createImageBufferSurface(deviceSize, &m saaSampleCount); 483 OwnPtr<ImageBufferSurface> surface = createImageBufferSurface(deviceSize, &m saaSampleCount);
471 if (!surface->isValid()) 484 if (!surface->isValid())
472 return; 485 return;
486
473 m_imageBuffer = ImageBuffer::create(surface.release()); 487 m_imageBuffer = ImageBuffer::create(surface.release());
488 m_imageBuffer->setClient(this);
474 489
475 m_didFailToCreateImageBuffer = false; 490 m_didFailToCreateImageBuffer = false;
476 491
477 updateExternallyAllocatedMemory(); 492 updateExternallyAllocatedMemory();
478 493
479 if (is3D()) { 494 if (is3D()) {
480 // Early out for WebGL canvases 495 // Early out for WebGL canvases
481 return; 496 return;
482 } 497 }
483 498
499 m_imageBuffer->setClient(this);
484 m_imageBuffer->context()->setShouldClampToSourceRect(false); 500 m_imageBuffer->context()->setShouldClampToSourceRect(false);
485 m_imageBuffer->context()->setImageInterpolationQuality(CanvasDefaultInterpol ationQuality); 501 m_imageBuffer->context()->setImageInterpolationQuality(CanvasDefaultInterpol ationQuality);
486 // Enabling MSAA overrides a request to disable antialiasing. This is true r egardless of whether the 502 // Enabling MSAA overrides a request to disable antialiasing. This is true r egardless of whether the
487 // rendering mode is accelerated or not. For consistency, we don't want to a pply AA in accelerated 503 // rendering mode is accelerated or not. For consistency, we don't want to a pply AA in accelerated
488 // canvases but not in unaccelerated canvases. 504 // canvases but not in unaccelerated canvases.
489 if (!msaaSampleCount && document().settings() && !document().settings()->ant ialiased2dCanvasEnabled()) 505 if (!msaaSampleCount && document().settings() && !document().settings()->ant ialiased2dCanvasEnabled())
490 m_imageBuffer->context()->setShouldAntialias(false); 506 m_imageBuffer->context()->setShouldAntialias(false);
491 // GraphicsContext's defaults don't always agree with the 2d canvas spec. 507 // GraphicsContext's defaults don't always agree with the 2d canvas spec.
492 // See CanvasRenderingContext2D::State::State() for more information. 508 // See CanvasRenderingContext2D::State::State() for more information.
493 m_imageBuffer->context()->setMiterLimit(10); 509 m_imageBuffer->context()->setMiterLimit(10);
494 m_imageBuffer->context()->setStrokeThickness(1); 510 m_imageBuffer->context()->setStrokeThickness(1);
495 m_contextStateSaver = adoptPtr(new GraphicsContextStateSaver(*m_imageBuffer- >context())); 511 m_contextStateSaver = adoptPtr(new GraphicsContextStateSaver(*m_imageBuffer- >context()));
496 512
497 // Recalculate compositing requirements if acceleration state changed. 513 // Recalculate compositing requirements if acceleration state changed.
498 if (m_context) 514 if (m_context)
499 scheduleLayerUpdate(); 515 scheduleLayerUpdate();
516 return;
517 }
518
519 void HTMLCanvasElement::notifySurfaceInvalid()
520 {
521 if (m_context && m_context->is2d()) {
522 CanvasRenderingContext2D* context2d = toCanvasRenderingContext2D(m_conte xt.get());
523 context2d->loseContext();
524 }
500 } 525 }
501 526
502 void HTMLCanvasElement::updateExternallyAllocatedMemory() const 527 void HTMLCanvasElement::updateExternallyAllocatedMemory() const
503 { 528 {
504 int bufferCount = 0; 529 int bufferCount = 0;
505 if (m_imageBuffer) 530 if (m_imageBuffer)
506 bufferCount++; 531 bufferCount++;
507 if (is3D()) 532 if (is3D())
508 bufferCount += 2; 533 bufferCount += 2;
509 if (m_copiedImage) 534 if (m_copiedImage)
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
581 } 606 }
582 } 607 }
583 608
584 void HTMLCanvasElement::discardImageBuffer() 609 void HTMLCanvasElement::discardImageBuffer()
585 { 610 {
586 m_contextStateSaver.clear(); // uses context owned by m_imageBuffer 611 m_contextStateSaver.clear(); // uses context owned by m_imageBuffer
587 m_imageBuffer.clear(); 612 m_imageBuffer.clear();
588 updateExternallyAllocatedMemory(); 613 updateExternallyAllocatedMemory();
589 } 614 }
590 615
616 bool HTMLCanvasElement::hasValidImageBuffer() const
617 {
618 return m_imageBuffer && m_imageBuffer->isSurfaceValid();
619 }
620
591 void HTMLCanvasElement::clearCopiedImage() 621 void HTMLCanvasElement::clearCopiedImage()
592 { 622 {
593 m_copiedImage.clear(); 623 m_copiedImage.clear();
594 m_didClearImageBuffer = false; 624 m_didClearImageBuffer = false;
595 updateExternallyAllocatedMemory(); 625 updateExternallyAllocatedMemory();
596 } 626 }
597 627
598 AffineTransform HTMLCanvasElement::baseTransform() const 628 AffineTransform HTMLCanvasElement::baseTransform() const
599 { 629 {
600 ASSERT(hasImageBuffer() && !m_didFailToCreateImageBuffer); 630 ASSERT(hasImageBuffer() && !m_didFailToCreateImageBuffer);
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
653 { 683 {
654 return !originClean(); 684 return !originClean();
655 } 685 }
656 686
657 FloatSize HTMLCanvasElement::sourceSize() const 687 FloatSize HTMLCanvasElement::sourceSize() const
658 { 688 {
659 return FloatSize(width(), height()); 689 return FloatSize(width(), height());
660 } 690 }
661 691
662 } 692 }
OLDNEW
« no previous file with comments | « Source/core/html/HTMLCanvasElement.h ('k') | Source/core/html/canvas/Canvas2DContextAttributes.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698