Chinmay Garde 591f55b39d Allow embedder controlled composition of Flutter layers. (flutter/engine#10195)
This patch allows embedders to split the Flutter layer tree into multiple
chunks. These chunks are meant to be composed one on top of another. This gives
embedders a chance to interleave their own contents between these chunks.

The Flutter embedder API already provides hooks for the specification of
textures for the Flutter engine to compose within its own hierarchy (for camera
feeds, video, etc..). However, not all embedders can render the contents of such
sources into textures the Flutter engine can accept. Moreover, this composition
model may have overheads that are non-trivial for certain use cases. In such
cases, the embedder may choose to specify multiple render target for Flutter to
render into instead of just one.

The use of this API allows embedders to perform composition very similar to the
iOS embedder. This composition model is used on that platform for the embedding
of UIKit view such and web view and map views within the Flutter hierarchy.
However, do note that iOS also has threading configurations that are currently
not available to custom embedders.

The embedder API updates in this patch are ABI stable and existing embedders
will continue to work are normal. For embedders that want to enable this
composition mode, the API is designed to make it easy to opt into the same in an
incremental manner.

Rendering of contents into the “root” rendering surface remains unchanged.
However, now the application can push “platform views” via a scene builder.
These platform views need to handled by a FlutterCompositor specified in a new
field at the end of the FlutterProjectArgs struct.

When a new platform view in introduced within the layer tree, the compositor
will ask the embedder to create a new render target for that platform view.
Render targets can currently be OpenGL framebuffers, OpenGL textures or software
buffers. The type of the render target returned by the embedder must be
compatible with the root render surface. That is, if the root render surface is
an OpenGL framebuffer, the render target for each platform view must either be a
texture or a framebuffer in the same OpenGL context. New render target types as
well as root renderers for newer APIs like Metal & Vulkan can and will be added
in the future. The addition of these APIs will be done in an ABI & API stable
manner.

As Flutter renders frames, it gives the embedder a callback with information
about the position of the various platform views in the effective hierarchy.
The embedder is then meant to put the contents of the render targets that it
setup and had previously given to the engine onto the screen (of course
interleaving the contents of the platform views).

Unit-tests have been added that test not only the structure and properties of
layer hierarchy given to the compositor, but also the contents of the texels
rendered by a test compositor using both the OpenGL and software rendering
backends.

Fixes b/132812775
Fixes flutter/flutter#35410
2019-08-13 14:53:19 -07:00

359 lines
10 KiB
C++

// 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/testing/test_gl_surface.h"
#include <EGL/egl.h>
#include <GLES2/gl2.h>
#include <sstream>
#include <string>
#include "flutter/fml/logging.h"
#include "third_party/skia/include/core/SkSurface.h"
#include "third_party/skia/include/gpu/gl/GrGLAssembleInterface.h"
#include "third_party/skia/src/gpu/gl/GrGLDefines.h"
namespace flutter {
namespace testing {
static std::string GetEGLError() {
std::stringstream stream;
auto error = ::eglGetError();
stream << "EGL Result: '";
switch (error) {
case EGL_SUCCESS:
stream << "EGL_SUCCESS";
break;
case EGL_NOT_INITIALIZED:
stream << "EGL_NOT_INITIALIZED";
break;
case EGL_BAD_ACCESS:
stream << "EGL_BAD_ACCESS";
break;
case EGL_BAD_ALLOC:
stream << "EGL_BAD_ALLOC";
break;
case EGL_BAD_ATTRIBUTE:
stream << "EGL_BAD_ATTRIBUTE";
break;
case EGL_BAD_CONTEXT:
stream << "EGL_BAD_CONTEXT";
break;
case EGL_BAD_CONFIG:
stream << "EGL_BAD_CONFIG";
break;
case EGL_BAD_CURRENT_SURFACE:
stream << "EGL_BAD_CURRENT_SURFACE";
break;
case EGL_BAD_DISPLAY:
stream << "EGL_BAD_DISPLAY";
break;
case EGL_BAD_SURFACE:
stream << "EGL_BAD_SURFACE";
break;
case EGL_BAD_MATCH:
stream << "EGL_BAD_MATCH";
break;
case EGL_BAD_PARAMETER:
stream << "EGL_BAD_PARAMETER";
break;
case EGL_BAD_NATIVE_PIXMAP:
stream << "EGL_BAD_NATIVE_PIXMAP";
break;
case EGL_BAD_NATIVE_WINDOW:
stream << "EGL_BAD_NATIVE_WINDOW";
break;
case EGL_CONTEXT_LOST:
stream << "EGL_CONTEXT_LOST";
break;
default:
stream << "Unknown";
}
stream << "' (0x" << std::hex << error << std::dec << ").";
return stream.str();
}
constexpr size_t kTestGLSurfaceWidth = 800;
constexpr size_t kTestGLSurfaceHeight = 600;
TestGLSurface::TestGLSurface() {
display_ = ::eglGetDisplay(EGL_DEFAULT_DISPLAY);
FML_CHECK(display_ != EGL_NO_DISPLAY);
auto result = ::eglInitialize(display_, NULL, NULL);
FML_CHECK(result == EGL_TRUE) << GetEGLError();
EGLConfig config = {0};
EGLint num_config = 0;
const EGLint attribute_list[] = {EGL_RED_SIZE,
8,
EGL_GREEN_SIZE,
8,
EGL_BLUE_SIZE,
8,
EGL_ALPHA_SIZE,
8,
EGL_SURFACE_TYPE,
EGL_PBUFFER_BIT,
EGL_CONFORMANT,
EGL_OPENGL_ES2_BIT,
EGL_RENDERABLE_TYPE,
EGL_OPENGL_ES2_BIT,
EGL_NONE};
result = ::eglChooseConfig(display_, attribute_list, &config, 1, &num_config);
FML_CHECK(result == EGL_TRUE) << GetEGLError();
FML_CHECK(num_config == 1) << GetEGLError();
{
const EGLint surface_attributes[] = {
EGL_WIDTH, kTestGLSurfaceWidth, //
EGL_HEIGHT, kTestGLSurfaceHeight, //
EGL_NONE,
};
onscreen_surface_ =
::eglCreatePbufferSurface(display_, // display connection
config, // config
surface_attributes // surface attributes
);
FML_CHECK(onscreen_surface_ != EGL_NO_SURFACE) << GetEGLError();
offscreen_surface_ =
::eglCreatePbufferSurface(display_, // display connection
config, // config
surface_attributes // surface attributes
);
FML_CHECK(offscreen_surface_ != EGL_NO_SURFACE) << GetEGLError();
}
{
const EGLint context_attributes[] = {
EGL_CONTEXT_CLIENT_VERSION, //
2, //
EGL_NONE //
};
onscreen_context_ =
::eglCreateContext(display_, // display connection
config, // config
EGL_NO_CONTEXT, // sharegroup
context_attributes // context attributes
);
FML_CHECK(onscreen_context_ != EGL_NO_CONTEXT) << GetEGLError();
offscreen_context_ =
::eglCreateContext(display_, // display connection
config, // config
onscreen_context_, // sharegroup
context_attributes // context attributes
);
FML_CHECK(offscreen_context_ != EGL_NO_CONTEXT) << GetEGLError();
}
}
TestGLSurface::~TestGLSurface() {
context_ = nullptr;
auto result = ::eglDestroyContext(display_, onscreen_context_);
FML_CHECK(result == EGL_TRUE) << GetEGLError();
result = ::eglDestroyContext(display_, offscreen_context_);
FML_CHECK(result == EGL_TRUE) << GetEGLError();
result = ::eglDestroySurface(display_, onscreen_surface_);
FML_CHECK(result == EGL_TRUE) << GetEGLError();
result = ::eglDestroySurface(display_, offscreen_surface_);
FML_CHECK(result == EGL_TRUE) << GetEGLError();
result = ::eglTerminate(display_);
FML_CHECK(result == EGL_TRUE);
}
SkISize TestGLSurface::GetSize() const {
return SkISize::Make(kTestGLSurfaceWidth, kTestGLSurfaceHeight);
}
bool TestGLSurface::MakeCurrent() {
auto result = ::eglMakeCurrent(display_, onscreen_surface_, onscreen_surface_,
onscreen_context_);
if (result == EGL_FALSE) {
FML_LOG(ERROR) << "Could not make the context current. " << GetEGLError();
}
return result == EGL_TRUE;
}
bool TestGLSurface::ClearCurrent() {
auto result = ::eglMakeCurrent(display_, EGL_NO_SURFACE, EGL_NO_SURFACE,
EGL_NO_CONTEXT);
if (result == EGL_FALSE) {
FML_LOG(ERROR) << "Could not clear the current context. " << GetEGLError();
}
return result == EGL_TRUE;
}
bool TestGLSurface::Present() {
auto result = ::eglSwapBuffers(display_, onscreen_surface_);
if (result == EGL_FALSE) {
FML_LOG(ERROR) << "Could not swap buffers. " << GetEGLError();
}
return result == EGL_TRUE;
}
uint32_t TestGLSurface::GetFramebuffer() const {
// Return FBO0
return 0;
}
bool TestGLSurface::MakeResourceCurrent() {
auto result = ::eglMakeCurrent(display_, offscreen_surface_,
offscreen_surface_, offscreen_context_);
if (result == EGL_FALSE) {
FML_LOG(ERROR) << "Could not make the resource context current. "
<< GetEGLError();
}
return result == EGL_TRUE;
}
void* TestGLSurface::GetProcAddress(const char* name) const {
if (name == nullptr) {
return nullptr;
}
auto symbol = ::eglGetProcAddress(name);
if (symbol == NULL) {
FML_LOG(ERROR) << "Could not fetch symbol for name: " << name;
}
return reinterpret_cast<void*>(symbol);
}
sk_sp<GrContext> TestGLSurface::GetGrContext() {
if (context_) {
return context_;
}
context_ = CreateGrContext();
return context_;
}
sk_sp<GrContext> TestGLSurface::CreateGrContext() {
if (!MakeCurrent()) {
return nullptr;
}
auto get_string =
reinterpret_cast<PFNGLGETSTRINGPROC>(GetProcAddress("glGetString"));
if (!get_string) {
return nullptr;
}
auto c_version = reinterpret_cast<const char*>(get_string(GL_VERSION));
if (c_version == NULL) {
return nullptr;
}
GrGLGetProc get_proc = [](void* context, const char name[]) -> GrGLFuncPtr {
return reinterpret_cast<GrGLFuncPtr>(
reinterpret_cast<TestGLSurface*>(context)->GetProcAddress(name));
};
std::string version(c_version);
auto interface = version.find("OpenGL ES") == std::string::npos
? GrGLMakeAssembledGLInterface(this, get_proc)
: GrGLMakeAssembledGLESInterface(this, get_proc);
if (!interface) {
return nullptr;
}
context_ = GrContext::MakeGL(interface);
return context_;
}
sk_sp<SkSurface> TestGLSurface::GetOnscreenSurface() {
GrGLFramebufferInfo framebuffer_info = {};
framebuffer_info.fFBOID = GetFramebuffer();
framebuffer_info.fFormat = GR_GL_RGBA8;
const auto size = GetSize();
GrBackendRenderTarget backend_render_target(
size.width(), // width
size.height(), // height
1, // sample count
8, // stencil bits
framebuffer_info // framebuffer info
);
SkSurfaceProps surface_properties(
SkSurfaceProps::InitType::kLegacyFontHost_InitType);
auto surface = SkSurface::MakeFromBackendRenderTarget(
GetGrContext().get(), // context
backend_render_target, // backend render target
kBottomLeft_GrSurfaceOrigin, // surface origin
kN32_SkColorType, // color type
SkColorSpace::MakeSRGB(), // color space
&surface_properties, // surface properties
nullptr, // release proc
nullptr // release context
);
if (!surface) {
FML_LOG(ERROR) << "Could not wrap the surface while attempting to "
"snapshot the GL surface.";
return nullptr;
}
return surface;
}
sk_sp<SkImage> TestGLSurface::GetRasterSurfaceSnapshot() {
auto surface = GetOnscreenSurface();
if (!surface) {
FML_LOG(ERROR) << "Aborting snapshot because of on-screen surface "
"acquisition failure.";
return nullptr;
}
auto device_snapshot = surface->makeImageSnapshot();
if (!device_snapshot) {
FML_LOG(ERROR) << "Could not create the device snapshot while attempting "
"to snapshot the GL surface.";
return nullptr;
}
auto host_snapshot = device_snapshot->makeRasterImage();
if (!host_snapshot) {
FML_LOG(ERROR) << "Could not create the host snapshot while attempting to "
"snapshot the GL surface.";
return nullptr;
}
return host_snapshot;
}
} // namespace testing
} // namespace flutter