SDL_render_ngage.cpp 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2026 Sam Lantinga <slouken@libsdl.org>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #ifdef __cplusplus
  19. extern "C" {
  20. #endif
  21. #include "../../SDL_hints_c.h"
  22. #include "../../events/SDL_keyboard_c.h"
  23. #include "../SDL_sysrender.h"
  24. #include "SDL_internal.h"
  25. #include "SDL_render_ngage_c.h"
  26. #ifdef __cplusplus
  27. }
  28. #endif
  29. #ifdef SDL_VIDEO_RENDER_NGAGE
  30. #include "SDL_render_ngage_c.hpp"
  31. #include "SDL_render_ops.hpp"
  32. const TUint32 WindowClientHandle = 0x571D0A;
  33. extern CRenderer *gRenderer;
  34. #ifdef __cplusplus
  35. extern "C" {
  36. #endif
  37. void NGAGE_Clear(const Uint32 color)
  38. {
  39. gRenderer->Clear(color);
  40. }
  41. bool NGAGE_Copy(SDL_Renderer *renderer, SDL_Texture *texture, SDL_Rect *srcrect, SDL_Rect *dstrect)
  42. {
  43. return gRenderer->Copy(renderer, texture, srcrect, dstrect);
  44. }
  45. bool NGAGE_CopyEx(SDL_Renderer *renderer, SDL_Texture *texture, NGAGE_CopyExData *copydata)
  46. {
  47. return gRenderer->CopyEx(renderer, texture, copydata);
  48. }
  49. bool NGAGE_CreateTextureData(NGAGE_TextureData *data, const int width, const int height, const int access)
  50. {
  51. return gRenderer->CreateTextureData(data, width, height, access);
  52. }
  53. void NGAGE_DestroyTextureData(NGAGE_TextureData *data)
  54. {
  55. if (data) {
  56. if (data->gc) {
  57. delete data->gc;
  58. data->gc = NULL;
  59. }
  60. if (data->device) {
  61. delete data->device;
  62. data->device = NULL;
  63. }
  64. if (data->mask_bitmap) {
  65. delete data->mask_bitmap;
  66. data->mask_bitmap = NULL;
  67. }
  68. delete data->bitmap;
  69. data->bitmap = NULL;
  70. }
  71. }
  72. void *NGAGE_GetBitmapDataAddress(NGAGE_TextureData *data)
  73. {
  74. if (!data || !data->bitmap) {
  75. return NULL;
  76. }
  77. return data->bitmap->DataAddress();
  78. }
  79. int NGAGE_GetBitmapScanLineLength(NGAGE_TextureData *data)
  80. {
  81. if (!data || !data->bitmap) {
  82. return 0;
  83. }
  84. return (int)CFbsBitmap::ScanLineLength(data->bitmap->SizeInPixels().iWidth, EColor4K);
  85. }
  86. void NGAGE_DrawLines(NGAGE_Vertex *verts, const int count)
  87. {
  88. gRenderer->DrawLines(verts, count);
  89. }
  90. void NGAGE_DrawPoints(NGAGE_Vertex *verts, const int count)
  91. {
  92. gRenderer->DrawPoints(verts, count);
  93. }
  94. void NGAGE_DrawGeometry(NGAGE_Vertex *verts, const int count)
  95. {
  96. gRenderer->DrawGeometry(verts, count);
  97. }
  98. void NGAGE_FillRects(NGAGE_Vertex *verts, const int count)
  99. {
  100. gRenderer->FillRects(verts, count);
  101. }
  102. void NGAGE_Flip()
  103. {
  104. gRenderer->Flip();
  105. }
  106. void NGAGE_SetClipRect(const SDL_Rect *rect)
  107. {
  108. gRenderer->SetClipRect(rect->x, rect->y, rect->w, rect->h);
  109. }
  110. void NGAGE_SetDrawColor(const Uint32 color)
  111. {
  112. if (gRenderer) {
  113. gRenderer->SetDrawColor(color);
  114. }
  115. }
  116. void NGAGE_PumpEventsInternal()
  117. {
  118. gRenderer->PumpEvents();
  119. }
  120. void NGAGE_SuspendScreenSaverInternal(bool suspend)
  121. {
  122. gRenderer->SuspendScreenSaver(suspend);
  123. }
  124. void NGAGE_SetRenderTargetInternal(NGAGE_TextureData *target)
  125. {
  126. if (gRenderer) {
  127. gRenderer->SetRenderTarget(target);
  128. }
  129. }
  130. static void SDLCALL NGAGE_ShowFPSChanged(void *userdata, const char *name, const char *oldValue, const char *newValue)
  131. {
  132. CRenderer *renderer = (CRenderer *)userdata;
  133. renderer->SetShowFPS(SDL_GetStringBoolean(newValue, false));
  134. }
  135. void *NGAGE_GetBackbufferAddress(void)
  136. {
  137. return gRenderer->GetCurrentBitmap()->DataAddress();
  138. }
  139. int NGAGE_GetBackbufferPitch(void)
  140. {
  141. return CFbsBitmap::ScanLineLength(NGAGE_SCREEN_WIDTH, EColor4K) / 2;
  142. }
  143. #ifdef __cplusplus
  144. }
  145. #endif
  146. CRenderer *CRenderer::NewL()
  147. {
  148. CRenderer *self = new (ELeave) CRenderer();
  149. CleanupStack::PushL(self);
  150. self->ConstructL();
  151. CleanupStack::Pop(self);
  152. return self;
  153. }
  154. CRenderer::CRenderer() : iRenderer(0), iDirectScreen(0), iScreenGc(0), iWsSession(), iWsWindowGroup(), iWsWindowGroupID(0), iWsWindow(), iWsScreen(0), iWsEventStatus(), iWsEvent(), iShowFPS(EFalse), iFPS(0), iFont(0), iCurrentRenderTarget(0), iPixelBufferA(0), iPixelBufferB(0), iPixelBufferSize(0), iScratchBitmap(0), iMaskBitmap(0), iPointsBuffer(0), iPointsBufferSize(0) {}
  155. CRenderer::~CRenderer()
  156. {
  157. SDL_RemoveHintCallback(SDL_HINT_RENDER_NGAGE_SHOW_FPS, NGAGE_ShowFPSChanged, this);
  158. delete iRenderer;
  159. iRenderer = 0;
  160. SDL_free(iPixelBufferA);
  161. SDL_free(iPixelBufferB);
  162. delete iScratchBitmap;
  163. iScratchBitmap = 0;
  164. delete iMaskBitmap;
  165. iMaskBitmap = 0;
  166. delete[] iPointsBuffer;
  167. }
  168. void CRenderer::ConstructL()
  169. {
  170. TInt error = KErrNone;
  171. error = iWsSession.Connect();
  172. if (error != KErrNone) {
  173. SDL_Log("Failed to connect to window server: %d", error);
  174. User::Leave(error);
  175. }
  176. iWsScreen = new (ELeave) CWsScreenDevice(iWsSession);
  177. error = iWsScreen->Construct();
  178. if (error != KErrNone) {
  179. SDL_Log("Failed to construct screen device: %d", error);
  180. User::Leave(error);
  181. }
  182. iWsWindowGroup = RWindowGroup(iWsSession);
  183. error = iWsWindowGroup.Construct(WindowClientHandle);
  184. if (error != KErrNone) {
  185. SDL_Log("Failed to construct window group: %d", error);
  186. User::Leave(error);
  187. }
  188. iWsWindowGroup.SetOrdinalPosition(0);
  189. RProcess thisProcess;
  190. TParse exeName;
  191. exeName.Set(thisProcess.FileName(), NULL, NULL);
  192. TBuf<32> winGroupName;
  193. winGroupName.Append(0);
  194. winGroupName.Append(0);
  195. winGroupName.Append(0); // UID
  196. winGroupName.Append(0);
  197. winGroupName.Append(exeName.Name()); // Caption
  198. winGroupName.Append(0);
  199. winGroupName.Append(0); // DOC name
  200. iWsWindowGroup.SetName(winGroupName);
  201. iWsWindow = RWindow(iWsSession);
  202. error = iWsWindow.Construct(iWsWindowGroup, WindowClientHandle - 1);
  203. if (error != KErrNone) {
  204. SDL_Log("Failed to construct window: %d", error);
  205. User::Leave(error);
  206. }
  207. iWsWindow.SetBackgroundColor(KRgbWhite);
  208. iWsWindow.SetRequiredDisplayMode(EColor4K);
  209. iWsWindow.Activate();
  210. iWsWindow.SetSize(iWsScreen->SizeInPixels());
  211. iWsWindow.SetVisible(ETrue);
  212. iWsWindowGroupID = iWsWindowGroup.Identifier();
  213. TRAPD(errc, iRenderer = iRenderer->NewL());
  214. if (errc != KErrNone) {
  215. SDL_Log("Failed to create renderer: %d", errc);
  216. return;
  217. }
  218. iDirectScreen = CDirectScreenAccess::NewL(
  219. iWsSession,
  220. *(iWsScreen),
  221. iWsWindow, *this);
  222. // Select font.
  223. TFontSpec fontSpec(_L("LatinBold12"), 12);
  224. TInt errd = iWsScreen->GetNearestFontInTwips((CFont *&)iFont, fontSpec);
  225. if (errd != KErrNone) {
  226. SDL_Log("Failed to get font: %d", errd);
  227. return;
  228. }
  229. // Activate events.
  230. iWsEventStatus = KRequestPending;
  231. iWsSession.EventReady(&iWsEventStatus);
  232. DisableKeyBlocking();
  233. iIsFocused = ETrue;
  234. iShowFPS = EFalse;
  235. iSuspendScreenSaver = EFalse;
  236. if (!iDirectScreen->IsActive()) {
  237. TRAPD(err, iDirectScreen->StartL());
  238. if (KErrNone != err) {
  239. return;
  240. }
  241. iDirectScreen->ScreenDevice()->SetAutoUpdate(ETrue);
  242. }
  243. SDL_AddHintCallback(SDL_HINT_RENDER_NGAGE_SHOW_FPS, NGAGE_ShowFPSChanged, this);
  244. }
  245. void CRenderer::Restart(RDirectScreenAccess::TTerminationReasons aReason)
  246. {
  247. if (!iDirectScreen->IsActive()) {
  248. TRAPD(err, iDirectScreen->StartL());
  249. if (KErrNone != err) {
  250. return;
  251. }
  252. iDirectScreen->ScreenDevice()->SetAutoUpdate(ETrue);
  253. }
  254. }
  255. void CRenderer::AbortNow(RDirectScreenAccess::TTerminationReasons aReason)
  256. {
  257. if (iDirectScreen->IsActive()) {
  258. iDirectScreen->Cancel();
  259. }
  260. }
  261. void CRenderer::Clear(TUint32 iColor)
  262. {
  263. CFbsBitGc *gc = GetCurrentGc();
  264. if (gc) {
  265. gc->SetBrushColor(iColor);
  266. gc->Clear();
  267. }
  268. }
  269. #ifdef __cplusplus
  270. extern "C" {
  271. #endif
  272. Uint32 NGAGE_ConvertColor(float r, float g, float b, float a, float color_scale)
  273. {
  274. TFixed ff = 255 << 16; // 255.f
  275. TFixed scalef = Real2Fix(color_scale);
  276. TFixed rf = Real2Fix(r);
  277. TFixed gf = Real2Fix(g);
  278. TFixed bf = Real2Fix(b);
  279. TFixed af = Real2Fix(a);
  280. rf = FixMul(rf, scalef);
  281. gf = FixMul(gf, scalef);
  282. bf = FixMul(bf, scalef);
  283. rf = SDL_clamp(rf, 0, ff);
  284. gf = SDL_clamp(gf, 0, ff);
  285. bf = SDL_clamp(bf, 0, ff);
  286. af = SDL_clamp(af, 0, ff);
  287. rf = FixMul(rf, ff) >> 16;
  288. gf = FixMul(gf, ff) >> 16;
  289. bf = FixMul(bf, ff) >> 16;
  290. af = FixMul(af, ff) >> 16;
  291. return (af << 24) | (bf << 16) | (gf << 8) | rf;
  292. }
  293. #ifdef __cplusplus
  294. }
  295. #endif
  296. bool CRenderer::Copy(SDL_Renderer *renderer, SDL_Texture *texture, const SDL_Rect *srcrect, const SDL_Rect *dstrect)
  297. {
  298. if (!texture) {
  299. return false;
  300. }
  301. NGAGE_TextureData *phdata = (NGAGE_TextureData *)texture->internal;
  302. if (!phdata || !phdata->bitmap) {
  303. return false;
  304. }
  305. SDL_FColor *c = &texture->color;
  306. const int bytes_per_pixel = 2;
  307. int sw = srcrect->w;
  308. int sh = srcrect->h;
  309. // Fast path: render target texture with no color mod.
  310. // BitBlt directly from its bitmap — DataAddress() is unreliable
  311. // for bitmaps that have been drawn into via a CFbsBitGc.
  312. bool no_color_mod = (c->a == 1.f && c->r == 1.f && c->g == 1.f && c->b == 1.f);
  313. float sx, sy;
  314. SDL_GetRenderScale(renderer, &sx, &sy);
  315. bool no_scale = (sx == 1.f && sy == 1.f);
  316. SDL_BlendMode blend;
  317. SDL_GetTextureBlendMode(texture, &blend);
  318. bool no_color_key = (blend != SDL_BLENDMODE_BLEND);
  319. if (phdata->gc && no_color_mod && no_scale && no_color_key) {
  320. CFbsBitGc *gc = GetCurrentGc();
  321. if (gc) {
  322. TRect aSource(TPoint(srcrect->x, srcrect->y), TSize(sw, sh));
  323. TPoint aDest(dstrect->x, dstrect->y);
  324. gc->BitBlt(aDest, phdata->bitmap, aSource);
  325. }
  326. return true;
  327. }
  328. // Fast path: color-key with no color mod and no scale.
  329. // Blit directly from the source bitmap into the destination, skipping transparent pixels.
  330. if (no_color_mod && no_scale && !no_color_key && phdata->has_color_key) {
  331. void *tex_data_ck = phdata->bitmap->DataAddress();
  332. CFbsBitmap *dst_bmp = GetCurrentBitmap();
  333. if (dst_bmp && tex_data_ck) {
  334. int tex_stride_ck = CFbsBitmap::ScanLineLength(phdata->bitmap->SizeInPixels().iWidth, EColor4K) / 2;
  335. TUint16 *src_base = static_cast<TUint16 *>(tex_data_ck) + srcrect->y * tex_stride_ck + srcrect->x;
  336. BlitWithAlphaKey(dst_bmp, dstrect->x, dstrect->y, src_base, sw, sh, tex_stride_ck);
  337. }
  338. return true;
  339. }
  340. int src_pitch = sw * bytes_per_pixel;
  341. int tex_pitch = CFbsBitmap::ScanLineLength(texture->w, EColor4K);
  342. void *tex_data = phdata->bitmap->DataAddress();
  343. if (!tex_data) {
  344. return false;
  345. }
  346. TInt required_size = src_pitch * sh;
  347. if (required_size > iPixelBufferSize) {
  348. void *new_buffer_a = SDL_realloc(iPixelBufferA, required_size);
  349. if (!new_buffer_a) {
  350. return false;
  351. }
  352. iPixelBufferA = new_buffer_a;
  353. void *new_buffer_b = SDL_realloc(iPixelBufferB, required_size);
  354. if (!new_buffer_b) {
  355. return false;
  356. }
  357. iPixelBufferB = new_buffer_b;
  358. iPixelBufferSize = required_size;
  359. }
  360. // Ensure scratch bitmap is allocated and large enough.
  361. if (!iScratchBitmap) {
  362. iScratchBitmap = new CFbsBitmap();
  363. if (!iScratchBitmap) {
  364. return false;
  365. }
  366. }
  367. TSize scratch_size = iScratchBitmap->SizeInPixels();
  368. if (scratch_size.iWidth < sw || scratch_size.iHeight < sh) {
  369. iScratchBitmap->Reset();
  370. TInt err = iScratchBitmap->Create(TSize(sw, sh), EColor4K);
  371. if (err != KErrNone) {
  372. return false;
  373. }
  374. }
  375. // Extract the srcrect region from the texture into buffer A.
  376. {
  377. TUint16 *tex_pixels = (TUint16 *)tex_data;
  378. TUint16 *buf_pixels = (TUint16 *)iPixelBufferA;
  379. int tex_pitch_u16 = tex_pitch / 2;
  380. for (int y = 0; y < sh; ++y) {
  381. TUint16 *src_row = tex_pixels + (srcrect->y + y) * tex_pitch_u16 + srcrect->x;
  382. TUint16 *dst_row = buf_pixels + y * sw;
  383. Mem::Copy(dst_row, src_row, src_pitch);
  384. }
  385. }
  386. void *source = iPixelBufferA;
  387. void *dest = iPixelBufferB;
  388. if (!no_color_mod) {
  389. ApplyColorMod(dest, source, src_pitch, sw, sh, texture->color);
  390. void *tmp = source;
  391. source = dest;
  392. dest = tmp;
  393. }
  394. if (!no_scale) {
  395. TFixed scale_x = Real2Fix(sx);
  396. TFixed scale_y = Real2Fix(sy);
  397. TFixed center_x = Int2Fix(sw / 2);
  398. TFixed center_y = Int2Fix(sh / 2);
  399. ApplyScale(dest, source, src_pitch, sw, sh, center_x, center_y, scale_x, scale_y);
  400. void *tmp = source;
  401. source = dest;
  402. dest = tmp;
  403. }
  404. // Copy result into scratch bitmap and blit from there.
  405. // The source texture is never modified.
  406. {
  407. TUint16 *scratch_pixels = (TUint16 *)iScratchBitmap->DataAddress();
  408. TUint16 *res_pixels = (TUint16 *)source;
  409. int scratch_pitch_u16 = CFbsBitmap::ScanLineLength(iScratchBitmap->SizeInPixels().iWidth, EColor4K) / 2;
  410. // Always copy all pixels into the scratch bitmap.
  411. for (int y = 0; y < sh; ++y) {
  412. TUint16 *dst_row = scratch_pixels + y * scratch_pitch_u16;
  413. TUint16 *src_row = res_pixels + y * sw;
  414. Mem::Copy(dst_row, src_row, src_pitch);
  415. }
  416. CFbsBitGc *gc = GetCurrentGc();
  417. if (gc) {
  418. TRect aSource(TPoint(0, 0), TSize(sw, sh));
  419. TPoint aDest(dstrect->x, dstrect->y);
  420. if (!no_color_key && phdata->has_color_key) {
  421. CFbsBitmap *dst_bmp = GetCurrentBitmap();
  422. if (dst_bmp) {
  423. BlitWithAlphaKey(dst_bmp, dstrect->x, dstrect->y, res_pixels, sw, sh, sw);
  424. }
  425. } else {
  426. gc->BitBlt(aDest, iScratchBitmap, aSource);
  427. }
  428. }
  429. }
  430. return true;
  431. }
  432. bool CRenderer::CopyEx(SDL_Renderer *renderer, SDL_Texture *texture, const NGAGE_CopyExData *copydata)
  433. {
  434. NGAGE_TextureData *phdata = (NGAGE_TextureData *)texture->internal;
  435. if (!phdata || !phdata->bitmap) {
  436. return false;
  437. }
  438. SDL_FColor *c = &texture->color;
  439. const int bytes_per_pixel = 2;
  440. int sw = copydata->srcrect.w;
  441. int sh = copydata->srcrect.h;
  442. int src_pitch = sw * bytes_per_pixel;
  443. int tex_pitch = CFbsBitmap::ScanLineLength(texture->w, EColor4K);
  444. void *tex_data = phdata->bitmap->DataAddress();
  445. if (!tex_data) {
  446. return false;
  447. }
  448. TInt required_size = src_pitch * sh;
  449. if (required_size > iPixelBufferSize) {
  450. void *new_buffer_a = SDL_realloc(iPixelBufferA, required_size);
  451. if (!new_buffer_a) {
  452. return false;
  453. }
  454. iPixelBufferA = new_buffer_a;
  455. void *new_buffer_b = SDL_realloc(iPixelBufferB, required_size);
  456. if (!new_buffer_b) {
  457. return false;
  458. }
  459. iPixelBufferB = new_buffer_b;
  460. iPixelBufferSize = required_size;
  461. }
  462. // Ensure scratch bitmap is allocated and large enough for the srcrect.
  463. if (!iScratchBitmap) {
  464. iScratchBitmap = new CFbsBitmap();
  465. if (!iScratchBitmap) {
  466. return false;
  467. }
  468. }
  469. TSize scratch_size = iScratchBitmap->SizeInPixels();
  470. if (scratch_size.iWidth < sw || scratch_size.iHeight < sh) {
  471. iScratchBitmap->Reset();
  472. TInt err = iScratchBitmap->Create(TSize(sw, sh), EColor4K);
  473. if (err != KErrNone) {
  474. return false;
  475. }
  476. }
  477. // Extract the srcrect region from the texture into buffer A.
  478. {
  479. TUint16 *tex_pixels = (TUint16 *)tex_data;
  480. TUint16 *buf_pixels = (TUint16 *)iPixelBufferA;
  481. int tex_pitch_u16 = tex_pitch / 2;
  482. for (int y = 0; y < sh; ++y) {
  483. TUint16 *src_row = tex_pixels + (copydata->srcrect.y + y) * tex_pitch_u16 + copydata->srcrect.x;
  484. TUint16 *dst_row = buf_pixels + y * sw;
  485. Mem::Copy(dst_row, src_row, src_pitch);
  486. }
  487. }
  488. void *source = iPixelBufferA;
  489. void *dest = iPixelBufferB;
  490. if (copydata->flip) {
  491. ApplyFlip(dest, source, src_pitch, sw, sh, copydata->flip);
  492. void *tmp = source;
  493. source = dest;
  494. dest = tmp;
  495. }
  496. if (copydata->scale_x != 1.f || copydata->scale_y != 1.f) {
  497. ApplyScale(dest, source, src_pitch, sw, sh, copydata->center.x, copydata->center.y, copydata->scale_x, copydata->scale_y);
  498. void *tmp = source;
  499. source = dest;
  500. dest = tmp;
  501. }
  502. if (copydata->angle) {
  503. ApplyRotation(dest, source, src_pitch, sw, sh, copydata->center.x, copydata->center.y, copydata->angle);
  504. void *tmp = source;
  505. source = dest;
  506. dest = tmp;
  507. }
  508. if (c->a != 1.f || c->r != 1.f || c->g != 1.f || c->b != 1.f) {
  509. ApplyColorMod(dest, source, src_pitch, sw, sh, texture->color);
  510. void *tmp = source;
  511. source = dest;
  512. dest = tmp;
  513. }
  514. // Copy the final result into the scratch bitmap and blit from there.
  515. // The source texture is never modified.
  516. {
  517. SDL_BlendMode blend;
  518. SDL_GetTextureBlendMode(texture, &blend);
  519. bool has_color_key = (blend == SDL_BLENDMODE_BLEND);
  520. TUint16 *scratch_pixels = (TUint16 *)iScratchBitmap->DataAddress();
  521. TUint16 *res_pixels = (TUint16 *)source;
  522. int scratch_pitch_u16 = CFbsBitmap::ScanLineLength(iScratchBitmap->SizeInPixels().iWidth, EColor4K) / 2;
  523. // Always copy all pixels into the scratch bitmap.
  524. for (int y = 0; y < sh; ++y) {
  525. TUint16 *dst_row = scratch_pixels + y * scratch_pitch_u16;
  526. TUint16 *src_row = res_pixels + y * sw;
  527. Mem::Copy(dst_row, src_row, src_pitch);
  528. }
  529. CFbsBitGc *gc = GetCurrentGc();
  530. if (gc) {
  531. TRect aSource(TPoint(0, 0), TSize(sw, sh));
  532. TPoint aDest(copydata->dstrect.x, copydata->dstrect.y);
  533. if (has_color_key && phdata->has_color_key) {
  534. CFbsBitmap *dst_bmp = GetCurrentBitmap();
  535. if (dst_bmp) {
  536. BlitWithAlphaKey(dst_bmp, copydata->dstrect.x, copydata->dstrect.y, res_pixels, sw, sh, sw);
  537. }
  538. } else {
  539. gc->BitBlt(aDest, iScratchBitmap, aSource);
  540. }
  541. }
  542. }
  543. return true;
  544. }
  545. bool CRenderer::CreateTextureData(NGAGE_TextureData *aTextureData, const TInt aWidth, const TInt aHeight, const TInt aAccess)
  546. {
  547. if (!aTextureData) {
  548. return false;
  549. }
  550. aTextureData->bitmap = new CFbsBitmap();
  551. if (!aTextureData->bitmap) {
  552. return false;
  553. }
  554. TInt error = aTextureData->bitmap->Create(TSize(aWidth, aHeight), EColor4K);
  555. if (error != KErrNone) {
  556. delete aTextureData->bitmap;
  557. aTextureData->bitmap = NULL;
  558. return false;
  559. }
  560. if (aAccess == SDL_TEXTUREACCESS_TARGET) {
  561. TRAPD(err1, aTextureData->device = CFbsBitmapDevice::NewL(aTextureData->bitmap));
  562. if (err1 != KErrNone || !aTextureData->device) {
  563. delete aTextureData->bitmap;
  564. aTextureData->bitmap = NULL;
  565. return false;
  566. }
  567. TRAPD(err2, aTextureData->gc = CFbsBitGc::NewL());
  568. if (err2 != KErrNone || !aTextureData->gc) {
  569. delete aTextureData->device;
  570. aTextureData->device = NULL;
  571. delete aTextureData->bitmap;
  572. aTextureData->bitmap = NULL;
  573. return false;
  574. }
  575. aTextureData->gc->Activate(aTextureData->device);
  576. } else {
  577. aTextureData->gc = NULL;
  578. aTextureData->device = NULL;
  579. }
  580. return true;
  581. }
  582. void CRenderer::DrawLines(NGAGE_Vertex *aVerts, const TInt aCount)
  583. {
  584. CFbsBitGc *gc = GetCurrentGc();
  585. if (gc) {
  586. gc->SetPenStyle(CGraphicsContext::ESolidPen);
  587. // Draw lines as pairs of points (start, end)
  588. for (TInt i = 0; i < aCount - 1; i += 2) {
  589. TPoint start(aVerts[i].x, aVerts[i].y);
  590. TPoint end(aVerts[i + 1].x, aVerts[i + 1].y);
  591. TRgb color = TRgb(aVerts[i].color.r, aVerts[i].color.g, aVerts[i].color.b);
  592. gc->SetPenColor(color);
  593. gc->DrawLine(start, end);
  594. }
  595. }
  596. }
  597. void CRenderer::DrawPoints(NGAGE_Vertex *aVerts, const TInt aCount)
  598. {
  599. CFbsBitGc *gc = GetCurrentGc();
  600. if (gc) {
  601. for (TInt i = 0; i < aCount; i++, aVerts++) {
  602. TUint32 aColor = (((TUint8)aVerts->color.a << 24) |
  603. ((TUint8)aVerts->color.b << 16) |
  604. ((TUint8)aVerts->color.g << 8) |
  605. (TUint8)aVerts->color.r);
  606. gc->SetPenColor(aColor);
  607. gc->Plot(TPoint(aVerts->x, aVerts->y));
  608. }
  609. }
  610. }
  611. // Gouraud-shaded triangle scanline fill directly into an EColor4K (XRGB4444) framebuffer.
  612. // Colors are interpolated per-scanline endpoint and per-pixel.
  613. static void FillTriangle(TUint16 *aPixels, TInt aStride,
  614. TInt aBmpW, TInt aBmpH,
  615. TInt aX0, TInt aY0, TInt aR0, TInt aG0, TInt aB0,
  616. TInt aX1, TInt aY1, TInt aR1, TInt aG1, TInt aB1,
  617. TInt aX2, TInt aY2, TInt aR2, TInt aG2, TInt aB2)
  618. {
  619. // Sort vertices by Y ascending (bubble sort on 3 elements).
  620. // Swap positions and colors together.
  621. #define SWAP3(ax, ay, ar, ag, ab, bx, by, br, bg, bb) \
  622. do { \
  623. TInt _t; \
  624. _t = ax; \
  625. ax = bx; \
  626. bx = _t; \
  627. _t = ay; \
  628. ay = by; \
  629. by = _t; \
  630. _t = ar; \
  631. ar = br; \
  632. br = _t; \
  633. _t = ag; \
  634. ag = bg; \
  635. bg = _t; \
  636. _t = ab; \
  637. ab = bb; \
  638. bb = _t; \
  639. } while (0)
  640. if (aY0 > aY1) {
  641. SWAP3(aX0, aY0, aR0, aG0, aB0, aX1, aY1, aR1, aG1, aB1);
  642. }
  643. if (aY1 > aY2) {
  644. SWAP3(aX1, aY1, aR1, aG1, aB1, aX2, aY2, aR2, aG2, aB2);
  645. }
  646. if (aY0 > aY1) {
  647. SWAP3(aX0, aY0, aR0, aG0, aB0, aX1, aY1, aR1, aG1, aB1);
  648. }
  649. #undef SWAP3
  650. TInt totalHeight = aY2 - aY0;
  651. if (totalHeight == 0) {
  652. return;
  653. }
  654. // Walk upper half [y0..y1] and lower half [y1..y2].
  655. for (TInt part = 0; part < 2; ++part) {
  656. TInt segHeight = (part == 0) ? (aY1 - aY0) : (aY2 - aY1);
  657. if (segHeight == 0) {
  658. continue;
  659. }
  660. TInt yStart = (part == 0) ? aY0 : aY1;
  661. TInt yEnd = (part == 0) ? aY1 : aY2;
  662. for (TInt y = yStart; y <= yEnd; ++y) {
  663. if (y < 0 || y >= aBmpH) {
  664. continue;
  665. }
  666. // 16.16 interpolation factors for long edge (v0->v2) and short edge.
  667. TInt tLong = ((y - aY0) << 16) / totalHeight;
  668. TInt tShort = ((y - yStart) << 16) / segHeight;
  669. // Interpolate X along both edges.
  670. TInt xLong = aX0 + (((aX2 - aX0) * tLong) >> 16);
  671. TInt xShort = (part == 0)
  672. ? aX0 + (((aX1 - aX0) * tShort) >> 16)
  673. : aX1 + (((aX2 - aX1) * tShort) >> 16);
  674. // Interpolate color along both edges (values 0-255).
  675. TInt rLong = aR0 + (((aR2 - aR0) * tLong) >> 16);
  676. TInt gLong = aG0 + (((aG2 - aG0) * tLong) >> 16);
  677. TInt bLong = aB0 + (((aB2 - aB0) * tLong) >> 16);
  678. TInt rShort, gShort, bShort;
  679. if (part == 0) {
  680. rShort = aR0 + (((aR1 - aR0) * tShort) >> 16);
  681. gShort = aG0 + (((aG1 - aG0) * tShort) >> 16);
  682. bShort = aB0 + (((aB1 - aB0) * tShort) >> 16);
  683. } else {
  684. rShort = aR1 + (((aR2 - aR1) * tShort) >> 16);
  685. gShort = aG1 + (((aG2 - aG1) * tShort) >> 16);
  686. bShort = aB1 + (((aB2 - aB1) * tShort) >> 16);
  687. }
  688. // Determine left/right endpoints and their colors.
  689. TInt xLeft, xRight;
  690. TInt rLeft, gLeft, bLeft;
  691. TInt rRight, gRight, bRight;
  692. if (xLong < xShort) {
  693. xLeft = xLong;
  694. xRight = xShort;
  695. rLeft = rLong;
  696. gLeft = gLong;
  697. bLeft = bLong;
  698. rRight = rShort;
  699. gRight = gShort;
  700. bRight = bShort;
  701. } else {
  702. xLeft = xShort;
  703. xRight = xLong;
  704. rLeft = rShort;
  705. gLeft = gShort;
  706. bLeft = bShort;
  707. rRight = rLong;
  708. gRight = gLong;
  709. bRight = bLong;
  710. }
  711. // Clamp X to bitmap width.
  712. if (xLeft < 0) {
  713. xLeft = 0;
  714. }
  715. if (xRight >= aBmpW) {
  716. xRight = aBmpW - 1;
  717. }
  718. TInt spanWidth = xRight - xLeft;
  719. TUint16 *row = aPixels + y * aStride;
  720. // Compute per-pixel color deltas once per span (one division each)
  721. // then step incrementally; avoids a division per pixel.
  722. if (spanWidth > 0) {
  723. TInt dr = ((rRight - rLeft) << 16) / spanWidth;
  724. TInt dg = ((gRight - gLeft) << 16) / spanWidth;
  725. TInt db = ((bRight - bLeft) << 16) / spanWidth;
  726. TInt r = rLeft << 16;
  727. TInt g = gLeft << 16;
  728. TInt b = bLeft << 16;
  729. for (TInt x = xLeft; x <= xRight; ++x) {
  730. // Pack to XRGB4444.
  731. row[x] = (TUint16)((((r >> 16) >> 4) << 8) | (((g >> 16) >> 4) << 4) | ((b >> 16) >> 4));
  732. r += dr;
  733. g += dg;
  734. b += db;
  735. }
  736. } else {
  737. row[xLeft] = (TUint16)(((rLeft >> 4) << 8) | ((gLeft >> 4) << 4) | (bLeft >> 4));
  738. }
  739. }
  740. }
  741. }
  742. void CRenderer::DrawGeometry(NGAGE_Vertex *aVerts, const TInt aCount)
  743. {
  744. if (aCount < 3) {
  745. return;
  746. }
  747. CFbsBitmap *bmp = GetCurrentBitmap();
  748. if (bmp) {
  749. TUint16 *pixels = reinterpret_cast<TUint16 *>(bmp->DataAddress());
  750. if (pixels) {
  751. TSize bmpSize = bmp->SizeInPixels();
  752. TInt stride = CFbsBitmap::ScanLineLength(bmpSize.iWidth, EColor4K) / 2;
  753. for (TInt i = 0; i + 2 < aCount; i += 3) {
  754. FillTriangle(pixels, stride,
  755. bmpSize.iWidth, bmpSize.iHeight,
  756. aVerts[i].x, aVerts[i].y,
  757. aVerts[i].color.r, aVerts[i].color.g, aVerts[i].color.b,
  758. aVerts[i + 1].x, aVerts[i + 1].y,
  759. aVerts[i + 1].color.r, aVerts[i + 1].color.g, aVerts[i + 1].color.b,
  760. aVerts[i + 2].x, aVerts[i + 2].y,
  761. aVerts[i + 2].color.r, aVerts[i + 2].color.g, aVerts[i + 2].color.b);
  762. }
  763. return;
  764. }
  765. }
  766. }
  767. void CRenderer::FillRects(NGAGE_Vertex *aVerts, const TInt aCount)
  768. {
  769. CFbsBitGc *gc = GetCurrentGc();
  770. if (gc) {
  771. for (TInt i = 0; i < aCount; i++, aVerts++) {
  772. TPoint pos(aVerts[i].x, aVerts[i].y);
  773. TSize size(
  774. aVerts[i + 1].x,
  775. aVerts[i + 1].y);
  776. TRect rect(pos, size);
  777. TUint32 aColor = (((TUint8)aVerts->color.a << 24) |
  778. ((TUint8)aVerts->color.b << 16) |
  779. ((TUint8)aVerts->color.g << 8) |
  780. (TUint8)aVerts->color.r);
  781. gc->SetPenColor(aColor);
  782. gc->SetBrushColor(aColor);
  783. gc->SetBrushStyle(CGraphicsContext::ESolidBrush);
  784. gc->SetPenStyle(CGraphicsContext::ENullPen);
  785. gc->DrawRect(rect);
  786. }
  787. }
  788. }
  789. void CRenderer::Flip()
  790. {
  791. if (!iRenderer) {
  792. SDL_Log("iRenderer is NULL.");
  793. return;
  794. }
  795. if (!iIsFocused) {
  796. return;
  797. }
  798. iRenderer->Gc()->UseFont(iFont);
  799. if (iShowFPS && iRenderer->Gc()) {
  800. UpdateFPS();
  801. TBuf<64> info;
  802. iRenderer->Gc()->SetPenStyle(CGraphicsContext::ESolidPen);
  803. iRenderer->Gc()->SetBrushStyle(CGraphicsContext::ENullBrush);
  804. iRenderer->Gc()->SetPenColor(KRgbCyan);
  805. TRect aTextRect(TPoint(3, 203 - iFont->HeightInPixels()), TSize(45, iFont->HeightInPixels() + 2));
  806. iRenderer->Gc()->SetBrushStyle(CGraphicsContext::ESolidBrush);
  807. iRenderer->Gc()->SetBrushColor(KRgbBlack);
  808. iRenderer->Gc()->DrawRect(aTextRect);
  809. // Draw messages.
  810. info.Format(_L("FPS: %d"), iFPS);
  811. iRenderer->Gc()->DrawText(info, TPoint(5, 203));
  812. } else {
  813. // This is a workaround that helps regulating the FPS.
  814. iRenderer->Gc()->DrawText(_L(""), TPoint(0, 0));
  815. }
  816. iRenderer->Gc()->DiscardFont();
  817. iRenderer->Flip(iDirectScreen);
  818. // Keep the backlight on.
  819. if (iSuspendScreenSaver) {
  820. User::ResetInactivityTime();
  821. }
  822. // Suspend the current thread for a short while.
  823. // Give some time to other threads and active objects.
  824. User::After(0);
  825. }
  826. void CRenderer::SetDrawColor(TUint32 iColor)
  827. {
  828. CFbsBitGc *gc = GetCurrentGc();
  829. if (gc) {
  830. gc->SetPenColor(iColor);
  831. gc->SetBrushColor(iColor);
  832. gc->SetBrushStyle(CGraphicsContext::ESolidBrush);
  833. }
  834. if (iRenderer) {
  835. TRAPD(err, iRenderer->SetCurrentColor(iColor));
  836. if (err != KErrNone) {
  837. return;
  838. }
  839. }
  840. }
  841. void CRenderer::SetClipRect(TInt aX, TInt aY, TInt aWidth, TInt aHeight)
  842. {
  843. CFbsBitGc *gc = GetCurrentGc();
  844. if (gc) {
  845. TRect viewportRect(aX, aY, aX + aWidth, aY + aHeight);
  846. gc->SetClippingRect(viewportRect);
  847. }
  848. }
  849. void CRenderer::UpdateFPS()
  850. {
  851. static TTime lastTime;
  852. static TInt frameCount = 0;
  853. TTime currentTime;
  854. const TUint KOneSecond = 1000000; // 1s in ms.
  855. currentTime.HomeTime();
  856. ++frameCount;
  857. TTimeIntervalMicroSeconds timeDiff = currentTime.MicroSecondsFrom(lastTime);
  858. if (timeDiff.Int64() >= KOneSecond) {
  859. // Calculate FPS.
  860. iFPS = frameCount;
  861. // Reset frame count and last time.
  862. frameCount = 0;
  863. lastTime = currentTime;
  864. }
  865. }
  866. void CRenderer::SuspendScreenSaver(TBool aSuspend)
  867. {
  868. iSuspendScreenSaver = aSuspend;
  869. }
  870. void CRenderer::SetRenderTarget(NGAGE_TextureData *aTarget)
  871. {
  872. iCurrentRenderTarget = aTarget;
  873. }
  874. CFbsBitGc *CRenderer::GetCurrentGc()
  875. {
  876. if (iCurrentRenderTarget && iCurrentRenderTarget->gc) {
  877. return iCurrentRenderTarget->gc;
  878. }
  879. return iRenderer ? iRenderer->Gc() : NULL;
  880. }
  881. CFbsBitmap *CRenderer::GetCurrentBitmap()
  882. {
  883. if (iCurrentRenderTarget && iCurrentRenderTarget->bitmap) {
  884. return iCurrentRenderTarget->bitmap;
  885. }
  886. return iRenderer ? iRenderer->Bitmap() : NULL;
  887. }
  888. static SDL_Scancode ConvertScancode(int key)
  889. {
  890. SDL_Keycode keycode;
  891. switch (key) {
  892. case EStdKeyBackspace: // Clear key
  893. keycode = SDLK_BACKSPACE;
  894. break;
  895. case 0x31: // 1
  896. keycode = SDLK_1;
  897. break;
  898. case 0x32: // 2
  899. keycode = SDLK_2;
  900. break;
  901. case 0x33: // 3
  902. keycode = SDLK_3;
  903. break;
  904. case 0x34: // 4
  905. keycode = SDLK_4;
  906. break;
  907. case 0x35: // 5
  908. keycode = SDLK_5;
  909. break;
  910. case 0x36: // 6
  911. keycode = SDLK_6;
  912. break;
  913. case 0x37: // 7
  914. keycode = SDLK_7;
  915. break;
  916. case 0x38: // 8
  917. keycode = SDLK_8;
  918. break;
  919. case 0x39: // 9
  920. keycode = SDLK_9;
  921. break;
  922. case 0x30: // 0
  923. keycode = SDLK_0;
  924. break;
  925. case 0x2a: // Asterisk
  926. keycode = SDLK_ASTERISK;
  927. break;
  928. case EStdKeyHash: // Hash
  929. keycode = SDLK_HASH;
  930. break;
  931. case EStdKeyDevice0: // Left softkey
  932. keycode = SDLK_SOFTLEFT;
  933. break;
  934. case EStdKeyDevice1: // Right softkey
  935. keycode = SDLK_SOFTRIGHT;
  936. break;
  937. case EStdKeyApplication0: // Call softkey
  938. keycode = SDLK_CALL;
  939. break;
  940. case EStdKeyApplication1: // End call softkey
  941. keycode = SDLK_ENDCALL;
  942. break;
  943. case EStdKeyDevice3: // Middle softkey
  944. keycode = SDLK_SELECT;
  945. break;
  946. case EStdKeyUpArrow: // Up arrow
  947. keycode = SDLK_UP;
  948. break;
  949. case EStdKeyDownArrow: // Down arrow
  950. keycode = SDLK_DOWN;
  951. break;
  952. case EStdKeyLeftArrow: // Left arrow
  953. keycode = SDLK_LEFT;
  954. break;
  955. case EStdKeyRightArrow: // Right arrow
  956. keycode = SDLK_RIGHT;
  957. break;
  958. default:
  959. keycode = SDLK_UNKNOWN;
  960. break;
  961. }
  962. return SDL_GetScancodeFromKey(keycode, NULL);
  963. }
  964. void CRenderer::HandleEvent(const TWsEvent &aWsEvent)
  965. {
  966. Uint64 timestamp;
  967. switch (aWsEvent.Type()) {
  968. case EEventKeyDown: /* Key events */
  969. timestamp = SDL_GetPerformanceCounter();
  970. SDL_SendKeyboardKey(timestamp, 1, aWsEvent.Key()->iCode, ConvertScancode(aWsEvent.Key()->iScanCode), true);
  971. break;
  972. case EEventKeyUp: /* Key events */
  973. timestamp = SDL_GetPerformanceCounter();
  974. SDL_SendKeyboardKey(timestamp, 1, aWsEvent.Key()->iCode, ConvertScancode(aWsEvent.Key()->iScanCode), false);
  975. break;
  976. case EEventFocusGained:
  977. DisableKeyBlocking();
  978. if (!iDirectScreen->IsActive()) {
  979. TRAPD(err, iDirectScreen->StartL());
  980. if (KErrNone != err) {
  981. return;
  982. }
  983. iDirectScreen->ScreenDevice()->SetAutoUpdate(ETrue);
  984. iIsFocused = ETrue;
  985. }
  986. Flip();
  987. break;
  988. case EEventFocusLost:
  989. {
  990. if (iDirectScreen->IsActive()) {
  991. iDirectScreen->Cancel();
  992. }
  993. iIsFocused = EFalse;
  994. break;
  995. }
  996. default:
  997. break;
  998. }
  999. }
  1000. void CRenderer::DisableKeyBlocking()
  1001. {
  1002. TRawEvent aEvent;
  1003. aEvent.Set((TRawEvent::TType) /*EDisableKeyBlock*/ 51);
  1004. iWsSession.SimulateRawEvent(aEvent);
  1005. }
  1006. void CRenderer::PumpEvents()
  1007. {
  1008. while (iWsEventStatus != KRequestPending) {
  1009. iWsSession.GetEvent(iWsEvent);
  1010. HandleEvent(iWsEvent);
  1011. iWsEventStatus = KRequestPending;
  1012. iWsSession.EventReady(&iWsEventStatus);
  1013. }
  1014. }
  1015. #endif // SDL_VIDEO_RENDER_NGAGE