Move prefs::kLastPolicyStatisticsUpdate to the policy component.
[chromium-blink-merge.git] / cc / output / software_renderer.cc
blob394d79a7a35b290cea4ea8ce789837223bbd7833
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 "skia/ext/opacity_draw_filter.h"
23 #include "third_party/skia/include/core/SkCanvas.h"
24 #include "third_party/skia/include/core/SkColor.h"
25 #include "third_party/skia/include/core/SkImageFilter.h"
26 #include "third_party/skia/include/core/SkMatrix.h"
27 #include "third_party/skia/include/core/SkShader.h"
28 #include "third_party/skia/include/effects/SkLayerRasterizer.h"
29 #include "ui/gfx/rect_conversions.h"
30 #include "ui/gfx/skia_util.h"
31 #include "ui/gfx/transform.h"
33 namespace cc {
35 namespace {
37 static inline bool IsScalarNearlyInteger(SkScalar scalar) {
38 return SkScalarNearlyZero(scalar - SkScalarRound(scalar));
41 bool IsScaleAndIntegerTranslate(const SkMatrix& matrix) {
42 return IsScalarNearlyInteger(matrix[SkMatrix::kMTransX]) &&
43 IsScalarNearlyInteger(matrix[SkMatrix::kMTransY]) &&
44 SkScalarNearlyZero(matrix[SkMatrix::kMSkewX]) &&
45 SkScalarNearlyZero(matrix[SkMatrix::kMSkewY]) &&
46 SkScalarNearlyZero(matrix[SkMatrix::kMPersp0]) &&
47 SkScalarNearlyZero(matrix[SkMatrix::kMPersp1]) &&
48 SkScalarNearlyZero(matrix[SkMatrix::kMPersp2] - 1.0f);
51 } // anonymous namespace
53 scoped_ptr<SoftwareRenderer> SoftwareRenderer::Create(
54 RendererClient* client,
55 const LayerTreeSettings* settings,
56 OutputSurface* output_surface,
57 ResourceProvider* resource_provider) {
58 return make_scoped_ptr(new SoftwareRenderer(
59 client, settings, output_surface, resource_provider));
62 SoftwareRenderer::SoftwareRenderer(RendererClient* client,
63 const LayerTreeSettings* settings,
64 OutputSurface* output_surface,
65 ResourceProvider* resource_provider)
66 : DirectRenderer(client, settings, output_surface, resource_provider),
67 visible_(true),
68 is_scissor_enabled_(false),
69 is_backbuffer_discarded_(false),
70 output_device_(output_surface->software_device()),
71 current_canvas_(NULL) {
72 if (resource_provider_) {
73 capabilities_.max_texture_size = resource_provider_->max_texture_size();
74 capabilities_.best_texture_format =
75 resource_provider_->best_texture_format();
77 capabilities_.using_set_visibility = true;
78 // The updater can access bitmaps while the SoftwareRenderer is using them.
79 capabilities_.allow_partial_texture_updates = true;
80 capabilities_.using_partial_swap = true;
82 capabilities_.using_map_image = settings_->use_map_image;
83 capabilities_.using_shared_memory_resources = true;
86 SoftwareRenderer::~SoftwareRenderer() {}
88 const RendererCapabilities& SoftwareRenderer::Capabilities() const {
89 return capabilities_;
92 void SoftwareRenderer::BeginDrawingFrame(DrawingFrame* frame) {
93 TRACE_EVENT0("cc", "SoftwareRenderer::BeginDrawingFrame");
94 root_canvas_ = output_device_->BeginPaint(
95 gfx::ToEnclosingRect(frame->root_damage_rect));
98 void SoftwareRenderer::FinishDrawingFrame(DrawingFrame* frame) {
99 TRACE_EVENT0("cc", "SoftwareRenderer::FinishDrawingFrame");
100 current_framebuffer_lock_.reset();
101 current_canvas_ = NULL;
102 root_canvas_ = NULL;
104 current_frame_data_.reset(new SoftwareFrameData);
105 output_device_->EndPaint(current_frame_data_.get());
108 void SoftwareRenderer::SwapBuffers() {
109 CompositorFrame compositor_frame;
110 compositor_frame.metadata = client_->MakeCompositorFrameMetadata();
111 compositor_frame.software_frame_data = current_frame_data_.Pass();
112 output_surface_->SwapBuffers(&compositor_frame);
115 void SoftwareRenderer::ReceiveSwapBuffersAck(const CompositorFrameAck& ack) {
116 output_device_->ReclaimSoftwareFrame(ack.last_software_frame_id);
119 bool SoftwareRenderer::FlippedFramebuffer() const {
120 return false;
123 void SoftwareRenderer::EnsureScissorTestEnabled() {
124 is_scissor_enabled_ = true;
125 SetClipRect(scissor_rect_);
128 void SoftwareRenderer::EnsureScissorTestDisabled() {
129 // There is no explicit notion of enabling/disabling scissoring in software
130 // rendering, but the underlying effect we want is to clear any existing
131 // clipRect on the current SkCanvas. This is done by setting clipRect to
132 // the viewport's dimensions.
133 is_scissor_enabled_ = false;
134 SkBaseDevice* device = current_canvas_->getDevice();
135 SetClipRect(gfx::Rect(device->width(), device->height()));
138 void SoftwareRenderer::Finish() {}
140 void SoftwareRenderer::BindFramebufferToOutputSurface(DrawingFrame* frame) {
141 DCHECK(!output_surface_->HasExternalStencilTest());
142 current_framebuffer_lock_.reset();
143 current_canvas_ = root_canvas_;
146 bool SoftwareRenderer::BindFramebufferToTexture(
147 DrawingFrame* frame,
148 const ScopedResource* texture,
149 gfx::Rect target_rect) {
150 current_framebuffer_lock_.reset();
151 current_framebuffer_lock_ = make_scoped_ptr(
152 new ResourceProvider::ScopedWriteLockSoftware(
153 resource_provider_, texture->id()));
154 current_canvas_ = current_framebuffer_lock_->sk_canvas();
155 InitializeViewport(frame,
156 target_rect,
157 gfx::Rect(target_rect.size()),
158 target_rect.size());
159 return true;
162 void SoftwareRenderer::SetScissorTestRect(gfx::Rect scissor_rect) {
163 is_scissor_enabled_ = true;
164 scissor_rect_ = scissor_rect;
165 SetClipRect(scissor_rect);
168 void SoftwareRenderer::SetClipRect(gfx::Rect rect) {
169 // Skia applies the current matrix to clip rects so we reset it temporary.
170 SkMatrix current_matrix = current_canvas_->getTotalMatrix();
171 current_canvas_->resetMatrix();
172 current_canvas_->clipRect(gfx::RectToSkRect(rect), SkRegion::kReplace_Op);
173 current_canvas_->setMatrix(current_matrix);
176 void SoftwareRenderer::ClearCanvas(SkColor color) {
177 // SkCanvas::clear doesn't respect the current clipping region
178 // so we SkCanvas::drawColor instead if scissoring is active.
179 if (is_scissor_enabled_)
180 current_canvas_->drawColor(color, SkXfermode::kSrc_Mode);
181 else
182 current_canvas_->clear(color);
185 void SoftwareRenderer::ClearFramebuffer(DrawingFrame* frame) {
186 if (frame->current_render_pass->has_transparent_background) {
187 ClearCanvas(SkColorSetARGB(0, 0, 0, 0));
188 } else {
189 #ifndef NDEBUG
190 // On DEBUG builds, opaque render passes are cleared to blue
191 // to easily see regions that were not drawn on the screen.
192 ClearCanvas(SkColorSetARGB(255, 0, 0, 255));
193 #endif
197 void SoftwareRenderer::SetDrawViewport(gfx::Rect window_space_viewport) {}
199 bool SoftwareRenderer::IsSoftwareResource(
200 ResourceProvider::ResourceId resource_id) const {
201 switch (resource_provider_->GetResourceType(resource_id)) {
202 case ResourceProvider::GLTexture:
203 return false;
204 case ResourceProvider::Bitmap:
205 return true;
206 case ResourceProvider::InvalidType:
207 break;
210 LOG(FATAL) << "Invalid resource type.";
211 return false;
214 void SoftwareRenderer::DoDrawQuad(DrawingFrame* frame, const DrawQuad* quad) {
215 TRACE_EVENT0("cc", "SoftwareRenderer::DoDrawQuad");
216 gfx::Transform quad_rect_matrix;
217 QuadRectTransform(&quad_rect_matrix, quad->quadTransform(), quad->rect);
218 gfx::Transform contents_device_transform =
219 frame->window_matrix * frame->projection_matrix * quad_rect_matrix;
220 contents_device_transform.FlattenTo2d();
221 SkMatrix sk_device_matrix;
222 gfx::TransformToFlattenedSkMatrix(contents_device_transform,
223 &sk_device_matrix);
224 current_canvas_->setMatrix(sk_device_matrix);
226 current_paint_.reset();
227 if (!IsScaleAndIntegerTranslate(sk_device_matrix)) {
228 // TODO(danakj): Until we can enable AA only on exterior edges of the
229 // layer, disable AA if any interior edges are present. crbug.com/248175
230 bool all_four_edges_are_exterior = quad->IsTopEdge() &&
231 quad->IsLeftEdge() &&
232 quad->IsBottomEdge() &&
233 quad->IsRightEdge();
234 if (settings_->allow_antialiasing && all_four_edges_are_exterior)
235 current_paint_.setAntiAlias(true);
236 current_paint_.setFilterBitmap(true);
239 if (quad->ShouldDrawWithBlending()) {
240 current_paint_.setAlpha(quad->opacity() * 255);
241 current_paint_.setXfermodeMode(SkXfermode::kSrcOver_Mode);
242 } else {
243 current_paint_.setXfermodeMode(SkXfermode::kSrc_Mode);
246 switch (quad->material) {
247 case DrawQuad::CHECKERBOARD:
248 DrawCheckerboardQuad(frame, CheckerboardDrawQuad::MaterialCast(quad));
249 break;
250 case DrawQuad::DEBUG_BORDER:
251 DrawDebugBorderQuad(frame, DebugBorderDrawQuad::MaterialCast(quad));
252 break;
253 case DrawQuad::PICTURE_CONTENT:
254 DrawPictureQuad(frame, PictureDrawQuad::MaterialCast(quad));
255 break;
256 case DrawQuad::RENDER_PASS:
257 DrawRenderPassQuad(frame, RenderPassDrawQuad::MaterialCast(quad));
258 break;
259 case DrawQuad::SOLID_COLOR:
260 DrawSolidColorQuad(frame, SolidColorDrawQuad::MaterialCast(quad));
261 break;
262 case DrawQuad::TEXTURE_CONTENT:
263 DrawTextureQuad(frame, TextureDrawQuad::MaterialCast(quad));
264 break;
265 case DrawQuad::TILED_CONTENT:
266 DrawTileQuad(frame, TileDrawQuad::MaterialCast(quad));
267 break;
268 case DrawQuad::INVALID:
269 case DrawQuad::IO_SURFACE_CONTENT:
270 case DrawQuad::YUV_VIDEO_CONTENT:
271 case DrawQuad::STREAM_VIDEO_CONTENT:
272 DrawUnsupportedQuad(frame, quad);
273 NOTREACHED();
274 break;
277 current_canvas_->resetMatrix();
280 void SoftwareRenderer::DrawCheckerboardQuad(const DrawingFrame* frame,
281 const CheckerboardDrawQuad* quad) {
282 gfx::RectF visible_quad_vertex_rect = MathUtil::ScaleRectProportional(
283 QuadVertexRect(), quad->rect, quad->visible_rect);
284 current_paint_.setColor(quad->color);
285 current_paint_.setAlpha(quad->opacity() * SkColorGetA(quad->color));
286 current_canvas_->drawRect(gfx::RectFToSkRect(visible_quad_vertex_rect),
287 current_paint_);
290 void SoftwareRenderer::DrawDebugBorderQuad(const DrawingFrame* frame,
291 const DebugBorderDrawQuad* quad) {
292 // We need to apply the matrix manually to have pixel-sized stroke width.
293 SkPoint vertices[4];
294 gfx::RectFToSkRect(QuadVertexRect()).toQuad(vertices);
295 SkPoint transformed_vertices[4];
296 current_canvas_->getTotalMatrix().mapPoints(transformed_vertices,
297 vertices,
299 current_canvas_->resetMatrix();
301 current_paint_.setColor(quad->color);
302 current_paint_.setAlpha(quad->opacity() * SkColorGetA(quad->color));
303 current_paint_.setStyle(SkPaint::kStroke_Style);
304 current_paint_.setStrokeWidth(quad->width);
305 current_canvas_->drawPoints(SkCanvas::kPolygon_PointMode,
306 4, transformed_vertices, current_paint_);
309 void SoftwareRenderer::DrawPictureQuad(const DrawingFrame* frame,
310 const PictureDrawQuad* quad) {
311 SkMatrix content_matrix;
312 content_matrix.setRectToRect(
313 gfx::RectFToSkRect(quad->tex_coord_rect),
314 gfx::RectFToSkRect(QuadVertexRect()),
315 SkMatrix::kFill_ScaleToFit);
316 current_canvas_->concat(content_matrix);
318 // TODO(aelias): This isn't correct in all cases. We should detect these
319 // cases and fall back to a persistent bitmap backing
320 // (http://crbug.com/280374).
321 skia::RefPtr<SkDrawFilter> opacity_filter =
322 skia::AdoptRef(new skia::OpacityDrawFilter(quad->opacity(), true));
323 DCHECK(!current_canvas_->getDrawFilter());
324 current_canvas_->setDrawFilter(opacity_filter.get());
326 TRACE_EVENT0("cc",
327 "SoftwareRenderer::DrawPictureQuad");
328 quad->picture_pile->RasterDirect(
329 current_canvas_, quad->content_rect, quad->contents_scale, NULL);
331 current_canvas_->setDrawFilter(NULL);
334 void SoftwareRenderer::DrawSolidColorQuad(const DrawingFrame* frame,
335 const SolidColorDrawQuad* quad) {
336 gfx::RectF visible_quad_vertex_rect = MathUtil::ScaleRectProportional(
337 QuadVertexRect(), quad->rect, quad->visible_rect);
338 current_paint_.setColor(quad->color);
339 current_paint_.setAlpha(quad->opacity() * SkColorGetA(quad->color));
340 current_canvas_->drawRect(gfx::RectFToSkRect(visible_quad_vertex_rect),
341 current_paint_);
344 void SoftwareRenderer::DrawTextureQuad(const DrawingFrame* frame,
345 const TextureDrawQuad* quad) {
346 if (!IsSoftwareResource(quad->resource_id)) {
347 DrawUnsupportedQuad(frame, quad);
348 return;
351 // TODO(skaslev): Add support for non-premultiplied alpha.
352 ResourceProvider::ScopedReadLockSoftware lock(resource_provider_,
353 quad->resource_id);
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),
357 bitmap->width(),
358 bitmap->height());
359 gfx::RectF visible_uv_rect =
360 MathUtil::ScaleRectProportional(uv_rect, quad->rect, quad->visible_rect);
361 SkRect sk_uv_rect = gfx::RectFToSkRect(visible_uv_rect);
362 gfx::RectF visible_quad_vertex_rect = MathUtil::ScaleRectProportional(
363 QuadVertexRect(), quad->rect, quad->visible_rect);
364 SkRect quad_rect = gfx::RectFToSkRect(visible_quad_vertex_rect);
366 if (quad->flipped)
367 current_canvas_->scale(1, -1);
369 bool blend_background = quad->background_color != SK_ColorTRANSPARENT &&
370 !bitmap->isOpaque();
371 bool needs_layer = blend_background && (current_paint_.getAlpha() != 0xFF);
372 if (needs_layer) {
373 current_canvas_->saveLayerAlpha(&quad_rect, current_paint_.getAlpha());
374 current_paint_.setAlpha(0xFF);
376 if (blend_background) {
377 SkPaint background_paint;
378 background_paint.setColor(quad->background_color);
379 current_canvas_->drawRect(quad_rect, background_paint);
382 current_canvas_->drawBitmapRectToRect(*bitmap,
383 &sk_uv_rect,
384 quad_rect,
385 &current_paint_);
387 if (needs_layer)
388 current_canvas_->restore();
391 void SoftwareRenderer::DrawTileQuad(const DrawingFrame* frame,
392 const TileDrawQuad* quad) {
393 DCHECK(!output_surface_->ForcedDrawToSoftwareDevice());
394 DCHECK(IsSoftwareResource(quad->resource_id));
395 ResourceProvider::ScopedReadLockSoftware lock(resource_provider_,
396 quad->resource_id);
397 gfx::RectF visible_tex_coord_rect = MathUtil::ScaleRectProportional(
398 quad->tex_coord_rect, quad->rect, quad->visible_rect);
399 gfx::RectF visible_quad_vertex_rect = MathUtil::ScaleRectProportional(
400 QuadVertexRect(), quad->rect, quad->visible_rect);
402 SkRect uv_rect = gfx::RectFToSkRect(visible_tex_coord_rect);
403 current_paint_.setFilterBitmap(true);
404 current_canvas_->drawBitmapRectToRect(
405 *lock.sk_bitmap(),
406 &uv_rect,
407 gfx::RectFToSkRect(visible_quad_vertex_rect),
408 &current_paint_);
411 void SoftwareRenderer::DrawRenderPassQuad(const DrawingFrame* frame,
412 const RenderPassDrawQuad* quad) {
413 CachedResource* content_texture =
414 render_pass_textures_.get(quad->render_pass_id);
415 if (!content_texture || !content_texture->id())
416 return;
418 DCHECK(IsSoftwareResource(content_texture->id()));
419 ResourceProvider::ScopedReadLockSoftware lock(resource_provider_,
420 content_texture->id());
422 SkRect dest_rect = gfx::RectFToSkRect(QuadVertexRect());
423 SkRect dest_visible_rect = gfx::RectFToSkRect(MathUtil::ScaleRectProportional(
424 QuadVertexRect(), quad->rect, quad->visible_rect));
425 SkRect content_rect = SkRect::MakeWH(quad->rect.width(), quad->rect.height());
427 SkMatrix content_mat;
428 content_mat.setRectToRect(content_rect, dest_rect,
429 SkMatrix::kFill_ScaleToFit);
431 const SkBitmap* content = lock.sk_bitmap();
432 skia::RefPtr<SkShader> shader = skia::AdoptRef(
433 SkShader::CreateBitmapShader(*content,
434 SkShader::kClamp_TileMode,
435 SkShader::kClamp_TileMode));
436 shader->setLocalMatrix(content_mat);
437 current_paint_.setShader(shader.get());
439 SkImageFilter* filter = quad->filter.get();
440 if (filter)
441 current_paint_.setImageFilter(filter);
443 if (quad->mask_resource_id) {
444 ResourceProvider::ScopedReadLockSoftware mask_lock(resource_provider_,
445 quad->mask_resource_id);
447 const SkBitmap* mask = mask_lock.sk_bitmap();
449 SkRect mask_rect = SkRect::MakeXYWH(
450 quad->mask_uv_rect.x() * mask->width(),
451 quad->mask_uv_rect.y() * mask->height(),
452 quad->mask_uv_rect.width() * mask->width(),
453 quad->mask_uv_rect.height() * mask->height());
455 SkMatrix mask_mat;
456 mask_mat.setRectToRect(mask_rect, dest_rect, SkMatrix::kFill_ScaleToFit);
458 skia::RefPtr<SkShader> mask_shader = skia::AdoptRef(
459 SkShader::CreateBitmapShader(*mask,
460 SkShader::kClamp_TileMode,
461 SkShader::kClamp_TileMode));
462 mask_shader->setLocalMatrix(mask_mat);
464 SkPaint mask_paint;
465 mask_paint.setShader(mask_shader.get());
467 skia::RefPtr<SkLayerRasterizer> mask_rasterizer =
468 skia::AdoptRef(new SkLayerRasterizer);
469 mask_rasterizer->addLayer(mask_paint);
471 current_paint_.setRasterizer(mask_rasterizer.get());
472 current_canvas_->drawRect(dest_visible_rect, current_paint_);
473 } else {
474 // TODO(skaslev): Apply background filters and blend with content
475 current_canvas_->drawRect(dest_visible_rect, current_paint_);
479 void SoftwareRenderer::DrawUnsupportedQuad(const DrawingFrame* frame,
480 const DrawQuad* quad) {
481 #ifdef NDEBUG
482 current_paint_.setColor(SK_ColorWHITE);
483 #else
484 current_paint_.setColor(SK_ColorMAGENTA);
485 #endif
486 current_paint_.setAlpha(quad->opacity() * 255);
487 current_canvas_->drawRect(gfx::RectFToSkRect(QuadVertexRect()),
488 current_paint_);
491 void SoftwareRenderer::CopyCurrentRenderPassToBitmap(
492 DrawingFrame* frame,
493 scoped_ptr<CopyOutputRequest> request) {
494 gfx::Rect copy_rect = frame->current_render_pass->output_rect;
495 if (request->has_area())
496 copy_rect.Intersect(request->area());
497 gfx::Rect window_copy_rect = MoveFromDrawToWindowSpace(copy_rect);
499 scoped_ptr<SkBitmap> bitmap(new SkBitmap);
500 bitmap->setConfig(SkBitmap::kARGB_8888_Config,
501 window_copy_rect.width(),
502 window_copy_rect.height());
503 current_canvas_->readPixels(
504 bitmap.get(), window_copy_rect.x(), window_copy_rect.y());
506 request->SendBitmapResult(bitmap.Pass());
509 void SoftwareRenderer::DiscardBackbuffer() {
510 if (is_backbuffer_discarded_)
511 return;
513 output_surface_->DiscardBackbuffer();
515 is_backbuffer_discarded_ = true;
517 // Damage tracker needs a full reset every time framebuffer is discarded.
518 client_->SetFullRootLayerDamage();
521 void SoftwareRenderer::EnsureBackbuffer() {
522 if (!is_backbuffer_discarded_)
523 return;
525 output_surface_->EnsureBackbuffer();
526 is_backbuffer_discarded_ = false;
529 void SoftwareRenderer::GetFramebufferPixels(void* pixels, gfx::Rect rect) {
530 TRACE_EVENT0("cc", "SoftwareRenderer::GetFramebufferPixels");
531 SkBitmap subset_bitmap;
532 rect += current_viewport_rect_.OffsetFromOrigin();
533 output_device_->CopyToBitmap(rect, &subset_bitmap);
534 subset_bitmap.copyPixelsTo(pixels,
535 4 * rect.width() * rect.height(),
536 4 * rect.width());
539 void SoftwareRenderer::SetVisible(bool visible) {
540 if (visible_ == visible)
541 return;
542 visible_ = visible;
544 if (visible_)
545 EnsureBackbuffer();
546 else
547 DiscardBackbuffer();
550 void SoftwareRenderer::SetDiscardBackBufferWhenNotVisible(bool discard) {
551 // The software renderer always discards the backbuffer when not visible.
554 } // namespace cc