Add support for scaling the GPUSurfaceSoftware. (flutter/engine#3600)
This commit is contained in:
@@ -51,6 +51,27 @@ bool SurfaceFrame::PerformSubmit() {
|
||||
return false;
|
||||
}
|
||||
|
||||
Surface::Surface() : scale_(1.0) {}
|
||||
|
||||
Surface::~Surface() = default;
|
||||
|
||||
bool Surface::SupportsScaling() const {
|
||||
return false;
|
||||
}
|
||||
|
||||
double Surface::GetScale() const {
|
||||
return scale_;
|
||||
}
|
||||
|
||||
void Surface::SetScale(double scale) {
|
||||
static constexpr double kMaxScale = 1.0;
|
||||
static constexpr double kMinScale = 0.25;
|
||||
if (scale > kMaxScale) {
|
||||
scale = kMaxScale;
|
||||
} else if (scale < kMinScale) {
|
||||
scale = kMinScale;
|
||||
}
|
||||
scale_ = scale;
|
||||
}
|
||||
|
||||
} // namespace shell
|
||||
|
||||
@@ -42,6 +42,8 @@ class SurfaceFrame {
|
||||
|
||||
class Surface {
|
||||
public:
|
||||
Surface();
|
||||
|
||||
virtual ~Surface();
|
||||
|
||||
virtual bool Setup() = 0;
|
||||
@@ -51,6 +53,15 @@ class Surface {
|
||||
virtual std::unique_ptr<SurfaceFrame> AcquireFrame(const SkISize& size) = 0;
|
||||
|
||||
virtual GrContext* GetContext() = 0;
|
||||
|
||||
virtual bool SupportsScaling() const;
|
||||
|
||||
double GetScale() const;
|
||||
|
||||
void SetScale(double scale);
|
||||
|
||||
private:
|
||||
double scale_;
|
||||
};
|
||||
|
||||
} // namespace shell
|
||||
|
||||
@@ -22,13 +22,22 @@ bool GPUSurfaceSoftware::IsValid() {
|
||||
return delegate_ != nullptr;
|
||||
}
|
||||
|
||||
bool GPUSurfaceSoftware::SupportsScaling() const {
|
||||
return true;
|
||||
}
|
||||
|
||||
std::unique_ptr<SurfaceFrame> GPUSurfaceSoftware::AcquireFrame(
|
||||
const SkISize& size) {
|
||||
const SkISize& logical_size) {
|
||||
if (!IsValid()) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
auto backing_store = delegate_->AcquireBackingStore(size);
|
||||
// Check if we need to support surface scaling.
|
||||
const auto scale = SupportsScaling() ? GetScale() : 1.0;
|
||||
const auto size = SkISize::Make(logical_size.width() * scale,
|
||||
logical_size.height() * scale);
|
||||
|
||||
sk_sp<SkSurface> backing_store = delegate_->AcquireBackingStore(size);
|
||||
|
||||
if (backing_store == nullptr) {
|
||||
return nullptr;
|
||||
@@ -38,6 +47,13 @@ std::unique_ptr<SurfaceFrame> GPUSurfaceSoftware::AcquireFrame(
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// If the surface has been scaled, we need to apply the inverse scaling to the
|
||||
// underlying canvas so that coordinates are mapped to the same spot
|
||||
// irrespective of surface scaling.
|
||||
SkCanvas* canvas = backing_store->getCanvas();
|
||||
canvas->resetMatrix();
|
||||
canvas->scale(scale, scale);
|
||||
|
||||
SurfaceFrame::SubmitCallback
|
||||
on_submit = [self = weak_factory_.GetWeakPtr()](
|
||||
const SurfaceFrame& surface_frame, SkCanvas* canvas)
|
||||
|
||||
@@ -32,6 +32,8 @@ class GPUSurfaceSoftware : public Surface {
|
||||
|
||||
GrContext* GetContext() override;
|
||||
|
||||
bool SupportsScaling() const override;
|
||||
|
||||
private:
|
||||
GPUSurfaceSoftwareDelegate* delegate_;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user