Nader Jawad 8f5d66b748 Do not use SkImageFilter for Stretch
Refactor stretch implementation to manually
draw the shader as a rect instead of using
SkImageFilter

Bug: 184184033
Test: Re-ran stretch tests
Change-Id: I6263f5474b185b3f81ef9fd1bec3e43e86da49f2
2021-04-07 16:07:00 -07:00

100 lines
3.0 KiB
C++

/*
* Copyright (C) 2021 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#include "utils/MathUtils.h"
#include <SkImage.h>
#include <SkImageFilter.h>
#include <SkPoint.h>
#include <SkRect.h>
#include <SkRuntimeEffect.h>
namespace android::uirenderer {
// TODO: Inherit from base RenderEffect type?
class StretchEffect {
public:
enum class StretchInterpolator {
SmoothStep,
};
StretchEffect(const SkRect& area, const SkVector& direction, float maxStretchAmountX,
float maxStretchAmountY)
: stretchArea(area)
, maxStretchAmountX(maxStretchAmountX)
, maxStretchAmountY(maxStretchAmountY)
, mStretchDirection(direction) {}
StretchEffect() {}
bool isEmpty() const {
return MathUtils::isZero(mStretchDirection.x()) && MathUtils::isZero(mStretchDirection.y());
}
void setEmpty() {
*this = StretchEffect{};
}
StretchEffect& operator=(const StretchEffect& other) {
this->stretchArea = other.stretchArea;
this->mStretchDirection = other.mStretchDirection;
this->mStretchShader = other.mStretchShader;
this->maxStretchAmountX = other.maxStretchAmountX;
this->maxStretchAmountY = other.maxStretchAmountY;
return *this;
}
void mergeWith(const StretchEffect& other) {
if (other.isEmpty()) {
return;
}
if (isEmpty()) {
*this = other;
return;
}
setStretchDirection(mStretchDirection + other.mStretchDirection);
if (isEmpty()) {
return setEmpty();
}
stretchArea.join(other.stretchArea);
maxStretchAmountX = std::max(maxStretchAmountX, other.maxStretchAmountX);
maxStretchAmountY = std::max(maxStretchAmountY, other.maxStretchAmountY);
}
sk_sp<SkShader> getShader(const sk_sp<SkImage>& snapshotImage) const;
SkRect stretchArea {0, 0, 0, 0};
float maxStretchAmountX = 0;
float maxStretchAmountY = 0;
void setStretchDirection(const SkVector& direction) {
mStretchShader = nullptr;
mStretchDirection = direction;
}
const SkVector getStretchDirection() const { return mStretchDirection; }
private:
static sk_sp<SkRuntimeEffect> getStretchEffect();
mutable SkVector mStretchDirection{0, 0};
mutable std::unique_ptr<SkRuntimeShaderBuilder> mBuilder;
mutable sk_sp<SkShader> mStretchShader;
};
} // namespace android::uirenderer