Flutter Engine
embedder_test_backingstore_producer.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/embedder/tests/embedder_test_backingstore_producer.h"
6 
7 #include "flutter/fml/logging.h"
8 #include "third_party/skia/include/core/SkSurface.h"
9 
10 namespace flutter {
11 namespace testing {
12 
14  sk_sp<GrDirectContext> context,
15  RenderTargetType type)
16  : context_(context), type_(type) {}
17 
19 
21  const FlutterBackingStoreConfig* config,
22  FlutterBackingStore* renderer_out) {
23  switch (type_) {
25  return CreateSoftware(config, renderer_out);
26 #ifdef SHELL_ENABLE_GL
28  return CreateTexture(config, renderer_out);
30  return CreateFramebuffer(config, renderer_out);
31 #endif
32  default:
33  return false;
34  }
35 }
36 
37 bool EmbedderTestBackingStoreProducer::CreateFramebuffer(
38  const FlutterBackingStoreConfig* config,
39  FlutterBackingStore* backing_store_out) {
40 #ifdef SHELL_ENABLE_GL
41  const auto image_info =
42  SkImageInfo::MakeN32Premul(config->size.width, config->size.height);
43 
44  auto surface = SkSurface::MakeRenderTarget(
45  context_.get(), // context
46  SkBudgeted::kNo, // budgeted
47  image_info, // image info
48  1, // sample count
49  kBottomLeft_GrSurfaceOrigin, // surface origin
50  nullptr, // surface properties
51  false // mipmaps
52  );
53 
54  if (!surface) {
55  FML_LOG(ERROR) << "Could not create render target for compositor layer.";
56  return false;
57  }
58 
59  GrBackendRenderTarget render_target = surface->getBackendRenderTarget(
60  SkSurface::BackendHandleAccess::kDiscardWrite_BackendHandleAccess);
61 
62  if (!render_target.isValid()) {
63  FML_LOG(ERROR) << "Backend render target was invalid.";
64  return false;
65  }
66 
67  GrGLFramebufferInfo framebuffer_info = {};
68  if (!render_target.getGLFramebufferInfo(&framebuffer_info)) {
69  FML_LOG(ERROR) << "Could not access backend framebuffer info.";
70  return false;
71  }
72 
73  backing_store_out->type = kFlutterBackingStoreTypeOpenGL;
74  backing_store_out->user_data = surface.get();
76  backing_store_out->open_gl.framebuffer.target = framebuffer_info.fFormat;
77  backing_store_out->open_gl.framebuffer.name = framebuffer_info.fFBOID;
78  // The balancing unref is in the destruction callback.
79  surface->ref();
80  backing_store_out->open_gl.framebuffer.user_data = surface.get();
81  backing_store_out->open_gl.framebuffer.destruction_callback =
82  [](void* user_data) { reinterpret_cast<SkSurface*>(user_data)->unref(); };
83 
84  return true;
85 #else
86  return false;
87 #endif
88 }
89 
90 bool EmbedderTestBackingStoreProducer::CreateTexture(
91  const FlutterBackingStoreConfig* config,
92  FlutterBackingStore* backing_store_out) {
93 #ifdef SHELL_ENABLE_GL
94  const auto image_info =
95  SkImageInfo::MakeN32Premul(config->size.width, config->size.height);
96 
97  auto surface = SkSurface::MakeRenderTarget(
98  context_.get(), // context
99  SkBudgeted::kNo, // budgeted
100  image_info, // image info
101  1, // sample count
102  kBottomLeft_GrSurfaceOrigin, // surface origin
103  nullptr, // surface properties
104  false // mipmaps
105  );
106 
107  if (!surface) {
108  FML_LOG(ERROR) << "Could not create render target for compositor layer.";
109  return false;
110  }
111 
112  GrBackendTexture render_texture = surface->getBackendTexture(
113  SkSurface::BackendHandleAccess::kDiscardWrite_BackendHandleAccess);
114 
115  if (!render_texture.isValid()) {
116  FML_LOG(ERROR) << "Backend render texture was invalid.";
117  return false;
118  }
119 
120  GrGLTextureInfo texture_info = {};
121  if (!render_texture.getGLTextureInfo(&texture_info)) {
122  FML_LOG(ERROR) << "Could not access backend texture info.";
123  return false;
124  }
125 
126  backing_store_out->type = kFlutterBackingStoreTypeOpenGL;
127  backing_store_out->user_data = surface.get();
128  backing_store_out->open_gl.type = kFlutterOpenGLTargetTypeTexture;
129  backing_store_out->open_gl.texture.target = texture_info.fTarget;
130  backing_store_out->open_gl.texture.name = texture_info.fID;
131  backing_store_out->open_gl.texture.format = texture_info.fFormat;
132  // The balancing unref is in the destruction callback.
133  surface->ref();
134  backing_store_out->open_gl.texture.user_data = surface.get();
135  backing_store_out->open_gl.texture.destruction_callback =
136  [](void* user_data) { reinterpret_cast<SkSurface*>(user_data)->unref(); };
137 
138  return true;
139 #else
140  return false;
141 #endif
142 }
143 
144 bool EmbedderTestBackingStoreProducer::CreateSoftware(
145  const FlutterBackingStoreConfig* config,
146  FlutterBackingStore* backing_store_out) {
147  auto surface = SkSurface::MakeRaster(
148  SkImageInfo::MakeN32Premul(config->size.width, config->size.height));
149 
150  if (!surface) {
151  FML_LOG(ERROR)
152  << "Could not create the render target for compositor layer.";
153  return false;
154  }
155 
156  SkPixmap pixmap;
157  if (!surface->peekPixels(&pixmap)) {
158  FML_LOG(ERROR) << "Could not peek pixels of pixmap.";
159  return false;
160  }
161 
162  backing_store_out->type = kFlutterBackingStoreTypeSoftware;
163  backing_store_out->user_data = surface.get();
164  backing_store_out->software.allocation = pixmap.addr();
165  backing_store_out->software.row_bytes = pixmap.rowBytes();
166  backing_store_out->software.height = pixmap.height();
167  // The balancing unref is in the destruction callback.
168  surface->ref();
169  backing_store_out->software.user_data = surface.get();
170  backing_store_out->software.destruction_callback = [](void* user_data) {
171  reinterpret_cast<SkSurface*>(user_data)->unref();
172  };
173 
174  return true;
175 }
176 
177 } // namespace testing
178 } // namespace flutter
Specified an software allocation for Flutter to render into using the CPU.
Definition: embedder.h:846
FlutterOpenGLFramebuffer framebuffer
Definition: embedder.h:769
#define FML_LOG(severity)
Definition: logging.h:65
EmbedderTestBackingStoreProducer(sk_sp< GrDirectContext > context, RenderTargetType type)
VoidCallback destruction_callback
Definition: embedder.h:787
FlutterOpenGLTargetType type
Definition: embedder.h:763
VoidCallback destruction_callback
Definition: embedder.h:291
G_BEGIN_DECLS FlValue gpointer user_data
FlutterSoftwareBackingStore software
The description of the software backing store.
Definition: embedder.h:865
VoidCallback destruction_callback
Definition: embedder.h:266
size_t row_bytes
The number of bytes in a single row of the allocation.
Definition: embedder.h:778
FlutterOpenGLBackingStore open_gl
The description of the OpenGL backing store.
Definition: embedder.h:863
double width
Definition: embedder.h:311
size_t height
The number of rows in the allocation.
Definition: embedder.h:780
FlutterBackingStoreType type
Specifies the type of backing store.
Definition: embedder.h:857
double height
Definition: embedder.h:312
uint32_t name
The name of the texture.
Definition: embedder.h:259
FlutterSize size
The size of the render target the engine expects to render into.
Definition: embedder.h:873
void * user_data
User data to be returned on the invocation of the destruction callback.
Definition: embedder.h:287
void * user_data
User data to be returned on the invocation of the destruction callback.
Definition: embedder.h:263
uint32_t format
The texture format (example GL_RGBA8).
Definition: embedder.h:261
uint32_t name
The name of the framebuffer.
Definition: embedder.h:284
FlutterOpenGLTexture texture
A texture for Flutter to render into.
Definition: embedder.h:766
bool Create(const FlutterBackingStoreConfig *config, FlutterBackingStore *renderer_out)