Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "android_webview/browser/in_process_view_renderer.h" | 5 #include "android_webview/browser/in_process_view_renderer.h" |
| 6 | 6 |
| 7 #include <android/bitmap.h> | 7 #include <android/bitmap.h> |
| 8 | 8 |
| 9 #include "android_webview/browser/aw_gl_surface.h" | 9 #include "android_webview/browser/aw_gl_surface.h" |
| 10 #include "android_webview/browser/scoped_app_gl_state_restore.h" | 10 #include "android_webview/browser/scoped_app_gl_state_restore.h" |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 103 } | 103 } |
| 104 | 104 |
| 105 return succeeded; | 105 return succeeded; |
| 106 } | 106 } |
| 107 | 107 |
| 108 bool RenderPictureToCanvas(SkPicture* picture, SkCanvas* canvas) { | 108 bool RenderPictureToCanvas(SkPicture* picture, SkCanvas* canvas) { |
| 109 canvas->drawPicture(*picture); | 109 canvas->drawPicture(*picture); |
| 110 return true; | 110 return true; |
| 111 } | 111 } |
| 112 | 112 |
| 113 class ScopedPixelAccess { | |
| 114 public: | |
| 115 ScopedPixelAccess(JNIEnv* env, jobject java_canvas) { | |
| 116 AwDrawSWFunctionTable* sw_functions = | |
| 117 BrowserViewRenderer::GetAwDrawSWFunctionTable(); | |
| 118 pixels_ = sw_functions ? | |
| 119 sw_functions->access_pixels(env, java_canvas) : NULL; | |
| 120 } | |
| 121 ~ScopedPixelAccess() { | |
| 122 if (pixels_) | |
| 123 BrowserViewRenderer::GetAwDrawSWFunctionTable()->release_pixels(pixels_); | |
| 124 } | |
| 125 AwPixelInfo* pixels() { return pixels_; } | |
| 126 | |
| 127 private: | |
|
sgurun-gerrit only
2013/08/10 01:04:47
Need DISALLOW_COPY_AND_ASSIGN
joth
2013/08/10 01:22:32
Done.
| |
| 128 AwPixelInfo* pixels_; | |
| 129 }; | |
| 130 | |
| 113 bool HardwareEnabled() { | 131 bool HardwareEnabled() { |
| 114 static bool g_hw_enabled = !CommandLine::ForCurrentProcess()->HasSwitch( | 132 static bool g_hw_enabled = !CommandLine::ForCurrentProcess()->HasSwitch( |
| 115 switches::kDisableWebViewGLMode); | 133 switches::kDisableWebViewGLMode); |
| 116 return g_hw_enabled; | 134 return g_hw_enabled; |
| 117 } | 135 } |
| 118 | 136 |
| 119 // Provides software rendering functions from the Android glue layer. | 137 // Provides software rendering functions from the Android glue layer. |
| 120 // Allows preventing extra copies of data when rendering. | 138 // Allows preventing extra copies of data when rendering. |
| 121 AwDrawSWFunctionTable* g_sw_draw_functions = NULL; | 139 AwDrawSWFunctionTable* g_sw_draw_functions = NULL; |
| 122 | 140 |
| (...skipping 283 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 406 // static | 424 // static |
| 407 bool InProcessViewRenderer::RenderViaAuxilaryBitmapIfNeeded( | 425 bool InProcessViewRenderer::RenderViaAuxilaryBitmapIfNeeded( |
| 408 jobject java_canvas, | 426 jobject java_canvas, |
| 409 BrowserViewRenderer::JavaHelper* java_helper, | 427 BrowserViewRenderer::JavaHelper* java_helper, |
| 410 const gfx::Vector2d& scroll_correction, | 428 const gfx::Vector2d& scroll_correction, |
| 411 const gfx::Rect& clip, | 429 const gfx::Rect& clip, |
| 412 InProcessViewRenderer::RenderMethod render_source, | 430 InProcessViewRenderer::RenderMethod render_source, |
| 413 void* owner_key) { | 431 void* owner_key) { |
| 414 TRACE_EVENT0("android_webview", | 432 TRACE_EVENT0("android_webview", |
| 415 "InProcessViewRenderer::RenderViaAuxilaryBitmapIfNeeded"); | 433 "InProcessViewRenderer::RenderViaAuxilaryBitmapIfNeeded"); |
| 434 | |
| 416 JNIEnv* env = AttachCurrentThread(); | 435 JNIEnv* env = AttachCurrentThread(); |
| 436 ScopedPixelAccess auto_release_pixels(env, java_canvas); | |
| 437 AwPixelInfo* pixels = auto_release_pixels.pixels(); | |
| 438 SkMatrix matrix; | |
| 439 SkBitmap::Config config(SkBitmap::kNo_Config); | |
| 440 if (pixels) { | |
| 441 config = | |
|
sgurun-gerrit only
2013/08/10 01:04:47
feels like a switch would be more readable...
joth
2013/08/10 01:22:32
Done.
| |
| 442 pixels->config == AwConfig_ARGB_8888 ? SkBitmap::kARGB_8888_Config : | |
| 443 pixels->config == AwConfig_RGB_565 ? SkBitmap::kRGB_565_Config : | |
| 444 SkBitmap::kNo_Config; | |
| 417 | 445 |
| 418 AwDrawSWFunctionTable* sw_functions = GetAwDrawSWFunctionTable(); | 446 for (int i = 0; i < 9; i++) { |
| 419 AwPixelInfo* pixels = sw_functions ? | 447 matrix.set(i, pixels->matrix[i]); |
| 420 sw_functions->access_pixels(env, java_canvas) : NULL; | 448 } |
| 421 if (pixels == NULL) { | 449 // Workaround for http://crbug.com/271096: SW draw only supports |
| 450 // translate & scale transforms. | |
| 451 if (matrix.getType() & ~(SkMatrix::kTranslate_Mask | SkMatrix::kScale_Mask)) | |
| 452 config = SkBitmap::kNo_Config; | |
| 453 } | |
| 454 | |
| 455 if (config == SkBitmap::kNo_Config) { | |
| 422 // Render into an auxiliary bitmap if pixel info is not available. | 456 // Render into an auxiliary bitmap if pixel info is not available. |
| 423 ScopedJavaLocalRef<jobject> jcanvas(env, java_canvas); | 457 ScopedJavaLocalRef<jobject> jcanvas(env, java_canvas); |
| 424 TRACE_EVENT0("android_webview", "RenderToAuxBitmap"); | 458 TRACE_EVENT0("android_webview", "RenderToAuxBitmap"); |
| 425 ScopedJavaLocalRef<jobject> jbitmap(java_helper->CreateBitmap( | 459 ScopedJavaLocalRef<jobject> jbitmap(java_helper->CreateBitmap( |
| 426 env, clip.width(), clip.height(), jcanvas, owner_key)); | 460 env, clip.width(), clip.height(), jcanvas, owner_key)); |
| 427 if (!jbitmap.obj()) { | 461 if (!jbitmap.obj()) { |
| 428 TRACE_EVENT_INSTANT0("android_webview", | 462 TRACE_EVENT_INSTANT0("android_webview", |
| 429 "EarlyOut_BitmapAllocFail", | 463 "EarlyOut_BitmapAllocFail", |
| 430 TRACE_EVENT_SCOPE_THREAD); | 464 TRACE_EVENT_SCOPE_THREAD); |
| 431 return false; | 465 return false; |
| 432 } | 466 } |
| 433 | 467 |
| 434 if (!RasterizeIntoBitmap(env, jbitmap, | 468 if (!RasterizeIntoBitmap(env, jbitmap, |
| 435 clip.x() - scroll_correction.x(), | 469 clip.x() - scroll_correction.x(), |
| 436 clip.y() - scroll_correction.y(), | 470 clip.y() - scroll_correction.y(), |
| 437 render_source)) { | 471 render_source)) { |
| 438 TRACE_EVENT_INSTANT0("android_webview", | 472 TRACE_EVENT_INSTANT0("android_webview", |
| 439 "EarlyOut_RasterizeFail", | 473 "EarlyOut_RasterizeFail", |
| 440 TRACE_EVENT_SCOPE_THREAD); | 474 TRACE_EVENT_SCOPE_THREAD); |
| 441 return false; | 475 return false; |
| 442 } | 476 } |
| 443 | 477 |
| 444 java_helper->DrawBitmapIntoCanvas(env, jbitmap, jcanvas, | 478 java_helper->DrawBitmapIntoCanvas(env, jbitmap, jcanvas, |
| 445 clip.x(), clip.y()); | 479 clip.x(), clip.y()); |
| 446 return true; | 480 return true; |
| 447 } | 481 } |
| 448 | 482 |
| 449 // Draw in a SkCanvas built over the pixel information. | 483 // Draw in a SkCanvas built over the pixel information. |
| 450 bool succeeded = false; | 484 SkBitmap bitmap; |
| 451 { | 485 bitmap.setConfig(config, |
| 452 SkBitmap bitmap; | 486 pixels->width, |
| 453 bitmap.setConfig(static_cast<SkBitmap::Config>(pixels->config), | 487 pixels->height, |
| 454 pixels->width, | 488 pixels->row_bytes); |
| 455 pixels->height, | 489 bitmap.setPixels(pixels->pixels); |
| 456 pixels->row_bytes); | 490 SkDevice device(bitmap); |
| 457 bitmap.setPixels(pixels->pixels); | 491 SkCanvas canvas(&device); |
| 458 SkDevice device(bitmap); | 492 canvas.setMatrix(matrix); |
| 459 SkCanvas canvas(&device); | |
| 460 SkMatrix matrix; | |
| 461 for (int i = 0; i < 9; i++) | |
| 462 matrix.set(i, pixels->matrix[i]); | |
| 463 canvas.setMatrix(matrix); | |
| 464 | 493 |
| 465 if (pixels->clip_region_size) { | 494 if (pixels->clip_rect_count) { |
| 466 SkRegion clip_region; | 495 SkRegion clip; |
| 467 size_t bytes_read = clip_region.readFromMemory(pixels->clip_region); | 496 for (int i = 0; i < pixels->clip_rect_count; ++i) { |
| 468 DCHECK_EQ(pixels->clip_region_size, bytes_read); | 497 clip.op(SkIRect::MakeXYWH(pixels->clip_rects[i + 0], |
| 469 canvas.setClipRegion(clip_region); | 498 pixels->clip_rects[i + 1], |
| 470 } else { | 499 pixels->clip_rects[i + 2], |
| 471 canvas.clipRect(gfx::RectToSkRect(clip)); | 500 pixels->clip_rects[i + 3]), |
| 501 SkRegion::kUnion_Op); | |
| 472 } | 502 } |
| 473 canvas.translate(scroll_correction.x(), | 503 canvas.setClipRegion(clip); |
| 474 scroll_correction.y()); | 504 } else if (pixels->clip_region && pixels->clip_region_size) { |
| 505 SkRegion clip_region; | |
| 506 size_t bytes_read = clip_region.readFromMemory(pixels->clip_region); | |
| 507 DCHECK_EQ(pixels->clip_region_size, bytes_read); | |
| 508 canvas.setClipRegion(clip_region); | |
| 509 } | |
| 510 canvas.translate(scroll_correction.x(), | |
| 511 scroll_correction.y()); | |
| 475 | 512 |
| 476 succeeded = render_source.Run(&canvas); | 513 return render_source.Run(&canvas); |
| 477 } | |
| 478 | |
| 479 sw_functions->release_pixels(pixels); | |
| 480 return succeeded; | |
| 481 } | 514 } |
| 482 | 515 |
| 483 skia::RefPtr<SkPicture> InProcessViewRenderer::CapturePicture(int width, | 516 skia::RefPtr<SkPicture> InProcessViewRenderer::CapturePicture(int width, |
| 484 int height) { | 517 int height) { |
| 518 TRACE_EVENT0("android_webview", "InProcessViewRenderer::CapturePicture"); | |
| 519 | |
| 485 // Return empty Picture objects for empty SkPictures. | 520 // Return empty Picture objects for empty SkPictures. |
| 486 skia::RefPtr<SkPicture> picture = skia::AdoptRef(new SkPicture); | 521 skia::RefPtr<SkPicture> picture = skia::AdoptRef(new SkPicture); |
| 487 if (width <= 0 || height <= 0) { | 522 if (width <= 0 || height <= 0) { |
| 488 return picture; | 523 return picture; |
| 489 } | 524 } |
| 490 | 525 |
| 491 // Reset scroll back to the origin, will go back to the old | 526 // Reset scroll back to the origin, will go back to the old |
| 492 // value when scroll_reset is out of scope. | 527 // value when scroll_reset is out of scope. |
| 493 base::AutoReset<gfx::Vector2dF> scroll_reset(&scroll_offset_css_, | 528 base::AutoReset<gfx::Vector2dF> scroll_reset(&scroll_offset_css_, |
| 494 gfx::Vector2d()); | 529 gfx::Vector2d()); |
| (...skipping 288 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 783 base::StringAppendF(&str, | 818 base::StringAppendF(&str, |
| 784 "surface width height: [%d %d] ", | 819 "surface width height: [%d %d] ", |
| 785 draw_info->width, | 820 draw_info->width, |
| 786 draw_info->height); | 821 draw_info->height); |
| 787 base::StringAppendF(&str, "is_layer: %d ", draw_info->is_layer); | 822 base::StringAppendF(&str, "is_layer: %d ", draw_info->is_layer); |
| 788 } | 823 } |
| 789 return str; | 824 return str; |
| 790 } | 825 } |
| 791 | 826 |
| 792 } // namespace android_webview | 827 } // namespace android_webview |
| OLD | NEW |