1 /*
   2  * Copyright (c) 2011, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.webkit.perf;
  27 
  28 import java.nio.ByteBuffer;
  29 
  30 import com.sun.javafx.logging.PlatformLogger;
  31 import com.sun.webkit.graphics.Ref;
  32 import com.sun.webkit.graphics.RenderTheme;
  33 import com.sun.webkit.graphics.ScrollBarTheme;
  34 import com.sun.webkit.graphics.WCFont;
  35 import com.sun.webkit.graphics.WCGradient;
  36 import com.sun.webkit.graphics.WCGraphicsContext;
  37 import com.sun.webkit.graphics.WCIcon;
  38 import com.sun.webkit.graphics.WCImage;
  39 import com.sun.webkit.graphics.WCPath;
  40 import com.sun.webkit.graphics.WCPoint;
  41 import com.sun.webkit.graphics.WCRectangle;
  42 import com.sun.webkit.graphics.WCTransform;
  43 
  44 public final class WCGraphicsPerfLogger extends WCGraphicsContext {
  45     private static final PlatformLogger log = PlatformLogger.getLogger(WCGraphicsPerfLogger.class.getName());
  46 
  47     private static final PerfLogger logger = PerfLogger.getLogger(log);
  48 
  49     private final WCGraphicsContext gc;
  50 
  51     public WCGraphicsPerfLogger(WCGraphicsContext gc) {
  52         this.gc = gc;
  53     }
  54 
  55     public synchronized static boolean isEnabled() {
  56         return logger.isEnabled();
  57     }
  58 
  59     public static void log() {
  60         logger.log();
  61     }
  62 
  63     public static void reset() {
  64         logger.reset();
  65     }
  66 
  67     @Override
  68     public Object getPlatformGraphics() {
  69         return gc.getPlatformGraphics();
  70     }
  71 
  72     @Override
  73     public void drawString(WCFont f, int[] glyphs,
  74                            float[] advanceDXY,
  75                            float x, float y)
  76     {
  77         logger.resumeCount("DRAWSTRING_GV");
  78         gc.drawString(f, glyphs, advanceDXY, x, y);
  79         logger.suspendCount("DRAWSTRING_GV");
  80     }
  81 
  82     @Override
  83     public void strokeRect(float x, float y, float w, float h, float lengthWidth) {
  84         logger.resumeCount("STROKERECT_FFFFF");
  85         gc.strokeRect(x,y,w,h,lengthWidth);
  86         logger.suspendCount("STROKERECT_FFFFF");
  87     }
  88 
  89     @Override
  90     public void fillRect(float x, float y, float w, float h, Integer rgba) {
  91         logger.resumeCount("FILLRECT_FFFFI");
  92         gc.fillRect(x, y, w, h, rgba);
  93         logger.suspendCount("FILLRECT_FFFFI");
  94     }
  95 
  96     @Override public void fillRoundedRect(float x, float y, float w, float h,
  97             float topLeftW, float topLeftH, float topRightW, float topRightH,
  98             float bottomLeftW, float bottomLeftH, float bottomRightW, float bottomRightH,
  99             int rgba) {
 100         logger.resumeCount("FILL_ROUNDED_RECT");
 101         gc.fillRoundedRect(x, y, w, h, topLeftW, topLeftH, topRightW, topRightH,
 102                 bottomLeftW, bottomLeftH, bottomRightW, bottomRightH, rgba);
 103         logger.suspendCount("FILL_ROUNDED_RECT");
 104     }
 105 
 106     @Override
 107     public void clearRect(float x, float y, float w, float h) {
 108         logger.resumeCount("CLEARRECT");
 109         gc.clearRect(x, y, w, h);
 110         logger.suspendCount("CLEARRECT");
 111     }
 112 
 113     @Override
 114     public void setFillColor(int rgba) {
 115         logger.resumeCount("SETFILLCOLOR");
 116         gc.setFillColor(rgba);
 117         logger.suspendCount("SETFILLCOLOR");
 118     }
 119 
 120     @Override
 121     public void setFillGradient(WCGradient gradient) {
 122         logger.resumeCount("SET_FILL_GRADIENT");
 123         gc.setFillGradient(gradient);
 124         logger.suspendCount("SET_FILL_GRADIENT");
 125     }
 126 
 127     @Override
 128     public void setTextMode(boolean fill, boolean stroke, boolean clip) {
 129         logger.resumeCount("SET_TEXT_MODE");
 130         gc.setTextMode(fill, stroke, clip);
 131         logger.suspendCount("SET_TEXT_MODE");
 132     }
 133 
 134     @Override
 135     public void setFontSmoothingType(int fontSmoothingType) {
 136         logger.resumeCount("SET_FONT_SMOOTHING_TYPE");
 137         gc.setFontSmoothingType(fontSmoothingType);
 138         logger.suspendCount("SET_FONT_SMOOTHING_TYPE");
 139     }
 140 
 141     @Override
 142     public int getFontSmoothingType() {
 143         logger.resumeCount("GET_FONT_SMOOTHING_TYPE");
 144         int n = gc.getFontSmoothingType();
 145         logger.suspendCount("GET_FONT_SMOOTHING_TYPE");
 146         return n;
 147     }
 148 
 149     @Override
 150     public void setStrokeStyle(int style) {
 151         logger.resumeCount("SETSTROKESTYLE");
 152         gc.setStrokeStyle(style);
 153         logger.suspendCount("SETSTROKESTYLE");
 154     }
 155 
 156     @Override
 157     public void setStrokeColor(int rgba) {
 158         logger.resumeCount("SETSTROKECOLOR");
 159         gc.setStrokeColor(rgba);
 160         logger.suspendCount("SETSTROKECOLOR");
 161     }
 162 
 163     @Override
 164     public void setStrokeWidth(float width) {
 165         logger.resumeCount("SETSTROKEWIDTH");
 166         gc.setStrokeWidth(width);
 167         logger.suspendCount("SETSTROKEWIDTH");
 168     }
 169 
 170     @Override
 171     public void setStrokeGradient(WCGradient gradient) {
 172         logger.resumeCount("SET_STROKE_GRADIENT");
 173         gc.setStrokeGradient(gradient);
 174         logger.suspendCount("SET_STROKE_GRADIENT");
 175     }
 176 
 177     @Override
 178     public void setLineDash(float offset, float... sizes) {
 179         logger.resumeCount("SET_LINE_DASH");
 180         gc.setLineDash(offset, sizes);
 181         logger.suspendCount("SET_LINE_DASH");
 182     }
 183 
 184     @Override
 185     public void setLineCap(int lineCap) {
 186         logger.resumeCount("SET_LINE_CAP");
 187         gc.setLineCap(lineCap);
 188         logger.suspendCount("SET_LINE_CAP");
 189     }
 190 
 191     @Override
 192     public void setLineJoin(int lineJoin) {
 193         logger.resumeCount("SET_LINE_JOIN");
 194         gc.setLineJoin(lineJoin);
 195         logger.suspendCount("SET_LINE_JOIN");
 196     }
 197 
 198     @Override
 199     public void setMiterLimit(float miterLimit) {
 200         logger.resumeCount("SET_MITER_LIMIT");
 201         gc.setMiterLimit(miterLimit);
 202         logger.suspendCount("SET_MITER_LIMIT");
 203     }
 204 
 205     @Override
 206     public void setShadow(float dx, float dy, float blur, int color) {
 207         logger.resumeCount("SETSHADOW");
 208         gc.setShadow(dx, dy, blur, color);
 209         logger.suspendCount("SETSHADOW");
 210     }
 211 
 212     @Override
 213     public void drawPolygon(WCPath path, boolean shouldAntialias) {
 214         logger.resumeCount("DRAWPOLYGON");
 215         gc.drawPolygon(path, shouldAntialias);
 216         logger.suspendCount("DRAWPOLYGON");
 217     }
 218 
 219     @Override
 220     public void drawLine(int x0, int y0, int x1, int y1) {
 221         logger.resumeCount("DRAWLINE");
 222         gc.drawLine(x0, y0, x1, y1);
 223         logger.suspendCount("DRAWLINE");
 224     }
 225 
 226     @Override
 227     public void drawImage(WCImage img,
 228                           float dstx, float dsty, float dstw, float dsth,
 229                           float srcx, float srcy, float srcw, float srch) {
 230         logger.resumeCount("DRAWIMAGE");
 231         gc.drawImage(img, dstx, dsty, dstw, dsth, srcx, srcy, srcw, srch);
 232         logger.suspendCount("DRAWIMAGE");
 233     }
 234 
 235     @Override
 236     public void drawIcon(WCIcon icon, int x, int y) {
 237         logger.resumeCount("DRAWICON");
 238         gc.drawIcon(icon, x, y);
 239         logger.suspendCount("DRAWICON");
 240     }
 241 
 242     @Override
 243     public void drawPattern(WCImage texture, WCRectangle srcRect,
 244             WCTransform patternTransform, WCPoint phase,
 245             WCRectangle destRect) {
 246         logger.resumeCount("DRAWPATTERN");
 247         gc.drawPattern(texture, srcRect, patternTransform, phase, destRect);
 248         logger.suspendCount("DRAWPATTERN");
 249     }
 250 
 251     @Override
 252     public void translate(float x, float y) {
 253         logger.resumeCount("TRANSLATE");
 254         gc.translate(x, y);
 255         logger.suspendCount("TRANSLATE");
 256     }
 257 
 258     @Override
 259     public void scale(float scaleX, float scaleY) {
 260         logger.resumeCount("SCALE");
 261         gc.scale(scaleX, scaleY);
 262         logger.suspendCount("SCALE");
 263     }
 264 
 265     @Override
 266     public void rotate(float radians) {
 267         logger.resumeCount("ROTATE");
 268         gc.rotate(radians);
 269         logger.suspendCount("ROTATE");
 270     }
 271 
 272     @Override
 273     public void saveState() {
 274         logger.resumeCount("SAVESTATE");
 275         gc.saveState();
 276         logger.suspendCount("SAVESTATE");
 277     }
 278 
 279     @Override
 280     public void restoreState() {
 281         logger.resumeCount("RESTORESTATE");
 282         gc.restoreState();
 283         logger.suspendCount("RESTORESTATE");
 284     }
 285 
 286     @Override
 287     public void setClip(WCPath path, boolean isOut) {
 288         logger.resumeCount("CLIP_PATH");
 289         gc.setClip(path, isOut);
 290         logger.suspendCount("CLIP_PATH");
 291     }
 292 
 293     @Override
 294     public void setClip(WCRectangle clip) {
 295         logger.resumeCount("SETCLIP_R");
 296         gc.setClip(clip);
 297         logger.suspendCount("SETCLIP_R");
 298     }
 299 
 300     @Override
 301     public void setClip(int cx, int cy, int cw, int ch) {
 302         logger.resumeCount("SETCLIP_IIII");
 303         gc.setClip(cx, cy, cw, ch);
 304         logger.suspendCount("SETCLIP_IIII");
 305     }
 306 
 307     @Override
 308     public WCRectangle getClip() {
 309         logger.resumeCount("SETCLIP_IIII");
 310         WCRectangle r = gc.getClip();
 311         logger.suspendCount("SETCLIP_IIII");
 312         return r;
 313     }
 314 
 315     @Override
 316     public void drawRect(int x, int y, int w, int h) {
 317         logger.resumeCount("DRAWRECT");
 318         gc.drawRect(x, y, w, h);
 319         logger.suspendCount("DRAWRECT");
 320     }
 321 
 322     @Override
 323     public void setComposite(int composite) {
 324         logger.resumeCount("SETCOMPOSITE");
 325         gc.setComposite(composite);
 326         logger.suspendCount("SETCOMPOSITE");
 327     }
 328 
 329     @Override
 330     public void strokeArc(int x, int y, int w, int h, int startAngle,
 331                           int angleSpan) {
 332         logger.resumeCount("STROKEARC");
 333         gc.strokeArc(x, y, w, h, startAngle, angleSpan);
 334         logger.suspendCount("STROKEARC");
 335     }
 336 
 337     @Override
 338     public void drawEllipse(int x, int y, int w, int h) {
 339         logger.resumeCount("DRAWELLIPSE");
 340         gc.drawEllipse(x, y, w, h);
 341         logger.suspendCount("DRAWELLIPSE");
 342     }
 343 
 344     @Override
 345     public void drawFocusRing(int x, int y, int w, int h, int rgba) {
 346         logger.resumeCount("DRAWFOCUSRING");
 347         gc.drawFocusRing(x, y, w, h, rgba);
 348         logger.suspendCount("DRAWFOCUSRING");
 349     }
 350 
 351     @Override
 352     public void setAlpha(float alpha) {
 353         logger.resumeCount("SETALPHA");
 354         gc.setAlpha(alpha);
 355         logger.suspendCount("SETALPHA");
 356     }
 357 
 358     @Override
 359     public float getAlpha() {
 360         logger.resumeCount("GETALPHA");
 361         float a = gc.getAlpha();
 362         logger.suspendCount("GETALPHA");
 363         return a;
 364     }
 365 
 366     @Override
 367     public void beginTransparencyLayer(float opacity) {
 368         logger.resumeCount("BEGINTRANSPARENCYLAYER");
 369         gc.beginTransparencyLayer(opacity);
 370         logger.suspendCount("BEGINTRANSPARENCYLAYER");
 371     }
 372 
 373     @Override
 374     public void endTransparencyLayer() {
 375         logger.resumeCount("ENDTRANSPARENCYLAYER");
 376         gc.endTransparencyLayer();
 377         logger.suspendCount("ENDTRANSPARENCYLAYER");
 378     }
 379 
 380     @Override
 381     public void drawString(WCFont f, String str, boolean rtl,
 382                            int from, int to,
 383                            float x, float y)
 384     {
 385         logger.resumeCount("DRAWSTRING");
 386         gc.drawString(f, str, rtl, from, to, x, y);
 387         logger.suspendCount("DRAWSTRING");
 388     }
 389 
 390     @Override
 391     public void strokePath(WCPath path) {
 392         logger.resumeCount("STROKE_PATH");
 393         gc.strokePath(path);
 394         logger.suspendCount("STROKE_PATH");
 395     }
 396 
 397     @Override
 398     public void fillPath(WCPath path) {
 399         logger.resumeCount("FILL_PATH");
 400         gc.fillPath(path);
 401         logger.suspendCount("FILL_PATH");
 402     }
 403 
 404     @Override
 405     public WCImage getImage() {
 406         logger.resumeCount("GETIMAGE");
 407         WCImage res = gc.getImage();
 408         logger.suspendCount("GETIMAGE");
 409         return res;
 410     }
 411 
 412     @Override
 413     public void drawWidget(RenderTheme theme, Ref widget, int x, int y) {
 414         logger.resumeCount("DRAWWIDGET");
 415         gc.drawWidget(theme, widget, x, y);
 416         logger.suspendCount("DRAWWIDGET");
 417     }
 418 
 419     @Override
 420     public void drawScrollbar(ScrollBarTheme theme, Ref widget,
 421                               int x, int y, int pressedPart, int hoveredPart)
 422     {
 423         logger.resumeCount("DRAWSCROLLBAR");
 424         gc.drawScrollbar(theme, widget, x, y, pressedPart, hoveredPart);
 425         logger.suspendCount("DRAWSCROLLBAR");
 426     }
 427 
 428     @Override
 429     public void dispose() {
 430         logger.resumeCount("DISPOSE");
 431         gc.dispose();
 432         logger.suspendCount("DISPOSE");
 433     }
 434 
 435     @Override
 436     public void flush() {
 437         logger.resumeCount("FLUSH");
 438         gc.flush();
 439         logger.suspendCount("FLUSH");
 440     }
 441 
 442     @Override
 443     public void setTransform(WCTransform t) {
 444         logger.resumeCount("SETTRANSFORM");
 445         gc.setTransform(t);
 446         logger.suspendCount("SETTRANSFORM");
 447     }
 448 
 449     @Override
 450     public WCTransform getTransform() {
 451         logger.resumeCount("GETTRANSFORM");
 452         WCTransform t = gc.getTransform();
 453         logger.suspendCount("GETTRANSFORM");
 454         return t;
 455     }
 456 
 457     @Override
 458     public void concatTransform(WCTransform t) {
 459         logger.resumeCount("CONCATTRANSFORM");
 460         gc.concatTransform(t);
 461         logger.suspendCount("CONCATTRANSFORM");
 462     }
 463 
 464     @Override
 465     public void drawBitmapImage(ByteBuffer image, int x, int y, int w, int h) {
 466         logger.resumeCount("DRAWBITMAPIMAGE");
 467         gc.drawBitmapImage(image, x, y, w, h);
 468         logger.suspendCount("DRAWBITMAPIMAGE");
 469     }
 470 
 471     @Override
 472     public WCGradient createLinearGradient(WCPoint p1, WCPoint p2) {
 473         logger.resumeCount("CREATE_LINEAR_GRADIENT");
 474         WCGradient gradient = gc.createLinearGradient(p1, p2);
 475         logger.suspendCount("CREATE_LINEAR_GRADIENT");
 476         return gradient;
 477     }
 478 
 479     @Override
 480     public WCGradient createRadialGradient(WCPoint p1, float r1, WCPoint p2, float r2) {
 481         logger.resumeCount("CREATE_RADIAL_GRADIENT");
 482         WCGradient gradient = gc.createRadialGradient(p1, r1, p2, r2);
 483         logger.suspendCount("CREATE_RADIAL_GRADIENT");
 484         return gradient;
 485     }
 486 }