08a21cb5b169f7a1af550a7754049d473237be84
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / html / HTMLCanvasElement.cpp
1 /*
2  * Copyright (C) 2004, 2006, 2007 Apple Inc. All rights reserved.
3  * Copyright (C) 2007 Alp Toker <alp@atoker.com>
4  * Copyright (C) 2010 Torch Mobile (Beijing) Co. Ltd. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
16  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
19  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
23  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27
28 #include "config.h"
29 #include "core/html/HTMLCanvasElement.h"
30
31 #include "bindings/core/v8/ExceptionMessages.h"
32 #include "bindings/core/v8/ExceptionState.h"
33 #include "bindings/core/v8/ScriptController.h"
34 #include "core/HTMLNames.h"
35 #include "core/dom/Document.h"
36 #include "core/dom/ExceptionCode.h"
37 #include "core/frame/LocalFrame.h"
38 #include "core/frame/Settings.h"
39 #include "core/html/ImageData.h"
40 #include "core/html/canvas/Canvas2DContextAttributes.h"
41 #include "core/html/canvas/CanvasRenderingContext2D.h"
42 #include "core/html/canvas/WebGLContextAttributes.h"
43 #include "core/html/canvas/WebGLContextEvent.h"
44 #include "core/html/canvas/WebGLRenderingContext.h"
45 #include "core/rendering/RenderHTMLCanvas.h"
46 #include "platform/MIMETypeRegistry.h"
47 #include "platform/RuntimeEnabledFeatures.h"
48 #include "platform/graphics/Canvas2DImageBufferSurface.h"
49 #include "platform/graphics/GraphicsContextStateSaver.h"
50 #include "platform/graphics/ImageBuffer.h"
51 #include "platform/graphics/RecordingImageBufferSurface.h"
52 #include "platform/graphics/UnacceleratedImageBufferSurface.h"
53 #include "platform/graphics/gpu/WebGLImageBufferSurface.h"
54 #include "platform/transforms/AffineTransform.h"
55 #include "public/platform/Platform.h"
56 #include <math.h>
57 #include <v8.h>
58
59 namespace blink {
60
61 using namespace HTMLNames;
62
63 // These values come from the WhatWG spec.
64 static const int DefaultWidth = 300;
65 static const int DefaultHeight = 150;
66
67 // Firefox limits width/height to 32767 pixels, but slows down dramatically before it
68 // reaches that limit. We limit by area instead, giving us larger maximum dimensions,
69 // in exchange for a smaller maximum canvas size.
70 static const int MaxCanvasArea = 32768 * 8192; // Maximum canvas area in CSS pixels
71
72 //In Skia, we will also limit width/height to 32767.
73 static const int MaxSkiaDim = 32767; // Maximum width/height in CSS pixels.
74
75 DEFINE_EMPTY_DESTRUCTOR_WILL_BE_REMOVED(CanvasObserver);
76
77 inline HTMLCanvasElement::HTMLCanvasElement(Document& document)
78     : HTMLElement(canvasTag, document)
79     , DocumentVisibilityObserver(document)
80     , m_size(DefaultWidth, DefaultHeight)
81     , m_ignoreReset(false)
82     , m_accelerationDisabled(false)
83     , m_externallyAllocatedMemory(0)
84     , m_originClean(true)
85     , m_didFailToCreateImageBuffer(false)
86     , m_didClearImageBuffer(false)
87 {
88     ScriptWrappable::init(this);
89 }
90
91 DEFINE_NODE_FACTORY(HTMLCanvasElement)
92
93 HTMLCanvasElement::~HTMLCanvasElement()
94 {
95     resetDirtyRect();
96     v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory(-m_externallyAllocatedMemory);
97 #if !ENABLE(OILPAN)
98     HashSet<RawPtr<CanvasObserver> >::iterator end = m_observers.end();
99     for (HashSet<RawPtr<CanvasObserver> >::iterator it = m_observers.begin(); it != end; ++it)
100         (*it)->canvasDestroyed(this);
101     // Ensure these go away before the ImageBuffer.
102     m_contextStateSaver.clear();
103     m_context.clear();
104 #endif
105 }
106
107 void HTMLCanvasElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
108 {
109     if (name == widthAttr || name == heightAttr)
110         reset();
111     HTMLElement::parseAttribute(name, value);
112 }
113
114 RenderObject* HTMLCanvasElement::createRenderer(RenderStyle* style)
115 {
116     LocalFrame* frame = document().frame();
117     if (frame && frame->script().canExecuteScripts(NotAboutToExecuteScript))
118         return new RenderHTMLCanvas(this);
119     return HTMLElement::createRenderer(style);
120 }
121
122 Node::InsertionNotificationRequest HTMLCanvasElement::insertedInto(ContainerNode* node)
123 {
124     setIsInCanvasSubtree(true);
125     return HTMLElement::insertedInto(node);
126 }
127
128 void HTMLCanvasElement::addObserver(CanvasObserver* observer)
129 {
130     m_observers.add(observer);
131 }
132
133 void HTMLCanvasElement::removeObserver(CanvasObserver* observer)
134 {
135     m_observers.remove(observer);
136 }
137
138 void HTMLCanvasElement::setHeight(int value)
139 {
140     setIntegralAttribute(heightAttr, value);
141 }
142
143 void HTMLCanvasElement::setWidth(int value)
144 {
145     setIntegralAttribute(widthAttr, value);
146 }
147
148 CanvasRenderingContext* HTMLCanvasElement::getContext(const String& type, CanvasContextAttributes* attrs)
149 {
150     // A Canvas can either be "2D" or "webgl" but never both. If you request a 2D canvas and the existing
151     // context is already 2D, just return that. If the existing context is WebGL, then destroy it
152     // before creating a new 2D context. Vice versa when requesting a WebGL canvas. Requesting a
153     // context with any other type string will destroy any existing context.
154     enum ContextType {
155         Context2d,
156         ContextWebkit3d,
157         ContextExperimentalWebgl,
158         ContextWebgl,
159         // Only add new items to the end and keep the order of existing items.
160         ContextTypeCount,
161     };
162
163     // FIXME - The code depends on the context not going away once created, to prevent JS from
164     // seeing a dangling pointer. So for now we will disallow the context from being changed
165     // once it is created.
166     if (type == "2d") {
167         if (m_context && !m_context->is2d())
168             return 0;
169         if (!m_context) {
170             blink::Platform::current()->histogramEnumeration("Canvas.ContextType", Context2d, ContextTypeCount);
171             m_context = CanvasRenderingContext2D::create(this, static_cast<Canvas2DContextAttributes*>(attrs), document().inQuirksMode());
172             setNeedsCompositingUpdate();
173         }
174         return m_context.get();
175     }
176
177     // Accept the the provisional "experimental-webgl" or official "webgl" context ID.
178     if (type == "webgl" || type == "experimental-webgl") {
179         ContextType contextType = (type == "webgl") ? ContextWebgl : ContextExperimentalWebgl;
180         if (!m_context) {
181             blink::Platform::current()->histogramEnumeration("Canvas.ContextType", contextType, ContextTypeCount);
182             m_context = WebGLRenderingContext::create(this, static_cast<WebGLContextAttributes*>(attrs));
183             setNeedsCompositingUpdate();
184             updateExternallyAllocatedMemory();
185         } else if (!m_context->is3d()) {
186             dispatchEvent(WebGLContextEvent::create(EventTypeNames::webglcontextcreationerror, false, true, "Canvas has an existing, non-WebGL context"));
187             return 0;
188         }
189         return m_context.get();
190     }
191
192     return 0;
193 }
194
195 void HTMLCanvasElement::didDraw(const FloatRect& rect)
196 {
197     if (rect.isEmpty())
198         return;
199     clearCopiedImage();
200     if (m_dirtyRect.isEmpty())
201         blink::Platform::current()->currentThread()->addTaskObserver(this);
202     m_dirtyRect.unite(rect);
203 }
204
205 void HTMLCanvasElement::didFinalizeFrame()
206 {
207     if (m_dirtyRect.isEmpty())
208         return;
209
210     // Propagate the m_dirtyRect accumulated so far to the compositor
211     // before restarting with a blank dirty rect.
212     FloatRect srcRect(0, 0, size().width(), size().height());
213     m_dirtyRect.intersect(srcRect);
214     if (RenderBox* ro = renderBox()) {
215         FloatRect mappedDirtyRect = mapRect(m_dirtyRect, srcRect, ro->contentBoxRect());
216         ro->invalidatePaintRectangle(enclosingIntRect(mappedDirtyRect));
217     }
218     notifyObserversCanvasChanged(m_dirtyRect);
219     blink::Platform::current()->currentThread()->removeTaskObserver(this);
220     m_dirtyRect = FloatRect();
221 }
222
223 void HTMLCanvasElement::resetDirtyRect()
224 {
225     if (m_dirtyRect.isEmpty())
226         return;
227     blink::Platform::current()->currentThread()->removeTaskObserver(this);
228     m_dirtyRect = FloatRect();
229 }
230
231 void HTMLCanvasElement::didProcessTask()
232 {
233     // This method gets invoked if didDraw was called earlier in the current task.
234     ASSERT(!m_dirtyRect.isEmpty());
235     if (is3D()) {
236         didFinalizeFrame();
237     } else {
238         ASSERT(hasImageBuffer());
239         m_imageBuffer->finalizeFrame();
240     }
241     ASSERT(m_dirtyRect.isEmpty());
242 }
243
244 void HTMLCanvasElement::willProcessTask()
245 {
246     ASSERT_NOT_REACHED();
247 }
248
249 void HTMLCanvasElement::notifyObserversCanvasChanged(const FloatRect& rect)
250 {
251     WillBeHeapHashSet<RawPtrWillBeWeakMember<CanvasObserver> >::iterator end = m_observers.end();
252     for (WillBeHeapHashSet<RawPtrWillBeWeakMember<CanvasObserver> >::iterator it = m_observers.begin(); it != end; ++it)
253         (*it)->canvasChanged(this, rect);
254 }
255
256 void HTMLCanvasElement::reset()
257 {
258     if (m_ignoreReset)
259         return;
260
261     resetDirtyRect();
262
263     bool ok;
264     bool hadImageBuffer = hasImageBuffer();
265
266     int w = getAttribute(widthAttr).toInt(&ok);
267     if (!ok || w < 0)
268         w = DefaultWidth;
269
270     int h = getAttribute(heightAttr).toInt(&ok);
271     if (!ok || h < 0)
272         h = DefaultHeight;
273
274     if (m_contextStateSaver) {
275         // Reset to the initial graphics context state.
276         m_contextStateSaver->restore();
277         m_contextStateSaver->save();
278     }
279
280     if (m_context && m_context->is2d())
281         toCanvasRenderingContext2D(m_context.get())->reset();
282
283     IntSize oldSize = size();
284     IntSize newSize(w, h);
285
286     // If the size of an existing buffer matches, we can just clear it instead of reallocating.
287     // This optimization is only done for 2D canvases for now.
288     if (hadImageBuffer && oldSize == newSize && m_context && m_context->is2d()) {
289         if (!m_didClearImageBuffer)
290             clearImageBuffer();
291         return;
292     }
293
294     setSurfaceSize(newSize);
295
296     if (m_context && m_context->is3d() && oldSize != size())
297         toWebGLRenderingContext(m_context.get())->reshape(width(), height());
298
299     if (RenderObject* renderer = this->renderer()) {
300         if (renderer->isCanvas()) {
301             if (oldSize != size()) {
302                 toRenderHTMLCanvas(renderer)->canvasSizeChanged();
303                 if (renderBox() && renderBox()->hasAcceleratedCompositing())
304                     renderBox()->contentChanged(CanvasChanged);
305             }
306             if (hadImageBuffer)
307                 renderer->paintInvalidationForWholeRenderer();
308         }
309     }
310
311     WillBeHeapHashSet<RawPtrWillBeWeakMember<CanvasObserver> >::iterator end = m_observers.end();
312     for (WillBeHeapHashSet<RawPtrWillBeWeakMember<CanvasObserver> >::iterator it = m_observers.begin(); it != end; ++it)
313         (*it)->canvasResized(this);
314 }
315
316 bool HTMLCanvasElement::paintsIntoCanvasBuffer() const
317 {
318     ASSERT(m_context);
319
320     if (!m_context->isAccelerated())
321         return true;
322
323     if (renderBox() && renderBox()->hasAcceleratedCompositing())
324         return false;
325
326     return true;
327 }
328
329
330 void HTMLCanvasElement::paint(GraphicsContext* context, const LayoutRect& r)
331 {
332     if (m_context) {
333         if (!paintsIntoCanvasBuffer() && !document().printing())
334             return;
335         m_context->paintRenderingResultsToCanvas();
336     }
337
338     if (hasImageBuffer()) {
339         ImageBuffer* imageBuffer = buffer();
340         if (imageBuffer) {
341             CompositeOperator compositeOperator = !m_context || m_context->hasAlpha() ? CompositeSourceOver : CompositeCopy;
342             if (m_presentedImage)
343                 context->drawImage(m_presentedImage.get(), pixelSnappedIntRect(r), compositeOperator, DoNotRespectImageOrientation);
344             else
345                 context->drawImageBuffer(imageBuffer, pixelSnappedIntRect(r), 0, compositeOperator);
346         }
347     } else {
348         // When alpha is false, we should draw to opaque black.
349         if (m_context && !m_context->hasAlpha())
350             context->fillRect(FloatRect(r), Color(0, 0, 0));
351     }
352
353     if (is3D())
354         toWebGLRenderingContext(m_context.get())->markLayerComposited();
355 }
356
357 bool HTMLCanvasElement::is3D() const
358 {
359     return m_context && m_context->is3d();
360 }
361
362 void HTMLCanvasElement::makePresentationCopy()
363 {
364     if (!m_presentedImage) {
365         // The buffer contains the last presented data, so save a copy of it.
366         m_presentedImage = buffer()->copyImage(CopyBackingStore, Unscaled);
367         updateExternallyAllocatedMemory();
368     }
369 }
370
371 void HTMLCanvasElement::clearPresentationCopy()
372 {
373     m_presentedImage.clear();
374     updateExternallyAllocatedMemory();
375 }
376
377 void HTMLCanvasElement::setSurfaceSize(const IntSize& size)
378 {
379     m_size = size;
380     m_didFailToCreateImageBuffer = false;
381     discardImageBuffer();
382     clearCopiedImage();
383     if (m_context && m_context->is2d()) {
384         CanvasRenderingContext2D* context2d = toCanvasRenderingContext2D(m_context.get());
385         if (context2d->isContextLost()) {
386             context2d->restoreContext();
387         }
388     }
389 }
390
391 String HTMLCanvasElement::toEncodingMimeType(const String& mimeType)
392 {
393     String lowercaseMimeType = mimeType.lower();
394
395     // FIXME: Make isSupportedImageMIMETypeForEncoding threadsafe (to allow this method to be used on a worker thread).
396     if (mimeType.isNull() || !MIMETypeRegistry::isSupportedImageMIMETypeForEncoding(lowercaseMimeType))
397         lowercaseMimeType = "image/png";
398
399     return lowercaseMimeType;
400 }
401
402 const AtomicString HTMLCanvasElement::imageSourceURL() const
403 {
404     return AtomicString(toDataURLInternal("image/png", 0, true));
405 }
406
407 String HTMLCanvasElement::toDataURLInternal(const String& mimeType, const double* quality, bool isSaving) const
408 {
409     if (m_size.isEmpty() || !buffer())
410         return String("data:,");
411
412     String encodingMimeType = toEncodingMimeType(mimeType);
413
414     // Try to get ImageData first, as that may avoid lossy conversions.
415     RefPtrWillBeRawPtr<ImageData> imageData = getImageData();
416
417     if (imageData)
418         return ImageDataToDataURL(ImageDataBuffer(imageData->size(), imageData->data()), encodingMimeType, quality);
419
420     if (m_context && m_context->is3d()) {
421         toWebGLRenderingContext(m_context.get())->setSavingImage(isSaving);
422         m_context->paintRenderingResultsToCanvas();
423         toWebGLRenderingContext(m_context.get())->setSavingImage(false);
424     }
425
426     return buffer()->toDataURL(encodingMimeType, quality);
427 }
428
429 String HTMLCanvasElement::toDataURL(const String& mimeType, const double* quality, ExceptionState& exceptionState) const
430 {
431     if (!m_originClean) {
432         exceptionState.throwSecurityError("Tainted canvases may not be exported.");
433         return String();
434     }
435
436     return toDataURLInternal(mimeType, quality);
437 }
438
439 PassRefPtrWillBeRawPtr<ImageData> HTMLCanvasElement::getImageData() const
440 {
441     if (!m_context || !m_context->is3d())
442         return nullptr;
443     return toWebGLRenderingContext(m_context.get())->paintRenderingResultsToImageData();
444 }
445
446 SecurityOrigin* HTMLCanvasElement::securityOrigin() const
447 {
448     return document().securityOrigin();
449 }
450
451 bool HTMLCanvasElement::shouldAccelerate(const IntSize& size) const
452 {
453     if (m_context && !m_context->is2d())
454         return false;
455
456     if (m_accelerationDisabled)
457         return false;
458
459     Settings* settings = document().settings();
460     if (!settings || !settings->accelerated2dCanvasEnabled())
461         return false;
462
463     // Do not use acceleration for small canvas.
464     if (size.width() * size.height() < settings->minimumAccelerated2dCanvasSize())
465         return false;
466
467     if (!blink::Platform::current()->canAccelerate2dCanvas())
468         return false;
469
470     return true;
471 }
472
473 PassOwnPtr<ImageBufferSurface> HTMLCanvasElement::createImageBufferSurface(const IntSize& deviceSize, int* msaaSampleCount)
474 {
475     OpacityMode opacityMode = !m_context || m_context->hasAlpha() ? NonOpaque : Opaque;
476
477     *msaaSampleCount = 0;
478     if (is3D())
479         return adoptPtr(new WebGLImageBufferSurface(size(), opacityMode));
480
481     if (RuntimeEnabledFeatures::displayList2dCanvasEnabled()) {
482         OwnPtr<ImageBufferSurface> surface = adoptPtr(new RecordingImageBufferSurface(size(), opacityMode));
483         if (surface->isValid())
484             return surface.release();
485     }
486
487     if (shouldAccelerate(deviceSize)) {
488         if (document().settings())
489             *msaaSampleCount = document().settings()->accelerated2dCanvasMSAASampleCount();
490         OwnPtr<ImageBufferSurface> surface = adoptPtr(new Canvas2DImageBufferSurface(size(), opacityMode, *msaaSampleCount));
491         if (surface->isValid())
492             return surface.release();
493     }
494
495     return adoptPtr(new UnacceleratedImageBufferSurface(size(), opacityMode));
496 }
497
498 void HTMLCanvasElement::createImageBuffer()
499 {
500     createImageBufferInternal();
501     if (m_didFailToCreateImageBuffer && m_context && m_context->is2d())
502         toCanvasRenderingContext2D(m_context.get())->loseContext();
503 }
504
505 void HTMLCanvasElement::createImageBufferInternal()
506 {
507     ASSERT(!m_imageBuffer);
508     ASSERT(!m_contextStateSaver);
509
510     m_didFailToCreateImageBuffer = true;
511     m_didClearImageBuffer = true;
512
513     IntSize deviceSize = size();
514     if (deviceSize.width() * deviceSize.height() > MaxCanvasArea)
515         return;
516
517     if (deviceSize.width() > MaxSkiaDim || deviceSize.height() > MaxSkiaDim)
518         return;
519
520     if (!deviceSize.width() || !deviceSize.height())
521         return;
522
523     int msaaSampleCount;
524     OwnPtr<ImageBufferSurface> surface = createImageBufferSurface(deviceSize, &msaaSampleCount);
525     if (!surface->isValid())
526         return;
527
528     m_imageBuffer = ImageBuffer::create(surface.release());
529     m_imageBuffer->setClient(this);
530
531     m_didFailToCreateImageBuffer = false;
532
533     updateExternallyAllocatedMemory();
534
535     if (is3D()) {
536         // Early out for WebGL canvases
537         return;
538     }
539
540     m_imageBuffer->setClient(this);
541     m_imageBuffer->context()->setShouldClampToSourceRect(false);
542     m_imageBuffer->context()->disableAntialiasingOptimizationForHairlineImages();
543     m_imageBuffer->context()->setImageInterpolationQuality(CanvasDefaultInterpolationQuality);
544     // Enabling MSAA overrides a request to disable antialiasing. This is true regardless of whether the
545     // rendering mode is accelerated or not. For consistency, we don't want to apply AA in accelerated
546     // canvases but not in unaccelerated canvases.
547     if (!msaaSampleCount && document().settings() && !document().settings()->antialiased2dCanvasEnabled())
548         m_imageBuffer->context()->setShouldAntialias(false);
549     // GraphicsContext's defaults don't always agree with the 2d canvas spec.
550     // See CanvasRenderingContext2D::State::State() for more information.
551     m_imageBuffer->context()->setMiterLimit(10);
552     m_imageBuffer->context()->setStrokeThickness(1);
553 #if ENABLE(ASSERT)
554     m_imageBuffer->context()->disableDestructionChecks(); // 2D canvas is allowed to leave context in an unfinalized state.
555 #endif
556     m_contextStateSaver = adoptPtr(new GraphicsContextStateSaver(*m_imageBuffer->context()));
557
558     if (m_context)
559         setNeedsCompositingUpdate();
560 }
561
562 void HTMLCanvasElement::notifySurfaceInvalid()
563 {
564     if (m_context && m_context->is2d()) {
565         CanvasRenderingContext2D* context2d = toCanvasRenderingContext2D(m_context.get());
566         context2d->loseContext();
567     }
568 }
569
570 void HTMLCanvasElement::trace(Visitor* visitor)
571 {
572 #if ENABLE(OILPAN)
573     visitor->trace(m_observers);
574     visitor->trace(m_context);
575 #endif
576     DocumentVisibilityObserver::trace(visitor);
577     HTMLElement::trace(visitor);
578 }
579
580 void HTMLCanvasElement::updateExternallyAllocatedMemory() const
581 {
582     int bufferCount = 0;
583     if (m_imageBuffer)
584         bufferCount++;
585     if (is3D())
586         bufferCount += 2;
587     if (m_copiedImage)
588         bufferCount++;
589     if (m_presentedImage)
590         bufferCount++;
591
592     Checked<intptr_t, RecordOverflow> checkedExternallyAllocatedMemory = 4 * bufferCount;
593     checkedExternallyAllocatedMemory *= width();
594     checkedExternallyAllocatedMemory *= height();
595     intptr_t externallyAllocatedMemory;
596     if (checkedExternallyAllocatedMemory.safeGet(externallyAllocatedMemory) == CheckedState::DidOverflow)
597         externallyAllocatedMemory = std::numeric_limits<intptr_t>::max();
598
599     // Subtracting two intptr_t that are known to be positive will never underflow.
600     v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory(externallyAllocatedMemory - m_externallyAllocatedMemory);
601     m_externallyAllocatedMemory = externallyAllocatedMemory;
602 }
603
604 GraphicsContext* HTMLCanvasElement::drawingContext() const
605 {
606     return buffer() ? m_imageBuffer->context() : 0;
607 }
608
609 GraphicsContext* HTMLCanvasElement::existingDrawingContext() const
610 {
611     if (!hasImageBuffer())
612         return 0;
613
614     return drawingContext();
615 }
616
617 ImageBuffer* HTMLCanvasElement::buffer() const
618 {
619     if (!hasImageBuffer() && !m_didFailToCreateImageBuffer)
620         const_cast<HTMLCanvasElement*>(this)->createImageBuffer();
621     return m_imageBuffer.get();
622 }
623
624 void HTMLCanvasElement::ensureUnacceleratedImageBuffer()
625 {
626     if ((hasImageBuffer() && !m_imageBuffer->isAccelerated()) || m_didFailToCreateImageBuffer)
627         return;
628     discardImageBuffer();
629     OpacityMode opacityMode = !m_context || m_context->hasAlpha() ? NonOpaque : Opaque;
630     m_imageBuffer = ImageBuffer::create(size(), opacityMode);
631     m_didFailToCreateImageBuffer = !m_imageBuffer;
632 }
633
634 Image* HTMLCanvasElement::copiedImage() const
635 {
636     if (!m_copiedImage && buffer()) {
637         if (m_context && m_context->is3d()) {
638             toWebGLRenderingContext(m_context.get())->setSavingImage(true);
639             m_context->paintRenderingResultsToCanvas();
640             toWebGLRenderingContext(m_context.get())->setSavingImage(false);
641         }
642         m_copiedImage = buffer()->copyImage(CopyBackingStore, Unscaled);
643         updateExternallyAllocatedMemory();
644     }
645     return m_copiedImage.get();
646 }
647
648 void HTMLCanvasElement::clearImageBuffer()
649 {
650     ASSERT(hasImageBuffer() && !m_didFailToCreateImageBuffer);
651     ASSERT(!m_didClearImageBuffer);
652     ASSERT(m_context);
653
654     m_didClearImageBuffer = true;
655
656     if (m_context->is2d()) {
657         // No need to undo transforms/clip/etc. because we are called right
658         // after the context is reset.
659         toCanvasRenderingContext2D(m_context.get())->clearRect(0, 0, width(), height());
660     }
661 }
662
663 void HTMLCanvasElement::discardImageBuffer()
664 {
665     m_contextStateSaver.clear(); // uses context owned by m_imageBuffer
666     m_imageBuffer.clear();
667     resetDirtyRect();
668     updateExternallyAllocatedMemory();
669 }
670
671 bool HTMLCanvasElement::hasValidImageBuffer() const
672 {
673     return m_imageBuffer && m_imageBuffer->isSurfaceValid();
674 }
675
676 void HTMLCanvasElement::clearCopiedImage()
677 {
678     if (m_copiedImage) {
679         m_copiedImage.clear();
680         updateExternallyAllocatedMemory();
681     }
682     m_didClearImageBuffer = false;
683 }
684
685 AffineTransform HTMLCanvasElement::baseTransform() const
686 {
687     ASSERT(hasImageBuffer() && !m_didFailToCreateImageBuffer);
688     return m_imageBuffer->baseTransform();
689 }
690
691 void HTMLCanvasElement::didChangeVisibilityState(PageVisibilityState visibility)
692 {
693     if (hasImageBuffer()) {
694         bool hidden = visibility != PageVisibilityStateVisible;
695         if (hidden) {
696             clearCopiedImage();
697             if (is3D()) {
698                 discardImageBuffer();
699             }
700         }
701         if (hasImageBuffer()) {
702             m_imageBuffer->setIsHidden(hidden);
703         }
704     }
705 }
706
707 void HTMLCanvasElement::didMoveToNewDocument(Document& oldDocument)
708 {
709     setObservedDocument(document());
710     HTMLElement::didMoveToNewDocument(oldDocument);
711 }
712
713 PassRefPtr<Image> HTMLCanvasElement::getSourceImageForCanvas(SourceImageMode mode, SourceImageStatus* status) const
714 {
715     if (!width() || !height()) {
716         *status = ZeroSizeCanvasSourceImageStatus;
717         return nullptr;
718     }
719
720     if (!buffer()) {
721         *status = InvalidSourceImageStatus;
722         return nullptr;
723     }
724
725     if (mode == CopySourceImageIfVolatile) {
726         *status = NormalSourceImageStatus;
727         return copiedImage();
728     }
729
730     if (m_context && m_context->is3d()) {
731         m_context->paintRenderingResultsToCanvas();
732         *status = ExternalSourceImageStatus;
733     } else {
734         *status = NormalSourceImageStatus;
735     }
736     return m_imageBuffer->copyImage(DontCopyBackingStore, Unscaled);
737 }
738
739 bool HTMLCanvasElement::wouldTaintOrigin(SecurityOrigin*) const
740 {
741     return !originClean();
742 }
743
744 FloatSize HTMLCanvasElement::sourceSize() const
745 {
746     return FloatSize(width(), height());
747 }
748
749 }