Wed, 31 Dec 2014 06:09:35 +0100
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++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
michael@0 | 2 | * vim: sw=2 ts=8 et : |
michael@0 | 3 | */ |
michael@0 | 4 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 5 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 6 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 7 | |
michael@0 | 8 | #include "mozilla/DebugOnly.h" |
michael@0 | 9 | |
michael@0 | 10 | #include "mozilla/gfx/Point.h" |
michael@0 | 11 | #include "mozilla/layers/PGrallocBufferChild.h" |
michael@0 | 12 | #include "mozilla/layers/PGrallocBufferParent.h" |
michael@0 | 13 | #include "mozilla/layers/LayerTransactionChild.h" |
michael@0 | 14 | #include "mozilla/layers/ShadowLayers.h" |
michael@0 | 15 | #include "mozilla/layers/LayerManagerComposite.h" |
michael@0 | 16 | #include "mozilla/layers/CompositorTypes.h" |
michael@0 | 17 | #include "mozilla/layers/TextureHost.h" |
michael@0 | 18 | #include "mozilla/unused.h" |
michael@0 | 19 | #include "nsXULAppAPI.h" |
michael@0 | 20 | |
michael@0 | 21 | #include "ShadowLayerUtilsGralloc.h" |
michael@0 | 22 | |
michael@0 | 23 | #include "nsIMemoryReporter.h" |
michael@0 | 24 | |
michael@0 | 25 | #include "gfxPlatform.h" |
michael@0 | 26 | #include "gfx2DGlue.h" |
michael@0 | 27 | #include "GLContext.h" |
michael@0 | 28 | |
michael@0 | 29 | #include "GeckoProfiler.h" |
michael@0 | 30 | |
michael@0 | 31 | #include "cutils/properties.h" |
michael@0 | 32 | |
michael@0 | 33 | #include "MainThreadUtils.h" |
michael@0 | 34 | |
michael@0 | 35 | using namespace android; |
michael@0 | 36 | using namespace base; |
michael@0 | 37 | using namespace mozilla::layers; |
michael@0 | 38 | using namespace mozilla::gl; |
michael@0 | 39 | |
michael@0 | 40 | namespace IPC { |
michael@0 | 41 | |
michael@0 | 42 | void |
michael@0 | 43 | ParamTraits<MagicGrallocBufferHandle>::Write(Message* aMsg, |
michael@0 | 44 | const paramType& aParam) |
michael@0 | 45 | { |
michael@0 | 46 | #if ANDROID_VERSION >= 19 |
michael@0 | 47 | sp<GraphicBuffer> flattenable = aParam.mGraphicBuffer; |
michael@0 | 48 | #else |
michael@0 | 49 | Flattenable *flattenable = aParam.mGraphicBuffer.get(); |
michael@0 | 50 | #endif |
michael@0 | 51 | size_t nbytes = flattenable->getFlattenedSize(); |
michael@0 | 52 | size_t nfds = flattenable->getFdCount(); |
michael@0 | 53 | |
michael@0 | 54 | char data[nbytes]; |
michael@0 | 55 | int fds[nfds]; |
michael@0 | 56 | |
michael@0 | 57 | #if ANDROID_VERSION >= 19 |
michael@0 | 58 | // Make a copy of "data" and "fds" for flatten() to avoid casting problem |
michael@0 | 59 | void *pdata = (void *)data; |
michael@0 | 60 | int *pfds = fds; |
michael@0 | 61 | |
michael@0 | 62 | flattenable->flatten(pdata, nbytes, pfds, nfds); |
michael@0 | 63 | |
michael@0 | 64 | // In Kitkat, flatten() will change the value of nbytes and nfds, which dues |
michael@0 | 65 | // to multiple parcelable object consumption. The actual size and fd count |
michael@0 | 66 | // which returned by getFlattenedSize() and getFdCount() are not changed. |
michael@0 | 67 | // So we change nbytes and nfds back by call corresponding calls. |
michael@0 | 68 | nbytes = flattenable->getFlattenedSize(); |
michael@0 | 69 | nfds = flattenable->getFdCount(); |
michael@0 | 70 | #else |
michael@0 | 71 | flattenable->flatten(data, nbytes, fds, nfds); |
michael@0 | 72 | #endif |
michael@0 | 73 | |
michael@0 | 74 | aMsg->WriteSize(nbytes); |
michael@0 | 75 | aMsg->WriteSize(nfds); |
michael@0 | 76 | |
michael@0 | 77 | aMsg->WriteBytes(data, nbytes); |
michael@0 | 78 | for (size_t n = 0; n < nfds; ++n) { |
michael@0 | 79 | // These buffers can't die in transit because they're created |
michael@0 | 80 | // synchonously and the parent-side buffer can only be dropped if |
michael@0 | 81 | // there's a crash. |
michael@0 | 82 | aMsg->WriteFileDescriptor(FileDescriptor(fds[n], false)); |
michael@0 | 83 | } |
michael@0 | 84 | } |
michael@0 | 85 | |
michael@0 | 86 | bool |
michael@0 | 87 | ParamTraits<MagicGrallocBufferHandle>::Read(const Message* aMsg, |
michael@0 | 88 | void** aIter, paramType* aResult) |
michael@0 | 89 | { |
michael@0 | 90 | size_t nbytes; |
michael@0 | 91 | size_t nfds; |
michael@0 | 92 | const char* data; |
michael@0 | 93 | |
michael@0 | 94 | if (!aMsg->ReadSize(aIter, &nbytes) || |
michael@0 | 95 | !aMsg->ReadSize(aIter, &nfds) || |
michael@0 | 96 | !aMsg->ReadBytes(aIter, &data, nbytes)) { |
michael@0 | 97 | return false; |
michael@0 | 98 | } |
michael@0 | 99 | |
michael@0 | 100 | int fds[nfds]; |
michael@0 | 101 | |
michael@0 | 102 | for (size_t n = 0; n < nfds; ++n) { |
michael@0 | 103 | FileDescriptor fd; |
michael@0 | 104 | if (!aMsg->ReadFileDescriptor(aIter, &fd)) { |
michael@0 | 105 | return false; |
michael@0 | 106 | } |
michael@0 | 107 | // If the GraphicBuffer was shared cross-process, SCM_RIGHTS does |
michael@0 | 108 | // the right thing and dup's the fd. If it's shared cross-thread, |
michael@0 | 109 | // SCM_RIGHTS doesn't dup the fd. That's surprising, but we just |
michael@0 | 110 | // deal with it here. NB: only the "default" (master) process can |
michael@0 | 111 | // alloc gralloc buffers. |
michael@0 | 112 | bool sameProcess = (XRE_GetProcessType() == GeckoProcessType_Default); |
michael@0 | 113 | int dupFd = sameProcess ? dup(fd.fd) : fd.fd; |
michael@0 | 114 | fds[n] = dupFd; |
michael@0 | 115 | } |
michael@0 | 116 | |
michael@0 | 117 | sp<GraphicBuffer> buffer(new GraphicBuffer()); |
michael@0 | 118 | #if ANDROID_VERSION >= 19 |
michael@0 | 119 | // Make a copy of "data" and "fds" for unflatten() to avoid casting problem |
michael@0 | 120 | void const *pdata = (void const *)data; |
michael@0 | 121 | int const *pfds = fds; |
michael@0 | 122 | |
michael@0 | 123 | if (NO_ERROR == buffer->unflatten(pdata, nbytes, pfds, nfds)) { |
michael@0 | 124 | #else |
michael@0 | 125 | Flattenable *flattenable = buffer.get(); |
michael@0 | 126 | |
michael@0 | 127 | if (NO_ERROR == flattenable->unflatten(data, nbytes, fds, nfds)) { |
michael@0 | 128 | #endif |
michael@0 | 129 | aResult->mGraphicBuffer = buffer; |
michael@0 | 130 | return true; |
michael@0 | 131 | } |
michael@0 | 132 | return false; |
michael@0 | 133 | } |
michael@0 | 134 | |
michael@0 | 135 | } // namespace IPC |
michael@0 | 136 | |
michael@0 | 137 | namespace mozilla { |
michael@0 | 138 | namespace layers { |
michael@0 | 139 | |
michael@0 | 140 | MagicGrallocBufferHandle::MagicGrallocBufferHandle(const sp<GraphicBuffer>& aGraphicBuffer) |
michael@0 | 141 | : mGraphicBuffer(aGraphicBuffer) |
michael@0 | 142 | { |
michael@0 | 143 | } |
michael@0 | 144 | |
michael@0 | 145 | //----------------------------------------------------------------------------- |
michael@0 | 146 | // Parent process |
michael@0 | 147 | |
michael@0 | 148 | static gfxImageFormat |
michael@0 | 149 | ImageFormatForPixelFormat(android::PixelFormat aFormat) |
michael@0 | 150 | { |
michael@0 | 151 | switch (aFormat) { |
michael@0 | 152 | case PIXEL_FORMAT_RGBA_8888: |
michael@0 | 153 | return gfxImageFormat::ARGB32; |
michael@0 | 154 | case PIXEL_FORMAT_RGBX_8888: |
michael@0 | 155 | return gfxImageFormat::RGB24; |
michael@0 | 156 | case PIXEL_FORMAT_RGB_565: |
michael@0 | 157 | return gfxImageFormat::RGB16_565; |
michael@0 | 158 | default: |
michael@0 | 159 | MOZ_CRASH("Unknown gralloc pixel format"); |
michael@0 | 160 | } |
michael@0 | 161 | return gfxImageFormat::ARGB32; |
michael@0 | 162 | } |
michael@0 | 163 | |
michael@0 | 164 | static android::PixelFormat |
michael@0 | 165 | PixelFormatForImageFormat(gfxImageFormat aFormat) |
michael@0 | 166 | { |
michael@0 | 167 | switch (aFormat) { |
michael@0 | 168 | case gfxImageFormat::ARGB32: |
michael@0 | 169 | return android::PIXEL_FORMAT_RGBA_8888; |
michael@0 | 170 | case gfxImageFormat::RGB24: |
michael@0 | 171 | return android::PIXEL_FORMAT_RGBX_8888; |
michael@0 | 172 | case gfxImageFormat::RGB16_565: |
michael@0 | 173 | return android::PIXEL_FORMAT_RGB_565; |
michael@0 | 174 | case gfxImageFormat::A8: |
michael@0 | 175 | NS_WARNING("gralloc does not support gfxImageFormat::A8"); |
michael@0 | 176 | return android::PIXEL_FORMAT_UNKNOWN; |
michael@0 | 177 | default: |
michael@0 | 178 | MOZ_CRASH("Unknown gralloc pixel format"); |
michael@0 | 179 | } |
michael@0 | 180 | return android::PIXEL_FORMAT_RGBA_8888; |
michael@0 | 181 | } |
michael@0 | 182 | |
michael@0 | 183 | static size_t |
michael@0 | 184 | BytesPerPixelForPixelFormat(android::PixelFormat aFormat) |
michael@0 | 185 | { |
michael@0 | 186 | switch (aFormat) { |
michael@0 | 187 | case PIXEL_FORMAT_RGBA_8888: |
michael@0 | 188 | case PIXEL_FORMAT_RGBX_8888: |
michael@0 | 189 | case PIXEL_FORMAT_BGRA_8888: |
michael@0 | 190 | return 4; |
michael@0 | 191 | case PIXEL_FORMAT_RGB_888: |
michael@0 | 192 | return 3; |
michael@0 | 193 | case PIXEL_FORMAT_RGB_565: |
michael@0 | 194 | case PIXEL_FORMAT_RGBA_5551: |
michael@0 | 195 | case PIXEL_FORMAT_RGBA_4444: |
michael@0 | 196 | return 2; |
michael@0 | 197 | default: |
michael@0 | 198 | return 0; |
michael@0 | 199 | } |
michael@0 | 200 | return 0; |
michael@0 | 201 | } |
michael@0 | 202 | |
michael@0 | 203 | static android::PixelFormat |
michael@0 | 204 | PixelFormatForContentType(gfxContentType aContentType) |
michael@0 | 205 | { |
michael@0 | 206 | return PixelFormatForImageFormat( |
michael@0 | 207 | gfxPlatform::GetPlatform()->OptimalFormatForContent(aContentType)); |
michael@0 | 208 | } |
michael@0 | 209 | |
michael@0 | 210 | static gfxContentType |
michael@0 | 211 | ContentTypeFromPixelFormat(android::PixelFormat aFormat) |
michael@0 | 212 | { |
michael@0 | 213 | return gfxASurface::ContentFromFormat(ImageFormatForPixelFormat(aFormat)); |
michael@0 | 214 | } |
michael@0 | 215 | |
michael@0 | 216 | class GrallocReporter MOZ_FINAL : public nsIMemoryReporter |
michael@0 | 217 | { |
michael@0 | 218 | friend class GrallocBufferActor; |
michael@0 | 219 | |
michael@0 | 220 | public: |
michael@0 | 221 | NS_DECL_ISUPPORTS |
michael@0 | 222 | |
michael@0 | 223 | GrallocReporter() |
michael@0 | 224 | { |
michael@0 | 225 | #ifdef DEBUG |
michael@0 | 226 | // There must be only one instance of this class, due to |sAmount| |
michael@0 | 227 | // being static. Assert this. |
michael@0 | 228 | static bool hasRun = false; |
michael@0 | 229 | MOZ_ASSERT(!hasRun); |
michael@0 | 230 | hasRun = true; |
michael@0 | 231 | #endif |
michael@0 | 232 | } |
michael@0 | 233 | |
michael@0 | 234 | NS_IMETHOD CollectReports(nsIHandleReportCallback* aHandleReport, |
michael@0 | 235 | nsISupports* aData) |
michael@0 | 236 | { |
michael@0 | 237 | return MOZ_COLLECT_REPORT( |
michael@0 | 238 | "gralloc", KIND_OTHER, UNITS_BYTES, sAmount, |
michael@0 | 239 | "Special RAM that can be shared between processes and directly accessed by " |
michael@0 | 240 | "both the CPU and GPU. Gralloc memory is usually a relatively precious " |
michael@0 | 241 | "resource, with much less available than generic RAM. When it's exhausted, " |
michael@0 | 242 | "graphics performance can suffer. This value can be incorrect because of race " |
michael@0 | 243 | "conditions."); |
michael@0 | 244 | } |
michael@0 | 245 | |
michael@0 | 246 | private: |
michael@0 | 247 | static int64_t sAmount; |
michael@0 | 248 | }; |
michael@0 | 249 | |
michael@0 | 250 | NS_IMPL_ISUPPORTS(GrallocReporter, nsIMemoryReporter) |
michael@0 | 251 | |
michael@0 | 252 | int64_t GrallocReporter::sAmount = 0; |
michael@0 | 253 | |
michael@0 | 254 | void InitGralloc() { |
michael@0 | 255 | NS_ASSERTION(NS_IsMainThread(), "Should be on main thread."); |
michael@0 | 256 | RegisterStrongMemoryReporter(new GrallocReporter()); |
michael@0 | 257 | } |
michael@0 | 258 | |
michael@0 | 259 | GrallocBufferActor::GrallocBufferActor() |
michael@0 | 260 | : mAllocBytes(0) |
michael@0 | 261 | , mTextureHost(nullptr) |
michael@0 | 262 | { |
michael@0 | 263 | } |
michael@0 | 264 | |
michael@0 | 265 | GrallocBufferActor::~GrallocBufferActor() |
michael@0 | 266 | { |
michael@0 | 267 | if (mAllocBytes > 0) { |
michael@0 | 268 | GrallocReporter::sAmount -= mAllocBytes; |
michael@0 | 269 | } |
michael@0 | 270 | } |
michael@0 | 271 | |
michael@0 | 272 | /*static*/ PGrallocBufferParent* |
michael@0 | 273 | GrallocBufferActor::Create(const gfx::IntSize& aSize, |
michael@0 | 274 | const uint32_t& aFormat, |
michael@0 | 275 | const uint32_t& aUsage, |
michael@0 | 276 | MaybeMagicGrallocBufferHandle* aOutHandle) |
michael@0 | 277 | { |
michael@0 | 278 | PROFILER_LABEL("GrallocBufferActor", "Create"); |
michael@0 | 279 | GrallocBufferActor* actor = new GrallocBufferActor(); |
michael@0 | 280 | *aOutHandle = null_t(); |
michael@0 | 281 | uint32_t format = aFormat; |
michael@0 | 282 | uint32_t usage = aUsage; |
michael@0 | 283 | |
michael@0 | 284 | if (format == 0 || usage == 0) { |
michael@0 | 285 | printf_stderr("GrallocBufferActor::Create -- format and usage must be non-zero"); |
michael@0 | 286 | return actor; |
michael@0 | 287 | } |
michael@0 | 288 | |
michael@0 | 289 | // If the requested size is too big (i.e. exceeds the commonly used max GL texture size) |
michael@0 | 290 | // then we risk OOMing the parent process. It's better to just deny the allocation and |
michael@0 | 291 | // kill the child process, which is what the following code does. |
michael@0 | 292 | // TODO: actually use GL_MAX_TEXTURE_SIZE instead of hardcoding 4096 |
michael@0 | 293 | if (aSize.width > 4096 || aSize.height > 4096) { |
michael@0 | 294 | printf_stderr("GrallocBufferActor::Create -- requested gralloc buffer is too big. Killing child instead."); |
michael@0 | 295 | delete actor; |
michael@0 | 296 | return nullptr; |
michael@0 | 297 | } |
michael@0 | 298 | |
michael@0 | 299 | sp<GraphicBuffer> buffer(new GraphicBuffer(aSize.width, aSize.height, format, usage)); |
michael@0 | 300 | if (buffer->initCheck() != OK) |
michael@0 | 301 | return actor; |
michael@0 | 302 | |
michael@0 | 303 | size_t bpp = BytesPerPixelForPixelFormat(format); |
michael@0 | 304 | actor->mAllocBytes = aSize.width * aSize.height * bpp; |
michael@0 | 305 | GrallocReporter::sAmount += actor->mAllocBytes; |
michael@0 | 306 | |
michael@0 | 307 | actor->mGraphicBuffer = buffer; |
michael@0 | 308 | *aOutHandle = MagicGrallocBufferHandle(buffer); |
michael@0 | 309 | |
michael@0 | 310 | return actor; |
michael@0 | 311 | } |
michael@0 | 312 | |
michael@0 | 313 | void GrallocBufferActor::ActorDestroy(ActorDestroyReason) |
michael@0 | 314 | { |
michael@0 | 315 | // Used only for hacky fix for bug 966446. |
michael@0 | 316 | if (mTextureHost) { |
michael@0 | 317 | mTextureHost->ForgetBufferActor(); |
michael@0 | 318 | mTextureHost = nullptr; |
michael@0 | 319 | } |
michael@0 | 320 | } |
michael@0 | 321 | |
michael@0 | 322 | void GrallocBufferActor::AddTextureHost(TextureHost* aTextureHost) |
michael@0 | 323 | { |
michael@0 | 324 | mTextureHost = aTextureHost; |
michael@0 | 325 | } |
michael@0 | 326 | |
michael@0 | 327 | void GrallocBufferActor::RemoveTextureHost() |
michael@0 | 328 | { |
michael@0 | 329 | mTextureHost = nullptr; |
michael@0 | 330 | } |
michael@0 | 331 | |
michael@0 | 332 | /*static*/ bool |
michael@0 | 333 | LayerManagerComposite::SupportsDirectTexturing() |
michael@0 | 334 | { |
michael@0 | 335 | return true; |
michael@0 | 336 | } |
michael@0 | 337 | |
michael@0 | 338 | /*static*/ void |
michael@0 | 339 | LayerManagerComposite::PlatformSyncBeforeReplyUpdate() |
michael@0 | 340 | { |
michael@0 | 341 | // Nothing to be done for gralloc. |
michael@0 | 342 | } |
michael@0 | 343 | |
michael@0 | 344 | //----------------------------------------------------------------------------- |
michael@0 | 345 | // Child process |
michael@0 | 346 | |
michael@0 | 347 | /*static*/ PGrallocBufferChild* |
michael@0 | 348 | GrallocBufferActor::Create() |
michael@0 | 349 | { |
michael@0 | 350 | return new GrallocBufferActor(); |
michael@0 | 351 | } |
michael@0 | 352 | |
michael@0 | 353 | void |
michael@0 | 354 | GrallocBufferActor::InitFromHandle(const MagicGrallocBufferHandle& aHandle) |
michael@0 | 355 | { |
michael@0 | 356 | MOZ_ASSERT(!mGraphicBuffer.get()); |
michael@0 | 357 | MOZ_ASSERT(aHandle.mGraphicBuffer.get()); |
michael@0 | 358 | |
michael@0 | 359 | mGraphicBuffer = aHandle.mGraphicBuffer; |
michael@0 | 360 | } |
michael@0 | 361 | |
michael@0 | 362 | PGrallocBufferChild* |
michael@0 | 363 | ShadowLayerForwarder::AllocGrallocBuffer(const gfx::IntSize& aSize, |
michael@0 | 364 | uint32_t aFormat, |
michael@0 | 365 | uint32_t aUsage, |
michael@0 | 366 | MaybeMagicGrallocBufferHandle* aHandle) |
michael@0 | 367 | { |
michael@0 | 368 | if (!mShadowManager->IPCOpen()) { |
michael@0 | 369 | return nullptr; |
michael@0 | 370 | } |
michael@0 | 371 | return mShadowManager->SendPGrallocBufferConstructor(aSize, aFormat, aUsage, aHandle); |
michael@0 | 372 | } |
michael@0 | 373 | |
michael@0 | 374 | void |
michael@0 | 375 | ShadowLayerForwarder::DeallocGrallocBuffer(PGrallocBufferChild* aChild) |
michael@0 | 376 | { |
michael@0 | 377 | MOZ_ASSERT(aChild); |
michael@0 | 378 | PGrallocBufferChild::Send__delete__(aChild); |
michael@0 | 379 | } |
michael@0 | 380 | |
michael@0 | 381 | //----------------------------------------------------------------------------- |
michael@0 | 382 | // Both processes |
michael@0 | 383 | |
michael@0 | 384 | android::GraphicBuffer* |
michael@0 | 385 | GrallocBufferActor::GetGraphicBuffer() |
michael@0 | 386 | { |
michael@0 | 387 | return mGraphicBuffer.get(); |
michael@0 | 388 | } |
michael@0 | 389 | |
michael@0 | 390 | /*static*/ void |
michael@0 | 391 | ShadowLayerForwarder::PlatformSyncBeforeUpdate() |
michael@0 | 392 | { |
michael@0 | 393 | // Nothing to be done for gralloc. |
michael@0 | 394 | } |
michael@0 | 395 | |
michael@0 | 396 | } // namespace layers |
michael@0 | 397 | } // namespace mozilla |