Don't show supervised user as "already on this device" while they're being imported.
[chromium-blink-merge.git] / cc / output / software_renderer.cc
blob3381ab19e022390cd5fca50cfa9b126532cad17b
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_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/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, quad->quadTransform(), quad->rect);
254 gfx::Transform contents_device_transform =
255 frame->window_matrix * frame->projection_matrix * quad_rect_matrix;
256 contents_device_transform.FlattenTo2d();
257 SkMatrix sk_device_matrix;
258 gfx::TransformToFlattenedSkMatrix(contents_device_transform,
259 &sk_device_matrix);
260 current_canvas_->setMatrix(sk_device_matrix);
262 current_paint_.reset();
263 if (settings_->force_antialiasing ||
264 !IsScaleAndIntegerTranslate(sk_device_matrix)) {
265 // TODO(danakj): Until we can enable AA only on exterior edges of the
266 // layer, disable AA if any interior edges are present. crbug.com/248175
267 bool all_four_edges_are_exterior = quad->IsTopEdge() &&
268 quad->IsLeftEdge() &&
269 quad->IsBottomEdge() &&
270 quad->IsRightEdge();
271 if (settings_->allow_antialiasing &&
272 (settings_->force_antialiasing || all_four_edges_are_exterior))
273 current_paint_.setAntiAlias(true);
274 current_paint_.setFilterQuality(kLow_SkFilterQuality);
277 if (quad->ShouldDrawWithBlending() ||
278 quad->shared_quad_state->blend_mode != SkXfermode::kSrcOver_Mode) {
279 current_paint_.setAlpha(quad->opacity() * 255);
280 current_paint_.setXfermodeMode(quad->shared_quad_state->blend_mode);
281 } else {
282 current_paint_.setXfermodeMode(SkXfermode::kSrc_Mode);
285 if (draw_region) {
286 gfx::QuadF local_draw_region(*draw_region);
287 SkPath draw_region_clip_path;
288 local_draw_region -=
289 gfx::Vector2dF(quad->visible_rect.x(), quad->visible_rect.y());
290 local_draw_region.Scale(1.0f / quad->visible_rect.width(),
291 1.0f / quad->visible_rect.height());
292 local_draw_region -= gfx::Vector2dF(0.5f, 0.5f);
294 SkPoint clip_points[4];
295 QuadFToSkPoints(local_draw_region, clip_points);
296 draw_region_clip_path.addPoly(clip_points, 4, true);
298 current_canvas_->clipPath(draw_region_clip_path, SkRegion::kIntersect_Op,
299 false);
302 switch (quad->material) {
303 case DrawQuad::CHECKERBOARD:
304 // TODO(enne) For now since checkerboards shouldn't be part of a 3D
305 // context, clipping regions aren't supported so we skip drawing them
306 // if this becomes the case.
307 if (!draw_region) {
308 DrawCheckerboardQuad(frame, CheckerboardDrawQuad::MaterialCast(quad));
310 break;
311 case DrawQuad::DEBUG_BORDER:
312 DrawDebugBorderQuad(frame, DebugBorderDrawQuad::MaterialCast(quad));
313 break;
314 case DrawQuad::PICTURE_CONTENT:
315 DrawPictureQuad(frame, PictureDrawQuad::MaterialCast(quad));
316 break;
317 case DrawQuad::RENDER_PASS:
318 DrawRenderPassQuad(frame, RenderPassDrawQuad::MaterialCast(quad));
319 break;
320 case DrawQuad::SOLID_COLOR:
321 DrawSolidColorQuad(frame, SolidColorDrawQuad::MaterialCast(quad));
322 break;
323 case DrawQuad::TEXTURE_CONTENT:
324 DrawTextureQuad(frame, TextureDrawQuad::MaterialCast(quad));
325 break;
326 case DrawQuad::TILED_CONTENT:
327 DrawTileQuad(frame, TileDrawQuad::MaterialCast(quad));
328 break;
329 case DrawQuad::SURFACE_CONTENT:
330 // Surface content should be fully resolved to other quad types before
331 // reaching a direct renderer.
332 NOTREACHED();
333 break;
334 case DrawQuad::INVALID:
335 case DrawQuad::IO_SURFACE_CONTENT:
336 case DrawQuad::YUV_VIDEO_CONTENT:
337 case DrawQuad::STREAM_VIDEO_CONTENT:
338 DrawUnsupportedQuad(frame, quad);
339 NOTREACHED();
340 break;
343 current_canvas_->resetMatrix();
344 if (draw_region) {
345 current_canvas_->restore();
349 void SoftwareRenderer::DrawCheckerboardQuad(const DrawingFrame* frame,
350 const CheckerboardDrawQuad* quad) {
351 gfx::RectF visible_quad_vertex_rect = MathUtil::ScaleRectProportional(
352 QuadVertexRect(), quad->rect, quad->visible_rect);
353 current_paint_.setColor(quad->color);
354 current_paint_.setAlpha(quad->opacity());
355 current_canvas_->drawRect(gfx::RectFToSkRect(visible_quad_vertex_rect),
356 current_paint_);
359 void SoftwareRenderer::DrawDebugBorderQuad(const DrawingFrame* frame,
360 const DebugBorderDrawQuad* quad) {
361 // We need to apply the matrix manually to have pixel-sized stroke width.
362 SkPoint vertices[4];
363 gfx::RectFToSkRect(QuadVertexRect()).toQuad(vertices);
364 SkPoint transformed_vertices[4];
365 current_canvas_->getTotalMatrix().mapPoints(transformed_vertices,
366 vertices,
368 current_canvas_->resetMatrix();
370 current_paint_.setColor(quad->color);
371 current_paint_.setAlpha(quad->opacity() * SkColorGetA(quad->color));
372 current_paint_.setStyle(SkPaint::kStroke_Style);
373 current_paint_.setStrokeWidth(quad->width);
374 current_canvas_->drawPoints(SkCanvas::kPolygon_PointMode,
375 4, transformed_vertices, current_paint_);
378 void SoftwareRenderer::DrawPictureQuad(const DrawingFrame* frame,
379 const PictureDrawQuad* quad) {
380 SkMatrix content_matrix;
381 content_matrix.setRectToRect(
382 gfx::RectFToSkRect(quad->tex_coord_rect),
383 gfx::RectFToSkRect(QuadVertexRect()),
384 SkMatrix::kFill_ScaleToFit);
385 current_canvas_->concat(content_matrix);
387 // TODO(aelias): This isn't correct in all cases. We should detect these
388 // cases and fall back to a persistent bitmap backing
389 // (http://crbug.com/280374).
390 skia::RefPtr<SkDrawFilter> opacity_filter =
391 skia::AdoptRef(new skia::OpacityDrawFilter(
392 quad->opacity(), frame->disable_picture_quad_image_filtering ||
393 quad->nearest_neighbor));
394 DCHECK(!current_canvas_->getDrawFilter());
395 current_canvas_->setDrawFilter(opacity_filter.get());
397 TRACE_EVENT0("cc",
398 "SoftwareRenderer::DrawPictureQuad");
400 quad->raster_source->PlaybackToSharedCanvas(
401 current_canvas_, quad->content_rect, quad->contents_scale);
403 current_canvas_->setDrawFilter(NULL);
406 void SoftwareRenderer::DrawSolidColorQuad(const DrawingFrame* frame,
407 const SolidColorDrawQuad* quad) {
408 gfx::RectF visible_quad_vertex_rect = MathUtil::ScaleRectProportional(
409 QuadVertexRect(), quad->rect, quad->visible_rect);
410 current_paint_.setColor(quad->color);
411 current_paint_.setAlpha(quad->opacity() * SkColorGetA(quad->color));
412 current_canvas_->drawRect(gfx::RectFToSkRect(visible_quad_vertex_rect),
413 current_paint_);
416 void SoftwareRenderer::DrawTextureQuad(const DrawingFrame* frame,
417 const TextureDrawQuad* quad) {
418 if (!IsSoftwareResource(quad->resource_id)) {
419 DrawUnsupportedQuad(frame, quad);
420 return;
423 // TODO(skaslev): Add support for non-premultiplied alpha.
424 ResourceProvider::ScopedReadLockSoftware lock(resource_provider_,
425 quad->resource_id);
426 if (!lock.valid())
427 return;
428 const SkBitmap* bitmap = lock.sk_bitmap();
429 gfx::RectF uv_rect = gfx::ScaleRect(gfx::BoundingRect(quad->uv_top_left,
430 quad->uv_bottom_right),
431 bitmap->width(),
432 bitmap->height());
433 gfx::RectF visible_uv_rect =
434 MathUtil::ScaleRectProportional(uv_rect, quad->rect, quad->visible_rect);
435 SkRect sk_uv_rect = gfx::RectFToSkRect(visible_uv_rect);
436 gfx::RectF visible_quad_vertex_rect = MathUtil::ScaleRectProportional(
437 QuadVertexRect(), quad->rect, quad->visible_rect);
438 SkRect quad_rect = gfx::RectFToSkRect(visible_quad_vertex_rect);
440 if (quad->y_flipped)
441 current_canvas_->scale(1, -1);
443 bool blend_background = quad->background_color != SK_ColorTRANSPARENT &&
444 !bitmap->isOpaque();
445 bool needs_layer = blend_background && (current_paint_.getAlpha() != 0xFF);
446 if (needs_layer) {
447 current_canvas_->saveLayerAlpha(&quad_rect, current_paint_.getAlpha());
448 current_paint_.setAlpha(0xFF);
450 if (blend_background) {
451 SkPaint background_paint;
452 background_paint.setColor(quad->background_color);
453 current_canvas_->drawRect(quad_rect, background_paint);
455 current_paint_.setFilterQuality(
456 quad->nearest_neighbor ? kNone_SkFilterQuality : kLow_SkFilterQuality);
457 SkShader::TileMode tile_mode = WrapModeToTileMode(lock.wrap_mode());
458 if (tile_mode != SkShader::kClamp_TileMode) {
459 SkMatrix matrix;
460 matrix.setRectToRect(sk_uv_rect, quad_rect, SkMatrix::kFill_ScaleToFit);
461 skia::RefPtr<SkShader> shader = skia::AdoptRef(
462 SkShader::CreateBitmapShader(*bitmap, tile_mode, tile_mode, &matrix));
463 SkPaint paint;
464 paint.setStyle(SkPaint::kFill_Style);
465 paint.setShader(shader.get());
466 current_canvas_->drawRect(quad_rect, paint);
467 } else {
468 current_canvas_->drawBitmapRectToRect(*bitmap,
469 &sk_uv_rect,
470 quad_rect,
471 &current_paint_);
474 if (needs_layer)
475 current_canvas_->restore();
478 void SoftwareRenderer::DrawTileQuad(const DrawingFrame* frame,
479 const TileDrawQuad* quad) {
480 // |resource_provider_| can be NULL in resourceless software draws, which
481 // should never produce tile quads in the first place.
482 DCHECK(resource_provider_);
483 DCHECK(IsSoftwareResource(quad->resource_id));
485 ResourceProvider::ScopedReadLockSoftware lock(resource_provider_,
486 quad->resource_id);
487 if (!lock.valid())
488 return;
489 DCHECK_EQ(GL_CLAMP_TO_EDGE, lock.wrap_mode());
491 gfx::RectF visible_tex_coord_rect = MathUtil::ScaleRectProportional(
492 quad->tex_coord_rect, quad->rect, quad->visible_rect);
493 gfx::RectF visible_quad_vertex_rect = MathUtil::ScaleRectProportional(
494 QuadVertexRect(), quad->rect, quad->visible_rect);
496 SkRect uv_rect = gfx::RectFToSkRect(visible_tex_coord_rect);
497 current_paint_.setFilterQuality(
498 quad->nearest_neighbor ? kNone_SkFilterQuality : kLow_SkFilterQuality);
499 current_canvas_->drawBitmapRectToRect(
500 *lock.sk_bitmap(),
501 &uv_rect,
502 gfx::RectFToSkRect(visible_quad_vertex_rect),
503 &current_paint_);
506 void SoftwareRenderer::DrawRenderPassQuad(const DrawingFrame* frame,
507 const RenderPassDrawQuad* quad) {
508 ScopedResource* content_texture =
509 render_pass_textures_.get(quad->render_pass_id);
510 DCHECK(content_texture);
511 DCHECK(content_texture->id());
512 DCHECK(IsSoftwareResource(content_texture->id()));
514 ResourceProvider::ScopedReadLockSoftware lock(resource_provider_,
515 content_texture->id());
516 if (!lock.valid())
517 return;
518 SkShader::TileMode content_tile_mode = WrapModeToTileMode(lock.wrap_mode());
520 SkRect dest_rect = gfx::RectFToSkRect(QuadVertexRect());
521 SkRect dest_visible_rect = gfx::RectFToSkRect(MathUtil::ScaleRectProportional(
522 QuadVertexRect(), quad->rect, quad->visible_rect));
523 SkRect content_rect = SkRect::MakeWH(quad->rect.width(), quad->rect.height());
525 SkMatrix content_mat;
526 content_mat.setRectToRect(content_rect, dest_rect,
527 SkMatrix::kFill_ScaleToFit);
529 const SkBitmap* content = lock.sk_bitmap();
531 SkBitmap filter_bitmap;
532 if (!quad->filters.IsEmpty()) {
533 skia::RefPtr<SkImageFilter> filter = RenderSurfaceFilters::BuildImageFilter(
534 quad->filters, content_texture->size());
535 // TODO(ajuma): Apply the filter in the same pass as the content where
536 // possible (e.g. when there's no origin offset). See crbug.com/308201.
537 if (filter) {
538 SkImageInfo info = SkImageInfo::MakeN32Premul(
539 content_texture->size().width(), content_texture->size().height());
540 if (filter_bitmap.tryAllocPixels(info)) {
541 SkCanvas canvas(filter_bitmap);
542 SkPaint paint;
543 paint.setImageFilter(filter.get());
544 canvas.clear(SK_ColorTRANSPARENT);
545 canvas.translate(SkIntToScalar(-quad->rect.origin().x()),
546 SkIntToScalar(-quad->rect.origin().y()));
547 canvas.scale(quad->filters_scale.x(), quad->filters_scale.y());
548 canvas.drawSprite(*content, 0, 0, &paint);
553 skia::RefPtr<SkShader> shader;
554 if (filter_bitmap.isNull()) {
555 shader = skia::AdoptRef(SkShader::CreateBitmapShader(
556 *content, content_tile_mode, content_tile_mode, &content_mat));
557 } else {
558 shader = skia::AdoptRef(SkShader::CreateBitmapShader(
559 filter_bitmap, content_tile_mode, content_tile_mode, &content_mat));
561 current_paint_.setShader(shader.get());
563 if (quad->mask_resource_id) {
564 ResourceProvider::ScopedReadLockSoftware mask_lock(resource_provider_,
565 quad->mask_resource_id);
566 if (!lock.valid())
567 return;
568 SkShader::TileMode mask_tile_mode = WrapModeToTileMode(
569 mask_lock.wrap_mode());
571 const SkBitmap* mask = mask_lock.sk_bitmap();
573 // Scale normalized uv rect into absolute texel coordinates.
574 SkRect mask_rect =
575 gfx::RectFToSkRect(gfx::ScaleRect(quad->MaskUVRect(),
576 quad->mask_texture_size.width(),
577 quad->mask_texture_size.height()));
579 SkMatrix mask_mat;
580 mask_mat.setRectToRect(mask_rect, dest_rect, SkMatrix::kFill_ScaleToFit);
582 skia::RefPtr<SkShader> mask_shader =
583 skia::AdoptRef(SkShader::CreateBitmapShader(
584 *mask, mask_tile_mode, mask_tile_mode, &mask_mat));
586 SkPaint mask_paint;
587 mask_paint.setShader(mask_shader.get());
589 SkLayerRasterizer::Builder builder;
590 builder.addLayer(mask_paint);
592 skia::RefPtr<SkLayerRasterizer> mask_rasterizer =
593 skia::AdoptRef(builder.detachRasterizer());
595 current_paint_.setRasterizer(mask_rasterizer.get());
596 current_canvas_->drawRect(dest_visible_rect, current_paint_);
597 } else {
598 // TODO(skaslev): Apply background filters
599 current_canvas_->drawRect(dest_visible_rect, current_paint_);
603 void SoftwareRenderer::DrawUnsupportedQuad(const DrawingFrame* frame,
604 const DrawQuad* quad) {
605 #ifdef NDEBUG
606 current_paint_.setColor(SK_ColorWHITE);
607 #else
608 current_paint_.setColor(SK_ColorMAGENTA);
609 #endif
610 current_paint_.setAlpha(quad->opacity() * 255);
611 current_canvas_->drawRect(gfx::RectFToSkRect(QuadVertexRect()),
612 current_paint_);
615 void SoftwareRenderer::CopyCurrentRenderPassToBitmap(
616 DrawingFrame* frame,
617 scoped_ptr<CopyOutputRequest> request) {
618 gfx::Rect copy_rect = frame->current_render_pass->output_rect;
619 if (request->has_area())
620 copy_rect.Intersect(request->area());
621 gfx::Rect window_copy_rect = MoveFromDrawToWindowSpace(frame, copy_rect);
623 scoped_ptr<SkBitmap> bitmap(new SkBitmap);
624 bitmap->setInfo(SkImageInfo::MakeN32Premul(window_copy_rect.width(),
625 window_copy_rect.height()));
626 current_canvas_->readPixels(
627 bitmap.get(), window_copy_rect.x(), window_copy_rect.y());
629 request->SendBitmapResult(bitmap.Pass());
632 void SoftwareRenderer::DiscardBackbuffer() {
633 if (is_backbuffer_discarded_)
634 return;
636 output_surface_->DiscardBackbuffer();
638 is_backbuffer_discarded_ = true;
640 // Damage tracker needs a full reset every time framebuffer is discarded.
641 client_->SetFullRootLayerDamage();
644 void SoftwareRenderer::EnsureBackbuffer() {
645 if (!is_backbuffer_discarded_)
646 return;
648 output_surface_->EnsureBackbuffer();
649 is_backbuffer_discarded_ = false;
652 void SoftwareRenderer::DidChangeVisibility() {
653 if (visible())
654 EnsureBackbuffer();
655 else
656 DiscardBackbuffer();
659 } // namespace cc