Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(308)

Side by Side Diff: third_party/WebKit/Source/platform/graphics/Image.cpp

Issue 2582383002: Clamp background tiles when possible (Closed)
Patch Set: relax asserts Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « third_party/WebKit/Source/platform/graphics/Image.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2006 Samuel Weinig (sam.weinig@gmail.com) 2 * Copyright (C) 2006 Samuel Weinig (sam.weinig@gmail.com)
3 * Copyright (C) 2004, 2005, 2006 Apple Computer, Inc. All rights reserved. 3 * Copyright (C) 2004, 2005, 2006 Apple Computer, Inc. All rights reserved.
4 * 4 *
5 * Redistribution and use in source and binary forms, with or without 5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions 6 * modification, are permitted provided that the following conditions
7 * are met: 7 * are met:
8 * 1. Redistributions of source code must retain the above copyright 8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright 10 * 2. Redistributions in binary form must reproduce the above copyright
(...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after
218 } 218 }
219 219
220 startAnimation(); 220 startAnimation();
221 } 221 }
222 222
223 namespace { 223 namespace {
224 224
225 sk_sp<SkShader> createPatternShader(const SkImage* image, 225 sk_sp<SkShader> createPatternShader(const SkImage* image,
226 const SkMatrix& shaderMatrix, 226 const SkMatrix& shaderMatrix,
227 const SkPaint& paint, 227 const SkPaint& paint,
228 const FloatSize& spacing) { 228 const FloatSize& spacing,
229 SkShader::TileMode tmx,
230 SkShader::TileMode tmy) {
229 if (spacing.isZero()) 231 if (spacing.isZero())
230 return image->makeShader(SkShader::kRepeat_TileMode, 232 return image->makeShader(tmx, tmy, &shaderMatrix);
231 SkShader::kRepeat_TileMode, &shaderMatrix);
232 233
233 // Arbitrary tiling is currently only supported for SkPictureShader, so we use 234 // Arbitrary tiling is currently only supported for SkPictureShader, so we use
234 // that instead of a plain bitmap shader to implement spacing. 235 // that instead of a plain bitmap shader to implement spacing.
235 const SkRect tileRect = SkRect::MakeWH(image->width() + spacing.width(), 236 const SkRect tileRect = SkRect::MakeWH(image->width() + spacing.width(),
236 image->height() + spacing.height()); 237 image->height() + spacing.height());
237 238
238 SkPictureRecorder recorder; 239 SkPictureRecorder recorder;
239 SkCanvas* canvas = recorder.beginRecording(tileRect); 240 SkCanvas* canvas = recorder.beginRecording(tileRect);
240 canvas->drawImage(image, 0, 0, &paint); 241 canvas->drawImage(image, 0, 0, &paint);
241 242
242 return SkShader::MakePictureShader( 243 return SkShader::MakePictureShader(recorder.finishRecordingAsPicture(), tmx,
243 recorder.finishRecordingAsPicture(), SkShader::kRepeat_TileMode, 244 tmy, &shaderMatrix, nullptr);
244 SkShader::kRepeat_TileMode, &shaderMatrix, nullptr); 245 }
246
247 SkShader::TileMode computeTileMode(float left,
248 float right,
249 float min,
250 float max) {
251 DCHECK(left < right);
252 return left >= min && right <= max ? SkShader::kClamp_TileMode
253 : SkShader::kRepeat_TileMode;
245 } 254 }
246 255
247 } // anonymous namespace 256 } // anonymous namespace
248 257
249 void Image::drawPattern(GraphicsContext& context, 258 void Image::drawPattern(GraphicsContext& context,
250 const FloatRect& floatSrcRect, 259 const FloatRect& floatSrcRect,
251 const FloatSize& scale, 260 const FloatSize& scale,
252 const FloatPoint& phase, 261 const FloatPoint& phase,
253 SkBlendMode compositeOp, 262 SkBlendMode compositeOp,
254 const FloatRect& destRect, 263 const FloatRect& destRect,
(...skipping 24 matching lines...) Expand all
279 // set to the pattern's transform, which just includes scale. 288 // set to the pattern's transform, which just includes scale.
280 localMatrix.preScale(scale.width(), scale.height()); 289 localMatrix.preScale(scale.width(), scale.height());
281 290
282 // Fetch this now as subsetting may swap the image. 291 // Fetch this now as subsetting may swap the image.
283 auto imageID = image->uniqueID(); 292 auto imageID = image->uniqueID();
284 293
285 image = image->makeSubset(enclosingIntRect(normSrcRect)); 294 image = image->makeSubset(enclosingIntRect(normSrcRect));
286 if (!image) 295 if (!image)
287 return; 296 return;
288 297
298 const FloatSize tileSize(
299 image->width() * scale.width() + repeatSpacing.width(),
300 image->height() * scale.height() + repeatSpacing.height());
301 const auto tmx = computeTileMode(destRect.x(), destRect.maxX(), adjustedX,
302 adjustedX + tileSize.width());
303 const auto tmy = computeTileMode(destRect.y(), destRect.maxY(), adjustedY,
304 adjustedY + tileSize.height());
305
289 { 306 {
290 SkPaint paint = context.fillPaint(); 307 SkPaint paint = context.fillPaint();
291 paint.setColor(SK_ColorBLACK); 308 paint.setColor(SK_ColorBLACK);
292 paint.setBlendMode(compositeOp); 309 paint.setBlendMode(compositeOp);
293 paint.setFilterQuality( 310 paint.setFilterQuality(
294 context.computeFilterQuality(this, destRect, normSrcRect)); 311 context.computeFilterQuality(this, destRect, normSrcRect));
295 paint.setAntiAlias(context.shouldAntialias()); 312 paint.setAntiAlias(context.shouldAntialias());
296 paint.setShader(createPatternShader( 313 paint.setShader(
297 image.get(), localMatrix, paint, 314 createPatternShader(image.get(), localMatrix, paint,
298 FloatSize(repeatSpacing.width() / scale.width(), 315 FloatSize(repeatSpacing.width() / scale.width(),
299 repeatSpacing.height() / scale.height()))); 316 repeatSpacing.height() / scale.height()),
317 tmx, tmy));
300 context.drawRect(destRect, paint); 318 context.drawRect(destRect, paint);
301 } 319 }
302 320
303 if (currentFrameIsLazyDecoded()) 321 if (currentFrameIsLazyDecoded())
304 PlatformInstrumentation::didDrawLazyPixelRef(imageID); 322 PlatformInstrumentation::didDrawLazyPixelRef(imageID);
305 } 323 }
306 324
307 PassRefPtr<Image> Image::imageForDefaultFrame() { 325 PassRefPtr<Image> Image::imageForDefaultFrame() {
308 RefPtr<Image> image(this); 326 RefPtr<Image> image(this);
309 327
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
364 FloatRect subset = dest; 382 FloatRect subset = dest;
365 subset.setX((dest.x() - tile.x()) / scale.width()); 383 subset.setX((dest.x() - tile.x()) / scale.width());
366 subset.setY((dest.y() - tile.y()) / scale.height()); 384 subset.setY((dest.y() - tile.y()) / scale.height());
367 subset.setWidth(dest.width() / scale.width()); 385 subset.setWidth(dest.width() / scale.width());
368 subset.setHeight(dest.height() / scale.height()); 386 subset.setHeight(dest.height() / scale.height());
369 387
370 return subset; 388 return subset;
371 } 389 }
372 390
373 } // namespace blink 391 } // namespace blink
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/platform/graphics/Image.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698