aboutsummaryrefslogtreecommitdiff
path: root/java/org/cef/browser/CefRenderer.java
blob: 2a2954658d71e94f473f8a3cfab17bac47ae3bd7 (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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
// Copyright (c) 2013 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.

package org.cef.browser;

import com.jogamp.opengl.GL2;

import java.awt.Rectangle;
import java.nio.ByteBuffer;
import java.nio.FloatBuffer;

class CefRenderer {
    private boolean transparent_;
    private GL2 initialized_context_ = null;
    private int[] texture_id_ = new int[1];
    private int view_width_ = 0;
    private int view_height_ = 0;
    private float spin_x_ = 0f;
    private float spin_y_ = 0f;
    private Rectangle popup_rect_ = new Rectangle(0, 0, 0, 0);
    private Rectangle original_popup_rect_ = new Rectangle(0, 0, 0, 0);
    private boolean use_draw_pixels_ = false;

    protected CefRenderer(boolean transparent) {
        transparent_ = transparent;
    }

    protected boolean isTransparent() {
        return transparent_;
    }

    protected int getTextureID() {
        return texture_id_[0];
    }

    @SuppressWarnings("static-access")
    protected void initialize(GL2 gl2) {
        if (initialized_context_ == gl2) return;

        initialized_context_ = gl2;

        if (!gl2.getContext().isHardwareRasterizer()) {
            // Workaround for Windows Remote Desktop which requires pot textures.
            System.out.println(
                    "opengl rendering may be slow as hardware rendering isn't available");
            use_draw_pixels_ = true;
            return;
        }

        gl2.glHint(gl2.GL_POLYGON_SMOOTH_HINT, gl2.GL_NICEST);

        gl2.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);

        // Necessary for non-power-of-2 textures to render correctly.
        gl2.glPixelStorei(gl2.GL_UNPACK_ALIGNMENT, 1);

        // Create the texture.
        gl2.glGenTextures(1, texture_id_, 0);
        assert (texture_id_[0] != 0);

        gl2.glBindTexture(gl2.GL_TEXTURE_2D, texture_id_[0]);
        gl2.glTexParameteri(gl2.GL_TEXTURE_2D, gl2.GL_TEXTURE_MIN_FILTER, gl2.GL_NEAREST);
        gl2.glTexParameteri(gl2.GL_TEXTURE_2D, gl2.GL_TEXTURE_MAG_FILTER, gl2.GL_NEAREST);
        gl2.glTexEnvf(gl2.GL_TEXTURE_ENV, gl2.GL_TEXTURE_ENV_MODE, gl2.GL_MODULATE);
    }

    protected void cleanup(GL2 gl2) {
        if (texture_id_[0] != 0) gl2.glDeleteTextures(1, texture_id_, 0);
        view_width_ = view_height_ = 0;
    }

    @SuppressWarnings("static-access")
    protected void render(GL2 gl2) {
        if (use_draw_pixels_ || view_width_ == 0 || view_height_ == 0) return;

        assert (initialized_context_ != null);

        final float[] vertex_data = {// tu,   tv,     x,     y,    z
                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};
        FloatBuffer vertices = FloatBuffer.wrap(vertex_data);

        gl2.glClear(gl2.GL_COLOR_BUFFER_BIT | gl2.GL_DEPTH_BUFFER_BIT);

        gl2.glMatrixMode(gl2.GL_MODELVIEW);
        gl2.glLoadIdentity();

        // Match GL units to screen coordinates.
        gl2.glViewport(0, 0, view_width_, view_height_);
        gl2.glMatrixMode(gl2.GL_PROJECTION);
        gl2.glLoadIdentity();

        // Draw the background gradient.
        gl2.glPushAttrib(gl2.GL_ALL_ATTRIB_BITS);
        gl2.glBegin(gl2.GL_QUADS);
        gl2.glColor4f(1.0f, 0.0f, 0.0f, 1.0f); // red
        gl2.glVertex2f(-1.0f, -1.0f);
        gl2.glVertex2f(1.0f, -1.0f);
        gl2.glColor4f(0.0f, 0.0f, 1.0f, 1.0f); // blue
        gl2.glVertex2f(1.0f, 1.0f);
        gl2.glVertex2f(-1.0f, 1.0f);
        gl2.glEnd();
        gl2.glPopAttrib();

        // Rotate the view based on the mouse spin.
        if (spin_x_ != 0) gl2.glRotatef(-spin_x_, 1.0f, 0.0f, 0.0f);
        if (spin_y_ != 0) gl2.glRotatef(-spin_y_, 0.0f, 1.0f, 0.0f);

        if (transparent_) {
            // Alpha blending style. Texture values have premultiplied alpha.
            gl2.glBlendFunc(gl2.GL_ONE, gl2.GL_ONE_MINUS_SRC_ALPHA);

            // Enable alpha blending.
            gl2.glEnable(gl2.GL_BLEND);
        }

        // Enable 2D textures.
        gl2.glEnable(gl2.GL_TEXTURE_2D);

        // Draw the facets with the texture.
        assert (texture_id_[0] != 0);
        gl2.glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
        gl2.glBindTexture(gl2.GL_TEXTURE_2D, texture_id_[0]);
        gl2.glInterleavedArrays(gl2.GL_T2F_V3F, 0, vertices);
        gl2.glDrawArrays(gl2.GL_QUADS, 0, 4);

        // Disable 2D textures.
        gl2.glDisable(gl2.GL_TEXTURE_2D);

        if (transparent_) {
            // Disable alpha blending.
            gl2.glDisable(gl2.GL_BLEND);
        }
    }

    protected void onPopupSize(Rectangle rect) {
        if (rect.width <= 0 || rect.height <= 0) return;
        original_popup_rect_ = rect;
        popup_rect_ = getPopupRectInWebView(original_popup_rect_);
    }

    protected Rectangle getPopupRect() {
        return (Rectangle) popup_rect_.clone();
    }

    protected Rectangle getPopupRectInWebView(Rectangle original_rect) {
        Rectangle 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;
    }

    protected void clearPopupRects() {
        popup_rect_.setBounds(0, 0, 0, 0);
        original_popup_rect_.setBounds(0, 0, 0, 0);
    }

    @SuppressWarnings("static-access")
    protected void onPaint(GL2 gl2, boolean popup, Rectangle[] dirtyRects, ByteBuffer buffer,
            int width, int height) {
        initialize(gl2);

        if (use_draw_pixels_) {
            gl2.glRasterPos2f(-1, 1);
            gl2.glPixelZoom(1, -1);
            gl2.glDrawPixels(width, height, GL2.GL_BGRA, GL2.GL_UNSIGNED_BYTE, buffer);
            return;
        }

        if (transparent_) {
            // Enable alpha blending.
            gl2.glEnable(gl2.GL_BLEND);
        }

        // Enable 2D textures.
        gl2.glEnable(gl2.GL_TEXTURE_2D);

        assert (texture_id_[0] != 0);
        gl2.glBindTexture(gl2.GL_TEXTURE_2D, texture_id_[0]);

        if (!popup) {
            int old_width = view_width_;
            int old_height = view_height_;

            view_width_ = width;
            view_height_ = height;

            gl2.glPixelStorei(gl2.GL_UNPACK_ROW_LENGTH, view_width_);

            if (old_width != view_width_ || old_height != view_height_) {
                // Update/resize the whole texture.
                gl2.glPixelStorei(gl2.GL_UNPACK_SKIP_PIXELS, 0);
                gl2.glPixelStorei(gl2.GL_UNPACK_SKIP_ROWS, 0);
                gl2.glTexImage2D(gl2.GL_TEXTURE_2D, 0, gl2.GL_RGBA, view_width_, view_height_, 0,
                        gl2.GL_BGRA, gl2.GL_UNSIGNED_INT_8_8_8_8_REV, buffer);
            } else {
                // Update just the dirty rectangles.
                for (int i = 0; i < dirtyRects.length; ++i) {
                    Rectangle rect = dirtyRects[i];
                    gl2.glPixelStorei(gl2.GL_UNPACK_SKIP_PIXELS, rect.x);
                    gl2.glPixelStorei(gl2.GL_UNPACK_SKIP_ROWS, rect.y);
                    gl2.glTexSubImage2D(gl2.GL_TEXTURE_2D, 0, rect.x, rect.y, rect.width,
                            rect.height, gl2.GL_BGRA, gl2.GL_UNSIGNED_INT_8_8_8_8_REV, buffer);
                }
            }
        } else if (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.
            gl2.glPixelStorei(gl2.GL_UNPACK_ROW_LENGTH, width);
            gl2.glPixelStorei(gl2.GL_UNPACK_SKIP_PIXELS, skip_pixels);
            gl2.glPixelStorei(gl2.GL_UNPACK_SKIP_ROWS, skip_rows);
            gl2.glTexSubImage2D(gl2.GL_TEXTURE_2D, 0, x, y, w, h, gl2.GL_BGRA,
                    gl2.GL_UNSIGNED_INT_8_8_8_8_REV, buffer);
        }

        // Disable 2D textures.
        gl2.glDisable(gl2.GL_TEXTURE_2D);

        if (transparent_) {
            // Disable alpha blending.
            gl2.glDisable(gl2.GL_BLEND);
        }
    }

    protected void setSpin(float spinX, float spinY) {
        spin_x_ = spinX;
        spin_y_ = spinY;
    }

    protected void incrementSpin(float spinDX, float spinDY) {
        spin_x_ -= spinDX;
        spin_y_ -= spinDY;
    }
}