blob: 1b242b3bd463d3055bd3b2d1df8b010413f442cc [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2006 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
17package android.view;
18
19import android.graphics.Canvas;
20import android.graphics.Rect;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080021
22/**
23 * Abstract interface to someone holding a display surface. Allows you to
24 * control the surface size and format, edit the pixels in the surface, and
25 * monitor changes to the surface. This interface is typically available
26 * through the {@link SurfaceView} class.
27 *
28 * <p>When using this interface from a thread different than the one running
29 * its {@link SurfaceView}, you will want to carefully read the
30 * {@link #lockCanvas} and {@link Callback#surfaceCreated Callback.surfaceCreated}.
31 */
32public interface SurfaceHolder {
Mathias Agopiand2112302010-12-07 19:38:17 -080033
34 /** @deprecated this is ignored, this value is set automatically when needed. */
Mathias Agopian317a6282009-08-13 17:29:02 -070035 @Deprecated
Mathias Agopiand2112302010-12-07 19:38:17 -080036 public static final int SURFACE_TYPE_NORMAL = 0;
37 /** @deprecated this is ignored, this value is set automatically when needed. */
Mathias Agopian317a6282009-08-13 17:29:02 -070038 @Deprecated
Mathias Agopiand2112302010-12-07 19:38:17 -080039 public static final int SURFACE_TYPE_HARDWARE = 1;
40 /** @deprecated this is ignored, this value is set automatically when needed. */
41 @Deprecated
42 public static final int SURFACE_TYPE_GPU = 2;
43 /** @deprecated this is ignored, this value is set automatically when needed. */
44 @Deprecated
45 public static final int SURFACE_TYPE_PUSH_BUFFERS = 3;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046
47 /**
48 * Exception that is thrown from {@link #lockCanvas} when called on a Surface
49 * whose is SURFACE_TYPE_PUSH_BUFFERS.
50 */
51 public static class BadSurfaceTypeException extends RuntimeException {
52 public BadSurfaceTypeException() {
53 }
54
55 public BadSurfaceTypeException(String name) {
56 super(name);
57 }
58 }
59
60 /**
61 * A client may implement this interface to receive information about
62 * changes to the surface. When used with a {@link SurfaceView}, the
63 * Surface being held is only available between calls to
64 * {@link #surfaceCreated(SurfaceHolder)} and
65 * {@link #surfaceDestroyed(SurfaceHolder). The Callback is set with
66 * {@link SurfaceHolder#addCallback SurfaceHolder.addCallback} method.
67 */
68 public interface Callback {
69 /**
70 * This is called immediately after the surface is first created.
71 * Implementations of this should start up whatever rendering code
72 * they desire. Note that only one thread can ever draw into
73 * a {@link Surface}, so you should not draw into the Surface here
74 * if your normal rendering will be in another thread.
75 *
76 * @param holder The SurfaceHolder whose surface is being created.
77 */
78 public void surfaceCreated(SurfaceHolder holder);
79
80 /**
81 * This is called immediately after any structural changes (format or
82 * size) have been made to the surface. You should at this point update
83 * the imagery in the surface. This method is always called at least
84 * once, after {@link #surfaceCreated}.
85 *
86 * @param holder The SurfaceHolder whose surface has changed.
87 * @param format The new PixelFormat of the surface.
88 * @param width The new width of the surface.
89 * @param height The new height of the surface.
90 */
91 public void surfaceChanged(SurfaceHolder holder, int format, int width,
92 int height);
93
94 /**
95 * This is called immediately before a surface is being destroyed. After
96 * returning from this call, you should no longer try to access this
97 * surface. If you have a rendering thread that directly accesses
98 * the surface, you must ensure that thread is no longer touching the
99 * Surface before returning from this function.
100 *
101 * @param holder The SurfaceHolder whose surface is being destroyed.
102 */
103 public void surfaceDestroyed(SurfaceHolder holder);
104 }
105
106 /**
Dianne Hackbornd76b67c2010-07-13 17:48:30 -0700107 * Additional callbacks that can be received for {@link Callback}.
108 */
109 public interface Callback2 extends Callback {
110 /**
111 * Called when the application needs to redraw the content of its
112 * surface, after it is resized or for some other reason. By not
113 * returning here until the redraw is complete, you can ensure that
114 * the user will not see your surface in a bad state (at its new
115 * size before it has been correctly drawn that way). This will
116 * typically be preceeded by a call to {@link #surfaceChanged}.
117 *
118 * @param holder The SurfaceHolder whose surface has changed.
119 */
120 public void surfaceRedrawNeeded(SurfaceHolder holder);
121 }
122
123 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800124 * Add a Callback interface for this holder. There can several Callback
125 * interfaces associated to a holder.
126 *
127 * @param callback The new Callback interface.
128 */
129 public void addCallback(Callback callback);
130
131 /**
132 * Removes a previously added Callback interface from this holder.
133 *
134 * @param callback The Callback interface to remove.
135 */
136 public void removeCallback(Callback callback);
137
138 /**
139 * Use this method to find out if the surface is in the process of being
140 * created from Callback methods. This is intended to be used with
141 * {@link Callback#surfaceChanged}.
142 *
143 * @return true if the surface is in the process of being created.
144 */
145 public boolean isCreating();
146
147 /**
Mathias Agopiand2112302010-12-07 19:38:17 -0800148 * Sets the surface's type.
149 *
150 * @deprecated this is ignored, this value is set automatically when needed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800151 */
Mathias Agopiand2112302010-12-07 19:38:17 -0800152 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800153 public void setType(int type);
154
155 /**
156 * Make the surface a fixed size. It will never change from this size.
157 * When working with a {link SurfaceView}, this must be called from the
158 * same thread running the SurfaceView's window.
159 *
160 * @param width The surface's width.
161 * @param height The surface's height.
162 */
163 public void setFixedSize(int width, int height);
164
165 /**
166 * Allow the surface to resized based on layout of its container (this is
167 * the default). When this is enabled, you should monitor
168 * {@link Callback#surfaceChanged} for changes to the size of the surface.
169 * When working with a {link SurfaceView}, this must be called from the
170 * same thread running the SurfaceView's window.
171 */
172 public void setSizeFromLayout();
173
174 /**
175 * Set the desired PixelFormat of the surface. The default is OPAQUE.
176 * When working with a {link SurfaceView}, this must be called from the
177 * same thread running the SurfaceView's window.
178 *
179 * @param format A constant from PixelFormat.
180 *
181 * @see android.graphics.PixelFormat
182 */
183 public void setFormat(int format);
184
185 /**
186 * Enable or disable option to keep the screen turned on while this
187 * surface is displayed. The default is false, allowing it to turn off.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800188 * This is safe to call from any thread.
189 *
190 * @param screenOn Supply to true to force the screen to stay on, false
191 * to allow it to turn off.
192 */
193 public void setKeepScreenOn(boolean screenOn);
194
195 /**
196 * Start editing the pixels in the surface. The returned Canvas can be used
197 * to draw into the surface's bitmap. A null is returned if the surface has
198 * not been created or otherwise can not be edited. You will usually need
199 * to implement {@link Callback#surfaceCreated Callback.surfaceCreated}
200 * to find out when the Surface is available for use.
201 *
202 * <p>The content of the Surface is never preserved between unlockCanvas() and
203 * lockCanvas(), for this reason, every pixel within the Surface area
204 * must be written. The only exception to this rule is when a dirty
205 * rectangle is specified, in which case, non dirty pixels will be
206 * preserved.
207 *
208 * <p>If you call this repeatedly when the Surface is not ready (before
209 * {@link Callback#surfaceCreated Callback.surfaceCreated} or after
210 * {@link Callback#surfaceDestroyed Callback.surfaceDestroyed}), your calls
211 * will be throttled to a slow rate in order to avoid consuming CPU.
212 *
213 * <p>If null is not returned, this function internally holds a lock until
214 * the corresponding {@link #unlockCanvasAndPost} call, preventing
215 * {@link SurfaceView} from creating, destroying, or modifying the surface
216 * while it is being drawn. This can be more convenience than accessing
217 * the Surface directly, as you do not need to do special synchronization
218 * with a drawing thread in {@link Callback#surfaceDestroyed
219 * Callback.surfaceDestroyed}.
220 *
221 * @return Canvas Use to draw into the surface.
222 */
223 public Canvas lockCanvas();
224
225
226 /**
227 * Just like {@link #lockCanvas()} but allows to specify a dirty rectangle.
228 * Every
229 * pixel within that rectangle must be written; however pixels outside
230 * the dirty rectangle will be preserved by the next call to lockCanvas().
231 *
232 * @see android.view.SurfaceHolder#lockCanvas
233 *
234 * @param dirty Area of the Surface that will be modified.
235 * @return Canvas Use to draw into the surface.
236 */
237 public Canvas lockCanvas(Rect dirty);
238
239 /**
240 * Finish editing pixels in the surface. After this call, the surface's
241 * current pixels will be shown on the screen, but its content is lost,
242 * in particular there is no guarantee that the content of the Surface
243 * will remain unchanged when lockCanvas() is called again.
244 *
245 * @see #lockCanvas()
246 *
247 * @param canvas The Canvas previously returned by lockCanvas().
248 */
249 public void unlockCanvasAndPost(Canvas canvas);
250
251 /**
252 * Retrieve the current size of the surface. Note: do not modify the
253 * returned Rect. This is only safe to call from the thread of
254 * {@link SurfaceView}'s window, or while inside of
255 * {@link #lockCanvas()}.
256 *
257 * @return Rect The surface's dimensions. The left and top are always 0.
258 */
259 public Rect getSurfaceFrame();
260
261 /**
262 * Direct access to the surface object. The Surface may not always be
263 * available -- for example when using a {@link SurfaceView} the holder's
264 * Surface is not created until the view has been attached to the window
265 * manager and performed a layout in order to determine the dimensions
266 * and screen position of the Surface. You will thus usually need
267 * to implement {@link Callback#surfaceCreated Callback.surfaceCreated}
268 * to find out when the Surface is available for use.
269 *
270 * <p>Note that if you directly access the Surface from another thread,
271 * it is critical that you correctly implement
272 * {@link Callback#surfaceCreated Callback.surfaceCreated} and
273 * {@link Callback#surfaceDestroyed Callback.surfaceDestroyed} to ensure
274 * that thread only accesses the Surface while it is valid, and that the
275 * Surface does not get destroyed while the thread is using it.
276 *
277 * <p>This method is intended to be used by frameworks which often need
278 * direct access to the Surface object (usually to pass it to native code).
279 * When designing APIs always use SurfaceHolder to pass surfaces around
280 * as opposed to the Surface object itself. A rule of thumb is that
281 * application code should never have to call this method.
282 *
283 * @return Surface The surface.
284 */
285 public Surface getSurface();
286}