/*
 * Copyright 2019 Google LLC
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "include/core/SkScalar.h"
#include "include/private/gpu/ganesh/GrGLTypesPriv.h"
#include "src/gpu/Swizzle.h"
#include "src/gpu/ganesh/gl/GrGLDefines_impl.h"

GrGLTextureParameters::SamplerOverriddenState::SamplerOverriddenState()
        // These are the OpenGL defaults.
        : fMinFilter(GR_GL_NEAREST_MIPMAP_LINEAR)
        , fMagFilter(GR_GL_LINEAR)
        , fWrapS(GR_GL_REPEAT)
        , fWrapT(GR_GL_REPEAT)
        , fMinLOD(-1000.f)
        , fMaxLOD(1000.f)
        , fMaxAniso(1.f)
        , fBorderColorInvalid(false) {}

void GrGLTextureParameters::SamplerOverriddenState::invalidate() {
    fMinFilter = ~0U;
    fMagFilter = ~0U;
    fWrapS = ~0U;
    fWrapT = ~0U;
    fMinLOD = SK_ScalarNaN;
    fMaxLOD = SK_ScalarNaN;
    fMaxAniso = -1.f;
    fBorderColorInvalid = true;
}

GrGLTextureParameters::NonsamplerState::NonsamplerState()
        // These are the OpenGL defaults.
        : fBaseMipMapLevel(0), fMaxMipmapLevel(1000), fSwizzleIsRGBA(true) {}

void GrGLTextureParameters::NonsamplerState::invalidate() {
    fSwizzleIsRGBA = false;
    fBaseMipMapLevel = ~0;
    fMaxMipmapLevel = ~0;
}

void GrGLTextureParameters::invalidate() {
    fSamplerOverriddenState.invalidate();
    fNonsamplerState.invalidate();
}

void GrGLTextureParameters::set(const SamplerOverriddenState* samplerState,
                                const NonsamplerState& nonsamplerState,
                                ResetTimestamp currTimestamp) {
    if (samplerState) {
        fSamplerOverriddenState = *samplerState;
    }
    fNonsamplerState = nonsamplerState;
    fResetTimestamp = currTimestamp;
}

void GrGLBackendTextureInfo::assign(const GrGLBackendTextureInfo& that, bool thisIsValid) {
    fInfo = that.fInfo;
    SkSafeRef(that.fParams);
    if (thisIsValid) {
        SkSafeUnref(fParams);
    }
    fParams = that.fParams;
}

void GrGLBackendTextureInfo::cleanup() { SkSafeUnref(fParams); }

GrGLSurfaceInfo GrGLTextureSpecToSurfaceInfo(const GrGLTextureSpec& glSpec,
                                             uint32_t sampleCount,
                                             uint32_t levelCount,
                                             GrProtected isProtected) {
    GrGLSurfaceInfo info;
    // Shared info
    info.fSampleCount = sampleCount;
    info.fLevelCount = levelCount;
    info.fProtected = isProtected;

    // GL info
    info.fTarget = glSpec.fTarget;
    info.fFormat = glSpec.fFormat;

    return info;
}
