Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0 OR MIT
0002 /**************************************************************************
0003  *
0004  * Copyright (c) 2018 VMware, Inc., Palo Alto, CA., USA
0005  * All Rights Reserved.
0006  *
0007  * Permission is hereby granted, free of charge, to any person obtaining a
0008  * copy of this software and associated documentation files (the
0009  * "Software"), to deal in the Software without restriction, including
0010  * without limitation the rights to use, copy, modify, merge, publish,
0011  * distribute, sub license, and/or sell copies of the Software, and to
0012  * permit persons to whom the Software is furnished to do so, subject to
0013  * the following conditions:
0014  *
0015  * The above copyright notice and this permission notice (including the
0016  * next paragraph) shall be included in all copies or substantial portions
0017  * of the Software.
0018  *
0019  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
0020  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
0021  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
0022  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
0023  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
0024  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
0025  * USE OR OTHER DEALINGS IN THE SOFTWARE.
0026  *
0027  * Authors:
0028  * Deepak Rawat <drawat@vmware.com>
0029  * Rob Clark <robdclark@gmail.com>
0030  *
0031  **************************************************************************/
0032 
0033 #include <drm/drm_atomic.h>
0034 #include <drm/drm_damage_helper.h>
0035 #include <drm/drm_device.h>
0036 #include <drm/drm_framebuffer.h>
0037 
0038 static void convert_clip_rect_to_rect(const struct drm_clip_rect *src,
0039                       struct drm_mode_rect *dest,
0040                       uint32_t num_clips, uint32_t src_inc)
0041 {
0042     while (num_clips > 0) {
0043         dest->x1 = src->x1;
0044         dest->y1 = src->y1;
0045         dest->x2 = src->x2;
0046         dest->y2 = src->y2;
0047         src += src_inc;
0048         dest++;
0049         num_clips--;
0050     }
0051 }
0052 
0053 /**
0054  * drm_atomic_helper_check_plane_damage - Verify plane damage on atomic_check.
0055  * @state: The driver state object.
0056  * @plane_state: Plane state for which to verify damage.
0057  *
0058  * This helper function makes sure that damage from plane state is discarded
0059  * for full modeset. If there are more reasons a driver would want to do a full
0060  * plane update rather than processing individual damage regions, then those
0061  * cases should be taken care of here.
0062  *
0063  * Note that &drm_plane_state.fb_damage_clips == NULL in plane state means that
0064  * full plane update should happen. It also ensure helper iterator will return
0065  * &drm_plane_state.src as damage.
0066  */
0067 void drm_atomic_helper_check_plane_damage(struct drm_atomic_state *state,
0068                       struct drm_plane_state *plane_state)
0069 {
0070     struct drm_crtc_state *crtc_state;
0071 
0072     if (plane_state->crtc) {
0073         crtc_state = drm_atomic_get_new_crtc_state(state,
0074                                plane_state->crtc);
0075 
0076         if (WARN_ON(!crtc_state))
0077             return;
0078 
0079         if (drm_atomic_crtc_needs_modeset(crtc_state)) {
0080             drm_property_blob_put(plane_state->fb_damage_clips);
0081             plane_state->fb_damage_clips = NULL;
0082         }
0083     }
0084 }
0085 EXPORT_SYMBOL(drm_atomic_helper_check_plane_damage);
0086 
0087 /**
0088  * drm_atomic_helper_dirtyfb - Helper for dirtyfb.
0089  * @fb: DRM framebuffer.
0090  * @file_priv: Drm file for the ioctl call.
0091  * @flags: Dirty fb annotate flags.
0092  * @color: Color for annotate fill.
0093  * @clips: Dirty region.
0094  * @num_clips: Count of clip in clips.
0095  *
0096  * A helper to implement &drm_framebuffer_funcs.dirty using damage interface
0097  * during plane update. If num_clips is 0 then this helper will do a full plane
0098  * update. This is the same behaviour expected by DIRTFB IOCTL.
0099  *
0100  * Note that this helper is blocking implementation. This is what current
0101  * drivers and userspace expect in their DIRTYFB IOCTL implementation, as a way
0102  * to rate-limit userspace and make sure its rendering doesn't get ahead of
0103  * uploading new data too much.
0104  *
0105  * Return: Zero on success, negative errno on failure.
0106  */
0107 int drm_atomic_helper_dirtyfb(struct drm_framebuffer *fb,
0108                   struct drm_file *file_priv, unsigned int flags,
0109                   unsigned int color, struct drm_clip_rect *clips,
0110                   unsigned int num_clips)
0111 {
0112     struct drm_modeset_acquire_ctx ctx;
0113     struct drm_property_blob *damage = NULL;
0114     struct drm_mode_rect *rects = NULL;
0115     struct drm_atomic_state *state;
0116     struct drm_plane *plane;
0117     int ret = 0;
0118 
0119     /*
0120      * When called from ioctl, we are interruptible, but not when called
0121      * internally (ie. defio worker)
0122      */
0123     drm_modeset_acquire_init(&ctx,
0124         file_priv ? DRM_MODESET_ACQUIRE_INTERRUPTIBLE : 0);
0125 
0126     state = drm_atomic_state_alloc(fb->dev);
0127     if (!state) {
0128         ret = -ENOMEM;
0129         goto out_drop_locks;
0130     }
0131     state->acquire_ctx = &ctx;
0132 
0133     if (clips) {
0134         uint32_t inc = 1;
0135 
0136         if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) {
0137             inc = 2;
0138             num_clips /= 2;
0139         }
0140 
0141         rects = kcalloc(num_clips, sizeof(*rects), GFP_KERNEL);
0142         if (!rects) {
0143             ret = -ENOMEM;
0144             goto out;
0145         }
0146 
0147         convert_clip_rect_to_rect(clips, rects, num_clips, inc);
0148         damage = drm_property_create_blob(fb->dev,
0149                           num_clips * sizeof(*rects),
0150                           rects);
0151         if (IS_ERR(damage)) {
0152             ret = PTR_ERR(damage);
0153             damage = NULL;
0154             goto out;
0155         }
0156     }
0157 
0158 retry:
0159     drm_for_each_plane(plane, fb->dev) {
0160         struct drm_plane_state *plane_state;
0161 
0162         ret = drm_modeset_lock(&plane->mutex, state->acquire_ctx);
0163         if (ret)
0164             goto out;
0165 
0166         if (plane->state->fb != fb) {
0167             drm_modeset_unlock(&plane->mutex);
0168             continue;
0169         }
0170 
0171         plane_state = drm_atomic_get_plane_state(state, plane);
0172         if (IS_ERR(plane_state)) {
0173             ret = PTR_ERR(plane_state);
0174             goto out;
0175         }
0176 
0177         drm_property_replace_blob(&plane_state->fb_damage_clips,
0178                       damage);
0179     }
0180 
0181     ret = drm_atomic_commit(state);
0182 
0183 out:
0184     if (ret == -EDEADLK) {
0185         drm_atomic_state_clear(state);
0186         ret = drm_modeset_backoff(&ctx);
0187         if (!ret)
0188             goto retry;
0189     }
0190 
0191     drm_property_blob_put(damage);
0192     kfree(rects);
0193     drm_atomic_state_put(state);
0194 
0195 out_drop_locks:
0196     drm_modeset_drop_locks(&ctx);
0197     drm_modeset_acquire_fini(&ctx);
0198 
0199     return ret;
0200 
0201 }
0202 EXPORT_SYMBOL(drm_atomic_helper_dirtyfb);
0203 
0204 /**
0205  * drm_atomic_helper_damage_iter_init - Initialize the damage iterator.
0206  * @iter: The iterator to initialize.
0207  * @old_state: Old plane state for validation.
0208  * @state: Plane state from which to iterate the damage clips.
0209  *
0210  * Initialize an iterator, which clips plane damage
0211  * &drm_plane_state.fb_damage_clips to plane &drm_plane_state.src. This iterator
0212  * returns full plane src in case damage is not present because either
0213  * user-space didn't sent or driver discarded it (it want to do full plane
0214  * update). Currently this iterator returns full plane src in case plane src
0215  * changed but that can be changed in future to return damage.
0216  *
0217  * For the case when plane is not visible or plane update should not happen the
0218  * first call to iter_next will return false. Note that this helper use clipped
0219  * &drm_plane_state.src, so driver calling this helper should have called
0220  * drm_atomic_helper_check_plane_state() earlier.
0221  */
0222 void
0223 drm_atomic_helper_damage_iter_init(struct drm_atomic_helper_damage_iter *iter,
0224                    const struct drm_plane_state *old_state,
0225                    const struct drm_plane_state *state)
0226 {
0227     memset(iter, 0, sizeof(*iter));
0228 
0229     if (!state || !state->crtc || !state->fb || !state->visible)
0230         return;
0231 
0232     iter->clips = (struct drm_rect *)drm_plane_get_damage_clips(state);
0233     iter->num_clips = drm_plane_get_damage_clips_count(state);
0234 
0235     /* Round down for x1/y1 and round up for x2/y2 to catch all pixels */
0236     iter->plane_src.x1 = state->src.x1 >> 16;
0237     iter->plane_src.y1 = state->src.y1 >> 16;
0238     iter->plane_src.x2 = (state->src.x2 >> 16) + !!(state->src.x2 & 0xFFFF);
0239     iter->plane_src.y2 = (state->src.y2 >> 16) + !!(state->src.y2 & 0xFFFF);
0240 
0241     if (!iter->clips || !drm_rect_equals(&state->src, &old_state->src)) {
0242         iter->clips = NULL;
0243         iter->num_clips = 0;
0244         iter->full_update = true;
0245     }
0246 }
0247 EXPORT_SYMBOL(drm_atomic_helper_damage_iter_init);
0248 
0249 /**
0250  * drm_atomic_helper_damage_iter_next - Advance the damage iterator.
0251  * @iter: The iterator to advance.
0252  * @rect: Return a rectangle in fb coordinate clipped to plane src.
0253  *
0254  * Since plane src is in 16.16 fixed point and damage clips are whole number,
0255  * this iterator round off clips that intersect with plane src. Round down for
0256  * x1/y1 and round up for x2/y2 for the intersected coordinate. Similar rounding
0257  * off for full plane src, in case it's returned as damage. This iterator will
0258  * skip damage clips outside of plane src.
0259  *
0260  * Return: True if the output is valid, false if reached the end.
0261  *
0262  * If the first call to iterator next returns false then it means no need to
0263  * update the plane.
0264  */
0265 bool
0266 drm_atomic_helper_damage_iter_next(struct drm_atomic_helper_damage_iter *iter,
0267                    struct drm_rect *rect)
0268 {
0269     bool ret = false;
0270 
0271     if (iter->full_update) {
0272         *rect = iter->plane_src;
0273         iter->full_update = false;
0274         return true;
0275     }
0276 
0277     while (iter->curr_clip < iter->num_clips) {
0278         *rect = iter->clips[iter->curr_clip];
0279         iter->curr_clip++;
0280 
0281         if (drm_rect_intersect(rect, &iter->plane_src)) {
0282             ret = true;
0283             break;
0284         }
0285     }
0286 
0287     return ret;
0288 }
0289 EXPORT_SYMBOL(drm_atomic_helper_damage_iter_next);
0290 
0291 /**
0292  * drm_atomic_helper_damage_merged - Merged plane damage
0293  * @old_state: Old plane state for validation.
0294  * @state: Plane state from which to iterate the damage clips.
0295  * @rect: Returns the merged damage rectangle
0296  *
0297  * This function merges any valid plane damage clips into one rectangle and
0298  * returns it in @rect.
0299  *
0300  * For details see: drm_atomic_helper_damage_iter_init() and
0301  * drm_atomic_helper_damage_iter_next().
0302  *
0303  * Returns:
0304  * True if there is valid plane damage otherwise false.
0305  */
0306 bool drm_atomic_helper_damage_merged(const struct drm_plane_state *old_state,
0307                      struct drm_plane_state *state,
0308                      struct drm_rect *rect)
0309 {
0310     struct drm_atomic_helper_damage_iter iter;
0311     struct drm_rect clip;
0312     bool valid = false;
0313 
0314     rect->x1 = INT_MAX;
0315     rect->y1 = INT_MAX;
0316     rect->x2 = 0;
0317     rect->y2 = 0;
0318 
0319     drm_atomic_helper_damage_iter_init(&iter, old_state, state);
0320     drm_atomic_for_each_plane_damage(&iter, &clip) {
0321         rect->x1 = min(rect->x1, clip.x1);
0322         rect->y1 = min(rect->y1, clip.y1);
0323         rect->x2 = max(rect->x2, clip.x2);
0324         rect->y2 = max(rect->y2, clip.y2);
0325         valid = true;
0326     }
0327 
0328     return valid;
0329 }
0330 EXPORT_SYMBOL(drm_atomic_helper_damage_merged);