Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 11 matching lines...) Expand all Loading... | |
| 22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 25 */ | 25 */ |
| 26 | 26 |
| 27 #include "config.h" | 27 #include "config.h" |
| 28 #include "platform/graphics/Image.h" | 28 #include "platform/graphics/Image.h" |
| 29 | 29 |
| 30 #include "platform/Length.h" | 30 #include "platform/Length.h" |
| 31 #include "platform/MIMETypeRegistry.h" | 31 #include "platform/MIMETypeRegistry.h" |
| 32 #include "platform/PlatformInstrumentation.h" | |
| 33 #include "platform/RuntimeEnabledFeatures.h" | |
| 32 #include "platform/SharedBuffer.h" | 34 #include "platform/SharedBuffer.h" |
| 33 #include "platform/TraceEvent.h" | 35 #include "platform/TraceEvent.h" |
| 34 #include "platform/geometry/FloatPoint.h" | 36 #include "platform/geometry/FloatPoint.h" |
| 35 #include "platform/geometry/FloatRect.h" | 37 #include "platform/geometry/FloatRect.h" |
| 36 #include "platform/geometry/FloatSize.h" | 38 #include "platform/geometry/FloatSize.h" |
| 37 #include "platform/graphics/BitmapImage.h" | 39 #include "platform/graphics/BitmapImage.h" |
| 40 #include "platform/graphics/DeferredImageDecoder.h" | |
| 38 #include "platform/graphics/GraphicsContext.h" | 41 #include "platform/graphics/GraphicsContext.h" |
| 39 #include "platform/graphics/GraphicsContextStateSaver.h" | 42 #include "platform/graphics/GraphicsContextStateSaver.h" |
| 40 #include "public/platform/Platform.h" | 43 #include "public/platform/Platform.h" |
| 41 #include "public/platform/WebData.h" | 44 #include "public/platform/WebData.h" |
| 45 #include "third_party/skia/include/core/SkCanvas.h" | |
| 42 #include "third_party/skia/include/core/SkImage.h" | 46 #include "third_party/skia/include/core/SkImage.h" |
| 43 #include "wtf/MainThread.h" | 47 #include "wtf/MainThread.h" |
| 44 #include "wtf/StdLibExtras.h" | 48 #include "wtf/StdLibExtras.h" |
| 45 | 49 |
| 46 #include <math.h> | 50 #include <math.h> |
| 47 | 51 |
| 48 namespace blink { | 52 namespace blink { |
| 49 | 53 |
| 50 Image::Image(ImageObserver* observer) | 54 Image::Image(ImageObserver* observer) |
| 51 : m_imageObserver(observer) | 55 : m_imageObserver(observer) |
| (...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 215 drawPattern(ctxt, srcRect, tileScaleFactor, patternPhase, op, dstRect); | 219 drawPattern(ctxt, srcRect, tileScaleFactor, patternPhase, op, dstRect); |
| 216 } | 220 } |
| 217 | 221 |
| 218 startAnimation(); | 222 startAnimation(); |
| 219 } | 223 } |
| 220 | 224 |
| 221 void Image::drawPattern(GraphicsContext* context, const FloatRect& floatSrcRect, const FloatSize& scale, | 225 void Image::drawPattern(GraphicsContext* context, const FloatRect& floatSrcRect, const FloatSize& scale, |
| 222 const FloatPoint& phase, SkXfermode::Mode op, const FloatRect& destRect, con st IntSize& repeatSpacing) | 226 const FloatPoint& phase, SkXfermode::Mode op, const FloatRect& destRect, con st IntSize& repeatSpacing) |
| 223 { | 227 { |
| 224 TRACE_EVENT0("skia", "Image::drawPattern"); | 228 TRACE_EVENT0("skia", "Image::drawPattern"); |
| 225 if (RefPtr<NativeImageSkia> bitmap = nativeImageForCurrentFrame()) | 229 SkBitmap bitmap = bitmapForCurrentFrame(); |
| 226 bitmap->drawPattern(context, adjustForNegativeSize(floatSrcRect), scale, phase, op, destRect, repeatSpacing); | 230 if (!bitmap.isNull()) { |
| 231 drawBitmapPattern(context, bitmap, adjustForNegativeSize(floatSrcRect), scale, phase, op, destRect, repeatSpacing); | |
|
f(malita)
2015/03/15 00:32:45
I think we could inline drawBitmapPattern here - I
Stephen White
2015/03/15 16:49:31
Done. Also inlined drawBitmap() into BitmapImage,
| |
| 232 } | |
| 233 } | |
| 234 | |
| 235 void Image::drawBitmap( | |
| 236 GraphicsContext* context, | |
| 237 const SkBitmap& bitmap, | |
| 238 const SkRect& srcRect, | |
| 239 const SkRect& destRect, | |
| 240 SkXfermode::Mode op) | |
| 241 { | |
| 242 TRACE_EVENT0("skia", "Image::drawBitmap"); | |
| 243 | |
| 244 bool isLazyDecoded = DeferredImageDecoder::isLazyDecoded(bitmap); | |
| 245 bool isOpaque = bitmap.isOpaque(); | |
| 246 | |
| 247 { | |
| 248 SkPaint paint; | |
| 249 int initialSaveCount = context->preparePaintForDrawRectToRect(&paint, sr cRect, destRect, op, !isOpaque, isLazyDecoded, bitmap.isImmutable()); | |
| 250 // We want to filter it if we decided to do interpolation above, or if | |
| 251 // there is something interesting going on with the matrix (like a rotat ion). | |
| 252 // Note: for serialization, we will want to subset the bitmap first so w e | |
| 253 // don't send extra pixels. | |
| 254 context->drawBitmapRect(bitmap, &srcRect, destRect, &paint); | |
| 255 context->canvas()->restoreToCount(initialSaveCount); | |
| 256 } | |
| 257 | |
| 258 if (isLazyDecoded) | |
| 259 PlatformInstrumentation::didDrawLazyPixelRef(bitmap.getGenerationID()); | |
| 260 } | |
| 261 | |
| 262 static SkBitmap createBitmapWithSpace(const SkBitmap& bitmap, int spaceWidth, in t spaceHeight) | |
| 263 { | |
| 264 SkImageInfo info = bitmap.info(); | |
| 265 info = SkImageInfo::Make(info.width() + spaceWidth, info.height() + spaceHei ght, info.colorType(), kPremul_SkAlphaType); | |
| 266 | |
| 267 SkBitmap result; | |
| 268 result.allocPixels(info); | |
| 269 result.eraseColor(SK_ColorTRANSPARENT); | |
| 270 bitmap.copyPixelsTo(reinterpret_cast<uint8_t*>(result.getPixels()), result.r owBytes() * result.height(), result.rowBytes()); | |
| 271 | |
| 272 return result; | |
| 273 } | |
| 274 | |
| 275 void Image::drawBitmapPattern( | |
| 276 GraphicsContext* context, | |
| 277 const SkBitmap& bitmap, | |
| 278 const FloatRect& floatSrcRect, | |
| 279 const FloatSize& scale, | |
| 280 const FloatPoint& phase, | |
| 281 SkXfermode::Mode compositeOp, | |
| 282 const FloatRect& destRect, | |
| 283 const IntSize& repeatSpacing) | |
| 284 { | |
| 285 FloatRect normSrcRect = floatSrcRect; | |
| 286 normSrcRect.intersect(FloatRect(0, 0, bitmap.width(), bitmap.height())); | |
| 287 if (destRect.isEmpty() || normSrcRect.isEmpty()) | |
| 288 return; // nothing to draw | |
| 289 | |
| 290 SkMatrix localMatrix; | |
| 291 // We also need to translate it such that the origin of the pattern is the | |
| 292 // origin of the destination rect, which is what WebKit expects. Skia uses | |
| 293 // the coordinate system origin as the base for the pattern. If WebKit wants | |
| 294 // a shifted image, it will shift it from there using the localMatrix. | |
| 295 const float adjustedX = phase.x() + normSrcRect.x() * scale.width(); | |
| 296 const float adjustedY = phase.y() + normSrcRect.y() * scale.height(); | |
| 297 localMatrix.setTranslate(SkFloatToScalar(adjustedX), SkFloatToScalar(adjuste dY)); | |
| 298 | |
| 299 // Because no resizing occurred, the shader transform should be | |
| 300 // set to the pattern's transform, which just includes scale. | |
| 301 localMatrix.preScale(scale.width(), scale.height()); | |
| 302 | |
| 303 SkBitmap bitmapToPaint; | |
| 304 bitmap.extractSubset(&bitmapToPaint, enclosingIntRect(normSrcRect)); | |
| 305 if (!repeatSpacing.isZero()) { | |
| 306 SkScalar ctmScaleX = 1.0; | |
| 307 SkScalar ctmScaleY = 1.0; | |
| 308 | |
| 309 if (!RuntimeEnabledFeatures::slimmingPaintEnabled()) { | |
| 310 AffineTransform ctm = context->getCTM(); | |
| 311 ctmScaleX = ctm.xScale(); | |
| 312 ctmScaleY = ctm.yScale(); | |
| 313 } | |
| 314 | |
| 315 bitmapToPaint = createBitmapWithSpace( | |
| 316 bitmapToPaint, | |
| 317 repeatSpacing.width() * ctmScaleX / scale.width(), | |
| 318 repeatSpacing.height() * ctmScaleY / scale.height()); | |
| 319 } | |
| 320 RefPtr<SkShader> shader = adoptRef(SkShader::CreateBitmapShader(bitmapToPain t, SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode, &localMatrix)); | |
| 321 | |
| 322 bool isLazyDecoded = DeferredImageDecoder::isLazyDecoded(bitmap); | |
| 323 { | |
| 324 SkPaint paint; | |
| 325 int initialSaveCount = context->preparePaintForDrawRectToRect(&paint, fl oatSrcRect, | |
| 326 destRect, compositeOp, !bitmap.isOpaque(), isLazyDecoded, bitmap.isI mmutable()); | |
| 327 paint.setShader(shader.get()); | |
| 328 context->drawRect(destRect, paint); | |
| 329 context->canvas()->restoreToCount(initialSaveCount); | |
| 330 } | |
| 331 | |
| 332 if (isLazyDecoded) | |
| 333 PlatformInstrumentation::didDrawLazyPixelRef(bitmap.getGenerationID()); | |
| 227 } | 334 } |
| 228 | 335 |
| 229 void Image::computeIntrinsicDimensions(Length& intrinsicWidth, Length& intrinsic Height, FloatSize& intrinsicRatio) | 336 void Image::computeIntrinsicDimensions(Length& intrinsicWidth, Length& intrinsic Height, FloatSize& intrinsicRatio) |
| 230 { | 337 { |
| 231 intrinsicRatio = size(); | 338 intrinsicRatio = size(); |
| 232 intrinsicWidth = Length(intrinsicRatio.width(), Fixed); | 339 intrinsicWidth = Length(intrinsicRatio.width(), Fixed); |
| 233 intrinsicHeight = Length(intrinsicRatio.height(), Fixed); | 340 intrinsicHeight = Length(intrinsicRatio.height(), Fixed); |
| 234 } | 341 } |
| 235 | 342 |
| 236 PassRefPtr<Image> Image::imageForDefaultFrame() | 343 PassRefPtr<Image> Image::imageForDefaultFrame() |
| 237 { | 344 { |
| 238 RefPtr<Image> image(this); | 345 RefPtr<Image> image(this); |
| 239 | 346 |
| 240 return image.release(); | 347 return image.release(); |
| 241 } | 348 } |
| 242 | 349 |
| 243 PassRefPtr<SkImage> Image::skImage() | 350 PassRefPtr<SkImage> Image::skImage() |
| 244 { | 351 { |
| 245 return nullptr; | 352 return nullptr; |
| 246 } | 353 } |
| 247 | 354 |
| 248 } // namespace blink | 355 } // namespace blink |
| OLD | NEW |