Flutter Engine
The Flutter Engine
RasterWindowContext_android.cpp
Go to the documentation of this file.
1
2/*
3 * Copyright 2016 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
13
16
17namespace {
18class RasterWindowContext_android : public RasterWindowContext {
19public:
20 RasterWindowContext_android(ANativeWindow*, const DisplayParams& params);
21
23
24 bool isValid() override { return SkToBool(fNativeWindow); }
25 void resize(int w, int h) override;
26 void setDisplayParams(const DisplayParams& params) override;
27
28private:
29 void setBuffersGeometry();
30 void onSwapBuffers() override;
31
32 sk_sp<SkSurface> fBackbufferSurface = nullptr;
33 ANativeWindow* fNativeWindow = nullptr;
34 ANativeWindow_Buffer fBuffer;
35 ARect fBounds;
36};
37
38RasterWindowContext_android::RasterWindowContext_android(ANativeWindow* window,
39 const DisplayParams& params)
41 fNativeWindow = window;
42 fWidth = ANativeWindow_getWidth(fNativeWindow);
43 fHeight = ANativeWindow_getHeight(fNativeWindow);
44 this->setBuffersGeometry();
45}
46
47void RasterWindowContext_android::setBuffersGeometry() {
48 int32_t format = 0;
49 switch(fDisplayParams.fColorType) {
51 format = WINDOW_FORMAT_RGBA_8888;
52 break;
54 format = WINDOW_FORMAT_RGB_565;
55 break;
56 default:
57 SK_ABORT("Unsupported Android color type");
58 }
59 ANativeWindow_setBuffersGeometry(fNativeWindow, fWidth, fHeight, format);
60}
61
62void RasterWindowContext_android::setDisplayParams(const DisplayParams& params) {
63 fDisplayParams = params;
64 this->setBuffersGeometry();
65}
66
67void RasterWindowContext_android::resize(int w, int h) {
68 fWidth = w;
69 fHeight = h;
70 this->setBuffersGeometry();
71}
72
73sk_sp<SkSurface> RasterWindowContext_android::getBackbufferSurface() {
74 if (nullptr == fBackbufferSurface) {
75 ANativeWindow_lock(fNativeWindow, &fBuffer, &fBounds);
76 const int bytePerPixel = fBuffer.format == WINDOW_FORMAT_RGB_565 ? 2 : 4;
77 SkImageInfo info = SkImageInfo::Make(fWidth, fHeight,
78 fDisplayParams.fColorType,
80 fDisplayParams.fColorSpace);
81 fBackbufferSurface =
82 SkSurfaces::WrapPixels(info, fBuffer.bits, fBuffer.stride * bytePerPixel, nullptr);
83 }
84 return fBackbufferSurface;
85}
86
87
88void RasterWindowContext_android::onSwapBuffers() {
89 ANativeWindow_unlockAndPost(fNativeWindow);
90 fBackbufferSurface.reset(nullptr);
91}
92} // anonymous namespace
93
94namespace skwindow {
95std::unique_ptr<WindowContext> MakeRasterForAndroid(ANativeWindow* window,
96 const DisplayParams& params) {
97 std::unique_ptr<WindowContext> ctx(new RasterWindowContext_android(window, params));
98 if (!ctx->isValid()) {
99 return nullptr;
100 }
101 return ctx;
102}
103
104} // namespace skwindow
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition: DM.cpp:213
const SkRect fBounds
@ kPremul_SkAlphaType
pixel components are premultiplied by alpha
Definition: SkAlphaType.h:29
#define SK_ABORT(message,...)
Definition: SkAssert.h:70
@ 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
static constexpr bool SkToBool(const T &x)
Definition: SkTo.h:35
virtual bool isValid()=0
virtual sk_sp< SkSurface > getBackbufferSurface()=0
virtual void setDisplayParams(const DisplayParams &params)=0
virtual void resize(int w, int h)=0
virtual void onSwapBuffers()=0
const EmbeddedViewParams * params
GLFWwindow * window
Definition: main.cc:45
uint32_t uint32_t * format
SK_API sk_sp< SkSurface > WrapPixels(const SkImageInfo &imageInfo, void *pixels, size_t rowBytes, const SkSurfaceProps *surfaceProps=nullptr)
std::unique_ptr< WindowContext > MakeRasterForAndroid(ANativeWindow *window, const DisplayParams &params)
SkScalar w
SkScalar h
static SkImageInfo Make(int width, int height, SkColorType ct, SkAlphaType at)
struct ANativeWindow ANativeWindow