+static bool
+panfrost_should_afbc(struct panfrost_device *dev, const struct panfrost_resource *pres)
+{
+ /* AFBC resources may be rendered to, textured from, or shared across
+ * processes, but may not be used as e.g buffers */
+ const unsigned valid_binding =
+ PIPE_BIND_DEPTH_STENCIL |
+ PIPE_BIND_RENDER_TARGET |
+ PIPE_BIND_BLENDABLE |
+ PIPE_BIND_SAMPLER_VIEW |
+ PIPE_BIND_DISPLAY_TARGET |
+ PIPE_BIND_SCANOUT |
+ PIPE_BIND_SHARED;
+
+ if (pres->base.bind & ~valid_binding)
+ return false;
+
+ /* AFBC introduced with Mali T760 */
+ if (dev->quirks & MIDGARD_NO_AFBC)
+ return false;
+
+ /* AFBC<-->staging is expensive */
+ if (pres->base.usage == PIPE_USAGE_STREAM)
+ return false;
+
+ /* Only a small selection of formats are AFBC'able */
+ if (!panfrost_format_supports_afbc(pres->internal_format))
+ return false;
+
+ /* AFBC does not support layered (GLES3 style) multisampling. Use
+ * EXT_multisampled_render_to_texture instead */
+ if (pres->base.nr_samples > 1)
+ return false;
+
+ /* TODO: Is AFBC of 3D textures possible? */
+ if ((pres->base.target != PIPE_TEXTURE_2D) && (pres->base.target != PIPE_TEXTURE_RECT))
+ return false;
+
+ /* For one tile, AFBC is a loss compared to u-interleaved */
+ if (pres->base.width0 <= 16 && pres->base.height0 <= 16)
+ return false;
+
+ /* Otherwise, we'd prefer AFBC as it is dramatically more efficient
+ * than linear or usually even u-interleaved */
+ return true;
+}
+