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/copy_output_request.h"
13 #include "cc/output/output_surface.h"
14 #include "cc/output/software_output_device.h"
15 #include "cc/quads/checkerboard_draw_quad.h"
16 #include "cc/quads/debug_border_draw_quad.h"
17 #include "cc/quads/picture_draw_quad.h"
18 #include "cc/quads/render_pass_draw_quad.h"
19 #include "cc/quads/solid_color_draw_quad.h"
20 #include "cc/quads/texture_draw_quad.h"
21 #include "cc/quads/tile_draw_quad.h"
22 #include "third_party/skia/include/core/SkCanvas.h"
23 #include "third_party/skia/include/core/SkColor.h"
24 #include "third_party/skia/include/core/SkDevice.h"
25 #include "third_party/skia/include/core/SkMatrix.h"
26 #include "third_party/skia/include/core/SkShader.h"
27 #include "third_party/skia/include/effects/SkLayerRasterizer.h"
28 #include "ui/gfx/rect_conversions.h"
29 #include "ui/gfx/skia_util.h"
30 #include "ui/gfx/transform.h"
36 static inline bool IsScalarNearlyInteger(SkScalar scalar
) {
37 return SkScalarNearlyZero(scalar
- SkScalarRound(scalar
));
40 bool IsScaleAndIntegerTranslate(const SkMatrix
& matrix
) {
41 return IsScalarNearlyInteger(matrix
[SkMatrix::kMTransX
]) &&
42 IsScalarNearlyInteger(matrix
[SkMatrix::kMTransY
]) &&
43 SkScalarNearlyZero(matrix
[SkMatrix::kMSkewX
]) &&
44 SkScalarNearlyZero(matrix
[SkMatrix::kMSkewY
]) &&
45 SkScalarNearlyZero(matrix
[SkMatrix::kMPersp0
]) &&
46 SkScalarNearlyZero(matrix
[SkMatrix::kMPersp1
]) &&
47 SkScalarNearlyZero(matrix
[SkMatrix::kMPersp2
] - 1.0f
);
50 } // anonymous namespace
52 scoped_ptr
<SoftwareRenderer
> SoftwareRenderer::Create(
53 RendererClient
* client
,
54 OutputSurface
* output_surface
,
55 ResourceProvider
* resource_provider
) {
56 return make_scoped_ptr(
57 new SoftwareRenderer(client
, output_surface
, resource_provider
));
60 SoftwareRenderer::SoftwareRenderer(RendererClient
* client
,
61 OutputSurface
* output_surface
,
62 ResourceProvider
* resource_provider
)
63 : DirectRenderer(client
, output_surface
, resource_provider
),
65 is_scissor_enabled_(false),
66 output_device_(output_surface
->software_device()),
67 current_canvas_(NULL
) {
68 if (resource_provider_
) {
69 capabilities_
.max_texture_size
= resource_provider_
->max_texture_size();
70 capabilities_
.best_texture_format
=
71 resource_provider_
->best_texture_format();
73 capabilities_
.using_set_visibility
= true;
74 // The updater can access bitmaps while the SoftwareRenderer is using them.
75 capabilities_
.allow_partial_texture_updates
= true;
76 capabilities_
.using_partial_swap
= true;
78 capabilities_
.using_map_image
= Settings().use_map_image
;
79 capabilities_
.using_shared_memory_resources
= true;
82 SoftwareRenderer::~SoftwareRenderer() {}
84 const RendererCapabilities
& SoftwareRenderer::Capabilities() const {
88 void SoftwareRenderer::BeginDrawingFrame(DrawingFrame
* frame
) {
89 TRACE_EVENT0("cc", "SoftwareRenderer::BeginDrawingFrame");
90 root_canvas_
= output_device_
->BeginPaint(
91 gfx::ToEnclosingRect(frame
->root_damage_rect
));
94 void SoftwareRenderer::FinishDrawingFrame(DrawingFrame
* frame
) {
95 TRACE_EVENT0("cc", "SoftwareRenderer::FinishDrawingFrame");
96 current_framebuffer_lock_
.reset();
97 current_canvas_
= NULL
;
100 current_frame_data_
.reset(new SoftwareFrameData
);
101 output_device_
->EndPaint(current_frame_data_
.get());
104 void SoftwareRenderer::SwapBuffers() {
105 CompositorFrame compositor_frame
;
106 compositor_frame
.metadata
= client_
->MakeCompositorFrameMetadata();
107 compositor_frame
.software_frame_data
= current_frame_data_
.Pass();
108 output_surface_
->SwapBuffers(&compositor_frame
);
111 void SoftwareRenderer::ReceiveSwapBuffersAck(const CompositorFrameAck
& ack
) {
112 output_device_
->ReclaimSoftwareFrame(ack
.last_software_frame_id
);
115 bool SoftwareRenderer::FlippedFramebuffer() const {
119 void SoftwareRenderer::EnsureScissorTestEnabled() {
120 is_scissor_enabled_
= true;
121 SetClipRect(scissor_rect_
);
124 void SoftwareRenderer::EnsureScissorTestDisabled() {
125 // There is no explicit notion of enabling/disabling scissoring in software
126 // rendering, but the underlying effect we want is to clear any existing
127 // clipRect on the current SkCanvas. This is done by setting clipRect to
128 // the viewport's dimensions.
129 is_scissor_enabled_
= false;
130 SkDevice
* device
= current_canvas_
->getDevice();
131 SetClipRect(gfx::Rect(device
->width(), device
->height()));
134 void SoftwareRenderer::Finish() {}
136 void SoftwareRenderer::BindFramebufferToOutputSurface(DrawingFrame
* frame
) {
137 DCHECK(!client_
->ExternalStencilTestEnabled());
138 current_framebuffer_lock_
.reset();
139 current_canvas_
= root_canvas_
;
142 bool SoftwareRenderer::BindFramebufferToTexture(
144 const ScopedResource
* texture
,
145 gfx::Rect target_rect
) {
146 current_framebuffer_lock_
.reset();
147 current_framebuffer_lock_
= make_scoped_ptr(
148 new ResourceProvider::ScopedWriteLockSoftware(
149 resource_provider_
, texture
->id()));
150 current_canvas_
= current_framebuffer_lock_
->sk_canvas();
151 InitializeViewport(frame
,
153 gfx::Rect(target_rect
.size()),
158 void SoftwareRenderer::SetScissorTestRect(gfx::Rect scissor_rect
) {
159 is_scissor_enabled_
= true;
160 scissor_rect_
= scissor_rect
;
161 SetClipRect(scissor_rect
);
164 void SoftwareRenderer::SetClipRect(gfx::Rect rect
) {
165 // Skia applies the current matrix to clip rects so we reset it temporary.
166 SkMatrix current_matrix
= current_canvas_
->getTotalMatrix();
167 current_canvas_
->resetMatrix();
168 current_canvas_
->clipRect(gfx::RectToSkRect(rect
), SkRegion::kReplace_Op
);
169 current_canvas_
->setMatrix(current_matrix
);
172 void SoftwareRenderer::ClearCanvas(SkColor color
) {
173 // SkCanvas::clear doesn't respect the current clipping region
174 // so we SkCanvas::drawColor instead if scissoring is active.
175 if (is_scissor_enabled_
)
176 current_canvas_
->drawColor(color
, SkXfermode::kSrc_Mode
);
178 current_canvas_
->clear(color
);
181 void SoftwareRenderer::ClearFramebuffer(DrawingFrame
* frame
) {
182 if (frame
->current_render_pass
->has_transparent_background
) {
183 ClearCanvas(SkColorSetARGB(0, 0, 0, 0));
186 // On DEBUG builds, opaque render passes are cleared to blue
187 // to easily see regions that were not drawn on the screen.
188 ClearCanvas(SkColorSetARGB(255, 0, 0, 255));
193 void SoftwareRenderer::SetDrawViewport(gfx::Rect window_space_viewport
) {}
195 bool SoftwareRenderer::IsSoftwareResource(
196 ResourceProvider::ResourceId resource_id
) const {
197 switch (resource_provider_
->GetResourceType(resource_id
)) {
198 case ResourceProvider::GLTexture
:
200 case ResourceProvider::Bitmap
:
202 case ResourceProvider::InvalidType
:
206 LOG(FATAL
) << "Invalid resource type.";
210 void SoftwareRenderer::DoDrawQuad(DrawingFrame
* frame
, const DrawQuad
* quad
) {
211 TRACE_EVENT0("cc", "SoftwareRenderer::DoDrawQuad");
212 gfx::Transform quad_rect_matrix
;
213 QuadRectTransform(&quad_rect_matrix
, quad
->quadTransform(), quad
->rect
);
214 gfx::Transform contents_device_transform
=
215 frame
->window_matrix
* frame
->projection_matrix
* quad_rect_matrix
;
216 contents_device_transform
.FlattenTo2d();
217 SkMatrix sk_device_matrix
;
218 gfx::TransformToFlattenedSkMatrix(contents_device_transform
,
220 current_canvas_
->setMatrix(sk_device_matrix
);
222 current_paint_
.reset();
223 if (!IsScaleAndIntegerTranslate(sk_device_matrix
)) {
224 // TODO(danakj): Until we can enable AA only on exterior edges of the
225 // layer, disable AA if any interior edges are present. crbug.com/248175
226 bool all_four_edges_are_exterior
= quad
->IsTopEdge() &&
227 quad
->IsLeftEdge() &&
228 quad
->IsBottomEdge() &&
230 if (Settings().allow_antialiasing
&&
231 all_four_edges_are_exterior
)
232 current_paint_
.setAntiAlias(true);
233 current_paint_
.setFilterBitmap(true);
236 if (quad
->ShouldDrawWithBlending()) {
237 current_paint_
.setAlpha(quad
->opacity() * 255);
238 current_paint_
.setXfermodeMode(SkXfermode::kSrcOver_Mode
);
240 current_paint_
.setXfermodeMode(SkXfermode::kSrc_Mode
);
243 switch (quad
->material
) {
244 case DrawQuad::CHECKERBOARD
:
245 DrawCheckerboardQuad(frame
, CheckerboardDrawQuad::MaterialCast(quad
));
247 case DrawQuad::DEBUG_BORDER
:
248 DrawDebugBorderQuad(frame
, DebugBorderDrawQuad::MaterialCast(quad
));
250 case DrawQuad::PICTURE_CONTENT
:
251 DrawPictureQuad(frame
, PictureDrawQuad::MaterialCast(quad
));
253 case DrawQuad::RENDER_PASS
:
254 DrawRenderPassQuad(frame
, RenderPassDrawQuad::MaterialCast(quad
));
256 case DrawQuad::SOLID_COLOR
:
257 DrawSolidColorQuad(frame
, SolidColorDrawQuad::MaterialCast(quad
));
259 case DrawQuad::TEXTURE_CONTENT
:
260 DrawTextureQuad(frame
, TextureDrawQuad::MaterialCast(quad
));
262 case DrawQuad::TILED_CONTENT
:
263 DrawTileQuad(frame
, TileDrawQuad::MaterialCast(quad
));
265 case DrawQuad::INVALID
:
266 case DrawQuad::IO_SURFACE_CONTENT
:
267 case DrawQuad::YUV_VIDEO_CONTENT
:
268 case DrawQuad::STREAM_VIDEO_CONTENT
:
269 DrawUnsupportedQuad(frame
, quad
);
274 current_canvas_
->resetMatrix();
277 void SoftwareRenderer::DrawCheckerboardQuad(const DrawingFrame
* frame
,
278 const CheckerboardDrawQuad
* quad
) {
279 current_paint_
.setColor(quad
->color
);
280 current_paint_
.setAlpha(quad
->opacity() * SkColorGetA(quad
->color
));
281 current_canvas_
->drawRect(gfx::RectFToSkRect(QuadVertexRect()),
285 void SoftwareRenderer::DrawDebugBorderQuad(const DrawingFrame
* frame
,
286 const DebugBorderDrawQuad
* quad
) {
287 // We need to apply the matrix manually to have pixel-sized stroke width.
289 gfx::RectFToSkRect(QuadVertexRect()).toQuad(vertices
);
290 SkPoint transformed_vertices
[4];
291 current_canvas_
->getTotalMatrix().mapPoints(transformed_vertices
,
294 current_canvas_
->resetMatrix();
296 current_paint_
.setColor(quad
->color
);
297 current_paint_
.setAlpha(quad
->opacity() * SkColorGetA(quad
->color
));
298 current_paint_
.setStyle(SkPaint::kStroke_Style
);
299 current_paint_
.setStrokeWidth(quad
->width
);
300 current_canvas_
->drawPoints(SkCanvas::kPolygon_PointMode
,
301 4, transformed_vertices
, current_paint_
);
304 void SoftwareRenderer::DrawPictureQuad(const DrawingFrame
* frame
,
305 const PictureDrawQuad
* quad
) {
306 SkMatrix content_matrix
;
307 content_matrix
.setRectToRect(
308 gfx::RectFToSkRect(quad
->tex_coord_rect
),
309 gfx::RectFToSkRect(QuadVertexRect()),
310 SkMatrix::kFill_ScaleToFit
);
311 current_canvas_
->concat(content_matrix
);
313 if (quad
->ShouldDrawWithBlending()) {
314 TRACE_EVENT0("cc", "SoftwareRenderer::DrawPictureQuad with blending");
315 SkBitmap temp_bitmap
;
316 temp_bitmap
.setConfig(SkBitmap::kARGB_8888_Config
,
317 quad
->texture_size
.width(),
318 quad
->texture_size
.height());
319 temp_bitmap
.allocPixels();
320 SkDevice
temp_device(temp_bitmap
);
321 SkCanvas
temp_canvas(&temp_device
);
323 quad
->picture_pile
->RasterToBitmap(
324 &temp_canvas
, quad
->content_rect
, quad
->contents_scale
, NULL
);
326 current_paint_
.setFilterBitmap(true);
327 current_canvas_
->drawBitmap(temp_bitmap
, 0, 0, ¤t_paint_
);
330 "SoftwareRenderer::DrawPictureQuad direct from PicturePile");
331 quad
->picture_pile
->RasterDirect(
332 current_canvas_
, quad
->content_rect
, quad
->contents_scale
, NULL
);
336 void SoftwareRenderer::DrawSolidColorQuad(const DrawingFrame
* frame
,
337 const SolidColorDrawQuad
* quad
) {
338 current_paint_
.setColor(quad
->color
);
339 current_paint_
.setAlpha(quad
->opacity() * SkColorGetA(quad
->color
));
340 current_canvas_
->drawRect(gfx::RectFToSkRect(QuadVertexRect()),
344 void SoftwareRenderer::DrawTextureQuad(const DrawingFrame
* frame
,
345 const TextureDrawQuad
* quad
) {
346 if (!IsSoftwareResource(quad
->resource_id
)) {
347 DrawUnsupportedQuad(frame
, quad
);
351 // TODO(skaslev): Add support for non-premultiplied alpha.
352 ResourceProvider::ScopedReadLockSoftware
lock(resource_provider_
,
354 const SkBitmap
* bitmap
= lock
.sk_bitmap();
355 gfx::RectF uv_rect
= gfx::ScaleRect(gfx::BoundingRect(quad
->uv_top_left
,
356 quad
->uv_bottom_right
),
359 SkRect sk_uv_rect
= gfx::RectFToSkRect(uv_rect
);
360 SkRect quad_rect
= gfx::RectFToSkRect(QuadVertexRect());
363 current_canvas_
->scale(1, -1);
365 bool blend_background
= quad
->background_color
!= SK_ColorTRANSPARENT
&&
367 bool needs_layer
= blend_background
&& (current_paint_
.getAlpha() != 0xFF);
369 current_canvas_
->saveLayerAlpha(&quad_rect
, current_paint_
.getAlpha());
370 current_paint_
.setAlpha(0xFF);
372 if (blend_background
) {
373 SkPaint background_paint
;
374 background_paint
.setColor(quad
->background_color
);
375 current_canvas_
->drawRect(quad_rect
, background_paint
);
378 current_canvas_
->drawBitmapRectToRect(*bitmap
,
384 current_canvas_
->restore();
387 void SoftwareRenderer::DrawTileQuad(const DrawingFrame
* frame
,
388 const TileDrawQuad
* quad
) {
389 DCHECK(!output_surface_
->ForcedDrawToSoftwareDevice());
390 DCHECK(IsSoftwareResource(quad
->resource_id
));
391 ResourceProvider::ScopedReadLockSoftware
lock(resource_provider_
,
394 SkRect uv_rect
= gfx::RectFToSkRect(quad
->tex_coord_rect
);
395 current_paint_
.setFilterBitmap(true);
396 current_canvas_
->drawBitmapRectToRect(*lock
.sk_bitmap(), &uv_rect
,
397 gfx::RectFToSkRect(QuadVertexRect()),
401 void SoftwareRenderer::DrawRenderPassQuad(const DrawingFrame
* frame
,
402 const RenderPassDrawQuad
* quad
) {
403 CachedResource
* content_texture
=
404 render_pass_textures_
.get(quad
->render_pass_id
);
405 if (!content_texture
|| !content_texture
->id())
408 DCHECK(IsSoftwareResource(content_texture
->id()));
409 ResourceProvider::ScopedReadLockSoftware
lock(resource_provider_
,
410 content_texture
->id());
412 SkRect dest_rect
= gfx::RectFToSkRect(QuadVertexRect());
413 SkRect content_rect
= SkRect::MakeWH(quad
->rect
.width(), quad
->rect
.height());
415 SkMatrix content_mat
;
416 content_mat
.setRectToRect(content_rect
, dest_rect
,
417 SkMatrix::kFill_ScaleToFit
);
419 const SkBitmap
* content
= lock
.sk_bitmap();
420 skia::RefPtr
<SkShader
> shader
= skia::AdoptRef(
421 SkShader::CreateBitmapShader(*content
,
422 SkShader::kClamp_TileMode
,
423 SkShader::kClamp_TileMode
));
424 shader
->setLocalMatrix(content_mat
);
425 current_paint_
.setShader(shader
.get());
427 SkImageFilter
* filter
= quad
->filter
.get();
429 current_paint_
.setImageFilter(filter
);
431 if (quad
->mask_resource_id
) {
432 ResourceProvider::ScopedReadLockSoftware
mask_lock(resource_provider_
,
433 quad
->mask_resource_id
);
435 const SkBitmap
* mask
= mask_lock
.sk_bitmap();
437 SkRect mask_rect
= SkRect::MakeXYWH(
438 quad
->mask_uv_rect
.x() * mask
->width(),
439 quad
->mask_uv_rect
.y() * mask
->height(),
440 quad
->mask_uv_rect
.width() * mask
->width(),
441 quad
->mask_uv_rect
.height() * mask
->height());
444 mask_mat
.setRectToRect(mask_rect
, dest_rect
, SkMatrix::kFill_ScaleToFit
);
446 skia::RefPtr
<SkShader
> mask_shader
= skia::AdoptRef(
447 SkShader::CreateBitmapShader(*mask
,
448 SkShader::kClamp_TileMode
,
449 SkShader::kClamp_TileMode
));
450 mask_shader
->setLocalMatrix(mask_mat
);
453 mask_paint
.setShader(mask_shader
.get());
455 skia::RefPtr
<SkLayerRasterizer
> mask_rasterizer
=
456 skia::AdoptRef(new SkLayerRasterizer
);
457 mask_rasterizer
->addLayer(mask_paint
);
459 current_paint_
.setRasterizer(mask_rasterizer
.get());
460 current_canvas_
->drawRect(dest_rect
, current_paint_
);
462 // TODO(skaslev): Apply background filters and blend with content
463 current_canvas_
->drawRect(dest_rect
, current_paint_
);
467 void SoftwareRenderer::DrawUnsupportedQuad(const DrawingFrame
* frame
,
468 const DrawQuad
* quad
) {
470 current_paint_
.setColor(SK_ColorWHITE
);
472 current_paint_
.setColor(SK_ColorMAGENTA
);
474 current_paint_
.setAlpha(quad
->opacity() * 255);
475 current_canvas_
->drawRect(gfx::RectFToSkRect(QuadVertexRect()),
479 void SoftwareRenderer::CopyCurrentRenderPassToBitmap(
481 scoped_ptr
<CopyOutputRequest
> request
) {
482 gfx::Rect copy_rect
= frame
->current_render_pass
->output_rect
;
483 if (request
->has_area()) {
484 // Intersect with the request's area, positioned with its origin at the
485 // origin of the full copy_rect.
486 copy_rect
.Intersect(request
->area() - copy_rect
.OffsetFromOrigin());
488 gfx::Rect window_copy_rect
= MoveFromDrawToWindowSpace(copy_rect
);
490 scoped_ptr
<SkBitmap
> bitmap(new SkBitmap
);
491 bitmap
->setConfig(SkBitmap::kARGB_8888_Config
,
492 window_copy_rect
.width(),
493 window_copy_rect
.height());
494 current_canvas_
->readPixels(
495 bitmap
.get(), window_copy_rect
.x(), window_copy_rect
.y());
497 request
->SendBitmapResult(bitmap
.Pass());
500 void SoftwareRenderer::GetFramebufferPixels(void* pixels
, gfx::Rect rect
) {
501 TRACE_EVENT0("cc", "SoftwareRenderer::GetFramebufferPixels");
502 SkBitmap subset_bitmap
;
503 rect
+= current_viewport_rect_
.OffsetFromOrigin();
504 output_device_
->CopyToBitmap(rect
, &subset_bitmap
);
505 subset_bitmap
.copyPixelsTo(pixels
,
506 4 * rect
.width() * rect
.height(),
510 void SoftwareRenderer::SetVisible(bool visible
) {
511 if (visible_
== visible
)
516 void SoftwareRenderer::SetDiscardBackBufferWhenNotVisible(bool discard
) {
517 // TODO(piman, skaslev): Can we release the backbuffer? We don't currently
518 // receive memory policy yet anyway.