Add signalSyncPoint to the WebGraphicsContext3D command buffer impls.
[chromium-blink-merge.git] / cc / output / software_renderer.cc
blobfd4b5a645f1caedf56baa07fa6b263a324de86dd
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/output_surface.h"
13 #include "cc/output/software_output_device.h"
14 #include "cc/quads/debug_border_draw_quad.h"
15 #include "cc/quads/render_pass_draw_quad.h"
16 #include "cc/quads/solid_color_draw_quad.h"
17 #include "cc/quads/texture_draw_quad.h"
18 #include "cc/quads/tile_draw_quad.h"
19 #include "third_party/skia/include/core/SkCanvas.h"
20 #include "third_party/skia/include/core/SkColor.h"
21 #include "third_party/skia/include/core/SkDevice.h"
22 #include "third_party/skia/include/core/SkMatrix.h"
23 #include "third_party/skia/include/core/SkShader.h"
24 #include "third_party/skia/include/effects/SkLayerRasterizer.h"
25 #include "ui/gfx/rect_conversions.h"
26 #include "ui/gfx/skia_util.h"
27 #include "ui/gfx/transform.h"
29 namespace cc {
31 namespace {
33 void ToSkMatrix(SkMatrix* flattened, const gfx::Transform& m) {
34 // Convert from 4x4 to 3x3 by dropping the third row and column.
35 flattened->set(0, SkDoubleToScalar(m.matrix().getDouble(0, 0)));
36 flattened->set(1, SkDoubleToScalar(m.matrix().getDouble(0, 1)));
37 flattened->set(2, SkDoubleToScalar(m.matrix().getDouble(0, 3)));
38 flattened->set(3, SkDoubleToScalar(m.matrix().getDouble(1, 0)));
39 flattened->set(4, SkDoubleToScalar(m.matrix().getDouble(1, 1)));
40 flattened->set(5, SkDoubleToScalar(m.matrix().getDouble(1, 3)));
41 flattened->set(6, SkDoubleToScalar(m.matrix().getDouble(3, 0)));
42 flattened->set(7, SkDoubleToScalar(m.matrix().getDouble(3, 1)));
43 flattened->set(8, SkDoubleToScalar(m.matrix().getDouble(3, 3)));
46 bool IsScaleAndTranslate(const SkMatrix& matrix) {
47 return SkScalarNearlyZero(matrix[SkMatrix::kMSkewX]) &&
48 SkScalarNearlyZero(matrix[SkMatrix::kMSkewY]) &&
49 SkScalarNearlyZero(matrix[SkMatrix::kMPersp0]) &&
50 SkScalarNearlyZero(matrix[SkMatrix::kMPersp1]) &&
51 SkScalarNearlyZero(matrix[SkMatrix::kMPersp2] - 1.0f);
54 } // anonymous namespace
56 scoped_ptr<SoftwareRenderer> SoftwareRenderer::Create(
57 RendererClient* client,
58 OutputSurface* output_surface,
59 ResourceProvider* resource_provider) {
60 return make_scoped_ptr(
61 new SoftwareRenderer(client, output_surface, resource_provider));
64 SoftwareRenderer::SoftwareRenderer(RendererClient* client,
65 OutputSurface* output_surface,
66 ResourceProvider* resource_provider)
67 : DirectRenderer(client, resource_provider),
68 visible_(true),
69 is_scissor_enabled_(false),
70 is_viewport_changed_(true),
71 output_surface_(output_surface),
72 output_device_(output_surface->software_device()),
73 current_canvas_(NULL) {
74 capabilities_.max_texture_size = resource_provider_->max_texture_size();
75 capabilities_.best_texture_format = resource_provider_->best_texture_format();
76 capabilities_.using_set_visibility = true;
77 // The updater can access bitmaps while the SoftwareRenderer is using them.
78 capabilities_.allow_partial_texture_updates = true;
79 capabilities_.using_partial_swap = true;
80 if (Settings().compositor_frame_message && client_->HasImplThread())
81 capabilities_.using_swap_complete_callback = true;
82 compositor_frame_.software_frame_data.reset(new SoftwareFrameData());
85 SoftwareRenderer::~SoftwareRenderer() {}
87 const RendererCapabilities& SoftwareRenderer::Capabilities() const {
88 return capabilities_;
91 void SoftwareRenderer::ViewportChanged() {
92 is_viewport_changed_ = true;
95 void SoftwareRenderer::BeginDrawingFrame(DrawingFrame* frame) {
96 TRACE_EVENT0("cc", "SoftwareRenderer::BeginDrawingFrame");
97 if (is_viewport_changed_) {
98 is_viewport_changed_ = false;
99 output_device_->Resize(ViewportSize());
101 root_canvas_ = output_device_->BeginPaint(
102 gfx::ToEnclosingRect(frame->root_damage_rect));
105 void SoftwareRenderer::FinishDrawingFrame(DrawingFrame* frame) {
106 TRACE_EVENT0("cc", "SoftwareRenderer::FinishDrawingFrame");
107 current_framebuffer_lock_.reset();
108 current_canvas_ = NULL;
109 root_canvas_ = NULL;
110 if (Settings().compositor_frame_message) {
111 compositor_frame_.metadata = client_->MakeCompositorFrameMetadata();
112 output_device_->EndPaint(compositor_frame_.software_frame_data.get());
113 } else {
114 output_device_->EndPaint(NULL);
118 bool SoftwareRenderer::SwapBuffers(const LatencyInfo& latency_info) {
119 if (Settings().compositor_frame_message)
120 output_surface_->SendFrameToParentCompositor(&compositor_frame_);
121 return true;
124 void SoftwareRenderer::ReceiveCompositorFrameAck(
125 const CompositorFrameAck& ack) {
126 output_device_->ReclaimDIB(ack.last_dib_id);
129 bool SoftwareRenderer::FlippedFramebuffer() const {
130 return false;
133 void SoftwareRenderer::EnsureScissorTestEnabled() {
134 is_scissor_enabled_ = true;
135 SetClipRect(scissor_rect_);
138 void SoftwareRenderer::EnsureScissorTestDisabled() {
139 // There is no explicit notion of enabling/disabling scissoring in software
140 // rendering, but the underlying effect we want is to clear any existing
141 // clipRect on the current SkCanvas. This is done by setting clipRect to
142 // the viewport's dimensions.
143 is_scissor_enabled_ = false;
144 SkDevice* device = current_canvas_->getDevice();
145 SetClipRect(gfx::Rect(device->width(), device->height()));
148 void SoftwareRenderer::Finish() {}
150 void SoftwareRenderer::BindFramebufferToOutputSurface(DrawingFrame* frame) {
151 current_framebuffer_lock_.reset();
152 current_canvas_ = root_canvas_;
155 bool SoftwareRenderer::BindFramebufferToTexture(
156 DrawingFrame* frame,
157 const ScopedResource* texture,
158 gfx::Rect framebuffer_rect) {
159 current_framebuffer_lock_ = make_scoped_ptr(
160 new ResourceProvider::ScopedWriteLockSoftware(
161 resource_provider_, texture->id()));
162 current_canvas_ = current_framebuffer_lock_->sk_canvas();
163 InitializeMatrices(frame, framebuffer_rect, false);
164 SetDrawViewportSize(framebuffer_rect.size());
166 return true;
169 void SoftwareRenderer::SetScissorTestRect(gfx::Rect scissor_rect) {
170 is_scissor_enabled_ = true;
171 scissor_rect_ = scissor_rect;
172 SetClipRect(scissor_rect);
175 void SoftwareRenderer::SetClipRect(gfx::Rect rect) {
176 // Skia applies the current matrix to clip rects so we reset it temporary.
177 SkMatrix current_matrix = current_canvas_->getTotalMatrix();
178 current_canvas_->resetMatrix();
179 current_canvas_->clipRect(gfx::RectToSkRect(rect), SkRegion::kReplace_Op);
180 current_canvas_->setMatrix(current_matrix);
183 void SoftwareRenderer::ClearCanvas(SkColor color) {
184 // SkCanvas::clear doesn't respect the current clipping region
185 // so we SkCanvas::drawColor instead if scissoring is active.
186 if (is_scissor_enabled_)
187 current_canvas_->drawColor(color, SkXfermode::kSrc_Mode);
188 else
189 current_canvas_->clear(color);
192 void SoftwareRenderer::ClearFramebuffer(DrawingFrame* frame) {
193 if (frame->current_render_pass->has_transparent_background) {
194 ClearCanvas(SkColorSetARGB(0, 0, 0, 0));
195 } else {
196 #ifndef NDEBUG
197 // On DEBUG builds, opaque render passes are cleared to blue
198 // to easily see regions that were not drawn on the screen.
199 ClearCanvas(SkColorSetARGB(255, 0, 0, 255));
200 #endif
204 void SoftwareRenderer::SetDrawViewportSize(gfx::Size viewport_size) {}
206 bool SoftwareRenderer::IsSoftwareResource(
207 ResourceProvider::ResourceId resource_id) const {
208 switch (resource_provider_->GetResourceType(resource_id)) {
209 case ResourceProvider::GLTexture:
210 return false;
211 case ResourceProvider::Bitmap:
212 return true;
215 LOG(FATAL) << "Invalid resource type.";
216 return false;
219 void SoftwareRenderer::DoDrawQuad(DrawingFrame* frame, const DrawQuad* quad) {
220 TRACE_EVENT0("cc", "SoftwareRenderer::DoDrawQuad");
221 gfx::Transform quad_rect_matrix;
222 QuadRectTransform(&quad_rect_matrix, quad->quadTransform(), quad->rect);
223 gfx::Transform contents_device_transform =
224 frame->window_matrix * frame->projection_matrix * quad_rect_matrix;
225 contents_device_transform.FlattenTo2d();
226 SkMatrix sk_device_matrix;
227 ToSkMatrix(&sk_device_matrix, contents_device_transform);
228 current_canvas_->setMatrix(sk_device_matrix);
230 current_paint_.reset();
231 if (!IsScaleAndTranslate(sk_device_matrix)) {
232 current_paint_.setAntiAlias(true);
233 current_paint_.setFilterBitmap(true);
236 if (quad->ShouldDrawWithBlending()) {
237 current_paint_.setAlpha(quad->opacity() * 255);
238 current_paint_.setXfermodeMode(SkXfermode::kSrcOver_Mode);
239 } else {
240 current_paint_.setXfermodeMode(SkXfermode::kSrc_Mode);
243 switch (quad->material) {
244 case DrawQuad::DEBUG_BORDER:
245 DrawDebugBorderQuad(frame, DebugBorderDrawQuad::MaterialCast(quad));
246 break;
247 case DrawQuad::SOLID_COLOR:
248 DrawSolidColorQuad(frame, SolidColorDrawQuad::MaterialCast(quad));
249 break;
250 case DrawQuad::TEXTURE_CONTENT:
251 DrawTextureQuad(frame, TextureDrawQuad::MaterialCast(quad));
252 break;
253 case DrawQuad::TILED_CONTENT:
254 DrawTileQuad(frame, TileDrawQuad::MaterialCast(quad));
255 break;
256 case DrawQuad::RENDER_PASS:
257 DrawRenderPassQuad(frame, RenderPassDrawQuad::MaterialCast(quad));
258 break;
259 default:
260 DrawUnsupportedQuad(frame, quad);
261 break;
264 current_canvas_->resetMatrix();
267 void SoftwareRenderer::DrawDebugBorderQuad(const DrawingFrame* frame,
268 const DebugBorderDrawQuad* quad) {
269 // We need to apply the matrix manually to have pixel-sized stroke width.
270 SkPoint vertices[4];
271 gfx::RectFToSkRect(QuadVertexRect()).toQuad(vertices);
272 SkPoint transformed_vertices[4];
273 current_canvas_->getTotalMatrix().mapPoints(transformed_vertices,
274 vertices,
276 current_canvas_->resetMatrix();
278 current_paint_.setColor(quad->color);
279 current_paint_.setAlpha(quad->opacity() * SkColorGetA(quad->color));
280 current_paint_.setStyle(SkPaint::kStroke_Style);
281 current_paint_.setStrokeWidth(quad->width);
282 current_canvas_->drawPoints(SkCanvas::kPolygon_PointMode,
283 4, transformed_vertices, current_paint_);
286 void SoftwareRenderer::DrawSolidColorQuad(const DrawingFrame* frame,
287 const SolidColorDrawQuad* quad) {
288 current_paint_.setColor(quad->color);
289 current_paint_.setAlpha(quad->opacity() * SkColorGetA(quad->color));
290 current_canvas_->drawRect(gfx::RectFToSkRect(QuadVertexRect()),
291 current_paint_);
294 void SoftwareRenderer::DrawTextureQuad(const DrawingFrame* frame,
295 const TextureDrawQuad* quad) {
296 if (!IsSoftwareResource(quad->resource_id)) {
297 DrawUnsupportedQuad(frame, quad);
298 return;
301 // FIXME: Add support for non-premultiplied alpha.
302 ResourceProvider::ScopedReadLockSoftware lock(resource_provider_,
303 quad->resource_id);
304 const SkBitmap* bitmap = lock.sk_bitmap();
305 gfx::RectF uv_rect = gfx::ScaleRect(gfx::BoundingRect(quad->uv_top_left,
306 quad->uv_bottom_right),
307 bitmap->width(),
308 bitmap->height());
309 SkRect sk_uv_rect = gfx::RectFToSkRect(uv_rect);
310 if (quad->flipped)
311 current_canvas_->scale(1, -1);
312 current_canvas_->drawBitmapRectToRect(*bitmap, &sk_uv_rect,
313 gfx::RectFToSkRect(QuadVertexRect()),
314 &current_paint_);
317 void SoftwareRenderer::DrawTileQuad(const DrawingFrame* frame,
318 const TileDrawQuad* quad) {
319 DCHECK(IsSoftwareResource(quad->resource_id));
320 ResourceProvider::ScopedReadLockSoftware lock(resource_provider_,
321 quad->resource_id);
323 SkRect uv_rect = gfx::RectFToSkRect(quad->tex_coord_rect);
324 current_paint_.setFilterBitmap(true);
325 current_canvas_->drawBitmapRectToRect(*lock.sk_bitmap(), &uv_rect,
326 gfx::RectFToSkRect(QuadVertexRect()),
327 &current_paint_);
330 void SoftwareRenderer::DrawRenderPassQuad(const DrawingFrame* frame,
331 const RenderPassDrawQuad* quad) {
332 CachedResource* content_texture =
333 render_pass_textures_.get(quad->render_pass_id);
334 if (!content_texture || !content_texture->id())
335 return;
337 DCHECK(IsSoftwareResource(content_texture->id()));
338 ResourceProvider::ScopedReadLockSoftware lock(resource_provider_,
339 content_texture->id());
341 SkRect dest_rect = gfx::RectFToSkRect(QuadVertexRect());
342 SkRect content_rect = SkRect::MakeWH(quad->rect.width(), quad->rect.height());
344 SkMatrix content_mat;
345 content_mat.setRectToRect(content_rect, dest_rect,
346 SkMatrix::kFill_ScaleToFit);
348 const SkBitmap* content = lock.sk_bitmap();
349 skia::RefPtr<SkShader> shader = skia::AdoptRef(
350 SkShader::CreateBitmapShader(*content,
351 SkShader::kClamp_TileMode,
352 SkShader::kClamp_TileMode));
353 shader->setLocalMatrix(content_mat);
354 current_paint_.setShader(shader.get());
356 SkImageFilter* filter = quad->filter.get();
357 if (filter)
358 current_paint_.setImageFilter(filter);
360 if (quad->mask_resource_id) {
361 ResourceProvider::ScopedReadLockSoftware mask_lock(resource_provider_,
362 quad->mask_resource_id);
364 const SkBitmap* mask = mask_lock.sk_bitmap();
366 SkRect mask_rect = SkRect::MakeXYWH(
367 quad->mask_uv_rect.x() * mask->width(),
368 quad->mask_uv_rect.y() * mask->height(),
369 quad->mask_uv_rect.width() * mask->width(),
370 quad->mask_uv_rect.height() * mask->height());
372 SkMatrix mask_mat;
373 mask_mat.setRectToRect(mask_rect, dest_rect, SkMatrix::kFill_ScaleToFit);
375 skia::RefPtr<SkShader> mask_shader = skia::AdoptRef(
376 SkShader::CreateBitmapShader(*mask,
377 SkShader::kClamp_TileMode,
378 SkShader::kClamp_TileMode));
379 mask_shader->setLocalMatrix(mask_mat);
381 SkPaint mask_paint;
382 mask_paint.setShader(mask_shader.get());
384 skia::RefPtr<SkLayerRasterizer> mask_rasterizer =
385 skia::AdoptRef(new SkLayerRasterizer);
386 mask_rasterizer->addLayer(mask_paint);
388 current_paint_.setRasterizer(mask_rasterizer.get());
389 current_canvas_->drawRect(dest_rect, current_paint_);
390 } else {
391 // FIXME: Apply background filters and blend with content
392 current_canvas_->drawRect(dest_rect, current_paint_);
396 void SoftwareRenderer::DrawUnsupportedQuad(const DrawingFrame* frame,
397 const DrawQuad* quad) {
398 #ifdef NDEBUG
399 current_paint_.setColor(SK_ColorWHITE);
400 #else
401 current_paint_.setColor(SK_ColorMAGENTA);
402 #endif
403 current_paint_.setAlpha(quad->opacity() * 255);
404 current_canvas_->drawRect(gfx::RectFToSkRect(QuadVertexRect()),
405 current_paint_);
408 void SoftwareRenderer::CopyCurrentRenderPassToBitmap(DrawingFrame* frame,
409 SkBitmap* bitmap) {
410 gfx::Size render_pass_size = frame->current_render_pass->output_rect.size();
411 bitmap->setConfig(SkBitmap::kARGB_8888_Config,
412 render_pass_size.width(),
413 render_pass_size.height());
414 current_canvas_->readPixels(bitmap, 0, 0);
417 void SoftwareRenderer::GetFramebufferPixels(void* pixels, gfx::Rect rect) {
418 TRACE_EVENT0("cc", "SoftwareRenderer::GetFramebufferPixels");
419 SkBitmap subset_bitmap;
420 output_device_->CopyToBitmap(rect, &subset_bitmap);
421 subset_bitmap.copyPixelsTo(pixels,
422 4 * rect.width() * rect.height(),
423 4 * rect.width());
426 void SoftwareRenderer::SetVisible(bool visible) {
427 if (visible_ == visible)
428 return;
429 visible_ = visible;
432 } // namespace cc