Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / content / common / gpu / texture_image_transport_surface.h
1 // Copyright (c) 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 CONTENT_COMMON_GPU_TEXTURE_IMAGE_TRANSPORT_SURFACE_H_
6 #define CONTENT_COMMON_GPU_TEXTURE_IMAGE_TRANSPORT_SURFACE_H_
7
8 #include <string>
9
10 #include "base/basictypes.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "content/common/gpu/gpu_command_buffer_stub.h"
13 #include "content/common/gpu/image_transport_surface.h"
14 #include "gpu/command_buffer/common/mailbox.h"
15 #include "gpu/command_buffer/service/texture_manager.h"
16 #include "ui/gl/gl_context.h"
17 #include "ui/gl/gl_surface.h"
18
19 namespace content {
20 class GpuChannelManager;
21
22 class TextureImageTransportSurface
23     : public ImageTransportSurface,
24       public GpuCommandBufferStub::DestructionObserver,
25       public gfx::GLSurface {
26  public:
27   TextureImageTransportSurface(GpuChannelManager* manager,
28                                GpuCommandBufferStub* stub,
29                                const gfx::GLSurfaceHandle& handle);
30
31   // gfx::GLSurface implementation.
32   virtual bool Initialize() OVERRIDE;
33   virtual void Destroy() OVERRIDE;
34   virtual bool DeferDraws() OVERRIDE;
35   virtual bool IsOffscreen() OVERRIDE;
36   virtual bool SwapBuffers() OVERRIDE;
37   virtual gfx::Size GetSize() OVERRIDE;
38   virtual void* GetHandle() OVERRIDE;
39   virtual unsigned GetFormat() OVERRIDE;
40   virtual std::string GetExtensions() OVERRIDE;
41   virtual unsigned int GetBackingFrameBufferObject() OVERRIDE;
42   virtual bool PostSubBuffer(int x, int y, int width, int height) OVERRIDE;
43   virtual bool SetBackbufferAllocation(bool allocated) OVERRIDE;
44   virtual void SetFrontbufferAllocation(bool allocated) OVERRIDE;
45   virtual void* GetShareHandle() OVERRIDE;
46   virtual void* GetDisplay() OVERRIDE;
47   virtual void* GetConfig() OVERRIDE;
48
49  protected:
50   // ImageTransportSurface implementation.
51   virtual void OnBufferPresented(
52       const AcceleratedSurfaceMsg_BufferPresented_Params& params) OVERRIDE;
53   virtual void OnResizeViewACK() OVERRIDE;
54   virtual void OnResize(gfx::Size size, float scale_factor) OVERRIDE;
55   virtual void SetLatencyInfo(
56       const std::vector<ui::LatencyInfo>& latency_info) OVERRIDE;
57   virtual void WakeUpGpu() OVERRIDE;
58
59   // GpuCommandBufferStub::DestructionObserver implementation.
60   virtual void OnWillDestroyStub() OVERRIDE;
61
62  private:
63
64   gfx::Size backbuffer_size() const {
65     DCHECK(backbuffer_.get());
66     GLsizei width = 0;
67     GLsizei height = 0;
68     backbuffer_->texture()->GetLevelSize(GL_TEXTURE_2D, 0, &width, &height);
69     return gfx::Size(width, height);
70   }
71
72   virtual ~TextureImageTransportSurface();
73   void CreateBackTexture();
74   void AttachBackTextureToFBO();
75   void ReleaseBackTexture();
76   void ReleaseFrontTexture();
77   void BufferPresentedImpl(const gpu::Mailbox& mailbox_name);
78
79   // The framebuffer that represents this surface (service id). Allocated lazily
80   // in OnMakeCurrent.
81   uint32 fbo_id_;
82
83   // The current backbuffer.
84   scoped_refptr<gpu::gles2::TextureRef> backbuffer_;
85   scoped_refptr<gpu::gles2::TextureRef> frontbuffer_;
86
87   // The mailbox name for the current backbuffer texture. Needs to be unique per
88   // GL texture and is invalid while service_id is zero.
89   gpu::Mailbox back_mailbox_;
90   gpu::Mailbox front_mailbox_;
91
92   // The current size of the GLSurface. Used to disambiguate from the current
93   // texture size which might be outdated (since we use two buffers).
94   gfx::Size current_size_;
95   float scale_factor_;
96
97   // Whether or not the command buffer stub has been destroyed.
98   bool stub_destroyed_;
99
100   bool backbuffer_suggested_allocation_;
101   bool frontbuffer_suggested_allocation_;
102
103   scoped_ptr<ImageTransportHelper> helper_;
104   gfx::GLSurfaceHandle handle_;
105
106   // The offscreen surface used to make the context current. However note that
107   // the actual rendering is always redirected to an FBO.
108   scoped_refptr<gfx::GLSurface> surface_;
109
110   // Whether a SwapBuffers is pending.
111   bool is_swap_buffers_pending_;
112
113   // Whether we unscheduled command buffer because of pending SwapBuffers.
114   bool did_unschedule_;
115
116   // Holds a reference to the mailbox manager for cleanup.
117   scoped_refptr<gpu::gles2::MailboxManager> mailbox_manager_;
118
119   std::vector<ui::LatencyInfo> latency_info_;
120   DISALLOW_COPY_AND_ASSIGN(TextureImageTransportSurface);
121 };
122
123 }  // namespace content
124
125 #endif  // CONTENT_COMMON_GPU_TEXTURE_IMAGE_TRANSPORT_SURFACE_H_