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