@@ -1345,5 +1345,93 @@ TEST_F(ShellTest, RasterizerMakeRasterSnapshot) {
13451345 DestroyShell (std::move (shell), std::move (task_runners));
13461346}
13471347
1348+ static sk_sp<SkPicture> MakeSizedPicture (int width, int height) {
1349+ SkPictureRecorder recorder;
1350+ SkCanvas* recording_canvas =
1351+ recorder.beginRecording (SkRect::MakeXYWH (0 , 0 , width, height));
1352+ recording_canvas->drawRect (SkRect::MakeXYWH (0 , 0 , width, height),
1353+ SkPaint (SkColor4f::FromColor (SK_ColorRED)));
1354+ return recorder.finishRecordingAsPicture ();
1355+ }
1356+
1357+ TEST_F (ShellTest, OnServiceProtocolEstimateRasterCacheMemoryWorks) {
1358+ Settings settings = CreateSettingsForFixture ();
1359+ std::unique_ptr<Shell> shell = CreateShell (settings);
1360+
1361+ // 1. Construct a picture and a picture layer to be raster cached.
1362+ sk_sp<SkPicture> picture = MakeSizedPicture (10 , 10 );
1363+ fml::RefPtr<SkiaUnrefQueue> queue = fml::MakeRefCounted<SkiaUnrefQueue>(
1364+ GetCurrentTaskRunner (), fml::TimeDelta::FromSeconds (0 ));
1365+ auto picture_layer = std::make_shared<PictureLayer>(
1366+ SkPoint::Make (0 , 0 ),
1367+ flutter::SkiaGPUObject<SkPicture>({MakeSizedPicture (100 , 100 ), queue}),
1368+ false , false );
1369+ picture_layer->set_paint_bounds (SkRect::MakeWH (100 , 100 ));
1370+
1371+ // 2. Rasterize the picture and the picture layer in the raster cache.
1372+ std::promise<bool > rasterized;
1373+ shell->GetTaskRunners ().GetRasterTaskRunner ()->PostTask (
1374+ [&shell, &rasterized, &picture, &picture_layer] {
1375+ auto * compositor_context = shell->GetRasterizer ()->compositor_context ();
1376+ auto & raster_cache = compositor_context->raster_cache ();
1377+ // 2.1. Rasterize the picture. Call Draw multiple times to pass the
1378+ // access threshold (default to 3) so a cache can be generated.
1379+ SkCanvas dummy_canvas;
1380+ bool picture_cache_generated;
1381+ for (int i = 0 ; i < 4 ; i += 1 ) {
1382+ picture_cache_generated =
1383+ raster_cache.Prepare (nullptr , // GrDirectContext
1384+ picture.get (), SkMatrix::I (),
1385+ nullptr , // SkColorSpace
1386+ true , // isComplex
1387+ false // willChange
1388+ );
1389+ raster_cache.Draw (*picture, dummy_canvas);
1390+ }
1391+ ASSERT_TRUE (picture_cache_generated);
1392+
1393+ // 2.2. Rasterize the picture layer.
1394+ Stopwatch raster_time;
1395+ Stopwatch ui_time;
1396+ MutatorsStack mutators_stack;
1397+ TextureRegistry texture_registry;
1398+ PrerollContext preroll_context = {
1399+ nullptr , /* raster_cache */
1400+ nullptr , /* gr_context */
1401+ nullptr , /* external_view_embedder */
1402+ mutators_stack, nullptr , /* color_space */
1403+ kGiantRect , /* cull_rect */
1404+ false , /* layer reads from surface */
1405+ raster_time, ui_time, texture_registry,
1406+ false , /* checkerboard_offscreen_layers */
1407+ 100 .0f , /* frame_physical_depth */
1408+ 1 .0f , /* frame_device_pixel_ratio */
1409+ 0 .0f , /* total_elevation */
1410+ false , /* has_platform_view */
1411+ };
1412+ raster_cache.Prepare (&preroll_context, picture_layer.get (),
1413+ SkMatrix::I ());
1414+ rasterized.set_value (true );
1415+ });
1416+ rasterized.get_future ().wait ();
1417+
1418+ // 3. Call the service protocol and check its output.
1419+ ServiceProtocol::Handler::ServiceProtocolMap empty_params;
1420+ rapidjson::Document document;
1421+ OnServiceProtocol (
1422+ shell.get (), ServiceProtocolEnum::kEstimateRasterCacheMemory ,
1423+ shell->GetTaskRunners ().GetRasterTaskRunner (), empty_params, &document);
1424+ rapidjson::StringBuffer buffer;
1425+ rapidjson::Writer<rapidjson::StringBuffer> writer (buffer);
1426+ document.Accept (writer);
1427+ std::string expected_json =
1428+ " {\" type\" :\" EstimateRasterCacheMemory\" ,\" layerBytes\" :40000,\" picture"
1429+ " Bytes\" :400}" ;
1430+ std::string actual_json = buffer.GetString ();
1431+ ASSERT_EQ (actual_json, expected_json);
1432+
1433+ DestroyShell (std::move (shell));
1434+ }
1435+
13481436} // namespace testing
13491437} // namespace flutter
0 commit comments