blob: 2a96b6914afc2adeadcd06efd3c0d9c37144e99d [file] [log] [blame]
Romain Guycf51a412013-04-08 19:40:31 -07001/*
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
Chris Craik96a5c4c2015-01-27 15:46:35 -080017#include "PixelBuffer.h"
Romain Guycf51a412013-04-08 19:40:31 -070018
Romain Guy9e6f3ac2013-06-20 16:31:35 -070019#include "Debug.h"
Romain Guycf51a412013-04-08 19:40:31 -070020#include "Extensions.h"
Romain Guycf51a412013-04-08 19:40:31 -070021#include "Properties.h"
Chris Craik96a5c4c2015-01-27 15:46:35 -080022#include "renderstate/RenderState.h"
John Reck2de77712016-01-20 11:09:53 -080023#include "utils/GLUtils.h"
Chris Craik96a5c4c2015-01-27 15:46:35 -080024
25#include <utils/Log.h>
Romain Guycf51a412013-04-08 19:40:31 -070026
27namespace android {
28namespace uirenderer {
29
30///////////////////////////////////////////////////////////////////////////////
31// CPU pixel buffer
32///////////////////////////////////////////////////////////////////////////////
33
34class CpuPixelBuffer: public PixelBuffer {
35public:
36 CpuPixelBuffer(GLenum format, uint32_t width, uint32_t height);
Romain Guycf51a412013-04-08 19:40:31 -070037
Chris Craikd41c4d82015-01-05 15:51:13 -080038 uint8_t* map(AccessMode mode = kAccessMode_ReadWrite) override;
Romain Guycf51a412013-04-08 19:40:31 -070039
Chris Craikd41c4d82015-01-05 15:51:13 -080040 void upload(uint32_t x, uint32_t y, uint32_t width, uint32_t height, int offset) override;
Romain Guycf51a412013-04-08 19:40:31 -070041
John Reckf3ad3242016-02-24 15:36:35 -080042protected:
43 void unmap() override;
44
Romain Guycf51a412013-04-08 19:40:31 -070045private:
Chris Craik51d6a3d2014-12-22 17:16:56 -080046 std::unique_ptr<uint8_t[]> mBuffer;
Romain Guycf51a412013-04-08 19:40:31 -070047};
48
Chris Craik51d6a3d2014-12-22 17:16:56 -080049CpuPixelBuffer::CpuPixelBuffer(GLenum format, uint32_t width, uint32_t height)
50 : PixelBuffer(format, width, height)
51 , mBuffer(new uint8_t[width * height * formatSize(format)]) {
Romain Guycf51a412013-04-08 19:40:31 -070052}
53
54uint8_t* CpuPixelBuffer::map(AccessMode mode) {
55 if (mAccessMode == kAccessMode_None) {
56 mAccessMode = mode;
57 }
Chris Craik51d6a3d2014-12-22 17:16:56 -080058 return mBuffer.get();
Romain Guycf51a412013-04-08 19:40:31 -070059}
60
61void CpuPixelBuffer::unmap() {
62 mAccessMode = kAccessMode_None;
63}
64
Romain Guycf51a412013-04-08 19:40:31 -070065void CpuPixelBuffer::upload(uint32_t x, uint32_t y, uint32_t width, uint32_t height, int offset) {
66 glTexSubImage2D(GL_TEXTURE_2D, 0, x, y, width, height,
Chris Craik51d6a3d2014-12-22 17:16:56 -080067 mFormat, GL_UNSIGNED_BYTE, &mBuffer[offset]);
Romain Guycf51a412013-04-08 19:40:31 -070068}
69
70///////////////////////////////////////////////////////////////////////////////
71// GPU pixel buffer
72///////////////////////////////////////////////////////////////////////////////
73
74class GpuPixelBuffer: public PixelBuffer {
75public:
76 GpuPixelBuffer(GLenum format, uint32_t width, uint32_t height);
77 ~GpuPixelBuffer();
78
Chris Craikd41c4d82015-01-05 15:51:13 -080079 uint8_t* map(AccessMode mode = kAccessMode_ReadWrite) override;
Romain Guycf51a412013-04-08 19:40:31 -070080
Chris Craikd41c4d82015-01-05 15:51:13 -080081 void upload(uint32_t x, uint32_t y, uint32_t width, uint32_t height, int offset) override;
Romain Guycf51a412013-04-08 19:40:31 -070082
John Reckf3ad3242016-02-24 15:36:35 -080083protected:
84 void unmap() override;
85
Romain Guycf51a412013-04-08 19:40:31 -070086private:
87 GLuint mBuffer;
88 uint8_t* mMappedPointer;
89 Caches& mCaches;
90};
91
Chris Craik96a5c4c2015-01-27 15:46:35 -080092GpuPixelBuffer::GpuPixelBuffer(GLenum format,
93 uint32_t width, uint32_t height)
Chris Craikd41c4d82015-01-05 15:51:13 -080094 : PixelBuffer(format, width, height)
95 , mMappedPointer(nullptr)
Chris Craik96a5c4c2015-01-27 15:46:35 -080096 , mCaches(Caches::getInstance()){
Romain Guycf51a412013-04-08 19:40:31 -070097 glGenBuffers(1, &mBuffer);
Chris Craik96a5c4c2015-01-27 15:46:35 -080098
Chris Craik44eb2c02015-01-29 09:45:09 -080099 mCaches.pixelBufferState().bind(mBuffer);
Chris Craikd41c4d82015-01-05 15:51:13 -0800100 glBufferData(GL_PIXEL_UNPACK_BUFFER, getSize(), nullptr, GL_DYNAMIC_DRAW);
Chris Craik44eb2c02015-01-29 09:45:09 -0800101 mCaches.pixelBufferState().unbind();
Romain Guycf51a412013-04-08 19:40:31 -0700102}
103
104GpuPixelBuffer::~GpuPixelBuffer() {
105 glDeleteBuffers(1, &mBuffer);
106}
107
108uint8_t* GpuPixelBuffer::map(AccessMode mode) {
109 if (mAccessMode == kAccessMode_None) {
Chris Craik44eb2c02015-01-29 09:45:09 -0800110 mCaches.pixelBufferState().bind(mBuffer);
Romain Guycf51a412013-04-08 19:40:31 -0700111 mMappedPointer = (uint8_t*) glMapBufferRange(GL_PIXEL_UNPACK_BUFFER, 0, getSize(), mode);
John Reck2de77712016-01-20 11:09:53 -0800112 if (CC_UNLIKELY(!mMappedPointer)) {
113 GLUtils::dumpGLErrors();
114 LOG_ALWAYS_FATAL("Failed to map PBO");
Romain Guy9e6f3ac2013-06-20 16:31:35 -0700115 }
Romain Guycf51a412013-04-08 19:40:31 -0700116 mAccessMode = mode;
John Reckf3ad3242016-02-24 15:36:35 -0800117 mCaches.pixelBufferState().unbind();
Romain Guycf51a412013-04-08 19:40:31 -0700118 }
119
120 return mMappedPointer;
121}
122
123void GpuPixelBuffer::unmap() {
124 if (mAccessMode != kAccessMode_None) {
125 if (mMappedPointer) {
Chris Craik44eb2c02015-01-29 09:45:09 -0800126 mCaches.pixelBufferState().bind(mBuffer);
Romain Guy03c00b52013-06-20 18:30:28 -0700127 GLboolean status = glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER);
128 if (status == GL_FALSE) {
129 ALOGE("Corrupted GPU pixel buffer");
130 }
Romain Guycf51a412013-04-08 19:40:31 -0700131 }
132 mAccessMode = kAccessMode_None;
Chris Craikd41c4d82015-01-05 15:51:13 -0800133 mMappedPointer = nullptr;
Romain Guycf51a412013-04-08 19:40:31 -0700134 }
135}
136
Romain Guycf51a412013-04-08 19:40:31 -0700137void GpuPixelBuffer::upload(uint32_t x, uint32_t y, uint32_t width, uint32_t height, int offset) {
138 // If the buffer is not mapped, unmap() will not bind it
Chris Craik44eb2c02015-01-29 09:45:09 -0800139 mCaches.pixelBufferState().bind(mBuffer);
Romain Guycf51a412013-04-08 19:40:31 -0700140 unmap();
141 glTexSubImage2D(GL_TEXTURE_2D, 0, x, y, width, height, mFormat,
Kévin PETIT73fc5582014-02-13 11:03:40 +0000142 GL_UNSIGNED_BYTE, reinterpret_cast<void*>(offset));
John Reckf3ad3242016-02-24 15:36:35 -0800143 mCaches.pixelBufferState().unbind();
Romain Guycf51a412013-04-08 19:40:31 -0700144}
145
146///////////////////////////////////////////////////////////////////////////////
147// Factory
148///////////////////////////////////////////////////////////////////////////////
149
Chris Craik96a5c4c2015-01-27 15:46:35 -0800150PixelBuffer* PixelBuffer::create(GLenum format,
151 uint32_t width, uint32_t height, BufferType type) {
Romain Guyf9f00162013-05-09 11:50:12 -0700152 if (type == kBufferType_Auto && Caches::getInstance().gpuPixelBuffersEnabled) {
153 return new GpuPixelBuffer(format, width, height);
Romain Guycf51a412013-04-08 19:40:31 -0700154 }
155 return new CpuPixelBuffer(format, width, height);
156}
157
158}; // namespace uirenderer
159}; // namespace android