DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Header

Mercurial (9136070a0561)

VCS Links

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */

#include "CanvasTranslator.h"

#include "mozilla/gfx/2D.h"
#include "mozilla/gfx/Logging.h"
#include "RecordedCanvasEventImpl.h"

#if defined(XP_WIN)
#  include "mozilla/gfx/DeviceManagerDx.h"
#  include "mozilla/layers/TextureD3D11.h"
#endif

namespace mozilla {
namespace layers {

// When in a transaction we wait for a short time because we're expecting more
// events from the content process. We don't want to wait for too long in case
// other content processes are waiting for events to process.
static const TimeDuration kReadEventTimeout = TimeDuration::FromMilliseconds(5);

static TextureData* CreateTextureData(TextureType aTextureType,
                                      const gfx::IntSize& aSize,
                                      gfx::SurfaceFormat aFormat) {
  TextureData* textureData = nullptr;
  switch (aTextureType) {
#ifdef XP_WIN
    case TextureType::D3D11: {
      RefPtr<ID3D11Device> device =
          gfx::DeviceManagerDx::Get()->GetCanvasDevice();
      MOZ_RELEASE_ASSERT(device, "Failed to get a device for canvas drawing.");
      textureData =
          D3D11TextureData::Create(aSize, aFormat, ALLOC_CLEAR_BUFFER, device);
      break;
    }
#endif
    default:
      MOZ_CRASH("Unsupported TextureType for CanvasTranslator.");
  }

  if (!textureData) {
    MOZ_CRASH("Failed to create TextureData.");
  }

  return textureData;
}

/* static */
UniquePtr<CanvasTranslator> CanvasTranslator::Create() {
  return UniquePtr<CanvasTranslator>(new CanvasTranslator());
}

CanvasTranslator::CanvasTranslator() : gfx::InlineTranslator() {}

CanvasTranslator::~CanvasTranslator() {
  if (mReferenceTextureData) {
    mReferenceTextureData->Unlock();
  }
}

bool CanvasTranslator::Init(
    const TextureType& aTextureType,
    const ipc::SharedMemoryBasic::Handle& aReadHandle,
    const CrossProcessSemaphoreHandle& aReaderSem,
    const CrossProcessSemaphoreHandle& aWriterSem,
    UniquePtr<CanvasEventRingBuffer::ReaderServices> aReaderServices) {
  mTextureType = aTextureType;
  mReferenceTextureData.reset(CreateTextureData(
      aTextureType, gfx::IntSize(1, 1), gfx::SurfaceFormat::B8G8R8A8));
  mReferenceTextureData->Lock(OpenMode::OPEN_READ_WRITE);
  mBaseDT = mReferenceTextureData->BorrowDrawTarget();
  return mStream.InitReader(aReadHandle, aReaderSem, aWriterSem,
                            std::move(aReaderServices));
}

bool CanvasTranslator::TranslateRecording() {
  int32_t eventType = mStream.ReadNextEvent();
  while (mStream.good()) {
    bool success = RecordedEvent::DoWithEventFromStream(
        mStream, static_cast<RecordedEvent::EventType>(eventType),
        [&](RecordedEvent* recordedEvent) -> bool {
          // Make sure that the whole event was read from the stream.
          if (!mStream.good()) {
            return false;
          }

          return recordedEvent->PlayEvent(this);
        });

    if (!success && !HandleExtensionEvent(eventType)) {
      gfxDevCrash(gfx::LogReason::PlayEventFailed)
          << "Failed to play canvas event type: " << eventType;
      mIsValid = false;
      return true;
    }

    if (!mIsInTransaction) {
      return mStream.StopIfEmpty();
    }

    if (!mStream.HasDataToRead()) {
      // We're going to wait for the next event, so take the opportunity to
      // flush the rendering.
      Flush();
      if (!mStream.WaitForDataToRead(kReadEventTimeout, 0)) {
        return true;
      }
    }

    eventType = mStream.ReadNextEvent();
  }

  mIsValid = false;
  return true;
}

#define READ_AND_PLAY_CANVAS_EVENT_TYPE(_typeenum, _class) \
  case _typeenum: {                                        \
    auto e = _class(mStream);                              \
    if (!mStream.good()) {                                 \
      return false;                                        \
    }                                                      \
    return e.PlayCanvasEvent(this);                        \
  }

bool CanvasTranslator::HandleExtensionEvent(int32_t aType) {
  // This is where we handle extensions to the Moz2D Recording events to handle
  // canvas specific things.
  switch (aType) {
    FOR_EACH_CANVAS_EVENT(READ_AND_PLAY_CANVAS_EVENT_TYPE)
    default:
      return false;
  }
}

void CanvasTranslator::BeginTransaction() { mIsInTransaction = true; }

void CanvasTranslator::Flush() {
#if defined(XP_WIN)
  gfx::AutoSerializeWithMoz2D serializeWithMoz2D(
      GetReferenceDrawTarget()->GetBackendType());
  RefPtr<ID3D11Device> device = gfx::DeviceManagerDx::Get()->GetCanvasDevice();
  RefPtr<ID3D11DeviceContext> deviceContext;
  device->GetImmediateContext(getter_AddRefs(deviceContext));
  deviceContext->Flush();
#endif
}

void CanvasTranslator::EndTransaction() {
  Flush();
  mIsInTransaction = false;
}

void CanvasTranslator::AddSurfaceDescriptor(gfx::ReferencePtr aRefPtr,
                                            TextureData* aTextureData) {
  UniquePtr<SurfaceDescriptor> descriptor = MakeUnique<SurfaceDescriptor>();
  if (!aTextureData->Serialize(*descriptor)) {
    MOZ_CRASH("Failed to serialize");
  }

  MonitorAutoLock lock(mSurfaceDescriptorsMonitor);
  mSurfaceDescriptors[aRefPtr] = std::move(descriptor);
  mSurfaceDescriptorsMonitor.Notify();
}

already_AddRefed<gfx::DrawTarget> CanvasTranslator::CreateDrawTarget(
    gfx::ReferencePtr aRefPtr, const gfx::IntSize& aSize,
    gfx::SurfaceFormat aFormat) {
  gfx::AutoSerializeWithMoz2D serializeWithMoz2D(
      GetReferenceDrawTarget()->GetBackendType());
  TextureData* textureData = CreateTextureData(mTextureType, aSize, aFormat);
  textureData->Lock(OpenMode::OPEN_READ_WRITE);
  mTextureDatas[aRefPtr] = UniquePtr<TextureData>(textureData);
  AddSurfaceDescriptor(aRefPtr, textureData);
  RefPtr<gfx::DrawTarget> dt = textureData->BorrowDrawTarget();
  AddDrawTarget(aRefPtr, dt);

  return dt.forget();
}

void CanvasTranslator::RemoveDrawTarget(gfx::ReferencePtr aDrawTarget) {
  InlineTranslator::RemoveDrawTarget(aDrawTarget);
  gfx::AutoSerializeWithMoz2D serializeWithMoz2D(
      GetReferenceDrawTarget()->GetBackendType());
  mTextureDatas.erase(aDrawTarget);
}

TextureData* CanvasTranslator::LookupTextureData(
    gfx::ReferencePtr aDrawTarget) {
  TextureMap::const_iterator result = mTextureDatas.find(aDrawTarget);
  if (result == mTextureDatas.end()) {
    return nullptr;
  }
  return result->second.get();
}

UniquePtr<SurfaceDescriptor> CanvasTranslator::WaitForSurfaceDescriptor(
    gfx::ReferencePtr aDrawTarget) {
  MonitorAutoLock lock(mSurfaceDescriptorsMonitor);
  DescriptorMap::iterator result;
  while ((result = mSurfaceDescriptors.find(aDrawTarget)) ==
         mSurfaceDescriptors.end()) {
    mSurfaceDescriptorsMonitor.Wait();
  }

  UniquePtr<SurfaceDescriptor> descriptor = std::move(result->second);
  mSurfaceDescriptors.erase(aDrawTarget);
  return descriptor;
}

gfx::DataSourceSurface* CanvasTranslator::LookupDataSurface(
    gfx::ReferencePtr aRefPtr) {
  return mDataSurfaces.GetWeak(aRefPtr);
}

void CanvasTranslator::AddDataSurface(
    gfx::ReferencePtr aRefPtr, RefPtr<gfx::DataSourceSurface>&& aSurface) {
  mDataSurfaces.Put(aRefPtr, aSurface);
}

void CanvasTranslator::RemoveDataSurface(gfx::ReferencePtr aRefPtr) {
  mDataSurfaces.Remove(aRefPtr);
}

void CanvasTranslator::SetPreparedMap(
    gfx::ReferencePtr aSurface,
    UniquePtr<gfx::DataSourceSurface::ScopedMap> aMap) {
  mMappedSurface = aSurface;
  mPreparedMap = std::move(aMap);
}

UniquePtr<gfx::DataSourceSurface::ScopedMap> CanvasTranslator::GetPreparedMap(
    gfx::ReferencePtr aSurface) {
  MOZ_RELEASE_ASSERT(mMappedSurface == aSurface,
                     "aSurface must match previously stored surface.");

  mMappedSurface = nullptr;
  return std::move(mPreparedMap);
}

}  // namespace layers
}  // namespace mozilla