cc: Added inline to Tile::IsReadyToDraw
[chromium-blink-merge.git] / cc / output / software_renderer.cc
blob77c44eadc3d2e62c70ef9080d8bac02d47c4109a
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/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"
32 namespace cc {
34 namespace {
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),
64 visible_(true),
65 is_scissor_enabled_(false),
66 is_backbuffer_discarded_(false),
67 output_device_(output_surface->software_device()),
68 current_canvas_(NULL) {
69 if (resource_provider_) {
70 capabilities_.max_texture_size = resource_provider_->max_texture_size();
71 capabilities_.best_texture_format =
72 resource_provider_->best_texture_format();
74 capabilities_.using_set_visibility = true;
75 // The updater can access bitmaps while the SoftwareRenderer is using them.
76 capabilities_.allow_partial_texture_updates = true;
77 capabilities_.using_partial_swap = true;
79 capabilities_.using_map_image = Settings().use_map_image;
80 capabilities_.using_shared_memory_resources = true;
83 SoftwareRenderer::~SoftwareRenderer() {}
85 const RendererCapabilities& SoftwareRenderer::Capabilities() const {
86 return capabilities_;
89 void SoftwareRenderer::BeginDrawingFrame(DrawingFrame* frame) {
90 TRACE_EVENT0("cc", "SoftwareRenderer::BeginDrawingFrame");
91 root_canvas_ = output_device_->BeginPaint(
92 gfx::ToEnclosingRect(frame->root_damage_rect));
95 void SoftwareRenderer::FinishDrawingFrame(DrawingFrame* frame) {
96 TRACE_EVENT0("cc", "SoftwareRenderer::FinishDrawingFrame");
97 current_framebuffer_lock_.reset();
98 current_canvas_ = NULL;
99 root_canvas_ = NULL;
101 current_frame_data_.reset(new SoftwareFrameData);
102 output_device_->EndPaint(current_frame_data_.get());
105 void SoftwareRenderer::SwapBuffers() {
106 CompositorFrame compositor_frame;
107 compositor_frame.metadata = client_->MakeCompositorFrameMetadata();
108 compositor_frame.software_frame_data = current_frame_data_.Pass();
109 output_surface_->SwapBuffers(&compositor_frame);
112 void SoftwareRenderer::ReceiveSwapBuffersAck(const CompositorFrameAck& ack) {
113 output_device_->ReclaimSoftwareFrame(ack.last_software_frame_id);
116 bool SoftwareRenderer::FlippedFramebuffer() const {
117 return false;
120 void SoftwareRenderer::EnsureScissorTestEnabled() {
121 is_scissor_enabled_ = true;
122 SetClipRect(scissor_rect_);
125 void SoftwareRenderer::EnsureScissorTestDisabled() {
126 // There is no explicit notion of enabling/disabling scissoring in software
127 // rendering, but the underlying effect we want is to clear any existing
128 // clipRect on the current SkCanvas. This is done by setting clipRect to
129 // the viewport's dimensions.
130 is_scissor_enabled_ = false;
131 SkBaseDevice* device = current_canvas_->getDevice();
132 SetClipRect(gfx::Rect(device->width(), device->height()));
135 void SoftwareRenderer::Finish() {}
137 void SoftwareRenderer::BindFramebufferToOutputSurface(DrawingFrame* frame) {
138 DCHECK(!client_->ExternalStencilTestEnabled());
139 current_framebuffer_lock_.reset();
140 current_canvas_ = root_canvas_;
143 bool SoftwareRenderer::BindFramebufferToTexture(
144 DrawingFrame* frame,
145 const ScopedResource* texture,
146 gfx::Rect target_rect) {
147 current_framebuffer_lock_.reset();
148 current_framebuffer_lock_ = make_scoped_ptr(
149 new ResourceProvider::ScopedWriteLockSoftware(
150 resource_provider_, texture->id()));
151 current_canvas_ = current_framebuffer_lock_->sk_canvas();
152 InitializeViewport(frame,
153 target_rect,
154 gfx::Rect(target_rect.size()),
155 target_rect.size());
156 return true;
159 void SoftwareRenderer::SetScissorTestRect(gfx::Rect scissor_rect) {
160 is_scissor_enabled_ = true;
161 scissor_rect_ = scissor_rect;
162 SetClipRect(scissor_rect);
165 void SoftwareRenderer::SetClipRect(gfx::Rect rect) {
166 // Skia applies the current matrix to clip rects so we reset it temporary.
167 SkMatrix current_matrix = current_canvas_->getTotalMatrix();
168 current_canvas_->resetMatrix();
169 current_canvas_->clipRect(gfx::RectToSkRect(rect), SkRegion::kReplace_Op);
170 current_canvas_->setMatrix(current_matrix);
173 void SoftwareRenderer::ClearCanvas(SkColor color) {
174 // SkCanvas::clear doesn't respect the current clipping region
175 // so we SkCanvas::drawColor instead if scissoring is active.
176 if (is_scissor_enabled_)
177 current_canvas_->drawColor(color, SkXfermode::kSrc_Mode);
178 else
179 current_canvas_->clear(color);
182 void SoftwareRenderer::ClearFramebuffer(DrawingFrame* frame) {
183 if (frame->current_render_pass->has_transparent_background) {
184 ClearCanvas(SkColorSetARGB(0, 0, 0, 0));
185 } else {
186 #ifndef NDEBUG
187 // On DEBUG builds, opaque render passes are cleared to blue
188 // to easily see regions that were not drawn on the screen.
189 ClearCanvas(SkColorSetARGB(255, 0, 0, 255));
190 #endif
194 void SoftwareRenderer::SetDrawViewport(gfx::Rect window_space_viewport) {}
196 bool SoftwareRenderer::IsSoftwareResource(
197 ResourceProvider::ResourceId resource_id) const {
198 switch (resource_provider_->GetResourceType(resource_id)) {
199 case ResourceProvider::GLTexture:
200 return false;
201 case ResourceProvider::Bitmap:
202 return true;
203 case ResourceProvider::InvalidType:
204 break;
207 LOG(FATAL) << "Invalid resource type.";
208 return false;
211 void SoftwareRenderer::DoDrawQuad(DrawingFrame* frame, const DrawQuad* quad) {
212 TRACE_EVENT0("cc", "SoftwareRenderer::DoDrawQuad");
213 gfx::Transform quad_rect_matrix;
214 QuadRectTransform(&quad_rect_matrix, quad->quadTransform(), quad->rect);
215 gfx::Transform contents_device_transform =
216 frame->window_matrix * frame->projection_matrix * quad_rect_matrix;
217 contents_device_transform.FlattenTo2d();
218 SkMatrix sk_device_matrix;
219 gfx::TransformToFlattenedSkMatrix(contents_device_transform,
220 &sk_device_matrix);
221 current_canvas_->setMatrix(sk_device_matrix);
223 current_paint_.reset();
224 if (!IsScaleAndIntegerTranslate(sk_device_matrix)) {
225 // TODO(danakj): Until we can enable AA only on exterior edges of the
226 // layer, disable AA if any interior edges are present. crbug.com/248175
227 bool all_four_edges_are_exterior = quad->IsTopEdge() &&
228 quad->IsLeftEdge() &&
229 quad->IsBottomEdge() &&
230 quad->IsRightEdge();
231 if (Settings().allow_antialiasing &&
232 all_four_edges_are_exterior)
233 current_paint_.setAntiAlias(true);
234 current_paint_.setFilterBitmap(true);
237 if (quad->ShouldDrawWithBlending()) {
238 current_paint_.setAlpha(quad->opacity() * 255);
239 current_paint_.setXfermodeMode(SkXfermode::kSrcOver_Mode);
240 } else {
241 current_paint_.setXfermodeMode(SkXfermode::kSrc_Mode);
244 switch (quad->material) {
245 case DrawQuad::CHECKERBOARD:
246 DrawCheckerboardQuad(frame, CheckerboardDrawQuad::MaterialCast(quad));
247 break;
248 case DrawQuad::DEBUG_BORDER:
249 DrawDebugBorderQuad(frame, DebugBorderDrawQuad::MaterialCast(quad));
250 break;
251 case DrawQuad::PICTURE_CONTENT:
252 DrawPictureQuad(frame, PictureDrawQuad::MaterialCast(quad));
253 break;
254 case DrawQuad::RENDER_PASS:
255 DrawRenderPassQuad(frame, RenderPassDrawQuad::MaterialCast(quad));
256 break;
257 case DrawQuad::SOLID_COLOR:
258 DrawSolidColorQuad(frame, SolidColorDrawQuad::MaterialCast(quad));
259 break;
260 case DrawQuad::TEXTURE_CONTENT:
261 DrawTextureQuad(frame, TextureDrawQuad::MaterialCast(quad));
262 break;
263 case DrawQuad::TILED_CONTENT:
264 DrawTileQuad(frame, TileDrawQuad::MaterialCast(quad));
265 break;
266 case DrawQuad::INVALID:
267 case DrawQuad::IO_SURFACE_CONTENT:
268 case DrawQuad::YUV_VIDEO_CONTENT:
269 case DrawQuad::STREAM_VIDEO_CONTENT:
270 DrawUnsupportedQuad(frame, quad);
271 NOTREACHED();
272 break;
275 current_canvas_->resetMatrix();
278 void SoftwareRenderer::DrawCheckerboardQuad(const DrawingFrame* frame,
279 const CheckerboardDrawQuad* quad) {
280 gfx::RectF visible_quad_vertex_rect = MathUtil::ScaleRectProportional(
281 QuadVertexRect(), quad->rect, quad->visible_rect);
282 current_paint_.setColor(quad->color);
283 current_paint_.setAlpha(quad->opacity() * SkColorGetA(quad->color));
284 current_canvas_->drawRect(gfx::RectFToSkRect(visible_quad_vertex_rect),
285 current_paint_);
288 void SoftwareRenderer::DrawDebugBorderQuad(const DrawingFrame* frame,
289 const DebugBorderDrawQuad* quad) {
290 // We need to apply the matrix manually to have pixel-sized stroke width.
291 SkPoint vertices[4];
292 gfx::RectFToSkRect(QuadVertexRect()).toQuad(vertices);
293 SkPoint transformed_vertices[4];
294 current_canvas_->getTotalMatrix().mapPoints(transformed_vertices,
295 vertices,
297 current_canvas_->resetMatrix();
299 current_paint_.setColor(quad->color);
300 current_paint_.setAlpha(quad->opacity() * SkColorGetA(quad->color));
301 current_paint_.setStyle(SkPaint::kStroke_Style);
302 current_paint_.setStrokeWidth(quad->width);
303 current_canvas_->drawPoints(SkCanvas::kPolygon_PointMode,
304 4, transformed_vertices, current_paint_);
307 void SoftwareRenderer::DrawPictureQuad(const DrawingFrame* frame,
308 const PictureDrawQuad* quad) {
309 SkMatrix content_matrix;
310 content_matrix.setRectToRect(
311 gfx::RectFToSkRect(quad->tex_coord_rect),
312 gfx::RectFToSkRect(QuadVertexRect()),
313 SkMatrix::kFill_ScaleToFit);
314 current_canvas_->concat(content_matrix);
316 // TODO(aelias): This isn't correct in all cases. We should detect these
317 // cases and fall back to a persistent bitmap backing
318 // (http://crbug.com/280374).
319 DCHECK(!current_canvas_->getDrawFilter());
320 current_canvas_->setDrawFilter(new skia::OpacityDrawFilter(quad->opacity(),
321 true));
323 TRACE_EVENT0("cc",
324 "SoftwareRenderer::DrawPictureQuad");
325 quad->picture_pile->RasterDirect(
326 current_canvas_, quad->content_rect, quad->contents_scale, NULL);
328 current_canvas_->setDrawFilter(NULL);
331 void SoftwareRenderer::DrawSolidColorQuad(const DrawingFrame* frame,
332 const SolidColorDrawQuad* quad) {
333 gfx::RectF visible_quad_vertex_rect = MathUtil::ScaleRectProportional(
334 QuadVertexRect(), quad->rect, quad->visible_rect);
335 current_paint_.setColor(quad->color);
336 current_paint_.setAlpha(quad->opacity() * SkColorGetA(quad->color));
337 current_canvas_->drawRect(gfx::RectFToSkRect(visible_quad_vertex_rect),
338 current_paint_);
341 void SoftwareRenderer::DrawTextureQuad(const DrawingFrame* frame,
342 const TextureDrawQuad* quad) {
343 if (!IsSoftwareResource(quad->resource_id)) {
344 DrawUnsupportedQuad(frame, quad);
345 return;
348 // TODO(skaslev): Add support for non-premultiplied alpha.
349 ResourceProvider::ScopedReadLockSoftware lock(resource_provider_,
350 quad->resource_id);
351 const SkBitmap* bitmap = lock.sk_bitmap();
352 gfx::RectF uv_rect = gfx::ScaleRect(gfx::BoundingRect(quad->uv_top_left,
353 quad->uv_bottom_right),
354 bitmap->width(),
355 bitmap->height());
356 gfx::RectF visible_uv_rect =
357 MathUtil::ScaleRectProportional(uv_rect, quad->rect, quad->visible_rect);
358 SkRect sk_uv_rect = gfx::RectFToSkRect(visible_uv_rect);
359 gfx::RectF visible_quad_vertex_rect = MathUtil::ScaleRectProportional(
360 QuadVertexRect(), quad->rect, quad->visible_rect);
361 SkRect quad_rect = gfx::RectFToSkRect(visible_quad_vertex_rect);
363 if (quad->flipped)
364 current_canvas_->scale(1, -1);
366 bool blend_background = quad->background_color != SK_ColorTRANSPARENT &&
367 !bitmap->isOpaque();
368 bool needs_layer = blend_background && (current_paint_.getAlpha() != 0xFF);
369 if (needs_layer) {
370 current_canvas_->saveLayerAlpha(&quad_rect, current_paint_.getAlpha());
371 current_paint_.setAlpha(0xFF);
373 if (blend_background) {
374 SkPaint background_paint;
375 background_paint.setColor(quad->background_color);
376 current_canvas_->drawRect(quad_rect, background_paint);
379 current_canvas_->drawBitmapRectToRect(*bitmap,
380 &sk_uv_rect,
381 quad_rect,
382 &current_paint_);
384 if (needs_layer)
385 current_canvas_->restore();
388 void SoftwareRenderer::DrawTileQuad(const DrawingFrame* frame,
389 const TileDrawQuad* quad) {
390 DCHECK(!output_surface_->ForcedDrawToSoftwareDevice());
391 DCHECK(IsSoftwareResource(quad->resource_id));
392 ResourceProvider::ScopedReadLockSoftware lock(resource_provider_,
393 quad->resource_id);
394 gfx::RectF visible_tex_coord_rect = MathUtil::ScaleRectProportional(
395 quad->tex_coord_rect, quad->rect, quad->visible_rect);
396 gfx::RectF visible_quad_vertex_rect = MathUtil::ScaleRectProportional(
397 QuadVertexRect(), quad->rect, quad->visible_rect);
399 SkRect uv_rect = gfx::RectFToSkRect(visible_tex_coord_rect);
400 current_paint_.setFilterBitmap(true);
401 current_canvas_->drawBitmapRectToRect(
402 *lock.sk_bitmap(),
403 &uv_rect,
404 gfx::RectFToSkRect(visible_quad_vertex_rect),
405 &current_paint_);
408 void SoftwareRenderer::DrawRenderPassQuad(const DrawingFrame* frame,
409 const RenderPassDrawQuad* quad) {
410 CachedResource* content_texture =
411 render_pass_textures_.get(quad->render_pass_id);
412 if (!content_texture || !content_texture->id())
413 return;
415 DCHECK(IsSoftwareResource(content_texture->id()));
416 ResourceProvider::ScopedReadLockSoftware lock(resource_provider_,
417 content_texture->id());
419 SkRect dest_rect = gfx::RectFToSkRect(QuadVertexRect());
420 SkRect dest_visible_rect = gfx::RectFToSkRect(MathUtil::ScaleRectProportional(
421 QuadVertexRect(), quad->rect, quad->visible_rect));
422 SkRect content_rect = SkRect::MakeWH(quad->rect.width(), quad->rect.height());
424 SkMatrix content_mat;
425 content_mat.setRectToRect(content_rect, dest_rect,
426 SkMatrix::kFill_ScaleToFit);
428 const SkBitmap* content = lock.sk_bitmap();
429 skia::RefPtr<SkShader> shader = skia::AdoptRef(
430 SkShader::CreateBitmapShader(*content,
431 SkShader::kClamp_TileMode,
432 SkShader::kClamp_TileMode));
433 shader->setLocalMatrix(content_mat);
434 current_paint_.setShader(shader.get());
436 SkImageFilter* filter = quad->filter.get();
437 if (filter)
438 current_paint_.setImageFilter(filter);
440 if (quad->mask_resource_id) {
441 ResourceProvider::ScopedReadLockSoftware mask_lock(resource_provider_,
442 quad->mask_resource_id);
444 const SkBitmap* mask = mask_lock.sk_bitmap();
446 SkRect mask_rect = SkRect::MakeXYWH(
447 quad->mask_uv_rect.x() * mask->width(),
448 quad->mask_uv_rect.y() * mask->height(),
449 quad->mask_uv_rect.width() * mask->width(),
450 quad->mask_uv_rect.height() * mask->height());
452 SkMatrix mask_mat;
453 mask_mat.setRectToRect(mask_rect, dest_rect, SkMatrix::kFill_ScaleToFit);
455 skia::RefPtr<SkShader> mask_shader = skia::AdoptRef(
456 SkShader::CreateBitmapShader(*mask,
457 SkShader::kClamp_TileMode,
458 SkShader::kClamp_TileMode));
459 mask_shader->setLocalMatrix(mask_mat);
461 SkPaint mask_paint;
462 mask_paint.setShader(mask_shader.get());
464 skia::RefPtr<SkLayerRasterizer> mask_rasterizer =
465 skia::AdoptRef(new SkLayerRasterizer);
466 mask_rasterizer->addLayer(mask_paint);
468 current_paint_.setRasterizer(mask_rasterizer.get());
469 current_canvas_->drawRect(dest_visible_rect, current_paint_);
470 } else {
471 // TODO(skaslev): Apply background filters and blend with content
472 current_canvas_->drawRect(dest_visible_rect, current_paint_);
476 void SoftwareRenderer::DrawUnsupportedQuad(const DrawingFrame* frame,
477 const DrawQuad* quad) {
478 #ifdef NDEBUG
479 current_paint_.setColor(SK_ColorWHITE);
480 #else
481 current_paint_.setColor(SK_ColorMAGENTA);
482 #endif
483 current_paint_.setAlpha(quad->opacity() * 255);
484 current_canvas_->drawRect(gfx::RectFToSkRect(QuadVertexRect()),
485 current_paint_);
488 void SoftwareRenderer::CopyCurrentRenderPassToBitmap(
489 DrawingFrame* frame,
490 scoped_ptr<CopyOutputRequest> request) {
491 gfx::Rect copy_rect = frame->current_render_pass->output_rect;
492 if (request->has_area()) {
493 // Intersect with the request's area, positioned with its origin at the
494 // origin of the full copy_rect.
495 copy_rect.Intersect(request->area() - copy_rect.OffsetFromOrigin());
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