Update V8 to version 4.5.107.
[chromium-blink-merge.git] / cc / output / software_renderer.cc
bloba8110efb4dc8b8b8d5dd98bb14df5ff6b5db67ed
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/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_filter_canvas.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/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 current_frame_data_.reset(new SoftwareFrameData);
117 output_device_->EndPaint(current_frame_data_.get());
120 void SoftwareRenderer::SwapBuffers(const CompositorFrameMetadata& metadata) {
121 TRACE_EVENT0("cc,benchmark", "SoftwareRenderer::SwapBuffers");
122 CompositorFrame compositor_frame;
123 compositor_frame.metadata = metadata;
124 compositor_frame.software_frame_data = current_frame_data_.Pass();
125 output_surface_->SwapBuffers(&compositor_frame);
128 void SoftwareRenderer::ReceiveSwapBuffersAck(const CompositorFrameAck& ack) {
129 output_device_->ReclaimSoftwareFrame(ack.last_software_frame_id);
132 bool SoftwareRenderer::FlippedFramebuffer(const DrawingFrame* frame) const {
133 return false;
136 void SoftwareRenderer::EnsureScissorTestEnabled() {
137 is_scissor_enabled_ = true;
138 SetClipRect(scissor_rect_);
141 void SoftwareRenderer::EnsureScissorTestDisabled() {
142 // There is no explicit notion of enabling/disabling scissoring in software
143 // rendering, but the underlying effect we want is to clear any existing
144 // clipRect on the current SkCanvas. This is done by setting clipRect to
145 // the viewport's dimensions.
146 is_scissor_enabled_ = false;
147 SkISize size = current_canvas_->getDeviceSize();
148 SetClipRect(gfx::Rect(size.width(), size.height()));
151 void SoftwareRenderer::Finish() {}
153 void SoftwareRenderer::BindFramebufferToOutputSurface(DrawingFrame* frame) {
154 DCHECK(!output_surface_->HasExternalStencilTest());
155 current_framebuffer_lock_ = nullptr;
156 current_framebuffer_canvas_.clear();
157 current_canvas_ = root_canvas_;
160 bool SoftwareRenderer::BindFramebufferToTexture(
161 DrawingFrame* frame,
162 const ScopedResource* texture,
163 const gfx::Rect& target_rect) {
164 DCHECK(texture->id());
166 // Explicitly release lock, otherwise we can crash when try to lock
167 // same texture again.
168 current_framebuffer_lock_ = nullptr;
169 current_framebuffer_lock_ = make_scoped_ptr(
170 new ResourceProvider::ScopedWriteLockSoftware(
171 resource_provider_, texture->id()));
172 current_framebuffer_canvas_ =
173 skia::AdoptRef(new SkCanvas(current_framebuffer_lock_->sk_bitmap()));
174 current_canvas_ = current_framebuffer_canvas_.get();
175 return true;
178 void SoftwareRenderer::SetScissorTestRect(const gfx::Rect& scissor_rect) {
179 is_scissor_enabled_ = true;
180 scissor_rect_ = scissor_rect;
181 SetClipRect(scissor_rect);
184 void SoftwareRenderer::SetClipRect(const gfx::Rect& rect) {
185 // Skia applies the current matrix to clip rects so we reset it temporary.
186 SkMatrix current_matrix = current_canvas_->getTotalMatrix();
187 current_canvas_->resetMatrix();
188 current_canvas_->clipRect(gfx::RectToSkRect(rect), SkRegion::kReplace_Op);
189 current_canvas_->setMatrix(current_matrix);
192 void SoftwareRenderer::ClearCanvas(SkColor color) {
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 (draw_region) {
248 current_canvas_->save();
251 TRACE_EVENT0("cc", "SoftwareRenderer::DoDrawQuad");
252 gfx::Transform quad_rect_matrix;
253 QuadRectTransform(&quad_rect_matrix,
254 quad->shared_quad_state->quad_to_target_transform,
255 quad->rect);
256 gfx::Transform contents_device_transform =
257 frame->window_matrix * frame->projection_matrix * quad_rect_matrix;
258 contents_device_transform.FlattenTo2d();
259 SkMatrix sk_device_matrix;
260 gfx::TransformToFlattenedSkMatrix(contents_device_transform,
261 &sk_device_matrix);
262 current_canvas_->setMatrix(sk_device_matrix);
264 current_paint_.reset();
265 if (settings_->force_antialiasing ||
266 !IsScaleAndIntegerTranslate(sk_device_matrix)) {
267 // TODO(danakj): Until we can enable AA only on exterior edges of the
268 // layer, disable AA if any interior edges are present. crbug.com/248175
269 bool all_four_edges_are_exterior = quad->IsTopEdge() &&
270 quad->IsLeftEdge() &&
271 quad->IsBottomEdge() &&
272 quad->IsRightEdge();
273 if (settings_->allow_antialiasing &&
274 (settings_->force_antialiasing || all_four_edges_are_exterior))
275 current_paint_.setAntiAlias(true);
276 current_paint_.setFilterQuality(kLow_SkFilterQuality);
279 if (quad->ShouldDrawWithBlending() ||
280 quad->shared_quad_state->blend_mode != SkXfermode::kSrcOver_Mode) {
281 current_paint_.setAlpha(quad->shared_quad_state->opacity * 255);
282 current_paint_.setXfermodeMode(quad->shared_quad_state->blend_mode);
283 } else {
284 current_paint_.setXfermodeMode(SkXfermode::kSrc_Mode);
287 if (draw_region) {
288 gfx::QuadF local_draw_region(*draw_region);
289 SkPath draw_region_clip_path;
290 local_draw_region -=
291 gfx::Vector2dF(quad->visible_rect.x(), quad->visible_rect.y());
292 local_draw_region.Scale(1.0f / quad->visible_rect.width(),
293 1.0f / quad->visible_rect.height());
294 local_draw_region -= gfx::Vector2dF(0.5f, 0.5f);
296 SkPoint clip_points[4];
297 QuadFToSkPoints(local_draw_region, clip_points);
298 draw_region_clip_path.addPoly(clip_points, 4, true);
300 current_canvas_->clipPath(draw_region_clip_path, SkRegion::kIntersect_Op,
301 false);
304 switch (quad->material) {
305 case DrawQuad::CHECKERBOARD:
306 // TODO(enne) For now since checkerboards shouldn't be part of a 3D
307 // context, clipping regions aren't supported so we skip drawing them
308 // if this becomes the case.
309 if (!draw_region) {
310 DrawCheckerboardQuad(frame, CheckerboardDrawQuad::MaterialCast(quad));
312 break;
313 case DrawQuad::DEBUG_BORDER:
314 DrawDebugBorderQuad(frame, DebugBorderDrawQuad::MaterialCast(quad));
315 break;
316 case DrawQuad::PICTURE_CONTENT:
317 DrawPictureQuad(frame, PictureDrawQuad::MaterialCast(quad));
318 break;
319 case DrawQuad::RENDER_PASS:
320 DrawRenderPassQuad(frame, RenderPassDrawQuad::MaterialCast(quad));
321 break;
322 case DrawQuad::SOLID_COLOR:
323 DrawSolidColorQuad(frame, SolidColorDrawQuad::MaterialCast(quad));
324 break;
325 case DrawQuad::TEXTURE_CONTENT:
326 DrawTextureQuad(frame, TextureDrawQuad::MaterialCast(quad));
327 break;
328 case DrawQuad::TILED_CONTENT:
329 DrawTileQuad(frame, TileDrawQuad::MaterialCast(quad));
330 break;
331 case DrawQuad::SURFACE_CONTENT:
332 // Surface content should be fully resolved to other quad types before
333 // reaching a direct renderer.
334 NOTREACHED();
335 break;
336 case DrawQuad::INVALID:
337 case DrawQuad::IO_SURFACE_CONTENT:
338 case DrawQuad::YUV_VIDEO_CONTENT:
339 case DrawQuad::STREAM_VIDEO_CONTENT:
340 DrawUnsupportedQuad(frame, quad);
341 NOTREACHED();
342 break;
345 current_canvas_->resetMatrix();
346 if (draw_region) {
347 current_canvas_->restore();
351 void SoftwareRenderer::DrawCheckerboardQuad(const DrawingFrame* frame,
352 const CheckerboardDrawQuad* quad) {
353 gfx::RectF visible_quad_vertex_rect = MathUtil::ScaleRectProportional(
354 QuadVertexRect(), quad->rect, quad->visible_rect);
355 current_paint_.setColor(quad->color);
356 current_paint_.setAlpha(quad->shared_quad_state->opacity);
357 current_canvas_->drawRect(gfx::RectFToSkRect(visible_quad_vertex_rect),
358 current_paint_);
361 void SoftwareRenderer::DrawDebugBorderQuad(const DrawingFrame* frame,
362 const DebugBorderDrawQuad* quad) {
363 // We need to apply the matrix manually to have pixel-sized stroke width.
364 SkPoint vertices[4];
365 gfx::RectFToSkRect(QuadVertexRect()).toQuad(vertices);
366 SkPoint transformed_vertices[4];
367 current_canvas_->getTotalMatrix().mapPoints(transformed_vertices,
368 vertices,
370 current_canvas_->resetMatrix();
372 current_paint_.setColor(quad->color);
373 current_paint_.setAlpha(quad->shared_quad_state->opacity *
374 SkColorGetA(quad->color));
375 current_paint_.setStyle(SkPaint::kStroke_Style);
376 current_paint_.setStrokeWidth(quad->width);
377 current_canvas_->drawPoints(SkCanvas::kPolygon_PointMode,
378 4, transformed_vertices, current_paint_);
381 void SoftwareRenderer::DrawPictureQuad(const DrawingFrame* frame,
382 const PictureDrawQuad* quad) {
383 SkMatrix content_matrix;
384 content_matrix.setRectToRect(
385 gfx::RectFToSkRect(quad->tex_coord_rect),
386 gfx::RectFToSkRect(QuadVertexRect()),
387 SkMatrix::kFill_ScaleToFit);
388 current_canvas_->concat(content_matrix);
390 const bool needs_transparency =
391 SkScalarRoundToInt(quad->shared_quad_state->opacity * 255) < 255;
392 const bool disable_image_filtering =
393 frame->disable_picture_quad_image_filtering || quad->nearest_neighbor;
395 TRACE_EVENT0("cc", "SoftwareRenderer::DrawPictureQuad");
397 if (needs_transparency || disable_image_filtering) {
398 // TODO(aelias): This isn't correct in all cases. We should detect these
399 // cases and fall back to a persistent bitmap backing
400 // (http://crbug.com/280374).
401 skia::OpacityFilterCanvas filtered_canvas(current_canvas_,
402 quad->shared_quad_state->opacity,
403 disable_image_filtering);
404 quad->raster_source->PlaybackToSharedCanvas(
405 &filtered_canvas, quad->content_rect, quad->contents_scale);
406 } else {
407 quad->raster_source->PlaybackToSharedCanvas(
408 current_canvas_, quad->content_rect, quad->contents_scale);
412 void SoftwareRenderer::DrawSolidColorQuad(const DrawingFrame* frame,
413 const SolidColorDrawQuad* quad) {
414 gfx::RectF visible_quad_vertex_rect = MathUtil::ScaleRectProportional(
415 QuadVertexRect(), quad->rect, quad->visible_rect);
416 current_paint_.setColor(quad->color);
417 current_paint_.setAlpha(quad->shared_quad_state->opacity *
418 SkColorGetA(quad->color));
419 current_canvas_->drawRect(gfx::RectFToSkRect(visible_quad_vertex_rect),
420 current_paint_);
423 void SoftwareRenderer::DrawTextureQuad(const DrawingFrame* frame,
424 const TextureDrawQuad* quad) {
425 if (!IsSoftwareResource(quad->resource_id())) {
426 DrawUnsupportedQuad(frame, quad);
427 return;
430 // TODO(skaslev): Add support for non-premultiplied alpha.
431 ResourceProvider::ScopedReadLockSoftware lock(resource_provider_,
432 quad->resource_id());
433 if (!lock.valid())
434 return;
435 const SkBitmap* bitmap = lock.sk_bitmap();
436 gfx::RectF uv_rect = gfx::ScaleRect(gfx::BoundingRect(quad->uv_top_left,
437 quad->uv_bottom_right),
438 bitmap->width(),
439 bitmap->height());
440 gfx::RectF visible_uv_rect =
441 MathUtil::ScaleRectProportional(uv_rect, quad->rect, quad->visible_rect);
442 SkRect sk_uv_rect = gfx::RectFToSkRect(visible_uv_rect);
443 gfx::RectF visible_quad_vertex_rect = MathUtil::ScaleRectProportional(
444 QuadVertexRect(), quad->rect, quad->visible_rect);
445 SkRect quad_rect = gfx::RectFToSkRect(visible_quad_vertex_rect);
447 if (quad->y_flipped)
448 current_canvas_->scale(1, -1);
450 bool blend_background = quad->background_color != SK_ColorTRANSPARENT &&
451 !bitmap->isOpaque();
452 bool needs_layer = blend_background && (current_paint_.getAlpha() != 0xFF);
453 if (needs_layer) {
454 current_canvas_->saveLayerAlpha(&quad_rect, current_paint_.getAlpha());
455 current_paint_.setAlpha(0xFF);
457 if (blend_background) {
458 SkPaint background_paint;
459 background_paint.setColor(quad->background_color);
460 current_canvas_->drawRect(quad_rect, background_paint);
462 current_paint_.setFilterQuality(
463 quad->nearest_neighbor ? kNone_SkFilterQuality : kLow_SkFilterQuality);
464 SkShader::TileMode tile_mode = WrapModeToTileMode(lock.wrap_mode());
465 if (tile_mode != SkShader::kClamp_TileMode) {
466 SkMatrix matrix;
467 matrix.setRectToRect(sk_uv_rect, quad_rect, SkMatrix::kFill_ScaleToFit);
468 skia::RefPtr<SkShader> shader = skia::AdoptRef(
469 SkShader::CreateBitmapShader(*bitmap, tile_mode, tile_mode, &matrix));
470 SkPaint paint;
471 paint.setStyle(SkPaint::kFill_Style);
472 paint.setShader(shader.get());
473 current_canvas_->drawRect(quad_rect, paint);
474 } else {
475 current_canvas_->drawBitmapRectToRect(*bitmap,
476 &sk_uv_rect,
477 quad_rect,
478 &current_paint_);
481 if (needs_layer)
482 current_canvas_->restore();
485 void SoftwareRenderer::DrawTileQuad(const DrawingFrame* frame,
486 const TileDrawQuad* quad) {
487 // |resource_provider_| can be NULL in resourceless software draws, which
488 // should never produce tile quads in the first place.
489 DCHECK(resource_provider_);
490 DCHECK(IsSoftwareResource(quad->resource_id()));
492 ResourceProvider::ScopedReadLockSoftware lock(resource_provider_,
493 quad->resource_id());
494 if (!lock.valid())
495 return;
496 DCHECK_EQ(GL_CLAMP_TO_EDGE, lock.wrap_mode());
498 gfx::RectF visible_tex_coord_rect = MathUtil::ScaleRectProportional(
499 quad->tex_coord_rect, quad->rect, quad->visible_rect);
500 gfx::RectF visible_quad_vertex_rect = MathUtil::ScaleRectProportional(
501 QuadVertexRect(), quad->rect, quad->visible_rect);
503 SkRect uv_rect = gfx::RectFToSkRect(visible_tex_coord_rect);
504 current_paint_.setFilterQuality(
505 quad->nearest_neighbor ? kNone_SkFilterQuality : kLow_SkFilterQuality);
506 current_canvas_->drawBitmapRectToRect(
507 *lock.sk_bitmap(),
508 &uv_rect,
509 gfx::RectFToSkRect(visible_quad_vertex_rect),
510 &current_paint_);
513 void SoftwareRenderer::DrawRenderPassQuad(const DrawingFrame* frame,
514 const RenderPassDrawQuad* quad) {
515 ScopedResource* content_texture =
516 render_pass_textures_.get(quad->render_pass_id);
517 DCHECK(content_texture);
518 DCHECK(content_texture->id());
519 DCHECK(IsSoftwareResource(content_texture->id()));
521 ResourceProvider::ScopedReadLockSoftware lock(resource_provider_,
522 content_texture->id());
523 if (!lock.valid())
524 return;
525 SkShader::TileMode content_tile_mode = WrapModeToTileMode(lock.wrap_mode());
527 SkRect dest_rect = gfx::RectFToSkRect(QuadVertexRect());
528 SkRect dest_visible_rect = gfx::RectFToSkRect(MathUtil::ScaleRectProportional(
529 QuadVertexRect(), quad->rect, quad->visible_rect));
530 SkRect content_rect = SkRect::MakeWH(quad->rect.width(), quad->rect.height());
532 SkMatrix content_mat;
533 content_mat.setRectToRect(content_rect, dest_rect,
534 SkMatrix::kFill_ScaleToFit);
536 const SkBitmap* content = lock.sk_bitmap();
538 SkBitmap filter_bitmap;
539 if (!quad->filters.IsEmpty()) {
540 skia::RefPtr<SkImageFilter> filter = RenderSurfaceFilters::BuildImageFilter(
541 quad->filters, content_texture->size());
542 // TODO(ajuma): Apply the filter in the same pass as the content where
543 // possible (e.g. when there's no origin offset). See crbug.com/308201.
544 if (filter) {
545 SkImageInfo info = SkImageInfo::MakeN32Premul(
546 content_texture->size().width(), content_texture->size().height());
547 if (filter_bitmap.tryAllocPixels(info)) {
548 SkCanvas canvas(filter_bitmap);
549 SkPaint paint;
550 paint.setImageFilter(filter.get());
551 canvas.clear(SK_ColorTRANSPARENT);
552 canvas.translate(SkIntToScalar(-quad->rect.origin().x()),
553 SkIntToScalar(-quad->rect.origin().y()));
554 canvas.scale(quad->filters_scale.x(), quad->filters_scale.y());
555 canvas.drawSprite(*content, 0, 0, &paint);
560 skia::RefPtr<SkShader> shader;
561 if (filter_bitmap.isNull()) {
562 shader = skia::AdoptRef(SkShader::CreateBitmapShader(
563 *content, content_tile_mode, content_tile_mode, &content_mat));
564 } else {
565 shader = skia::AdoptRef(SkShader::CreateBitmapShader(
566 filter_bitmap, content_tile_mode, content_tile_mode, &content_mat));
568 current_paint_.setShader(shader.get());
570 if (quad->mask_resource_id()) {
571 ResourceProvider::ScopedReadLockSoftware mask_lock(
572 resource_provider_, quad->mask_resource_id());
573 if (!lock.valid())
574 return;
575 SkShader::TileMode mask_tile_mode = WrapModeToTileMode(
576 mask_lock.wrap_mode());
578 const SkBitmap* mask = mask_lock.sk_bitmap();
580 // Scale normalized uv rect into absolute texel coordinates.
581 SkRect mask_rect =
582 gfx::RectFToSkRect(gfx::ScaleRect(quad->MaskUVRect(),
583 quad->mask_texture_size.width(),
584 quad->mask_texture_size.height()));
586 SkMatrix mask_mat;
587 mask_mat.setRectToRect(mask_rect, dest_rect, SkMatrix::kFill_ScaleToFit);
589 skia::RefPtr<SkShader> mask_shader =
590 skia::AdoptRef(SkShader::CreateBitmapShader(
591 *mask, mask_tile_mode, mask_tile_mode, &mask_mat));
593 SkPaint mask_paint;
594 mask_paint.setShader(mask_shader.get());
596 SkLayerRasterizer::Builder builder;
597 builder.addLayer(mask_paint);
599 skia::RefPtr<SkLayerRasterizer> mask_rasterizer =
600 skia::AdoptRef(builder.detachRasterizer());
602 current_paint_.setRasterizer(mask_rasterizer.get());
603 current_canvas_->drawRect(dest_visible_rect, current_paint_);
604 } else {
605 // TODO(skaslev): Apply background filters
606 current_canvas_->drawRect(dest_visible_rect, current_paint_);
610 void SoftwareRenderer::DrawUnsupportedQuad(const DrawingFrame* frame,
611 const DrawQuad* quad) {
612 #ifdef NDEBUG
613 current_paint_.setColor(SK_ColorWHITE);
614 #else
615 current_paint_.setColor(SK_ColorMAGENTA);
616 #endif
617 current_paint_.setAlpha(quad->shared_quad_state->opacity * 255);
618 current_canvas_->drawRect(gfx::RectFToSkRect(QuadVertexRect()),
619 current_paint_);
622 void SoftwareRenderer::CopyCurrentRenderPassToBitmap(
623 DrawingFrame* frame,
624 scoped_ptr<CopyOutputRequest> request) {
625 gfx::Rect copy_rect = frame->current_render_pass->output_rect;
626 if (request->has_area())
627 copy_rect.Intersect(request->area());
628 gfx::Rect window_copy_rect = MoveFromDrawToWindowSpace(frame, copy_rect);
630 scoped_ptr<SkBitmap> bitmap(new SkBitmap);
631 bitmap->setInfo(SkImageInfo::MakeN32Premul(window_copy_rect.width(),
632 window_copy_rect.height()));
633 current_canvas_->readPixels(
634 bitmap.get(), window_copy_rect.x(), window_copy_rect.y());
636 request->SendBitmapResult(bitmap.Pass());
639 void SoftwareRenderer::DiscardBackbuffer() {
640 if (is_backbuffer_discarded_)
641 return;
643 output_surface_->DiscardBackbuffer();
645 is_backbuffer_discarded_ = true;
647 // Damage tracker needs a full reset every time framebuffer is discarded.
648 client_->SetFullRootLayerDamage();
651 void SoftwareRenderer::EnsureBackbuffer() {
652 if (!is_backbuffer_discarded_)
653 return;
655 output_surface_->EnsureBackbuffer();
656 is_backbuffer_discarded_ = false;
659 void SoftwareRenderer::DidChangeVisibility() {
660 if (visible())
661 EnsureBackbuffer();
662 else
663 DiscardBackbuffer();
666 } // namespace cc