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.
5 #include "cc/output/software_renderer.h"
7 #include "base/debug/trace_event.h"
8 #include "cc/base/math_util.h"
9 #include "cc/output/compositor_frame.h"
10 #include "cc/output/compositor_frame_ack.h"
11 #include "cc/output/compositor_frame_metadata.h"
12 #include "cc/output/output_surface.h"
13 #include "cc/output/software_output_device.h"
14 #include "cc/quads/debug_border_draw_quad.h"
15 #include "cc/quads/render_pass_draw_quad.h"
16 #include "cc/quads/solid_color_draw_quad.h"
17 #include "cc/quads/texture_draw_quad.h"
18 #include "cc/quads/tile_draw_quad.h"
19 #include "third_party/skia/include/core/SkCanvas.h"
20 #include "third_party/skia/include/core/SkColor.h"
21 #include "third_party/skia/include/core/SkDevice.h"
22 #include "third_party/skia/include/core/SkMatrix.h"
23 #include "third_party/skia/include/core/SkShader.h"
24 #include "third_party/skia/include/effects/SkLayerRasterizer.h"
25 #include "ui/gfx/rect_conversions.h"
26 #include "ui/gfx/skia_util.h"
27 #include "ui/gfx/transform.h"
33 void ToSkMatrix(SkMatrix
* flattened
, const gfx::Transform
& m
) {
34 // Convert from 4x4 to 3x3 by dropping the third row and column.
35 flattened
->set(0, SkDoubleToScalar(m
.matrix().getDouble(0, 0)));
36 flattened
->set(1, SkDoubleToScalar(m
.matrix().getDouble(0, 1)));
37 flattened
->set(2, SkDoubleToScalar(m
.matrix().getDouble(0, 3)));
38 flattened
->set(3, SkDoubleToScalar(m
.matrix().getDouble(1, 0)));
39 flattened
->set(4, SkDoubleToScalar(m
.matrix().getDouble(1, 1)));
40 flattened
->set(5, SkDoubleToScalar(m
.matrix().getDouble(1, 3)));
41 flattened
->set(6, SkDoubleToScalar(m
.matrix().getDouble(3, 0)));
42 flattened
->set(7, SkDoubleToScalar(m
.matrix().getDouble(3, 1)));
43 flattened
->set(8, SkDoubleToScalar(m
.matrix().getDouble(3, 3)));
46 bool IsScaleAndTranslate(const SkMatrix
& matrix
) {
47 return SkScalarNearlyZero(matrix
[SkMatrix::kMSkewX
]) &&
48 SkScalarNearlyZero(matrix
[SkMatrix::kMSkewY
]) &&
49 SkScalarNearlyZero(matrix
[SkMatrix::kMPersp0
]) &&
50 SkScalarNearlyZero(matrix
[SkMatrix::kMPersp1
]) &&
51 SkScalarNearlyZero(matrix
[SkMatrix::kMPersp2
] - 1.0f
);
54 } // anonymous namespace
56 scoped_ptr
<SoftwareRenderer
> SoftwareRenderer::Create(
57 RendererClient
* client
,
58 OutputSurface
* output_surface
,
59 ResourceProvider
* resource_provider
) {
60 return make_scoped_ptr(
61 new SoftwareRenderer(client
, output_surface
, resource_provider
));
64 SoftwareRenderer::SoftwareRenderer(RendererClient
* client
,
65 OutputSurface
* output_surface
,
66 ResourceProvider
* resource_provider
)
67 : DirectRenderer(client
, resource_provider
),
69 is_scissor_enabled_(false),
70 is_viewport_changed_(true),
71 output_surface_(output_surface
),
72 output_device_(output_surface
->software_device()),
73 current_canvas_(NULL
) {
74 resource_provider_
->set_default_resource_type(ResourceProvider::Bitmap
);
76 capabilities_
.max_texture_size
= resource_provider_
->max_texture_size();
77 capabilities_
.best_texture_format
= resource_provider_
->best_texture_format();
78 capabilities_
.using_set_visibility
= true;
79 // The updater can access bitmaps while the SoftwareRenderer is using them.
80 capabilities_
.allow_partial_texture_updates
= true;
81 capabilities_
.using_partial_swap
= true;
82 if (Settings().compositor_frame_message
&& client_
->HasImplThread())
83 capabilities_
.using_swap_complete_callback
= true;
84 compositor_frame_
.software_frame_data
.reset(new SoftwareFrameData());
87 SoftwareRenderer::~SoftwareRenderer() {}
89 const RendererCapabilities
& SoftwareRenderer::Capabilities() const {
93 void SoftwareRenderer::ViewportChanged() {
94 is_viewport_changed_
= true;
97 void SoftwareRenderer::BeginDrawingFrame(DrawingFrame
* frame
) {
98 TRACE_EVENT0("cc", "SoftwareRenderer::BeginDrawingFrame");
99 if (is_viewport_changed_
) {
100 is_viewport_changed_
= false;
101 output_device_
->Resize(ViewportSize());
103 root_canvas_
= output_device_
->BeginPaint(
104 gfx::ToEnclosingRect(frame
->root_damage_rect
));
107 void SoftwareRenderer::FinishDrawingFrame(DrawingFrame
* frame
) {
108 TRACE_EVENT0("cc", "SoftwareRenderer::FinishDrawingFrame");
109 current_framebuffer_lock_
.reset();
110 current_canvas_
= NULL
;
112 if (Settings().compositor_frame_message
) {
113 compositor_frame_
.metadata
= client_
->MakeCompositorFrameMetadata();
114 output_device_
->EndPaint(compositor_frame_
.software_frame_data
.get());
116 output_device_
->EndPaint(NULL
);
120 bool SoftwareRenderer::SwapBuffers() {
121 if (Settings().compositor_frame_message
)
122 output_surface_
->SendFrameToParentCompositor(&compositor_frame_
);
126 void SoftwareRenderer::ReceiveCompositorFrameAck(
127 const CompositorFrameAck
& ack
) {
128 if (capabilities_
.using_swap_complete_callback
)
129 client_
->OnSwapBuffersComplete();
130 output_device_
->ReclaimDIB(ack
.last_dib_id
);
133 bool SoftwareRenderer::FlippedFramebuffer() const {
137 void SoftwareRenderer::EnsureScissorTestEnabled() {
138 is_scissor_enabled_
= true;
139 SetClipRect(scissor_rect_
);
142 void SoftwareRenderer::EnsureScissorTestDisabled() {
143 // There is no explicit notion of enabling/disabling scissoring in software
144 // rendering, but the underlying effect we want is to clear any existing
145 // clipRect on the current SkCanvas. This is done by setting clipRect to
146 // the viewport's dimensions.
147 is_scissor_enabled_
= false;
148 SkDevice
* device
= current_canvas_
->getDevice();
149 SetClipRect(gfx::Rect(device
->width(), device
->height()));
152 void SoftwareRenderer::Finish() {}
154 void SoftwareRenderer::BindFramebufferToOutputSurface(DrawingFrame
* frame
) {
155 current_framebuffer_lock_
.reset();
156 current_canvas_
= root_canvas_
;
159 bool SoftwareRenderer::BindFramebufferToTexture(
161 const ScopedResource
* texture
,
162 gfx::Rect framebuffer_rect
) {
163 current_framebuffer_lock_
= make_scoped_ptr(
164 new ResourceProvider::ScopedWriteLockSoftware(
165 resource_provider_
, texture
->id()));
166 current_canvas_
= current_framebuffer_lock_
->sk_canvas();
167 InitializeMatrices(frame
, framebuffer_rect
, false);
168 SetDrawViewportSize(framebuffer_rect
.size());
173 void SoftwareRenderer::SetScissorTestRect(gfx::Rect scissor_rect
) {
174 is_scissor_enabled_
= true;
175 scissor_rect_
= scissor_rect
;
176 SetClipRect(scissor_rect
);
179 void SoftwareRenderer::SetClipRect(gfx::Rect rect
) {
180 // Skia applies the current matrix to clip rects so we reset it temporary.
181 SkMatrix current_matrix
= current_canvas_
->getTotalMatrix();
182 current_canvas_
->resetMatrix();
183 current_canvas_
->clipRect(gfx::RectToSkRect(rect
), SkRegion::kReplace_Op
);
184 current_canvas_
->setMatrix(current_matrix
);
187 void SoftwareRenderer::ClearCanvas(SkColor color
) {
188 // SkCanvas::clear doesn't respect the current clipping region
189 // so we SkCanvas::drawColor instead if scissoring is active.
190 if (is_scissor_enabled_
)
191 current_canvas_
->drawColor(color
, SkXfermode::kSrc_Mode
);
193 current_canvas_
->clear(color
);
196 void SoftwareRenderer::ClearFramebuffer(DrawingFrame
* frame
) {
197 if (frame
->current_render_pass
->has_transparent_background
) {
198 ClearCanvas(SkColorSetARGB(0, 0, 0, 0));
201 // On DEBUG builds, opaque render passes are cleared to blue
202 // to easily see regions that were not drawn on the screen.
203 ClearCanvas(SkColorSetARGB(255, 0, 0, 255));
208 void SoftwareRenderer::SetDrawViewportSize(gfx::Size viewport_size
) {}
210 bool SoftwareRenderer::IsSoftwareResource(
211 ResourceProvider::ResourceId resource_id
) const {
212 switch (resource_provider_
->GetResourceType(resource_id
)) {
213 case ResourceProvider::GLTexture
:
215 case ResourceProvider::Bitmap
:
219 LOG(FATAL
) << "Invalid resource type.";
223 void SoftwareRenderer::DoDrawQuad(DrawingFrame
* frame
, const DrawQuad
* quad
) {
224 TRACE_EVENT0("cc", "SoftwareRenderer::DoDrawQuad");
225 gfx::Transform quad_rect_matrix
;
226 QuadRectTransform(&quad_rect_matrix
, quad
->quadTransform(), quad
->rect
);
227 gfx::Transform contents_device_transform
=
228 frame
->window_matrix
* frame
->projection_matrix
* quad_rect_matrix
;
229 contents_device_transform
.FlattenTo2d();
230 SkMatrix sk_device_matrix
;
231 ToSkMatrix(&sk_device_matrix
, contents_device_transform
);
232 current_canvas_
->setMatrix(sk_device_matrix
);
234 current_paint_
.reset();
235 if (!IsScaleAndTranslate(sk_device_matrix
)) {
236 current_paint_
.setAntiAlias(true);
237 current_paint_
.setFilterBitmap(true);
240 if (quad
->ShouldDrawWithBlending()) {
241 current_paint_
.setAlpha(quad
->opacity() * 255);
242 current_paint_
.setXfermodeMode(SkXfermode::kSrcOver_Mode
);
244 current_paint_
.setXfermodeMode(SkXfermode::kSrc_Mode
);
247 switch (quad
->material
) {
248 case DrawQuad::DEBUG_BORDER
:
249 DrawDebugBorderQuad(frame
, DebugBorderDrawQuad::MaterialCast(quad
));
251 case DrawQuad::SOLID_COLOR
:
252 DrawSolidColorQuad(frame
, SolidColorDrawQuad::MaterialCast(quad
));
254 case DrawQuad::TEXTURE_CONTENT
:
255 DrawTextureQuad(frame
, TextureDrawQuad::MaterialCast(quad
));
257 case DrawQuad::TILED_CONTENT
:
258 DrawTileQuad(frame
, TileDrawQuad::MaterialCast(quad
));
260 case DrawQuad::RENDER_PASS
:
261 DrawRenderPassQuad(frame
, RenderPassDrawQuad::MaterialCast(quad
));
264 DrawUnsupportedQuad(frame
, quad
);
268 current_canvas_
->resetMatrix();
271 void SoftwareRenderer::DrawDebugBorderQuad(const DrawingFrame
* frame
,
272 const DebugBorderDrawQuad
* quad
) {
273 // We need to apply the matrix manually to have pixel-sized stroke width.
275 gfx::RectFToSkRect(QuadVertexRect()).toQuad(vertices
);
276 SkPoint transformed_vertices
[4];
277 current_canvas_
->getTotalMatrix().mapPoints(transformed_vertices
,
280 current_canvas_
->resetMatrix();
282 current_paint_
.setColor(quad
->color
);
283 current_paint_
.setAlpha(quad
->opacity() * SkColorGetA(quad
->color
));
284 current_paint_
.setStyle(SkPaint::kStroke_Style
);
285 current_paint_
.setStrokeWidth(quad
->width
);
286 current_canvas_
->drawPoints(SkCanvas::kPolygon_PointMode
,
287 4, transformed_vertices
, current_paint_
);
290 void SoftwareRenderer::DrawSolidColorQuad(const DrawingFrame
* frame
,
291 const SolidColorDrawQuad
* quad
) {
292 current_paint_
.setColor(quad
->color
);
293 current_paint_
.setAlpha(quad
->opacity() * SkColorGetA(quad
->color
));
294 current_canvas_
->drawRect(gfx::RectFToSkRect(QuadVertexRect()),
298 void SoftwareRenderer::DrawTextureQuad(const DrawingFrame
* frame
,
299 const TextureDrawQuad
* quad
) {
300 if (!IsSoftwareResource(quad
->resource_id
)) {
301 DrawUnsupportedQuad(frame
, quad
);
305 // FIXME: Add support for non-premultiplied alpha.
306 ResourceProvider::ScopedReadLockSoftware
lock(resource_provider_
,
308 const SkBitmap
* bitmap
= lock
.sk_bitmap();
309 gfx::RectF uv_rect
= gfx::ScaleRect(gfx::BoundingRect(quad
->uv_top_left
,
310 quad
->uv_bottom_right
),
313 SkRect sk_uv_rect
= gfx::RectFToSkRect(uv_rect
);
315 current_canvas_
->scale(1, -1);
316 current_canvas_
->drawBitmapRectToRect(*bitmap
, &sk_uv_rect
,
317 gfx::RectFToSkRect(QuadVertexRect()),
321 void SoftwareRenderer::DrawTileQuad(const DrawingFrame
* frame
,
322 const TileDrawQuad
* quad
) {
323 DCHECK(IsSoftwareResource(quad
->resource_id
));
324 ResourceProvider::ScopedReadLockSoftware
lock(resource_provider_
,
327 SkRect uv_rect
= gfx::RectFToSkRect(quad
->tex_coord_rect
);
328 current_paint_
.setFilterBitmap(true);
329 current_canvas_
->drawBitmapRectToRect(*lock
.sk_bitmap(), &uv_rect
,
330 gfx::RectFToSkRect(QuadVertexRect()),
334 void SoftwareRenderer::DrawRenderPassQuad(const DrawingFrame
* frame
,
335 const RenderPassDrawQuad
* quad
) {
336 CachedResource
* content_texture
=
337 render_pass_textures_
.get(quad
->render_pass_id
);
338 if (!content_texture
|| !content_texture
->id())
341 DCHECK(IsSoftwareResource(content_texture
->id()));
342 ResourceProvider::ScopedReadLockSoftware
lock(resource_provider_
,
343 content_texture
->id());
345 SkRect dest_rect
= gfx::RectFToSkRect(QuadVertexRect());
346 SkRect content_rect
= SkRect::MakeWH(quad
->rect
.width(), quad
->rect
.height());
348 SkMatrix content_mat
;
349 content_mat
.setRectToRect(content_rect
, dest_rect
,
350 SkMatrix::kFill_ScaleToFit
);
352 const SkBitmap
* content
= lock
.sk_bitmap();
353 skia::RefPtr
<SkShader
> shader
= skia::AdoptRef(
354 SkShader::CreateBitmapShader(*content
,
355 SkShader::kClamp_TileMode
,
356 SkShader::kClamp_TileMode
));
357 shader
->setLocalMatrix(content_mat
);
358 current_paint_
.setShader(shader
.get());
360 SkImageFilter
* filter
= quad
->filter
.get();
362 current_paint_
.setImageFilter(filter
);
364 if (quad
->mask_resource_id
) {
365 ResourceProvider::ScopedReadLockSoftware
mask_lock(resource_provider_
,
366 quad
->mask_resource_id
);
368 const SkBitmap
* mask
= mask_lock
.sk_bitmap();
370 SkRect mask_rect
= SkRect::MakeXYWH(
371 quad
->mask_uv_rect
.x() * mask
->width(),
372 quad
->mask_uv_rect
.y() * mask
->height(),
373 quad
->mask_uv_rect
.width() * mask
->width(),
374 quad
->mask_uv_rect
.height() * mask
->height());
377 mask_mat
.setRectToRect(mask_rect
, dest_rect
, SkMatrix::kFill_ScaleToFit
);
379 skia::RefPtr
<SkShader
> mask_shader
= skia::AdoptRef(
380 SkShader::CreateBitmapShader(*mask
,
381 SkShader::kClamp_TileMode
,
382 SkShader::kClamp_TileMode
));
383 mask_shader
->setLocalMatrix(mask_mat
);
386 mask_paint
.setShader(mask_shader
.get());
388 skia::RefPtr
<SkLayerRasterizer
> mask_rasterizer
=
389 skia::AdoptRef(new SkLayerRasterizer
);
390 mask_rasterizer
->addLayer(mask_paint
);
392 current_paint_
.setRasterizer(mask_rasterizer
.get());
393 current_canvas_
->drawRect(dest_rect
, current_paint_
);
395 // FIXME: Apply background filters and blend with content
396 current_canvas_
->drawRect(dest_rect
, current_paint_
);
400 void SoftwareRenderer::DrawUnsupportedQuad(const DrawingFrame
* frame
,
401 const DrawQuad
* quad
) {
403 current_paint_
.setColor(SK_ColorWHITE
);
405 current_paint_
.setColor(SK_ColorMAGENTA
);
407 current_paint_
.setAlpha(quad
->opacity() * 255);
408 current_canvas_
->drawRect(gfx::RectFToSkRect(QuadVertexRect()),
412 void SoftwareRenderer::GetFramebufferPixels(void* pixels
, gfx::Rect rect
) {
413 TRACE_EVENT0("cc", "SoftwareRenderer::GetFramebufferPixels");
414 SkBitmap subset_bitmap
;
415 output_device_
->CopyToBitmap(rect
, &subset_bitmap
);
416 subset_bitmap
.copyPixelsTo(pixels
,
417 4 * rect
.width() * rect
.height(),
421 void SoftwareRenderer::SetVisible(bool visible
) {
422 if (visible_
== visible
)