summaryrefslogtreecommitdiff
path: root/sink/surfaceflingersink/surfaceflinger_wrap.cpp
blob: e2e98ed569d5a68ff5da3bc72e29d20cb1ec77de (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
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/* GStreamer
 * Copyright (C) <2009> Prajnashi S <prajnashi@gmail.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */
//#define ENABLE_GST_PLAYER_LOG
#include <surfaceflinger/ISurface.h>
#include <surfaceflinger/Surface.h>
#include <surfaceflinger/ISurfaceComposer.h>
#include <surfaceflinger/SurfaceComposerClient.h>
#include <binder/MemoryBase.h>
#include <binder/MemoryHeapBase.h>
#include <cutils/log.h>
#include "surfaceflinger_wrap.h"
#include <gst/gst.h>

using namespace android;

typedef struct
{
  sp < MemoryHeapBase > frame_heap;
  sp < ISurface > isurface;
  sp < SurfaceControl > surface;
  int32_t hor_stride;
  int32_t ver_stride;
  uint32_t width;
  uint32_t height;
  PixelFormat format;
  int frame_offset[2];
  int buf_index;
} VideoFlingerDevice;

/* max frame buffers */
#define  MAX_FRAME_BUFFERS     2

static int videoflinger_device_create_new_surface (VideoFlingerDevice *
    videodev);

/* 
 * The only purpose of class "MediaPlayer" is to call Surface::getISurface()
 * in frameworks/base/include/ui/Surface.h, which is private function and accessed
 * by friend class MediaPlayer.
 *
 * We define a fake one to cheat compiler
 */
namespace android
{
  class MediaPlayer
  {
  public:
    static sp < ISurface > getSurface (const Surface * surface)
    {
      return surface->getISurface ();
    };
  };
};


VideoFlingerDeviceHandle
videoflinger_device_create (void *isurface)
{
  VideoFlingerDevice *videodev = NULL;

  GST_INFO ("Enter\n");
  videodev = new VideoFlingerDevice;
  if (videodev == NULL) {
    return NULL;
  }
  videodev->frame_heap.clear ();
  videodev->isurface = (ISurface *) isurface;
  videodev->surface.clear ();
  videodev->format = -1;
  videodev->width = 0;
  videodev->height = 0;
  videodev->hor_stride = 0;
  videodev->ver_stride = 0;
  videodev->buf_index = 0;
  for (int i = 0; i < MAX_FRAME_BUFFERS; i++) {
    videodev->frame_offset[i] = 0;
  }

  GST_INFO ("Leave\n");
  return (VideoFlingerDeviceHandle) videodev;
}



int
videoflinger_device_create_new_surface (VideoFlingerDevice * videodev)
{
  status_t state;
  int pid = getpid ();

  GST_INFO ("Enter\n");

  /* Create a new Surface object with 320x240
   * TODO: Create surface according to device's screen size and rotate it
   * 90, but not a pre-defined value.
   */
  sp < SurfaceComposerClient > videoClient = new SurfaceComposerClient;
  if (videoClient.get () == NULL) {
    GST_ERROR ("Fail to create SurfaceComposerClient\n");
    return -1;
  }

  /* release privious surface */
  videodev->surface.clear ();
  videodev->isurface.clear ();

  int width = (videodev->width) > 320 ? 320 : videodev->width;
  int height = (videodev->height) > 320 ? 320 : videodev->height;

  videodev->surface = videoClient->createSurface (pid,
      0,
      width,
      height,
      PIXEL_FORMAT_RGB_565,
      ISurfaceComposer::eFXSurfaceNormal | ISurfaceComposer::ePushBuffers);
  if (videodev->surface.get () == NULL) {
    GST_ERROR ("Fail to create Surface\n");
    return -1;
  }

  videoClient->openTransaction ();

  /* set Surface toppest z-order, this will bypass all isurface created 
   * in java side and make sure this surface displaied in toppest */
  state = videodev->surface->setLayer (INT_MAX);
  if (state != NO_ERROR) {
    GST_INFO ("videoSurface->setLayer(), state = %d", state);
    videodev->surface.clear ();
    return -1;
  }

  /* show surface */
  state = videodev->surface->show ();
  /*state =  videodev->surface->setLayer(INT_MAX);
     if (state != NO_ERROR)
     {
     GST_INFO("videoSurface->show(), state = %d", state);
     videodev->surface.clear();
     return -1;
     } */

  /* get ISurface interface */
  videodev->isurface =
      MediaPlayer::getSurface (videodev->surface->getSurface ().get ());

  videoClient->closeTransaction ();

  /* Smart pointer videoClient shall be deleted automatically
   * when function exists
   */
  GST_INFO ("Leave\n");
  return 0;
}

int
videoflinger_device_release (VideoFlingerDeviceHandle handle)
{
  GST_INFO ("Enter");

  if (handle == NULL) {
    return -1;
  }

  /* unregister frame buffer */
  videoflinger_device_unregister_framebuffers (handle);

  /* release ISurface & Surface */
  VideoFlingerDevice *videodev = (VideoFlingerDevice *) handle;
  videodev->isurface.clear ();
  videodev->surface.clear ();

  /* delete device */
  delete videodev;

  GST_INFO ("Leave");
  return 0;
}

int
videoflinger_device_register_framebuffers (VideoFlingerDeviceHandle handle,
    int w, int h, VIDEO_FLINGER_PIXEL_FORMAT format)
{
  int surface_format = 0;

  GST_INFO ("Enter");
  if (handle == NULL) {
    GST_ERROR ("videodev is NULL");
    return -1;
  }

  /* TODO: Now, only PIXEL_FORMAT_RGB_565 is supported. Change here to support
   * more pixel type
   */
  if (format != VIDEO_FLINGER_RGB_565) {
    GST_ERROR ("Unsupport format: %d", format);
    return -1;
  }
  surface_format = PIXEL_FORMAT_RGB_565;

  VideoFlingerDevice *videodev = (VideoFlingerDevice *) handle;
  /* unregister previous buffers */
  if (videodev->frame_heap.get ()) {
    videoflinger_device_unregister_framebuffers (handle);
  }

  /* reset framebuffers */
  videodev->format = surface_format;
  videodev->width = (w + 1) & -2;
  videodev->height = (h + 1) & -2;
  videodev->hor_stride = videodev->width;
  videodev->ver_stride = videodev->height;

  /* create isurface internally, if no ISurface interface input */
  if (videodev->isurface.get () == NULL) {
    videoflinger_device_create_new_surface (videodev);
  }

  /* use double buffer in post */
  int frameSize = videodev->width * videodev->height * 2;
  GST_INFO
      ("format=%d, width=%d, height=%d, hor_stride=%d, ver_stride=%d, frameSize=%d",
      videodev->format, videodev->width, videodev->height, videodev->hor_stride,
      videodev->ver_stride, frameSize);

  /* create frame buffer heap base */
  videodev->frame_heap = new MemoryHeapBase (frameSize * MAX_FRAME_BUFFERS);
  if (videodev->frame_heap->heapID () < 0) {
    GST_ERROR ("Error creating frame buffer heap!");
    return -1;
  }

  /* create frame buffer heap and register with surfaceflinger */
  ISurface::BufferHeap buffers (videodev->width,
      videodev->height,
      videodev->hor_stride,
      videodev->ver_stride, videodev->format, videodev->frame_heap);

  if (videodev->isurface->registerBuffers (buffers) < 0) {
    GST_ERROR ("Cannot register frame buffer!");
    videodev->frame_heap.clear ();
    return -1;
  }

  for (int i = 0; i < MAX_FRAME_BUFFERS; i++) {
    videodev->frame_offset[i] = i * frameSize;
  }
  videodev->buf_index = 0;
  GST_INFO ("Leave");

  return 0;
}

void
videoflinger_device_unregister_framebuffers (VideoFlingerDeviceHandle handle)
{
  GST_INFO ("Enter");

  if (handle == NULL) {
    return;
  }

  VideoFlingerDevice *videodev = (VideoFlingerDevice *) handle;
  if (videodev->frame_heap.get ()) {
    GST_INFO ("Unregister frame buffers.  videodev->isurface = %p",
        videodev->isurface.get ());

    /* release ISurface */
    GST_INFO ("Unregister frame buffer");
    videodev->isurface->unregisterBuffers ();

    /* release MemoryHeapBase */
    GST_INFO ("Clear frame buffers.");
    videodev->frame_heap.clear ();

    /* reset offset */
    for (int i = 0; i < MAX_FRAME_BUFFERS; i++) {
      videodev->frame_offset[i] = 0;
    }

    videodev->format = -1;
    videodev->width = 0;
    videodev->height = 0;
    videodev->hor_stride = 0;
    videodev->ver_stride = 0;
    videodev->buf_index = 0;
  }

  GST_INFO ("Leave");
}

void
videoflinger_device_post (VideoFlingerDeviceHandle handle, void *buf,
    int bufsize)
{
  GST_INFO ("Enter");

  if (handle == NULL) {
    return;
  }

  VideoFlingerDevice *videodev = (VideoFlingerDevice *) handle;

  if (++videodev->buf_index == MAX_FRAME_BUFFERS)
    videodev->buf_index = 0;

  memcpy (static_cast <
      unsigned char *>(videodev->frame_heap->base ()) +
      videodev->frame_offset[videodev->buf_index], buf, bufsize);

  GST_INFO ("Post buffer[%d].\n", videodev->buf_index);
  videodev->isurface->postBuffer (videodev->frame_offset[videodev->buf_index]);

  GST_INFO ("Leave");
}