893N/A/*
2362N/A * Copyright (c) 2007, 2008, Oracle and/or its affiliates. All rights reserved.
893N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
893N/A *
893N/A * This code is free software; you can redistribute it and/or modify it
893N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
893N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
893N/A *
893N/A * This code is distributed in the hope that it will be useful, but WITHOUT
893N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
893N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
893N/A * version 2 for more details (a copy is included in the LICENSE file that
893N/A * accompanied this code).
893N/A *
893N/A * You should have received a copy of the GNU General Public License version
893N/A * 2 along with this work; if not, write to the Free Software Foundation,
893N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
893N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
893N/A */
893N/A
893N/A#include <malloc.h>
893N/A#include <string.h>
893N/A
893N/A#include "ShaderList.h"
893N/A#include "Trace.h"
893N/A
893N/A/**
893N/A * Creates a new ShaderInfo that wraps the given fragment program handle
893N/A * and related data and stores it at the front of the provided ShaderList.
893N/A * If the addition causes the ShaderList to outgrow its defined capacity,
893N/A * the least-recently used item in the list (including its fragment program
1580N/A * object) will be disposed.
1580N/A */
893N/Avoid
893N/AShaderList_AddProgram(ShaderList *programList,
893N/A jlong programID,
893N/A jint compType, jint compMode, jint flags)
893N/A{
893N/A ShaderInfo *info;
893N/A
893N/A J2dTraceLn(J2D_TRACE_INFO, "ShaderList_AddProgram");
893N/A
893N/A // create new ShaderInfo
1580N/A info = (ShaderInfo *)malloc(sizeof(ShaderInfo));
893N/A if (info == NULL) {
893N/A J2dTraceLn(J2D_TRACE_ERROR,
893N/A "OGLContext_AddProgram: could not allocate ShaderInfo");
893N/A return;
893N/A }
893N/A
893N/A // fill in the information
893N/A info->next = programList->head;
893N/A info->programID = programID;
893N/A info->compType = compType;
893N/A info->compMode = compMode;
893N/A info->flags = flags;
893N/A
893N/A // insert it at the head of the list
893N/A programList->head = info;
893N/A
893N/A // run through the list and see if we need to delete the least
893N/A // recently used item
893N/A {
893N/A int i = 1;
893N/A ShaderInfo *prev = NULL;
893N/A ShaderInfo *curr = info->next;
893N/A while (curr != NULL) {
893N/A if (i >= programList->maxItems) {
893N/A prev->next = NULL;
893N/A programList->dispose(curr->programID);
893N/A free(curr);
893N/A break;
893N/A }
893N/A i++;
893N/A prev = curr;
893N/A curr = curr->next;
893N/A }
893N/A }
893N/A}
893N/A
893N/A/**
893N/A * Locates a fragment program handle given a list of shader programs
893N/A * (ShaderInfos), using the provided composite state and flags as search
893N/A * parameters. The "flags" parameter is a bitwise-or'd value that helps
893N/A * differentiate one program for another; the interpretation of this value
893N/A * varies depending on the type of shader (BufImgOp, Paint, etc) but here
893N/A * it is only used to find another ShaderInfo with that same "flags" value.
1580N/A * If no matching program can be located, this method returns 0.
1580N/A */
1580N/Ajlong
1580N/AShaderList_FindProgram(ShaderList *programList,
1580N/A jint compType, jint compMode, jint flags)
1580N/A{
1580N/A ShaderInfo *prev = NULL;
893N/A ShaderInfo *info = programList->head;
893N/A
893N/A J2dTraceLn(J2D_TRACE_INFO, "ShaderList_FindProgram");
893N/A
893N/A while (info != NULL) {
893N/A if (compType == info->compType &&
893N/A compMode == info->compMode &&
893N/A flags == info->flags)
893N/A {
893N/A // it's a match: move it to the front of the list (if it's not
893N/A // there already) and patch up the links
893N/A if (info != programList->head) {
893N/A prev->next = info->next;
893N/A info->next = programList->head;
893N/A programList->head = info;
893N/A }
893N/A return info->programID;
893N/A }
893N/A prev = info;
893N/A info = info->next;
893N/A }
893N/A return 0;
893N/A}
893N/A
893N/A/**
893N/A * Disposes all entries (and their associated shader program objects)
893N/A * contained in the given ShaderList.
893N/A */
893N/Avoid
893N/AShaderList_Dispose(ShaderList *programList)
893N/A{
893N/A ShaderInfo *info = programList->head;
893N/A
893N/A J2dTraceLn(J2D_TRACE_INFO, "ShaderList_Dispose");
893N/A
893N/A while (info != NULL) {
893N/A ShaderInfo *tmp = info->next;
893N/A programList->dispose(info->programID);
893N/A free(info);
893N/A info = tmp;
893N/A }
893N/A
893N/A programList->head = NULL;
893N/A}
893N/A