Home | History | Annotate | Download | only in gm
      1 /*
      2  * Copyright 2015 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 "sk_tool_utils.h"
     10 
     11 #include "Resources.h"
     12 #include "SkCanvas.h"
     13 #include "SkGradientShader.h"
     14 #include "SkStream.h"
     15 #include "SkTextBlob.h"
     16 #include "SkTypeface.h"
     17 
     18 namespace skiagm {
     19 class TextBlobColorTrans : public GM {
     20 public:
     21     // This gm tests that textblobs can be translated and have their colors regenerated
     22     // correctly.  With smaller atlas sizes, it can also trigger regeneration of texture coords on
     23     // the GPU backend
     24     TextBlobColorTrans() { }
     25 
     26 protected:
     27     void onOnceBeforeDraw() override {
     28         SkTextBlobBuilder builder;
     29 
     30         // make textblob
     31         // Large text is used to trigger atlas eviction
     32         SkPaint paint;
     33         paint.setTextSize(256);
     34         const char* text = "AB";
     35         sk_tool_utils::set_portable_typeface(&paint);
     36 
     37         SkRect bounds;
     38         paint.measureText(text, strlen(text), &bounds);
     39 
     40         SkScalar yOffset = bounds.height();
     41         sk_tool_utils::add_to_text_blob(&builder, text, paint, 0, yOffset - 30);
     42 
     43         // A8
     44         paint.setTextSize(28);
     45         text = "The quick brown fox jumps over the lazy dog.";
     46         paint.setSubpixelText(false);
     47         paint.setLCDRenderText(false);
     48         paint.measureText(text, strlen(text), &bounds);
     49         sk_tool_utils::add_to_text_blob(&builder, text, paint, 0, yOffset - 8);
     50 
     51         // build
     52         fBlob = builder.make();
     53     }
     54 
     55     SkString onShortName() override {
     56         return SkString("textblobcolortrans");
     57     }
     58 
     59     SkISize onISize() override {
     60         return SkISize::Make(kWidth, kHeight);
     61     }
     62 
     63     void onDraw(SkCanvas* canvas) override {
     64 
     65         canvas->drawColor(sk_tool_utils::color_to_565(SK_ColorGRAY));
     66 
     67         SkPaint paint;
     68         canvas->translate(10, 40);
     69 
     70         SkRect bounds = fBlob->bounds();
     71 
     72         // Colors were chosen to map to pairs of canonical colors.  The GPU Backend will cache A8
     73         // Texture Blobs based on the canonical color they map to.  Canonical colors are used to
     74         // create masks.  For A8 there are 8 of them.
     75         SkColor colors[] = {SK_ColorCYAN, sk_tool_utils::color_to_565(SK_ColorLTGRAY),
     76                 SK_ColorYELLOW, SK_ColorWHITE};
     77 
     78         size_t count = SK_ARRAY_COUNT(colors);
     79         size_t colorIndex = 0;
     80         for (int y = 0; y + SkScalarFloorToInt(bounds.height()) < kHeight;
     81              y += SkScalarFloorToInt(bounds.height())) {
     82             paint.setColor(colors[colorIndex++ % count]);
     83             canvas->save();
     84             canvas->translate(0, SkIntToScalar(y));
     85             canvas->drawTextBlob(fBlob, 0, 0, paint);
     86             canvas->restore();
     87         }
     88     }
     89 
     90 private:
     91     sk_sp<SkTextBlob> fBlob;
     92 
     93     static constexpr int kWidth = 675;
     94     static constexpr int kHeight = 1600;
     95 
     96     typedef GM INHERITED;
     97 };
     98 
     99 //////////////////////////////////////////////////////////////////////////////
    100 
    101 DEF_GM(return new TextBlobColorTrans;)
    102 }
    103