summaryrefslogtreecommitdiffstats
path: root/ppapi/proxy/image_data.cc
blob: 2af702602ca957bcf447f09b959afd1a952c800f (plain)
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
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ppapi/proxy/image_data.h"

#if defined(OS_LINUX)
#include <sys/shm.h>
#endif

#if defined(OS_MACOSX)
#include <sys/stat.h>
#include <sys/mman.h>
#endif

namespace pp {
namespace proxy {

ImageData::ImageData(const PP_ImageDataDesc& desc, ImageHandle handle)
    : desc_(desc),
      handle_(handle),
      mapped_data_(NULL) {
}

ImageData::~ImageData() {
  Unmap();
}

ImageData* ImageData::AsImageData() {
  return this;
}

void* ImageData::Map() {
#if defined(OS_WIN)
  NOTIMPLEMENTED();
  return NULL;
#elif defined(OS_MACOSX)
  struct stat st;
  if (fstat(handle_.fd, &st) != 0)
    return NULL;
  void* memory = mmap(NULL, st.st_size, PROT_READ | PROT_WRITE,
                      MAP_SHARED, handle_.fd, 0);
  if (memory == MAP_FAILED)
    return NULL;
  mapped_data_ = memory;
  return mapped_data_;
#else
  return NULL;
  int shmkey = handle_;
  void* address = shmat(shmkey, NULL, 0);
  // Mark for deletion in case we crash so the kernel will clean it up.
  shmctl(shmkey, IPC_RMID, 0);
  if (address == (void*)-1)
    return NULL;
  mapped_data_ = address;
  return address;
#endif
}

void ImageData::Unmap() {
#if defined(OS_WIN)
  NOTIMPLEMENTED();
#elif defined(OS_MACOSX)
  if (mapped_data_) {
    struct stat st;
    if (fstat(handle_.fd, &st) == 0)
      munmap(mapped_data_, st.st_size);
  }
#else
  if (mapped_data_)
    shmdt(mapped_data_);
#endif
  mapped_data_ = NULL;
}

#if defined(OS_WIN)
const ImageHandle ImageData::NullHandle = NULL;
#elif defined(OS_MACOSX)
const ImageHandle ImageData::NullHandle = ImageHandle();
#else
const ImageHandle ImageData::NullHandle = 0;
#endif

ImageHandle ImageData::HandleFromInt(int32_t i) {
#if defined(OS_WIN)
    return reinterpret_cast<ImageHandle>(i);
#elif defined(OS_MACOSX)
    return ImageHandle(i, false);
#else
    return static_cast<ImageHandle>(i);
#endif
}

}  // namespace proxy
}  // namespace pp