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 "core/platform/graphics/ImageBuffer.h"
     37 #include "core/platform/graphics/transforms/AffineTransform.h"
     38 
     39 #include "wtf/Noncopyable.h"
     40 #include "wtf/OwnPtr.h"
     41 
     42 class SkBitmap;
     43 class SkCanvas;
     44 
     45 namespace WebCore {
     46 
     47 class GraphicsContext;
     48 class TransparencyWin_NoLayer_Test;
     49 class TransparencyWin_WhiteLayer_Test;
     50 class TransparencyWin_TextComposite_Test;
     51 class TransparencyWin_OpaqueCompositeLayer_Test;
     52 
     53 // Helper class that abstracts away drawing ClearType text and Windows form
     54 // controls either to the original context directly, or to an offscreen context
     55 // that is composited later manually. This is to get around Windows' inability
     56 // to handle the alpha channel, semitransparent text, and transformed form
     57 // controls.
     58 class TransparencyWin {
     59     WTF_MAKE_NONCOPYABLE(TransparencyWin);
     60 public:
     61     enum LayerMode {
     62         // No extra layer is created. Drawing will happen to the source.
     63         // Valid only with KeepTransform and ScaleTransform. The region being
     64         // drawn onto must be opaque, since the modified region will be forced
     65         // to opaque when drawing is complete.
     66         NoLayer,
     67 
     68         // Makes a temporary layer consisting of the composited layers below
     69         // it. This result must be opaque. When complete, the result will be
     70         // compared to the original, and the difference will be added to a thee
     71         // destination layer.
     72         //
     73         // This mode only works if the lower layers are opque (normally the
     74         // case for a web page) and layers are only drawn in the stack order,
     75         // meaning you can never draw underneath a layer.
     76         //
     77         // This doesn't technically produce the correct answer in all cases. If
     78         // you have an opaque base, a transparency layer, than a semitransparent
     79         // drawing on top, the result will actually be blended in twice. But
     80         // this isn't a very important case. This mode is used for form
     81         // controls which are always opaque except for occationally some
     82         // antialiasing. It means form control antialiasing will be too light in
     83         // some cases, but only if you have extra layers.
     84         OpaqueCompositeLayer,
     85 
     86         // Allows semitransparent text to be drawn on any background (even if it
     87         // is itself semitransparent), but disables ClearType.
     88         //
     89         // It makes a trmporary layer filled with white. This is composited with
     90         // the lower layer with a custom color applied to produce the result.
     91         // The caller must draw the text in black, and set the desired final
     92         // text color by calling setTextCompositeColor().
     93         //
     94         // Only valid with KeepTransform, which is the only mode where drawing
     95         // text in this fashion makes sense.
     96         TextComposite,
     97 
     98         // Makes a temporary layer filled with white. When complete, the layer
     99         // will be forced to be opqaue (since Windows may have messed up the
    100         // alpha channel) and composited down. Any areas not drawn into will
    101         // remain white.
    102         //
    103         // This is the mode of last resort. If the opacity of the final image
    104         // is unknown and we can't do the text trick (since we know its color),
    105         // then we have to live with potential white halos. This is used for
    106         // form control drawing, for example.
    107         WhiteLayer,
    108     };
    109 
    110     enum TransformMode {
    111         // There are no changes to the transform. Use this when drawing
    112         // horizontal text. The current transform must not have rotation.
    113         KeepTransform,
    114 
    115         // Drawing happens in an Untransformed space, and then that bitmap is
    116         // transformed according to the current context when it is copied down.
    117         // Requires that a layer be created (layer mode is not NoLayer).
    118         Untransform,
    119 
    120         // When the current transform only has a scaling factor applied and
    121         // you're drawing form elements, use this parameter. This will unscale
    122         // the coordinate space, so the OS will just draw the form controls
    123         // larger or smaller depending on the destination size.
    124         ScaleTransform,
    125     };
    126 
    127     // You MUST call init() below.
    128     // |region| is expressed relative to the current transformation.
    129     TransparencyWin();
    130     ~TransparencyWin();
    131 
    132     // Initializes the members if you use the 0-argument constructor. Don't call
    133     // this if you use the multiple-argument constructor.
    134     void init(GraphicsContext* dest,
    135               LayerMode layerMode,
    136               TransformMode transformMode,
    137               const IntRect& region);
    138 
    139     // Combines the source and destination bitmaps using the given mode.
    140     // Calling this function before the destructor runs is mandatory in most
    141     // cases, and harmless otherwise.  The mandatory cases are:
    142     //       (m_layerMode != NoLayer) || (m_transformMode == ScaleTransform)
    143     void composite();
    144 
    145     // Returns the context for drawing into, which may be the destination
    146     // context, or a temporary one.
    147     GraphicsContext* context() const { return m_drawContext; }
    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