Skip to content

Commit

Permalink
Add support for scaling the GPUSurfaceSoftware. (flutter#3600)
Browse files Browse the repository at this point in the history
  • Loading branch information
chinmaygarde authored Apr 17, 2017
1 parent eebda8f commit 306a477
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 2 deletions.
21 changes: 21 additions & 0 deletions shell/common/surface.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
11 changes: 11 additions & 0 deletions shell/common/surface.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ class SurfaceFrame {

class Surface {
public:
Surface();

virtual ~Surface();

virtual bool Setup() = 0;
Expand All @@ -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
Expand Down
20 changes: 18 additions & 2 deletions shell/gpu/gpu_surface_software.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions shell/gpu/gpu_surface_software.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ class GPUSurfaceSoftware : public Surface {

GrContext* GetContext() override;

bool SupportsScaling() const override;

private:
GPUSurfaceSoftwareDelegate* delegate_;

Expand Down

0 comments on commit 306a477

Please sign in to comment.