OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2006 Eric Seidel <eric@webkit.org> | 2 * Copyright (C) 2006 Eric Seidel <eric@webkit.org> |
3 * Copyright (C) 2008, 2009 Apple Inc. All rights reserved. | 3 * Copyright (C) 2008, 2009 Apple Inc. All rights reserved. |
4 * Copyright (C) Research In Motion Limited 2011. All rights reserved. | 4 * Copyright (C) Research In Motion Limited 2011. All rights reserved. |
5 * | 5 * |
6 * Redistribution and use in source and binary forms, with or without | 6 * Redistribution and use in source and binary forms, with or without |
7 * modification, are permitted provided that the following conditions | 7 * modification, are permitted provided that the following conditions |
8 * are met: | 8 * are met: |
9 * 1. Redistributions of source code must retain the above copyright | 9 * 1. Redistributions of source code must retain the above copyright |
10 * notice, this list of conditions and the following disclaimer. | 10 * notice, this list of conditions and the following disclaimer. |
(...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
187 if (intrinsicSize.isEmpty()) | 187 if (intrinsicSize.isEmpty()) |
188 intrinsicSize = rootElement->currentViewBoxRect().size(); | 188 intrinsicSize = rootElement->currentViewBoxRect().size(); |
189 | 189 |
190 if (!intrinsicSize.isEmpty()) | 190 if (!intrinsicSize.isEmpty()) |
191 return expandedIntSize(intrinsicSize); | 191 return expandedIntSize(intrinsicSize); |
192 | 192 |
193 // As last resort, use CSS replaced element fallback size. | 193 // As last resort, use CSS replaced element fallback size. |
194 return IntSize(300, 150); | 194 return IntSize(300, 150); |
195 } | 195 } |
196 | 196 |
197 void SVGImage::drawForContainer(GraphicsContext* context, const FloatSize contai
nerSize, float zoom, const FloatRect& dstRect, | 197 void SVGImage::drawForContainer(SkCanvas* canvas, const SkPaint& paint, const Fl
oatSize containerSize, float zoom, const FloatRect& dstRect, |
198 const FloatRect& srcRect, SkXfermode::Mode compositeOp) | 198 const FloatRect& srcRect) |
199 { | 199 { |
200 if (!m_page) | 200 if (!m_page) |
201 return; | 201 return; |
202 | 202 |
203 // Temporarily disable the image observer to prevent changeInRect() calls du
e re-laying out the image. | 203 // Temporarily disable the image observer to prevent changeInRect() calls du
e re-laying out the image. |
204 ImageObserverDisabler imageObserverDisabler(this); | 204 ImageObserverDisabler imageObserverDisabler(this); |
205 | 205 |
206 IntSize roundedContainerSize = roundedIntSize(containerSize); | 206 IntSize roundedContainerSize = roundedIntSize(containerSize); |
207 setContainerSize(roundedContainerSize); | 207 setContainerSize(roundedContainerSize); |
208 | 208 |
209 FloatRect scaledSrc = srcRect; | 209 FloatRect scaledSrc = srcRect; |
210 scaledSrc.scale(1 / zoom); | 210 scaledSrc.scale(1 / zoom); |
211 | 211 |
212 // Compensate for the container size rounding by adjusting the source rect. | 212 // Compensate for the container size rounding by adjusting the source rect. |
213 FloatSize adjustedSrcSize = scaledSrc.size(); | 213 FloatSize adjustedSrcSize = scaledSrc.size(); |
214 adjustedSrcSize.scale(roundedContainerSize.width() / containerSize.width(),
roundedContainerSize.height() / containerSize.height()); | 214 adjustedSrcSize.scale(roundedContainerSize.width() / containerSize.width(),
roundedContainerSize.height() / containerSize.height()); |
215 scaledSrc.setSize(adjustedSrcSize); | 215 scaledSrc.setSize(adjustedSrcSize); |
216 | 216 |
217 draw(context, dstRect, scaledSrc, compositeOp, DoNotRespectImageOrientation)
; | 217 draw(canvas, paint, dstRect, scaledSrc, DoNotRespectImageOrientation, nullpt
r); |
218 } | 218 } |
219 | 219 |
220 bool SVGImage::bitmapForCurrentFrame(SkBitmap* bitmap) | 220 bool SVGImage::bitmapForCurrentFrame(SkBitmap* bitmap) |
221 { | 221 { |
222 if (!m_page) | 222 if (!m_page) |
223 return false; | 223 return false; |
224 | 224 |
225 OwnPtr<ImageBuffer> buffer = ImageBuffer::create(size()); | 225 OwnPtr<ImageBuffer> buffer = ImageBuffer::create(size()); |
226 if (!buffer) | 226 if (!buffer) |
227 return false; | 227 return false; |
228 | 228 |
229 drawForContainer(buffer->context(), size(), 1, rect(), rect(), SkXfermode::k
SrcOver_Mode); | 229 SkPaint paint; |
| 230 drawForContainer(buffer->canvas(), paint, size(), 1, rect(), rect()); |
230 | 231 |
231 *bitmap = buffer->bitmap(); | 232 *bitmap = buffer->bitmap(); |
232 return true; | 233 return true; |
233 } | 234 } |
234 | 235 |
235 void SVGImage::drawPatternForContainer(GraphicsContext* context, const FloatSize
containerSize, | 236 void SVGImage::drawPatternForContainer(GraphicsContext* context, const FloatSize
containerSize, |
236 float zoom, const FloatRect& srcRect, const FloatSize& tileScale, const Floa
tPoint& phase, | 237 float zoom, const FloatRect& srcRect, const FloatSize& tileScale, const Floa
tPoint& phase, |
237 SkXfermode::Mode compositeOp, const FloatRect& dstRect, | 238 SkXfermode::Mode compositeOp, const FloatRect& dstRect, |
238 const IntSize& repeatSpacing) | 239 const IntSize& repeatSpacing) |
239 { | 240 { |
240 // Tile adjusted for scaling/stretch. | 241 // Tile adjusted for scaling/stretch. |
241 FloatRect tile(srcRect); | 242 FloatRect tile(srcRect); |
242 tile.scale(tileScale.width(), tileScale.height()); | 243 tile.scale(tileScale.width(), tileScale.height()); |
243 | 244 |
244 // Expand the tile to account for repeat spacing. | 245 // Expand the tile to account for repeat spacing. |
245 FloatRect spacedTile(tile); | 246 FloatRect spacedTile(tile); |
246 spacedTile.expand(repeatSpacing); | 247 spacedTile.expand(repeatSpacing); |
247 | 248 |
248 SkPictureBuilder patternPicture(spacedTile); | 249 SkPictureBuilder patternPicture(spacedTile); |
249 { | 250 { |
250 DrawingRecorder patternPictureRecorder(patternPicture.context(), *this,
DisplayItem::Type::SVGImage, spacedTile); | 251 DrawingRecorder patternPictureRecorder(patternPicture.context(), *this,
DisplayItem::Type::SVGImage, spacedTile); |
251 if (!patternPictureRecorder.canUseCachedDrawing()) { | 252 if (!patternPictureRecorder.canUseCachedDrawing()) { |
252 // When generating an expanded tile, make sure we don't draw into th
e spacing area. | 253 // When generating an expanded tile, make sure we don't draw into th
e spacing area. |
253 if (tile != spacedTile) | 254 if (tile != spacedTile) |
254 patternPicture.context().clip(tile); | 255 patternPicture.context().clip(tile); |
255 drawForContainer(&patternPicture.context(), containerSize, zoom, til
e, srcRect, SkXfermode::kSrcOver_Mode); | 256 SkPaint paint; |
| 257 drawForContainer(patternPicture.context().canvas(), paint, container
Size, zoom, tile, srcRect); |
256 } | 258 } |
257 } | 259 } |
258 RefPtr<const SkPicture> tilePicture = patternPicture.endRecording(); | 260 RefPtr<const SkPicture> tilePicture = patternPicture.endRecording(); |
259 | 261 |
260 SkMatrix patternTransform; | 262 SkMatrix patternTransform; |
261 patternTransform.setTranslate(phase.x() + spacedTile.x(), phase.y() + spaced
Tile.y()); | 263 patternTransform.setTranslate(phase.x() + spacedTile.x(), phase.y() + spaced
Tile.y()); |
262 RefPtr<SkShader> patternShader = adoptRef(SkShader::CreatePictureShader( | 264 RefPtr<SkShader> patternShader = adoptRef(SkShader::CreatePictureShader( |
263 tilePicture.get(), SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMod
e, | 265 tilePicture.get(), SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMod
e, |
264 &patternTransform, nullptr)); | 266 &patternTransform, nullptr)); |
265 | 267 |
266 SkPaint paint; | 268 SkPaint paint; |
267 paint.setShader(patternShader.get()); | 269 paint.setShader(patternShader.get()); |
268 paint.setXfermodeMode(compositeOp); | 270 paint.setXfermodeMode(compositeOp); |
269 paint.setColorFilter(context->colorFilter()); | 271 paint.setColorFilter(context->colorFilter()); |
270 context->drawRect(dstRect, paint); | 272 context->drawRect(dstRect, paint); |
271 } | 273 } |
272 | 274 |
273 void SVGImage::draw(GraphicsContext* context, const FloatRect& dstRect, const Fl
oatRect& srcRect, SkXfermode::Mode compositeOp, RespectImageOrientationEnum) | 275 static bool drawNeedsLayer(const SkPaint& paint) |
| 276 { |
| 277 if (SkColorGetA(paint.getColor()) < 255) |
| 278 return true; |
| 279 |
| 280 SkXfermode::Mode xfermode; |
| 281 if (SkXfermode::AsMode(paint.getXfermode(), &xfermode)) { |
| 282 if (xfermode != SkXfermode::kSrcOver_Mode) |
| 283 return true; |
| 284 } |
| 285 |
| 286 return false; |
| 287 } |
| 288 |
| 289 void SVGImage::draw(SkCanvas* canvas, const SkPaint& paint, const FloatRect& dst
Rect, const FloatRect& srcRect, RespectImageOrientationEnum, const ImageFilterQu
alityHelper*) |
274 { | 290 { |
275 if (!m_page) | 291 if (!m_page) |
276 return; | 292 return; |
277 | 293 |
278 float opacity = context->getNormalizedAlpha() / 255.f; | |
279 | |
280 FrameView* view = frameView(); | 294 FrameView* view = frameView(); |
281 view->resize(containerSize()); | 295 view->resize(containerSize()); |
282 | 296 |
283 // Always call scrollToFragment, even if the url is empty, because | 297 // Always call scrollToFragment, even if the url is empty, because |
284 // there may have been a previous url/fragment that needs to be reset. | 298 // there may have been a previous url/fragment that needs to be reset. |
285 view->scrollToFragment(m_url); | 299 view->scrollToFragment(m_url); |
286 | 300 |
| 301 SkPictureBuilder imagePicture(dstRect); |
287 { | 302 { |
288 DisplayItemListContextRecorder contextRecorder(*context); | 303 ClipRecorder clipRecorder(imagePicture.context(), *this, DisplayItem::Cl
ipNodeImage, LayoutRect(enclosingIntRect(dstRect))); |
289 GraphicsContext& paintContext = contextRecorder.context(); | |
290 | |
291 ClipRecorder clipRecorder(paintContext, *this, DisplayItem::ClipNodeImag
e, LayoutRect(enclosingIntRect(dstRect))); | |
292 | |
293 bool hasCompositing = compositeOp != SkXfermode::kSrcOver_Mode; | |
294 OwnPtr<CompositingRecorder> compositingRecorder; | |
295 if (hasCompositing || opacity < 1) | |
296 compositingRecorder = adoptPtr(new CompositingRecorder(paintContext,
*this, compositeOp, opacity)); | |
297 | 304 |
298 // We can only draw the entire frame, clipped to the rect we want. So co
mpute where the top left | 305 // We can only draw the entire frame, clipped to the rect we want. So co
mpute where the top left |
299 // of the image would be if we were drawing without clipping, and transl
ate accordingly. | 306 // of the image would be if we were drawing without clipping, and transl
ate accordingly. |
300 FloatSize scale(dstRect.width() / srcRect.width(), dstRect.height() / sr
cRect.height()); | 307 FloatSize scale(dstRect.width() / srcRect.width(), dstRect.height() / sr
cRect.height()); |
301 FloatSize topLeftOffset(srcRect.location().x() * scale.width(), srcRect.
location().y() * scale.height()); | 308 FloatSize topLeftOffset(srcRect.location().x() * scale.width(), srcRect.
location().y() * scale.height()); |
302 FloatPoint destOffset = dstRect.location() - topLeftOffset; | 309 FloatPoint destOffset = dstRect.location() - topLeftOffset; |
303 AffineTransform transform = AffineTransform::translation(destOffset.x(),
destOffset.y()); | 310 AffineTransform transform = AffineTransform::translation(destOffset.x(),
destOffset.y()); |
304 transform.scale(scale.width(), scale.height()); | 311 transform.scale(scale.width(), scale.height()); |
305 TransformRecorder transformRecorder(paintContext, *this, transform); | 312 TransformRecorder transformRecorder(imagePicture.context(), *this, trans
form); |
306 | 313 |
307 view->updateLayoutAndStyleForPainting(); | 314 view->updateLayoutAndStyleForPainting(); |
308 view->paint(&paintContext, enclosingIntRect(srcRect)); | 315 view->paint(&imagePicture.context(), enclosingIntRect(srcRect)); |
309 ASSERT(!view->needsLayout()); | 316 ASSERT(!view->needsLayout()); |
310 } | 317 } |
311 | 318 |
| 319 { |
| 320 SkAutoCanvasRestore ar(canvas, false); |
| 321 if (drawNeedsLayer(paint)) { |
| 322 SkRect layerRect = dstRect; |
| 323 canvas->saveLayer(&layerRect, &paint); |
| 324 } |
| 325 RefPtr<const SkPicture> recording = imagePicture.endRecording(); |
| 326 canvas->drawPicture(recording.get()); |
| 327 } |
| 328 |
312 if (imageObserver()) | 329 if (imageObserver()) |
313 imageObserver()->didDraw(this); | 330 imageObserver()->didDraw(this); |
314 | 331 |
315 // Start any (SMIL) animations if needed. This will restart or continue | 332 // Start any (SMIL) animations if needed. This will restart or continue |
316 // animations if preceded by calls to resetAnimation or stopAnimation | 333 // animations if preceded by calls to resetAnimation or stopAnimation |
317 // respectively. | 334 // respectively. |
318 startAnimation(); | 335 startAnimation(); |
319 } | 336 } |
320 | 337 |
321 LayoutBox* SVGImage::embeddedContentBox() const | 338 LayoutBox* SVGImage::embeddedContentBox() const |
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
456 | 473 |
457 return m_page; | 474 return m_page; |
458 } | 475 } |
459 | 476 |
460 String SVGImage::filenameExtension() const | 477 String SVGImage::filenameExtension() const |
461 { | 478 { |
462 return "svg"; | 479 return "svg"; |
463 } | 480 } |
464 | 481 |
465 } | 482 } |
OLD | NEW |