Home | History | Annotate | Download | only in win
      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 "platform/graphics/ImageBuffer.h"
     35 #include "platform/transforms/AffineTransform.h"
     36 #include "wtf/Noncopyable.h"
     37 #include "wtf/OwnPtr.h"
     38 #include <windows.h>
     39 
     40 class SkBitmap;
     41 class SkCanvas;
     42 
     43 namespace WebCore {
     44 
     45 class GraphicsContext;
     46 class TransparencyWin_NoLayer_Test;
     47 class TransparencyWin_WhiteLayer_Test;
     48 class TransparencyWin_TextComposite_Test;
     49 class TransparencyWin_OpaqueCompositeLayer_Test;
     50 
     51 // Helper class that abstracts away drawing ClearType text and Windows form
     52 // controls either to the original context directly, or to an offscreen context
     53 // that is composited later manually. This is to get around Windows' inability
     54 // to handle the alpha channel, semitransparent text, and transformed form
     55 // controls.
     56 class PLATFORM_EXPORT TransparencyWin {
     57     WTF_MAKE_NONCOPYABLE(TransparencyWin);
     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*, LayerMode, TransformMode, const IntRect&);
    133 
    134     // Combines the source and destination bitmaps using the given mode.
    135     // Calling this function before the destructor runs is mandatory in most
    136     // cases, and harmless otherwise. The mandatory cases are:
    137     //       (m_layerMode != NoLayer) || (m_transformMode == ScaleTransform)
    138     void composite();
    139 
    140     // Returns the context for drawing into, which may be the destination
    141     // context, or a temporary one.
    142     GraphicsContext* context() const { return m_drawContext; }
    143 
    144     // When the mode is TextComposite, this sets the color that the text will
    145     // get. See the enum above for more.
    146     void setTextCompositeColor(Color);
    147 
    148     // Returns the input bounds translated into the destination space. This is
    149     // not necessary for KeepTransform since the rectangle will be unchanged.
    150     const IntRect& drawRect() { return m_drawRect; }
    151 
    152 private:
    153     friend TransparencyWin_NoLayer_Test;
    154     friend TransparencyWin_WhiteLayer_Test;
    155     friend TransparencyWin_TextComposite_Test;
    156     friend TransparencyWin_OpaqueCompositeLayer_Test;
    157 
    158     class OwnedBuffers;
    159 
    160     void computeLayerSize();
    161 
    162     // Sets up a new layer, if any. setupLayer() will call the appopriate layer-
    163     // specific helper. Must be called after computeLayerSize();
    164     void setupLayer();
    165     void setupLayerForNoLayer();
    166     void setupLayerForOpaqueCompositeLayer();
    167     void setupLayerForTextComposite();
    168     void setupLayerForWhiteLayer();
    169 
    170     // Sets up the transformation on the newly created layer. setupTransform()
    171     // will call the appropriate transform-specific helper. Must be called after
    172     // setupLayer().
    173     void setupTransform(const IntRect& region);
    174     void setupTransformForKeepTransform(const IntRect& region);
    175     void setupTransformForUntransform();
    176     void setupTransformForScaleTransform();
    177 
    178     void initializeNewContext();
    179 
    180     void compositeOpaqueComposite();
    181     void compositeTextComposite();
    182 
    183     // Fixes the alpha channel to make the region inside m_transformedRect
    184     // opaque.
    185     void makeLayerOpaque();
    186 
    187     // The context our drawing will eventually end up in.
    188     GraphicsContext* m_destContext;
    189 
    190     // The original transform from the destination context.
    191     AffineTransform m_orgTransform;
    192 
    193     LayerMode m_layerMode;
    194     TransformMode m_transformMode;
    195 
    196     // The rectangle we're drawing in the destination's coordinate space
    197     IntRect m_sourceRect;
    198 
    199     // The source rectangle transformed into pixels in the final image. For
    200     // Untransform this has no meaning, since the destination might not be a
    201     // rectangle.
    202     IntRect m_transformedSourceRect;
    203 
    204     // The size of the layer we created. If there's no layer, this is the size
    205     // of the region we're using in the source.
    206     IntSize m_layerSize;
    207 
    208     // The rectangle we're drawing to in the draw context's coordinate space.
    209     // This will be the same as the source rectangle except for ScaleTransform
    210     // where we create a new virtual coordinate space for the layer.
    211     IntRect m_drawRect;
    212 
    213     // Points to the graphics context to draw text to, which will either be
    214     // the original context or the copy, depending on our mode.
    215     GraphicsContext* m_drawContext;
    216 
    217     // This flag is set when we call save() on the draw context during
    218     // initialization. It allows us to avoid doing an extra save()/restore()
    219     // when one is unnecessary.
    220     bool m_savedOnDrawContext;
    221 
    222     // Used only when m_mode = TextComposite, this is the color that the text
    223     // will end up being once we figure out the transparency.
    224     Color m_textCompositeColor;
    225 
    226     // Layer we're drawing to.
    227     ImageBuffer* m_layerBuffer;
    228 
    229     // When the layer type is OpaqueCompositeLayer, this will contain a copy
    230     // of the original contents of the m_layerBuffer before Windows drew on it.
    231     // It allows us to re-create what Windows did to the layer. It is an
    232     // SkBitmap instead of an ImageBuffer because an SkBitmap is lighter-weight
    233     // (ImageBuffers are also GDI surfaces, which we don't need here).
    234     SkBitmap* m_referenceBitmap;
    235 
    236     // If the given size of bitmap can be cached, they will be stored here. Both
    237     // the bitmap and the reference are guaranteed to be allocated if this
    238     // member is non-null.
    239     static OwnedBuffers* m_cachedBuffers;
    240 
    241     // If a buffer was too big to be cached, it will be created temporarily, and
    242     // this member tracks its scope to make sure it gets deleted. Always use
    243     // m_layerBuffer, which will either point to this object, or the statically
    244     // cached one. Don't access directly.
    245     OwnPtr<OwnedBuffers> m_ownedBuffers;
    246 
    247     // Sometimes we're asked to create layers that have negative dimensions.
    248     // This API is not designed to fail to initialize, so we hide the fact
    249     // that they are illegal and can't be rendered (failing silently, drawing
    250     // nothing).
    251     bool m_validLayer;
    252 };
    253 
    254 } // namespace WebCore
    255 
    256 #endif // TransaprencyWin_h
    257