MCPcopy Create free account
hub / github.com/Snapchat/Valdi / snapshotView

Method snapshotView

valdi/src/valdi/snap_drawing/SnapDrawingViewTransaction.cpp:197–255  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

195void SnapDrawingViewTransaction::cancelAllViewAnimations(const Ref<View>& view) {}
196
197void SnapDrawingViewTransaction::willEnqueueViewToPool(const Ref<View>& view, Valdi::Function<void(View&)> onEnqueue) {
198 auto snapDrawingView = toLayer(view);
199 if (snapDrawingView == nullptr) {
200 return;
201 }
202 snapDrawingView->setAttachedData(nullptr);
203
204 snapDrawingView->prepareForReuse();
205 snapDrawingView->removeAllAnimations();
206
207 onEnqueue(*view);
208}
209
210void SnapDrawingViewTransaction::snapshotView(const Ref<View>& view,
211 Valdi::Function<void(Valdi::Result<Valdi::BytesView>)> cb) {
212 auto layer = toLayer(view);
213 if (layer == nullptr) {
214 cb(Valdi::Error(STRING_LITERAL("View is not a SnapDrawing layer")));
215 return;
216 }
217
218 auto bridgeLayer = Valdi::castOrNull<BridgeLayer>(layer);
219 if (bridgeLayer != nullptr) {
220 auto viewNode = snap::drawing::valdiViewNodeFromLayer(*bridgeLayer);
221 auto innerView = bridgeLayer->getBridgedView();
222 if (viewNode != nullptr && innerView != nullptr) {
223 innerView->getViewTransaction(viewNode->getViewNodeTree())
224 .snapshotView(innerView->getView(), std::move(cb));
225 } else {
226 cb(Valdi::Error(STRING_LITERAL("BridgeLayer does not have associated View")));
227 }
228
229 return;
230 }
231
232 auto root = dynamic_cast<LayerRoot*>(layer->getRoot());
233 auto scale = (root != nullptr) ? root->getScale() : 1.0f;
234
235 auto viewWidth = layer->getFrame().width() * scale;
236 auto viewHeight = layer->getFrame().height() * scale;
237
238 if (viewWidth < 1 || viewHeight < 1) {
239 cb(Valdi::Error(STRING_FORMAT("View is too small: {}x{}", viewWidth, viewHeight)));
240 return;
241 }
242
243 auto iViewWidth = static_cast<uint32_t>(viewWidth);
244 auto iViewHeight = static_cast<uint32_t>(viewHeight);
245
246 BitmapGraphicsContext graphicsContext;
247 auto surface = graphicsContext.createBitmapSurface(
248 Valdi::BitmapInfo(iViewWidth, iViewHeight, Valdi::ColorTypeRGBA8888, Valdi::AlphaTypePremul, 0));
249
250 auto canvasResult = surface->prepareCanvas();
251
252 if (!canvasResult) {
253 cb(canvasResult.error());
254 return;

Callers

nothing calls this directly

Calls 15

toLayerFunction · 0.85
valdiViewNodeFromLayerFunction · 0.85
drawLayerInCanvasFunction · 0.85
getBridgedViewMethod · 0.80
getViewNodeTreeMethod · 0.80
getScaleMethod · 0.80
getFrameMethod · 0.80
createBitmapSurfaceMethod · 0.80
toPNGMethod · 0.80
errorMethod · 0.65
valueMethod · 0.65
cbFunction · 0.50

Tested by

no test coverage detected