Home | History | Annotate | Download | only in ports
      1 
      2 /*
      3  * Copyright 2011 Google Inc.
      4  *
      5  * Use of this source code is governed by a BSD-style license that can be
      6  * found in the LICENSE file.
      7  */
      8 #include "SkFontHost.h"
      9 #include <math.h>
     10 
     11 // define this to use pre-compiled tables for gamma. This is slightly faster,
     12 // and doesn't create any RW global memory, but means we cannot change the
     13 // gamma at runtime.
     14 //#define USE_PREDEFINED_GAMMA_TABLES
     15 
     16 #ifndef USE_PREDEFINED_GAMMA_TABLES
     17     // define this if you want to spew out the "C" code for the tables, given
     18     // the current values for SK_BLACK_GAMMA and SK_WHITE_GAMMA.
     19     #define DUMP_GAMMA_TABLESx
     20 #endif
     21 
     22 ///////////////////////////////////////////////////////////////////////////////
     23 
     24 #include "SkGraphics.h"
     25 
     26 // declared here, so we can link against it elsewhere
     27 void skia_set_text_gamma(float blackGamma, float whiteGamma);
     28 
     29 #ifdef USE_PREDEFINED_GAMMA_TABLES
     30 
     31 #include "sk_predefined_gamma.h"
     32 
     33 void skia_set_text_gamma(float blackGamma, float whiteGamma) {}
     34 
     35 #else   // use writable globals for gamma tables
     36 
     37 static void build_power_table(uint8_t table[], float ee) {
     38 //    SkDebugf("------ build_power_table %g\n", ee);
     39     for (int i = 0; i < 256; i++) {
     40         float x = i / 255.f;
     41         //   printf(" %d %g", i, x);
     42         x = powf(x, ee);
     43         //   printf(" %g", x);
     44         int xx = SkScalarRound(SkFloatToScalar(x * 255));
     45         //   printf(" %d\n", xx);
     46         table[i] = SkToU8(xx);
     47     }
     48 }
     49 
     50 static bool gGammaIsBuilt;
     51 static uint8_t gBlackGamma[256], gWhiteGamma[256];
     52 
     53 static float gBlackGammaCoeff = 1.4f;
     54 static float gWhiteGammaCoeff = 1/1.4f;
     55 
     56 void skia_set_text_gamma(float blackGamma, float whiteGamma) {
     57     gBlackGammaCoeff = blackGamma;
     58     gWhiteGammaCoeff = whiteGamma;
     59     gGammaIsBuilt = false;
     60     SkGraphics::PurgeFontCache();
     61     build_power_table(gBlackGamma, gBlackGammaCoeff);
     62     build_power_table(gWhiteGamma, gWhiteGammaCoeff);
     63 }
     64 
     65 #ifdef DUMP_GAMMA_TABLES
     66 
     67 #include "SkString.h"
     68 
     69 static void dump_a_table(const char name[], const uint8_t table[],
     70                          float gamma) {
     71     SkDebugf("\n");
     72     SkDebugf("\/\/ Gamma table for %g\n", gamma);
     73     SkDebugf("static const uint8_t %s[] = {\n", name);
     74     for (int y = 0; y < 16; y++) {
     75         SkString line, tmp;
     76         for (int x = 0; x < 16; x++) {
     77             tmp.printf("0x%02X, ", *table++);
     78             line.append(tmp);
     79         }
     80         SkDebugf("    %s\n", line.c_str());
     81     }
     82     SkDebugf("};\n");
     83 }
     84 
     85 #endif
     86 
     87 #endif
     88 
     89 ///////////////////////////////////////////////////////////////////////////////
     90 
     91 void SkFontHost::GetGammaTables(const uint8_t* tables[2]) {
     92 #ifndef USE_PREDEFINED_GAMMA_TABLES
     93     if (!gGammaIsBuilt) {
     94         build_power_table(gBlackGamma, gBlackGammaCoeff);
     95         build_power_table(gWhiteGamma, gWhiteGammaCoeff);
     96         gGammaIsBuilt = true;
     97 
     98 #ifdef DUMP_GAMMA_TABLES
     99         dump_a_table("gBlackGamma", gBlackGamma, gBlackGammaCoeff);
    100         dump_a_table("gWhiteGamma", gWhiteGamma, gWhiteGammaCoeff);
    101 #endif
    102     }
    103 #endif
    104     tables[0] = gBlackGamma;
    105     tables[1] = gWhiteGamma;
    106 }
    107 
    108