Home | History | Annotate | Download | only in gltrace
      1 /*
      2  * Copyright (C) 2011 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.ide.eclipse.gltrace;
     18 
     19 import com.android.ide.eclipse.gltrace.GLProtoBuf.GLMessage;
     20 
     21 import org.eclipse.swt.graphics.Image;
     22 import org.eclipse.swt.graphics.ImageData;
     23 import org.eclipse.swt.graphics.PaletteData;
     24 import org.eclipse.swt.widgets.Display;
     25 import org.liblzf.CLZF;
     26 
     27 /** Utilities to deal with protobuf encoded {@link GLMessage}. */
     28 public class ProtoBufUtils {
     29     private static ImageData getImageData(GLMessage glMsg) {
     30         int width = glMsg.getFb().getWidth();
     31         int height = glMsg.getFb().getHeight();
     32 
     33         if (width * height == 0) {
     34             return null;
     35         }
     36 
     37         byte[] compressed = glMsg.getFb().getContents(0).toByteArray();
     38         byte[] uncompressed = new byte[width * height * 4];
     39 
     40         int size = CLZF.lzf_decompress(compressed, compressed.length,
     41                                 uncompressed, uncompressed.length);
     42         assert size == width * height * 4 : "Unexpected image size after decompression.";
     43 
     44         int redMask   = 0xff000000;
     45         int greenMask = 0x00ff0000;
     46         int blueMask  = 0x0000ff00;
     47         PaletteData palette = new PaletteData(redMask, greenMask, blueMask);
     48         ImageData imageData = new ImageData(
     49                 width,
     50                 height,
     51                 32,         // depth
     52                 palette,
     53                 1,          // scan line padding
     54                 uncompressed);
     55         byte[] alpha = new byte[width*height];
     56         for (int i = 0; i < width * height; i++) {
     57             alpha[i] = uncompressed[i * 4 + 3];
     58         }
     59         imageData.alphaData = alpha;
     60 
     61         imageData = imageData.scaledTo(imageData.width, -imageData.height);
     62         return imageData;
     63     }
     64 
     65     /** Obtains the image stored in provided protocol buffer message. */
     66     public static Image getImage(Display display, GLMessage glMsg) {
     67         if (!glMsg.hasFb()) {
     68             return null;
     69         }
     70 
     71         ImageData imageData = null;
     72         try {
     73             imageData = getImageData(glMsg);
     74         } catch (Exception e) {
     75             GlTracePlugin.getDefault().logMessage(
     76                     "Unexpected error while retrieving framebuffer image: " + e);
     77             return null;
     78         }
     79 
     80         if (imageData == null) {
     81             return null;
     82         }
     83 
     84         return new Image(display, imageData);
     85     }
     86 }
     87