| OLD | NEW |
| 1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "cc/software_renderer.h" | 5 #include "cc/software_renderer.h" |
| 6 | 6 |
| 7 #include "cc/debug_border_draw_quad.h" | 7 #include "cc/debug_border_draw_quad.h" |
| 8 #include "cc/render_pass_draw_quad.h" | 8 #include "cc/render_pass_draw_quad.h" |
| 9 #include "cc/solid_color_draw_quad.h" | 9 #include "cc/solid_color_draw_quad.h" |
| 10 #include "cc/texture_draw_quad.h" | 10 #include "cc/texture_draw_quad.h" |
| 11 #include "cc/tile_draw_quad.h" | 11 #include "cc/tile_draw_quad.h" |
| 12 #include "third_party/skia/include/core/SkCanvas.h" | 12 #include "third_party/skia/include/core/SkCanvas.h" |
| 13 #include "third_party/skia/include/core/SkColor.h" | 13 #include "third_party/skia/include/core/SkColor.h" |
| 14 #include "third_party/skia/include/core/SkMatrix.h" | 14 #include "third_party/skia/include/core/SkMatrix.h" |
| 15 #include "third_party/skia/include/core/SkShader.h" | 15 #include "third_party/skia/include/core/SkShader.h" |
| 16 #include "third_party/skia/include/effects/SkLayerRasterizer.h" | 16 #include "third_party/skia/include/effects/SkLayerRasterizer.h" |
| 17 #include "ui/gfx/rect_conversions.h" | 17 #include "ui/gfx/rect_conversions.h" |
| 18 #include "ui/gfx/skia_util.h" |
| 18 #include <public/WebCompositorSoftwareOutputDevice.h> | 19 #include <public/WebCompositorSoftwareOutputDevice.h> |
| 19 #include <public/WebImage.h> | 20 #include <public/WebImage.h> |
| 20 #include <public/WebSize.h> | 21 #include <public/WebSize.h> |
| 21 #include <public/WebTransformationMatrix.h> | 22 #include <public/WebTransformationMatrix.h> |
| 22 | 23 |
| 23 using WebKit::WebCompositorSoftwareOutputDevice; | 24 using WebKit::WebCompositorSoftwareOutputDevice; |
| 24 using WebKit::WebSize; | 25 using WebKit::WebSize; |
| 25 using WebKit::WebTransformationMatrix; | 26 using WebKit::WebTransformationMatrix; |
| 26 | 27 |
| 27 namespace cc { | 28 namespace cc { |
| 28 | 29 |
| 29 namespace { | 30 namespace { |
| 30 | 31 |
| 31 SkRect toSkRect(const gfx::RectF& rect) | |
| 32 { | |
| 33 return SkRect::MakeXYWH(rect.x(), rect.y(), rect.width(), rect.height()); | |
| 34 } | |
| 35 | |
| 36 SkIRect toSkIRect(const gfx::Rect& rect) | |
| 37 { | |
| 38 return SkIRect::MakeXYWH(rect.x(), rect.y(), rect.width(), rect.height()); | |
| 39 } | |
| 40 | |
| 41 void toSkMatrix(SkMatrix* flattened, const WebTransformationMatrix& m) | 32 void toSkMatrix(SkMatrix* flattened, const WebTransformationMatrix& m) |
| 42 { | 33 { |
| 43 // Convert from 4x4 to 3x3 by dropping the third row and column. | 34 // Convert from 4x4 to 3x3 by dropping the third row and column. |
| 44 flattened->set(0, m.m11()); | 35 flattened->set(0, SkDoubleToScalar(m.m11())); |
| 45 flattened->set(1, m.m21()); | 36 flattened->set(1, SkDoubleToScalar(m.m21())); |
| 46 flattened->set(2, m.m41()); | 37 flattened->set(2, SkDoubleToScalar(m.m41())); |
| 47 flattened->set(3, m.m12()); | 38 flattened->set(3, SkDoubleToScalar(m.m12())); |
| 48 flattened->set(4, m.m22()); | 39 flattened->set(4, SkDoubleToScalar(m.m22())); |
| 49 flattened->set(5, m.m42()); | 40 flattened->set(5, SkDoubleToScalar(m.m42())); |
| 50 flattened->set(6, m.m14()); | 41 flattened->set(6, SkDoubleToScalar(m.m14())); |
| 51 flattened->set(7, m.m24()); | 42 flattened->set(7, SkDoubleToScalar(m.m24())); |
| 52 flattened->set(8, m.m44()); | 43 flattened->set(8, SkDoubleToScalar(m.m44())); |
| 53 } | 44 } |
| 54 | 45 |
| 55 bool isScaleAndTranslate(const SkMatrix& matrix) | 46 bool isScaleAndTranslate(const SkMatrix& matrix) |
| 56 { | 47 { |
| 57 return SkScalarNearlyZero(matrix[SkMatrix::kMSkewX]) && | 48 return SkScalarNearlyZero(matrix[SkMatrix::kMSkewX]) && |
| 58 SkScalarNearlyZero(matrix[SkMatrix::kMSkewY]) && | 49 SkScalarNearlyZero(matrix[SkMatrix::kMSkewY]) && |
| 59 SkScalarNearlyZero(matrix[SkMatrix::kMPersp0]) && | 50 SkScalarNearlyZero(matrix[SkMatrix::kMPersp0]) && |
| 60 SkScalarNearlyZero(matrix[SkMatrix::kMPersp1]) && | 51 SkScalarNearlyZero(matrix[SkMatrix::kMPersp1]) && |
| 61 SkScalarNearlyZero(matrix[SkMatrix::kMPersp2] - 1.0f); | 52 SkScalarNearlyZero(matrix[SkMatrix::kMPersp2] - 1.0f); |
| 62 } | 53 } |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 131 m_currentFramebufferLock = make_scoped_ptr(new ResourceProvider::ScopedWrite
LockSoftware(m_resourceProvider, texture->id())); | 122 m_currentFramebufferLock = make_scoped_ptr(new ResourceProvider::ScopedWrite
LockSoftware(m_resourceProvider, texture->id())); |
| 132 m_skCurrentCanvas = m_currentFramebufferLock->skCanvas(); | 123 m_skCurrentCanvas = m_currentFramebufferLock->skCanvas(); |
| 133 initializeMatrices(frame, framebufferRect, false); | 124 initializeMatrices(frame, framebufferRect, false); |
| 134 setDrawViewportSize(framebufferRect.size()); | 125 setDrawViewportSize(framebufferRect.size()); |
| 135 | 126 |
| 136 return true; | 127 return true; |
| 137 } | 128 } |
| 138 | 129 |
| 139 void SoftwareRenderer::setScissorTestRect(const gfx::Rect& scissorRect) | 130 void SoftwareRenderer::setScissorTestRect(const gfx::Rect& scissorRect) |
| 140 { | 131 { |
| 141 m_skCurrentCanvas->clipRect(toSkRect(scissorRect), SkRegion::kReplace_Op); | 132 m_skCurrentCanvas->clipRect(gfx::RectToSkRect(scissorRect), SkRegion::kRepla
ce_Op); |
| 142 } | 133 } |
| 143 | 134 |
| 144 void SoftwareRenderer::clearFramebuffer(DrawingFrame& frame) | 135 void SoftwareRenderer::clearFramebuffer(DrawingFrame& frame) |
| 145 { | 136 { |
| 146 if (frame.currentRenderPass->hasTransparentBackground()) { | 137 if (frame.currentRenderPass->hasTransparentBackground()) { |
| 147 m_skCurrentCanvas->clear(SkColorSetARGB(0, 0, 0, 0)); | 138 m_skCurrentCanvas->clear(SkColorSetARGB(0, 0, 0, 0)); |
| 148 } else { | 139 } else { |
| 149 #ifndef NDEBUG | 140 #ifndef NDEBUG |
| 150 // On DEBUG builds, opaque render passes are cleared to blue to easily s
ee regions that were not drawn on the screen. | 141 // On DEBUG builds, opaque render passes are cleared to blue to easily s
ee regions that were not drawn on the screen. |
| 151 m_skCurrentCanvas->clear(SkColorSetARGB(255, 0, 0, 255)); | 142 m_skCurrentCanvas->clear(SkColorSetARGB(255, 0, 0, 255)); |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 212 break; | 203 break; |
| 213 } | 204 } |
| 214 | 205 |
| 215 m_skCurrentCanvas->resetMatrix(); | 206 m_skCurrentCanvas->resetMatrix(); |
| 216 } | 207 } |
| 217 | 208 |
| 218 void SoftwareRenderer::drawDebugBorderQuad(const DrawingFrame& frame, const Debu
gBorderDrawQuad* quad) | 209 void SoftwareRenderer::drawDebugBorderQuad(const DrawingFrame& frame, const Debu
gBorderDrawQuad* quad) |
| 219 { | 210 { |
| 220 // We need to apply the matrix manually to have pixel-sized stroke width. | 211 // We need to apply the matrix manually to have pixel-sized stroke width. |
| 221 SkPoint vertices[4]; | 212 SkPoint vertices[4]; |
| 222 toSkRect(quadVertexRect()).toQuad(vertices); | 213 gfx::RectFToSkRect(quadVertexRect()).toQuad(vertices); |
| 223 SkPoint transformedVertices[4]; | 214 SkPoint transformedVertices[4]; |
| 224 m_skCurrentCanvas->getTotalMatrix().mapPoints(transformedVertices, vertices,
4); | 215 m_skCurrentCanvas->getTotalMatrix().mapPoints(transformedVertices, vertices,
4); |
| 225 m_skCurrentCanvas->resetMatrix(); | 216 m_skCurrentCanvas->resetMatrix(); |
| 226 | 217 |
| 227 m_skCurrentPaint.setColor(quad->color()); | 218 m_skCurrentPaint.setColor(quad->color()); |
| 228 m_skCurrentPaint.setAlpha(quad->opacity() * SkColorGetA(quad->color())); | 219 m_skCurrentPaint.setAlpha(quad->opacity() * SkColorGetA(quad->color())); |
| 229 m_skCurrentPaint.setStyle(SkPaint::kStroke_Style); | 220 m_skCurrentPaint.setStyle(SkPaint::kStroke_Style); |
| 230 m_skCurrentPaint.setStrokeWidth(quad->width()); | 221 m_skCurrentPaint.setStrokeWidth(quad->width()); |
| 231 m_skCurrentCanvas->drawPoints(SkCanvas::kPolygon_PointMode, 4, transformedVe
rtices, m_skCurrentPaint); | 222 m_skCurrentCanvas->drawPoints(SkCanvas::kPolygon_PointMode, 4, transformedVe
rtices, m_skCurrentPaint); |
| 232 } | 223 } |
| 233 | 224 |
| 234 void SoftwareRenderer::drawSolidColorQuad(const DrawingFrame& frame, const Solid
ColorDrawQuad* quad) | 225 void SoftwareRenderer::drawSolidColorQuad(const DrawingFrame& frame, const Solid
ColorDrawQuad* quad) |
| 235 { | 226 { |
| 236 m_skCurrentPaint.setColor(quad->color()); | 227 m_skCurrentPaint.setColor(quad->color()); |
| 237 m_skCurrentPaint.setAlpha(quad->opacity() * SkColorGetA(quad->color())); | 228 m_skCurrentPaint.setAlpha(quad->opacity() * SkColorGetA(quad->color())); |
| 238 m_skCurrentCanvas->drawRect(toSkRect(quadVertexRect()), m_skCurrentPaint); | 229 m_skCurrentCanvas->drawRect(gfx::RectFToSkRect(quadVertexRect()), m_skCurren
tPaint); |
| 239 } | 230 } |
| 240 | 231 |
| 241 void SoftwareRenderer::drawTextureQuad(const DrawingFrame& frame, const TextureD
rawQuad* quad) | 232 void SoftwareRenderer::drawTextureQuad(const DrawingFrame& frame, const TextureD
rawQuad* quad) |
| 242 { | 233 { |
| 243 if (!isSoftwareResource(quad->resourceId())) { | 234 if (!isSoftwareResource(quad->resourceId())) { |
| 244 drawUnsupportedQuad(frame, quad); | 235 drawUnsupportedQuad(frame, quad); |
| 245 return; | 236 return; |
| 246 } | 237 } |
| 247 | 238 |
| 248 // FIXME: Add support for non-premultiplied alpha. | 239 // FIXME: Add support for non-premultiplied alpha. |
| 249 ResourceProvider::ScopedReadLockSoftware lock(m_resourceProvider, quad->reso
urceId()); | 240 ResourceProvider::ScopedReadLockSoftware lock(m_resourceProvider, quad->reso
urceId()); |
| 250 const SkBitmap* bitmap = lock.skBitmap(); | 241 const SkBitmap* bitmap = lock.skBitmap(); |
| 251 gfx::RectF uvRect = gfx::ScaleRect(quad->uvRect(), bitmap->width(), bitmap->
height()); | 242 gfx::RectF uvRect = gfx::ScaleRect(quad->uvRect(), bitmap->width(), bitmap->
height()); |
| 252 SkRect skUvRect = toSkRect(uvRect); | 243 SkRect skUvRect = gfx::RectFToSkRect(uvRect); |
| 253 if (quad->flipped()) | 244 if (quad->flipped()) |
| 254 m_skCurrentCanvas->scale(1, -1); | 245 m_skCurrentCanvas->scale(1, -1); |
| 255 m_skCurrentCanvas->drawBitmapRectToRect(*bitmap, &skUvRect, | 246 m_skCurrentCanvas->drawBitmapRectToRect(*bitmap, &skUvRect, |
| 256 toSkRect(quadVertexRect()), | 247 gfx::RectFToSkRect(quadVertexRect())
, |
| 257 &m_skCurrentPaint); | 248 &m_skCurrentPaint); |
| 258 } | 249 } |
| 259 | 250 |
| 260 void SoftwareRenderer::drawTileQuad(const DrawingFrame& frame, const TileDrawQua
d* quad) | 251 void SoftwareRenderer::drawTileQuad(const DrawingFrame& frame, const TileDrawQua
d* quad) |
| 261 { | 252 { |
| 262 DCHECK(isSoftwareResource(quad->resourceId())); | 253 DCHECK(isSoftwareResource(quad->resourceId())); |
| 263 ResourceProvider::ScopedReadLockSoftware lock(m_resourceProvider, quad->reso
urceId()); | 254 ResourceProvider::ScopedReadLockSoftware lock(m_resourceProvider, quad->reso
urceId()); |
| 264 | 255 |
| 265 SkRect uvRect = SkRect::MakeXYWH( | 256 SkRect uvRect = SkRect::MakeXYWH( |
| 266 quad->textureOffset().x(), quad->textureOffset().y(), | 257 quad->textureOffset().x(), quad->textureOffset().y(), |
| 267 quad->quadRect().width(), quad->quadRect().height()); | 258 quad->quadRect().width(), quad->quadRect().height()); |
| 268 m_skCurrentPaint.setFilterBitmap(true); | 259 m_skCurrentPaint.setFilterBitmap(true); |
| 269 m_skCurrentCanvas->drawBitmapRectToRect(*lock.skBitmap(), &uvRect, | 260 m_skCurrentCanvas->drawBitmapRectToRect(*lock.skBitmap(), &uvRect, |
| 270 toSkRect(quadVertexRect()), | 261 gfx::RectFToSkRect(quadVertexRect())
, |
| 271 &m_skCurrentPaint); | 262 &m_skCurrentPaint); |
| 272 } | 263 } |
| 273 | 264 |
| 274 void SoftwareRenderer::drawRenderPassQuad(const DrawingFrame& frame, const Rende
rPassDrawQuad* quad) | 265 void SoftwareRenderer::drawRenderPassQuad(const DrawingFrame& frame, const Rende
rPassDrawQuad* quad) |
| 275 { | 266 { |
| 276 CachedResource* contentTexture = m_renderPassTextures.get(quad->renderPassId
()); | 267 CachedResource* contentTexture = m_renderPassTextures.get(quad->renderPassId
()); |
| 277 if (!contentTexture || !contentTexture->id()) | 268 if (!contentTexture || !contentTexture->id()) |
| 278 return; | 269 return; |
| 279 | 270 |
| 280 DCHECK(isSoftwareResource(contentTexture->id())); | 271 DCHECK(isSoftwareResource(contentTexture->id())); |
| 281 ResourceProvider::ScopedReadLockSoftware lock(m_resourceProvider, contentTex
ture->id()); | 272 ResourceProvider::ScopedReadLockSoftware lock(m_resourceProvider, contentTex
ture->id()); |
| 282 | 273 |
| 283 SkRect destRect = toSkRect(quadVertexRect()); | 274 SkRect destRect = gfx::RectFToSkRect(quadVertexRect()); |
| 284 | 275 |
| 285 const SkBitmap* content = lock.skBitmap(); | 276 const SkBitmap* content = lock.skBitmap(); |
| 286 | 277 |
| 287 SkRect contentRect; | 278 SkRect contentRect; |
| 288 content->getBounds(&contentRect); | 279 content->getBounds(&contentRect); |
| 289 | 280 |
| 290 SkMatrix contentMat; | 281 SkMatrix contentMat; |
| 291 contentMat.setRectToRect(contentRect, destRect, SkMatrix::kFill_ScaleToFit); | 282 contentMat.setRectToRect(contentRect, destRect, SkMatrix::kFill_ScaleToFit); |
| 292 | 283 |
| 293 SkAutoTUnref<SkShader> shader(SkShader::CreateBitmapShader(*content, | 284 SkAutoTUnref<SkShader> shader(SkShader::CreateBitmapShader(*content, |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 326 } else { | 317 } else { |
| 327 // FIXME: Apply background filters and blend with content | 318 // FIXME: Apply background filters and blend with content |
| 328 m_skCurrentCanvas->drawRect(destRect, m_skCurrentPaint); | 319 m_skCurrentCanvas->drawRect(destRect, m_skCurrentPaint); |
| 329 } | 320 } |
| 330 } | 321 } |
| 331 | 322 |
| 332 void SoftwareRenderer::drawUnsupportedQuad(const DrawingFrame& frame, const Draw
Quad* quad) | 323 void SoftwareRenderer::drawUnsupportedQuad(const DrawingFrame& frame, const Draw
Quad* quad) |
| 333 { | 324 { |
| 334 m_skCurrentPaint.setColor(SK_ColorMAGENTA); | 325 m_skCurrentPaint.setColor(SK_ColorMAGENTA); |
| 335 m_skCurrentPaint.setAlpha(quad->opacity() * 255); | 326 m_skCurrentPaint.setAlpha(quad->opacity() * 255); |
| 336 m_skCurrentCanvas->drawRect(toSkRect(quadVertexRect()), m_skCurrentPaint); | 327 m_skCurrentCanvas->drawRect(gfx::RectFToSkRect(quadVertexRect()), m_skCurren
tPaint); |
| 337 } | 328 } |
| 338 | 329 |
| 339 bool SoftwareRenderer::swapBuffers() | 330 bool SoftwareRenderer::swapBuffers() |
| 340 { | 331 { |
| 341 if (m_client->hasImplThread()) | 332 if (m_client->hasImplThread()) |
| 342 m_client->onSwapBuffersComplete(); | 333 m_client->onSwapBuffersComplete(); |
| 343 return true; | 334 return true; |
| 344 } | 335 } |
| 345 | 336 |
| 346 void SoftwareRenderer::getFramebufferPixels(void *pixels, const gfx::Rect& rect) | 337 void SoftwareRenderer::getFramebufferPixels(void *pixels, const gfx::Rect& rect) |
| 347 { | 338 { |
| 348 SkBitmap fullBitmap = m_outputDevice->lock(false)->getSkBitmap(); | 339 SkBitmap fullBitmap = m_outputDevice->lock(false)->getSkBitmap(); |
| 349 SkBitmap subsetBitmap; | 340 SkBitmap subsetBitmap; |
| 350 SkIRect invertRect = SkIRect::MakeXYWH(rect.x(), viewportSize().height() - r
ect.bottom(), rect.width(), rect.height()); | 341 SkIRect invertRect = SkIRect::MakeXYWH(rect.x(), viewportSize().height() - r
ect.bottom(), rect.width(), rect.height()); |
| 351 fullBitmap.extractSubset(&subsetBitmap, invertRect); | 342 fullBitmap.extractSubset(&subsetBitmap, invertRect); |
| 352 subsetBitmap.copyPixelsTo(pixels, rect.width() * rect.height() * 4, rect.wid
th() * 4); | 343 subsetBitmap.copyPixelsTo(pixels, rect.width() * rect.height() * 4, rect.wid
th() * 4); |
| 353 m_outputDevice->unlock(); | 344 m_outputDevice->unlock(); |
| 354 } | 345 } |
| 355 | 346 |
| 356 void SoftwareRenderer::setVisible(bool visible) | 347 void SoftwareRenderer::setVisible(bool visible) |
| 357 { | 348 { |
| 358 if (m_visible == visible) | 349 if (m_visible == visible) |
| 359 return; | 350 return; |
| 360 m_visible = visible; | 351 m_visible = visible; |
| 361 } | 352 } |
| 362 | 353 |
| 363 } // namespace cc | 354 } // namespace cc |
| OLD | NEW |