aboutsummaryrefslogtreecommitdiff
path: root/eclipse/plugins/com.android.ide.eclipse.gldebugger/src/com/android/ide/eclipse/gltrace/ProtoBufUtils.java
blob: 074e4409e725a8406b976e20f87eb454f539ff80 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.ide.eclipse.gltrace;

import com.android.ide.eclipse.gltrace.GLProtoBuf.GLMessage;

import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.PaletteData;
import org.eclipse.swt.widgets.Display;
import org.liblzf.CLZF;

/** Utilities to deal with protobuf encoded {@link GLMessage}. */
public class ProtoBufUtils {
    private static ImageData getImageData(GLMessage glMsg) {
        int width = glMsg.getFb().getWidth();
        int height = glMsg.getFb().getHeight();

        if (width * height == 0) {
            return null;
        }

        byte[] compressed = glMsg.getFb().getContents(0).toByteArray();
        byte[] uncompressed = new byte[width * height * 4];

        int size = CLZF.lzf_decompress(compressed, compressed.length,
                                uncompressed, uncompressed.length);
        assert size == width * height * 4 : "Unexpected image size after decompression.";

        int redMask   = 0xff000000;
        int greenMask = 0x00ff0000;
        int blueMask  = 0x0000ff00;
        PaletteData palette = new PaletteData(redMask, greenMask, blueMask);
        ImageData imageData = new ImageData(
                width,
                height,
                32,         // depth
                palette,
                1,          // scan line padding
                uncompressed);
        byte[] alpha = new byte[width*height];
        for (int i = 0; i < width * height; i++) {
            alpha[i] = uncompressed[i * 4 + 3];
        }
        imageData.alphaData = alpha;

        imageData = imageData.scaledTo(imageData.width, -imageData.height);
        return imageData;
    }

    /** Obtains the image stored in provided protocol buffer message. */
    public static Image getImage(Display display, GLMessage glMsg) {
        if (!glMsg.hasFb()) {
            return null;
        }

        ImageData imageData = null;
        try {
            imageData = getImageData(glMsg);
        } catch (Exception e) {
            GlTracePlugin.getDefault().logMessage(
                    "Unexpected error while retrieving framebuffer image: " + e);
            return null;
        }

        if (imageData == null) {
            return null;
        }

        return new Image(display, imageData);
    }
}