@@ -297,34 +297,40 @@ static std::shared_ptr<SkBitmap> CreateAtlasBitmap(const GlyphAtlas& atlas,
297
297
return bitmap;
298
298
}
299
299
300
- static std::shared_ptr<Texture> UploadGlyphTextureAtlas (
300
+ static std::shared_ptr<Texture> CreateGlyphTextureAtlas (
301
301
const std::shared_ptr<Allocator>& allocator,
302
- std::shared_ptr<SkBitmap> bitmap,
303
302
const ISize& atlas_size,
304
303
PixelFormat format) {
305
304
TRACE_EVENT0 (" impeller" , __FUNCTION__);
306
305
if (!allocator) {
307
306
return nullptr ;
308
307
}
309
308
310
- FML_DCHECK (bitmap != nullptr );
311
- const auto & pixmap = bitmap->pixmap ();
312
-
313
309
TextureDescriptor texture_descriptor;
314
310
texture_descriptor.storage_mode = StorageMode::kHostVisible ;
315
311
texture_descriptor.format = format;
316
312
texture_descriptor.size = atlas_size;
317
313
318
- if (pixmap.rowBytes () * pixmap.height () !=
319
- texture_descriptor.GetByteSizeOfBaseMipLevel ()) {
320
- return nullptr ;
321
- }
322
-
323
314
auto texture = allocator->CreateTexture (texture_descriptor);
324
315
if (!texture || !texture->IsValid ()) {
325
316
return nullptr ;
326
317
}
327
318
texture->SetLabel (" GlyphAtlas" );
319
+ return texture;
320
+ }
321
+
322
+ bool UploadGlyphTextureAtlas (const std::shared_ptr<Texture>& texture,
323
+ std::shared_ptr<SkBitmap> bitmap) {
324
+ TRACE_EVENT0 (" impeller" , __FUNCTION__);
325
+
326
+ FML_DCHECK (bitmap != nullptr );
327
+ const auto & pixmap = bitmap->pixmap ();
328
+
329
+ auto texture_descriptor = texture->GetTextureDescriptor ();
330
+ if (pixmap.rowBytes () * pixmap.height () !=
331
+ texture_descriptor.GetByteSizeOfBaseMipLevel ()) {
332
+ return false ;
333
+ }
328
334
329
335
auto mapping = std::make_shared<fml::NonOwnedMapping>(
330
336
reinterpret_cast <const uint8_t *>(bitmap->getAddr (0 , 0 )), // data
@@ -333,9 +339,9 @@ static std::shared_ptr<Texture> UploadGlyphTextureAtlas(
333
339
);
334
340
335
341
if (!texture->SetContents (mapping)) {
336
- return nullptr ;
342
+ return false ;
337
343
}
338
- return texture ;
344
+ return true ;
339
345
}
340
346
341
347
std::shared_ptr<GlyphAtlas> TextRenderContextSkia::CreateGlyphAtlas (
@@ -421,16 +427,29 @@ std::shared_ptr<GlyphAtlas> TextRenderContextSkia::CreateGlyphAtlas(
421
427
format = PixelFormat::kR8G8B8A8UNormInt ;
422
428
break ;
423
429
}
424
- auto texture = UploadGlyphTextureAtlas (GetContext ()->GetResourceAllocator (),
425
- bitmap, atlas_size, format);
426
- if (!texture) {
427
- return nullptr ;
428
- }
429
430
430
431
// ---------------------------------------------------------------------------
431
432
// Step 8: Record the texture in the glyph atlas.
433
+ //
434
+ // If the last_texture is the same size and type, reuse this instead of
435
+ // creating a new texture.
432
436
// ---------------------------------------------------------------------------
433
- glyph_atlas->SetTexture (std::move (texture));
437
+ auto old_texture = last_atlas->GetTexture ();
438
+ if (old_texture != nullptr &&
439
+ old_texture->GetTextureDescriptor ().size == atlas_size &&
440
+ old_texture->GetTextureDescriptor ().format == format) {
441
+ if (!UploadGlyphTextureAtlas (old_texture, bitmap)) {
442
+ return nullptr ;
443
+ }
444
+ glyph_atlas->SetTexture (std::move (old_texture));
445
+ } else {
446
+ auto texture = CreateGlyphTextureAtlas (GetContext ()->GetResourceAllocator (),
447
+ atlas_size, format);
448
+ if (!texture || !UploadGlyphTextureAtlas (texture, bitmap)) {
449
+ return nullptr ;
450
+ }
451
+ glyph_atlas->SetTexture (std::move (texture));
452
+ }
434
453
435
454
return glyph_atlas;
436
455
}
0 commit comments