|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + */ |
| 7 | + |
| 8 | +package com.facebook.react.uimanager.drawable |
| 9 | + |
| 10 | +import android.graphics.Canvas |
| 11 | +import android.graphics.ColorFilter |
| 12 | +import android.graphics.Paint |
| 13 | +import android.graphics.PixelFormat |
| 14 | +import android.graphics.PorterDuff |
| 15 | +import android.graphics.PorterDuffXfermode |
| 16 | +import android.graphics.drawable.Animatable |
| 17 | +import android.graphics.drawable.Drawable |
| 18 | +import com.facebook.drawee.controller.ControllerListener |
| 19 | +import com.facebook.drawee.drawable.DrawableParent |
| 20 | +import com.facebook.drawee.generic.GenericDraweeHierarchy |
| 21 | +import com.facebook.drawee.generic.GenericDraweeHierarchyBuilder |
| 22 | +import com.facebook.drawee.view.DraweeHolder |
| 23 | +import com.facebook.imagepipeline.image.ImageInfo |
| 24 | +import com.facebook.react.views.image.ImageResizeMode |
| 25 | + |
| 26 | +/** |
| 27 | + * A Drawable that uses DraweeHolder to load and display an image mask. |
| 28 | + * This avoids creating Bitmap copies and leverages Fresco's caching and lifecycle management. |
| 29 | + * |
| 30 | + * The mask is applied using Porter-Duff DST_IN mode - the Drawable's alpha channel |
| 31 | + * determines what parts of the view are visible. |
| 32 | + */ |
| 33 | +internal class DraweeMaskDrawable( |
| 34 | + resources: android.content.res.Resources, |
| 35 | +) : Drawable(), MaskDrawable { |
| 36 | + |
| 37 | + private val draweeHolder: DraweeHolder<GenericDraweeHierarchy> = |
| 38 | + DraweeHolder(GenericDraweeHierarchyBuilder.newInstance(resources).setFadeDuration(0).build()) |
| 39 | + |
| 40 | + |
| 41 | + // Note: Porter-Duff compositing is applied at the view level, not here |
| 42 | + // This Drawable just draws the image normally - its alpha channel will be used for masking |
| 43 | + |
| 44 | + init { |
| 45 | + // Set ourselves as callback to invalidate when DraweeDrawable changes |
| 46 | + // This will be updated when the controller is set |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Sets the ImageRequest to load as the mask. |
| 51 | + * The DraweeHolder will handle loading, caching, and lifecycle management. |
| 52 | + */ |
| 53 | + fun setImageRequest(imageRequest: com.facebook.imagepipeline.request.ImageRequest?) { |
| 54 | + if (imageRequest == null) { |
| 55 | + draweeHolder.controller = null |
| 56 | + return |
| 57 | + } |
| 58 | + |
| 59 | + val controllerBuilder = com.facebook.drawee.backends.pipeline.Fresco.newDraweeControllerBuilder() |
| 60 | + controllerBuilder |
| 61 | + .setImageRequest(imageRequest) |
| 62 | + .setAutoPlayAnimations(true) |
| 63 | + .setOldController(draweeHolder.controller) |
| 64 | + |
| 65 | + draweeHolder.controller = controllerBuilder.build() |
| 66 | + controllerBuilder.reset() |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Attaches the DraweeHolder (should be called when view is attached to window) |
| 71 | + */ |
| 72 | + override fun onAttach() { |
| 73 | + draweeHolder.onAttach() |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Detaches the DraweeHolder (should be called when view is detached from window) |
| 78 | + */ |
| 79 | + fun onDetach() { |
| 80 | + draweeHolder.onDetach() |
| 81 | + } |
| 82 | + |
| 83 | + override fun draw(canvas: Canvas) { |
| 84 | + val drawable = draweeHolder.topLevelDrawable ?: return |
| 85 | + val bounds = bounds |
| 86 | + |
| 87 | + // Set bounds if not already set |
| 88 | + if (drawable.bounds.isEmpty) { |
| 89 | + drawable.bounds = bounds |
| 90 | + } |
| 91 | + |
| 92 | + // Draw the DraweeDrawable normally |
| 93 | + // The Porter-Duff DST_IN mode will be applied at the view level when this Drawable |
| 94 | + // is drawn with a Paint that has the xfermode set |
| 95 | + drawable.draw(canvas) |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Draws this Drawable with Porter-Duff compositing applied. |
| 100 | + * This is called from ReactViewGroup.draw() to apply the mask. |
| 101 | + * |
| 102 | + * Since we're already inside a saveLayer in ReactViewGroup.draw(), we create |
| 103 | + * a nested saveLayer with Porter-Duff Paint. When we restore this inner layer, |
| 104 | + * it composites with the outer layer using Porter-Duff DST_IN mode. |
| 105 | + * |
| 106 | + * This avoids creating Bitmap copies - the DraweeDrawable is drawn directly. |
| 107 | + */ |
| 108 | + override fun drawWithMaskMode(canvas: Canvas, maskPaint: Paint) { |
| 109 | + val drawable = draweeHolder.topLevelDrawable ?: return |
| 110 | + val bounds = bounds |
| 111 | + |
| 112 | + if (bounds.isEmpty) { |
| 113 | + return |
| 114 | + } |
| 115 | + |
| 116 | + // Set bounds if not already set |
| 117 | + if (drawable.bounds.isEmpty) { |
| 118 | + drawable.bounds = bounds |
| 119 | + } |
| 120 | + |
| 121 | + // Create a nested saveLayer with Porter-Duff DST_IN mode |
| 122 | + // When we restore this layer, it will composite with the outer layer (which contains |
| 123 | + // the background + children) using DST_IN mode, effectively masking the content |
| 124 | + val saveCount = canvas.saveLayer( |
| 125 | + bounds.left.toFloat(), |
| 126 | + bounds.top.toFloat(), |
| 127 | + bounds.right.toFloat(), |
| 128 | + bounds.bottom.toFloat(), |
| 129 | + maskPaint |
| 130 | + ) |
| 131 | + |
| 132 | + // Draw the DraweeDrawable to the inner layer |
| 133 | + drawable.draw(canvas) |
| 134 | + |
| 135 | + // Restore the inner layer - this composites it with the outer layer using DST_IN mode |
| 136 | + canvas.restoreToCount(saveCount) |
| 137 | + } |
| 138 | + |
| 139 | + override fun setBounds(left: Int, top: Int, right: Int, bottom: Int) { |
| 140 | + super.setBounds(left, top, right, bottom) |
| 141 | + draweeHolder.topLevelDrawable?.setBounds(left, top, right, bottom) |
| 142 | + } |
| 143 | + |
| 144 | + override fun setAlpha(alpha: Int) { |
| 145 | + draweeHolder.topLevelDrawable?.alpha = alpha |
| 146 | + invalidateSelf() |
| 147 | + } |
| 148 | + |
| 149 | + override fun setColorFilter(colorFilter: ColorFilter?) { |
| 150 | + draweeHolder.topLevelDrawable?.colorFilter = colorFilter |
| 151 | + invalidateSelf() |
| 152 | + } |
| 153 | + |
| 154 | + @Deprecated("Deprecated in Java") |
| 155 | + override fun getOpacity(): Int { |
| 156 | + return draweeHolder.topLevelDrawable?.opacity ?: PixelFormat.TRANSLUCENT |
| 157 | + } |
| 158 | +} |
| 159 | + |
0 commit comments