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