gl_texture_surface.cpp
3.22 KB
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
// Taken from awesomium examples
#include "gl_texture_surface.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
GLRAMTextureSurface::GLRAMTextureSurface(int width, int height) : texture_id_(NULL),
buffer_(0), bpp_(4), rowspan_(0), width_(width), height_(height) {
rowspan_ = width_ * bpp_;
buffer_ = new unsigned char[rowspan_ * height_];
needs_update_ = false;
texture_id_ = web_make_texture(width_, height_);
}
GLRAMTextureSurface::~GLRAMTextureSurface() {
web_del_texture(texture_id_);
delete[] buffer_;
}
void* GLRAMTextureSurface::GetTexture() const {
const_cast<GLRAMTextureSurface*>(this)->UpdateTexture();
return texture_id_;
}
void GLRAMTextureSurface::Paint(unsigned char* src_buffer,
int src_row_span,
const Awesomium::Rect& src_rect,
const Awesomium::Rect& dest_rect) {
for (int row = 0; row < dest_rect.height; row++)
memcpy(buffer_ + (row + dest_rect.y) * rowspan_ + (dest_rect.x * 4),
src_buffer + (row + src_rect.y) * src_row_span + (src_rect.x * 4),
dest_rect.width * 4);
needs_update_ = true;
}
void GLRAMTextureSurface::Scroll(int dx,
int dy,
const Awesomium::Rect& clip_rect) {
if (abs(dx) >= clip_rect.width || abs(dy) >= clip_rect.height)
return;
if (dx < 0 && dy == 0) {
// Area shifted left by dx
unsigned char* tempBuffer = new unsigned char[(clip_rect.width + dx) * 4];
for (int i = 0; i < clip_rect.height; i++) {
memcpy(tempBuffer, buffer_ + (i + clip_rect.y) * rowspan_ +
(clip_rect.x - dx) * 4, (clip_rect.width + dx) * 4);
memcpy(buffer_ + (i + clip_rect.y) * rowspan_ + (clip_rect.x) * 4,
tempBuffer, (clip_rect.width + dx) * 4);
}
delete[] tempBuffer;
} else if (dx > 0 && dy == 0) {
// Area shifted right by dx
unsigned char* tempBuffer = new unsigned char[(clip_rect.width - dx) * 4];
for (int i = 0; i < clip_rect.height; i++) {
memcpy(tempBuffer, buffer_ + (i + clip_rect.y) * rowspan_ +
(clip_rect.x) * 4, (clip_rect.width - dx) * 4);
memcpy(buffer_ + (i + clip_rect.y) * rowspan_ + (clip_rect.x + dx) * 4,
tempBuffer, (clip_rect.width - dx) * 4);
}
delete[] tempBuffer;
} else if (dy < 0 && dx == 0) {
// Area shifted down by dy
for (int i = 0; i < clip_rect.height + dy ; i++)
memcpy(buffer_ + (i + clip_rect.y) * rowspan_ + (clip_rect.x * 4),
buffer_ + (i + clip_rect.y - dy) * rowspan_ + (clip_rect.x * 4),
clip_rect.width * 4);
} else if (dy > 0 && dx == 0) {
// Area shifted up by dy
for (int i = clip_rect.height - 1; i >= dy; i--)
memcpy(buffer_ + (i + clip_rect.y) * rowspan_ + (clip_rect.x * 4),
buffer_ + (i + clip_rect.y - dy) * rowspan_ + (clip_rect.x * 4),
clip_rect.width * 4);
}
needs_update_ = true;
}
void GLRAMTextureSurface::UpdateTexture() {
if (needs_update_) {
web_texture_update(texture_id_, width_, height_, buffer_);
needs_update_ = false;
}
}
GLTextureSurfaceFactory::GLTextureSurfaceFactory() {
}
GLTextureSurfaceFactory::~GLTextureSurfaceFactory() {
}
Awesomium::Surface* GLTextureSurfaceFactory::CreateSurface(Awesomium::WebView* view, int width, int height) {
return new GLRAMTextureSurface(width, height);
}
void GLTextureSurfaceFactory::DestroySurface(Awesomium::Surface* surface) {
delete static_cast<GLRAMTextureSurface*>(surface);
}