Add ICU message format support
[chromium-blink-merge.git] / cc / output / software_renderer.cc
blob000d83a741533961e177e694f260b0d4dac17bde
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/trace_event/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/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_filter_canvas.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/SkPath.h"
28 #include "third_party/skia/include/core/SkPoint.h"
29 #include "third_party/skia/include/core/SkShader.h"
30 #include "third_party/skia/include/effects/SkLayerRasterizer.h"
31 #include "ui/gfx/geometry/rect_conversions.h"
32 #include "ui/gfx/skia_util.h"
33 #include "ui/gfx/transform.h"
35 namespace cc {
36 namespace {
38 static inline bool IsScalarNearlyInteger(SkScalar scalar) {
39 return SkScalarNearlyZero(scalar - SkScalarRoundToScalar(scalar));
42 bool IsScaleAndIntegerTranslate(const SkMatrix& matrix) {
43 return IsScalarNearlyInteger(matrix[SkMatrix::kMTransX]) &&
44 IsScalarNearlyInteger(matrix[SkMatrix::kMTransY]) &&
45 SkScalarNearlyZero(matrix[SkMatrix::kMSkewX]) &&
46 SkScalarNearlyZero(matrix[SkMatrix::kMSkewY]) &&
47 SkScalarNearlyZero(matrix[SkMatrix::kMPersp0]) &&
48 SkScalarNearlyZero(matrix[SkMatrix::kMPersp1]) &&
49 SkScalarNearlyZero(matrix[SkMatrix::kMPersp2] - 1.0f);
52 static SkShader::TileMode WrapModeToTileMode(GLint wrap_mode) {
53 switch (wrap_mode) {
54 case GL_REPEAT:
55 return SkShader::kRepeat_TileMode;
56 case GL_CLAMP_TO_EDGE:
57 return SkShader::kClamp_TileMode;
59 NOTREACHED();
60 return SkShader::kClamp_TileMode;
63 } // anonymous namespace
65 scoped_ptr<SoftwareRenderer> SoftwareRenderer::Create(
66 RendererClient* client,
67 const RendererSettings* settings,
68 OutputSurface* output_surface,
69 ResourceProvider* resource_provider) {
70 return make_scoped_ptr(new SoftwareRenderer(
71 client, settings, output_surface, resource_provider));
74 SoftwareRenderer::SoftwareRenderer(RendererClient* client,
75 const RendererSettings* settings,
76 OutputSurface* output_surface,
77 ResourceProvider* resource_provider)
78 : DirectRenderer(client, settings, output_surface, resource_provider),
79 is_scissor_enabled_(false),
80 is_backbuffer_discarded_(false),
81 output_device_(output_surface->software_device()),
82 current_canvas_(NULL) {
83 if (resource_provider_) {
84 capabilities_.max_texture_size = resource_provider_->max_texture_size();
85 capabilities_.best_texture_format =
86 resource_provider_->best_texture_format();
88 // The updater can access bitmaps while the SoftwareRenderer is using them.
89 capabilities_.allow_partial_texture_updates = true;
90 capabilities_.using_partial_swap = true;
92 capabilities_.using_shared_memory_resources = true;
94 capabilities_.allow_rasterize_on_demand = true;
97 SoftwareRenderer::~SoftwareRenderer() {}
99 const RendererCapabilitiesImpl& SoftwareRenderer::Capabilities() const {
100 return capabilities_;
103 void SoftwareRenderer::BeginDrawingFrame(DrawingFrame* frame) {
104 TRACE_EVENT0("cc", "SoftwareRenderer::BeginDrawingFrame");
105 root_canvas_ = output_device_->BeginPaint(
106 gfx::ToEnclosingRect(frame->root_damage_rect));
109 void SoftwareRenderer::FinishDrawingFrame(DrawingFrame* frame) {
110 TRACE_EVENT0("cc", "SoftwareRenderer::FinishDrawingFrame");
111 current_framebuffer_lock_ = nullptr;
112 current_framebuffer_canvas_.clear();
113 current_canvas_ = NULL;
114 root_canvas_ = NULL;
116 output_device_->EndPaint();
119 void SoftwareRenderer::SwapBuffers(const CompositorFrameMetadata& metadata) {
120 TRACE_EVENT0("cc,benchmark", "SoftwareRenderer::SwapBuffers");
121 CompositorFrame compositor_frame;
122 compositor_frame.metadata = metadata;
123 output_surface_->SwapBuffers(&compositor_frame);
126 bool SoftwareRenderer::FlippedFramebuffer(const DrawingFrame* frame) const {
127 return false;
130 void SoftwareRenderer::EnsureScissorTestEnabled() {
131 is_scissor_enabled_ = true;
132 SetClipRect(scissor_rect_);
135 void SoftwareRenderer::EnsureScissorTestDisabled() {
136 // There is no explicit notion of enabling/disabling scissoring in software
137 // rendering, but the underlying effect we want is to clear any existing
138 // clipRect on the current SkCanvas. This is done by setting clipRect to
139 // the viewport's dimensions.
140 if (!current_canvas_)
141 return;
142 is_scissor_enabled_ = false;
143 SkISize size = current_canvas_->getDeviceSize();
144 SetClipRect(gfx::Rect(size.width(), size.height()));
147 void SoftwareRenderer::Finish() {}
149 void SoftwareRenderer::BindFramebufferToOutputSurface(DrawingFrame* frame) {
150 DCHECK(!output_surface_->HasExternalStencilTest());
151 current_framebuffer_lock_ = nullptr;
152 current_framebuffer_canvas_.clear();
153 current_canvas_ = root_canvas_;
156 bool SoftwareRenderer::BindFramebufferToTexture(
157 DrawingFrame* frame,
158 const ScopedResource* texture,
159 const gfx::Rect& target_rect) {
160 DCHECK(texture->id());
162 // Explicitly release lock, otherwise we can crash when try to lock
163 // same texture again.
164 current_framebuffer_lock_ = nullptr;
165 current_framebuffer_lock_ = make_scoped_ptr(
166 new ResourceProvider::ScopedWriteLockSoftware(
167 resource_provider_, texture->id()));
168 current_framebuffer_canvas_ =
169 skia::AdoptRef(new SkCanvas(current_framebuffer_lock_->sk_bitmap()));
170 current_canvas_ = current_framebuffer_canvas_.get();
171 return true;
174 void SoftwareRenderer::SetScissorTestRect(const gfx::Rect& scissor_rect) {
175 is_scissor_enabled_ = true;
176 scissor_rect_ = scissor_rect;
177 SetClipRect(scissor_rect);
180 void SoftwareRenderer::SetClipRect(const gfx::Rect& rect) {
181 if (!current_canvas_)
182 return;
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 if (!current_canvas_)
192 return;
193 // SkCanvas::clear doesn't respect the current clipping region
194 // so we SkCanvas::drawColor instead if scissoring is active.
195 if (is_scissor_enabled_)
196 current_canvas_->drawColor(color, SkXfermode::kSrc_Mode);
197 else
198 current_canvas_->clear(color);
201 void SoftwareRenderer::ClearFramebuffer(DrawingFrame* frame) {
202 if (frame->current_render_pass->has_transparent_background) {
203 ClearCanvas(SkColorSetARGB(0, 0, 0, 0));
204 } else {
205 #ifndef NDEBUG
206 // On DEBUG builds, opaque render passes are cleared to blue
207 // to easily see regions that were not drawn on the screen.
208 ClearCanvas(SkColorSetARGB(255, 0, 0, 255));
209 #endif
213 void SoftwareRenderer::PrepareSurfaceForPass(
214 DrawingFrame* frame,
215 SurfaceInitializationMode initialization_mode,
216 const gfx::Rect& render_pass_scissor) {
217 switch (initialization_mode) {
218 case SURFACE_INITIALIZATION_MODE_PRESERVE:
219 EnsureScissorTestDisabled();
220 return;
221 case SURFACE_INITIALIZATION_MODE_FULL_SURFACE_CLEAR:
222 EnsureScissorTestDisabled();
223 ClearFramebuffer(frame);
224 break;
225 case SURFACE_INITIALIZATION_MODE_SCISSORED_CLEAR:
226 SetScissorTestRect(render_pass_scissor);
227 ClearFramebuffer(frame);
228 break;
232 bool SoftwareRenderer::IsSoftwareResource(ResourceId resource_id) const {
233 switch (resource_provider_->GetResourceType(resource_id)) {
234 case ResourceProvider::RESOURCE_TYPE_GL_TEXTURE:
235 return false;
236 case ResourceProvider::RESOURCE_TYPE_BITMAP:
237 return true;
240 LOG(FATAL) << "Invalid resource type.";
241 return false;
244 void SoftwareRenderer::DoDrawQuad(DrawingFrame* frame,
245 const DrawQuad* quad,
246 const gfx::QuadF* draw_region) {
247 if (!current_canvas_)
248 return;
249 if (draw_region) {
250 current_canvas_->save();
253 TRACE_EVENT0("cc", "SoftwareRenderer::DoDrawQuad");
254 gfx::Transform quad_rect_matrix;
255 QuadRectTransform(&quad_rect_matrix,
256 quad->shared_quad_state->quad_to_target_transform,
257 quad->rect);
258 gfx::Transform contents_device_transform =
259 frame->window_matrix * frame->projection_matrix * quad_rect_matrix;
260 contents_device_transform.FlattenTo2d();
261 SkMatrix sk_device_matrix;
262 gfx::TransformToFlattenedSkMatrix(contents_device_transform,
263 &sk_device_matrix);
264 current_canvas_->setMatrix(sk_device_matrix);
266 current_paint_.reset();
267 if (settings_->force_antialiasing ||
268 !IsScaleAndIntegerTranslate(sk_device_matrix)) {
269 // TODO(danakj): Until we can enable AA only on exterior edges of the
270 // layer, disable AA if any interior edges are present. crbug.com/248175
271 bool all_four_edges_are_exterior = quad->IsTopEdge() &&
272 quad->IsLeftEdge() &&
273 quad->IsBottomEdge() &&
274 quad->IsRightEdge();
275 if (settings_->allow_antialiasing &&
276 (settings_->force_antialiasing || all_four_edges_are_exterior))
277 current_paint_.setAntiAlias(true);
278 current_paint_.setFilterQuality(kLow_SkFilterQuality);
281 if (quad->ShouldDrawWithBlending() ||
282 quad->shared_quad_state->blend_mode != SkXfermode::kSrcOver_Mode) {
283 current_paint_.setAlpha(quad->shared_quad_state->opacity * 255);
284 current_paint_.setXfermodeMode(quad->shared_quad_state->blend_mode);
285 } else {
286 current_paint_.setXfermodeMode(SkXfermode::kSrc_Mode);
289 if (draw_region) {
290 gfx::QuadF local_draw_region(*draw_region);
291 SkPath draw_region_clip_path;
292 local_draw_region -=
293 gfx::Vector2dF(quad->visible_rect.x(), quad->visible_rect.y());
294 local_draw_region.Scale(1.0f / quad->visible_rect.width(),
295 1.0f / quad->visible_rect.height());
296 local_draw_region -= gfx::Vector2dF(0.5f, 0.5f);
298 SkPoint clip_points[4];
299 QuadFToSkPoints(local_draw_region, clip_points);
300 draw_region_clip_path.addPoly(clip_points, 4, true);
302 current_canvas_->clipPath(draw_region_clip_path, SkRegion::kIntersect_Op,
303 false);
306 switch (quad->material) {
307 case DrawQuad::DEBUG_BORDER:
308 DrawDebugBorderQuad(frame, DebugBorderDrawQuad::MaterialCast(quad));
309 break;
310 case DrawQuad::PICTURE_CONTENT:
311 DrawPictureQuad(frame, PictureDrawQuad::MaterialCast(quad));
312 break;
313 case DrawQuad::RENDER_PASS:
314 DrawRenderPassQuad(frame, RenderPassDrawQuad::MaterialCast(quad));
315 break;
316 case DrawQuad::SOLID_COLOR:
317 DrawSolidColorQuad(frame, SolidColorDrawQuad::MaterialCast(quad));
318 break;
319 case DrawQuad::TEXTURE_CONTENT:
320 DrawTextureQuad(frame, TextureDrawQuad::MaterialCast(quad));
321 break;
322 case DrawQuad::TILED_CONTENT:
323 DrawTileQuad(frame, TileDrawQuad::MaterialCast(quad));
324 break;
325 case DrawQuad::SURFACE_CONTENT:
326 // Surface content should be fully resolved to other quad types before
327 // reaching a direct renderer.
328 NOTREACHED();
329 break;
330 case DrawQuad::INVALID:
331 case DrawQuad::IO_SURFACE_CONTENT:
332 case DrawQuad::YUV_VIDEO_CONTENT:
333 case DrawQuad::STREAM_VIDEO_CONTENT:
334 DrawUnsupportedQuad(frame, quad);
335 NOTREACHED();
336 break;
339 current_canvas_->resetMatrix();
340 if (draw_region) {
341 current_canvas_->restore();
345 void SoftwareRenderer::DrawDebugBorderQuad(const DrawingFrame* frame,
346 const DebugBorderDrawQuad* quad) {
347 // We need to apply the matrix manually to have pixel-sized stroke width.
348 SkPoint vertices[4];
349 gfx::RectFToSkRect(QuadVertexRect()).toQuad(vertices);
350 SkPoint transformed_vertices[4];
351 current_canvas_->getTotalMatrix().mapPoints(transformed_vertices,
352 vertices,
354 current_canvas_->resetMatrix();
356 current_paint_.setColor(quad->color);
357 current_paint_.setAlpha(quad->shared_quad_state->opacity *
358 SkColorGetA(quad->color));
359 current_paint_.setStyle(SkPaint::kStroke_Style);
360 current_paint_.setStrokeWidth(quad->width);
361 current_canvas_->drawPoints(SkCanvas::kPolygon_PointMode,
362 4, transformed_vertices, current_paint_);
365 void SoftwareRenderer::DrawPictureQuad(const DrawingFrame* frame,
366 const PictureDrawQuad* quad) {
367 SkMatrix content_matrix;
368 content_matrix.setRectToRect(
369 gfx::RectFToSkRect(quad->tex_coord_rect),
370 gfx::RectFToSkRect(QuadVertexRect()),
371 SkMatrix::kFill_ScaleToFit);
372 current_canvas_->concat(content_matrix);
374 const bool needs_transparency =
375 SkScalarRoundToInt(quad->shared_quad_state->opacity * 255) < 255;
376 const bool disable_image_filtering =
377 frame->disable_picture_quad_image_filtering || quad->nearest_neighbor;
379 TRACE_EVENT0("cc", "SoftwareRenderer::DrawPictureQuad");
381 if (needs_transparency || disable_image_filtering) {
382 // TODO(aelias): This isn't correct in all cases. We should detect these
383 // cases and fall back to a persistent bitmap backing
384 // (http://crbug.com/280374).
385 skia::OpacityFilterCanvas filtered_canvas(current_canvas_,
386 quad->shared_quad_state->opacity,
387 disable_image_filtering);
388 quad->raster_source->PlaybackToSharedCanvas(
389 &filtered_canvas, quad->content_rect, quad->contents_scale);
390 } else {
391 quad->raster_source->PlaybackToSharedCanvas(
392 current_canvas_, quad->content_rect, quad->contents_scale);
396 void SoftwareRenderer::DrawSolidColorQuad(const DrawingFrame* frame,
397 const SolidColorDrawQuad* quad) {
398 gfx::RectF visible_quad_vertex_rect = MathUtil::ScaleRectProportional(
399 QuadVertexRect(), quad->rect, quad->visible_rect);
400 current_paint_.setColor(quad->color);
401 current_paint_.setAlpha(quad->shared_quad_state->opacity *
402 SkColorGetA(quad->color));
403 current_canvas_->drawRect(gfx::RectFToSkRect(visible_quad_vertex_rect),
404 current_paint_);
407 void SoftwareRenderer::DrawTextureQuad(const DrawingFrame* frame,
408 const TextureDrawQuad* quad) {
409 if (!IsSoftwareResource(quad->resource_id())) {
410 DrawUnsupportedQuad(frame, quad);
411 return;
414 // TODO(skaslev): Add support for non-premultiplied alpha.
415 ResourceProvider::ScopedReadLockSoftware lock(resource_provider_,
416 quad->resource_id());
417 if (!lock.valid())
418 return;
419 const SkBitmap* bitmap = lock.sk_bitmap();
420 gfx::RectF uv_rect = gfx::ScaleRect(gfx::BoundingRect(quad->uv_top_left,
421 quad->uv_bottom_right),
422 bitmap->width(),
423 bitmap->height());
424 gfx::RectF visible_uv_rect =
425 MathUtil::ScaleRectProportional(uv_rect, quad->rect, quad->visible_rect);
426 SkRect sk_uv_rect = gfx::RectFToSkRect(visible_uv_rect);
427 gfx::RectF visible_quad_vertex_rect = MathUtil::ScaleRectProportional(
428 QuadVertexRect(), quad->rect, quad->visible_rect);
429 SkRect quad_rect = gfx::RectFToSkRect(visible_quad_vertex_rect);
431 if (quad->y_flipped)
432 current_canvas_->scale(1, -1);
434 bool blend_background = quad->background_color != SK_ColorTRANSPARENT &&
435 !bitmap->isOpaque();
436 bool needs_layer = blend_background && (current_paint_.getAlpha() != 0xFF);
437 if (needs_layer) {
438 current_canvas_->saveLayerAlpha(&quad_rect, current_paint_.getAlpha());
439 current_paint_.setAlpha(0xFF);
441 if (blend_background) {
442 SkPaint background_paint;
443 background_paint.setColor(quad->background_color);
444 current_canvas_->drawRect(quad_rect, background_paint);
446 current_paint_.setFilterQuality(
447 quad->nearest_neighbor ? kNone_SkFilterQuality : kLow_SkFilterQuality);
448 SkShader::TileMode tile_mode = WrapModeToTileMode(lock.wrap_mode());
449 if (tile_mode != SkShader::kClamp_TileMode) {
450 SkMatrix matrix;
451 matrix.setRectToRect(sk_uv_rect, quad_rect, SkMatrix::kFill_ScaleToFit);
452 skia::RefPtr<SkShader> shader = skia::AdoptRef(
453 SkShader::CreateBitmapShader(*bitmap, tile_mode, tile_mode, &matrix));
454 SkPaint paint;
455 paint.setStyle(SkPaint::kFill_Style);
456 paint.setShader(shader.get());
457 current_canvas_->drawRect(quad_rect, paint);
458 } else {
459 current_canvas_->drawBitmapRect(*bitmap, &sk_uv_rect, quad_rect,
460 &current_paint_);
463 if (needs_layer)
464 current_canvas_->restore();
467 void SoftwareRenderer::DrawTileQuad(const DrawingFrame* frame,
468 const TileDrawQuad* quad) {
469 // |resource_provider_| can be NULL in resourceless software draws, which
470 // should never produce tile quads in the first place.
471 DCHECK(resource_provider_);
472 DCHECK(IsSoftwareResource(quad->resource_id()));
474 ResourceProvider::ScopedReadLockSoftware lock(resource_provider_,
475 quad->resource_id());
476 if (!lock.valid())
477 return;
478 DCHECK_EQ(GL_CLAMP_TO_EDGE, lock.wrap_mode());
480 gfx::RectF visible_tex_coord_rect = MathUtil::ScaleRectProportional(
481 quad->tex_coord_rect, quad->rect, quad->visible_rect);
482 gfx::RectF visible_quad_vertex_rect = MathUtil::ScaleRectProportional(
483 QuadVertexRect(), quad->rect, quad->visible_rect);
485 SkRect uv_rect = gfx::RectFToSkRect(visible_tex_coord_rect);
486 current_paint_.setFilterQuality(
487 quad->nearest_neighbor ? kNone_SkFilterQuality : kLow_SkFilterQuality);
488 current_canvas_->drawBitmapRect(*lock.sk_bitmap(), &uv_rect,
489 gfx::RectFToSkRect(visible_quad_vertex_rect),
490 &current_paint_);
493 void SoftwareRenderer::DrawRenderPassQuad(const DrawingFrame* frame,
494 const RenderPassDrawQuad* quad) {
495 ScopedResource* content_texture =
496 render_pass_textures_.get(quad->render_pass_id);
497 DCHECK(content_texture);
498 DCHECK(content_texture->id());
499 DCHECK(IsSoftwareResource(content_texture->id()));
501 ResourceProvider::ScopedReadLockSoftware lock(resource_provider_,
502 content_texture->id());
503 if (!lock.valid())
504 return;
505 SkShader::TileMode content_tile_mode = WrapModeToTileMode(lock.wrap_mode());
507 SkRect dest_rect = gfx::RectFToSkRect(QuadVertexRect());
508 SkRect dest_visible_rect = gfx::RectFToSkRect(MathUtil::ScaleRectProportional(
509 QuadVertexRect(), quad->rect, quad->visible_rect));
510 SkRect content_rect = SkRect::MakeWH(quad->rect.width(), quad->rect.height());
512 SkMatrix content_mat;
513 content_mat.setRectToRect(content_rect, dest_rect,
514 SkMatrix::kFill_ScaleToFit);
516 const SkBitmap* content = lock.sk_bitmap();
518 SkBitmap filter_bitmap;
519 if (!quad->filters.IsEmpty()) {
520 skia::RefPtr<SkImageFilter> filter = RenderSurfaceFilters::BuildImageFilter(
521 quad->filters, content_texture->size());
522 // TODO(ajuma): Apply the filter in the same pass as the content where
523 // possible (e.g. when there's no origin offset). See crbug.com/308201.
524 if (filter) {
525 SkImageInfo info = SkImageInfo::MakeN32Premul(
526 content_texture->size().width(), content_texture->size().height());
527 if (filter_bitmap.tryAllocPixels(info)) {
528 SkCanvas canvas(filter_bitmap);
529 SkPaint paint;
530 paint.setImageFilter(filter.get());
531 canvas.clear(SK_ColorTRANSPARENT);
532 canvas.translate(SkIntToScalar(-quad->rect.origin().x()),
533 SkIntToScalar(-quad->rect.origin().y()));
534 canvas.scale(quad->filters_scale.x(), quad->filters_scale.y());
535 canvas.drawSprite(*content, 0, 0, &paint);
540 skia::RefPtr<SkShader> shader;
541 if (filter_bitmap.isNull()) {
542 shader = skia::AdoptRef(SkShader::CreateBitmapShader(
543 *content, content_tile_mode, content_tile_mode, &content_mat));
544 } else {
545 shader = skia::AdoptRef(SkShader::CreateBitmapShader(
546 filter_bitmap, content_tile_mode, content_tile_mode, &content_mat));
548 current_paint_.setShader(shader.get());
550 if (quad->mask_resource_id()) {
551 ResourceProvider::ScopedReadLockSoftware mask_lock(
552 resource_provider_, quad->mask_resource_id());
553 if (!lock.valid())
554 return;
555 SkShader::TileMode mask_tile_mode = WrapModeToTileMode(
556 mask_lock.wrap_mode());
558 const SkBitmap* mask = mask_lock.sk_bitmap();
560 // Scale normalized uv rect into absolute texel coordinates.
561 SkRect mask_rect =
562 gfx::RectFToSkRect(gfx::ScaleRect(quad->MaskUVRect(),
563 quad->mask_texture_size.width(),
564 quad->mask_texture_size.height()));
566 SkMatrix mask_mat;
567 mask_mat.setRectToRect(mask_rect, dest_rect, SkMatrix::kFill_ScaleToFit);
569 skia::RefPtr<SkShader> mask_shader =
570 skia::AdoptRef(SkShader::CreateBitmapShader(
571 *mask, mask_tile_mode, mask_tile_mode, &mask_mat));
573 SkPaint mask_paint;
574 mask_paint.setShader(mask_shader.get());
576 SkLayerRasterizer::Builder builder;
577 builder.addLayer(mask_paint);
579 skia::RefPtr<SkLayerRasterizer> mask_rasterizer =
580 skia::AdoptRef(builder.detachRasterizer());
582 current_paint_.setRasterizer(mask_rasterizer.get());
583 current_canvas_->drawRect(dest_visible_rect, current_paint_);
584 } else {
585 // TODO(skaslev): Apply background filters
586 current_canvas_->drawRect(dest_visible_rect, current_paint_);
590 void SoftwareRenderer::DrawUnsupportedQuad(const DrawingFrame* frame,
591 const DrawQuad* quad) {
592 #ifdef NDEBUG
593 current_paint_.setColor(SK_ColorWHITE);
594 #else
595 current_paint_.setColor(SK_ColorMAGENTA);
596 #endif
597 current_paint_.setAlpha(quad->shared_quad_state->opacity * 255);
598 current_canvas_->drawRect(gfx::RectFToSkRect(QuadVertexRect()),
599 current_paint_);
602 void SoftwareRenderer::CopyCurrentRenderPassToBitmap(
603 DrawingFrame* frame,
604 scoped_ptr<CopyOutputRequest> request) {
605 gfx::Rect copy_rect = frame->current_render_pass->output_rect;
606 if (request->has_area())
607 copy_rect.Intersect(request->area());
608 gfx::Rect window_copy_rect = MoveFromDrawToWindowSpace(frame, copy_rect);
610 scoped_ptr<SkBitmap> bitmap(new SkBitmap);
611 bitmap->setInfo(SkImageInfo::MakeN32Premul(window_copy_rect.width(),
612 window_copy_rect.height()));
613 current_canvas_->readPixels(
614 bitmap.get(), window_copy_rect.x(), window_copy_rect.y());
616 request->SendBitmapResult(bitmap.Pass());
619 void SoftwareRenderer::DiscardBackbuffer() {
620 if (is_backbuffer_discarded_)
621 return;
623 output_surface_->DiscardBackbuffer();
625 is_backbuffer_discarded_ = true;
627 // Damage tracker needs a full reset every time framebuffer is discarded.
628 client_->SetFullRootLayerDamage();
631 void SoftwareRenderer::EnsureBackbuffer() {
632 if (!is_backbuffer_discarded_)
633 return;
635 output_surface_->EnsureBackbuffer();
636 is_backbuffer_discarded_ = false;
639 void SoftwareRenderer::DidChangeVisibility() {
640 if (visible())
641 EnsureBackbuffer();
642 else
643 DiscardBackbuffer();
646 } // namespace cc