| 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/flow/layers/texture_layer.h" |
| 6 | |
| 7 | #include "flutter/common/graphics/texture.h" |
| 8 | |
| 9 | namespace flutter { |
| 10 | |
| 11 | TextureLayer::TextureLayer(const SkPoint& offset, |
| 12 | const SkSize& size, |
| 13 | int64_t texture_id, |
| 14 | bool freeze, |
| 15 | DlImageSampling sampling) |
| 16 | : offset_(offset), |
| 17 | size_(size), |
| 18 | texture_id_(texture_id), |
| 19 | freeze_(freeze), |
| 20 | sampling_(sampling) {} |
| 21 | |
| 22 | void TextureLayer::Diff(DiffContext* context, const Layer* old_layer) { |
| 23 | DiffContext::AutoSubtreeRestore subtree(context); |
| 24 | if (!context->IsSubtreeDirty()) { |
| 25 | FML_DCHECK(old_layer); |
| 26 | auto prev = old_layer->as_texture_layer(); |
| 27 | // TODO(knopp) It would be nice to be able to determine that a texture is |
| 28 | // dirty |
| 29 | context->MarkSubtreeDirty(previous_paint_region: context->GetOldLayerPaintRegion(layer: prev)); |
| 30 | } |
| 31 | |
| 32 | // Make sure DiffContext knows there is a TextureLayer in this subtree. |
| 33 | // This prevents ContainerLayer from skipping TextureLayer diffing when |
| 34 | // TextureLayer is inside retained layer. |
| 35 | // See ContainerLayer::DiffChildren |
| 36 | // https://github.com/flutter/flutter/issues/92925 |
| 37 | context->MarkSubtreeHasTextureLayer(); |
| 38 | context->AddLayerBounds(rect: SkRect::MakeXYWH(x: offset_.x(), y: offset_.y(), |
| 39 | w: size_.width(), h: size_.height())); |
| 40 | context->SetLayerPaintRegion(layer: this, region: context->CurrentSubtreeRegion()); |
| 41 | } |
| 42 | |
| 43 | void TextureLayer::Preroll(PrerollContext* context) { |
| 44 | set_paint_bounds(SkRect::MakeXYWH(x: offset_.x(), y: offset_.y(), w: size_.width(), |
| 45 | h: size_.height())); |
| 46 | context->has_texture_layer = true; |
| 47 | context->renderable_state_flags = LayerStateStack::kCallerCanApplyOpacity; |
| 48 | } |
| 49 | |
| 50 | void TextureLayer::Paint(PaintContext& context) const { |
| 51 | FML_DCHECK(needs_painting(context)); |
| 52 | |
| 53 | std::shared_ptr<Texture> texture = |
| 54 | context.texture_registry |
| 55 | ? context.texture_registry->GetTexture(id: texture_id_) |
| 56 | : nullptr; |
| 57 | if (!texture) { |
| 58 | TRACE_EVENT_INSTANT0("flutter" , "null texture" ); |
| 59 | return; |
| 60 | } |
| 61 | DlPaint paint; |
| 62 | Texture::PaintContext ctx{ |
| 63 | .canvas = context.canvas, |
| 64 | .gr_context = context.gr_context, |
| 65 | .aiks_context = context.aiks_context, |
| 66 | .paint = context.state_stack.fill(paint), |
| 67 | }; |
| 68 | texture->Paint(context&: ctx, bounds: paint_bounds(), freeze: freeze_, sampling: sampling_); |
| 69 | } |
| 70 | |
| 71 | } // namespace flutter |
| 72 | |