forked from flutter/engine
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathandroid_surface_software.cc
More file actions
171 lines (137 loc) · 4.88 KB
/
android_surface_software.cc
File metadata and controls
171 lines (137 loc) · 4.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "flutter/shell/platform/android/android_surface_software.h"
#include <memory>
#include <vector>
#include "flutter/fml/logging.h"
#include "flutter/fml/platform/android/jni_weak_ref.h"
#include "flutter/fml/platform/android/scoped_java_ref.h"
#include "flutter/fml/trace_event.h"
#include "flutter/shell/platform/android/android_shell_holder.h"
#include "flutter/shell/platform/android/jni/platform_view_android_jni.h"
namespace flutter {
namespace {
bool GetSkColorType(int32_t buffer_format,
SkColorType* color_type,
SkAlphaType* alpha_type) {
switch (buffer_format) {
case WINDOW_FORMAT_RGB_565:
*color_type = kRGB_565_SkColorType;
*alpha_type = kOpaque_SkAlphaType;
return true;
case WINDOW_FORMAT_RGBA_8888:
*color_type = kRGBA_8888_SkColorType;
*alpha_type = kPremul_SkAlphaType;
return true;
default:
return false;
}
}
} // anonymous namespace
AndroidSurfaceSoftware::AndroidSurfaceSoftware(
std::shared_ptr<AndroidContext> android_context,
std::shared_ptr<PlatformViewAndroidJNI> jni_facade,
AndroidSurface::Factory surface_factory)
: external_view_embedder_(
std::make_unique<AndroidExternalViewEmbedder>(android_context,
jni_facade,
surface_factory)) {
GetSkColorType(WINDOW_FORMAT_RGBA_8888, &target_color_type_,
&target_alpha_type_);
}
AndroidSurfaceSoftware::~AndroidSurfaceSoftware() = default;
bool AndroidSurfaceSoftware::IsValid() const {
return true;
}
bool AndroidSurfaceSoftware::ResourceContextMakeCurrent() {
// Resource Context always not available on software backend.
return false;
}
bool AndroidSurfaceSoftware::ResourceContextClearCurrent() {
return false;
}
std::unique_ptr<Surface> AndroidSurfaceSoftware::CreateGPUSurface(
GrDirectContext* gr_context) {
if (!IsValid()) {
return nullptr;
}
auto surface =
std::make_unique<GPUSurfaceSoftware>(this, true /* render to surface */);
if (!surface->IsValid()) {
return nullptr;
}
return surface;
}
sk_sp<SkSurface> AndroidSurfaceSoftware::AcquireBackingStore(
const SkISize& size) {
TRACE_EVENT0("flutter", "AndroidSurfaceSoftware::AcquireBackingStore");
if (!IsValid()) {
return nullptr;
}
if (sk_surface_ != nullptr &&
SkISize::Make(sk_surface_->width(), sk_surface_->height()) == size) {
// The old and new surface sizes are the same. Nothing to do here.
return sk_surface_;
}
SkImageInfo image_info =
SkImageInfo::Make(size.fWidth, size.fHeight, target_color_type_,
target_alpha_type_, SkColorSpace::MakeSRGB());
sk_surface_ = SkSurface::MakeRaster(image_info);
return sk_surface_;
}
bool AndroidSurfaceSoftware::PresentBackingStore(
sk_sp<SkSurface> backing_store) {
TRACE_EVENT0("flutter", "AndroidSurfaceSoftware::PresentBackingStore");
if (!IsValid() || backing_store == nullptr) {
return false;
}
SkPixmap pixmap;
if (!backing_store->peekPixels(&pixmap)) {
return false;
}
ANativeWindow_Buffer native_buffer;
if (ANativeWindow_lock(native_window_->handle(), &native_buffer, nullptr)) {
return false;
}
SkColorType color_type;
SkAlphaType alpha_type;
if (GetSkColorType(native_buffer.format, &color_type, &alpha_type)) {
SkImageInfo native_image_info = SkImageInfo::Make(
native_buffer.width, native_buffer.height, color_type, alpha_type);
std::unique_ptr<SkCanvas> canvas = SkCanvas::MakeRasterDirect(
native_image_info, native_buffer.bits,
native_buffer.stride * SkColorTypeBytesPerPixel(color_type));
if (canvas) {
SkBitmap bitmap;
if (bitmap.installPixels(pixmap)) {
canvas->drawBitmapRect(
bitmap, SkRect::MakeIWH(native_buffer.width, native_buffer.height),
nullptr);
}
}
}
ANativeWindow_unlockAndPost(native_window_->handle());
return true;
}
// |GPUSurfaceSoftwareDelegate|
ExternalViewEmbedder* AndroidSurfaceSoftware::GetExternalViewEmbedder() {
return external_view_embedder_.get();
}
void AndroidSurfaceSoftware::TeardownOnScreenContext() {}
bool AndroidSurfaceSoftware::OnScreenSurfaceResize(const SkISize& size) {
return true;
}
bool AndroidSurfaceSoftware::SetNativeWindow(
fml::RefPtr<AndroidNativeWindow> window) {
native_window_ = std::move(window);
if (!(native_window_ && native_window_->IsValid()))
return false;
int32_t window_format = ANativeWindow_getFormat(native_window_->handle());
if (window_format < 0)
return false;
if (!GetSkColorType(window_format, &target_color_type_, &target_alpha_type_))
return false;
return true;
}
} // namespace flutter