aboutsummaryrefslogtreecommitdiffhomepage
path: root/platform_tools/android/apps/canvasproof/src/main/java/org/skia/canvasproof/HwuiPictureView.java
blob: 872089c9d304fb68adc00cff1a5ae2f2e5e0e1bf (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
/*
 * Copyright 2015 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

package org.skia.canvasproof;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Picture;
import android.util.Log;
import android.view.View;
import java.io.IOException;
import java.io.InputStream;

public class HwuiPictureView extends View {
    private static final String TAG = "HwuiPictureView";
    private Picture picture;
    private float scale;
    private Picture defaultPicture;

    public boolean fullTime;

    HwuiPictureView(Context context) {
        super(context);
        this.scale = 1.0f;
    }
    public void setScale(float s) {
        this.scale = s;
    }
    public void setPicture(Picture p) {
        this.picture = p;
        this.invalidate();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        if (this.picture != null) {
            canvas.save();
            canvas.scale(scale, scale);
            HwuiPictureView.draw(canvas, this.picture);
            canvas.restore();
            if (fullTime) {
                this.invalidate();
            }
        }
    }

    static private void draw(Canvas canvas, Picture p) {
        if (android.os.Build.VERSION.SDK_INT > 22) {
            try {
                canvas.drawPicture(p);
                return;
            } catch (java.lang.Exception e) {
                Log.e(TAG, "Exception while drawing picture in Hwui");
            }
        }
        if (p.getWidth() > 0 && p.getHeight() > 0) {
            // Fallback to software rendering.
            Bitmap bm = Bitmap.createBitmap(p.getWidth(), p.getHeight(),
                                            Bitmap.Config.ARGB_8888);
            (new Canvas(bm)).drawPicture(p);
            canvas.drawBitmap(bm, 0.0f, 0.0f, null);
            bm.recycle();
        }
    }
}