<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>What is the proper procedure for restoring bitmaps on Android?</title>
		<link>http://www.allegro.cc/forums/view/615380</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Sat, 16 May 2015 01:43:48 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Android is driving me crazy. First, blend modes didn&#39;t work and I had to write a shader to subtract two textures, and now bitmaps are getting corrupted after I suspend and resume my game.</p><p>So, what&#39;s happening:</p><p>When I resume my game after going to background (via pressing Home button), I get not one, but two ALLEGRO_EVENT_DISPLAY_RESUME_DRAWING events. </p><p>If I recreate all bitmaps on both of them, all bitmaps with NO_PRESERVE_TEXTURE get corrupted very badly. For example, when I later draw a one-pixel line on such bitmap, it gets displayed as a wide band.</p><p>If I only react to the first event, everything gets corrupted (as it should be, probably)</p><p>If I only react to the second event (beyond calling al_acknowledge_drawing_resume), the picture is more correct, but there are still some problems with alpha channel.</p><p>Here&#39;s the code I use:</p><div class="source-code"><div class="toolbar"><span class="button numbers"><b>#</b></span><span class="button select">Select</span><span class="button expand">Expand</span></div><div class="inner"><span class="number">  1</span><span class="c">// Game.cpp, main loop:</span>
<span class="number">  2</span>
<span class="number">  3</span><span class="k1">case</span> DisplayEvent::DISPLAY_RESUME_DRAWING:
<span class="number">  4</span>    al_acknowledge_drawing_resume<span class="k2">(</span> <a href="http://www.allegro.cc/manual/al_get_current_display"><span class="a">al_get_current_display</span></a><span class="k2">(</span><span class="k2">)</span> <span class="k2">)</span><span class="k2">;</span>
<span class="number">  5</span>    render.SetGlobalScaling<span class="k2">(</span> SCALING_KEEP_ASPECT, <a href="http://www.allegro.cc/manual/SCREEN_W"><span class="a">SCREEN_W</span></a>, <a href="http://www.allegro.cc/manual/SCREEN_H"><span class="a">SCREEN_H</span></a> <span class="k2">)</span><span class="k2">;</span>
<span class="number">  6</span>    GetBitmapManager<span class="k2">(</span><span class="k2">)</span>.ReloadAllResources<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
<span class="number">  7</span>    m_forcedPause <span class="k3">=</span> <span class="k1">false</span><span class="k2">;</span>
<span class="number">  8</span>    <span class="k1">break</span><span class="k2">;</span>
<span class="number">  9</span>
<span class="number"> 10</span><span class="c">// Bitmap manager, a simplified code for ReloadAllResources():</span>
<span class="number"> 11</span><span class="k1">void</span> BitmapManager::ReloadAllResources<span class="k2">(</span><span class="k2">)</span>
<span class="number"> 12</span><span class="k2">{</span>
<span class="number"> 13</span>    <span class="k1">const</span> <span class="k1">int</span> oldFlags <span class="k3">=</span> <a href="http://www.allegro.cc/manual/al_get_new_bitmap_flags"><span class="a">al_get_new_bitmap_flags</span></a><span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
<span class="number"> 14</span>    
<span class="number"> 15</span>    <span class="k1">for</span> <span class="k2">(</span> ResVector::iterator iter <span class="k3">=</span> m_resources.begin<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
<span class="number"> 16</span>        iter <span class="k3">!</span><span class="k3">=</span> m_resources.end<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
<span class="number"> 17</span>        <span class="k3">+</span><span class="k3">+</span>iter <span class="k2">)</span>
<span class="number"> 18</span>    <span class="k2">{</span>
<span class="number"> 19</span>         <a href="http://www.allegro.cc/manual/ALLEGRO_BITMAP"><span class="a">ALLEGRO_BITMAP</span></a> <span class="k3">*</span>pOld <span class="k3">=</span> <span class="k2">(</span><span class="k2">(</span>AllegroImage5<span class="k3">*</span><span class="k2">)</span>iter-&gt;m_res<span class="k2">)</span><span class="k3">-</span><span class="k3">&gt;</span>GetBitmap<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
<span class="number"> 20</span>         ALLEGRO_BTIMAP <span class="k3">*</span>pNew <span class="k3">=</span> <span class="n">0</span><span class="k2">;</span>
<span class="number"> 21</span>         <span class="k1">int</span> newFlags <span class="k3">=</span> <a href="http://www.allegro.cc/manual/al_get_bitmap_flags"><span class="a">al_get_bitmap_flags</span></a><span class="k2">(</span> pOld <span class="k2">)</span><span class="k2">;</span>
<span class="number"> 22</span>         <a href="http://www.allegro.cc/manual/al_set_new_bitmap_flags"><span class="a">al_set_new_bitmap_flags</span></a><span class="k2">(</span> newFlags <span class="k2">)</span><span class="k2">;</span>
<span class="number"> 23</span>         <span class="k1">if</span> <span class="k2">(</span> IsResourceFromFile<span class="k2">(</span> iter <span class="k2">)</span> <span class="k2">)</span>
<span class="number"> 24</span>             pNew <span class="k3">=</span> <a href="http://www.allegro.cc/manual/al_load_bitmap"><span class="a">al_load_bitmap</span></a><span class="k2">(</span> iter-&gt;filename.c_str<span class="k2">(</span><span class="k2">)</span> <span class="k2">)</span><span class="k2">;</span>
<span class="number"> 25</span>         <span class="k1">else</span> <span class="k1">if</span> <span class="k2">(</span> <span class="k3">!</span><span class="k2">(</span>newFlags <span class="k3">&amp;</span> ALLEGRO_NO_PRESERVE_TEXTURE <span class="k2">)</span><span class="k2">)</span> 
<span class="number"> 26</span>             pNew <span class="k3">=</span> <a href="http://www.allegro.cc/manual/al_clone_bitmap"><span class="a">al_clone_bitmap</span></a><span class="k2">(</span> pOld <span class="k2">)</span><span class="k2">;</span>
<span class="number"> 27</span>         <span class="k1">else</span>
<span class="number"> 28</span>         <span class="k2">{</span>
<span class="number"> 29</span>             <span class="k1">const</span> <span class="k1">int</span> w <span class="k3">=</span> <a href="http://www.allegro.cc/manual/al_get_bitmap_width"><span class="a">al_get_bitmap_width</span></a><span class="k2">(</span> pOld <span class="k2">)</span><span class="k2">;</span>
<span class="number"> 30</span>             <span class="k1">const</span> <span class="k1">int</span> h <span class="k3">=</span> <a href="http://www.allegro.cc/manual/al_get_bitmap_height"><span class="a">al_get_bitmap_height</span></a><span class="k2">(</span> pOld <span class="k2">)</span><span class="k2">;</span>
<span class="number"> 31</span>             pNew <span class="k3">=</span> <a href="http://www.allegro.cc/manual/al_create_bitmap"><span class="a">al_create_bitmap</span></a><span class="k2">(</span> w, h <span class="k2">)</span><span class="k2">;</span>
<span class="number"> 32</span>         <span class="k2">}</span>
<span class="number"> 33</span>
<span class="number"> 34</span>         <a href="http://www.allegro.cc/manual/al_destroy_bitmap"><span class="a">al_destroy_bitmap</span></a><span class="k2">(</span> pOld <span class="k2">)</span><span class="k2">;</span>
<span class="number"> 35</span>         <span class="k1">delete</span> iter-&gt;m_res<span class="k2">;</span>
<span class="number"> 36</span>         iter-&gt;m_res <span class="k3">=</span> <span class="k1">new</span> AllegroImage5<span class="k2">(</span> pNew <span class="k2">)</span><span class="k2">;</span>
<span class="number"> 37</span>    <span class="k2">}</span>
<span class="number"> 38</span>
<span class="number"> 39</span>    <a href="http://www.allegro.cc/manual/al_set_new_bitmap_flags"><span class="a">al_set_new_bitmap_flags</span></a><span class="k2">(</span> oldFlags <span class="k2">)</span><span class="k2">;</span>
<span class="number"> 40</span><span class="k2">}</span>
</div></div><p>

So, what am I doing wrong? I&#39;ve tried everything I could think of already.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Max Savenkov)</author>
		<pubDate>Fri, 15 May 2015 13:57:01 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I&#39;m not really overly familiar with Allegro 5, let alone the Android port, but what stands out to me are <span class="source-code">oldFlags</span> and <span class="source-code">newFlags</span>. You extract the flags from the original bitmap, but you don&#39;t seem to apply them to the new bitmap. And the <span class="source-code"><a href="http://www.allegro.cc/manual/al_set_new_bitmap_flags"><span class="a">al_set_new_bitmap_flags</span></a></span> doesn&#39;t appear to do anything since you just reassign the exact same flags that were already set. It&#39;s difficult to know though what Allegro does in the background with all this global state... I would have expected you to use <span class="source-code"><a href="http://www.allegro.cc/manual/al_set_new_bitmap_flags"><span class="a">al_set_new_bitmap_flags</span></a></span> to temporary change the flags before creating the new bitmap and then restore the original setting at the end...
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (bamccaig)</author>
		<pubDate>Fri, 15 May 2015 21:50:59 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Ah, sorry, I forgot to copy that line <img src="http://www.allegro.cc/forums/smileys/sad.gif" alt=":(" /> Amended.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Max Savenkov)</author>
		<pubDate>Fri, 15 May 2015 23:52:40 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Do you ever clear the new bitmaps? You create new bitmaps in the case of un-preserved textures and bitmaps not reloaded from a file. If you aren&#39;t drawing over the whole bitmaps then you could have uninitialized memory in them.</p><p>That&#39;s all I can really see as you re-load bitmaps that were from files and you clone bitmaps that don&#39;t have the NO_PRESERVE_TEXTURE flag set.</p><p>Are all your resources in m_resources bitmaps? Ie, are they all AllegroImage5 objects?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Edgar Reynaldo)</author>
		<pubDate>Sat, 16 May 2015 01:23:46 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Yes, they are all stored in m_resources. I do not clear NO_PRESERVE_TEXTURE bitmaps, as they are usually drawn over completely on the next frame. It&#39;s still would be more proper to clear them, of course, than to rely on that, so I&#39;ll do that.</p><p>What bothers me more, is those ALLEGRO_EVENT_DISPLAY_RESUME_DRAWING events... Why am I getting two of them, and why recreating bitmaps twice screws things up royally (compared to a common screw up that happens if I only recreated them once, during handling of the second event)? I can&#39;t find any relevant references to surfaceChanged being called twice in Google...</p><p><b>EDIT</b>: I&#39;ve done some digging, and it seems that I found the reason for some of my troubles. Based on attached log, I deduced the following sequence of events:</p><p>1) The game is paused, halt_drawing is acknowledged, the game goes to background<br />2) The game is resumed, a new Surface is created in response to onCreate, nativeOnCreate is called, resume_drawing event is posted to queue.<br />3) My code begins to react to resume_drawing event by re-creating bitmaps<br />4) SUDDENLY! In the middle of it all, a second onCreate is called in another thread! The current Surface is destroyed - but my code is in the middle of recreation of bitmaps, and has no notion that something has gone wrong. After this part, all bitmaps I try to re-create are corrupted, because they have no Surface to be created against.<br />5) The second resume_drawing event reaches my code. I begin to recreate bitmaps once again, but most of their data is already corrupted (?).</p><p>This explains why things looks the best when I only react to the second resume_drawing event. I couldn&#39;t discern why onCreate is called twice, but there are mentions of such behaviour here and there. It seems like it&#39;s normal behaviour on Android sometimes. So the question is, how to make Allegro handle it correctly? The main problem I see is that Surface is destroyed when my own code is in the middle of work with bitmaps. Is there any way to postpone destruction of the old Surface? I don&#39;t quite see what to do here...
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Max Savenkov)</author>
		<pubDate>Sat, 16 May 2015 01:43:48 +0000</pubDate>
	</item>
</rss>
