OLD | NEW |
(Empty) | |
| 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 "ImageBuffer.h" |
| 37 #include "Noncopyable.h" |
| 38 #include "TransformationMatrix.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 { |
| 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 // Returns the context for drawing into, which may be the destination |
| 138 // context, or a temporary one. |
| 139 GraphicsContext* context() const { return m_drawContext; } |
| 140 |
| 141 PlatformGraphicsContext* platformContext() const { return m_drawContext->pla
tformContext(); } |
| 142 |
| 143 // When the mode is TextComposite, this sets the color that the text will |
| 144 // get. See the enum above for more. |
| 145 void setTextCompositeColor(Color color); |
| 146 |
| 147 // Returns the input bounds translated into the destination space. This is |
| 148 // not necessary for KeepTransform since the rectangle will be unchanged. |
| 149 const IntRect& drawRect() { return m_drawRect; } |
| 150 |
| 151 private: |
| 152 friend TransparencyWin_NoLayer_Test; |
| 153 friend TransparencyWin_WhiteLayer_Test; |
| 154 friend TransparencyWin_TextComposite_Test; |
| 155 friend TransparencyWin_OpaqueCompositeLayer_Test; |
| 156 |
| 157 void setupLayer(const IntRect& region); |
| 158 void setupTransform(const IntRect& region); |
| 159 |
| 160 void initializeNewContext(); |
| 161 |
| 162 void compositeOpaqueComposite(); |
| 163 void compositeTextComposite(); |
| 164 |
| 165 // Fixes the alpha channel to make the region inside m_transformedRect |
| 166 // opaque. |
| 167 void makeLayerOpaque(); |
| 168 |
| 169 // The context our drawing will eventually end up in. |
| 170 GraphicsContext* m_destContext; |
| 171 |
| 172 // The original transform from the destination context. |
| 173 TransformationMatrix m_orgTransform; |
| 174 |
| 175 LayerMode m_layerMode; |
| 176 TransformMode m_transformMode; |
| 177 |
| 178 // The rectangle we're drawing in the destination's coordinate space |
| 179 IntRect m_sourceRect; |
| 180 |
| 181 // The source rectangle transformed into pixels in the final image. For |
| 182 // Untransform this has no meaning, since the destination might not be a |
| 183 // rectangle. |
| 184 IntRect m_transformedSourceRect; |
| 185 |
| 186 // The size of the layer we created. If there's no layer, this is the size |
| 187 // of the region we're using in the source. |
| 188 IntSize m_layerSize; |
| 189 |
| 190 // The rectangle we're drawing to in the draw context's coordinate space. |
| 191 // This will be the same as the source rectangle except for ScaleTransform |
| 192 // where we create a new virtual coordinate space for the layer. |
| 193 IntRect m_drawRect; |
| 194 |
| 195 // Points to the graphics context to draw text to, which will either be |
| 196 // the original context or the copy, depending on our mode. |
| 197 GraphicsContext* m_drawContext; |
| 198 |
| 199 // This flag is set when we call save() on the draw context during |
| 200 // initialization. It allows us to avoid doing an extra save()/restore() |
| 201 // when one is unnecessary. |
| 202 bool m_savedOnDrawContext; |
| 203 |
| 204 // Used only when m_mode = TextComposite, this is the color that the text |
| 205 // will end up being once we figure out the transparency. |
| 206 Color m_textCompositeColor; |
| 207 |
| 208 // Layer we're drawing to. |
| 209 ImageBuffer* m_layerBuffer; |
| 210 |
| 211 // When the layer type is OpaqueCompositeLayer, this will contain a copy |
| 212 // of the original contents of the m_layerBuffer before Windows drew on it. |
| 213 // It allows us to re-create what Windows did to the layer. It is an |
| 214 // SkBitmap instead of an ImageBuffer because an SkBitmap is lighter-weight |
| 215 // (ImageBuffers are also GDI surfaces, which we don't need here). |
| 216 SkBitmap* m_referenceBitmap; |
| 217 |
| 218 // Don't access directly. If a buffer was too big to be cached, it will be |
| 219 // created temporarily, and this member tracks its scope to make sure it |
| 220 // gets deleted. Always use m_layerBuffer, which will either point to this |
| 221 // object, or the statically cached one. |
| 222 OwnPtr<ImageBuffer> m_ownedBuffer; |
| 223 OwnPtr<SkBitmap> m_ownedReferenceBuffer; |
| 224 }; |
| 225 |
| 226 } // namespace WebCore |
| 227 |
| 228 #endif // TransaprencyWin_h |
OLD | NEW |