Home | History | Annotate | Download | only in quads
      1 // Copyright 2012 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #ifndef CC_QUADS_DRAW_QUAD_H_
      6 #define CC_QUADS_DRAW_QUAD_H_
      7 
      8 #include "base/callback.h"
      9 #include "cc/base/cc_export.h"
     10 #include "cc/quads/shared_quad_state.h"
     11 #include "cc/resources/resource_provider.h"
     12 
     13 namespace base {
     14 class Value;
     15 class DictionaryValue;
     16 }
     17 
     18 namespace cc {
     19 
     20 // DrawQuad is a bag of data used for drawing a quad. Because different
     21 // materials need different bits of per-quad data to render, classes that derive
     22 // from DrawQuad store additional data in their derived instance. The Material
     23 // enum is used to "safely" downcast to the derived class.
     24 // Note: quads contain rects and sizes, which live in different spaces. There is
     25 // the "content space", which is the arbitrary space in which the quad's
     26 // geometry is defined (generally related to the layer that produced the quad,
     27 // e.g. the content space for TiledLayerImpls, or the geometry space for
     28 // PictureLayerImpls). There is also the "target space", which is the space, in
     29 // "physical" pixels, of the render target where the quads is drawn. The quad's
     30 // transform maps the content space to the target space.
     31 class CC_EXPORT DrawQuad {
     32  public:
     33   enum Material {
     34     INVALID,
     35     CHECKERBOARD,
     36     DEBUG_BORDER,
     37     IO_SURFACE_CONTENT,
     38     PICTURE_CONTENT,
     39     RENDER_PASS,
     40     SOLID_COLOR,
     41     STREAM_VIDEO_CONTENT,
     42     SURFACE_CONTENT,
     43     TEXTURE_CONTENT,
     44     TILED_CONTENT,
     45     YUV_VIDEO_CONTENT,
     46     MATERIAL_LAST = YUV_VIDEO_CONTENT
     47   };
     48 
     49   virtual ~DrawQuad();
     50 
     51   scoped_ptr<DrawQuad> Copy(
     52       const SharedQuadState* copied_shared_quad_state) const;
     53 
     54   // TODO(danakj): Chromify or remove these SharedQuadState helpers.
     55   const gfx::Transform& quadTransform() const {
     56     return shared_quad_state->content_to_target_transform;
     57   }
     58   gfx::Rect visibleContentRect() const {
     59     return shared_quad_state->visible_content_rect;
     60   }
     61   gfx::Rect clipRect() const { return shared_quad_state->clip_rect; }
     62   bool isClipped() const { return shared_quad_state->is_clipped; }
     63   float opacity() const { return shared_quad_state->opacity; }
     64 
     65   Material material;
     66 
     67   // This rect, after applying the quad_transform(), gives the geometry that
     68   // this quad should draw to. This rect lives in content space.
     69   gfx::Rect rect;
     70 
     71   // This specifies the region of the quad that is opaque. This rect lives in
     72   // content space.
     73   gfx::Rect opaque_rect;
     74 
     75   // Allows changing the rect that gets drawn to make it smaller. This value
     76   // should be clipped to |rect|. This rect lives in content space.
     77   gfx::Rect visible_rect;
     78 
     79   // By default blending is used when some part of the quad is not opaque.
     80   // With this setting, it is possible to force blending on regardless of the
     81   // opaque area.
     82   bool needs_blending;
     83 
     84   // Stores state common to a large bundle of quads; kept separate for memory
     85   // efficiency. There is special treatment to reconstruct these pointers
     86   // during serialization.
     87   const SharedQuadState* shared_quad_state;
     88 
     89   bool IsDebugQuad() const { return material == DEBUG_BORDER; }
     90 
     91   bool ShouldDrawWithBlending() const {
     92     if (needs_blending || shared_quad_state->opacity < 1.0f)
     93       return true;
     94     if (visible_rect.IsEmpty())
     95       return false;
     96     return !opaque_rect.Contains(visible_rect);
     97   }
     98 
     99   typedef ResourceProvider::ResourceId ResourceId;
    100   typedef base::Callback<ResourceId(ResourceId)> ResourceIteratorCallback;
    101   virtual void IterateResources(const ResourceIteratorCallback& callback) = 0;
    102 
    103   // Is the left edge of this tile aligned with the originating layer's
    104   // left edge?
    105   bool IsLeftEdge() const { return !rect.x(); }
    106 
    107   // Is the top edge of this tile aligned with the originating layer's
    108   // top edge?
    109   bool IsTopEdge() const { return !rect.y(); }
    110 
    111   // Is the right edge of this tile aligned with the originating layer's
    112   // right edge?
    113   bool IsRightEdge() const {
    114     return rect.right() == shared_quad_state->content_bounds.width();
    115   }
    116 
    117   // Is the bottom edge of this tile aligned with the originating layer's
    118   // bottom edge?
    119   bool IsBottomEdge() const {
    120     return rect.bottom() == shared_quad_state->content_bounds.height();
    121   }
    122 
    123   // Is any edge of this tile aligned with the originating layer's
    124   // corresponding edge?
    125   bool IsEdge() const {
    126     return IsLeftEdge() || IsTopEdge() || IsRightEdge() || IsBottomEdge();
    127   }
    128 
    129   scoped_ptr<base::Value> AsValue() const;
    130 
    131  protected:
    132   DrawQuad();
    133 
    134   void SetAll(const SharedQuadState* shared_quad_state,
    135               Material material,
    136               const gfx::Rect& rect,
    137               const gfx::Rect& opaque_rect,
    138               const gfx::Rect& visible_rect,
    139               bool needs_blending);
    140   virtual void ExtendValue(base::DictionaryValue* value) const = 0;
    141 };
    142 
    143 }  // namespace cc
    144 
    145 #endif  // CC_QUADS_DRAW_QUAD_H_
    146