| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * Copyright (C) 2009 Google Inc. All rights reserved. | |
| 3 * | |
| 4 * Redistribution and use in source and binary forms, with or without | |
| 5 * modification, are permitted provided that the following conditions are | |
| 6 * met: | |
| 7 * | |
| 8 * * Redistributions of source code must retain the above copyright | |
| 9 * notice, this list of conditions and the following disclaimer. | |
| 10 * * Redistributions in binary form must reproduce the above | |
| 11 * copyright notice, this list of conditions and the following disclaimer | |
| 12 * in the documentation and/or other materials provided with the | |
| 13 * distribution. | |
| 14 * * Neither the name of Google Inc. nor the names of its | |
| 15 * contributors may be used to endorse or promote products derived from | |
| 16 * this software without specific prior written permission. | |
| 17 * | |
| 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
| 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
| 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
| 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
| 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
| 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
| 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
| 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
| 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
| 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
| 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 29 */ | |
| 30 | |
| 31 #include "config.h" | |
| 32 | |
| 33 #include "core/platform/chromium/support/GraphicsContext3DPrivate.h" | |
| 34 | |
| 35 #include "GrContext.h" | |
| 36 #include "GrGLInterface.h" | |
| 37 #include "core/platform/graphics/Extensions3D.h" | |
| 38 #include "core/platform/graphics/GraphicsContext.h" | |
| 39 #include "core/platform/graphics/ImageBuffer.h" | |
| 40 #include "core/platform/graphics/gpu/DrawingBuffer.h" | |
| 41 #include <public/WebGraphicsContext3D.h> | |
| 42 #include <public/WebGraphicsContext3DProvider.h> | |
| 43 #include <public/WebGraphicsMemoryAllocation.h> | |
| 44 #include <wtf/text/CString.h> | |
| 45 #include <wtf/text/StringHash.h> | |
| 46 | |
| 47 namespace { | |
| 48 | |
| 49 // The limit of the number of textures we hold in the GrContext's bitmap->textur
e cache. | |
| 50 const int maxGaneshTextureCacheCount = 2048; | |
| 51 // The limit of the bytes allocated toward textures in the GrContext's bitmap->t
exture cache. | |
| 52 const size_t maxGaneshTextureCacheBytes = 96 * 1024 * 1024; | |
| 53 | |
| 54 } | |
| 55 | |
| 56 namespace WebCore { | |
| 57 | |
| 58 //---------------------------------------------------------------------- | |
| 59 // GraphicsContext3DPrivate | |
| 60 | |
| 61 GraphicsContext3DPrivate::GraphicsContext3DPrivate(PassOwnPtr<WebKit::WebGraphic
sContext3D> webContext, bool preserveDrawingBuffer) | |
| 62 : m_impl(webContext.get()) | |
| 63 , m_ownedWebContext(webContext) | |
| 64 , m_initializedAvailableExtensions(false) | |
| 65 , m_layerComposited(false) | |
| 66 , m_preserveDrawingBuffer(preserveDrawingBuffer) | |
| 67 , m_resourceSafety(ResourceSafetyUnknown) | |
| 68 , m_grContext(0) | |
| 69 { | |
| 70 } | |
| 71 | |
| 72 GraphicsContext3DPrivate::GraphicsContext3DPrivate(PassOwnPtr<WebKit::WebGraphic
sContext3DProvider> provider, bool preserveDrawingBuffer) | |
| 73 : m_provider(provider) | |
| 74 , m_impl(m_provider->context3d()) | |
| 75 , m_initializedAvailableExtensions(false) | |
| 76 , m_layerComposited(false) | |
| 77 , m_preserveDrawingBuffer(preserveDrawingBuffer) | |
| 78 , m_resourceSafety(ResourceSafetyUnknown) | |
| 79 , m_grContext(m_provider->grContext()) | |
| 80 { | |
| 81 } | |
| 82 | |
| 83 | |
| 84 GraphicsContext3DPrivate::~GraphicsContext3DPrivate() | |
| 85 { | |
| 86 if (m_ownedGrContext) { | |
| 87 m_ownedWebContext->setMemoryAllocationChangedCallbackCHROMIUM(0); | |
| 88 m_ownedGrContext->contextDestroyed(); | |
| 89 } | |
| 90 } | |
| 91 | |
| 92 PassRefPtr<GraphicsContext3D> GraphicsContext3DPrivate::createGraphicsContextFro
mWebContext(PassOwnPtr<WebKit::WebGraphicsContext3D> webContext, bool preserveDr
awingBuffer) | |
| 93 { | |
| 94 RefPtr<GraphicsContext3D> context = adoptRef(new GraphicsContext3D()); | |
| 95 | |
| 96 OwnPtr<GraphicsContext3DPrivate> priv = adoptPtr(new GraphicsContext3DPrivat
e(webContext, preserveDrawingBuffer)); | |
| 97 context->m_private = priv.release(); | |
| 98 return context.release(); | |
| 99 } | |
| 100 | |
| 101 PassRefPtr<GraphicsContext3D> GraphicsContext3DPrivate::createGraphicsContextFro
mProvider(PassOwnPtr<WebKit::WebGraphicsContext3DProvider> provider, bool preser
veDrawingBuffer) | |
| 102 { | |
| 103 RefPtr<GraphicsContext3D> context = adoptRef(new GraphicsContext3D()); | |
| 104 | |
| 105 OwnPtr<GraphicsContext3DPrivate> priv = adoptPtr(new GraphicsContext3DPrivat
e(provider, preserveDrawingBuffer)); | |
| 106 context->m_private = priv.release(); | |
| 107 return context.release(); | |
| 108 } | |
| 109 | |
| 110 WebKit::WebGraphicsContext3D* GraphicsContext3DPrivate::extractWebGraphicsContex
t3D(GraphicsContext3D* context) | |
| 111 { | |
| 112 if (!context) | |
| 113 return 0; | |
| 114 return context->m_private->webContext(); | |
| 115 } | |
| 116 | |
| 117 class GrMemoryAllocationChangedCallbackAdapter : public WebKit::WebGraphicsConte
xt3D::WebGraphicsMemoryAllocationChangedCallbackCHROMIUM { | |
| 118 public: | |
| 119 GrMemoryAllocationChangedCallbackAdapter(GrContext* context) | |
| 120 : m_context(context) | |
| 121 { | |
| 122 } | |
| 123 | |
| 124 virtual void onMemoryAllocationChanged(WebKit::WebGraphicsMemoryAllocation a
llocation) OVERRIDE | |
| 125 { | |
| 126 if (!m_context) | |
| 127 return; | |
| 128 | |
| 129 if (!allocation.gpuResourceSizeInBytes) { | |
| 130 m_context->freeGpuResources(); | |
| 131 m_context->setTextureCacheLimits(0, 0); | |
| 132 } else | |
| 133 m_context->setTextureCacheLimits(maxGaneshTextureCacheCount, maxGane
shTextureCacheBytes); | |
| 134 } | |
| 135 | |
| 136 private: | |
| 137 GrContext* m_context; | |
| 138 }; | |
| 139 | |
| 140 namespace { | |
| 141 void bindWebGraphicsContext3DGLContextCallback(const GrGLInterface* interface) | |
| 142 { | |
| 143 reinterpret_cast<WebKit::WebGraphicsContext3D*>(interface->fCallbackData)->m
akeContextCurrent(); | |
| 144 } | |
| 145 } | |
| 146 | |
| 147 GrContext* GraphicsContext3DPrivate::grContext() | |
| 148 { | |
| 149 if (m_grContext) | |
| 150 return m_grContext; | |
| 151 if (!m_ownedWebContext) | |
| 152 return 0; | |
| 153 | |
| 154 SkAutoTUnref<GrGLInterface> interface(m_ownedWebContext->createGrGLInterface
()); | |
| 155 if (!interface) | |
| 156 return 0; | |
| 157 | |
| 158 interface->fCallback = bindWebGraphicsContext3DGLContextCallback; | |
| 159 interface->fCallbackData = reinterpret_cast<GrGLInterfaceCallbackData>(m_own
edWebContext.get()); | |
| 160 | |
| 161 m_ownedGrContext.reset(GrContext::Create(kOpenGL_GrBackend, reinterpret_cast
<GrBackendContext>(interface.get()))); | |
| 162 m_grContext = m_ownedGrContext; | |
| 163 if (!m_grContext) | |
| 164 return 0; | |
| 165 | |
| 166 m_grContext->setTextureCacheLimits(maxGaneshTextureCacheCount, maxGaneshText
ureCacheBytes); | |
| 167 m_grContextMemoryAllocationCallbackAdapter = adoptPtr(new GrMemoryAllocation
ChangedCallbackAdapter(m_grContext)); | |
| 168 m_ownedWebContext->setMemoryAllocationChangedCallbackCHROMIUM(m_grContextMem
oryAllocationCallbackAdapter.get()); | |
| 169 | |
| 170 return m_grContext; | |
| 171 } | |
| 172 | |
| 173 void GraphicsContext3DPrivate::markContextChanged() | |
| 174 { | |
| 175 m_layerComposited = false; | |
| 176 } | |
| 177 | |
| 178 bool GraphicsContext3DPrivate::layerComposited() const | |
| 179 { | |
| 180 return m_layerComposited; | |
| 181 } | |
| 182 | |
| 183 void GraphicsContext3DPrivate::markLayerComposited() | |
| 184 { | |
| 185 m_layerComposited = true; | |
| 186 } | |
| 187 | |
| 188 void GraphicsContext3DPrivate::paintFramebufferToCanvas(int framebuffer, int wid
th, int height, bool premultiplyAlpha, ImageBuffer* imageBuffer) | |
| 189 { | |
| 190 unsigned char* pixels = 0; | |
| 191 size_t bufferSize = 4 * width * height; | |
| 192 | |
| 193 const SkBitmap* canvasBitmap = imageBuffer->context()->bitmap(); | |
| 194 const SkBitmap* readbackBitmap = 0; | |
| 195 ASSERT(canvasBitmap->config() == SkBitmap::kARGB_8888_Config); | |
| 196 if (canvasBitmap->width() == width && canvasBitmap->height() == height) { | |
| 197 // This is the fastest and most common case. We read back | |
| 198 // directly into the canvas's backing store. | |
| 199 readbackBitmap = canvasBitmap; | |
| 200 m_resizingBitmap.reset(); | |
| 201 } else { | |
| 202 // We need to allocate a temporary bitmap for reading back the | |
| 203 // pixel data. We will then use Skia to rescale this bitmap to | |
| 204 // the size of the canvas's backing store. | |
| 205 if (m_resizingBitmap.width() != width || m_resizingBitmap.height() != he
ight) { | |
| 206 m_resizingBitmap.setConfig(SkBitmap::kARGB_8888_Config, | |
| 207 width, | |
| 208 height); | |
| 209 if (!m_resizingBitmap.allocPixels()) | |
| 210 return; | |
| 211 } | |
| 212 readbackBitmap = &m_resizingBitmap; | |
| 213 } | |
| 214 | |
| 215 // Read back the frame buffer. | |
| 216 SkAutoLockPixels bitmapLock(*readbackBitmap); | |
| 217 pixels = static_cast<unsigned char*>(readbackBitmap->getPixels()); | |
| 218 | |
| 219 m_impl->readBackFramebuffer(pixels, 4 * width * height, framebuffer, width,
height); | |
| 220 | |
| 221 if (premultiplyAlpha) { | |
| 222 for (size_t i = 0; i < bufferSize; i += 4) { | |
| 223 pixels[i + 0] = std::min(255, pixels[i + 0] * pixels[i + 3] / 255); | |
| 224 pixels[i + 1] = std::min(255, pixels[i + 1] * pixels[i + 3] / 255); | |
| 225 pixels[i + 2] = std::min(255, pixels[i + 2] * pixels[i + 3] / 255); | |
| 226 } | |
| 227 } | |
| 228 | |
| 229 readbackBitmap->notifyPixelsChanged(); | |
| 230 if (m_resizingBitmap.readyToDraw()) { | |
| 231 // We need to draw the resizing bitmap into the canvas's backing store. | |
| 232 SkCanvas canvas(*canvasBitmap); | |
| 233 SkRect dst; | |
| 234 dst.set(SkIntToScalar(0), SkIntToScalar(0), SkIntToScalar(canvasBitmap->
width()), SkIntToScalar(canvasBitmap->height())); | |
| 235 canvas.drawBitmapRect(m_resizingBitmap, 0, dst); | |
| 236 } | |
| 237 } | |
| 238 | |
| 239 class GraphicsContext3DContextLostCallbackAdapter : public WebKit::WebGraphicsCo
ntext3D::WebGraphicsContextLostCallback { | |
| 240 public: | |
| 241 GraphicsContext3DContextLostCallbackAdapter(PassOwnPtr<GraphicsContext3D::Co
ntextLostCallback> callback) | |
| 242 : m_contextLostCallback(callback) { } | |
| 243 virtual ~GraphicsContext3DContextLostCallbackAdapter() { } | |
| 244 | |
| 245 virtual void onContextLost() | |
| 246 { | |
| 247 if (m_contextLostCallback) | |
| 248 m_contextLostCallback->onContextLost(); | |
| 249 } | |
| 250 private: | |
| 251 OwnPtr<GraphicsContext3D::ContextLostCallback> m_contextLostCallback; | |
| 252 }; | |
| 253 | |
| 254 void GraphicsContext3DPrivate::setContextLostCallback(PassOwnPtr<GraphicsContext
3D::ContextLostCallback> callback) | |
| 255 { | |
| 256 if (m_ownedWebContext) { | |
| 257 m_contextLostCallbackAdapter = adoptPtr(new GraphicsContext3DContextLost
CallbackAdapter(callback)); | |
| 258 m_ownedWebContext->setContextLostCallback(m_contextLostCallbackAdapter.g
et()); | |
| 259 } | |
| 260 } | |
| 261 | |
| 262 class GraphicsContext3DErrorMessageCallbackAdapter : public WebKit::WebGraphicsC
ontext3D::WebGraphicsErrorMessageCallback { | |
| 263 public: | |
| 264 GraphicsContext3DErrorMessageCallbackAdapter(PassOwnPtr<GraphicsContext3D::E
rrorMessageCallback> callback) | |
| 265 : m_errorMessageCallback(callback) { } | |
| 266 virtual ~GraphicsContext3DErrorMessageCallbackAdapter() { } | |
| 267 | |
| 268 virtual void onErrorMessage(const WebKit::WebString& message, WebKit::WGC3Di
nt id) | |
| 269 { | |
| 270 if (m_errorMessageCallback) | |
| 271 m_errorMessageCallback->onErrorMessage(message, id); | |
| 272 } | |
| 273 private: | |
| 274 OwnPtr<GraphicsContext3D::ErrorMessageCallback> m_errorMessageCallback; | |
| 275 }; | |
| 276 | |
| 277 void GraphicsContext3DPrivate::setErrorMessageCallback(PassOwnPtr<GraphicsContex
t3D::ErrorMessageCallback> callback) | |
| 278 { | |
| 279 if (m_ownedWebContext) { | |
| 280 m_errorMessageCallbackAdapter = adoptPtr(new GraphicsContext3DErrorMessa
geCallbackAdapter(callback)); | |
| 281 m_ownedWebContext->setErrorMessageCallback(m_errorMessageCallbackAdapter
.get()); | |
| 282 } | |
| 283 } | |
| 284 | |
| 285 Extensions3D* GraphicsContext3DPrivate::getExtensions() | |
| 286 { | |
| 287 if (!m_extensions) | |
| 288 m_extensions = adoptPtr(new Extensions3D(this)); | |
| 289 return m_extensions.get(); | |
| 290 } | |
| 291 | |
| 292 namespace { | |
| 293 | |
| 294 void splitStringHelper(const String& str, HashSet<String>& set) | |
| 295 { | |
| 296 Vector<String> substrings; | |
| 297 str.split(" ", substrings); | |
| 298 for (size_t i = 0; i < substrings.size(); ++i) | |
| 299 set.add(substrings[i]); | |
| 300 } | |
| 301 | |
| 302 String mapExtensionName(const String& name) | |
| 303 { | |
| 304 if (name == "GL_ANGLE_framebuffer_blit" | |
| 305 || name == "GL_ANGLE_framebuffer_multisample") | |
| 306 return "GL_CHROMIUM_framebuffer_multisample"; | |
| 307 return name; | |
| 308 } | |
| 309 | |
| 310 } // anonymous namespace | |
| 311 | |
| 312 void GraphicsContext3DPrivate::initializeExtensions() | |
| 313 { | |
| 314 if (m_initializedAvailableExtensions) | |
| 315 return; | |
| 316 | |
| 317 m_initializedAvailableExtensions = true; | |
| 318 bool success = m_impl->makeContextCurrent(); | |
| 319 ASSERT(success); | |
| 320 if (!success) | |
| 321 return; | |
| 322 | |
| 323 String extensionsString = m_impl->getString(GraphicsContext3D::EXTENSIONS); | |
| 324 splitStringHelper(extensionsString, m_enabledExtensions); | |
| 325 | |
| 326 String requestableExtensionsString = m_impl->getRequestableExtensionsCHROMIU
M(); | |
| 327 splitStringHelper(requestableExtensionsString, m_requestableExtensions); | |
| 328 } | |
| 329 | |
| 330 | |
| 331 bool GraphicsContext3DPrivate::supportsExtension(const String& name) | |
| 332 { | |
| 333 initializeExtensions(); | |
| 334 String mappedName = mapExtensionName(name); | |
| 335 return m_enabledExtensions.contains(mappedName) || m_requestableExtensions.c
ontains(mappedName); | |
| 336 } | |
| 337 | |
| 338 bool GraphicsContext3DPrivate::ensureExtensionEnabled(const String& name) | |
| 339 { | |
| 340 initializeExtensions(); | |
| 341 | |
| 342 String mappedName = mapExtensionName(name); | |
| 343 if (m_enabledExtensions.contains(mappedName)) | |
| 344 return true; | |
| 345 | |
| 346 if (m_requestableExtensions.contains(mappedName)) { | |
| 347 m_impl->requestExtensionCHROMIUM(mappedName.ascii().data()); | |
| 348 m_enabledExtensions.clear(); | |
| 349 m_requestableExtensions.clear(); | |
| 350 m_initializedAvailableExtensions = false; | |
| 351 } | |
| 352 | |
| 353 initializeExtensions(); | |
| 354 return m_enabledExtensions.contains(mappedName); | |
| 355 } | |
| 356 | |
| 357 bool GraphicsContext3DPrivate::isExtensionEnabled(const String& name) | |
| 358 { | |
| 359 initializeExtensions(); | |
| 360 String mappedName = mapExtensionName(name); | |
| 361 return m_enabledExtensions.contains(mappedName); | |
| 362 } | |
| 363 | |
| 364 | |
| 365 bool GraphicsContext3DPrivate::isResourceSafe() | |
| 366 { | |
| 367 if (m_resourceSafety == ResourceSafetyUnknown) | |
| 368 m_resourceSafety = getExtensions()->isEnabled("GL_CHROMIUM_resource_safe
") ? ResourceSafe : ResourceUnsafe; | |
| 369 return m_resourceSafety == ResourceSafe; | |
| 370 } | |
| 371 | |
| 372 } // namespace WebCore | |
| OLD | NEW |