1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
|
/*
* Copyright (C) 2010 Sencha, Inc.
* Copyright (C) 2010 Igalia S.L.
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "WidgetRenderingContext.h"
#ifdef GTK_API_VERSION_2
#include "GraphicsContext.h"
#include "GtkVersioning.h"
#include "PlatformContextCairo.h"
#include "RefPtrCairo.h"
#include "RenderThemeGtk.h"
#include "Timer.h"
#include <gdk/gdk.h>
#include <gtk/gtk.h>
#include <wtf/StdLibExtras.h>
namespace WebCore {
static GdkPixmap* gScratchBuffer = 0;
static void purgeScratchBuffer()
{
if (gScratchBuffer)
g_object_unref(gScratchBuffer);
gScratchBuffer = 0;
}
// Widget rendering needs a scratch image as the buffer for the intermediate
// render. Instead of creating and destroying the buffer for every operation,
// we create a buffer which will be automatically purged via a timer.
class PurgeScratchBufferTimer : public TimerBase {
private:
virtual void fired() { purgeScratchBuffer(); }
};
static void scheduleScratchBufferPurge()
{
DEFINE_STATIC_LOCAL(PurgeScratchBufferTimer, purgeScratchBufferTimer, ());
if (purgeScratchBufferTimer.isActive())
purgeScratchBufferTimer.stop();
purgeScratchBufferTimer.startOneShot(2);
}
WidgetRenderingContext::WidgetRenderingContext(GraphicsContext* graphicsContext, const IntRect& targetRect)
: m_graphicsContext(graphicsContext)
, m_targetRect(targetRect)
, m_hadError(false)
{
RenderThemeGtk* theme = static_cast<RenderThemeGtk*>(RenderTheme::defaultTheme().get());
// Fallback: We failed to create an RGBA colormap earlier, so we cannot properly paint
// to a temporary surface and preserve transparency. To ensure decent widget rendering, just
// paint directly to the target drawable. This will not render CSS rotational transforms properly.
if (!theme->m_themePartsHaveRGBAColormap && graphicsContext->gdkWindow()) {
m_target = graphicsContext->gdkWindow();
return;
}
// We never want to create a scratch buffer larger than the size of our target GdkDrawable.
// This prevents giant pixmap allocations for very large widgets in smaller views.
// FIXME: We need to implement this check for WebKit2 as well.
if (GdkDrawable* drawable = graphicsContext->gdkWindow()) {
int maxWidth = 0, maxHeight = 0;
if (GDK_IS_PIXMAP(drawable))
gdk_pixmap_get_size(GDK_PIXMAP(drawable), &maxWidth, &maxHeight);
else if (GDK_IS_WINDOW(drawable)) {
maxWidth = gdk_window_get_width(GDK_WINDOW(drawable));
maxHeight = gdk_window_get_height(GDK_WINDOW(drawable));
}
m_targetRect.setSize(m_targetRect.size().shrunkTo(IntSize(maxWidth, maxHeight)));
}
// Widgets sometimes need to draw outside their boundaries for things such as
// exterior focus. We want to allocate a some extra pixels in our surface for this.
static int extraSpace = 15;
m_targetRect.inflate(extraSpace);
// This offset will map a point in the coordinate system of the widget to the coordinate system of the painting buffer.
m_paintOffset = targetRect.location() - m_targetRect.location();
int width = m_targetRect.width() + (extraSpace * 2);
int height = m_targetRect.height() + (extraSpace * 2);
int scratchWidth = 0;
int scratchHeight = 0;
if (gScratchBuffer)
gdk_pixmap_get_size(gScratchBuffer, &scratchWidth, &scratchHeight);
// We do not need to recreate the buffer if the current buffer is large enough.
if (!gScratchBuffer || scratchWidth < width || scratchHeight < height) {
purgeScratchBuffer();
// Round to the nearest 32 pixels so we do not grow the buffer for similar sized requests.
width = (1 + (width >> 5)) << 5;
height = (1 + (height >> 5)) << 5;
gScratchBuffer = gdk_pixmap_new(0, width, height, gdk_colormap_get_visual(theme->m_colormap)->depth);
gdk_drawable_set_colormap(gScratchBuffer, theme->m_colormap);
}
m_target = gScratchBuffer;
// Clear the scratch buffer.
RefPtr<cairo_t> scratchBufferContext = adoptRef(gdk_cairo_create(gScratchBuffer));
cairo_set_operator(scratchBufferContext.get(), CAIRO_OPERATOR_CLEAR);
cairo_paint(scratchBufferContext.get());
}
WidgetRenderingContext::~WidgetRenderingContext()
{
// We do not need to blit back to the target in the fallback case. See above.
RenderThemeGtk* theme = static_cast<RenderThemeGtk*>(RenderTheme::defaultTheme().get());
if (!theme->m_themePartsHaveRGBAColormap && m_graphicsContext->gdkWindow())
return;
// Don't paint the results back if there was an error.
if (m_hadError) {
scheduleScratchBufferPurge();
return;
}
// FIXME: It's unclear if it is necessary to preserve the current source here.
cairo_t* cairoContext = m_graphicsContext->platformContext()->cr();
RefPtr<cairo_pattern_t> previousSource(cairo_get_source(cairoContext));
gdk_cairo_set_source_pixmap(cairoContext, gScratchBuffer, m_targetRect.x(), m_targetRect.y());
cairo_rectangle(cairoContext, m_targetRect.x(), m_targetRect.y(), m_targetRect.width(), m_targetRect.height());
cairo_fill(cairoContext);
cairo_set_source(cairoContext, previousSource.get());
scheduleScratchBufferPurge();
}
void WidgetRenderingContext::calculateClipRect(const IntRect& rect, GdkRectangle* clipRect)
{
clipRect->x = m_paintOffset.width() + rect.x();
clipRect->y = m_paintOffset.height() + rect.y();
clipRect->width = m_targetRect.width();
clipRect->height = m_targetRect.height();
}
void WidgetRenderingContext::gtkPaintBox(const IntRect& rect, GtkWidget* widget, GtkStateType stateType, GtkShadowType shadowType, const gchar* detail)
{
GdkRectangle clipRect;
calculateClipRect(rect, &clipRect);
// Some widgets also need their allocation adjusted to account for extra space.
// Right now only scrollbar buttons have significant allocations.
GtkAllocation allocation;
gtk_widget_get_allocation(widget, &allocation);
allocation.x += clipRect.x;
allocation.y += clipRect.y;
gtk_widget_set_allocation(widget, &allocation);
gtk_paint_box(gtk_widget_get_style(widget), m_target, stateType, shadowType, &clipRect,
widget, detail, clipRect.x, clipRect.y, rect.width(), rect.height());
}
void WidgetRenderingContext::gtkPaintFlatBox(const IntRect& rect, GtkWidget* widget, GtkStateType stateType, GtkShadowType shadowType, const gchar* detail)
{
GdkRectangle clipRect;
calculateClipRect(rect, &clipRect);
gtk_paint_flat_box(gtk_widget_get_style(widget), m_target, stateType, shadowType, &clipRect,
widget, detail, clipRect.x, clipRect.y, rect.width(), rect.height());
}
void WidgetRenderingContext::gtkPaintFocus(const IntRect& rect, GtkWidget* widget, GtkStateType stateType, const gchar* detail)
{
GdkRectangle clipRect;
calculateClipRect(rect, &clipRect);
gtk_paint_focus(gtk_widget_get_style(widget), m_target, stateType, &clipRect, widget,
detail, clipRect.x, clipRect.y, rect.width(), rect.height());
}
void WidgetRenderingContext::gtkPaintSlider(const IntRect& rect, GtkWidget* widget, GtkStateType stateType, GtkShadowType shadowType, const gchar* detail, GtkOrientation orientation)
{
GdkRectangle clipRect;
calculateClipRect(rect, &clipRect);
gtk_paint_slider(gtk_widget_get_style(widget), m_target, stateType, shadowType, &clipRect, widget,
detail, clipRect.x, clipRect.y, rect.width(), rect.height(), orientation);
}
void WidgetRenderingContext::gtkPaintCheck(const IntRect& rect, GtkWidget* widget, GtkStateType stateType, GtkShadowType shadowType, const gchar* detail)
{
GdkRectangle clipRect;
calculateClipRect(rect, &clipRect);
gtk_paint_check(gtk_widget_get_style(widget), m_target, stateType, shadowType, &clipRect, widget,
detail, clipRect.x, clipRect.y, rect.width(), rect.height());
}
void WidgetRenderingContext::gtkPaintOption(const IntRect& rect, GtkWidget* widget, GtkStateType stateType, GtkShadowType shadowType, const gchar* detail)
{
GdkRectangle clipRect;
calculateClipRect(rect, &clipRect);
gtk_paint_option(gtk_widget_get_style(widget), m_target, stateType, shadowType, &clipRect, widget,
detail, clipRect.x, clipRect.y, rect.width(), rect.height());
}
void WidgetRenderingContext::gtkPaintShadow(const IntRect& rect, GtkWidget* widget, GtkStateType stateType, GtkShadowType shadowType, const gchar* detail)
{
GdkRectangle clipRect;
calculateClipRect(rect, &clipRect);
gtk_paint_shadow(gtk_widget_get_style(widget), m_target, stateType, shadowType, &clipRect, widget,
detail, clipRect.x, clipRect.y, rect.width(), rect.height());
}
void WidgetRenderingContext::gtkPaintArrow(const IntRect& rect, GtkWidget* widget, GtkStateType stateType, GtkShadowType shadowType, int arrowDirection, const gchar* detail)
{
GdkRectangle clipRect;
calculateClipRect(rect, &clipRect);
gtk_paint_arrow(gtk_widget_get_style(widget), m_target, stateType, shadowType, &clipRect, widget, detail,
static_cast<GtkArrowType>(arrowDirection), TRUE, clipRect.x, clipRect.y, rect.width(), rect.height());
}
void WidgetRenderingContext::gtkPaintVLine(const IntRect& rect, GtkWidget* widget, GtkStateType stateType, const gchar* detail)
{
GdkRectangle clipRect;
calculateClipRect(rect, &clipRect);
gtk_paint_vline(gtk_widget_get_style(widget), m_target, stateType, &clipRect, widget, detail,
clipRect.y, clipRect.y + rect.height(), clipRect.x);
}
}
#endif // GTK_API_VERSION_2
|