Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
android_surface_software.cc
Go to the documentation of this file.
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/platform/android/android_surface_software.h"
6
7#include <memory>
8#include <vector>
9
10#include "flutter/fml/logging.h"
11#include "flutter/fml/platform/android/jni_weak_ref.h"
12#include "flutter/fml/platform/android/scoped_java_ref.h"
13#include "flutter/fml/trace_event.h"
14#include "flutter/shell/platform/android/android_shell_holder.h"
15#include "flutter/shell/platform/android/jni/platform_view_android_jni.h"
16
19
20namespace flutter {
21
22namespace {
23
24bool GetSkColorType(int32_t buffer_format,
27 switch (buffer_format) {
28 case WINDOW_FORMAT_RGB_565:
31 return true;
32 case WINDOW_FORMAT_RGBA_8888:
35 return true;
36 default:
37 return false;
38 }
39}
40
41} // anonymous namespace
42
44 GetSkColorType(WINDOW_FORMAT_RGBA_8888, &target_color_type_,
45 &target_alpha_type_);
46}
47
49
51 return true;
52}
53
55 // Resource Context always not available on software backend.
56 return false;
57}
58
62
64 // The software AndroidSurface neither uses any passed in Skia context
65 // nor does it interact with the AndroidContext's raster Skia context.
66 GrDirectContext* gr_context) {
67 if (!IsValid()) {
68 return nullptr;
69 }
70
71 auto surface =
72 std::make_unique<GPUSurfaceSoftware>(this, true /* render to surface */);
73
74 if (!surface->IsValid()) {
75 return nullptr;
76 }
77
78 return surface;
79}
80
82 const SkISize& size) {
83 TRACE_EVENT0("flutter", "AndroidSurfaceSoftware::AcquireBackingStore");
84 if (!IsValid()) {
85 return nullptr;
86 }
87
88 if (sk_surface_ != nullptr &&
89 SkISize::Make(sk_surface_->width(), sk_surface_->height()) == size) {
90 // The old and new surface sizes are the same. Nothing to do here.
91 return sk_surface_;
92 }
93
94 SkImageInfo image_info =
95 SkImageInfo::Make(size.fWidth, size.fHeight, target_color_type_,
96 target_alpha_type_, SkColorSpace::MakeSRGB());
97
98 sk_surface_ = SkSurfaces::Raster(image_info);
99
100 return sk_surface_;
101}
102
104 sk_sp<SkSurface> backing_store) {
105 TRACE_EVENT0("flutter", "AndroidSurfaceSoftware::PresentBackingStore");
106 if (!IsValid() || backing_store == nullptr) {
107 return false;
108 }
109
110 SkPixmap pixmap;
111 if (!backing_store->peekPixels(&pixmap)) {
112 return false;
113 }
114
115 ANativeWindow_Buffer native_buffer;
116 if (ANativeWindow_lock(native_window_->handle(), &native_buffer, nullptr)) {
117 return false;
118 }
119
122 if (GetSkColorType(native_buffer.format, &color_type, &alpha_type)) {
123 SkImageInfo native_image_info = SkImageInfo::Make(
124 native_buffer.width, native_buffer.height, color_type, alpha_type);
125
126 std::unique_ptr<SkCanvas> canvas = SkCanvas::MakeRasterDirect(
127 native_image_info, native_buffer.bits,
128 native_buffer.stride * SkColorTypeBytesPerPixel(color_type));
129
130 if (canvas) {
132 if (bitmap.installPixels(pixmap)) {
133 canvas->drawImageRect(
134 bitmap.asImage(),
135 SkRect::MakeIWH(native_buffer.width, native_buffer.height),
137 }
138 }
139 }
140
141 ANativeWindow_unlockAndPost(native_window_->handle());
142
143 return true;
144}
145
147
149 return true;
150}
151
154 native_window_ = std::move(window);
155 if (!(native_window_ && native_window_->IsValid())) {
156 return false;
157 }
158 int32_t window_format = ANativeWindow_getFormat(native_window_->handle());
159 if (window_format < 0) {
160 return false;
161 }
162 if (!GetSkColorType(window_format, &target_color_type_,
163 &target_alpha_type_)) {
164 return false;
165 }
166 return true;
167}
168
169} // namespace flutter
SkAlphaType
Definition SkAlphaType.h:26
@ kOpaque_SkAlphaType
pixel is opaque
Definition SkAlphaType.h:28
@ kPremul_SkAlphaType
pixel components are premultiplied by alpha
Definition SkAlphaType.h:29
SkColorType
Definition SkColorType.h:19
@ kRGB_565_SkColorType
pixel with 5 bits red, 6 bits green, 5 bits blue, in 16-bit word
Definition SkColorType.h:22
@ kRGBA_8888_SkColorType
pixel with 8 bits for red, green, blue, alpha; in 32-bit word
Definition SkColorType.h:24
SK_API int SkColorTypeBytesPerPixel(SkColorType ct)
static std::unique_ptr< SkCanvas > MakeRasterDirect(const SkImageInfo &info, void *pixels, size_t rowBytes, const SkSurfaceProps *props=nullptr)
static sk_sp< SkColorSpace > MakeSRGB()
int width() const
Definition SkSurface.h:178
int height() const
Definition SkSurface.h:184
bool PresentBackingStore(sk_sp< SkSurface > backing_store) override
Called by the platform when a frame has been rendered into the backing store and the platform must di...
sk_sp< SkSurface > AcquireBackingStore(const SkISize &size) override
Called when the GPU surface needs a new buffer to render a new frame into.
bool OnScreenSurfaceResize(const SkISize &size) override
bool SetNativeWindow(fml::RefPtr< AndroidNativeWindow > window) override
std::unique_ptr< Surface > CreateGPUSurface(GrDirectContext *gr_context) override
GLFWwindow * window
Definition main.cc:45
VkSurfaceKHR surface
Definition main.cc:49
SK_API sk_sp< SkSurface > Raster(const SkImageInfo &imageInfo, size_t rowBytes, const SkSurfaceProps *surfaceProps)
it will be possible to load the file into Perfetto s trace viewer disable asset Prevents usage of any non test fonts unless they were explicitly Loaded via prefetched default font Indicates whether the embedding started a prefetch of the default font manager before creating the engine run In non interactive keep the shell running after the Dart script has completed enable serial On low power devices with low core running concurrent GC tasks on threads can cause them to contend with the UI thread which could potentially lead to jank This option turns off all concurrent GC activities domain network JSON encoded network policy per domain This overrides the DisallowInsecureConnections switch Embedder can specify whether to allow or disallow insecure connections at a domain level old gen heap size
Definition switches.h:259
uint32_t color_type
uint32_t alpha_type
static constexpr SkISize Make(int32_t w, int32_t h)
Definition SkSize.h:20
static SkImageInfo Make(int width, int height, SkColorType ct, SkAlphaType at)
static SkRect MakeIWH(int w, int h)
Definition SkRect.h:623
#define TRACE_EVENT0(category_group, name)