API migration: picture.draw -> picture.playback
[chromium-blink-merge.git] / cc / output / software_renderer.cc
blob1b18648e115cb92efc48350704422c280b15e8ee
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/render_surface_filters.h"
15 #include "cc/output/software_output_device.h"
16 #include "cc/quads/checkerboard_draw_quad.h"
17 #include "cc/quads/debug_border_draw_quad.h"
18 #include "cc/quads/picture_draw_quad.h"
19 #include "cc/quads/render_pass_draw_quad.h"
20 #include "cc/quads/solid_color_draw_quad.h"
21 #include "cc/quads/texture_draw_quad.h"
22 #include "cc/quads/tile_draw_quad.h"
23 #include "skia/ext/opacity_draw_filter.h"
24 #include "third_party/skia/include/core/SkCanvas.h"
25 #include "third_party/skia/include/core/SkColor.h"
26 #include "third_party/skia/include/core/SkImageFilter.h"
27 #include "third_party/skia/include/core/SkMatrix.h"
28 #include "third_party/skia/include/core/SkShader.h"
29 #include "third_party/skia/include/effects/SkLayerRasterizer.h"
30 #include "ui/gfx/geometry/rect_conversions.h"
31 #include "ui/gfx/skia_util.h"
32 #include "ui/gfx/transform.h"
34 namespace cc {
35 namespace {
37 static inline bool IsScalarNearlyInteger(SkScalar scalar) {
38 return SkScalarNearlyZero(scalar - SkScalarRoundToScalar(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 static SkShader::TileMode WrapModeToTileMode(GLint wrap_mode) {
52 switch (wrap_mode) {
53 case GL_REPEAT:
54 return SkShader::kRepeat_TileMode;
55 case GL_CLAMP_TO_EDGE:
56 return SkShader::kClamp_TileMode;
58 NOTREACHED();
59 return SkShader::kClamp_TileMode;
62 } // anonymous namespace
64 scoped_ptr<SoftwareRenderer> SoftwareRenderer::Create(
65 RendererClient* client,
66 const RendererSettings* settings,
67 OutputSurface* output_surface,
68 ResourceProvider* resource_provider) {
69 return make_scoped_ptr(new SoftwareRenderer(
70 client, settings, output_surface, resource_provider));
73 SoftwareRenderer::SoftwareRenderer(RendererClient* client,
74 const RendererSettings* settings,
75 OutputSurface* output_surface,
76 ResourceProvider* resource_provider)
77 : DirectRenderer(client, settings, output_surface, resource_provider),
78 is_scissor_enabled_(false),
79 is_backbuffer_discarded_(false),
80 output_device_(output_surface->software_device()),
81 current_canvas_(NULL) {
82 if (resource_provider_) {
83 capabilities_.max_texture_size = resource_provider_->max_texture_size();
84 capabilities_.best_texture_format =
85 resource_provider_->best_texture_format();
87 // The updater can access bitmaps while the SoftwareRenderer is using them.
88 capabilities_.allow_partial_texture_updates = true;
89 capabilities_.using_partial_swap = true;
91 capabilities_.using_shared_memory_resources = true;
93 capabilities_.allow_rasterize_on_demand = true;
96 SoftwareRenderer::~SoftwareRenderer() {}
98 const RendererCapabilitiesImpl& SoftwareRenderer::Capabilities() const {
99 return capabilities_;
102 void SoftwareRenderer::BeginDrawingFrame(DrawingFrame* frame) {
103 TRACE_EVENT0("cc", "SoftwareRenderer::BeginDrawingFrame");
104 root_canvas_ = output_device_->BeginPaint(
105 gfx::ToEnclosingRect(frame->root_damage_rect));
108 void SoftwareRenderer::FinishDrawingFrame(DrawingFrame* frame) {
109 TRACE_EVENT0("cc", "SoftwareRenderer::FinishDrawingFrame");
110 current_framebuffer_lock_ = nullptr;
111 current_framebuffer_canvas_.clear();
112 current_canvas_ = NULL;
113 root_canvas_ = NULL;
115 current_frame_data_.reset(new SoftwareFrameData);
116 output_device_->EndPaint(current_frame_data_.get());
119 void SoftwareRenderer::SwapBuffers(const CompositorFrameMetadata& metadata) {
120 TRACE_EVENT0("cc,benchmark", "SoftwareRenderer::SwapBuffers");
121 CompositorFrame compositor_frame;
122 compositor_frame.metadata = metadata;
123 compositor_frame.software_frame_data = current_frame_data_.Pass();
124 output_surface_->SwapBuffers(&compositor_frame);
127 void SoftwareRenderer::ReceiveSwapBuffersAck(const CompositorFrameAck& ack) {
128 output_device_->ReclaimSoftwareFrame(ack.last_software_frame_id);
131 bool SoftwareRenderer::FlippedFramebuffer(const DrawingFrame* frame) const {
132 return false;
135 void SoftwareRenderer::EnsureScissorTestEnabled() {
136 is_scissor_enabled_ = true;
137 SetClipRect(scissor_rect_);
140 void SoftwareRenderer::EnsureScissorTestDisabled() {
141 // There is no explicit notion of enabling/disabling scissoring in software
142 // rendering, but the underlying effect we want is to clear any existing
143 // clipRect on the current SkCanvas. This is done by setting clipRect to
144 // the viewport's dimensions.
145 is_scissor_enabled_ = false;
146 SkISize size = current_canvas_->getDeviceSize();
147 SetClipRect(gfx::Rect(size.width(), size.height()));
150 void SoftwareRenderer::Finish() {}
152 void SoftwareRenderer::BindFramebufferToOutputSurface(DrawingFrame* frame) {
153 DCHECK(!output_surface_->HasExternalStencilTest());
154 current_framebuffer_lock_ = nullptr;
155 current_framebuffer_canvas_.clear();
156 current_canvas_ = root_canvas_;
159 bool SoftwareRenderer::BindFramebufferToTexture(
160 DrawingFrame* frame,
161 const ScopedResource* texture,
162 const gfx::Rect& target_rect) {
163 current_framebuffer_lock_ = make_scoped_ptr(
164 new ResourceProvider::ScopedWriteLockSoftware(
165 resource_provider_, texture->id()));
166 current_framebuffer_canvas_ =
167 skia::AdoptRef(new SkCanvas(current_framebuffer_lock_->sk_bitmap()));
168 current_canvas_ = current_framebuffer_canvas_.get();
169 InitializeViewport(frame,
170 target_rect,
171 gfx::Rect(target_rect.size()),
172 target_rect.size());
173 return true;
176 void SoftwareRenderer::SetScissorTestRect(const gfx::Rect& scissor_rect) {
177 is_scissor_enabled_ = true;
178 scissor_rect_ = scissor_rect;
179 SetClipRect(scissor_rect);
182 void SoftwareRenderer::SetClipRect(const gfx::Rect& rect) {
183 // Skia applies the current matrix to clip rects so we reset it temporary.
184 SkMatrix current_matrix = current_canvas_->getTotalMatrix();
185 current_canvas_->resetMatrix();
186 current_canvas_->clipRect(gfx::RectToSkRect(rect), SkRegion::kReplace_Op);
187 current_canvas_->setMatrix(current_matrix);
190 void SoftwareRenderer::ClearCanvas(SkColor color) {
191 // SkCanvas::clear doesn't respect the current clipping region
192 // so we SkCanvas::drawColor instead if scissoring is active.
193 if (is_scissor_enabled_)
194 current_canvas_->drawColor(color, SkXfermode::kSrc_Mode);
195 else
196 current_canvas_->clear(color);
199 void SoftwareRenderer::DiscardPixels(bool has_external_stencil_test,
200 bool draw_rect_covers_full_surface) {}
202 void SoftwareRenderer::ClearFramebuffer(DrawingFrame* frame,
203 bool has_external_stencil_test) {
204 if (frame->current_render_pass->has_transparent_background) {
205 ClearCanvas(SkColorSetARGB(0, 0, 0, 0));
206 } else {
207 #ifndef NDEBUG
208 // On DEBUG builds, opaque render passes are cleared to blue
209 // to easily see regions that were not drawn on the screen.
210 ClearCanvas(SkColorSetARGB(255, 0, 0, 255));
211 #endif
215 void SoftwareRenderer::SetDrawViewport(
216 const gfx::Rect& window_space_viewport) {}
218 bool SoftwareRenderer::IsSoftwareResource(
219 ResourceProvider::ResourceId resource_id) const {
220 switch (resource_provider_->GetResourceType(resource_id)) {
221 case ResourceProvider::GLTexture:
222 return false;
223 case ResourceProvider::Bitmap:
224 return true;
225 case ResourceProvider::InvalidType:
226 break;
229 LOG(FATAL) << "Invalid resource type.";
230 return false;
233 void SoftwareRenderer::DoDrawQuad(DrawingFrame* frame, const DrawQuad* quad) {
234 TRACE_EVENT0("cc", "SoftwareRenderer::DoDrawQuad");
235 gfx::Transform quad_rect_matrix;
236 QuadRectTransform(&quad_rect_matrix, quad->quadTransform(), quad->rect);
237 gfx::Transform contents_device_transform =
238 frame->window_matrix * frame->projection_matrix * quad_rect_matrix;
239 contents_device_transform.FlattenTo2d();
240 SkMatrix sk_device_matrix;
241 gfx::TransformToFlattenedSkMatrix(contents_device_transform,
242 &sk_device_matrix);
243 current_canvas_->setMatrix(sk_device_matrix);
245 current_paint_.reset();
246 if (settings_->force_antialiasing ||
247 !IsScaleAndIntegerTranslate(sk_device_matrix)) {
248 // TODO(danakj): Until we can enable AA only on exterior edges of the
249 // layer, disable AA if any interior edges are present. crbug.com/248175
250 bool all_four_edges_are_exterior = quad->IsTopEdge() &&
251 quad->IsLeftEdge() &&
252 quad->IsBottomEdge() &&
253 quad->IsRightEdge();
254 if (settings_->allow_antialiasing &&
255 (settings_->force_antialiasing || all_four_edges_are_exterior))
256 current_paint_.setAntiAlias(true);
257 current_paint_.setFilterLevel(SkPaint::kLow_FilterLevel);
260 if (quad->ShouldDrawWithBlending() ||
261 quad->shared_quad_state->blend_mode != SkXfermode::kSrcOver_Mode) {
262 current_paint_.setAlpha(quad->opacity() * 255);
263 current_paint_.setXfermodeMode(quad->shared_quad_state->blend_mode);
264 } else {
265 current_paint_.setXfermodeMode(SkXfermode::kSrc_Mode);
268 switch (quad->material) {
269 case DrawQuad::CHECKERBOARD:
270 DrawCheckerboardQuad(frame, CheckerboardDrawQuad::MaterialCast(quad));
271 break;
272 case DrawQuad::DEBUG_BORDER:
273 DrawDebugBorderQuad(frame, DebugBorderDrawQuad::MaterialCast(quad));
274 break;
275 case DrawQuad::PICTURE_CONTENT:
276 DrawPictureQuad(frame, PictureDrawQuad::MaterialCast(quad));
277 break;
278 case DrawQuad::RENDER_PASS:
279 DrawRenderPassQuad(frame, RenderPassDrawQuad::MaterialCast(quad));
280 break;
281 case DrawQuad::SOLID_COLOR:
282 DrawSolidColorQuad(frame, SolidColorDrawQuad::MaterialCast(quad));
283 break;
284 case DrawQuad::TEXTURE_CONTENT:
285 DrawTextureQuad(frame, TextureDrawQuad::MaterialCast(quad));
286 break;
287 case DrawQuad::TILED_CONTENT:
288 DrawTileQuad(frame, TileDrawQuad::MaterialCast(quad));
289 break;
290 case DrawQuad::SURFACE_CONTENT:
291 // Surface content should be fully resolved to other quad types before
292 // reaching a direct renderer.
293 NOTREACHED();
294 break;
295 case DrawQuad::INVALID:
296 case DrawQuad::IO_SURFACE_CONTENT:
297 case DrawQuad::YUV_VIDEO_CONTENT:
298 case DrawQuad::STREAM_VIDEO_CONTENT:
299 DrawUnsupportedQuad(frame, quad);
300 NOTREACHED();
301 break;
304 current_canvas_->resetMatrix();
307 void SoftwareRenderer::DrawCheckerboardQuad(const DrawingFrame* frame,
308 const CheckerboardDrawQuad* quad) {
309 gfx::RectF visible_quad_vertex_rect = MathUtil::ScaleRectProportional(
310 QuadVertexRect(), quad->rect, quad->visible_rect);
311 current_paint_.setColor(quad->color);
312 current_paint_.setAlpha(quad->opacity() * SkColorGetA(quad->color));
313 current_canvas_->drawRect(gfx::RectFToSkRect(visible_quad_vertex_rect),
314 current_paint_);
317 void SoftwareRenderer::DrawDebugBorderQuad(const DrawingFrame* frame,
318 const DebugBorderDrawQuad* quad) {
319 // We need to apply the matrix manually to have pixel-sized stroke width.
320 SkPoint vertices[4];
321 gfx::RectFToSkRect(QuadVertexRect()).toQuad(vertices);
322 SkPoint transformed_vertices[4];
323 current_canvas_->getTotalMatrix().mapPoints(transformed_vertices,
324 vertices,
326 current_canvas_->resetMatrix();
328 current_paint_.setColor(quad->color);
329 current_paint_.setAlpha(quad->opacity() * SkColorGetA(quad->color));
330 current_paint_.setStyle(SkPaint::kStroke_Style);
331 current_paint_.setStrokeWidth(quad->width);
332 current_canvas_->drawPoints(SkCanvas::kPolygon_PointMode,
333 4, transformed_vertices, current_paint_);
336 void SoftwareRenderer::DrawPictureQuad(const DrawingFrame* frame,
337 const PictureDrawQuad* quad) {
338 SkMatrix content_matrix;
339 content_matrix.setRectToRect(
340 gfx::RectFToSkRect(quad->tex_coord_rect),
341 gfx::RectFToSkRect(QuadVertexRect()),
342 SkMatrix::kFill_ScaleToFit);
343 current_canvas_->concat(content_matrix);
345 // TODO(aelias): This isn't correct in all cases. We should detect these
346 // cases and fall back to a persistent bitmap backing
347 // (http://crbug.com/280374).
348 skia::RefPtr<SkDrawFilter> opacity_filter =
349 skia::AdoptRef(new skia::OpacityDrawFilter(
350 quad->opacity(), frame->disable_picture_quad_image_filtering ||
351 quad->nearest_neighbor));
352 DCHECK(!current_canvas_->getDrawFilter());
353 current_canvas_->setDrawFilter(opacity_filter.get());
355 TRACE_EVENT0("cc",
356 "SoftwareRenderer::DrawPictureQuad");
358 quad->raster_source->PlaybackToSharedCanvas(
359 current_canvas_, quad->content_rect, quad->contents_scale);
361 current_canvas_->setDrawFilter(NULL);
364 void SoftwareRenderer::DrawSolidColorQuad(const DrawingFrame* frame,
365 const SolidColorDrawQuad* quad) {
366 gfx::RectF visible_quad_vertex_rect = MathUtil::ScaleRectProportional(
367 QuadVertexRect(), quad->rect, quad->visible_rect);
368 current_paint_.setColor(quad->color);
369 current_paint_.setAlpha(quad->opacity() * SkColorGetA(quad->color));
370 current_canvas_->drawRect(gfx::RectFToSkRect(visible_quad_vertex_rect),
371 current_paint_);
374 void SoftwareRenderer::DrawTextureQuad(const DrawingFrame* frame,
375 const TextureDrawQuad* quad) {
376 if (!IsSoftwareResource(quad->resource_id)) {
377 DrawUnsupportedQuad(frame, quad);
378 return;
381 // TODO(skaslev): Add support for non-premultiplied alpha.
382 ResourceProvider::ScopedReadLockSoftware lock(resource_provider_,
383 quad->resource_id);
384 if (!lock.valid())
385 return;
386 const SkBitmap* bitmap = lock.sk_bitmap();
387 gfx::RectF uv_rect = gfx::ScaleRect(gfx::BoundingRect(quad->uv_top_left,
388 quad->uv_bottom_right),
389 bitmap->width(),
390 bitmap->height());
391 gfx::RectF visible_uv_rect =
392 MathUtil::ScaleRectProportional(uv_rect, quad->rect, quad->visible_rect);
393 SkRect sk_uv_rect = gfx::RectFToSkRect(visible_uv_rect);
394 gfx::RectF visible_quad_vertex_rect = MathUtil::ScaleRectProportional(
395 QuadVertexRect(), quad->rect, quad->visible_rect);
396 SkRect quad_rect = gfx::RectFToSkRect(visible_quad_vertex_rect);
398 if (quad->flipped)
399 current_canvas_->scale(1, -1);
401 bool blend_background = quad->background_color != SK_ColorTRANSPARENT &&
402 !bitmap->isOpaque();
403 bool needs_layer = blend_background && (current_paint_.getAlpha() != 0xFF);
404 if (needs_layer) {
405 current_canvas_->saveLayerAlpha(&quad_rect, current_paint_.getAlpha());
406 current_paint_.setAlpha(0xFF);
408 if (blend_background) {
409 SkPaint background_paint;
410 background_paint.setColor(quad->background_color);
411 current_canvas_->drawRect(quad_rect, background_paint);
413 SkShader::TileMode tile_mode = WrapModeToTileMode(lock.wrap_mode());
414 if (tile_mode != SkShader::kClamp_TileMode) {
415 SkMatrix matrix;
416 matrix.setRectToRect(sk_uv_rect, quad_rect, SkMatrix::kFill_ScaleToFit);
417 skia::RefPtr<SkShader> shader = skia::AdoptRef(
418 SkShader::CreateBitmapShader(*bitmap, tile_mode, tile_mode, &matrix));
419 SkPaint paint;
420 paint.setStyle(SkPaint::kFill_Style);
421 paint.setShader(shader.get());
422 current_canvas_->drawRect(quad_rect, paint);
423 } else {
424 current_canvas_->drawBitmapRectToRect(*bitmap,
425 &sk_uv_rect,
426 quad_rect,
427 &current_paint_);
430 if (needs_layer)
431 current_canvas_->restore();
434 void SoftwareRenderer::DrawTileQuad(const DrawingFrame* frame,
435 const TileDrawQuad* quad) {
436 // |resource_provider_| can be NULL in resourceless software draws, which
437 // should never produce tile quads in the first place.
438 DCHECK(resource_provider_);
439 DCHECK(IsSoftwareResource(quad->resource_id));
441 ResourceProvider::ScopedReadLockSoftware lock(resource_provider_,
442 quad->resource_id);
443 if (!lock.valid())
444 return;
445 DCHECK_EQ(GL_CLAMP_TO_EDGE, lock.wrap_mode());
447 gfx::RectF visible_tex_coord_rect = MathUtil::ScaleRectProportional(
448 quad->tex_coord_rect, quad->rect, quad->visible_rect);
449 gfx::RectF visible_quad_vertex_rect = MathUtil::ScaleRectProportional(
450 QuadVertexRect(), quad->rect, quad->visible_rect);
452 SkRect uv_rect = gfx::RectFToSkRect(visible_tex_coord_rect);
453 current_paint_.setFilterLevel(quad->nearest_neighbor
454 ? SkPaint::kNone_FilterLevel
455 : SkPaint::kLow_FilterLevel);
456 current_canvas_->drawBitmapRectToRect(
457 *lock.sk_bitmap(),
458 &uv_rect,
459 gfx::RectFToSkRect(visible_quad_vertex_rect),
460 &current_paint_);
463 void SoftwareRenderer::DrawRenderPassQuad(const DrawingFrame* frame,
464 const RenderPassDrawQuad* quad) {
465 ScopedResource* content_texture =
466 render_pass_textures_.get(quad->render_pass_id);
467 if (!content_texture || !content_texture->id())
468 return;
470 DCHECK(IsSoftwareResource(content_texture->id()));
471 ResourceProvider::ScopedReadLockSoftware lock(resource_provider_,
472 content_texture->id());
473 if (!lock.valid())
474 return;
475 SkShader::TileMode content_tile_mode = WrapModeToTileMode(lock.wrap_mode());
477 SkRect dest_rect = gfx::RectFToSkRect(QuadVertexRect());
478 SkRect dest_visible_rect = gfx::RectFToSkRect(MathUtil::ScaleRectProportional(
479 QuadVertexRect(), quad->rect, quad->visible_rect));
480 SkRect content_rect = SkRect::MakeWH(quad->rect.width(), quad->rect.height());
482 SkMatrix content_mat;
483 content_mat.setRectToRect(content_rect, dest_rect,
484 SkMatrix::kFill_ScaleToFit);
486 const SkBitmap* content = lock.sk_bitmap();
488 SkBitmap filter_bitmap;
489 if (!quad->filters.IsEmpty()) {
490 skia::RefPtr<SkImageFilter> filter = RenderSurfaceFilters::BuildImageFilter(
491 quad->filters, content_texture->size());
492 // TODO(ajuma): Apply the filter in the same pass as the content where
493 // possible (e.g. when there's no origin offset). See crbug.com/308201.
494 if (filter) {
495 SkImageInfo info = SkImageInfo::MakeN32Premul(
496 content_texture->size().width(), content_texture->size().height());
497 if (filter_bitmap.tryAllocPixels(info)) {
498 SkCanvas canvas(filter_bitmap);
499 SkPaint paint;
500 paint.setImageFilter(filter.get());
501 canvas.clear(SK_ColorTRANSPARENT);
502 canvas.translate(SkIntToScalar(-quad->rect.origin().x()),
503 SkIntToScalar(-quad->rect.origin().y()));
504 canvas.scale(quad->filters_scale.x(), quad->filters_scale.y());
505 canvas.drawSprite(*content, 0, 0, &paint);
510 skia::RefPtr<SkShader> shader;
511 if (filter_bitmap.isNull()) {
512 shader = skia::AdoptRef(SkShader::CreateBitmapShader(
513 *content, content_tile_mode, content_tile_mode, &content_mat));
514 } else {
515 shader = skia::AdoptRef(SkShader::CreateBitmapShader(
516 filter_bitmap, content_tile_mode, content_tile_mode, &content_mat));
518 current_paint_.setShader(shader.get());
520 if (quad->mask_resource_id) {
521 ResourceProvider::ScopedReadLockSoftware mask_lock(resource_provider_,
522 quad->mask_resource_id);
523 if (!lock.valid())
524 return;
525 SkShader::TileMode mask_tile_mode = WrapModeToTileMode(
526 mask_lock.wrap_mode());
528 const SkBitmap* mask = mask_lock.sk_bitmap();
530 // Scale normalized uv rect into absolute texel coordinates.
531 SkRect mask_rect =
532 gfx::RectFToSkRect(gfx::ScaleRect(quad->MaskUVRect(),
533 quad->mask_texture_size.width(),
534 quad->mask_texture_size.height()));
536 SkMatrix mask_mat;
537 mask_mat.setRectToRect(mask_rect, dest_rect, SkMatrix::kFill_ScaleToFit);
539 skia::RefPtr<SkShader> mask_shader =
540 skia::AdoptRef(SkShader::CreateBitmapShader(
541 *mask, mask_tile_mode, mask_tile_mode, &mask_mat));
543 SkPaint mask_paint;
544 mask_paint.setShader(mask_shader.get());
546 SkLayerRasterizer::Builder builder;
547 builder.addLayer(mask_paint);
549 skia::RefPtr<SkLayerRasterizer> mask_rasterizer =
550 skia::AdoptRef(builder.detachRasterizer());
552 current_paint_.setRasterizer(mask_rasterizer.get());
553 current_canvas_->drawRect(dest_visible_rect, current_paint_);
554 } else {
555 // TODO(skaslev): Apply background filters
556 current_canvas_->drawRect(dest_visible_rect, current_paint_);
560 void SoftwareRenderer::DrawUnsupportedQuad(const DrawingFrame* frame,
561 const DrawQuad* quad) {
562 #ifdef NDEBUG
563 current_paint_.setColor(SK_ColorWHITE);
564 #else
565 current_paint_.setColor(SK_ColorMAGENTA);
566 #endif
567 current_paint_.setAlpha(quad->opacity() * 255);
568 current_canvas_->drawRect(gfx::RectFToSkRect(QuadVertexRect()),
569 current_paint_);
572 void SoftwareRenderer::CopyCurrentRenderPassToBitmap(
573 DrawingFrame* frame,
574 scoped_ptr<CopyOutputRequest> request) {
575 gfx::Rect copy_rect = frame->current_render_pass->output_rect;
576 if (request->has_area())
577 copy_rect.Intersect(request->area());
578 gfx::Rect window_copy_rect = MoveFromDrawToWindowSpace(frame, copy_rect);
580 scoped_ptr<SkBitmap> bitmap(new SkBitmap);
581 bitmap->setInfo(SkImageInfo::MakeN32Premul(window_copy_rect.width(),
582 window_copy_rect.height()));
583 current_canvas_->readPixels(
584 bitmap.get(), window_copy_rect.x(), window_copy_rect.y());
586 request->SendBitmapResult(bitmap.Pass());
589 void SoftwareRenderer::DiscardBackbuffer() {
590 if (is_backbuffer_discarded_)
591 return;
593 output_surface_->DiscardBackbuffer();
595 is_backbuffer_discarded_ = true;
597 // Damage tracker needs a full reset every time framebuffer is discarded.
598 client_->SetFullRootLayerDamage();
601 void SoftwareRenderer::EnsureBackbuffer() {
602 if (!is_backbuffer_discarded_)
603 return;
605 output_surface_->EnsureBackbuffer();
606 is_backbuffer_discarded_ = false;
609 void SoftwareRenderer::DidChangeVisibility() {
610 if (visible())
611 EnsureBackbuffer();
612 else
613 DiscardBackbuffer();
616 } // namespace cc