gecko-dev/layout/painting/DashedCornerFinder.cpp
Ehsan Akhgari aca985efab Bug 1318805 - Move the code for the Web Painting module to layout/painting; r=mattwoodrow
This will help make it clearer that this code lives in a different
module for contributors.


--HG--
rename : layout/base/ActiveLayerTracker.cpp => layout/painting/ActiveLayerTracker.cpp
rename : layout/base/ActiveLayerTracker.h => layout/painting/ActiveLayerTracker.h
rename : layout/base/BorderCache.h => layout/painting/BorderCache.h
rename : layout/base/BorderConsts.h => layout/painting/BorderConsts.h
rename : layout/base/DashedCornerFinder.cpp => layout/painting/DashedCornerFinder.cpp
rename : layout/base/DashedCornerFinder.h => layout/painting/DashedCornerFinder.h
rename : layout/base/DisplayItemClip.cpp => layout/painting/DisplayItemClip.cpp
rename : layout/base/DisplayItemClip.h => layout/painting/DisplayItemClip.h
rename : layout/base/DisplayItemScrollClip.cpp => layout/painting/DisplayItemScrollClip.cpp
rename : layout/base/DisplayItemScrollClip.h => layout/painting/DisplayItemScrollClip.h
rename : layout/base/DisplayListClipState.cpp => layout/painting/DisplayListClipState.cpp
rename : layout/base/DisplayListClipState.h => layout/painting/DisplayListClipState.h
rename : layout/base/DottedCornerFinder.cpp => layout/painting/DottedCornerFinder.cpp
rename : layout/base/DottedCornerFinder.h => layout/painting/DottedCornerFinder.h
rename : layout/base/FrameLayerBuilder.cpp => layout/painting/FrameLayerBuilder.cpp
rename : layout/base/FrameLayerBuilder.h => layout/painting/FrameLayerBuilder.h
rename : layout/base/LayerState.h => layout/painting/LayerState.h
rename : layout/base/MaskLayerImageCache.cpp => layout/painting/MaskLayerImageCache.cpp
rename : layout/base/MaskLayerImageCache.h => layout/painting/MaskLayerImageCache.h
rename : layout/base/PaintTracker.cpp => layout/painting/PaintTracker.cpp
rename : layout/base/PaintTracker.h => layout/painting/PaintTracker.h
rename : layout/base/nsCSSRendering.cpp => layout/painting/nsCSSRendering.cpp
rename : layout/base/nsCSSRendering.h => layout/painting/nsCSSRendering.h
rename : layout/base/nsCSSRenderingBorders.cpp => layout/painting/nsCSSRenderingBorders.cpp
rename : layout/base/nsCSSRenderingBorders.h => layout/painting/nsCSSRenderingBorders.h
rename : layout/base/nsDisplayItemTypes.h => layout/painting/nsDisplayItemTypes.h
rename : layout/base/nsDisplayItemTypesList.h => layout/painting/nsDisplayItemTypesList.h
rename : layout/base/nsDisplayList.cpp => layout/painting/nsDisplayList.cpp
rename : layout/base/nsDisplayList.h => layout/painting/nsDisplayList.h
rename : layout/base/nsDisplayListInvalidation.cpp => layout/painting/nsDisplayListInvalidation.cpp
rename : layout/base/nsDisplayListInvalidation.h => layout/painting/nsDisplayListInvalidation.h
2016-11-21 20:01:15 -05:00

428 lines
12 KiB
C++

/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "DashedCornerFinder.h"
#include "mozilla/Move.h"
#include "BorderCache.h"
#include "BorderConsts.h"
namespace mozilla {
using namespace gfx;
struct BestDashLength
{
typedef mozilla::gfx::Float Float;
Float dashLength;
size_t count;
BestDashLength()
: dashLength(0.0f), count(0)
{}
BestDashLength(Float aDashLength, size_t aCount)
: dashLength(aDashLength), count(aCount)
{}
};
static const size_t DashedCornerCacheSize = 256;
nsDataHashtable<FourFloatsHashKey, BestDashLength> DashedCornerCache;
DashedCornerFinder::DashedCornerFinder(const Bezier& aOuterBezier,
const Bezier& aInnerBezier,
Float aBorderWidthH, Float aBorderWidthV,
const Size& aCornerDim)
: mOuterBezier(aOuterBezier),
mInnerBezier(aInnerBezier),
mLastOuterP(aOuterBezier.mPoints[0]), mLastInnerP(aInnerBezier.mPoints[0]),
mLastOuterT(0.0f), mLastInnerT(0.0f),
mBestDashLength(DOT_LENGTH * DASH_LENGTH),
mHasZeroBorderWidth(false), mHasMore(true),
mMaxCount(aCornerDim.width + aCornerDim.height),
mType(OTHER),
mI(0), mCount(0)
{
NS_ASSERTION(aBorderWidthH > 0.0f || aBorderWidthV > 0.0f,
"At least one side should have non-zero width.");
DetermineType(aBorderWidthH, aBorderWidthV);
Reset();
}
void
DashedCornerFinder::DetermineType(Float aBorderWidthH, Float aBorderWidthV)
{
if (aBorderWidthH < aBorderWidthV) {
// Always draw from wider side to thinner side.
Swap(mInnerBezier.mPoints[0], mInnerBezier.mPoints[3]);
Swap(mInnerBezier.mPoints[1], mInnerBezier.mPoints[2]);
Swap(mOuterBezier.mPoints[0], mOuterBezier.mPoints[3]);
Swap(mOuterBezier.mPoints[1], mOuterBezier.mPoints[2]);
mLastOuterP = mOuterBezier.mPoints[0];
mLastInnerP = mInnerBezier.mPoints[0];
}
// See the comment at mType declaration for each condition.
Float borderRadiusA = fabs(mOuterBezier.mPoints[0].x -
mOuterBezier.mPoints[3].x);
Float borderRadiusB = fabs(mOuterBezier.mPoints[0].y -
mOuterBezier.mPoints[3].y);
if (aBorderWidthH == aBorderWidthV &&
borderRadiusA == borderRadiusB &&
borderRadiusA > aBorderWidthH * 2.0f) {
Float curveHeight = borderRadiusA - aBorderWidthH / 2.0;
mType = PERFECT;
Float borderLength = M_PI * curveHeight / 2.0f;
Float dashWidth = aBorderWidthH * DOT_LENGTH * DASH_LENGTH;
size_t count = ceil(borderLength / dashWidth);
if (count % 2) {
count++;
}
mCount = count / 2 + 1;
mBestDashLength = borderLength / (aBorderWidthH * count);
}
Float minBorderWidth = std::min(aBorderWidthH, aBorderWidthV);
if (minBorderWidth == 0.0f) {
mHasZeroBorderWidth = true;
}
if (mType == OTHER && !mHasZeroBorderWidth) {
Float minBorderRadius = std::min(borderRadiusA, borderRadiusB);
Float maxBorderRadius = std::max(borderRadiusA, borderRadiusB);
Float maxBorderWidth = std::max(aBorderWidthH, aBorderWidthV);
FindBestDashLength(minBorderWidth, maxBorderWidth,
minBorderRadius, maxBorderRadius);
}
}
bool
DashedCornerFinder::HasMore(void) const
{
if (mHasZeroBorderWidth) {
return mI < mMaxCount && mHasMore;
}
return mI < mCount;
}
DashedCornerFinder::Result
DashedCornerFinder::Next(void)
{
Float lastOuterT, lastInnerT, outerT, innerT;
if (mI == 0) {
lastOuterT = 0.0f;
lastInnerT = 0.0f;
} else {
if (mType == PERFECT) {
lastOuterT = lastInnerT = (mI * 2.0f - 0.5f) / ((mCount - 1) * 2.0f);
} else {
Float last2OuterT = mLastOuterT;
Float last2InnerT = mLastInnerT;
(void)FindNext(mBestDashLength);
//
// mLastOuterT lastOuterT
// | |
// v v
// +---+---+---+---+ <- last2OuterT
// | |###|###| |
// | |###|###| |
// | |###|###| |
// +---+---+---+---+ <- last2InnerT
// ^ ^
// | |
// mLastInnerT lastInnerT
lastOuterT = (mLastOuterT + last2OuterT) / 2.0f;
lastInnerT = (mLastInnerT + last2InnerT) / 2.0f;
}
}
if ((!mHasZeroBorderWidth && mI == mCount - 1) ||
(mHasZeroBorderWidth && !mHasMore)) {
outerT = 1.0f;
innerT = 1.0f;
} else {
if (mType == PERFECT) {
outerT = innerT = (mI * 2.0f + 0.5f) / ((mCount - 1) * 2.0f);
} else {
Float last2OuterT = mLastOuterT;
Float last2InnerT = mLastInnerT;
(void)FindNext(mBestDashLength);
//
// outerT last2OuterT
// | |
// v v
// mLastOuterT -> +---+---+---+---+
// | |###|###| |
// | |###|###| |
// | |###|###| |
// mLastInnerT -> +---+---+---+---+
// ^ ^
// | |
// innerT last2InnerT
outerT = (mLastOuterT + last2OuterT) / 2.0f;
innerT = (mLastInnerT + last2InnerT) / 2.0f;
}
}
mI++;
Bezier outerSectionBezier;
Bezier innerSectionBezier;
GetSubBezier(&outerSectionBezier, mOuterBezier, lastOuterT, outerT);
GetSubBezier(&innerSectionBezier, mInnerBezier, lastInnerT, innerT);
return DashedCornerFinder::Result(outerSectionBezier, innerSectionBezier);
}
void
DashedCornerFinder::Reset(void)
{
mLastOuterP = mOuterBezier.mPoints[0];
mLastInnerP = mInnerBezier.mPoints[0];
mLastOuterT = 0.0f;
mLastInnerT = 0.0f;
mHasMore = true;
}
Float
DashedCornerFinder::FindNext(Float dashLength)
{
Float upper = 1.0f;
Float lower = mLastOuterT;
Point OuterP, InnerP;
// Start from upper bound to check if this is the last segment.
Float outerT = upper;
Float innerT;
Float W = 0.0f;
Float L = 0.0f;
const Float LENGTH_MARGIN = 0.1f;
for (size_t i = 0; i < MAX_LOOP; i++) {
OuterP = GetBezierPoint(mOuterBezier, outerT);
InnerP = FindBezierNearestPoint(mInnerBezier, OuterP, outerT, &innerT);
// Calculate approximate dash length.
//
// W = (W1 + W2) / 2
// L = (OuterL + InnerL) / 2
// dashLength = L / W
//
// ____----+----____
// OuterP ___--- | ---___ mLastOuterP
// +--- | ---+
// | | |
// | | |
// | W | W1 |
// | | |
// W2 | | |
// | | ______------+
// | ____+---- mLastInnerP
// | ___---
// | __---
// +--
// InnerP
// OuterL
// ____---------____
// OuterP ___--- ---___ mLastOuterP
// +--- ---+
// | L |
// | ___----------______ |
// | __--- -----+
// | __-- |
// +-- |
// | InnerL ______------+
// | ____----- mLastInnerP
// | ___---
// | __---
// +--
// InnerP
Float W1 = (mLastOuterP - mLastInnerP).Length();
Float W2 = (OuterP - InnerP).Length();
Float OuterL = GetBezierLength(mOuterBezier, mLastOuterT, outerT);
Float InnerL = GetBezierLength(mInnerBezier, mLastInnerT, innerT);
W = (W1 + W2) / 2.0f;
L = (OuterL + InnerL) / 2.0f;
if (L > W * dashLength + LENGTH_MARGIN) {
if (i > 0) {
upper = outerT;
}
} else if (L < W * dashLength - LENGTH_MARGIN) {
if (i == 0) {
// This is the last segment with shorter dashLength.
mHasMore = false;
break;
}
lower = outerT;
} else {
break;
}
outerT = (upper + lower) / 2.0f;
}
mLastOuterP = OuterP;
mLastInnerP = InnerP;
mLastOuterT = outerT;
mLastInnerT = innerT;
if (W == 0.0f) {
return 1.0f;
}
return L / W;
}
void
DashedCornerFinder::FindBestDashLength(Float aMinBorderWidth,
Float aMaxBorderWidth,
Float aMinBorderRadius,
Float aMaxBorderRadius)
{
// If dashLength is not calculateable, find it with binary search,
// such that there exists i that OuterP_i == OuterP_n and
// InnerP_i == InnerP_n with given dashLength.
FourFloats key(aMinBorderWidth, aMaxBorderWidth,
aMinBorderRadius, aMaxBorderRadius);
BestDashLength best;
if (DashedCornerCache.Get(key, &best)) {
mCount = best.count;
mBestDashLength = best.dashLength;
return;
}
Float lower = 1.0f;
Float upper = DOT_LENGTH * DASH_LENGTH;
Float dashLength = upper;
size_t targetCount = 0;
const Float LENGTH_MARGIN = 0.1f;
for (size_t j = 0; j < MAX_LOOP; j++) {
size_t count;
Float actualDashLength;
if (!GetCountAndLastDashLength(dashLength, &count, &actualDashLength)) {
if (j == 0) {
mCount = mMaxCount;
break;
}
}
if (j == 0) {
if (count == 1) {
// If only 1 segment fits, fill entire region
//
// count = 1
// mCount = 1
// | 1 |
// +---+---+
// |###|###|
// |###|###|
// |###|###|
// +---+---+
// 1
mCount = 1;
break;
}
// targetCount should be 2n.
//
// targetCount = 2
// mCount = 2
// | 1 | 2 |
// +---+---+---+---+
// |###| | |###|
// |###| | |###|
// |###| | |###|
// +---+---+---+---+
// 1 2
//
// targetCount = 6
// mCount = 4
// | 1 | 2 | 3 | 4 | 5 | 6 |
// +---+---+---+---+---+---+---+---+---+---+---+---+
// |###| | |###|###| | |###|###| | |###|
// |###| | |###|###| | |###|###| | |###|
// |###| | |###|###| | |###|###| | |###|
// +---+---+---+---+---+---+---+---+---+---+---+---+
// 1 2 3 4
if (count % 2) {
targetCount = count + 1;
} else {
targetCount = count;
}
mCount = targetCount / 2 + 1;
}
if (count == targetCount) {
mBestDashLength = dashLength;
// actualDashLength won't be greater than dashLength.
if (actualDashLength > dashLength - LENGTH_MARGIN) {
break;
}
// We started from upper bound, no need to update range when j == 0.
if (j > 0) {
upper = dashLength;
}
} else {
// |j == 0 && count != targetCount| means that |targetCount = count + 1|,
// and we started from upper bound, no need to update range when j == 0.
if (j > 0) {
if (count > targetCount) {
lower = dashLength;
} else {
upper = dashLength;
}
}
}
dashLength = (upper + lower) / 2.0f;
}
if (DashedCornerCache.Count() > DashedCornerCacheSize) {
DashedCornerCache.Clear();
}
DashedCornerCache.Put(key, BestDashLength(mBestDashLength, mCount));
}
bool
DashedCornerFinder::GetCountAndLastDashLength(Float aDashLength,
size_t* aCount,
Float* aActualDashLength)
{
// Return the number of segments and the last segment's dashLength for
// the given dashLength.
Reset();
for (size_t i = 0; i < mMaxCount; i++) {
Float actualDashLength = FindNext(aDashLength);
if (mLastOuterT >= 1.0f) {
*aCount = i + 1;
*aActualDashLength = actualDashLength;
return true;
}
}
return false;
}
} // namespace mozilla