gfx/gl/SharedSurfaceGralloc.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: c++; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 40; -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 #include "mozilla/Preferences.h"
michael@0 7
michael@0 8 #include "SharedSurfaceGralloc.h"
michael@0 9
michael@0 10 #include "GLContext.h"
michael@0 11 #include "SharedSurfaceGL.h"
michael@0 12 #include "SurfaceFactory.h"
michael@0 13 #include "GLLibraryEGL.h"
michael@0 14 #include "mozilla/layers/GrallocTextureClient.h"
michael@0 15 #include "mozilla/layers/ShadowLayers.h"
michael@0 16
michael@0 17 #include "ui/GraphicBuffer.h"
michael@0 18 #include "../layers/ipc/ShadowLayers.h"
michael@0 19 #include "ScopedGLHelpers.h"
michael@0 20
michael@0 21 #include "gfxPlatform.h"
michael@0 22 #include "gfx2DGlue.h"
michael@0 23 #include "gfxPrefs.h"
michael@0 24
michael@0 25 #define DEBUG_GRALLOC
michael@0 26 #ifdef DEBUG_GRALLOC
michael@0 27 #define DEBUG_PRINT(...) do { printf_stderr(__VA_ARGS__); } while (0)
michael@0 28 #else
michael@0 29 #define DEBUG_PRINT(...) do { } while (0)
michael@0 30 #endif
michael@0 31
michael@0 32 using namespace mozilla;
michael@0 33 using namespace mozilla::gfx;
michael@0 34 using namespace gl;
michael@0 35 using namespace layers;
michael@0 36 using namespace android;
michael@0 37
michael@0 38 SurfaceFactory_Gralloc::SurfaceFactory_Gralloc(GLContext* prodGL,
michael@0 39 const SurfaceCaps& caps,
michael@0 40 layers::ISurfaceAllocator* allocator)
michael@0 41 : SurfaceFactory_GL(prodGL, SharedSurfaceType::Gralloc, caps)
michael@0 42 {
michael@0 43 if (caps.surfaceAllocator) {
michael@0 44 allocator = caps.surfaceAllocator;
michael@0 45 }
michael@0 46
michael@0 47 MOZ_ASSERT(allocator);
michael@0 48
michael@0 49 mAllocator = allocator;
michael@0 50 }
michael@0 51
michael@0 52 SharedSurface_Gralloc*
michael@0 53 SharedSurface_Gralloc::Create(GLContext* prodGL,
michael@0 54 const GLFormats& formats,
michael@0 55 const gfx::IntSize& size,
michael@0 56 bool hasAlpha,
michael@0 57 ISurfaceAllocator* allocator)
michael@0 58 {
michael@0 59 GLLibraryEGL* egl = &sEGLLibrary;
michael@0 60 MOZ_ASSERT(egl);
michael@0 61
michael@0 62 DEBUG_PRINT("SharedSurface_Gralloc::Create -------\n");
michael@0 63
michael@0 64 if (!HasExtensions(egl, prodGL))
michael@0 65 return nullptr;
michael@0 66
michael@0 67 gfxContentType type = hasAlpha ? gfxContentType::COLOR_ALPHA
michael@0 68 : gfxContentType::COLOR;
michael@0 69
michael@0 70 gfxImageFormat format
michael@0 71 = gfxPlatform::GetPlatform()->OptimalFormatForContent(type);
michael@0 72
michael@0 73 RefPtr<GrallocTextureClientOGL> grallocTC =
michael@0 74 new GrallocTextureClientOGL(
michael@0 75 allocator,
michael@0 76 gfx::ImageFormatToSurfaceFormat(format),
michael@0 77 gfx::BackendType::NONE, // we don't need to use it with a DrawTarget
michael@0 78 TEXTURE_FLAGS_DEFAULT);
michael@0 79
michael@0 80 if (!grallocTC->AllocateForGLRendering(size)) {
michael@0 81 return nullptr;
michael@0 82 }
michael@0 83
michael@0 84 sp<GraphicBuffer> buffer = grallocTC->GetGraphicBuffer();
michael@0 85
michael@0 86 EGLDisplay display = egl->Display();
michael@0 87 EGLClientBuffer clientBuffer = buffer->getNativeBuffer();
michael@0 88 EGLint attrs[] = {
michael@0 89 LOCAL_EGL_NONE, LOCAL_EGL_NONE
michael@0 90 };
michael@0 91 EGLImage image = egl->fCreateImage(display,
michael@0 92 EGL_NO_CONTEXT,
michael@0 93 LOCAL_EGL_NATIVE_BUFFER_ANDROID,
michael@0 94 clientBuffer, attrs);
michael@0 95 if (!image) {
michael@0 96 return nullptr;
michael@0 97 }
michael@0 98
michael@0 99 prodGL->MakeCurrent();
michael@0 100 GLuint prodTex = 0;
michael@0 101 prodGL->fGenTextures(1, &prodTex);
michael@0 102 ScopedBindTexture autoTex(prodGL, prodTex);
michael@0 103
michael@0 104 prodGL->fTexParameteri(LOCAL_GL_TEXTURE_2D, LOCAL_GL_TEXTURE_MIN_FILTER, LOCAL_GL_LINEAR);
michael@0 105 prodGL->fTexParameteri(LOCAL_GL_TEXTURE_2D, LOCAL_GL_TEXTURE_MAG_FILTER, LOCAL_GL_LINEAR);
michael@0 106 prodGL->fTexParameteri(LOCAL_GL_TEXTURE_2D, LOCAL_GL_TEXTURE_WRAP_S, LOCAL_GL_CLAMP_TO_EDGE);
michael@0 107 prodGL->fTexParameteri(LOCAL_GL_TEXTURE_2D, LOCAL_GL_TEXTURE_WRAP_T, LOCAL_GL_CLAMP_TO_EDGE);
michael@0 108
michael@0 109 prodGL->fEGLImageTargetTexture2D(LOCAL_GL_TEXTURE_2D, image);
michael@0 110
michael@0 111 egl->fDestroyImage(display, image);
michael@0 112
michael@0 113 SharedSurface_Gralloc *surf = new SharedSurface_Gralloc(prodGL, size, hasAlpha, egl, allocator, grallocTC, prodTex);
michael@0 114
michael@0 115 DEBUG_PRINT("SharedSurface_Gralloc::Create: success -- surface %p, GraphicBuffer %p.\n", surf, buffer.get());
michael@0 116
michael@0 117 return surf;
michael@0 118 }
michael@0 119
michael@0 120
michael@0 121 bool
michael@0 122 SharedSurface_Gralloc::HasExtensions(GLLibraryEGL* egl, GLContext* gl)
michael@0 123 {
michael@0 124 return egl->HasKHRImageBase() &&
michael@0 125 gl->IsExtensionSupported(GLContext::OES_EGL_image);
michael@0 126 }
michael@0 127
michael@0 128 SharedSurface_Gralloc::~SharedSurface_Gralloc()
michael@0 129 {
michael@0 130
michael@0 131 DEBUG_PRINT("[SharedSurface_Gralloc %p] destroyed\n", this);
michael@0 132
michael@0 133 mGL->MakeCurrent();
michael@0 134 mGL->fDeleteTextures(1, &mProdTex);
michael@0 135 }
michael@0 136
michael@0 137 void
michael@0 138 SharedSurface_Gralloc::Fence()
michael@0 139 {
michael@0 140 // We should be able to rely on genlock write locks/read locks.
michael@0 141 // But they're broken on some configs, and even a glFinish doesn't
michael@0 142 // work. glReadPixels seems to, though.
michael@0 143 if (gfxPrefs::GrallocFenceWithReadPixels()) {
michael@0 144 mGL->MakeCurrent();
michael@0 145 // read a 1x1 pixel
michael@0 146 unsigned char pixels[4];
michael@0 147 mGL->fReadPixels(0, 0, 1, 1, LOCAL_GL_RGBA, LOCAL_GL_UNSIGNED_BYTE, &pixels[0]);
michael@0 148 }
michael@0 149 }
michael@0 150
michael@0 151 bool
michael@0 152 SharedSurface_Gralloc::WaitSync()
michael@0 153 {
michael@0 154 return true;
michael@0 155 }

mercurial