summaryrefslogtreecommitdiffstats
path: root/chrome/gpu/gpu_view_x.cc
blob: d9fd9d7a224021f6c6c1f7a7fb61598a3297af3b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/gpu/gpu_view_x.h"

#include "app/gfx/gl/gl_bindings.h"
#include "base/scoped_ptr.h"
#include "chrome/common/gpu_messages.h"
#include "chrome/gpu/gpu_backing_store_glx.h"
#include "chrome/gpu/gpu_backing_store_glx_context.h"
#include "chrome/gpu/gpu_thread.h"
#include "chrome/gpu/gpu_video_layer_glx.h"

// X stuff must be last since it does "#define Status int" which messes up some
// of the header files we indirectly pull in.
#include <X11/Xutil.h>

GpuViewX::GpuViewX(GpuThread* gpu_thread,
                   XID parent,
                   int32 routing_id)
    : gpu_thread_(gpu_thread),
      routing_id_(routing_id),
      window_(parent) {
  gpu_thread_->AddRoute(routing_id_, this);
}

GpuViewX::~GpuViewX() {
  gpu_thread_->RemoveRoute(routing_id_);
  // TODO(brettw) may want to delete any dangling backing stores, or perhaps
  // assert if one still exists.
}

GLXContext GpuViewX::BindContext() {
  GLXContext ctx = gpu_thread_->GetGLXContext()->BindContext(window_);
  CHECK(ctx);
  return ctx;
}

void GpuViewX::OnMessageReceived(const IPC::Message& msg) {
  IPC_BEGIN_MESSAGE_MAP(GpuViewX, msg)
    IPC_MESSAGE_HANDLER(GpuMsg_NewBackingStore, OnNewBackingStore)
    IPC_MESSAGE_HANDLER(GpuMsg_NewVideoLayer, OnNewVideoLayer)
    IPC_MESSAGE_HANDLER(GpuMsg_WindowPainted, OnWindowPainted)
  IPC_END_MESSAGE_MAP_EX()
}

void GpuViewX::OnChannelConnected(int32 peer_pid) {
}

void GpuViewX::OnChannelError() {
  // TODO(brettw) do we need to delete ourselves now?
}

void GpuViewX::DidScrollBackingStoreRect(int dx, int dy,
                                         const gfx::Rect& rect) {
}

void GpuViewX::Repaint() {
  BindContext();

  const gfx::Size& size = backing_store_->size();

  glViewport(0, 0, size.width(), size.height());

  // TODO(apatrick): These functions are not available in GLES2.
  // glMatrixMode(GL_MODELVIEW);
  // glLoadIdentity();

  glEnable(GL_TEXTURE_2D);
  glBindTexture(GL_TEXTURE_2D, backing_store_->texture_id());

  // TODO(brettw) use vertex buffers.
  // TODO(brettw) make this so we use the texture size rather than the whole
  // area size so we don't stretch bitmaps.
  // TODO(apatrick): These functions are not available in GLES2.
  // glBegin(GL_QUADS);
  //  glTexCoord2f(0.0f, 0.0f);
  //  glVertex2f(-1.0, 1.0);

  //  glTexCoord2f(0.0f, 1.0f);
  //  glVertex2f(-1.0, -1.0);

  //  glTexCoord2f(1.0f, 1.0f);
  //  glVertex2f(1.0, -1.0);

  //  glTexCoord2f(1.0f, 0.0f);
  //  glVertex2f(1.0, 1.0);
  // glEnd();
  DCHECK(glGetError() == GL_NO_ERROR);

  if (video_layer_.get()) {
    video_layer_->Render(backing_store_->size());
    DCHECK(glGetError() == GL_NO_ERROR);
  }

  // TODO(brettw) when we no longer stretch non-fitting bitmaps, we should
  // paint white over any unpainted area here.

  glXSwapBuffers(gpu_thread_->display(), window_);
}

void GpuViewX::OnNewBackingStore(int32 routing_id, const gfx::Size& size) {
  backing_store_.reset(
      new GpuBackingStoreGLX(this, gpu_thread_, routing_id, size));
}

void GpuViewX::OnNewVideoLayer(int32 routing_id, const gfx::Size& size) {
  video_layer_.reset(
      new GpuVideoLayerGLX(this, gpu_thread_, routing_id, size));
}

void GpuViewX::OnWindowPainted() {
  Repaint();
}