Home | History | Annotate | Download | only in chromium
      1 /*
      2  * Copyright (C) 2009 Google Inc. All rights reserved.
      3  *
      4  * Redistribution and use in source and binary forms, with or without
      5  * modification, are permitted provided that the following conditions are
      6  * met:
      7  *
      8  *     * Redistributions of source code must retain the above copyright
      9  * notice, this list of conditions and the following disclaimer.
     10  *     * Redistributions in binary form must reproduce the above
     11  * copyright notice, this list of conditions and the following disclaimer
     12  * in the documentation and/or other materials provided with the
     13  * distribution.
     14  *     * Neither the name of Google Inc. nor the names of its
     15  * contributors may be used to endorse or promote products derived from
     16  * this software without specific prior written permission.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     29  */
     30 
     31 #ifndef TransparencyWin_h
     32 #define TransparencyWin_h
     33 
     34 #include <windows.h>
     35 
     36 #include "AffineTransform.h"
     37 #include "ImageBuffer.h"
     38 #include "Noncopyable.h"
     39 #include "wtf/OwnPtr.h"
     40 
     41 class SkBitmap;
     42 class SkCanvas;
     43 
     44 namespace WebCore {
     45 
     46 class GraphicsContext;
     47 class TransparencyWin_NoLayer_Test;
     48 class TransparencyWin_WhiteLayer_Test;
     49 class TransparencyWin_TextComposite_Test;
     50 class TransparencyWin_OpaqueCompositeLayer_Test;
     51 
     52 // Helper class that abstracts away drawing ClearType text and Windows form
     53 // controls either to the original context directly, or to an offscreen context
     54 // that is composited later manually. This is to get around Windows' inability
     55 // to handle the alpha channel, semitransparent text, and transformed form
     56 // controls.
     57 class TransparencyWin : public Noncopyable {
     58 public:
     59     enum LayerMode {
     60         // No extra layer is created. Drawing will happen to the source.
     61         // Valid only with KeepTransform and ScaleTransform. The region being
     62         // drawn onto must be opaque, since the modified region will be forced
     63         // to opaque when drawing is complete.
     64         NoLayer,
     65 
     66         // Makes a temporary layer consisting of the composited layers below
     67         // it. This result must be opaque. When complete, the result will be
     68         // compared to the original, and the difference will be added to a thee
     69         // destination layer.
     70         //
     71         // This mode only works if the lower layers are opque (normally the
     72         // case for a web page) and layers are only drawn in the stack order,
     73         // meaning you can never draw underneath a layer.
     74         //
     75         // This doesn't technically produce the correct answer in all cases. If
     76         // you have an opaque base, a transparency layer, than a semitransparent
     77         // drawing on top, the result will actually be blended in twice. But
     78         // this isn't a very important case. This mode is used for form
     79         // controls which are always opaque except for occationally some
     80         // antialiasing. It means form control antialiasing will be too light in
     81         // some cases, but only if you have extra layers.
     82         OpaqueCompositeLayer,
     83 
     84         // Allows semitransparent text to be drawn on any background (even if it
     85         // is itself semitransparent), but disables ClearType.
     86         //
     87         // It makes a trmporary layer filled with white. This is composited with
     88         // the lower layer with a custom color applied to produce the result.
     89         // The caller must draw the text in black, and set the desired final
     90         // text color by calling setTextCompositeColor().
     91         //
     92         // Only valid with KeepTransform, which is the only mode where drawing
     93         // text in this fashion makes sense.
     94         TextComposite,
     95 
     96         // Makes a temporary layer filled with white. When complete, the layer
     97         // will be forced to be opqaue (since Windows may have messed up the
     98         // alpha channel) and composited down. Any areas not drawn into will
     99         // remain white.
    100         //
    101         // This is the mode of last resort. If the opacity of the final image
    102         // is unknown and we can't do the text trick (since we know its color),
    103         // then we have to live with potential white halos. This is used for
    104         // form control drawing, for example.
    105         WhiteLayer,
    106     };
    107 
    108     enum TransformMode {
    109         // There are no changes to the transform. Use this when drawing
    110         // horizontal text. The current transform must not have rotation.
    111         KeepTransform,
    112 
    113         // Drawing happens in an Untransformed space, and then that bitmap is
    114         // transformed according to the current context when it is copied down.
    115         // Requires that a layer be created (layer mode is not NoLayer).
    116         Untransform,
    117 
    118         // When the current transform only has a scaling factor applied and
    119         // you're drawing form elements, use this parameter. This will unscale
    120         // the coordinate space, so the OS will just draw the form controls
    121         // larger or smaller depending on the destination size.
    122         ScaleTransform,
    123     };
    124 
    125     // You MUST call init() below.
    126     // |region| is expressed relative to the current transformation.
    127     TransparencyWin();
    128     ~TransparencyWin();
    129 
    130     // Initializes the members if you use the 0-argument constructor. Don't call
    131     // this if you use the multiple-argument constructor.
    132     void init(GraphicsContext* dest,
    133               LayerMode layerMode,
    134               TransformMode transformMode,
    135               const IntRect& region);
    136 
    137     // Combines the source and destination bitmaps using the given mode.
    138     // Calling this function before the destructor runs is mandatory in most
    139     // cases, and harmless otherwise.  The mandatory cases are:
    140     //       (m_layerMode != NoLayer) || (m_transformMode == ScaleTransform)
    141     void composite();
    142 
    143     // Returns the context for drawing into, which may be the destination
    144     // context, or a temporary one.
    145     GraphicsContext* context() const { return m_drawContext; }
    146 
    147     PlatformGraphicsContext* platformContext() const { return m_drawContext ? m_drawContext->platformContext() : 0; }
    148 
    149     // When the mode is TextComposite, this sets the color that the text will
    150     // get. See the enum above for more.
    151     void setTextCompositeColor(Color color);
    152 
    153     // Returns the input bounds translated into the destination space. This is
    154     // not necessary for KeepTransform since the rectangle will be unchanged.
    155     const IntRect& drawRect() { return m_drawRect; }
    156 
    157 private:
    158     friend TransparencyWin_NoLayer_Test;
    159     friend TransparencyWin_WhiteLayer_Test;
    160     friend TransparencyWin_TextComposite_Test;
    161     friend TransparencyWin_OpaqueCompositeLayer_Test;
    162 
    163     class OwnedBuffers;
    164 
    165     void computeLayerSize();
    166 
    167     // Sets up a new layer, if any. setupLayer() will call the appopriate layer-
    168     // specific helper. Must be called after computeLayerSize();
    169     void setupLayer();
    170     void setupLayerForNoLayer();
    171     void setupLayerForOpaqueCompositeLayer();
    172     void setupLayerForTextComposite();
    173     void setupLayerForWhiteLayer();
    174 
    175     // Sets up the transformation on the newly created layer. setupTransform()
    176     // will call the appropriate transform-specific helper. Must be called after
    177     // setupLayer().
    178     void setupTransform(const IntRect& region);
    179     void setupTransformForKeepTransform(const IntRect& region);
    180     void setupTransformForUntransform();
    181     void setupTransformForScaleTransform();
    182 
    183     void initializeNewContext();
    184 
    185     void compositeOpaqueComposite();
    186     void compositeTextComposite();
    187 
    188     // Fixes the alpha channel to make the region inside m_transformedRect
    189     // opaque.
    190     void makeLayerOpaque();
    191 
    192     // The context our drawing will eventually end up in.
    193     GraphicsContext* m_destContext;
    194 
    195     // The original transform from the destination context.
    196     AffineTransform m_orgTransform;
    197 
    198     LayerMode m_layerMode;
    199     TransformMode m_transformMode;
    200 
    201     // The rectangle we're drawing in the destination's coordinate space
    202     IntRect m_sourceRect;
    203 
    204     // The source rectangle transformed into pixels in the final image. For
    205     // Untransform this has no meaning, since the destination might not be a
    206     // rectangle.
    207     IntRect m_transformedSourceRect;
    208 
    209     // The size of the layer we created. If there's no layer, this is the size
    210     // of the region we're using in the source.
    211     IntSize m_layerSize;
    212 
    213     // The rectangle we're drawing to in the draw context's coordinate space.
    214     // This will be the same as the source rectangle except for ScaleTransform
    215     // where we create a new virtual coordinate space for the layer.
    216     IntRect m_drawRect;
    217 
    218     // Points to the graphics context to draw text to, which will either be
    219     // the original context or the copy, depending on our mode.
    220     GraphicsContext* m_drawContext;
    221 
    222     // This flag is set when we call save() on the draw context during
    223     // initialization. It allows us to avoid doing an extra save()/restore()
    224     // when one is unnecessary.
    225     bool m_savedOnDrawContext;
    226 
    227     // Used only when m_mode = TextComposite, this is the color that the text
    228     // will end up being once we figure out the transparency.
    229     Color m_textCompositeColor;
    230 
    231     // Layer we're drawing to.
    232     ImageBuffer* m_layerBuffer;
    233 
    234     // When the layer type is OpaqueCompositeLayer, this will contain a copy
    235     // of the original contents of the m_layerBuffer before Windows drew on it.
    236     // It allows us to re-create what Windows did to the layer. It is an
    237     // SkBitmap instead of an ImageBuffer because an SkBitmap is lighter-weight
    238     // (ImageBuffers are also GDI surfaces, which we don't need here).
    239     SkBitmap* m_referenceBitmap;
    240 
    241     // If the given size of bitmap can be cached, they will be stored here. Both
    242     // the bitmap and the reference are guaranteed to be allocated if this
    243     // member is non-null.
    244     static OwnedBuffers* m_cachedBuffers;
    245 
    246     // If a buffer was too big to be cached, it will be created temporarily, and
    247     // this member tracks its scope to make sure it gets deleted. Always use
    248     // m_layerBuffer, which will either point to this object, or the statically
    249     // cached one. Don't access directly.
    250     OwnPtr<OwnedBuffers> m_ownedBuffers;
    251 
    252     // Sometimes we're asked to create layers that have negative dimensions.
    253     // This API is not designed to fail to initialize, so we hide the fact
    254     // that they are illegal and can't be rendered (failing silently, drawing
    255     // nothing).
    256     bool m_validLayer;
    257 };
    258 
    259 } // namespace WebCore
    260 
    261 #endif // TransaprencyWin_h
    262