Home | History | Annotate | Download | only in gm
      1 /*
      2  * Copyright 2013 Google Inc.
      3  *
      4  * Use of this source code is governed by a BSD-style license that can be
      5  * found in the LICENSE file.
      6  */
      7 
      8 #include "gm.h"
      9 #include "SkCanvas.h"
     10 #include "SkGraphics.h"
     11 #include "SkTypeface.h"
     12 
     13 // GM to stress the GPU font cache
     14 
     15 static SkScalar draw_string(SkCanvas* canvas, const SkString& text, SkScalar x,
     16                            SkScalar y, const SkPaint& paint) {
     17     canvas->drawText(text.c_str(), text.size(), x, y, paint);
     18     return x + paint.measureText(text.c_str(), text.size());
     19 }
     20 
     21 class FontCacheGM : public skiagm::GM {
     22 public:
     23     FontCacheGM() {
     24         fTypefaces[0] = NULL;
     25         fTypefaces[1] = NULL;
     26     }
     27 
     28     virtual ~FontCacheGM() {
     29         SkSafeUnref(fTypefaces[0]);
     30         SkSafeUnref(fTypefaces[1]);
     31     }
     32 
     33 protected:
     34     SkString onShortName() override {
     35         return SkString("fontcache");
     36     }
     37 
     38     SkISize onISize() override {
     39         return SkISize::Make(1280, 640);
     40     }
     41 
     42     void onOnceBeforeDraw() override {
     43         fTypefaces[0] = sk_tool_utils::create_portable_typeface("serif", SkTypeface::kItalic);
     44         fTypefaces[1] = sk_tool_utils::create_portable_typeface("sans-serif", SkTypeface::kItalic);
     45     }
     46 
     47     void onDraw(SkCanvas* canvas) override {
     48         SkPaint paint;
     49         paint.setAntiAlias(true);
     50         paint.setLCDRenderText(true);
     51         paint.setSubpixelText(true);
     52         paint.setTypeface(fTypefaces[0]);
     53         paint.setTextSize(192);
     54 
     55         // Make sure the nul character does not cause problems.
     56         paint.measureText("\0", 1);
     57 
     58         SkScalar x = 20;
     59         SkScalar y = 128;
     60         SkString text("ABCDEFGHIJ");
     61         draw_string(canvas, text, x, y, paint);
     62         y += 100;
     63         SkString text2("KLMNOPQRS");
     64         draw_string(canvas, text2, x, y, paint);
     65         y += 100;
     66         SkString text3("TUVWXYZ012");
     67         draw_string(canvas, text3, x, y, paint);
     68         y += 100;
     69         paint.setTypeface(fTypefaces[1]);
     70         draw_string(canvas, text, x, y, paint);
     71         y += 100;
     72         draw_string(canvas, text2, x, y, paint);
     73         y += 100;
     74         draw_string(canvas, text3, x, y, paint);
     75         y += 100;
     76     }
     77 
     78 private:
     79     SkTypeface* fTypefaces[2];
     80     typedef GM INHERITED;
     81 };
     82 
     83 
     84 //////////////////////////////////////////////////////////////////////////////
     85 
     86 DEF_GM( return SkNEW(FontCacheGM); )
     87