PixelDumpSupportGtk.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * Copyright (C) 2009 Zan Dobersek <zandobersek@gmail.com>
  3. * Copyright (C) 2010 Igalia S.L.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
  15. * its contributors may be used to endorse or promote products derived
  16. * from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
  19. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  27. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #include "config.h"
  30. #include "DumpRenderTree.h"
  31. #include "GtkVersioning.h"
  32. #include "PixelDumpSupportCairo.h"
  33. #include "WebCoreSupport/DumpRenderTreeSupportGtk.h"
  34. #include <webkit/webkit.h>
  35. static void paintOverlay(cairo_surface_t* surface)
  36. {
  37. cairo_t* context = cairo_create(surface);
  38. // Paint a transparent black overlay from which the repainted rectangles are then cleared.
  39. // The alpha component of the overlay should have a value of 0.66, as on other ports.
  40. cairo_set_source_rgba(context, 0.0, 0.0, 0.0, 0.66);
  41. cairo_rectangle(context, 0, 0, cairo_image_surface_get_width(surface), cairo_image_surface_get_height(surface));
  42. cairo_fill(context);
  43. GSList* trackedRectsList = DumpRenderTreeSupportGtk::trackedRepaintRects(mainFrame);
  44. for (GSList* listElement = trackedRectsList; listElement; listElement = g_slist_next(listElement)) {
  45. GdkRectangle* rect = static_cast<GdkRectangle*>(listElement->data);
  46. cairo_set_operator(context, CAIRO_OPERATOR_CLEAR);
  47. cairo_rectangle(context, rect->x, rect->y, rect->width, rect->height);
  48. cairo_fill(context);
  49. }
  50. g_slist_free_full(trackedRectsList, g_free);
  51. cairo_destroy(context);
  52. }
  53. static void fillRepaintOverlayIntoContext(cairo_t* context, gint width, gint height)
  54. {
  55. cairo_surface_t* overlaySurface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height);
  56. paintOverlay(overlaySurface);
  57. cairo_set_source_surface(context, overlaySurface, 0, 0);
  58. cairo_rectangle(context, 0, 0, width, height);
  59. cairo_fill(context);
  60. cairo_surface_destroy(overlaySurface);
  61. }
  62. PassRefPtr<BitmapContext> createBitmapContextFromWebView(bool, bool, bool, bool drawSelectionRect)
  63. {
  64. WebKitWebView* view = webkit_web_frame_get_web_view(mainFrame);
  65. GtkWidget* viewContainer = gtk_widget_get_parent(GTK_WIDGET(view));
  66. gint width, height;
  67. #ifdef GTK_API_VERSION_2
  68. GdkPixmap* pixmap = gtk_widget_get_snapshot(viewContainer, 0);
  69. gdk_pixmap_get_size(pixmap, &width, &height);
  70. #else
  71. width = gtk_widget_get_allocated_width(viewContainer);
  72. height = gtk_widget_get_allocated_height(viewContainer);
  73. #endif
  74. while (gtk_events_pending())
  75. gtk_main_iteration();
  76. cairo_surface_t* imageSurface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height);
  77. cairo_t* context = cairo_create(imageSurface);
  78. #ifdef GTK_API_VERSION_2
  79. gdk_cairo_set_source_pixmap(context, pixmap, 0, 0);
  80. cairo_paint(context);
  81. g_object_unref(pixmap);
  82. #else
  83. gtk_widget_draw(viewContainer, context);
  84. #endif
  85. if (DumpRenderTreeSupportGtk::isTrackingRepaints(mainFrame))
  86. fillRepaintOverlayIntoContext(context, width, height);
  87. if (drawSelectionRect) {
  88. cairo_rectangle_int_t rectangle;
  89. DumpRenderTreeSupportGtk::rectangleForSelection(mainFrame, &rectangle);
  90. cairo_set_line_width(context, 1.0);
  91. cairo_rectangle(context, rectangle.x, rectangle.y, rectangle.width, rectangle.height);
  92. cairo_set_source_rgba(context, 1.0, 0.0, 0.0, 1.0);
  93. cairo_stroke(context);
  94. }
  95. cairo_surface_destroy(imageSurface);
  96. return BitmapContext::createByAdoptingBitmapAndContext(0, context);
  97. }