summaryrefslogtreecommitdiff
path: root/packages/SystemUI/src/com/android/systemui/screenshot/ImageTile.java
blob: a95c91bfeceb325041681d0e1b858ba7fdab21a5 (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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
 * Copyright (C) 2020 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.systemui.screenshot;

import static android.graphics.ColorSpace.Named.SRGB;

import static java.util.Objects.requireNonNull;

import android.annotation.NonNull;
import android.graphics.Bitmap;
import android.graphics.ColorSpace;
import android.graphics.RecordingCanvas;
import android.graphics.Rect;
import android.graphics.RenderNode;
import android.media.Image;

/**
 * Holds a hardware image, coordinates and render node to draw the tile. The tile manages clipping
 * and dimensions. The tile must be drawn translated to the correct target position:
 * <pre>
 *     ImageTile tile = getTile();
 *     canvas.save();
 *     canvas.translate(tile.getLeft(), tile.getTop());
 *     canvas.drawRenderNode(tile.getDisplayList());
 *     canvas.restore();
 * </pre>
 */
class ImageTile implements AutoCloseable {
    private final Image mImage;
    private final Rect mLocation;
    private RenderNode mNode;

    private static final ColorSpace COLOR_SPACE = ColorSpace.get(SRGB);

    /**
     * Create an image tile from the given image.
     *
     * @param image an image containing a hardware buffer
     * @param location the captured area represented by image tile (virtual coordinates)
     */
    ImageTile(@NonNull Image image, @NonNull Rect location) {
        mImage = requireNonNull(image, "image");
        mLocation = requireNonNull(location);
        requireNonNull(mImage.getHardwareBuffer(), "image must be a hardware image");
    }

    synchronized RenderNode getDisplayList() {
        if (mNode == null) {
            mNode = new RenderNode("Tile{" + Integer.toHexString(mImage.hashCode()) + "}");
        }
        if (mNode.hasDisplayList()) {
            return mNode;
        }
        final int w = Math.min(mImage.getWidth(), mLocation.width());
        final int h = Math.min(mImage.getHeight(), mLocation.height());
        mNode.setPosition(0, 0, w, h);

        RecordingCanvas canvas = mNode.beginRecording(w, h);
        canvas.save();
        canvas.clipRect(0, 0, mLocation.width(), mLocation.height());
        canvas.drawBitmap(Bitmap.wrapHardwareBuffer(mImage.getHardwareBuffer(), COLOR_SPACE),
                0, 0, null);
        canvas.restore();
        mNode.endRecording();
        return mNode;
    }

    Rect getLocation() {
        return mLocation;
    }

    int getLeft() {
        return mLocation.left;
    }

    int getTop() {
        return mLocation.top;
    }

    int getRight() {
        return mLocation.right;
    }

    int getBottom() {
        return mLocation.bottom;
    }

    @Override
    public synchronized void close() {
        mImage.close();
        if (mNode != null) {
            mNode.discardDisplayList();
        }
    }

    @Override
    public String toString() {
        return "{location=" + mLocation + ", source=" + mImage
                + ", buffer=" + mImage.getHardwareBuffer() + "}";
    }
}