Support HTTP/2 drafts 14 and 15 simultaneously.
[chromium-blink-merge.git] / cc / output / software_renderer.cc
blob442cd8d5aea01c8ea12a18bb3bcb09d5f8d68161
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/debug/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/SkShader.h"
29 #include "third_party/skia/include/effects/SkLayerRasterizer.h"
30 #include "ui/gfx/geometry/rect_conversions.h"
31 #include "ui/gfx/skia_util.h"
32 #include "ui/gfx/transform.h"
34 namespace cc {
35 namespace {
37 static inline bool IsScalarNearlyInteger(SkScalar scalar) {
38 return SkScalarNearlyZero(scalar - SkScalarRoundToScalar(scalar));
41 bool IsScaleAndIntegerTranslate(const SkMatrix& matrix) {
42 return IsScalarNearlyInteger(matrix[SkMatrix::kMTransX]) &&
43 IsScalarNearlyInteger(matrix[SkMatrix::kMTransY]) &&
44 SkScalarNearlyZero(matrix[SkMatrix::kMSkewX]) &&
45 SkScalarNearlyZero(matrix[SkMatrix::kMSkewY]) &&
46 SkScalarNearlyZero(matrix[SkMatrix::kMPersp0]) &&
47 SkScalarNearlyZero(matrix[SkMatrix::kMPersp1]) &&
48 SkScalarNearlyZero(matrix[SkMatrix::kMPersp2] - 1.0f);
51 static SkShader::TileMode WrapModeToTileMode(GLint wrap_mode) {
52 switch (wrap_mode) {
53 case GL_REPEAT:
54 return SkShader::kRepeat_TileMode;
55 case GL_CLAMP_TO_EDGE:
56 return SkShader::kClamp_TileMode;
58 NOTREACHED();
59 return SkShader::kClamp_TileMode;
62 } // anonymous namespace
64 scoped_ptr<SoftwareRenderer> SoftwareRenderer::Create(
65 RendererClient* client,
66 const LayerTreeSettings* settings,
67 OutputSurface* output_surface,
68 ResourceProvider* resource_provider) {
69 return make_scoped_ptr(new SoftwareRenderer(
70 client, settings, output_surface, resource_provider));
73 SoftwareRenderer::SoftwareRenderer(RendererClient* client,
74 const LayerTreeSettings* settings,
75 OutputSurface* output_surface,
76 ResourceProvider* resource_provider)
77 : DirectRenderer(client, settings, output_surface, resource_provider),
78 is_scissor_enabled_(false),
79 is_backbuffer_discarded_(false),
80 output_device_(output_surface->software_device()),
81 current_canvas_(NULL) {
82 if (resource_provider_) {
83 capabilities_.max_texture_size = resource_provider_->max_texture_size();
84 capabilities_.best_texture_format =
85 resource_provider_->best_texture_format();
87 // The updater can access bitmaps while the SoftwareRenderer is using them.
88 capabilities_.allow_partial_texture_updates = true;
89 capabilities_.using_partial_swap = true;
91 capabilities_.using_shared_memory_resources = true;
93 capabilities_.allow_rasterize_on_demand = true;
96 SoftwareRenderer::~SoftwareRenderer() {}
98 const RendererCapabilitiesImpl& SoftwareRenderer::Capabilities() const {
99 return capabilities_;
102 void SoftwareRenderer::BeginDrawingFrame(DrawingFrame* frame) {
103 TRACE_EVENT0("cc", "SoftwareRenderer::BeginDrawingFrame");
104 root_canvas_ = output_device_->BeginPaint(
105 gfx::ToEnclosingRect(frame->root_damage_rect));
108 void SoftwareRenderer::FinishDrawingFrame(DrawingFrame* frame) {
109 TRACE_EVENT0("cc", "SoftwareRenderer::FinishDrawingFrame");
110 current_framebuffer_lock_ = nullptr;
111 current_canvas_ = NULL;
112 root_canvas_ = NULL;
114 current_frame_data_.reset(new SoftwareFrameData);
115 output_device_->EndPaint(current_frame_data_.get());
118 void SoftwareRenderer::SwapBuffers(const CompositorFrameMetadata& metadata) {
119 TRACE_EVENT0("cc,benchmark", "SoftwareRenderer::SwapBuffers");
120 CompositorFrame compositor_frame;
121 compositor_frame.metadata = metadata;
122 compositor_frame.software_frame_data = current_frame_data_.Pass();
123 output_surface_->SwapBuffers(&compositor_frame);
126 void SoftwareRenderer::ReceiveSwapBuffersAck(const CompositorFrameAck& ack) {
127 output_device_->ReclaimSoftwareFrame(ack.last_software_frame_id);
130 bool SoftwareRenderer::FlippedFramebuffer(const DrawingFrame* frame) const {
131 return false;
134 void SoftwareRenderer::EnsureScissorTestEnabled() {
135 is_scissor_enabled_ = true;
136 SetClipRect(scissor_rect_);
139 void SoftwareRenderer::EnsureScissorTestDisabled() {
140 // There is no explicit notion of enabling/disabling scissoring in software
141 // rendering, but the underlying effect we want is to clear any existing
142 // clipRect on the current SkCanvas. This is done by setting clipRect to
143 // the viewport's dimensions.
144 is_scissor_enabled_ = false;
145 SkISize size = current_canvas_->getDeviceSize();
146 SetClipRect(gfx::Rect(size.width(), size.height()));
149 void SoftwareRenderer::Finish() {}
151 void SoftwareRenderer::BindFramebufferToOutputSurface(DrawingFrame* frame) {
152 DCHECK(!output_surface_->HasExternalStencilTest());
153 current_framebuffer_lock_ = nullptr;
154 current_canvas_ = root_canvas_;
157 bool SoftwareRenderer::BindFramebufferToTexture(
158 DrawingFrame* frame,
159 const ScopedResource* texture,
160 const gfx::Rect& target_rect) {
161 current_framebuffer_lock_ = make_scoped_ptr(
162 new ResourceProvider::ScopedWriteLockSoftware(
163 resource_provider_, texture->id()));
164 current_canvas_ = current_framebuffer_lock_->sk_canvas();
165 InitializeViewport(frame,
166 target_rect,
167 gfx::Rect(target_rect.size()),
168 target_rect.size());
169 return true;
172 void SoftwareRenderer::SetScissorTestRect(const gfx::Rect& scissor_rect) {
173 is_scissor_enabled_ = true;
174 scissor_rect_ = scissor_rect;
175 SetClipRect(scissor_rect);
178 void SoftwareRenderer::SetClipRect(const gfx::Rect& rect) {
179 // Skia applies the current matrix to clip rects so we reset it temporary.
180 SkMatrix current_matrix = current_canvas_->getTotalMatrix();
181 current_canvas_->resetMatrix();
182 current_canvas_->clipRect(gfx::RectToSkRect(rect), SkRegion::kReplace_Op);
183 current_canvas_->setMatrix(current_matrix);
186 void SoftwareRenderer::ClearCanvas(SkColor color) {
187 // SkCanvas::clear doesn't respect the current clipping region
188 // so we SkCanvas::drawColor instead if scissoring is active.
189 if (is_scissor_enabled_)
190 current_canvas_->drawColor(color, SkXfermode::kSrc_Mode);
191 else
192 current_canvas_->clear(color);
195 void SoftwareRenderer::DiscardPixels(bool has_external_stencil_test,
196 bool draw_rect_covers_full_surface) {}
198 void SoftwareRenderer::ClearFramebuffer(DrawingFrame* frame,
199 bool has_external_stencil_test) {
200 if (frame->current_render_pass->has_transparent_background) {
201 ClearCanvas(SkColorSetARGB(0, 0, 0, 0));
202 } else {
203 #ifndef NDEBUG
204 // On DEBUG builds, opaque render passes are cleared to blue
205 // to easily see regions that were not drawn on the screen.
206 ClearCanvas(SkColorSetARGB(255, 0, 0, 255));
207 #endif
211 void SoftwareRenderer::SetDrawViewport(
212 const gfx::Rect& window_space_viewport) {}
214 bool SoftwareRenderer::IsSoftwareResource(
215 ResourceProvider::ResourceId resource_id) const {
216 switch (resource_provider_->GetResourceType(resource_id)) {
217 case ResourceProvider::GLTexture:
218 return false;
219 case ResourceProvider::Bitmap:
220 return true;
221 case ResourceProvider::InvalidType:
222 break;
225 LOG(FATAL) << "Invalid resource type.";
226 return false;
229 void SoftwareRenderer::DoDrawQuad(DrawingFrame* frame, const DrawQuad* quad) {
230 TRACE_EVENT0("cc", "SoftwareRenderer::DoDrawQuad");
231 gfx::Transform quad_rect_matrix;
232 QuadRectTransform(&quad_rect_matrix, quad->quadTransform(), quad->rect);
233 gfx::Transform contents_device_transform =
234 frame->window_matrix * frame->projection_matrix * quad_rect_matrix;
235 contents_device_transform.FlattenTo2d();
236 SkMatrix sk_device_matrix;
237 gfx::TransformToFlattenedSkMatrix(contents_device_transform,
238 &sk_device_matrix);
239 current_canvas_->setMatrix(sk_device_matrix);
241 current_paint_.reset();
242 if (settings_->force_antialiasing ||
243 !IsScaleAndIntegerTranslate(sk_device_matrix)) {
244 // TODO(danakj): Until we can enable AA only on exterior edges of the
245 // layer, disable AA if any interior edges are present. crbug.com/248175
246 bool all_four_edges_are_exterior = quad->IsTopEdge() &&
247 quad->IsLeftEdge() &&
248 quad->IsBottomEdge() &&
249 quad->IsRightEdge();
250 if (settings_->allow_antialiasing &&
251 (settings_->force_antialiasing || all_four_edges_are_exterior))
252 current_paint_.setAntiAlias(true);
253 current_paint_.setFilterLevel(SkPaint::kLow_FilterLevel);
256 if (quad->ShouldDrawWithBlending() ||
257 quad->shared_quad_state->blend_mode != SkXfermode::kSrcOver_Mode) {
258 current_paint_.setAlpha(quad->opacity() * 255);
259 current_paint_.setXfermodeMode(quad->shared_quad_state->blend_mode);
260 } else {
261 current_paint_.setXfermodeMode(SkXfermode::kSrc_Mode);
264 switch (quad->material) {
265 case DrawQuad::CHECKERBOARD:
266 DrawCheckerboardQuad(frame, CheckerboardDrawQuad::MaterialCast(quad));
267 break;
268 case DrawQuad::DEBUG_BORDER:
269 DrawDebugBorderQuad(frame, DebugBorderDrawQuad::MaterialCast(quad));
270 break;
271 case DrawQuad::PICTURE_CONTENT:
272 DrawPictureQuad(frame, PictureDrawQuad::MaterialCast(quad));
273 break;
274 case DrawQuad::RENDER_PASS:
275 DrawRenderPassQuad(frame, RenderPassDrawQuad::MaterialCast(quad));
276 break;
277 case DrawQuad::SOLID_COLOR:
278 DrawSolidColorQuad(frame, SolidColorDrawQuad::MaterialCast(quad));
279 break;
280 case DrawQuad::TEXTURE_CONTENT:
281 DrawTextureQuad(frame, TextureDrawQuad::MaterialCast(quad));
282 break;
283 case DrawQuad::TILED_CONTENT:
284 DrawTileQuad(frame, TileDrawQuad::MaterialCast(quad));
285 break;
286 case DrawQuad::SURFACE_CONTENT:
287 // Surface content should be fully resolved to other quad types before
288 // reaching a direct renderer.
289 NOTREACHED();
290 break;
291 case DrawQuad::INVALID:
292 case DrawQuad::IO_SURFACE_CONTENT:
293 case DrawQuad::YUV_VIDEO_CONTENT:
294 case DrawQuad::STREAM_VIDEO_CONTENT:
295 DrawUnsupportedQuad(frame, quad);
296 NOTREACHED();
297 break;
300 current_canvas_->resetMatrix();
303 void SoftwareRenderer::DrawCheckerboardQuad(const DrawingFrame* frame,
304 const CheckerboardDrawQuad* quad) {
305 gfx::RectF visible_quad_vertex_rect = MathUtil::ScaleRectProportional(
306 QuadVertexRect(), quad->rect, quad->visible_rect);
307 current_paint_.setColor(quad->color);
308 current_paint_.setAlpha(quad->opacity() * SkColorGetA(quad->color));
309 current_canvas_->drawRect(gfx::RectFToSkRect(visible_quad_vertex_rect),
310 current_paint_);
313 void SoftwareRenderer::DrawDebugBorderQuad(const DrawingFrame* frame,
314 const DebugBorderDrawQuad* quad) {
315 // We need to apply the matrix manually to have pixel-sized stroke width.
316 SkPoint vertices[4];
317 gfx::RectFToSkRect(QuadVertexRect()).toQuad(vertices);
318 SkPoint transformed_vertices[4];
319 current_canvas_->getTotalMatrix().mapPoints(transformed_vertices,
320 vertices,
322 current_canvas_->resetMatrix();
324 current_paint_.setColor(quad->color);
325 current_paint_.setAlpha(quad->opacity() * SkColorGetA(quad->color));
326 current_paint_.setStyle(SkPaint::kStroke_Style);
327 current_paint_.setStrokeWidth(quad->width);
328 current_canvas_->drawPoints(SkCanvas::kPolygon_PointMode,
329 4, transformed_vertices, current_paint_);
332 void SoftwareRenderer::DrawPictureQuad(const DrawingFrame* frame,
333 const PictureDrawQuad* quad) {
334 SkMatrix content_matrix;
335 content_matrix.setRectToRect(
336 gfx::RectFToSkRect(quad->tex_coord_rect),
337 gfx::RectFToSkRect(QuadVertexRect()),
338 SkMatrix::kFill_ScaleToFit);
339 current_canvas_->concat(content_matrix);
341 // TODO(aelias): This isn't correct in all cases. We should detect these
342 // cases and fall back to a persistent bitmap backing
343 // (http://crbug.com/280374).
344 skia::RefPtr<SkDrawFilter> opacity_filter =
345 skia::AdoptRef(new skia::OpacityDrawFilter(
346 quad->opacity(), frame->disable_picture_quad_image_filtering));
347 DCHECK(!current_canvas_->getDrawFilter());
348 current_canvas_->setDrawFilter(opacity_filter.get());
350 TRACE_EVENT0("cc",
351 "SoftwareRenderer::DrawPictureQuad");
353 quad->raster_source->PlaybackToSharedCanvas(
354 current_canvas_, quad->content_rect, quad->contents_scale);
356 current_canvas_->setDrawFilter(NULL);
359 void SoftwareRenderer::DrawSolidColorQuad(const DrawingFrame* frame,
360 const SolidColorDrawQuad* quad) {
361 gfx::RectF visible_quad_vertex_rect = MathUtil::ScaleRectProportional(
362 QuadVertexRect(), quad->rect, quad->visible_rect);
363 current_paint_.setColor(quad->color);
364 current_paint_.setAlpha(quad->opacity() * SkColorGetA(quad->color));
365 current_canvas_->drawRect(gfx::RectFToSkRect(visible_quad_vertex_rect),
366 current_paint_);
369 void SoftwareRenderer::DrawTextureQuad(const DrawingFrame* frame,
370 const TextureDrawQuad* quad) {
371 if (!IsSoftwareResource(quad->resource_id)) {
372 DrawUnsupportedQuad(frame, quad);
373 return;
376 // TODO(skaslev): Add support for non-premultiplied alpha.
377 ResourceProvider::ScopedReadLockSoftware lock(resource_provider_,
378 quad->resource_id);
379 if (!lock.valid())
380 return;
381 const SkBitmap* bitmap = lock.sk_bitmap();
382 gfx::RectF uv_rect = gfx::ScaleRect(gfx::BoundingRect(quad->uv_top_left,
383 quad->uv_bottom_right),
384 bitmap->width(),
385 bitmap->height());
386 gfx::RectF visible_uv_rect =
387 MathUtil::ScaleRectProportional(uv_rect, quad->rect, quad->visible_rect);
388 SkRect sk_uv_rect = gfx::RectFToSkRect(visible_uv_rect);
389 gfx::RectF visible_quad_vertex_rect = MathUtil::ScaleRectProportional(
390 QuadVertexRect(), quad->rect, quad->visible_rect);
391 SkRect quad_rect = gfx::RectFToSkRect(visible_quad_vertex_rect);
393 if (quad->flipped)
394 current_canvas_->scale(1, -1);
396 bool blend_background = quad->background_color != SK_ColorTRANSPARENT &&
397 !bitmap->isOpaque();
398 bool needs_layer = blend_background && (current_paint_.getAlpha() != 0xFF);
399 if (needs_layer) {
400 current_canvas_->saveLayerAlpha(&quad_rect, current_paint_.getAlpha());
401 current_paint_.setAlpha(0xFF);
403 if (blend_background) {
404 SkPaint background_paint;
405 background_paint.setColor(quad->background_color);
406 current_canvas_->drawRect(quad_rect, background_paint);
408 SkShader::TileMode tile_mode = WrapModeToTileMode(lock.wrap_mode());
409 if (tile_mode != SkShader::kClamp_TileMode) {
410 SkMatrix matrix;
411 matrix.setRectToRect(sk_uv_rect, quad_rect, SkMatrix::kFill_ScaleToFit);
412 skia::RefPtr<SkShader> shader = skia::AdoptRef(
413 SkShader::CreateBitmapShader(*bitmap, tile_mode, tile_mode, &matrix));
414 SkPaint paint;
415 paint.setStyle(SkPaint::kFill_Style);
416 paint.setShader(shader.get());
417 current_canvas_->drawRect(quad_rect, paint);
418 } else {
419 current_canvas_->drawBitmapRectToRect(*bitmap,
420 &sk_uv_rect,
421 quad_rect,
422 &current_paint_);
425 if (needs_layer)
426 current_canvas_->restore();
429 void SoftwareRenderer::DrawTileQuad(const DrawingFrame* frame,
430 const TileDrawQuad* quad) {
431 // |resource_provider_| can be NULL in resourceless software draws, which
432 // should never produce tile quads in the first place.
433 DCHECK(resource_provider_);
434 DCHECK(IsSoftwareResource(quad->resource_id));
436 ResourceProvider::ScopedReadLockSoftware lock(resource_provider_,
437 quad->resource_id);
438 if (!lock.valid())
439 return;
440 DCHECK_EQ(GL_CLAMP_TO_EDGE, lock.wrap_mode());
442 gfx::RectF visible_tex_coord_rect = MathUtil::ScaleRectProportional(
443 quad->tex_coord_rect, quad->rect, quad->visible_rect);
444 gfx::RectF visible_quad_vertex_rect = MathUtil::ScaleRectProportional(
445 QuadVertexRect(), quad->rect, quad->visible_rect);
447 SkRect uv_rect = gfx::RectFToSkRect(visible_tex_coord_rect);
448 current_paint_.setFilterLevel(SkPaint::kLow_FilterLevel);
449 current_canvas_->drawBitmapRectToRect(
450 *lock.sk_bitmap(),
451 &uv_rect,
452 gfx::RectFToSkRect(visible_quad_vertex_rect),
453 &current_paint_);
456 void SoftwareRenderer::DrawRenderPassQuad(const DrawingFrame* frame,
457 const RenderPassDrawQuad* quad) {
458 ScopedResource* content_texture =
459 render_pass_textures_.get(quad->render_pass_id);
460 if (!content_texture || !content_texture->id())
461 return;
463 DCHECK(IsSoftwareResource(content_texture->id()));
464 ResourceProvider::ScopedReadLockSoftware lock(resource_provider_,
465 content_texture->id());
466 if (!lock.valid())
467 return;
468 SkShader::TileMode content_tile_mode = WrapModeToTileMode(lock.wrap_mode());
470 SkRect dest_rect = gfx::RectFToSkRect(QuadVertexRect());
471 SkRect dest_visible_rect = gfx::RectFToSkRect(MathUtil::ScaleRectProportional(
472 QuadVertexRect(), quad->rect, quad->visible_rect));
473 SkRect content_rect = SkRect::MakeWH(quad->rect.width(), quad->rect.height());
475 SkMatrix content_mat;
476 content_mat.setRectToRect(content_rect, dest_rect,
477 SkMatrix::kFill_ScaleToFit);
479 const SkBitmap* content = lock.sk_bitmap();
481 SkBitmap filter_bitmap;
482 if (!quad->filters.IsEmpty()) {
483 skia::RefPtr<SkImageFilter> filter = RenderSurfaceFilters::BuildImageFilter(
484 quad->filters, content_texture->size());
485 // TODO(ajuma): Apply the filter in the same pass as the content where
486 // possible (e.g. when there's no origin offset). See crbug.com/308201.
487 if (filter) {
488 SkImageInfo info = SkImageInfo::MakeN32Premul(
489 content_texture->size().width(), content_texture->size().height());
490 if (filter_bitmap.tryAllocPixels(info)) {
491 SkCanvas canvas(filter_bitmap);
492 SkPaint paint;
493 paint.setImageFilter(filter.get());
494 canvas.clear(SK_ColorTRANSPARENT);
495 canvas.translate(SkIntToScalar(-quad->rect.origin().x()),
496 SkIntToScalar(-quad->rect.origin().y()));
497 canvas.scale(quad->filters_scale.x(), quad->filters_scale.y());
498 canvas.drawSprite(*content, 0, 0, &paint);
503 skia::RefPtr<SkShader> shader;
504 if (filter_bitmap.isNull()) {
505 shader = skia::AdoptRef(SkShader::CreateBitmapShader(
506 *content, content_tile_mode, content_tile_mode, &content_mat));
507 } else {
508 shader = skia::AdoptRef(SkShader::CreateBitmapShader(
509 filter_bitmap, content_tile_mode, content_tile_mode, &content_mat));
511 current_paint_.setShader(shader.get());
513 if (quad->mask_resource_id) {
514 ResourceProvider::ScopedReadLockSoftware mask_lock(resource_provider_,
515 quad->mask_resource_id);
516 if (!lock.valid())
517 return;
518 SkShader::TileMode mask_tile_mode = WrapModeToTileMode(
519 mask_lock.wrap_mode());
521 const SkBitmap* mask = mask_lock.sk_bitmap();
523 // Scale normalized uv rect into absolute texel coordinates.
524 SkRect mask_rect =
525 gfx::RectFToSkRect(gfx::ScaleRect(quad->MaskUVRect(),
526 quad->mask_texture_size.width(),
527 quad->mask_texture_size.height()));
529 SkMatrix mask_mat;
530 mask_mat.setRectToRect(mask_rect, dest_rect, SkMatrix::kFill_ScaleToFit);
532 skia::RefPtr<SkShader> mask_shader =
533 skia::AdoptRef(SkShader::CreateBitmapShader(
534 *mask, mask_tile_mode, mask_tile_mode, &mask_mat));
536 SkPaint mask_paint;
537 mask_paint.setShader(mask_shader.get());
539 SkLayerRasterizer::Builder builder;
540 builder.addLayer(mask_paint);
542 skia::RefPtr<SkLayerRasterizer> mask_rasterizer =
543 skia::AdoptRef(builder.detachRasterizer());
545 current_paint_.setRasterizer(mask_rasterizer.get());
546 current_canvas_->drawRect(dest_visible_rect, current_paint_);
547 } else {
548 // TODO(skaslev): Apply background filters
549 current_canvas_->drawRect(dest_visible_rect, current_paint_);
553 void SoftwareRenderer::DrawUnsupportedQuad(const DrawingFrame* frame,
554 const DrawQuad* quad) {
555 #ifdef NDEBUG
556 current_paint_.setColor(SK_ColorWHITE);
557 #else
558 current_paint_.setColor(SK_ColorMAGENTA);
559 #endif
560 current_paint_.setAlpha(quad->opacity() * 255);
561 current_canvas_->drawRect(gfx::RectFToSkRect(QuadVertexRect()),
562 current_paint_);
565 void SoftwareRenderer::CopyCurrentRenderPassToBitmap(
566 DrawingFrame* frame,
567 scoped_ptr<CopyOutputRequest> request) {
568 gfx::Rect copy_rect = frame->current_render_pass->output_rect;
569 if (request->has_area())
570 copy_rect.Intersect(request->area());
571 gfx::Rect window_copy_rect = MoveFromDrawToWindowSpace(frame, copy_rect);
573 scoped_ptr<SkBitmap> bitmap(new SkBitmap);
574 bitmap->setInfo(SkImageInfo::MakeN32Premul(window_copy_rect.width(),
575 window_copy_rect.height()));
576 current_canvas_->readPixels(
577 bitmap.get(), window_copy_rect.x(), window_copy_rect.y());
579 request->SendBitmapResult(bitmap.Pass());
582 void SoftwareRenderer::DiscardBackbuffer() {
583 if (is_backbuffer_discarded_)
584 return;
586 output_surface_->DiscardBackbuffer();
588 is_backbuffer_discarded_ = true;
590 // Damage tracker needs a full reset every time framebuffer is discarded.
591 client_->SetFullRootLayerDamage();
594 void SoftwareRenderer::EnsureBackbuffer() {
595 if (!is_backbuffer_discarded_)
596 return;
598 output_surface_->EnsureBackbuffer();
599 is_backbuffer_discarded_ = false;
602 void SoftwareRenderer::DidChangeVisibility() {
603 if (visible())
604 EnsureBackbuffer();
605 else
606 DiscardBackbuffer();
609 } // namespace cc