2014-07-01 00:30:29 +02:00
|
|
|
// Copyright (c) 2012 The Chromium Embedded Framework Authors. All rights
|
|
|
|
// reserved. Use of this source code is governed by a BSD-style license
|
|
|
|
// that can be found in the LICENSE file.
|
|
|
|
|
2015-01-31 01:55:56 +01:00
|
|
|
#include "cefclient/browser/osr_renderer.h"
|
2014-07-01 00:30:29 +02:00
|
|
|
|
|
|
|
#if defined(OS_WIN)
|
|
|
|
#include <gl/gl.h>
|
|
|
|
#elif defined(OS_MACOSX)
|
|
|
|
#include <OpenGL/gl.h>
|
|
|
|
#elif defined(OS_LINUX)
|
|
|
|
#include <GL/gl.h>
|
|
|
|
#else
|
|
|
|
#error Platform is not supported.
|
|
|
|
#endif
|
|
|
|
|
2016-09-01 13:24:30 +02:00
|
|
|
#include "include/base/cef_logging.h"
|
2014-07-11 22:10:05 +02:00
|
|
|
#include "include/wrapper/cef_helpers.h"
|
2014-07-01 00:30:29 +02:00
|
|
|
|
|
|
|
#ifndef GL_BGR
|
|
|
|
#define GL_BGR 0x80E0
|
|
|
|
#endif
|
|
|
|
#ifndef GL_BGRA
|
|
|
|
#define GL_BGRA 0x80E1
|
|
|
|
#endif
|
|
|
|
#ifndef GL_UNSIGNED_INT_8_8_8_8_REV
|
|
|
|
#define GL_UNSIGNED_INT_8_8_8_8_REV 0x8367
|
|
|
|
#endif
|
|
|
|
|
2014-12-02 12:23:57 +01:00
|
|
|
// DCHECK on gl errors.
|
2016-09-01 13:24:30 +02:00
|
|
|
#if DCHECK_IS_ON()
|
2014-12-02 12:23:57 +01:00
|
|
|
#define VERIFY_NO_ERROR { \
|
|
|
|
int _gl_error = glGetError(); \
|
|
|
|
DCHECK(_gl_error == GL_NO_ERROR) << \
|
|
|
|
"glGetError returned " << _gl_error; \
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
#define VERIFY_NO_ERROR
|
|
|
|
#endif
|
2014-07-01 00:30:29 +02:00
|
|
|
|
2015-01-23 18:37:23 +01:00
|
|
|
namespace client {
|
|
|
|
|
2015-04-09 16:59:34 +02:00
|
|
|
OsrRenderer::Settings::Settings()
|
|
|
|
: transparent(false),
|
|
|
|
show_update_rect(false),
|
|
|
|
background_color(CefColorSetARGB(255, 255, 255, 255)) {
|
|
|
|
}
|
|
|
|
|
|
|
|
OsrRenderer::OsrRenderer(const Settings& settings)
|
|
|
|
: settings_(settings),
|
2014-07-01 00:30:29 +02:00
|
|
|
initialized_(false),
|
|
|
|
texture_id_(0),
|
|
|
|
view_width_(0),
|
|
|
|
view_height_(0),
|
|
|
|
spin_x_(0),
|
|
|
|
spin_y_(0) {
|
|
|
|
}
|
|
|
|
|
2015-01-23 18:37:23 +01:00
|
|
|
OsrRenderer::~OsrRenderer() {
|
2014-07-01 00:30:29 +02:00
|
|
|
Cleanup();
|
|
|
|
}
|
|
|
|
|
2015-01-23 18:37:23 +01:00
|
|
|
void OsrRenderer::Initialize() {
|
2014-07-01 00:30:29 +02:00
|
|
|
if (initialized_)
|
|
|
|
return;
|
|
|
|
|
2014-12-02 12:23:57 +01:00
|
|
|
glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST); VERIFY_NO_ERROR;
|
2014-07-01 00:30:29 +02:00
|
|
|
|
2015-04-09 16:59:34 +02:00
|
|
|
glClearColor(float(CefColorGetR(settings_.background_color)) / 255.0f,
|
|
|
|
float(CefColorGetG(settings_.background_color)) / 255.0f,
|
|
|
|
float(CefColorGetB(settings_.background_color)) / 255.0f,
|
|
|
|
1.0f); VERIFY_NO_ERROR;
|
2014-07-01 00:30:29 +02:00
|
|
|
|
|
|
|
// Necessary for non-power-of-2 textures to render correctly.
|
2014-12-02 12:23:57 +01:00
|
|
|
glPixelStorei(GL_UNPACK_ALIGNMENT, 1); VERIFY_NO_ERROR;
|
2014-07-01 00:30:29 +02:00
|
|
|
|
|
|
|
// Create the texture.
|
2014-12-02 12:23:57 +01:00
|
|
|
glGenTextures(1, &texture_id_); VERIFY_NO_ERROR;
|
|
|
|
DCHECK_NE(texture_id_, 0U); VERIFY_NO_ERROR;
|
2014-07-01 00:30:29 +02:00
|
|
|
|
2014-12-02 12:23:57 +01:00
|
|
|
glBindTexture(GL_TEXTURE_2D, texture_id_); VERIFY_NO_ERROR;
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
|
|
|
|
GL_NEAREST); VERIFY_NO_ERROR;
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
|
|
|
|
GL_NEAREST); VERIFY_NO_ERROR;
|
|
|
|
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); VERIFY_NO_ERROR;
|
2014-07-01 00:30:29 +02:00
|
|
|
|
|
|
|
initialized_ = true;
|
|
|
|
}
|
|
|
|
|
2015-01-23 18:37:23 +01:00
|
|
|
void OsrRenderer::Cleanup() {
|
2014-07-01 00:30:29 +02:00
|
|
|
if (texture_id_ != 0)
|
|
|
|
glDeleteTextures(1, &texture_id_);
|
|
|
|
}
|
|
|
|
|
2015-01-23 18:37:23 +01:00
|
|
|
void OsrRenderer::Render() {
|
2014-07-01 00:30:29 +02:00
|
|
|
if (view_width_ == 0 || view_height_ == 0)
|
|
|
|
return;
|
|
|
|
|
2014-07-11 22:10:05 +02:00
|
|
|
DCHECK(initialized_);
|
2014-07-01 00:30:29 +02:00
|
|
|
|
|
|
|
struct {
|
|
|
|
float tu, tv;
|
|
|
|
float x, y, z;
|
|
|
|
} static vertices[] = {
|
|
|
|
{0.0f, 1.0f, -1.0f, -1.0f, 0.0f},
|
|
|
|
{1.0f, 1.0f, 1.0f, -1.0f, 0.0f},
|
|
|
|
{1.0f, 0.0f, 1.0f, 1.0f, 0.0f},
|
|
|
|
{0.0f, 0.0f, -1.0f, 1.0f, 0.0f}
|
|
|
|
};
|
|
|
|
|
2014-12-02 12:23:57 +01:00
|
|
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); VERIFY_NO_ERROR;
|
2014-07-01 00:30:29 +02:00
|
|
|
|
2014-12-02 12:23:57 +01:00
|
|
|
glMatrixMode(GL_MODELVIEW); VERIFY_NO_ERROR;
|
|
|
|
glLoadIdentity(); VERIFY_NO_ERROR;
|
2014-07-01 00:30:29 +02:00
|
|
|
|
|
|
|
// Match GL units to screen coordinates.
|
2014-12-02 12:23:57 +01:00
|
|
|
glViewport(0, 0, view_width_, view_height_); VERIFY_NO_ERROR;
|
|
|
|
glMatrixMode(GL_PROJECTION); VERIFY_NO_ERROR;
|
|
|
|
glLoadIdentity(); VERIFY_NO_ERROR;
|
2014-07-01 00:30:29 +02:00
|
|
|
|
|
|
|
// Draw the background gradient.
|
2014-12-02 12:23:57 +01:00
|
|
|
glPushAttrib(GL_ALL_ATTRIB_BITS); VERIFY_NO_ERROR;
|
|
|
|
// Don't check for errors until glEnd().
|
2014-07-01 00:30:29 +02:00
|
|
|
glBegin(GL_QUADS);
|
|
|
|
glColor4f(1.0, 0.0, 0.0, 1.0); // red
|
|
|
|
glVertex2f(-1.0, -1.0);
|
|
|
|
glVertex2f(1.0, -1.0);
|
|
|
|
glColor4f(0.0, 0.0, 1.0, 1.0); // blue
|
|
|
|
glVertex2f(1.0, 1.0);
|
|
|
|
glVertex2f(-1.0, 1.0);
|
2014-12-02 12:23:57 +01:00
|
|
|
glEnd(); VERIFY_NO_ERROR;
|
|
|
|
glPopAttrib(); VERIFY_NO_ERROR;
|
2014-07-01 00:30:29 +02:00
|
|
|
|
|
|
|
// Rotate the view based on the mouse spin.
|
2014-12-02 12:23:57 +01:00
|
|
|
if (spin_x_ != 0) {
|
|
|
|
glRotatef(-spin_x_, 1.0f, 0.0f, 0.0f); VERIFY_NO_ERROR;
|
|
|
|
}
|
|
|
|
if (spin_y_ != 0) {
|
|
|
|
glRotatef(-spin_y_, 0.0f, 1.0f, 0.0f); VERIFY_NO_ERROR;
|
|
|
|
}
|
2014-07-01 00:30:29 +02:00
|
|
|
|
2015-04-09 16:59:34 +02:00
|
|
|
if (settings_.transparent) {
|
2014-07-01 00:30:29 +02:00
|
|
|
// Alpha blending style. Texture values have premultiplied alpha.
|
2014-12-02 12:23:57 +01:00
|
|
|
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA); VERIFY_NO_ERROR;
|
2014-07-01 00:30:29 +02:00
|
|
|
|
|
|
|
// Enable alpha blending.
|
2014-12-02 12:23:57 +01:00
|
|
|
glEnable(GL_BLEND); VERIFY_NO_ERROR;
|
2014-07-01 00:30:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Enable 2D textures.
|
2014-12-02 12:23:57 +01:00
|
|
|
glEnable(GL_TEXTURE_2D); VERIFY_NO_ERROR;
|
2014-07-01 00:30:29 +02:00
|
|
|
|
|
|
|
// Draw the facets with the texture.
|
2014-12-02 12:23:57 +01:00
|
|
|
DCHECK_NE(texture_id_, 0U); VERIFY_NO_ERROR;
|
|
|
|
glBindTexture(GL_TEXTURE_2D, texture_id_); VERIFY_NO_ERROR;
|
|
|
|
glInterleavedArrays(GL_T2F_V3F, 0, vertices); VERIFY_NO_ERROR;
|
|
|
|
glDrawArrays(GL_QUADS, 0, 4); VERIFY_NO_ERROR;
|
2014-07-01 00:30:29 +02:00
|
|
|
|
|
|
|
// Disable 2D textures.
|
2014-12-02 12:23:57 +01:00
|
|
|
glDisable(GL_TEXTURE_2D); VERIFY_NO_ERROR;
|
2014-07-01 00:30:29 +02:00
|
|
|
|
2015-04-09 16:59:34 +02:00
|
|
|
if (settings_.transparent) {
|
2014-07-01 00:30:29 +02:00
|
|
|
// Disable alpha blending.
|
2014-12-02 12:23:57 +01:00
|
|
|
glDisable(GL_BLEND); VERIFY_NO_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Draw a rectangle around the update region.
|
2015-04-09 16:59:34 +02:00
|
|
|
if (settings_.show_update_rect && !update_rect_.IsEmpty()) {
|
2014-12-02 12:23:57 +01:00
|
|
|
int left = update_rect_.x;
|
|
|
|
int right = update_rect_.x + update_rect_.width;
|
|
|
|
int top = update_rect_.y;
|
|
|
|
int bottom = update_rect_.y + update_rect_.height;
|
|
|
|
|
|
|
|
#if defined(OS_LINUX)
|
|
|
|
// Shrink the box so that top & right sides are drawn.
|
|
|
|
top += 1;
|
|
|
|
right -= 1;
|
|
|
|
#else
|
|
|
|
// Shrink the box so that left & bottom sides are drawn.
|
|
|
|
left += 1;
|
|
|
|
bottom -= 1;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
glPushAttrib(GL_ALL_ATTRIB_BITS); VERIFY_NO_ERROR
|
|
|
|
glMatrixMode(GL_PROJECTION); VERIFY_NO_ERROR;
|
|
|
|
glPushMatrix(); VERIFY_NO_ERROR;
|
|
|
|
glLoadIdentity(); VERIFY_NO_ERROR;
|
|
|
|
glOrtho(0, view_width_, view_height_, 0, 0, 1); VERIFY_NO_ERROR;
|
|
|
|
|
|
|
|
glLineWidth(1); VERIFY_NO_ERROR;
|
|
|
|
glColor3f(1.0f, 0.0f, 0.0f); VERIFY_NO_ERROR;
|
|
|
|
// Don't check for errors until glEnd().
|
|
|
|
glBegin(GL_LINE_STRIP);
|
|
|
|
glVertex2i(left, top);
|
|
|
|
glVertex2i(right, top);
|
|
|
|
glVertex2i(right, bottom);
|
|
|
|
glVertex2i(left, bottom);
|
|
|
|
glVertex2i(left, top);
|
|
|
|
glEnd(); VERIFY_NO_ERROR;
|
|
|
|
|
|
|
|
glPopMatrix(); VERIFY_NO_ERROR;
|
|
|
|
glPopAttrib(); VERIFY_NO_ERROR;
|
2014-07-01 00:30:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-23 18:37:23 +01:00
|
|
|
void OsrRenderer::OnPopupShow(CefRefPtr<CefBrowser> browser,
|
|
|
|
bool show) {
|
2014-07-01 00:30:29 +02:00
|
|
|
if (!show) {
|
|
|
|
// Clear the popup rectangle.
|
|
|
|
ClearPopupRects();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-23 18:37:23 +01:00
|
|
|
void OsrRenderer::OnPopupSize(CefRefPtr<CefBrowser> browser,
|
|
|
|
const CefRect& rect) {
|
2014-07-01 00:30:29 +02:00
|
|
|
if (rect.width <= 0 || rect.height <= 0)
|
|
|
|
return;
|
|
|
|
original_popup_rect_ = rect;
|
|
|
|
popup_rect_ = GetPopupRectInWebView(original_popup_rect_);
|
|
|
|
}
|
|
|
|
|
2015-01-23 18:37:23 +01:00
|
|
|
CefRect OsrRenderer::GetPopupRectInWebView(const CefRect& original_rect) {
|
2014-07-01 00:30:29 +02:00
|
|
|
CefRect rc(original_rect);
|
|
|
|
// if x or y are negative, move them to 0.
|
|
|
|
if (rc.x < 0)
|
|
|
|
rc.x = 0;
|
|
|
|
if (rc.y < 0)
|
|
|
|
rc.y = 0;
|
|
|
|
// if popup goes outside the view, try to reposition origin
|
|
|
|
if (rc.x + rc.width > view_width_)
|
|
|
|
rc.x = view_width_ - rc.width;
|
|
|
|
if (rc.y + rc.height > view_height_)
|
|
|
|
rc.y = view_height_ - rc.height;
|
|
|
|
// if x or y became negative, move them to 0 again.
|
|
|
|
if (rc.x < 0)
|
|
|
|
rc.x = 0;
|
|
|
|
if (rc.y < 0)
|
|
|
|
rc.y = 0;
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2015-01-23 18:37:23 +01:00
|
|
|
void OsrRenderer::ClearPopupRects() {
|
2014-07-01 00:30:29 +02:00
|
|
|
popup_rect_.Set(0, 0, 0, 0);
|
|
|
|
original_popup_rect_.Set(0, 0, 0, 0);
|
|
|
|
}
|
|
|
|
|
2015-01-23 18:37:23 +01:00
|
|
|
void OsrRenderer::OnPaint(CefRefPtr<CefBrowser> browser,
|
|
|
|
CefRenderHandler::PaintElementType type,
|
|
|
|
const CefRenderHandler::RectList& dirtyRects,
|
|
|
|
const void* buffer, int width, int height) {
|
2014-07-01 00:30:29 +02:00
|
|
|
if (!initialized_)
|
|
|
|
Initialize();
|
|
|
|
|
2015-04-09 16:59:34 +02:00
|
|
|
if (settings_.transparent) {
|
2014-07-01 00:30:29 +02:00
|
|
|
// Enable alpha blending.
|
2014-12-02 12:23:57 +01:00
|
|
|
glEnable(GL_BLEND); VERIFY_NO_ERROR;
|
2014-07-01 00:30:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Enable 2D textures.
|
2014-12-02 12:23:57 +01:00
|
|
|
glEnable(GL_TEXTURE_2D); VERIFY_NO_ERROR;
|
2014-07-01 00:30:29 +02:00
|
|
|
|
2014-07-11 22:10:05 +02:00
|
|
|
DCHECK_NE(texture_id_, 0U);
|
2014-12-02 12:23:57 +01:00
|
|
|
glBindTexture(GL_TEXTURE_2D, texture_id_); VERIFY_NO_ERROR;
|
2014-07-01 00:30:29 +02:00
|
|
|
|
|
|
|
if (type == PET_VIEW) {
|
|
|
|
int old_width = view_width_;
|
|
|
|
int old_height = view_height_;
|
|
|
|
|
|
|
|
view_width_ = width;
|
|
|
|
view_height_ = height;
|
|
|
|
|
2015-04-09 16:59:34 +02:00
|
|
|
if (settings_.show_update_rect)
|
2014-12-02 12:23:57 +01:00
|
|
|
update_rect_ = dirtyRects[0];
|
|
|
|
|
|
|
|
glPixelStorei(GL_UNPACK_ROW_LENGTH, view_width_); VERIFY_NO_ERROR;
|
2014-07-01 00:30:29 +02:00
|
|
|
|
2014-12-02 12:23:57 +01:00
|
|
|
if (old_width != view_width_ || old_height != view_height_ ||
|
|
|
|
(dirtyRects.size() == 1 &&
|
|
|
|
dirtyRects[0] == CefRect(0, 0, view_width_, view_height_))) {
|
2014-07-01 00:30:29 +02:00
|
|
|
// Update/resize the whole texture.
|
2014-12-02 12:23:57 +01:00
|
|
|
glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0); VERIFY_NO_ERROR;
|
|
|
|
glPixelStorei(GL_UNPACK_SKIP_ROWS, 0); VERIFY_NO_ERROR;
|
|
|
|
glTexImage2D(
|
|
|
|
GL_TEXTURE_2D, 0, GL_RGBA, view_width_, view_height_, 0,
|
|
|
|
GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, buffer); VERIFY_NO_ERROR;
|
2014-07-01 00:30:29 +02:00
|
|
|
} else {
|
|
|
|
// Update just the dirty rectangles.
|
|
|
|
CefRenderHandler::RectList::const_iterator i = dirtyRects.begin();
|
|
|
|
for (; i != dirtyRects.end(); ++i) {
|
|
|
|
const CefRect& rect = *i;
|
2015-01-30 19:07:13 +01:00
|
|
|
DCHECK(rect.x + rect.width <= view_width_);
|
|
|
|
DCHECK(rect.y + rect.height <= view_height_);
|
2014-12-02 12:23:57 +01:00
|
|
|
glPixelStorei(GL_UNPACK_SKIP_PIXELS, rect.x); VERIFY_NO_ERROR;
|
|
|
|
glPixelStorei(GL_UNPACK_SKIP_ROWS, rect.y); VERIFY_NO_ERROR;
|
2014-07-01 00:30:29 +02:00
|
|
|
glTexSubImage2D(GL_TEXTURE_2D, 0, rect.x, rect.y, rect.width,
|
|
|
|
rect.height, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV,
|
2014-12-02 12:23:57 +01:00
|
|
|
buffer); VERIFY_NO_ERROR;
|
2014-07-01 00:30:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (type == PET_POPUP && popup_rect_.width > 0 &&
|
|
|
|
popup_rect_.height > 0) {
|
|
|
|
int skip_pixels = 0, x = popup_rect_.x;
|
|
|
|
int skip_rows = 0, y = popup_rect_.y;
|
|
|
|
int w = width;
|
|
|
|
int h = height;
|
|
|
|
|
|
|
|
// Adjust the popup to fit inside the view.
|
|
|
|
if (x < 0) {
|
|
|
|
skip_pixels = -x;
|
|
|
|
x = 0;
|
|
|
|
}
|
|
|
|
if (y < 0) {
|
|
|
|
skip_rows = -y;
|
|
|
|
y = 0;
|
|
|
|
}
|
|
|
|
if (x + w > view_width_)
|
|
|
|
w -= x + w - view_width_;
|
|
|
|
if (y + h > view_height_)
|
|
|
|
h -= y + h - view_height_;
|
|
|
|
|
|
|
|
// Update the popup rectangle.
|
2014-12-02 12:23:57 +01:00
|
|
|
glPixelStorei(GL_UNPACK_ROW_LENGTH, width); VERIFY_NO_ERROR;
|
|
|
|
glPixelStorei(GL_UNPACK_SKIP_PIXELS, skip_pixels); VERIFY_NO_ERROR;
|
|
|
|
glPixelStorei(GL_UNPACK_SKIP_ROWS, skip_rows); VERIFY_NO_ERROR;
|
2014-07-01 00:30:29 +02:00
|
|
|
glTexSubImage2D(GL_TEXTURE_2D, 0, x, y, w, h, GL_BGRA,
|
2014-12-02 12:23:57 +01:00
|
|
|
GL_UNSIGNED_INT_8_8_8_8_REV, buffer); VERIFY_NO_ERROR;
|
2014-07-01 00:30:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Disable 2D textures.
|
2014-12-02 12:23:57 +01:00
|
|
|
glDisable(GL_TEXTURE_2D); VERIFY_NO_ERROR;
|
2014-07-01 00:30:29 +02:00
|
|
|
|
2015-04-09 16:59:34 +02:00
|
|
|
if (settings_.transparent) {
|
2014-07-01 00:30:29 +02:00
|
|
|
// Disable alpha blending.
|
2014-12-02 12:23:57 +01:00
|
|
|
glDisable(GL_BLEND); VERIFY_NO_ERROR;
|
2014-07-01 00:30:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-23 18:37:23 +01:00
|
|
|
void OsrRenderer::SetSpin(float spinX, float spinY) {
|
2014-07-01 00:30:29 +02:00
|
|
|
spin_x_ = spinX;
|
|
|
|
spin_y_ = spinY;
|
|
|
|
}
|
|
|
|
|
2015-01-23 18:37:23 +01:00
|
|
|
void OsrRenderer::IncrementSpin(float spinDX, float spinDY) {
|
2014-07-01 00:30:29 +02:00
|
|
|
spin_x_ -= spinDX;
|
|
|
|
spin_y_ -= spinDY;
|
|
|
|
}
|
2015-01-23 18:37:23 +01:00
|
|
|
|
|
|
|
} // namespace client
|