title: Accessing layer contents in Android-surfaceflinger
body: <p>I am reading the <strong>layer</strong> contents to check whether its the same content as that rendered by the app on-screen. I am reading the contents before they are being composited by <strong>SurfaceFlinger</strong>. Here is the block of code in <strong>HWCLayerVersion1</strong>::<strong>setAcquireFenceFd</strong>() in <strong>HWComposer.cpp</strong>, to dump the layer content/<strong>pixels</strong> to a raw-file. </p>
  
  <pre><code>    getLayer()-&gt;acquireFenceFd = fenceFd;
      private_handle_t *hnd = (private_handle_t*)getLayer()-&gt;handle; // the handle of the layer
  
      /*code for checking layer contents*/
      if(private_handle_t::validate(getLayer()-&gt;handle)==0){
               ALOGD("beta: we are gonna read a valid buffer-&gt; %08x", intptr_t(getLayer()-&gt;handle));
               char filename[64];
               memset(filename, 0, 64);
               int name = clock();
               sprintf(filename, "/data/dump.%08x.raw", intptr_t(getLayer()-&gt;handle));                
               if(getLayer()-&gt;acquireFenceFd &gt;= 0){
                    int ret = sync_wait(getLayer()-&gt;acquireFenceFd, -1);
                    if(ret &lt; 0){
                          ALOGD("beta: sync_wait failed");
                    } else{
                        FILE *file = fopen(filename,"w+");
                        //ALOGD("beta: writing pixels");
                        fwrite((void*)hnd-&gt;base, hnd-&gt;size, 1, file);
                        close(getLayer()-&gt;acquireFenceFd);
                        //getLayer()-&gt;acquireFenceFd = -1;
                    } 
                } else {
                     ALOGD("beta: fencefd not valid");
                }
      }
  </code></pre>
  
  <p>When I am reading the pixels using <strong>IrfanView</strong> with the appropriate attributes, the image only faintly resembles the actual content, but the colors are all smudged. What is the reason behind this? Is the buffer being rendered while I am reading the content? I'm totally new to AOSP, and any help would be appreciated. </p>