1 /*
   2  * Copyright (c) 2005, 2006, 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 #include "splashscreen_impl.h"
  27 #include "splashscreen_gfx_impl.h"
  28 
  29 int splashIsVisible = 0;
  30 
  31 Splash *
  32 SplashGetInstance()
  33 {
  34     static Splash splash;
  35     static int preInitialized = 0;
  36     if (!preInitialized) {
  37         memset(&splash, 0, sizeof(Splash));
  38         splash.currentFrame = -1;
  39         preInitialized = 1;
  40     }
  41     return &splash;
  42 }
  43 
  44 SPLASHEXPORT void
  45 SplashSetFileJarName(const char* fileName, const char* jarName) {
  46     Splash *splash = SplashGetInstance();
  47 
  48     free(splash->fileName);
  49     splash->fileName = SplashConvertStringAlloc(fileName, &splash->fileNameLen);
  50 
  51     free(splash->jarName);
  52     splash->jarName = SplashConvertStringAlloc(jarName, &splash->jarNameLen);
  53 }
  54 
  55 SPLASHEXPORT void
  56 SplashInit()
  57 {
  58     Splash *splash = SplashGetInstance();
  59 
  60     memset(splash, 0, sizeof(Splash));
  61     splash->currentFrame = -1;
  62     initFormat(&splash->imageFormat, QUAD_RED_MASK, QUAD_GREEN_MASK,
  63         QUAD_BLUE_MASK, QUAD_ALPHA_MASK);
  64     SplashInitPlatform(splash);
  65 }
  66 
  67 SPLASHEXPORT void
  68 SplashClose()
  69 {
  70     Splash *splash = SplashGetInstance();
  71 
  72     if (splash->isVisible > 0) {
  73         SplashLock(splash);
  74         splash->isVisible = -1;
  75         SplashClosePlatform(splash);
  76         SplashUnlock(splash);
  77     }
  78 }
  79 
  80 void
  81 SplashCleanup(Splash * splash)
  82 {
  83     int i;
  84 
  85     splash->currentFrame = -1;
  86     SplashCleanupPlatform(splash);
  87     if (splash->frames) {
  88         for (i = 0; i < splash->frameCount; i++) {
  89             if (splash->frames[i].bitmapBits) {
  90                 free(splash->frames[i].bitmapBits);
  91                 splash->frames[i].bitmapBits = NULL;
  92             }
  93         }
  94         free(splash->frames);
  95         splash->frames = NULL;
  96     }
  97     if (splash->overlayData) {
  98         free(splash->overlayData);
  99         splash->overlayData = NULL;
 100     }
 101     SplashSetFileJarName(NULL, NULL);
 102 }
 103 
 104 void
 105 SplashDone(Splash * splash)
 106 {
 107     SplashCleanup(splash);
 108     SplashDonePlatform(splash);
 109 }
 110 
 111 int
 112 SplashIsStillLooping(Splash * splash)
 113 {
 114     if (splash->currentFrame < 0) {
 115         return 0;
 116     }
 117     return splash->loopCount != 1 ||
 118         splash->currentFrame + 1 < splash->frameCount;
 119 }
 120 
 121 void
 122 SplashUpdateScreenData(Splash * splash)
 123 {
 124     ImageRect srcRect, dstRect;
 125     if (splash->currentFrame < 0) {
 126         return;
 127     }
 128 
 129     initRect(&srcRect, 0, 0, splash->width, splash->height, 1,
 130         splash->width * sizeof(rgbquad_t),
 131         splash->frames[splash->currentFrame].bitmapBits, &splash->imageFormat);
 132     if (splash->screenData) {
 133         free(splash->screenData);
 134     }
 135     splash->screenStride = splash->width * splash->screenFormat.depthBytes;
 136     if (splash->byteAlignment > 1) {
 137         splash->screenStride =
 138             (splash->screenStride + splash->byteAlignment - 1) &
 139             ~(splash->byteAlignment - 1);
 140     }
 141     splash->screenData = malloc(splash->height * splash->screenStride);
 142     initRect(&dstRect, 0, 0, splash->width, splash->height, 1,
 143         splash->screenStride, splash->screenData, &splash->screenFormat);
 144     if (splash->overlayData) {
 145         convertRect2(&srcRect, &dstRect, CVT_BLEND, &splash->overlayRect);
 146     }
 147     else {
 148         convertRect(&srcRect, &dstRect, CVT_COPY);
 149     }
 150 }
 151 
 152 void
 153 SplashNextFrame(Splash * splash)
 154 {
 155     if (splash->currentFrame < 0) {
 156         return;
 157     }
 158     do {
 159         if (!SplashIsStillLooping(splash)) {
 160             return;
 161         }
 162         splash->time += splash->frames[splash->currentFrame].delay;
 163         if (++splash->currentFrame >= splash->frameCount) {
 164             splash->currentFrame = 0;
 165             if (splash->loopCount > 0) {
 166                 splash->loopCount--;
 167             }
 168         }
 169     } while (splash->time + splash->frames[splash->currentFrame].delay -
 170         SplashTime() <= 0);
 171 }
 172 
 173 int
 174 BitmapToYXBandedRectangles(ImageRect * pSrcRect, RECT_T * out)
 175 {
 176     RECT_T *pPrevLine = NULL, *pFirst = out, *pThis = pFirst;
 177     int i, j, i0;
 178     int length;
 179 
 180     for (j = 0; j < pSrcRect->numLines; j++) {
 181 
 182         /* generate data for a scanline */
 183 
 184         byte_t *pSrc = (byte_t *) pSrcRect->pBits + j * pSrcRect->stride;
 185         RECT_T *pLine = pThis;
 186 
 187         i = 0;
 188 
 189         do {
 190             while (i < pSrcRect->numSamples &&
 191                    getRGBA(pSrc, pSrcRect->format) < ALPHA_THRESHOLD) {
 192                 pSrc += pSrcRect->depthBytes;
 193                 ++i;
 194             }
 195             if (i >= pSrcRect->numSamples) {
 196                 break;
 197             }
 198             i0 = i;
 199             while (i < pSrcRect->numSamples &&
 200                    getRGBA(pSrc, pSrcRect->format) >= ALPHA_THRESHOLD) {
 201                 pSrc += pSrcRect->depthBytes;
 202                 ++i;
 203             }
 204             RECT_SET(*pThis, i0, j, i - i0, 1);
 205             ++pThis;
 206         } while (i < pSrcRect->numSamples);
 207 
 208         /*  check if the previous scanline is exactly the same, merge if so
 209            (this is the only optimization we can use for YXBanded rectangles, and win32 supports
 210            YXBanded only */
 211 
 212         length = pThis - pLine;
 213         if (pPrevLine && pLine - pPrevLine == length) {
 214             for (i = 0; i < length && RECT_EQ_X(pPrevLine[i], pLine[i]); ++i) {
 215             }
 216             if (i == pLine - pPrevLine) {
 217                 // do merge
 218                 for (i = 0; i < length; i++) {
 219                     RECT_INC_HEIGHT(pPrevLine[i]);
 220                 }
 221                 pThis = pLine;
 222                 continue;
 223             }
 224         }
 225         /* or else use the generated scanline */
 226 
 227         pPrevLine = pLine;
 228     }
 229     return pThis - pFirst;
 230 }
 231 
 232 typedef struct FILEFORMAT
 233 {
 234     int sign;
 235     int (*decodeStream) (Splash * splash, SplashStream * stream);
 236 } FILEFORMAT;
 237 
 238 static const FILEFORMAT formats[] = {
 239     {0x47, SplashDecodeGifStream},
 240     {0x89, SplashDecodePngStream},
 241     {0xFF, SplashDecodeJpegStream}
 242 };
 243 
 244 static int
 245 SplashLoadStream(SplashStream * stream)
 246 {
 247     int success = 0;
 248     int c;
 249     size_t i;
 250 
 251     Splash *splash = SplashGetInstance();
 252     if (splash->isVisible < 0) {
 253         return 0;
 254     }
 255 
 256     SplashLock(splash);
 257 
 258     /* the formats we support can be easily distinguished by the first byte */
 259     c = stream->peek(stream);
 260     if (c != -1) {
 261         for (i = 0; i < sizeof(formats) / sizeof(FILEFORMAT); i++) {
 262             if (c == formats[i].sign) {
 263                 success = formats[i].decodeStream(splash, stream);
 264                 break;
 265             }
 266         }
 267     }
 268     stream->close(stream);
 269 
 270     if (!success) {             // failed to decode
 271         if (splash->isVisible == 0) {
 272             SplashCleanup(splash);
 273         }
 274         SplashUnlock(splash);   // SplashClose locks
 275         if (splash->isVisible == 0) {
 276             SplashClose();
 277         }
 278     }
 279     else {
 280         splash->currentFrame = 0;
 281         if (splash->isVisible == 0) {
 282             SplashStart(splash);
 283         } else {
 284             SplashReconfigure(splash);
 285             splash->time = SplashTime();
 286         }
 287         SplashUnlock(splash);
 288     }
 289     return success;
 290 }
 291 
 292 SPLASHEXPORT int
 293 SplashLoadFile(const char *filename)
 294 {
 295     SplashStream stream;
 296     return SplashStreamInitFile(&stream, filename) &&
 297                 SplashLoadStream(&stream);
 298 }
 299 
 300 SPLASHEXPORT int
 301 SplashLoadMemory(void *data, int size)
 302 {
 303     SplashStream stream;
 304     return SplashStreamInitMemory(&stream, data, size) &&
 305                 SplashLoadStream(&stream);
 306 }
 307 
 308 /* SplashStart MUST be called from under the lock */
 309 
 310 void
 311 SplashStart(Splash * splash)
 312 {
 313     if (splash->isVisible == 0) {
 314         SplashCreateThread(splash);
 315         splash->isVisible = 1;
 316     }
 317 }
 318 
 319 /* SplashStream functions */
 320 
 321 static int readFile(void* pStream, void* pData, int nBytes) {
 322     FILE* f = ((SplashStream*)pStream)->arg.stdio.f;
 323     return fread(pData, 1, nBytes, f);
 324 }
 325 static int peekFile(void* pStream) {
 326     FILE* f = ((SplashStream*)pStream)->arg.stdio.f;
 327     int c = fgetc(f);
 328     if (c != EOF) {
 329         ungetc(c, f);
 330         return c;
 331     } else {
 332         return -1;
 333     }
 334 }
 335 
 336 static void closeFile(void* pStream) {
 337     FILE* f = ((SplashStream*)pStream)->arg.stdio.f;
 338     fclose(f);
 339 }
 340 
 341 static int readMem(void* pStream, void* pData, int nBytes) {
 342     unsigned char* pSrc = (unsigned char*)(((SplashStream*)pStream)->arg.mem.pData);
 343     unsigned char* pSrcEnd = (unsigned char*)(((SplashStream*)pStream)->arg.mem.pDataEnd);
 344     if (nBytes > pSrcEnd - pSrc) {
 345         nBytes = pSrcEnd - pSrc;
 346     }
 347     if (nBytes>0) {
 348         memcpy(pData, pSrc, nBytes);
 349         pSrc += nBytes;
 350         ((SplashStream*)pStream)->arg.mem.pData = (void*)pSrc;
 351     }
 352     return nBytes;
 353 }
 354 
 355 static int peekMem(void* pStream) {
 356     unsigned char* pSrc = (unsigned char*)(((SplashStream*)pStream)->arg.mem.pData);
 357     unsigned char* pSrcEnd = (unsigned char*)(((SplashStream*)pStream)->arg.mem.pDataEnd);
 358     if (pSrc >= pSrcEnd) {
 359         return -1;
 360     } else {
 361         return (int)*pSrc;
 362     }
 363 }
 364 
 365 static void closeMem(void* pStream) {
 366 }
 367 
 368 int SplashStreamInitFile(SplashStream * pStream, const char* filename) {
 369     pStream->arg.stdio.f = fopen(filename, "rb");
 370     pStream->read = readFile;
 371     pStream->peek = peekFile;
 372     pStream->close = closeFile;
 373     return pStream->arg.stdio.f != 0;
 374 }
 375 
 376 int SplashStreamInitMemory(SplashStream * pStream, void* pData, int size) {
 377     pStream->arg.mem.pData = (unsigned char*)pData;
 378     pStream->arg.mem.pDataEnd = (unsigned char*)pData + size;
 379     pStream->read = readMem;
 380     pStream->peek = peekMem;
 381     pStream->close = closeMem;
 382     return 1;
 383 }