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"
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
) {
55 return SkShader::kRepeat_TileMode
;
56 case GL_CLAMP_TO_EDGE
:
57 return SkShader::kClamp_TileMode
;
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(frame
->root_damage_rect
);
108 void SoftwareRenderer::FinishDrawingFrame(DrawingFrame
* frame
) {
109 TRACE_EVENT0("cc", "SoftwareRenderer::FinishDrawingFrame");
110 current_framebuffer_lock_
= nullptr;
111 current_framebuffer_canvas_
.clear();
112 current_canvas_
= NULL
;
115 output_device_
->EndPaint();
118 void SoftwareRenderer::SwapBuffers(const CompositorFrameMetadata
& metadata
) {
119 TRACE_EVENT0("cc,benchmark", "SoftwareRenderer::SwapBuffers");
120 CompositorFrame compositor_frame
;
121 compositor_frame
.metadata
= metadata
;
122 output_surface_
->SwapBuffers(&compositor_frame
);
125 bool SoftwareRenderer::FlippedFramebuffer(const DrawingFrame
* frame
) const {
129 void SoftwareRenderer::EnsureScissorTestEnabled() {
130 is_scissor_enabled_
= true;
131 SetClipRect(scissor_rect_
);
134 void SoftwareRenderer::EnsureScissorTestDisabled() {
135 // There is no explicit notion of enabling/disabling scissoring in software
136 // rendering, but the underlying effect we want is to clear any existing
137 // clipRect on the current SkCanvas. This is done by setting clipRect to
138 // the viewport's dimensions.
139 if (!current_canvas_
)
141 is_scissor_enabled_
= false;
142 SkISize size
= current_canvas_
->getDeviceSize();
143 SetClipRect(gfx::Rect(size
.width(), size
.height()));
146 void SoftwareRenderer::Finish() {}
148 void SoftwareRenderer::BindFramebufferToOutputSurface(DrawingFrame
* frame
) {
149 DCHECK(!output_surface_
->HasExternalStencilTest());
150 current_framebuffer_lock_
= nullptr;
151 current_framebuffer_canvas_
.clear();
152 current_canvas_
= root_canvas_
;
155 bool SoftwareRenderer::BindFramebufferToTexture(
157 const ScopedResource
* texture
,
158 const gfx::Rect
& target_rect
) {
159 DCHECK(texture
->id());
161 // Explicitly release lock, otherwise we can crash when try to lock
162 // same texture again.
163 current_framebuffer_lock_
= nullptr;
164 current_framebuffer_lock_
= make_scoped_ptr(
165 new ResourceProvider::ScopedWriteLockSoftware(
166 resource_provider_
, texture
->id()));
167 current_framebuffer_canvas_
=
168 skia::AdoptRef(new SkCanvas(current_framebuffer_lock_
->sk_bitmap()));
169 current_canvas_
= current_framebuffer_canvas_
.get();
173 void SoftwareRenderer::SetScissorTestRect(const gfx::Rect
& scissor_rect
) {
174 is_scissor_enabled_
= true;
175 scissor_rect_
= scissor_rect
;
176 SetClipRect(scissor_rect
);
179 void SoftwareRenderer::SetClipRect(const gfx::Rect
& rect
) {
180 if (!current_canvas_
)
182 // Skia applies the current matrix to clip rects so we reset it temporary.
183 SkMatrix current_matrix
= current_canvas_
->getTotalMatrix();
184 current_canvas_
->resetMatrix();
185 current_canvas_
->clipRect(gfx::RectToSkRect(rect
), SkRegion::kReplace_Op
);
186 current_canvas_
->setMatrix(current_matrix
);
189 void SoftwareRenderer::ClearCanvas(SkColor color
) {
190 if (!current_canvas_
)
192 // SkCanvas::clear doesn't respect the current clipping region
193 // so we SkCanvas::drawColor instead if scissoring is active.
194 if (is_scissor_enabled_
)
195 current_canvas_
->drawColor(color
, SkXfermode::kSrc_Mode
);
197 current_canvas_
->clear(color
);
200 void SoftwareRenderer::ClearFramebuffer(DrawingFrame
* frame
) {
201 if (frame
->current_render_pass
->has_transparent_background
) {
202 ClearCanvas(SkColorSetARGB(0, 0, 0, 0));
205 // On DEBUG builds, opaque render passes are cleared to blue
206 // to easily see regions that were not drawn on the screen.
207 ClearCanvas(SkColorSetARGB(255, 0, 0, 255));
212 void SoftwareRenderer::PrepareSurfaceForPass(
214 SurfaceInitializationMode initialization_mode
,
215 const gfx::Rect
& render_pass_scissor
) {
216 switch (initialization_mode
) {
217 case SURFACE_INITIALIZATION_MODE_PRESERVE
:
218 EnsureScissorTestDisabled();
220 case SURFACE_INITIALIZATION_MODE_FULL_SURFACE_CLEAR
:
221 EnsureScissorTestDisabled();
222 ClearFramebuffer(frame
);
224 case SURFACE_INITIALIZATION_MODE_SCISSORED_CLEAR
:
225 SetScissorTestRect(render_pass_scissor
);
226 ClearFramebuffer(frame
);
231 bool SoftwareRenderer::IsSoftwareResource(ResourceId resource_id
) const {
232 switch (resource_provider_
->GetResourceType(resource_id
)) {
233 case ResourceProvider::RESOURCE_TYPE_GL_TEXTURE
:
235 case ResourceProvider::RESOURCE_TYPE_BITMAP
:
239 LOG(FATAL
) << "Invalid resource type.";
243 void SoftwareRenderer::DoDrawQuad(DrawingFrame
* frame
,
244 const DrawQuad
* quad
,
245 const gfx::QuadF
* draw_region
) {
246 if (!current_canvas_
)
249 current_canvas_
->save();
252 TRACE_EVENT0("cc", "SoftwareRenderer::DoDrawQuad");
253 gfx::Transform quad_rect_matrix
;
254 QuadRectTransform(&quad_rect_matrix
,
255 quad
->shared_quad_state
->quad_to_target_transform
,
256 gfx::RectF(quad
->rect
));
257 gfx::Transform contents_device_transform
=
258 frame
->window_matrix
* frame
->projection_matrix
* quad_rect_matrix
;
259 contents_device_transform
.FlattenTo2d();
260 SkMatrix sk_device_matrix
;
261 gfx::TransformToFlattenedSkMatrix(contents_device_transform
,
263 current_canvas_
->setMatrix(sk_device_matrix
);
265 current_paint_
.reset();
266 if (settings_
->force_antialiasing
||
267 !IsScaleAndIntegerTranslate(sk_device_matrix
)) {
268 // TODO(danakj): Until we can enable AA only on exterior edges of the
269 // layer, disable AA if any interior edges are present. crbug.com/248175
270 bool all_four_edges_are_exterior
= quad
->IsTopEdge() &&
271 quad
->IsLeftEdge() &&
272 quad
->IsBottomEdge() &&
274 if (settings_
->allow_antialiasing
&&
275 (settings_
->force_antialiasing
|| all_four_edges_are_exterior
))
276 current_paint_
.setAntiAlias(true);
277 current_paint_
.setFilterQuality(kLow_SkFilterQuality
);
280 if (quad
->ShouldDrawWithBlending() ||
281 quad
->shared_quad_state
->blend_mode
!= SkXfermode::kSrcOver_Mode
) {
282 current_paint_
.setAlpha(quad
->shared_quad_state
->opacity
* 255);
283 current_paint_
.setXfermodeMode(quad
->shared_quad_state
->blend_mode
);
285 current_paint_
.setXfermodeMode(SkXfermode::kSrc_Mode
);
289 gfx::QuadF
local_draw_region(*draw_region
);
290 SkPath draw_region_clip_path
;
292 gfx::Vector2dF(quad
->visible_rect
.x(), quad
->visible_rect
.y());
293 local_draw_region
.Scale(1.0f
/ quad
->visible_rect
.width(),
294 1.0f
/ quad
->visible_rect
.height());
295 local_draw_region
-= gfx::Vector2dF(0.5f
, 0.5f
);
297 SkPoint clip_points
[4];
298 QuadFToSkPoints(local_draw_region
, clip_points
);
299 draw_region_clip_path
.addPoly(clip_points
, 4, true);
301 current_canvas_
->clipPath(draw_region_clip_path
, SkRegion::kIntersect_Op
,
305 switch (quad
->material
) {
306 case DrawQuad::DEBUG_BORDER
:
307 DrawDebugBorderQuad(frame
, DebugBorderDrawQuad::MaterialCast(quad
));
309 case DrawQuad::PICTURE_CONTENT
:
310 DrawPictureQuad(frame
, PictureDrawQuad::MaterialCast(quad
));
312 case DrawQuad::RENDER_PASS
:
313 DrawRenderPassQuad(frame
, RenderPassDrawQuad::MaterialCast(quad
));
315 case DrawQuad::SOLID_COLOR
:
316 DrawSolidColorQuad(frame
, SolidColorDrawQuad::MaterialCast(quad
));
318 case DrawQuad::TEXTURE_CONTENT
:
319 DrawTextureQuad(frame
, TextureDrawQuad::MaterialCast(quad
));
321 case DrawQuad::TILED_CONTENT
:
322 DrawTileQuad(frame
, TileDrawQuad::MaterialCast(quad
));
324 case DrawQuad::SURFACE_CONTENT
:
325 // Surface content should be fully resolved to other quad types before
326 // reaching a direct renderer.
329 case DrawQuad::INVALID
:
330 case DrawQuad::IO_SURFACE_CONTENT
:
331 case DrawQuad::YUV_VIDEO_CONTENT
:
332 case DrawQuad::STREAM_VIDEO_CONTENT
:
333 DrawUnsupportedQuad(frame
, quad
);
338 current_canvas_
->resetMatrix();
340 current_canvas_
->restore();
344 void SoftwareRenderer::DrawDebugBorderQuad(const DrawingFrame
* frame
,
345 const DebugBorderDrawQuad
* quad
) {
346 // We need to apply the matrix manually to have pixel-sized stroke width.
348 gfx::RectFToSkRect(QuadVertexRect()).toQuad(vertices
);
349 SkPoint transformed_vertices
[4];
350 current_canvas_
->getTotalMatrix().mapPoints(transformed_vertices
,
353 current_canvas_
->resetMatrix();
355 current_paint_
.setColor(quad
->color
);
356 current_paint_
.setAlpha(quad
->shared_quad_state
->opacity
*
357 SkColorGetA(quad
->color
));
358 current_paint_
.setStyle(SkPaint::kStroke_Style
);
359 current_paint_
.setStrokeWidth(quad
->width
);
360 current_canvas_
->drawPoints(SkCanvas::kPolygon_PointMode
,
361 4, transformed_vertices
, current_paint_
);
364 void SoftwareRenderer::DrawPictureQuad(const DrawingFrame
* frame
,
365 const PictureDrawQuad
* quad
) {
366 SkMatrix content_matrix
;
367 content_matrix
.setRectToRect(
368 gfx::RectFToSkRect(quad
->tex_coord_rect
),
369 gfx::RectFToSkRect(QuadVertexRect()),
370 SkMatrix::kFill_ScaleToFit
);
371 current_canvas_
->concat(content_matrix
);
373 const bool needs_transparency
=
374 SkScalarRoundToInt(quad
->shared_quad_state
->opacity
* 255) < 255;
375 const bool disable_image_filtering
=
376 frame
->disable_picture_quad_image_filtering
|| quad
->nearest_neighbor
;
378 TRACE_EVENT0("cc", "SoftwareRenderer::DrawPictureQuad");
380 if (needs_transparency
|| disable_image_filtering
) {
381 // TODO(aelias): This isn't correct in all cases. We should detect these
382 // cases and fall back to a persistent bitmap backing
383 // (http://crbug.com/280374).
384 skia::OpacityFilterCanvas
filtered_canvas(current_canvas_
,
385 quad
->shared_quad_state
->opacity
,
386 disable_image_filtering
);
387 quad
->raster_source
->PlaybackToSharedCanvas(
388 &filtered_canvas
, quad
->content_rect
, quad
->contents_scale
);
390 quad
->raster_source
->PlaybackToSharedCanvas(
391 current_canvas_
, quad
->content_rect
, quad
->contents_scale
);
395 void SoftwareRenderer::DrawSolidColorQuad(const DrawingFrame
* frame
,
396 const SolidColorDrawQuad
* quad
) {
397 gfx::RectF visible_quad_vertex_rect
= MathUtil::ScaleRectProportional(
398 QuadVertexRect(), gfx::RectF(quad
->rect
), gfx::RectF(quad
->visible_rect
));
399 current_paint_
.setColor(quad
->color
);
400 current_paint_
.setAlpha(quad
->shared_quad_state
->opacity
*
401 SkColorGetA(quad
->color
));
402 current_canvas_
->drawRect(gfx::RectFToSkRect(visible_quad_vertex_rect
),
406 void SoftwareRenderer::DrawTextureQuad(const DrawingFrame
* frame
,
407 const TextureDrawQuad
* quad
) {
408 if (!IsSoftwareResource(quad
->resource_id())) {
409 DrawUnsupportedQuad(frame
, quad
);
413 // TODO(skaslev): Add support for non-premultiplied alpha.
414 ResourceProvider::ScopedReadLockSoftware
lock(resource_provider_
,
415 quad
->resource_id());
418 const SkBitmap
* bitmap
= lock
.sk_bitmap();
419 gfx::RectF uv_rect
= gfx::ScaleRect(gfx::BoundingRect(quad
->uv_top_left
,
420 quad
->uv_bottom_right
),
423 gfx::RectF visible_uv_rect
= MathUtil::ScaleRectProportional(
424 uv_rect
, gfx::RectF(quad
->rect
), gfx::RectF(quad
->visible_rect
));
425 SkRect sk_uv_rect
= gfx::RectFToSkRect(visible_uv_rect
);
426 gfx::RectF visible_quad_vertex_rect
= MathUtil::ScaleRectProportional(
427 QuadVertexRect(), gfx::RectF(quad
->rect
), gfx::RectF(quad
->visible_rect
));
428 SkRect quad_rect
= gfx::RectFToSkRect(visible_quad_vertex_rect
);
431 current_canvas_
->scale(1, -1);
433 bool blend_background
= quad
->background_color
!= SK_ColorTRANSPARENT
&&
435 bool needs_layer
= blend_background
&& (current_paint_
.getAlpha() != 0xFF);
437 current_canvas_
->saveLayerAlpha(&quad_rect
, current_paint_
.getAlpha());
438 current_paint_
.setAlpha(0xFF);
440 if (blend_background
) {
441 SkPaint background_paint
;
442 background_paint
.setColor(quad
->background_color
);
443 current_canvas_
->drawRect(quad_rect
, background_paint
);
445 current_paint_
.setFilterQuality(
446 quad
->nearest_neighbor
? kNone_SkFilterQuality
: kLow_SkFilterQuality
);
447 SkShader::TileMode tile_mode
= WrapModeToTileMode(lock
.wrap_mode());
448 if (tile_mode
!= SkShader::kClamp_TileMode
) {
450 matrix
.setRectToRect(sk_uv_rect
, quad_rect
, SkMatrix::kFill_ScaleToFit
);
451 skia::RefPtr
<SkShader
> shader
= skia::AdoptRef(
452 SkShader::CreateBitmapShader(*bitmap
, tile_mode
, tile_mode
, &matrix
));
454 paint
.setStyle(SkPaint::kFill_Style
);
455 paint
.setShader(shader
.get());
456 current_canvas_
->drawRect(quad_rect
, paint
);
458 current_canvas_
->drawBitmapRect(*bitmap
, sk_uv_rect
, quad_rect
,
463 current_canvas_
->restore();
466 void SoftwareRenderer::DrawTileQuad(const DrawingFrame
* frame
,
467 const TileDrawQuad
* quad
) {
468 // |resource_provider_| can be NULL in resourceless software draws, which
469 // should never produce tile quads in the first place.
470 DCHECK(resource_provider_
);
471 DCHECK(IsSoftwareResource(quad
->resource_id()));
473 ResourceProvider::ScopedReadLockSoftware
lock(resource_provider_
,
474 quad
->resource_id());
477 DCHECK_EQ(GL_CLAMP_TO_EDGE
, lock
.wrap_mode());
479 gfx::RectF visible_tex_coord_rect
= MathUtil::ScaleRectProportional(
480 quad
->tex_coord_rect
, gfx::RectF(quad
->rect
),
481 gfx::RectF(quad
->visible_rect
));
482 gfx::RectF visible_quad_vertex_rect
= MathUtil::ScaleRectProportional(
483 QuadVertexRect(), gfx::RectF(quad
->rect
), gfx::RectF(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
),
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());
505 SkShader::TileMode content_tile_mode
= WrapModeToTileMode(lock
.wrap_mode());
507 SkRect dest_rect
= gfx::RectFToSkRect(QuadVertexRect());
508 SkRect dest_visible_rect
= gfx::RectFToSkRect(
509 MathUtil::ScaleRectProportional(QuadVertexRect(), gfx::RectF(quad
->rect
),
510 gfx::RectF(quad
->visible_rect
)));
511 SkRect content_rect
= SkRect::MakeWH(quad
->rect
.width(), quad
->rect
.height());
513 SkMatrix content_mat
;
514 content_mat
.setRectToRect(content_rect
, dest_rect
,
515 SkMatrix::kFill_ScaleToFit
);
517 const SkBitmap
* content
= lock
.sk_bitmap();
519 SkBitmap filter_bitmap
;
520 if (!quad
->filters
.IsEmpty()) {
521 skia::RefPtr
<SkImageFilter
> filter
= RenderSurfaceFilters::BuildImageFilter(
522 quad
->filters
, content_texture
->size());
523 // TODO(ajuma): Apply the filter in the same pass as the content where
524 // possible (e.g. when there's no origin offset). See crbug.com/308201.
525 filter_bitmap
= ApplyImageFilter(filter
.get(), quad
, content
);
528 skia::RefPtr
<SkShader
> shader
;
529 if (filter_bitmap
.isNull()) {
530 shader
= skia::AdoptRef(SkShader::CreateBitmapShader(
531 *content
, content_tile_mode
, content_tile_mode
, &content_mat
));
533 shader
= skia::AdoptRef(SkShader::CreateBitmapShader(
534 filter_bitmap
, content_tile_mode
, content_tile_mode
, &content_mat
));
537 scoped_ptr
<ResourceProvider::ScopedReadLockSoftware
> mask_lock
;
538 if (quad
->mask_resource_id()) {
539 mask_lock
= scoped_ptr
<ResourceProvider::ScopedReadLockSoftware
>(
540 new ResourceProvider::ScopedReadLockSoftware(resource_provider_
,
541 quad
->mask_resource_id()));
543 if (!mask_lock
->valid())
546 SkShader::TileMode mask_tile_mode
=
547 WrapModeToTileMode(mask_lock
->wrap_mode());
549 const SkBitmap
* mask
= mask_lock
->sk_bitmap();
551 // Scale normalized uv rect into absolute texel coordinates.
553 gfx::RectFToSkRect(gfx::ScaleRect(quad
->MaskUVRect(),
554 quad
->mask_texture_size
.width(),
555 quad
->mask_texture_size
.height()));
558 mask_mat
.setRectToRect(mask_rect
, dest_rect
, SkMatrix::kFill_ScaleToFit
);
560 skia::RefPtr
<SkShader
> mask_shader
=
561 skia::AdoptRef(SkShader::CreateBitmapShader(
562 *mask
, mask_tile_mode
, mask_tile_mode
, &mask_mat
));
565 mask_paint
.setShader(mask_shader
.get());
567 SkLayerRasterizer::Builder builder
;
568 builder
.addLayer(mask_paint
);
570 skia::RefPtr
<SkLayerRasterizer
> mask_rasterizer
=
571 skia::AdoptRef(builder
.detachRasterizer());
573 current_paint_
.setRasterizer(mask_rasterizer
.get());
576 // If we have a background filter shader, render its results first.
577 skia::RefPtr
<SkShader
> background_filter_shader
=
578 GetBackgroundFilterShader(frame
, quad
, content_tile_mode
);
579 if (background_filter_shader
) {
580 current_paint_
.setShader(background_filter_shader
.get());
581 current_canvas_
->drawRect(dest_visible_rect
, current_paint_
);
583 current_paint_
.setShader(shader
.get());
584 current_canvas_
->drawRect(dest_visible_rect
, current_paint_
);
587 void SoftwareRenderer::DrawUnsupportedQuad(const DrawingFrame
* frame
,
588 const DrawQuad
* quad
) {
590 current_paint_
.setColor(SK_ColorWHITE
);
592 current_paint_
.setColor(SK_ColorMAGENTA
);
594 current_paint_
.setAlpha(quad
->shared_quad_state
->opacity
* 255);
595 current_canvas_
->drawRect(gfx::RectFToSkRect(QuadVertexRect()),
599 void SoftwareRenderer::CopyCurrentRenderPassToBitmap(
601 scoped_ptr
<CopyOutputRequest
> request
) {
602 gfx::Rect copy_rect
= frame
->current_render_pass
->output_rect
;
603 if (request
->has_area())
604 copy_rect
.Intersect(request
->area());
605 gfx::Rect window_copy_rect
= MoveFromDrawToWindowSpace(frame
, copy_rect
);
607 scoped_ptr
<SkBitmap
> bitmap(new SkBitmap
);
608 bitmap
->setInfo(SkImageInfo::MakeN32Premul(window_copy_rect
.width(),
609 window_copy_rect
.height()));
610 current_canvas_
->readPixels(
611 bitmap
.get(), window_copy_rect
.x(), window_copy_rect
.y());
613 request
->SendBitmapResult(bitmap
.Pass());
616 void SoftwareRenderer::DiscardBackbuffer() {
617 if (is_backbuffer_discarded_
)
620 output_surface_
->DiscardBackbuffer();
622 is_backbuffer_discarded_
= true;
624 // Damage tracker needs a full reset every time framebuffer is discarded.
625 client_
->SetFullRootLayerDamage();
628 void SoftwareRenderer::EnsureBackbuffer() {
629 if (!is_backbuffer_discarded_
)
632 output_surface_
->EnsureBackbuffer();
633 is_backbuffer_discarded_
= false;
636 void SoftwareRenderer::DidChangeVisibility() {
643 bool SoftwareRenderer::ShouldApplyBackgroundFilters(
644 const RenderPassDrawQuad
* quad
) const {
645 if (quad
->background_filters
.IsEmpty())
648 // TODO(hendrikw): Look into allowing background filters to see pixels from
649 // other render targets. See crbug.com/314867.
654 SkBitmap
SoftwareRenderer::ApplyImageFilter(SkImageFilter
* filter
,
655 const RenderPassDrawQuad
* quad
,
656 const SkBitmap
* to_filter
) const {
660 SkBitmap filter_bitmap
;
661 if (filter_bitmap
.tryAllocPixels(SkImageInfo::MakeN32Premul(
662 to_filter
->width(), to_filter
->height()))) {
663 SkCanvas
canvas(filter_bitmap
);
665 paint
.setImageFilter(filter
);
666 canvas
.clear(SK_ColorTRANSPARENT
);
667 canvas
.translate(SkIntToScalar(-quad
->rect
.origin().x()),
668 SkIntToScalar(-quad
->rect
.origin().y()));
669 canvas
.scale(quad
->filters_scale
.x(), quad
->filters_scale
.y());
670 canvas
.drawSprite(*to_filter
, 0, 0, &paint
);
672 return filter_bitmap
;
675 SkBitmap
SoftwareRenderer::GetBackdropBitmap(
676 const gfx::Rect
& bounding_rect
) const {
678 bitmap
.setInfo(SkImageInfo::MakeN32Premul(bounding_rect
.width(),
679 bounding_rect
.height()));
680 current_canvas_
->readPixels(&bitmap
, bounding_rect
.x(), bounding_rect
.y());
684 gfx::Rect
SoftwareRenderer::GetBackdropBoundingBoxForRenderPassQuad(
685 const DrawingFrame
* frame
,
686 const RenderPassDrawQuad
* quad
,
687 const gfx::Transform
& contents_device_transform
) const {
688 DCHECK(ShouldApplyBackgroundFilters(quad
));
689 gfx::Rect backdrop_rect
= gfx::ToEnclosingRect(
690 MathUtil::MapClippedRect(contents_device_transform
, QuadVertexRect()));
692 int top
, right
, bottom
, left
;
693 quad
->background_filters
.GetOutsets(&top
, &right
, &bottom
, &left
);
694 backdrop_rect
.Inset(-left
, -top
, -right
, -bottom
);
696 backdrop_rect
.Intersect(MoveFromDrawToWindowSpace(
697 frame
, frame
->current_render_pass
->output_rect
));
699 return backdrop_rect
;
702 skia::RefPtr
<SkShader
> SoftwareRenderer::GetBackgroundFilterShader(
703 const DrawingFrame
* frame
,
704 const RenderPassDrawQuad
* quad
,
705 SkShader::TileMode content_tile_mode
) const {
706 if (!ShouldApplyBackgroundFilters(quad
))
707 return skia::RefPtr
<SkShader
>();
709 gfx::Transform quad_rect_matrix
;
710 QuadRectTransform(&quad_rect_matrix
,
711 quad
->shared_quad_state
->quad_to_target_transform
,
712 gfx::RectF(quad
->rect
));
713 gfx::Transform contents_device_transform
=
714 frame
->window_matrix
* frame
->projection_matrix
* quad_rect_matrix
;
715 contents_device_transform
.FlattenTo2d();
717 gfx::Rect backdrop_rect
= GetBackdropBoundingBoxForRenderPassQuad(
718 frame
, quad
, contents_device_transform
);
720 // Figure out the transformations to move it back to pixel space.
721 gfx::Transform contents_device_transform_inverse
;
722 if (!contents_device_transform
.GetInverse(&contents_device_transform_inverse
))
723 return skia::RefPtr
<SkShader
>();
725 SkMatrix filter_backdrop_transform
=
726 contents_device_transform_inverse
.matrix();
727 filter_backdrop_transform
.preTranslate(backdrop_rect
.x(), backdrop_rect
.y());
729 // Draw what's behind, and apply the filter to it.
730 SkBitmap backdrop_bitmap
= GetBackdropBitmap(backdrop_rect
);
732 skia::RefPtr
<SkImageFilter
> filter
= RenderSurfaceFilters::BuildImageFilter(
733 quad
->background_filters
,
734 gfx::Size(backdrop_bitmap
.width(), backdrop_bitmap
.height()));
735 SkBitmap filter_backdrop_bitmap
=
736 ApplyImageFilter(filter
.get(), quad
, &backdrop_bitmap
);
738 if (filter_backdrop_bitmap
.empty())
739 return skia::RefPtr
<SkShader
>();
741 return skia::AdoptRef(SkShader::CreateBitmapShader(
742 filter_backdrop_bitmap
, content_tile_mode
, content_tile_mode
,
743 &filter_backdrop_transform
));