OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 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 "webkit/tools/pepper_test_plugin/pepper_3d_test.h" |
| 6 |
| 7 namespace { |
| 8 const int32 kCommandBufferSize = 1024 * 1024; |
| 9 } // namespace |
| 10 |
| 11 namespace NPAPIClient { |
| 12 |
| 13 Pepper3DTest::Pepper3DTest(NPP id, NPNetscapeFuncs *host_functions) |
| 14 : PluginTest(id, host_functions), |
| 15 pepper_extensions_(NULL), |
| 16 device_3d_(NULL), |
| 17 pgl_context_(PGL_NO_CONTEXT) { |
| 18 memset(&context_3d_, 0, sizeof(context_3d_)); |
| 19 |
| 20 esInitContext(&es_context_); |
| 21 memset(&es_data_, 0, sizeof(es_data_)); |
| 22 es_context_.userData = &es_data_; |
| 23 } |
| 24 |
| 25 Pepper3DTest::~Pepper3DTest() { |
| 26 } |
| 27 |
| 28 NPError Pepper3DTest::New(uint16 mode, int16 argc, const char* argn[], |
| 29 const char* argv[], NPSavedData* saved) { |
| 30 return PluginTest::New(mode, argc, argn, argv, saved); |
| 31 } |
| 32 |
| 33 NPError Pepper3DTest::Destroy() { |
| 34 DestroyContext(); |
| 35 pglTerminate(); |
| 36 return NPERR_NO_ERROR; |
| 37 } |
| 38 |
| 39 NPError Pepper3DTest::SetWindow(NPWindow* window) { |
| 40 // Create context if needed. |
| 41 CreateContext(); |
| 42 |
| 43 es_context_.width = window->width; |
| 44 es_context_.height = window->height; |
| 45 |
| 46 return NPERR_NO_ERROR; |
| 47 } |
| 48 |
| 49 void Pepper3DTest::RepaintCallback(NPP npp, NPDeviceContext3D* /* context */) { |
| 50 Pepper3DTest* plugin = static_cast<Pepper3DTest*>(npp->pdata); |
| 51 plugin->Paint(); |
| 52 } |
| 53 |
| 54 void Pepper3DTest::CreateContext() { |
| 55 if (pgl_context_ != PGL_NO_CONTEXT) |
| 56 return; |
| 57 |
| 58 HostFunctions()->getvalue(id(), NPNVPepperExtensions, &pepper_extensions_); |
| 59 if (pepper_extensions_ == NULL) { |
| 60 SetError("Could not acquire pepper extensions"); |
| 61 SignalTestCompleted(); |
| 62 return; |
| 63 } |
| 64 |
| 65 device_3d_ = pepper_extensions_->acquireDevice(id(), NPPepper3DDevice); |
| 66 if (device_3d_ == NULL) { |
| 67 SetError("Could not acquire 3D device"); |
| 68 SignalTestCompleted(); |
| 69 return; |
| 70 } |
| 71 |
| 72 // Initialize a 3D context. |
| 73 NPDeviceContext3DConfig config; |
| 74 config.commandBufferSize = kCommandBufferSize; |
| 75 if (device_3d_->initializeContext(id(), &config, &context_3d_) |
| 76 != NPERR_NO_ERROR) { |
| 77 SetError("Could not initialize 3D context"); |
| 78 SignalTestCompleted(); |
| 79 return; |
| 80 } |
| 81 context_3d_.repaintCallback = RepaintCallback; |
| 82 |
| 83 // Initialize PGL and create a PGL context. |
| 84 if (!pglInitialize()) { |
| 85 SetError("Could not initialize PGL"); |
| 86 SignalTestCompleted(); |
| 87 return; |
| 88 } |
| 89 pgl_context_ = pglCreateContext(id(), device_3d_, &context_3d_); |
| 90 if (pgl_context_ == PGL_NO_CONTEXT) { |
| 91 SetError("Could not initialize PGL context"); |
| 92 SignalTestCompleted(); |
| 93 return; |
| 94 } |
| 95 |
| 96 // Initialize OpenGL. |
| 97 MakeContextCurrent(); |
| 98 InitGL(); |
| 99 pglMakeCurrent(PGL_NO_CONTEXT); |
| 100 } |
| 101 |
| 102 void Pepper3DTest::DestroyContext() { |
| 103 if (pgl_context_ == PGL_NO_CONTEXT) |
| 104 return; |
| 105 |
| 106 MakeContextCurrent(); |
| 107 ReleaseGL(); |
| 108 if (!pglDestroyContext(pgl_context_)) { |
| 109 SetError("Could not destroy PGL context"); |
| 110 } |
| 111 pgl_context_ = PGL_NO_CONTEXT; |
| 112 |
| 113 if (device_3d_->destroyContext(id(), &context_3d_) != NPERR_NO_ERROR) { |
| 114 SetError("Could not destroy 3D context"); |
| 115 } |
| 116 } |
| 117 |
| 118 void Pepper3DTest::MakeContextCurrent() { |
| 119 DCHECK(pgl_context_ != PGL_NO_CONTEXT); |
| 120 |
| 121 if (!pglMakeCurrent(pgl_context_)) { |
| 122 SetError("Could not make PGL context current"); |
| 123 } |
| 124 } |
| 125 |
| 126 void Pepper3DTest::Paint() { |
| 127 MakeContextCurrent(); |
| 128 DrawGL(); |
| 129 TestGL(); |
| 130 SwapBuffers(); |
| 131 pglMakeCurrent(PGL_NO_CONTEXT); |
| 132 |
| 133 // Painting once is enough to check correctness. |
| 134 SignalTestCompleted(); |
| 135 } |
| 136 |
| 137 void Pepper3DTest::SwapBuffers() { |
| 138 if (!pglSwapBuffers()) { |
| 139 SetError("Could not swap buffers"); |
| 140 } |
| 141 } |
| 142 |
| 143 void Pepper3DTest::InitGL() { |
| 144 if (!stInit(&es_context_)) { |
| 145 SetError("Could not initialize OpenGL resources"); |
| 146 } |
| 147 } |
| 148 |
| 149 void Pepper3DTest::ReleaseGL() { |
| 150 stShutDown(&es_context_); |
| 151 } |
| 152 |
| 153 void Pepper3DTest::DrawGL() { |
| 154 stDraw(&es_context_); |
| 155 } |
| 156 |
| 157 void Pepper3DTest::TestGL() { |
| 158 // NW quadrant is red. |
| 159 GLint x = es_context_.width / 4; |
| 160 GLint y = (3 * es_context_.height) / 4; |
| 161 GLubyte red_color[3] = {255, 0, 0}; |
| 162 TestPixel(x, y, red_color); |
| 163 |
| 164 // NE quadrant is green. |
| 165 x = (3 * es_context_.width) / 4; |
| 166 y = (3 * es_context_.height) / 4; |
| 167 GLubyte green_color[3] = {0, 255, 0}; |
| 168 TestPixel(x, y, green_color); |
| 169 |
| 170 // SW quadrant is blue. |
| 171 x = es_context_.width / 4; |
| 172 y = es_context_.height / 4; |
| 173 GLubyte blue_color[3] = {0, 0, 255}; |
| 174 TestPixel(x, y, blue_color); |
| 175 |
| 176 // SE quadrant is yellow. |
| 177 x = (3 * es_context_.width) / 4; |
| 178 y = es_context_.height / 4; |
| 179 GLubyte yellow_color[3] = {255, 255, 0}; |
| 180 TestPixel(x, y, yellow_color); |
| 181 |
| 182 // Mid-point is black. |
| 183 x = es_context_.width / 2; |
| 184 y = es_context_.height / 2; |
| 185 GLubyte black_color[3] = {0, 0, 0}; |
| 186 TestPixel(x, y, black_color); |
| 187 } |
| 188 |
| 189 void Pepper3DTest::TestPixel(int x, int y, const GLubyte expected_color[3]) { |
| 190 GLubyte pixel_color[4]; |
| 191 glReadPixels(x, y, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, pixel_color); |
| 192 |
| 193 ExpectIntegerEqual(pixel_color[0], expected_color[0]); |
| 194 ExpectIntegerEqual(pixel_color[1], expected_color[1]); |
| 195 ExpectIntegerEqual(pixel_color[2], expected_color[2]); |
| 196 } |
| 197 |
| 198 } // namespace NPAPIClient |
OLD | NEW |