bokan | e7a058a | 2017-03-02 22:42:51 | [diff] [blame] | 1 | // Copyright 2016 The Chromium 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 | |
danakj | 83d0eb4f | 2017-09-19 17:31:58 | [diff] [blame] | 5 | #include "cc/layers/nine_patch_generator.h" |
bokan | e7a058a | 2017-03-02 22:42:51 | [diff] [blame] | 6 | |
Xianzhu Wang | 61ace67 | 2020-02-08 02:32:42 | [diff] [blame] | 7 | #include "base/trace_event/traced_value.h" |
| 8 | #include "cc/base/math_util.h" |
bokan | e7a058a | 2017-03-02 22:42:51 | [diff] [blame] | 9 | #include "cc/trees/layer_tree_impl.h" |
Vladimir Levin | 4f331b42 | 2020-08-31 18:38:47 | [diff] [blame] | 10 | #include "components/viz/common/quads/compositor_render_pass.h" |
danakj | e5805be | 2017-09-15 19:24:55 | [diff] [blame] | 11 | #include "components/viz/common/quads/texture_draw_quad.h" |
bokan | e7a058a | 2017-03-02 22:42:51 | [diff] [blame] | 12 | #include "ui/gfx/geometry/rect_conversions.h" |
| 13 | #include "ui/gfx/geometry/rect_f.h" |
| 14 | |
| 15 | namespace cc { |
| 16 | |
| 17 | namespace { |
| 18 | |
| 19 | // Maximum number of patches that can be produced for one NinePatchLayer. |
| 20 | const int kMaxOcclusionPatches = 12; |
| 21 | const int kMaxPatches = 9; |
| 22 | |
| 23 | gfx::RectF BoundsToRect(int x1, int y1, int x2, int y2) { |
| 24 | return gfx::RectF(x1, y1, x2 - x1, y2 - y1); |
| 25 | } |
| 26 | |
| 27 | gfx::RectF NormalizedRect(const gfx::RectF& rect, |
| 28 | float total_width, |
| 29 | float total_height) { |
| 30 | return gfx::RectF(rect.x() / total_width, rect.y() / total_height, |
| 31 | rect.width() / total_width, rect.height() / total_height); |
| 32 | } |
| 33 | |
| 34 | } // namespace |
| 35 | |
| 36 | NinePatchGenerator::Patch::Patch(const gfx::RectF& image_rect, |
| 37 | const gfx::Size& total_image_bounds, |
| 38 | const gfx::RectF& output_rect) |
| 39 | : image_rect(image_rect), |
| 40 | normalized_image_rect(NormalizedRect(image_rect, |
| 41 | total_image_bounds.width(), |
| 42 | total_image_bounds.height())), |
| 43 | output_rect(output_rect) {} |
| 44 | |
| 45 | NinePatchGenerator::NinePatchGenerator() |
| 46 | : fill_center_(false), nearest_neighbor_(false) {} |
| 47 | |
| 48 | bool NinePatchGenerator::SetLayout(const gfx::Size& image_bounds, |
| 49 | const gfx::Size& output_bounds, |
| 50 | const gfx::Rect& aperture, |
| 51 | const gfx::Rect& border, |
| 52 | const gfx::Rect& output_occlusion, |
| 53 | bool fill_center, |
| 54 | bool nearest_neighbor) { |
| 55 | if (image_bounds_ == image_bounds && output_bounds_ == output_bounds && |
| 56 | image_aperture_ == aperture && border_ == border && |
| 57 | fill_center_ == fill_center && output_occlusion_ == output_occlusion && |
| 58 | nearest_neighbor_ == nearest_neighbor) |
| 59 | return false; |
| 60 | |
| 61 | image_bounds_ = image_bounds; |
| 62 | output_bounds_ = output_bounds; |
| 63 | image_aperture_ = aperture; |
| 64 | border_ = border; |
| 65 | fill_center_ = fill_center; |
| 66 | output_occlusion_ = output_occlusion; |
| 67 | nearest_neighbor_ = nearest_neighbor; |
| 68 | |
| 69 | return true; |
| 70 | } |
| 71 | |
| 72 | void NinePatchGenerator::CheckGeometryLimitations() { |
| 73 | // |border| is in layer space. It cannot exceed the bounds of the layer. |
| 74 | DCHECK_GE(output_bounds_.width(), border_.width()); |
| 75 | DCHECK_GE(output_bounds_.height(), border_.height()); |
| 76 | |
| 77 | // Sanity Check on |border| |
| 78 | DCHECK_LE(border_.x(), border_.width()); |
| 79 | DCHECK_LE(border_.y(), border_.height()); |
| 80 | DCHECK_GE(border_.x(), 0); |
| 81 | DCHECK_GE(border_.y(), 0); |
| 82 | |
| 83 | // |aperture| is in image space. It cannot exceed the bounds of the bitmap. |
| 84 | DCHECK(!image_aperture_.size().IsEmpty()); |
| 85 | DCHECK(gfx::Rect(image_bounds_).Contains(image_aperture_)) |
| 86 | << "image_bounds_ " << gfx::Rect(image_bounds_).ToString() |
| 87 | << " image_aperture_ " << image_aperture_.ToString(); |
| 88 | |
| 89 | // Sanity check on |output_occlusion_|. It should always be within the |
| 90 | // border. |
| 91 | gfx::Rect border_rect(border_.x(), border_.y(), |
| 92 | output_bounds_.width() - border_.width(), |
| 93 | output_bounds_.height() - border_.height()); |
| 94 | DCHECK(output_occlusion_.IsEmpty() || output_occlusion_.Contains(border_rect)) |
| 95 | << "border_rect " << border_rect.ToString() << " output_occlusion_ " |
| 96 | << output_occlusion_.ToString(); |
| 97 | } |
| 98 | |
| 99 | std::vector<NinePatchGenerator::Patch> |
| 100 | NinePatchGenerator::ComputeQuadsWithoutOcclusion() const { |
| 101 | float image_width = image_bounds_.width(); |
| 102 | float image_height = image_bounds_.height(); |
| 103 | float output_width = output_bounds_.width(); |
| 104 | float output_height = output_bounds_.height(); |
| 105 | gfx::RectF output_aperture(border_.x(), border_.y(), |
| 106 | output_width - border_.width(), |
| 107 | output_height - border_.height()); |
| 108 | |
| 109 | std::vector<Patch> patches; |
| 110 | patches.reserve(kMaxPatches); |
| 111 | |
| 112 | // Top-left. |
| 113 | patches.push_back( |
| 114 | Patch(BoundsToRect(0, 0, image_aperture_.x(), image_aperture_.y()), |
| 115 | image_bounds_, |
| 116 | BoundsToRect(0, 0, output_aperture.x(), output_aperture.y()))); |
| 117 | |
| 118 | // Top-right. |
| 119 | patches.push_back(Patch(BoundsToRect(image_aperture_.right(), 0, image_width, |
| 120 | image_aperture_.y()), |
| 121 | image_bounds_, |
| 122 | BoundsToRect(output_aperture.right(), 0, output_width, |
| 123 | output_aperture.y()))); |
| 124 | |
| 125 | // Bottom-left. |
| 126 | patches.push_back(Patch(BoundsToRect(0, image_aperture_.bottom(), |
| 127 | image_aperture_.x(), image_height), |
| 128 | image_bounds_, |
| 129 | BoundsToRect(0, output_aperture.bottom(), |
| 130 | output_aperture.x(), output_height))); |
| 131 | |
| 132 | // Bottom-right. |
| 133 | patches.push_back( |
| 134 | Patch(BoundsToRect(image_aperture_.right(), image_aperture_.bottom(), |
| 135 | image_width, image_height), |
| 136 | image_bounds_, |
| 137 | BoundsToRect(output_aperture.right(), output_aperture.bottom(), |
| 138 | output_width, output_height))); |
| 139 | |
| 140 | // Top. |
| 141 | patches.push_back( |
| 142 | Patch(BoundsToRect(image_aperture_.x(), 0, image_aperture_.right(), |
| 143 | image_aperture_.y()), |
| 144 | image_bounds_, |
| 145 | BoundsToRect(output_aperture.x(), 0, output_aperture.right(), |
| 146 | output_aperture.y()))); |
| 147 | |
| 148 | // Left. |
| 149 | patches.push_back( |
| 150 | Patch(BoundsToRect(0, image_aperture_.y(), image_aperture_.x(), |
| 151 | image_aperture_.bottom()), |
| 152 | image_bounds_, |
| 153 | BoundsToRect(0, output_aperture.y(), output_aperture.x(), |
| 154 | output_aperture.bottom()))); |
| 155 | |
| 156 | // Right. |
| 157 | patches.push_back( |
| 158 | Patch(BoundsToRect(image_aperture_.right(), image_aperture_.y(), |
| 159 | image_width, image_aperture_.bottom()), |
| 160 | image_bounds_, |
| 161 | BoundsToRect(output_aperture.right(), output_aperture.y(), |
| 162 | output_width, output_aperture.bottom()))); |
| 163 | |
| 164 | // Bottom. |
| 165 | patches.push_back( |
| 166 | Patch(BoundsToRect(image_aperture_.x(), image_aperture_.bottom(), |
| 167 | image_aperture_.right(), image_height), |
| 168 | image_bounds_, |
| 169 | BoundsToRect(output_aperture.x(), output_aperture.bottom(), |
| 170 | output_aperture.right(), output_height))); |
| 171 | |
| 172 | // Center. |
| 173 | if (fill_center_) { |
| 174 | patches.push_back( |
| 175 | Patch(BoundsToRect(image_aperture_.x(), image_aperture_.y(), |
| 176 | image_aperture_.right(), image_aperture_.bottom()), |
| 177 | image_bounds_, |
| 178 | BoundsToRect(output_aperture.x(), output_aperture.y(), |
| 179 | output_aperture.right(), output_aperture.bottom()))); |
| 180 | } |
| 181 | |
| 182 | return patches; |
| 183 | } |
| 184 | |
| 185 | std::vector<NinePatchGenerator::Patch> |
| 186 | NinePatchGenerator::ComputeQuadsWithOcclusion() const { |
| 187 | float image_width = image_bounds_.width(); |
| 188 | float image_height = image_bounds_.height(); |
| 189 | |
| 190 | float output_width = output_bounds_.width(); |
| 191 | float output_height = output_bounds_.height(); |
| 192 | |
| 193 | float layer_border_right = border_.width() - border_.x(); |
| 194 | float layer_border_bottom = border_.height() - border_.y(); |
| 195 | |
| 196 | float image_aperture_right = image_width - image_aperture_.right(); |
| 197 | float image_aperture_bottom = image_height - image_aperture_.bottom(); |
| 198 | |
| 199 | float output_occlusion_right = output_width - output_occlusion_.right(); |
| 200 | float output_occlusion_bottom = output_height - output_occlusion_.bottom(); |
| 201 | |
| 202 | gfx::RectF image_occlusion(BoundsToRect( |
| 203 | border_.x() == 0 |
| 204 | ? 0 |
| 205 | : (output_occlusion_.x() * image_aperture_.x() / border_.x()), |
| 206 | border_.y() == 0 |
| 207 | ? 0 |
| 208 | : (output_occlusion_.y() * image_aperture_.y() / border_.y()), |
| 209 | image_width - (layer_border_right == 0 |
| 210 | ? 0 |
| 211 | : output_occlusion_right * image_aperture_right / |
| 212 | layer_border_right), |
| 213 | image_height - (layer_border_bottom == 0 |
| 214 | ? 0 |
| 215 | : output_occlusion_bottom * image_aperture_bottom / |
| 216 | layer_border_bottom))); |
| 217 | gfx::RectF output_aperture(border_.x(), border_.y(), |
| 218 | output_width - border_.width(), |
| 219 | output_height - border_.height()); |
| 220 | |
| 221 | std::vector<Patch> patches; |
| 222 | patches.reserve(kMaxOcclusionPatches); |
| 223 | |
| 224 | // Top-left-left. |
| 225 | patches.push_back( |
| 226 | Patch(BoundsToRect(0, 0, image_occlusion.x(), image_aperture_.y()), |
| 227 | image_bounds_, |
| 228 | BoundsToRect(0, 0, output_occlusion_.x(), output_aperture.y()))); |
| 229 | |
| 230 | // Top-left-right. |
| 231 | patches.push_back( |
| 232 | Patch(BoundsToRect(image_occlusion.x(), 0, image_aperture_.x(), |
| 233 | image_occlusion.y()), |
| 234 | image_bounds_, |
| 235 | BoundsToRect(output_occlusion_.x(), 0, output_aperture.x(), |
| 236 | output_occlusion_.y()))); |
| 237 | |
| 238 | // Top-center. |
| 239 | patches.push_back( |
| 240 | Patch(BoundsToRect(image_aperture_.x(), 0, image_aperture_.right(), |
| 241 | image_occlusion.y()), |
| 242 | image_bounds_, |
| 243 | BoundsToRect(output_aperture.x(), 0, output_aperture.right(), |
| 244 | output_occlusion_.y()))); |
| 245 | |
| 246 | // Top-right-left. |
| 247 | patches.push_back( |
| 248 | Patch(BoundsToRect(image_aperture_.right(), 0, image_occlusion.right(), |
| 249 | image_occlusion.y()), |
| 250 | image_bounds_, |
| 251 | BoundsToRect(output_aperture.right(), 0, output_occlusion_.right(), |
| 252 | output_occlusion_.y()))); |
| 253 | |
| 254 | // Top-right-right. |
| 255 | patches.push_back(Patch(BoundsToRect(image_occlusion.right(), 0, image_width, |
| 256 | image_aperture_.y()), |
| 257 | image_bounds_, |
| 258 | BoundsToRect(output_occlusion_.right(), 0, |
| 259 | output_width, output_aperture.y()))); |
| 260 | |
| 261 | // Left-center. |
| 262 | patches.push_back( |
| 263 | Patch(BoundsToRect(0, image_aperture_.y(), image_occlusion.x(), |
| 264 | image_aperture_.bottom()), |
| 265 | image_bounds_, |
| 266 | BoundsToRect(0, output_aperture.y(), output_occlusion_.x(), |
| 267 | output_aperture.bottom()))); |
| 268 | |
| 269 | // Right-center. |
| 270 | patches.push_back( |
| 271 | Patch(BoundsToRect(image_occlusion.right(), image_aperture_.y(), |
| 272 | image_width, image_aperture_.bottom()), |
| 273 | image_bounds_, |
| 274 | BoundsToRect(output_occlusion_.right(), output_aperture.y(), |
| 275 | output_width, output_aperture.bottom()))); |
| 276 | |
| 277 | // Bottom-left-left. |
| 278 | patches.push_back(Patch(BoundsToRect(0, image_aperture_.bottom(), |
| 279 | image_occlusion.x(), image_height), |
| 280 | image_bounds_, |
| 281 | BoundsToRect(0, output_aperture.bottom(), |
| 282 | output_occlusion_.x(), output_height))); |
| 283 | |
| 284 | // Bottom-left-right. |
| 285 | patches.push_back( |
| 286 | Patch(BoundsToRect(image_occlusion.x(), image_occlusion.bottom(), |
| 287 | image_aperture_.x(), image_height), |
| 288 | image_bounds_, |
| 289 | BoundsToRect(output_occlusion_.x(), output_occlusion_.bottom(), |
| 290 | output_aperture.x(), output_height))); |
| 291 | |
| 292 | // Bottom-center. |
| 293 | patches.push_back( |
| 294 | Patch(BoundsToRect(image_aperture_.x(), image_occlusion.bottom(), |
| 295 | image_aperture_.right(), image_height), |
| 296 | image_bounds_, |
| 297 | BoundsToRect(output_aperture.x(), output_occlusion_.bottom(), |
| 298 | output_aperture.right(), output_height))); |
| 299 | |
| 300 | // Bottom-right-left. |
| 301 | patches.push_back( |
| 302 | Patch(BoundsToRect(image_aperture_.right(), image_occlusion.bottom(), |
| 303 | image_occlusion.right(), image_height), |
| 304 | image_bounds_, |
| 305 | BoundsToRect(output_aperture.right(), output_occlusion_.bottom(), |
| 306 | output_occlusion_.right(), output_height))); |
| 307 | |
| 308 | // Bottom-right-right. |
| 309 | patches.push_back( |
| 310 | Patch(BoundsToRect(image_occlusion.right(), image_aperture_.bottom(), |
| 311 | image_width, image_height), |
| 312 | image_bounds_, |
| 313 | BoundsToRect(output_occlusion_.right(), output_aperture.bottom(), |
| 314 | output_width, output_height))); |
| 315 | |
| 316 | return patches; |
| 317 | } |
| 318 | |
| 319 | std::vector<NinePatchGenerator::Patch> NinePatchGenerator::GeneratePatches() |
| 320 | const { |
| 321 | DCHECK(!output_bounds_.IsEmpty()); |
| 322 | |
| 323 | std::vector<Patch> patches; |
| 324 | |
| 325 | if (output_occlusion_.IsEmpty() || fill_center_) |
| 326 | patches = ComputeQuadsWithoutOcclusion(); |
| 327 | else |
| 328 | patches = ComputeQuadsWithOcclusion(); |
| 329 | |
| 330 | return patches; |
| 331 | } |
| 332 | |
| 333 | void NinePatchGenerator::AppendQuads(LayerImpl* layer_impl, |
| 334 | UIResourceId ui_resource_id, |
Vladimir Levin | 4f331b42 | 2020-08-31 18:38:47 | [diff] [blame] | 335 | viz::CompositorRenderPass* render_pass, |
Alex Zhang | abad229 | 2017-08-23 21:55:19 | [diff] [blame] | 336 | viz::SharedQuadState* shared_quad_state, |
bokan | e7a058a | 2017-03-02 22:42:51 | [diff] [blame] | 337 | const std::vector<Patch>& patches) { |
| 338 | if (!ui_resource_id) |
| 339 | return; |
| 340 | |
Fady Samuel | c80a4a86 | 2017-07-28 10:23:36 | [diff] [blame] | 341 | viz::ResourceId resource = |
bokan | e7a058a | 2017-03-02 22:42:51 | [diff] [blame] | 342 | layer_impl->layer_tree_impl()->ResourceIdForUIResource(ui_resource_id); |
| 343 | |
| 344 | if (!resource) |
| 345 | return; |
| 346 | |
| 347 | const float vertex_opacity[] = {1.0f, 1.0f, 1.0f, 1.0f}; |
| 348 | const bool opaque = |
| 349 | layer_impl->layer_tree_impl()->IsUIResourceOpaque(ui_resource_id); |
| 350 | constexpr bool flipped = false; |
| 351 | constexpr bool premultiplied_alpha = true; |
| 352 | |
| 353 | for (const auto& patch : patches) { |
| 354 | gfx::Rect output_rect = gfx::ToEnclosingRect(patch.output_rect); |
| 355 | gfx::Rect visible_rect = |
| 356 | layer_impl->draw_properties() |
| 357 | .occlusion_in_content_space.GetUnoccludedContentRect(output_rect); |
yiyix | 78ccdd9 | 2017-08-29 03:59:46 | [diff] [blame] | 358 | bool needs_blending = !opaque; |
bokan | e7a058a | 2017-03-02 22:42:51 | [diff] [blame] | 359 | if (!visible_rect.IsEmpty()) { |
| 360 | gfx::RectF image_rect = patch.normalized_image_rect; |
danakj | e5805be | 2017-09-15 19:24:55 | [diff] [blame] | 361 | auto* quad = render_pass->CreateAndAppendDrawQuad<viz::TextureDrawQuad>(); |
yiyix | 78ccdd9 | 2017-08-29 03:59:46 | [diff] [blame] | 362 | quad->SetNew(shared_quad_state, output_rect, visible_rect, needs_blending, |
| 363 | resource, premultiplied_alpha, image_rect.origin(), |
| 364 | image_rect.bottom_right(), SK_ColorTRANSPARENT, |
Daniel Nicoara | 3a0e00b8 | 2018-11-27 19:22:52 | [diff] [blame] | 365 | vertex_opacity, flipped, nearest_neighbor_, |
| 366 | /*secure_output_only=*/false, |
Maggie Chen | a507bbb1 | 2019-06-04 00:19:51 | [diff] [blame] | 367 | gfx::ProtectedVideoType::kClear); |
bokan | e7a058a | 2017-03-02 22:42:51 | [diff] [blame] | 368 | layer_impl->ValidateQuadResources(quad); |
| 369 | } |
| 370 | } |
| 371 | } |
| 372 | |
Xianzhu Wang | 61ace67 | 2020-02-08 02:32:42 | [diff] [blame] | 373 | void NinePatchGenerator::AsValueInto( |
| 374 | base::trace_event::TracedValue* state) const { |
| 375 | MathUtil::AddToTracedValue("ImageAperture", image_aperture_, state); |
| 376 | MathUtil::AddToTracedValue("ImageBounds", image_bounds_, state); |
| 377 | MathUtil::AddToTracedValue("Border", border_, state); |
| 378 | state->SetBoolean("FillCenter", fill_center_); |
| 379 | MathUtil::AddToTracedValue("OutputOcclusion", output_occlusion_, state); |
bokan | e7a058a | 2017-03-02 22:42:51 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | } // namespace cc |