| 1 | /* |
| 2 | * Copyright (C) 2013 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 | // Provides the implementation of the GraphicBuffer interface in |
| 18 | // renderer compostior |
| 19 | |
| 20 | #include "graphic_buffer_impl.h" |
| 21 | |
| 22 | #include <utils/Errors.h> |
| 23 | |
| 24 | namespace android { |
| 25 | |
| 26 | GraphicBufferImpl::GraphicBufferImpl(uint32_t w, uint32_t h) |
| 27 | : mBuffer(new android::GraphicBuffer(w, h, PIXEL_FORMAT_RGBA_8888, |
| 28 | android::GraphicBuffer::USAGE_HW_TEXTURE | |
| 29 | android::GraphicBuffer::USAGE_SW_READ_OFTEN | |
| 30 | android::GraphicBuffer::USAGE_SW_WRITE_OFTEN)) { |
| 31 | } |
| 32 | |
| 33 | GraphicBufferImpl::~GraphicBufferImpl() { |
| 34 | } |
| 35 | |
| 36 | // static |
| 37 | long GraphicBufferImpl::Create(int w, int h) { |
| 38 | GraphicBufferImpl* buffer = new GraphicBufferImpl( |
| 39 | static_cast<uint32_t>(w), static_cast<uint32_t>(h)); |
| 40 | if (buffer->InitCheck() != NO_ERROR) { |
| 41 | delete buffer; |
| 42 | return 0; |
| 43 | } |
| 44 | return reinterpret_cast<intptr_t>(buffer); |
| 45 | } |
| 46 | |
| 47 | // static |
| 48 | void GraphicBufferImpl::Release(long buffer_id) { |
| 49 | GraphicBufferImpl* buffer = reinterpret_cast<GraphicBufferImpl*>(buffer_id); |
| 50 | delete buffer; |
| 51 | } |
| 52 | |
| 53 | // static |
| 54 | int GraphicBufferImpl::MapStatic(long buffer_id, AwMapMode mode, void** vaddr) { |
| 55 | GraphicBufferImpl* buffer = reinterpret_cast<GraphicBufferImpl*>(buffer_id); |
| 56 | return buffer->Map(mode, vaddr); |
| 57 | } |
| 58 | |
| 59 | // static |
| 60 | int GraphicBufferImpl::UnmapStatic(long buffer_id) { |
| 61 | GraphicBufferImpl* buffer = reinterpret_cast<GraphicBufferImpl*>(buffer_id); |
| 62 | return buffer->Unmap(); |
| 63 | } |
| 64 | |
| 65 | // static |
| 66 | void* GraphicBufferImpl::GetNativeBufferStatic(long buffer_id) { |
| 67 | GraphicBufferImpl* buffer = reinterpret_cast<GraphicBufferImpl*>(buffer_id); |
| 68 | return buffer->GetNativeBuffer(); |
| 69 | } |
| 70 | |
| 71 | // static |
| 72 | uint32_t GraphicBufferImpl::GetStrideStatic(long buffer_id) { |
| 73 | GraphicBufferImpl* buffer = reinterpret_cast<GraphicBufferImpl*>(buffer_id); |
| 74 | return buffer->GetStride(); |
| 75 | } |
| 76 | |
| 77 | status_t GraphicBufferImpl::Map(AwMapMode mode, void** vaddr) { |
| 78 | int usage = 0; |
| 79 | switch (mode) { |
| 80 | case MAP_READ_ONLY: |
| 81 | usage = android::GraphicBuffer::USAGE_SW_READ_OFTEN; |
| 82 | break; |
| 83 | case MAP_WRITE_ONLY: |
| 84 | usage = android::GraphicBuffer::USAGE_SW_WRITE_OFTEN; |
| 85 | break; |
| 86 | case MAP_READ_WRITE: |
| 87 | usage = android::GraphicBuffer::USAGE_SW_READ_OFTEN | |
| 88 | android::GraphicBuffer::USAGE_SW_WRITE_OFTEN; |
| 89 | break; |
| 90 | default: |
| 91 | return INVALID_OPERATION; |
| 92 | } |
| 93 | return mBuffer->lock(usage, vaddr); |
| 94 | } |
| 95 | |
| 96 | status_t GraphicBufferImpl::Unmap() { |
| 97 | return mBuffer->unlock(); |
| 98 | } |
| 99 | |
| 100 | status_t GraphicBufferImpl::InitCheck() const { |
| 101 | return mBuffer->initCheck(); |
| 102 | } |
| 103 | |
| 104 | void* GraphicBufferImpl::GetNativeBuffer() const { |
| 105 | return mBuffer->getNativeBuffer(); |
| 106 | } |
| 107 | |
| 108 | uint32_t GraphicBufferImpl::GetStride() const { |
| 109 | static const int kBytesPerPixel = 4; |
| 110 | return mBuffer->getStride() * kBytesPerPixel; |
| 111 | } |
| 112 | |
| 113 | } // namespace android |