Home | History | Annotate | Download | only in enc_common
      1 #include "IOStream.h"
      2 
      3 #include "GL2Encoder.h"
      4 
      5 #include <GLES3/gl31.h>
      6 
      7 #include <vector>
      8 
      9 void IOStream::readbackPixels(void* context, int width, int height, unsigned int format, unsigned int type, void* pixels) {
     10     GL2Encoder *ctx = (GL2Encoder *)context;
     11     assert (ctx->state() != NULL);
     12 
     13     int startOffset = 0;
     14     int pixelRowSize = 0;
     15     int totalRowSize = 0;
     16     int skipRows = 0;
     17 
     18     ctx->state()->getPackingOffsets2D(width, height, format, type,
     19                                       &startOffset,
     20                                       &pixelRowSize,
     21                                       &totalRowSize,
     22                                       &skipRows);
     23 
     24     size_t pixelDataSize =
     25         ctx->state()->pixelDataSize(
     26             width, height, 1, format, type, 1 /* is pack */);
     27 
     28     if (startOffset == 0 &&
     29         pixelRowSize == totalRowSize) {
     30         // fast path
     31         readback(pixels, pixelDataSize);
     32     } else if (pixelRowSize == totalRowSize) {
     33         // fast path but with skip in the beginning
     34         std::vector<char> paddingToDiscard(startOffset, 0);
     35         readback(&paddingToDiscard[0], startOffset);
     36         readback((char*)pixels + startOffset, pixelDataSize - startOffset);
     37     } else {
     38         int totalReadback = 0;
     39 
     40         if (startOffset > 0) {
     41             std::vector<char> paddingToDiscard(startOffset, 0);
     42             readback(&paddingToDiscard[0], startOffset);
     43             totalReadback += startOffset;
     44         }
     45         // need to read back row by row
     46         size_t paddingSize = totalRowSize - pixelRowSize;
     47         std::vector<char> paddingToDiscard(paddingSize, 0);
     48 
     49         char* start = (char*)pixels + startOffset;
     50 
     51         for (int i = 0; i < height; i++) {
     52             readback(start, pixelRowSize);
     53             totalReadback += pixelRowSize;
     54             readback(&paddingToDiscard[0], paddingSize);
     55             totalReadback += paddingSize;
     56             start += totalRowSize;
     57         }
     58     }
     59 }
     60