[email protected] | d61675de4 | 2012-09-24 21:32:57 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | c4040a52 | 2012-10-21 15:01:40 | [diff] [blame] | 5 | #include "cc/software_renderer.h" |
[email protected] | d61675de4 | 2012-09-24 21:32:57 | [diff] [blame] | 6 | |
[email protected] | ffbfdc49 | 2012-11-15 03:35:32 | [diff] [blame] | 7 | #include "base/debug/trace_event.h" |
[email protected] | aa0a9d3 | 2012-10-24 01:58:10 | [diff] [blame] | 8 | #include "cc/debug_border_draw_quad.h" |
[email protected] | 55a124d0 | 2012-10-22 03:07:13 | [diff] [blame] | 9 | #include "cc/render_pass_draw_quad.h" |
[email protected] | 4456eee2 | 2012-10-19 18:16:38 | [diff] [blame] | 10 | #include "cc/solid_color_draw_quad.h" |
| 11 | #include "cc/texture_draw_quad.h" |
[email protected] | da2c912 | 2012-10-20 23:13:06 | [diff] [blame] | 12 | #include "cc/tile_draw_quad.h" |
[email protected] | 0b056ee | 2012-10-15 21:31:21 | [diff] [blame] | 13 | #include "third_party/skia/include/core/SkCanvas.h" |
| 14 | #include "third_party/skia/include/core/SkColor.h" |
| 15 | #include "third_party/skia/include/core/SkMatrix.h" |
| 16 | #include "third_party/skia/include/core/SkShader.h" |
| 17 | #include "third_party/skia/include/effects/SkLayerRasterizer.h" |
[email protected] | 1fea814 | 2012-10-20 04:12:41 | [diff] [blame] | 18 | #include "ui/gfx/rect_conversions.h" |
[email protected] | 79fbdab0 | 2012-11-14 07:28:11 | [diff] [blame] | 19 | #include "ui/gfx/skia_util.h" |
[email protected] | 59cb7b35 | 2012-10-30 06:45:48 | [diff] [blame] | 20 | #include <public/WebCompositorSoftwareOutputDevice.h> |
[email protected] | d61675de4 | 2012-09-24 21:32:57 | [diff] [blame] | 21 | #include <public/WebImage.h> |
| 22 | #include <public/WebSize.h> |
| 23 | #include <public/WebTransformationMatrix.h> |
| 24 | |
| 25 | using WebKit::WebCompositorSoftwareOutputDevice; |
[email protected] | d61675de4 | 2012-09-24 21:32:57 | [diff] [blame] | 26 | using WebKit::WebSize; |
| 27 | using WebKit::WebTransformationMatrix; |
| 28 | |
| 29 | namespace cc { |
| 30 | |
| 31 | namespace { |
| 32 | |
[email protected] | d61675de4 | 2012-09-24 21:32:57 | [diff] [blame] | 33 | void toSkMatrix(SkMatrix* flattened, const WebTransformationMatrix& m) |
| 34 | { |
| 35 | // Convert from 4x4 to 3x3 by dropping the third row and column. |
[email protected] | 79fbdab0 | 2012-11-14 07:28:11 | [diff] [blame] | 36 | flattened->set(0, SkDoubleToScalar(m.m11())); |
| 37 | flattened->set(1, SkDoubleToScalar(m.m21())); |
| 38 | flattened->set(2, SkDoubleToScalar(m.m41())); |
| 39 | flattened->set(3, SkDoubleToScalar(m.m12())); |
| 40 | flattened->set(4, SkDoubleToScalar(m.m22())); |
| 41 | flattened->set(5, SkDoubleToScalar(m.m42())); |
| 42 | flattened->set(6, SkDoubleToScalar(m.m14())); |
| 43 | flattened->set(7, SkDoubleToScalar(m.m24())); |
| 44 | flattened->set(8, SkDoubleToScalar(m.m44())); |
[email protected] | d61675de4 | 2012-09-24 21:32:57 | [diff] [blame] | 45 | } |
| 46 | |
[email protected] | 276508a | 2012-10-16 00:56:34 | [diff] [blame] | 47 | bool isScaleAndTranslate(const SkMatrix& matrix) |
| 48 | { |
| 49 | return SkScalarNearlyZero(matrix[SkMatrix::kMSkewX]) && |
| 50 | SkScalarNearlyZero(matrix[SkMatrix::kMSkewY]) && |
| 51 | SkScalarNearlyZero(matrix[SkMatrix::kMPersp0]) && |
| 52 | SkScalarNearlyZero(matrix[SkMatrix::kMPersp1]) && |
| 53 | SkScalarNearlyZero(matrix[SkMatrix::kMPersp2] - 1.0f); |
| 54 | } |
| 55 | |
[email protected] | d61675de4 | 2012-09-24 21:32:57 | [diff] [blame] | 56 | } // anonymous namespace |
| 57 | |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 58 | scoped_ptr<SoftwareRenderer> SoftwareRenderer::create(RendererClient* client, ResourceProvider* resourceProvider, WebCompositorSoftwareOutputDevice* outputDevice) |
[email protected] | d61675de4 | 2012-09-24 21:32:57 | [diff] [blame] | 59 | { |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 60 | return make_scoped_ptr(new SoftwareRenderer(client, resourceProvider, outputDevice)); |
[email protected] | d61675de4 | 2012-09-24 21:32:57 | [diff] [blame] | 61 | } |
| 62 | |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 63 | SoftwareRenderer::SoftwareRenderer(RendererClient* client, ResourceProvider* resourceProvider, WebCompositorSoftwareOutputDevice* outputDevice) |
| 64 | : DirectRenderer(client, resourceProvider) |
[email protected] | d61675de4 | 2012-09-24 21:32:57 | [diff] [blame] | 65 | , m_visible(true) |
| 66 | , m_outputDevice(outputDevice) |
[email protected] | d61675de4 | 2012-09-24 21:32:57 | [diff] [blame] | 67 | , m_skCurrentCanvas(0) |
| 68 | { |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 69 | m_resourceProvider->setDefaultResourceType(ResourceProvider::Bitmap); |
[email protected] | d61675de4 | 2012-09-24 21:32:57 | [diff] [blame] | 70 | |
| 71 | m_capabilities.maxTextureSize = INT_MAX; |
[email protected] | d9c2852 | 2012-10-18 23:35:43 | [diff] [blame] | 72 | m_capabilities.bestTextureFormat = GL_RGBA; |
[email protected] | d61675de4 | 2012-09-24 21:32:57 | [diff] [blame] | 73 | m_capabilities.contextHasCachedFrontBuffer = true; |
| 74 | m_capabilities.usingSetVisibility = true; |
| 75 | |
| 76 | viewportChanged(); |
| 77 | } |
| 78 | |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 79 | SoftwareRenderer::~SoftwareRenderer() |
[email protected] | d61675de4 | 2012-09-24 21:32:57 | [diff] [blame] | 80 | { |
| 81 | } |
| 82 | |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 83 | const RendererCapabilities& SoftwareRenderer::capabilities() const |
[email protected] | d61675de4 | 2012-09-24 21:32:57 | [diff] [blame] | 84 | { |
| 85 | return m_capabilities; |
| 86 | } |
| 87 | |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 88 | void SoftwareRenderer::viewportChanged() |
[email protected] | d61675de4 | 2012-09-24 21:32:57 | [diff] [blame] | 89 | { |
| 90 | m_outputDevice->didChangeViewportSize(WebSize(viewportSize().width(), viewportSize().height())); |
| 91 | } |
| 92 | |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 93 | void SoftwareRenderer::beginDrawingFrame(DrawingFrame& frame) |
[email protected] | d61675de4 | 2012-09-24 21:32:57 | [diff] [blame] | 94 | { |
[email protected] | ffbfdc49 | 2012-11-15 03:35:32 | [diff] [blame] | 95 | TRACE_EVENT0("cc", "SoftwareRenderer::beginDrawingFrame"); |
[email protected] | 0704caf | 2012-10-16 03:39:47 | [diff] [blame] | 96 | m_skRootCanvas = make_scoped_ptr(new SkCanvas(m_outputDevice->lock(true)->getSkBitmap())); |
[email protected] | d61675de4 | 2012-09-24 21:32:57 | [diff] [blame] | 97 | } |
| 98 | |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 99 | void SoftwareRenderer::finishDrawingFrame(DrawingFrame& frame) |
[email protected] | d61675de4 | 2012-09-24 21:32:57 | [diff] [blame] | 100 | { |
[email protected] | ffbfdc49 | 2012-11-15 03:35:32 | [diff] [blame] | 101 | TRACE_EVENT0("cc", "SoftwareRenderer::finishDrawingFrame"); |
[email protected] | 0704caf | 2012-10-16 03:39:47 | [diff] [blame] | 102 | m_currentFramebufferLock.reset(); |
[email protected] | d61675de4 | 2012-09-24 21:32:57 | [diff] [blame] | 103 | m_skCurrentCanvas = 0; |
[email protected] | 0704caf | 2012-10-16 03:39:47 | [diff] [blame] | 104 | m_skRootCanvas.reset(); |
[email protected] | d61675de4 | 2012-09-24 21:32:57 | [diff] [blame] | 105 | m_outputDevice->unlock(); |
| 106 | } |
| 107 | |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 108 | bool SoftwareRenderer::flippedFramebuffer() const |
[email protected] | d61675de4 | 2012-09-24 21:32:57 | [diff] [blame] | 109 | { |
| 110 | return false; |
| 111 | } |
| 112 | |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 113 | void SoftwareRenderer::finish() |
[email protected] | d61675de4 | 2012-09-24 21:32:57 | [diff] [blame] | 114 | { |
| 115 | } |
| 116 | |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 117 | void SoftwareRenderer::bindFramebufferToOutputSurface(DrawingFrame& frame) |
[email protected] | d61675de4 | 2012-09-24 21:32:57 | [diff] [blame] | 118 | { |
[email protected] | 0704caf | 2012-10-16 03:39:47 | [diff] [blame] | 119 | m_currentFramebufferLock.reset(); |
[email protected] | 75fe7f82 | 2012-09-28 17:54:14 | [diff] [blame] | 120 | m_skCurrentCanvas = m_skRootCanvas.get(); |
[email protected] | d61675de4 | 2012-09-24 21:32:57 | [diff] [blame] | 121 | } |
| 122 | |
[email protected] | f7685bc | 2012-11-08 17:32:33 | [diff] [blame] | 123 | bool SoftwareRenderer::bindFramebufferToTexture(DrawingFrame& frame, const ScopedResource* texture, const gfx::Rect& framebufferRect) |
[email protected] | d61675de4 | 2012-09-24 21:32:57 | [diff] [blame] | 124 | { |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 125 | m_currentFramebufferLock = make_scoped_ptr(new ResourceProvider::ScopedWriteLockSoftware(m_resourceProvider, texture->id())); |
[email protected] | d61675de4 | 2012-09-24 21:32:57 | [diff] [blame] | 126 | m_skCurrentCanvas = m_currentFramebufferLock->skCanvas(); |
| 127 | initializeMatrices(frame, framebufferRect, false); |
| 128 | setDrawViewportSize(framebufferRect.size()); |
| 129 | |
| 130 | return true; |
| 131 | } |
| 132 | |
[email protected] | 7ead093 | 2012-11-08 07:46:52 | [diff] [blame] | 133 | void SoftwareRenderer::setScissorTestRect(const gfx::Rect& scissorRect) |
[email protected] | d61675de4 | 2012-09-24 21:32:57 | [diff] [blame] | 134 | { |
[email protected] | 79fbdab0 | 2012-11-14 07:28:11 | [diff] [blame] | 135 | m_skCurrentCanvas->clipRect(gfx::RectToSkRect(scissorRect), SkRegion::kReplace_Op); |
[email protected] | d61675de4 | 2012-09-24 21:32:57 | [diff] [blame] | 136 | } |
| 137 | |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 138 | void SoftwareRenderer::clearFramebuffer(DrawingFrame& frame) |
[email protected] | d61675de4 | 2012-09-24 21:32:57 | [diff] [blame] | 139 | { |
[email protected] | 0b056ee | 2012-10-15 21:31:21 | [diff] [blame] | 140 | if (frame.currentRenderPass->hasTransparentBackground()) { |
| 141 | m_skCurrentCanvas->clear(SkColorSetARGB(0, 0, 0, 0)); |
| 142 | } else { |
| 143 | #ifndef NDEBUG |
| 144 | // On DEBUG builds, opaque render passes are cleared to blue to easily see regions that were not drawn on the screen. |
| 145 | m_skCurrentCanvas->clear(SkColorSetARGB(255, 0, 0, 255)); |
| 146 | #endif |
| 147 | } |
[email protected] | d61675de4 | 2012-09-24 21:32:57 | [diff] [blame] | 148 | } |
| 149 | |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 150 | void SoftwareRenderer::setDrawViewportSize(const gfx::Size& viewportSize) |
[email protected] | d61675de4 | 2012-09-24 21:32:57 | [diff] [blame] | 151 | { |
| 152 | } |
| 153 | |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 154 | bool SoftwareRenderer::isSoftwareResource(ResourceProvider::ResourceId id) const |
[email protected] | d61675de4 | 2012-09-24 21:32:57 | [diff] [blame] | 155 | { |
| 156 | switch (m_resourceProvider->resourceType(id)) { |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 157 | case ResourceProvider::GLTexture: |
[email protected] | d61675de4 | 2012-09-24 21:32:57 | [diff] [blame] | 158 | return false; |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 159 | case ResourceProvider::Bitmap: |
[email protected] | d61675de4 | 2012-09-24 21:32:57 | [diff] [blame] | 160 | return true; |
| 161 | } |
| 162 | |
[email protected] | 6eb6bb6 | 2012-11-10 06:30:59 | [diff] [blame] | 163 | LOG(FATAL) << "Invalid resource type."; |
[email protected] | d61675de4 | 2012-09-24 21:32:57 | [diff] [blame] | 164 | return false; |
| 165 | } |
| 166 | |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 167 | void SoftwareRenderer::drawQuad(DrawingFrame& frame, const DrawQuad* quad) |
[email protected] | d61675de4 | 2012-09-24 21:32:57 | [diff] [blame] | 168 | { |
[email protected] | ffbfdc49 | 2012-11-15 03:35:32 | [diff] [blame] | 169 | TRACE_EVENT0("cc", "SoftwareRenderer::drawQuad"); |
[email protected] | d61675de4 | 2012-09-24 21:32:57 | [diff] [blame] | 170 | WebTransformationMatrix quadRectMatrix; |
| 171 | quadRectTransform(&quadRectMatrix, quad->quadTransform(), quad->quadRect()); |
[email protected] | 0b056ee | 2012-10-15 21:31:21 | [diff] [blame] | 172 | WebTransformationMatrix contentsDeviceTransform = (frame.windowMatrix * frame.projectionMatrix * quadRectMatrix).to2dTransform(); |
| 173 | SkMatrix skDeviceMatrix; |
| 174 | toSkMatrix(&skDeviceMatrix, contentsDeviceTransform); |
| 175 | m_skCurrentCanvas->setMatrix(skDeviceMatrix); |
[email protected] | d61675de4 | 2012-09-24 21:32:57 | [diff] [blame] | 176 | |
| 177 | m_skCurrentPaint.reset(); |
[email protected] | 276508a | 2012-10-16 00:56:34 | [diff] [blame] | 178 | if (!isScaleAndTranslate(skDeviceMatrix)) { |
| 179 | m_skCurrentPaint.setAntiAlias(true); |
| 180 | m_skCurrentPaint.setFilterBitmap(true); |
| 181 | } |
[email protected] | 0b056ee | 2012-10-15 21:31:21 | [diff] [blame] | 182 | if (quad->needsBlending()) { |
[email protected] | d61675de4 | 2012-09-24 21:32:57 | [diff] [blame] | 183 | m_skCurrentPaint.setAlpha(quad->opacity() * 255); |
[email protected] | 0b056ee | 2012-10-15 21:31:21 | [diff] [blame] | 184 | m_skCurrentPaint.setXfermodeMode(SkXfermode::kSrcOver_Mode); |
| 185 | } else { |
[email protected] | d61675de4 | 2012-09-24 21:32:57 | [diff] [blame] | 186 | m_skCurrentPaint.setXfermodeMode(SkXfermode::kSrc_Mode); |
[email protected] | 0b056ee | 2012-10-15 21:31:21 | [diff] [blame] | 187 | } |
[email protected] | d61675de4 | 2012-09-24 21:32:57 | [diff] [blame] | 188 | |
| 189 | switch (quad->material()) { |
[email protected] | e48727e | 2012-11-16 22:10:02 | [diff] [blame^] | 190 | case DrawQuad::DEBUG_BORDER: |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 191 | drawDebugBorderQuad(frame, DebugBorderDrawQuad::materialCast(quad)); |
[email protected] | d61675de4 | 2012-09-24 21:32:57 | [diff] [blame] | 192 | break; |
[email protected] | e48727e | 2012-11-16 22:10:02 | [diff] [blame^] | 193 | case DrawQuad::SOLID_COLOR: |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 194 | drawSolidColorQuad(frame, SolidColorDrawQuad::materialCast(quad)); |
[email protected] | d61675de4 | 2012-09-24 21:32:57 | [diff] [blame] | 195 | break; |
[email protected] | e48727e | 2012-11-16 22:10:02 | [diff] [blame^] | 196 | case DrawQuad::TEXTURE_CONTENT: |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 197 | drawTextureQuad(frame, TextureDrawQuad::materialCast(quad)); |
[email protected] | d61675de4 | 2012-09-24 21:32:57 | [diff] [blame] | 198 | break; |
[email protected] | e48727e | 2012-11-16 22:10:02 | [diff] [blame^] | 199 | case DrawQuad::TILED_CONTENT: |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 200 | drawTileQuad(frame, TileDrawQuad::materialCast(quad)); |
[email protected] | d61675de4 | 2012-09-24 21:32:57 | [diff] [blame] | 201 | break; |
[email protected] | e48727e | 2012-11-16 22:10:02 | [diff] [blame^] | 202 | case DrawQuad::RENDER_PASS: |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 203 | drawRenderPassQuad(frame, RenderPassDrawQuad::materialCast(quad)); |
[email protected] | 0b056ee | 2012-10-15 21:31:21 | [diff] [blame] | 204 | break; |
[email protected] | d61675de4 | 2012-09-24 21:32:57 | [diff] [blame] | 205 | default: |
| 206 | drawUnsupportedQuad(frame, quad); |
| 207 | break; |
| 208 | } |
| 209 | |
| 210 | m_skCurrentCanvas->resetMatrix(); |
| 211 | } |
| 212 | |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 213 | void SoftwareRenderer::drawDebugBorderQuad(const DrawingFrame& frame, const DebugBorderDrawQuad* quad) |
[email protected] | d61675de4 | 2012-09-24 21:32:57 | [diff] [blame] | 214 | { |
| 215 | // We need to apply the matrix manually to have pixel-sized stroke width. |
| 216 | SkPoint vertices[4]; |
[email protected] | 79fbdab0 | 2012-11-14 07:28:11 | [diff] [blame] | 217 | gfx::RectFToSkRect(quadVertexRect()).toQuad(vertices); |
[email protected] | d61675de4 | 2012-09-24 21:32:57 | [diff] [blame] | 218 | SkPoint transformedVertices[4]; |
| 219 | m_skCurrentCanvas->getTotalMatrix().mapPoints(transformedVertices, vertices, 4); |
| 220 | m_skCurrentCanvas->resetMatrix(); |
| 221 | |
| 222 | m_skCurrentPaint.setColor(quad->color()); |
| 223 | m_skCurrentPaint.setAlpha(quad->opacity() * SkColorGetA(quad->color())); |
| 224 | m_skCurrentPaint.setStyle(SkPaint::kStroke_Style); |
| 225 | m_skCurrentPaint.setStrokeWidth(quad->width()); |
| 226 | m_skCurrentCanvas->drawPoints(SkCanvas::kPolygon_PointMode, 4, transformedVertices, m_skCurrentPaint); |
| 227 | } |
| 228 | |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 229 | void SoftwareRenderer::drawSolidColorQuad(const DrawingFrame& frame, const SolidColorDrawQuad* quad) |
[email protected] | d61675de4 | 2012-09-24 21:32:57 | [diff] [blame] | 230 | { |
| 231 | m_skCurrentPaint.setColor(quad->color()); |
| 232 | m_skCurrentPaint.setAlpha(quad->opacity() * SkColorGetA(quad->color())); |
[email protected] | 79fbdab0 | 2012-11-14 07:28:11 | [diff] [blame] | 233 | m_skCurrentCanvas->drawRect(gfx::RectFToSkRect(quadVertexRect()), m_skCurrentPaint); |
[email protected] | d61675de4 | 2012-09-24 21:32:57 | [diff] [blame] | 234 | } |
| 235 | |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 236 | void SoftwareRenderer::drawTextureQuad(const DrawingFrame& frame, const TextureDrawQuad* quad) |
[email protected] | d61675de4 | 2012-09-24 21:32:57 | [diff] [blame] | 237 | { |
| 238 | if (!isSoftwareResource(quad->resourceId())) { |
| 239 | drawUnsupportedQuad(frame, quad); |
| 240 | return; |
| 241 | } |
| 242 | |
| 243 | // FIXME: Add support for non-premultiplied alpha. |
[email protected] | 058a724a | 2012-10-30 18:37:02 | [diff] [blame] | 244 | ResourceProvider::ScopedReadLockSoftware lock(m_resourceProvider, quad->resourceId()); |
| 245 | const SkBitmap* bitmap = lock.skBitmap(); |
[email protected] | 59cb7b35 | 2012-10-30 06:45:48 | [diff] [blame] | 246 | gfx::RectF uvRect = gfx::ScaleRect(quad->uvRect(), bitmap->width(), bitmap->height()); |
[email protected] | 79fbdab0 | 2012-11-14 07:28:11 | [diff] [blame] | 247 | SkRect skUvRect = gfx::RectFToSkRect(uvRect); |
[email protected] | d61675de4 | 2012-09-24 21:32:57 | [diff] [blame] | 248 | if (quad->flipped()) |
| 249 | m_skCurrentCanvas->scale(1, -1); |
[email protected] | 66dce29 | 2012-11-06 09:12:00 | [diff] [blame] | 250 | m_skCurrentCanvas->drawBitmapRectToRect(*bitmap, &skUvRect, |
[email protected] | 79fbdab0 | 2012-11-14 07:28:11 | [diff] [blame] | 251 | gfx::RectFToSkRect(quadVertexRect()), |
[email protected] | 66dce29 | 2012-11-06 09:12:00 | [diff] [blame] | 252 | &m_skCurrentPaint); |
[email protected] | d61675de4 | 2012-09-24 21:32:57 | [diff] [blame] | 253 | } |
| 254 | |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 255 | void SoftwareRenderer::drawTileQuad(const DrawingFrame& frame, const TileDrawQuad* quad) |
[email protected] | d61675de4 | 2012-09-24 21:32:57 | [diff] [blame] | 256 | { |
[email protected] | 1d99317 | 2012-10-18 18:15:04 | [diff] [blame] | 257 | DCHECK(isSoftwareResource(quad->resourceId())); |
[email protected] | 058a724a | 2012-10-30 18:37:02 | [diff] [blame] | 258 | ResourceProvider::ScopedReadLockSoftware lock(m_resourceProvider, quad->resourceId()); |
[email protected] | d61675de4 | 2012-09-24 21:32:57 | [diff] [blame] | 259 | |
[email protected] | 66dce29 | 2012-11-06 09:12:00 | [diff] [blame] | 260 | SkRect uvRect = SkRect::MakeXYWH( |
| 261 | quad->textureOffset().x(), quad->textureOffset().y(), |
| 262 | quad->quadRect().width(), quad->quadRect().height()); |
[email protected] | 0457b750 | 2012-11-13 21:48:42 | [diff] [blame] | 263 | m_skCurrentPaint.setFilterBitmap(true); |
[email protected] | 66dce29 | 2012-11-06 09:12:00 | [diff] [blame] | 264 | m_skCurrentCanvas->drawBitmapRectToRect(*lock.skBitmap(), &uvRect, |
[email protected] | 79fbdab0 | 2012-11-14 07:28:11 | [diff] [blame] | 265 | gfx::RectFToSkRect(quadVertexRect()), |
[email protected] | 66dce29 | 2012-11-06 09:12:00 | [diff] [blame] | 266 | &m_skCurrentPaint); |
[email protected] | d61675de4 | 2012-09-24 21:32:57 | [diff] [blame] | 267 | } |
| 268 | |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 269 | void SoftwareRenderer::drawRenderPassQuad(const DrawingFrame& frame, const RenderPassDrawQuad* quad) |
[email protected] | 0b056ee | 2012-10-15 21:31:21 | [diff] [blame] | 270 | { |
[email protected] | f7685bc | 2012-11-08 17:32:33 | [diff] [blame] | 271 | CachedResource* contentTexture = m_renderPassTextures.get(quad->renderPassId()); |
[email protected] | 058a724a | 2012-10-30 18:37:02 | [diff] [blame] | 272 | if (!contentTexture || !contentTexture->id()) |
[email protected] | 0b056ee | 2012-10-15 21:31:21 | [diff] [blame] | 273 | return; |
| 274 | |
[email protected] | 632bd766 | 2012-11-14 21:21:54 | [diff] [blame] | 275 | const RenderPass* renderPass = frame.renderPassesById->get(quad->renderPassId()); |
| 276 | DCHECK(renderPass); |
| 277 | if (!renderPass) |
| 278 | return; |
| 279 | |
[email protected] | 058a724a | 2012-10-30 18:37:02 | [diff] [blame] | 280 | DCHECK(isSoftwareResource(contentTexture->id())); |
| 281 | ResourceProvider::ScopedReadLockSoftware lock(m_resourceProvider, contentTexture->id()); |
[email protected] | 0b056ee | 2012-10-15 21:31:21 | [diff] [blame] | 282 | |
[email protected] | 79fbdab0 | 2012-11-14 07:28:11 | [diff] [blame] | 283 | SkRect destRect = gfx::RectFToSkRect(quadVertexRect()); |
[email protected] | 0b056ee | 2012-10-15 21:31:21 | [diff] [blame] | 284 | |
[email protected] | 058a724a | 2012-10-30 18:37:02 | [diff] [blame] | 285 | const SkBitmap* content = lock.skBitmap(); |
[email protected] | 0b056ee | 2012-10-15 21:31:21 | [diff] [blame] | 286 | |
[email protected] | 058a724a | 2012-10-30 18:37:02 | [diff] [blame] | 287 | SkRect contentRect; |
| 288 | content->getBounds(&contentRect); |
| 289 | |
| 290 | SkMatrix contentMat; |
| 291 | contentMat.setRectToRect(contentRect, destRect, SkMatrix::kFill_ScaleToFit); |
| 292 | |
| 293 | SkAutoTUnref<SkShader> shader(SkShader::CreateBitmapShader(*content, |
[email protected] | 0b056ee | 2012-10-15 21:31:21 | [diff] [blame] | 294 | SkShader::kClamp_TileMode, |
| 295 | SkShader::kClamp_TileMode)); |
[email protected] | 058a724a | 2012-10-30 18:37:02 | [diff] [blame] | 296 | shader->setLocalMatrix(contentMat); |
[email protected] | 0b056ee | 2012-10-15 21:31:21 | [diff] [blame] | 297 | m_skCurrentPaint.setShader(shader); |
| 298 | |
[email protected] | 632bd766 | 2012-11-14 21:21:54 | [diff] [blame] | 299 | SkImageFilter* filter = renderPass->filter(); |
| 300 | if (filter) |
| 301 | m_skCurrentPaint.setImageFilter(filter); |
| 302 | |
[email protected] | 0b056ee | 2012-10-15 21:31:21 | [diff] [blame] | 303 | if (quad->maskResourceId()) { |
[email protected] | 058a724a | 2012-10-30 18:37:02 | [diff] [blame] | 304 | ResourceProvider::ScopedReadLockSoftware maskLock(m_resourceProvider, quad->maskResourceId()); |
| 305 | |
| 306 | const SkBitmap* mask = maskLock.skBitmap(); |
| 307 | |
| 308 | SkRect maskRect = SkRect::MakeXYWH( |
| 309 | quad->maskTexCoordOffsetX() * mask->width(), |
| 310 | quad->maskTexCoordOffsetY() * mask->height(), |
| 311 | quad->maskTexCoordScaleX() * mask->width(), |
| 312 | quad->maskTexCoordScaleY() * mask->height()); |
[email protected] | 0b056ee | 2012-10-15 21:31:21 | [diff] [blame] | 313 | |
| 314 | SkMatrix maskMat; |
[email protected] | 058a724a | 2012-10-30 18:37:02 | [diff] [blame] | 315 | maskMat.setRectToRect(maskRect, destRect, SkMatrix::kFill_ScaleToFit); |
[email protected] | 0b056ee | 2012-10-15 21:31:21 | [diff] [blame] | 316 | |
[email protected] | 058a724a | 2012-10-30 18:37:02 | [diff] [blame] | 317 | SkAutoTUnref<SkShader> maskShader(SkShader::CreateBitmapShader(*mask, |
[email protected] | 0b056ee | 2012-10-15 21:31:21 | [diff] [blame] | 318 | SkShader::kClamp_TileMode, |
| 319 | SkShader::kClamp_TileMode)); |
| 320 | maskShader->setLocalMatrix(maskMat); |
| 321 | |
| 322 | SkPaint maskPaint; |
| 323 | maskPaint.setShader(maskShader); |
| 324 | |
| 325 | SkAutoTUnref<SkLayerRasterizer> maskRasterizer(new SkLayerRasterizer); |
| 326 | maskRasterizer->addLayer(maskPaint); |
| 327 | |
| 328 | m_skCurrentPaint.setRasterizer(maskRasterizer); |
| 329 | m_skCurrentCanvas->drawRect(destRect, m_skCurrentPaint); |
| 330 | } else { |
[email protected] | 058a724a | 2012-10-30 18:37:02 | [diff] [blame] | 331 | // FIXME: Apply background filters and blend with content |
[email protected] | 0b056ee | 2012-10-15 21:31:21 | [diff] [blame] | 332 | m_skCurrentCanvas->drawRect(destRect, m_skCurrentPaint); |
| 333 | } |
| 334 | } |
| 335 | |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 336 | void SoftwareRenderer::drawUnsupportedQuad(const DrawingFrame& frame, const DrawQuad* quad) |
[email protected] | d61675de4 | 2012-09-24 21:32:57 | [diff] [blame] | 337 | { |
| 338 | m_skCurrentPaint.setColor(SK_ColorMAGENTA); |
| 339 | m_skCurrentPaint.setAlpha(quad->opacity() * 255); |
[email protected] | 79fbdab0 | 2012-11-14 07:28:11 | [diff] [blame] | 340 | m_skCurrentCanvas->drawRect(gfx::RectFToSkRect(quadVertexRect()), m_skCurrentPaint); |
[email protected] | d61675de4 | 2012-09-24 21:32:57 | [diff] [blame] | 341 | } |
| 342 | |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 343 | bool SoftwareRenderer::swapBuffers() |
[email protected] | d61675de4 | 2012-09-24 21:32:57 | [diff] [blame] | 344 | { |
[email protected] | 61de581 | 2012-11-08 07:03:44 | [diff] [blame] | 345 | if (m_client->hasImplThread()) |
[email protected] | d61675de4 | 2012-09-24 21:32:57 | [diff] [blame] | 346 | m_client->onSwapBuffersComplete(); |
| 347 | return true; |
| 348 | } |
| 349 | |
[email protected] | aad0a007 | 2012-11-01 18:15:58 | [diff] [blame] | 350 | void SoftwareRenderer::getFramebufferPixels(void *pixels, const gfx::Rect& rect) |
[email protected] | d61675de4 | 2012-09-24 21:32:57 | [diff] [blame] | 351 | { |
[email protected] | ffbfdc49 | 2012-11-15 03:35:32 | [diff] [blame] | 352 | TRACE_EVENT0("cc", "SoftwareRenderer::getFramebufferPixels"); |
[email protected] | d61675de4 | 2012-09-24 21:32:57 | [diff] [blame] | 353 | SkBitmap fullBitmap = m_outputDevice->lock(false)->getSkBitmap(); |
| 354 | SkBitmap subsetBitmap; |
[email protected] | aad0a007 | 2012-11-01 18:15:58 | [diff] [blame] | 355 | SkIRect invertRect = SkIRect::MakeXYWH(rect.x(), viewportSize().height() - rect.bottom(), rect.width(), rect.height()); |
[email protected] | d61675de4 | 2012-09-24 21:32:57 | [diff] [blame] | 356 | fullBitmap.extractSubset(&subsetBitmap, invertRect); |
| 357 | subsetBitmap.copyPixelsTo(pixels, rect.width() * rect.height() * 4, rect.width() * 4); |
| 358 | m_outputDevice->unlock(); |
| 359 | } |
| 360 | |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 361 | void SoftwareRenderer::setVisible(bool visible) |
[email protected] | d61675de4 | 2012-09-24 21:32:57 | [diff] [blame] | 362 | { |
| 363 | if (m_visible == visible) |
| 364 | return; |
| 365 | m_visible = visible; |
| 366 | } |
| 367 | |
[email protected] | bc5e77c | 2012-11-05 20:00:49 | [diff] [blame] | 368 | } // namespace cc |