Modified: trunk/Source/WebCore/platform/graphics/win/Direct2DOperations.cpp (250084 => 250085)
--- trunk/Source/WebCore/platform/graphics/win/Direct2DOperations.cpp 2019-09-19 04:36:22 UTC (rev 250084)
+++ trunk/Source/WebCore/platform/graphics/win/Direct2DOperations.cpp 2019-09-19 04:53:49 UTC (rev 250085)
@@ -667,9 +667,12 @@
if (!rectToClear.intersects(renderTargetRect))
return;
+ rectToClear.intersect(renderTargetRect);
+
platformContext.setTags(1, __LINE__);
- rectToClear.intersect(renderTargetRect);
- renderTarget->FillRectangle(rectToClear, platformContext.brushWithColor(Color(D2D1::ColorF(0, 0, 0, 0))).get());
+ renderTarget->PushAxisAlignedClip(rectToClear, D2D1_ANTIALIAS_MODE_PER_PRIMITIVE);
+ renderTarget->Clear(D2D1::ColorF(0, 0, 0, 0));
+ renderTarget->PopAxisAlignedClip();
});
}
@@ -679,7 +682,7 @@
auto renderTarget = platformContext.renderTarget();
const FontPlatformData& platformData = font.platformData();
- platformContext.save();
+ PlatformContextStateSaver stateSaver(platformContext);
D2D1_MATRIX_3X2_F matrix;
renderTarget->GetTransform(&matrix);
@@ -737,7 +740,7 @@
// if (hasSimpleShadow)
// graphicsContext.setShadow(shadowOffset, shadowBlur, shadowColor);
- platformContext.restore();
+ stateSaver.restore();
platformContext.notifyPostDrawObserver();
}
@@ -991,7 +994,6 @@
ASSERT(platformContext.renderTarget());
D2D1_TAG first, second;
HRESULT hr = platformContext.renderTarget()->Flush(&first, &second);
- platformContext.notifyPostDrawObserver();
RELEASE_ASSERT(SUCCEEDED(hr));
}
@@ -1166,13 +1168,8 @@
void clipPath(PlatformContextDirect2D& platformContext, ID2D1Geometry* path)
{
- ASSERT(platformContext.hasSavedState());
- if (!platformContext.hasSavedState())
- return;
-
COMPtr<ID2D1Layer> clipLayer;
HRESULT hr = platformContext.renderTarget()->CreateLayer(&clipLayer);
- ASSERT(SUCCEEDED(hr));
if (!SUCCEEDED(hr))
return;
Modified: trunk/Source/WebCore/platform/graphics/win/ImageBufferDataDirect2D.cpp (250084 => 250085)
--- trunk/Source/WebCore/platform/graphics/win/ImageBufferDataDirect2D.cpp 2019-09-19 04:36:22 UTC (rev 250084)
+++ trunk/Source/WebCore/platform/graphics/win/ImageBufferDataDirect2D.cpp 2019-09-19 04:53:49 UTC (rev 250085)
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2016-2018 Apple Inc. All rights reserved.
+ * Copyright (C) 2016-2019 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
@@ -79,85 +79,114 @@
}
}
-bool ImageBufferData::ensureBackingStore(const IntSize& size) const
+static bool copyRectFromSourceToDest(const IntRect& sourceRect, const IntSize& sourceBufferSize, const uint8_t* source, const IntSize& destBufferSize, uint8_t* dest, const IntPoint& destBufferPosition)
{
- if (size == backingStoreSize && !data.isEmpty())
- return true;
+ if (!IntRect({ }, destBufferSize).contains(IntRect(destBufferPosition, sourceRect.size())))
+ return false;
- auto numBytes = size.area<RecordOverflow>() * 4;
- if (numBytes.hasOverflowed())
+ if (!IntRect({ }, sourceBufferSize).contains(sourceRect))
return false;
- backingStoreSize = size;
- data.resizeToFit(numBytes.unsafeGet());
- return data.capacity() == numBytes.unsafeGet();
+ unsigned srcStride = sourceBufferSize.width() * 4;
+ unsigned destStride = destBufferSize.width() * 4;
+
+ unsigned srcRowOffset = sourceRect.location().x() * 4;
+ unsigned destRowOffset = destBufferPosition.x() * 4;
+
+ const uint8_t* srcRows = source + srcStride * sourceRect.location().y();
+ uint8_t* destRows = dest + destStride * destBufferPosition.y();
+
+ const unsigned bytesPerRow = sourceRect.width() * 4;
+ for (unsigned y = 0; y < sourceRect.height(); ++y) {
+ const uint8_t* srcRow = srcRows + srcStride * y + srcRowOffset;
+ uint8_t* destRow = destRows + destStride * y + destRowOffset;
+ memcpy(destRow, srcRow, bytesPerRow);
+ }
+
+ return true;
}
-RefPtr<Uint8ClampedArray> ImageBufferData::getData(AlphaPremultiplication desiredFormat, const IntRect& rect, const IntSize& size, bool /* accelerateRendering */, float /* resolutionScale */) const
+// Note: Assumes that bytes are already in RGBA format
+template <AlphaPremultiplication desiredFormat>
+bool copyRectFromSourceToDestAndSetPremultiplication(const IntRect& sourceRect, const IntSize& sourceBufferSize, const uint8_t* source, const IntSize& destBufferSize, uint8_t* dest, const IntPoint& destBufferPosition)
{
- auto numBytes = rect.area<RecordOverflow>() * 4;
- if (numBytes.hasOverflowed())
- return nullptr;
+ if (!IntRect({ }, destBufferSize).contains(IntRect(destBufferPosition, sourceRect.size())))
+ return false;
- auto result = Uint8ClampedArray::tryCreateUninitialized(numBytes.unsafeGet());
- if (!result)
- return nullptr;
+ if (!IntRect({ }, sourceBufferSize).contains(sourceRect))
+ return false;
- if (!bitmap)
- return result;
+ unsigned srcStride = sourceBufferSize.width() * 4;
+ unsigned destStride = destBufferSize.width() * 4;
- auto bytesPerRowInData = rect.width() * 4;
- auto height = rect.height();
- auto width = rect.width();
+ unsigned srcRowOffset = sourceRect.location().x() * 4;
+ unsigned destRowOffset = destBufferPosition.x() * 4;
- if (bitmapBufferSync == BitmapBufferSync::BufferOutOfSync || data.size() != numBytes.unsafeGet()) {
- context->endDraw();
+ const uint8_t* srcRows = source + srcStride * sourceRect.location().y();
+ uint8_t* destRows = dest + destStride * destBufferPosition.y();
- COMPtr<ID2D1DeviceContext> d2dDeviceContext(Query, platformContext->renderTarget());
- ASSERT(!!d2dDeviceContext);
+ const unsigned bytesPerRow = sourceRect.width() * 4;
+ for (unsigned y = 0; y < sourceRect.height(); ++y) {
+ const uint8_t* srcRow = srcRows + srcStride * y + srcRowOffset;
+ uint8_t* destRow = destRows + destStride * y + destRowOffset;
- // Copy GPU data from the ID2D1Bitmap to a CPU-backed ID2D1Bitmap1
- COMPtr<ID2D1Bitmap1> cpuBitmap;
- D2D1_BITMAP_PROPERTIES1 bitmapProperties2 = D2D1::BitmapProperties1(D2D1_BITMAP_OPTIONS_CPU_READ | D2D1_BITMAP_OPTIONS_CANNOT_DRAW, Direct2D::pixelFormat());
- HRESULT hr = d2dDeviceContext->CreateBitmap(size, nullptr, bytesPerRowInData, bitmapProperties2, &cpuBitmap);
- if (!SUCCEEDED(hr))
- return nullptr;
+ for (size_t x = 0; x < sourceRect.width(); ++x) {
+ unsigned pos = x * 4;
- auto targetPos = D2D1::Point2U();
- D2D1_RECT_U dataRect = rect;
- hr = cpuBitmap->CopyFromBitmap(&targetPos, bitmap.get(), &dataRect);
- if (!SUCCEEDED(hr))
- return nullptr;
+ unsigned red = srcRow[x];
+ unsigned green = srcRow[x + 1];
+ unsigned blue = srcRow[x + 2];
+ unsigned alpha = srcRow[x + 3];
- D2D1_MAPPED_RECT mappedData;
- hr = cpuBitmap->Map(D2D1_MAP_OPTIONS_READ, &mappedData);
- if (!SUCCEEDED(hr))
- return nullptr;
+ if (desiredFormat == AlphaPremultiplication::Premultiplied) {
+ if (alpha != 255) {
+ red = (red * alpha + 254) / 255;
+ green = (green * alpha + 254) / 255;
+ blue = (blue * alpha + 254) / 255;
+ }
+ } else {
+ if (alpha && alpha != 255) {
+ red = red * 255 / alpha;
+ green = green * 255 / alpha;
+ blue = blue * 255 / alpha;
+ }
+ }
- if (mappedData.bits) {
- if (!ensureBackingStore(bitmap->GetPixelSize()))
- return nullptr;
+ uint32_t* pixel = reinterpret_cast<uint32_t*>(destRow) + x;
+ *pixel = (alpha << 24) | red << 16 | green << 8 | blue;
+ }
+ }
- // Software filters expect RGBA bytes. We need to swizzle from Direct2D's BGRA to be compatible.
- if (desiredFormat == AlphaPremultiplication::Unpremultiplied)
- swizzleAndPremultiply<AlphaPremultiplication::Unpremultiplied>(mappedData.bits, height, width, mappedData.pitch, bytesPerRowInData, data.data());
- else
- swizzleAndPremultiply<AlphaPremultiplication::Premultiplied>(mappedData.bits, height, width, mappedData.pitch, bytesPerRowInData, data.data());
+ return true;
+}
- byteFormat = desiredFormat;
+bool ImageBufferData::copyRectFromData(const IntRect& rect, RefPtr<Uint8ClampedArray>& result) const
+{
+ auto rawBitmapSize = bitmap->GetSize();
- bitmapBufferSync = BitmapBufferSync::InSync;
- }
+ float scaleFactor = backingStoreSize.width() / rawBitmapSize.width;
- hr = cpuBitmap->Unmap();
- ASSERT(SUCCEEDED(hr));
+ IntRect scaledRect = rect;
+ scaledRect.scale(scaleFactor);
- context->beginDraw();
- }
+ if (!IntRect(IntPoint(), backingStoreSize).contains(scaledRect))
+ return false;
- result->setRange(data.data(), data.size(), 0);
+ return copyRectFromSourceToDest(scaledRect, backingStoreSize, data.data(), rect.size(), result->data(), IntPoint());
+}
- return result;
+bool ImageBufferData::ensureBackingStore(const IntSize& size) const
+{
+ if (size == backingStoreSize && !data.isEmpty())
+ return true;
+
+ auto numBytes = size.area<RecordOverflow>() * 4;
+ if (numBytes.hasOverflowed())
+ return false;
+
+ backingStoreSize = size;
+ data.resizeToFit(numBytes.unsafeGet());
+ return data.capacity() == numBytes.unsafeGet();
}
// Swizzle the red and blue bytes of the pixels in a buffer
@@ -190,6 +219,138 @@
}
}
+// Note: Assumes that bytes are already in RGBA format
+template <AlphaPremultiplication desiredFormat>
+void inPlaceChangePremultiplication(uint8_t* byteData, unsigned byteCount)
+{
+ size_t pixelCount = byteCount / 4;
+
+ for (size_t i = 0; i < pixelCount; ++i) {
+ unsigned x = pixelCount * 4;
+
+ unsigned red = byteData[x];
+ unsigned green = byteData[x + 1];
+ unsigned blue = byteData[x + 2];
+ unsigned alpha = byteData[x + 3];
+
+ if (desiredFormat == AlphaPremultiplication::Premultiplied) {
+ if (alpha != 255) {
+ red = (red * alpha + 254) / 255;
+ green = (green * alpha + 254) / 255;
+ blue = (blue * alpha + 254) / 255;
+ }
+ } else {
+ if (alpha && alpha != 255) {
+ red = red * 255 / alpha;
+ green = green * 255 / alpha;
+ blue = blue * 255 / alpha;
+ }
+ }
+
+ uint32_t* pixel = reinterpret_cast<uint32_t*>(byteData) + i;
+ *pixel = (alpha << 24) | red << 16 | green << 8 | blue;
+ }
+}
+
+RefPtr<Uint8ClampedArray> ImageBufferData::getData(AlphaPremultiplication desiredFormat, const IntRect& rect, const IntSize& size, bool /* accelerateRendering */, float /* resolutionScale */) const
+{
+ auto numBytes = rect.area<RecordOverflow>() * 4;
+ if (numBytes.hasOverflowed())
+ return nullptr;
+
+ auto result = Uint8ClampedArray::tryCreateUninitialized(numBytes.unsafeGet());
+ if (!result)
+ return nullptr;
+
+ if (!bitmap)
+ return result;
+
+ if (!readDataFromBitmapIfNeeded(desiredFormat))
+ return nullptr;
+
+ auto rawBitmapSize = bitmap->GetSize();
+ IntSize bitmapSize(clampTo<int>(rawBitmapSize.width), clampTo<int>(rawBitmapSize.height));
+
+ if (rect.location() == IntPoint() && rect.size() == bitmapSize) {
+ RELEASE_ASSERT(numBytes.unsafeGet() == data.size());
+ result->setRange(data.data(), data.size(), 0);
+ } else {
+ // Only want part of the bitmap.
+ if (!copyRectFromData(rect, result))
+ return nullptr;
+ }
+
+ if (byteFormat != desiredFormat) {
+ // In-memory data does not match desired format. Need to swizzle the results.
+ if (desiredFormat == AlphaPremultiplication::Unpremultiplied)
+ inPlaceChangePremultiplication<AlphaPremultiplication::Unpremultiplied>(result->data(), result->length());
+ else
+ inPlaceChangePremultiplication<AlphaPremultiplication::Premultiplied>(result->data(), result->length());
+ }
+
+ return result;
+}
+
+bool ImageBufferData::readDataFromBitmapIfNeeded(AlphaPremultiplication desiredFormat) const
+{
+ if (bitmapBufferSync != BitmapBufferSync::BufferOutOfSync)
+ return true;
+
+ IntSize pixelSize = bitmap->GetPixelSize();
+
+ auto numBytes = pixelSize.area<RecordOverflow>() * 4;
+ if (numBytes.hasOverflowed())
+ return false;
+
+ context->endDraw();
+
+ COMPtr<ID2D1DeviceContext> d2dDeviceContext(Query, platformContext->renderTarget());
+ ASSERT(!!d2dDeviceContext);
+
+ auto bytesPerRowInData = pixelSize.width() * 4;
+
+ // Copy GPU data from the ID2D1Bitmap to a CPU-backed ID2D1Bitmap1
+ COMPtr<ID2D1Bitmap1> cpuBitmap;
+ D2D1_BITMAP_PROPERTIES1 bitmapProperties2 = D2D1::BitmapProperties1(D2D1_BITMAP_OPTIONS_CPU_READ | D2D1_BITMAP_OPTIONS_CANNOT_DRAW, Direct2D::pixelFormat());
+ HRESULT hr = d2dDeviceContext->CreateBitmap(pixelSize, nullptr, bytesPerRowInData, bitmapProperties2, &cpuBitmap);
+ if (!SUCCEEDED(hr))
+ return false;
+
+ auto targetPos = D2D1::Point2U();
+ D2D1_RECT_U dataRect = D2D1::RectU(0, 0, pixelSize.width(), pixelSize.height());
+ hr = cpuBitmap->CopyFromBitmap(&targetPos, bitmap.get(), &dataRect);
+ if (!SUCCEEDED(hr))
+ return false;
+
+ D2D1_MAPPED_RECT mappedData;
+ hr = cpuBitmap->Map(D2D1_MAP_OPTIONS_READ, &mappedData);
+ if (!SUCCEEDED(hr))
+ return false;
+
+ if (!mappedData.bits)
+ return false;
+
+ if (!ensureBackingStore(pixelSize))
+ return false;
+
+ // Software filters expect RGBA bytes. We need to swizzle from Direct2D's BGRA to be compatible.
+ if (desiredFormat == AlphaPremultiplication::Premultiplied)
+ swizzleAndPremultiply<AlphaPremultiplication::Premultiplied>(mappedData.bits, pixelSize.height(), pixelSize.width(), mappedData.pitch, bytesPerRowInData, data.data());
+ else
+ swizzleAndPremultiply<AlphaPremultiplication::Unpremultiplied>(mappedData.bits, pixelSize.height(), pixelSize.width(), mappedData.pitch, bytesPerRowInData, data.data());
+
+ byteFormat = desiredFormat;
+
+ bitmapBufferSync = BitmapBufferSync::InSync;
+
+ hr = cpuBitmap->Unmap();
+ ASSERT(SUCCEEDED(hr));
+
+ context->beginDraw();
+
+ return true;
+}
+
void ImageBufferData::putData(const Uint8ClampedArray& source, AlphaPremultiplication sourceFormat, const IntSize& sourceSize, const IntRect& sourceRect, const IntPoint& destPoint, const IntSize& size, bool /* accelerateRendering */, float resolutionScale)
{
ASSERT(sourceRect.width() > 0);
@@ -229,22 +390,49 @@
return;
#if !ASSERT_DISABLED
- auto pixelSize = bitmap->GetPixelSize();
- ASSERT(pixelSize.width >= sourceSize.width());
- ASSERT(pixelSize.width >= size.width());
- ASSERT(pixelSize.height >= sourceSize.height());
- ASSERT(pixelSize.height >= size.height());
+ if (bitmap) {
+ auto pixelSize = bitmap->GetPixelSize();
+ ASSERT(pixelSize.width >= sourceSize.width());
+ ASSERT(pixelSize.width >= size.width());
+ ASSERT(pixelSize.height >= sourceSize.height());
+ ASSERT(pixelSize.height >= size.height());
+ }
#endif
if (!ensureBackingStore(size))
return;
- memcpy(data.data(), source.data(), source.length());
+ if (sourceSize == size) {
+ memcpy(data.data(), source.data(), source.length());
+ byteFormat = sourceFormat;
+ } else {
+ readDataFromBitmapIfNeeded(byteFormat);
+ if (!copyRectFromSourceToData(sourceRect, source, sourceFormat))
+ return;
+ }
- byteFormat = sourceFormat;
bitmapBufferSync = BitmapBufferSync::BitmapOutOfSync;
}
+bool ImageBufferData::copyRectFromSourceToData(const IntRect& sourceRect, const Uint8ClampedArray& source, AlphaPremultiplication sourceFormat)
+{
+ IntSize pixelSize = bitmap->GetPixelSize();
+
+ IntRect sourceRectToCopy = sourceRect;
+ if (!IntRect(IntPoint(), pixelSize).contains(sourceRect))
+ return false;
+
+ auto destBufferPosition = sourceRect.location();
+
+ if (sourceFormat == byteFormat)
+ return copyRectFromSourceToDest(sourceRect, sourceRect.size(), source.data(), backingStoreSize, data.data(), destBufferPosition);
+
+ if (byteFormat == AlphaPremultiplication::Unpremultiplied)
+ copyRectFromSourceToDestAndSetPremultiplication<AlphaPremultiplication::Unpremultiplied>(sourceRect, sourceRect.size(), source.data(), backingStoreSize, data.data(), destBufferPosition);
+ else
+ copyRectFromSourceToDestAndSetPremultiplication<AlphaPremultiplication::Premultiplied>(sourceRect, sourceRect.size(), source.data(), backingStoreSize, data.data(), destBufferPosition);
+}
+
void ImageBufferData::loadDataToBitmapIfNeeded()
{
if (bitmapBufferSync != BitmapBufferSync::BitmapOutOfSync || data.isEmpty())