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