OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "cc/output/software_renderer.h" | |
6 | |
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/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_draw_filter.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" | |
34 | |
35 namespace cc { | |
36 namespace { | |
37 | |
38 static inline bool IsScalarNearlyInteger(SkScalar scalar) { | |
39 return SkScalarNearlyZero(scalar - SkScalarRoundToScalar(scalar)); | |
40 } | |
41 | |
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); | |
50 } | |
51 | |
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; | |
58 } | |
59 NOTREACHED(); | |
60 return SkShader::kClamp_TileMode; | |
61 } | |
62 | |
63 } // anonymous namespace | |
64 | |
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)); | |
72 } | |
73 | |
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(); | |
87 } | |
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; | |
91 | |
92 capabilities_.using_shared_memory_resources = true; | |
93 | |
94 capabilities_.allow_rasterize_on_demand = true; | |
95 } | |
96 | |
97 SoftwareRenderer::~SoftwareRenderer() {} | |
98 | |
99 const RendererCapabilitiesImpl& SoftwareRenderer::Capabilities() const { | |
100 return capabilities_; | |
101 } | |
102 | |
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)); | |
107 } | |
108 | |
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; | |
115 | |
116 current_frame_data_.reset(new SoftwareFrameData); | |
117 output_device_->EndPaint(current_frame_data_.get()); | |
118 } | |
119 | |
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); | |
126 } | |
127 | |
128 void SoftwareRenderer::ReceiveSwapBuffersAck(const CompositorFrameAck& ack) { | |
129 output_device_->ReclaimSoftwareFrame(ack.last_software_frame_id); | |
130 } | |
131 | |
132 bool SoftwareRenderer::FlippedFramebuffer(const DrawingFrame* frame) const { | |
133 return false; | |
134 } | |
135 | |
136 void SoftwareRenderer::EnsureScissorTestEnabled() { | |
137 is_scissor_enabled_ = true; | |
138 SetClipRect(scissor_rect_); | |
139 } | |
140 | |
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())); | |
149 } | |
150 | |
151 void SoftwareRenderer::Finish() {} | |
152 | |
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_; | |
158 } | |
159 | |
160 bool SoftwareRenderer::BindFramebufferToTexture( | |
161 DrawingFrame* frame, | |
162 const ScopedResource* texture, | |
163 const gfx::Rect& target_rect) { | |
164 DCHECK(texture->id()); | |
165 | |
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; | |
176 } | |
177 | |
178 void SoftwareRenderer::SetScissorTestRect(const gfx::Rect& scissor_rect) { | |
179 is_scissor_enabled_ = true; | |
180 scissor_rect_ = scissor_rect; | |
181 SetClipRect(scissor_rect); | |
182 } | |
183 | |
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); | |
190 } | |
191 | |
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); | |
199 } | |
200 | |
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 | |
210 } | |
211 } | |
212 | |
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; | |
229 } | |
230 } | |
231 | |
232 void SoftwareRenderer::SetDrawViewport( | |
233 const gfx::Rect& window_space_viewport) {} | |
234 | |
235 bool SoftwareRenderer::IsSoftwareResource( | |
236 ResourceProvider::ResourceId resource_id) const { | |
237 switch (resource_provider_->GetResourceType(resource_id)) { | |
238 case ResourceProvider::RESOURCE_TYPE_GL_TEXTURE: | |
239 return false; | |
240 case ResourceProvider::RESOURCE_TYPE_BITMAP: | |
241 return true; | |
242 case ResourceProvider::RESOURCE_TYPE_INVALID: | |
243 break; | |
244 } | |
245 | |
246 LOG(FATAL) << "Invalid resource type."; | |
247 return false; | |
248 } | |
249 | |
250 void SoftwareRenderer::DoDrawQuad(DrawingFrame* frame, | |
251 const DrawQuad* quad, | |
252 const gfx::QuadF* draw_region) { | |
253 if (draw_region) { | |
254 current_canvas_->save(); | |
255 } | |
256 | |
257 TRACE_EVENT0("cc", "SoftwareRenderer::DoDrawQuad"); | |
258 gfx::Transform quad_rect_matrix; | |
259 QuadRectTransform(&quad_rect_matrix, quad->quadTransform(), quad->rect); | |
260 gfx::Transform contents_device_transform = | |
261 frame->window_matrix * frame->projection_matrix * quad_rect_matrix; | |
262 contents_device_transform.FlattenTo2d(); | |
263 SkMatrix sk_device_matrix; | |
264 gfx::TransformToFlattenedSkMatrix(contents_device_transform, | |
265 &sk_device_matrix); | |
266 current_canvas_->setMatrix(sk_device_matrix); | |
267 | |
268 current_paint_.reset(); | |
269 if (settings_->force_antialiasing || | |
270 !IsScaleAndIntegerTranslate(sk_device_matrix)) { | |
271 // TODO(danakj): Until we can enable AA only on exterior edges of the | |
272 // layer, disable AA if any interior edges are present. crbug.com/248175 | |
273 bool all_four_edges_are_exterior = quad->IsTopEdge() && | |
274 quad->IsLeftEdge() && | |
275 quad->IsBottomEdge() && | |
276 quad->IsRightEdge(); | |
277 if (settings_->allow_antialiasing && | |
278 (settings_->force_antialiasing || all_four_edges_are_exterior)) | |
279 current_paint_.setAntiAlias(true); | |
280 current_paint_.setFilterQuality(kLow_SkFilterQuality); | |
281 } | |
282 | |
283 if (quad->ShouldDrawWithBlending() || | |
284 quad->shared_quad_state->blend_mode != SkXfermode::kSrcOver_Mode) { | |
285 current_paint_.setAlpha(quad->opacity() * 255); | |
286 current_paint_.setXfermodeMode(quad->shared_quad_state->blend_mode); | |
287 } else { | |
288 current_paint_.setXfermodeMode(SkXfermode::kSrc_Mode); | |
289 } | |
290 | |
291 if (draw_region) { | |
292 gfx::QuadF local_draw_region(*draw_region); | |
293 SkPath draw_region_clip_path; | |
294 local_draw_region -= | |
295 gfx::Vector2dF(quad->visible_rect.x(), quad->visible_rect.y()); | |
296 local_draw_region.Scale(1.0f / quad->visible_rect.width(), | |
297 1.0f / quad->visible_rect.height()); | |
298 local_draw_region -= gfx::Vector2dF(0.5f, 0.5f); | |
299 | |
300 SkPoint clip_points[4]; | |
301 QuadFToSkPoints(local_draw_region, clip_points); | |
302 draw_region_clip_path.addPoly(clip_points, 4, true); | |
303 | |
304 current_canvas_->clipPath(draw_region_clip_path, SkRegion::kIntersect_Op, | |
305 false); | |
306 } | |
307 | |
308 switch (quad->material) { | |
309 case DrawQuad::CHECKERBOARD: | |
310 // TODO(enne) For now since checkerboards shouldn't be part of a 3D | |
311 // context, clipping regions aren't supported so we skip drawing them | |
312 // if this becomes the case. | |
313 if (!draw_region) { | |
314 DrawCheckerboardQuad(frame, CheckerboardDrawQuad::MaterialCast(quad)); | |
315 } | |
316 break; | |
317 case DrawQuad::DEBUG_BORDER: | |
318 DrawDebugBorderQuad(frame, DebugBorderDrawQuad::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 case DrawQuad::UNUSED_SPACE_FOR_PICTURE_CONTENT: | |
342 DrawUnsupportedQuad(frame, quad); | |
343 NOTREACHED(); | |
344 break; | |
345 } | |
346 | |
347 current_canvas_->resetMatrix(); | |
348 if (draw_region) { | |
349 current_canvas_->restore(); | |
350 } | |
351 } | |
352 | |
353 void SoftwareRenderer::DrawCheckerboardQuad(const DrawingFrame* frame, | |
354 const CheckerboardDrawQuad* quad) { | |
355 gfx::RectF visible_quad_vertex_rect = MathUtil::ScaleRectProportional( | |
356 QuadVertexRect(), quad->rect, quad->visible_rect); | |
357 current_paint_.setColor(quad->color); | |
358 current_paint_.setAlpha(quad->opacity()); | |
359 current_canvas_->drawRect(gfx::RectFToSkRect(visible_quad_vertex_rect), | |
360 current_paint_); | |
361 } | |
362 | |
363 void SoftwareRenderer::DrawDebugBorderQuad(const DrawingFrame* frame, | |
364 const DebugBorderDrawQuad* quad) { | |
365 // We need to apply the matrix manually to have pixel-sized stroke width. | |
366 SkPoint vertices[4]; | |
367 gfx::RectFToSkRect(QuadVertexRect()).toQuad(vertices); | |
368 SkPoint transformed_vertices[4]; | |
369 current_canvas_->getTotalMatrix().mapPoints(transformed_vertices, | |
370 vertices, | |
371 4); | |
372 current_canvas_->resetMatrix(); | |
373 | |
374 current_paint_.setColor(quad->color); | |
375 current_paint_.setAlpha(quad->opacity() * SkColorGetA(quad->color)); | |
376 current_paint_.setStyle(SkPaint::kStroke_Style); | |
377 current_paint_.setStrokeWidth(quad->width); | |
378 current_canvas_->drawPoints(SkCanvas::kPolygon_PointMode, | |
379 4, transformed_vertices, current_paint_); | |
380 } | |
381 | |
382 void SoftwareRenderer::DrawSolidColorQuad(const DrawingFrame* frame, | |
383 const SolidColorDrawQuad* quad) { | |
384 gfx::RectF visible_quad_vertex_rect = MathUtil::ScaleRectProportional( | |
385 QuadVertexRect(), quad->rect, quad->visible_rect); | |
386 current_paint_.setColor(quad->color); | |
387 current_paint_.setAlpha(quad->opacity() * SkColorGetA(quad->color)); | |
388 current_canvas_->drawRect(gfx::RectFToSkRect(visible_quad_vertex_rect), | |
389 current_paint_); | |
390 } | |
391 | |
392 void SoftwareRenderer::DrawTextureQuad(const DrawingFrame* frame, | |
393 const TextureDrawQuad* quad) { | |
394 if (!IsSoftwareResource(quad->resource_id)) { | |
395 DrawUnsupportedQuad(frame, quad); | |
396 return; | |
397 } | |
398 | |
399 // TODO(skaslev): Add support for non-premultiplied alpha. | |
400 ResourceProvider::ScopedReadLockSoftware lock(resource_provider_, | |
401 quad->resource_id); | |
402 if (!lock.valid()) | |
403 return; | |
404 const SkBitmap* bitmap = lock.sk_bitmap(); | |
405 gfx::RectF uv_rect = gfx::ScaleRect(gfx::BoundingRect(quad->uv_top_left, | |
406 quad->uv_bottom_right), | |
407 bitmap->width(), | |
408 bitmap->height()); | |
409 gfx::RectF visible_uv_rect = | |
410 MathUtil::ScaleRectProportional(uv_rect, quad->rect, quad->visible_rect); | |
411 SkRect sk_uv_rect = gfx::RectFToSkRect(visible_uv_rect); | |
412 gfx::RectF visible_quad_vertex_rect = MathUtil::ScaleRectProportional( | |
413 QuadVertexRect(), quad->rect, quad->visible_rect); | |
414 SkRect quad_rect = gfx::RectFToSkRect(visible_quad_vertex_rect); | |
415 | |
416 if (quad->flipped) | |
417 current_canvas_->scale(1, -1); | |
418 | |
419 bool blend_background = quad->background_color != SK_ColorTRANSPARENT && | |
420 !bitmap->isOpaque(); | |
421 bool needs_layer = blend_background && (current_paint_.getAlpha() != 0xFF); | |
422 if (needs_layer) { | |
423 current_canvas_->saveLayerAlpha(&quad_rect, current_paint_.getAlpha()); | |
424 current_paint_.setAlpha(0xFF); | |
425 } | |
426 if (blend_background) { | |
427 SkPaint background_paint; | |
428 background_paint.setColor(quad->background_color); | |
429 current_canvas_->drawRect(quad_rect, background_paint); | |
430 } | |
431 SkShader::TileMode tile_mode = WrapModeToTileMode(lock.wrap_mode()); | |
432 if (tile_mode != SkShader::kClamp_TileMode) { | |
433 SkMatrix matrix; | |
434 matrix.setRectToRect(sk_uv_rect, quad_rect, SkMatrix::kFill_ScaleToFit); | |
435 skia::RefPtr<SkShader> shader = skia::AdoptRef( | |
436 SkShader::CreateBitmapShader(*bitmap, tile_mode, tile_mode, &matrix)); | |
437 SkPaint paint; | |
438 paint.setStyle(SkPaint::kFill_Style); | |
439 paint.setShader(shader.get()); | |
440 current_canvas_->drawRect(quad_rect, paint); | |
441 } else { | |
442 current_canvas_->drawBitmapRect(*bitmap, sk_uv_rect, quad_rect, | |
443 ¤t_paint_); | |
444 } | |
445 | |
446 if (needs_layer) | |
447 current_canvas_->restore(); | |
448 } | |
449 | |
450 void SoftwareRenderer::DrawTileQuad(const DrawingFrame* frame, | |
451 const TileDrawQuad* quad) { | |
452 // |resource_provider_| can be NULL in resourceless software draws, which | |
453 // should never produce tile quads in the first place. | |
454 DCHECK(resource_provider_); | |
455 DCHECK(IsSoftwareResource(quad->resource_id)); | |
456 | |
457 ResourceProvider::ScopedReadLockSoftware lock(resource_provider_, | |
458 quad->resource_id); | |
459 if (!lock.valid()) | |
460 return; | |
461 DCHECK_EQ(GL_CLAMP_TO_EDGE, lock.wrap_mode()); | |
462 | |
463 gfx::RectF visible_tex_coord_rect = MathUtil::ScaleRectProportional( | |
464 quad->tex_coord_rect, quad->rect, quad->visible_rect); | |
465 gfx::RectF visible_quad_vertex_rect = MathUtil::ScaleRectProportional( | |
466 QuadVertexRect(), quad->rect, quad->visible_rect); | |
467 | |
468 SkRect uv_rect = gfx::RectFToSkRect(visible_tex_coord_rect); | |
469 current_paint_.setFilterQuality( | |
470 quad->nearest_neighbor ? kNone_SkFilterQuality : kLow_SkFilterQuality); | |
471 current_canvas_->drawBitmapRect(*lock.sk_bitmap(), uv_rect, | |
472 gfx::RectFToSkRect(visible_quad_vertex_rect), | |
473 ¤t_paint_); | |
474 } | |
475 | |
476 void SoftwareRenderer::DrawRenderPassQuad(const DrawingFrame* frame, | |
477 const RenderPassDrawQuad* quad) { | |
478 ScopedResource* content_texture = | |
479 render_pass_textures_.get(quad->render_pass_id); | |
480 if (!content_texture || !content_texture->id()) | |
481 return; | |
482 | |
483 DCHECK(IsSoftwareResource(content_texture->id())); | |
484 ResourceProvider::ScopedReadLockSoftware lock(resource_provider_, | |
485 content_texture->id()); | |
486 if (!lock.valid()) | |
487 return; | |
488 SkShader::TileMode content_tile_mode = WrapModeToTileMode(lock.wrap_mode()); | |
489 | |
490 SkRect dest_rect = gfx::RectFToSkRect(QuadVertexRect()); | |
491 SkRect dest_visible_rect = gfx::RectFToSkRect(MathUtil::ScaleRectProportional( | |
492 QuadVertexRect(), quad->rect, quad->visible_rect)); | |
493 SkRect content_rect = SkRect::MakeWH(quad->rect.width(), quad->rect.height()); | |
494 | |
495 SkMatrix content_mat; | |
496 content_mat.setRectToRect(content_rect, dest_rect, | |
497 SkMatrix::kFill_ScaleToFit); | |
498 | |
499 const SkBitmap* content = lock.sk_bitmap(); | |
500 | |
501 SkBitmap filter_bitmap; | |
502 if (!quad->filters.IsEmpty()) { | |
503 skia::RefPtr<SkImageFilter> filter = RenderSurfaceFilters::BuildImageFilter( | |
504 quad->filters, content_texture->size()); | |
505 // TODO(ajuma): Apply the filter in the same pass as the content where | |
506 // possible (e.g. when there's no origin offset). See crbug.com/308201. | |
507 if (filter) { | |
508 SkImageInfo info = SkImageInfo::MakeN32Premul( | |
509 content_texture->size().width(), content_texture->size().height()); | |
510 if (filter_bitmap.tryAllocPixels(info)) { | |
511 SkCanvas canvas(filter_bitmap); | |
512 SkPaint paint; | |
513 paint.setImageFilter(filter.get()); | |
514 canvas.clear(SK_ColorTRANSPARENT); | |
515 canvas.translate(SkIntToScalar(-quad->rect.origin().x()), | |
516 SkIntToScalar(-quad->rect.origin().y())); | |
517 canvas.scale(quad->filters_scale.x(), quad->filters_scale.y()); | |
518 canvas.drawSprite(*content, 0, 0, &paint); | |
519 } | |
520 } | |
521 } | |
522 | |
523 skia::RefPtr<SkShader> shader; | |
524 if (filter_bitmap.isNull()) { | |
525 shader = skia::AdoptRef(SkShader::CreateBitmapShader( | |
526 *content, content_tile_mode, content_tile_mode, &content_mat)); | |
527 } else { | |
528 shader = skia::AdoptRef(SkShader::CreateBitmapShader( | |
529 filter_bitmap, content_tile_mode, content_tile_mode, &content_mat)); | |
530 } | |
531 current_paint_.setShader(shader.get()); | |
532 | |
533 if (quad->mask_resource_id) { | |
534 ResourceProvider::ScopedReadLockSoftware mask_lock(resource_provider_, | |
535 quad->mask_resource_id); | |
536 if (!lock.valid()) | |
537 return; | |
538 SkShader::TileMode mask_tile_mode = WrapModeToTileMode( | |
539 mask_lock.wrap_mode()); | |
540 | |
541 const SkBitmap* mask = mask_lock.sk_bitmap(); | |
542 | |
543 // Scale normalized uv rect into absolute texel coordinates. | |
544 SkRect mask_rect = | |
545 gfx::RectFToSkRect(gfx::ScaleRect(quad->MaskUVRect(), | |
546 quad->mask_texture_size.width(), | |
547 quad->mask_texture_size.height())); | |
548 | |
549 SkMatrix mask_mat; | |
550 mask_mat.setRectToRect(mask_rect, dest_rect, SkMatrix::kFill_ScaleToFit); | |
551 | |
552 skia::RefPtr<SkShader> mask_shader = | |
553 skia::AdoptRef(SkShader::CreateBitmapShader( | |
554 *mask, mask_tile_mode, mask_tile_mode, &mask_mat)); | |
555 | |
556 SkPaint mask_paint; | |
557 mask_paint.setShader(mask_shader.get()); | |
558 | |
559 SkLayerRasterizer::Builder builder; | |
560 builder.addLayer(mask_paint); | |
561 | |
562 skia::RefPtr<SkLayerRasterizer> mask_rasterizer = | |
563 skia::AdoptRef(builder.detachRasterizer()); | |
564 | |
565 current_paint_.setRasterizer(mask_rasterizer.get()); | |
566 current_canvas_->drawRect(dest_visible_rect, current_paint_); | |
567 } else { | |
568 // TODO(skaslev): Apply background filters | |
569 current_canvas_->drawRect(dest_visible_rect, current_paint_); | |
570 } | |
571 } | |
572 | |
573 void SoftwareRenderer::DrawUnsupportedQuad(const DrawingFrame* frame, | |
574 const DrawQuad* quad) { | |
575 #ifdef NDEBUG | |
576 current_paint_.setColor(SK_ColorWHITE); | |
577 #else | |
578 current_paint_.setColor(SK_ColorMAGENTA); | |
579 #endif | |
580 current_paint_.setAlpha(quad->opacity() * 255); | |
581 current_canvas_->drawRect(gfx::RectFToSkRect(QuadVertexRect()), | |
582 current_paint_); | |
583 } | |
584 | |
585 void SoftwareRenderer::CopyCurrentRenderPassToBitmap( | |
586 DrawingFrame* frame, | |
587 scoped_ptr<CopyOutputRequest> request) { | |
588 gfx::Rect copy_rect = frame->current_render_pass->output_rect; | |
589 if (request->has_area()) | |
590 copy_rect.Intersect(request->area()); | |
591 gfx::Rect window_copy_rect = MoveFromDrawToWindowSpace(frame, copy_rect); | |
592 | |
593 scoped_ptr<SkBitmap> bitmap(new SkBitmap); | |
594 bitmap->setInfo(SkImageInfo::MakeN32Premul(window_copy_rect.width(), | |
595 window_copy_rect.height())); | |
596 current_canvas_->readPixels( | |
597 bitmap.get(), window_copy_rect.x(), window_copy_rect.y()); | |
598 | |
599 request->SendBitmapResult(bitmap.Pass()); | |
600 } | |
601 | |
602 void SoftwareRenderer::DiscardBackbuffer() { | |
603 if (is_backbuffer_discarded_) | |
604 return; | |
605 | |
606 output_surface_->DiscardBackbuffer(); | |
607 | |
608 is_backbuffer_discarded_ = true; | |
609 | |
610 // Damage tracker needs a full reset every time framebuffer is discarded. | |
611 client_->SetFullRootLayerDamage(); | |
612 } | |
613 | |
614 void SoftwareRenderer::EnsureBackbuffer() { | |
615 if (!is_backbuffer_discarded_) | |
616 return; | |
617 | |
618 output_surface_->EnsureBackbuffer(); | |
619 is_backbuffer_discarded_ = false; | |
620 } | |
621 | |
622 void SoftwareRenderer::DidChangeVisibility() { | |
623 if (visible()) | |
624 EnsureBackbuffer(); | |
625 else | |
626 DiscardBackbuffer(); | |
627 } | |
628 | |
629 } // namespace cc | |
OLD | NEW |