webkit引擎源码
CanvasRenderingContext2D* HTMLCanvasElement::createContext2d(const String& type)
{
ASSERT_UNUSED(HTMLCanvasElement::is2dType(type), type);
ASSERT(!m_context);
bool usesDashboardCompatibilityMode = false;
#if ENABLE(DASHBOARD_SUPPORT)
usesDashboardCompatibilityMode = document().settings().usesDashboardBackwardCompatibilityMode();
#endif
// Make sure we don't use more pixel memory than the system can support.
size_t requestedPixelMemory = 4 * width() * height();//计算占用内存
if (activePixelMemory + requestedPixelMemory > maxActivePixelMemory()) {
StringBuilder stringBuilder;
stringBuilder.appendLiteral("Total canvas memory use exceeds the maximum limit (");
stringBuilder.appendNumber(maxActivePixelMemory() / 1024 / 1024);
stringBuilder.appendLiteral(" MB).");
document().addConsoleMessage(MessageSource::JS, MessageLevel::Warning, stringBuilder.toString());
return nullptr;
}
m_context = CanvasRenderingContext2D::create(*this, document().inQuirksMode(), usesDashboardCompatibilityMode);
downcast<CanvasRenderingContext2D>(*m_context).setUsesDisplayListDrawing(m_usesDisplayListDrawing);
downcast<CanvasRenderingContext2D>(*m_context).setTracksDisplayListReplay(m_tracksDisplayListReplay);
#if USE(IOSURFACE_CANVAS_BACKING_STORE) || ENABLE(ACCELERATED_2D_CANVAS)
// Need to make sure a RenderLayer and compositing layer get created for the Canvas.
invalidateStyleAndLayerComposition();
#endif
return static_cast<CanvasRenderingContext2D*>(m_context.get());
}
100 * 100 canvas 占的内存是 100 * 100 * 4 bytes = 40,000 bytes
https://github.com/WebKit/webkit/blob/master/Source/WebCore/html/HTMLCanvasElement.cpp#L365
网友评论