rsdAllocation.cpp revision b322033c13487a174bb9c26466e9684d1ff4de8d
1/*
2 * Copyright (C) 2011-2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17
18#include "rsdCore.h"
19#include "rsdBcc.h"
20#include "rsdRuntime.h"
21#include "rsdAllocation.h"
22#include "rsdFrameBufferObj.h"
23
24#include "rsAllocation.h"
25
26#include "system/window.h"
27#include "hardware/gralloc.h"
28#include "ui/Rect.h"
29#include "ui/GraphicBufferMapper.h"
30#include "gui/SurfaceTexture.h"
31
32#include <GLES/gl.h>
33#include <GLES2/gl2.h>
34#include <GLES/glext.h>
35
36using namespace android;
37using namespace android::renderscript;
38
39
40
41const static GLenum gFaceOrder[] = {
42    GL_TEXTURE_CUBE_MAP_POSITIVE_X,
43    GL_TEXTURE_CUBE_MAP_NEGATIVE_X,
44    GL_TEXTURE_CUBE_MAP_POSITIVE_Y,
45    GL_TEXTURE_CUBE_MAP_NEGATIVE_Y,
46    GL_TEXTURE_CUBE_MAP_POSITIVE_Z,
47    GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
48};
49
50
51GLenum rsdTypeToGLType(RsDataType t) {
52    switch (t) {
53    case RS_TYPE_UNSIGNED_5_6_5:    return GL_UNSIGNED_SHORT_5_6_5;
54    case RS_TYPE_UNSIGNED_5_5_5_1:  return GL_UNSIGNED_SHORT_5_5_5_1;
55    case RS_TYPE_UNSIGNED_4_4_4_4:  return GL_UNSIGNED_SHORT_4_4_4_4;
56
57    //case RS_TYPE_FLOAT_16:      return GL_HALF_FLOAT;
58    case RS_TYPE_FLOAT_32:      return GL_FLOAT;
59    case RS_TYPE_UNSIGNED_8:    return GL_UNSIGNED_BYTE;
60    case RS_TYPE_UNSIGNED_16:   return GL_UNSIGNED_SHORT;
61    case RS_TYPE_SIGNED_8:      return GL_BYTE;
62    case RS_TYPE_SIGNED_16:     return GL_SHORT;
63    default:    break;
64    }
65    return 0;
66}
67
68GLenum rsdKindToGLFormat(RsDataKind k) {
69    switch (k) {
70    case RS_KIND_PIXEL_L: return GL_LUMINANCE;
71    case RS_KIND_PIXEL_A: return GL_ALPHA;
72    case RS_KIND_PIXEL_LA: return GL_LUMINANCE_ALPHA;
73    case RS_KIND_PIXEL_RGB: return GL_RGB;
74    case RS_KIND_PIXEL_RGBA: return GL_RGBA;
75    case RS_KIND_PIXEL_DEPTH: return GL_DEPTH_COMPONENT16;
76    default: break;
77    }
78    return 0;
79}
80
81
82static void Update2DTexture(const Context *rsc, const Allocation *alloc, const void *ptr,
83                            uint32_t xoff, uint32_t yoff, uint32_t lod,
84                            RsAllocationCubemapFace face, uint32_t w, uint32_t h) {
85    DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
86
87    rsAssert(drv->textureID);
88    RSD_CALL_GL(glBindTexture, drv->glTarget, drv->textureID);
89    RSD_CALL_GL(glPixelStorei, GL_UNPACK_ALIGNMENT, 1);
90    GLenum t = GL_TEXTURE_2D;
91    if (alloc->mHal.state.hasFaces) {
92        t = gFaceOrder[face];
93    }
94    RSD_CALL_GL(glTexSubImage2D, t, lod, xoff, yoff, w, h, drv->glFormat, drv->glType, ptr);
95}
96
97
98static void Upload2DTexture(const Context *rsc, const Allocation *alloc, bool isFirstUpload) {
99    DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
100
101    RSD_CALL_GL(glBindTexture, drv->glTarget, drv->textureID);
102    RSD_CALL_GL(glPixelStorei, GL_UNPACK_ALIGNMENT, 1);
103
104    uint32_t faceCount = 1;
105    if (alloc->mHal.state.hasFaces) {
106        faceCount = 6;
107    }
108
109    rsdGLCheckError(rsc, "Upload2DTexture 1 ");
110    for (uint32_t face = 0; face < faceCount; face ++) {
111        for (uint32_t lod = 0; lod < alloc->mHal.state.type->getLODCount(); lod++) {
112            const uint8_t *p = (const uint8_t *)drv->mallocPtr;
113            p += alloc->mHal.state.type->getLODFaceOffset(lod, (RsAllocationCubemapFace)face, 0, 0);
114
115            GLenum t = GL_TEXTURE_2D;
116            if (alloc->mHal.state.hasFaces) {
117                t = gFaceOrder[face];
118            }
119
120            if (isFirstUpload) {
121                RSD_CALL_GL(glTexImage2D, t, lod, drv->glFormat,
122                             alloc->mHal.state.type->getLODDimX(lod),
123                             alloc->mHal.state.type->getLODDimY(lod),
124                             0, drv->glFormat, drv->glType, p);
125            } else {
126                RSD_CALL_GL(glTexSubImage2D, t, lod, 0, 0,
127                                alloc->mHal.state.type->getLODDimX(lod),
128                                alloc->mHal.state.type->getLODDimY(lod),
129                                drv->glFormat, drv->glType, p);
130            }
131        }
132    }
133
134    if (alloc->mHal.state.mipmapControl == RS_ALLOCATION_MIPMAP_ON_SYNC_TO_TEXTURE) {
135        RSD_CALL_GL(glGenerateMipmap, drv->glTarget);
136    }
137    rsdGLCheckError(rsc, "Upload2DTexture");
138}
139
140static void UploadToTexture(const Context *rsc, const Allocation *alloc) {
141    DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
142
143    if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_IO_INPUT) {
144        if (!drv->textureID) {
145            RSD_CALL_GL(glGenTextures, 1, &drv->textureID);
146        }
147        return;
148    }
149
150    if (!drv->glType || !drv->glFormat) {
151        return;
152    }
153
154    if (!alloc->getPtr()) {
155        return;
156    }
157
158    bool isFirstUpload = false;
159
160    if (!drv->textureID) {
161        RSD_CALL_GL(glGenTextures, 1, &drv->textureID);
162        isFirstUpload = true;
163    }
164
165    Upload2DTexture(rsc, alloc, isFirstUpload);
166
167    if (!(alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_SCRIPT)) {
168        if (drv->mallocPtr) {
169            free(drv->mallocPtr);
170            drv->mallocPtr = NULL;
171        }
172    }
173    rsdGLCheckError(rsc, "UploadToTexture");
174}
175
176static void AllocateRenderTarget(const Context *rsc, const Allocation *alloc) {
177    DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
178
179    if (!drv->glFormat) {
180        return;
181    }
182
183    if (!drv->renderTargetID) {
184        RSD_CALL_GL(glGenRenderbuffers, 1, &drv->renderTargetID);
185
186        if (!drv->renderTargetID) {
187            // This should generally not happen
188            ALOGE("allocateRenderTarget failed to gen mRenderTargetID");
189            rsc->dumpDebug();
190            return;
191        }
192        RSD_CALL_GL(glBindRenderbuffer, GL_RENDERBUFFER, drv->renderTargetID);
193        RSD_CALL_GL(glRenderbufferStorage, GL_RENDERBUFFER, drv->glFormat,
194                              alloc->mHal.state.dimensionX, alloc->mHal.state.dimensionY);
195    }
196    rsdGLCheckError(rsc, "AllocateRenderTarget");
197}
198
199static void UploadToBufferObject(const Context *rsc, const Allocation *alloc) {
200    DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
201
202    rsAssert(!alloc->mHal.state.type->getDimY());
203    rsAssert(!alloc->mHal.state.type->getDimZ());
204
205    //alloc->mHal.state.usageFlags |= RS_ALLOCATION_USAGE_GRAPHICS_VERTEX;
206
207    if (!drv->bufferID) {
208        RSD_CALL_GL(glGenBuffers, 1, &drv->bufferID);
209    }
210    if (!drv->bufferID) {
211        ALOGE("Upload to buffer object failed");
212        drv->uploadDeferred = true;
213        return;
214    }
215    RSD_CALL_GL(glBindBuffer, drv->glTarget, drv->bufferID);
216    RSD_CALL_GL(glBufferData, drv->glTarget, alloc->mHal.state.type->getSizeBytes(),
217                 drv->mallocPtr, GL_DYNAMIC_DRAW);
218    RSD_CALL_GL(glBindBuffer, drv->glTarget, 0);
219    rsdGLCheckError(rsc, "UploadToBufferObject");
220}
221
222bool rsdAllocationInit(const Context *rsc, Allocation *alloc, bool forceZero) {
223    DrvAllocation *drv = (DrvAllocation *)calloc(1, sizeof(DrvAllocation));
224    if (!drv) {
225        return false;
226    }
227
228    void * ptr = alloc->mHal.state.usrPtr;
229    if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_IO_OUTPUT) {
230    } else {
231        ptr = malloc(alloc->mHal.state.type->getSizeBytes());
232        if (!ptr) {
233            free(drv);
234            return false;
235        }
236    }
237
238    drv->glTarget = GL_NONE;
239    if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_TEXTURE) {
240        if (alloc->mHal.state.hasFaces) {
241            drv->glTarget = GL_TEXTURE_CUBE_MAP;
242        } else {
243            drv->glTarget = GL_TEXTURE_2D;
244        }
245    } else {
246        if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_VERTEX) {
247            drv->glTarget = GL_ARRAY_BUFFER;
248        }
249    }
250
251    drv->glType = rsdTypeToGLType(alloc->mHal.state.type->getElement()->getComponent().getType());
252    drv->glFormat = rsdKindToGLFormat(alloc->mHal.state.type->getElement()->getComponent().getKind());
253
254
255    alloc->mHal.drvState.mallocPtr = ptr;
256    drv->mallocPtr = (uint8_t *)ptr;
257    alloc->mHal.drv = drv;
258    if (forceZero && ptr) {
259        memset(ptr, 0, alloc->mHal.state.type->getSizeBytes());
260    }
261
262    if (alloc->mHal.state.usageFlags & ~RS_ALLOCATION_USAGE_SCRIPT) {
263        drv->uploadDeferred = true;
264    }
265
266    drv->width = alloc->getType()->getDimX();
267    drv->height = alloc->getType()->getDimY();
268
269    drv->readBackFBO = NULL;
270
271    return true;
272}
273
274void rsdAllocationDestroy(const Context *rsc, Allocation *alloc) {
275    DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
276
277    if (drv->bufferID) {
278        // Causes a SW crash....
279        //ALOGV(" mBufferID %i", mBufferID);
280        //glDeleteBuffers(1, &mBufferID);
281        //mBufferID = 0;
282    }
283    if (drv->textureID) {
284        RSD_CALL_GL(glDeleteTextures, 1, &drv->textureID);
285        drv->textureID = 0;
286    }
287    if (drv->renderTargetID) {
288        RSD_CALL_GL(glDeleteRenderbuffers, 1, &drv->renderTargetID);
289        drv->renderTargetID = 0;
290    }
291
292    if (drv->mallocPtr && !alloc->mHal.state.usrPtr) {
293        free(drv->mallocPtr);
294        drv->mallocPtr = NULL;
295    }
296    if (drv->readBackFBO != NULL) {
297        delete drv->readBackFBO;
298        drv->readBackFBO = NULL;
299    }
300    free(drv);
301    alloc->mHal.drv = NULL;
302}
303
304void rsdAllocationResize(const Context *rsc, const Allocation *alloc,
305                         const Type *newType, bool zeroNew) {
306    DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
307
308    drv->mallocPtr = (uint8_t *)realloc(drv->mallocPtr, newType->getSizeBytes());
309
310    // fixme
311    ((Allocation *)alloc)->mHal.drvState.mallocPtr = drv->mallocPtr;
312
313    const uint32_t oldDimX = alloc->mHal.state.dimensionX;
314    const uint32_t dimX = newType->getDimX();
315
316    if (dimX > oldDimX) {
317        const Element *e = alloc->mHal.state.type->getElement();
318        uint32_t stride = e->getSizeBytes();
319        memset(((uint8_t *)drv->mallocPtr) + stride * oldDimX, 0, stride * (dimX - oldDimX));
320    }
321}
322
323static void rsdAllocationSyncFromFBO(const Context *rsc, const Allocation *alloc) {
324    if (!alloc->getIsScript()) {
325        return; // nothing to sync
326    }
327
328    RsdHal *dc = (RsdHal *)rsc->mHal.drv;
329    RsdFrameBufferObj *lastFbo = dc->gl.currentFrameBuffer;
330
331    DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
332    if (!drv->textureID && !drv->renderTargetID) {
333        return; // nothing was rendered here yet, so nothing to sync
334    }
335    if (drv->readBackFBO == NULL) {
336        drv->readBackFBO = new RsdFrameBufferObj();
337        drv->readBackFBO->setColorTarget(drv, 0);
338        drv->readBackFBO->setDimensions(alloc->getType()->getDimX(),
339                                        alloc->getType()->getDimY());
340    }
341
342    // Bind the framebuffer object so we can read back from it
343    drv->readBackFBO->setActive(rsc);
344
345    // Do the readback
346    RSD_CALL_GL(glReadPixels, 0, 0, alloc->getType()->getDimX(), alloc->getType()->getDimY(),
347                 drv->glFormat, drv->glType, alloc->getPtr());
348
349    // Revert framebuffer to its original
350    lastFbo->setActive(rsc);
351}
352
353
354void rsdAllocationSyncAll(const Context *rsc, const Allocation *alloc,
355                         RsAllocationUsageType src) {
356    DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
357
358    if (src == RS_ALLOCATION_USAGE_GRAPHICS_RENDER_TARGET) {
359        if(!alloc->getIsRenderTarget()) {
360            rsc->setError(RS_ERROR_FATAL_DRIVER,
361                          "Attempting to sync allocation from render target, "
362                          "for non-render target allocation");
363        } else if (alloc->getType()->getElement()->getKind() != RS_KIND_PIXEL_RGBA) {
364            rsc->setError(RS_ERROR_FATAL_DRIVER, "Cannot only sync from RGBA"
365                                                 "render target");
366        } else {
367            rsdAllocationSyncFromFBO(rsc, alloc);
368        }
369        return;
370    }
371
372    rsAssert(src == RS_ALLOCATION_USAGE_SCRIPT);
373
374    if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_TEXTURE) {
375        UploadToTexture(rsc, alloc);
376    } else {
377        if ((alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_RENDER_TARGET) &&
378                ~(alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_IO_OUTPUT)) {
379            AllocateRenderTarget(rsc, alloc);
380        }
381    }
382    if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_VERTEX) {
383        UploadToBufferObject(rsc, alloc);
384    }
385
386    drv->uploadDeferred = false;
387}
388
389void rsdAllocationMarkDirty(const Context *rsc, const Allocation *alloc) {
390    DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
391    drv->uploadDeferred = true;
392}
393
394int32_t rsdAllocationInitSurfaceTexture(const Context *rsc, const Allocation *alloc) {
395    DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
396    UploadToTexture(rsc, alloc);
397    return drv->textureID;
398}
399
400static bool IoGetBuffer(const Context *rsc, Allocation *alloc, ANativeWindow *nw) {
401    DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
402
403    int32_t r = nw->dequeueBuffer(nw, &drv->wndBuffer);
404    if (r) {
405        rsc->setError(RS_ERROR_DRIVER, "Error getting next IO output buffer.");
406        return false;
407    }
408
409    // This lock is implicitly released by the queue buffer in IoSend
410    r = nw->lockBuffer(nw, drv->wndBuffer);
411    if (r) {
412        rsc->setError(RS_ERROR_DRIVER, "Error locking next IO output buffer.");
413        return false;
414    }
415
416    // Must lock the whole surface
417    GraphicBufferMapper &mapper = GraphicBufferMapper::get();
418    Rect bounds(drv->wndBuffer->width, drv->wndBuffer->height);
419
420    void *dst = NULL;
421    mapper.lock(drv->wndBuffer->handle,
422            GRALLOC_USAGE_SW_READ_NEVER | GRALLOC_USAGE_SW_WRITE_OFTEN,
423            bounds, &dst);
424    alloc->mHal.drvState.mallocPtr = dst;
425
426    return true;
427}
428
429void rsdAllocationSetSurfaceTexture(const Context *rsc, Allocation *alloc, ANativeWindow *nw) {
430    DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
431
432    //ALOGE("rsdAllocationSetSurfaceTexture %p  %p", alloc, nw);
433
434    if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_RENDER_TARGET) {
435        //TODO finish support for render target + script
436        drv->wnd = nw;
437        return;
438    }
439
440
441    // Cleanup old surface if there is one.
442    if (alloc->mHal.state.wndSurface) {
443        ANativeWindow *old = alloc->mHal.state.wndSurface;
444        GraphicBufferMapper &mapper = GraphicBufferMapper::get();
445        mapper.unlock(drv->wndBuffer->handle);
446        old->queueBuffer(old, drv->wndBuffer);
447    }
448
449    if (nw != NULL) {
450        int32_t r;
451        uint32_t flags = 0;
452        if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_SCRIPT) {
453            flags |= GRALLOC_USAGE_SW_READ_RARELY | GRALLOC_USAGE_SW_WRITE_OFTEN;
454        }
455        if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_RENDER_TARGET) {
456            flags |= GRALLOC_USAGE_HW_RENDER;
457        }
458
459        r = native_window_set_usage(nw, flags);
460        if (r) {
461            rsc->setError(RS_ERROR_DRIVER, "Error setting IO output buffer usage.");
462            return;
463        }
464
465        r = native_window_set_buffers_dimensions(nw, alloc->mHal.state.dimensionX,
466                                                 alloc->mHal.state.dimensionY);
467        if (r) {
468            rsc->setError(RS_ERROR_DRIVER, "Error setting IO output buffer dimensions.");
469            return;
470        }
471
472        r = native_window_set_buffer_count(nw, 3);
473        if (r) {
474            rsc->setError(RS_ERROR_DRIVER, "Error setting IO output buffer count.");
475            return;
476        }
477
478        IoGetBuffer(rsc, alloc, nw);
479    }
480}
481
482void rsdAllocationIoSend(const Context *rsc, Allocation *alloc) {
483    DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
484    ANativeWindow *nw = alloc->mHal.state.wndSurface;
485
486    if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_RENDER_TARGET) {
487        RsdHal *dc = (RsdHal *)rsc->mHal.drv;
488        RSD_CALL_GL(eglSwapBuffers, dc->gl.egl.display, dc->gl.egl.surface);
489        return;
490    }
491
492    if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_SCRIPT) {
493        GraphicBufferMapper &mapper = GraphicBufferMapper::get();
494        mapper.unlock(drv->wndBuffer->handle);
495        int32_t r = nw->queueBuffer(nw, drv->wndBuffer);
496        if (r) {
497            rsc->setError(RS_ERROR_DRIVER, "Error sending IO output buffer.");
498            return;
499        }
500
501        IoGetBuffer(rsc, alloc, nw);
502    }
503}
504
505void rsdAllocationIoReceive(const Context *rsc, Allocation *alloc) {
506    DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
507    alloc->mHal.state.surfaceTexture->updateTexImage();
508}
509
510
511void rsdAllocationData1D(const Context *rsc, const Allocation *alloc,
512                         uint32_t xoff, uint32_t lod, uint32_t count,
513                         const void *data, size_t sizeBytes) {
514    DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
515
516    const uint32_t eSize = alloc->mHal.state.type->getElementSizeBytes();
517    uint8_t * ptr = drv->mallocPtr;
518    ptr += eSize * xoff;
519    uint32_t size = count * eSize;
520
521    if (alloc->mHal.state.hasReferences) {
522        alloc->incRefs(data, count);
523        alloc->decRefs(ptr, count);
524    }
525
526    memcpy(ptr, data, size);
527    drv->uploadDeferred = true;
528}
529
530void rsdAllocationData2D(const Context *rsc, const Allocation *alloc,
531                         uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face,
532                         uint32_t w, uint32_t h, const void *data, size_t sizeBytes) {
533    DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
534
535    uint32_t eSize = alloc->mHal.state.elementSizeBytes;
536    uint32_t lineSize = eSize * w;
537    uint32_t destW = alloc->mHal.state.dimensionX;
538
539    if (drv->mallocPtr) {
540        const uint8_t *src = static_cast<const uint8_t *>(data);
541        uint8_t *dst = drv->mallocPtr;
542        dst += alloc->mHal.state.type->getLODFaceOffset(lod, face, xoff, yoff);
543
544        for (uint32_t line=yoff; line < (yoff+h); line++) {
545            if (alloc->mHal.state.hasReferences) {
546                alloc->incRefs(src, w);
547                alloc->decRefs(dst, w);
548            }
549            memcpy(dst, src, lineSize);
550            src += lineSize;
551            dst += destW * eSize;
552        }
553        drv->uploadDeferred = true;
554    } else {
555        Update2DTexture(rsc, alloc, data, xoff, yoff, lod, face, w, h);
556    }
557}
558
559void rsdAllocationData3D(const Context *rsc, const Allocation *alloc,
560                         uint32_t xoff, uint32_t yoff, uint32_t zoff,
561                         uint32_t lod, RsAllocationCubemapFace face,
562                         uint32_t w, uint32_t h, uint32_t d, const void *data, uint32_t sizeBytes) {
563
564}
565
566void rsdAllocationData1D_alloc(const android::renderscript::Context *rsc,
567                               const android::renderscript::Allocation *dstAlloc,
568                               uint32_t dstXoff, uint32_t dstLod, uint32_t count,
569                               const android::renderscript::Allocation *srcAlloc,
570                               uint32_t srcXoff, uint32_t srcLod) {
571}
572
573uint8_t *getOffsetPtr(const android::renderscript::Allocation *alloc,
574                      uint32_t xoff, uint32_t yoff, uint32_t lod,
575                      RsAllocationCubemapFace face) {
576    uint8_t *ptr = static_cast<uint8_t *>(alloc->getPtr());
577    ptr += alloc->getType()->getLODOffset(lod, xoff, yoff);
578
579    if (face != 0) {
580        uint32_t totalSizeBytes = alloc->getType()->getSizeBytes();
581        uint32_t faceOffset = totalSizeBytes / 6;
582        ptr += faceOffset * (uint32_t)face;
583    }
584    return ptr;
585}
586
587
588void rsdAllocationData2D_alloc_script(const android::renderscript::Context *rsc,
589                                      const android::renderscript::Allocation *dstAlloc,
590                                      uint32_t dstXoff, uint32_t dstYoff, uint32_t dstLod,
591                                      RsAllocationCubemapFace dstFace, uint32_t w, uint32_t h,
592                                      const android::renderscript::Allocation *srcAlloc,
593                                      uint32_t srcXoff, uint32_t srcYoff, uint32_t srcLod,
594                                      RsAllocationCubemapFace srcFace) {
595    uint32_t elementSize = dstAlloc->getType()->getElementSizeBytes();
596    for (uint32_t i = 0; i < h; i ++) {
597        uint8_t *dstPtr = getOffsetPtr(dstAlloc, dstXoff, dstYoff + i, dstLod, dstFace);
598        uint8_t *srcPtr = getOffsetPtr(srcAlloc, srcXoff, srcYoff + i, srcLod, srcFace);
599        memcpy(dstPtr, srcPtr, w * elementSize);
600
601        //ALOGE("COPIED dstXoff(%u), dstYoff(%u), dstLod(%u), dstFace(%u), w(%u), h(%u), srcXoff(%u), srcYoff(%u), srcLod(%u), srcFace(%u)",
602        //     dstXoff, dstYoff, dstLod, dstFace, w, h, srcXoff, srcYoff, srcLod, srcFace);
603    }
604}
605
606void rsdAllocationData2D_alloc(const android::renderscript::Context *rsc,
607                               const android::renderscript::Allocation *dstAlloc,
608                               uint32_t dstXoff, uint32_t dstYoff, uint32_t dstLod,
609                               RsAllocationCubemapFace dstFace, uint32_t w, uint32_t h,
610                               const android::renderscript::Allocation *srcAlloc,
611                               uint32_t srcXoff, uint32_t srcYoff, uint32_t srcLod,
612                               RsAllocationCubemapFace srcFace) {
613    if (!dstAlloc->getIsScript() && !srcAlloc->getIsScript()) {
614        rsc->setError(RS_ERROR_FATAL_DRIVER, "Non-script allocation copies not "
615                                             "yet implemented.");
616        return;
617    }
618    rsdAllocationData2D_alloc_script(rsc, dstAlloc, dstXoff, dstYoff,
619                                     dstLod, dstFace, w, h, srcAlloc,
620                                     srcXoff, srcYoff, srcLod, srcFace);
621}
622
623void rsdAllocationData3D_alloc(const android::renderscript::Context *rsc,
624                               const android::renderscript::Allocation *dstAlloc,
625                               uint32_t dstXoff, uint32_t dstYoff, uint32_t dstZoff,
626                               uint32_t dstLod, RsAllocationCubemapFace dstFace,
627                               uint32_t w, uint32_t h, uint32_t d,
628                               const android::renderscript::Allocation *srcAlloc,
629                               uint32_t srcXoff, uint32_t srcYoff, uint32_t srcZoff,
630                               uint32_t srcLod, RsAllocationCubemapFace srcFace) {
631}
632
633void rsdAllocationElementData1D(const Context *rsc, const Allocation *alloc,
634                                uint32_t x,
635                                const void *data, uint32_t cIdx, uint32_t sizeBytes) {
636    DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
637
638    uint32_t eSize = alloc->mHal.state.elementSizeBytes;
639    uint8_t * ptr = drv->mallocPtr;
640    ptr += eSize * x;
641
642    const Element * e = alloc->mHal.state.type->getElement()->getField(cIdx);
643    ptr += alloc->mHal.state.type->getElement()->getFieldOffsetBytes(cIdx);
644
645    if (alloc->mHal.state.hasReferences) {
646        e->incRefs(data);
647        e->decRefs(ptr);
648    }
649
650    memcpy(ptr, data, sizeBytes);
651    drv->uploadDeferred = true;
652}
653
654void rsdAllocationElementData2D(const Context *rsc, const Allocation *alloc,
655                                uint32_t x, uint32_t y,
656                                const void *data, uint32_t cIdx, uint32_t sizeBytes) {
657    DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
658
659    uint32_t eSize = alloc->mHal.state.elementSizeBytes;
660    uint8_t * ptr = drv->mallocPtr;
661    ptr += eSize * (x + y * alloc->mHal.state.dimensionX);
662
663    const Element * e = alloc->mHal.state.type->getElement()->getField(cIdx);
664    ptr += alloc->mHal.state.type->getElement()->getFieldOffsetBytes(cIdx);
665
666    if (alloc->mHal.state.hasReferences) {
667        e->incRefs(data);
668        e->decRefs(ptr);
669    }
670
671    memcpy(ptr, data, sizeBytes);
672    drv->uploadDeferred = true;
673}
674
675
676