source: trunk/src/image/codec/ps3-image.cpp @ 2306

Last change on this file since 2306 was 2306, checked in by sam, 7 years ago

ps3: use network fallback for image files.

  • Property svn:keywords set to Id
File size: 4.9 KB
Line 
1//
2// Lol Engine
3//
4// Copyright: (c) 2010-2013 Sam Hocevar <sam@hocevar.net>
5//   This program is free software; you can redistribute it and/or
6//   modify it under the terms of the Do What The Fuck You Want To
7//   Public License, Version 2, as published by Sam Hocevar. See
8//   http://www.wtfpl.net/ for more details.
9//
10
11#if defined HAVE_CONFIG_H
12#   include "config.h"
13#endif
14
15#if defined __CELLOS_LV2__
16
17#include <cell/sysmodule.h>
18#include <cell/codec/pngdec.h>
19
20#include "core.h"
21#include "../../image/image-private.h"
22
23using namespace std;
24
25namespace lol
26{
27
28/*
29 * Image implementation class
30 */
31
32DECLARE_IMAGE_LOADER(Ps3ImageData, 100)
33{
34public:
35    virtual bool Open(char const *);
36    virtual bool Close();
37
38    virtual void *GetData() const;
39
40private:
41    static void* Malloc(uint32_t size, void* data) { return malloc(size); };
42    static int32_t Free(void* ptr, void* data) { free(ptr); return 0; };
43    uint8_t *pixels;
44};
45
46/*
47 * Public Image class
48 */
49
50bool Ps3ImageData::Open(char const *path)
51{
52    int32_t err;
53
54    /* Initialise decoding library */
55    CellPngDecMainHandle hmain;
56
57    err = cellSysmoduleLoadModule(CELL_SYSMODULE_FS);
58    if (err != CELL_OK)
59    {
60#if !LOL_RELEASE
61        Log::Error("could not open Fs sysmodule\n");
62#endif
63        return false;
64    }
65
66    err = cellSysmoduleLoadModule(CELL_SYSMODULE_PNGDEC);
67    if (err != CELL_OK)
68    {
69#if !LOL_RELEASE
70        Log::Error("could not open PngDec sysmodule\n");
71#endif
72        return false;
73    }
74
75    CellPngDecThreadInParam in_param;
76    in_param.spuThreadEnable = CELL_PNGDEC_SPU_THREAD_ENABLE;
77    in_param.ppuThreadPriority = 1000;
78    in_param.spuThreadPriority = 200;
79    in_param.cbCtrlMallocFunc = Ps3ImageData::Malloc;
80    in_param.cbCtrlMallocArg = NULL;
81    in_param.cbCtrlFreeFunc = Ps3ImageData::Free;
82    in_param.cbCtrlFreeArg = NULL;
83    CellPngDecThreadOutParam out_param;
84    err = cellPngDecCreate(&hmain, &in_param, &out_param);
85    if (err != CELL_OK)
86    {
87#if !LOL_RELEASE
88        Log::Error("could not create PngDec library\n");
89#endif
90        return false;
91    }
92
93    /* Create decoder */
94    CellPngDecSubHandle hsub;
95
96    String fullpath = String("/app_home/") + String(System::GetDataDir()) + String(path);
97
98    CellPngDecSrc dec_src;
99    dec_src.srcSelect = CELL_PNGDEC_FILE;
100    dec_src.fileName = &fullpath[0];
101    dec_src.fileOffset = 0;
102    dec_src.fileSize = 0;
103    dec_src.streamPtr = NULL;
104    dec_src.streamSize = 0;
105    dec_src.spuThreadEnable  = CELL_PNGDEC_SPU_THREAD_ENABLE;
106    CellPngDecOpnInfo open_info;
107    err = cellPngDecOpen(hmain, &hsub, &dec_src, &open_info);
108    if (err != CELL_OK)
109    {
110#if !LOL_RELEASE
111        Log::Error("could not open %s for decoding\n", &fullpath[0]);
112#endif
113        return false;
114    }
115
116    CellPngDecInfo info;
117    err = cellPngDecReadHeader(hmain, hsub, &info);
118    if (err != CELL_OK)
119    {
120#if !LOL_RELEASE
121        Log::Error("could not read image header in %s\n", &fullpath[0]);
122#endif
123        return false;
124    }
125
126    CellPngDecInParam in_dec_param;
127    in_dec_param.commandPtr = NULL;
128    in_dec_param.outputMode = CELL_PNGDEC_TOP_TO_BOTTOM;
129    in_dec_param.outputColorSpace = CELL_PNGDEC_RGBA;
130    in_dec_param.outputBitDepth = 8;
131    in_dec_param.outputPackFlag = CELL_PNGDEC_1BYTE_PER_1PIXEL;
132    in_dec_param.outputAlphaSelect = CELL_PNGDEC_STREAM_ALPHA;
133    in_dec_param.outputColorAlpha = 0xff;
134    CellPngDecOutParam out_dec_param;
135    err = cellPngDecSetParameter(hmain, hsub, &in_dec_param, &out_dec_param);
136    if (err != CELL_OK)
137    {
138#if !LOL_RELEASE
139        Log::Error("could not configure PngDec decoder\n");
140#endif
141        return false;
142    }
143
144    /* Decode image */
145    size = ivec2(info.imageWidth, info.imageHeight);
146    format = Image::FORMAT_RGBA;
147    pixels = (uint8_t *)malloc(info.imageWidth * 4 * info.imageHeight);
148    CellPngDecDataCtrlParam data_ctrl_param;
149    data_ctrl_param.outputBytesPerLine = info.imageWidth * 4;
150    CellPngDecDataOutInfo data_out_info;
151    err = cellPngDecDecodeData(hmain, hsub, pixels,
152                               &data_ctrl_param, &data_out_info);
153    if (err != CELL_OK)
154    {
155#if !LOL_RELEASE
156        Log::Error("could not run PngDec decoder on %s\n", &fullpath[0]);
157#endif
158        return false;
159    }
160
161    /* Close decoder */
162    err = cellPngDecClose(hmain, hsub);
163    if (err != CELL_OK)
164    {
165#if !LOL_RELEASE
166        Log::Error("could not close PngDec decoder\n");
167#endif
168        return false;
169    }
170
171    /* Deinitialise library */
172    err = cellPngDecDestroy(hmain);
173    if (err != CELL_OK)
174    {
175#if !LOL_RELEASE
176        Log::Error("could not destroy PngDec decoder\n");
177#endif
178        return false;
179    }
180    err = cellSysmoduleUnloadModule(CELL_SYSMODULE_PNGDEC);
181    err = cellSysmoduleUnloadModule(CELL_SYSMODULE_FS);
182
183    return true;
184}
185
186bool Ps3ImageData::Close()
187{
188    free(pixels);
189
190    return true;
191}
192
193void * Ps3ImageData::GetData() const
194{
195    return pixels;
196}
197
198} /* namespace lol */
199
200#endif /* defined __CELLOS_LV2__ */
201
Note: See TracBrowser for help on using the repository browser.