• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "flutter/shell/gpu/gpu_surface_software.h"
6 
7 #include <memory>
8 #include "flutter/fml/logging.h"
9 
10 namespace flutter {
11 
GPUSurfaceSoftware(GPUSurfaceSoftwareDelegate * delegate)12 GPUSurfaceSoftware::GPUSurfaceSoftware(GPUSurfaceSoftwareDelegate* delegate)
13     : delegate_(delegate), weak_factory_(this) {}
14 
15 GPUSurfaceSoftware::~GPUSurfaceSoftware() = default;
16 
17 // |Surface|
IsValid()18 bool GPUSurfaceSoftware::IsValid() {
19   return delegate_ != nullptr;
20 }
21 
22 // |Surface|
AcquireFrame(const SkISize & logical_size)23 std::unique_ptr<SurfaceFrame> GPUSurfaceSoftware::AcquireFrame(
24     const SkISize& logical_size) {
25   if (!IsValid()) {
26     return nullptr;
27   }
28 
29   const auto size = SkISize::Make(logical_size.width(), logical_size.height());
30 
31   sk_sp<SkSurface> backing_store = delegate_->AcquireBackingStore(size);
32 
33   if (backing_store == nullptr) {
34     return nullptr;
35   }
36 
37   if (size != SkISize::Make(backing_store->width(), backing_store->height())) {
38     return nullptr;
39   }
40 
41   // If the surface has been scaled, we need to apply the inverse scaling to the
42   // underlying canvas so that coordinates are mapped to the same spot
43   // irrespective of surface scaling.
44   SkCanvas* canvas = backing_store->getCanvas();
45   canvas->resetMatrix();
46 
47   SurfaceFrame::SubmitCallback on_submit =
48       [self = weak_factory_.GetWeakPtr()](const SurfaceFrame& surface_frame,
49                                           SkCanvas* canvas) -> bool {
50     // If the surface itself went away, there is nothing more to do.
51     if (!self || !self->IsValid() || canvas == nullptr) {
52       return false;
53     }
54 
55     canvas->flush();
56 
57     return self->delegate_->PresentBackingStore(surface_frame.SkiaSurface());
58   };
59 
60   return std::make_unique<SurfaceFrame>(backing_store, on_submit);
61 }
62 
63 // |Surface|
GetRootTransformation() const64 SkMatrix GPUSurfaceSoftware::GetRootTransformation() const {
65   // This backend does not currently support root surface transformations. Just
66   // return identity.
67   SkMatrix matrix;
68   matrix.reset();
69   return matrix;
70 }
71 
72 // |Surface|
GetContext()73 GrContext* GPUSurfaceSoftware::GetContext() {
74   // There is no GrContext associated with a software surface.
75   return nullptr;
76 }
77 
78 // |Surface|
GetExternalViewEmbedder()79 flutter::ExternalViewEmbedder* GPUSurfaceSoftware::GetExternalViewEmbedder() {
80   return delegate_->GetExternalViewEmbedder();
81 }
82 
83 }  // namespace flutter
84