1/*
2 * Copyright (c) 2011, 2012, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26
27#import "PrinterSurfaceData.h"
28#import "jni_util.h"
29#import <JavaNativeFoundation/JavaNativeFoundation.h>
30
31
32//#define DEBUG 1
33#if defined DEBUG
34    #define PRINT(msg) {fprintf(stderr, "%s\n", msg);}
35#else
36    #define PRINT(msg) {}
37#endif
38
39static LockFunc PrintSD_Lock;
40static UnlockFunc PrintSD_Unlock;
41static GetRasInfoFunc PrintSD_GetRasInfo;
42static ReleaseFunc PrintSD_ReleaseRasInfo;
43static void flush(JNIEnv *env, QuartzSDOps *qsdo);
44
45static void PrintSD_startCGContext(JNIEnv *env, QuartzSDOps *qsdo, SDRenderType renderType)
46{
47PRINT(" PrintSD_startCGContext")
48
49    if (qsdo->cgRef != NULL)
50    {
51        flush(env, qsdo);
52
53        SetUpCGContext(env, qsdo, renderType);
54    }
55}
56
57static void PrintSD_finishCGContext(JNIEnv *env, QuartzSDOps *qsdo)
58{
59PRINT("    PrintSD_finishCGContext")
60
61    if (qsdo->cgRef != NULL)
62    {
63        CompleteCGContext(env, qsdo);
64    }
65}
66
67static void PrintSD_dispose(JNIEnv *env, SurfaceDataOps *sdo)
68{
69PRINT(" PrintSD_dispose")
70    QuartzSDOps *qsdo = (QuartzSDOps *)sdo;
71
72    (*env)->DeleteGlobalRef(env, qsdo->javaGraphicsStatesObjects);
73
74    if (qsdo->graphicsStateInfo.batchedLines != NULL)
75    {
76        free(qsdo->graphicsStateInfo.batchedLines);
77        qsdo->graphicsStateInfo.batchedLines = NULL;
78    }
79
80    qsdo->BeginSurface            = NULL;
81    qsdo->FinishSurface            = NULL;
82}
83
84JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CPrinterSurfaceData_initOps(JNIEnv *env, jobject jthis, jlong nsRef, jobject jGraphicsState, jobjectArray jGraphicsStateObject, jint width, jint height)
85{
86JNF_COCOA_ENTER(env);
87
88PRINT("Java_sun_lwawt_macosx_CPrinterSurfaceData_initOps")
89
90    PrintSDOps *psdo = (PrintSDOps*)SurfaceData_InitOps(env, jthis, sizeof(PrintSDOps));
91    if (psdo == NULL) {
92        JNU_ThrowOutOfMemoryError(env, "Initialization of SurfaceData failed.");
93        return;
94    }
95
96    psdo->nsRef            = (NSGraphicsContext*)jlong_to_ptr(nsRef);
97    psdo->width            = width;
98    psdo->height        = height;
99
100    QuartzSDOps *qsdo = (QuartzSDOps*)psdo;
101    qsdo->BeginSurface            = PrintSD_startCGContext;
102    qsdo->FinishSurface            = PrintSD_finishCGContext;
103    qsdo->cgRef                    = [psdo->nsRef graphicsPort];
104
105    qsdo->javaGraphicsStates            = (jint*)((*env)->GetDirectBufferAddress(env, jGraphicsState));
106    qsdo->javaGraphicsStatesObjects        = (*env)->NewGlobalRef(env, jGraphicsStateObject);
107
108    qsdo->graphicsStateInfo.batchedLines        = NULL;
109    qsdo->graphicsStateInfo.batchedLinesCount    = 0;
110
111    SurfaceDataOps *sdo = (SurfaceDataOps*)qsdo;
112    sdo->Lock        = PrintSD_Lock;
113    sdo->Unlock        = PrintSD_Unlock;
114    sdo->GetRasInfo    = PrintSD_GetRasInfo;
115    sdo->Release    = PrintSD_ReleaseRasInfo;
116    sdo->Setup        = NULL;
117    sdo->Dispose    = PrintSD_dispose;
118
119JNF_COCOA_EXIT(env);
120}
121
122static jint PrintSD_Lock(JNIEnv *env, SurfaceDataOps *sdo, SurfaceDataRasInfo *pRasInfo, jint lockflags)
123{
124PRINT(" PrintSD_Lock")
125    jint status = SD_FAILURE;
126
127    //QuartzSDOps *qsdo = (QuartzSDOps*)sdo;
128    //PrintSD_startCGContext(env, qsdo, SD_Image);
129
130    status = SD_SUCCESS;
131
132    return status;
133}
134static void PrintSD_Unlock(JNIEnv *env, SurfaceDataOps *sdo, SurfaceDataRasInfo *pRasInfo)
135{
136PRINT(" PrintSD_Unlock")
137
138    //QuartzSDOps *qsdo = (QuartzSDOps*)sdo;
139    //PrintSD_finishCGContext(env, qsdo);
140}
141static void PrintSD_GetRasInfo(JNIEnv *env, SurfaceDataOps *sdo, SurfaceDataRasInfo *pRasInfo)
142{
143PRINT(" PrintSD_GetRasInfo")
144    PrintSDOps *psdo = (PrintSDOps*)sdo;
145
146    pRasInfo->pixelStride = 4; // ARGB
147    pRasInfo->scanStride = psdo->width * pRasInfo->pixelStride;
148
149    pRasInfo->rasBase = NULL; //psdo->dataForSun2D;
150}
151static void PrintSD_ReleaseRasInfo(JNIEnv *env, SurfaceDataOps *sdo, SurfaceDataRasInfo *pRasInfo)
152{
153PRINT(" PrintSD_ReleaseRasInfo")
154
155    pRasInfo->pixelStride = 0;
156    pRasInfo->scanStride = 0;
157    pRasInfo->rasBase = NULL;
158}
159
160static void dataProvider_FreeSun2DPixels(void *info, const void *data, size_t size)
161{
162PRINT("dataProvider_FreeSun2DPixels")
163   // CGBitmapFreeData(info);
164    free(info);
165}
166JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CPrinterSurfaceData__1flush
167  (JNIEnv *env, jobject jsurfacedata)
168{
169    flush(env, (QuartzSDOps*)SurfaceData_GetOps(env, jsurfacedata));
170}
171static void flush(JNIEnv *env, QuartzSDOps *qsdo)
172{
173}
174