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
30//#define DEBUG 1
31#if defined DEBUG
32    #define PRINT(msg) {fprintf(stderr, "%s\n", msg);}
33#else
34    #define PRINT(msg) {}
35#endif
36
37static LockFunc PrintSD_Lock;
38static UnlockFunc PrintSD_Unlock;
39static GetRasInfoFunc PrintSD_GetRasInfo;
40static ReleaseFunc PrintSD_ReleaseRasInfo;
41static void flush(JNIEnv *env, QuartzSDOps *qsdo);
42
43static void PrintSD_startCGContext(JNIEnv *env, QuartzSDOps *qsdo, SDRenderType renderType)
44{
45PRINT(" PrintSD_startCGContext")
46
47    if (qsdo->cgRef != NULL)
48    {
49        flush(env, qsdo);
50
51        SetUpCGContext(env, qsdo, renderType);
52    }
53}
54
55static void PrintSD_finishCGContext(JNIEnv *env, QuartzSDOps *qsdo)
56{
57PRINT("    PrintSD_finishCGContext")
58
59    if (qsdo->cgRef != NULL)
60    {
61        CompleteCGContext(env, qsdo);
62    }
63}
64
65static void PrintSD_dispose(JNIEnv *env, SurfaceDataOps *sdo)
66{
67PRINT(" PrintSD_dispose")
68    QuartzSDOps *qsdo = (QuartzSDOps *)sdo;
69
70    (*env)->DeleteGlobalRef(env, qsdo->javaGraphicsStatesObjects);
71
72    if (qsdo->graphicsStateInfo.batchedLines != NULL)
73    {
74        free(qsdo->graphicsStateInfo.batchedLines);
75        qsdo->graphicsStateInfo.batchedLines = NULL;
76    }
77
78    qsdo->BeginSurface            = NULL;
79    qsdo->FinishSurface            = NULL;
80}
81
82JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CPrinterSurfaceData_initOps(JNIEnv *env, jobject jthis, jlong nsRef, jobject jGraphicsState, jobjectArray jGraphicsStateObject, jint width, jint height)
83{
84JNI_COCOA_ENTER(env);
85
86PRINT("Java_sun_lwawt_macosx_CPrinterSurfaceData_initOps")
87
88    PrintSDOps *psdo = (PrintSDOps*)SurfaceData_InitOps(env, jthis, sizeof(PrintSDOps));
89    if (psdo == NULL) {
90        JNU_ThrowOutOfMemoryError(env, "Initialization of SurfaceData failed.");
91        return;
92    }
93
94    psdo->nsRef            = (NSGraphicsContext*)jlong_to_ptr(nsRef);
95    psdo->width            = width;
96    psdo->height        = height;
97
98    QuartzSDOps *qsdo = (QuartzSDOps*)psdo;
99    qsdo->BeginSurface            = PrintSD_startCGContext;
100    qsdo->FinishSurface            = PrintSD_finishCGContext;
101    qsdo->cgRef                    = [psdo->nsRef graphicsPort];
102
103    qsdo->javaGraphicsStates            = (jint*)((*env)->GetDirectBufferAddress(env, jGraphicsState));
104    qsdo->javaGraphicsStatesObjects        = (*env)->NewGlobalRef(env, jGraphicsStateObject);
105
106    qsdo->graphicsStateInfo.batchedLines        = NULL;
107    qsdo->graphicsStateInfo.batchedLinesCount    = 0;
108
109    SurfaceDataOps *sdo = (SurfaceDataOps*)qsdo;
110    sdo->Lock        = PrintSD_Lock;
111    sdo->Unlock        = PrintSD_Unlock;
112    sdo->GetRasInfo    = PrintSD_GetRasInfo;
113    sdo->Release    = PrintSD_ReleaseRasInfo;
114    sdo->Setup        = NULL;
115    sdo->Dispose    = PrintSD_dispose;
116
117JNI_COCOA_EXIT(env);
118}
119
120static jint PrintSD_Lock(JNIEnv *env, SurfaceDataOps *sdo, SurfaceDataRasInfo *pRasInfo, jint lockflags)
121{
122PRINT(" PrintSD_Lock")
123    jint status = SD_FAILURE;
124
125    //QuartzSDOps *qsdo = (QuartzSDOps*)sdo;
126    //PrintSD_startCGContext(env, qsdo, SD_Image);
127
128    status = SD_SUCCESS;
129
130    return status;
131}
132static void PrintSD_Unlock(JNIEnv *env, SurfaceDataOps *sdo, SurfaceDataRasInfo *pRasInfo)
133{
134PRINT(" PrintSD_Unlock")
135
136    //QuartzSDOps *qsdo = (QuartzSDOps*)sdo;
137    //PrintSD_finishCGContext(env, qsdo);
138}
139static void PrintSD_GetRasInfo(JNIEnv *env, SurfaceDataOps *sdo, SurfaceDataRasInfo *pRasInfo)
140{
141PRINT(" PrintSD_GetRasInfo")
142    PrintSDOps *psdo = (PrintSDOps*)sdo;
143
144    pRasInfo->pixelStride = 4; // ARGB
145    pRasInfo->scanStride = psdo->width * pRasInfo->pixelStride;
146
147    pRasInfo->rasBase = NULL; //psdo->dataForSun2D;
148}
149static void PrintSD_ReleaseRasInfo(JNIEnv *env, SurfaceDataOps *sdo, SurfaceDataRasInfo *pRasInfo)
150{
151PRINT(" PrintSD_ReleaseRasInfo")
152
153    pRasInfo->pixelStride = 0;
154    pRasInfo->scanStride = 0;
155    pRasInfo->rasBase = NULL;
156}
157
158static void dataProvider_FreeSun2DPixels(void *info, const void *data, size_t size)
159{
160PRINT("dataProvider_FreeSun2DPixels")
161   // CGBitmapFreeData(info);
162    free(info);
163}
164JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CPrinterSurfaceData__1flush
165  (JNIEnv *env, jobject jsurfacedata)
166{
167    flush(env, (QuartzSDOps*)SurfaceData_GetOps(env, jsurfacedata));
168}
169static void flush(JNIEnv *env, QuartzSDOps *qsdo)
170{
171}
172