| 91 | } |
| 92 | |
| 93 | Valdi::Result<Ref<Valdi::IBitmap>> ImageQueue::allocateBitmap(int width, int height, Valdi::ColorType colorType) { |
| 94 | auto bitmapInfo = Valdi::BitmapInfo(width, height, colorType, Valdi::AlphaType::AlphaTypePremul, 0); |
| 95 | |
| 96 | { |
| 97 | std::lock_guard<Valdi::Mutex> lock(_mutex); |
| 98 | if (!isCacheCompatibleWithBitmapInfo(bitmapInfo)) { |
| 99 | _cacheBitmapInfo = {bitmapInfo}; |
| 100 | _cachedBitmaps.clear(); |
| 101 | } |
| 102 | |
| 103 | if (!_cachedBitmaps.empty()) { |
| 104 | auto cachedBitmap = std::move(_cachedBitmaps.back()); |
| 105 | _cachedBitmaps.pop_back(); |
| 106 | |
| 107 | Ref<Valdi::IBitmap> pooledBitmap = Valdi::makeShared<PooledBitmap>(this, std::move(cachedBitmap)); |
| 108 | return pooledBitmap; |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | // Cache is empty, allocating the bitmap |
| 113 | |
| 114 | auto bitmap = Bitmap::make(bitmapInfo); |
| 115 | if (!bitmap) { |
| 116 | return bitmap.moveError(); |
| 117 | } |
| 118 | |
| 119 | Ref<Valdi::IBitmap> pooledBitmap = Valdi::makeShared<PooledBitmap>(this, bitmap.moveValue()); |
| 120 | return pooledBitmap; |
| 121 | } |
| 122 | |
| 123 | void ImageQueue::enqueueCachedBitmap(const Ref<Valdi::IBitmap>& sourceBitmap) { |
| 124 | std::lock_guard<Valdi::Mutex> lock(_mutex); |