| OLD | NEW |
| (Empty) |
| 1 // Copyright 2017 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 #include "remoting/client/ios/display/gl_demo_screen.h" | |
| 6 | |
| 7 #include "base/logging.h" | |
| 8 #include "remoting/client/display/canvas.h" | |
| 9 #include "remoting/client/display/gl_math.h" | |
| 10 | |
| 11 namespace remoting { | |
| 12 | |
| 13 namespace { | |
| 14 | |
| 15 const GLfloat square[] = {-1.0, -1.0, 1.0, -1.0, -1.0, 1.0, 1.0, 1.0}; | |
| 16 | |
| 17 const GLchar* fragmentShaderSource = | |
| 18 "precision mediump float;" | |
| 19 "void main() {" | |
| 20 " gl_FragColor = vec4(1.0, 1.0, 0.0, 1.0);" | |
| 21 "} "; | |
| 22 | |
| 23 const GLchar* vertexShaderSource = | |
| 24 "precision mediump float;" | |
| 25 "attribute vec4 a_position;" | |
| 26 "void main() {" | |
| 27 " gl_Position = a_position;" | |
| 28 "}"; | |
| 29 | |
| 30 const GLchar* a_position = "a_position"; | |
| 31 | |
| 32 } // namespace | |
| 33 | |
| 34 // This is a demo screen that can be added to the renderer to test the drawable | |
| 35 // integration. This will draw an expanding checkerboard pattern to the screen. | |
| 36 GlDemoScreen::GlDemoScreen() : weak_factory_(this) {} | |
| 37 | |
| 38 GlDemoScreen::~GlDemoScreen() {} | |
| 39 | |
| 40 void GlDemoScreen::SetCanvas(base::WeakPtr<Canvas> canvas) { | |
| 41 canvas_ = canvas; | |
| 42 | |
| 43 // Create and compile vertex shader. | |
| 44 GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER); | |
| 45 glShaderSource(vertexShader, 1, &vertexShaderSource, NULL); | |
| 46 glCompileShader(vertexShader); | |
| 47 | |
| 48 // Create and compile fragment shader. | |
| 49 GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER); | |
| 50 glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL); | |
| 51 glCompileShader(fragmentShader); | |
| 52 | |
| 53 // Create and link program. | |
| 54 program_ = glCreateProgram(); | |
| 55 glAttachShader(program_, vertexShader); | |
| 56 glAttachShader(program_, fragmentShader); | |
| 57 glLinkProgram(program_); | |
| 58 } | |
| 59 | |
| 60 int GlDemoScreen::GetZIndex() { | |
| 61 return Drawable::DESKTOP + 1; | |
| 62 } | |
| 63 | |
| 64 bool GlDemoScreen::Draw() { | |
| 65 if (!canvas_) { | |
| 66 return false; | |
| 67 } | |
| 68 | |
| 69 // TODO(nicholss): width and height should be dynamic based on the canvas. | |
| 70 int width = 640; | |
| 71 int height = 1024; | |
| 72 square_size_++; | |
| 73 if (square_size_ > 300) { | |
| 74 square_size_ = 1; | |
| 75 } | |
| 76 | |
| 77 // Set the viewport. | |
| 78 glViewport(0, 0, width, height); | |
| 79 | |
| 80 // Clear. | |
| 81 glClearColor(0, 1, 0, 1); | |
| 82 glClear(GL_COLOR_BUFFER_BIT); | |
| 83 | |
| 84 // Use program. | |
| 85 glUseProgram(program_); | |
| 86 | |
| 87 int skip = 0; | |
| 88 for (int i = 0; i < width; i += square_size_) { | |
| 89 if (skip == square_size_) { | |
| 90 skip = 0; | |
| 91 } else { | |
| 92 skip = square_size_; | |
| 93 } | |
| 94 for (int j = skip; j < height; j += square_size_ * 2) { | |
| 95 glViewport(i, j, square_size_, square_size_); | |
| 96 | |
| 97 // Send geometry to vertex shader. | |
| 98 GLuint aPosition = glGetAttribLocation(program_, a_position); | |
| 99 | |
| 100 glVertexAttribPointer(aPosition, 2, GL_FLOAT, GL_FALSE, 0, square); | |
| 101 glEnableVertexAttribArray(aPosition); | |
| 102 | |
| 103 // Draw. | |
| 104 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); | |
| 105 } | |
| 106 } | |
| 107 return false; | |
| 108 } | |
| 109 | |
| 110 base::WeakPtr<Drawable> GlDemoScreen::GetWeakPtr() { | |
| 111 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 112 return weak_factory_.GetWeakPtr(); | |
| 113 } | |
| 114 | |
| 115 } // namespace remoting | |
| OLD | NEW |