Home | History | Annotate | Download | only in graphics
      1 page.title=Surface and SurfaceHolder
      2 @jd:body
      3 
      4 <!--
      5     Copyright 2014 The Android Open Source Project
      6 
      7     Licensed under the Apache License, Version 2.0 (the "License");
      8     you may not use this file except in compliance with the License.
      9     You may obtain a copy of the License at
     10 
     11         http://www.apache.org/licenses/LICENSE-2.0
     12 
     13     Unless required by applicable law or agreed to in writing, software
     14     distributed under the License is distributed on an "AS IS" BASIS,
     15     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     16     See the License for the specific language governing permissions and
     17     limitations under the License.
     18 -->
     19 <div id="qv-wrapper">
     20   <div id="qv">
     21     <h2>In this document</h2>
     22     <ol id="auto-toc">
     23     </ol>
     24   </div>
     25 </div>
     26 
     27 <p>The
     28 <a href="http://developer.android.com/reference/android/view/Surface.html">Surface</a>
     29 class has been part of the public API since 1.0.  Its description simply says,
     30 "Handle onto a raw buffer that is being managed by the screen compositor."  The
     31 statement was accurate when initially written but falls well short of the mark
     32 on a modern system.</p>
     33 
     34 <p>The Surface represents the producer side of a buffer queue that is often (but
     35 not always!) consumed by SurfaceFlinger.  When you render onto a Surface, the
     36 result ends up in a buffer that gets shipped to the consumer.  A Surface is not
     37 simply a raw chunk of memory you can scribble on.</p>
     38 
     39 <p>The BufferQueue for a display Surface is typically configured for
     40 triple-buffering; but buffers are allocated on demand.  So if the producer
     41 generates buffers slowly enough -- maybe it's animating at 30fps on a 60fps
     42 display -- there might only be two allocated buffers in the queue.  This helps
     43 minimize memory consumption.  You can see a summary of the buffers associated
     44 with every layer in the <code>dumpsys SurfaceFlinger</code> output.</p>
     45 
     46 <h2 id="canvas">Canvas Rendering</h2>
     47 
     48 <p>Once upon a time, all rendering was done in software, and you can still do this
     49 today.  The low-level implementation is provided by the Skia graphics library.
     50 If you want to draw a rectangle, you make a library call, and it sets bytes in a
     51 buffer appropriately.  To ensure that a buffer isn't updated by two clients at
     52 once, or written to while being displayed, you have to lock the buffer to access
     53 it.  <code>lockCanvas()</code> locks the buffer and returns a Canvas to use for drawing,
     54 and <code>unlockCanvasAndPost()</code> unlocks the buffer and sends it to the compositor.</p>
     55 
     56 <p>As time went on, and devices with general-purpose 3D engines appeared, Android
     57 reoriented itself around OpenGL ES.  However, it was important to keep the old
     58 API working, for apps as well as app framework code, so an effort was made to
     59 hardware-accelerate the Canvas API.  As you can see from the charts on the
     60 <a href="http://developer.android.com/guide/topics/graphics/hardware-accel.html">Hardware
     61 Acceleration</a>
     62 page, this was a bit of a bumpy ride.  Note in particular that while the Canvas
     63 provided to a View's <code>onDraw()</code> method may be hardware-accelerated, the Canvas
     64 obtained when an app locks a Surface directly with <code>lockCanvas()</code> never is.</p>
     65 
     66 <p>When you lock a Surface for Canvas access, the "CPU renderer" connects to the
     67 producer side of the BufferQueue and does not disconnect until the Surface is
     68 destroyed.  Most other producers (like GLES) can be disconnected and reconnected
     69 to a Surface, but the Canvas-based "CPU renderer" cannot.  This means you can't
     70 draw on a surface with GLES or send it frames from a video decoder if you've
     71 ever locked it for a Canvas.</p>
     72 
     73 <p>The first time the producer requests a buffer from a BufferQueue, it is
     74 allocated and initialized to zeroes.  Initialization is necessary to avoid
     75 inadvertently sharing data between processes.  When you re-use a buffer,
     76 however, the previous contents will still be present.  If you repeatedly call
     77 <code>lockCanvas()</code> and <code>unlockCanvasAndPost()</code> without
     78 drawing anything, you'll cycle between previously-rendered frames.</p>
     79 
     80 <p>The Surface lock/unlock code keeps a reference to the previously-rendered
     81 buffer.  If you specify a dirty region when locking the Surface, it will copy
     82 the non-dirty pixels from the previous buffer.  There's a fair chance the buffer
     83 will be handled by SurfaceFlinger or HWC; but since we need to only read from
     84 it, there's no need to wait for exclusive access.</p>
     85 
     86 <p>The main non-Canvas way for an application to draw directly on a Surface is
     87 through OpenGL ES.  That's described in the <a href="#eglsurface">EGLSurface and
     88 OpenGL ES</a> section.</p>
     89 
     90 <h2 id="surfaceholder">SurfaceHolder</h2>
     91 
     92 <p>Some things that work with Surfaces want a SurfaceHolder, notably SurfaceView.
     93 The original idea was that Surface represented the raw compositor-managed
     94 buffer, while SurfaceHolder was managed by the app and kept track of
     95 higher-level information like the dimensions and format.  The Java-language
     96 definition mirrors the underlying native implementation.  It's arguably no
     97 longer useful to split it this way, but it has long been part of the public API.</p>
     98 
     99 <p>Generally speaking, anything having to do with a View will involve a
    100 SurfaceHolder.  Some other APIs, such as MediaCodec, will operate on the Surface
    101 itself.  You can easily get the Surface from the SurfaceHolder, so hang on to
    102 the latter when you have it.</p>
    103 
    104 <p>APIs to get and set Surface parameters, such as the size and format, are
    105 implemented through SurfaceHolder.</p>
    106