GPU workaround to simulate Out of Memory errors with large textures
[chromium-blink-merge.git] / cc / output / software_renderer.cc
blob2f3de4336a933ae30da33d5ca552c658dd53664e
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;
239 case ResourceProvider::RESOURCE_TYPE_INVALID:
240 break;
243 LOG(FATAL) << "Invalid resource type.";
244 return false;
247 void SoftwareRenderer::DoDrawQuad(DrawingFrame* frame,
248 const DrawQuad* quad,
249 const gfx::QuadF* draw_region) {
250 if (draw_region) {
251 current_canvas_->save();
254 TRACE_EVENT0("cc", "SoftwareRenderer::DoDrawQuad");
255 gfx::Transform quad_rect_matrix;
256 QuadRectTransform(&quad_rect_matrix, quad->quadTransform(), 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,
262 &sk_device_matrix);
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() &&
273 quad->IsRightEdge();
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->opacity() * 255);
283 current_paint_.setXfermodeMode(quad->shared_quad_state->blend_mode);
284 } else {
285 current_paint_.setXfermodeMode(SkXfermode::kSrc_Mode);
288 if (draw_region) {
289 gfx::QuadF local_draw_region(*draw_region);
290 SkPath draw_region_clip_path;
291 local_draw_region -=
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,
302 false);
305 switch (quad->material) {
306 case DrawQuad::CHECKERBOARD:
307 // TODO(enne) For now since checkerboards shouldn't be part of a 3D
308 // context, clipping regions aren't supported so we skip drawing them
309 // if this becomes the case.
310 if (!draw_region) {
311 DrawCheckerboardQuad(frame, CheckerboardDrawQuad::MaterialCast(quad));
313 break;
314 case DrawQuad::DEBUG_BORDER:
315 DrawDebugBorderQuad(frame, DebugBorderDrawQuad::MaterialCast(quad));
316 break;
317 case DrawQuad::PICTURE_CONTENT:
318 DrawPictureQuad(frame, PictureDrawQuad::MaterialCast(quad));
319 break;
320 case DrawQuad::RENDER_PASS:
321 DrawRenderPassQuad(frame, RenderPassDrawQuad::MaterialCast(quad));
322 break;
323 case DrawQuad::SOLID_COLOR:
324 DrawSolidColorQuad(frame, SolidColorDrawQuad::MaterialCast(quad));
325 break;
326 case DrawQuad::TEXTURE_CONTENT:
327 DrawTextureQuad(frame, TextureDrawQuad::MaterialCast(quad));
328 break;
329 case DrawQuad::TILED_CONTENT:
330 DrawTileQuad(frame, TileDrawQuad::MaterialCast(quad));
331 break;
332 case DrawQuad::SURFACE_CONTENT:
333 // Surface content should be fully resolved to other quad types before
334 // reaching a direct renderer.
335 NOTREACHED();
336 break;
337 case DrawQuad::INVALID:
338 case DrawQuad::IO_SURFACE_CONTENT:
339 case DrawQuad::YUV_VIDEO_CONTENT:
340 case DrawQuad::STREAM_VIDEO_CONTENT:
341 DrawUnsupportedQuad(frame, quad);
342 NOTREACHED();
343 break;
346 current_canvas_->resetMatrix();
347 if (draw_region) {
348 current_canvas_->restore();
352 void SoftwareRenderer::DrawCheckerboardQuad(const DrawingFrame* frame,
353 const CheckerboardDrawQuad* quad) {
354 gfx::RectF visible_quad_vertex_rect = MathUtil::ScaleRectProportional(
355 QuadVertexRect(), quad->rect, quad->visible_rect);
356 current_paint_.setColor(quad->color);
357 current_paint_.setAlpha(quad->opacity());
358 current_canvas_->drawRect(gfx::RectFToSkRect(visible_quad_vertex_rect),
359 current_paint_);
362 void SoftwareRenderer::DrawDebugBorderQuad(const DrawingFrame* frame,
363 const DebugBorderDrawQuad* quad) {
364 // We need to apply the matrix manually to have pixel-sized stroke width.
365 SkPoint vertices[4];
366 gfx::RectFToSkRect(QuadVertexRect()).toQuad(vertices);
367 SkPoint transformed_vertices[4];
368 current_canvas_->getTotalMatrix().mapPoints(transformed_vertices,
369 vertices,
371 current_canvas_->resetMatrix();
373 current_paint_.setColor(quad->color);
374 current_paint_.setAlpha(quad->opacity() * 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 // TODO(aelias): This isn't correct in all cases. We should detect these
391 // cases and fall back to a persistent bitmap backing
392 // (http://crbug.com/280374).
393 skia::RefPtr<SkDrawFilter> opacity_filter =
394 skia::AdoptRef(new skia::OpacityDrawFilter(
395 quad->opacity(), frame->disable_picture_quad_image_filtering ||
396 quad->nearest_neighbor));
397 DCHECK(!current_canvas_->getDrawFilter());
398 current_canvas_->setDrawFilter(opacity_filter.get());
400 TRACE_EVENT0("cc",
401 "SoftwareRenderer::DrawPictureQuad");
403 quad->raster_source->PlaybackToSharedCanvas(
404 current_canvas_, quad->content_rect, quad->contents_scale);
406 current_canvas_->setDrawFilter(NULL);
409 void SoftwareRenderer::DrawSolidColorQuad(const DrawingFrame* frame,
410 const SolidColorDrawQuad* quad) {
411 gfx::RectF visible_quad_vertex_rect = MathUtil::ScaleRectProportional(
412 QuadVertexRect(), quad->rect, quad->visible_rect);
413 current_paint_.setColor(quad->color);
414 current_paint_.setAlpha(quad->opacity() * SkColorGetA(quad->color));
415 current_canvas_->drawRect(gfx::RectFToSkRect(visible_quad_vertex_rect),
416 current_paint_);
419 void SoftwareRenderer::DrawTextureQuad(const DrawingFrame* frame,
420 const TextureDrawQuad* quad) {
421 if (!IsSoftwareResource(quad->resource_id)) {
422 DrawUnsupportedQuad(frame, quad);
423 return;
426 // TODO(skaslev): Add support for non-premultiplied alpha.
427 ResourceProvider::ScopedReadLockSoftware lock(resource_provider_,
428 quad->resource_id);
429 if (!lock.valid())
430 return;
431 const SkBitmap* bitmap = lock.sk_bitmap();
432 gfx::RectF uv_rect = gfx::ScaleRect(gfx::BoundingRect(quad->uv_top_left,
433 quad->uv_bottom_right),
434 bitmap->width(),
435 bitmap->height());
436 gfx::RectF visible_uv_rect =
437 MathUtil::ScaleRectProportional(uv_rect, quad->rect, quad->visible_rect);
438 SkRect sk_uv_rect = gfx::RectFToSkRect(visible_uv_rect);
439 gfx::RectF visible_quad_vertex_rect = MathUtil::ScaleRectProportional(
440 QuadVertexRect(), quad->rect, quad->visible_rect);
441 SkRect quad_rect = gfx::RectFToSkRect(visible_quad_vertex_rect);
443 if (quad->flipped)
444 current_canvas_->scale(1, -1);
446 bool blend_background = quad->background_color != SK_ColorTRANSPARENT &&
447 !bitmap->isOpaque();
448 bool needs_layer = blend_background && (current_paint_.getAlpha() != 0xFF);
449 if (needs_layer) {
450 current_canvas_->saveLayerAlpha(&quad_rect, current_paint_.getAlpha());
451 current_paint_.setAlpha(0xFF);
453 if (blend_background) {
454 SkPaint background_paint;
455 background_paint.setColor(quad->background_color);
456 current_canvas_->drawRect(quad_rect, background_paint);
458 SkShader::TileMode tile_mode = WrapModeToTileMode(lock.wrap_mode());
459 if (tile_mode != SkShader::kClamp_TileMode) {
460 SkMatrix matrix;
461 matrix.setRectToRect(sk_uv_rect, quad_rect, SkMatrix::kFill_ScaleToFit);
462 skia::RefPtr<SkShader> shader = skia::AdoptRef(
463 SkShader::CreateBitmapShader(*bitmap, tile_mode, tile_mode, &matrix));
464 SkPaint paint;
465 paint.setStyle(SkPaint::kFill_Style);
466 paint.setShader(shader.get());
467 current_canvas_->drawRect(quad_rect, paint);
468 } else {
469 current_canvas_->drawBitmapRectToRect(*bitmap,
470 &sk_uv_rect,
471 quad_rect,
472 &current_paint_);
475 if (needs_layer)
476 current_canvas_->restore();
479 void SoftwareRenderer::DrawTileQuad(const DrawingFrame* frame,
480 const TileDrawQuad* quad) {
481 // |resource_provider_| can be NULL in resourceless software draws, which
482 // should never produce tile quads in the first place.
483 DCHECK(resource_provider_);
484 DCHECK(IsSoftwareResource(quad->resource_id));
486 ResourceProvider::ScopedReadLockSoftware lock(resource_provider_,
487 quad->resource_id);
488 if (!lock.valid())
489 return;
490 DCHECK_EQ(GL_CLAMP_TO_EDGE, lock.wrap_mode());
492 gfx::RectF visible_tex_coord_rect = MathUtil::ScaleRectProportional(
493 quad->tex_coord_rect, quad->rect, quad->visible_rect);
494 gfx::RectF visible_quad_vertex_rect = MathUtil::ScaleRectProportional(
495 QuadVertexRect(), quad->rect, quad->visible_rect);
497 SkRect uv_rect = gfx::RectFToSkRect(visible_tex_coord_rect);
498 current_paint_.setFilterQuality(
499 quad->nearest_neighbor ? kNone_SkFilterQuality : kLow_SkFilterQuality);
500 current_canvas_->drawBitmapRectToRect(
501 *lock.sk_bitmap(),
502 &uv_rect,
503 gfx::RectFToSkRect(visible_quad_vertex_rect),
504 &current_paint_);
507 void SoftwareRenderer::DrawRenderPassQuad(const DrawingFrame* frame,
508 const RenderPassDrawQuad* quad) {
509 ScopedResource* content_texture =
510 render_pass_textures_.get(quad->render_pass_id);
511 if (!content_texture || !content_texture->id())
512 return;
514 DCHECK(IsSoftwareResource(content_texture->id()));
515 ResourceProvider::ScopedReadLockSoftware lock(resource_provider_,
516 content_texture->id());
517 if (!lock.valid())
518 return;
519 SkShader::TileMode content_tile_mode = WrapModeToTileMode(lock.wrap_mode());
521 SkRect dest_rect = gfx::RectFToSkRect(QuadVertexRect());
522 SkRect dest_visible_rect = gfx::RectFToSkRect(MathUtil::ScaleRectProportional(
523 QuadVertexRect(), quad->rect, quad->visible_rect));
524 SkRect content_rect = SkRect::MakeWH(quad->rect.width(), quad->rect.height());
526 SkMatrix content_mat;
527 content_mat.setRectToRect(content_rect, dest_rect,
528 SkMatrix::kFill_ScaleToFit);
530 const SkBitmap* content = lock.sk_bitmap();
532 SkBitmap filter_bitmap;
533 if (!quad->filters.IsEmpty()) {
534 skia::RefPtr<SkImageFilter> filter = RenderSurfaceFilters::BuildImageFilter(
535 quad->filters, content_texture->size());
536 // TODO(ajuma): Apply the filter in the same pass as the content where
537 // possible (e.g. when there's no origin offset). See crbug.com/308201.
538 if (filter) {
539 SkImageInfo info = SkImageInfo::MakeN32Premul(
540 content_texture->size().width(), content_texture->size().height());
541 if (filter_bitmap.tryAllocPixels(info)) {
542 SkCanvas canvas(filter_bitmap);
543 SkPaint paint;
544 paint.setImageFilter(filter.get());
545 canvas.clear(SK_ColorTRANSPARENT);
546 canvas.translate(SkIntToScalar(-quad->rect.origin().x()),
547 SkIntToScalar(-quad->rect.origin().y()));
548 canvas.scale(quad->filters_scale.x(), quad->filters_scale.y());
549 canvas.drawSprite(*content, 0, 0, &paint);
554 skia::RefPtr<SkShader> shader;
555 if (filter_bitmap.isNull()) {
556 shader = skia::AdoptRef(SkShader::CreateBitmapShader(
557 *content, content_tile_mode, content_tile_mode, &content_mat));
558 } else {
559 shader = skia::AdoptRef(SkShader::CreateBitmapShader(
560 filter_bitmap, content_tile_mode, content_tile_mode, &content_mat));
562 current_paint_.setShader(shader.get());
564 if (quad->mask_resource_id) {
565 ResourceProvider::ScopedReadLockSoftware mask_lock(resource_provider_,
566 quad->mask_resource_id);
567 if (!lock.valid())
568 return;
569 SkShader::TileMode mask_tile_mode = WrapModeToTileMode(
570 mask_lock.wrap_mode());
572 const SkBitmap* mask = mask_lock.sk_bitmap();
574 // Scale normalized uv rect into absolute texel coordinates.
575 SkRect mask_rect =
576 gfx::RectFToSkRect(gfx::ScaleRect(quad->MaskUVRect(),
577 quad->mask_texture_size.width(),
578 quad->mask_texture_size.height()));
580 SkMatrix mask_mat;
581 mask_mat.setRectToRect(mask_rect, dest_rect, SkMatrix::kFill_ScaleToFit);
583 skia::RefPtr<SkShader> mask_shader =
584 skia::AdoptRef(SkShader::CreateBitmapShader(
585 *mask, mask_tile_mode, mask_tile_mode, &mask_mat));
587 SkPaint mask_paint;
588 mask_paint.setShader(mask_shader.get());
590 SkLayerRasterizer::Builder builder;
591 builder.addLayer(mask_paint);
593 skia::RefPtr<SkLayerRasterizer> mask_rasterizer =
594 skia::AdoptRef(builder.detachRasterizer());
596 current_paint_.setRasterizer(mask_rasterizer.get());
597 current_canvas_->drawRect(dest_visible_rect, current_paint_);
598 } else {
599 // TODO(skaslev): Apply background filters
600 current_canvas_->drawRect(dest_visible_rect, current_paint_);
604 void SoftwareRenderer::DrawUnsupportedQuad(const DrawingFrame* frame,
605 const DrawQuad* quad) {
606 #ifdef NDEBUG
607 current_paint_.setColor(SK_ColorWHITE);
608 #else
609 current_paint_.setColor(SK_ColorMAGENTA);
610 #endif
611 current_paint_.setAlpha(quad->opacity() * 255);
612 current_canvas_->drawRect(gfx::RectFToSkRect(QuadVertexRect()),
613 current_paint_);
616 void SoftwareRenderer::CopyCurrentRenderPassToBitmap(
617 DrawingFrame* frame,
618 scoped_ptr<CopyOutputRequest> request) {
619 gfx::Rect copy_rect = frame->current_render_pass->output_rect;
620 if (request->has_area())
621 copy_rect.Intersect(request->area());
622 gfx::Rect window_copy_rect = MoveFromDrawToWindowSpace(frame, copy_rect);
624 scoped_ptr<SkBitmap> bitmap(new SkBitmap);
625 bitmap->setInfo(SkImageInfo::MakeN32Premul(window_copy_rect.width(),
626 window_copy_rect.height()));
627 current_canvas_->readPixels(
628 bitmap.get(), window_copy_rect.x(), window_copy_rect.y());
630 request->SendBitmapResult(bitmap.Pass());
633 void SoftwareRenderer::DiscardBackbuffer() {
634 if (is_backbuffer_discarded_)
635 return;
637 output_surface_->DiscardBackbuffer();
639 is_backbuffer_discarded_ = true;
641 // Damage tracker needs a full reset every time framebuffer is discarded.
642 client_->SetFullRootLayerDamage();
645 void SoftwareRenderer::EnsureBackbuffer() {
646 if (!is_backbuffer_discarded_)
647 return;
649 output_surface_->EnsureBackbuffer();
650 is_backbuffer_discarded_ = false;
653 void SoftwareRenderer::DidChangeVisibility() {
654 if (visible())
655 EnsureBackbuffer();
656 else
657 DiscardBackbuffer();
660 } // namespace cc