| OLD | NEW |
| (Empty) |
| 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 "sky/engine/config.h" | |
| 29 #include "sky/engine/core/html/HTMLCanvasElement.h" | |
| 30 | |
| 31 #include <math.h> | |
| 32 | |
| 33 #include "base/bind.h" | |
| 34 #include "gen/sky/core/HTMLNames.h" | |
| 35 #include "gen/sky/platform/RuntimeEnabledFeatures.h" | |
| 36 #include "sky/engine/bindings/exception_messages.h" | |
| 37 #include "sky/engine/bindings/exception_state.h" | |
| 38 #include "sky/engine/core/dom/Document.h" | |
| 39 #include "sky/engine/core/dom/ExceptionCode.h" | |
| 40 #include "sky/engine/core/dom/Microtask.h" | |
| 41 #include "sky/engine/core/frame/LocalFrame.h" | |
| 42 #include "sky/engine/core/frame/Settings.h" | |
| 43 #include "sky/engine/core/html/ImageData.h" | |
| 44 #include "sky/engine/core/html/canvas/Canvas2DContextAttributes.h" | |
| 45 #include "sky/engine/core/html/canvas/CanvasRenderingContext2D.h" | |
| 46 #include "sky/engine/core/html/canvas/WebGLContextAttributes.h" | |
| 47 #include "sky/engine/core/html/canvas/WebGLContextEvent.h" | |
| 48 #include "sky/engine/core/html/canvas/WebGLRenderingContext.h" | |
| 49 #include "sky/engine/core/rendering/RenderHTMLCanvas.h" | |
| 50 #include "sky/engine/core/rendering/RenderLayer.h" | |
| 51 #include "sky/engine/platform/MIMETypeRegistry.h" | |
| 52 #include "sky/engine/platform/graphics/Canvas2DImageBufferSurface.h" | |
| 53 #include "sky/engine/platform/graphics/GraphicsContextStateSaver.h" | |
| 54 #include "sky/engine/platform/graphics/ImageBuffer.h" | |
| 55 #include "sky/engine/platform/graphics/UnacceleratedImageBufferSurface.h" | |
| 56 #include "sky/engine/platform/graphics/gpu/WebGLImageBufferSurface.h" | |
| 57 #include "sky/engine/platform/transforms/AffineTransform.h" | |
| 58 #include "sky/engine/public/platform/Platform.h" | |
| 59 | |
| 60 namespace blink { | |
| 61 | |
| 62 // These values come from the WhatWG spec. | |
| 63 static const int DefaultWidth = 300; | |
| 64 static const int DefaultHeight = 150; | |
| 65 | |
| 66 // Firefox limits width/height to 32767 pixels, but slows down dramatically befo
re it | |
| 67 // reaches that limit. We limit by area instead, giving us larger maximum dimens
ions, | |
| 68 // in exchange for a smaller maximum canvas size. | |
| 69 static const int MaxCanvasArea = 32768 * 8192; // Maximum canvas area in CSS pix
els | |
| 70 | |
| 71 //In Skia, we will also limit width/height to 32767. | |
| 72 static const int MaxSkiaDim = 32767; // Maximum width/height in CSS pixels. | |
| 73 | |
| 74 DEFINE_EMPTY_DESTRUCTOR_WILL_BE_REMOVED(CanvasObserver); | |
| 75 | |
| 76 inline HTMLCanvasElement::HTMLCanvasElement(Document& document) | |
| 77 : HTMLElement(HTMLNames::canvasTag, document) | |
| 78 , m_size(DefaultWidth, DefaultHeight) | |
| 79 , m_ignoreReset(false) | |
| 80 , m_accelerationDisabled(false) | |
| 81 , m_externallyAllocatedMemory(0) | |
| 82 , m_didFailToCreateImageBuffer(false) | |
| 83 , m_didClearImageBuffer(false) | |
| 84 { | |
| 85 } | |
| 86 | |
| 87 DEFINE_NODE_FACTORY(HTMLCanvasElement) | |
| 88 | |
| 89 HTMLCanvasElement::~HTMLCanvasElement() | |
| 90 { | |
| 91 resetDirtyRect(); | |
| 92 #if !ENABLE(OILPAN) | |
| 93 HashSet<RawPtr<CanvasObserver> >::iterator end = m_observers.end(); | |
| 94 for (HashSet<RawPtr<CanvasObserver> >::iterator it = m_observers.begin(); it
!= end; ++it) | |
| 95 (*it)->canvasDestroyed(this); | |
| 96 // Ensure these go away before the ImageBuffer. | |
| 97 m_contextStateSaver.clear(); | |
| 98 m_context.clear(); | |
| 99 #endif | |
| 100 } | |
| 101 | |
| 102 void HTMLCanvasElement::parseAttribute(const QualifiedName& name, const AtomicSt
ring& value) | |
| 103 { | |
| 104 if (name == HTMLNames::widthAttr || name == HTMLNames::heightAttr) | |
| 105 reset(); | |
| 106 HTMLElement::parseAttribute(name, value); | |
| 107 } | |
| 108 | |
| 109 RenderObject* HTMLCanvasElement::createRenderer(RenderStyle* style) | |
| 110 { | |
| 111 return new RenderHTMLCanvas(this); | |
| 112 } | |
| 113 | |
| 114 void HTMLCanvasElement::addObserver(CanvasObserver* observer) | |
| 115 { | |
| 116 m_observers.add(observer); | |
| 117 } | |
| 118 | |
| 119 void HTMLCanvasElement::removeObserver(CanvasObserver* observer) | |
| 120 { | |
| 121 m_observers.remove(observer); | |
| 122 } | |
| 123 | |
| 124 void HTMLCanvasElement::setHeight(int value) | |
| 125 { | |
| 126 setIntegralAttribute(HTMLNames::heightAttr, value); | |
| 127 } | |
| 128 | |
| 129 void HTMLCanvasElement::setWidth(int value) | |
| 130 { | |
| 131 setIntegralAttribute(HTMLNames::widthAttr, value); | |
| 132 } | |
| 133 | |
| 134 CanvasRenderingContext2D* HTMLCanvasElement::getContext(const String& type, Canv
asContextAttributes* attrs) | |
| 135 { | |
| 136 // A Canvas can either be "2D" or "webgl" but never both. If you request a 2
D canvas and the existing | |
| 137 // context is already 2D, just return that. If the existing context is WebGL
, then destroy it | |
| 138 // before creating a new 2D context. Vice versa when requesting a WebGL canv
as. Requesting a | |
| 139 // context with any other type string will destroy any existing context. | |
| 140 enum ContextType { | |
| 141 // Do not change assigned numbers of existing items: add new features to
the end of the list. | |
| 142 Context2d = 0, | |
| 143 ContextExperimentalWebgl = 2, | |
| 144 ContextWebgl = 3, | |
| 145 ContextTypeCount, | |
| 146 }; | |
| 147 | |
| 148 // FIXME - The code depends on the context not going away once created, to p
revent JS from | |
| 149 // seeing a dangling pointer. So for now we will disallow the context from b
eing changed | |
| 150 // once it is created. | |
| 151 if (type == "2d") { | |
| 152 if (m_context && !m_context->is2d()) | |
| 153 return 0; | |
| 154 if (!m_context) { | |
| 155 blink::Platform::current()->histogramEnumeration("Canvas.ContextType
", Context2d, ContextTypeCount); | |
| 156 m_context = CanvasRenderingContext2D::create(this, static_cast<Canva
s2DContextAttributes*>(attrs)); | |
| 157 } | |
| 158 return static_cast<CanvasRenderingContext2D*>(m_context.get()); | |
| 159 } | |
| 160 | |
| 161 // Accept the the provisional "experimental-webgl" or official "webgl" conte
xt ID. | |
| 162 // if (type == "webgl" || type == "experimental-webgl") { | |
| 163 // ContextType contextType = (type == "webgl") ? ContextWebgl : ContextE
xperimentalWebgl; | |
| 164 // if (!m_context) { | |
| 165 // blink::Platform::current()->histogramEnumeration("Canvas.ContextT
ype", contextType, ContextTypeCount); | |
| 166 // m_context = WebGLRenderingContext::create(this, static_cast<WebGL
ContextAttributes*>(attrs)); | |
| 167 // updateExternallyAllocatedMemory(); | |
| 168 // } else if (!m_context->is3d()) { | |
| 169 // dispatchEvent(WebGLContextEvent::create(EventTypeNames::webglcont
extcreationerror, false, true, "Canvas has an existing, non-WebGL context")); | |
| 170 // return 0; | |
| 171 // } | |
| 172 // return m_context.get(); | |
| 173 // } | |
| 174 | |
| 175 return nullptr; | |
| 176 } | |
| 177 | |
| 178 void HTMLCanvasElement::didDraw(const FloatRect& rect) | |
| 179 { | |
| 180 if (rect.isEmpty()) | |
| 181 return; | |
| 182 clearCopiedImage(); | |
| 183 if (m_dirtyRect.isEmpty()) { | |
| 184 m_finalizeFrameTask.Reset(base::Bind( | |
| 185 &HTMLCanvasElement::finalizeFrameMicrotask, base::Unretained(this)))
; | |
| 186 Microtask::enqueueMicrotask(m_finalizeFrameTask.callback()); | |
| 187 } | |
| 188 m_dirtyRect.unite(rect); | |
| 189 } | |
| 190 | |
| 191 void HTMLCanvasElement::didFinalizeFrame() | |
| 192 { | |
| 193 if (m_dirtyRect.isEmpty()) | |
| 194 return; | |
| 195 | |
| 196 // Propagate the m_dirtyRect accumulated so far to the compositor | |
| 197 // before restarting with a blank dirty rect. | |
| 198 FloatRect srcRect(0, 0, size().width(), size().height()); | |
| 199 m_dirtyRect.intersect(srcRect); | |
| 200 notifyObserversCanvasChanged(m_dirtyRect); | |
| 201 m_finalizeFrameTask.Cancel(); | |
| 202 m_dirtyRect = FloatRect(); | |
| 203 } | |
| 204 | |
| 205 void HTMLCanvasElement::resetDirtyRect() | |
| 206 { | |
| 207 if (m_dirtyRect.isEmpty()) | |
| 208 return; | |
| 209 m_finalizeFrameTask.Cancel(); | |
| 210 m_dirtyRect = FloatRect(); | |
| 211 } | |
| 212 | |
| 213 void HTMLCanvasElement::finalizeFrameMicrotask() | |
| 214 { | |
| 215 // This method gets invoked if didDraw was called earlier in the current tas
k. | |
| 216 if (is3D()) { | |
| 217 didFinalizeFrame(); | |
| 218 } else { | |
| 219 ASSERT(hasImageBuffer()); | |
| 220 m_imageBuffer->finalizeFrame(); | |
| 221 } | |
| 222 ASSERT(m_finalizeFrameTask.IsCancelled()); | |
| 223 } | |
| 224 | |
| 225 void HTMLCanvasElement::notifyObserversCanvasChanged(const FloatRect& rect) | |
| 226 { | |
| 227 HashSet<RawPtr<CanvasObserver> >::iterator end = m_observers.end(); | |
| 228 for (HashSet<RawPtr<CanvasObserver> >::iterator it = m_observers.begin(); it
!= end; ++it) | |
| 229 (*it)->canvasChanged(this, rect); | |
| 230 } | |
| 231 | |
| 232 void HTMLCanvasElement::reset() | |
| 233 { | |
| 234 if (m_ignoreReset) | |
| 235 return; | |
| 236 | |
| 237 resetDirtyRect(); | |
| 238 | |
| 239 bool ok; | |
| 240 bool hadImageBuffer = hasImageBuffer(); | |
| 241 | |
| 242 int w = getAttribute(HTMLNames::widthAttr).toInt(&ok); | |
| 243 if (!ok || w < 0) | |
| 244 w = DefaultWidth; | |
| 245 | |
| 246 int h = getAttribute(HTMLNames::heightAttr).toInt(&ok); | |
| 247 if (!ok || h < 0) | |
| 248 h = DefaultHeight; | |
| 249 | |
| 250 if (m_contextStateSaver) { | |
| 251 // Reset to the initial graphics context state. | |
| 252 m_contextStateSaver->restore(); | |
| 253 m_contextStateSaver->save(); | |
| 254 } | |
| 255 | |
| 256 if (m_context && m_context->is2d()) | |
| 257 toCanvasRenderingContext2D(m_context.get())->reset(); | |
| 258 | |
| 259 IntSize oldSize = size(); | |
| 260 IntSize newSize(w, h); | |
| 261 | |
| 262 // If the size of an existing buffer matches, we can just clear it instead o
f reallocating. | |
| 263 // This optimization is only done for 2D canvases for now. | |
| 264 if (hadImageBuffer && oldSize == newSize && m_context && m_context->is2d())
{ | |
| 265 if (!m_didClearImageBuffer) | |
| 266 clearImageBuffer(); | |
| 267 return; | |
| 268 } | |
| 269 | |
| 270 setSurfaceSize(newSize); | |
| 271 | |
| 272 if (m_context && m_context->is3d() && oldSize != size()) | |
| 273 toWebGLRenderingContext(m_context.get())->reshape(width(), height()); | |
| 274 | |
| 275 if (RenderObject* renderer = this->renderer()) { | |
| 276 if (renderer->isCanvas()) { | |
| 277 if (hadImageBuffer) | |
| 278 document().scheduleVisualUpdate(); | |
| 279 } | |
| 280 } | |
| 281 | |
| 282 HashSet<RawPtr<CanvasObserver> >::iterator end = m_observers.end(); | |
| 283 for (HashSet<RawPtr<CanvasObserver> >::iterator it = m_observers.begin(); it
!= end; ++it) | |
| 284 (*it)->canvasResized(this); | |
| 285 } | |
| 286 | |
| 287 bool HTMLCanvasElement::paintsIntoCanvasBuffer() const | |
| 288 { | |
| 289 ASSERT(m_context); | |
| 290 return true; | |
| 291 } | |
| 292 | |
| 293 void HTMLCanvasElement::paint(GraphicsContext* context, const LayoutRect& r) | |
| 294 { | |
| 295 if (m_context) { | |
| 296 if (!paintsIntoCanvasBuffer()) | |
| 297 return; | |
| 298 m_context->paintRenderingResultsToCanvas(); | |
| 299 } | |
| 300 | |
| 301 if (hasImageBuffer()) { | |
| 302 ImageBuffer* imageBuffer = buffer(); | |
| 303 if (imageBuffer) { | |
| 304 CompositeOperator compositeOperator = !m_context || m_context->hasAl
pha() ? CompositeSourceOver : CompositeCopy; | |
| 305 if (m_presentedImage) | |
| 306 context->drawImage(m_presentedImage.get(), pixelSnappedIntRect(r
), compositeOperator, DoNotRespectImageOrientation); | |
| 307 else | |
| 308 context->drawImageBuffer(imageBuffer, pixelSnappedIntRect(r), 0,
compositeOperator); | |
| 309 } | |
| 310 } else { | |
| 311 // When alpha is false, we should draw to opaque black. | |
| 312 if (m_context && !m_context->hasAlpha()) | |
| 313 context->fillRect(FloatRect(r), Color(0, 0, 0)); | |
| 314 } | |
| 315 | |
| 316 if (is3D()) | |
| 317 toWebGLRenderingContext(m_context.get())->markLayerComposited(); | |
| 318 } | |
| 319 | |
| 320 bool HTMLCanvasElement::is3D() const | |
| 321 { | |
| 322 return m_context && m_context->is3d(); | |
| 323 } | |
| 324 | |
| 325 void HTMLCanvasElement::makePresentationCopy() | |
| 326 { | |
| 327 if (!m_presentedImage) { | |
| 328 // The buffer contains the last presented data, so save a copy of it. | |
| 329 m_presentedImage = buffer()->copyImage(CopyBackingStore, Unscaled); | |
| 330 updateExternallyAllocatedMemory(); | |
| 331 } | |
| 332 } | |
| 333 | |
| 334 void HTMLCanvasElement::clearPresentationCopy() | |
| 335 { | |
| 336 m_presentedImage.clear(); | |
| 337 updateExternallyAllocatedMemory(); | |
| 338 } | |
| 339 | |
| 340 void HTMLCanvasElement::setSurfaceSize(const IntSize& size) | |
| 341 { | |
| 342 m_size = size; | |
| 343 m_didFailToCreateImageBuffer = false; | |
| 344 discardImageBuffer(); | |
| 345 clearCopiedImage(); | |
| 346 if (m_context && m_context->is2d()) { | |
| 347 CanvasRenderingContext2D* context2d = toCanvasRenderingContext2D(m_conte
xt.get()); | |
| 348 if (context2d->isContextLost()) { | |
| 349 context2d->restoreContext(); | |
| 350 } | |
| 351 } | |
| 352 } | |
| 353 | |
| 354 String HTMLCanvasElement::toEncodingMimeType(const String& mimeType) | |
| 355 { | |
| 356 String lowercaseMimeType = mimeType.lower(); | |
| 357 | |
| 358 // FIXME: Make isSupportedImageMIMETypeForEncoding threadsafe (to allow this
method to be used on a worker thread). | |
| 359 if (mimeType.isNull() || !MIMETypeRegistry::isSupportedImageMIMETypeForEncod
ing(lowercaseMimeType)) | |
| 360 lowercaseMimeType = "image/png"; | |
| 361 | |
| 362 return lowercaseMimeType; | |
| 363 } | |
| 364 | |
| 365 const AtomicString HTMLCanvasElement::imageSourceURL() const | |
| 366 { | |
| 367 return AtomicString(toDataURLInternal("image/png", 0, true)); | |
| 368 } | |
| 369 | |
| 370 String HTMLCanvasElement::toDataURLInternal(const String& mimeType, const double
* quality, bool isSaving) const | |
| 371 { | |
| 372 if (m_size.isEmpty() || !buffer()) | |
| 373 return String("data:,"); | |
| 374 | |
| 375 String encodingMimeType = toEncodingMimeType(mimeType); | |
| 376 | |
| 377 // Try to get ImageData first, as that may avoid lossy conversions. | |
| 378 RefPtr<ImageData> imageData = getImageData(); | |
| 379 | |
| 380 if (imageData) | |
| 381 return ImageDataToDataURL(ImageDataBuffer(imageData->size(), imageData->
data()), encodingMimeType, quality); | |
| 382 | |
| 383 if (m_context && m_context->is3d()) { | |
| 384 toWebGLRenderingContext(m_context.get())->setSavingImage(isSaving); | |
| 385 m_context->paintRenderingResultsToCanvas(); | |
| 386 toWebGLRenderingContext(m_context.get())->setSavingImage(false); | |
| 387 } | |
| 388 | |
| 389 return buffer()->toDataURL(encodingMimeType, quality); | |
| 390 } | |
| 391 | |
| 392 String HTMLCanvasElement::toDataURL(const String& mimeType, const double* qualit
y, ExceptionState& exceptionState) const | |
| 393 { | |
| 394 return toDataURLInternal(mimeType, quality); | |
| 395 } | |
| 396 | |
| 397 PassRefPtr<ImageData> HTMLCanvasElement::getImageData() const | |
| 398 { | |
| 399 if (!m_context || !m_context->is3d()) | |
| 400 return nullptr; | |
| 401 return toWebGLRenderingContext(m_context.get())->paintRenderingResultsToImag
eData(); | |
| 402 } | |
| 403 | |
| 404 bool HTMLCanvasElement::shouldAccelerate(const IntSize& size) const | |
| 405 { | |
| 406 if (m_context && !m_context->is2d()) | |
| 407 return false; | |
| 408 | |
| 409 if (m_accelerationDisabled) | |
| 410 return false; | |
| 411 | |
| 412 Settings* settings = document().settings(); | |
| 413 if (!settings || !settings->accelerated2dCanvasEnabled()) | |
| 414 return false; | |
| 415 | |
| 416 // Do not use acceleration for small canvas. | |
| 417 if (size.width() * size.height() < settings->minimumAccelerated2dCanvasSize(
)) | |
| 418 return false; | |
| 419 | |
| 420 if (!blink::Platform::current()->canAccelerate2dCanvas()) | |
| 421 return false; | |
| 422 | |
| 423 return true; | |
| 424 } | |
| 425 | |
| 426 PassOwnPtr<ImageBufferSurface> HTMLCanvasElement::createImageBufferSurface(const
IntSize& deviceSize, int* msaaSampleCount) | |
| 427 { | |
| 428 OpacityMode opacityMode = !m_context || m_context->hasAlpha() ? NonOpaque :
Opaque; | |
| 429 | |
| 430 *msaaSampleCount = 0; | |
| 431 if (is3D()) | |
| 432 return adoptPtr(new WebGLImageBufferSurface(size(), opacityMode)); | |
| 433 | |
| 434 if (shouldAccelerate(deviceSize)) { | |
| 435 if (document().settings()) | |
| 436 *msaaSampleCount = document().settings()->accelerated2dCanvasMSAASam
pleCount(); | |
| 437 OwnPtr<ImageBufferSurface> surface = adoptPtr(new Canvas2DImageBufferSur
face(size(), opacityMode, *msaaSampleCount)); | |
| 438 if (surface->isValid()) | |
| 439 return surface.release(); | |
| 440 } | |
| 441 | |
| 442 return adoptPtr(new UnacceleratedImageBufferSurface(size(), opacityMode)); | |
| 443 } | |
| 444 | |
| 445 void HTMLCanvasElement::createImageBuffer() | |
| 446 { | |
| 447 createImageBufferInternal(); | |
| 448 if (m_didFailToCreateImageBuffer && m_context && m_context->is2d()) | |
| 449 toCanvasRenderingContext2D(m_context.get())->loseContext(); | |
| 450 } | |
| 451 | |
| 452 void HTMLCanvasElement::createImageBufferInternal() | |
| 453 { | |
| 454 ASSERT(!m_imageBuffer); | |
| 455 ASSERT(!m_contextStateSaver); | |
| 456 | |
| 457 m_didFailToCreateImageBuffer = true; | |
| 458 m_didClearImageBuffer = true; | |
| 459 | |
| 460 IntSize deviceSize = size(); | |
| 461 if (deviceSize.width() * deviceSize.height() > MaxCanvasArea) | |
| 462 return; | |
| 463 | |
| 464 if (deviceSize.width() > MaxSkiaDim || deviceSize.height() > MaxSkiaDim) | |
| 465 return; | |
| 466 | |
| 467 if (!deviceSize.width() || !deviceSize.height()) | |
| 468 return; | |
| 469 | |
| 470 int msaaSampleCount; | |
| 471 OwnPtr<ImageBufferSurface> surface = createImageBufferSurface(deviceSize, &m
saaSampleCount); | |
| 472 if (!surface->isValid()) | |
| 473 return; | |
| 474 | |
| 475 m_imageBuffer = ImageBuffer::create(surface.release()); | |
| 476 m_imageBuffer->setClient(this); | |
| 477 | |
| 478 m_didFailToCreateImageBuffer = false; | |
| 479 | |
| 480 updateExternallyAllocatedMemory(); | |
| 481 | |
| 482 if (is3D()) { | |
| 483 // Early out for WebGL canvases | |
| 484 return; | |
| 485 } | |
| 486 | |
| 487 m_imageBuffer->setClient(this); | |
| 488 m_imageBuffer->context()->setShouldClampToSourceRect(false); | |
| 489 m_imageBuffer->context()->disableAntialiasingOptimizationForHairlineImages()
; | |
| 490 m_imageBuffer->context()->setImageInterpolationQuality(CanvasDefaultInterpol
ationQuality); | |
| 491 // Enabling MSAA overrides a request to disable antialiasing. This is true r
egardless of whether the | |
| 492 // rendering mode is accelerated or not. For consistency, we don't want to a
pply AA in accelerated | |
| 493 // canvases but not in unaccelerated canvases. | |
| 494 if (!msaaSampleCount && document().settings() && !document().settings()->ant
ialiased2dCanvasEnabled()) | |
| 495 m_imageBuffer->context()->setShouldAntialias(false); | |
| 496 // GraphicsContext's defaults don't always agree with the 2d canvas spec. | |
| 497 // See CanvasRenderingContext2D::State::State() for more information. | |
| 498 m_imageBuffer->context()->setMiterLimit(10); | |
| 499 m_imageBuffer->context()->setStrokeThickness(1); | |
| 500 #if ENABLE(ASSERT) | |
| 501 m_imageBuffer->context()->disableDestructionChecks(); // 2D canvas is allowe
d to leave context in an unfinalized state. | |
| 502 #endif | |
| 503 m_contextStateSaver = adoptPtr(new GraphicsContextStateSaver(*m_imageBuffer-
>context())); | |
| 504 } | |
| 505 | |
| 506 void HTMLCanvasElement::notifySurfaceInvalid() | |
| 507 { | |
| 508 if (m_context && m_context->is2d()) { | |
| 509 CanvasRenderingContext2D* context2d = toCanvasRenderingContext2D(m_conte
xt.get()); | |
| 510 context2d->loseContext(); | |
| 511 } | |
| 512 } | |
| 513 | |
| 514 void HTMLCanvasElement::updateExternallyAllocatedMemory() const | |
| 515 { | |
| 516 int bufferCount = 0; | |
| 517 if (m_imageBuffer) | |
| 518 bufferCount++; | |
| 519 if (is3D()) | |
| 520 bufferCount += 2; | |
| 521 if (m_copiedImage) | |
| 522 bufferCount++; | |
| 523 if (m_presentedImage) | |
| 524 bufferCount++; | |
| 525 | |
| 526 Checked<intptr_t, RecordOverflow> checkedExternallyAllocatedMemory = 4 * buf
ferCount; | |
| 527 checkedExternallyAllocatedMemory *= width(); | |
| 528 checkedExternallyAllocatedMemory *= height(); | |
| 529 intptr_t externallyAllocatedMemory; | |
| 530 if (checkedExternallyAllocatedMemory.safeGet(externallyAllocatedMemory) == C
heckedState::DidOverflow) | |
| 531 externallyAllocatedMemory = std::numeric_limits<intptr_t>::max(); | |
| 532 | |
| 533 // Subtracting two intptr_t that are known to be positive will never underfl
ow. | |
| 534 m_externallyAllocatedMemory = externallyAllocatedMemory; | |
| 535 } | |
| 536 | |
| 537 GraphicsContext* HTMLCanvasElement::drawingContext() const | |
| 538 { | |
| 539 return buffer() ? m_imageBuffer->context() : 0; | |
| 540 } | |
| 541 | |
| 542 GraphicsContext* HTMLCanvasElement::existingDrawingContext() const | |
| 543 { | |
| 544 if (!hasImageBuffer()) | |
| 545 return 0; | |
| 546 | |
| 547 return drawingContext(); | |
| 548 } | |
| 549 | |
| 550 ImageBuffer* HTMLCanvasElement::buffer() const | |
| 551 { | |
| 552 if (!hasImageBuffer() && !m_didFailToCreateImageBuffer) | |
| 553 const_cast<HTMLCanvasElement*>(this)->createImageBuffer(); | |
| 554 return m_imageBuffer.get(); | |
| 555 } | |
| 556 | |
| 557 void HTMLCanvasElement::ensureUnacceleratedImageBuffer() | |
| 558 { | |
| 559 if ((hasImageBuffer() && !m_imageBuffer->isAccelerated()) || m_didFailToCrea
teImageBuffer) | |
| 560 return; | |
| 561 discardImageBuffer(); | |
| 562 OpacityMode opacityMode = !m_context || m_context->hasAlpha() ? NonOpaque :
Opaque; | |
| 563 m_imageBuffer = ImageBuffer::create(size(), opacityMode); | |
| 564 m_didFailToCreateImageBuffer = !m_imageBuffer; | |
| 565 } | |
| 566 | |
| 567 Image* HTMLCanvasElement::copiedImage() const | |
| 568 { | |
| 569 if (!m_copiedImage && buffer()) { | |
| 570 if (m_context && m_context->is3d()) { | |
| 571 toWebGLRenderingContext(m_context.get())->setSavingImage(true); | |
| 572 m_context->paintRenderingResultsToCanvas(); | |
| 573 toWebGLRenderingContext(m_context.get())->setSavingImage(false); | |
| 574 } | |
| 575 m_copiedImage = buffer()->copyImage(CopyBackingStore, Unscaled); | |
| 576 updateExternallyAllocatedMemory(); | |
| 577 } | |
| 578 return m_copiedImage.get(); | |
| 579 } | |
| 580 | |
| 581 void HTMLCanvasElement::clearImageBuffer() | |
| 582 { | |
| 583 ASSERT(hasImageBuffer() && !m_didFailToCreateImageBuffer); | |
| 584 ASSERT(!m_didClearImageBuffer); | |
| 585 ASSERT(m_context); | |
| 586 | |
| 587 m_didClearImageBuffer = true; | |
| 588 | |
| 589 if (m_context->is2d()) { | |
| 590 // No need to undo transforms/clip/etc. because we are called right | |
| 591 // after the context is reset. | |
| 592 toCanvasRenderingContext2D(m_context.get())->clearRect(0, 0, width(), he
ight()); | |
| 593 } | |
| 594 } | |
| 595 | |
| 596 void HTMLCanvasElement::discardImageBuffer() | |
| 597 { | |
| 598 m_contextStateSaver.clear(); // uses context owned by m_imageBuffer | |
| 599 m_imageBuffer.clear(); | |
| 600 resetDirtyRect(); | |
| 601 updateExternallyAllocatedMemory(); | |
| 602 } | |
| 603 | |
| 604 bool HTMLCanvasElement::hasValidImageBuffer() const | |
| 605 { | |
| 606 return m_imageBuffer && m_imageBuffer->isSurfaceValid(); | |
| 607 } | |
| 608 | |
| 609 void HTMLCanvasElement::clearCopiedImage() | |
| 610 { | |
| 611 if (m_copiedImage) { | |
| 612 m_copiedImage.clear(); | |
| 613 updateExternallyAllocatedMemory(); | |
| 614 } | |
| 615 m_didClearImageBuffer = false; | |
| 616 } | |
| 617 | |
| 618 AffineTransform HTMLCanvasElement::baseTransform() const | |
| 619 { | |
| 620 ASSERT(hasImageBuffer() && !m_didFailToCreateImageBuffer); | |
| 621 return m_imageBuffer->baseTransform(); | |
| 622 } | |
| 623 | |
| 624 PassRefPtr<Image> HTMLCanvasElement::getSourceImageForCanvas(SourceImageMode mod
e, SourceImageStatus* status) const | |
| 625 { | |
| 626 if (!width() || !height()) { | |
| 627 *status = ZeroSizeCanvasSourceImageStatus; | |
| 628 return nullptr; | |
| 629 } | |
| 630 | |
| 631 if (!buffer()) { | |
| 632 *status = InvalidSourceImageStatus; | |
| 633 return nullptr; | |
| 634 } | |
| 635 | |
| 636 if (mode == CopySourceImageIfVolatile) { | |
| 637 *status = NormalSourceImageStatus; | |
| 638 return copiedImage(); | |
| 639 } | |
| 640 | |
| 641 if (m_context && m_context->is3d()) { | |
| 642 m_context->paintRenderingResultsToCanvas(); | |
| 643 *status = ExternalSourceImageStatus; | |
| 644 } else { | |
| 645 *status = NormalSourceImageStatus; | |
| 646 } | |
| 647 return m_imageBuffer->copyImage(DontCopyBackingStore, Unscaled); | |
| 648 } | |
| 649 | |
| 650 FloatSize HTMLCanvasElement::sourceSize() const | |
| 651 { | |
| 652 return FloatSize(width(), height()); | |
| 653 } | |
| 654 | |
| 655 } | |
| OLD | NEW |