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_OUTPUT_DIRECT_RENDERER_H_ 6 #define CC_OUTPUT_DIRECT_RENDERER_H_ 7 8 #include "base/basictypes.h" 9 #include "base/callback.h" 10 #include "cc/base/cc_export.h" 11 #include "cc/output/renderer.h" 12 #include "cc/resources/resource_provider.h" 13 #include "cc/resources/scoped_resource.h" 14 15 namespace cc { 16 17 class ResourceProvider; 18 19 // This is the base class for code shared between the GL and software 20 // renderer implementations. "Direct" refers to the fact that it does not 21 // delegate rendering to another compositor. 22 class CC_EXPORT DirectRenderer : public Renderer { 23 public: 24 virtual ~DirectRenderer(); 25 26 ResourceProvider* resource_provider() const { return resource_provider_; } 27 28 virtual bool CanReadPixels() const OVERRIDE; 29 virtual void DecideRenderPassAllocationsForFrame( 30 const RenderPassList& render_passes_in_draw_order) OVERRIDE; 31 virtual bool HaveCachedResourcesForRenderPassId(RenderPass::Id id) const 32 OVERRIDE; 33 virtual void DrawFrame(RenderPassList* render_passes_in_draw_order, 34 bool disable_picture_quad_image_filtering) OVERRIDE; 35 36 struct CC_EXPORT DrawingFrame { 37 DrawingFrame(); 38 ~DrawingFrame(); 39 40 const RenderPass* root_render_pass; 41 const RenderPass* current_render_pass; 42 const ScopedResource* current_texture; 43 44 gfx::RectF root_damage_rect; 45 46 gfx::Transform projection_matrix; 47 gfx::Transform window_matrix; 48 49 bool disable_picture_quad_image_filtering; 50 }; 51 52 void SetEnlargePassTextureAmountForTesting(gfx::Vector2d amount); 53 54 protected: 55 DirectRenderer(RendererClient* client, 56 OutputSurface* output_surface, 57 ResourceProvider* resource_provider); 58 59 class CachedResource : public ScopedResource { 60 public: 61 static scoped_ptr<CachedResource> Create( 62 ResourceProvider* resource_provider) { 63 return make_scoped_ptr(new CachedResource(resource_provider)); 64 } 65 virtual ~CachedResource() {} 66 67 bool is_complete() const { return is_complete_; } 68 void set_is_complete(bool is_complete) { is_complete_ = is_complete; } 69 70 protected: 71 explicit CachedResource(ResourceProvider* resource_provider) 72 : ScopedResource(resource_provider), 73 is_complete_(false) {} 74 75 private: 76 bool is_complete_; 77 78 DISALLOW_COPY_AND_ASSIGN(CachedResource); 79 }; 80 81 static gfx::RectF QuadVertexRect(); 82 static void QuadRectTransform(gfx::Transform* quad_rect_transform, 83 const gfx::Transform& quad_transform, 84 const gfx::RectF& quad_rect); 85 void InitializeViewport(DrawingFrame* frame, 86 gfx::Rect draw_rect, 87 gfx::Rect viewport_rect, 88 gfx::Size surface_size); 89 gfx::Rect MoveFromDrawToWindowSpace(const gfx::RectF& draw_rect) const; 90 91 gfx::Rect DeviceClipRect(const DrawingFrame* frame) const; 92 static gfx::RectF ComputeScissorRectForRenderPass(const DrawingFrame* frame); 93 void SetScissorStateForQuad(const DrawingFrame* frame, const DrawQuad& quad); 94 void SetScissorStateForQuadWithRenderPassScissor( 95 const DrawingFrame* frame, 96 const DrawQuad& quad, 97 const gfx::RectF& render_pass_scissor, 98 bool* should_skip_quad); 99 void SetScissorTestRectInDrawSpace(const DrawingFrame* frame, 100 gfx::RectF draw_space_rect); 101 102 static gfx::Size RenderPassTextureSize(const RenderPass* render_pass); 103 static GLenum RenderPassTextureFormat(const RenderPass* render_pass); 104 105 void DrawRenderPass(DrawingFrame* frame, const RenderPass* render_pass); 106 bool UseRenderPass(DrawingFrame* frame, const RenderPass* render_pass); 107 108 virtual void BindFramebufferToOutputSurface(DrawingFrame* frame) = 0; 109 virtual bool BindFramebufferToTexture(DrawingFrame* frame, 110 const ScopedResource* resource, 111 gfx::Rect target_rect) = 0; 112 virtual void SetDrawViewport(gfx::Rect window_space_viewport) = 0; 113 virtual void SetScissorTestRect(gfx::Rect scissor_rect) = 0; 114 virtual void ClearFramebuffer(DrawingFrame* frame) = 0; 115 virtual void DoDrawQuad(DrawingFrame* frame, const DrawQuad* quad) = 0; 116 virtual void BeginDrawingFrame(DrawingFrame* frame) = 0; 117 virtual void FinishDrawingFrame(DrawingFrame* frame) = 0; 118 virtual void FinishDrawingQuadList(); 119 virtual bool FlippedFramebuffer() const = 0; 120 virtual void EnsureScissorTestEnabled() = 0; 121 virtual void EnsureScissorTestDisabled() = 0; 122 virtual void DiscardBackbuffer() {} 123 virtual void EnsureBackbuffer() {} 124 125 virtual void CopyCurrentRenderPassToBitmap( 126 DrawingFrame* frame, 127 scoped_ptr<CopyOutputRequest> request) = 0; 128 129 ScopedPtrHashMap<RenderPass::Id, CachedResource> render_pass_textures_; 130 OutputSurface* output_surface_; 131 ResourceProvider* resource_provider_; 132 133 // For use in coordinate conversion, this stores the output rect, viewport 134 // rect (= unflipped version of glViewport rect), and the size of target 135 // framebuffer. During a draw, this stores the values for the current render 136 // pass; in between draws, they retain the values for the root render pass of 137 // the last draw. 138 gfx::Rect current_draw_rect_; 139 gfx::Rect current_viewport_rect_; 140 gfx::Size current_surface_size_; 141 142 private: 143 gfx::Vector2d enlarge_pass_texture_amount_; 144 145 DISALLOW_COPY_AND_ASSIGN(DirectRenderer); 146 }; 147 148 } // namespace cc 149 150 #endif // CC_OUTPUT_DIRECT_RENDERER_H_ 151