Pass gfx::Rect and gfx::RectF by const ref.
[chromium-blink-merge.git] / cc / output / software_renderer.cc
blob7a0593dee4e36b9da12d7a3e1dcadb1c9e80e8a4
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/rect_conversions.h"
31 #include "ui/gfx/skia_util.h"
32 #include "ui/gfx/transform.h"
34 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 LayerTreeSettings* 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 LayerTreeSettings* settings,
76 OutputSurface* output_surface,
77 ResourceProvider* resource_provider)
78 : DirectRenderer(client, settings, output_surface, resource_provider),
79 visible_(true),
80 is_scissor_enabled_(false),
81 is_backbuffer_discarded_(false),
82 output_device_(output_surface->software_device()),
83 current_canvas_(NULL) {
84 if (resource_provider_) {
85 capabilities_.max_texture_size = resource_provider_->max_texture_size();
86 capabilities_.best_texture_format =
87 resource_provider_->best_texture_format();
89 // The updater can access bitmaps while the SoftwareRenderer is using them.
90 capabilities_.allow_partial_texture_updates = true;
91 capabilities_.using_partial_swap = true;
93 capabilities_.using_map_image = settings_->use_map_image;
94 capabilities_.using_shared_memory_resources = true;
97 SoftwareRenderer::~SoftwareRenderer() {}
99 const RendererCapabilities& 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_.reset();
112 current_canvas_ = NULL;
113 root_canvas_ = NULL;
115 current_frame_data_.reset(new SoftwareFrameData);
116 output_device_->EndPaint(current_frame_data_.get());
119 void SoftwareRenderer::SwapBuffers(const CompositorFrameMetadata& metadata) {
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 {
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 SkBaseDevice* device = current_canvas_->getDevice();
146 SetClipRect(gfx::Rect(device->width(), device->height()));
149 void SoftwareRenderer::Finish() {}
151 void SoftwareRenderer::BindFramebufferToOutputSurface(DrawingFrame* frame) {
152 DCHECK(!output_surface_->HasExternalStencilTest());
153 current_framebuffer_lock_.reset();
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_.reset();
162 current_framebuffer_lock_ = make_scoped_ptr(
163 new ResourceProvider::ScopedWriteLockSoftware(
164 resource_provider_, texture->id()));
165 current_canvas_ = current_framebuffer_lock_->sk_canvas();
166 InitializeViewport(frame,
167 target_rect,
168 gfx::Rect(target_rect.size()),
169 target_rect.size());
170 return true;
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 // Skia applies the current matrix to clip rects so we reset it temporary.
181 SkMatrix current_matrix = current_canvas_->getTotalMatrix();
182 current_canvas_->resetMatrix();
183 current_canvas_->clipRect(gfx::RectToSkRect(rect), SkRegion::kReplace_Op);
184 current_canvas_->setMatrix(current_matrix);
187 void SoftwareRenderer::ClearCanvas(SkColor color) {
188 // SkCanvas::clear doesn't respect the current clipping region
189 // so we SkCanvas::drawColor instead if scissoring is active.
190 if (is_scissor_enabled_)
191 current_canvas_->drawColor(color, SkXfermode::kSrc_Mode);
192 else
193 current_canvas_->clear(color);
196 void SoftwareRenderer::DiscardPixels(bool has_external_stencil_test,
197 bool draw_rect_covers_full_surface) {}
199 void SoftwareRenderer::ClearFramebuffer(DrawingFrame* frame,
200 bool has_external_stencil_test) {
201 if (frame->current_render_pass->has_transparent_background) {
202 ClearCanvas(SkColorSetARGB(0, 0, 0, 0));
203 } else {
204 #ifndef NDEBUG
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));
208 #endif
212 void SoftwareRenderer::SetDrawViewport(
213 const gfx::Rect& window_space_viewport) {}
215 bool SoftwareRenderer::IsSoftwareResource(
216 ResourceProvider::ResourceId resource_id) const {
217 switch (resource_provider_->GetResourceType(resource_id)) {
218 case ResourceProvider::GLTexture:
219 return false;
220 case ResourceProvider::Bitmap:
221 return true;
222 case ResourceProvider::InvalidType:
223 break;
226 LOG(FATAL) << "Invalid resource type.";
227 return false;
230 void SoftwareRenderer::DoDrawQuad(DrawingFrame* frame, const DrawQuad* quad) {
231 TRACE_EVENT0("cc", "SoftwareRenderer::DoDrawQuad");
232 gfx::Transform quad_rect_matrix;
233 QuadRectTransform(&quad_rect_matrix, quad->quadTransform(), quad->rect);
234 gfx::Transform contents_device_transform =
235 frame->window_matrix * frame->projection_matrix * quad_rect_matrix;
236 contents_device_transform.FlattenTo2d();
237 SkMatrix sk_device_matrix;
238 gfx::TransformToFlattenedSkMatrix(contents_device_transform,
239 &sk_device_matrix);
240 current_canvas_->setMatrix(sk_device_matrix);
242 current_paint_.reset();
243 if (!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 && all_four_edges_are_exterior)
251 current_paint_.setAntiAlias(true);
252 current_paint_.setFilterBitmap(true);
255 if (quad->ShouldDrawWithBlending()) {
256 current_paint_.setAlpha(quad->opacity() * 255);
257 current_paint_.setXfermodeMode(SkXfermode::kSrcOver_Mode);
258 } else {
259 current_paint_.setXfermodeMode(SkXfermode::kSrc_Mode);
262 switch (quad->material) {
263 case DrawQuad::CHECKERBOARD:
264 DrawCheckerboardQuad(frame, CheckerboardDrawQuad::MaterialCast(quad));
265 break;
266 case DrawQuad::DEBUG_BORDER:
267 DrawDebugBorderQuad(frame, DebugBorderDrawQuad::MaterialCast(quad));
268 break;
269 case DrawQuad::PICTURE_CONTENT:
270 DrawPictureQuad(frame, PictureDrawQuad::MaterialCast(quad));
271 break;
272 case DrawQuad::RENDER_PASS:
273 DrawRenderPassQuad(frame, RenderPassDrawQuad::MaterialCast(quad));
274 break;
275 case DrawQuad::SOLID_COLOR:
276 DrawSolidColorQuad(frame, SolidColorDrawQuad::MaterialCast(quad));
277 break;
278 case DrawQuad::TEXTURE_CONTENT:
279 DrawTextureQuad(frame, TextureDrawQuad::MaterialCast(quad));
280 break;
281 case DrawQuad::TILED_CONTENT:
282 DrawTileQuad(frame, TileDrawQuad::MaterialCast(quad));
283 break;
284 case DrawQuad::INVALID:
285 case DrawQuad::IO_SURFACE_CONTENT:
286 case DrawQuad::YUV_VIDEO_CONTENT:
287 case DrawQuad::STREAM_VIDEO_CONTENT:
288 DrawUnsupportedQuad(frame, quad);
289 NOTREACHED();
290 break;
293 current_canvas_->resetMatrix();
296 void SoftwareRenderer::DrawCheckerboardQuad(const DrawingFrame* frame,
297 const CheckerboardDrawQuad* quad) {
298 gfx::RectF visible_quad_vertex_rect = MathUtil::ScaleRectProportional(
299 QuadVertexRect(), quad->rect, quad->visible_rect);
300 current_paint_.setColor(quad->color);
301 current_paint_.setAlpha(quad->opacity() * SkColorGetA(quad->color));
302 current_canvas_->drawRect(gfx::RectFToSkRect(visible_quad_vertex_rect),
303 current_paint_);
306 void SoftwareRenderer::DrawDebugBorderQuad(const DrawingFrame* frame,
307 const DebugBorderDrawQuad* quad) {
308 // We need to apply the matrix manually to have pixel-sized stroke width.
309 SkPoint vertices[4];
310 gfx::RectFToSkRect(QuadVertexRect()).toQuad(vertices);
311 SkPoint transformed_vertices[4];
312 current_canvas_->getTotalMatrix().mapPoints(transformed_vertices,
313 vertices,
315 current_canvas_->resetMatrix();
317 current_paint_.setColor(quad->color);
318 current_paint_.setAlpha(quad->opacity() * SkColorGetA(quad->color));
319 current_paint_.setStyle(SkPaint::kStroke_Style);
320 current_paint_.setStrokeWidth(quad->width);
321 current_canvas_->drawPoints(SkCanvas::kPolygon_PointMode,
322 4, transformed_vertices, current_paint_);
325 void SoftwareRenderer::DrawPictureQuad(const DrawingFrame* frame,
326 const PictureDrawQuad* quad) {
327 SkMatrix content_matrix;
328 content_matrix.setRectToRect(
329 gfx::RectFToSkRect(quad->tex_coord_rect),
330 gfx::RectFToSkRect(QuadVertexRect()),
331 SkMatrix::kFill_ScaleToFit);
332 current_canvas_->concat(content_matrix);
334 // TODO(aelias): This isn't correct in all cases. We should detect these
335 // cases and fall back to a persistent bitmap backing
336 // (http://crbug.com/280374).
337 skia::RefPtr<SkDrawFilter> opacity_filter =
338 skia::AdoptRef(new skia::OpacityDrawFilter(
339 quad->opacity(), frame->disable_picture_quad_image_filtering));
340 DCHECK(!current_canvas_->getDrawFilter());
341 current_canvas_->setDrawFilter(opacity_filter.get());
343 TRACE_EVENT0("cc",
344 "SoftwareRenderer::DrawPictureQuad");
345 quad->picture_pile->RasterDirect(
346 current_canvas_, quad->content_rect, quad->contents_scale, NULL);
348 current_canvas_->setDrawFilter(NULL);
351 void SoftwareRenderer::DrawSolidColorQuad(const DrawingFrame* frame,
352 const SolidColorDrawQuad* quad) {
353 gfx::RectF visible_quad_vertex_rect = MathUtil::ScaleRectProportional(
354 QuadVertexRect(), quad->rect, quad->visible_rect);
355 current_paint_.setColor(quad->color);
356 current_paint_.setAlpha(quad->opacity() * SkColorGetA(quad->color));
357 current_canvas_->drawRect(gfx::RectFToSkRect(visible_quad_vertex_rect),
358 current_paint_);
361 void SoftwareRenderer::DrawTextureQuad(const DrawingFrame* frame,
362 const TextureDrawQuad* quad) {
363 if (!IsSoftwareResource(quad->resource_id)) {
364 DrawUnsupportedQuad(frame, quad);
365 return;
368 // TODO(skaslev): Add support for non-premultiplied alpha.
369 ResourceProvider::ScopedReadLockSoftware lock(resource_provider_,
370 quad->resource_id);
371 const SkBitmap* bitmap = lock.sk_bitmap();
372 gfx::RectF uv_rect = gfx::ScaleRect(gfx::BoundingRect(quad->uv_top_left,
373 quad->uv_bottom_right),
374 bitmap->width(),
375 bitmap->height());
376 gfx::RectF visible_uv_rect =
377 MathUtil::ScaleRectProportional(uv_rect, quad->rect, quad->visible_rect);
378 SkRect sk_uv_rect = gfx::RectFToSkRect(visible_uv_rect);
379 gfx::RectF visible_quad_vertex_rect = MathUtil::ScaleRectProportional(
380 QuadVertexRect(), quad->rect, quad->visible_rect);
381 SkRect quad_rect = gfx::RectFToSkRect(visible_quad_vertex_rect);
383 if (quad->flipped)
384 current_canvas_->scale(1, -1);
386 bool blend_background = quad->background_color != SK_ColorTRANSPARENT &&
387 !bitmap->isOpaque();
388 bool needs_layer = blend_background && (current_paint_.getAlpha() != 0xFF);
389 if (needs_layer) {
390 current_canvas_->saveLayerAlpha(&quad_rect, current_paint_.getAlpha());
391 current_paint_.setAlpha(0xFF);
393 if (blend_background) {
394 SkPaint background_paint;
395 background_paint.setColor(quad->background_color);
396 current_canvas_->drawRect(quad_rect, background_paint);
398 SkShader::TileMode tile_mode = WrapModeToTileMode(lock.wrap_mode());
399 if (tile_mode != SkShader::kClamp_TileMode) {
400 SkMatrix matrix;
401 matrix.setRectToRect(sk_uv_rect, quad_rect, SkMatrix::kFill_ScaleToFit);
402 skia::RefPtr<SkShader> shader = skia::AdoptRef(
403 SkShader::CreateBitmapShader(*bitmap, tile_mode, tile_mode));
404 shader->setLocalMatrix(matrix);
405 SkPaint paint;
406 paint.setStyle(SkPaint::kFill_Style);
407 paint.setShader(shader.get());
408 current_canvas_->drawRect(quad_rect, paint);
409 } else {
410 current_canvas_->drawBitmapRectToRect(*bitmap,
411 &sk_uv_rect,
412 quad_rect,
413 &current_paint_);
416 if (needs_layer)
417 current_canvas_->restore();
420 void SoftwareRenderer::DrawTileQuad(const DrawingFrame* frame,
421 const TileDrawQuad* quad) {
422 DCHECK(!output_surface_->ForcedDrawToSoftwareDevice());
423 DCHECK(IsSoftwareResource(quad->resource_id));
425 ResourceProvider::ScopedReadLockSoftware lock(resource_provider_,
426 quad->resource_id);
427 DCHECK_EQ(GL_CLAMP_TO_EDGE, lock.wrap_mode());
429 gfx::RectF visible_tex_coord_rect = MathUtil::ScaleRectProportional(
430 quad->tex_coord_rect, quad->rect, quad->visible_rect);
431 gfx::RectF visible_quad_vertex_rect = MathUtil::ScaleRectProportional(
432 QuadVertexRect(), quad->rect, quad->visible_rect);
434 SkRect uv_rect = gfx::RectFToSkRect(visible_tex_coord_rect);
435 current_paint_.setFilterBitmap(true);
436 current_canvas_->drawBitmapRectToRect(
437 *lock.sk_bitmap(),
438 &uv_rect,
439 gfx::RectFToSkRect(visible_quad_vertex_rect),
440 &current_paint_);
443 void SoftwareRenderer::DrawRenderPassQuad(const DrawingFrame* frame,
444 const RenderPassDrawQuad* quad) {
445 ScopedResource* content_texture =
446 render_pass_textures_.get(quad->render_pass_id);
447 if (!content_texture || !content_texture->id())
448 return;
450 DCHECK(IsSoftwareResource(content_texture->id()));
451 ResourceProvider::ScopedReadLockSoftware lock(resource_provider_,
452 content_texture->id());
453 SkShader::TileMode content_tile_mode = WrapModeToTileMode(lock.wrap_mode());
455 SkRect dest_rect = gfx::RectFToSkRect(QuadVertexRect());
456 SkRect dest_visible_rect = gfx::RectFToSkRect(MathUtil::ScaleRectProportional(
457 QuadVertexRect(), quad->rect, quad->visible_rect));
458 SkRect content_rect = SkRect::MakeWH(quad->rect.width(), quad->rect.height());
460 SkMatrix content_mat;
461 content_mat.setRectToRect(content_rect, dest_rect,
462 SkMatrix::kFill_ScaleToFit);
464 const SkBitmap* content = lock.sk_bitmap();
466 SkBitmap filter_bitmap;
467 if (!quad->filters.IsEmpty()) {
468 skia::RefPtr<SkImageFilter> filter = RenderSurfaceFilters::BuildImageFilter(
469 quad->filters, content_texture->size());
470 // TODO(ajuma): In addition origin translation, the canvas should also be
471 // scaled to accomodate device pixel ratio and pinch zoom. See
472 // crbug.com/281516 and crbug.com/281518.
473 // TODO(ajuma): Apply the filter in the same pass as the content where
474 // possible (e.g. when there's no origin offset). See crbug.com/308201.
475 if (filter) {
476 bool is_opaque = false;
477 skia::RefPtr<SkBaseDevice> device =
478 skia::AdoptRef(new SkBitmapDevice(SkBitmap::kARGB_8888_Config,
479 content_texture->size().width(),
480 content_texture->size().height(),
481 is_opaque));
482 SkCanvas canvas(device.get());
483 SkPaint paint;
484 paint.setImageFilter(filter.get());
485 canvas.clear(SK_ColorTRANSPARENT);
486 canvas.translate(SkIntToScalar(-quad->rect.origin().x()),
487 SkIntToScalar(-quad->rect.origin().y()));
488 canvas.drawSprite(*content, 0, 0, &paint);
489 bool will_change_pixels = false;
490 filter_bitmap = device->accessBitmap(will_change_pixels);
494 skia::RefPtr<SkShader> shader;
495 if (filter_bitmap.isNull()) {
496 shader = skia::AdoptRef(SkShader::CreateBitmapShader(
497 *content, content_tile_mode, content_tile_mode));
498 } else {
499 shader = skia::AdoptRef(SkShader::CreateBitmapShader(
500 filter_bitmap, content_tile_mode, content_tile_mode));
502 shader->setLocalMatrix(content_mat);
503 current_paint_.setShader(shader.get());
505 if (quad->mask_resource_id) {
506 ResourceProvider::ScopedReadLockSoftware mask_lock(resource_provider_,
507 quad->mask_resource_id);
508 SkShader::TileMode mask_tile_mode = WrapModeToTileMode(
509 mask_lock.wrap_mode());
511 const SkBitmap* mask = mask_lock.sk_bitmap();
513 SkRect mask_rect = SkRect::MakeXYWH(
514 quad->mask_uv_rect.x() * mask->width(),
515 quad->mask_uv_rect.y() * mask->height(),
516 quad->mask_uv_rect.width() * mask->width(),
517 quad->mask_uv_rect.height() * mask->height());
519 SkMatrix mask_mat;
520 mask_mat.setRectToRect(mask_rect, dest_rect, SkMatrix::kFill_ScaleToFit);
522 skia::RefPtr<SkShader> mask_shader = skia::AdoptRef(
523 SkShader::CreateBitmapShader(*mask, mask_tile_mode, mask_tile_mode));
524 mask_shader->setLocalMatrix(mask_mat);
526 SkPaint mask_paint;
527 mask_paint.setShader(mask_shader.get());
529 skia::RefPtr<SkLayerRasterizer> mask_rasterizer =
530 skia::AdoptRef(new SkLayerRasterizer);
531 mask_rasterizer->addLayer(mask_paint);
533 current_paint_.setRasterizer(mask_rasterizer.get());
534 current_canvas_->drawRect(dest_visible_rect, current_paint_);
535 } else {
536 // TODO(skaslev): Apply background filters and blend with content
537 current_canvas_->drawRect(dest_visible_rect, current_paint_);
541 void SoftwareRenderer::DrawUnsupportedQuad(const DrawingFrame* frame,
542 const DrawQuad* quad) {
543 #ifdef NDEBUG
544 current_paint_.setColor(SK_ColorWHITE);
545 #else
546 current_paint_.setColor(SK_ColorMAGENTA);
547 #endif
548 current_paint_.setAlpha(quad->opacity() * 255);
549 current_canvas_->drawRect(gfx::RectFToSkRect(QuadVertexRect()),
550 current_paint_);
553 void SoftwareRenderer::CopyCurrentRenderPassToBitmap(
554 DrawingFrame* frame,
555 scoped_ptr<CopyOutputRequest> request) {
556 gfx::Rect copy_rect = frame->current_render_pass->output_rect;
557 if (request->has_area())
558 copy_rect.Intersect(request->area());
559 gfx::Rect window_copy_rect = MoveFromDrawToWindowSpace(copy_rect);
561 scoped_ptr<SkBitmap> bitmap(new SkBitmap);
562 bitmap->setConfig(SkBitmap::kARGB_8888_Config,
563 window_copy_rect.width(),
564 window_copy_rect.height());
565 current_canvas_->readPixels(
566 bitmap.get(), window_copy_rect.x(), window_copy_rect.y());
568 request->SendBitmapResult(bitmap.Pass());
571 void SoftwareRenderer::DiscardBackbuffer() {
572 if (is_backbuffer_discarded_)
573 return;
575 output_surface_->DiscardBackbuffer();
577 is_backbuffer_discarded_ = true;
579 // Damage tracker needs a full reset every time framebuffer is discarded.
580 client_->SetFullRootLayerDamage();
583 void SoftwareRenderer::EnsureBackbuffer() {
584 if (!is_backbuffer_discarded_)
585 return;
587 output_surface_->EnsureBackbuffer();
588 is_backbuffer_discarded_ = false;
591 void SoftwareRenderer::GetFramebufferPixels(void* pixels,
592 const gfx::Rect& rect) {
593 TRACE_EVENT0("cc", "SoftwareRenderer::GetFramebufferPixels");
594 SkBitmap subset_bitmap;
595 gfx::Rect frame_rect(rect);
596 frame_rect += current_viewport_rect_.OffsetFromOrigin();
597 output_device_->CopyToBitmap(frame_rect, &subset_bitmap);
598 subset_bitmap.copyPixelsTo(pixels,
599 4 * frame_rect.width() * frame_rect.height(),
600 4 * frame_rect.width());
603 void SoftwareRenderer::SetVisible(bool visible) {
604 if (visible_ == visible)
605 return;
606 visible_ = visible;
608 if (visible_)
609 EnsureBackbuffer();
610 else
611 DiscardBackbuffer();
614 } // namespace cc