1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
|
/*
* Copyright (C) 2017 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "CanvasBase.h"
#include "ByteArrayPixelBuffer.h"
#include "CanvasRenderingContext.h"
#include "Chrome.h"
#include "Document.h"
#include "Element.h"
#include "GraphicsClient.h"
#include "GraphicsContext.h"
#include "HTMLCanvasElement.h"
#include "HostWindow.h"
#include "ImageBuffer.h"
#include "InspectorInstrumentation.h"
#include "IntRect.h"
#include "NoiseInjectionPolicy.h"
#include "RenderElement.h"
#include "ScriptTelemetryCategory.h"
#include "StyleCanvasImage.h"
#include "WebCoreOpaqueRoot.h"
#include "WorkerClient.h"
#include "WorkerGlobalScope.h"
#include <JavaScriptCore/JSCInlines.h>
#include <JavaScriptCore/JSLock.h>
#include <atomic>
#include <wtf/Vector.h>
#include <wtf/WeakRandom.h>
#include <wtf/text/MakeString.h>
namespace WebCore {
constexpr InterpolationQuality defaultInterpolationQuality = InterpolationQuality::Low;
static std::optional<size_t> maxCanvasAreaForTesting;
static std::optional<uint64_t> canvasNoiseHashSaltIfNeeded(ScriptExecutionContext& context)
{
auto policies = context.noiseInjectionPolicies();
if (policies.contains(NoiseInjectionPolicy::Minimal)
|| (policies.contains(NoiseInjectionPolicy::Enhanced) && context.requiresScriptExecutionTelemetry(ScriptTelemetryCategory::Canvas)))
return context.noiseInjectionHashSalt();
return { };
}
CanvasBase::CanvasBase(IntSize size, ScriptExecutionContext& context)
: m_size { size }
, m_canvasNoiseHashSalt { canvasNoiseHashSaltIfNeeded(context) }
{
}
CanvasBase::~CanvasBase()
{
ASSERT(m_didNotifyObserversCanvasDestroyed);
ASSERT(m_observers.isEmptyIgnoringNullReferences());
ASSERT(!m_imageBuffer);
m_canvasNoiseHashSalt = std::nullopt;
}
ImageBuffer* CanvasBase::buffer() const
{
if (!hasCreatedImageBuffer())
createImageBuffer();
return m_imageBuffer.get();
}
RefPtr<ImageBuffer> CanvasBase::makeRenderingResultsAvailable(ShouldApplyPostProcessingToDirtyRect shouldApplyPostProcessingToDirtyRect)
{
if (auto* context = renderingContext()) {
RefPtr buffer = context->surfaceBufferToImageBuffer(CanvasRenderingContext::SurfaceBuffer::DrawingBuffer);
if (m_canvasNoiseHashSalt && shouldApplyPostProcessingToDirtyRect == ShouldApplyPostProcessingToDirtyRect::Yes)
m_canvasNoiseInjection.postProcessDirtyCanvasBuffer(buffer.get(), *m_canvasNoiseHashSalt, context->is2d() ? CanvasNoiseInjectionPostProcessArea::DirtyRect : CanvasNoiseInjectionPostProcessArea::FullBuffer);
return buffer;
}
return buffer();
}
size_t CanvasBase::memoryCost() const
{
// May be called from GC threads.
return m_imageBufferMemoryCost.load(std::memory_order_relaxed);
}
#if ENABLE(RESOURCE_USAGE)
size_t CanvasBase::externalMemoryCost() const
{
// For the purposes of Web Inspector, external memory means memory reported as 1) being traceable from JS objects, i.e. GC owned memory
// 2) not allocated from "Page" category, e.g. from bmalloc.
return memoryCost();
}
#endif
static inline size_t maxCanvasArea()
{
if (maxCanvasAreaForTesting)
return *maxCanvasAreaForTesting;
// Firefox limits width/height to 32767 pixels, but slows down dramatically before it
// reaches that limit. We limit by area instead, giving us larger maximum dimensions,
// in exchange for a smaller maximum canvas size. The maximum canvas size is in device pixels.
#if PLATFORM(IOS_FAMILY)
return 8192 * 8192;
#else
return 16384 * 16384;
#endif
}
void CanvasBase::setMaxCanvasAreaForTesting(std::optional<size_t> size)
{
maxCanvasAreaForTesting = size;
}
void CanvasBase::addObserver(CanvasObserver& observer)
{
m_observers.add(observer);
if (is<StyleCanvasImage>(observer))
InspectorInstrumentation::didChangeCSSCanvasClientNodes(*this);
}
void CanvasBase::removeObserver(CanvasObserver& observer)
{
m_observers.remove(observer);
if (is<StyleCanvasImage>(observer))
InspectorInstrumentation::didChangeCSSCanvasClientNodes(*this);
}
bool CanvasBase::hasObserver(CanvasObserver& observer) const
{
return m_observers.contains(observer);
}
void CanvasBase::notifyObserversCanvasChanged(const FloatRect& rect)
{
for (auto& observer : m_observers)
observer.canvasChanged(*this, rect);
}
void CanvasBase::didDraw(const std::optional<FloatRect>& rect, ShouldApplyPostProcessingToDirtyRect shouldApplyPostProcessingToDirtyRect)
{
addCanvasNeedingPreparationForDisplayOrFlush();
IntRect dirtyRect { { }, size() };
if (rect)
dirtyRect.intersect(enclosingIntRect(*rect));
notifyObserversCanvasChanged(dirtyRect);
// FIXME: We should exclude rects with ShouldApplyPostProcessingToDirtyRect::No
if (shouldInjectNoiseBeforeReadback()) {
if (shouldApplyPostProcessingToDirtyRect == ShouldApplyPostProcessingToDirtyRect::Yes) {
m_canvasNoiseInjection.updateDirtyRect(dirtyRect);
} else if (!rect)
m_canvasNoiseInjection.clearDirtyRect();
}
}
void CanvasBase::notifyObserversCanvasResized()
{
for (auto& observer : m_observers)
observer.canvasResized(*this);
}
void CanvasBase::notifyObserversCanvasDestroyed()
{
ASSERT(!m_didNotifyObserversCanvasDestroyed);
for (auto& observer : std::exchange(m_observers, WeakHashSet<CanvasObserver>()))
observer.canvasDestroyed(*this);
#if ASSERT_ENABLED
m_didNotifyObserversCanvasDestroyed = true;
#endif
}
void CanvasBase::addDisplayBufferObserver(CanvasDisplayBufferObserver& observer)
{
m_displayBufferObservers.add(observer);
}
void CanvasBase::removeDisplayBufferObserver(CanvasDisplayBufferObserver& observer)
{
m_displayBufferObservers.remove(observer);
}
void CanvasBase::notifyObserversCanvasDisplayBufferPrepared()
{
for (auto& observer : m_displayBufferObservers)
observer.canvasDisplayBufferPrepared(*this);
}
UncheckedKeyHashSet<Element*> CanvasBase::cssCanvasClients() const
{
UncheckedKeyHashSet<Element*> cssCanvasClients;
for (auto& observer : m_observers) {
auto* image = dynamicDowncast<StyleCanvasImage>(observer);
if (!image)
continue;
for (auto entry : image->clients()) {
auto& client = entry.key;
if (auto element = client.element())
cssCanvasClients.add(element);
}
}
return cssCanvasClients;
}
bool CanvasBase::hasActiveInspectorCanvasCallTracer() const
{
auto* context = renderingContext();
return context && context->hasActiveInspectorCanvasCallTracer();
}
void CanvasBase::setSize(const IntSize& size)
{
if (size == m_size)
return;
m_size = size;
if (auto* context = renderingContext())
InspectorInstrumentation::didChangeCanvasSize(*context);
}
RefPtr<ImageBuffer> CanvasBase::setImageBuffer(RefPtr<ImageBuffer>&& buffer) const
{
m_contextStateSaver = nullptr;
RefPtr returnBuffer = std::exchange(m_imageBuffer, WTFMove(buffer));
IntSize oldSize = m_size;
size_t oldMemoryCost = m_imageBufferMemoryCost.load(std::memory_order_relaxed);
size_t newMemoryCost = 0;
if (m_imageBuffer) {
m_size = m_imageBuffer->truncatedLogicalSize();
newMemoryCost = m_imageBuffer->memoryCost();
m_imageBuffer->context().setShadowsIgnoreTransforms(true);
m_imageBuffer->context().setImageInterpolationQuality(defaultInterpolationQuality);
m_imageBuffer->context().setStrokeThickness(1);
m_contextStateSaver = makeUnique<GraphicsContextStateSaver>(m_imageBuffer->context());
}
m_imageBufferMemoryCost.store(newMemoryCost, std::memory_order_relaxed);
if (newMemoryCost) {
if (RefPtr scriptExecutionContext = this->scriptExecutionContext()) {
JSC::JSLockHolder lock(scriptExecutionContext->vm());
scriptExecutionContext->vm().heap.reportExtraMemoryAllocated(static_cast<JSCell*>(nullptr), newMemoryCost);
}
}
if (auto* context = renderingContext()) {
if (oldSize != m_size)
InspectorInstrumentation::didChangeCanvasSize(*context);
if (oldMemoryCost != newMemoryCost)
InspectorInstrumentation::didChangeCanvasMemory(*context);
}
return returnBuffer;
}
bool CanvasBase::shouldAccelerate(const IntSize& size) const
{
return shouldAccelerate(size.unclampedArea());
}
bool CanvasBase::shouldAccelerate(uint64_t area) const
{
#if USE(CA) || USE(SKIA)
if (!scriptExecutionContext()->settingsValues().canvasUsesAcceleratedDrawing)
return false;
if (area < scriptExecutionContext()->settingsValues().minimumAccelerated2DContextArea)
return false;
#if PLATFORM(GTK)
if (!scriptExecutionContext()->settingsValues().acceleratedCompositingEnabled)
return false;
#endif
return true;
#else
UNUSED_PARAM(area);
return false;
#endif
}
RefPtr<ImageBuffer> CanvasBase::allocateImageBuffer() const
{
uint64_t area = size().unclampedArea();
if (!area)
return nullptr;
if (area > maxCanvasArea()) {
auto message = makeString("Canvas area exceeds the maximum limit (width * height > "_s, maxCanvasArea(), ")."_s);
scriptExecutionContext()->addConsoleMessage(MessageSource::JS, MessageLevel::Warning, message);
return nullptr;
}
auto* context = renderingContext();
bool willReadFrequently = context ? context->willReadFrequently() : false;
RenderingMode renderingMode;
if (auto renderingModeForTesting = context ? context->renderingModeForTesting() : std::nullopt)
renderingMode = *renderingModeForTesting;
else
renderingMode = !willReadFrequently && shouldAccelerate(area) ? RenderingMode::Accelerated : RenderingMode::Unaccelerated;
auto colorSpace = context ? context->colorSpace() : DestinationColorSpace::SRGB();
auto pixelFormat = context ? context->pixelFormat() : ImageBufferPixelFormat::BGRA8;
return ImageBuffer::create(size(), renderingMode, RenderingPurpose::Canvas, 1, colorSpace, pixelFormat, scriptExecutionContext()->graphicsClient());
}
bool CanvasBase::shouldInjectNoiseBeforeReadback() const
{
// Note, every early-return resulting from this check potentially leaks this state. This is a risk that we're accepting right now.
return !!m_canvasNoiseHashSalt;
}
void CanvasBase::recordLastFillText(const String& text)
{
if (!shouldInjectNoiseBeforeReadback())
return;
m_lastFillText = text;
}
void CanvasBase::addCanvasNeedingPreparationForDisplayOrFlush()
{
auto* context = renderingContext();
if (!context)
return;
if (context->isInPreparationForDisplayOrFlush())
return;
if (auto* document = dynamicDowncast<Document>(scriptExecutionContext()))
document->addCanvasNeedingPreparationForDisplayOrFlush(*context);
// FIXME: WorkerGlobalContext does not have prepare phase yet.
}
void CanvasBase::removeCanvasNeedingPreparationForDisplayOrFlush()
{
auto* context = renderingContext();
if (!context)
return;
if (!context->isInPreparationForDisplayOrFlush())
return;
if (auto* document = dynamicDowncast<Document>(scriptExecutionContext()))
document->removeCanvasNeedingPreparationForDisplayOrFlush(*context);
// FIXME: WorkerGlobalContext does not have prepare phase yet.
}
bool CanvasBase::postProcessPixelBufferResults(Ref<PixelBuffer>&& pixelBuffer) const
{
if (m_canvasNoiseHashSalt)
return m_canvasNoiseInjection.postProcessPixelBufferResults(std::forward<Ref<PixelBuffer>>(pixelBuffer), *m_canvasNoiseHashSalt);
return false;
}
RefPtr<ImageBuffer> CanvasBase::createImageForNoiseInjection() const
{
RefPtr context = canvasBaseScriptExecutionContext();
if (!context)
return { };
auto seed = static_cast<unsigned>(context->noiseInjectionHashSalt().value_or(0));
auto buffer = ImageBuffer::create(size(), RenderingMode::Unaccelerated, RenderingPurpose::Unspecified, 1, DestinationColorSpace::SRGB(), ImageBufferPixelFormat::BGRA8);
if (!buffer)
return { };
auto componentValues = WeakRandom { seed }.getUint32();
auto fillColor = SRGBA<uint8_t> {
static_cast<uint8_t>((componentValues >> 24) & 0xFF),
static_cast<uint8_t>((componentValues >> 16) & 0xFF),
static_cast<uint8_t>((componentValues >> 8) & 0xFF),
static_cast<uint8_t>(componentValues & 0xFF),
};
buffer->context().setFillColor(fillColor);
buffer->context().fillRect({ IntPoint { }, size() });
return buffer;
}
void CanvasBase::resetGraphicsContextState() const
{
if (m_contextStateSaver) {
// Reset to the initial graphics context state.
m_contextStateSaver->restore();
m_contextStateSaver->save();
}
}
WebCoreOpaqueRoot root(CanvasBase* canvas)
{
return WebCoreOpaqueRoot { canvas };
}
} // namespace WebCore
|