115e9d3e66e161ce23df30bc13f8a0c87d196b463robertphillips@google.com/*
215e9d3e66e161ce23df30bc13f8a0c87d196b463robertphillips@google.com * Copyright 2012 Google Inc.
315e9d3e66e161ce23df30bc13f8a0c87d196b463robertphillips@google.com *
415e9d3e66e161ce23df30bc13f8a0c87d196b463robertphillips@google.com * Use of this source code is governed by a BSD-style license that can be
515e9d3e66e161ce23df30bc13f8a0c87d196b463robertphillips@google.com * found in the LICENSE file.
615e9d3e66e161ce23df30bc13f8a0c87d196b463robertphillips@google.com */
748a4cbc6684b9bb8396708c3275932b51ffa9467reed@google.com
848a4cbc6684b9bb8396708c3275932b51ffa9467reed@google.com
948a4cbc6684b9bb8396708c3275932b51ffa9467reed@google.com#include "SkDeviceProfile.h"
101b24933e52f50773de29332387a12721811f3012mtklein#include "SkMutex.h"
1148a4cbc6684b9bb8396708c3275932b51ffa9467reed@google.com
1209042b80d22837c760bb530124aaa67469b19b8frobertphillips@google.com#define DEFAULT_GAMMAEXP        2.2f
1309042b80d22837c760bb530124aaa67469b19b8frobertphillips@google.com#define DEFAULT_CONTRASTSCALE   0.5f
1448a4cbc6684b9bb8396708c3275932b51ffa9467reed@google.com#define DEFAULT_LCDCONFIG       SkDeviceProfile::kNone_LCDConfig
1548a4cbc6684b9bb8396708c3275932b51ffa9467reed@google.com#define DEFAULT_FONTHINTLEVEL   SkDeviceProfile::kSlight_FontHintLevel
1648a4cbc6684b9bb8396708c3275932b51ffa9467reed@google.com
1748a4cbc6684b9bb8396708c3275932b51ffa9467reed@google.comstatic float pin(float value, float min, float max) {
1848a4cbc6684b9bb8396708c3275932b51ffa9467reed@google.com    if (value < min) {
1948a4cbc6684b9bb8396708c3275932b51ffa9467reed@google.com        value = min;
2048a4cbc6684b9bb8396708c3275932b51ffa9467reed@google.com    } else if (value > max) {
2148a4cbc6684b9bb8396708c3275932b51ffa9467reed@google.com        value = max;
2248a4cbc6684b9bb8396708c3275932b51ffa9467reed@google.com    }
2348a4cbc6684b9bb8396708c3275932b51ffa9467reed@google.com    return value;
2448a4cbc6684b9bb8396708c3275932b51ffa9467reed@google.com}
2548a4cbc6684b9bb8396708c3275932b51ffa9467reed@google.com
2648a4cbc6684b9bb8396708c3275932b51ffa9467reed@google.comSkDeviceProfile::SkDeviceProfile(float gammaExp, float contrast,
2748a4cbc6684b9bb8396708c3275932b51ffa9467reed@google.com                                 LCDConfig config, FontHintLevel level) {
2848a4cbc6684b9bb8396708c3275932b51ffa9467reed@google.com    fGammaExponent = pin(gammaExp, 0, 10);
2948a4cbc6684b9bb8396708c3275932b51ffa9467reed@google.com    fContrastScale = pin(contrast, 0, 1);
3048a4cbc6684b9bb8396708c3275932b51ffa9467reed@google.com    fLCDConfig = config;
3148a4cbc6684b9bb8396708c3275932b51ffa9467reed@google.com    fFontHintLevel = level;
3248a4cbc6684b9bb8396708c3275932b51ffa9467reed@google.com}
3348a4cbc6684b9bb8396708c3275932b51ffa9467reed@google.com
3448a4cbc6684b9bb8396708c3275932b51ffa9467reed@google.comvoid SkDeviceProfile::generateTableForLuminanceByte(U8CPU lumByte,
3548a4cbc6684b9bb8396708c3275932b51ffa9467reed@google.com                                                    uint8_t table[256]) const {
3648a4cbc6684b9bb8396708c3275932b51ffa9467reed@google.com}
3748a4cbc6684b9bb8396708c3275932b51ffa9467reed@google.com
3848a4cbc6684b9bb8396708c3275932b51ffa9467reed@google.com///////////////////////////////////////////////////////////////////////////////
3948a4cbc6684b9bb8396708c3275932b51ffa9467reed@google.com
4048a4cbc6684b9bb8396708c3275932b51ffa9467reed@google.comSkDeviceProfile* SkDeviceProfile::Create(float gammaExp,
4148a4cbc6684b9bb8396708c3275932b51ffa9467reed@google.com                                         float contrast,
4248a4cbc6684b9bb8396708c3275932b51ffa9467reed@google.com                                         LCDConfig config,
4348a4cbc6684b9bb8396708c3275932b51ffa9467reed@google.com                                         FontHintLevel level) {
442880df2609eba09b555ca37be04b6ad89290c765Tom Hudson    return new SkDeviceProfile(gammaExp, contrast, config, level);
4548a4cbc6684b9bb8396708c3275932b51ffa9467reed@google.com}
4648a4cbc6684b9bb8396708c3275932b51ffa9467reed@google.com
47415c480a88d448e4c09643443ce3038a5ee108c5bungeman@google.comSK_DECLARE_STATIC_MUTEX(gMutex);
4848a4cbc6684b9bb8396708c3275932b51ffa9467reed@google.comstatic SkDeviceProfile* gDefaultProfile;
4948a4cbc6684b9bb8396708c3275932b51ffa9467reed@google.comstatic SkDeviceProfile* gGlobalProfile;
5048a4cbc6684b9bb8396708c3275932b51ffa9467reed@google.com
5148a4cbc6684b9bb8396708c3275932b51ffa9467reed@google.comSkDeviceProfile* SkDeviceProfile::GetDefault() {
5248a4cbc6684b9bb8396708c3275932b51ffa9467reed@google.com    SkAutoMutexAcquire amc(gMutex);
5348a4cbc6684b9bb8396708c3275932b51ffa9467reed@google.com
542880df2609eba09b555ca37be04b6ad89290c765Tom Hudson    if (nullptr == gDefaultProfile) {
5548a4cbc6684b9bb8396708c3275932b51ffa9467reed@google.com        gDefaultProfile = SkDeviceProfile::Create(DEFAULT_GAMMAEXP,
5648a4cbc6684b9bb8396708c3275932b51ffa9467reed@google.com                                                  DEFAULT_CONTRASTSCALE,
5748a4cbc6684b9bb8396708c3275932b51ffa9467reed@google.com                                                  DEFAULT_LCDCONFIG,
5848a4cbc6684b9bb8396708c3275932b51ffa9467reed@google.com                                                  DEFAULT_FONTHINTLEVEL);
5948a4cbc6684b9bb8396708c3275932b51ffa9467reed@google.com    }
6048a4cbc6684b9bb8396708c3275932b51ffa9467reed@google.com    return gDefaultProfile;
6148a4cbc6684b9bb8396708c3275932b51ffa9467reed@google.com}
6248a4cbc6684b9bb8396708c3275932b51ffa9467reed@google.com
6348a4cbc6684b9bb8396708c3275932b51ffa9467reed@google.comSkDeviceProfile* SkDeviceProfile::RefGlobal() {
6448a4cbc6684b9bb8396708c3275932b51ffa9467reed@google.com    SkAutoMutexAcquire amc(gMutex);
6548a4cbc6684b9bb8396708c3275932b51ffa9467reed@google.com
662880df2609eba09b555ca37be04b6ad89290c765Tom Hudson    if (nullptr == gGlobalProfile) {
6748a4cbc6684b9bb8396708c3275932b51ffa9467reed@google.com        gGlobalProfile = SkDeviceProfile::GetDefault();
6848a4cbc6684b9bb8396708c3275932b51ffa9467reed@google.com    }
6948a4cbc6684b9bb8396708c3275932b51ffa9467reed@google.com    gGlobalProfile->ref();
7048a4cbc6684b9bb8396708c3275932b51ffa9467reed@google.com    return gGlobalProfile;
7148a4cbc6684b9bb8396708c3275932b51ffa9467reed@google.com}
72fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
7348a4cbc6684b9bb8396708c3275932b51ffa9467reed@google.comvoid SkDeviceProfile::SetGlobal(SkDeviceProfile* profile) {
7448a4cbc6684b9bb8396708c3275932b51ffa9467reed@google.com    SkAutoMutexAcquire amc(gMutex);
7548a4cbc6684b9bb8396708c3275932b51ffa9467reed@google.com
7648a4cbc6684b9bb8396708c3275932b51ffa9467reed@google.com    SkRefCnt_SafeAssign(gGlobalProfile, profile);
7748a4cbc6684b9bb8396708c3275932b51ffa9467reed@google.com}
78