16c319ca1275c8db892c39b48fc54864c949f9171Romain Guy/*
26c319ca1275c8db892c39b48fc54864c949f9171Romain Guy * Copyright (C) 2011 The Android Open Source Project
36c319ca1275c8db892c39b48fc54864c949f9171Romain Guy *
46c319ca1275c8db892c39b48fc54864c949f9171Romain Guy * Licensed under the Apache License, Version 2.0 (the "License");
56c319ca1275c8db892c39b48fc54864c949f9171Romain Guy * you may not use this file except in compliance with the License.
66c319ca1275c8db892c39b48fc54864c949f9171Romain Guy * You may obtain a copy of the License at
76c319ca1275c8db892c39b48fc54864c949f9171Romain Guy *
86c319ca1275c8db892c39b48fc54864c949f9171Romain Guy *      http://www.apache.org/licenses/LICENSE-2.0
96c319ca1275c8db892c39b48fc54864c949f9171Romain Guy *
106c319ca1275c8db892c39b48fc54864c949f9171Romain Guy * Unless required by applicable law or agreed to in writing, software
116c319ca1275c8db892c39b48fc54864c949f9171Romain Guy * distributed under the License is distributed on an "AS IS" BASIS,
126c319ca1275c8db892c39b48fc54864c949f9171Romain Guy * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
136c319ca1275c8db892c39b48fc54864c949f9171Romain Guy * See the License for the specific language governing permissions and
146c319ca1275c8db892c39b48fc54864c949f9171Romain Guy * limitations under the License.
156c319ca1275c8db892c39b48fc54864c949f9171Romain Guy */
166c319ca1275c8db892c39b48fc54864c949f9171Romain Guy
176c319ca1275c8db892c39b48fc54864c949f9171Romain Guypackage android.view;
186c319ca1275c8db892c39b48fc54864c949f9171Romain Guy
1902ccac69fd1c0a03c24c5f3ace0ad4bed337b1fdRomain Guyimport android.graphics.Bitmap;
20c89b14bba0f6cc2c91629080617f7ed215f697f3Romain Guyimport android.graphics.Canvas;
21302a9df1d50373c82923bb84ff665dfce584fb22Romain Guyimport android.graphics.Matrix;
22d15ebf25c595b855f6978d0600218e3ea5f31e92Chet Haaseimport android.graphics.Paint;
232bf68f063b0077ddef6ebfe54f2ae5e063c2c229Romain Guyimport android.graphics.Rect;
246c319ca1275c8db892c39b48fc54864c949f9171Romain Guy
256c319ca1275c8db892c39b48fc54864c949f9171Romain Guy/**
266c319ca1275c8db892c39b48fc54864c949f9171Romain Guy * A hardware layer can be used to render graphics operations into a hardware
276c319ca1275c8db892c39b48fc54864c949f9171Romain Guy * friendly buffer. For instance, with an OpenGL backend, a hardware layer
286c319ca1275c8db892c39b48fc54864c949f9171Romain Guy * would use a Frame Buffer Object (FBO.) The hardware layer can be used as
296c319ca1275c8db892c39b48fc54864c949f9171Romain Guy * a drawing cache when a complex set of graphics operations needs to be
306c319ca1275c8db892c39b48fc54864c949f9171Romain Guy * drawn several times.
316c319ca1275c8db892c39b48fc54864c949f9171Romain Guy */
326c319ca1275c8db892c39b48fc54864c949f9171Romain Guyabstract class HardwareLayer {
33aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    /**
34aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * Indicates an unknown dimension (width or height.)
35aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     */
36aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    static final int DIMENSION_UNDEFINED = -1;
37aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
386c319ca1275c8db892c39b48fc54864c949f9171Romain Guy    int mWidth;
396c319ca1275c8db892c39b48fc54864c949f9171Romain Guy    int mHeight;
40a1cff5043d0fbd78fcf9c48e7658e56a5b0c2de3Chet Haase    DisplayList mDisplayList;
416c319ca1275c8db892c39b48fc54864c949f9171Romain Guy
4202ccac69fd1c0a03c24c5f3ace0ad4bed337b1fdRomain Guy    boolean mOpaque;
436c319ca1275c8db892c39b48fc54864c949f9171Romain Guy
446c319ca1275c8db892c39b48fc54864c949f9171Romain Guy    /**
45aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     * Creates a new hardware layer with undefined dimensions.
46aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy     */
47aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    HardwareLayer() {
48aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy        this(DIMENSION_UNDEFINED, DIMENSION_UNDEFINED, false);
49aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    }
50aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy
51aa6c24c21c727a196451332448d4e3b11a80be69Romain Guy    /**
526c319ca1275c8db892c39b48fc54864c949f9171Romain Guy     * Creates a new hardware layer at least as large as the supplied
536c319ca1275c8db892c39b48fc54864c949f9171Romain Guy     * dimensions.
546c319ca1275c8db892c39b48fc54864c949f9171Romain Guy     *
556c319ca1275c8db892c39b48fc54864c949f9171Romain Guy     * @param width The minimum width of the layer
566c319ca1275c8db892c39b48fc54864c949f9171Romain Guy     * @param height The minimum height of the layer
576c319ca1275c8db892c39b48fc54864c949f9171Romain Guy     * @param isOpaque Whether the layer should be opaque or not
586c319ca1275c8db892c39b48fc54864c949f9171Romain Guy     */
596c319ca1275c8db892c39b48fc54864c949f9171Romain Guy    HardwareLayer(int width, int height, boolean isOpaque) {
606c319ca1275c8db892c39b48fc54864c949f9171Romain Guy        mWidth = width;
616c319ca1275c8db892c39b48fc54864c949f9171Romain Guy        mHeight = height;
626c319ca1275c8db892c39b48fc54864c949f9171Romain Guy        mOpaque = isOpaque;
636c319ca1275c8db892c39b48fc54864c949f9171Romain Guy    }
646c319ca1275c8db892c39b48fc54864c949f9171Romain Guy
656c319ca1275c8db892c39b48fc54864c949f9171Romain Guy    /**
66d15ebf25c595b855f6978d0600218e3ea5f31e92Chet Haase     * Update the paint used when drawing this layer.
67d15ebf25c595b855f6978d0600218e3ea5f31e92Chet Haase     *
68d15ebf25c595b855f6978d0600218e3ea5f31e92Chet Haase     * @param paint The paint used when the layer is drawn into the destination canvas.
69d15ebf25c595b855f6978d0600218e3ea5f31e92Chet Haase     * @see View#setLayerPaint(android.graphics.Paint)
70d15ebf25c595b855f6978d0600218e3ea5f31e92Chet Haase     */
71d15ebf25c595b855f6978d0600218e3ea5f31e92Chet Haase    void setLayerPaint(Paint paint) {}
72d15ebf25c595b855f6978d0600218e3ea5f31e92Chet Haase
73d15ebf25c595b855f6978d0600218e3ea5f31e92Chet Haase    /**
746c319ca1275c8db892c39b48fc54864c949f9171Romain Guy     * Returns the minimum width of the layer.
756c319ca1275c8db892c39b48fc54864c949f9171Romain Guy     *
766c319ca1275c8db892c39b48fc54864c949f9171Romain Guy     * @return The minimum desired width of the hardware layer
776c319ca1275c8db892c39b48fc54864c949f9171Romain Guy     */
786c319ca1275c8db892c39b48fc54864c949f9171Romain Guy    int getWidth() {
796c319ca1275c8db892c39b48fc54864c949f9171Romain Guy        return mWidth;
806c319ca1275c8db892c39b48fc54864c949f9171Romain Guy    }
816c319ca1275c8db892c39b48fc54864c949f9171Romain Guy
826c319ca1275c8db892c39b48fc54864c949f9171Romain Guy    /**
836c319ca1275c8db892c39b48fc54864c949f9171Romain Guy     * Returns the minimum height of the layer.
846c319ca1275c8db892c39b48fc54864c949f9171Romain Guy     *
856c319ca1275c8db892c39b48fc54864c949f9171Romain Guy     * @return The minimum desired height of the hardware layer
866c319ca1275c8db892c39b48fc54864c949f9171Romain Guy     */
876c319ca1275c8db892c39b48fc54864c949f9171Romain Guy    int getHeight() {
886c319ca1275c8db892c39b48fc54864c949f9171Romain Guy        return mHeight;
896c319ca1275c8db892c39b48fc54864c949f9171Romain Guy    }
906c319ca1275c8db892c39b48fc54864c949f9171Romain Guy
916c319ca1275c8db892c39b48fc54864c949f9171Romain Guy    /**
92a1cff5043d0fbd78fcf9c48e7658e56a5b0c2de3Chet Haase     * Returns the DisplayList for the layer.
93a1cff5043d0fbd78fcf9c48e7658e56a5b0c2de3Chet Haase     *
94a1cff5043d0fbd78fcf9c48e7658e56a5b0c2de3Chet Haase     * @return The DisplayList of the hardware layer
95a1cff5043d0fbd78fcf9c48e7658e56a5b0c2de3Chet Haase     */
96a1cff5043d0fbd78fcf9c48e7658e56a5b0c2de3Chet Haase    DisplayList getDisplayList() {
97a1cff5043d0fbd78fcf9c48e7658e56a5b0c2de3Chet Haase        return mDisplayList;
98a1cff5043d0fbd78fcf9c48e7658e56a5b0c2de3Chet Haase    }
99a1cff5043d0fbd78fcf9c48e7658e56a5b0c2de3Chet Haase
100a1cff5043d0fbd78fcf9c48e7658e56a5b0c2de3Chet Haase    /**
101a1cff5043d0fbd78fcf9c48e7658e56a5b0c2de3Chet Haase     * Sets the DisplayList for the layer.
102a1cff5043d0fbd78fcf9c48e7658e56a5b0c2de3Chet Haase     *
103a1cff5043d0fbd78fcf9c48e7658e56a5b0c2de3Chet Haase     * @param displayList The new DisplayList for this layer
104a1cff5043d0fbd78fcf9c48e7658e56a5b0c2de3Chet Haase     */
105a1cff5043d0fbd78fcf9c48e7658e56a5b0c2de3Chet Haase    void setDisplayList(DisplayList displayList) {
106a1cff5043d0fbd78fcf9c48e7658e56a5b0c2de3Chet Haase        mDisplayList = displayList;
107a1cff5043d0fbd78fcf9c48e7658e56a5b0c2de3Chet Haase    }
108a1cff5043d0fbd78fcf9c48e7658e56a5b0c2de3Chet Haase
109a1cff5043d0fbd78fcf9c48e7658e56a5b0c2de3Chet Haase    /**
1106c319ca1275c8db892c39b48fc54864c949f9171Romain Guy     * Returns whether or not this layer is opaque.
1116c319ca1275c8db892c39b48fc54864c949f9171Romain Guy     *
1126c319ca1275c8db892c39b48fc54864c949f9171Romain Guy     * @return True if the layer is opaque, false otherwise
1136c319ca1275c8db892c39b48fc54864c949f9171Romain Guy     */
1146c319ca1275c8db892c39b48fc54864c949f9171Romain Guy    boolean isOpaque() {
1156c319ca1275c8db892c39b48fc54864c949f9171Romain Guy        return mOpaque;
1166c319ca1275c8db892c39b48fc54864c949f9171Romain Guy    }
1176c319ca1275c8db892c39b48fc54864c949f9171Romain Guy
1186c319ca1275c8db892c39b48fc54864c949f9171Romain Guy    /**
119846a533945576e5cb1a66529ca3a52d71749f04fRomain Guy     * Sets whether or not this layer should be considered opaque.
120846a533945576e5cb1a66529ca3a52d71749f04fRomain Guy     *
121846a533945576e5cb1a66529ca3a52d71749f04fRomain Guy     * @param isOpaque True if the layer is opaque, false otherwise
122846a533945576e5cb1a66529ca3a52d71749f04fRomain Guy     */
123846a533945576e5cb1a66529ca3a52d71749f04fRomain Guy    abstract void setOpaque(boolean isOpaque);
124846a533945576e5cb1a66529ca3a52d71749f04fRomain Guy
125846a533945576e5cb1a66529ca3a52d71749f04fRomain Guy    /**
1266c319ca1275c8db892c39b48fc54864c949f9171Romain Guy     * Indicates whether this layer can be rendered.
1276c319ca1275c8db892c39b48fc54864c949f9171Romain Guy     *
1286c319ca1275c8db892c39b48fc54864c949f9171Romain Guy     * @return True if the layer can be rendered into, false otherwise
1296c319ca1275c8db892c39b48fc54864c949f9171Romain Guy     */
1306c319ca1275c8db892c39b48fc54864c949f9171Romain Guy    abstract boolean isValid();
1316c319ca1275c8db892c39b48fc54864c949f9171Romain Guy
1326c319ca1275c8db892c39b48fc54864c949f9171Romain Guy    /**
13302ccac69fd1c0a03c24c5f3ace0ad4bed337b1fdRomain Guy     * Resize the layer, if necessary, to be at least as large
1346c319ca1275c8db892c39b48fc54864c949f9171Romain Guy     * as the supplied dimensions.
1356c319ca1275c8db892c39b48fc54864c949f9171Romain Guy     *
1366c319ca1275c8db892c39b48fc54864c949f9171Romain Guy     * @param width The new desired minimum width for this layer
1376c319ca1275c8db892c39b48fc54864c949f9171Romain Guy     * @param height The new desired minimum height for this layer
138603f6de35f21d74ae242d52d501f4f5c25ff4f4cChet Haase     * @return True if the resulting layer is valid, false otherwise
1396c319ca1275c8db892c39b48fc54864c949f9171Romain Guy     */
140603f6de35f21d74ae242d52d501f4f5c25ff4f4cChet Haase    abstract boolean resize(int width, int height);
1416c319ca1275c8db892c39b48fc54864c949f9171Romain Guy
1426c319ca1275c8db892c39b48fc54864c949f9171Romain Guy    /**
1436c319ca1275c8db892c39b48fc54864c949f9171Romain Guy     * Returns a hardware canvas that can be used to render onto
1446c319ca1275c8db892c39b48fc54864c949f9171Romain Guy     * this layer.
1456c319ca1275c8db892c39b48fc54864c949f9171Romain Guy     *
1466c319ca1275c8db892c39b48fc54864c949f9171Romain Guy     * @return A hardware canvas, or null if a canvas cannot be created
1476c319ca1275c8db892c39b48fc54864c949f9171Romain Guy     */
1486c319ca1275c8db892c39b48fc54864c949f9171Romain Guy    abstract HardwareCanvas getCanvas();
1496c319ca1275c8db892c39b48fc54864c949f9171Romain Guy
1506c319ca1275c8db892c39b48fc54864c949f9171Romain Guy    /**
1516c319ca1275c8db892c39b48fc54864c949f9171Romain Guy     * Destroys resources without waiting for a GC.
1526c319ca1275c8db892c39b48fc54864c949f9171Romain Guy     */
1536c319ca1275c8db892c39b48fc54864c949f9171Romain Guy    abstract void destroy();
1546c319ca1275c8db892c39b48fc54864c949f9171Romain Guy
1556c319ca1275c8db892c39b48fc54864c949f9171Romain Guy    /**
1566c319ca1275c8db892c39b48fc54864c949f9171Romain Guy     * This must be invoked before drawing onto this layer.
157c89b14bba0f6cc2c91629080617f7ed215f697f3Romain Guy     * @param currentCanvas
1586c319ca1275c8db892c39b48fc54864c949f9171Romain Guy     */
159c89b14bba0f6cc2c91629080617f7ed215f697f3Romain Guy    abstract HardwareCanvas start(Canvas currentCanvas);
1606c319ca1275c8db892c39b48fc54864c949f9171Romain Guy
1616c319ca1275c8db892c39b48fc54864c949f9171Romain Guy    /**
1626c319ca1275c8db892c39b48fc54864c949f9171Romain Guy     * This must be invoked after drawing onto this layer.
163c89b14bba0f6cc2c91629080617f7ed215f697f3Romain Guy     * @param currentCanvas
1646c319ca1275c8db892c39b48fc54864c949f9171Romain Guy     */
165c89b14bba0f6cc2c91629080617f7ed215f697f3Romain Guy    abstract void end(Canvas currentCanvas);
16602ccac69fd1c0a03c24c5f3ace0ad4bed337b1fdRomain Guy
16702ccac69fd1c0a03c24c5f3ace0ad4bed337b1fdRomain Guy    /**
16802ccac69fd1c0a03c24c5f3ace0ad4bed337b1fdRomain Guy     * Copies this layer into the specified bitmap.
16902ccac69fd1c0a03c24c5f3ace0ad4bed337b1fdRomain Guy     *
17002ccac69fd1c0a03c24c5f3ace0ad4bed337b1fdRomain Guy     * @param bitmap The bitmap to copy they layer into
17102ccac69fd1c0a03c24c5f3ace0ad4bed337b1fdRomain Guy     *
17202ccac69fd1c0a03c24c5f3ace0ad4bed337b1fdRomain Guy     * @return True if the copy was successful, false otherwise
17302ccac69fd1c0a03c24c5f3ace0ad4bed337b1fdRomain Guy     */
17402ccac69fd1c0a03c24c5f3ace0ad4bed337b1fdRomain Guy    abstract boolean copyInto(Bitmap bitmap);
17502ccac69fd1c0a03c24c5f3ace0ad4bed337b1fdRomain Guy
17602ccac69fd1c0a03c24c5f3ace0ad4bed337b1fdRomain Guy    /**
17702ccac69fd1c0a03c24c5f3ace0ad4bed337b1fdRomain Guy     * Update the layer's properties. This method should be used
17802ccac69fd1c0a03c24c5f3ace0ad4bed337b1fdRomain Guy     * when the underlying storage is modified by an external entity.
17902ccac69fd1c0a03c24c5f3ace0ad4bed337b1fdRomain Guy     * To change the underlying storage, use the {@link #resize(int, int)}
18002ccac69fd1c0a03c24c5f3ace0ad4bed337b1fdRomain Guy     * method instead.
18102ccac69fd1c0a03c24c5f3ace0ad4bed337b1fdRomain Guy     *
18202ccac69fd1c0a03c24c5f3ace0ad4bed337b1fdRomain Guy     * @param width The new width of this layer
18302ccac69fd1c0a03c24c5f3ace0ad4bed337b1fdRomain Guy     * @param height The new height of this layer
18402ccac69fd1c0a03c24c5f3ace0ad4bed337b1fdRomain Guy     * @param isOpaque Whether this layer is opaque
18502ccac69fd1c0a03c24c5f3ace0ad4bed337b1fdRomain Guy     */
18602ccac69fd1c0a03c24c5f3ace0ad4bed337b1fdRomain Guy    void update(int width, int height, boolean isOpaque) {
18702ccac69fd1c0a03c24c5f3ace0ad4bed337b1fdRomain Guy        mWidth = width;
18802ccac69fd1c0a03c24c5f3ace0ad4bed337b1fdRomain Guy        mHeight = height;
18902ccac69fd1c0a03c24c5f3ace0ad4bed337b1fdRomain Guy        mOpaque = isOpaque;
19002ccac69fd1c0a03c24c5f3ace0ad4bed337b1fdRomain Guy    }
191302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy
192302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy    /**
193302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy     * Sets an optional transform on this layer.
194302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy     *
195302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy     * @param matrix The transform to apply to the layer.
196302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy     */
197302a9df1d50373c82923bb84ff665dfce584fb22Romain Guy    abstract void setTransform(Matrix matrix);
1982bf68f063b0077ddef6ebfe54f2ae5e063c2c229Romain Guy
1992bf68f063b0077ddef6ebfe54f2ae5e063c2c229Romain Guy    /**
2002bf68f063b0077ddef6ebfe54f2ae5e063c2c229Romain Guy     * Specifies the display list to use to refresh the layer.
2017e52caf6db5feef2b847cfaa3d13690257122c3aMichael Jurka     *
2022bf68f063b0077ddef6ebfe54f2ae5e063c2c229Romain Guy     * @param displayList The display list containing the drawing commands to
2032bf68f063b0077ddef6ebfe54f2ae5e063c2c229Romain Guy     *                    execute in this layer
2042bf68f063b0077ddef6ebfe54f2ae5e063c2c229Romain Guy     * @param dirtyRect The dirty region of the layer that needs to be redrawn
2052bf68f063b0077ddef6ebfe54f2ae5e063c2c229Romain Guy     */
20611cb642756093a4af901b1525375b1eb2b5c3e2bRomain Guy    abstract void redrawLater(DisplayList displayList, Rect dirtyRect);
207ef09a210dd6ea481158b7028ec2424a7f5769ed2Romain Guy
208ef09a210dd6ea481158b7028ec2424a7f5769ed2Romain Guy    /**
209ef09a210dd6ea481158b7028ec2424a7f5769ed2Romain Guy     * Indicates that this layer has lost its underlying storage.
210ef09a210dd6ea481158b7028ec2424a7f5769ed2Romain Guy     */
211ef09a210dd6ea481158b7028ec2424a7f5769ed2Romain Guy    abstract void clearStorage();
2126c319ca1275c8db892c39b48fc54864c949f9171Romain Guy}
213